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>
52 #include <netinet/ip_var.h>
54 static int in_mask2len(struct in_addr *);
55 static void in_len2mask(struct in_addr *, int);
56 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
57 struct ifnet *, struct thread *);
59 static int in_addprefix(struct in_ifaddr *, int);
60 static int in_scrubprefix(struct in_ifaddr *);
61 static void in_socktrim(struct sockaddr_in *);
62 static int in_ifinit(struct ifnet *,
63 struct in_ifaddr *, struct sockaddr_in *, int);
64 static void in_purgemaddrs(struct ifnet *);
66 static int subnetsarelocal = 0;
67 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
68 &subnetsarelocal, 0, "Treat all subnets as directly connected");
69 static int sameprefixcarponly = 0;
70 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
71 &sameprefixcarponly, 0,
72 "Refuse to create same prefixes on different interfaces");
74 extern struct inpcbinfo ripcbinfo;
75 extern struct inpcbinfo udbinfo;
78 * Return 1 if an internet address is for a ``local'' host
79 * (one to which we have a connection). If subnetsarelocal
80 * is true, this includes other subnets of the local net.
81 * Otherwise, it includes only the directly-connected (sub)nets.
84 in_localaddr(struct in_addr in)
86 register u_long i = ntohl(in.s_addr);
87 register struct in_ifaddr *ia;
89 if (subnetsarelocal) {
90 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
91 if ((i & ia->ia_netmask) == ia->ia_net)
94 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
95 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
102 * Return 1 if an internet address is for the local host and configured
103 * on one of its interfaces.
106 in_localip(struct in_addr in)
108 struct in_ifaddr *ia;
110 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
111 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
118 * Determine whether an IP address is in a reserved set of addresses
119 * that may not be forwarded, or whether datagrams to that destination
123 in_canforward(struct in_addr in)
125 register u_long i = ntohl(in.s_addr);
128 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
131 net = i & IN_CLASSA_NET;
132 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
139 * Trim a mask in a sockaddr
142 in_socktrim(struct sockaddr_in *ap)
144 register char *cplim = (char *) &ap->sin_addr;
145 register char *cp = (char *) (&ap->sin_addr + 1);
148 while (--cp >= cplim)
150 (ap)->sin_len = cp - (char *) (ap) + 1;
157 struct in_addr *mask;
163 for (x = 0; x < sizeof(*mask); x++) {
168 if (x < sizeof(*mask)) {
169 for (y = 0; y < 8; y++) {
170 if ((p[x] & (0x80 >> y)) == 0)
178 in_len2mask(struct in_addr *mask, int len)
184 bzero(mask, sizeof(*mask));
185 for (i = 0; i < len / 8; i++)
188 p[i] = (0xff00 >> (len % 8)) & 0xff;
192 * Generic internet control operations (ioctl's).
193 * Ifp is 0 if not an interface-specific ioctl.
197 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
200 register struct ifreq *ifr = (struct ifreq *)data;
201 register struct in_ifaddr *ia = 0, *iap;
202 register struct ifaddr *ifa;
203 struct in_addr allhosts_addr;
205 struct in_ifaddr *oia;
206 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
207 struct sockaddr_in oldaddr;
208 int error, hostIsNew, iaIsNew, maskIsNew, s;
213 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
218 error = priv_check(td, PRIV_NET_ADDIFADDR);
224 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
228 error = priv_check(td, PRIV_NET_DELIFADDR);
234 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
239 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
243 * Find address for this interface, if it exists.
245 * If an alias address was specified, find that one instead of
246 * the first one on the interface, if possible.
249 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
250 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
251 if (iap->ia_ifp == ifp &&
252 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
257 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
259 if (iap->ia_addr.sin_family == AF_INET) {
273 return (EADDRNOTAVAIL);
274 if (ifra->ifra_addr.sin_family == AF_INET) {
275 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
276 if (ia->ia_ifp == ifp &&
277 ia->ia_addr.sin_addr.s_addr ==
278 ifra->ifra_addr.sin_addr.s_addr)
281 if ((ifp->if_flags & IFF_POINTOPOINT)
282 && (cmd == SIOCAIFADDR)
283 && (ifra->ifra_dstaddr.sin_addr.s_addr
288 if (cmd == SIOCDIFADDR && ia == 0)
289 return (EADDRNOTAVAIL);
295 error = priv_check(td, PRIV_NET_ADDIFADDR);
301 return (EADDRNOTAVAIL);
302 if (ia == (struct in_ifaddr *)0) {
303 ia = (struct in_ifaddr *)
304 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
305 if (ia == (struct in_ifaddr *)NULL)
308 * Protect from ipintr() traversing address list
309 * while we're modifying it.
314 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
315 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
316 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
318 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
320 ia->ia_sockmask.sin_len = 8;
321 ia->ia_sockmask.sin_family = AF_INET;
322 if (ifp->if_flags & IFF_BROADCAST) {
323 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
324 ia->ia_broadaddr.sin_family = AF_INET;
328 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
336 error = priv_check(td, PRIV_NET_ADDIFADDR);
346 if (ia == (struct in_ifaddr *)0)
347 return (EADDRNOTAVAIL);
353 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
357 if ((ifp->if_flags & IFF_BROADCAST) == 0)
359 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
363 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
365 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
369 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
373 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
375 oldaddr = ia->ia_dstaddr;
376 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
379 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
381 IFF_UNLOCKGIANT(ifp);
383 ia->ia_dstaddr = oldaddr;
387 if (ia->ia_flags & IFA_ROUTE) {
388 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
389 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
390 ia->ia_ifa.ifa_dstaddr =
391 (struct sockaddr *)&ia->ia_dstaddr;
392 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
397 if ((ifp->if_flags & IFF_BROADCAST) == 0)
399 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
403 error = in_ifinit(ifp, ia,
404 (struct sockaddr_in *) &ifr->ifr_addr, 1);
405 if (error != 0 && iaIsNew)
408 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
409 in_addmulti(&allhosts_addr, ifp);
410 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
415 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
416 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
423 if (ia->ia_addr.sin_family == AF_INET) {
424 if (ifra->ifra_addr.sin_len == 0) {
425 ifra->ifra_addr = ia->ia_addr;
427 } else if (ifra->ifra_addr.sin_addr.s_addr ==
428 ia->ia_addr.sin_addr.s_addr)
431 if (ifra->ifra_mask.sin_len) {
433 ia->ia_sockmask = ifra->ifra_mask;
434 ia->ia_sockmask.sin_family = AF_INET;
436 ntohl(ia->ia_sockmask.sin_addr.s_addr);
439 if ((ifp->if_flags & IFF_POINTOPOINT) &&
440 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
442 ia->ia_dstaddr = ifra->ifra_dstaddr;
443 maskIsNew = 1; /* We lie; but the effect's the same */
445 if (ifra->ifra_addr.sin_family == AF_INET &&
446 (hostIsNew || maskIsNew))
447 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
448 if (error != 0 && iaIsNew)
451 if ((ifp->if_flags & IFF_BROADCAST) &&
452 (ifra->ifra_broadaddr.sin_family == AF_INET))
453 ia->ia_broadaddr = ifra->ifra_broadaddr;
455 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
456 in_addmulti(&allhosts_addr, ifp);
457 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
463 * in_ifscrub kills the interface route.
467 * in_ifadown gets rid of all the rest of
468 * the routes. This is not quite the right
469 * thing to do, but at least if we are running
470 * a routing process they will come back.
472 in_ifadown(&ia->ia_ifa, 1);
473 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
478 if (ifp == 0 || ifp->if_ioctl == 0)
481 error = (*ifp->if_ioctl)(ifp, cmd, data);
482 IFF_UNLOCKGIANT(ifp);
487 * Protect from ipintr() traversing address list while we're modifying
491 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
492 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
493 if (ia->ia_addr.sin_family == AF_INET) {
494 LIST_REMOVE(ia, ia_hash);
496 * If this is the last IPv4 address configured on this
497 * interface, leave the all-hosts group.
498 * XXX: This is quite ugly because of locking and structure.
503 struct in_multi *inm;
507 IN_LOOKUP_MULTI(allhosts_addr, ifp, inm);
509 in_delmulti_locked(inm);
511 IFF_UNLOCKGIANT(ifp);
514 IFAFREE(&ia->ia_ifa);
522 * SIOCGLIFADDR: get first address. (?!?)
523 * SIOCGLIFADDR with IFLR_PREFIX:
524 * get first address that matches the specified prefix.
525 * SIOCALIFADDR: add the specified address.
526 * SIOCALIFADDR with IFLR_PREFIX:
527 * EINVAL since we can't deduce hostid part of the address.
528 * SIOCDLIFADDR: delete the specified address.
529 * SIOCDLIFADDR with IFLR_PREFIX:
530 * delete the first address that matches the specified prefix.
532 * EINVAL on invalid parameters
533 * EADDRNOTAVAIL on prefix match failed/specified address not found
534 * other values may be returned from in_ioctl()
537 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
538 struct ifnet *ifp, struct thread *td)
540 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
545 panic("invalid argument to in_lifaddr_ioctl");
551 /* address must be specified on GET with IFLR_PREFIX */
552 if ((iflr->flags & IFLR_PREFIX) == 0)
557 /* address must be specified on ADD and DELETE */
558 if (iflr->addr.ss_family != AF_INET)
560 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
562 /* XXX need improvement */
563 if (iflr->dstaddr.ss_family
564 && iflr->dstaddr.ss_family != AF_INET)
566 if (iflr->dstaddr.ss_family
567 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
570 default: /*shouldn't happen*/
573 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
579 struct in_aliasreq ifra;
581 if (iflr->flags & IFLR_PREFIX)
584 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
585 bzero(&ifra, sizeof(ifra));
586 bcopy(iflr->iflr_name, ifra.ifra_name,
587 sizeof(ifra.ifra_name));
589 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
591 if (iflr->dstaddr.ss_family) { /*XXX*/
592 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
593 iflr->dstaddr.ss_len);
596 ifra.ifra_mask.sin_family = AF_INET;
597 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
598 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
600 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
605 struct in_ifaddr *ia;
606 struct in_addr mask, candidate, match;
607 struct sockaddr_in *sin;
610 bzero(&mask, sizeof(mask));
611 if (iflr->flags & IFLR_PREFIX) {
612 /* lookup a prefix rather than address. */
613 in_len2mask(&mask, iflr->prefixlen);
615 sin = (struct sockaddr_in *)&iflr->addr;
616 match.s_addr = sin->sin_addr.s_addr;
617 match.s_addr &= mask.s_addr;
619 /* if you set extra bits, that's wrong */
620 if (match.s_addr != sin->sin_addr.s_addr)
625 if (cmd == SIOCGLIFADDR) {
626 /* on getting an address, take the 1st match */
629 /* on deleting an address, do exact match */
630 in_len2mask(&mask, 32);
631 sin = (struct sockaddr_in *)&iflr->addr;
632 match.s_addr = sin->sin_addr.s_addr;
638 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
639 if (ifa->ifa_addr->sa_family != AF_INET6)
643 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
644 candidate.s_addr &= mask.s_addr;
645 if (candidate.s_addr == match.s_addr)
649 return EADDRNOTAVAIL;
650 ia = (struct in_ifaddr *)ifa;
652 if (cmd == SIOCGLIFADDR) {
653 /* fill in the if_laddrreq structure */
654 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
656 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
657 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
658 ia->ia_dstaddr.sin_len);
660 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
663 in_mask2len(&ia->ia_sockmask.sin_addr);
665 iflr->flags = 0; /*XXX*/
669 struct in_aliasreq ifra;
671 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
672 bzero(&ifra, sizeof(ifra));
673 bcopy(iflr->iflr_name, ifra.ifra_name,
674 sizeof(ifra.ifra_name));
676 bcopy(&ia->ia_addr, &ifra.ifra_addr,
677 ia->ia_addr.sin_len);
678 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
679 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
680 ia->ia_dstaddr.sin_len);
682 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
683 ia->ia_sockmask.sin_len);
685 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
691 return EOPNOTSUPP; /*just for safety*/
695 * Delete any existing route for an interface.
698 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
705 * Initialize an interface's internet address
706 * and routing table entry.
709 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
712 register u_long i = ntohl(sin->sin_addr.s_addr);
713 struct sockaddr_in oldaddr;
714 int s = splimp(), flags = RTF_UP, error = 0;
716 oldaddr = ia->ia_addr;
717 if (oldaddr.sin_family == AF_INET)
718 LIST_REMOVE(ia, ia_hash);
720 if (ia->ia_addr.sin_family == AF_INET)
721 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
724 * Give the interface a chance to initialize
725 * if this is its first address,
726 * and to validate the address if necessary.
730 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
731 IFF_UNLOCKGIANT(ifp);
734 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
735 ia->ia_addr = oldaddr;
736 if (ia->ia_addr.sin_family == AF_INET)
737 LIST_INSERT_HEAD(INADDR_HASH(
738 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
744 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
746 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
749 ia->ia_netmask = IN_CLASSA_NET;
750 else if (IN_CLASSB(i))
751 ia->ia_netmask = IN_CLASSB_NET;
753 ia->ia_netmask = IN_CLASSC_NET;
755 * The subnet mask usually includes at least the standard network part,
756 * but may may be smaller in the case of supernetting.
757 * If it is set, we believe it.
759 if (ia->ia_subnetmask == 0) {
760 ia->ia_subnetmask = ia->ia_netmask;
761 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
763 ia->ia_netmask &= ia->ia_subnetmask;
764 ia->ia_net = i & ia->ia_netmask;
765 ia->ia_subnet = i & ia->ia_subnetmask;
766 in_socktrim(&ia->ia_sockmask);
769 * XXX: carp(4) does not have interface route
771 if (ifp->if_type == IFT_CARP)
775 * Add route for the network.
777 ia->ia_ifa.ifa_metric = ifp->if_metric;
778 if (ifp->if_flags & IFF_BROADCAST) {
779 ia->ia_broadaddr.sin_addr.s_addr =
780 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
781 ia->ia_netbroadcast.s_addr =
782 htonl(ia->ia_net | ~ ia->ia_netmask);
783 } else if (ifp->if_flags & IFF_LOOPBACK) {
784 ia->ia_dstaddr = ia->ia_addr;
786 } else if (ifp->if_flags & IFF_POINTOPOINT) {
787 if (ia->ia_dstaddr.sin_family != AF_INET)
791 if ((error = in_addprefix(ia, flags)) != 0)
797 #define rtinitflags(x) \
798 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
801 * Check if we have a route for the given prefix already or add a one
805 in_addprefix(struct in_ifaddr *target, int flags)
807 struct in_ifaddr *ia;
808 struct in_addr prefix, mask, p, m;
811 if ((flags & RTF_HOST) != 0)
812 prefix = target->ia_dstaddr.sin_addr;
814 prefix = target->ia_addr.sin_addr;
815 mask = target->ia_sockmask.sin_addr;
816 prefix.s_addr &= mask.s_addr;
819 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
820 if (rtinitflags(ia)) {
821 p = ia->ia_addr.sin_addr;
823 if (prefix.s_addr != p.s_addr)
826 p = ia->ia_addr.sin_addr;
827 m = ia->ia_sockmask.sin_addr;
828 p.s_addr &= m.s_addr;
830 if (prefix.s_addr != p.s_addr ||
831 mask.s_addr != m.s_addr)
836 * If we got a matching prefix route inserted by other
837 * interface address, we are done here.
839 if (ia->ia_flags & IFA_ROUTE) {
840 if (sameprefixcarponly &&
841 target->ia_ifp->if_type != IFT_CARP &&
842 ia->ia_ifp->if_type != IFT_CARP)
850 * No-one seem to have this prefix route, so we try to insert it.
852 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
854 target->ia_flags |= IFA_ROUTE;
859 * If there is no other address in the system that can serve a route to the
860 * same prefix, remove the route. Hand over the route to the new address
864 in_scrubprefix(struct in_ifaddr *target)
866 struct in_ifaddr *ia;
867 struct in_addr prefix, mask, p;
870 if ((target->ia_flags & IFA_ROUTE) == 0)
873 if (rtinitflags(target))
874 prefix = target->ia_dstaddr.sin_addr;
876 prefix = target->ia_addr.sin_addr;
877 mask = target->ia_sockmask.sin_addr;
878 prefix.s_addr &= mask.s_addr;
881 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
883 p = ia->ia_dstaddr.sin_addr;
885 p = ia->ia_addr.sin_addr;
886 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
889 if (prefix.s_addr != p.s_addr)
893 * If we got a matching prefix address, move IFA_ROUTE and
894 * the route itself to it. Make sure that routing daemons
897 * XXX: a special case for carp(4) interface
899 if ((ia->ia_flags & IFA_ROUTE) == 0
901 && (ia->ia_ifp->if_type != IFT_CARP)
904 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
905 rtinitflags(target));
906 target->ia_flags &= ~IFA_ROUTE;
908 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
909 rtinitflags(ia) | RTF_UP);
911 ia->ia_flags |= IFA_ROUTE;
917 * As no-one seem to have this prefix, we can remove the route.
919 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
920 target->ia_flags &= ~IFA_ROUTE;
927 * Return 1 if the address might be a local broadcast address.
930 in_broadcast(struct in_addr in, struct ifnet *ifp)
932 register struct ifaddr *ifa;
935 if (in.s_addr == INADDR_BROADCAST ||
936 in.s_addr == INADDR_ANY)
938 if ((ifp->if_flags & IFF_BROADCAST) == 0)
940 t = ntohl(in.s_addr);
942 * Look through the list of addresses for a match
943 * with a broadcast address.
945 #define ia ((struct in_ifaddr *)ifa)
946 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
947 if (ifa->ifa_addr->sa_family == AF_INET &&
948 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
949 in.s_addr == ia->ia_netbroadcast.s_addr ||
951 * Check for old-style (host 0) broadcast.
953 t == ia->ia_subnet || t == ia->ia_net) &&
955 * Check for an all one subnetmask. These
956 * only exist when an interface gets a secondary
959 ia->ia_subnetmask != (u_long)0xffffffff)
966 * Delete all IPv4 multicast address records, and associated link-layer
967 * multicast address records, associated with ifp.
970 in_purgemaddrs(struct ifnet *ifp)
972 struct in_multi *inm;
973 struct in_multi *oinm;
976 printf("%s: purging ifp %p\n", __func__, ifp);
980 LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
981 if (inm->inm_ifp == ifp)
982 in_delmulti_locked(inm);
985 IFF_UNLOCKGIANT(ifp);
989 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
992 in_ifdetach(struct ifnet *ifp)
995 in_pcbpurgeif0(&ripcbinfo, ifp);
996 in_pcbpurgeif0(&udbinfo, ifp);