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");
71 struct in_multihead in_multihead; /* XXX BSS initialization */
73 extern struct inpcbinfo ripcbinfo;
74 extern struct inpcbinfo udbinfo;
77 * Return 1 if an internet address is for a ``local'' host
78 * (one to which we have a connection). If subnetsarelocal
79 * is true, this includes other subnets of the local net.
80 * Otherwise, it includes only the directly-connected (sub)nets.
86 register u_long i = ntohl(in.s_addr);
87 register struct in_ifaddr *ia;
89 if (subnetsarelocal) {
90 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
91 if ((i & ia->ia_netmask) == ia->ia_net)
94 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
95 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
102 * Return 1 if an internet address is for the local host and configured
103 * on one of its interfaces.
109 struct in_ifaddr *ia;
111 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
112 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
119 * Determine whether an IP address is in a reserved set of addresses
120 * that may not be forwarded, or whether datagrams to that destination
127 register u_long i = ntohl(in.s_addr);
130 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
133 net = i & IN_CLASSA_NET;
134 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
141 * Trim a mask in a sockaddr
145 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(mask, len)
182 struct in_addr *mask;
189 bzero(mask, sizeof(*mask));
190 for (i = 0; i < len / 8; i++)
193 p[i] = (0xff00 >> (len % 8)) & 0xff;
197 * Generic internet control operations (ioctl's).
198 * Ifp is 0 if not an interface-specific ioctl.
202 in_control(so, cmd, data, ifp, td)
206 register struct ifnet *ifp;
209 register struct ifreq *ifr = (struct ifreq *)data;
210 register struct in_ifaddr *ia = 0, *iap;
211 register struct ifaddr *ifa;
213 struct in_ifaddr *oia;
214 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
215 struct sockaddr_in oldaddr;
216 int error, hostIsNew, iaIsNew, maskIsNew, s;
223 if (td && (error = suser(td)) != 0)
229 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
233 * Find address for this interface, if it exists.
235 * If an alias address was specified, find that one instead of
236 * the first one on the interface, if possible.
239 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
240 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
241 if (iap->ia_ifp == ifp &&
242 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
247 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
249 if (iap->ia_addr.sin_family == AF_INET) {
261 return (EADDRNOTAVAIL);
262 if (ifra->ifra_addr.sin_family == AF_INET) {
263 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
264 if (ia->ia_ifp == ifp &&
265 ia->ia_addr.sin_addr.s_addr ==
266 ifra->ifra_addr.sin_addr.s_addr)
269 if ((ifp->if_flags & IFF_POINTOPOINT)
270 && (cmd == SIOCAIFADDR)
271 && (ifra->ifra_dstaddr.sin_addr.s_addr
276 if (cmd == SIOCDIFADDR && ia == 0)
277 return (EADDRNOTAVAIL);
282 if (td && (error = suser(td)) != 0)
286 return (EADDRNOTAVAIL);
287 if (ia == (struct in_ifaddr *)0) {
288 ia = (struct in_ifaddr *)
289 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
290 if (ia == (struct in_ifaddr *)NULL)
293 * Protect from ipintr() traversing address list
294 * while we're modifying it.
297 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
301 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
302 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
303 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
305 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
307 ia->ia_sockmask.sin_len = 8;
308 ia->ia_sockmask.sin_family = AF_INET;
309 if (ifp->if_flags & IFF_BROADCAST) {
310 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
311 ia->ia_broadaddr.sin_family = AF_INET;
320 if (td && (error = suser(td)) != 0)
328 if (ia == (struct in_ifaddr *)0)
329 return (EADDRNOTAVAIL);
335 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
339 if ((ifp->if_flags & IFF_BROADCAST) == 0)
341 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
345 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
347 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
351 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
355 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
357 oldaddr = ia->ia_dstaddr;
358 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
361 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
363 IFF_UNLOCKGIANT(ifp);
365 ia->ia_dstaddr = oldaddr;
369 if (ia->ia_flags & IFA_ROUTE) {
370 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
371 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
372 ia->ia_ifa.ifa_dstaddr =
373 (struct sockaddr *)&ia->ia_dstaddr;
374 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
379 if ((ifp->if_flags & IFF_BROADCAST) == 0)
381 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
385 error = in_ifinit(ifp, ia,
386 (struct sockaddr_in *) &ifr->ifr_addr, 1);
387 if (error != 0 && iaIsNew)
390 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
394 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
395 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
402 if (ia->ia_addr.sin_family == AF_INET) {
403 if (ifra->ifra_addr.sin_len == 0) {
404 ifra->ifra_addr = ia->ia_addr;
406 } else if (ifra->ifra_addr.sin_addr.s_addr ==
407 ia->ia_addr.sin_addr.s_addr)
410 if (ifra->ifra_mask.sin_len) {
412 ia->ia_sockmask = ifra->ifra_mask;
413 ia->ia_sockmask.sin_family = AF_INET;
415 ntohl(ia->ia_sockmask.sin_addr.s_addr);
418 if ((ifp->if_flags & IFF_POINTOPOINT) &&
419 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
421 ia->ia_dstaddr = ifra->ifra_dstaddr;
422 maskIsNew = 1; /* We lie; but the effect's the same */
424 if (ifra->ifra_addr.sin_family == AF_INET &&
425 (hostIsNew || maskIsNew))
426 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
427 if (error != 0 && iaIsNew)
430 if ((ifp->if_flags & IFF_BROADCAST) &&
431 (ifra->ifra_broadaddr.sin_family == AF_INET))
432 ia->ia_broadaddr = ifra->ifra_broadaddr;
434 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
439 * in_ifscrub kills the interface route.
443 * in_ifadown gets rid of all the rest of
444 * the routes. This is not quite the right
445 * thing to do, but at least if we are running
446 * a routing process they will come back.
448 in_ifadown(&ia->ia_ifa, 1);
450 * XXX horrible hack to detect that we are being called
453 if (ifaddr_byindex(ifp->if_index) == NULL) {
454 in_pcbpurgeif0(&ripcbinfo, ifp);
455 in_pcbpurgeif0(&udbinfo, ifp);
457 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
462 if (ifp == 0 || ifp->if_ioctl == 0)
465 error = (*ifp->if_ioctl)(ifp, cmd, data);
466 IFF_UNLOCKGIANT(ifp);
471 * Protect from ipintr() traversing address list while we're modifying
475 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
476 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
477 LIST_REMOVE(ia, ia_hash);
478 IFAFREE(&ia->ia_ifa);
486 * SIOCGLIFADDR: get first address. (?!?)
487 * SIOCGLIFADDR with IFLR_PREFIX:
488 * get first address that matches the specified prefix.
489 * SIOCALIFADDR: add the specified address.
490 * SIOCALIFADDR with IFLR_PREFIX:
491 * EINVAL since we can't deduce hostid part of the address.
492 * SIOCDLIFADDR: delete the specified address.
493 * SIOCDLIFADDR with IFLR_PREFIX:
494 * delete the first address that matches the specified prefix.
496 * EINVAL on invalid parameters
497 * EADDRNOTAVAIL on prefix match failed/specified address not found
498 * other values may be returned from in_ioctl()
501 in_lifaddr_ioctl(so, cmd, data, ifp, td)
508 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
513 panic("invalid argument to in_lifaddr_ioctl");
519 /* address must be specified on GET with IFLR_PREFIX */
520 if ((iflr->flags & IFLR_PREFIX) == 0)
525 /* address must be specified on ADD and DELETE */
526 if (iflr->addr.ss_family != AF_INET)
528 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
530 /* XXX need improvement */
531 if (iflr->dstaddr.ss_family
532 && iflr->dstaddr.ss_family != AF_INET)
534 if (iflr->dstaddr.ss_family
535 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
538 default: /*shouldn't happen*/
541 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
547 struct in_aliasreq ifra;
549 if (iflr->flags & IFLR_PREFIX)
552 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
553 bzero(&ifra, sizeof(ifra));
554 bcopy(iflr->iflr_name, ifra.ifra_name,
555 sizeof(ifra.ifra_name));
557 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
559 if (iflr->dstaddr.ss_family) { /*XXX*/
560 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
561 iflr->dstaddr.ss_len);
564 ifra.ifra_mask.sin_family = AF_INET;
565 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
566 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
568 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
573 struct in_ifaddr *ia;
574 struct in_addr mask, candidate, match;
575 struct sockaddr_in *sin;
578 bzero(&mask, sizeof(mask));
579 if (iflr->flags & IFLR_PREFIX) {
580 /* lookup a prefix rather than address. */
581 in_len2mask(&mask, iflr->prefixlen);
583 sin = (struct sockaddr_in *)&iflr->addr;
584 match.s_addr = sin->sin_addr.s_addr;
585 match.s_addr &= mask.s_addr;
587 /* if you set extra bits, that's wrong */
588 if (match.s_addr != sin->sin_addr.s_addr)
593 if (cmd == SIOCGLIFADDR) {
594 /* on getting an address, take the 1st match */
597 /* on deleting an address, do exact match */
598 in_len2mask(&mask, 32);
599 sin = (struct sockaddr_in *)&iflr->addr;
600 match.s_addr = sin->sin_addr.s_addr;
606 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
607 if (ifa->ifa_addr->sa_family != AF_INET6)
611 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
612 candidate.s_addr &= mask.s_addr;
613 if (candidate.s_addr == match.s_addr)
617 return EADDRNOTAVAIL;
618 ia = (struct in_ifaddr *)ifa;
620 if (cmd == SIOCGLIFADDR) {
621 /* fill in the if_laddrreq structure */
622 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
624 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
625 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
626 ia->ia_dstaddr.sin_len);
628 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
631 in_mask2len(&ia->ia_sockmask.sin_addr);
633 iflr->flags = 0; /*XXX*/
637 struct in_aliasreq ifra;
639 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
640 bzero(&ifra, sizeof(ifra));
641 bcopy(iflr->iflr_name, ifra.ifra_name,
642 sizeof(ifra.ifra_name));
644 bcopy(&ia->ia_addr, &ifra.ifra_addr,
645 ia->ia_addr.sin_len);
646 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
647 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
648 ia->ia_dstaddr.sin_len);
650 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
651 ia->ia_sockmask.sin_len);
653 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
659 return EOPNOTSUPP; /*just for safety*/
663 * Delete any existing route for an interface.
667 register struct ifnet *ifp;
668 register struct in_ifaddr *ia;
674 * Initialize an interface's internet address
675 * and routing table entry.
678 in_ifinit(ifp, ia, sin, scrub)
679 register struct ifnet *ifp;
680 register struct in_ifaddr *ia;
681 struct sockaddr_in *sin;
684 register u_long i = ntohl(sin->sin_addr.s_addr);
685 struct sockaddr_in oldaddr;
686 int s = splimp(), flags = RTF_UP, error = 0;
688 oldaddr = ia->ia_addr;
689 if (oldaddr.sin_family == AF_INET)
690 LIST_REMOVE(ia, ia_hash);
692 if (ia->ia_addr.sin_family == AF_INET)
693 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
696 * Give the interface a chance to initialize
697 * if this is its first address,
698 * and to validate the address if necessary.
702 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
703 IFF_UNLOCKGIANT(ifp);
706 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
707 ia->ia_addr = oldaddr;
708 if (ia->ia_addr.sin_family == AF_INET)
709 LIST_INSERT_HEAD(INADDR_HASH(
710 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
716 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
718 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
721 ia->ia_netmask = IN_CLASSA_NET;
722 else if (IN_CLASSB(i))
723 ia->ia_netmask = IN_CLASSB_NET;
725 ia->ia_netmask = IN_CLASSC_NET;
727 * The subnet mask usually includes at least the standard network part,
728 * but may may be smaller in the case of supernetting.
729 * If it is set, we believe it.
731 if (ia->ia_subnetmask == 0) {
732 ia->ia_subnetmask = ia->ia_netmask;
733 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
735 ia->ia_netmask &= ia->ia_subnetmask;
736 ia->ia_net = i & ia->ia_netmask;
737 ia->ia_subnet = i & ia->ia_subnetmask;
738 in_socktrim(&ia->ia_sockmask);
741 * XXX: carp(4) does not have interface route
743 if (ifp->if_type == IFT_CARP)
747 * Add route for the network.
749 ia->ia_ifa.ifa_metric = ifp->if_metric;
750 if (ifp->if_flags & IFF_BROADCAST) {
751 ia->ia_broadaddr.sin_addr.s_addr =
752 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
753 ia->ia_netbroadcast.s_addr =
754 htonl(ia->ia_net | ~ ia->ia_netmask);
755 } else if (ifp->if_flags & IFF_LOOPBACK) {
756 ia->ia_dstaddr = ia->ia_addr;
758 } else if (ifp->if_flags & IFF_POINTOPOINT) {
759 if (ia->ia_dstaddr.sin_family != AF_INET)
763 if ((error = in_addprefix(ia, flags)) != 0)
767 * If the interface supports multicast, join the "all hosts"
768 * multicast group on that interface.
770 if (ifp->if_flags & IFF_MULTICAST) {
773 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
774 in_addmulti(&addr, ifp);
779 #define rtinitflags(x) \
780 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
783 * Check if we have a route for the given prefix already or add a one
787 in_addprefix(target, flags)
788 struct in_ifaddr *target;
791 struct in_ifaddr *ia;
792 struct in_addr prefix, mask, p;
795 if ((flags & RTF_HOST) != 0)
796 prefix = target->ia_dstaddr.sin_addr;
798 prefix = target->ia_addr.sin_addr;
799 mask = target->ia_sockmask.sin_addr;
800 prefix.s_addr &= mask.s_addr;
803 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
805 p = ia->ia_dstaddr.sin_addr;
807 p = ia->ia_addr.sin_addr;
808 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
811 if (prefix.s_addr != p.s_addr)
815 * If we got a matching prefix route inserted by other
816 * interface address, we are done here.
818 if (ia->ia_flags & IFA_ROUTE)
823 * No-one seem to have this prefix route, so we try to insert it.
825 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
827 target->ia_flags |= IFA_ROUTE;
832 * If there is no other address in the system that can serve a route to the
833 * same prefix, remove the route. Hand over the route to the new address
837 in_scrubprefix(target)
838 struct in_ifaddr *target;
840 struct in_ifaddr *ia;
841 struct in_addr prefix, mask, p;
844 if ((target->ia_flags & IFA_ROUTE) == 0)
847 if (rtinitflags(target))
848 prefix = target->ia_dstaddr.sin_addr;
850 prefix = target->ia_addr.sin_addr;
851 mask = target->ia_sockmask.sin_addr;
852 prefix.s_addr &= mask.s_addr;
855 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
857 p = ia->ia_dstaddr.sin_addr;
859 p = ia->ia_addr.sin_addr;
860 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
863 if (prefix.s_addr != p.s_addr)
867 * If we got a matching prefix address, move IFA_ROUTE and
868 * the route itself to it. Make sure that routing daemons
871 * XXX: a special case for carp(4) interface
873 if ((ia->ia_flags & IFA_ROUTE) == 0
875 && (ia->ia_ifp->if_type != IFT_CARP)
878 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
879 rtinitflags(target));
880 target->ia_flags &= ~IFA_ROUTE;
882 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
883 rtinitflags(ia) | RTF_UP);
885 ia->ia_flags |= IFA_ROUTE;
891 * As no-one seem to have this prefix, we can remove the route.
893 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
894 target->ia_flags &= ~IFA_ROUTE;
901 * Return 1 if the address might be a local broadcast address.
904 in_broadcast(in, ifp)
908 register struct ifaddr *ifa;
911 if (in.s_addr == INADDR_BROADCAST ||
912 in.s_addr == INADDR_ANY)
914 if ((ifp->if_flags & IFF_BROADCAST) == 0)
916 t = ntohl(in.s_addr);
918 * Look through the list of addresses for a match
919 * with a broadcast address.
921 #define ia ((struct in_ifaddr *)ifa)
922 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
923 if (ifa->ifa_addr->sa_family == AF_INET &&
924 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
925 in.s_addr == ia->ia_netbroadcast.s_addr ||
927 * Check for old-style (host 0) broadcast.
929 t == ia->ia_subnet || t == ia->ia_net) &&
931 * Check for an all one subnetmask. These
932 * only exist when an interface gets a secondary
935 ia->ia_subnetmask != (u_long)0xffffffff)
941 * Add an address to the list of IP multicast addresses for a given interface.
945 register struct in_addr *ap;
946 register struct ifnet *ifp;
948 register struct in_multi *inm;
950 struct sockaddr_in sin;
951 struct ifmultiaddr *ifma;
955 * Call generic routine to add membership or increment
956 * refcount. It wants addresses in the form of a sockaddr,
957 * so we build one here (being careful to zero the unused bytes).
959 bzero(&sin, sizeof sin);
960 sin.sin_family = AF_INET;
961 sin.sin_len = sizeof sin;
963 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
970 * If ifma->ifma_protospec is null, then if_addmulti() created
971 * a new record. Otherwise, we are done.
973 if (ifma->ifma_protospec != NULL) {
975 return ifma->ifma_protospec;
978 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
979 at interrupt time? If so, need to fix if_addmulti. XXX */
980 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
989 inm->inm_ifma = ifma;
990 ifma->ifma_protospec = inm;
991 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
994 * Let IGMP know that we have joined a new IP multicast group.
1002 * Delete a multicast address record.
1006 register struct in_multi *inm;
1008 struct ifmultiaddr *ifma = inm->inm_ifma;
1009 struct in_multi my_inm;
1012 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1013 if (ifma->ifma_refcount == 1) {
1015 * No remaining claims to this record; let IGMP know that
1016 * we are leaving the multicast group.
1017 * But do it after the if_delmulti() which might reset
1018 * the interface and nuke the packet.
1021 ifma->ifma_protospec = NULL;
1022 LIST_REMOVE(inm, inm_link);
1023 free(inm, M_IPMADDR);
1025 /* XXX - should be separate API for when we have an ifma? */
1026 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1027 if (my_inm.inm_ifp != NULL)
1028 igmp_leavegroup(&my_inm);