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$");
36 #include "opt_mpath.h"
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 #define V_subnetsarelocal VNET(subnetsarelocal)
80 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
81 &VNET_NAME(subnetsarelocal), 0,
82 "Treat all subnets as directly connected");
83 static VNET_DEFINE(int, sameprefixcarponly);
84 #define V_sameprefixcarponly VNET(sameprefixcarponly)
85 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
86 &VNET_NAME(sameprefixcarponly), 0,
87 "Refuse to create same prefixes on different interfaces");
89 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
90 #define V_ripcbinfo VNET(ripcbinfo)
93 * Return 1 if an internet address is for a ``local'' host
94 * (one to which we have a connection). If subnetsarelocal
95 * is true, this includes other subnets of the local net.
96 * Otherwise, it includes only the directly-connected (sub)nets.
99 in_localaddr(struct in_addr in)
101 register u_long i = ntohl(in.s_addr);
102 register struct in_ifaddr *ia;
105 if (V_subnetsarelocal) {
106 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
107 if ((i & ia->ia_netmask) == ia->ia_net) {
113 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
114 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
125 * Return 1 if an internet address is for the local host and configured
126 * on one of its interfaces.
129 in_localip(struct in_addr in)
131 struct in_ifaddr *ia;
134 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
135 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
145 * Determine whether an IP address is in a reserved set of addresses
146 * that may not be forwarded, or whether datagrams to that destination
150 in_canforward(struct in_addr in)
152 register u_long i = ntohl(in.s_addr);
155 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
158 net = i & IN_CLASSA_NET;
159 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
166 * Trim a mask in a sockaddr
169 in_socktrim(struct sockaddr_in *ap)
171 register char *cplim = (char *) &ap->sin_addr;
172 register char *cp = (char *) (&ap->sin_addr + 1);
175 while (--cp >= cplim)
177 (ap)->sin_len = cp - (char *) (ap) + 1;
184 struct in_addr *mask;
190 for (x = 0; x < sizeof(*mask); x++) {
195 if (x < sizeof(*mask)) {
196 for (y = 0; y < 8; y++) {
197 if ((p[x] & (0x80 >> y)) == 0)
205 in_len2mask(struct in_addr *mask, int len)
211 bzero(mask, sizeof(*mask));
212 for (i = 0; i < len / 8; i++)
215 p[i] = (0xff00 >> (len % 8)) & 0xff;
219 * Generic internet control operations (ioctl's).
221 * ifp is NULL if not an interface-specific ioctl.
225 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
228 register struct ifreq *ifr = (struct ifreq *)data;
229 register struct in_ifaddr *ia, *iap;
230 register struct ifaddr *ifa;
231 struct in_addr allhosts_addr;
233 struct in_ifinfo *ii;
234 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
235 struct sockaddr_in oldaddr;
236 int error, hostIsNew, iaIsNew, maskIsNew;
242 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
245 * Filter out ioctls we implement directly; forward the rest on to
246 * in_lifaddr_ioctl() and ifp->if_ioctl().
263 error = priv_check(td, PRIV_NET_ADDIFADDR);
269 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
273 error = priv_check(td, PRIV_NET_DELIFADDR);
279 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
284 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
287 if (ifp == NULL || ifp->if_ioctl == NULL)
289 return ((*ifp->if_ioctl)(ifp, cmd, data));
293 return (EADDRNOTAVAIL);
296 * Security checks before we get involved in any work.
305 error = priv_check(td, PRIV_NET_ADDIFADDR);
313 error = priv_check(td, PRIV_NET_DELIFADDR);
321 * Find address for this interface, if it exists.
323 * If an alias address was specified, find that one instead of the
324 * first one on the interface, if possible.
326 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
328 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
329 if (iap->ia_ifp == ifp &&
330 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
331 if (td == NULL || prison_check_ip4(td->td_ucred,
338 ifa_ref(&ia->ia_ifa);
342 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
344 if (iap->ia_addr.sin_family == AF_INET) {
346 prison_check_ip4(td->td_ucred,
347 &iap->ia_addr.sin_addr) != 0)
354 ifa_ref(&ia->ia_ifa);
364 if (ifra->ifra_addr.sin_family == AF_INET) {
365 struct in_ifaddr *oia;
368 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
369 if (ia->ia_ifp == ifp &&
370 ia->ia_addr.sin_addr.s_addr ==
371 ifra->ifra_addr.sin_addr.s_addr)
374 if (ia != NULL && ia != oia)
375 ifa_ref(&ia->ia_ifa);
376 if (oia != NULL && ia != oia)
377 ifa_free(&oia->ia_ifa);
379 if ((ifp->if_flags & IFF_POINTOPOINT)
380 && (cmd == SIOCAIFADDR)
381 && (ifra->ifra_dstaddr.sin_addr.s_addr
383 error = EDESTADDRREQ;
387 if (cmd == SIOCDIFADDR && ia == NULL) {
388 error = EADDRNOTAVAIL;
396 ia = (struct in_ifaddr *)
397 malloc(sizeof *ia, M_IFADDR, M_NOWAIT |
406 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
407 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
408 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
410 ia->ia_sockmask.sin_len = 8;
411 ia->ia_sockmask.sin_family = AF_INET;
412 if (ifp->if_flags & IFF_BROADCAST) {
413 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
414 ia->ia_broadaddr.sin_family = AF_INET;
418 ifa_ref(ifa); /* if_addrhead */
420 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
422 ifa_ref(ifa); /* in_ifaddrhead */
424 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
436 error = EADDRNOTAVAIL;
443 * Most paths in this switch return directly or via out. Only paths
444 * that remove the address break in order to hit common removal code.
448 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
452 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
456 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
460 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
464 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
468 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
472 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
476 oldaddr = ia->ia_dstaddr;
477 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
478 if (ifp->if_ioctl != NULL) {
479 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
482 ia->ia_dstaddr = oldaddr;
486 if (ia->ia_flags & IFA_ROUTE) {
487 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
488 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
489 ia->ia_ifa.ifa_dstaddr =
490 (struct sockaddr *)&ia->ia_dstaddr;
491 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
496 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
500 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
504 error = in_ifinit(ifp, ia,
505 (struct sockaddr_in *) &ifr->ifr_addr, 1);
506 if (error != 0 && iaIsNew)
509 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
511 (ifp->if_flags & IFF_MULTICAST) != 0) {
512 error = in_joingroup(ifp, &allhosts_addr,
513 NULL, &ii->ii_allhosts);
515 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
521 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
522 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
529 if (ia->ia_addr.sin_family == AF_INET) {
530 if (ifra->ifra_addr.sin_len == 0) {
531 ifra->ifra_addr = ia->ia_addr;
533 } else if (ifra->ifra_addr.sin_addr.s_addr ==
534 ia->ia_addr.sin_addr.s_addr)
537 if (ifra->ifra_mask.sin_len) {
540 * Need to scrub the prefix here in case
541 * the issued command is SIOCAIFADDR with
542 * the same address, but with a different
543 * prefix length. And if the prefix length
544 * is the same as before, then the call is
545 * un-necessarily executed here.
548 ia->ia_sockmask = ifra->ifra_mask;
549 ia->ia_sockmask.sin_family = AF_INET;
551 ntohl(ia->ia_sockmask.sin_addr.s_addr);
554 if ((ifp->if_flags & IFF_POINTOPOINT) &&
555 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
557 ia->ia_dstaddr = ifra->ifra_dstaddr;
558 maskIsNew = 1; /* We lie; but the effect's the same */
560 if (ifra->ifra_addr.sin_family == AF_INET &&
561 (hostIsNew || maskIsNew))
562 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
563 if (error != 0 && iaIsNew)
566 if ((ifp->if_flags & IFF_BROADCAST) &&
567 (ifra->ifra_broadaddr.sin_family == AF_INET))
568 ia->ia_broadaddr = ifra->ifra_broadaddr;
570 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
572 (ifp->if_flags & IFF_MULTICAST) != 0) {
573 error = in_joingroup(ifp, &allhosts_addr,
574 NULL, &ii->ii_allhosts);
576 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
582 * in_ifscrub kills the interface route.
587 * in_ifadown gets rid of all the rest of
588 * the routes. This is not quite the right
589 * thing to do, but at least if we are running
590 * a routing process they will come back.
592 in_ifadown(&ia->ia_ifa, 1);
593 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
598 panic("in_control: unsupported ioctl");
602 /* Re-check that ia is still part of the list. */
603 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
604 if (ifa == &ia->ia_ifa)
609 * If we lost the race with another thread, there is no need to
610 * try it again for the next loop as there is no other exit
611 * path between here and out.
614 error = EADDRNOTAVAIL;
617 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
619 ifa_free(&ia->ia_ifa); /* if_addrhead */
622 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
623 if (ia->ia_addr.sin_family == AF_INET) {
624 struct in_ifaddr *if_ia;
626 LIST_REMOVE(ia, ia_hash);
629 * If this is the last IPv4 address configured on this
630 * interface, leave the all-hosts group.
631 * No state-change report need be transmitted.
634 IFP_TO_IA(ifp, if_ia);
636 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
638 if (ii->ii_allhosts) {
639 (void)in_leavegroup_locked(ii->ii_allhosts,
641 ii->ii_allhosts = NULL;
645 ifa_free(&if_ia->ia_ifa);
648 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
651 ifa_free(&ia->ia_ifa);
657 * SIOCGLIFADDR: get first address. (?!?)
658 * SIOCGLIFADDR with IFLR_PREFIX:
659 * get first address that matches the specified prefix.
660 * SIOCALIFADDR: add the specified address.
661 * SIOCALIFADDR with IFLR_PREFIX:
662 * EINVAL since we can't deduce hostid part of the address.
663 * SIOCDLIFADDR: delete the specified address.
664 * SIOCDLIFADDR with IFLR_PREFIX:
665 * delete the first address that matches the specified prefix.
667 * EINVAL on invalid parameters
668 * EADDRNOTAVAIL on prefix match failed/specified address not found
669 * other values may be returned from in_ioctl()
672 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
673 struct ifnet *ifp, struct thread *td)
675 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
679 if (data == NULL || ifp == NULL) {
680 panic("invalid argument to in_lifaddr_ioctl");
686 /* address must be specified on GET with IFLR_PREFIX */
687 if ((iflr->flags & IFLR_PREFIX) == 0)
692 /* address must be specified on ADD and DELETE */
693 if (iflr->addr.ss_family != AF_INET)
695 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
697 /* XXX need improvement */
698 if (iflr->dstaddr.ss_family
699 && iflr->dstaddr.ss_family != AF_INET)
701 if (iflr->dstaddr.ss_family
702 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
705 default: /*shouldn't happen*/
708 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
714 struct in_aliasreq ifra;
716 if (iflr->flags & IFLR_PREFIX)
719 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
720 bzero(&ifra, sizeof(ifra));
721 bcopy(iflr->iflr_name, ifra.ifra_name,
722 sizeof(ifra.ifra_name));
724 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
726 if (iflr->dstaddr.ss_family) { /*XXX*/
727 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
728 iflr->dstaddr.ss_len);
731 ifra.ifra_mask.sin_family = AF_INET;
732 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
733 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
735 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
740 struct in_ifaddr *ia;
741 struct in_addr mask, candidate, match;
742 struct sockaddr_in *sin;
744 bzero(&mask, sizeof(mask));
745 bzero(&match, sizeof(match));
746 if (iflr->flags & IFLR_PREFIX) {
747 /* lookup a prefix rather than address. */
748 in_len2mask(&mask, iflr->prefixlen);
750 sin = (struct sockaddr_in *)&iflr->addr;
751 match.s_addr = sin->sin_addr.s_addr;
752 match.s_addr &= mask.s_addr;
754 /* if you set extra bits, that's wrong */
755 if (match.s_addr != sin->sin_addr.s_addr)
759 /* on getting an address, take the 1st match */
760 /* on deleting an address, do exact match */
761 if (cmd != SIOCGLIFADDR) {
762 in_len2mask(&mask, 32);
763 sin = (struct sockaddr_in *)&iflr->addr;
764 match.s_addr = sin->sin_addr.s_addr;
768 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
769 if (ifa->ifa_addr->sa_family != AF_INET6)
771 if (match.s_addr == 0)
773 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
774 candidate.s_addr &= mask.s_addr;
775 if (candidate.s_addr == match.s_addr)
779 return (EADDRNOTAVAIL);
780 ia = (struct in_ifaddr *)ifa;
782 if (cmd == SIOCGLIFADDR) {
783 /* fill in the if_laddrreq structure */
784 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
786 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
787 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
788 ia->ia_dstaddr.sin_len);
790 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
793 in_mask2len(&ia->ia_sockmask.sin_addr);
795 iflr->flags = 0; /*XXX*/
799 struct in_aliasreq ifra;
801 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
802 bzero(&ifra, sizeof(ifra));
803 bcopy(iflr->iflr_name, ifra.ifra_name,
804 sizeof(ifra.ifra_name));
806 bcopy(&ia->ia_addr, &ifra.ifra_addr,
807 ia->ia_addr.sin_len);
808 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
809 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
810 ia->ia_dstaddr.sin_len);
812 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
813 ia->ia_sockmask.sin_len);
815 return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
821 return (EOPNOTSUPP); /*just for safety*/
825 * Delete any existing route for an interface.
828 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
835 * Initialize an interface's internet address
836 * and routing table entry.
839 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
842 register u_long i = ntohl(sin->sin_addr.s_addr);
843 struct sockaddr_in oldaddr;
844 int s = splimp(), flags = RTF_UP, error = 0;
846 oldaddr = ia->ia_addr;
847 if (oldaddr.sin_family == AF_INET)
848 LIST_REMOVE(ia, ia_hash);
850 if (ia->ia_addr.sin_family == AF_INET) {
852 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
857 * Give the interface a chance to initialize
858 * if this is its first address,
859 * and to validate the address if necessary.
861 if (ifp->if_ioctl != NULL) {
862 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
865 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
866 ia->ia_addr = oldaddr;
868 if (ia->ia_addr.sin_family == AF_INET)
869 LIST_INSERT_HEAD(INADDR_HASH(
870 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
873 * If oldaddr family is not AF_INET (e.g.
874 * interface has been just created) in_control
875 * does not call LIST_REMOVE, and we end up
876 * with bogus ia entries in hash
878 LIST_REMOVE(ia, ia_hash);
885 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
887 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
890 ia->ia_netmask = IN_CLASSA_NET;
891 else if (IN_CLASSB(i))
892 ia->ia_netmask = IN_CLASSB_NET;
894 ia->ia_netmask = IN_CLASSC_NET;
896 * The subnet mask usually includes at least the standard network part,
897 * but may may be smaller in the case of supernetting.
898 * If it is set, we believe it.
900 if (ia->ia_subnetmask == 0) {
901 ia->ia_subnetmask = ia->ia_netmask;
902 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
904 ia->ia_netmask &= ia->ia_subnetmask;
905 ia->ia_net = i & ia->ia_netmask;
906 ia->ia_subnet = i & ia->ia_subnetmask;
907 in_socktrim(&ia->ia_sockmask);
909 * XXX: carp(4) does not have interface route
911 if (ifp->if_type == IFT_CARP)
914 * Add route for the network.
916 ia->ia_ifa.ifa_metric = ifp->if_metric;
917 if (ifp->if_flags & IFF_BROADCAST) {
918 ia->ia_broadaddr.sin_addr.s_addr =
919 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
920 ia->ia_netbroadcast.s_addr =
921 htonl(ia->ia_net | ~ ia->ia_netmask);
922 } else if (ifp->if_flags & IFF_LOOPBACK) {
923 ia->ia_dstaddr = ia->ia_addr;
925 } else if (ifp->if_flags & IFF_POINTOPOINT) {
926 if (ia->ia_dstaddr.sin_family != AF_INET)
930 if ((error = in_addprefix(ia, flags)) != 0)
933 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
936 if (ifp->if_flags & IFF_POINTOPOINT) {
937 if (ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
943 * add a loopback route to self
945 if (V_useloopback && !(ifp->if_flags & IFF_LOOPBACK)) {
948 bzero(&ia_ro, sizeof(ia_ro));
949 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr;
950 rtalloc_ign_fib(&ia_ro, 0, 0);
951 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
952 (ia_ro.ro_rt->rt_ifp == V_loif)) {
953 RT_LOCK(ia_ro.ro_rt);
954 RT_ADDREF(ia_ro.ro_rt);
955 RTFREE_LOCKED(ia_ro.ro_rt);
957 error = ifa_add_loopback_route((struct ifaddr *)ia,
958 (struct sockaddr *)&ia->ia_addr);
960 ia->ia_flags |= IFA_RTSELF;
961 if (ia_ro.ro_rt != NULL)
968 #define rtinitflags(x) \
969 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
973 * Generate a routing message when inserting or deleting
974 * an interface address alias.
976 static void in_addralias_rtmsg(int cmd, struct in_addr *prefix,
977 struct in_ifaddr *target)
980 struct sockaddr_in *pfx_addr;
981 struct rtentry msg_rt;
984 * This is a bit questionable because there is no
985 * additional route entry added/deleted for an address
986 * alias. Therefore this route report is inaccurate.
988 bzero(&pfx_ro, sizeof(pfx_ro));
989 pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst);
990 pfx_addr->sin_len = sizeof(*pfx_addr);
991 pfx_addr->sin_family = AF_INET;
992 pfx_addr->sin_addr = *prefix;
993 rtalloc_ign_fib(&pfx_ro, 0, 0);
994 if (pfx_ro.ro_rt != NULL) {
995 msg_rt = *pfx_ro.ro_rt;
998 * Point the gateway to the new interface
999 * address as if a new prefix route entry has
1000 * been added through the new address alias.
1001 * All other parts of the rtentry is accurate,
1002 * e.g., rt_key, rt_mask, rt_ifp etc.
1005 (struct sockaddr *)&target->ia_addr;
1007 (struct ifaddr *)target,
1009 RTFREE(pfx_ro.ro_rt);
1015 * Check if we have a route for the given prefix already or add one accordingly.
1018 in_addprefix(struct in_ifaddr *target, int flags)
1020 struct in_ifaddr *ia;
1021 struct in_addr prefix, mask, p, m;
1024 if ((flags & RTF_HOST) != 0) {
1025 prefix = target->ia_dstaddr.sin_addr;
1028 prefix = target->ia_addr.sin_addr;
1029 mask = target->ia_sockmask.sin_addr;
1030 prefix.s_addr &= mask.s_addr;
1034 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1035 if (rtinitflags(ia)) {
1036 p = ia->ia_addr.sin_addr;
1038 if (prefix.s_addr != p.s_addr)
1041 p = ia->ia_addr.sin_addr;
1042 m = ia->ia_sockmask.sin_addr;
1043 p.s_addr &= m.s_addr;
1045 if (prefix.s_addr != p.s_addr ||
1046 mask.s_addr != m.s_addr)
1051 * If we got a matching prefix route inserted by other
1052 * interface address, we are done here.
1054 if (ia->ia_flags & IFA_ROUTE) {
1056 if (ia->ia_addr.sin_addr.s_addr ==
1057 target->ia_addr.sin_addr.s_addr) {
1058 IN_IFADDR_RUNLOCK();
1063 if (V_sameprefixcarponly &&
1064 target->ia_ifp->if_type != IFT_CARP &&
1065 ia->ia_ifp->if_type != IFT_CARP) {
1066 IN_IFADDR_RUNLOCK();
1069 in_addralias_rtmsg(RTM_ADD, &prefix, target);
1070 IN_IFADDR_RUNLOCK();
1075 IN_IFADDR_RUNLOCK();
1078 * No-one seem to have this prefix route, so we try to insert it.
1080 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
1082 target->ia_flags |= IFA_ROUTE;
1086 extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
1089 * If there is no other address in the system that can serve a route to the
1090 * same prefix, remove the route. Hand over the route to the new address
1094 in_scrubprefix(struct in_ifaddr *target)
1096 struct in_ifaddr *ia;
1097 struct in_addr prefix, mask, p;
1099 struct sockaddr_in prefix0, mask0;
1102 * Remove the loopback route to the interface address.
1103 * The "useloopback" setting is not consulted because if the
1104 * user configures an interface address, turns off this
1105 * setting, and then tries to delete that interface address,
1106 * checking the current setting of "useloopback" would leave
1107 * that interface address loopback route untouched, which
1108 * would be wrong. Therefore the interface address loopback route
1109 * deletion is unconditional.
1111 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
1112 !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
1113 (target->ia_flags & IFA_RTSELF)) {
1117 bzero(&ia_ro, sizeof(ia_ro));
1118 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr;
1119 rtalloc_ign_fib(&ia_ro, 0, 0);
1120 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
1121 (ia_ro.ro_rt->rt_ifp == V_loif)) {
1122 RT_LOCK(ia_ro.ro_rt);
1123 if (ia_ro.ro_rt->rt_refcnt <= 1)
1126 RT_REMREF(ia_ro.ro_rt);
1127 RTFREE_LOCKED(ia_ro.ro_rt);
1130 error = ifa_del_loopback_route((struct ifaddr *)target,
1131 (struct sockaddr *)&target->ia_addr);
1133 target->ia_flags &= ~IFA_RTSELF;
1134 /* remove arp cache */
1135 arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
1138 if (rtinitflags(target))
1139 prefix = target->ia_dstaddr.sin_addr;
1141 prefix = target->ia_addr.sin_addr;
1142 mask = target->ia_sockmask.sin_addr;
1143 prefix.s_addr &= mask.s_addr;
1146 if ((target->ia_flags & IFA_ROUTE) == 0) {
1147 in_addralias_rtmsg(RTM_DELETE, &prefix, target);
1152 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1153 if (rtinitflags(ia))
1154 p = ia->ia_dstaddr.sin_addr;
1156 p = ia->ia_addr.sin_addr;
1157 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1160 if (prefix.s_addr != p.s_addr)
1164 * If we got a matching prefix address, move IFA_ROUTE and
1165 * the route itself to it. Make sure that routing daemons
1168 * XXX: a special case for carp(4) interface - this should
1169 * be more generally specified as an interface that
1170 * doesn't support such action.
1172 if ((ia->ia_flags & IFA_ROUTE) == 0
1173 && (ia->ia_ifp->if_type != IFT_CARP)
1175 IN_IFADDR_RUNLOCK();
1176 rtinit(&(target->ia_ifa), (int)RTM_DELETE,
1177 rtinitflags(target));
1178 target->ia_flags &= ~IFA_ROUTE;
1180 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
1181 rtinitflags(ia) | RTF_UP);
1183 ia->ia_flags |= IFA_ROUTE;
1187 IN_IFADDR_RUNLOCK();
1190 * remove all L2 entries on the given prefix
1192 bzero(&prefix0, sizeof(prefix0));
1193 prefix0.sin_len = sizeof(prefix0);
1194 prefix0.sin_family = AF_INET;
1195 prefix0.sin_addr.s_addr = target->ia_subnet;
1196 bzero(&mask0, sizeof(mask0));
1197 mask0.sin_len = sizeof(mask0);
1198 mask0.sin_family = AF_INET;
1199 mask0.sin_addr.s_addr = target->ia_subnetmask;
1200 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
1201 (struct sockaddr *)&mask0);
1204 * As no-one seem to have this prefix, we can remove the route.
1206 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1207 target->ia_flags &= ~IFA_ROUTE;
1214 * Return 1 if the address might be a local broadcast address.
1217 in_broadcast(struct in_addr in, struct ifnet *ifp)
1219 register struct ifaddr *ifa;
1222 if (in.s_addr == INADDR_BROADCAST ||
1223 in.s_addr == INADDR_ANY)
1225 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1227 t = ntohl(in.s_addr);
1229 * Look through the list of addresses for a match
1230 * with a broadcast address.
1232 #define ia ((struct in_ifaddr *)ifa)
1233 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1234 if (ifa->ifa_addr->sa_family == AF_INET &&
1235 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1236 in.s_addr == ia->ia_netbroadcast.s_addr ||
1238 * Check for old-style (host 0) broadcast.
1240 t == ia->ia_subnet || t == ia->ia_net) &&
1242 * Check for an all one subnetmask. These
1243 * only exist when an interface gets a secondary
1246 ia->ia_subnetmask != (u_long)0xffffffff)
1253 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1256 in_ifdetach(struct ifnet *ifp)
1259 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1260 in_pcbpurgeif0(&V_udbinfo, ifp);
1261 in_purgemaddrs(ifp);
1265 * Delete all IPv4 multicast address records, and associated link-layer
1266 * multicast address records, associated with ifp.
1267 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1268 * XXX This should not race with ifma_protospec being set during
1269 * a new allocation, if it does, we have bigger problems.
1272 in_purgemaddrs(struct ifnet *ifp)
1274 LIST_HEAD(,in_multi) purgeinms;
1275 struct in_multi *inm, *tinm;
1276 struct ifmultiaddr *ifma;
1278 LIST_INIT(&purgeinms);
1282 * Extract list of in_multi associated with the detaching ifp
1283 * which the PF_INET layer is about to release.
1284 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1285 * by code further down.
1288 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1289 if (ifma->ifma_addr->sa_family != AF_INET ||
1290 ifma->ifma_protospec == NULL)
1293 KASSERT(ifma->ifma_protospec != NULL,
1294 ("%s: ifma_protospec is NULL", __func__));
1296 inm = (struct in_multi *)ifma->ifma_protospec;
1297 LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1299 IF_ADDR_UNLOCK(ifp);
1301 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1302 LIST_REMOVE(inm, inm_link);
1303 inm_release_locked(inm);
1310 #include <net/if_dl.h>
1311 #include <netinet/if_ether.h>
1314 struct llentry base;
1315 struct sockaddr_in l3_addr4;
1318 static struct llentry *
1319 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
1321 struct in_llentry *lle;
1323 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
1324 if (lle == NULL) /* NB: caller generates msg */
1327 callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
1329 * For IPv4 this will trigger "arpresolve" to generate
1332 lle->base.la_expire = time_second; /* mark expired */
1333 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
1334 lle->base.lle_refcnt = 1;
1335 LLE_LOCK_INIT(&lle->base);
1340 * Deletes an address from the address table.
1341 * This function is called by the timer functions
1342 * such as arptimer() and nd6_llinfo_timer(), and
1343 * the caller does the locking.
1346 in_lltable_free(struct lltable *llt, struct llentry *lle)
1349 LLE_LOCK_DESTROY(lle);
1350 free(lle, M_LLTABLE);
1354 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1355 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1358 in_lltable_prefix_free(struct lltable *llt,
1359 const struct sockaddr *prefix,
1360 const struct sockaddr *mask)
1362 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1363 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1364 struct llentry *lle, *next;
1367 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
1368 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1370 if (IN_ARE_MASKED_ADDR_EQUAL((struct sockaddr_in *)L3_ADDR(lle),
1374 canceled = callout_drain(&lle->la_timer);
1386 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1390 KASSERT(l3addr->sa_family == AF_INET,
1391 ("sin_family %d", l3addr->sa_family));
1393 /* XXX rtalloc1 should take a const param */
1394 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1395 if (rt == NULL || (!(flags & LLE_PUB) &&
1396 ((rt->rt_flags & RTF_GATEWAY) ||
1397 (rt->rt_ifp != ifp)))) {
1399 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1400 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1411 * Return NULL if not found or marked for deletion.
1412 * If found return lle read locked.
1414 static struct llentry *
1415 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1417 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1418 struct ifnet *ifp = llt->llt_ifp;
1419 struct llentry *lle;
1420 struct llentries *lleh;
1423 IF_AFDATA_LOCK_ASSERT(ifp);
1424 KASSERT(l3addr->sa_family == AF_INET,
1425 ("sin_family %d", l3addr->sa_family));
1427 hashkey = sin->sin_addr.s_addr;
1428 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1429 LIST_FOREACH(lle, lleh, lle_next) {
1430 struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle);
1431 if (lle->la_flags & LLE_DELETED)
1433 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1438 if (flags & LLE_DELETE)
1439 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle);
1441 if (!(flags & LLE_CREATE))
1444 * A route that covers the given address must have
1445 * been installed 1st because we are doing a resolution,
1448 if (!(flags & LLE_IFADDR) &&
1449 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1452 lle = in_lltable_new(l3addr, flags);
1454 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1457 lle->la_flags = flags & ~LLE_CREATE;
1458 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1459 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1460 lle->la_flags |= (LLE_VALID | LLE_STATIC);
1464 lle->lle_head = lleh;
1465 LIST_INSERT_HEAD(lleh, lle, lle_next);
1466 } else if (flags & LLE_DELETE) {
1467 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1469 lle->la_flags = LLE_DELETED;
1472 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1478 if (LLE_IS_VALID(lle)) {
1479 if (flags & LLE_EXCLUSIVE)
1489 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1491 #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle))
1492 struct ifnet *ifp = llt->llt_ifp;
1493 struct llentry *lle;
1496 struct rt_msghdr rtm;
1497 struct sockaddr_inarp sin;
1498 struct sockaddr_dl sdl;
1502 LLTABLE_LOCK_ASSERT();
1505 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1506 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1507 struct sockaddr_dl *sdl;
1509 /* skip deleted entries */
1510 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1512 /* Skip if jailed and not a valid IP of the prison. */
1513 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1516 * produce a msg made of:
1518 * struct sockaddr_inarp; (IPv4)
1519 * struct sockaddr_dl;
1521 bzero(&arpc, sizeof(arpc));
1522 arpc.rtm.rtm_msglen = sizeof(arpc);
1523 arpc.rtm.rtm_version = RTM_VERSION;
1524 arpc.rtm.rtm_type = RTM_GET;
1525 arpc.rtm.rtm_flags = RTF_UP;
1526 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1527 arpc.sin.sin_family = AF_INET;
1528 arpc.sin.sin_len = sizeof(arpc.sin);
1529 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1532 if (lle->la_flags & LLE_PUB) {
1533 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1535 if (lle->la_flags & LLE_PROXY)
1536 arpc.sin.sin_other = SIN_PROXY;
1540 sdl->sdl_family = AF_LINK;
1541 sdl->sdl_len = sizeof(*sdl);
1542 sdl->sdl_index = ifp->if_index;
1543 sdl->sdl_type = ifp->if_type;
1544 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1545 sdl->sdl_alen = ifp->if_addrlen;
1546 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1549 bzero(LLADDR(sdl), ifp->if_addrlen);
1552 arpc.rtm.rtm_rmx.rmx_expire =
1553 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1554 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1555 if (lle->la_flags & LLE_STATIC)
1556 arpc.rtm.rtm_flags |= RTF_STATIC;
1557 arpc.rtm.rtm_index = ifp->if_index;
1558 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1568 in_domifattach(struct ifnet *ifp)
1570 struct in_ifinfo *ii;
1571 struct lltable *llt;
1573 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1575 llt = lltable_init(ifp, AF_INET);
1577 llt->llt_new = in_lltable_new;
1578 llt->llt_free = in_lltable_free;
1579 llt->llt_prefix_free = in_lltable_prefix_free;
1580 llt->llt_rtcheck = in_lltable_rtcheck;
1581 llt->llt_lookup = in_lltable_lookup;
1582 llt->llt_dump = in_lltable_dump;
1586 ii->ii_igmp = igmp_domifattach(ifp);
1592 in_domifdetach(struct ifnet *ifp, void *aux)
1594 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1596 igmp_domifdetach(ifp);
1597 lltable_free(ii->ii_llt);