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_arp.h>
53 #include <net/if_dl.h>
54 #include <net/if_llatbl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/in_pcb.h>
62 #include <netinet/ip_var.h>
63 #include <netinet/igmp_var.h>
64 #include <netinet/udp.h>
65 #include <netinet/udp_var.h>
67 static int in_mask2len(struct in_addr *);
68 static void in_len2mask(struct in_addr *, int);
69 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
70 struct ifnet *, struct thread *);
72 static int in_addprefix(struct in_ifaddr *, int);
73 static int in_scrubprefix(struct in_ifaddr *, u_int);
74 static void in_socktrim(struct sockaddr_in *);
75 static int in_ifinit(struct ifnet *,
76 struct in_ifaddr *, struct sockaddr_in *, int);
77 static void in_purgemaddrs(struct ifnet *);
79 static VNET_DEFINE(int, sameprefixcarponly);
80 #define V_sameprefixcarponly VNET(sameprefixcarponly)
81 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
82 &VNET_NAME(sameprefixcarponly), 0,
83 "Refuse to create same prefixes on different interfaces");
85 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
86 #define V_ripcbinfo VNET(ripcbinfo)
88 VNET_DECLARE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */
89 #define V_arpstat VNET(arpstat)
92 * Return 1 if an internet address is for a ``local'' host
93 * (one to which we have a connection).
96 in_localaddr(struct in_addr in)
98 register u_long i = ntohl(in.s_addr);
99 register struct in_ifaddr *ia;
102 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
103 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
113 * Return 1 if an internet address is for the local host and configured
114 * on one of its interfaces.
117 in_localip(struct in_addr in)
119 struct in_ifaddr *ia;
122 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
123 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
133 * Determine whether an IP address is in a reserved set of addresses
134 * that may not be forwarded, or whether datagrams to that destination
138 in_canforward(struct in_addr in)
140 register u_long i = ntohl(in.s_addr);
143 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
146 net = i & IN_CLASSA_NET;
147 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
154 * Trim a mask in a sockaddr
157 in_socktrim(struct sockaddr_in *ap)
159 register char *cplim = (char *) &ap->sin_addr;
160 register char *cp = (char *) (&ap->sin_addr + 1);
163 while (--cp >= cplim)
165 (ap)->sin_len = cp - (char *) (ap) + 1;
172 struct in_addr *mask;
178 for (x = 0; x < sizeof(*mask); x++) {
183 if (x < sizeof(*mask)) {
184 for (y = 0; y < 8; y++) {
185 if ((p[x] & (0x80 >> y)) == 0)
193 in_len2mask(struct in_addr *mask, int len)
199 bzero(mask, sizeof(*mask));
200 for (i = 0; i < len / 8; i++)
203 p[i] = (0xff00 >> (len % 8)) & 0xff;
207 * Generic internet control operations (ioctl's).
209 * ifp is NULL if not an interface-specific ioctl.
213 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
216 register struct ifreq *ifr = (struct ifreq *)data;
217 register struct in_ifaddr *ia, *iap;
218 register struct ifaddr *ifa;
219 struct in_addr allhosts_addr;
221 struct in_ifinfo *ii;
222 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
223 struct sockaddr_in oldaddr;
224 int error, hostIsNew, iaIsNew, maskIsNew;
230 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
233 * Filter out ioctls we implement directly; forward the rest on to
234 * in_lifaddr_ioctl() and ifp->if_ioctl().
251 error = priv_check(td, PRIV_NET_ADDIFADDR);
257 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
261 error = priv_check(td, PRIV_NET_DELIFADDR);
267 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
272 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
275 if (ifp == NULL || ifp->if_ioctl == NULL)
277 return ((*ifp->if_ioctl)(ifp, cmd, data));
281 return (EADDRNOTAVAIL);
284 * Security checks before we get involved in any work.
293 error = priv_check(td, PRIV_NET_ADDIFADDR);
301 error = priv_check(td, PRIV_NET_DELIFADDR);
309 * Find address for this interface, if it exists.
311 * If an alias address was specified, find that one instead of the
312 * first one on the interface, if possible.
314 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
316 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
317 if (iap->ia_ifp == ifp &&
318 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
319 if (td == NULL || prison_check_ip4(td->td_ucred,
326 ifa_ref(&ia->ia_ifa);
330 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
332 if (iap->ia_addr.sin_family == AF_INET) {
334 prison_check_ip4(td->td_ucred,
335 &iap->ia_addr.sin_addr) != 0)
342 ifa_ref(&ia->ia_ifa);
343 IF_ADDR_RUNLOCK(ifp);
352 if (ifra->ifra_addr.sin_family == AF_INET) {
353 struct in_ifaddr *oia;
356 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
357 if (ia->ia_ifp == ifp &&
358 ia->ia_addr.sin_addr.s_addr ==
359 ifra->ifra_addr.sin_addr.s_addr)
362 if (ia != NULL && ia != oia)
363 ifa_ref(&ia->ia_ifa);
364 if (oia != NULL && ia != oia)
365 ifa_free(&oia->ia_ifa);
367 if ((ifp->if_flags & IFF_POINTOPOINT)
368 && (cmd == SIOCAIFADDR)
369 && (ifra->ifra_dstaddr.sin_addr.s_addr
371 error = EDESTADDRREQ;
375 if (cmd == SIOCDIFADDR && ia == NULL) {
376 error = EADDRNOTAVAIL;
384 ia = (struct in_ifaddr *)
385 malloc(sizeof *ia, M_IFADDR, M_NOWAIT |
394 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
395 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
396 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
398 ia->ia_sockmask.sin_len = 8;
399 ia->ia_sockmask.sin_family = AF_INET;
400 if (ifp->if_flags & IFF_BROADCAST) {
401 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
402 ia->ia_broadaddr.sin_family = AF_INET;
406 ifa_ref(ifa); /* if_addrhead */
408 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
409 IF_ADDR_WUNLOCK(ifp);
410 ifa_ref(ifa); /* in_ifaddrhead */
412 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
424 error = EADDRNOTAVAIL;
431 * Most paths in this switch return directly or via out. Only paths
432 * that remove the address break in order to hit common removal code.
436 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
440 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
444 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
448 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
452 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
456 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
460 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
464 oldaddr = ia->ia_dstaddr;
465 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
466 if (ifp->if_ioctl != NULL) {
467 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
470 ia->ia_dstaddr = oldaddr;
474 if (ia->ia_flags & IFA_ROUTE) {
475 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
476 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
477 ia->ia_ifa.ifa_dstaddr =
478 (struct sockaddr *)&ia->ia_dstaddr;
479 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
484 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
488 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
492 error = in_ifinit(ifp, ia,
493 (struct sockaddr_in *) &ifr->ifr_addr, 1);
494 if (error != 0 && iaIsNew)
497 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
499 (ifp->if_flags & IFF_MULTICAST) != 0) {
500 error = in_joingroup(ifp, &allhosts_addr,
501 NULL, &ii->ii_allhosts);
503 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
509 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
510 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
517 if (ia->ia_addr.sin_family == AF_INET) {
518 if (ifra->ifra_addr.sin_len == 0) {
519 ifra->ifra_addr = ia->ia_addr;
521 } else if (ifra->ifra_addr.sin_addr.s_addr ==
522 ia->ia_addr.sin_addr.s_addr)
525 if (ifra->ifra_mask.sin_len) {
528 * Need to scrub the prefix here in case
529 * the issued command is SIOCAIFADDR with
530 * the same address, but with a different
531 * prefix length. And if the prefix length
532 * is the same as before, then the call is
533 * un-necessarily executed here.
535 in_ifscrub(ifp, ia, LLE_STATIC);
536 ia->ia_sockmask = ifra->ifra_mask;
537 ia->ia_sockmask.sin_family = AF_INET;
539 ntohl(ia->ia_sockmask.sin_addr.s_addr);
542 if ((ifp->if_flags & IFF_POINTOPOINT) &&
543 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
544 in_ifscrub(ifp, ia, LLE_STATIC);
545 ia->ia_dstaddr = ifra->ifra_dstaddr;
546 maskIsNew = 1; /* We lie; but the effect's the same */
548 if (ifra->ifra_addr.sin_family == AF_INET &&
549 (hostIsNew || maskIsNew))
550 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
551 if (error != 0 && iaIsNew)
554 if ((ifp->if_flags & IFF_BROADCAST) &&
555 (ifra->ifra_broadaddr.sin_family == AF_INET))
556 ia->ia_broadaddr = ifra->ifra_broadaddr;
558 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
560 (ifp->if_flags & IFF_MULTICAST) != 0) {
561 error = in_joingroup(ifp, &allhosts_addr,
562 NULL, &ii->ii_allhosts);
564 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
570 * in_ifscrub kills the interface route.
572 in_ifscrub(ifp, ia, LLE_STATIC);
575 * in_ifadown gets rid of all the rest of
576 * the routes. This is not quite the right
577 * thing to do, but at least if we are running
578 * a routing process they will come back.
580 in_ifadown(&ia->ia_ifa, 1);
581 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
586 panic("in_control: unsupported ioctl");
590 /* Re-check that ia is still part of the list. */
591 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
592 if (ifa == &ia->ia_ifa)
597 * If we lost the race with another thread, there is no need to
598 * try it again for the next loop as there is no other exit
599 * path between here and out.
601 IF_ADDR_WUNLOCK(ifp);
602 error = EADDRNOTAVAIL;
605 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
606 IF_ADDR_WUNLOCK(ifp);
607 ifa_free(&ia->ia_ifa); /* if_addrhead */
610 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
611 if (ia->ia_addr.sin_family == AF_INET) {
612 struct in_ifaddr *if_ia;
614 LIST_REMOVE(ia, ia_hash);
617 * If this is the last IPv4 address configured on this
618 * interface, leave the all-hosts group.
619 * No state-change report need be transmitted.
622 IFP_TO_IA(ifp, if_ia);
624 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
626 if (ii->ii_allhosts) {
627 (void)in_leavegroup_locked(ii->ii_allhosts,
629 ii->ii_allhosts = NULL;
633 ifa_free(&if_ia->ia_ifa);
636 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
639 ifa_free(&ia->ia_ifa);
645 * SIOCGLIFADDR: get first address. (?!?)
646 * SIOCGLIFADDR with IFLR_PREFIX:
647 * get first address that matches the specified prefix.
648 * SIOCALIFADDR: add the specified address.
649 * SIOCALIFADDR with IFLR_PREFIX:
650 * EINVAL since we can't deduce hostid part of the address.
651 * SIOCDLIFADDR: delete the specified address.
652 * SIOCDLIFADDR with IFLR_PREFIX:
653 * delete the first address that matches the specified prefix.
655 * EINVAL on invalid parameters
656 * EADDRNOTAVAIL on prefix match failed/specified address not found
657 * other values may be returned from in_ioctl()
660 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
661 struct ifnet *ifp, struct thread *td)
663 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
667 if (data == NULL || ifp == NULL) {
668 panic("invalid argument to in_lifaddr_ioctl");
674 /* address must be specified on GET with IFLR_PREFIX */
675 if ((iflr->flags & IFLR_PREFIX) == 0)
680 /* address must be specified on ADD and DELETE */
681 if (iflr->addr.ss_family != AF_INET)
683 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
685 /* XXX need improvement */
686 if (iflr->dstaddr.ss_family
687 && iflr->dstaddr.ss_family != AF_INET)
689 if (iflr->dstaddr.ss_family
690 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
693 default: /*shouldn't happen*/
696 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
702 struct in_aliasreq ifra;
704 if (iflr->flags & IFLR_PREFIX)
707 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
708 bzero(&ifra, sizeof(ifra));
709 bcopy(iflr->iflr_name, ifra.ifra_name,
710 sizeof(ifra.ifra_name));
712 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
714 if (iflr->dstaddr.ss_family) { /*XXX*/
715 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
716 iflr->dstaddr.ss_len);
719 ifra.ifra_mask.sin_family = AF_INET;
720 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
721 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
723 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
728 struct in_ifaddr *ia;
729 struct in_addr mask, candidate, match;
730 struct sockaddr_in *sin;
732 bzero(&mask, sizeof(mask));
733 bzero(&match, sizeof(match));
734 if (iflr->flags & IFLR_PREFIX) {
735 /* lookup a prefix rather than address. */
736 in_len2mask(&mask, iflr->prefixlen);
738 sin = (struct sockaddr_in *)&iflr->addr;
739 match.s_addr = sin->sin_addr.s_addr;
740 match.s_addr &= mask.s_addr;
742 /* if you set extra bits, that's wrong */
743 if (match.s_addr != sin->sin_addr.s_addr)
747 /* on getting an address, take the 1st match */
748 /* on deleting an address, do exact match */
749 if (cmd != SIOCGLIFADDR) {
750 in_len2mask(&mask, 32);
751 sin = (struct sockaddr_in *)&iflr->addr;
752 match.s_addr = sin->sin_addr.s_addr;
757 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
758 if (ifa->ifa_addr->sa_family != AF_INET)
760 if (match.s_addr == 0)
762 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
763 candidate.s_addr &= mask.s_addr;
764 if (candidate.s_addr == match.s_addr)
769 IF_ADDR_RUNLOCK(ifp);
771 return (EADDRNOTAVAIL);
772 ia = (struct in_ifaddr *)ifa;
774 if (cmd == SIOCGLIFADDR) {
775 /* fill in the if_laddrreq structure */
776 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
778 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
779 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
780 ia->ia_dstaddr.sin_len);
782 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
785 in_mask2len(&ia->ia_sockmask.sin_addr);
787 iflr->flags = 0; /*XXX*/
792 struct in_aliasreq ifra;
794 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
795 bzero(&ifra, sizeof(ifra));
796 bcopy(iflr->iflr_name, ifra.ifra_name,
797 sizeof(ifra.ifra_name));
799 bcopy(&ia->ia_addr, &ifra.ifra_addr,
800 ia->ia_addr.sin_len);
801 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
802 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
803 ia->ia_dstaddr.sin_len);
805 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
806 ia->ia_sockmask.sin_len);
809 return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
815 return (EOPNOTSUPP); /*just for safety*/
819 * Delete any existing route for an interface.
822 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia, u_int flags)
825 in_scrubprefix(ia, flags);
829 * Initialize an interface's internet address
830 * and routing table entry.
833 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
836 register u_long i = ntohl(sin->sin_addr.s_addr);
837 struct sockaddr_in oldaddr;
838 int s = splimp(), flags = RTF_UP, error = 0;
840 oldaddr = ia->ia_addr;
841 if (oldaddr.sin_family == AF_INET)
842 LIST_REMOVE(ia, ia_hash);
844 if (ia->ia_addr.sin_family == AF_INET) {
846 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
851 * Give the interface a chance to initialize
852 * if this is its first address,
853 * and to validate the address if necessary.
855 if (ifp->if_ioctl != NULL) {
856 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
859 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
860 ia->ia_addr = oldaddr;
862 if (ia->ia_addr.sin_family == AF_INET)
863 LIST_INSERT_HEAD(INADDR_HASH(
864 ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
867 * If oldaddr family is not AF_INET (e.g.
868 * interface has been just created) in_control
869 * does not call LIST_REMOVE, and we end up
870 * with bogus ia entries in hash
872 LIST_REMOVE(ia, ia_hash);
879 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
880 in_ifscrub(ifp, ia, LLE_STATIC);
881 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
884 * Be compatible with network classes, if netmask isn't supplied,
885 * guess it based on classes.
887 if (ia->ia_subnetmask == 0) {
889 ia->ia_subnetmask = IN_CLASSA_NET;
890 else if (IN_CLASSB(i))
891 ia->ia_subnetmask = IN_CLASSB_NET;
893 ia->ia_subnetmask = IN_CLASSC_NET;
894 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
896 ia->ia_subnet = i & ia->ia_subnetmask;
897 in_socktrim(&ia->ia_sockmask);
899 * XXX: carp(4) does not have interface route
901 if (ifp->if_type == IFT_CARP)
904 * Add route for the network.
906 ia->ia_ifa.ifa_metric = ifp->if_metric;
907 if (ifp->if_flags & IFF_BROADCAST) {
908 if (ia->ia_subnetmask == IN_RFC3021_MASK)
909 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
911 ia->ia_broadaddr.sin_addr.s_addr =
912 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
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)
927 if (ifp->if_flags & IFF_POINTOPOINT) {
928 if (ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
934 * add a loopback route to self
936 if (V_useloopback && !(ifp->if_flags & IFF_LOOPBACK)) {
939 bzero(&ia_ro, sizeof(ia_ro));
940 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr;
941 rtalloc_ign_fib(&ia_ro, 0, RT_DEFAULT_FIB);
942 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
943 (ia_ro.ro_rt->rt_ifp == V_loif)) {
944 RT_LOCK(ia_ro.ro_rt);
945 RT_ADDREF(ia_ro.ro_rt);
946 RTFREE_LOCKED(ia_ro.ro_rt);
948 error = ifa_add_loopback_route((struct ifaddr *)ia,
949 (struct sockaddr *)&ia->ia_addr);
951 ia->ia_flags |= IFA_RTSELF;
952 if (ia_ro.ro_rt != NULL)
959 #define rtinitflags(x) \
960 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
964 * Check if we have a route for the given prefix already or add one accordingly.
967 in_addprefix(struct in_ifaddr *target, int flags)
969 struct in_ifaddr *ia;
970 struct in_addr prefix, mask, p, m;
973 if ((flags & RTF_HOST) != 0) {
974 prefix = target->ia_dstaddr.sin_addr;
977 prefix = target->ia_addr.sin_addr;
978 mask = target->ia_sockmask.sin_addr;
979 prefix.s_addr &= mask.s_addr;
982 fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS : target->ia_ifp->if_fib;
985 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
986 if (rtinitflags(ia)) {
987 p = ia->ia_dstaddr.sin_addr;
989 if (prefix.s_addr != p.s_addr)
992 p = ia->ia_addr.sin_addr;
993 m = ia->ia_sockmask.sin_addr;
994 p.s_addr &= m.s_addr;
996 if (prefix.s_addr != p.s_addr ||
997 mask.s_addr != m.s_addr)
1002 * If we got a matching prefix route inserted by other
1003 * interface address, we are done here.
1005 if (ia->ia_flags & IFA_ROUTE) {
1007 if (ia->ia_addr.sin_addr.s_addr ==
1008 target->ia_addr.sin_addr.s_addr) {
1009 IN_IFADDR_RUNLOCK();
1014 if (V_sameprefixcarponly &&
1015 target->ia_ifp->if_type != IFT_CARP &&
1016 ia->ia_ifp->if_type != IFT_CARP) {
1017 IN_IFADDR_RUNLOCK();
1020 rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
1021 IN_IFADDR_RUNLOCK();
1026 IN_IFADDR_RUNLOCK();
1029 * No-one seem to have this prefix route, so we try to insert it.
1031 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
1033 target->ia_flags |= IFA_ROUTE;
1037 extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
1040 * If there is no other address in the system that can serve a route to the
1041 * same prefix, remove the route. Hand over the route to the new address
1045 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1047 struct in_ifaddr *ia;
1048 struct in_addr prefix, mask, p;
1049 int error = 0, fibnum;
1050 struct sockaddr_in prefix0, mask0;
1052 fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS : target->ia_ifp->if_fib;
1055 * Remove the loopback route to the interface address.
1056 * The "useloopback" setting is not consulted because if the
1057 * user configures an interface address, turns off this
1058 * setting, and then tries to delete that interface address,
1059 * checking the current setting of "useloopback" would leave
1060 * that interface address loopback route untouched, which
1061 * would be wrong. Therefore the interface address loopback route
1062 * deletion is unconditional.
1064 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
1065 !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
1066 (target->ia_flags & IFA_RTSELF)) {
1070 bzero(&ia_ro, sizeof(ia_ro));
1071 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr;
1072 rtalloc_ign_fib(&ia_ro, 0, 0);
1073 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
1074 (ia_ro.ro_rt->rt_ifp == V_loif)) {
1075 RT_LOCK(ia_ro.ro_rt);
1076 if (ia_ro.ro_rt->rt_refcnt <= 1)
1078 else if (flags & LLE_STATIC) {
1079 RT_REMREF(ia_ro.ro_rt);
1080 target->ia_flags &= ~IFA_RTSELF;
1082 RTFREE_LOCKED(ia_ro.ro_rt);
1084 if (freeit && (flags & LLE_STATIC)) {
1085 error = ifa_del_loopback_route((struct ifaddr *)target,
1086 (struct sockaddr *)&target->ia_addr);
1088 target->ia_flags &= ~IFA_RTSELF;
1090 if ((flags & LLE_STATIC) &&
1091 !(target->ia_ifp->if_flags & IFF_NOARP))
1092 /* remove arp cache */
1093 arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
1096 if (rtinitflags(target))
1097 prefix = target->ia_dstaddr.sin_addr;
1099 prefix = target->ia_addr.sin_addr;
1100 mask = target->ia_sockmask.sin_addr;
1101 prefix.s_addr &= mask.s_addr;
1104 if ((target->ia_flags & IFA_ROUTE) == 0) {
1105 rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
1110 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1111 if (rtinitflags(ia))
1112 p = ia->ia_dstaddr.sin_addr;
1114 p = ia->ia_addr.sin_addr;
1115 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1118 if ((prefix.s_addr != p.s_addr) ||
1119 !(ia->ia_ifp->if_flags & IFF_UP))
1123 * If we got a matching prefix address, move IFA_ROUTE and
1124 * the route itself to it. Make sure that routing daemons
1127 * XXX: a special case for carp(4) interface - this should
1128 * be more generally specified as an interface that
1129 * doesn't support such action.
1131 if ((ia->ia_flags & IFA_ROUTE) == 0
1132 && (ia->ia_ifp->if_type != IFT_CARP)) {
1133 ifa_ref(&ia->ia_ifa);
1134 IN_IFADDR_RUNLOCK();
1135 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
1136 rtinitflags(target));
1138 target->ia_flags &= ~IFA_ROUTE;
1140 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1142 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
1143 rtinitflags(ia) | RTF_UP);
1145 ia->ia_flags |= IFA_ROUTE;
1147 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1149 ifa_free(&ia->ia_ifa);
1153 IN_IFADDR_RUNLOCK();
1156 * remove all L2 entries on the given prefix
1158 bzero(&prefix0, sizeof(prefix0));
1159 prefix0.sin_len = sizeof(prefix0);
1160 prefix0.sin_family = AF_INET;
1161 prefix0.sin_addr.s_addr = target->ia_subnet;
1162 bzero(&mask0, sizeof(mask0));
1163 mask0.sin_len = sizeof(mask0);
1164 mask0.sin_family = AF_INET;
1165 mask0.sin_addr.s_addr = target->ia_subnetmask;
1166 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
1167 (struct sockaddr *)&mask0, flags);
1170 * As no-one seem to have this prefix, we can remove the route.
1172 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1174 target->ia_flags &= ~IFA_ROUTE;
1176 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1183 * Return 1 if the address might be a local broadcast address.
1186 in_broadcast(struct in_addr in, struct ifnet *ifp)
1188 register struct ifaddr *ifa;
1191 if (in.s_addr == INADDR_BROADCAST ||
1192 in.s_addr == INADDR_ANY)
1194 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1196 t = ntohl(in.s_addr);
1198 * Look through the list of addresses for a match
1199 * with a broadcast address.
1201 #define ia ((struct in_ifaddr *)ifa)
1202 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1203 if (ifa->ifa_addr->sa_family == AF_INET &&
1204 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1206 * Check for old-style (host 0) broadcast, but
1207 * taking into account that RFC 3021 obsoletes it.
1209 (ia->ia_subnetmask != IN_RFC3021_MASK &&
1210 t == ia->ia_subnet)) &&
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_RUNLOCK(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;
1289 * Deletes an address from the address table.
1290 * This function is called by the timer functions
1291 * such as arptimer() and nd6_llinfo_timer(), and
1292 * the caller does the locking.
1295 in_lltable_free(struct lltable *llt, struct llentry *lle)
1298 LLE_LOCK_DESTROY(lle);
1299 free(lle, M_LLTABLE);
1302 static struct llentry *
1303 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
1305 struct in_llentry *lle;
1307 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
1308 if (lle == NULL) /* NB: caller generates msg */
1312 * For IPv4 this will trigger "arpresolve" to generate
1315 lle->base.la_expire = time_uptime; /* mark expired */
1316 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
1317 lle->base.lle_refcnt = 1;
1318 lle->base.lle_free = in_lltable_free;
1319 LLE_LOCK_INIT(&lle->base);
1320 callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock,
1321 CALLOUT_RETURNUNLOCKED);
1323 return (&lle->base);
1326 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1327 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1330 in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
1331 const struct sockaddr *mask, u_int flags)
1333 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1334 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1335 struct llentry *lle, *next;
1337 size_t pkts_dropped;
1339 IF_AFDATA_WLOCK(llt->llt_ifp);
1340 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1341 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1343 * (flags & LLE_STATIC) means deleting all entries
1344 * including static ARP entries.
1346 if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)),
1347 pfx, msk) && ((flags & LLE_STATIC) ||
1348 !(lle->la_flags & LLE_STATIC))) {
1350 if (callout_stop(&lle->la_timer))
1352 pkts_dropped = llentry_free(lle);
1353 ARPSTAT_ADD(dropped, pkts_dropped);
1357 IF_AFDATA_WUNLOCK(llt->llt_ifp);
1362 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1366 KASSERT(l3addr->sa_family == AF_INET,
1367 ("sin_family %d", l3addr->sa_family));
1369 /* XXX rtalloc1 should take a const param */
1370 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1376 * If the gateway for an existing host route matches the target L3
1377 * address, which is a special route inserted by some implementation
1378 * such as MANET, and the interface is of the correct type, then
1379 * allow for ARP to proceed.
1381 if (rt->rt_flags & RTF_GATEWAY) {
1382 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
1383 rt->rt_ifp->if_type != IFT_ETHER ||
1384 (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1385 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
1386 sizeof(in_addr_t)) != 0) {
1393 * Make sure that at least the destination address is covered
1394 * by the route. This is for handling the case where 2 or more
1395 * interfaces have the same prefix. An incoming packet arrives
1396 * on one interface and the corresponding outgoing packet leaves
1397 * another interface.
1399 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
1400 const char *sa, *mask, *addr, *lim;
1403 mask = (const char *)rt_mask(rt);
1405 * Just being extra cautious to avoid some custom
1406 * code getting into trouble.
1413 sa = (const char *)rt_key(rt);
1414 addr = (const char *)l3addr;
1415 len = ((const struct sockaddr_in *)l3addr)->sin_len;
1418 for ( ; addr < lim; sa++, mask++, addr++) {
1419 if ((*sa ^ *addr) & *mask) {
1421 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1422 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1435 * Return NULL if not found or marked for deletion.
1436 * If found return lle read locked.
1438 static struct llentry *
1439 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1441 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1442 struct ifnet *ifp = llt->llt_ifp;
1443 struct llentry *lle;
1444 struct llentries *lleh;
1447 IF_AFDATA_LOCK_ASSERT(ifp);
1448 KASSERT(l3addr->sa_family == AF_INET,
1449 ("sin_family %d", l3addr->sa_family));
1451 hashkey = sin->sin_addr.s_addr;
1452 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1453 LIST_FOREACH(lle, lleh, lle_next) {
1454 struct sockaddr_in *sa2 = satosin(L3_ADDR(lle));
1455 if (lle->la_flags & LLE_DELETED)
1457 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1462 if (flags & LLE_DELETE)
1463 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle);
1465 if (!(flags & LLE_CREATE))
1467 IF_AFDATA_WLOCK_ASSERT(ifp);
1469 * A route that covers the given address must have
1470 * been installed 1st because we are doing a resolution,
1473 if (!(flags & LLE_IFADDR) &&
1474 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1477 lle = in_lltable_new(l3addr, flags);
1479 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1482 lle->la_flags = flags & ~LLE_CREATE;
1483 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1484 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1485 lle->la_flags |= (LLE_VALID | LLE_STATIC);
1489 lle->lle_head = lleh;
1490 lle->la_flags |= LLE_LINKED;
1491 LIST_INSERT_HEAD(lleh, lle, lle_next);
1492 } else if (flags & LLE_DELETE) {
1493 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1495 lle->la_flags |= LLE_DELETED;
1496 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1498 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1500 if ((lle->la_flags &
1501 (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
1509 if (LLE_IS_VALID(lle)) {
1510 if (flags & LLE_EXCLUSIVE)
1520 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1522 #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle))
1523 struct ifnet *ifp = llt->llt_ifp;
1524 struct llentry *lle;
1527 struct rt_msghdr rtm;
1528 struct sockaddr_inarp sin;
1529 struct sockaddr_dl sdl;
1533 LLTABLE_LOCK_ASSERT();
1536 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1537 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1538 struct sockaddr_dl *sdl;
1540 /* skip deleted entries */
1541 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1543 /* Skip if jailed and not a valid IP of the prison. */
1544 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1547 * produce a msg made of:
1549 * struct sockaddr_inarp; (IPv4)
1550 * struct sockaddr_dl;
1552 bzero(&arpc, sizeof(arpc));
1553 arpc.rtm.rtm_msglen = sizeof(arpc);
1554 arpc.rtm.rtm_version = RTM_VERSION;
1555 arpc.rtm.rtm_type = RTM_GET;
1556 arpc.rtm.rtm_flags = RTF_UP;
1557 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1558 arpc.sin.sin_family = AF_INET;
1559 arpc.sin.sin_len = sizeof(arpc.sin);
1560 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1563 if (lle->la_flags & LLE_PUB) {
1564 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1566 if (lle->la_flags & LLE_PROXY)
1567 arpc.sin.sin_other = SIN_PROXY;
1571 sdl->sdl_family = AF_LINK;
1572 sdl->sdl_len = sizeof(*sdl);
1573 sdl->sdl_index = ifp->if_index;
1574 sdl->sdl_type = ifp->if_type;
1575 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1576 sdl->sdl_alen = ifp->if_addrlen;
1577 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1580 bzero(LLADDR(sdl), ifp->if_addrlen);
1583 arpc.rtm.rtm_rmx.rmx_expire =
1584 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1585 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1586 if (lle->la_flags & LLE_STATIC)
1587 arpc.rtm.rtm_flags |= RTF_STATIC;
1588 arpc.rtm.rtm_index = ifp->if_index;
1589 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1599 in_domifattach(struct ifnet *ifp)
1601 struct in_ifinfo *ii;
1602 struct lltable *llt;
1604 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1606 llt = lltable_init(ifp, AF_INET);
1608 llt->llt_prefix_free = in_lltable_prefix_free;
1609 llt->llt_lookup = in_lltable_lookup;
1610 llt->llt_dump = in_lltable_dump;
1614 ii->ii_igmp = igmp_domifattach(ifp);
1620 in_domifdetach(struct ifnet *ifp, void *aux)
1622 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1624 igmp_domifdetach(ifp);
1625 lltable_free(ii->ii_llt);