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>
40 #include <sys/socket.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/in_pcb.h>
52 #include <netinet/igmp_var.h>
54 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
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 void in_delmulti_locked(register struct in_multi *, 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);
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");
77 * The IPv4 multicast list (in_multihead and associated structures) are
78 * protected by the global in_multi_mtx. See in_var.h for more details. For
79 * now, in_multi_mtx is marked as recursible due to IGMP's calling back into
80 * ip_output() to send IGMP packets while holding the lock; this probably is
81 * not quite desirable.
83 struct in_multihead in_multihead; /* XXX BSS initialization */
84 struct mtx in_multi_mtx;
85 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE);
87 extern struct inpcbinfo ripcbinfo;
88 extern struct inpcbinfo udbinfo;
91 * Return 1 if an internet address is for a ``local'' host
92 * (one to which we have a connection). If subnetsarelocal
93 * is true, this includes other subnets of the local net.
94 * Otherwise, it includes only the directly-connected (sub)nets.
100 register u_long i = ntohl(in.s_addr);
101 register struct in_ifaddr *ia;
103 if (subnetsarelocal) {
104 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
105 if ((i & ia->ia_netmask) == ia->ia_net)
108 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
109 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
116 * Return 1 if an internet address is for the local host and configured
117 * on one of its interfaces.
123 struct in_ifaddr *ia;
125 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
126 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
133 * Determine whether an IP address is in a reserved set of addresses
134 * that may not be forwarded, or whether datagrams to that destination
141 register u_long i = ntohl(in.s_addr);
144 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
147 net = i & IN_CLASSA_NET;
148 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
155 * Trim a mask in a sockaddr
159 struct sockaddr_in *ap;
161 register char *cplim = (char *) &ap->sin_addr;
162 register char *cp = (char *) (&ap->sin_addr + 1);
165 while (--cp >= cplim)
167 (ap)->sin_len = cp - (char *) (ap) + 1;
174 struct in_addr *mask;
180 for (x = 0; x < sizeof(*mask); x++) {
185 if (x < sizeof(*mask)) {
186 for (y = 0; y < 8; y++) {
187 if ((p[x] & (0x80 >> y)) == 0)
195 in_len2mask(mask, len)
196 struct in_addr *mask;
203 bzero(mask, sizeof(*mask));
204 for (i = 0; i < len / 8; i++)
207 p[i] = (0xff00 >> (len % 8)) & 0xff;
211 * Generic internet control operations (ioctl's).
212 * Ifp is 0 if not an interface-specific ioctl.
216 in_control(so, cmd, data, ifp, td)
220 register struct ifnet *ifp;
223 register struct ifreq *ifr = (struct ifreq *)data;
224 register struct in_ifaddr *ia = 0, *iap;
225 register struct ifaddr *ifa;
227 struct in_ifaddr *oia;
228 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
229 struct sockaddr_in oldaddr;
230 int error, hostIsNew, iaIsNew, maskIsNew, s;
237 if (td && (error = suser(td)) != 0)
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) {
261 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
263 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);
296 if (td && (error = suser(td)) != 0)
300 return (EADDRNOTAVAIL);
301 if (ia == (struct in_ifaddr *)0) {
302 ia = (struct in_ifaddr *)
303 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
304 if (ia == (struct in_ifaddr *)NULL)
307 * Protect from ipintr() traversing address list
308 * while we're modifying it.
313 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
314 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
315 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
317 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
319 ia->ia_sockmask.sin_len = 8;
320 ia->ia_sockmask.sin_family = AF_INET;
321 if (ifp->if_flags & IFF_BROADCAST) {
322 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
323 ia->ia_broadaddr.sin_family = AF_INET;
327 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
334 if (td && (error = suser(td)) != 0)
342 if (ia == (struct in_ifaddr *)0)
343 return (EADDRNOTAVAIL);
349 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
353 if ((ifp->if_flags & IFF_BROADCAST) == 0)
355 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
359 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
361 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
365 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
369 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
371 oldaddr = ia->ia_dstaddr;
372 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
375 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
377 IFF_UNLOCKGIANT(ifp);
379 ia->ia_dstaddr = oldaddr;
383 if (ia->ia_flags & IFA_ROUTE) {
384 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
385 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
386 ia->ia_ifa.ifa_dstaddr =
387 (struct sockaddr *)&ia->ia_dstaddr;
388 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
393 if ((ifp->if_flags & IFF_BROADCAST) == 0)
395 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
399 error = in_ifinit(ifp, ia,
400 (struct sockaddr_in *) &ifr->ifr_addr, 1);
401 if (error != 0 && iaIsNew)
404 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
408 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
409 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
416 if (ia->ia_addr.sin_family == AF_INET) {
417 if (ifra->ifra_addr.sin_len == 0) {
418 ifra->ifra_addr = ia->ia_addr;
420 } else if (ifra->ifra_addr.sin_addr.s_addr ==
421 ia->ia_addr.sin_addr.s_addr)
424 if (ifra->ifra_mask.sin_len) {
426 ia->ia_sockmask = ifra->ifra_mask;
427 ia->ia_sockmask.sin_family = AF_INET;
429 ntohl(ia->ia_sockmask.sin_addr.s_addr);
432 if ((ifp->if_flags & IFF_POINTOPOINT) &&
433 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
435 ia->ia_dstaddr = ifra->ifra_dstaddr;
436 maskIsNew = 1; /* We lie; but the effect's the same */
438 if (ifra->ifra_addr.sin_family == AF_INET &&
439 (hostIsNew || maskIsNew))
440 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
441 if (error != 0 && iaIsNew)
444 if ((ifp->if_flags & IFF_BROADCAST) &&
445 (ifra->ifra_broadaddr.sin_family == AF_INET))
446 ia->ia_broadaddr = ifra->ifra_broadaddr;
448 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
453 * in_ifscrub kills the interface route.
457 * in_ifadown gets rid of all the rest of
458 * the routes. This is not quite the right
459 * thing to do, but at least if we are running
460 * a routing process they will come back.
462 in_ifadown(&ia->ia_ifa, 1);
463 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
468 if (ifp == 0 || ifp->if_ioctl == 0)
471 error = (*ifp->if_ioctl)(ifp, cmd, data);
472 IFF_UNLOCKGIANT(ifp);
477 * Protect from ipintr() traversing address list while we're modifying
481 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
482 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
483 if (ia->ia_addr.sin_family == AF_INET)
484 LIST_REMOVE(ia, ia_hash);
485 IFAFREE(&ia->ia_ifa);
493 * SIOCGLIFADDR: get first address. (?!?)
494 * SIOCGLIFADDR with IFLR_PREFIX:
495 * get first address that matches the specified prefix.
496 * SIOCALIFADDR: add the specified address.
497 * SIOCALIFADDR with IFLR_PREFIX:
498 * EINVAL since we can't deduce hostid part of the address.
499 * SIOCDLIFADDR: delete the specified address.
500 * SIOCDLIFADDR with IFLR_PREFIX:
501 * delete the first address that matches the specified prefix.
503 * EINVAL on invalid parameters
504 * EADDRNOTAVAIL on prefix match failed/specified address not found
505 * other values may be returned from in_ioctl()
508 in_lifaddr_ioctl(so, cmd, data, ifp, td)
515 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
520 panic("invalid argument to in_lifaddr_ioctl");
526 /* address must be specified on GET with IFLR_PREFIX */
527 if ((iflr->flags & IFLR_PREFIX) == 0)
532 /* address must be specified on ADD and DELETE */
533 if (iflr->addr.ss_family != AF_INET)
535 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
537 /* XXX need improvement */
538 if (iflr->dstaddr.ss_family
539 && iflr->dstaddr.ss_family != AF_INET)
541 if (iflr->dstaddr.ss_family
542 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
545 default: /*shouldn't happen*/
548 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
554 struct in_aliasreq ifra;
556 if (iflr->flags & IFLR_PREFIX)
559 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
560 bzero(&ifra, sizeof(ifra));
561 bcopy(iflr->iflr_name, ifra.ifra_name,
562 sizeof(ifra.ifra_name));
564 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
566 if (iflr->dstaddr.ss_family) { /*XXX*/
567 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
568 iflr->dstaddr.ss_len);
571 ifra.ifra_mask.sin_family = AF_INET;
572 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
573 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
575 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
580 struct in_ifaddr *ia;
581 struct in_addr mask, candidate, match;
582 struct sockaddr_in *sin;
585 bzero(&mask, sizeof(mask));
586 if (iflr->flags & IFLR_PREFIX) {
587 /* lookup a prefix rather than address. */
588 in_len2mask(&mask, iflr->prefixlen);
590 sin = (struct sockaddr_in *)&iflr->addr;
591 match.s_addr = sin->sin_addr.s_addr;
592 match.s_addr &= mask.s_addr;
594 /* if you set extra bits, that's wrong */
595 if (match.s_addr != sin->sin_addr.s_addr)
600 if (cmd == SIOCGLIFADDR) {
601 /* on getting an address, take the 1st match */
604 /* on deleting an address, do exact match */
605 in_len2mask(&mask, 32);
606 sin = (struct sockaddr_in *)&iflr->addr;
607 match.s_addr = sin->sin_addr.s_addr;
613 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
614 if (ifa->ifa_addr->sa_family != AF_INET6)
618 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
619 candidate.s_addr &= mask.s_addr;
620 if (candidate.s_addr == match.s_addr)
624 return EADDRNOTAVAIL;
625 ia = (struct in_ifaddr *)ifa;
627 if (cmd == SIOCGLIFADDR) {
628 /* fill in the if_laddrreq structure */
629 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
631 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
632 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
633 ia->ia_dstaddr.sin_len);
635 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
638 in_mask2len(&ia->ia_sockmask.sin_addr);
640 iflr->flags = 0; /*XXX*/
644 struct in_aliasreq ifra;
646 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
647 bzero(&ifra, sizeof(ifra));
648 bcopy(iflr->iflr_name, ifra.ifra_name,
649 sizeof(ifra.ifra_name));
651 bcopy(&ia->ia_addr, &ifra.ifra_addr,
652 ia->ia_addr.sin_len);
653 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
654 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
655 ia->ia_dstaddr.sin_len);
657 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
658 ia->ia_sockmask.sin_len);
660 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
666 return EOPNOTSUPP; /*just for safety*/
670 * Delete any existing route for an interface.
674 register struct ifnet *ifp;
675 register struct in_ifaddr *ia;
681 * Initialize an interface's internet address
682 * and routing table entry.
685 in_ifinit(ifp, ia, sin, scrub)
686 register struct ifnet *ifp;
687 register struct in_ifaddr *ia;
688 struct sockaddr_in *sin;
691 register u_long i = ntohl(sin->sin_addr.s_addr);
692 struct sockaddr_in oldaddr;
693 int s = splimp(), flags = RTF_UP, error = 0;
695 oldaddr = ia->ia_addr;
696 if (oldaddr.sin_family == AF_INET)
697 LIST_REMOVE(ia, ia_hash);
699 if (ia->ia_addr.sin_family == AF_INET)
700 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
703 * Give the interface a chance to initialize
704 * if this is its first address,
705 * and to validate the address if necessary.
709 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
710 IFF_UNLOCKGIANT(ifp);
713 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
714 ia->ia_addr = oldaddr;
715 if (ia->ia_addr.sin_family == AF_INET)
716 LIST_INSERT_HEAD(INADDR_HASH(
717 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
720 * If oldaddr family is not AF_INET (e.g.
721 * interface has been just created) in_control
722 * does not call LIST_REMOVE, and we end up
723 * with bogus ia entries in hash
725 LIST_REMOVE(ia, ia_hash);
731 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
733 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
736 ia->ia_netmask = IN_CLASSA_NET;
737 else if (IN_CLASSB(i))
738 ia->ia_netmask = IN_CLASSB_NET;
740 ia->ia_netmask = IN_CLASSC_NET;
742 * The subnet mask usually includes at least the standard network part,
743 * but may may be smaller in the case of supernetting.
744 * If it is set, we believe it.
746 if (ia->ia_subnetmask == 0) {
747 ia->ia_subnetmask = ia->ia_netmask;
748 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
750 ia->ia_netmask &= ia->ia_subnetmask;
751 ia->ia_net = i & ia->ia_netmask;
752 ia->ia_subnet = i & ia->ia_subnetmask;
753 in_socktrim(&ia->ia_sockmask);
756 * XXX: carp(4) does not have interface route
758 if (ifp->if_type == IFT_CARP)
762 * Add route for the network.
764 ia->ia_ifa.ifa_metric = ifp->if_metric;
765 if (ifp->if_flags & IFF_BROADCAST) {
766 ia->ia_broadaddr.sin_addr.s_addr =
767 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
768 ia->ia_netbroadcast.s_addr =
769 htonl(ia->ia_net | ~ ia->ia_netmask);
770 } else if (ifp->if_flags & IFF_LOOPBACK) {
771 ia->ia_dstaddr = ia->ia_addr;
773 } else if (ifp->if_flags & IFF_POINTOPOINT) {
774 if (ia->ia_dstaddr.sin_family != AF_INET)
778 if ((error = in_addprefix(ia, flags)) != 0)
782 * If the interface supports multicast, join the "all hosts"
783 * multicast group on that interface.
785 if (ifp->if_flags & IFF_MULTICAST) {
788 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
789 in_addmulti(&addr, ifp);
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 a one
802 in_addprefix(target, flags)
803 struct in_ifaddr *target;
806 struct in_ifaddr *ia;
807 struct in_addr prefix, mask, p, m;
810 if ((flags & RTF_HOST) != 0)
811 prefix = target->ia_dstaddr.sin_addr;
813 prefix = target->ia_addr.sin_addr;
814 mask = target->ia_sockmask.sin_addr;
815 prefix.s_addr &= mask.s_addr;
818 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
819 if (rtinitflags(ia)) {
820 p = ia->ia_addr.sin_addr;
822 if (prefix.s_addr != p.s_addr)
825 p = ia->ia_addr.sin_addr;
826 m = ia->ia_sockmask.sin_addr;
827 p.s_addr &= m.s_addr;
829 if (prefix.s_addr != p.s_addr ||
830 mask.s_addr != m.s_addr)
835 * If we got a matching prefix route inserted by other
836 * interface address, we are done here.
838 if (ia->ia_flags & IFA_ROUTE) {
839 if (sameprefixcarponly &&
840 target->ia_ifp->if_type != IFT_CARP &&
841 ia->ia_ifp->if_type != IFT_CARP)
849 * No-one seem to have this prefix route, so we try to insert it.
851 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
853 target->ia_flags |= IFA_ROUTE;
858 * If there is no other address in the system that can serve a route to the
859 * same prefix, remove the route. Hand over the route to the new address
863 in_scrubprefix(target)
864 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(in, ifp)
934 register struct ifaddr *ifa;
937 if (in.s_addr == INADDR_BROADCAST ||
938 in.s_addr == INADDR_ANY)
940 if ((ifp->if_flags & IFF_BROADCAST) == 0)
942 t = ntohl(in.s_addr);
944 * Look through the list of addresses for a match
945 * with a broadcast address.
947 #define ia ((struct in_ifaddr *)ifa)
948 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
949 if (ifa->ifa_addr->sa_family == AF_INET &&
950 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
951 in.s_addr == ia->ia_netbroadcast.s_addr ||
953 * Check for old-style (host 0) broadcast.
955 t == ia->ia_subnet || t == ia->ia_net) &&
957 * Check for an all one subnetmask. These
958 * only exist when an interface gets a secondary
961 ia->ia_subnetmask != (u_long)0xffffffff)
967 * Add an address to the list of IP multicast addresses for a given interface.
971 register struct in_addr *ap;
972 register struct ifnet *ifp;
974 register struct in_multi *inm;
976 struct sockaddr_in sin;
977 struct ifmultiaddr *ifma;
982 * Call generic routine to add membership or increment
983 * refcount. It wants addresses in the form of a sockaddr,
984 * so we build one here (being careful to zero the unused bytes).
986 bzero(&sin, sizeof sin);
987 sin.sin_family = AF_INET;
988 sin.sin_len = sizeof sin;
990 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
993 IFF_UNLOCKGIANT(ifp);
998 * If ifma->ifma_protospec is null, then if_addmulti() created
999 * a new record. Otherwise, we are done.
1001 if (ifma->ifma_protospec != NULL) {
1003 IFF_UNLOCKGIANT(ifp);
1004 return ifma->ifma_protospec;
1007 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
1010 if_delmulti_ent(ifma);
1012 IFF_UNLOCKGIANT(ifp);
1016 inm->inm_addr = *ap;
1018 inm->inm_ifma = ifma;
1019 ifma->ifma_protospec = inm;
1020 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1023 * Let IGMP know that we have joined a new IP multicast group.
1025 igmp_joingroup(inm);
1027 IFF_UNLOCKGIANT(ifp);
1032 in_delmulti_locked(inm, all)
1033 register struct in_multi *inm;
1036 struct ifmultiaddr *ifma;
1037 struct in_multi my_inm;
1039 ifma = inm->inm_ifma;
1040 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1042 while (ifma->ifma_refcount > 1)
1043 if_delmulti_ent(ifma);
1044 if (ifma->ifma_refcount == 1) {
1046 * No remaining claims to this record; let IGMP know that
1047 * we are leaving the multicast group.
1048 * But do it after the if_delmulti() which might reset
1049 * the interface and nuke the packet.
1052 ifma->ifma_protospec = NULL;
1053 LIST_REMOVE(inm, inm_link);
1054 free(inm, M_IPMADDR);
1056 if_delmulti_ent(ifma);
1057 if (my_inm.inm_ifp != NULL)
1058 igmp_leavegroup(&my_inm);
1062 * Delete a multicast address record.
1066 register struct in_multi *inm;
1073 in_delmulti_locked(inm, 0);
1075 IFF_UNLOCKGIANT(ifp);
1079 * Delete all multicast address records associated with the ifp.
1082 in_delmulti_ifp(ifp)
1083 register struct ifnet *ifp;
1085 struct in_multi *inm;
1086 struct in_multi *oinm;
1090 LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
1091 if (inm->inm_ifp == ifp)
1092 in_delmulti_locked(inm, 1);
1095 IFF_UNLOCKGIANT(ifp);
1099 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1106 in_pcbpurgeif0(&ripcbinfo, ifp);
1107 in_pcbpurgeif0(&udbinfo, ifp);
1108 in_delmulti_ifp(ifp);