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
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/sockio.h>
37 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
43 #include <net/if_types.h>
44 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <netinet/in_var.h>
48 #include <netinet/in_pcb.h>
50 #include <netinet/igmp_var.h>
52 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
54 static int in_mask2len(struct in_addr *);
55 static void in_len2mask(struct in_addr *, int);
56 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
57 struct ifnet *, struct thread *);
59 static int in_addprefix(struct in_ifaddr *, int);
60 static int in_scrubprefix(struct in_ifaddr *);
61 static void in_socktrim(struct sockaddr_in *);
62 static int in_ifinit(struct ifnet *,
63 struct in_ifaddr *, struct sockaddr_in *, int);
65 static int subnetsarelocal = 0;
66 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
67 &subnetsarelocal, 0, "Treat all subnets as directly connected");
69 struct in_multihead in_multihead; /* XXX BSS initialization */
71 extern struct inpcbinfo ripcbinfo;
72 extern struct inpcbinfo udbinfo;
75 * Return 1 if an internet address is for a ``local'' host
76 * (one to which we have a connection). If subnetsarelocal
77 * is true, this includes other subnets of the local net.
78 * Otherwise, it includes only the directly-connected (sub)nets.
84 register u_long i = ntohl(in.s_addr);
85 register struct in_ifaddr *ia;
87 if (subnetsarelocal) {
88 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
89 if ((i & ia->ia_netmask) == ia->ia_net)
92 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
93 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
100 * Return 1 if an internet address is for the local host and configured
101 * on one of its interfaces.
107 struct in_ifaddr *ia;
109 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
110 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
117 * Determine whether an IP address is in a reserved set of addresses
118 * that may not be forwarded, or whether datagrams to that destination
125 register u_long i = ntohl(in.s_addr);
128 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
131 net = i & IN_CLASSA_NET;
132 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
139 * Trim a mask in a sockaddr
143 struct sockaddr_in *ap;
145 register char *cplim = (char *) &ap->sin_addr;
146 register char *cp = (char *) (&ap->sin_addr + 1);
149 while (--cp >= cplim)
151 (ap)->sin_len = cp - (char *) (ap) + 1;
158 struct in_addr *mask;
164 for (x = 0; x < sizeof(*mask); x++) {
169 if (x < sizeof(*mask)) {
170 for (y = 0; y < 8; y++) {
171 if ((p[x] & (0x80 >> y)) == 0)
179 in_len2mask(mask, len)
180 struct in_addr *mask;
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(so, cmd, data, ifp, td)
204 register struct ifnet *ifp;
207 register struct ifreq *ifr = (struct ifreq *)data;
208 register struct in_ifaddr *ia = 0, *iap;
209 register struct ifaddr *ifa;
211 struct in_ifaddr *oia;
212 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
213 struct sockaddr_in oldaddr;
214 int error, hostIsNew, iaIsNew, maskIsNew, s;
221 if (td && (error = suser(td)) != 0)
227 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
231 * Find address for this interface, if it exists.
233 * If an alias address was specified, find that one instead of
234 * the first one on the interface, if possible.
237 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
238 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
239 if (iap->ia_ifp == ifp &&
240 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
245 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
247 if (iap->ia_addr.sin_family == AF_INET) {
259 return (EADDRNOTAVAIL);
260 if (ifra->ifra_addr.sin_family == AF_INET) {
261 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
262 if (ia->ia_ifp == ifp &&
263 ia->ia_addr.sin_addr.s_addr ==
264 ifra->ifra_addr.sin_addr.s_addr)
267 if ((ifp->if_flags & IFF_POINTOPOINT)
268 && (cmd == SIOCAIFADDR)
269 && (ifra->ifra_dstaddr.sin_addr.s_addr
274 if (cmd == SIOCDIFADDR && ia == 0)
275 return (EADDRNOTAVAIL);
280 if (td && (error = suser(td)) != 0)
284 return (EADDRNOTAVAIL);
285 if (ia == (struct in_ifaddr *)0) {
286 ia = (struct in_ifaddr *)
287 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
288 if (ia == (struct in_ifaddr *)NULL)
291 * Protect from ipintr() traversing address list
292 * while we're modifying it.
295 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
299 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
300 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
301 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
303 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
305 ia->ia_sockmask.sin_len = 8;
306 ia->ia_sockmask.sin_family = AF_INET;
307 if (ifp->if_flags & IFF_BROADCAST) {
308 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
309 ia->ia_broadaddr.sin_family = AF_INET;
318 if (td && (error = suser(td)) != 0)
326 if (ia == (struct in_ifaddr *)0)
327 return (EADDRNOTAVAIL);
333 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
337 if ((ifp->if_flags & IFF_BROADCAST) == 0)
339 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
343 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
345 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
349 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
353 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
355 oldaddr = ia->ia_dstaddr;
356 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
357 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
358 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
359 ia->ia_dstaddr = oldaddr;
362 if (ia->ia_flags & IFA_ROUTE) {
363 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
364 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
365 ia->ia_ifa.ifa_dstaddr =
366 (struct sockaddr *)&ia->ia_dstaddr;
367 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
372 if ((ifp->if_flags & IFF_BROADCAST) == 0)
374 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
378 error = in_ifinit(ifp, ia,
379 (struct sockaddr_in *) &ifr->ifr_addr, 1);
380 if (error != 0 && iaIsNew)
383 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
387 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
388 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
395 if (ia->ia_addr.sin_family == AF_INET) {
396 if (ifra->ifra_addr.sin_len == 0) {
397 ifra->ifra_addr = ia->ia_addr;
399 } else if (ifra->ifra_addr.sin_addr.s_addr ==
400 ia->ia_addr.sin_addr.s_addr)
403 if (ifra->ifra_mask.sin_len) {
405 ia->ia_sockmask = ifra->ifra_mask;
406 ia->ia_sockmask.sin_family = AF_INET;
408 ntohl(ia->ia_sockmask.sin_addr.s_addr);
411 if ((ifp->if_flags & IFF_POINTOPOINT) &&
412 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
414 ia->ia_dstaddr = ifra->ifra_dstaddr;
415 maskIsNew = 1; /* We lie; but the effect's the same */
417 if (ifra->ifra_addr.sin_family == AF_INET &&
418 (hostIsNew || maskIsNew))
419 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
420 if (error != 0 && iaIsNew)
423 if ((ifp->if_flags & IFF_BROADCAST) &&
424 (ifra->ifra_broadaddr.sin_family == AF_INET))
425 ia->ia_broadaddr = ifra->ifra_broadaddr;
427 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
432 * in_ifscrub kills the interface route.
436 * in_ifadown gets rid of all the rest of
437 * the routes. This is not quite the right
438 * thing to do, but at least if we are running
439 * a routing process they will come back.
441 in_ifadown(&ia->ia_ifa, 1);
443 * XXX horrible hack to detect that we are being called
446 if (ifaddr_byindex(ifp->if_index) == NULL) {
447 in_pcbpurgeif0(&ripcbinfo, ifp);
448 in_pcbpurgeif0(&udbinfo, ifp);
450 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
455 if (ifp == 0 || ifp->if_ioctl == 0)
457 return ((*ifp->if_ioctl)(ifp, cmd, data));
461 * Protect from ipintr() traversing address list while we're modifying
465 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
466 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
467 LIST_REMOVE(ia, ia_hash);
468 IFAFREE(&ia->ia_ifa);
476 * SIOCGLIFADDR: get first address. (?!?)
477 * SIOCGLIFADDR with IFLR_PREFIX:
478 * get first address that matches the specified prefix.
479 * SIOCALIFADDR: add the specified address.
480 * SIOCALIFADDR with IFLR_PREFIX:
481 * EINVAL since we can't deduce hostid part of the address.
482 * SIOCDLIFADDR: delete the specified address.
483 * SIOCDLIFADDR with IFLR_PREFIX:
484 * delete the first address that matches the specified prefix.
486 * EINVAL on invalid parameters
487 * EADDRNOTAVAIL on prefix match failed/specified address not found
488 * other values may be returned from in_ioctl()
491 in_lifaddr_ioctl(so, cmd, data, ifp, td)
498 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
503 panic("invalid argument to in_lifaddr_ioctl");
509 /* address must be specified on GET with IFLR_PREFIX */
510 if ((iflr->flags & IFLR_PREFIX) == 0)
515 /* address must be specified on ADD and DELETE */
516 if (iflr->addr.ss_family != AF_INET)
518 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
520 /* XXX need improvement */
521 if (iflr->dstaddr.ss_family
522 && iflr->dstaddr.ss_family != AF_INET)
524 if (iflr->dstaddr.ss_family
525 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
528 default: /*shouldn't happen*/
531 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
537 struct in_aliasreq ifra;
539 if (iflr->flags & IFLR_PREFIX)
542 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
543 bzero(&ifra, sizeof(ifra));
544 bcopy(iflr->iflr_name, ifra.ifra_name,
545 sizeof(ifra.ifra_name));
547 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
549 if (iflr->dstaddr.ss_family) { /*XXX*/
550 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
551 iflr->dstaddr.ss_len);
554 ifra.ifra_mask.sin_family = AF_INET;
555 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
556 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
558 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
563 struct in_ifaddr *ia;
564 struct in_addr mask, candidate, match;
565 struct sockaddr_in *sin;
568 bzero(&mask, sizeof(mask));
569 if (iflr->flags & IFLR_PREFIX) {
570 /* lookup a prefix rather than address. */
571 in_len2mask(&mask, iflr->prefixlen);
573 sin = (struct sockaddr_in *)&iflr->addr;
574 match.s_addr = sin->sin_addr.s_addr;
575 match.s_addr &= mask.s_addr;
577 /* if you set extra bits, that's wrong */
578 if (match.s_addr != sin->sin_addr.s_addr)
583 if (cmd == SIOCGLIFADDR) {
584 /* on getting an address, take the 1st match */
587 /* on deleting an address, do exact match */
588 in_len2mask(&mask, 32);
589 sin = (struct sockaddr_in *)&iflr->addr;
590 match.s_addr = sin->sin_addr.s_addr;
596 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
597 if (ifa->ifa_addr->sa_family != AF_INET6)
601 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
602 candidate.s_addr &= mask.s_addr;
603 if (candidate.s_addr == match.s_addr)
607 return EADDRNOTAVAIL;
608 ia = (struct in_ifaddr *)ifa;
610 if (cmd == SIOCGLIFADDR) {
611 /* fill in the if_laddrreq structure */
612 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
614 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
615 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
616 ia->ia_dstaddr.sin_len);
618 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
621 in_mask2len(&ia->ia_sockmask.sin_addr);
623 iflr->flags = 0; /*XXX*/
627 struct in_aliasreq ifra;
629 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
630 bzero(&ifra, sizeof(ifra));
631 bcopy(iflr->iflr_name, ifra.ifra_name,
632 sizeof(ifra.ifra_name));
634 bcopy(&ia->ia_addr, &ifra.ifra_addr,
635 ia->ia_addr.sin_len);
636 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
637 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
638 ia->ia_dstaddr.sin_len);
640 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
641 ia->ia_sockmask.sin_len);
643 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
649 return EOPNOTSUPP; /*just for safety*/
653 * Delete any existing route for an interface.
657 register struct ifnet *ifp;
658 register struct in_ifaddr *ia;
664 * Initialize an interface's internet address
665 * and routing table entry.
668 in_ifinit(ifp, ia, sin, scrub)
669 register struct ifnet *ifp;
670 register struct in_ifaddr *ia;
671 struct sockaddr_in *sin;
674 register u_long i = ntohl(sin->sin_addr.s_addr);
675 struct sockaddr_in oldaddr;
676 int s = splimp(), flags = RTF_UP, error = 0;
678 oldaddr = ia->ia_addr;
679 if (oldaddr.sin_family == AF_INET)
680 LIST_REMOVE(ia, ia_hash);
682 if (ia->ia_addr.sin_family == AF_INET)
683 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
686 * Give the interface a chance to initialize
687 * if this is its first address,
688 * and to validate the address if necessary.
691 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
693 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
694 ia->ia_addr = oldaddr;
695 if (ia->ia_addr.sin_family == AF_INET)
696 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
702 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
704 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
707 ia->ia_netmask = IN_CLASSA_NET;
708 else if (IN_CLASSB(i))
709 ia->ia_netmask = IN_CLASSB_NET;
711 ia->ia_netmask = IN_CLASSC_NET;
713 * The subnet mask usually includes at least the standard network part,
714 * but may may be smaller in the case of supernetting.
715 * If it is set, we believe it.
717 if (ia->ia_subnetmask == 0) {
718 ia->ia_subnetmask = ia->ia_netmask;
719 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
721 ia->ia_netmask &= ia->ia_subnetmask;
722 ia->ia_net = i & ia->ia_netmask;
723 ia->ia_subnet = i & ia->ia_subnetmask;
724 in_socktrim(&ia->ia_sockmask);
726 * Add route for the network.
728 ia->ia_ifa.ifa_metric = ifp->if_metric;
729 if (ifp->if_flags & IFF_BROADCAST) {
730 ia->ia_broadaddr.sin_addr.s_addr =
731 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
732 ia->ia_netbroadcast.s_addr =
733 htonl(ia->ia_net | ~ ia->ia_netmask);
734 } else if (ifp->if_flags & IFF_LOOPBACK) {
735 ia->ia_dstaddr = ia->ia_addr;
737 } else if (ifp->if_flags & IFF_POINTOPOINT) {
738 if (ia->ia_dstaddr.sin_family != AF_INET)
742 if ((error = in_addprefix(ia, flags)) != 0)
746 * If the interface supports multicast, join the "all hosts"
747 * multicast group on that interface.
749 if (ifp->if_flags & IFF_MULTICAST) {
752 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
753 in_addmulti(&addr, ifp);
758 #define rtinitflags(x) \
759 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
762 * Check if we have a route for the given prefix already or add a one
766 in_addprefix(target, flags)
767 struct in_ifaddr *target;
770 struct in_ifaddr *ia;
771 struct in_addr prefix, mask, p;
774 if ((flags & RTF_HOST) != 0)
775 prefix = target->ia_dstaddr.sin_addr;
777 prefix = target->ia_addr.sin_addr;
778 mask = target->ia_sockmask.sin_addr;
779 prefix.s_addr &= mask.s_addr;
782 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
784 p = ia->ia_dstaddr.sin_addr;
786 p = ia->ia_addr.sin_addr;
787 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
790 if (prefix.s_addr != p.s_addr)
794 * If we got a matching prefix route inserted by other
795 * interface address, we are done here.
797 if (ia->ia_flags & IFA_ROUTE)
802 * No-one seem to have this prefix route, so we try to insert it.
804 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
806 target->ia_flags |= IFA_ROUTE;
811 * If there is no other address in the system that can serve a route to the
812 * same prefix, remove the route. Hand over the route to the new address
816 in_scrubprefix(target)
817 struct in_ifaddr *target;
819 struct in_ifaddr *ia;
820 struct in_addr prefix, mask, p;
823 if ((target->ia_flags & IFA_ROUTE) == 0)
826 if (rtinitflags(target))
827 prefix = target->ia_dstaddr.sin_addr;
829 prefix = target->ia_addr.sin_addr;
830 mask = target->ia_sockmask.sin_addr;
831 prefix.s_addr &= mask.s_addr;
834 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
836 p = ia->ia_dstaddr.sin_addr;
838 p = ia->ia_addr.sin_addr;
839 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
842 if (prefix.s_addr != p.s_addr)
846 * If we got a matching prefix address, move IFA_ROUTE and
847 * the route itself to it. Make sure that routing daemons
850 if ((ia->ia_flags & IFA_ROUTE) == 0) {
851 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
852 rtinitflags(target));
853 target->ia_flags &= ~IFA_ROUTE;
855 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
856 rtinitflags(ia) | RTF_UP);
858 ia->ia_flags |= IFA_ROUTE;
864 * As no-one seem to have this prefix, we can remove the route.
866 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
867 target->ia_flags &= ~IFA_ROUTE;
874 * Return 1 if the address might be a local broadcast address.
877 in_broadcast(in, ifp)
881 register struct ifaddr *ifa;
884 if (in.s_addr == INADDR_BROADCAST ||
885 in.s_addr == INADDR_ANY)
887 if ((ifp->if_flags & IFF_BROADCAST) == 0)
889 t = ntohl(in.s_addr);
891 * Look through the list of addresses for a match
892 * with a broadcast address.
894 #define ia ((struct in_ifaddr *)ifa)
895 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
896 if (ifa->ifa_addr->sa_family == AF_INET &&
897 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
898 in.s_addr == ia->ia_netbroadcast.s_addr ||
900 * Check for old-style (host 0) broadcast.
902 t == ia->ia_subnet || t == ia->ia_net) &&
904 * Check for an all one subnetmask. These
905 * only exist when an interface gets a secondary
908 ia->ia_subnetmask != (u_long)0xffffffff)
914 * Add an address to the list of IP multicast addresses for a given interface.
918 register struct in_addr *ap;
919 register struct ifnet *ifp;
921 register struct in_multi *inm;
923 struct sockaddr_in sin;
924 struct ifmultiaddr *ifma;
928 * Call generic routine to add membership or increment
929 * refcount. It wants addresses in the form of a sockaddr,
930 * so we build one here (being careful to zero the unused bytes).
932 bzero(&sin, sizeof sin);
933 sin.sin_family = AF_INET;
934 sin.sin_len = sizeof sin;
936 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
943 * If ifma->ifma_protospec is null, then if_addmulti() created
944 * a new record. Otherwise, we are done.
946 if (ifma->ifma_protospec != 0) {
948 return ifma->ifma_protospec;
951 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
952 at interrupt time? If so, need to fix if_addmulti. XXX */
953 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
962 inm->inm_ifma = ifma;
963 ifma->ifma_protospec = inm;
964 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
967 * Let IGMP know that we have joined a new IP multicast group.
975 * Delete a multicast address record.
979 register struct in_multi *inm;
981 struct ifmultiaddr *ifma = inm->inm_ifma;
982 struct in_multi my_inm;
985 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
986 if (ifma->ifma_refcount == 1) {
988 * No remaining claims to this record; let IGMP know that
989 * we are leaving the multicast group.
990 * But do it after the if_delmulti() which might reset
991 * the interface and nuke the packet.
994 ifma->ifma_protospec = 0;
995 LIST_REMOVE(inm, inm_link);
996 free(inm, M_IPMADDR);
998 /* XXX - should be separate API for when we have an ifma? */
999 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1000 if (my_inm.inm_ifp != NULL)
1001 igmp_leavegroup(&my_inm);