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>
46 #include <sys/vimage.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/ip_var.h>
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 *);
70 static int subnetsarelocal;
71 static int sameprefixcarponly;
72 extern struct inpcbinfo ripcbinfo;
73 extern struct inpcbinfo udbinfo;
76 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, subnets_are_local,
77 CTLFLAG_RW, subnetsarelocal, 0,
78 "Treat all subnets as directly connected");
79 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, same_prefix_carp_only,
80 CTLFLAG_RW, sameprefixcarponly, 0,
81 "Refuse to create same prefixes on different interfaces");
84 * Return 1 if an internet address is for a ``local'' host
85 * (one to which we have a connection). If subnetsarelocal
86 * is true, this includes other subnets of the local net.
87 * Otherwise, it includes only the directly-connected (sub)nets.
90 in_localaddr(struct in_addr in)
92 INIT_VNET_INET(curvnet);
93 register u_long i = ntohl(in.s_addr);
94 register struct in_ifaddr *ia;
96 if (V_subnetsarelocal) {
97 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
98 if ((i & ia->ia_netmask) == ia->ia_net)
101 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
102 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
109 * Return 1 if an internet address is for the local host and configured
110 * on one of its interfaces.
113 in_localip(struct in_addr in)
115 INIT_VNET_INET(curvnet);
116 struct in_ifaddr *ia;
118 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
119 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
126 * Determine whether an IP address is in a reserved set of addresses
127 * that may not be forwarded, or whether datagrams to that destination
131 in_canforward(struct in_addr in)
133 register u_long i = ntohl(in.s_addr);
136 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
139 net = i & IN_CLASSA_NET;
140 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
147 * Trim a mask in a sockaddr
150 in_socktrim(struct sockaddr_in *ap)
152 register char *cplim = (char *) &ap->sin_addr;
153 register char *cp = (char *) (&ap->sin_addr + 1);
156 while (--cp >= cplim)
158 (ap)->sin_len = cp - (char *) (ap) + 1;
165 struct in_addr *mask;
171 for (x = 0; x < sizeof(*mask); x++) {
176 if (x < sizeof(*mask)) {
177 for (y = 0; y < 8; y++) {
178 if ((p[x] & (0x80 >> y)) == 0)
186 in_len2mask(struct in_addr *mask, int len)
192 bzero(mask, sizeof(*mask));
193 for (i = 0; i < len / 8; i++)
196 p[i] = (0xff00 >> (len % 8)) & 0xff;
200 * Generic internet control operations (ioctl's).
201 * Ifp is 0 if not an interface-specific ioctl.
205 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
208 INIT_VNET_INET(curvnet); /* both so and ifp can be NULL here! */
209 register struct ifreq *ifr = (struct ifreq *)data;
210 register struct in_ifaddr *ia, *iap;
211 register struct ifaddr *ifa;
212 struct in_addr allhosts_addr;
214 struct in_ifaddr *oia;
215 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
216 struct sockaddr_in oldaddr;
217 int error, hostIsNew, iaIsNew, maskIsNew, s;
223 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
228 error = priv_check(td, PRIV_NET_ADDIFADDR);
234 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
238 error = priv_check(td, PRIV_NET_DELIFADDR);
244 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
249 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
253 * Find address for this interface, if it exists.
255 * If an alias address was specified, find that one instead of
256 * the first one on the interface, if possible.
259 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
260 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
261 if (iap->ia_ifp == ifp &&
262 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
267 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
269 if (iap->ia_addr.sin_family == AF_INET) {
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
295 return (EDESTADDRREQ);
298 if (cmd == SIOCDIFADDR && ia == NULL)
299 return (EADDRNOTAVAIL);
305 error = priv_check(td, (cmd == SIOCDIFADDR) ?
306 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
312 return (EADDRNOTAVAIL);
314 ia = (struct in_ifaddr *)
315 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
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(&V_in_ifaddrhead, ia, ia_link);
347 error = priv_check(td, PRIV_NET_ADDIFADDR);
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;
388 if (ifp->if_ioctl != NULL) {
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 == NULL || ifp->if_ioctl == NULL)
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(&V_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;
555 if (data == NULL || ifp == NULL) {
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 INIT_VNET_INET(ifp->if_vnet);
719 register u_long i = ntohl(sin->sin_addr.s_addr);
720 struct sockaddr_in oldaddr;
721 int s = splimp(), flags = RTF_UP, error = 0;
723 oldaddr = ia->ia_addr;
724 if (oldaddr.sin_family == AF_INET)
725 LIST_REMOVE(ia, ia_hash);
727 if (ia->ia_addr.sin_family == AF_INET)
728 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
731 * Give the interface a chance to initialize
732 * if this is its first address,
733 * and to validate the address if necessary.
735 if (ifp->if_ioctl != NULL) {
737 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
738 IFF_UNLOCKGIANT(ifp);
741 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
742 ia->ia_addr = oldaddr;
743 if (ia->ia_addr.sin_family == AF_INET)
744 LIST_INSERT_HEAD(INADDR_HASH(
745 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
748 * If oldaddr family is not AF_INET (e.g.
749 * interface has been just created) in_control
750 * does not call LIST_REMOVE, and we end up
751 * with bogus ia entries in hash
753 LIST_REMOVE(ia, ia_hash);
759 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
761 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
764 ia->ia_netmask = IN_CLASSA_NET;
765 else if (IN_CLASSB(i))
766 ia->ia_netmask = IN_CLASSB_NET;
768 ia->ia_netmask = IN_CLASSC_NET;
770 * The subnet mask usually includes at least the standard network part,
771 * but may may be smaller in the case of supernetting.
772 * If it is set, we believe it.
774 if (ia->ia_subnetmask == 0) {
775 ia->ia_subnetmask = ia->ia_netmask;
776 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
778 ia->ia_netmask &= ia->ia_subnetmask;
779 ia->ia_net = i & ia->ia_netmask;
780 ia->ia_subnet = i & ia->ia_subnetmask;
781 in_socktrim(&ia->ia_sockmask);
784 * XXX: carp(4) does not have interface route
786 if (ifp->if_type == IFT_CARP)
790 * Add route for the network.
792 ia->ia_ifa.ifa_metric = ifp->if_metric;
793 if (ifp->if_flags & IFF_BROADCAST) {
794 ia->ia_broadaddr.sin_addr.s_addr =
795 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
796 ia->ia_netbroadcast.s_addr =
797 htonl(ia->ia_net | ~ ia->ia_netmask);
798 } else if (ifp->if_flags & IFF_LOOPBACK) {
799 ia->ia_dstaddr = ia->ia_addr;
801 } else if (ifp->if_flags & IFF_POINTOPOINT) {
802 if (ia->ia_dstaddr.sin_family != AF_INET)
806 if ((error = in_addprefix(ia, flags)) != 0)
812 #define rtinitflags(x) \
813 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
816 * Check if we have a route for the given prefix already or add one accordingly.
819 in_addprefix(struct in_ifaddr *target, int flags)
821 INIT_VNET_INET(curvnet);
822 struct in_ifaddr *ia;
823 struct in_addr prefix, mask, p, m;
826 if ((flags & RTF_HOST) != 0) {
827 prefix = target->ia_dstaddr.sin_addr;
830 prefix = target->ia_addr.sin_addr;
831 mask = target->ia_sockmask.sin_addr;
832 prefix.s_addr &= mask.s_addr;
835 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
836 if (rtinitflags(ia)) {
837 p = ia->ia_addr.sin_addr;
839 if (prefix.s_addr != p.s_addr)
842 p = ia->ia_addr.sin_addr;
843 m = ia->ia_sockmask.sin_addr;
844 p.s_addr &= m.s_addr;
846 if (prefix.s_addr != p.s_addr ||
847 mask.s_addr != m.s_addr)
852 * If we got a matching prefix route inserted by other
853 * interface address, we are done here.
855 if (ia->ia_flags & IFA_ROUTE) {
856 if (V_sameprefixcarponly &&
857 target->ia_ifp->if_type != IFT_CARP &&
858 ia->ia_ifp->if_type != IFT_CARP)
866 * No-one seem to have this prefix route, so we try to insert it.
868 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
870 target->ia_flags |= IFA_ROUTE;
875 * If there is no other address in the system that can serve a route to the
876 * same prefix, remove the route. Hand over the route to the new address
880 in_scrubprefix(struct in_ifaddr *target)
882 INIT_VNET_INET(curvnet);
883 struct in_ifaddr *ia;
884 struct in_addr prefix, mask, p;
887 if ((target->ia_flags & IFA_ROUTE) == 0)
890 if (rtinitflags(target))
891 prefix = target->ia_dstaddr.sin_addr;
893 prefix = target->ia_addr.sin_addr;
894 mask = target->ia_sockmask.sin_addr;
895 prefix.s_addr &= mask.s_addr;
898 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
900 p = ia->ia_dstaddr.sin_addr;
902 p = ia->ia_addr.sin_addr;
903 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
906 if (prefix.s_addr != p.s_addr)
910 * If we got a matching prefix address, move IFA_ROUTE and
911 * the route itself to it. Make sure that routing daemons
914 * XXX: a special case for carp(4) interface
916 if ((ia->ia_flags & IFA_ROUTE) == 0
918 && (ia->ia_ifp->if_type != IFT_CARP)
921 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
922 rtinitflags(target));
923 target->ia_flags &= ~IFA_ROUTE;
925 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
926 rtinitflags(ia) | RTF_UP);
928 ia->ia_flags |= IFA_ROUTE;
934 * As no-one seem to have this prefix, we can remove the route.
936 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
937 target->ia_flags &= ~IFA_ROUTE;
944 * Return 1 if the address might be a local broadcast address.
947 in_broadcast(struct in_addr in, struct ifnet *ifp)
949 register struct ifaddr *ifa;
952 if (in.s_addr == INADDR_BROADCAST ||
953 in.s_addr == INADDR_ANY)
955 if ((ifp->if_flags & IFF_BROADCAST) == 0)
957 t = ntohl(in.s_addr);
959 * Look through the list of addresses for a match
960 * with a broadcast address.
962 #define ia ((struct in_ifaddr *)ifa)
963 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
964 if (ifa->ifa_addr->sa_family == AF_INET &&
965 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
966 in.s_addr == ia->ia_netbroadcast.s_addr ||
968 * Check for old-style (host 0) broadcast.
970 t == ia->ia_subnet || t == ia->ia_net) &&
972 * Check for an all one subnetmask. These
973 * only exist when an interface gets a secondary
976 ia->ia_subnetmask != (u_long)0xffffffff)
983 * Delete all IPv4 multicast address records, and associated link-layer
984 * multicast address records, associated with ifp.
987 in_purgemaddrs(struct ifnet *ifp)
989 INIT_VNET_INET(ifp->if_vnet);
990 struct in_multi *inm;
991 struct in_multi *oinm;
994 printf("%s: purging ifp %p\n", __func__, ifp);
998 LIST_FOREACH_SAFE(inm, &V_in_multihead, inm_link, oinm) {
999 if (inm->inm_ifp == ifp)
1000 in_delmulti_locked(inm);
1003 IFF_UNLOCKGIANT(ifp);
1007 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1010 in_ifdetach(struct ifnet *ifp)
1012 INIT_VNET_INET(ifp->if_vnet);
1014 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1015 in_pcbpurgeif0(&V_udbinfo, ifp);
1016 in_purgemaddrs(ifp);