1 | /* |
---|
2 | * Copyright (c) 1982, 1986, 1991, 1993 |
---|
3 | * The Regents of the University of California. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * 4. Neither the name of the University nor the names of its contributors |
---|
14 | * may be used to endorse or promote products derived from this software |
---|
15 | * without specific prior written permission. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | * SUCH DAMAGE. |
---|
28 | * |
---|
29 | * @(#)in.c 8.4 (Berkeley) 1/9/95 |
---|
30 | * $FreeBSD: src/sys/netinet/in.c,v 1.75 2004/04/07 20:46:13 imp Exp $ |
---|
31 | */ |
---|
32 | |
---|
33 | #ifdef HAVE_CONFIG_H |
---|
34 | #include "config.h" |
---|
35 | #endif |
---|
36 | |
---|
37 | #include <sys/param.h> |
---|
38 | #include <sys/queue.h> |
---|
39 | #include <sys/systm.h> |
---|
40 | #include <sys/sockio.h> |
---|
41 | #include <errno.h> |
---|
42 | #include <sys/malloc.h> |
---|
43 | #include <sys/socket.h> |
---|
44 | #include <sys/socketvar.h> |
---|
45 | #include <sys/kernel.h> |
---|
46 | #include <sys/sysctl.h> |
---|
47 | |
---|
48 | #include <net/if.h> |
---|
49 | #include <net/route.h> |
---|
50 | |
---|
51 | #include <netinet/in_systm.h> |
---|
52 | #include <netinet/in.h> |
---|
53 | #include <netinet/in_var.h> |
---|
54 | #include <netinet/if_ether.h> |
---|
55 | |
---|
56 | #include <netinet/igmp_var.h> |
---|
57 | |
---|
58 | /* |
---|
59 | * This structure is used to keep track of in_multi chains which belong to |
---|
60 | * deleted interface addresses. |
---|
61 | */ |
---|
62 | static LIST_HEAD(, multi_kludge) in_mk; /* XXX BSS initialization */ |
---|
63 | |
---|
64 | struct multi_kludge { |
---|
65 | LIST_ENTRY(multi_kludge) mk_entry; |
---|
66 | struct ifnet *mk_ifp; |
---|
67 | struct in_multihead mk_head; |
---|
68 | }; |
---|
69 | |
---|
70 | static void in_socktrim(struct sockaddr_in *); |
---|
71 | static int in_ifinit(struct ifnet *, |
---|
72 | struct in_ifaddr *, struct sockaddr_in *, int); |
---|
73 | static void in_ifscrub(struct ifnet *, struct in_ifaddr *); |
---|
74 | |
---|
75 | static int subnetsarelocal = 0; |
---|
76 | SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW, |
---|
77 | &subnetsarelocal, 0, ""); |
---|
78 | /* |
---|
79 | * Return 1 if an internet address is for a ``local'' host |
---|
80 | * (one to which we have a connection). If subnetsarelocal |
---|
81 | * is true, this includes other subnets of the local net. |
---|
82 | * Otherwise, it includes only the directly-connected (sub)nets. |
---|
83 | */ |
---|
84 | int |
---|
85 | in_localaddr(struct in_addr in) |
---|
86 | { |
---|
87 | register u_long i = ntohl(in.s_addr); |
---|
88 | register struct in_ifaddr *ia; |
---|
89 | |
---|
90 | if (subnetsarelocal) { |
---|
91 | for (ia = in_ifaddr; ia; ia = ia->ia_next) |
---|
92 | if ((i & ia->ia_netmask) == ia->ia_net) |
---|
93 | return (1); |
---|
94 | } else { |
---|
95 | for (ia = in_ifaddr; ia; ia = ia->ia_next) |
---|
96 | if ((i & ia->ia_subnetmask) == ia->ia_subnet) |
---|
97 | return (1); |
---|
98 | } |
---|
99 | return (0); |
---|
100 | } |
---|
101 | |
---|
102 | /* |
---|
103 | * Determine whether an IP address is in a reserved set of addresses |
---|
104 | * that may not be forwarded, or whether datagrams to that destination |
---|
105 | * may be forwarded. |
---|
106 | */ |
---|
107 | int |
---|
108 | in_canforward(struct in_addr in) |
---|
109 | { |
---|
110 | register u_long i = ntohl(in.s_addr); |
---|
111 | register u_long net; |
---|
112 | |
---|
113 | if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) |
---|
114 | return (0); |
---|
115 | if (IN_CLASSA(i)) { |
---|
116 | net = i & IN_CLASSA_NET; |
---|
117 | if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) |
---|
118 | return (0); |
---|
119 | } |
---|
120 | return (1); |
---|
121 | } |
---|
122 | |
---|
123 | /* |
---|
124 | * Trim a mask in a sockaddr |
---|
125 | */ |
---|
126 | static void |
---|
127 | in_socktrim(struct sockaddr_in *ap) |
---|
128 | { |
---|
129 | register char *cplim = (char *) &ap->sin_addr; |
---|
130 | register char *cp = (char *) (&ap->sin_addr + 1); |
---|
131 | |
---|
132 | ap->sin_len = 0; |
---|
133 | while (--cp >= cplim) |
---|
134 | if (*cp) { |
---|
135 | (ap)->sin_len = cp - (char *) (ap) + 1; |
---|
136 | break; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | static int in_interfaces; /* number of external internet interfaces */ |
---|
141 | |
---|
142 | /* |
---|
143 | * Generic internet control operations (ioctl's). |
---|
144 | * Ifp is 0 if not an interface-specific ioctl. |
---|
145 | */ |
---|
146 | int |
---|
147 | in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp) |
---|
148 | { |
---|
149 | register struct ifreq *ifr = (struct ifreq *)data; |
---|
150 | register struct in_ifaddr *ia = 0, *iap; |
---|
151 | register struct ifaddr *ifa; |
---|
152 | struct in_ifaddr *oia; |
---|
153 | struct in_aliasreq *ifra = (struct in_aliasreq *)data; |
---|
154 | struct sockaddr_in oldaddr; |
---|
155 | int error, hostIsNew, maskIsNew, s; |
---|
156 | struct multi_kludge *mk; |
---|
157 | |
---|
158 | /* |
---|
159 | * Find address for this interface, if it exists. |
---|
160 | * |
---|
161 | * If an alias address was specified, find that one instead of |
---|
162 | * the first one on the interface. |
---|
163 | */ |
---|
164 | if (ifp) |
---|
165 | for (iap = in_ifaddr; iap; iap = iap->ia_next) |
---|
166 | if (iap->ia_ifp == ifp) { |
---|
167 | if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr == |
---|
168 | iap->ia_addr.sin_addr.s_addr) { |
---|
169 | ia = iap; |
---|
170 | break; |
---|
171 | } else if (ia == NULL) { |
---|
172 | ia = iap; |
---|
173 | if (ifr->ifr_addr.sa_family != AF_INET) |
---|
174 | break; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | switch (cmd) { |
---|
179 | |
---|
180 | case SIOCAIFADDR: |
---|
181 | case SIOCDIFADDR: |
---|
182 | if (ifra->ifra_addr.sin_family == AF_INET) { |
---|
183 | for (oia = ia; ia; ia = ia->ia_next) { |
---|
184 | if (ia->ia_ifp == ifp && |
---|
185 | ia->ia_addr.sin_addr.s_addr == |
---|
186 | ifra->ifra_addr.sin_addr.s_addr) |
---|
187 | break; |
---|
188 | } |
---|
189 | if ((ifp->if_flags & IFF_POINTOPOINT) |
---|
190 | && (cmd == SIOCAIFADDR) |
---|
191 | && (ifra->ifra_dstaddr.sin_addr.s_addr |
---|
192 | == INADDR_ANY)) { |
---|
193 | return EDESTADDRREQ; |
---|
194 | } |
---|
195 | } |
---|
196 | if (cmd == SIOCDIFADDR && ia == 0) |
---|
197 | return (EADDRNOTAVAIL); |
---|
198 | /* FALLTHROUGH */ |
---|
199 | case SIOCSIFADDR: |
---|
200 | case SIOCSIFNETMASK: |
---|
201 | case SIOCSIFDSTADDR: |
---|
202 | if ((so->so_state & SS_PRIV) == 0) |
---|
203 | return (EPERM); |
---|
204 | |
---|
205 | if (ifp == 0) |
---|
206 | panic("in_control"); |
---|
207 | if (ia == (struct in_ifaddr *)0) { |
---|
208 | oia = (struct in_ifaddr *) |
---|
209 | malloc(sizeof *oia, M_IFADDR, M_WAITOK); |
---|
210 | if (oia == (struct in_ifaddr *)NULL) |
---|
211 | return (ENOBUFS); |
---|
212 | bzero((caddr_t)oia, sizeof *oia); |
---|
213 | ia = in_ifaddr; |
---|
214 | /* |
---|
215 | * Protect from ipintr() traversing address list |
---|
216 | * while we're modifying it. |
---|
217 | */ |
---|
218 | s = splnet(); |
---|
219 | |
---|
220 | if (ia) { |
---|
221 | for ( ; ia->ia_next; ia = ia->ia_next) |
---|
222 | continue; |
---|
223 | ia->ia_next = oia; |
---|
224 | } else |
---|
225 | in_ifaddr = oia; |
---|
226 | ia = oia; |
---|
227 | ifa = ifp->if_addrlist; |
---|
228 | if (ifa) { |
---|
229 | for ( ; ifa->ifa_next; ifa = ifa->ifa_next) |
---|
230 | continue; |
---|
231 | ifa->ifa_next = (struct ifaddr *) ia; |
---|
232 | } else |
---|
233 | ifp->if_addrlist = (struct ifaddr *) ia; |
---|
234 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; |
---|
235 | ia->ia_ifa.ifa_dstaddr |
---|
236 | = (struct sockaddr *)&ia->ia_dstaddr; |
---|
237 | ia->ia_ifa.ifa_netmask |
---|
238 | = (struct sockaddr *)&ia->ia_sockmask; |
---|
239 | ia->ia_sockmask.sin_len = 8; |
---|
240 | if (ifp->if_flags & IFF_BROADCAST) { |
---|
241 | ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); |
---|
242 | ia->ia_broadaddr.sin_family = AF_INET; |
---|
243 | } |
---|
244 | ia->ia_ifp = ifp; |
---|
245 | if (!(ifp->if_flags & IFF_LOOPBACK)) |
---|
246 | in_interfaces++; |
---|
247 | splx(s); |
---|
248 | } |
---|
249 | break; |
---|
250 | |
---|
251 | case SIOCSIFBRDADDR: |
---|
252 | if ((so->so_state & SS_PRIV) == 0) |
---|
253 | return (EPERM); |
---|
254 | /* FALLTHROUGH */ |
---|
255 | |
---|
256 | case SIOCGIFADDR: |
---|
257 | case SIOCGIFNETMASK: |
---|
258 | case SIOCGIFDSTADDR: |
---|
259 | case SIOCGIFBRDADDR: |
---|
260 | if (ia == (struct in_ifaddr *)0) |
---|
261 | return (EADDRNOTAVAIL); |
---|
262 | break; |
---|
263 | } |
---|
264 | switch (cmd) { |
---|
265 | |
---|
266 | case SIOCGIFADDR: |
---|
267 | *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; |
---|
268 | break; |
---|
269 | |
---|
270 | case SIOCGIFBRDADDR: |
---|
271 | if ((ifp->if_flags & IFF_BROADCAST) == 0) |
---|
272 | return (EINVAL); |
---|
273 | *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; |
---|
274 | break; |
---|
275 | |
---|
276 | case SIOCGIFDSTADDR: |
---|
277 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) |
---|
278 | return (EINVAL); |
---|
279 | *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; |
---|
280 | break; |
---|
281 | |
---|
282 | case SIOCGIFNETMASK: |
---|
283 | *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; |
---|
284 | break; |
---|
285 | |
---|
286 | case SIOCSIFDSTADDR: |
---|
287 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) |
---|
288 | return (EINVAL); |
---|
289 | oldaddr = ia->ia_dstaddr; |
---|
290 | ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; |
---|
291 | if (ifp->if_ioctl && (error = (*ifp->if_ioctl) |
---|
292 | (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { |
---|
293 | ia->ia_dstaddr = oldaddr; |
---|
294 | return (error); |
---|
295 | } |
---|
296 | if (ia->ia_flags & IFA_ROUTE) { |
---|
297 | ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; |
---|
298 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); |
---|
299 | ia->ia_ifa.ifa_dstaddr = |
---|
300 | (struct sockaddr *)&ia->ia_dstaddr; |
---|
301 | rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); |
---|
302 | } |
---|
303 | break; |
---|
304 | |
---|
305 | case SIOCSIFBRDADDR: |
---|
306 | if ((ifp->if_flags & IFF_BROADCAST) == 0) |
---|
307 | return (EINVAL); |
---|
308 | ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; |
---|
309 | break; |
---|
310 | |
---|
311 | case SIOCSIFADDR: |
---|
312 | return (in_ifinit(ifp, ia, |
---|
313 | (struct sockaddr_in *) &ifr->ifr_addr, 1)); |
---|
314 | |
---|
315 | case SIOCSIFNETMASK: |
---|
316 | ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; |
---|
317 | ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); |
---|
318 | break; |
---|
319 | |
---|
320 | case SIOCAIFADDR: |
---|
321 | maskIsNew = 0; |
---|
322 | hostIsNew = 1; |
---|
323 | error = 0; |
---|
324 | if (ia->ia_addr.sin_family == AF_INET) { |
---|
325 | if (ifra->ifra_addr.sin_len == 0) { |
---|
326 | ifra->ifra_addr = ia->ia_addr; |
---|
327 | hostIsNew = 0; |
---|
328 | } else if (ifra->ifra_addr.sin_addr.s_addr == |
---|
329 | ia->ia_addr.sin_addr.s_addr) |
---|
330 | hostIsNew = 0; |
---|
331 | } |
---|
332 | if (ifra->ifra_mask.sin_len) { |
---|
333 | in_ifscrub(ifp, ia); |
---|
334 | ia->ia_sockmask = ifra->ifra_mask; |
---|
335 | ia->ia_subnetmask = |
---|
336 | ntohl(ia->ia_sockmask.sin_addr.s_addr); |
---|
337 | maskIsNew = 1; |
---|
338 | } |
---|
339 | if ((ifp->if_flags & IFF_POINTOPOINT) && |
---|
340 | (ifra->ifra_dstaddr.sin_family == AF_INET)) { |
---|
341 | in_ifscrub(ifp, ia); |
---|
342 | ia->ia_dstaddr = ifra->ifra_dstaddr; |
---|
343 | maskIsNew = 1; /* We lie; but the effect's the same */ |
---|
344 | } |
---|
345 | if (ifra->ifra_addr.sin_family == AF_INET && |
---|
346 | (hostIsNew || maskIsNew)) |
---|
347 | error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); |
---|
348 | if ((ifp->if_flags & IFF_BROADCAST) && |
---|
349 | (ifra->ifra_broadaddr.sin_family == AF_INET)) |
---|
350 | ia->ia_broadaddr = ifra->ifra_broadaddr; |
---|
351 | return (error); |
---|
352 | |
---|
353 | case SIOCDIFADDR: |
---|
354 | mk = malloc(sizeof *mk, M_IPMADDR, M_WAITOK); |
---|
355 | if (!mk) |
---|
356 | return ENOBUFS; |
---|
357 | |
---|
358 | in_ifscrub(ifp, ia); |
---|
359 | /* |
---|
360 | * Protect from ipintr() traversing address list |
---|
361 | * while we're modifying it. |
---|
362 | */ |
---|
363 | s = splnet(); |
---|
364 | |
---|
365 | if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia) |
---|
366 | ifp->if_addrlist = ifa->ifa_next; |
---|
367 | else { |
---|
368 | while (ifa->ifa_next && |
---|
369 | (ifa->ifa_next != (struct ifaddr *)ia)) |
---|
370 | ifa = ifa->ifa_next; |
---|
371 | if (ifa->ifa_next) |
---|
372 | ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next; |
---|
373 | else |
---|
374 | printf("Couldn't unlink inifaddr from ifp\n"); |
---|
375 | } |
---|
376 | oia = ia; |
---|
377 | if (oia == (ia = in_ifaddr)) |
---|
378 | in_ifaddr = ia->ia_next; |
---|
379 | else { |
---|
380 | while (ia->ia_next && (ia->ia_next != oia)) |
---|
381 | ia = ia->ia_next; |
---|
382 | if (ia->ia_next) |
---|
383 | ia->ia_next = oia->ia_next; |
---|
384 | else |
---|
385 | printf("Didn't unlink inifadr from list\n"); |
---|
386 | } |
---|
387 | |
---|
388 | if (!oia->ia_multiaddrs.lh_first) { |
---|
389 | IFAFREE(&oia->ia_ifa); |
---|
390 | FREE(mk, M_IPMADDR); |
---|
391 | splx(s); |
---|
392 | break; |
---|
393 | } |
---|
394 | |
---|
395 | /* |
---|
396 | * Multicast address kludge: |
---|
397 | * If there were any multicast addresses attached to this |
---|
398 | * interface address, either move them to another address |
---|
399 | * on this interface, or save them until such time as this |
---|
400 | * interface is reconfigured for IP. |
---|
401 | */ |
---|
402 | IFP_TO_IA(oia->ia_ifp, ia); |
---|
403 | if (ia) { /* there is another address */ |
---|
404 | struct in_multi *inm; |
---|
405 | for(inm = oia->ia_multiaddrs.lh_first; inm; |
---|
406 | inm = inm->inm_entry.le_next) { |
---|
407 | IFAFREE(&inm->inm_ia->ia_ifa); |
---|
408 | ia->ia_ifa.ifa_refcnt++; |
---|
409 | inm->inm_ia = ia; |
---|
410 | LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, |
---|
411 | inm_entry); |
---|
412 | } |
---|
413 | FREE(mk, M_IPMADDR); |
---|
414 | } else { /* last address on this if deleted, save */ |
---|
415 | struct in_multi *inm; |
---|
416 | |
---|
417 | LIST_INIT(&mk->mk_head); |
---|
418 | mk->mk_ifp = ifp; |
---|
419 | |
---|
420 | for(inm = oia->ia_multiaddrs.lh_first; inm; |
---|
421 | inm = inm->inm_entry.le_next) { |
---|
422 | LIST_INSERT_HEAD(&mk->mk_head, inm, inm_entry); |
---|
423 | } |
---|
424 | |
---|
425 | if (mk->mk_head.lh_first) { |
---|
426 | LIST_INSERT_HEAD(&in_mk, mk, mk_entry); |
---|
427 | } else { |
---|
428 | FREE(mk, M_IPMADDR); |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | IFAFREE((&oia->ia_ifa)); |
---|
433 | splx(s); |
---|
434 | break; |
---|
435 | |
---|
436 | default: |
---|
437 | if (ifp == 0 || ifp->if_ioctl == 0) |
---|
438 | return (EOPNOTSUPP); |
---|
439 | return ((*ifp->if_ioctl)(ifp, cmd, data)); |
---|
440 | } |
---|
441 | return (0); |
---|
442 | } |
---|
443 | |
---|
444 | /* |
---|
445 | * Delete any existing route for an interface. |
---|
446 | */ |
---|
447 | static void |
---|
448 | in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) |
---|
449 | { |
---|
450 | |
---|
451 | if ((ia->ia_flags & IFA_ROUTE) == 0) |
---|
452 | return; |
---|
453 | if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) |
---|
454 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); |
---|
455 | else |
---|
456 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); |
---|
457 | ia->ia_flags &= ~IFA_ROUTE; |
---|
458 | } |
---|
459 | |
---|
460 | /* |
---|
461 | * Initialize an interface's internet address |
---|
462 | * and routing table entry. |
---|
463 | */ |
---|
464 | static int |
---|
465 | in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, |
---|
466 | int scrub) |
---|
467 | { |
---|
468 | register u_long i = ntohl(sin->sin_addr.s_addr); |
---|
469 | struct sockaddr_in oldaddr; |
---|
470 | int s = splimp(), flags = RTF_UP, error; |
---|
471 | struct multi_kludge *mk; |
---|
472 | |
---|
473 | oldaddr = ia->ia_addr; |
---|
474 | ia->ia_addr = *sin; |
---|
475 | /* |
---|
476 | * Give the interface a chance to initialize |
---|
477 | * if this is its first address, |
---|
478 | * and to validate the address if necessary. |
---|
479 | */ |
---|
480 | if (ifp->if_ioctl && |
---|
481 | (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { |
---|
482 | splx(s); |
---|
483 | ia->ia_addr = oldaddr; |
---|
484 | return (error); |
---|
485 | } |
---|
486 | splx(s); |
---|
487 | if (scrub) { |
---|
488 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; |
---|
489 | in_ifscrub(ifp, ia); |
---|
490 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; |
---|
491 | } |
---|
492 | if (IN_CLASSA(i)) |
---|
493 | ia->ia_netmask = IN_CLASSA_NET; |
---|
494 | else if (IN_CLASSB(i)) |
---|
495 | ia->ia_netmask = IN_CLASSB_NET; |
---|
496 | else |
---|
497 | ia->ia_netmask = IN_CLASSC_NET; |
---|
498 | /* |
---|
499 | * The subnet mask usually includes at least the standard network part, |
---|
500 | * but may may be smaller in the case of supernetting. |
---|
501 | * If it is set, we believe it. |
---|
502 | */ |
---|
503 | if (ia->ia_subnetmask == 0) { |
---|
504 | ia->ia_subnetmask = ia->ia_netmask; |
---|
505 | ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); |
---|
506 | } else |
---|
507 | ia->ia_netmask &= ia->ia_subnetmask; |
---|
508 | ia->ia_net = i & ia->ia_netmask; |
---|
509 | ia->ia_subnet = i & ia->ia_subnetmask; |
---|
510 | in_socktrim(&ia->ia_sockmask); |
---|
511 | /* |
---|
512 | * Add route for the network. |
---|
513 | */ |
---|
514 | ia->ia_ifa.ifa_metric = ifp->if_metric; |
---|
515 | if (ifp->if_flags & IFF_BROADCAST) { |
---|
516 | ia->ia_broadaddr.sin_addr.s_addr = |
---|
517 | htonl(ia->ia_subnet | ~ia->ia_subnetmask); |
---|
518 | ia->ia_netbroadcast.s_addr = |
---|
519 | htonl(ia->ia_net | ~ ia->ia_netmask); |
---|
520 | } else if (ifp->if_flags & IFF_LOOPBACK) { |
---|
521 | ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; |
---|
522 | flags |= RTF_HOST; |
---|
523 | } else if (ifp->if_flags & IFF_POINTOPOINT) { |
---|
524 | if (ia->ia_dstaddr.sin_family != AF_INET) |
---|
525 | return (0); |
---|
526 | flags |= RTF_HOST; |
---|
527 | } |
---|
528 | if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) |
---|
529 | ia->ia_flags |= IFA_ROUTE; |
---|
530 | |
---|
531 | LIST_INIT(&ia->ia_multiaddrs); |
---|
532 | /* |
---|
533 | * If the interface supports multicast, join the "all hosts" |
---|
534 | * multicast group on that interface. |
---|
535 | */ |
---|
536 | if (ifp->if_flags & IFF_MULTICAST) { |
---|
537 | struct in_addr addr; |
---|
538 | |
---|
539 | /* |
---|
540 | * Continuation of multicast address hack: |
---|
541 | * If there was a multicast group list previously saved |
---|
542 | * for this interface, then we re-attach it to the first |
---|
543 | * address configured on the i/f. |
---|
544 | */ |
---|
545 | for(mk = in_mk.lh_first; mk; mk = mk->mk_entry.le_next) { |
---|
546 | if(mk->mk_ifp == ifp) { |
---|
547 | struct in_multi *inm; |
---|
548 | |
---|
549 | for(inm = mk->mk_head.lh_first; inm; |
---|
550 | inm = inm->inm_entry.le_next) { |
---|
551 | IFAFREE(&inm->inm_ia->ia_ifa); |
---|
552 | ia->ia_ifa.ifa_refcnt++; |
---|
553 | inm->inm_ia = ia; |
---|
554 | LIST_INSERT_HEAD(&ia->ia_multiaddrs, |
---|
555 | inm, inm_entry); |
---|
556 | } |
---|
557 | LIST_REMOVE(mk, mk_entry); |
---|
558 | free(mk, M_IPMADDR); |
---|
559 | break; |
---|
560 | } |
---|
561 | } |
---|
562 | |
---|
563 | addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); |
---|
564 | in_addmulti(&addr, ifp); |
---|
565 | } |
---|
566 | return (error); |
---|
567 | } |
---|
568 | |
---|
569 | |
---|
570 | /* |
---|
571 | * Return 1 if the address might be a local broadcast address. |
---|
572 | */ |
---|
573 | int |
---|
574 | in_broadcast(struct in_addr in, struct ifnet *ifp) |
---|
575 | { |
---|
576 | register struct ifaddr *ifa; |
---|
577 | u_long t; |
---|
578 | |
---|
579 | if (in.s_addr == INADDR_BROADCAST || |
---|
580 | in.s_addr == INADDR_ANY) |
---|
581 | return 1; |
---|
582 | if ((ifp->if_flags & IFF_BROADCAST) == 0) |
---|
583 | return 0; |
---|
584 | t = ntohl(in.s_addr); |
---|
585 | /* |
---|
586 | * Look through the list of addresses for a match |
---|
587 | * with a broadcast address. |
---|
588 | */ |
---|
589 | #define ia ((struct in_ifaddr *)ifa) |
---|
590 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) |
---|
591 | if (ifa->ifa_addr->sa_family == AF_INET && |
---|
592 | (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || |
---|
593 | in.s_addr == ia->ia_netbroadcast.s_addr || |
---|
594 | /* |
---|
595 | * Check for old-style (host 0) broadcast. |
---|
596 | */ |
---|
597 | t == ia->ia_subnet || t == ia->ia_net) && |
---|
598 | /* |
---|
599 | * Check for an all one subnetmask. These |
---|
600 | * only exist when an interface gets a secondary |
---|
601 | * address. |
---|
602 | */ |
---|
603 | ia->ia_subnetmask != (u_long)0xffffffff) |
---|
604 | return 1; |
---|
605 | return (0); |
---|
606 | #undef ia |
---|
607 | } |
---|
608 | /* |
---|
609 | * Add an address to the list of IP multicast addresses for a given interface. |
---|
610 | */ |
---|
611 | struct in_multi * |
---|
612 | in_addmulti(struct in_addr *ap, struct ifnet *ifp) |
---|
613 | { |
---|
614 | register struct in_multi *inm; |
---|
615 | struct ifreq ifr; |
---|
616 | struct in_ifaddr *ia; |
---|
617 | int s = splnet(); |
---|
618 | |
---|
619 | /* |
---|
620 | * See if address already in list. |
---|
621 | */ |
---|
622 | IN_LOOKUP_MULTI(*ap, ifp, inm); |
---|
623 | if (inm != NULL) { |
---|
624 | /* |
---|
625 | * Found it; just increment the reference count. |
---|
626 | */ |
---|
627 | ++inm->inm_refcount; |
---|
628 | } |
---|
629 | else { |
---|
630 | /* |
---|
631 | * New address; allocate a new multicast record |
---|
632 | * and link it into the interface's multicast list. |
---|
633 | */ |
---|
634 | inm = (struct in_multi *)malloc(sizeof(*inm), |
---|
635 | M_IPMADDR, M_NOWAIT); |
---|
636 | if (inm == NULL) { |
---|
637 | splx(s); |
---|
638 | return (NULL); |
---|
639 | } |
---|
640 | inm->inm_addr = *ap; |
---|
641 | inm->inm_ifp = ifp; |
---|
642 | inm->inm_refcount = 1; |
---|
643 | IFP_TO_IA(ifp, ia); |
---|
644 | if (ia == NULL) { |
---|
645 | free(inm, M_IPMADDR); |
---|
646 | splx(s); |
---|
647 | return (NULL); |
---|
648 | } |
---|
649 | inm->inm_ia = ia; |
---|
650 | ia->ia_ifa.ifa_refcnt++; /* gain a reference */ |
---|
651 | LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_entry); |
---|
652 | |
---|
653 | /* |
---|
654 | * Ask the network driver to update its multicast reception |
---|
655 | * filter appropriately for the new address. |
---|
656 | */ |
---|
657 | ((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET; |
---|
658 | ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr = *ap; |
---|
659 | if ((ifp->if_ioctl == NULL) || |
---|
660 | (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) { |
---|
661 | LIST_REMOVE(inm, inm_entry); |
---|
662 | IFAFREE(&ia->ia_ifa); /* release reference */ |
---|
663 | free(inm, M_IPMADDR); |
---|
664 | splx(s); |
---|
665 | return (NULL); |
---|
666 | } |
---|
667 | /* |
---|
668 | * Let IGMP know that we have joined a new IP multicast group. |
---|
669 | */ |
---|
670 | igmp_joingroup(inm); |
---|
671 | } |
---|
672 | splx(s); |
---|
673 | return (inm); |
---|
674 | } |
---|
675 | |
---|
676 | /* |
---|
677 | * Delete a multicast address record. |
---|
678 | */ |
---|
679 | void |
---|
680 | in_delmulti(struct in_multi *inm) |
---|
681 | { |
---|
682 | struct ifreq ifr; |
---|
683 | int s = splnet(); |
---|
684 | |
---|
685 | if (--inm->inm_refcount == 0) { |
---|
686 | /* |
---|
687 | * No remaining claims to this record; let IGMP know that |
---|
688 | * we are leaving the multicast group. |
---|
689 | */ |
---|
690 | igmp_leavegroup(inm); |
---|
691 | /* |
---|
692 | * Unlink from list. |
---|
693 | */ |
---|
694 | LIST_REMOVE(inm, inm_entry); |
---|
695 | IFAFREE(&inm->inm_ia->ia_ifa); /* release reference */ |
---|
696 | |
---|
697 | /* |
---|
698 | * Notify the network driver to update its multicast reception |
---|
699 | * filter. |
---|
700 | */ |
---|
701 | ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET; |
---|
702 | ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr = |
---|
703 | inm->inm_addr; |
---|
704 | (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI, |
---|
705 | (caddr_t)&ifr); |
---|
706 | free(inm, M_IPMADDR); |
---|
707 | } |
---|
708 | splx(s); |
---|
709 | } |
---|