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 int in_scrubprefix(struct in_ifaddr *);
63 static void in_socktrim(struct sockaddr_in *);
64 static int in_ifinit(struct ifnet *,
65 struct in_ifaddr *, struct sockaddr_in *, int);
67 static int subnetsarelocal = 0;
68 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
69 &subnetsarelocal, 0, "Treat all subnets as directly connected");
70 static int sameprefixcarponly = 0;
71 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
72 &sameprefixcarponly, 0,
73 "Refuse to create same prefixes on different interfaces");
76 * The IPv4 multicast list (in_multihead and associated structures) are
77 * protected by the global in_multi_mtx. See in_var.h for more details. For
78 * now, in_multi_mtx is marked as recursible due to IGMP's calling back into
79 * ip_output() to send IGMP packets while holding the lock; this probably is
80 * not quite desirable.
82 struct in_multihead in_multihead; /* XXX BSS initialization */
83 struct mtx in_multi_mtx;
84 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE);
86 extern struct inpcbinfo ripcbinfo;
87 extern struct inpcbinfo udbinfo;
90 * Return 1 if an internet address is for a ``local'' host
91 * (one to which we have a connection). If subnetsarelocal
92 * is true, this includes other subnets of the local net.
93 * Otherwise, it includes only the directly-connected (sub)nets.
99 register u_long i = ntohl(in.s_addr);
100 register struct in_ifaddr *ia;
102 if (subnetsarelocal) {
103 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
104 if ((i & ia->ia_netmask) == ia->ia_net)
107 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
108 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
115 * Return 1 if an internet address is for the local host and configured
116 * on one of its interfaces.
122 struct in_ifaddr *ia;
124 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
125 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
132 * Determine whether an IP address is in a reserved set of addresses
133 * that may not be forwarded, or whether datagrams to that destination
140 register u_long i = ntohl(in.s_addr);
143 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
146 net = i & IN_CLASSA_NET;
147 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
154 * Trim a mask in a sockaddr
158 struct sockaddr_in *ap;
160 register char *cplim = (char *) &ap->sin_addr;
161 register char *cp = (char *) (&ap->sin_addr + 1);
164 while (--cp >= cplim)
166 (ap)->sin_len = cp - (char *) (ap) + 1;
173 struct in_addr *mask;
179 for (x = 0; x < sizeof(*mask); x++) {
184 if (x < sizeof(*mask)) {
185 for (y = 0; y < 8; y++) {
186 if ((p[x] & (0x80 >> y)) == 0)
194 in_len2mask(mask, len)
195 struct in_addr *mask;
202 bzero(mask, sizeof(*mask));
203 for (i = 0; i < len / 8; i++)
206 p[i] = (0xff00 >> (len % 8)) & 0xff;
210 * Generic internet control operations (ioctl's).
211 * Ifp is 0 if not an interface-specific ioctl.
215 in_control(so, cmd, data, ifp, td)
219 register struct ifnet *ifp;
222 register struct ifreq *ifr = (struct ifreq *)data;
223 register struct in_ifaddr *ia = 0, *iap;
224 register struct ifaddr *ifa;
226 struct in_ifaddr *oia;
227 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
228 struct sockaddr_in oldaddr;
229 int error, hostIsNew, iaIsNew, maskIsNew, s;
236 if (td && (error = suser(td)) != 0)
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) {
274 return (EADDRNOTAVAIL);
275 if (ifra->ifra_addr.sin_family == AF_INET) {
276 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
277 if (ia->ia_ifp == ifp &&
278 ia->ia_addr.sin_addr.s_addr ==
279 ifra->ifra_addr.sin_addr.s_addr)
282 if ((ifp->if_flags & IFF_POINTOPOINT)
283 && (cmd == SIOCAIFADDR)
284 && (ifra->ifra_dstaddr.sin_addr.s_addr
289 if (cmd == SIOCDIFADDR && ia == 0)
290 return (EADDRNOTAVAIL);
295 if (td && (error = suser(td)) != 0)
299 return (EADDRNOTAVAIL);
300 if (ia == (struct in_ifaddr *)0) {
301 ia = (struct in_ifaddr *)
302 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
303 if (ia == (struct in_ifaddr *)NULL)
306 * Protect from ipintr() traversing address list
307 * while we're modifying it.
312 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
313 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
314 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
316 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
318 ia->ia_sockmask.sin_len = 8;
319 ia->ia_sockmask.sin_family = AF_INET;
320 if (ifp->if_flags & IFF_BROADCAST) {
321 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
322 ia->ia_broadaddr.sin_family = AF_INET;
326 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
333 if (td && (error = suser(td)) != 0)
341 if (ia == (struct in_ifaddr *)0)
342 return (EADDRNOTAVAIL);
348 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
352 if ((ifp->if_flags & IFF_BROADCAST) == 0)
354 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
358 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
360 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
364 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
368 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
370 oldaddr = ia->ia_dstaddr;
371 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
374 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
376 IFF_UNLOCKGIANT(ifp);
378 ia->ia_dstaddr = oldaddr;
382 if (ia->ia_flags & IFA_ROUTE) {
383 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
384 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
385 ia->ia_ifa.ifa_dstaddr =
386 (struct sockaddr *)&ia->ia_dstaddr;
387 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
392 if ((ifp->if_flags & IFF_BROADCAST) == 0)
394 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
398 error = in_ifinit(ifp, ia,
399 (struct sockaddr_in *) &ifr->ifr_addr, 1);
400 if (error != 0 && iaIsNew)
403 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
407 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
408 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
415 if (ia->ia_addr.sin_family == AF_INET) {
416 if (ifra->ifra_addr.sin_len == 0) {
417 ifra->ifra_addr = ia->ia_addr;
419 } else if (ifra->ifra_addr.sin_addr.s_addr ==
420 ia->ia_addr.sin_addr.s_addr)
423 if (ifra->ifra_mask.sin_len) {
425 ia->ia_sockmask = ifra->ifra_mask;
426 ia->ia_sockmask.sin_family = AF_INET;
428 ntohl(ia->ia_sockmask.sin_addr.s_addr);
431 if ((ifp->if_flags & IFF_POINTOPOINT) &&
432 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
434 ia->ia_dstaddr = ifra->ifra_dstaddr;
435 maskIsNew = 1; /* We lie; but the effect's the same */
437 if (ifra->ifra_addr.sin_family == AF_INET &&
438 (hostIsNew || maskIsNew))
439 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
440 if (error != 0 && iaIsNew)
443 if ((ifp->if_flags & IFF_BROADCAST) &&
444 (ifra->ifra_broadaddr.sin_family == AF_INET))
445 ia->ia_broadaddr = ifra->ifra_broadaddr;
447 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
452 * in_ifscrub kills the interface route.
456 * in_ifadown gets rid of all the rest of
457 * the routes. This is not quite the right
458 * thing to do, but at least if we are running
459 * a routing process they will come back.
461 in_ifadown(&ia->ia_ifa, 1);
462 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
467 if (ifp == 0 || ifp->if_ioctl == 0)
470 error = (*ifp->if_ioctl)(ifp, cmd, data);
471 IFF_UNLOCKGIANT(ifp);
476 * Protect from ipintr() traversing address list while we're modifying
480 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
481 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
482 if (ia->ia_addr.sin_family == AF_INET)
483 LIST_REMOVE(ia, ia_hash);
484 IFAFREE(&ia->ia_ifa);
492 * SIOCGLIFADDR: get first address. (?!?)
493 * SIOCGLIFADDR with IFLR_PREFIX:
494 * get first address that matches the specified prefix.
495 * SIOCALIFADDR: add the specified address.
496 * SIOCALIFADDR with IFLR_PREFIX:
497 * EINVAL since we can't deduce hostid part of the address.
498 * SIOCDLIFADDR: delete the specified address.
499 * SIOCDLIFADDR with IFLR_PREFIX:
500 * delete the first address that matches the specified prefix.
502 * EINVAL on invalid parameters
503 * EADDRNOTAVAIL on prefix match failed/specified address not found
504 * other values may be returned from in_ioctl()
507 in_lifaddr_ioctl(so, cmd, data, ifp, td)
514 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
519 panic("invalid argument to in_lifaddr_ioctl");
525 /* address must be specified on GET with IFLR_PREFIX */
526 if ((iflr->flags & IFLR_PREFIX) == 0)
531 /* address must be specified on ADD and DELETE */
532 if (iflr->addr.ss_family != AF_INET)
534 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
536 /* XXX need improvement */
537 if (iflr->dstaddr.ss_family
538 && iflr->dstaddr.ss_family != AF_INET)
540 if (iflr->dstaddr.ss_family
541 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
544 default: /*shouldn't happen*/
547 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
553 struct in_aliasreq ifra;
555 if (iflr->flags & IFLR_PREFIX)
558 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
559 bzero(&ifra, sizeof(ifra));
560 bcopy(iflr->iflr_name, ifra.ifra_name,
561 sizeof(ifra.ifra_name));
563 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
565 if (iflr->dstaddr.ss_family) { /*XXX*/
566 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
567 iflr->dstaddr.ss_len);
570 ifra.ifra_mask.sin_family = AF_INET;
571 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
572 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
574 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
579 struct in_ifaddr *ia;
580 struct in_addr mask, candidate, match;
581 struct sockaddr_in *sin;
584 bzero(&mask, sizeof(mask));
585 if (iflr->flags & IFLR_PREFIX) {
586 /* lookup a prefix rather than address. */
587 in_len2mask(&mask, iflr->prefixlen);
589 sin = (struct sockaddr_in *)&iflr->addr;
590 match.s_addr = sin->sin_addr.s_addr;
591 match.s_addr &= mask.s_addr;
593 /* if you set extra bits, that's wrong */
594 if (match.s_addr != sin->sin_addr.s_addr)
599 if (cmd == SIOCGLIFADDR) {
600 /* on getting an address, take the 1st match */
603 /* on deleting an address, do exact match */
604 in_len2mask(&mask, 32);
605 sin = (struct sockaddr_in *)&iflr->addr;
606 match.s_addr = sin->sin_addr.s_addr;
612 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
613 if (ifa->ifa_addr->sa_family != AF_INET6)
617 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
618 candidate.s_addr &= mask.s_addr;
619 if (candidate.s_addr == match.s_addr)
623 return EADDRNOTAVAIL;
624 ia = (struct in_ifaddr *)ifa;
626 if (cmd == SIOCGLIFADDR) {
627 /* fill in the if_laddrreq structure */
628 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
630 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
631 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
632 ia->ia_dstaddr.sin_len);
634 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
637 in_mask2len(&ia->ia_sockmask.sin_addr);
639 iflr->flags = 0; /*XXX*/
643 struct in_aliasreq ifra;
645 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
646 bzero(&ifra, sizeof(ifra));
647 bcopy(iflr->iflr_name, ifra.ifra_name,
648 sizeof(ifra.ifra_name));
650 bcopy(&ia->ia_addr, &ifra.ifra_addr,
651 ia->ia_addr.sin_len);
652 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
653 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
654 ia->ia_dstaddr.sin_len);
656 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
657 ia->ia_sockmask.sin_len);
659 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
665 return EOPNOTSUPP; /*just for safety*/
669 * Delete any existing route for an interface.
673 register struct ifnet *ifp;
674 register struct in_ifaddr *ia;
680 * Initialize an interface's internet address
681 * and routing table entry.
684 in_ifinit(ifp, ia, sin, scrub)
685 register struct ifnet *ifp;
686 register struct in_ifaddr *ia;
687 struct sockaddr_in *sin;
690 register u_long i = ntohl(sin->sin_addr.s_addr);
691 struct sockaddr_in oldaddr;
692 int s = splimp(), flags = RTF_UP, error = 0;
694 oldaddr = ia->ia_addr;
695 if (oldaddr.sin_family == AF_INET)
696 LIST_REMOVE(ia, ia_hash);
698 if (ia->ia_addr.sin_family == AF_INET)
699 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
702 * Give the interface a chance to initialize
703 * if this is its first address,
704 * and to validate the address if necessary.
708 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
709 IFF_UNLOCKGIANT(ifp);
712 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
713 ia->ia_addr = oldaddr;
714 if (ia->ia_addr.sin_family == AF_INET)
715 LIST_INSERT_HEAD(INADDR_HASH(
716 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
722 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
724 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
727 ia->ia_netmask = IN_CLASSA_NET;
728 else if (IN_CLASSB(i))
729 ia->ia_netmask = IN_CLASSB_NET;
731 ia->ia_netmask = IN_CLASSC_NET;
733 * The subnet mask usually includes at least the standard network part,
734 * but may may be smaller in the case of supernetting.
735 * If it is set, we believe it.
737 if (ia->ia_subnetmask == 0) {
738 ia->ia_subnetmask = ia->ia_netmask;
739 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
741 ia->ia_netmask &= ia->ia_subnetmask;
742 ia->ia_net = i & ia->ia_netmask;
743 ia->ia_subnet = i & ia->ia_subnetmask;
744 in_socktrim(&ia->ia_sockmask);
747 * XXX: carp(4) does not have interface route
749 if (ifp->if_type == IFT_CARP)
753 * Add route for the network.
755 ia->ia_ifa.ifa_metric = ifp->if_metric;
756 if (ifp->if_flags & IFF_BROADCAST) {
757 ia->ia_broadaddr.sin_addr.s_addr =
758 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
759 ia->ia_netbroadcast.s_addr =
760 htonl(ia->ia_net | ~ ia->ia_netmask);
761 } else if (ifp->if_flags & IFF_LOOPBACK) {
762 ia->ia_dstaddr = ia->ia_addr;
764 } else if (ifp->if_flags & IFF_POINTOPOINT) {
765 if (ia->ia_dstaddr.sin_family != AF_INET)
769 if ((error = in_addprefix(ia, flags)) != 0)
773 * If the interface supports multicast, join the "all hosts"
774 * multicast group on that interface.
776 if (ifp->if_flags & IFF_MULTICAST) {
779 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
780 in_addmulti(&addr, ifp);
785 #define rtinitflags(x) \
786 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
789 * Check if we have a route for the given prefix already or add a one
793 in_addprefix(target, flags)
794 struct in_ifaddr *target;
797 struct in_ifaddr *ia;
798 struct in_addr prefix, mask, p, m;
801 if ((flags & RTF_HOST) != 0)
802 prefix = target->ia_dstaddr.sin_addr;
804 prefix = target->ia_addr.sin_addr;
805 mask = target->ia_sockmask.sin_addr;
806 prefix.s_addr &= mask.s_addr;
809 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
810 if (rtinitflags(ia)) {
811 p = ia->ia_addr.sin_addr;
813 if (prefix.s_addr != p.s_addr)
816 p = ia->ia_addr.sin_addr;
817 m = ia->ia_sockmask.sin_addr;
818 p.s_addr &= m.s_addr;
820 if (prefix.s_addr != p.s_addr ||
821 mask.s_addr != m.s_addr)
826 * If we got a matching prefix route inserted by other
827 * interface address, we are done here.
829 if (ia->ia_flags & IFA_ROUTE) {
830 if (sameprefixcarponly &&
831 target->ia_ifp->if_type != IFT_CARP &&
832 ia->ia_ifp->if_type != IFT_CARP)
840 * No-one seem to have this prefix route, so we try to insert it.
842 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
844 target->ia_flags |= IFA_ROUTE;
849 * If there is no other address in the system that can serve a route to the
850 * same prefix, remove the route. Hand over the route to the new address
854 in_scrubprefix(target)
855 struct in_ifaddr *target;
857 struct in_ifaddr *ia;
858 struct in_addr prefix, mask, p;
861 if ((target->ia_flags & IFA_ROUTE) == 0)
864 if (rtinitflags(target))
865 prefix = target->ia_dstaddr.sin_addr;
867 prefix = target->ia_addr.sin_addr;
868 mask = target->ia_sockmask.sin_addr;
869 prefix.s_addr &= mask.s_addr;
872 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
874 p = ia->ia_dstaddr.sin_addr;
876 p = ia->ia_addr.sin_addr;
877 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
880 if (prefix.s_addr != p.s_addr)
884 * If we got a matching prefix address, move IFA_ROUTE and
885 * the route itself to it. Make sure that routing daemons
888 * XXX: a special case for carp(4) interface
890 if ((ia->ia_flags & IFA_ROUTE) == 0
892 && (ia->ia_ifp->if_type != IFT_CARP)
895 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
896 rtinitflags(target));
897 target->ia_flags &= ~IFA_ROUTE;
899 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
900 rtinitflags(ia) | RTF_UP);
902 ia->ia_flags |= IFA_ROUTE;
908 * As no-one seem to have this prefix, we can remove the route.
910 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
911 target->ia_flags &= ~IFA_ROUTE;
918 * Return 1 if the address might be a local broadcast address.
921 in_broadcast(in, ifp)
925 register struct ifaddr *ifa;
928 if (in.s_addr == INADDR_BROADCAST ||
929 in.s_addr == INADDR_ANY)
931 if ((ifp->if_flags & IFF_BROADCAST) == 0)
933 t = ntohl(in.s_addr);
935 * Look through the list of addresses for a match
936 * with a broadcast address.
938 #define ia ((struct in_ifaddr *)ifa)
939 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
940 if (ifa->ifa_addr->sa_family == AF_INET &&
941 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
942 in.s_addr == ia->ia_netbroadcast.s_addr ||
944 * Check for old-style (host 0) broadcast.
946 t == ia->ia_subnet || t == ia->ia_net) &&
948 * Check for an all one subnetmask. These
949 * only exist when an interface gets a secondary
952 ia->ia_subnetmask != (u_long)0xffffffff)
958 * Add an address to the list of IP multicast addresses for a given interface.
962 register struct in_addr *ap;
963 register struct ifnet *ifp;
965 register struct in_multi *inm;
967 struct sockaddr_in sin;
968 struct ifmultiaddr *ifma;
973 * Call generic routine to add membership or increment
974 * refcount. It wants addresses in the form of a sockaddr,
975 * so we build one here (being careful to zero the unused bytes).
977 bzero(&sin, sizeof sin);
978 sin.sin_family = AF_INET;
979 sin.sin_len = sizeof sin;
981 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
984 IFF_UNLOCKGIANT(ifp);
989 * If ifma->ifma_protospec is null, then if_addmulti() created
990 * a new record. Otherwise, we are done.
992 if (ifma->ifma_protospec != NULL) {
994 IFF_UNLOCKGIANT(ifp);
995 return ifma->ifma_protospec;
998 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
1002 IFF_UNLOCKGIANT(ifp);
1006 inm->inm_addr = *ap;
1008 inm->inm_ifma = ifma;
1009 ifma->ifma_protospec = inm;
1010 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1013 * Let IGMP know that we have joined a new IP multicast group.
1015 igmp_joingroup(inm);
1017 IFF_UNLOCKGIANT(ifp);
1022 * Delete a multicast address record.
1026 register struct in_multi *inm;
1028 struct ifmultiaddr *ifma;
1029 struct in_multi my_inm;
1035 ifma = inm->inm_ifma;
1036 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1037 if (ifma->ifma_refcount == 1) {
1039 * No remaining claims to this record; let IGMP know that
1040 * we are leaving the multicast group.
1041 * But do it after the if_delmulti() which might reset
1042 * the interface and nuke the packet.
1045 ifma->ifma_protospec = NULL;
1046 LIST_REMOVE(inm, inm_link);
1047 free(inm, M_IPMADDR);
1049 /* XXX - should be separate API for when we have an ifma? */
1050 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1051 if (my_inm.inm_ifp != NULL)
1052 igmp_leavegroup(&my_inm);
1054 IFF_UNLOCKGIANT(ifp);
1058 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1065 in_pcbpurgeif0(&ripcbinfo, ifp);
1066 in_pcbpurgeif0(&udbinfo, ifp);