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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 * @(#)in.c 8.4 (Berkeley) 1/9/95
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
39 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
46 #include <net/if_types.h>
47 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/in_pcb.h>
53 #include <netinet/igmp_var.h>
55 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
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 *);
62 static int in_addprefix(struct in_ifaddr *, 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 static void in_purgemaddrs(struct ifnet *);
69 static int subnetsarelocal = 0;
70 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
71 &subnetsarelocal, 0, "Treat all subnets as directly connected");
72 static int sameprefixcarponly = 0;
73 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
74 &sameprefixcarponly, 0,
75 "Refuse to create same prefixes on different interfaces");
78 * The IPv4 multicast list (in_multihead and associated structures) are
79 * protected by the global in_multi_mtx. See in_var.h for more details. For
80 * now, in_multi_mtx is marked as recursible due to IGMP's calling back into
81 * ip_output() to send IGMP packets while holding the lock; this probably is
82 * not quite desirable.
84 struct in_multihead in_multihead; /* XXX BSS initialization */
85 struct mtx in_multi_mtx;
86 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE);
88 extern struct inpcbinfo ripcbinfo;
89 extern struct inpcbinfo udbinfo;
92 * Return 1 if an internet address is for a ``local'' host
93 * (one to which we have a connection). If subnetsarelocal
94 * is true, this includes other subnets of the local net.
95 * Otherwise, it includes only the directly-connected (sub)nets.
98 in_localaddr(struct in_addr in)
100 register u_long i = ntohl(in.s_addr);
101 register struct in_ifaddr *ia;
103 if (subnetsarelocal) {
104 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
105 if ((i & ia->ia_netmask) == ia->ia_net)
108 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
109 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
116 * Return 1 if an internet address is for the local host and configured
117 * on one of its interfaces.
120 in_localip(struct in_addr in)
122 struct in_ifaddr *ia;
124 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
125 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
132 * Determine whether an IP address is in a reserved set of addresses
133 * that may not be forwarded, or whether datagrams to that destination
137 in_canforward(struct in_addr in)
139 register u_long i = ntohl(in.s_addr);
142 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
145 net = i & IN_CLASSA_NET;
146 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
153 * Trim a mask in a sockaddr
156 in_socktrim(struct sockaddr_in *ap)
158 register char *cplim = (char *) &ap->sin_addr;
159 register char *cp = (char *) (&ap->sin_addr + 1);
162 while (--cp >= cplim)
164 (ap)->sin_len = cp - (char *) (ap) + 1;
171 struct in_addr *mask;
177 for (x = 0; x < sizeof(*mask); x++) {
182 if (x < sizeof(*mask)) {
183 for (y = 0; y < 8; y++) {
184 if ((p[x] & (0x80 >> y)) == 0)
192 in_len2mask(struct in_addr *mask, int len)
198 bzero(mask, sizeof(*mask));
199 for (i = 0; i < len / 8; i++)
202 p[i] = (0xff00 >> (len % 8)) & 0xff;
206 * Generic internet control operations (ioctl's).
207 * Ifp is 0 if not an interface-specific ioctl.
211 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
214 register struct ifreq *ifr = (struct ifreq *)data;
215 register struct in_ifaddr *ia = 0, *iap;
216 register struct ifaddr *ifa;
217 struct in_addr allhosts_addr;
219 struct in_ifaddr *oia;
220 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
221 struct sockaddr_in oldaddr;
222 int error, hostIsNew, iaIsNew, maskIsNew, s;
227 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
232 error = priv_check(td, PRIV_NET_ADDIFADDR);
238 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
242 error = priv_check(td, PRIV_NET_DELIFADDR);
248 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
253 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
257 * Find address for this interface, if it exists.
259 * If an alias address was specified, find that one instead of
260 * the first one on the interface, if possible.
263 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
264 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
265 if (iap->ia_ifp == ifp &&
266 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
271 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
273 if (iap->ia_addr.sin_family == AF_INET) {
287 return (EADDRNOTAVAIL);
288 if (ifra->ifra_addr.sin_family == AF_INET) {
289 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
290 if (ia->ia_ifp == ifp &&
291 ia->ia_addr.sin_addr.s_addr ==
292 ifra->ifra_addr.sin_addr.s_addr)
295 if ((ifp->if_flags & IFF_POINTOPOINT)
296 && (cmd == SIOCAIFADDR)
297 && (ifra->ifra_dstaddr.sin_addr.s_addr
302 if (cmd == SIOCDIFADDR && ia == 0)
303 return (EADDRNOTAVAIL);
309 error = priv_check(td, PRIV_NET_ADDIFADDR);
315 return (EADDRNOTAVAIL);
316 if (ia == (struct in_ifaddr *)0) {
317 ia = (struct in_ifaddr *)
318 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
319 if (ia == (struct in_ifaddr *)NULL)
322 * Protect from ipintr() traversing address list
323 * while we're modifying it.
328 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
329 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
330 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
332 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
334 ia->ia_sockmask.sin_len = 8;
335 ia->ia_sockmask.sin_family = AF_INET;
336 if (ifp->if_flags & IFF_BROADCAST) {
337 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
338 ia->ia_broadaddr.sin_family = AF_INET;
342 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
350 error = priv_check(td, PRIV_NET_ADDIFADDR);
360 if (ia == (struct in_ifaddr *)0)
361 return (EADDRNOTAVAIL);
367 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
371 if ((ifp->if_flags & IFF_BROADCAST) == 0)
373 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
377 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
379 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
383 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
387 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
389 oldaddr = ia->ia_dstaddr;
390 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
393 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
395 IFF_UNLOCKGIANT(ifp);
397 ia->ia_dstaddr = oldaddr;
401 if (ia->ia_flags & IFA_ROUTE) {
402 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
403 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
404 ia->ia_ifa.ifa_dstaddr =
405 (struct sockaddr *)&ia->ia_dstaddr;
406 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
411 if ((ifp->if_flags & IFF_BROADCAST) == 0)
413 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
417 error = in_ifinit(ifp, ia,
418 (struct sockaddr_in *) &ifr->ifr_addr, 1);
419 if (error != 0 && iaIsNew)
422 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
423 in_addmulti(&allhosts_addr, ifp);
424 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
429 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
430 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
437 if (ia->ia_addr.sin_family == AF_INET) {
438 if (ifra->ifra_addr.sin_len == 0) {
439 ifra->ifra_addr = ia->ia_addr;
441 } else if (ifra->ifra_addr.sin_addr.s_addr ==
442 ia->ia_addr.sin_addr.s_addr)
445 if (ifra->ifra_mask.sin_len) {
447 ia->ia_sockmask = ifra->ifra_mask;
448 ia->ia_sockmask.sin_family = AF_INET;
450 ntohl(ia->ia_sockmask.sin_addr.s_addr);
453 if ((ifp->if_flags & IFF_POINTOPOINT) &&
454 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
456 ia->ia_dstaddr = ifra->ifra_dstaddr;
457 maskIsNew = 1; /* We lie; but the effect's the same */
459 if (ifra->ifra_addr.sin_family == AF_INET &&
460 (hostIsNew || maskIsNew))
461 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
462 if (error != 0 && iaIsNew)
465 if ((ifp->if_flags & IFF_BROADCAST) &&
466 (ifra->ifra_broadaddr.sin_family == AF_INET))
467 ia->ia_broadaddr = ifra->ifra_broadaddr;
469 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
470 in_addmulti(&allhosts_addr, ifp);
471 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
477 * in_ifscrub kills the interface route.
481 * in_ifadown gets rid of all the rest of
482 * the routes. This is not quite the right
483 * thing to do, but at least if we are running
484 * a routing process they will come back.
486 in_ifadown(&ia->ia_ifa, 1);
487 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
492 if (ifp == 0 || ifp->if_ioctl == 0)
495 error = (*ifp->if_ioctl)(ifp, cmd, data);
496 IFF_UNLOCKGIANT(ifp);
501 * Protect from ipintr() traversing address list while we're modifying
505 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
506 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
507 if (ia->ia_addr.sin_family == AF_INET) {
508 LIST_REMOVE(ia, ia_hash);
510 * If this is the last IPv4 address configured on this
511 * interface, leave the all-hosts group.
512 * XXX: This is quite ugly because of locking and structure.
517 struct in_multi *inm;
521 IN_LOOKUP_MULTI(allhosts_addr, ifp, inm);
523 in_delmulti_locked(inm);
525 IFF_UNLOCKGIANT(ifp);
528 IFAFREE(&ia->ia_ifa);
536 * SIOCGLIFADDR: get first address. (?!?)
537 * SIOCGLIFADDR with IFLR_PREFIX:
538 * get first address that matches the specified prefix.
539 * SIOCALIFADDR: add the specified address.
540 * SIOCALIFADDR with IFLR_PREFIX:
541 * EINVAL since we can't deduce hostid part of the address.
542 * SIOCDLIFADDR: delete the specified address.
543 * SIOCDLIFADDR with IFLR_PREFIX:
544 * delete the first address that matches the specified prefix.
546 * EINVAL on invalid parameters
547 * EADDRNOTAVAIL on prefix match failed/specified address not found
548 * other values may be returned from in_ioctl()
551 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
552 struct ifnet *ifp, struct thread *td)
554 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
559 panic("invalid argument to in_lifaddr_ioctl");
565 /* address must be specified on GET with IFLR_PREFIX */
566 if ((iflr->flags & IFLR_PREFIX) == 0)
571 /* address must be specified on ADD and DELETE */
572 if (iflr->addr.ss_family != AF_INET)
574 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
576 /* XXX need improvement */
577 if (iflr->dstaddr.ss_family
578 && iflr->dstaddr.ss_family != AF_INET)
580 if (iflr->dstaddr.ss_family
581 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
584 default: /*shouldn't happen*/
587 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
593 struct in_aliasreq ifra;
595 if (iflr->flags & IFLR_PREFIX)
598 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
599 bzero(&ifra, sizeof(ifra));
600 bcopy(iflr->iflr_name, ifra.ifra_name,
601 sizeof(ifra.ifra_name));
603 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
605 if (iflr->dstaddr.ss_family) { /*XXX*/
606 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
607 iflr->dstaddr.ss_len);
610 ifra.ifra_mask.sin_family = AF_INET;
611 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
612 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
614 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
619 struct in_ifaddr *ia;
620 struct in_addr mask, candidate, match;
621 struct sockaddr_in *sin;
624 bzero(&mask, sizeof(mask));
625 if (iflr->flags & IFLR_PREFIX) {
626 /* lookup a prefix rather than address. */
627 in_len2mask(&mask, iflr->prefixlen);
629 sin = (struct sockaddr_in *)&iflr->addr;
630 match.s_addr = sin->sin_addr.s_addr;
631 match.s_addr &= mask.s_addr;
633 /* if you set extra bits, that's wrong */
634 if (match.s_addr != sin->sin_addr.s_addr)
639 if (cmd == SIOCGLIFADDR) {
640 /* on getting an address, take the 1st match */
643 /* on deleting an address, do exact match */
644 in_len2mask(&mask, 32);
645 sin = (struct sockaddr_in *)&iflr->addr;
646 match.s_addr = sin->sin_addr.s_addr;
652 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
653 if (ifa->ifa_addr->sa_family != AF_INET6)
657 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
658 candidate.s_addr &= mask.s_addr;
659 if (candidate.s_addr == match.s_addr)
663 return EADDRNOTAVAIL;
664 ia = (struct in_ifaddr *)ifa;
666 if (cmd == SIOCGLIFADDR) {
667 /* fill in the if_laddrreq structure */
668 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
670 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
671 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
672 ia->ia_dstaddr.sin_len);
674 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
677 in_mask2len(&ia->ia_sockmask.sin_addr);
679 iflr->flags = 0; /*XXX*/
683 struct in_aliasreq ifra;
685 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
686 bzero(&ifra, sizeof(ifra));
687 bcopy(iflr->iflr_name, ifra.ifra_name,
688 sizeof(ifra.ifra_name));
690 bcopy(&ia->ia_addr, &ifra.ifra_addr,
691 ia->ia_addr.sin_len);
692 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
693 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
694 ia->ia_dstaddr.sin_len);
696 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
697 ia->ia_sockmask.sin_len);
699 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
705 return EOPNOTSUPP; /*just for safety*/
709 * Delete any existing route for an interface.
712 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
719 * Initialize an interface's internet address
720 * and routing table entry.
723 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
726 register u_long i = ntohl(sin->sin_addr.s_addr);
727 struct sockaddr_in oldaddr;
728 int s = splimp(), flags = RTF_UP, error = 0;
730 oldaddr = ia->ia_addr;
731 if (oldaddr.sin_family == AF_INET)
732 LIST_REMOVE(ia, ia_hash);
734 if (ia->ia_addr.sin_family == AF_INET)
735 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
738 * Give the interface a chance to initialize
739 * if this is its first address,
740 * and to validate the address if necessary.
744 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
745 IFF_UNLOCKGIANT(ifp);
748 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
749 ia->ia_addr = oldaddr;
750 if (ia->ia_addr.sin_family == AF_INET)
751 LIST_INSERT_HEAD(INADDR_HASH(
752 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
758 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
760 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
763 ia->ia_netmask = IN_CLASSA_NET;
764 else if (IN_CLASSB(i))
765 ia->ia_netmask = IN_CLASSB_NET;
767 ia->ia_netmask = IN_CLASSC_NET;
769 * The subnet mask usually includes at least the standard network part,
770 * but may may be smaller in the case of supernetting.
771 * If it is set, we believe it.
773 if (ia->ia_subnetmask == 0) {
774 ia->ia_subnetmask = ia->ia_netmask;
775 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
777 ia->ia_netmask &= ia->ia_subnetmask;
778 ia->ia_net = i & ia->ia_netmask;
779 ia->ia_subnet = i & ia->ia_subnetmask;
780 in_socktrim(&ia->ia_sockmask);
783 * XXX: carp(4) does not have interface route
785 if (ifp->if_type == IFT_CARP)
789 * Add route for the network.
791 ia->ia_ifa.ifa_metric = ifp->if_metric;
792 if (ifp->if_flags & IFF_BROADCAST) {
793 ia->ia_broadaddr.sin_addr.s_addr =
794 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
795 ia->ia_netbroadcast.s_addr =
796 htonl(ia->ia_net | ~ ia->ia_netmask);
797 } else if (ifp->if_flags & IFF_LOOPBACK) {
798 ia->ia_dstaddr = ia->ia_addr;
800 } else if (ifp->if_flags & IFF_POINTOPOINT) {
801 if (ia->ia_dstaddr.sin_family != AF_INET)
805 if ((error = in_addprefix(ia, flags)) != 0)
811 #define rtinitflags(x) \
812 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
815 * Check if we have a route for the given prefix already or add a one
819 in_addprefix(struct in_ifaddr *target, int flags)
821 struct in_ifaddr *ia;
822 struct in_addr prefix, mask, p, m;
825 if ((flags & RTF_HOST) != 0)
826 prefix = target->ia_dstaddr.sin_addr;
828 prefix = target->ia_addr.sin_addr;
829 mask = target->ia_sockmask.sin_addr;
830 prefix.s_addr &= mask.s_addr;
833 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
834 if (rtinitflags(ia)) {
835 p = ia->ia_addr.sin_addr;
837 if (prefix.s_addr != p.s_addr)
840 p = ia->ia_addr.sin_addr;
841 m = ia->ia_sockmask.sin_addr;
842 p.s_addr &= m.s_addr;
844 if (prefix.s_addr != p.s_addr ||
845 mask.s_addr != m.s_addr)
850 * If we got a matching prefix route inserted by other
851 * interface address, we are done here.
853 if (ia->ia_flags & IFA_ROUTE) {
854 if (sameprefixcarponly &&
855 target->ia_ifp->if_type != IFT_CARP &&
856 ia->ia_ifp->if_type != IFT_CARP)
864 * No-one seem to have this prefix route, so we try to insert it.
866 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
868 target->ia_flags |= IFA_ROUTE;
873 * If there is no other address in the system that can serve a route to the
874 * same prefix, remove the route. Hand over the route to the new address
878 in_scrubprefix(struct in_ifaddr *target)
880 struct in_ifaddr *ia;
881 struct in_addr prefix, mask, p;
884 if ((target->ia_flags & IFA_ROUTE) == 0)
887 if (rtinitflags(target))
888 prefix = target->ia_dstaddr.sin_addr;
890 prefix = target->ia_addr.sin_addr;
891 mask = target->ia_sockmask.sin_addr;
892 prefix.s_addr &= mask.s_addr;
895 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
897 p = ia->ia_dstaddr.sin_addr;
899 p = ia->ia_addr.sin_addr;
900 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
903 if (prefix.s_addr != p.s_addr)
907 * If we got a matching prefix address, move IFA_ROUTE and
908 * the route itself to it. Make sure that routing daemons
911 * XXX: a special case for carp(4) interface
913 if ((ia->ia_flags & IFA_ROUTE) == 0
915 && (ia->ia_ifp->if_type != IFT_CARP)
918 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
919 rtinitflags(target));
920 target->ia_flags &= ~IFA_ROUTE;
922 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
923 rtinitflags(ia) | RTF_UP);
925 ia->ia_flags |= IFA_ROUTE;
931 * As no-one seem to have this prefix, we can remove the route.
933 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
934 target->ia_flags &= ~IFA_ROUTE;
941 * Return 1 if the address might be a local broadcast address.
944 in_broadcast(struct in_addr in, struct ifnet *ifp)
946 register struct ifaddr *ifa;
949 if (in.s_addr == INADDR_BROADCAST ||
950 in.s_addr == INADDR_ANY)
952 if ((ifp->if_flags & IFF_BROADCAST) == 0)
954 t = ntohl(in.s_addr);
956 * Look through the list of addresses for a match
957 * with a broadcast address.
959 #define ia ((struct in_ifaddr *)ifa)
960 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
961 if (ifa->ifa_addr->sa_family == AF_INET &&
962 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
963 in.s_addr == ia->ia_netbroadcast.s_addr ||
965 * Check for old-style (host 0) broadcast.
967 t == ia->ia_subnet || t == ia->ia_net) &&
969 * Check for an all one subnetmask. These
970 * only exist when an interface gets a secondary
973 ia->ia_subnetmask != (u_long)0xffffffff)
980 * Add an address to the list of IP multicast addresses for a given interface.
983 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
985 struct in_multi *inm;
992 IN_LOOKUP_MULTI(*ap, ifp, inm);
995 * If we already joined this group, just bump the
996 * refcount and return it.
998 KASSERT(inm->inm_refcount >= 1,
999 ("%s: bad refcount %d", __func__, inm->inm_refcount));
1000 ++inm->inm_refcount;
1002 struct sockaddr_in sin;
1003 struct ifmultiaddr *ifma;
1004 struct in_multi *ninm;
1007 bzero(&sin, sizeof sin);
1008 sin.sin_family = AF_INET;
1009 sin.sin_len = sizeof(struct sockaddr_in);
1013 * Check if a link-layer group is already associated
1014 * with this network-layer group on the given ifnet.
1015 * If so, bump the refcount on the existing network-layer
1016 * group association and return it.
1018 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1021 if (ifma->ifma_protospec != NULL) {
1022 inm = (struct in_multi *)ifma->ifma_protospec;
1024 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
1025 inm->inm_addr.s_addr != ap->s_addr)
1026 panic("%s: ifma is inconsistent", __func__);
1028 ++inm->inm_refcount;
1033 * A new membership is needed; construct it and
1034 * perform the IGMP join.
1036 ninm = malloc(sizeof(*ninm), M_IPMADDR, M_NOWAIT | M_ZERO);
1038 if_delmulti_ifma(ifma);
1041 ninm->inm_addr = *ap;
1042 ninm->inm_ifp = ifp;
1043 ninm->inm_ifma = ifma;
1044 ninm->inm_refcount = 1;
1045 ifma->ifma_protospec = ninm;
1046 LIST_INSERT_HEAD(&in_multihead, ninm, inm_link);
1048 igmp_joingroup(ninm);
1054 IFF_UNLOCKGIANT(ifp);
1060 * Delete a multicast address record.
1061 * It is OK to call this routine if the underlying ifnet went away.
1063 * XXX: To deal with the ifp going away, we cheat; the link-layer code in net
1064 * will set ifma_ifp to NULL when the associated ifnet instance is detached
1066 * The only reason we need to violate layers and check ifma_ifp here at all
1067 * is because certain hardware drivers still require Giant to be held,
1068 * and it must always be taken before other locks.
1071 in_delmulti(struct in_multi *inm)
1075 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
1076 ifp = inm->inm_ifma->ifma_ifp;
1080 * Sanity check that netinet's notion of ifp is the
1083 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
1088 in_delmulti_locked(inm);
1092 IFF_UNLOCKGIANT(ifp);
1096 * Delete a multicast address record, with locks held.
1098 * It is OK to call this routine if the ifp went away.
1099 * Assumes that caller holds the IN_MULTI lock, and that
1100 * Giant was taken before other locks if required by the hardware.
1103 in_delmulti_locked(struct in_multi *inm)
1105 struct ifmultiaddr *ifma;
1107 IN_MULTI_LOCK_ASSERT();
1108 KASSERT(inm->inm_refcount >= 1, ("%s: freeing freed inm", __func__));
1110 if (--inm->inm_refcount == 0) {
1111 igmp_leavegroup(inm);
1113 ifma = inm->inm_ifma;
1115 printf("%s: purging ifma %p\n", __func__, ifma);
1117 KASSERT(ifma->ifma_protospec == inm,
1118 ("%s: ifma_protospec != inm", __func__));
1119 ifma->ifma_protospec = NULL;
1121 LIST_REMOVE(inm, inm_link);
1122 free(inm, M_IPMADDR);
1124 if_delmulti_ifma(ifma);
1129 * Delete all IPv4 multicast address records, and associated link-layer
1130 * multicast address records, associated with ifp.
1133 in_purgemaddrs(struct ifnet *ifp)
1135 struct in_multi *inm;
1136 struct in_multi *oinm;
1139 printf("%s: purging ifp %p\n", __func__, ifp);
1143 LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
1144 if (inm->inm_ifp == ifp)
1145 in_delmulti_locked(inm);
1148 IFF_UNLOCKGIANT(ifp);
1152 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1155 in_ifdetach(struct ifnet *ifp)
1158 in_pcbpurgeif0(&ripcbinfo, ifp);
1159 in_pcbpurgeif0(&udbinfo, ifp);
1160 in_purgemaddrs(ifp);