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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sockio.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
45 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/ip_var.h>
58 static int in_mask2len(struct in_addr *);
59 static void in_len2mask(struct in_addr *, int);
60 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
61 struct ifnet *, struct thread *);
63 static int in_addprefix(struct in_ifaddr *, int);
64 static int in_scrubprefix(struct in_ifaddr *);
65 static void in_socktrim(struct sockaddr_in *);
66 static int in_ifinit(struct ifnet *,
67 struct in_ifaddr *, struct sockaddr_in *, int);
68 static void in_purgemaddrs(struct ifnet *);
70 static int subnetsarelocal = 0;
71 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
72 &subnetsarelocal, 0, "Treat all subnets as directly connected");
73 static int sameprefixcarponly = 0;
74 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
75 &sameprefixcarponly, 0,
76 "Refuse to create same prefixes on different interfaces");
78 extern struct inpcbinfo ripcbinfo;
79 extern struct inpcbinfo udbinfo;
82 * Return 1 if an internet address is for a ``local'' host
83 * (one to which we have a connection). If subnetsarelocal
84 * is true, this includes other subnets of the local net.
85 * Otherwise, it includes only the directly-connected (sub)nets.
88 in_localaddr(struct in_addr in)
90 register u_long i = ntohl(in.s_addr);
91 register struct in_ifaddr *ia;
93 if (subnetsarelocal) {
94 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
95 if ((i & ia->ia_netmask) == ia->ia_net)
98 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
99 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
106 * Return 1 if an internet address is for the local host and configured
107 * on one of its interfaces.
110 in_localip(struct in_addr in)
112 struct in_ifaddr *ia;
114 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
115 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
122 * Determine whether an IP address is in a reserved set of addresses
123 * that may not be forwarded, or whether datagrams to that destination
127 in_canforward(struct in_addr in)
129 register u_long i = ntohl(in.s_addr);
132 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
135 net = i & IN_CLASSA_NET;
136 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
143 * Trim a mask in a sockaddr
146 in_socktrim(struct sockaddr_in *ap)
148 register char *cplim = (char *) &ap->sin_addr;
149 register char *cp = (char *) (&ap->sin_addr + 1);
152 while (--cp >= cplim)
154 (ap)->sin_len = cp - (char *) (ap) + 1;
161 struct in_addr *mask;
167 for (x = 0; x < sizeof(*mask); x++) {
172 if (x < sizeof(*mask)) {
173 for (y = 0; y < 8; y++) {
174 if ((p[x] & (0x80 >> y)) == 0)
182 in_len2mask(struct in_addr *mask, int len)
188 bzero(mask, sizeof(*mask));
189 for (i = 0; i < len / 8; i++)
192 p[i] = (0xff00 >> (len % 8)) & 0xff;
196 * Generic internet control operations (ioctl's).
197 * Ifp is 0 if not an interface-specific ioctl.
201 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
204 register struct ifreq *ifr = (struct ifreq *)data;
205 register struct in_ifaddr *ia = 0, *iap;
206 register struct ifaddr *ifa;
207 struct in_addr allhosts_addr;
209 struct in_ifaddr *oia;
210 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
211 struct sockaddr_in oldaddr;
212 int error, hostIsNew, iaIsNew, maskIsNew, s;
217 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
222 error = priv_check(td, PRIV_NET_ADDIFADDR);
228 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
232 error = priv_check(td, PRIV_NET_DELIFADDR);
238 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
243 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
247 * Find address for this interface, if it exists.
249 * If an alias address was specified, find that one instead of
250 * the first one on the interface, if possible.
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 if (td == NULL || prison_check_ip4(
258 td->td_ucred, &dst) == 0)
263 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
265 if (iap->ia_addr.sin_family == AF_INET) {
267 prison_check_ip4(td->td_ucred,
268 &iap->ia_addr.sin_addr) != 0)
283 return (EADDRNOTAVAIL);
284 if (ifra->ifra_addr.sin_family == AF_INET) {
285 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
286 if (ia->ia_ifp == ifp &&
287 ia->ia_addr.sin_addr.s_addr ==
288 ifra->ifra_addr.sin_addr.s_addr)
291 if ((ifp->if_flags & IFF_POINTOPOINT)
292 && (cmd == SIOCAIFADDR)
293 && (ifra->ifra_dstaddr.sin_addr.s_addr
298 if (cmd == SIOCDIFADDR && ia == 0)
299 return (EADDRNOTAVAIL);
305 error = priv_check(td, (cmd == SIOCDIFADDR) ?
306 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
312 return (EADDRNOTAVAIL);
313 if (ia == (struct in_ifaddr *)0) {
314 ia = (struct in_ifaddr *)
315 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
316 if (ia == (struct in_ifaddr *)NULL)
319 * Protect from ipintr() traversing address list
320 * while we're modifying it.
325 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
326 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
327 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
329 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
331 ia->ia_sockmask.sin_len = 8;
332 ia->ia_sockmask.sin_family = AF_INET;
333 if (ifp->if_flags & IFF_BROADCAST) {
334 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
335 ia->ia_broadaddr.sin_family = AF_INET;
339 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
347 error = priv_check(td, PRIV_NET_ADDIFADDR);
357 if (ia == (struct in_ifaddr *)0)
358 return (EADDRNOTAVAIL);
364 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
368 if ((ifp->if_flags & IFF_BROADCAST) == 0)
370 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
374 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
376 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
380 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
384 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
386 oldaddr = ia->ia_dstaddr;
387 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
390 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
392 IFF_UNLOCKGIANT(ifp);
394 ia->ia_dstaddr = oldaddr;
398 if (ia->ia_flags & IFA_ROUTE) {
399 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
400 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
401 ia->ia_ifa.ifa_dstaddr =
402 (struct sockaddr *)&ia->ia_dstaddr;
403 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
408 if ((ifp->if_flags & IFF_BROADCAST) == 0)
410 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
414 error = in_ifinit(ifp, ia,
415 (struct sockaddr_in *) &ifr->ifr_addr, 1);
416 if (error != 0 && iaIsNew)
419 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
420 in_addmulti(&allhosts_addr, ifp);
421 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
426 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
427 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
434 if (ia->ia_addr.sin_family == AF_INET) {
435 if (ifra->ifra_addr.sin_len == 0) {
436 ifra->ifra_addr = ia->ia_addr;
438 } else if (ifra->ifra_addr.sin_addr.s_addr ==
439 ia->ia_addr.sin_addr.s_addr)
442 if (ifra->ifra_mask.sin_len) {
444 ia->ia_sockmask = ifra->ifra_mask;
445 ia->ia_sockmask.sin_family = AF_INET;
447 ntohl(ia->ia_sockmask.sin_addr.s_addr);
450 if ((ifp->if_flags & IFF_POINTOPOINT) &&
451 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
453 ia->ia_dstaddr = ifra->ifra_dstaddr;
454 maskIsNew = 1; /* We lie; but the effect's the same */
456 if (ifra->ifra_addr.sin_family == AF_INET &&
457 (hostIsNew || maskIsNew))
458 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
459 if (error != 0 && iaIsNew)
462 if ((ifp->if_flags & IFF_BROADCAST) &&
463 (ifra->ifra_broadaddr.sin_family == AF_INET))
464 ia->ia_broadaddr = ifra->ifra_broadaddr;
466 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
467 in_addmulti(&allhosts_addr, ifp);
468 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
474 * in_ifscrub kills the interface route.
478 * in_ifadown gets rid of all the rest of
479 * the routes. This is not quite the right
480 * thing to do, but at least if we are running
481 * a routing process they will come back.
483 in_ifadown(&ia->ia_ifa, 1);
484 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
489 if (ifp == 0 || ifp->if_ioctl == 0)
492 error = (*ifp->if_ioctl)(ifp, cmd, data);
493 IFF_UNLOCKGIANT(ifp);
498 * Protect from ipintr() traversing address list while we're modifying
502 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
503 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
504 if (ia->ia_addr.sin_family == AF_INET) {
505 LIST_REMOVE(ia, ia_hash);
507 * If this is the last IPv4 address configured on this
508 * interface, leave the all-hosts group.
509 * XXX: This is quite ugly because of locking and structure.
514 struct in_multi *inm;
518 IN_LOOKUP_MULTI(allhosts_addr, ifp, inm);
520 in_delmulti_locked(inm);
522 IFF_UNLOCKGIANT(ifp);
525 IFAFREE(&ia->ia_ifa);
533 * SIOCGLIFADDR: get first address. (?!?)
534 * SIOCGLIFADDR with IFLR_PREFIX:
535 * get first address that matches the specified prefix.
536 * SIOCALIFADDR: add the specified address.
537 * SIOCALIFADDR with IFLR_PREFIX:
538 * EINVAL since we can't deduce hostid part of the address.
539 * SIOCDLIFADDR: delete the specified address.
540 * SIOCDLIFADDR with IFLR_PREFIX:
541 * delete the first address that matches the specified prefix.
543 * EINVAL on invalid parameters
544 * EADDRNOTAVAIL on prefix match failed/specified address not found
545 * other values may be returned from in_ioctl()
548 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
549 struct ifnet *ifp, struct thread *td)
551 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
556 panic("invalid argument to in_lifaddr_ioctl");
562 /* address must be specified on GET with IFLR_PREFIX */
563 if ((iflr->flags & IFLR_PREFIX) == 0)
568 /* address must be specified on ADD and DELETE */
569 if (iflr->addr.ss_family != AF_INET)
571 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
573 /* XXX need improvement */
574 if (iflr->dstaddr.ss_family
575 && iflr->dstaddr.ss_family != AF_INET)
577 if (iflr->dstaddr.ss_family
578 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
581 default: /*shouldn't happen*/
584 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
590 struct in_aliasreq ifra;
592 if (iflr->flags & IFLR_PREFIX)
595 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
596 bzero(&ifra, sizeof(ifra));
597 bcopy(iflr->iflr_name, ifra.ifra_name,
598 sizeof(ifra.ifra_name));
600 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
602 if (iflr->dstaddr.ss_family) { /*XXX*/
603 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
604 iflr->dstaddr.ss_len);
607 ifra.ifra_mask.sin_family = AF_INET;
608 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
609 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
611 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
616 struct in_ifaddr *ia;
617 struct in_addr mask, candidate, match;
618 struct sockaddr_in *sin;
620 bzero(&mask, sizeof(mask));
621 bzero(&match, sizeof(match));
622 if (iflr->flags & IFLR_PREFIX) {
623 /* lookup a prefix rather than address. */
624 in_len2mask(&mask, iflr->prefixlen);
626 sin = (struct sockaddr_in *)&iflr->addr;
627 match.s_addr = sin->sin_addr.s_addr;
628 match.s_addr &= mask.s_addr;
630 /* if you set extra bits, that's wrong */
631 if (match.s_addr != sin->sin_addr.s_addr)
635 /* on getting an address, take the 1st match */
636 /* on deleting an address, do exact match */
637 if (cmd != SIOCGLIFADDR) {
638 in_len2mask(&mask, 32);
639 sin = (struct sockaddr_in *)&iflr->addr;
640 match.s_addr = sin->sin_addr.s_addr;
644 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
645 if (ifa->ifa_addr->sa_family != AF_INET6)
647 if (match.s_addr == 0)
649 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
650 candidate.s_addr &= mask.s_addr;
651 if (candidate.s_addr == match.s_addr)
655 return EADDRNOTAVAIL;
656 ia = (struct in_ifaddr *)ifa;
658 if (cmd == SIOCGLIFADDR) {
659 /* fill in the if_laddrreq structure */
660 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
662 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
663 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
664 ia->ia_dstaddr.sin_len);
666 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
669 in_mask2len(&ia->ia_sockmask.sin_addr);
671 iflr->flags = 0; /*XXX*/
675 struct in_aliasreq ifra;
677 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
678 bzero(&ifra, sizeof(ifra));
679 bcopy(iflr->iflr_name, ifra.ifra_name,
680 sizeof(ifra.ifra_name));
682 bcopy(&ia->ia_addr, &ifra.ifra_addr,
683 ia->ia_addr.sin_len);
684 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
685 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
686 ia->ia_dstaddr.sin_len);
688 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
689 ia->ia_sockmask.sin_len);
691 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
697 return EOPNOTSUPP; /*just for safety*/
701 * Delete any existing route for an interface.
704 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
711 * Initialize an interface's internet address
712 * and routing table entry.
715 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
718 register u_long i = ntohl(sin->sin_addr.s_addr);
719 struct sockaddr_in oldaddr;
720 int s = splimp(), flags = RTF_UP, error = 0;
722 oldaddr = ia->ia_addr;
723 if (oldaddr.sin_family == AF_INET)
724 LIST_REMOVE(ia, ia_hash);
726 if (ia->ia_addr.sin_family == AF_INET)
727 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
730 * Give the interface a chance to initialize
731 * if this is its first address,
732 * and to validate the address if necessary.
736 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
737 IFF_UNLOCKGIANT(ifp);
740 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
741 ia->ia_addr = oldaddr;
742 if (ia->ia_addr.sin_family == AF_INET)
743 LIST_INSERT_HEAD(INADDR_HASH(
744 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
747 * If oldaddr family is not AF_INET (e.g.
748 * interface has been just created) in_control
749 * does not call LIST_REMOVE, and we end up
750 * with bogus ia entries in hash
752 LIST_REMOVE(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 one accordingly.
818 in_addprefix(struct in_ifaddr *target, int flags)
820 struct in_ifaddr *ia;
821 struct in_addr prefix, mask, p, m;
824 if ((flags & RTF_HOST) != 0) {
825 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 * Delete all IPv4 multicast address records, and associated link-layer
981 * multicast address records, associated with ifp.
984 in_purgemaddrs(struct ifnet *ifp)
986 struct in_multi *inm;
987 struct in_multi *oinm;
990 printf("%s: purging ifp %p\n", __func__, ifp);
994 LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
995 if (inm->inm_ifp == ifp)
996 in_delmulti_locked(inm);
999 IFF_UNLOCKGIANT(ifp);
1003 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1006 in_ifdetach(struct ifnet *ifp)
1009 in_pcbpurgeif0(&ripcbinfo, ifp);
1010 in_pcbpurgeif0(&udbinfo, ifp);
1011 in_purgemaddrs(ifp);