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 *);
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");
77 extern struct inpcbinfo ripcbinfo;
78 extern struct inpcbinfo udbinfo;
81 * Return 1 if an internet address is for a ``local'' host
82 * (one to which we have a connection). If subnetsarelocal
83 * is true, this includes other subnets of the local net.
84 * Otherwise, it includes only the directly-connected (sub)nets.
87 in_localaddr(struct in_addr in)
89 register u_long i = ntohl(in.s_addr);
90 register struct in_ifaddr *ia;
92 if (V_subnetsarelocal) {
93 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
94 if ((i & ia->ia_netmask) == ia->ia_net)
97 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
98 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
105 * Return 1 if an internet address is for the local host and configured
106 * on one of its interfaces.
109 in_localip(struct in_addr in)
111 struct in_ifaddr *ia;
113 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
114 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
121 * Determine whether an IP address is in a reserved set of addresses
122 * that may not be forwarded, or whether datagrams to that destination
126 in_canforward(struct in_addr in)
128 register u_long i = ntohl(in.s_addr);
131 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
134 net = i & IN_CLASSA_NET;
135 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
142 * Trim a mask in a sockaddr
145 in_socktrim(struct sockaddr_in *ap)
147 register char *cplim = (char *) &ap->sin_addr;
148 register char *cp = (char *) (&ap->sin_addr + 1);
151 while (--cp >= cplim)
153 (ap)->sin_len = cp - (char *) (ap) + 1;
160 struct in_addr *mask;
166 for (x = 0; x < sizeof(*mask); x++) {
171 if (x < sizeof(*mask)) {
172 for (y = 0; y < 8; y++) {
173 if ((p[x] & (0x80 >> y)) == 0)
181 in_len2mask(struct in_addr *mask, int len)
187 bzero(mask, sizeof(*mask));
188 for (i = 0; i < len / 8; i++)
191 p[i] = (0xff00 >> (len % 8)) & 0xff;
195 * Generic internet control operations (ioctl's).
196 * Ifp is 0 if not an interface-specific ioctl.
200 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
203 register struct ifreq *ifr = (struct ifreq *)data;
204 register struct in_ifaddr *ia = 0, *iap;
205 register struct ifaddr *ifa;
206 struct in_addr allhosts_addr;
208 struct in_ifaddr *oia;
209 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
210 struct sockaddr_in oldaddr;
211 int error, hostIsNew, iaIsNew, maskIsNew, s;
216 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
221 error = priv_check(td, PRIV_NET_ADDIFADDR);
227 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
231 error = priv_check(td, PRIV_NET_DELIFADDR);
237 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
242 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
246 * Find address for this interface, if it exists.
248 * If an alias address was specified, find that one instead of
249 * the first one on the interface, if possible.
252 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
253 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
254 if (iap->ia_ifp == ifp &&
255 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
260 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
262 if (iap->ia_addr.sin_family == AF_INET) {
276 return (EADDRNOTAVAIL);
277 if (ifra->ifra_addr.sin_family == AF_INET) {
278 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
279 if (ia->ia_ifp == ifp &&
280 ia->ia_addr.sin_addr.s_addr ==
281 ifra->ifra_addr.sin_addr.s_addr)
284 if ((ifp->if_flags & IFF_POINTOPOINT)
285 && (cmd == SIOCAIFADDR)
286 && (ifra->ifra_dstaddr.sin_addr.s_addr
291 if (cmd == SIOCDIFADDR && ia == 0)
292 return (EADDRNOTAVAIL);
298 error = priv_check(td, (cmd == SIOCDIFADDR) ?
299 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
305 return (EADDRNOTAVAIL);
306 if (ia == (struct in_ifaddr *)0) {
307 ia = (struct in_ifaddr *)
308 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
309 if (ia == (struct in_ifaddr *)NULL)
312 * Protect from ipintr() traversing address list
313 * while we're modifying it.
318 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
319 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
320 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
322 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
324 ia->ia_sockmask.sin_len = 8;
325 ia->ia_sockmask.sin_family = AF_INET;
326 if (ifp->if_flags & IFF_BROADCAST) {
327 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
328 ia->ia_broadaddr.sin_family = AF_INET;
332 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
340 error = priv_check(td, PRIV_NET_ADDIFADDR);
350 if (ia == (struct in_ifaddr *)0)
351 return (EADDRNOTAVAIL);
357 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
361 if ((ifp->if_flags & IFF_BROADCAST) == 0)
363 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
367 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
369 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
373 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
377 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
379 oldaddr = ia->ia_dstaddr;
380 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
383 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
385 IFF_UNLOCKGIANT(ifp);
387 ia->ia_dstaddr = oldaddr;
391 if (ia->ia_flags & IFA_ROUTE) {
392 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
393 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
394 ia->ia_ifa.ifa_dstaddr =
395 (struct sockaddr *)&ia->ia_dstaddr;
396 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
401 if ((ifp->if_flags & IFF_BROADCAST) == 0)
403 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
407 error = in_ifinit(ifp, ia,
408 (struct sockaddr_in *) &ifr->ifr_addr, 1);
409 if (error != 0 && iaIsNew)
412 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
413 in_addmulti(&allhosts_addr, ifp);
414 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
419 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
420 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
427 if (ia->ia_addr.sin_family == AF_INET) {
428 if (ifra->ifra_addr.sin_len == 0) {
429 ifra->ifra_addr = ia->ia_addr;
431 } else if (ifra->ifra_addr.sin_addr.s_addr ==
432 ia->ia_addr.sin_addr.s_addr)
435 if (ifra->ifra_mask.sin_len) {
437 ia->ia_sockmask = ifra->ifra_mask;
438 ia->ia_sockmask.sin_family = AF_INET;
440 ntohl(ia->ia_sockmask.sin_addr.s_addr);
443 if ((ifp->if_flags & IFF_POINTOPOINT) &&
444 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
446 ia->ia_dstaddr = ifra->ifra_dstaddr;
447 maskIsNew = 1; /* We lie; but the effect's the same */
449 if (ifra->ifra_addr.sin_family == AF_INET &&
450 (hostIsNew || maskIsNew))
451 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
452 if (error != 0 && iaIsNew)
455 if ((ifp->if_flags & IFF_BROADCAST) &&
456 (ifra->ifra_broadaddr.sin_family == AF_INET))
457 ia->ia_broadaddr = ifra->ifra_broadaddr;
459 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
460 in_addmulti(&allhosts_addr, ifp);
461 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
467 * in_ifscrub kills the interface route.
471 * in_ifadown gets rid of all the rest of
472 * the routes. This is not quite the right
473 * thing to do, but at least if we are running
474 * a routing process they will come back.
476 in_ifadown(&ia->ia_ifa, 1);
477 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
482 if (ifp == 0 || ifp->if_ioctl == 0)
485 error = (*ifp->if_ioctl)(ifp, cmd, data);
486 IFF_UNLOCKGIANT(ifp);
491 * Protect from ipintr() traversing address list while we're modifying
495 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
496 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
497 if (ia->ia_addr.sin_family == AF_INET) {
498 LIST_REMOVE(ia, ia_hash);
500 * If this is the last IPv4 address configured on this
501 * interface, leave the all-hosts group.
502 * XXX: This is quite ugly because of locking and structure.
507 struct in_multi *inm;
511 IN_LOOKUP_MULTI(allhosts_addr, ifp, inm);
513 in_delmulti_locked(inm);
515 IFF_UNLOCKGIANT(ifp);
518 IFAFREE(&ia->ia_ifa);
526 * SIOCGLIFADDR: get first address. (?!?)
527 * SIOCGLIFADDR with IFLR_PREFIX:
528 * get first address that matches the specified prefix.
529 * SIOCALIFADDR: add the specified address.
530 * SIOCALIFADDR with IFLR_PREFIX:
531 * EINVAL since we can't deduce hostid part of the address.
532 * SIOCDLIFADDR: delete the specified address.
533 * SIOCDLIFADDR with IFLR_PREFIX:
534 * delete the first address that matches the specified prefix.
536 * EINVAL on invalid parameters
537 * EADDRNOTAVAIL on prefix match failed/specified address not found
538 * other values may be returned from in_ioctl()
541 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
542 struct ifnet *ifp, struct thread *td)
544 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
549 panic("invalid argument to in_lifaddr_ioctl");
555 /* address must be specified on GET with IFLR_PREFIX */
556 if ((iflr->flags & IFLR_PREFIX) == 0)
561 /* address must be specified on ADD and DELETE */
562 if (iflr->addr.ss_family != AF_INET)
564 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
566 /* XXX need improvement */
567 if (iflr->dstaddr.ss_family
568 && iflr->dstaddr.ss_family != AF_INET)
570 if (iflr->dstaddr.ss_family
571 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
574 default: /*shouldn't happen*/
577 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
583 struct in_aliasreq ifra;
585 if (iflr->flags & IFLR_PREFIX)
588 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
589 bzero(&ifra, sizeof(ifra));
590 bcopy(iflr->iflr_name, ifra.ifra_name,
591 sizeof(ifra.ifra_name));
593 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
595 if (iflr->dstaddr.ss_family) { /*XXX*/
596 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
597 iflr->dstaddr.ss_len);
600 ifra.ifra_mask.sin_family = AF_INET;
601 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
602 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
604 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
609 struct in_ifaddr *ia;
610 struct in_addr mask, candidate, match;
611 struct sockaddr_in *sin;
613 bzero(&mask, sizeof(mask));
614 bzero(&match, sizeof(match));
615 if (iflr->flags & IFLR_PREFIX) {
616 /* lookup a prefix rather than address. */
617 in_len2mask(&mask, iflr->prefixlen);
619 sin = (struct sockaddr_in *)&iflr->addr;
620 match.s_addr = sin->sin_addr.s_addr;
621 match.s_addr &= mask.s_addr;
623 /* if you set extra bits, that's wrong */
624 if (match.s_addr != sin->sin_addr.s_addr)
628 /* on getting an address, take the 1st match */
629 /* on deleting an address, do exact match */
630 if (cmd != SIOCGLIFADDR) {
631 in_len2mask(&mask, 32);
632 sin = (struct sockaddr_in *)&iflr->addr;
633 match.s_addr = sin->sin_addr.s_addr;
637 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
638 if (ifa->ifa_addr->sa_family != AF_INET6)
640 if (match.s_addr == 0)
642 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
643 candidate.s_addr &= mask.s_addr;
644 if (candidate.s_addr == match.s_addr)
648 return EADDRNOTAVAIL;
649 ia = (struct in_ifaddr *)ifa;
651 if (cmd == SIOCGLIFADDR) {
652 /* fill in the if_laddrreq structure */
653 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
655 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
656 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
657 ia->ia_dstaddr.sin_len);
659 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
662 in_mask2len(&ia->ia_sockmask.sin_addr);
664 iflr->flags = 0; /*XXX*/
668 struct in_aliasreq ifra;
670 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
671 bzero(&ifra, sizeof(ifra));
672 bcopy(iflr->iflr_name, ifra.ifra_name,
673 sizeof(ifra.ifra_name));
675 bcopy(&ia->ia_addr, &ifra.ifra_addr,
676 ia->ia_addr.sin_len);
677 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
678 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
679 ia->ia_dstaddr.sin_len);
681 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
682 ia->ia_sockmask.sin_len);
684 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
690 return EOPNOTSUPP; /*just for safety*/
694 * Delete any existing route for an interface.
697 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
704 * Initialize an interface's internet address
705 * and routing table entry.
708 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
711 register u_long i = ntohl(sin->sin_addr.s_addr);
712 struct sockaddr_in oldaddr;
713 int s = splimp(), flags = RTF_UP, error = 0;
715 oldaddr = ia->ia_addr;
716 if (oldaddr.sin_family == AF_INET)
717 LIST_REMOVE(ia, ia_hash);
719 if (ia->ia_addr.sin_family == AF_INET)
720 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
723 * Give the interface a chance to initialize
724 * if this is its first address,
725 * and to validate the address if necessary.
729 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
730 IFF_UNLOCKGIANT(ifp);
733 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
734 ia->ia_addr = oldaddr;
735 if (ia->ia_addr.sin_family == AF_INET)
736 LIST_INSERT_HEAD(INADDR_HASH(
737 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
740 * If oldaddr family is not AF_INET (e.g.
741 * interface has been just created) in_control
742 * does not call LIST_REMOVE, and we end up
743 * with bogus ia entries in hash
745 LIST_REMOVE(ia, ia_hash);
751 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
753 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
756 ia->ia_netmask = IN_CLASSA_NET;
757 else if (IN_CLASSB(i))
758 ia->ia_netmask = IN_CLASSB_NET;
760 ia->ia_netmask = IN_CLASSC_NET;
762 * The subnet mask usually includes at least the standard network part,
763 * but may may be smaller in the case of supernetting.
764 * If it is set, we believe it.
766 if (ia->ia_subnetmask == 0) {
767 ia->ia_subnetmask = ia->ia_netmask;
768 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
770 ia->ia_netmask &= ia->ia_subnetmask;
771 ia->ia_net = i & ia->ia_netmask;
772 ia->ia_subnet = i & ia->ia_subnetmask;
773 in_socktrim(&ia->ia_sockmask);
776 * XXX: carp(4) does not have interface route
778 if (ifp->if_type == IFT_CARP)
782 * Add route for the network.
784 ia->ia_ifa.ifa_metric = ifp->if_metric;
785 if (ifp->if_flags & IFF_BROADCAST) {
786 ia->ia_broadaddr.sin_addr.s_addr =
787 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
788 ia->ia_netbroadcast.s_addr =
789 htonl(ia->ia_net | ~ ia->ia_netmask);
790 } else if (ifp->if_flags & IFF_LOOPBACK) {
791 ia->ia_dstaddr = ia->ia_addr;
793 } else if (ifp->if_flags & IFF_POINTOPOINT) {
794 if (ia->ia_dstaddr.sin_family != AF_INET)
798 if ((error = in_addprefix(ia, flags)) != 0)
804 #define rtinitflags(x) \
805 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
808 * Check if we have a route for the given prefix already or add one accordingly.
811 in_addprefix(struct in_ifaddr *target, int flags)
813 struct in_ifaddr *ia;
814 struct in_addr prefix, mask, p, m;
817 if ((flags & RTF_HOST) != 0) {
818 prefix = target->ia_dstaddr.sin_addr;
821 prefix = target->ia_addr.sin_addr;
822 mask = target->ia_sockmask.sin_addr;
823 prefix.s_addr &= mask.s_addr;
826 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
827 if (rtinitflags(ia)) {
828 p = ia->ia_addr.sin_addr;
830 if (prefix.s_addr != p.s_addr)
833 p = ia->ia_addr.sin_addr;
834 m = ia->ia_sockmask.sin_addr;
835 p.s_addr &= m.s_addr;
837 if (prefix.s_addr != p.s_addr ||
838 mask.s_addr != m.s_addr)
843 * If we got a matching prefix route inserted by other
844 * interface address, we are done here.
846 if (ia->ia_flags & IFA_ROUTE) {
847 if (V_sameprefixcarponly &&
848 target->ia_ifp->if_type != IFT_CARP &&
849 ia->ia_ifp->if_type != IFT_CARP)
857 * No-one seem to have this prefix route, so we try to insert it.
859 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
861 target->ia_flags |= IFA_ROUTE;
866 * If there is no other address in the system that can serve a route to the
867 * same prefix, remove the route. Hand over the route to the new address
871 in_scrubprefix(struct in_ifaddr *target)
873 struct in_ifaddr *ia;
874 struct in_addr prefix, mask, p;
877 if ((target->ia_flags & IFA_ROUTE) == 0)
880 if (rtinitflags(target))
881 prefix = target->ia_dstaddr.sin_addr;
883 prefix = target->ia_addr.sin_addr;
884 mask = target->ia_sockmask.sin_addr;
885 prefix.s_addr &= mask.s_addr;
888 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
890 p = ia->ia_dstaddr.sin_addr;
892 p = ia->ia_addr.sin_addr;
893 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
896 if (prefix.s_addr != p.s_addr)
900 * If we got a matching prefix address, move IFA_ROUTE and
901 * the route itself to it. Make sure that routing daemons
904 * XXX: a special case for carp(4) interface
906 if ((ia->ia_flags & IFA_ROUTE) == 0
908 && (ia->ia_ifp->if_type != IFT_CARP)
911 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
912 rtinitflags(target));
913 target->ia_flags &= ~IFA_ROUTE;
915 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
916 rtinitflags(ia) | RTF_UP);
918 ia->ia_flags |= IFA_ROUTE;
924 * As no-one seem to have this prefix, we can remove the route.
926 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
927 target->ia_flags &= ~IFA_ROUTE;
934 * Return 1 if the address might be a local broadcast address.
937 in_broadcast(struct in_addr in, struct ifnet *ifp)
939 register struct ifaddr *ifa;
942 if (in.s_addr == INADDR_BROADCAST ||
943 in.s_addr == INADDR_ANY)
945 if ((ifp->if_flags & IFF_BROADCAST) == 0)
947 t = ntohl(in.s_addr);
949 * Look through the list of addresses for a match
950 * with a broadcast address.
952 #define ia ((struct in_ifaddr *)ifa)
953 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
954 if (ifa->ifa_addr->sa_family == AF_INET &&
955 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
956 in.s_addr == ia->ia_netbroadcast.s_addr ||
958 * Check for old-style (host 0) broadcast.
960 t == ia->ia_subnet || t == ia->ia_net) &&
962 * Check for an all one subnetmask. These
963 * only exist when an interface gets a secondary
966 ia->ia_subnetmask != (u_long)0xffffffff)
973 * Delete all IPv4 multicast address records, and associated link-layer
974 * multicast address records, associated with ifp.
977 in_purgemaddrs(struct ifnet *ifp)
979 struct in_multi *inm;
980 struct in_multi *oinm;
983 printf("%s: purging ifp %p\n", __func__, ifp);
987 LIST_FOREACH_SAFE(inm, &V_in_multihead, inm_link, oinm) {
988 if (inm->inm_ifp == ifp)
989 in_delmulti_locked(inm);
992 IFF_UNLOCKGIANT(ifp);
996 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
999 in_ifdetach(struct ifnet *ifp)
1002 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1003 in_pcbpurgeif0(&V_udbinfo, ifp);
1004 in_purgemaddrs(ifp);