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>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
48 #include <net/if_types.h>
49 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/in_var.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/ip_var.h>
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 *);
61 static int in_addprefix(struct in_ifaddr *, int);
62 static int in_scrubprefix(struct in_ifaddr *);
63 static void in_socktrim(struct sockaddr_in *);
64 static int in_ifinit(struct ifnet *,
65 struct in_ifaddr *, struct sockaddr_in *, int);
66 static void in_purgemaddrs(struct ifnet *);
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");
76 extern struct inpcbinfo ripcbinfo;
77 extern struct inpcbinfo udbinfo;
80 * Return 1 if an internet address is for a ``local'' host
81 * (one to which we have a connection). If subnetsarelocal
82 * is true, this includes other subnets of the local net.
83 * Otherwise, it includes only the directly-connected (sub)nets.
86 in_localaddr(struct in_addr in)
88 register u_long i = ntohl(in.s_addr);
89 register struct in_ifaddr *ia;
91 if (subnetsarelocal) {
92 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
93 if ((i & ia->ia_netmask) == ia->ia_net)
96 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
97 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
104 * Return 1 if an internet address is for the local host and configured
105 * on one of its interfaces.
108 in_localip(struct in_addr in)
110 struct in_ifaddr *ia;
112 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
113 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
120 * Determine whether an IP address is in a reserved set of addresses
121 * that may not be forwarded, or whether datagrams to that destination
125 in_canforward(struct in_addr in)
127 register u_long i = ntohl(in.s_addr);
130 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
133 net = i & IN_CLASSA_NET;
134 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
141 * Trim a mask in a sockaddr
144 in_socktrim(struct sockaddr_in *ap)
146 register char *cplim = (char *) &ap->sin_addr;
147 register char *cp = (char *) (&ap->sin_addr + 1);
150 while (--cp >= cplim)
152 (ap)->sin_len = cp - (char *) (ap) + 1;
159 struct in_addr *mask;
165 for (x = 0; x < sizeof(*mask); x++) {
170 if (x < sizeof(*mask)) {
171 for (y = 0; y < 8; y++) {
172 if ((p[x] & (0x80 >> y)) == 0)
180 in_len2mask(struct in_addr *mask, int len)
186 bzero(mask, sizeof(*mask));
187 for (i = 0; i < len / 8; i++)
190 p[i] = (0xff00 >> (len % 8)) & 0xff;
194 * Generic internet control operations (ioctl's).
195 * Ifp is 0 if not an interface-specific ioctl.
199 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
202 register struct ifreq *ifr = (struct ifreq *)data;
203 register struct in_ifaddr *ia = 0, *iap;
204 register struct ifaddr *ifa;
205 struct in_addr allhosts_addr;
207 struct in_ifaddr *oia;
208 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
209 struct sockaddr_in oldaddr;
210 int error, hostIsNew, iaIsNew, maskIsNew, s;
215 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
220 error = priv_check(td, PRIV_NET_ADDIFADDR);
226 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
230 error = priv_check(td, PRIV_NET_DELIFADDR);
236 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
241 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
245 * Find address for this interface, if it exists.
247 * If an alias address was specified, find that one instead of
248 * the first one on the interface, if possible.
251 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
252 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
253 if (iap->ia_ifp == ifp &&
254 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
259 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
261 if (iap->ia_addr.sin_family == AF_INET) {
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)
283 if ((ifp->if_flags & IFF_POINTOPOINT)
284 && (cmd == SIOCAIFADDR)
285 && (ifra->ifra_dstaddr.sin_addr.s_addr
290 if (cmd == SIOCDIFADDR && ia == 0)
291 return (EADDRNOTAVAIL);
297 error = priv_check(td, PRIV_NET_ADDIFADDR);
303 return (EADDRNOTAVAIL);
304 if (ia == (struct in_ifaddr *)0) {
305 ia = (struct in_ifaddr *)
306 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
307 if (ia == (struct in_ifaddr *)NULL)
310 * Protect from ipintr() traversing address list
311 * while we're modifying it.
316 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
317 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
318 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
320 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
322 ia->ia_sockmask.sin_len = 8;
323 ia->ia_sockmask.sin_family = AF_INET;
324 if (ifp->if_flags & IFF_BROADCAST) {
325 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
326 ia->ia_broadaddr.sin_family = AF_INET;
330 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
338 error = priv_check(td, PRIV_NET_ADDIFADDR);
348 if (ia == (struct in_ifaddr *)0)
349 return (EADDRNOTAVAIL);
355 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
359 if ((ifp->if_flags & IFF_BROADCAST) == 0)
361 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
365 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
367 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
371 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
375 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
377 oldaddr = ia->ia_dstaddr;
378 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
381 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
383 IFF_UNLOCKGIANT(ifp);
385 ia->ia_dstaddr = oldaddr;
389 if (ia->ia_flags & IFA_ROUTE) {
390 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
391 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
392 ia->ia_ifa.ifa_dstaddr =
393 (struct sockaddr *)&ia->ia_dstaddr;
394 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
399 if ((ifp->if_flags & IFF_BROADCAST) == 0)
401 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
405 error = in_ifinit(ifp, ia,
406 (struct sockaddr_in *) &ifr->ifr_addr, 1);
407 if (error != 0 && iaIsNew)
410 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
411 in_addmulti(&allhosts_addr, ifp);
412 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
417 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
418 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
425 if (ia->ia_addr.sin_family == AF_INET) {
426 if (ifra->ifra_addr.sin_len == 0) {
427 ifra->ifra_addr = ia->ia_addr;
429 } else if (ifra->ifra_addr.sin_addr.s_addr ==
430 ia->ia_addr.sin_addr.s_addr)
433 if (ifra->ifra_mask.sin_len) {
435 ia->ia_sockmask = ifra->ifra_mask;
436 ia->ia_sockmask.sin_family = AF_INET;
438 ntohl(ia->ia_sockmask.sin_addr.s_addr);
441 if ((ifp->if_flags & IFF_POINTOPOINT) &&
442 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
444 ia->ia_dstaddr = ifra->ifra_dstaddr;
445 maskIsNew = 1; /* We lie; but the effect's the same */
447 if (ifra->ifra_addr.sin_family == AF_INET &&
448 (hostIsNew || maskIsNew))
449 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
450 if (error != 0 && iaIsNew)
453 if ((ifp->if_flags & IFF_BROADCAST) &&
454 (ifra->ifra_broadaddr.sin_family == AF_INET))
455 ia->ia_broadaddr = ifra->ifra_broadaddr;
457 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
458 in_addmulti(&allhosts_addr, ifp);
459 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
465 * in_ifscrub kills the interface route.
469 * in_ifadown gets rid of all the rest of
470 * the routes. This is not quite the right
471 * thing to do, but at least if we are running
472 * a routing process they will come back.
474 in_ifadown(&ia->ia_ifa, 1);
475 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
480 if (ifp == 0 || ifp->if_ioctl == 0)
483 error = (*ifp->if_ioctl)(ifp, cmd, data);
484 IFF_UNLOCKGIANT(ifp);
489 * Protect from ipintr() traversing address list while we're modifying
493 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
494 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
495 if (ia->ia_addr.sin_family == AF_INET) {
496 LIST_REMOVE(ia, ia_hash);
498 * If this is the last IPv4 address configured on this
499 * interface, leave the all-hosts group.
500 * XXX: This is quite ugly because of locking and structure.
505 struct in_multi *inm;
509 IN_LOOKUP_MULTI(allhosts_addr, ifp, inm);
511 in_delmulti_locked(inm);
513 IFF_UNLOCKGIANT(ifp);
516 IFAFREE(&ia->ia_ifa);
524 * SIOCGLIFADDR: get first address. (?!?)
525 * SIOCGLIFADDR with IFLR_PREFIX:
526 * get first address that matches the specified prefix.
527 * SIOCALIFADDR: add the specified address.
528 * SIOCALIFADDR with IFLR_PREFIX:
529 * EINVAL since we can't deduce hostid part of the address.
530 * SIOCDLIFADDR: delete the specified address.
531 * SIOCDLIFADDR with IFLR_PREFIX:
532 * delete the first address that matches the specified prefix.
534 * EINVAL on invalid parameters
535 * EADDRNOTAVAIL on prefix match failed/specified address not found
536 * other values may be returned from in_ioctl()
539 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
540 struct ifnet *ifp, struct thread *td)
542 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
547 panic("invalid argument to in_lifaddr_ioctl");
553 /* address must be specified on GET with IFLR_PREFIX */
554 if ((iflr->flags & IFLR_PREFIX) == 0)
559 /* address must be specified on ADD and DELETE */
560 if (iflr->addr.ss_family != AF_INET)
562 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
564 /* XXX need improvement */
565 if (iflr->dstaddr.ss_family
566 && iflr->dstaddr.ss_family != AF_INET)
568 if (iflr->dstaddr.ss_family
569 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
572 default: /*shouldn't happen*/
575 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
581 struct in_aliasreq ifra;
583 if (iflr->flags & IFLR_PREFIX)
586 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
587 bzero(&ifra, sizeof(ifra));
588 bcopy(iflr->iflr_name, ifra.ifra_name,
589 sizeof(ifra.ifra_name));
591 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
593 if (iflr->dstaddr.ss_family) { /*XXX*/
594 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
595 iflr->dstaddr.ss_len);
598 ifra.ifra_mask.sin_family = AF_INET;
599 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
600 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
602 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
607 struct in_ifaddr *ia;
608 struct in_addr mask, candidate, match;
609 struct sockaddr_in *sin;
611 bzero(&mask, sizeof(mask));
612 bzero(&match, sizeof(match));
613 if (iflr->flags & IFLR_PREFIX) {
614 /* lookup a prefix rather than address. */
615 in_len2mask(&mask, iflr->prefixlen);
617 sin = (struct sockaddr_in *)&iflr->addr;
618 match.s_addr = sin->sin_addr.s_addr;
619 match.s_addr &= mask.s_addr;
621 /* if you set extra bits, that's wrong */
622 if (match.s_addr != sin->sin_addr.s_addr)
626 /* on getting an address, take the 1st match */
627 /* on deleting an address, do exact match */
628 if (cmd != SIOCGLIFADDR) {
629 in_len2mask(&mask, 32);
630 sin = (struct sockaddr_in *)&iflr->addr;
631 match.s_addr = sin->sin_addr.s_addr;
635 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
636 if (ifa->ifa_addr->sa_family != AF_INET6)
638 if (match.s_addr == 0)
640 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
641 candidate.s_addr &= mask.s_addr;
642 if (candidate.s_addr == match.s_addr)
646 return EADDRNOTAVAIL;
647 ia = (struct in_ifaddr *)ifa;
649 if (cmd == SIOCGLIFADDR) {
650 /* fill in the if_laddrreq structure */
651 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
653 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
654 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
655 ia->ia_dstaddr.sin_len);
657 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
660 in_mask2len(&ia->ia_sockmask.sin_addr);
662 iflr->flags = 0; /*XXX*/
666 struct in_aliasreq ifra;
668 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
669 bzero(&ifra, sizeof(ifra));
670 bcopy(iflr->iflr_name, ifra.ifra_name,
671 sizeof(ifra.ifra_name));
673 bcopy(&ia->ia_addr, &ifra.ifra_addr,
674 ia->ia_addr.sin_len);
675 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
676 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
677 ia->ia_dstaddr.sin_len);
679 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
680 ia->ia_sockmask.sin_len);
682 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
688 return EOPNOTSUPP; /*just for safety*/
692 * Delete any existing route for an interface.
695 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
702 * Initialize an interface's internet address
703 * and routing table entry.
706 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
709 register u_long i = ntohl(sin->sin_addr.s_addr);
710 struct sockaddr_in oldaddr;
711 int s = splimp(), flags = RTF_UP, error = 0;
713 oldaddr = ia->ia_addr;
714 if (oldaddr.sin_family == AF_INET)
715 LIST_REMOVE(ia, ia_hash);
717 if (ia->ia_addr.sin_family == AF_INET)
718 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
721 * Give the interface a chance to initialize
722 * if this is its first address,
723 * and to validate the address if necessary.
727 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
728 IFF_UNLOCKGIANT(ifp);
731 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
732 ia->ia_addr = oldaddr;
733 if (ia->ia_addr.sin_family == AF_INET)
734 LIST_INSERT_HEAD(INADDR_HASH(
735 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
741 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
743 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
746 ia->ia_netmask = IN_CLASSA_NET;
747 else if (IN_CLASSB(i))
748 ia->ia_netmask = IN_CLASSB_NET;
750 ia->ia_netmask = IN_CLASSC_NET;
752 * The subnet mask usually includes at least the standard network part,
753 * but may may be smaller in the case of supernetting.
754 * If it is set, we believe it.
756 if (ia->ia_subnetmask == 0) {
757 ia->ia_subnetmask = ia->ia_netmask;
758 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
760 ia->ia_netmask &= ia->ia_subnetmask;
761 ia->ia_net = i & ia->ia_netmask;
762 ia->ia_subnet = i & ia->ia_subnetmask;
763 in_socktrim(&ia->ia_sockmask);
766 * XXX: carp(4) does not have interface route
768 if (ifp->if_type == IFT_CARP)
772 * Add route for the network.
774 ia->ia_ifa.ifa_metric = ifp->if_metric;
775 if (ifp->if_flags & IFF_BROADCAST) {
776 ia->ia_broadaddr.sin_addr.s_addr =
777 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
778 ia->ia_netbroadcast.s_addr =
779 htonl(ia->ia_net | ~ ia->ia_netmask);
780 } else if (ifp->if_flags & IFF_LOOPBACK) {
781 ia->ia_dstaddr = ia->ia_addr;
783 } else if (ifp->if_flags & IFF_POINTOPOINT) {
784 if (ia->ia_dstaddr.sin_family != AF_INET)
788 if ((error = in_addprefix(ia, flags)) != 0)
794 #define rtinitflags(x) \
795 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
798 * Check if we have a route for the given prefix already or add one accordingly.
801 in_addprefix(struct in_ifaddr *target, int flags)
803 struct in_ifaddr *ia;
804 struct in_addr prefix, mask, p, m;
807 if ((flags & RTF_HOST) != 0) {
808 prefix = target->ia_dstaddr.sin_addr;
811 prefix = target->ia_addr.sin_addr;
812 mask = target->ia_sockmask.sin_addr;
813 prefix.s_addr &= mask.s_addr;
816 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
817 if (rtinitflags(ia)) {
818 p = ia->ia_addr.sin_addr;
820 if (prefix.s_addr != p.s_addr)
823 p = ia->ia_addr.sin_addr;
824 m = ia->ia_sockmask.sin_addr;
825 p.s_addr &= m.s_addr;
827 if (prefix.s_addr != p.s_addr ||
828 mask.s_addr != m.s_addr)
833 * If we got a matching prefix route inserted by other
834 * interface address, we are done here.
836 if (ia->ia_flags & IFA_ROUTE) {
837 if (sameprefixcarponly &&
838 target->ia_ifp->if_type != IFT_CARP &&
839 ia->ia_ifp->if_type != IFT_CARP)
847 * No-one seem to have this prefix route, so we try to insert it.
849 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
851 target->ia_flags |= IFA_ROUTE;
856 * If there is no other address in the system that can serve a route to the
857 * same prefix, remove the route. Hand over the route to the new address
861 in_scrubprefix(struct in_ifaddr *target)
863 struct in_ifaddr *ia;
864 struct in_addr prefix, mask, p;
867 if ((target->ia_flags & IFA_ROUTE) == 0)
870 if (rtinitflags(target))
871 prefix = target->ia_dstaddr.sin_addr;
873 prefix = target->ia_addr.sin_addr;
874 mask = target->ia_sockmask.sin_addr;
875 prefix.s_addr &= mask.s_addr;
878 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
880 p = ia->ia_dstaddr.sin_addr;
882 p = ia->ia_addr.sin_addr;
883 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
886 if (prefix.s_addr != p.s_addr)
890 * If we got a matching prefix address, move IFA_ROUTE and
891 * the route itself to it. Make sure that routing daemons
894 * XXX: a special case for carp(4) interface
896 if ((ia->ia_flags & IFA_ROUTE) == 0
898 && (ia->ia_ifp->if_type != IFT_CARP)
901 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
902 rtinitflags(target));
903 target->ia_flags &= ~IFA_ROUTE;
905 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
906 rtinitflags(ia) | RTF_UP);
908 ia->ia_flags |= IFA_ROUTE;
914 * As no-one seem to have this prefix, we can remove the route.
916 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
917 target->ia_flags &= ~IFA_ROUTE;
924 * Return 1 if the address might be a local broadcast address.
927 in_broadcast(struct in_addr in, struct ifnet *ifp)
929 register struct ifaddr *ifa;
932 if (in.s_addr == INADDR_BROADCAST ||
933 in.s_addr == INADDR_ANY)
935 if ((ifp->if_flags & IFF_BROADCAST) == 0)
937 t = ntohl(in.s_addr);
939 * Look through the list of addresses for a match
940 * with a broadcast address.
942 #define ia ((struct in_ifaddr *)ifa)
943 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
944 if (ifa->ifa_addr->sa_family == AF_INET &&
945 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
946 in.s_addr == ia->ia_netbroadcast.s_addr ||
948 * Check for old-style (host 0) broadcast.
950 t == ia->ia_subnet || t == ia->ia_net) &&
952 * Check for an all one subnetmask. These
953 * only exist when an interface gets a secondary
956 ia->ia_subnetmask != (u_long)0xffffffff)
963 * Delete all IPv4 multicast address records, and associated link-layer
964 * multicast address records, associated with ifp.
967 in_purgemaddrs(struct ifnet *ifp)
969 struct in_multi *inm;
970 struct in_multi *oinm;
973 printf("%s: purging ifp %p\n", __func__, ifp);
977 LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
978 if (inm->inm_ifp == ifp)
979 in_delmulti_locked(inm);
982 IFF_UNLOCKGIANT(ifp);
986 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
989 in_ifdetach(struct ifnet *ifp)
992 in_pcbpurgeif0(&ripcbinfo, ifp);
993 in_pcbpurgeif0(&udbinfo, ifp);