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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sockio.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
45 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
51 #include <net/if_var.h>
52 #include <net/if_dl.h>
53 #include <net/if_llatbl.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/igmp_var.h>
63 #include <netinet/udp.h>
64 #include <netinet/udp_var.h>
66 static int in_mask2len(struct in_addr *);
67 static void in_len2mask(struct in_addr *, int);
68 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
69 struct ifnet *, struct thread *);
71 static int in_addprefix(struct in_ifaddr *, int);
72 static int in_scrubprefix(struct in_ifaddr *);
73 static void in_socktrim(struct sockaddr_in *);
74 static int in_ifinit(struct ifnet *,
75 struct in_ifaddr *, struct sockaddr_in *, int);
76 static void in_purgemaddrs(struct ifnet *);
78 static VNET_DEFINE(int, subnetsarelocal);
79 static VNET_DEFINE(int, sameprefixcarponly);
80 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
82 #define V_subnetsarelocal VNET(subnetsarelocal)
83 #define V_sameprefixcarponly VNET(sameprefixcarponly)
84 #define V_ripcbinfo VNET(ripcbinfo)
86 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
87 &VNET_NAME(subnetsarelocal), 0,
88 "Treat all subnets as directly connected");
89 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
90 &VNET_NAME(sameprefixcarponly), 0,
91 "Refuse to create same prefixes on different interfaces");
94 * Return 1 if an internet address is for a ``local'' host
95 * (one to which we have a connection). If subnetsarelocal
96 * is true, this includes other subnets of the local net.
97 * Otherwise, it includes only the directly-connected (sub)nets.
100 in_localaddr(struct in_addr in)
102 register u_long i = ntohl(in.s_addr);
103 register struct in_ifaddr *ia;
106 if (V_subnetsarelocal) {
107 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
108 if ((i & ia->ia_netmask) == ia->ia_net) {
114 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
115 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
126 * Return 1 if an internet address is for the local host and configured
127 * on one of its interfaces.
130 in_localip(struct in_addr in)
132 struct in_ifaddr *ia;
135 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
136 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
146 * Determine whether an IP address is in a reserved set of addresses
147 * that may not be forwarded, or whether datagrams to that destination
151 in_canforward(struct in_addr in)
153 register u_long i = ntohl(in.s_addr);
156 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
159 net = i & IN_CLASSA_NET;
160 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
167 * Trim a mask in a sockaddr
170 in_socktrim(struct sockaddr_in *ap)
172 register char *cplim = (char *) &ap->sin_addr;
173 register char *cp = (char *) (&ap->sin_addr + 1);
176 while (--cp >= cplim)
178 (ap)->sin_len = cp - (char *) (ap) + 1;
185 struct in_addr *mask;
191 for (x = 0; x < sizeof(*mask); x++) {
196 if (x < sizeof(*mask)) {
197 for (y = 0; y < 8; y++) {
198 if ((p[x] & (0x80 >> y)) == 0)
206 in_len2mask(struct in_addr *mask, int len)
212 bzero(mask, sizeof(*mask));
213 for (i = 0; i < len / 8; i++)
216 p[i] = (0xff00 >> (len % 8)) & 0xff;
220 * Generic internet control operations (ioctl's).
222 * ifp is NULL if not an interface-specific ioctl.
226 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
229 register struct ifreq *ifr = (struct ifreq *)data;
230 register struct in_ifaddr *ia, *iap;
231 register struct ifaddr *ifa;
232 struct in_addr allhosts_addr;
234 struct in_ifinfo *ii;
235 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
236 struct sockaddr_in oldaddr;
237 int error, hostIsNew, iaIsNew, maskIsNew;
243 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
246 * Filter out ioctls we implement directly; forward the rest on to
247 * in_lifaddr_ioctl() and ifp->if_ioctl().
264 error = priv_check(td, PRIV_NET_ADDIFADDR);
270 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
274 error = priv_check(td, PRIV_NET_DELIFADDR);
280 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
285 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
288 if (ifp == NULL || ifp->if_ioctl == NULL)
290 return ((*ifp->if_ioctl)(ifp, cmd, data));
294 return (EADDRNOTAVAIL);
297 * Security checks before we get involved in any work.
306 error = priv_check(td, PRIV_NET_ADDIFADDR);
314 error = priv_check(td, PRIV_NET_DELIFADDR);
322 * Find address for this interface, if it exists.
324 * If an alias address was specified, find that one instead of the
325 * first one on the interface, if possible.
327 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
329 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
330 if (iap->ia_ifp == ifp &&
331 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
332 if (td == NULL || prison_check_ip4(td->td_ucred,
339 ifa_ref(&ia->ia_ifa);
343 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
345 if (iap->ia_addr.sin_family == AF_INET) {
347 prison_check_ip4(td->td_ucred,
348 &iap->ia_addr.sin_addr) != 0)
355 ifa_ref(&ia->ia_ifa);
365 if (ifra->ifra_addr.sin_family == AF_INET) {
366 struct in_ifaddr *oia;
369 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
370 if (ia->ia_ifp == ifp &&
371 ia->ia_addr.sin_addr.s_addr ==
372 ifra->ifra_addr.sin_addr.s_addr)
375 if (ia != NULL && ia != oia)
376 ifa_ref(&ia->ia_ifa);
377 if (oia != NULL && ia != oia)
378 ifa_free(&oia->ia_ifa);
380 if ((ifp->if_flags & IFF_POINTOPOINT)
381 && (cmd == SIOCAIFADDR)
382 && (ifra->ifra_dstaddr.sin_addr.s_addr
384 error = EDESTADDRREQ;
388 if (cmd == SIOCDIFADDR && ia == NULL) {
389 error = EADDRNOTAVAIL;
397 ia = (struct in_ifaddr *)
398 malloc(sizeof *ia, M_IFADDR, M_NOWAIT |
407 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
408 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
409 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
411 ia->ia_sockmask.sin_len = 8;
412 ia->ia_sockmask.sin_family = AF_INET;
413 if (ifp->if_flags & IFF_BROADCAST) {
414 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
415 ia->ia_broadaddr.sin_family = AF_INET;
419 ifa_ref(ifa); /* if_addrhead */
421 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
423 ifa_ref(ifa); /* in_ifaddrhead */
425 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
437 error = EADDRNOTAVAIL;
444 * Most paths in this switch return directly or via out. Only paths
445 * that remove the address break in order to hit common removal code.
449 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
453 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
457 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
461 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
465 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
469 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
473 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
477 oldaddr = ia->ia_dstaddr;
478 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
479 if (ifp->if_ioctl != NULL) {
480 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
483 ia->ia_dstaddr = oldaddr;
487 if (ia->ia_flags & IFA_ROUTE) {
488 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
489 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
490 ia->ia_ifa.ifa_dstaddr =
491 (struct sockaddr *)&ia->ia_dstaddr;
492 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
497 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
501 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
505 error = in_ifinit(ifp, ia,
506 (struct sockaddr_in *) &ifr->ifr_addr, 1);
507 if (error != 0 && iaIsNew)
510 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
512 (ifp->if_flags & IFF_MULTICAST) != 0) {
513 error = in_joingroup(ifp, &allhosts_addr,
514 NULL, &ii->ii_allhosts);
516 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
522 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
523 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
530 if (ia->ia_addr.sin_family == AF_INET) {
531 if (ifra->ifra_addr.sin_len == 0) {
532 ifra->ifra_addr = ia->ia_addr;
534 } else if (ifra->ifra_addr.sin_addr.s_addr ==
535 ia->ia_addr.sin_addr.s_addr)
538 if (ifra->ifra_mask.sin_len) {
541 * Need to scrub the prefix here in case
542 * the issued command is SIOCAIFADDR with
543 * the same address, but with a different
544 * prefix length. And if the prefix length
545 * is the same as before, then the call is
546 * un-necessarily executed here.
549 ia->ia_sockmask = ifra->ifra_mask;
550 ia->ia_sockmask.sin_family = AF_INET;
552 ntohl(ia->ia_sockmask.sin_addr.s_addr);
555 if ((ifp->if_flags & IFF_POINTOPOINT) &&
556 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
558 ia->ia_dstaddr = ifra->ifra_dstaddr;
559 maskIsNew = 1; /* We lie; but the effect's the same */
561 if (ifra->ifra_addr.sin_family == AF_INET &&
562 (hostIsNew || maskIsNew))
563 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
564 if (error != 0 && iaIsNew)
567 if ((ifp->if_flags & IFF_BROADCAST) &&
568 (ifra->ifra_broadaddr.sin_family == AF_INET))
569 ia->ia_broadaddr = ifra->ifra_broadaddr;
571 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
573 (ifp->if_flags & IFF_MULTICAST) != 0) {
574 error = in_joingroup(ifp, &allhosts_addr,
575 NULL, &ii->ii_allhosts);
577 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
583 * in_ifscrub kills the interface route.
588 * in_ifadown gets rid of all the rest of
589 * the routes. This is not quite the right
590 * thing to do, but at least if we are running
591 * a routing process they will come back.
593 in_ifadown(&ia->ia_ifa, 1);
594 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
599 panic("in_control: unsupported ioctl");
603 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
605 ifa_free(&ia->ia_ifa); /* if_addrhead */
608 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
609 if (ia->ia_addr.sin_family == AF_INET) {
610 struct in_ifaddr *if_ia;
612 LIST_REMOVE(ia, ia_hash);
615 * If this is the last IPv4 address configured on this
616 * interface, leave the all-hosts group.
617 * No state-change report need be transmitted.
620 IFP_TO_IA(ifp, if_ia);
622 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
624 if (ii->ii_allhosts) {
625 (void)in_leavegroup_locked(ii->ii_allhosts,
627 ii->ii_allhosts = NULL;
631 ifa_free(&if_ia->ia_ifa);
634 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
637 ifa_free(&ia->ia_ifa);
643 * SIOCGLIFADDR: get first address. (?!?)
644 * SIOCGLIFADDR with IFLR_PREFIX:
645 * get first address that matches the specified prefix.
646 * SIOCALIFADDR: add the specified address.
647 * SIOCALIFADDR with IFLR_PREFIX:
648 * EINVAL since we can't deduce hostid part of the address.
649 * SIOCDLIFADDR: delete the specified address.
650 * SIOCDLIFADDR with IFLR_PREFIX:
651 * delete the first address that matches the specified prefix.
653 * EINVAL on invalid parameters
654 * EADDRNOTAVAIL on prefix match failed/specified address not found
655 * other values may be returned from in_ioctl()
658 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
659 struct ifnet *ifp, struct thread *td)
661 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
665 if (data == NULL || ifp == NULL) {
666 panic("invalid argument to in_lifaddr_ioctl");
672 /* address must be specified on GET with IFLR_PREFIX */
673 if ((iflr->flags & IFLR_PREFIX) == 0)
678 /* address must be specified on ADD and DELETE */
679 if (iflr->addr.ss_family != AF_INET)
681 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
683 /* XXX need improvement */
684 if (iflr->dstaddr.ss_family
685 && iflr->dstaddr.ss_family != AF_INET)
687 if (iflr->dstaddr.ss_family
688 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
691 default: /*shouldn't happen*/
694 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
700 struct in_aliasreq ifra;
702 if (iflr->flags & IFLR_PREFIX)
705 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
706 bzero(&ifra, sizeof(ifra));
707 bcopy(iflr->iflr_name, ifra.ifra_name,
708 sizeof(ifra.ifra_name));
710 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
712 if (iflr->dstaddr.ss_family) { /*XXX*/
713 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
714 iflr->dstaddr.ss_len);
717 ifra.ifra_mask.sin_family = AF_INET;
718 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
719 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
721 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
726 struct in_ifaddr *ia;
727 struct in_addr mask, candidate, match;
728 struct sockaddr_in *sin;
730 bzero(&mask, sizeof(mask));
731 bzero(&match, sizeof(match));
732 if (iflr->flags & IFLR_PREFIX) {
733 /* lookup a prefix rather than address. */
734 in_len2mask(&mask, iflr->prefixlen);
736 sin = (struct sockaddr_in *)&iflr->addr;
737 match.s_addr = sin->sin_addr.s_addr;
738 match.s_addr &= mask.s_addr;
740 /* if you set extra bits, that's wrong */
741 if (match.s_addr != sin->sin_addr.s_addr)
745 /* on getting an address, take the 1st match */
746 /* on deleting an address, do exact match */
747 if (cmd != SIOCGLIFADDR) {
748 in_len2mask(&mask, 32);
749 sin = (struct sockaddr_in *)&iflr->addr;
750 match.s_addr = sin->sin_addr.s_addr;
754 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
755 if (ifa->ifa_addr->sa_family != AF_INET6)
757 if (match.s_addr == 0)
759 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
760 candidate.s_addr &= mask.s_addr;
761 if (candidate.s_addr == match.s_addr)
765 return (EADDRNOTAVAIL);
766 ia = (struct in_ifaddr *)ifa;
768 if (cmd == SIOCGLIFADDR) {
769 /* fill in the if_laddrreq structure */
770 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
772 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
773 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
774 ia->ia_dstaddr.sin_len);
776 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
779 in_mask2len(&ia->ia_sockmask.sin_addr);
781 iflr->flags = 0; /*XXX*/
785 struct in_aliasreq ifra;
787 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
788 bzero(&ifra, sizeof(ifra));
789 bcopy(iflr->iflr_name, ifra.ifra_name,
790 sizeof(ifra.ifra_name));
792 bcopy(&ia->ia_addr, &ifra.ifra_addr,
793 ia->ia_addr.sin_len);
794 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
795 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
796 ia->ia_dstaddr.sin_len);
798 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
799 ia->ia_sockmask.sin_len);
801 return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
807 return (EOPNOTSUPP); /*just for safety*/
811 * Delete any existing route for an interface.
814 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
821 * Initialize an interface's internet address
822 * and routing table entry.
825 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
828 register u_long i = ntohl(sin->sin_addr.s_addr);
829 struct sockaddr_in oldaddr;
830 struct rtentry *rt = NULL;
831 struct rt_addrinfo info;
832 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
833 int s = splimp(), flags = RTF_UP, error = 0;
835 oldaddr = ia->ia_addr;
836 if (oldaddr.sin_family == AF_INET)
837 LIST_REMOVE(ia, ia_hash);
839 if (ia->ia_addr.sin_family == AF_INET) {
841 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
846 * Give the interface a chance to initialize
847 * if this is its first address,
848 * and to validate the address if necessary.
850 if (ifp->if_ioctl != NULL) {
851 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
854 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
855 ia->ia_addr = oldaddr;
857 if (ia->ia_addr.sin_family == AF_INET)
858 LIST_INSERT_HEAD(INADDR_HASH(
859 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
862 * If oldaddr family is not AF_INET (e.g.
863 * interface has been just created) in_control
864 * does not call LIST_REMOVE, and we end up
865 * with bogus ia entries in hash
867 LIST_REMOVE(ia, ia_hash);
874 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
876 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
879 ia->ia_netmask = IN_CLASSA_NET;
880 else if (IN_CLASSB(i))
881 ia->ia_netmask = IN_CLASSB_NET;
883 ia->ia_netmask = IN_CLASSC_NET;
885 * The subnet mask usually includes at least the standard network part,
886 * but may may be smaller in the case of supernetting.
887 * If it is set, we believe it.
889 if (ia->ia_subnetmask == 0) {
890 ia->ia_subnetmask = ia->ia_netmask;
891 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
893 ia->ia_netmask &= ia->ia_subnetmask;
894 ia->ia_net = i & ia->ia_netmask;
895 ia->ia_subnet = i & ia->ia_subnetmask;
896 in_socktrim(&ia->ia_sockmask);
899 * XXX: carp(4) does not have interface route
901 if (ifp->if_type == IFT_CARP)
905 * Add route for the network.
907 ia->ia_ifa.ifa_metric = ifp->if_metric;
908 if (ifp->if_flags & IFF_BROADCAST) {
909 ia->ia_broadaddr.sin_addr.s_addr =
910 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
911 ia->ia_netbroadcast.s_addr =
912 htonl(ia->ia_net | ~ ia->ia_netmask);
913 } else if (ifp->if_flags & IFF_LOOPBACK) {
914 ia->ia_dstaddr = ia->ia_addr;
916 } else if (ifp->if_flags & IFF_POINTOPOINT) {
917 if (ia->ia_dstaddr.sin_family != AF_INET)
921 if ((error = in_addprefix(ia, flags)) != 0)
924 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
928 * add a loopback route to self
930 if (V_useloopback && !(ifp->if_flags & IFF_LOOPBACK)) {
931 bzero(&info, sizeof(info));
932 info.rti_ifp = V_loif;
933 info.rti_flags = ia->ia_flags | RTF_HOST | RTF_STATIC;
934 info.rti_info[RTAX_DST] = (struct sockaddr *)&ia->ia_addr;
935 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
936 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0);
938 if (error == 0 && rt != NULL) {
940 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type =
942 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index =
943 rt->rt_ifp->if_index;
946 } else if (error != 0)
947 log(LOG_INFO, "in_ifinit: insertion failed\n");
953 #define rtinitflags(x) \
954 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
957 * Check if we have a route for the given prefix already or add one accordingly.
960 in_addprefix(struct in_ifaddr *target, int flags)
962 struct in_ifaddr *ia;
963 struct in_addr prefix, mask, p, m;
966 if ((flags & RTF_HOST) != 0) {
967 prefix = target->ia_dstaddr.sin_addr;
970 prefix = target->ia_addr.sin_addr;
971 mask = target->ia_sockmask.sin_addr;
972 prefix.s_addr &= mask.s_addr;
976 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
977 if (rtinitflags(ia)) {
978 p = ia->ia_addr.sin_addr;
980 if (prefix.s_addr != p.s_addr)
983 p = ia->ia_addr.sin_addr;
984 m = ia->ia_sockmask.sin_addr;
985 p.s_addr &= m.s_addr;
987 if (prefix.s_addr != p.s_addr ||
988 mask.s_addr != m.s_addr)
993 * If we got a matching prefix route inserted by other
994 * interface address, we are done here.
996 if (ia->ia_flags & IFA_ROUTE) {
997 if (V_sameprefixcarponly &&
998 target->ia_ifp->if_type != IFT_CARP &&
999 ia->ia_ifp->if_type != IFT_CARP) {
1000 IN_IFADDR_RUNLOCK();
1003 struct route pfx_ro;
1004 struct sockaddr_in *pfx_addr;
1005 struct rtentry msg_rt;
1008 * This is a bit questionable because there is no
1009 * additional route entry added for an address alias.
1010 * Therefore this route report is inaccurate. Perhaps
1011 * it's better to supply a empty rtentry as how it
1012 * is done in in_scrubprefix().
1014 bzero(&pfx_ro, sizeof(pfx_ro));
1015 pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst);
1016 pfx_addr->sin_len = sizeof(*pfx_addr);
1017 pfx_addr->sin_family = AF_INET;
1018 pfx_addr->sin_addr = prefix;
1019 rtalloc_ign_fib(&pfx_ro, 0, 0);
1020 if (pfx_ro.ro_rt != NULL) {
1021 msg_rt = *pfx_ro.ro_rt;
1023 * Point the gateway to the given interface
1024 * address as if a new prefix route entry has
1025 * been added through the new address alias.
1026 * All other parts of the rtentry is accurate,
1027 * e.g., rt_key, rt_mask, rt_ifp etc.
1030 (struct sockaddr *)&ia->ia_addr;
1031 rt_newaddrmsg(RTM_ADD,
1032 (struct ifaddr *)target,
1034 RTFREE(pfx_ro.ro_rt);
1037 IN_IFADDR_RUNLOCK();
1042 IN_IFADDR_RUNLOCK();
1045 * No-one seem to have this prefix route, so we try to insert it.
1047 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
1049 target->ia_flags |= IFA_ROUTE;
1053 extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
1056 * If there is no other address in the system that can serve a route to the
1057 * same prefix, remove the route. Hand over the route to the new address
1061 in_scrubprefix(struct in_ifaddr *target)
1063 struct in_ifaddr *ia;
1064 struct in_addr prefix, mask, p;
1066 struct sockaddr_in prefix0, mask0;
1067 struct rt_addrinfo info;
1068 struct sockaddr_dl null_sdl;
1071 * Remove the loopback route to the interface address.
1072 * The "useloopback" setting is not consulted because if the
1073 * user configures an interface address, turns off this
1074 * setting, and then tries to delete that interface address,
1075 * checking the current setting of "useloopback" would leave
1076 * that interface address loopback route untouched, which
1077 * would be wrong. Therefore the interface address loopback route
1078 * deletion is unconditional.
1080 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
1081 !(target->ia_ifp->if_flags & IFF_LOOPBACK)) {
1082 bzero(&null_sdl, sizeof(null_sdl));
1083 null_sdl.sdl_len = sizeof(null_sdl);
1084 null_sdl.sdl_family = AF_LINK;
1085 null_sdl.sdl_type = V_loif->if_type;
1086 null_sdl.sdl_index = V_loif->if_index;
1087 bzero(&info, sizeof(info));
1088 info.rti_flags = target->ia_flags | RTF_HOST | RTF_STATIC;
1089 info.rti_info[RTAX_DST] = (struct sockaddr *)&target->ia_addr;
1090 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
1091 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0);
1094 log(LOG_INFO, "in_scrubprefix: deletion failed\n");
1097 if ((target->ia_flags & IFA_ROUTE) == 0) {
1101 * Report a blank rtentry when a route has not been
1102 * installed for the given interface address.
1104 bzero(&rt, sizeof(rt));
1105 rt_newaddrmsg(RTM_DELETE,
1106 (struct ifaddr *)target,
1111 if (rtinitflags(target))
1112 prefix = target->ia_dstaddr.sin_addr;
1114 prefix = target->ia_addr.sin_addr;
1115 mask = target->ia_sockmask.sin_addr;
1116 prefix.s_addr &= mask.s_addr;
1117 /* remove arp cache */
1118 arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
1122 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1123 if (rtinitflags(ia))
1124 p = ia->ia_dstaddr.sin_addr;
1126 p = ia->ia_addr.sin_addr;
1127 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1130 if (prefix.s_addr != p.s_addr)
1134 * If we got a matching prefix address, move IFA_ROUTE and
1135 * the route itself to it. Make sure that routing daemons
1138 * XXX: a special case for carp(4) interface
1140 if ((ia->ia_flags & IFA_ROUTE) == 0
1142 && (ia->ia_ifp->if_type != IFT_CARP)
1145 IN_IFADDR_RUNLOCK();
1146 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
1147 rtinitflags(target));
1148 target->ia_flags &= ~IFA_ROUTE;
1150 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
1151 rtinitflags(ia) | RTF_UP);
1153 ia->ia_flags |= IFA_ROUTE;
1157 IN_IFADDR_RUNLOCK();
1160 * remove all L2 entries on the given prefix
1162 bzero(&prefix0, sizeof(prefix0));
1163 prefix0.sin_len = sizeof(prefix0);
1164 prefix0.sin_family = AF_INET;
1165 prefix0.sin_addr.s_addr = target->ia_subnet;
1166 bzero(&mask0, sizeof(mask0));
1167 mask0.sin_len = sizeof(mask0);
1168 mask0.sin_family = AF_INET;
1169 mask0.sin_addr.s_addr = target->ia_subnetmask;
1170 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
1171 (struct sockaddr *)&mask0);
1174 * As no-one seem to have this prefix, we can remove the route.
1176 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1177 target->ia_flags &= ~IFA_ROUTE;
1184 * Return 1 if the address might be a local broadcast address.
1187 in_broadcast(struct in_addr in, struct ifnet *ifp)
1189 register struct ifaddr *ifa;
1192 if (in.s_addr == INADDR_BROADCAST ||
1193 in.s_addr == INADDR_ANY)
1195 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1197 t = ntohl(in.s_addr);
1199 * Look through the list of addresses for a match
1200 * with a broadcast address.
1202 #define ia ((struct in_ifaddr *)ifa)
1203 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1204 if (ifa->ifa_addr->sa_family == AF_INET &&
1205 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1206 in.s_addr == ia->ia_netbroadcast.s_addr ||
1208 * Check for old-style (host 0) broadcast.
1210 t == ia->ia_subnet || t == ia->ia_net) &&
1212 * Check for an all one subnetmask. These
1213 * only exist when an interface gets a secondary
1216 ia->ia_subnetmask != (u_long)0xffffffff)
1223 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1226 in_ifdetach(struct ifnet *ifp)
1229 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1230 in_pcbpurgeif0(&V_udbinfo, ifp);
1231 in_purgemaddrs(ifp);
1235 * Delete all IPv4 multicast address records, and associated link-layer
1236 * multicast address records, associated with ifp.
1237 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1238 * XXX This should not race with ifma_protospec being set during
1239 * a new allocation, if it does, we have bigger problems.
1242 in_purgemaddrs(struct ifnet *ifp)
1244 LIST_HEAD(,in_multi) purgeinms;
1245 struct in_multi *inm, *tinm;
1246 struct ifmultiaddr *ifma;
1248 LIST_INIT(&purgeinms);
1252 * Extract list of in_multi associated with the detaching ifp
1253 * which the PF_INET layer is about to release.
1254 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1255 * by code further down.
1258 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1259 if (ifma->ifma_addr->sa_family != AF_INET ||
1260 ifma->ifma_protospec == NULL)
1263 KASSERT(ifma->ifma_protospec != NULL,
1264 ("%s: ifma_protospec is NULL", __func__));
1266 inm = (struct in_multi *)ifma->ifma_protospec;
1267 LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1269 IF_ADDR_UNLOCK(ifp);
1271 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1272 LIST_REMOVE(inm, inm_link);
1273 inm_release_locked(inm);
1280 #include <net/if_dl.h>
1281 #include <netinet/if_ether.h>
1284 struct llentry base;
1285 struct sockaddr_in l3_addr4;
1288 static struct llentry *
1289 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
1291 struct in_llentry *lle;
1293 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
1294 if (lle == NULL) /* NB: caller generates msg */
1297 callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
1299 * For IPv4 this will trigger "arpresolve" to generate
1302 lle->base.la_expire = time_second; /* mark expired */
1303 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
1304 lle->base.lle_refcnt = 1;
1305 LLE_LOCK_INIT(&lle->base);
1310 * Deletes an address from the address table.
1311 * This function is called by the timer functions
1312 * such as arptimer() and nd6_llinfo_timer(), and
1313 * the caller does the locking.
1316 in_lltable_free(struct lltable *llt, struct llentry *lle)
1319 LLE_LOCK_DESTROY(lle);
1320 free(lle, M_LLTABLE);
1324 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1325 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1328 in_lltable_prefix_free(struct lltable *llt,
1329 const struct sockaddr *prefix,
1330 const struct sockaddr *mask)
1332 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1333 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1334 struct llentry *lle, *next;
1337 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
1338 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1340 if (IN_ARE_MASKED_ADDR_EQUAL((struct sockaddr_in *)L3_ADDR(lle),
1342 callout_drain(&lle->la_timer);
1352 in_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr)
1356 KASSERT(l3addr->sa_family == AF_INET,
1357 ("sin_family %d", l3addr->sa_family));
1359 /* XXX rtalloc1 should take a const param */
1360 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1361 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
1362 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1363 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1373 * Return NULL if not found or marked for deletion.
1374 * If found return lle read locked.
1376 static struct llentry *
1377 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1379 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1380 struct ifnet *ifp = llt->llt_ifp;
1381 struct llentry *lle;
1382 struct llentries *lleh;
1385 IF_AFDATA_LOCK_ASSERT(ifp);
1386 KASSERT(l3addr->sa_family == AF_INET,
1387 ("sin_family %d", l3addr->sa_family));
1389 hashkey = sin->sin_addr.s_addr;
1390 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1391 LIST_FOREACH(lle, lleh, lle_next) {
1392 struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle);
1393 if (lle->la_flags & LLE_DELETED)
1395 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1400 if (flags & LLE_DELETE)
1401 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle);
1403 if (!(flags & LLE_CREATE))
1406 * A route that covers the given address must have
1407 * been installed 1st because we are doing a resolution,
1410 if (!(flags & LLE_IFADDR) &&
1411 in_lltable_rtcheck(ifp, l3addr) != 0)
1414 lle = in_lltable_new(l3addr, flags);
1416 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1419 lle->la_flags = flags & ~LLE_CREATE;
1420 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1421 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1422 lle->la_flags |= (LLE_VALID | LLE_STATIC);
1426 lle->lle_head = lleh;
1427 LIST_INSERT_HEAD(lleh, lle, lle_next);
1428 } else if (flags & LLE_DELETE) {
1429 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1431 lle->la_flags = LLE_DELETED;
1434 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1440 if (LLE_IS_VALID(lle)) {
1441 if (flags & LLE_EXCLUSIVE)
1451 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1453 #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle))
1454 struct ifnet *ifp = llt->llt_ifp;
1455 struct llentry *lle;
1458 struct rt_msghdr rtm;
1459 struct sockaddr_inarp sin;
1460 struct sockaddr_dl sdl;
1464 LLTABLE_LOCK_ASSERT();
1467 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1468 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1469 struct sockaddr_dl *sdl;
1471 /* skip deleted entries */
1472 if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
1474 /* Skip if jailed and not a valid IP of the prison. */
1475 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1478 * produce a msg made of:
1480 * struct sockaddr_inarp; (IPv4)
1481 * struct sockaddr_dl;
1483 bzero(&arpc, sizeof(arpc));
1484 arpc.rtm.rtm_msglen = sizeof(arpc);
1485 arpc.rtm.rtm_version = RTM_VERSION;
1486 arpc.rtm.rtm_type = RTM_GET;
1487 arpc.rtm.rtm_flags = RTF_UP;
1488 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1489 arpc.sin.sin_family = AF_INET;
1490 arpc.sin.sin_len = sizeof(arpc.sin);
1491 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1494 if (lle->la_flags & LLE_PUB) {
1495 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1497 if (lle->la_flags & LLE_PROXY)
1498 arpc.sin.sin_other = SIN_PROXY;
1502 sdl->sdl_family = AF_LINK;
1503 sdl->sdl_len = sizeof(*sdl);
1504 sdl->sdl_alen = ifp->if_addrlen;
1505 sdl->sdl_index = ifp->if_index;
1506 sdl->sdl_type = ifp->if_type;
1507 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1509 arpc.rtm.rtm_rmx.rmx_expire =
1510 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1511 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1512 if (lle->la_flags & LLE_STATIC)
1513 arpc.rtm.rtm_flags |= RTF_STATIC;
1514 arpc.rtm.rtm_index = ifp->if_index;
1515 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1525 in_domifattach(struct ifnet *ifp)
1527 struct in_ifinfo *ii;
1528 struct lltable *llt;
1530 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1532 llt = lltable_init(ifp, AF_INET);
1534 llt->llt_new = in_lltable_new;
1535 llt->llt_free = in_lltable_free;
1536 llt->llt_prefix_free = in_lltable_prefix_free;
1537 llt->llt_rtcheck = in_lltable_rtcheck;
1538 llt->llt_lookup = in_lltable_lookup;
1539 llt->llt_dump = in_lltable_dump;
1543 ii->ii_igmp = igmp_domifattach(ifp);
1549 in_domifdetach(struct ifnet *ifp, void *aux)
1551 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1553 igmp_domifdetach(ifp);
1554 lltable_free(ii->ii_llt);