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 * Copyright (C) 2001 WIDE Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * @(#)in.c 8.4 (Berkeley) 1/9/95
31 * $FreeBSD: releng/6.3/sys/netinet/in.c 171289 2007-07-07 00:54:46Z brian $
32 */
33
34 #include "opt_carp.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43
44 #include <net/if.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
47
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/in_pcb.h>
51
52 #include <netinet/igmp_var.h>
53
54 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
55
56 static int in_mask2len(struct in_addr *);
57 static void in_len2mask(struct in_addr *, int);
58 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
59 struct ifnet *, struct thread *);
60
61 static int in_addprefix(struct in_ifaddr *, int);
62 static void in_delmulti_locked(register struct in_multi *, int);
63 static int in_scrubprefix(struct in_ifaddr *);
64 static void in_socktrim(struct sockaddr_in *);
65 static int in_ifinit(struct ifnet *,
66 struct in_ifaddr *, struct sockaddr_in *, int);
67
68 static int subnetsarelocal = 0;
69 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
70 &subnetsarelocal, 0, "Treat all subnets as directly connected");
71 static int sameprefixcarponly = 0;
72 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
73 &sameprefixcarponly, 0,
74 "Refuse to create same prefixes on different interfaces");
75
76 /*
77 * The IPv4 multicast list (in_multihead and associated structures) are
78 * protected by the global in_multi_mtx. See in_var.h for more details. For
79 * now, in_multi_mtx is marked as recursible due to IGMP's calling back into
80 * ip_output() to send IGMP packets while holding the lock; this probably is
81 * not quite desirable.
82 */
83 struct in_multihead in_multihead; /* XXX BSS initialization */
84 struct mtx in_multi_mtx;
85 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE);
86
87 extern struct inpcbinfo ripcbinfo;
88 extern struct inpcbinfo udbinfo;
89
90 /*
91 * Return 1 if an internet address is for a ``local'' host
92 * (one to which we have a connection). If subnetsarelocal
93 * is true, this includes other subnets of the local net.
94 * Otherwise, it includes only the directly-connected (sub)nets.
95 */
96 int
97 in_localaddr(in)
98 struct in_addr in;
99 {
100 register u_long i = ntohl(in.s_addr);
101 register struct in_ifaddr *ia;
102
103 if (subnetsarelocal) {
104 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
105 if ((i & ia->ia_netmask) == ia->ia_net)
106 return (1);
107 } else {
108 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
109 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
110 return (1);
111 }
112 return (0);
113 }
114
115 /*
116 * Return 1 if an internet address is for the local host and configured
117 * on one of its interfaces.
118 */
119 int
120 in_localip(in)
121 struct in_addr in;
122 {
123 struct in_ifaddr *ia;
124
125 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
126 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
127 return 1;
128 }
129 return 0;
130 }
131
132 /*
133 * Determine whether an IP address is in a reserved set of addresses
134 * that may not be forwarded, or whether datagrams to that destination
135 * may be forwarded.
136 */
137 int
138 in_canforward(in)
139 struct in_addr in;
140 {
141 register u_long i = ntohl(in.s_addr);
142 register u_long net;
143
144 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
145 return (0);
146 if (IN_CLASSA(i)) {
147 net = i & IN_CLASSA_NET;
148 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
149 return (0);
150 }
151 return (1);
152 }
153
154 /*
155 * Trim a mask in a sockaddr
156 */
157 static void
158 in_socktrim(ap)
159 struct sockaddr_in *ap;
160 {
161 register char *cplim = (char *) &ap->sin_addr;
162 register char *cp = (char *) (&ap->sin_addr + 1);
163
164 ap->sin_len = 0;
165 while (--cp >= cplim)
166 if (*cp) {
167 (ap)->sin_len = cp - (char *) (ap) + 1;
168 break;
169 }
170 }
171
172 static int
173 in_mask2len(mask)
174 struct in_addr *mask;
175 {
176 int x, y;
177 u_char *p;
178
179 p = (u_char *)mask;
180 for (x = 0; x < sizeof(*mask); x++) {
181 if (p[x] != 0xff)
182 break;
183 }
184 y = 0;
185 if (x < sizeof(*mask)) {
186 for (y = 0; y < 8; y++) {
187 if ((p[x] & (0x80 >> y)) == 0)
188 break;
189 }
190 }
191 return x * 8 + y;
192 }
193
194 static void
195 in_len2mask(mask, len)
196 struct in_addr *mask;
197 int len;
198 {
199 int i;
200 u_char *p;
201
202 p = (u_char *)mask;
203 bzero(mask, sizeof(*mask));
204 for (i = 0; i < len / 8; i++)
205 p[i] = 0xff;
206 if (len % 8)
207 p[i] = (0xff00 >> (len % 8)) & 0xff;
208 }
209
210 /*
211 * Generic internet control operations (ioctl's).
212 * Ifp is 0 if not an interface-specific ioctl.
213 */
214 /* ARGSUSED */
215 int
216 in_control(so, cmd, data, ifp, td)
217 struct socket *so;
218 u_long cmd;
219 caddr_t data;
220 register struct ifnet *ifp;
221 struct thread *td;
222 {
223 register struct ifreq *ifr = (struct ifreq *)data;
224 register struct in_ifaddr *ia = 0, *iap;
225 register struct ifaddr *ifa;
226 struct in_addr dst;
227 struct in_ifaddr *oia;
228 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
229 struct sockaddr_in oldaddr;
230 int error, hostIsNew, iaIsNew, maskIsNew, s;
231
232 iaIsNew = 0;
233
234 switch (cmd) {
235 case SIOCALIFADDR:
236 case SIOCDLIFADDR:
237 if (td && (error = suser(td)) != 0)
238 return error;
239 /*fall through*/
240 case SIOCGLIFADDR:
241 if (!ifp)
242 return EINVAL;
243 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
244 }
245
246 /*
247 * Find address for this interface, if it exists.
248 *
249 * If an alias address was specified, find that one instead of
250 * the first one on the interface, if possible.
251 */
252 if (ifp) {
253 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
254 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
255 if (iap->ia_ifp == ifp &&
256 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
257 ia = iap;
258 break;
259 }
260 if (ia == NULL)
261 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
262 iap = ifatoia(ifa);
263 if (iap->ia_addr.sin_family == AF_INET) {
264 ia = iap;
265 break;
266 }
267 }
268 }
269
270 switch (cmd) {
271
272 case SIOCAIFADDR:
273 case SIOCDIFADDR:
274 if (ifp == 0)
275 return (EADDRNOTAVAIL);
276 if (ifra->ifra_addr.sin_family == AF_INET) {
277 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
278 if (ia->ia_ifp == ifp &&
279 ia->ia_addr.sin_addr.s_addr ==
280 ifra->ifra_addr.sin_addr.s_addr)
281 break;
282 }
283 if ((ifp->if_flags & IFF_POINTOPOINT)
284 && (cmd == SIOCAIFADDR)
285 && (ifra->ifra_dstaddr.sin_addr.s_addr
286 == INADDR_ANY)) {
287 return EDESTADDRREQ;
288 }
289 }
290 if (cmd == SIOCDIFADDR && ia == 0)
291 return (EADDRNOTAVAIL);
292 /* FALLTHROUGH */
293 case SIOCSIFADDR:
294 case SIOCSIFNETMASK:
295 case SIOCSIFDSTADDR:
296 if (td && (error = suser(td)) != 0)
297 return error;
298
299 if (ifp == 0)
300 return (EADDRNOTAVAIL);
301 if (ia == (struct in_ifaddr *)0) {
302 ia = (struct in_ifaddr *)
303 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
304 if (ia == (struct in_ifaddr *)NULL)
305 return (ENOBUFS);
306 /*
307 * Protect from ipintr() traversing address list
308 * while we're modifying it.
309 */
310 s = splnet();
311 ifa = &ia->ia_ifa;
312 IFA_LOCK_INIT(ifa);
313 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
314 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
315 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
316 ifa->ifa_refcnt = 1;
317 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
318
319 ia->ia_sockmask.sin_len = 8;
320 ia->ia_sockmask.sin_family = AF_INET;
321 if (ifp->if_flags & IFF_BROADCAST) {
322 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
323 ia->ia_broadaddr.sin_family = AF_INET;
324 }
325 ia->ia_ifp = ifp;
326
327 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
328 splx(s);
329 iaIsNew = 1;
330 }
331 break;
332
333 case SIOCSIFBRDADDR:
334 if (td && (error = suser(td)) != 0)
335 return error;
336 /* FALLTHROUGH */
337
338 case SIOCGIFADDR:
339 case SIOCGIFNETMASK:
340 case SIOCGIFDSTADDR:
341 case SIOCGIFBRDADDR:
342 if (ia == (struct in_ifaddr *)0)
343 return (EADDRNOTAVAIL);
344 break;
345 }
346 switch (cmd) {
347
348 case SIOCGIFADDR:
349 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
350 return (0);
351
352 case SIOCGIFBRDADDR:
353 if ((ifp->if_flags & IFF_BROADCAST) == 0)
354 return (EINVAL);
355 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
356 return (0);
357
358 case SIOCGIFDSTADDR:
359 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
360 return (EINVAL);
361 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
362 return (0);
363
364 case SIOCGIFNETMASK:
365 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
366 return (0);
367
368 case SIOCSIFDSTADDR:
369 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
370 return (EINVAL);
371 oldaddr = ia->ia_dstaddr;
372 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
373 if (ifp->if_ioctl) {
374 IFF_LOCKGIANT(ifp);
375 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
376 (caddr_t)ia);
377 IFF_UNLOCKGIANT(ifp);
378 if (error) {
379 ia->ia_dstaddr = oldaddr;
380 return (error);
381 }
382 }
383 if (ia->ia_flags & IFA_ROUTE) {
384 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
385 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
386 ia->ia_ifa.ifa_dstaddr =
387 (struct sockaddr *)&ia->ia_dstaddr;
388 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
389 }
390 return (0);
391
392 case SIOCSIFBRDADDR:
393 if ((ifp->if_flags & IFF_BROADCAST) == 0)
394 return (EINVAL);
395 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
396 return (0);
397
398 case SIOCSIFADDR:
399 error = in_ifinit(ifp, ia,
400 (struct sockaddr_in *) &ifr->ifr_addr, 1);
401 if (error != 0 && iaIsNew)
402 break;
403 if (error == 0)
404 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
405 return (0);
406
407 case SIOCSIFNETMASK:
408 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
409 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
410 return (0);
411
412 case SIOCAIFADDR:
413 maskIsNew = 0;
414 hostIsNew = 1;
415 error = 0;
416 if (ia->ia_addr.sin_family == AF_INET) {
417 if (ifra->ifra_addr.sin_len == 0) {
418 ifra->ifra_addr = ia->ia_addr;
419 hostIsNew = 0;
420 } else if (ifra->ifra_addr.sin_addr.s_addr ==
421 ia->ia_addr.sin_addr.s_addr)
422 hostIsNew = 0;
423 }
424 if (ifra->ifra_mask.sin_len) {
425 in_ifscrub(ifp, ia);
426 ia->ia_sockmask = ifra->ifra_mask;
427 ia->ia_sockmask.sin_family = AF_INET;
428 ia->ia_subnetmask =
429 ntohl(ia->ia_sockmask.sin_addr.s_addr);
430 maskIsNew = 1;
431 }
432 if ((ifp->if_flags & IFF_POINTOPOINT) &&
433 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
434 in_ifscrub(ifp, ia);
435 ia->ia_dstaddr = ifra->ifra_dstaddr;
436 maskIsNew = 1; /* We lie; but the effect's the same */
437 }
438 if (ifra->ifra_addr.sin_family == AF_INET &&
439 (hostIsNew || maskIsNew))
440 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
441 if (error != 0 && iaIsNew)
442 break;
443
444 if ((ifp->if_flags & IFF_BROADCAST) &&
445 (ifra->ifra_broadaddr.sin_family == AF_INET))
446 ia->ia_broadaddr = ifra->ifra_broadaddr;
447 if (error == 0)
448 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
449 return (error);
450
451 case SIOCDIFADDR:
452 /*
453 * in_ifscrub kills the interface route.
454 */
455 in_ifscrub(ifp, ia);
456 /*
457 * in_ifadown gets rid of all the rest of
458 * the routes. This is not quite the right
459 * thing to do, but at least if we are running
460 * a routing process they will come back.
461 */
462 in_ifadown(&ia->ia_ifa, 1);
463 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
464 error = 0;
465 break;
466
467 default:
468 if (ifp == 0 || ifp->if_ioctl == 0)
469 return (EOPNOTSUPP);
470 IFF_LOCKGIANT(ifp);
471 error = (*ifp->if_ioctl)(ifp, cmd, data);
472 IFF_UNLOCKGIANT(ifp);
473 return (error);
474 }
475
476 /*
477 * Protect from ipintr() traversing address list while we're modifying
478 * it.
479 */
480 s = splnet();
481 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
482 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
483 if (ia->ia_addr.sin_family == AF_INET)
484 LIST_REMOVE(ia, ia_hash);
485 IFAFREE(&ia->ia_ifa);
486 splx(s);
487
488 return (error);
489 }
490
491 /*
492 * SIOC[GAD]LIFADDR.
493 * SIOCGLIFADDR: get first address. (?!?)
494 * SIOCGLIFADDR with IFLR_PREFIX:
495 * get first address that matches the specified prefix.
496 * SIOCALIFADDR: add the specified address.
497 * SIOCALIFADDR with IFLR_PREFIX:
498 * EINVAL since we can't deduce hostid part of the address.
499 * SIOCDLIFADDR: delete the specified address.
500 * SIOCDLIFADDR with IFLR_PREFIX:
501 * delete the first address that matches the specified prefix.
502 * return values:
503 * EINVAL on invalid parameters
504 * EADDRNOTAVAIL on prefix match failed/specified address not found
505 * other values may be returned from in_ioctl()
506 */
507 static int
508 in_lifaddr_ioctl(so, cmd, data, ifp, td)
509 struct socket *so;
510 u_long cmd;
511 caddr_t data;
512 struct ifnet *ifp;
513 struct thread *td;
514 {
515 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
516 struct ifaddr *ifa;
517
518 /* sanity checks */
519 if (!data || !ifp) {
520 panic("invalid argument to in_lifaddr_ioctl");
521 /*NOTRECHED*/
522 }
523
524 switch (cmd) {
525 case SIOCGLIFADDR:
526 /* address must be specified on GET with IFLR_PREFIX */
527 if ((iflr->flags & IFLR_PREFIX) == 0)
528 break;
529 /*FALLTHROUGH*/
530 case SIOCALIFADDR:
531 case SIOCDLIFADDR:
532 /* address must be specified on ADD and DELETE */
533 if (iflr->addr.ss_family != AF_INET)
534 return EINVAL;
535 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
536 return EINVAL;
537 /* XXX need improvement */
538 if (iflr->dstaddr.ss_family
539 && iflr->dstaddr.ss_family != AF_INET)
540 return EINVAL;
541 if (iflr->dstaddr.ss_family
542 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
543 return EINVAL;
544 break;
545 default: /*shouldn't happen*/
546 return EOPNOTSUPP;
547 }
548 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
549 return EINVAL;
550
551 switch (cmd) {
552 case SIOCALIFADDR:
553 {
554 struct in_aliasreq ifra;
555
556 if (iflr->flags & IFLR_PREFIX)
557 return EINVAL;
558
559 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
560 bzero(&ifra, sizeof(ifra));
561 bcopy(iflr->iflr_name, ifra.ifra_name,
562 sizeof(ifra.ifra_name));
563
564 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
565
566 if (iflr->dstaddr.ss_family) { /*XXX*/
567 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
568 iflr->dstaddr.ss_len);
569 }
570
571 ifra.ifra_mask.sin_family = AF_INET;
572 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
573 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
574
575 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
576 }
577 case SIOCGLIFADDR:
578 case SIOCDLIFADDR:
579 {
580 struct in_ifaddr *ia;
581 struct in_addr mask, candidate, match;
582 struct sockaddr_in *sin;
583 int cmp;
584
585 bzero(&mask, sizeof(mask));
586 if (iflr->flags & IFLR_PREFIX) {
587 /* lookup a prefix rather than address. */
588 in_len2mask(&mask, iflr->prefixlen);
589
590 sin = (struct sockaddr_in *)&iflr->addr;
591 match.s_addr = sin->sin_addr.s_addr;
592 match.s_addr &= mask.s_addr;
593
594 /* if you set extra bits, that's wrong */
595 if (match.s_addr != sin->sin_addr.s_addr)
596 return EINVAL;
597
598 cmp = 1;
599 } else {
600 if (cmd == SIOCGLIFADDR) {
601 /* on getting an address, take the 1st match */
602 cmp = 0; /*XXX*/
603 } else {
604 /* on deleting an address, do exact match */
605 in_len2mask(&mask, 32);
606 sin = (struct sockaddr_in *)&iflr->addr;
607 match.s_addr = sin->sin_addr.s_addr;
608
609 cmp = 1;
610 }
611 }
612
613 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
614 if (ifa->ifa_addr->sa_family != AF_INET6)
615 continue;
616 if (!cmp)
617 break;
618 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
619 candidate.s_addr &= mask.s_addr;
620 if (candidate.s_addr == match.s_addr)
621 break;
622 }
623 if (!ifa)
624 return EADDRNOTAVAIL;
625 ia = (struct in_ifaddr *)ifa;
626
627 if (cmd == SIOCGLIFADDR) {
628 /* fill in the if_laddrreq structure */
629 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
630
631 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
632 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
633 ia->ia_dstaddr.sin_len);
634 } else
635 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
636
637 iflr->prefixlen =
638 in_mask2len(&ia->ia_sockmask.sin_addr);
639
640 iflr->flags = 0; /*XXX*/
641
642 return 0;
643 } else {
644 struct in_aliasreq ifra;
645
646 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
647 bzero(&ifra, sizeof(ifra));
648 bcopy(iflr->iflr_name, ifra.ifra_name,
649 sizeof(ifra.ifra_name));
650
651 bcopy(&ia->ia_addr, &ifra.ifra_addr,
652 ia->ia_addr.sin_len);
653 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
654 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
655 ia->ia_dstaddr.sin_len);
656 }
657 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
658 ia->ia_sockmask.sin_len);
659
660 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
661 ifp, td);
662 }
663 }
664 }
665
666 return EOPNOTSUPP; /*just for safety*/
667 }
668
669 /*
670 * Delete any existing route for an interface.
671 */
672 void
673 in_ifscrub(ifp, ia)
674 register struct ifnet *ifp;
675 register struct in_ifaddr *ia;
676 {
677 in_scrubprefix(ia);
678 }
679
680 /*
681 * Initialize an interface's internet address
682 * and routing table entry.
683 */
684 static int
685 in_ifinit(ifp, ia, sin, scrub)
686 register struct ifnet *ifp;
687 register struct in_ifaddr *ia;
688 struct sockaddr_in *sin;
689 int scrub;
690 {
691 register u_long i = ntohl(sin->sin_addr.s_addr);
692 struct sockaddr_in oldaddr;
693 int s = splimp(), flags = RTF_UP, error = 0;
694
695 oldaddr = ia->ia_addr;
696 if (oldaddr.sin_family == AF_INET)
697 LIST_REMOVE(ia, ia_hash);
698 ia->ia_addr = *sin;
699 if (ia->ia_addr.sin_family == AF_INET)
700 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
701 ia, ia_hash);
702 /*
703 * Give the interface a chance to initialize
704 * if this is its first address,
705 * and to validate the address if necessary.
706 */
707 if (ifp->if_ioctl) {
708 IFF_LOCKGIANT(ifp);
709 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
710 IFF_UNLOCKGIANT(ifp);
711 if (error) {
712 splx(s);
713 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
714 ia->ia_addr = oldaddr;
715 if (ia->ia_addr.sin_family == AF_INET)
716 LIST_INSERT_HEAD(INADDR_HASH(
717 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
718 return (error);
719 }
720 }
721 splx(s);
722 if (scrub) {
723 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
724 in_ifscrub(ifp, ia);
725 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
726 }
727 if (IN_CLASSA(i))
728 ia->ia_netmask = IN_CLASSA_NET;
729 else if (IN_CLASSB(i))
730 ia->ia_netmask = IN_CLASSB_NET;
731 else
732 ia->ia_netmask = IN_CLASSC_NET;
733 /*
734 * The subnet mask usually includes at least the standard network part,
735 * but may may be smaller in the case of supernetting.
736 * If it is set, we believe it.
737 */
738 if (ia->ia_subnetmask == 0) {
739 ia->ia_subnetmask = ia->ia_netmask;
740 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
741 } else
742 ia->ia_netmask &= ia->ia_subnetmask;
743 ia->ia_net = i & ia->ia_netmask;
744 ia->ia_subnet = i & ia->ia_subnetmask;
745 in_socktrim(&ia->ia_sockmask);
746 #ifdef DEV_CARP
747 /*
748 * XXX: carp(4) does not have interface route
749 */
750 if (ifp->if_type == IFT_CARP)
751 return (0);
752 #endif
753 /*
754 * Add route for the network.
755 */
756 ia->ia_ifa.ifa_metric = ifp->if_metric;
757 if (ifp->if_flags & IFF_BROADCAST) {
758 ia->ia_broadaddr.sin_addr.s_addr =
759 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
760 ia->ia_netbroadcast.s_addr =
761 htonl(ia->ia_net | ~ ia->ia_netmask);
762 } else if (ifp->if_flags & IFF_LOOPBACK) {
763 ia->ia_dstaddr = ia->ia_addr;
764 flags |= RTF_HOST;
765 } else if (ifp->if_flags & IFF_POINTOPOINT) {
766 if (ia->ia_dstaddr.sin_family != AF_INET)
767 return (0);
768 flags |= RTF_HOST;
769 }
770 if ((error = in_addprefix(ia, flags)) != 0)
771 return (error);
772
773 /*
774 * If the interface supports multicast, join the "all hosts"
775 * multicast group on that interface.
776 */
777 if (ifp->if_flags & IFF_MULTICAST) {
778 struct in_addr addr;
779
780 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
781 in_addmulti(&addr, ifp);
782 }
783 return (error);
784 }
785
786 #define rtinitflags(x) \
787 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
788 ? RTF_HOST : 0)
789 /*
790 * Check if we have a route for the given prefix already or add a one
791 * accordingly.
792 */
793 static int
794 in_addprefix(target, flags)
795 struct in_ifaddr *target;
796 int flags;
797 {
798 struct in_ifaddr *ia;
799 struct in_addr prefix, mask, p, m;
800 int error;
801
802 if ((flags & RTF_HOST) != 0)
803 prefix = target->ia_dstaddr.sin_addr;
804 else {
805 prefix = target->ia_addr.sin_addr;
806 mask = target->ia_sockmask.sin_addr;
807 prefix.s_addr &= mask.s_addr;
808 }
809
810 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
811 if (rtinitflags(ia)) {
812 p = ia->ia_addr.sin_addr;
813
814 if (prefix.s_addr != p.s_addr)
815 continue;
816 } else {
817 p = ia->ia_addr.sin_addr;
818 m = ia->ia_sockmask.sin_addr;
819 p.s_addr &= m.s_addr;
820
821 if (prefix.s_addr != p.s_addr ||
822 mask.s_addr != m.s_addr)
823 continue;
824 }
825
826 /*
827 * If we got a matching prefix route inserted by other
828 * interface address, we are done here.
829 */
830 if (ia->ia_flags & IFA_ROUTE) {
831 if (sameprefixcarponly &&
832 target->ia_ifp->if_type != IFT_CARP &&
833 ia->ia_ifp->if_type != IFT_CARP)
834 return (EEXIST);
835 else
836 return (0);
837 }
838 }
839
840 /*
841 * No-one seem to have this prefix route, so we try to insert it.
842 */
843 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
844 if (!error)
845 target->ia_flags |= IFA_ROUTE;
846 return error;
847 }
848
849 /*
850 * If there is no other address in the system that can serve a route to the
851 * same prefix, remove the route. Hand over the route to the new address
852 * otherwise.
853 */
854 static int
855 in_scrubprefix(target)
856 struct in_ifaddr *target;
857 {
858 struct in_ifaddr *ia;
859 struct in_addr prefix, mask, p;
860 int error;
861
862 if ((target->ia_flags & IFA_ROUTE) == 0)
863 return 0;
864
865 if (rtinitflags(target))
866 prefix = target->ia_dstaddr.sin_addr;
867 else {
868 prefix = target->ia_addr.sin_addr;
869 mask = target->ia_sockmask.sin_addr;
870 prefix.s_addr &= mask.s_addr;
871 }
872
873 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
874 if (rtinitflags(ia))
875 p = ia->ia_dstaddr.sin_addr;
876 else {
877 p = ia->ia_addr.sin_addr;
878 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
879 }
880
881 if (prefix.s_addr != p.s_addr)
882 continue;
883
884 /*
885 * If we got a matching prefix address, move IFA_ROUTE and
886 * the route itself to it. Make sure that routing daemons
887 * get a heads-up.
888 *
889 * XXX: a special case for carp(4) interface
890 */
891 if ((ia->ia_flags & IFA_ROUTE) == 0
892 #ifdef DEV_CARP
893 && (ia->ia_ifp->if_type != IFT_CARP)
894 #endif
895 ) {
896 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
897 rtinitflags(target));
898 target->ia_flags &= ~IFA_ROUTE;
899
900 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
901 rtinitflags(ia) | RTF_UP);
902 if (error == 0)
903 ia->ia_flags |= IFA_ROUTE;
904 return error;
905 }
906 }
907
908 /*
909 * As no-one seem to have this prefix, we can remove the route.
910 */
911 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
912 target->ia_flags &= ~IFA_ROUTE;
913 return 0;
914 }
915
916 #undef rtinitflags
917
918 /*
919 * Return 1 if the address might be a local broadcast address.
920 */
921 int
922 in_broadcast(in, ifp)
923 struct in_addr in;
924 struct ifnet *ifp;
925 {
926 register struct ifaddr *ifa;
927 u_long t;
928
929 if (in.s_addr == INADDR_BROADCAST ||
930 in.s_addr == INADDR_ANY)
931 return 1;
932 if ((ifp->if_flags & IFF_BROADCAST) == 0)
933 return 0;
934 t = ntohl(in.s_addr);
935 /*
936 * Look through the list of addresses for a match
937 * with a broadcast address.
938 */
939 #define ia ((struct in_ifaddr *)ifa)
940 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
941 if (ifa->ifa_addr->sa_family == AF_INET &&
942 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
943 in.s_addr == ia->ia_netbroadcast.s_addr ||
944 /*
945 * Check for old-style (host 0) broadcast.
946 */
947 t == ia->ia_subnet || t == ia->ia_net) &&
948 /*
949 * Check for an all one subnetmask. These
950 * only exist when an interface gets a secondary
951 * address.
952 */
953 ia->ia_subnetmask != (u_long)0xffffffff)
954 return 1;
955 return (0);
956 #undef ia
957 }
958 /*
959 * Add an address to the list of IP multicast addresses for a given interface.
960 */
961 struct in_multi *
962 in_addmulti(ap, ifp)
963 register struct in_addr *ap;
964 register struct ifnet *ifp;
965 {
966 register struct in_multi *inm;
967 int error;
968 struct sockaddr_in sin;
969 struct ifmultiaddr *ifma;
970
971 IFF_LOCKGIANT(ifp);
972 IN_MULTI_LOCK();
973 /*
974 * Call generic routine to add membership or increment
975 * refcount. It wants addresses in the form of a sockaddr,
976 * so we build one here (being careful to zero the unused bytes).
977 */
978 bzero(&sin, sizeof sin);
979 sin.sin_family = AF_INET;
980 sin.sin_len = sizeof sin;
981 sin.sin_addr = *ap;
982 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
983 if (error) {
984 IN_MULTI_UNLOCK();
985 IFF_UNLOCKGIANT(ifp);
986 return 0;
987 }
988
989 /*
990 * If ifma->ifma_protospec is null, then if_addmulti() created
991 * a new record. Otherwise, we are done.
992 */
993 if (ifma->ifma_protospec != NULL) {
994 if_delmulti_ent(ifma); /* We don't need another reference */
995 IN_MULTI_UNLOCK();
996 IFF_UNLOCKGIANT(ifp);
997 return ifma->ifma_protospec;
998 }
999
1000 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
1001 M_NOWAIT | M_ZERO);
1002 if (inm == NULL) {
1003 if_delmulti_ent(ifma);
1004 IN_MULTI_UNLOCK();
1005 IFF_UNLOCKGIANT(ifp);
1006 return (NULL);
1007 }
1008
1009 inm->inm_addr = *ap;
1010 inm->inm_ifp = ifp;
1011 inm->inm_ifma = ifma;
1012 ifma->ifma_protospec = inm;
1013 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1014
1015 /*
1016 * Let IGMP know that we have joined a new IP multicast group.
1017 */
1018 igmp_joingroup(inm);
1019 IN_MULTI_UNLOCK();
1020 IFF_UNLOCKGIANT(ifp);
1021 return (inm);
1022 }
1023
1024 static void
1025 in_delmulti_locked(inm, all)
1026 register struct in_multi *inm;
1027 int all;
1028 {
1029 struct ifmultiaddr *ifma;
1030 struct in_multi my_inm;
1031
1032 ifma = inm->inm_ifma;
1033 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1034 if (all)
1035 while (ifma->ifma_refcount > 1)
1036 if_delmulti_ent(ifma);
1037 if (ifma->ifma_refcount == 1) {
1038 /*
1039 * No remaining claims to this record; let IGMP know that
1040 * we are leaving the multicast group.
1041 * But do it after the if_delmulti() which might reset
1042 * the interface and nuke the packet.
1043 */
1044 my_inm = *inm ;
1045 ifma->ifma_protospec = NULL;
1046 LIST_REMOVE(inm, inm_link);
1047 free(inm, M_IPMADDR);
1048 }
1049 if_delmulti_ent(ifma);
1050 if (my_inm.inm_ifp != NULL)
1051 igmp_leavegroup(&my_inm);
1052 }
1053
1054 /*
1055 * Delete a multicast address record.
1056 */
1057 void
1058 in_delmulti(inm)
1059 register struct in_multi *inm;
1060 {
1061 struct ifnet *ifp;
1062
1063 ifp = inm->inm_ifp;
1064 IFF_LOCKGIANT(ifp);
1065 IN_MULTI_LOCK();
1066 in_delmulti_locked(inm, 0);
1067 IN_MULTI_UNLOCK();
1068 IFF_UNLOCKGIANT(ifp);
1069 }
1070
1071 /*
1072 * Delete all multicast address records associated with the ifp.
1073 */
1074 void
1075 in_delmulti_ifp(ifp)
1076 register struct ifnet *ifp;
1077 {
1078 struct in_multi *inm;
1079 struct in_multi *oinm;
1080
1081 IFF_LOCKGIANT(ifp);
1082 IN_MULTI_LOCK();
1083 LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
1084 if (inm->inm_ifp == ifp)
1085 in_delmulti_locked(inm, 1);
1086 }
1087 IN_MULTI_UNLOCK();
1088 IFF_UNLOCKGIANT(ifp);
1089 }
1090
1091 /*
1092 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1093 */
1094 void
1095 in_ifdetach(ifp)
1096 struct ifnet *ifp;
1097 {
1098
1099 in_pcbpurgeif0(&ripcbinfo, ifp);
1100 in_pcbpurgeif0(&udbinfo, ifp);
1101 in_delmulti_ifp(ifp);
1102 }
Cache object: ae1eece7805c352a5b729656e0350893
|