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>
53 #include <netinet/igmp_var.h>
55 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
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);
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 error = priv_check(td, PRIV_NET_ADDIFADDR);
243 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
247 error = priv_check(td, PRIV_NET_DELIFADDR);
253 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
258 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
262 * Find address for this interface, if it exists.
264 * If an alias address was specified, find that one instead of
265 * the first one on the interface, if possible.
268 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
269 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
270 if (iap->ia_ifp == ifp &&
271 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
276 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
278 if (iap->ia_addr.sin_family == AF_INET) {
290 return (EADDRNOTAVAIL);
291 if (ifra->ifra_addr.sin_family == AF_INET) {
292 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
293 if (ia->ia_ifp == ifp &&
294 ia->ia_addr.sin_addr.s_addr ==
295 ifra->ifra_addr.sin_addr.s_addr)
298 if ((ifp->if_flags & IFF_POINTOPOINT)
299 && (cmd == SIOCAIFADDR)
300 && (ifra->ifra_dstaddr.sin_addr.s_addr
305 if (cmd == SIOCDIFADDR && ia == 0)
306 return (EADDRNOTAVAIL);
312 error = priv_check(td, PRIV_NET_ADDIFADDR);
318 return (EADDRNOTAVAIL);
319 if (ia == (struct in_ifaddr *)0) {
320 ia = (struct in_ifaddr *)
321 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
322 if (ia == (struct in_ifaddr *)NULL)
325 * Protect from ipintr() traversing address list
326 * while we're modifying it.
331 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
332 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
333 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
335 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
337 ia->ia_sockmask.sin_len = 8;
338 ia->ia_sockmask.sin_family = AF_INET;
339 if (ifp->if_flags & IFF_BROADCAST) {
340 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
341 ia->ia_broadaddr.sin_family = AF_INET;
345 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
353 error = priv_check(td, PRIV_NET_ADDIFADDR);
363 if (ia == (struct in_ifaddr *)0)
364 return (EADDRNOTAVAIL);
370 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
374 if ((ifp->if_flags & IFF_BROADCAST) == 0)
376 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
380 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
382 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
386 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
390 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
392 oldaddr = ia->ia_dstaddr;
393 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
396 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
398 IFF_UNLOCKGIANT(ifp);
400 ia->ia_dstaddr = oldaddr;
404 if (ia->ia_flags & IFA_ROUTE) {
405 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
406 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
407 ia->ia_ifa.ifa_dstaddr =
408 (struct sockaddr *)&ia->ia_dstaddr;
409 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
414 if ((ifp->if_flags & IFF_BROADCAST) == 0)
416 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
420 error = in_ifinit(ifp, ia,
421 (struct sockaddr_in *) &ifr->ifr_addr, 1);
422 if (error != 0 && iaIsNew)
425 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
429 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
430 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
437 if (ia->ia_addr.sin_family == AF_INET) {
438 if (ifra->ifra_addr.sin_len == 0) {
439 ifra->ifra_addr = ia->ia_addr;
441 } else if (ifra->ifra_addr.sin_addr.s_addr ==
442 ia->ia_addr.sin_addr.s_addr)
445 if (ifra->ifra_mask.sin_len) {
447 ia->ia_sockmask = ifra->ifra_mask;
448 ia->ia_sockmask.sin_family = AF_INET;
450 ntohl(ia->ia_sockmask.sin_addr.s_addr);
453 if ((ifp->if_flags & IFF_POINTOPOINT) &&
454 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
456 ia->ia_dstaddr = ifra->ifra_dstaddr;
457 maskIsNew = 1; /* We lie; but the effect's the same */
459 if (ifra->ifra_addr.sin_family == AF_INET &&
460 (hostIsNew || maskIsNew))
461 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
462 if (error != 0 && iaIsNew)
465 if ((ifp->if_flags & IFF_BROADCAST) &&
466 (ifra->ifra_broadaddr.sin_family == AF_INET))
467 ia->ia_broadaddr = ifra->ifra_broadaddr;
469 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 == 0 || ifp->if_ioctl == 0)
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(&in_ifaddrhead, ia, ia_link);
504 if (ia->ia_addr.sin_family == AF_INET)
505 LIST_REMOVE(ia, ia_hash);
506 IFAFREE(&ia->ia_ifa);
514 * SIOCGLIFADDR: get first address. (?!?)
515 * SIOCGLIFADDR with IFLR_PREFIX:
516 * get first address that matches the specified prefix.
517 * SIOCALIFADDR: add the specified address.
518 * SIOCALIFADDR with IFLR_PREFIX:
519 * EINVAL since we can't deduce hostid part of the address.
520 * SIOCDLIFADDR: delete the specified address.
521 * SIOCDLIFADDR with IFLR_PREFIX:
522 * delete the first address that matches the specified prefix.
524 * EINVAL on invalid parameters
525 * EADDRNOTAVAIL on prefix match failed/specified address not found
526 * other values may be returned from in_ioctl()
529 in_lifaddr_ioctl(so, cmd, data, ifp, td)
536 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
541 panic("invalid argument to in_lifaddr_ioctl");
547 /* address must be specified on GET with IFLR_PREFIX */
548 if ((iflr->flags & IFLR_PREFIX) == 0)
553 /* address must be specified on ADD and DELETE */
554 if (iflr->addr.ss_family != AF_INET)
556 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
558 /* XXX need improvement */
559 if (iflr->dstaddr.ss_family
560 && iflr->dstaddr.ss_family != AF_INET)
562 if (iflr->dstaddr.ss_family
563 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
566 default: /*shouldn't happen*/
569 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
575 struct in_aliasreq ifra;
577 if (iflr->flags & IFLR_PREFIX)
580 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
581 bzero(&ifra, sizeof(ifra));
582 bcopy(iflr->iflr_name, ifra.ifra_name,
583 sizeof(ifra.ifra_name));
585 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
587 if (iflr->dstaddr.ss_family) { /*XXX*/
588 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
589 iflr->dstaddr.ss_len);
592 ifra.ifra_mask.sin_family = AF_INET;
593 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
594 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
596 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
601 struct in_ifaddr *ia;
602 struct in_addr mask, candidate, match;
603 struct sockaddr_in *sin;
606 bzero(&mask, sizeof(mask));
607 if (iflr->flags & IFLR_PREFIX) {
608 /* lookup a prefix rather than address. */
609 in_len2mask(&mask, iflr->prefixlen);
611 sin = (struct sockaddr_in *)&iflr->addr;
612 match.s_addr = sin->sin_addr.s_addr;
613 match.s_addr &= mask.s_addr;
615 /* if you set extra bits, that's wrong */
616 if (match.s_addr != sin->sin_addr.s_addr)
621 if (cmd == SIOCGLIFADDR) {
622 /* on getting an address, take the 1st match */
625 /* on deleting an address, do exact match */
626 in_len2mask(&mask, 32);
627 sin = (struct sockaddr_in *)&iflr->addr;
628 match.s_addr = sin->sin_addr.s_addr;
634 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
635 if (ifa->ifa_addr->sa_family != AF_INET6)
639 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
640 candidate.s_addr &= mask.s_addr;
641 if (candidate.s_addr == match.s_addr)
645 return EADDRNOTAVAIL;
646 ia = (struct in_ifaddr *)ifa;
648 if (cmd == SIOCGLIFADDR) {
649 /* fill in the if_laddrreq structure */
650 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
652 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
653 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
654 ia->ia_dstaddr.sin_len);
656 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
659 in_mask2len(&ia->ia_sockmask.sin_addr);
661 iflr->flags = 0; /*XXX*/
665 struct in_aliasreq ifra;
667 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
668 bzero(&ifra, sizeof(ifra));
669 bcopy(iflr->iflr_name, ifra.ifra_name,
670 sizeof(ifra.ifra_name));
672 bcopy(&ia->ia_addr, &ifra.ifra_addr,
673 ia->ia_addr.sin_len);
674 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
675 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
676 ia->ia_dstaddr.sin_len);
678 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
679 ia->ia_sockmask.sin_len);
681 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
687 return EOPNOTSUPP; /*just for safety*/
691 * Delete any existing route for an interface.
695 register struct ifnet *ifp;
696 register struct in_ifaddr *ia;
702 * Initialize an interface's internet address
703 * and routing table entry.
706 in_ifinit(ifp, ia, sin, scrub)
707 register struct ifnet *ifp;
708 register struct in_ifaddr *ia;
709 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)
795 * If the interface supports multicast, join the "all hosts"
796 * multicast group on that interface.
798 if (ifp->if_flags & IFF_MULTICAST) {
801 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
802 in_addmulti(&addr, ifp);
807 #define rtinitflags(x) \
808 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
811 * Check if we have a route for the given prefix already or add a one
815 in_addprefix(target, flags)
816 struct in_ifaddr *target;
819 struct in_ifaddr *ia;
820 struct in_addr prefix, mask, p, m;
823 if ((flags & RTF_HOST) != 0)
824 prefix = target->ia_dstaddr.sin_addr;
826 prefix = target->ia_addr.sin_addr;
827 mask = target->ia_sockmask.sin_addr;
828 prefix.s_addr &= mask.s_addr;
831 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
832 if (rtinitflags(ia)) {
833 p = ia->ia_addr.sin_addr;
835 if (prefix.s_addr != p.s_addr)
838 p = ia->ia_addr.sin_addr;
839 m = ia->ia_sockmask.sin_addr;
840 p.s_addr &= m.s_addr;
842 if (prefix.s_addr != p.s_addr ||
843 mask.s_addr != m.s_addr)
848 * If we got a matching prefix route inserted by other
849 * interface address, we are done here.
851 if (ia->ia_flags & IFA_ROUTE) {
852 if (sameprefixcarponly &&
853 target->ia_ifp->if_type != IFT_CARP &&
854 ia->ia_ifp->if_type != IFT_CARP)
862 * No-one seem to have this prefix route, so we try to insert it.
864 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
866 target->ia_flags |= IFA_ROUTE;
871 * If there is no other address in the system that can serve a route to the
872 * same prefix, remove the route. Hand over the route to the new address
876 in_scrubprefix(target)
877 struct in_ifaddr *target;
879 struct in_ifaddr *ia;
880 struct in_addr prefix, mask, p;
883 if ((target->ia_flags & IFA_ROUTE) == 0)
886 if (rtinitflags(target))
887 prefix = target->ia_dstaddr.sin_addr;
889 prefix = target->ia_addr.sin_addr;
890 mask = target->ia_sockmask.sin_addr;
891 prefix.s_addr &= mask.s_addr;
894 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
896 p = ia->ia_dstaddr.sin_addr;
898 p = ia->ia_addr.sin_addr;
899 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
902 if (prefix.s_addr != p.s_addr)
906 * If we got a matching prefix address, move IFA_ROUTE and
907 * the route itself to it. Make sure that routing daemons
910 * XXX: a special case for carp(4) interface
912 if ((ia->ia_flags & IFA_ROUTE) == 0
914 && (ia->ia_ifp->if_type != IFT_CARP)
917 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
918 rtinitflags(target));
919 target->ia_flags &= ~IFA_ROUTE;
921 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
922 rtinitflags(ia) | RTF_UP);
924 ia->ia_flags |= IFA_ROUTE;
930 * As no-one seem to have this prefix, we can remove the route.
932 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
933 target->ia_flags &= ~IFA_ROUTE;
940 * Return 1 if the address might be a local broadcast address.
943 in_broadcast(in, ifp)
947 register struct ifaddr *ifa;
950 if (in.s_addr == INADDR_BROADCAST ||
951 in.s_addr == INADDR_ANY)
953 if ((ifp->if_flags & IFF_BROADCAST) == 0)
955 t = ntohl(in.s_addr);
957 * Look through the list of addresses for a match
958 * with a broadcast address.
960 #define ia ((struct in_ifaddr *)ifa)
961 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
962 if (ifa->ifa_addr->sa_family == AF_INET &&
963 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
964 in.s_addr == ia->ia_netbroadcast.s_addr ||
966 * Check for old-style (host 0) broadcast.
968 t == ia->ia_subnet || t == ia->ia_net) &&
970 * Check for an all one subnetmask. These
971 * only exist when an interface gets a secondary
974 ia->ia_subnetmask != (u_long)0xffffffff)
980 * Add an address to the list of IP multicast addresses for a given interface.
984 register struct in_addr *ap;
985 register struct ifnet *ifp;
987 register struct in_multi *inm;
989 struct sockaddr_in sin;
990 struct ifmultiaddr *ifma;
995 * Call generic routine to add membership or increment
996 * refcount. It wants addresses in the form of a sockaddr,
997 * so we build one here (being careful to zero the unused bytes).
999 bzero(&sin, sizeof sin);
1000 sin.sin_family = AF_INET;
1001 sin.sin_len = sizeof sin;
1003 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1006 IFF_UNLOCKGIANT(ifp);
1011 * If ifma->ifma_protospec is null, then if_addmulti() created
1012 * a new record. Otherwise, we are done.
1014 if (ifma->ifma_protospec != NULL) {
1016 IFF_UNLOCKGIANT(ifp);
1017 return ifma->ifma_protospec;
1020 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
1024 IFF_UNLOCKGIANT(ifp);
1028 inm->inm_addr = *ap;
1030 inm->inm_ifma = ifma;
1031 ifma->ifma_protospec = inm;
1032 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1035 * Let IGMP know that we have joined a new IP multicast group.
1037 igmp_joingroup(inm);
1039 IFF_UNLOCKGIANT(ifp);
1044 * Delete a multicast address record.
1048 register struct in_multi *inm;
1055 in_delmulti_locked(inm);
1057 IFF_UNLOCKGIANT(ifp);
1061 in_delmulti_locked(inm)
1062 register struct in_multi *inm;
1064 struct ifmultiaddr *ifma;
1065 struct in_multi my_inm;
1067 ifma = inm->inm_ifma;
1068 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1069 if (ifma->ifma_refcount == 1) {
1071 * No remaining claims to this record; let IGMP know that
1072 * we are leaving the multicast group.
1073 * But do it after the if_delmulti() which might reset
1074 * the interface and nuke the packet.
1077 ifma->ifma_protospec = NULL;
1078 LIST_REMOVE(inm, inm_link);
1079 free(inm, M_IPMADDR);
1081 /* XXX - should be separate API for when we have an ifma? */
1082 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1083 if (my_inm.inm_ifp != NULL)
1084 igmp_leavegroup(&my_inm);
1088 * Delete all multicast address records associated with the ifp.
1091 in_delmulti_ifp(ifp)
1092 register struct ifnet *ifp;
1094 struct in_multi *inm;
1095 struct in_multi *oinm;
1099 LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
1100 if (inm->inm_ifp == ifp)
1101 in_delmulti_locked(inm);
1104 IFF_UNLOCKGIANT(ifp);
1108 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1115 in_pcbpurgeif0(&ripcbinfo, ifp);
1116 in_pcbpurgeif0(&udbinfo, ifp);
1117 in_delmulti_ifp(ifp);