FreeBSD/Linux Kernel Cross Reference
sys/netinet/in.c
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)in.c 8.4 (Berkeley) 1/9/95
34 * $FreeBSD: releng/5.0/sys/netinet/in.c 105748 2002-10-22 22:50:38Z suz $
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sockio.h>
40 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
44
45 #include <net/if.h>
46 #include <net/if_types.h>
47 #include <net/route.h>
48
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/in_pcb.h>
52
53 #include <netinet/igmp_var.h>
54
55 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
56
57 static int in_mask2len(struct in_addr *);
58 static void in_len2mask(struct in_addr *, int);
59 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
60 struct ifnet *, struct thread *);
61
62 static void in_socktrim(struct sockaddr_in *);
63 static int in_ifinit(struct ifnet *,
64 struct in_ifaddr *, struct sockaddr_in *, int);
65
66 static int subnetsarelocal = 0;
67 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
68 &subnetsarelocal, 0, "");
69
70 struct in_multihead in_multihead; /* XXX BSS initialization */
71
72 extern struct inpcbinfo ripcbinfo;
73 extern struct inpcbinfo udbinfo;
74
75 /*
76 * Return 1 if an internet address is for a ``local'' host
77 * (one to which we have a connection). If subnetsarelocal
78 * is true, this includes other subnets of the local net.
79 * Otherwise, it includes only the directly-connected (sub)nets.
80 */
81 int
82 in_localaddr(in)
83 struct in_addr in;
84 {
85 register u_long i = ntohl(in.s_addr);
86 register struct in_ifaddr *ia;
87
88 if (subnetsarelocal) {
89 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
90 if ((i & ia->ia_netmask) == ia->ia_net)
91 return (1);
92 } else {
93 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
94 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
95 return (1);
96 }
97 return (0);
98 }
99
100 /*
101 * Determine whether an IP address is in a reserved set of addresses
102 * that may not be forwarded, or whether datagrams to that destination
103 * may be forwarded.
104 */
105 int
106 in_canforward(in)
107 struct in_addr in;
108 {
109 register u_long i = ntohl(in.s_addr);
110 register u_long net;
111
112 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
113 return (0);
114 if (IN_CLASSA(i)) {
115 net = i & IN_CLASSA_NET;
116 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
117 return (0);
118 }
119 return (1);
120 }
121
122 /*
123 * Trim a mask in a sockaddr
124 */
125 static void
126 in_socktrim(ap)
127 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
141 in_mask2len(mask)
142 struct in_addr *mask;
143 {
144 int x, y;
145 u_char *p;
146
147 p = (u_char *)mask;
148 for (x = 0; x < sizeof(*mask); x++) {
149 if (p[x] != 0xff)
150 break;
151 }
152 y = 0;
153 if (x < sizeof(*mask)) {
154 for (y = 0; y < 8; y++) {
155 if ((p[x] & (0x80 >> y)) == 0)
156 break;
157 }
158 }
159 return x * 8 + y;
160 }
161
162 static void
163 in_len2mask(mask, len)
164 struct in_addr *mask;
165 int len;
166 {
167 int i;
168 u_char *p;
169
170 p = (u_char *)mask;
171 bzero(mask, sizeof(*mask));
172 for (i = 0; i < len / 8; i++)
173 p[i] = 0xff;
174 if (len % 8)
175 p[i] = (0xff00 >> (len % 8)) & 0xff;
176 }
177
178 static int in_interfaces; /* number of external internet interfaces */
179
180 /*
181 * Generic internet control operations (ioctl's).
182 * Ifp is 0 if not an interface-specific ioctl.
183 */
184 /* ARGSUSED */
185 int
186 in_control(so, cmd, data, ifp, td)
187 struct socket *so;
188 u_long cmd;
189 caddr_t data;
190 register struct ifnet *ifp;
191 struct thread *td;
192 {
193 register struct ifreq *ifr = (struct ifreq *)data;
194 register struct in_ifaddr *ia = 0, *iap;
195 register struct ifaddr *ifa;
196 struct in_addr dst;
197 struct in_ifaddr *oia;
198 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
199 struct sockaddr_in oldaddr;
200 int error, hostIsNew, iaIsNew, maskIsNew, s;
201
202 iaIsNew = 0;
203
204 switch (cmd) {
205 case SIOCALIFADDR:
206 case SIOCDLIFADDR:
207 if (td && (error = suser(td)) != 0)
208 return error;
209 /*fall through*/
210 case SIOCGLIFADDR:
211 if (!ifp)
212 return EINVAL;
213 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
214 }
215
216 /*
217 * Find address for this interface, if it exists.
218 *
219 * If an alias address was specified, find that one instead of
220 * the first one on the interface, if possible.
221 */
222 if (ifp) {
223 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
224 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
225 if (iap->ia_ifp == ifp &&
226 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
227 ia = iap;
228 break;
229 }
230 if (ia == NULL)
231 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
232 iap = ifatoia(ifa);
233 if (iap->ia_addr.sin_family == AF_INET) {
234 ia = iap;
235 break;
236 }
237 }
238 }
239
240 switch (cmd) {
241
242 case SIOCAIFADDR:
243 case SIOCDIFADDR:
244 if (ifp == 0)
245 return (EADDRNOTAVAIL);
246 if (ifra->ifra_addr.sin_family == AF_INET) {
247 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
248 if (ia->ia_ifp == ifp &&
249 ia->ia_addr.sin_addr.s_addr ==
250 ifra->ifra_addr.sin_addr.s_addr)
251 break;
252 }
253 if ((ifp->if_flags & IFF_POINTOPOINT)
254 && (cmd == SIOCAIFADDR)
255 && (ifra->ifra_dstaddr.sin_addr.s_addr
256 == INADDR_ANY)) {
257 return EDESTADDRREQ;
258 }
259 }
260 if (cmd == SIOCDIFADDR && ia == 0)
261 return (EADDRNOTAVAIL);
262 /* FALLTHROUGH */
263 case SIOCSIFADDR:
264 case SIOCSIFNETMASK:
265 case SIOCSIFDSTADDR:
266 if (td && (error = suser(td)) != 0)
267 return error;
268
269 if (ifp == 0)
270 return (EADDRNOTAVAIL);
271 if (ia == (struct in_ifaddr *)0) {
272 ia = (struct in_ifaddr *)
273 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
274 if (ia == (struct in_ifaddr *)NULL)
275 return (ENOBUFS);
276 /*
277 * Protect from ipintr() traversing address list
278 * while we're modifying it.
279 */
280 s = splnet();
281
282 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
283 ifa = &ia->ia_ifa;
284 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
285
286 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
287 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
288 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
289 ia->ia_sockmask.sin_len = 8;
290 ia->ia_sockmask.sin_family = AF_INET;
291 if (ifp->if_flags & IFF_BROADCAST) {
292 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
293 ia->ia_broadaddr.sin_family = AF_INET;
294 }
295 ia->ia_ifp = ifp;
296 if (!(ifp->if_flags & IFF_LOOPBACK))
297 in_interfaces++;
298 splx(s);
299 iaIsNew = 1;
300 }
301 break;
302
303 case SIOCSIFBRDADDR:
304 if (td && (error = suser(td)) != 0)
305 return error;
306 /* FALLTHROUGH */
307
308 case SIOCGIFADDR:
309 case SIOCGIFNETMASK:
310 case SIOCGIFDSTADDR:
311 case SIOCGIFBRDADDR:
312 if (ia == (struct in_ifaddr *)0)
313 return (EADDRNOTAVAIL);
314 break;
315 }
316 switch (cmd) {
317
318 case SIOCGIFADDR:
319 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
320 return (0);
321
322 case SIOCGIFBRDADDR:
323 if ((ifp->if_flags & IFF_BROADCAST) == 0)
324 return (EINVAL);
325 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
326 return (0);
327
328 case SIOCGIFDSTADDR:
329 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
330 return (EINVAL);
331 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
332 return (0);
333
334 case SIOCGIFNETMASK:
335 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
336 return (0);
337
338 case SIOCSIFDSTADDR:
339 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
340 return (EINVAL);
341 oldaddr = ia->ia_dstaddr;
342 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
343 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
344 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
345 ia->ia_dstaddr = oldaddr;
346 return (error);
347 }
348 if (ia->ia_flags & IFA_ROUTE) {
349 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
350 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
351 ia->ia_ifa.ifa_dstaddr =
352 (struct sockaddr *)&ia->ia_dstaddr;
353 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
354 }
355 return (0);
356
357 case SIOCSIFBRDADDR:
358 if ((ifp->if_flags & IFF_BROADCAST) == 0)
359 return (EINVAL);
360 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
361 return (0);
362
363 case SIOCSIFADDR:
364 error = in_ifinit(ifp, ia,
365 (struct sockaddr_in *) &ifr->ifr_addr, 1);
366 if (error != 0 && iaIsNew)
367 break;
368 return (0);
369
370 case SIOCSIFNETMASK:
371 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
372 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
373 return (0);
374
375 case SIOCAIFADDR:
376 maskIsNew = 0;
377 hostIsNew = 1;
378 error = 0;
379 if (ia->ia_addr.sin_family == AF_INET) {
380 if (ifra->ifra_addr.sin_len == 0) {
381 ifra->ifra_addr = ia->ia_addr;
382 hostIsNew = 0;
383 } else if (ifra->ifra_addr.sin_addr.s_addr ==
384 ia->ia_addr.sin_addr.s_addr)
385 hostIsNew = 0;
386 }
387 if (ifra->ifra_mask.sin_len) {
388 in_ifscrub(ifp, ia);
389 ia->ia_sockmask = ifra->ifra_mask;
390 ia->ia_sockmask.sin_family = AF_INET;
391 ia->ia_subnetmask =
392 ntohl(ia->ia_sockmask.sin_addr.s_addr);
393 maskIsNew = 1;
394 }
395 if ((ifp->if_flags & IFF_POINTOPOINT) &&
396 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
397 in_ifscrub(ifp, ia);
398 ia->ia_dstaddr = ifra->ifra_dstaddr;
399 maskIsNew = 1; /* We lie; but the effect's the same */
400 }
401 if (ifra->ifra_addr.sin_family == AF_INET &&
402 (hostIsNew || maskIsNew))
403 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
404 if (error != 0 && iaIsNew)
405 break;
406
407 if ((ifp->if_flags & IFF_BROADCAST) &&
408 (ifra->ifra_broadaddr.sin_family == AF_INET))
409 ia->ia_broadaddr = ifra->ifra_broadaddr;
410 return (error);
411
412 case SIOCDIFADDR:
413 /*
414 * in_ifscrub kills the interface route.
415 */
416 in_ifscrub(ifp, ia);
417 /*
418 * in_ifadown gets rid of all the rest of
419 * the routes. This is not quite the right
420 * thing to do, but at least if we are running
421 * a routing process they will come back.
422 */
423 in_ifadown(&ia->ia_ifa, 1);
424 /*
425 * XXX horrible hack to detect that we are being called
426 * from if_detach()
427 */
428 if (ifaddr_byindex(ifp->if_index) != NULL) {
429 in_pcbpurgeif0(&ripcbinfo, ifp);
430 in_pcbpurgeif0(&udbinfo, ifp);
431 }
432 error = 0;
433 break;
434
435 default:
436 if (ifp == 0 || ifp->if_ioctl == 0)
437 return (EOPNOTSUPP);
438 return ((*ifp->if_ioctl)(ifp, cmd, data));
439 }
440
441 /*
442 * Protect from ipintr() traversing address list while we're modifying
443 * it.
444 */
445 s = splnet();
446 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
447 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
448 LIST_REMOVE(ia, ia_hash);
449 IFAFREE(&ia->ia_ifa);
450 splx(s);
451
452 return (error);
453 }
454
455 /*
456 * SIOC[GAD]LIFADDR.
457 * SIOCGLIFADDR: get first address. (?!?)
458 * SIOCGLIFADDR with IFLR_PREFIX:
459 * get first address that matches the specified prefix.
460 * SIOCALIFADDR: add the specified address.
461 * SIOCALIFADDR with IFLR_PREFIX:
462 * EINVAL since we can't deduce hostid part of the address.
463 * SIOCDLIFADDR: delete the specified address.
464 * SIOCDLIFADDR with IFLR_PREFIX:
465 * delete the first address that matches the specified prefix.
466 * return values:
467 * EINVAL on invalid parameters
468 * EADDRNOTAVAIL on prefix match failed/specified address not found
469 * other values may be returned from in_ioctl()
470 */
471 static int
472 in_lifaddr_ioctl(so, cmd, data, ifp, td)
473 struct socket *so;
474 u_long cmd;
475 caddr_t data;
476 struct ifnet *ifp;
477 struct thread *td;
478 {
479 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
480 struct ifaddr *ifa;
481
482 /* sanity checks */
483 if (!data || !ifp) {
484 panic("invalid argument to in_lifaddr_ioctl");
485 /*NOTRECHED*/
486 }
487
488 switch (cmd) {
489 case SIOCGLIFADDR:
490 /* address must be specified on GET with IFLR_PREFIX */
491 if ((iflr->flags & IFLR_PREFIX) == 0)
492 break;
493 /*FALLTHROUGH*/
494 case SIOCALIFADDR:
495 case SIOCDLIFADDR:
496 /* address must be specified on ADD and DELETE */
497 if (iflr->addr.ss_family != AF_INET)
498 return EINVAL;
499 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
500 return EINVAL;
501 /* XXX need improvement */
502 if (iflr->dstaddr.ss_family
503 && iflr->dstaddr.ss_family != AF_INET)
504 return EINVAL;
505 if (iflr->dstaddr.ss_family
506 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
507 return EINVAL;
508 break;
509 default: /*shouldn't happen*/
510 return EOPNOTSUPP;
511 }
512 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
513 return EINVAL;
514
515 switch (cmd) {
516 case SIOCALIFADDR:
517 {
518 struct in_aliasreq ifra;
519
520 if (iflr->flags & IFLR_PREFIX)
521 return EINVAL;
522
523 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
524 bzero(&ifra, sizeof(ifra));
525 bcopy(iflr->iflr_name, ifra.ifra_name,
526 sizeof(ifra.ifra_name));
527
528 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
529
530 if (iflr->dstaddr.ss_family) { /*XXX*/
531 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
532 iflr->dstaddr.ss_len);
533 }
534
535 ifra.ifra_mask.sin_family = AF_INET;
536 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
537 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
538
539 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
540 }
541 case SIOCGLIFADDR:
542 case SIOCDLIFADDR:
543 {
544 struct in_ifaddr *ia;
545 struct in_addr mask, candidate, match;
546 struct sockaddr_in *sin;
547 int cmp;
548
549 bzero(&mask, sizeof(mask));
550 if (iflr->flags & IFLR_PREFIX) {
551 /* lookup a prefix rather than address. */
552 in_len2mask(&mask, iflr->prefixlen);
553
554 sin = (struct sockaddr_in *)&iflr->addr;
555 match.s_addr = sin->sin_addr.s_addr;
556 match.s_addr &= mask.s_addr;
557
558 /* if you set extra bits, that's wrong */
559 if (match.s_addr != sin->sin_addr.s_addr)
560 return EINVAL;
561
562 cmp = 1;
563 } else {
564 if (cmd == SIOCGLIFADDR) {
565 /* on getting an address, take the 1st match */
566 cmp = 0; /*XXX*/
567 } else {
568 /* on deleting an address, do exact match */
569 in_len2mask(&mask, 32);
570 sin = (struct sockaddr_in *)&iflr->addr;
571 match.s_addr = sin->sin_addr.s_addr;
572
573 cmp = 1;
574 }
575 }
576
577 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
578 if (ifa->ifa_addr->sa_family != AF_INET6)
579 continue;
580 if (!cmp)
581 break;
582 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
583 candidate.s_addr &= mask.s_addr;
584 if (candidate.s_addr == match.s_addr)
585 break;
586 }
587 if (!ifa)
588 return EADDRNOTAVAIL;
589 ia = (struct in_ifaddr *)ifa;
590
591 if (cmd == SIOCGLIFADDR) {
592 /* fill in the if_laddrreq structure */
593 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
594
595 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
596 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
597 ia->ia_dstaddr.sin_len);
598 } else
599 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
600
601 iflr->prefixlen =
602 in_mask2len(&ia->ia_sockmask.sin_addr);
603
604 iflr->flags = 0; /*XXX*/
605
606 return 0;
607 } else {
608 struct in_aliasreq ifra;
609
610 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
611 bzero(&ifra, sizeof(ifra));
612 bcopy(iflr->iflr_name, ifra.ifra_name,
613 sizeof(ifra.ifra_name));
614
615 bcopy(&ia->ia_addr, &ifra.ifra_addr,
616 ia->ia_addr.sin_len);
617 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
618 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
619 ia->ia_dstaddr.sin_len);
620 }
621 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
622 ia->ia_sockmask.sin_len);
623
624 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
625 ifp, td);
626 }
627 }
628 }
629
630 return EOPNOTSUPP; /*just for safety*/
631 }
632
633 /*
634 * Delete any existing route for an interface.
635 */
636 void
637 in_ifscrub(ifp, ia)
638 register struct ifnet *ifp;
639 register struct in_ifaddr *ia;
640 {
641
642 if ((ia->ia_flags & IFA_ROUTE) == 0)
643 return;
644 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
645 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
646 else
647 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
648 ia->ia_flags &= ~IFA_ROUTE;
649 }
650
651 /*
652 * Initialize an interface's internet address
653 * and routing table entry.
654 */
655 static int
656 in_ifinit(ifp, ia, sin, scrub)
657 register struct ifnet *ifp;
658 register struct in_ifaddr *ia;
659 struct sockaddr_in *sin;
660 int scrub;
661 {
662 register u_long i = ntohl(sin->sin_addr.s_addr);
663 struct sockaddr_in oldaddr;
664 int s = splimp(), flags = RTF_UP, error = 0;
665
666 oldaddr = ia->ia_addr;
667 if (oldaddr.sin_family == AF_INET)
668 LIST_REMOVE(ia, ia_hash);
669 ia->ia_addr = *sin;
670 if (ia->ia_addr.sin_family == AF_INET)
671 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
672 ia, ia_hash);
673 /*
674 * Give the interface a chance to initialize
675 * if this is its first address,
676 * and to validate the address if necessary.
677 */
678 if (ifp->if_ioctl &&
679 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
680 splx(s);
681 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
682 ia->ia_addr = oldaddr;
683 if (ia->ia_addr.sin_family == AF_INET)
684 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
685 ia, ia_hash);
686 return (error);
687 }
688 splx(s);
689 if (scrub) {
690 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
691 in_ifscrub(ifp, ia);
692 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
693 }
694 if (IN_CLASSA(i))
695 ia->ia_netmask = IN_CLASSA_NET;
696 else if (IN_CLASSB(i))
697 ia->ia_netmask = IN_CLASSB_NET;
698 else
699 ia->ia_netmask = IN_CLASSC_NET;
700 /*
701 * The subnet mask usually includes at least the standard network part,
702 * but may may be smaller in the case of supernetting.
703 * If it is set, we believe it.
704 */
705 if (ia->ia_subnetmask == 0) {
706 ia->ia_subnetmask = ia->ia_netmask;
707 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
708 } else
709 ia->ia_netmask &= ia->ia_subnetmask;
710 ia->ia_net = i & ia->ia_netmask;
711 ia->ia_subnet = i & ia->ia_subnetmask;
712 in_socktrim(&ia->ia_sockmask);
713 /*
714 * Add route for the network.
715 */
716 ia->ia_ifa.ifa_metric = ifp->if_metric;
717 if (ifp->if_flags & IFF_BROADCAST) {
718 ia->ia_broadaddr.sin_addr.s_addr =
719 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
720 ia->ia_netbroadcast.s_addr =
721 htonl(ia->ia_net | ~ ia->ia_netmask);
722 } else if (ifp->if_flags & IFF_LOOPBACK) {
723 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
724 flags |= RTF_HOST;
725 } else if (ifp->if_flags & IFF_POINTOPOINT) {
726 if (ia->ia_dstaddr.sin_family != AF_INET)
727 return (0);
728 flags |= RTF_HOST;
729 }
730
731 /*-
732 * Don't add host routes for interface addresses of
733 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
734 * possible to assign several such address pairs with consistent
735 * results (no host route) and is required by BOOTP.
736 *
737 * XXX: This is ugly ! There should be a way for the caller to
738 * say that they don't want a host route.
739 */
740 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
741 ia->ia_netmask != IN_CLASSA_NET ||
742 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
743 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
744 ia->ia_addr = oldaddr;
745 return (error);
746 }
747 ia->ia_flags |= IFA_ROUTE;
748 }
749
750 /*
751 * If the interface supports multicast, join the "all hosts"
752 * multicast group on that interface.
753 */
754 if (ifp->if_flags & IFF_MULTICAST) {
755 struct in_addr addr;
756
757 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
758 in_addmulti(&addr, ifp);
759 }
760 return (error);
761 }
762
763
764 /*
765 * Return 1 if the address might be a local broadcast address.
766 */
767 int
768 in_broadcast(in, ifp)
769 struct in_addr in;
770 struct ifnet *ifp;
771 {
772 register struct ifaddr *ifa;
773 u_long t;
774
775 if (in.s_addr == INADDR_BROADCAST ||
776 in.s_addr == INADDR_ANY)
777 return 1;
778 if ((ifp->if_flags & IFF_BROADCAST) == 0)
779 return 0;
780 t = ntohl(in.s_addr);
781 /*
782 * Look through the list of addresses for a match
783 * with a broadcast address.
784 */
785 #define ia ((struct in_ifaddr *)ifa)
786 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
787 if (ifa->ifa_addr->sa_family == AF_INET &&
788 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
789 in.s_addr == ia->ia_netbroadcast.s_addr ||
790 /*
791 * Check for old-style (host 0) broadcast.
792 */
793 t == ia->ia_subnet || t == ia->ia_net) &&
794 /*
795 * Check for an all one subnetmask. These
796 * only exist when an interface gets a secondary
797 * address.
798 */
799 ia->ia_subnetmask != (u_long)0xffffffff)
800 return 1;
801 return (0);
802 #undef ia
803 }
804 /*
805 * Add an address to the list of IP multicast addresses for a given interface.
806 */
807 struct in_multi *
808 in_addmulti(ap, ifp)
809 register struct in_addr *ap;
810 register struct ifnet *ifp;
811 {
812 register struct in_multi *inm;
813 int error;
814 struct sockaddr_in sin;
815 struct ifmultiaddr *ifma;
816 int s = splnet();
817
818 /*
819 * Call generic routine to add membership or increment
820 * refcount. It wants addresses in the form of a sockaddr,
821 * so we build one here (being careful to zero the unused bytes).
822 */
823 bzero(&sin, sizeof sin);
824 sin.sin_family = AF_INET;
825 sin.sin_len = sizeof sin;
826 sin.sin_addr = *ap;
827 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
828 if (error) {
829 splx(s);
830 return 0;
831 }
832
833 /*
834 * If ifma->ifma_protospec is null, then if_addmulti() created
835 * a new record. Otherwise, we are done.
836 */
837 if (ifma->ifma_protospec != 0) {
838 splx(s);
839 return ifma->ifma_protospec;
840 }
841
842 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
843 at interrupt time? If so, need to fix if_addmulti. XXX */
844 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
845 M_NOWAIT | M_ZERO);
846 if (inm == NULL) {
847 splx(s);
848 return (NULL);
849 }
850
851 inm->inm_addr = *ap;
852 inm->inm_ifp = ifp;
853 inm->inm_ifma = ifma;
854 ifma->ifma_protospec = inm;
855 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
856
857 /*
858 * Let IGMP know that we have joined a new IP multicast group.
859 */
860 igmp_joingroup(inm);
861 splx(s);
862 return (inm);
863 }
864
865 /*
866 * Delete a multicast address record.
867 */
868 void
869 in_delmulti(inm)
870 register struct in_multi *inm;
871 {
872 struct ifmultiaddr *ifma = inm->inm_ifma;
873 struct in_multi my_inm;
874 int s = splnet();
875
876 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
877 if (ifma->ifma_refcount == 1) {
878 /*
879 * No remaining claims to this record; let IGMP know that
880 * we are leaving the multicast group.
881 * But do it after the if_delmulti() which might reset
882 * the interface and nuke the packet.
883 */
884 my_inm = *inm ;
885 ifma->ifma_protospec = 0;
886 LIST_REMOVE(inm, inm_link);
887 free(inm, M_IPMADDR);
888 }
889 /* XXX - should be separate API for when we have an ifma? */
890 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
891 if (my_inm.inm_ifp != NULL)
892 igmp_leavegroup(&my_inm);
893 splx(s);
894 }
Cache object: ceb0e30b425891ceef516db9e2c494ea
|