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.
101 register u_long i = ntohl(in.s_addr);
102 register struct in_ifaddr *ia;
104 if (subnetsarelocal) {
105 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
106 if ((i & ia->ia_netmask) == ia->ia_net)
109 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
110 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
117 * Return 1 if an internet address is for the local host and configured
118 * on one of its interfaces.
124 struct in_ifaddr *ia;
126 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
127 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
134 * Determine whether an IP address is in a reserved set of addresses
135 * that may not be forwarded, or whether datagrams to that destination
142 register u_long i = ntohl(in.s_addr);
145 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
148 net = i & IN_CLASSA_NET;
149 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
156 * Trim a mask in a sockaddr
160 struct sockaddr_in *ap;
162 register char *cplim = (char *) &ap->sin_addr;
163 register char *cp = (char *) (&ap->sin_addr + 1);
166 while (--cp >= cplim)
168 (ap)->sin_len = cp - (char *) (ap) + 1;
175 struct in_addr *mask;
181 for (x = 0; x < sizeof(*mask); x++) {
186 if (x < sizeof(*mask)) {
187 for (y = 0; y < 8; y++) {
188 if ((p[x] & (0x80 >> y)) == 0)
196 in_len2mask(mask, len)
197 struct in_addr *mask;
204 bzero(mask, sizeof(*mask));
205 for (i = 0; i < len / 8; i++)
208 p[i] = (0xff00 >> (len % 8)) & 0xff;
212 * Generic internet control operations (ioctl's).
213 * Ifp is 0 if not an interface-specific ioctl.
217 in_control(so, cmd, data, ifp, td)
221 register struct ifnet *ifp;
224 register struct ifreq *ifr = (struct ifreq *)data;
225 register struct in_ifaddr *ia = 0, *iap;
226 register struct ifaddr *ifa;
227 struct in_addr allhosts_addr;
229 struct in_ifaddr *oia;
230 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
231 struct sockaddr_in oldaddr;
232 int error, hostIsNew, iaIsNew, maskIsNew, s;
237 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
242 error = priv_check(td, PRIV_NET_ADDIFADDR);
248 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
252 error = priv_check(td, PRIV_NET_DELIFADDR);
258 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
263 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
267 * Find address for this interface, if it exists.
269 * If an alias address was specified, find that one instead of
270 * the first one on the interface, if possible.
273 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
274 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
275 if (iap->ia_ifp == ifp &&
276 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
281 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
283 if (iap->ia_addr.sin_family == AF_INET) {
297 return (EADDRNOTAVAIL);
298 if (ifra->ifra_addr.sin_family == AF_INET) {
299 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
300 if (ia->ia_ifp == ifp &&
301 ia->ia_addr.sin_addr.s_addr ==
302 ifra->ifra_addr.sin_addr.s_addr)
305 if ((ifp->if_flags & IFF_POINTOPOINT)
306 && (cmd == SIOCAIFADDR)
307 && (ifra->ifra_dstaddr.sin_addr.s_addr
312 if (cmd == SIOCDIFADDR && ia == 0)
313 return (EADDRNOTAVAIL);
319 error = priv_check(td, PRIV_NET_ADDIFADDR);
325 return (EADDRNOTAVAIL);
326 if (ia == (struct in_ifaddr *)0) {
327 ia = (struct in_ifaddr *)
328 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
329 if (ia == (struct in_ifaddr *)NULL)
332 * Protect from ipintr() traversing address list
333 * while we're modifying it.
338 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
339 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
340 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
342 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
344 ia->ia_sockmask.sin_len = 8;
345 ia->ia_sockmask.sin_family = AF_INET;
346 if (ifp->if_flags & IFF_BROADCAST) {
347 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
348 ia->ia_broadaddr.sin_family = AF_INET;
352 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
360 error = priv_check(td, PRIV_NET_ADDIFADDR);
370 if (ia == (struct in_ifaddr *)0)
371 return (EADDRNOTAVAIL);
377 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
381 if ((ifp->if_flags & IFF_BROADCAST) == 0)
383 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
387 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
389 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
393 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
397 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
399 oldaddr = ia->ia_dstaddr;
400 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
403 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
405 IFF_UNLOCKGIANT(ifp);
407 ia->ia_dstaddr = oldaddr;
411 if (ia->ia_flags & IFA_ROUTE) {
412 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
413 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
414 ia->ia_ifa.ifa_dstaddr =
415 (struct sockaddr *)&ia->ia_dstaddr;
416 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
421 if ((ifp->if_flags & IFF_BROADCAST) == 0)
423 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
427 error = in_ifinit(ifp, ia,
428 (struct sockaddr_in *) &ifr->ifr_addr, 1);
429 if (error != 0 && iaIsNew)
432 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
433 in_addmulti(&allhosts_addr, ifp);
434 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
439 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
440 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
447 if (ia->ia_addr.sin_family == AF_INET) {
448 if (ifra->ifra_addr.sin_len == 0) {
449 ifra->ifra_addr = ia->ia_addr;
451 } else if (ifra->ifra_addr.sin_addr.s_addr ==
452 ia->ia_addr.sin_addr.s_addr)
455 if (ifra->ifra_mask.sin_len) {
457 ia->ia_sockmask = ifra->ifra_mask;
458 ia->ia_sockmask.sin_family = AF_INET;
460 ntohl(ia->ia_sockmask.sin_addr.s_addr);
463 if ((ifp->if_flags & IFF_POINTOPOINT) &&
464 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
466 ia->ia_dstaddr = ifra->ifra_dstaddr;
467 maskIsNew = 1; /* We lie; but the effect's the same */
469 if (ifra->ifra_addr.sin_family == AF_INET &&
470 (hostIsNew || maskIsNew))
471 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
472 if (error != 0 && iaIsNew)
475 if ((ifp->if_flags & IFF_BROADCAST) &&
476 (ifra->ifra_broadaddr.sin_family == AF_INET))
477 ia->ia_broadaddr = ifra->ifra_broadaddr;
479 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
480 in_addmulti(&allhosts_addr, ifp);
481 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
487 * in_ifscrub kills the interface route.
491 * in_ifadown gets rid of all the rest of
492 * the routes. This is not quite the right
493 * thing to do, but at least if we are running
494 * a routing process they will come back.
496 in_ifadown(&ia->ia_ifa, 1);
497 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
502 if (ifp == 0 || ifp->if_ioctl == 0)
505 error = (*ifp->if_ioctl)(ifp, cmd, data);
506 IFF_UNLOCKGIANT(ifp);
511 * Protect from ipintr() traversing address list while we're modifying
515 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
516 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
517 if (ia->ia_addr.sin_family == AF_INET) {
518 LIST_REMOVE(ia, ia_hash);
520 * If this is the last IPv4 address configured on this
521 * interface, leave the all-hosts group.
522 * XXX: This is quite ugly because of locking and structure.
527 struct in_multi *inm;
531 IN_LOOKUP_MULTI(allhosts_addr, ifp, inm);
533 in_delmulti_locked(inm);
535 IFF_UNLOCKGIANT(ifp);
538 IFAFREE(&ia->ia_ifa);
546 * SIOCGLIFADDR: get first address. (?!?)
547 * SIOCGLIFADDR with IFLR_PREFIX:
548 * get first address that matches the specified prefix.
549 * SIOCALIFADDR: add the specified address.
550 * SIOCALIFADDR with IFLR_PREFIX:
551 * EINVAL since we can't deduce hostid part of the address.
552 * SIOCDLIFADDR: delete the specified address.
553 * SIOCDLIFADDR with IFLR_PREFIX:
554 * delete the first address that matches the specified prefix.
556 * EINVAL on invalid parameters
557 * EADDRNOTAVAIL on prefix match failed/specified address not found
558 * other values may be returned from in_ioctl()
561 in_lifaddr_ioctl(so, cmd, data, ifp, td)
568 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
573 panic("invalid argument to in_lifaddr_ioctl");
579 /* address must be specified on GET with IFLR_PREFIX */
580 if ((iflr->flags & IFLR_PREFIX) == 0)
585 /* address must be specified on ADD and DELETE */
586 if (iflr->addr.ss_family != AF_INET)
588 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
590 /* XXX need improvement */
591 if (iflr->dstaddr.ss_family
592 && iflr->dstaddr.ss_family != AF_INET)
594 if (iflr->dstaddr.ss_family
595 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
598 default: /*shouldn't happen*/
601 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
607 struct in_aliasreq ifra;
609 if (iflr->flags & IFLR_PREFIX)
612 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
613 bzero(&ifra, sizeof(ifra));
614 bcopy(iflr->iflr_name, ifra.ifra_name,
615 sizeof(ifra.ifra_name));
617 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
619 if (iflr->dstaddr.ss_family) { /*XXX*/
620 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
621 iflr->dstaddr.ss_len);
624 ifra.ifra_mask.sin_family = AF_INET;
625 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
626 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
628 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
633 struct in_ifaddr *ia;
634 struct in_addr mask, candidate, match;
635 struct sockaddr_in *sin;
638 bzero(&mask, sizeof(mask));
639 if (iflr->flags & IFLR_PREFIX) {
640 /* lookup a prefix rather than address. */
641 in_len2mask(&mask, iflr->prefixlen);
643 sin = (struct sockaddr_in *)&iflr->addr;
644 match.s_addr = sin->sin_addr.s_addr;
645 match.s_addr &= mask.s_addr;
647 /* if you set extra bits, that's wrong */
648 if (match.s_addr != sin->sin_addr.s_addr)
653 if (cmd == SIOCGLIFADDR) {
654 /* on getting an address, take the 1st match */
657 /* on deleting an address, do exact match */
658 in_len2mask(&mask, 32);
659 sin = (struct sockaddr_in *)&iflr->addr;
660 match.s_addr = sin->sin_addr.s_addr;
666 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
667 if (ifa->ifa_addr->sa_family != AF_INET6)
671 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
672 candidate.s_addr &= mask.s_addr;
673 if (candidate.s_addr == match.s_addr)
677 return EADDRNOTAVAIL;
678 ia = (struct in_ifaddr *)ifa;
680 if (cmd == SIOCGLIFADDR) {
681 /* fill in the if_laddrreq structure */
682 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
684 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
685 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
686 ia->ia_dstaddr.sin_len);
688 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
691 in_mask2len(&ia->ia_sockmask.sin_addr);
693 iflr->flags = 0; /*XXX*/
697 struct in_aliasreq ifra;
699 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
700 bzero(&ifra, sizeof(ifra));
701 bcopy(iflr->iflr_name, ifra.ifra_name,
702 sizeof(ifra.ifra_name));
704 bcopy(&ia->ia_addr, &ifra.ifra_addr,
705 ia->ia_addr.sin_len);
706 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
707 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
708 ia->ia_dstaddr.sin_len);
710 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
711 ia->ia_sockmask.sin_len);
713 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
719 return EOPNOTSUPP; /*just for safety*/
723 * Delete any existing route for an interface.
727 register struct ifnet *ifp;
728 register struct in_ifaddr *ia;
734 * Initialize an interface's internet address
735 * and routing table entry.
738 in_ifinit(ifp, ia, sin, scrub)
739 register struct ifnet *ifp;
740 register struct in_ifaddr *ia;
741 struct sockaddr_in *sin;
744 register u_long i = ntohl(sin->sin_addr.s_addr);
745 struct sockaddr_in oldaddr;
746 int s = splimp(), flags = RTF_UP, error = 0;
748 oldaddr = ia->ia_addr;
749 if (oldaddr.sin_family == AF_INET)
750 LIST_REMOVE(ia, ia_hash);
752 if (ia->ia_addr.sin_family == AF_INET)
753 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
756 * Give the interface a chance to initialize
757 * if this is its first address,
758 * and to validate the address if necessary.
762 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
763 IFF_UNLOCKGIANT(ifp);
766 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
767 ia->ia_addr = oldaddr;
768 if (ia->ia_addr.sin_family == AF_INET)
769 LIST_INSERT_HEAD(INADDR_HASH(
770 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
776 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
778 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
781 ia->ia_netmask = IN_CLASSA_NET;
782 else if (IN_CLASSB(i))
783 ia->ia_netmask = IN_CLASSB_NET;
785 ia->ia_netmask = IN_CLASSC_NET;
787 * The subnet mask usually includes at least the standard network part,
788 * but may may be smaller in the case of supernetting.
789 * If it is set, we believe it.
791 if (ia->ia_subnetmask == 0) {
792 ia->ia_subnetmask = ia->ia_netmask;
793 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
795 ia->ia_netmask &= ia->ia_subnetmask;
796 ia->ia_net = i & ia->ia_netmask;
797 ia->ia_subnet = i & ia->ia_subnetmask;
798 in_socktrim(&ia->ia_sockmask);
801 * XXX: carp(4) does not have interface route
803 if (ifp->if_type == IFT_CARP)
807 * Add route for the network.
809 ia->ia_ifa.ifa_metric = ifp->if_metric;
810 if (ifp->if_flags & IFF_BROADCAST) {
811 ia->ia_broadaddr.sin_addr.s_addr =
812 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
813 ia->ia_netbroadcast.s_addr =
814 htonl(ia->ia_net | ~ ia->ia_netmask);
815 } else if (ifp->if_flags & IFF_LOOPBACK) {
816 ia->ia_dstaddr = ia->ia_addr;
818 } else if (ifp->if_flags & IFF_POINTOPOINT) {
819 if (ia->ia_dstaddr.sin_family != AF_INET)
823 if ((error = in_addprefix(ia, flags)) != 0)
829 #define rtinitflags(x) \
830 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
833 * Check if we have a route for the given prefix already or add a one
837 in_addprefix(target, flags)
838 struct in_ifaddr *target;
841 struct in_ifaddr *ia;
842 struct in_addr prefix, mask, p, m;
845 if ((flags & RTF_HOST) != 0)
846 prefix = target->ia_dstaddr.sin_addr;
848 prefix = target->ia_addr.sin_addr;
849 mask = target->ia_sockmask.sin_addr;
850 prefix.s_addr &= mask.s_addr;
853 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
854 if (rtinitflags(ia)) {
855 p = ia->ia_addr.sin_addr;
857 if (prefix.s_addr != p.s_addr)
860 p = ia->ia_addr.sin_addr;
861 m = ia->ia_sockmask.sin_addr;
862 p.s_addr &= m.s_addr;
864 if (prefix.s_addr != p.s_addr ||
865 mask.s_addr != m.s_addr)
870 * If we got a matching prefix route inserted by other
871 * interface address, we are done here.
873 if (ia->ia_flags & IFA_ROUTE) {
874 if (sameprefixcarponly &&
875 target->ia_ifp->if_type != IFT_CARP &&
876 ia->ia_ifp->if_type != IFT_CARP)
884 * No-one seem to have this prefix route, so we try to insert it.
886 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
888 target->ia_flags |= IFA_ROUTE;
893 * If there is no other address in the system that can serve a route to the
894 * same prefix, remove the route. Hand over the route to the new address
898 in_scrubprefix(target)
899 struct in_ifaddr *target;
901 struct in_ifaddr *ia;
902 struct in_addr prefix, mask, p;
905 if ((target->ia_flags & IFA_ROUTE) == 0)
908 if (rtinitflags(target))
909 prefix = target->ia_dstaddr.sin_addr;
911 prefix = target->ia_addr.sin_addr;
912 mask = target->ia_sockmask.sin_addr;
913 prefix.s_addr &= mask.s_addr;
916 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
918 p = ia->ia_dstaddr.sin_addr;
920 p = ia->ia_addr.sin_addr;
921 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
924 if (prefix.s_addr != p.s_addr)
928 * If we got a matching prefix address, move IFA_ROUTE and
929 * the route itself to it. Make sure that routing daemons
932 * XXX: a special case for carp(4) interface
934 if ((ia->ia_flags & IFA_ROUTE) == 0
936 && (ia->ia_ifp->if_type != IFT_CARP)
939 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
940 rtinitflags(target));
941 target->ia_flags &= ~IFA_ROUTE;
943 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
944 rtinitflags(ia) | RTF_UP);
946 ia->ia_flags |= IFA_ROUTE;
952 * As no-one seem to have this prefix, we can remove the route.
954 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
955 target->ia_flags &= ~IFA_ROUTE;
962 * Return 1 if the address might be a local broadcast address.
965 in_broadcast(in, ifp)
969 register struct ifaddr *ifa;
972 if (in.s_addr == INADDR_BROADCAST ||
973 in.s_addr == INADDR_ANY)
975 if ((ifp->if_flags & IFF_BROADCAST) == 0)
977 t = ntohl(in.s_addr);
979 * Look through the list of addresses for a match
980 * with a broadcast address.
982 #define ia ((struct in_ifaddr *)ifa)
983 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
984 if (ifa->ifa_addr->sa_family == AF_INET &&
985 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
986 in.s_addr == ia->ia_netbroadcast.s_addr ||
988 * Check for old-style (host 0) broadcast.
990 t == ia->ia_subnet || t == ia->ia_net) &&
992 * Check for an all one subnetmask. These
993 * only exist when an interface gets a secondary
996 ia->ia_subnetmask != (u_long)0xffffffff)
1003 * Add an address to the list of IP multicast addresses for a given interface.
1006 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1008 struct in_multi *inm;
1015 IN_LOOKUP_MULTI(*ap, ifp, inm);
1018 * If we already joined this group, just bump the
1019 * refcount and return it.
1021 KASSERT(inm->inm_refcount >= 1,
1022 ("%s: bad refcount %d", __func__, inm->inm_refcount));
1023 ++inm->inm_refcount;
1025 struct sockaddr_in sin;
1026 struct ifmultiaddr *ifma;
1027 struct in_multi *ninm;
1030 bzero(&sin, sizeof sin);
1031 sin.sin_family = AF_INET;
1032 sin.sin_len = sizeof(struct sockaddr_in);
1036 * Check if a link-layer group is already associated
1037 * with this network-layer group on the given ifnet.
1038 * If so, bump the refcount on the existing network-layer
1039 * group association and return it.
1041 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1044 if (ifma->ifma_protospec != NULL) {
1045 inm = (struct in_multi *)ifma->ifma_protospec;
1047 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
1048 inm->inm_addr.s_addr != ap->s_addr)
1049 panic("%s: ifma is inconsistent", __func__);
1051 ++inm->inm_refcount;
1056 * A new membership is needed; construct it and
1057 * perform the IGMP join.
1059 ninm = malloc(sizeof(*ninm), M_IPMADDR, M_NOWAIT | M_ZERO);
1061 if_delmulti_ifma(ifma);
1064 ninm->inm_addr = *ap;
1065 ninm->inm_ifp = ifp;
1066 ninm->inm_ifma = ifma;
1067 ninm->inm_refcount = 1;
1068 ifma->ifma_protospec = ninm;
1069 LIST_INSERT_HEAD(&in_multihead, ninm, inm_link);
1071 igmp_joingroup(ninm);
1077 IFF_UNLOCKGIANT(ifp);
1083 * Delete a multicast address record.
1084 * It is OK to call this routine if the underlying ifnet went away.
1086 * XXX: To deal with the ifp going away, we cheat; the link-layer code in net
1087 * will set ifma_ifp to NULL when the associated ifnet instance is detached
1089 * The only reason we need to violate layers and check ifma_ifp here at all
1090 * is because certain hardware drivers still require Giant to be held,
1091 * and it must always be taken before other locks.
1094 in_delmulti(struct in_multi *inm)
1098 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
1099 ifp = inm->inm_ifma->ifma_ifp;
1103 * Sanity check that netinet's notion of ifp is the
1106 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
1111 in_delmulti_locked(inm);
1115 IFF_UNLOCKGIANT(ifp);
1119 * Delete a multicast address record, with locks held.
1121 * It is OK to call this routine if the ifp went away.
1122 * Assumes that caller holds the IN_MULTI lock, and that
1123 * Giant was taken before other locks if required by the hardware.
1126 in_delmulti_locked(struct in_multi *inm)
1128 struct ifmultiaddr *ifma;
1130 IN_MULTI_LOCK_ASSERT();
1131 KASSERT(inm->inm_refcount >= 1, ("%s: freeing freed inm", __func__));
1133 if (--inm->inm_refcount == 0) {
1134 igmp_leavegroup(inm);
1136 ifma = inm->inm_ifma;
1138 printf("%s: purging ifma %p\n", __func__, ifma);
1140 KASSERT(ifma->ifma_protospec == inm,
1141 ("%s: ifma_protospec != inm", __func__));
1142 ifma->ifma_protospec = NULL;
1144 LIST_REMOVE(inm, inm_link);
1145 free(inm, M_IPMADDR);
1147 if_delmulti_ifma(ifma);
1152 * Delete all IPv4 multicast address records, and associated link-layer
1153 * multicast address records, associated with ifp.
1156 in_purgemaddrs(struct ifnet *ifp)
1158 struct in_multi *inm;
1159 struct in_multi *oinm;
1162 printf("%s: purging ifp %p\n", __func__, ifp);
1166 LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
1167 if (inm->inm_ifp == ifp)
1168 in_delmulti_locked(inm);
1171 IFF_UNLOCKGIANT(ifp);
1175 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1182 in_pcbpurgeif0(&ripcbinfo, ifp);
1183 in_pcbpurgeif0(&udbinfo, ifp);
1184 in_purgemaddrs(ifp);