1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 481d96ce8SMax Laier * Copyright (C) 2001 WIDE Project. All rights reserved. 5df8bae1dSRodney W. Grimes * 6df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 7df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 8df8bae1dSRodney W. Grimes * are met: 9df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 10df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 11df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 13df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 14df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 15df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 16df8bae1dSRodney W. Grimes * without specific prior written permission. 17df8bae1dSRodney W. Grimes * 18df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28df8bae1dSRodney W. Grimes * SUCH DAMAGE. 29df8bae1dSRodney W. Grimes * 302180b925SGarrett Wollman * @(#)in.c 8.4 (Berkeley) 1/9/95 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 334b421e2dSMike Silbersack #include <sys/cdefs.h> 344b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 354b421e2dSMike Silbersack 36c7ea0aa6SQing Li #include "opt_mpath.h" 3750bb1704SGleb Smirnoff 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39c3322cb9SGleb Smirnoff #include <sys/eventhandler.h> 4026f9a767SRodney W. Grimes #include <sys/systm.h> 4151a53488SBruce Evans #include <sys/sockio.h> 42df8bae1dSRodney W. Grimes #include <sys/malloc.h> 43acd3428bSRobert Watson #include <sys/priv.h> 44df8bae1dSRodney W. Grimes #include <sys/socket.h> 455ce0eb7fSBjoern A. Zeeb #include <sys/jail.h> 46f6d24a78SPoul-Henning Kamp #include <sys/kernel.h> 475ce0eb7fSBjoern A. Zeeb #include <sys/proc.h> 48f6d24a78SPoul-Henning Kamp #include <sys/sysctl.h> 49ebc90701SQing Li #include <sys/syslog.h> 50f7a39160SGleb Smirnoff #include <sys/sx.h> 51df8bae1dSRodney W. Grimes 52df8bae1dSRodney W. Grimes #include <net/if.h> 53df813b7eSQing Li #include <net/if_var.h> 54e162ea60SGeorge V. Neville-Neil #include <net/if_arp.h> 55ebc90701SQing Li #include <net/if_dl.h> 566e6b3f7cSQing Li #include <net/if_llatbl.h> 576a800098SYoshinobu Inoue #include <net/if_types.h> 58df8bae1dSRodney W. Grimes #include <net/route.h> 59ebc90701SQing Li #include <net/vnet.h> 60df8bae1dSRodney W. Grimes 6108b68b0eSGleb Smirnoff #include <netinet/if_ether.h> 62df8bae1dSRodney W. Grimes #include <netinet/in.h> 63df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 64e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h> 6571498f30SBruce M Simpson #include <netinet/ip_var.h> 6608b68b0eSGleb Smirnoff #include <netinet/ip_carp.h> 67d10910e6SBruce M Simpson #include <netinet/igmp_var.h> 68eddfbb76SRobert Watson #include <netinet/udp.h> 69eddfbb76SRobert Watson #include <netinet/udp_var.h> 7055166637SPoul-Henning Kamp 7177b89ad8SGleb Smirnoff static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *); 72f7a39160SGleb Smirnoff static int in_difaddr_ioctl(caddr_t, struct ifnet *, struct thread *); 736a800098SYoshinobu Inoue 744d77a549SAlfred Perlstein static void in_socktrim(struct sockaddr_in *); 75ec002feeSBruce M Simpson static void in_purgemaddrs(struct ifnet *); 76df8bae1dSRodney W. Grimes 7708b68b0eSGleb Smirnoff static VNET_DEFINE(int, nosameprefix); 7808b68b0eSGleb Smirnoff #define V_nosameprefix VNET(nosameprefix) 796df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW, 8008b68b0eSGleb Smirnoff &VNET_NAME(nosameprefix), 0, 811ae95409SGleb Smirnoff "Refuse to create same prefixes on different interfaces"); 82477180fbSGarrett Wollman 8382cea7e6SBjoern A. Zeeb VNET_DECLARE(struct inpcbinfo, ripcbinfo); 8482cea7e6SBjoern A. Zeeb #define V_ripcbinfo VNET(ripcbinfo) 8582cea7e6SBjoern A. Zeeb 86f7a39160SGleb Smirnoff static struct sx in_control_sx; 87f7a39160SGleb Smirnoff SX_SYSINIT(in_control_sx, &in_control_sx, "in_control"); 88f7a39160SGleb Smirnoff 89df8bae1dSRodney W. Grimes /* 90df8bae1dSRodney W. Grimes * Return 1 if an internet address is for a ``local'' host 91b365d954SGleb Smirnoff * (one to which we have a connection). 92df8bae1dSRodney W. Grimes */ 9326f9a767SRodney W. Grimes int 94f2565d68SRobert Watson in_localaddr(struct in_addr in) 95df8bae1dSRodney W. Grimes { 96df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 97df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 98df8bae1dSRodney W. Grimes 992d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 1002d9cfabaSRobert Watson TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1012d9cfabaSRobert Watson if ((i & ia->ia_subnetmask) == ia->ia_subnet) { 1022d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 1032d9cfabaSRobert Watson return (1); 1042d9cfabaSRobert Watson } 1052d9cfabaSRobert Watson } 1062d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 107df8bae1dSRodney W. Grimes return (0); 108df8bae1dSRodney W. Grimes } 109df8bae1dSRodney W. Grimes 110df8bae1dSRodney W. Grimes /* 1112eccc90bSAndre Oppermann * Return 1 if an internet address is for the local host and configured 1122eccc90bSAndre Oppermann * on one of its interfaces. 1132eccc90bSAndre Oppermann */ 1142eccc90bSAndre Oppermann int 115f2565d68SRobert Watson in_localip(struct in_addr in) 1162eccc90bSAndre Oppermann { 1172eccc90bSAndre Oppermann struct in_ifaddr *ia; 1182eccc90bSAndre Oppermann 1192d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 1202eccc90bSAndre Oppermann LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 1212d9cfabaSRobert Watson if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) { 1222d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 123460473a0SBjoern A. Zeeb return (1); 1242eccc90bSAndre Oppermann } 1252d9cfabaSRobert Watson } 1262d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 127460473a0SBjoern A. Zeeb return (0); 1282eccc90bSAndre Oppermann } 1292eccc90bSAndre Oppermann 1302eccc90bSAndre Oppermann /* 131*28ebe80cSGleb Smirnoff * Return 1 if an internet address is configured on an interface. 132*28ebe80cSGleb Smirnoff */ 133*28ebe80cSGleb Smirnoff int 134*28ebe80cSGleb Smirnoff in_ifhasaddr(struct ifnet *ifp, struct in_addr in) 135*28ebe80cSGleb Smirnoff { 136*28ebe80cSGleb Smirnoff struct ifaddr *ifa; 137*28ebe80cSGleb Smirnoff struct in_ifaddr *ia; 138*28ebe80cSGleb Smirnoff 139*28ebe80cSGleb Smirnoff IF_ADDR_RLOCK(ifp); 140*28ebe80cSGleb Smirnoff TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 141*28ebe80cSGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET) 142*28ebe80cSGleb Smirnoff continue; 143*28ebe80cSGleb Smirnoff ia = (struct in_ifaddr *)ifa; 144*28ebe80cSGleb Smirnoff if (ia->ia_addr.sin_addr.s_addr == in.s_addr) { 145*28ebe80cSGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 146*28ebe80cSGleb Smirnoff return (1); 147*28ebe80cSGleb Smirnoff } 148*28ebe80cSGleb Smirnoff } 149*28ebe80cSGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 150*28ebe80cSGleb Smirnoff 151*28ebe80cSGleb Smirnoff return (0); 152*28ebe80cSGleb Smirnoff } 153*28ebe80cSGleb Smirnoff 154*28ebe80cSGleb Smirnoff /* 155f7a39160SGleb Smirnoff * Return a reference to the interface address which is different to 156f7a39160SGleb Smirnoff * the supplied one but with same IP address value. 157f7a39160SGleb Smirnoff */ 158f7a39160SGleb Smirnoff static struct in_ifaddr * 159f7a39160SGleb Smirnoff in_localip_more(struct in_ifaddr *ia) 160f7a39160SGleb Smirnoff { 161f7a39160SGleb Smirnoff in_addr_t in = IA_SIN(ia)->sin_addr.s_addr; 162f7a39160SGleb Smirnoff struct in_ifaddr *it; 163f7a39160SGleb Smirnoff 164f7a39160SGleb Smirnoff IN_IFADDR_RLOCK(); 165f7a39160SGleb Smirnoff LIST_FOREACH(it, INADDR_HASH(in), ia_hash) { 166f7a39160SGleb Smirnoff if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) { 167f7a39160SGleb Smirnoff ifa_ref(&it->ia_ifa); 168f7a39160SGleb Smirnoff IN_IFADDR_RUNLOCK(); 169f7a39160SGleb Smirnoff return (it); 170f7a39160SGleb Smirnoff } 171f7a39160SGleb Smirnoff } 172f7a39160SGleb Smirnoff IN_IFADDR_RUNLOCK(); 173f7a39160SGleb Smirnoff 174f7a39160SGleb Smirnoff return (NULL); 175f7a39160SGleb Smirnoff } 176f7a39160SGleb Smirnoff 177f7a39160SGleb Smirnoff /* 178df8bae1dSRodney W. Grimes * Determine whether an IP address is in a reserved set of addresses 179df8bae1dSRodney W. Grimes * that may not be forwarded, or whether datagrams to that destination 180df8bae1dSRodney W. Grimes * may be forwarded. 181df8bae1dSRodney W. Grimes */ 18226f9a767SRodney W. Grimes int 183f2565d68SRobert Watson in_canforward(struct in_addr in) 184df8bae1dSRodney W. Grimes { 185df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 186df8bae1dSRodney W. Grimes register u_long net; 187df8bae1dSRodney W. Grimes 188f8429ca2SBruce M Simpson if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i)) 189df8bae1dSRodney W. Grimes return (0); 190df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) { 191df8bae1dSRodney W. Grimes net = i & IN_CLASSA_NET; 192df8bae1dSRodney W. Grimes if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 193df8bae1dSRodney W. Grimes return (0); 194df8bae1dSRodney W. Grimes } 195df8bae1dSRodney W. Grimes return (1); 196df8bae1dSRodney W. Grimes } 197df8bae1dSRodney W. Grimes 198df8bae1dSRodney W. Grimes /* 199df8bae1dSRodney W. Grimes * Trim a mask in a sockaddr 200df8bae1dSRodney W. Grimes */ 2010312fbe9SPoul-Henning Kamp static void 202f2565d68SRobert Watson in_socktrim(struct sockaddr_in *ap) 203df8bae1dSRodney W. Grimes { 204df8bae1dSRodney W. Grimes register char *cplim = (char *) &ap->sin_addr; 205df8bae1dSRodney W. Grimes register char *cp = (char *) (&ap->sin_addr + 1); 206df8bae1dSRodney W. Grimes 207df8bae1dSRodney W. Grimes ap->sin_len = 0; 208df00058dSGarrett Wollman while (--cp >= cplim) 209df8bae1dSRodney W. Grimes if (*cp) { 210df8bae1dSRodney W. Grimes (ap)->sin_len = cp - (char *) (ap) + 1; 211df8bae1dSRodney W. Grimes break; 212df8bae1dSRodney W. Grimes } 213df8bae1dSRodney W. Grimes } 214df8bae1dSRodney W. Grimes 215df8bae1dSRodney W. Grimes /* 216df8bae1dSRodney W. Grimes * Generic internet control operations (ioctl's). 217df8bae1dSRodney W. Grimes */ 21826f9a767SRodney W. Grimes int 219f2565d68SRobert Watson in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 220f2565d68SRobert Watson struct thread *td) 221df8bae1dSRodney W. Grimes { 222f7a39160SGleb Smirnoff struct ifreq *ifr = (struct ifreq *)data; 223f7a39160SGleb Smirnoff struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr; 224821b5cafSGleb Smirnoff struct ifaddr *ifa; 225f7a39160SGleb Smirnoff struct in_ifaddr *ia; 226f7a39160SGleb Smirnoff int error; 227f7a39160SGleb Smirnoff 228f7a39160SGleb Smirnoff if (ifp == NULL) 229f7a39160SGleb Smirnoff return (EADDRNOTAVAIL); 23071212473SGleb Smirnoff 23171212473SGleb Smirnoff /* 232f7a39160SGleb Smirnoff * Filter out 4 ioctls we implement directly. Forward the rest 233f7a39160SGleb Smirnoff * to specific functions and ifp->if_ioctl(). 234bbb3fb61SRobert Watson */ 2356a800098SYoshinobu Inoue switch (cmd) { 236bbb3fb61SRobert Watson case SIOCGIFADDR: 237bbb3fb61SRobert Watson case SIOCGIFBRDADDR: 238bbb3fb61SRobert Watson case SIOCGIFDSTADDR: 239bbb3fb61SRobert Watson case SIOCGIFNETMASK: 240f7a39160SGleb Smirnoff break; 2416d00fd9cSGleb Smirnoff case SIOCDIFADDR: 242f7a39160SGleb Smirnoff sx_xlock(&in_control_sx); 243f7a39160SGleb Smirnoff error = in_difaddr_ioctl(data, ifp, td); 244f7a39160SGleb Smirnoff sx_xunlock(&in_control_sx); 245f7a39160SGleb Smirnoff return (error); 24677b89ad8SGleb Smirnoff case OSIOCAIFADDR: /* 9.x compat */ 2476d00fd9cSGleb Smirnoff case SIOCAIFADDR: 248f7a39160SGleb Smirnoff sx_xlock(&in_control_sx); 24977b89ad8SGleb Smirnoff error = in_aifaddr_ioctl(cmd, data, ifp, td); 250f7a39160SGleb Smirnoff sx_xunlock(&in_control_sx); 251f7a39160SGleb Smirnoff return (error); 252bbb3fb61SRobert Watson case SIOCSIFADDR: 253bbb3fb61SRobert Watson case SIOCSIFBRDADDR: 254bbb3fb61SRobert Watson case SIOCSIFDSTADDR: 255bbb3fb61SRobert Watson case SIOCSIFNETMASK: 25656cf9dc1SGleb Smirnoff /* We no longer support that old commands. */ 2576d00fd9cSGleb Smirnoff return (EINVAL); 258bbb3fb61SRobert Watson default: 259f7a39160SGleb Smirnoff if (ifp->if_ioctl == NULL) 260bbb3fb61SRobert Watson return (EOPNOTSUPP); 261bbb3fb61SRobert Watson return ((*ifp->if_ioctl)(ifp, cmd, data)); 2626a800098SYoshinobu Inoue } 2636a800098SYoshinobu Inoue 264821b5cafSGleb Smirnoff if (addr->sin_addr.s_addr != INADDR_ANY && 265821b5cafSGleb Smirnoff prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0) 266821b5cafSGleb Smirnoff return (EADDRNOTAVAIL); 267821b5cafSGleb Smirnoff 268cf7b18f1SRobert Watson /* 269a7f77a39SXin LI * Find address for this interface, if it exists. If an 270a7f77a39SXin LI * address was specified, find that one instead of the 271a7f77a39SXin LI * first one on the interface, if possible. 272df8bae1dSRodney W. Grimes */ 273821b5cafSGleb Smirnoff IF_ADDR_RLOCK(ifp); 274821b5cafSGleb Smirnoff TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2759706c950SGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET) 2769706c950SGleb Smirnoff continue; 277821b5cafSGleb Smirnoff ia = (struct in_ifaddr *)ifa; 278821b5cafSGleb Smirnoff if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr) 279df8bae1dSRodney W. Grimes break; 280ca925d9cSJonathan Lemon } 281a7f77a39SXin LI if (ifa == NULL) 282a7f77a39SXin LI TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 283a7f77a39SXin LI if (ifa->ifa_addr->sa_family == AF_INET) { 284a7f77a39SXin LI ia = (struct in_ifaddr *)ifa; 285a7f77a39SXin LI if (prison_check_ip4(td->td_ucred, 286a7f77a39SXin LI &ia->ia_addr.sin_addr) == 0) 287a7f77a39SXin LI break; 288a7f77a39SXin LI } 289f7a39160SGleb Smirnoff 290821b5cafSGleb Smirnoff if (ifa == NULL) { 291821b5cafSGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 292f7a39160SGleb Smirnoff return (EADDRNOTAVAIL); 293ac0aa473SBill Fenner } 294df8bae1dSRodney W. Grimes 295588885f2SRobert Watson error = 0; 296df8bae1dSRodney W. Grimes switch (cmd) { 297f7a39160SGleb Smirnoff case SIOCGIFADDR: 298f7a39160SGleb Smirnoff *addr = ia->ia_addr; 299f7a39160SGleb Smirnoff break; 3008c0fec80SRobert Watson 301f7a39160SGleb Smirnoff case SIOCGIFBRDADDR: 302f7a39160SGleb Smirnoff if ((ifp->if_flags & IFF_BROADCAST) == 0) { 303f7a39160SGleb Smirnoff error = EINVAL; 304df8bae1dSRodney W. Grimes break; 305df8bae1dSRodney W. Grimes } 306f7a39160SGleb Smirnoff *addr = ia->ia_broadaddr; 307f7a39160SGleb Smirnoff break; 308f7a39160SGleb Smirnoff 309f7a39160SGleb Smirnoff case SIOCGIFDSTADDR: 310f7a39160SGleb Smirnoff if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 311f7a39160SGleb Smirnoff error = EINVAL; 312f7a39160SGleb Smirnoff break; 313f7a39160SGleb Smirnoff } 314f7a39160SGleb Smirnoff *addr = ia->ia_dstaddr; 315f7a39160SGleb Smirnoff break; 316f7a39160SGleb Smirnoff 317f7a39160SGleb Smirnoff case SIOCGIFNETMASK: 318f7a39160SGleb Smirnoff *addr = ia->ia_sockmask; 319f7a39160SGleb Smirnoff break; 320f7a39160SGleb Smirnoff } 321f7a39160SGleb Smirnoff 322821b5cafSGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 323f7a39160SGleb Smirnoff 324f7a39160SGleb Smirnoff return (error); 3251067217dSGarrett Wollman } 326f7a39160SGleb Smirnoff 327f7a39160SGleb Smirnoff static int 32877b89ad8SGleb Smirnoff in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td) 329f7a39160SGleb Smirnoff { 330f7a39160SGleb Smirnoff const struct in_aliasreq *ifra = (struct in_aliasreq *)data; 331f7a39160SGleb Smirnoff const struct sockaddr_in *addr = &ifra->ifra_addr; 332f7a39160SGleb Smirnoff const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr; 333f7a39160SGleb Smirnoff const struct sockaddr_in *mask = &ifra->ifra_mask; 334f7a39160SGleb Smirnoff const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr; 33577b89ad8SGleb Smirnoff const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0; 336f7a39160SGleb Smirnoff struct ifaddr *ifa; 337f7a39160SGleb Smirnoff struct in_ifaddr *ia; 338f7a39160SGleb Smirnoff bool iaIsFirst; 339f7a39160SGleb Smirnoff int error = 0; 340f7a39160SGleb Smirnoff 341f7a39160SGleb Smirnoff error = priv_check(td, PRIV_NET_ADDIFADDR); 342f7a39160SGleb Smirnoff if (error) 343f7a39160SGleb Smirnoff return (error); 344f7a39160SGleb Smirnoff 345f7a39160SGleb Smirnoff /* 346f7a39160SGleb Smirnoff * ifra_addr must be present and be of INET family. 347f7a39160SGleb Smirnoff * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional. 348f7a39160SGleb Smirnoff */ 349f7a39160SGleb Smirnoff if (addr->sin_len != sizeof(struct sockaddr_in) || 350f7a39160SGleb Smirnoff addr->sin_family != AF_INET) 351f7a39160SGleb Smirnoff return (EINVAL); 352f7a39160SGleb Smirnoff if (broadaddr->sin_len != 0 && 353f7a39160SGleb Smirnoff (broadaddr->sin_len != sizeof(struct sockaddr_in) || 354f7a39160SGleb Smirnoff broadaddr->sin_family != AF_INET)) 355f7a39160SGleb Smirnoff return (EINVAL); 356f7a39160SGleb Smirnoff if (mask->sin_len != 0 && 357f7a39160SGleb Smirnoff (mask->sin_len != sizeof(struct sockaddr_in) || 358f7a39160SGleb Smirnoff mask->sin_family != AF_INET)) 359f7a39160SGleb Smirnoff return (EINVAL); 360f7a39160SGleb Smirnoff if ((ifp->if_flags & IFF_POINTOPOINT) && 361f7a39160SGleb Smirnoff (dstaddr->sin_len != sizeof(struct sockaddr_in) || 362f7a39160SGleb Smirnoff dstaddr->sin_addr.s_addr == INADDR_ANY)) 363f7a39160SGleb Smirnoff return (EDESTADDRREQ); 364f7a39160SGleb Smirnoff if (vhid > 0 && carp_attach_p == NULL) 365f7a39160SGleb Smirnoff return (EPROTONOSUPPORT); 366f7a39160SGleb Smirnoff 367f7a39160SGleb Smirnoff /* 368f7a39160SGleb Smirnoff * See whether address already exist. 369f7a39160SGleb Smirnoff */ 370f7a39160SGleb Smirnoff iaIsFirst = true; 371f7a39160SGleb Smirnoff ia = NULL; 372f7a39160SGleb Smirnoff IF_ADDR_RLOCK(ifp); 373f7a39160SGleb Smirnoff TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3749706c950SGleb Smirnoff struct in_ifaddr *it; 375f7a39160SGleb Smirnoff 3769706c950SGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET) 377f7a39160SGleb Smirnoff continue; 378f7a39160SGleb Smirnoff 3799706c950SGleb Smirnoff it = (struct in_ifaddr *)ifa; 380f7a39160SGleb Smirnoff iaIsFirst = false; 381f7a39160SGleb Smirnoff if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 382f7a39160SGleb Smirnoff prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) 383f7a39160SGleb Smirnoff ia = it; 3841067217dSGarrett Wollman } 385f7a39160SGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 386f7a39160SGleb Smirnoff 387f7a39160SGleb Smirnoff if (ia != NULL) 388f7a39160SGleb Smirnoff (void )in_difaddr_ioctl(data, ifp, td); 389f7a39160SGleb Smirnoff 39046758960SGleb Smirnoff ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK); 39146758960SGleb Smirnoff ia = (struct in_ifaddr *)ifa; 39259562606SGarrett Wollman ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 39359562606SGarrett Wollman ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 39459562606SGarrett Wollman ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 39519fc74fbSJeffrey Hsu 396f7a39160SGleb Smirnoff ia->ia_ifp = ifp; 397f7a39160SGleb Smirnoff ia->ia_addr = *addr; 398f7a39160SGleb Smirnoff if (mask->sin_len != 0) { 399f7a39160SGleb Smirnoff ia->ia_sockmask = *mask; 400f7a39160SGleb Smirnoff ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 401f7a39160SGleb Smirnoff } else { 402f7a39160SGleb Smirnoff in_addr_t i = ntohl(addr->sin_addr.s_addr); 403f7a39160SGleb Smirnoff 404f7a39160SGleb Smirnoff /* 405f7a39160SGleb Smirnoff * Be compatible with network classes, if netmask isn't 406f7a39160SGleb Smirnoff * supplied, guess it based on classes. 407f7a39160SGleb Smirnoff */ 408f7a39160SGleb Smirnoff if (IN_CLASSA(i)) 409f7a39160SGleb Smirnoff ia->ia_subnetmask = IN_CLASSA_NET; 410f7a39160SGleb Smirnoff else if (IN_CLASSB(i)) 411f7a39160SGleb Smirnoff ia->ia_subnetmask = IN_CLASSB_NET; 412f7a39160SGleb Smirnoff else 413f7a39160SGleb Smirnoff ia->ia_subnetmask = IN_CLASSC_NET; 414f7a39160SGleb Smirnoff ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 415f7a39160SGleb Smirnoff } 416f7a39160SGleb Smirnoff ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask; 417f7a39160SGleb Smirnoff in_socktrim(&ia->ia_sockmask); 418f7a39160SGleb Smirnoff 419df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 420f7a39160SGleb Smirnoff if (broadaddr->sin_len != 0) { 421f7a39160SGleb Smirnoff ia->ia_broadaddr = *broadaddr; 422f7a39160SGleb Smirnoff } else if (ia->ia_subnetmask == IN_RFC3021_MASK) { 423f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST; 424f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 425f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_family = AF_INET; 426f7a39160SGleb Smirnoff } else { 427f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_addr.s_addr = 428f7a39160SGleb Smirnoff htonl(ia->ia_subnet | ~ia->ia_subnetmask); 429f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 430df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_family = AF_INET; 431df8bae1dSRodney W. Grimes } 432f7a39160SGleb Smirnoff } 433f7a39160SGleb Smirnoff 434f7a39160SGleb Smirnoff if (ifp->if_flags & IFF_POINTOPOINT) 435f7a39160SGleb Smirnoff ia->ia_dstaddr = *dstaddr; 436f7a39160SGleb Smirnoff 437f7a39160SGleb Smirnoff /* XXXGL: rtinit() needs this strange assignment. */ 438f7a39160SGleb Smirnoff if (ifp->if_flags & IFF_LOOPBACK) 439f7a39160SGleb Smirnoff ia->ia_dstaddr = ia->ia_addr; 440f3d30eb2SGleb Smirnoff 4415af464bbSSteven Hartland if (vhid != 0) { 4425af464bbSSteven Hartland error = (*carp_attach_p)(&ia->ia_ifa, vhid); 4435af464bbSSteven Hartland if (error) 4445af464bbSSteven Hartland return (error); 4455af464bbSSteven Hartland } 4465af464bbSSteven Hartland 447a49b317cSAlexander V. Chernikov /* if_addrhead is already referenced by ifa_alloc() */ 448137f91e8SJohn Baldwin IF_ADDR_WLOCK(ifp); 4498021456aSRobert Watson TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 450137f91e8SJohn Baldwin IF_ADDR_WUNLOCK(ifp); 451f7a39160SGleb Smirnoff 4528c0fec80SRobert Watson ifa_ref(ifa); /* in_ifaddrhead */ 4532d9cfabaSRobert Watson IN_IFADDR_WLOCK(); 454603724d3SBjoern A. Zeeb TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 455f7a39160SGleb Smirnoff LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 4562d9cfabaSRobert Watson IN_IFADDR_WUNLOCK(); 457df8bae1dSRodney W. Grimes 458f7a39160SGleb Smirnoff /* 459f7a39160SGleb Smirnoff * Give the interface a chance to initialize 460f7a39160SGleb Smirnoff * if this is its first address, 461f7a39160SGleb Smirnoff * and to validate the address if necessary. 462f7a39160SGleb Smirnoff */ 463d34165f7SSteven Hartland if (ifp->if_ioctl != NULL) { 464f7a39160SGleb Smirnoff error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 465f7a39160SGleb Smirnoff if (error) 4665af464bbSSteven Hartland goto fail1; 467d34165f7SSteven Hartland } 468f7a39160SGleb Smirnoff 469f7a39160SGleb Smirnoff /* 470f7a39160SGleb Smirnoff * Add route for the network. 471f7a39160SGleb Smirnoff */ 472f7a39160SGleb Smirnoff if (vhid == 0) { 473f7a39160SGleb Smirnoff int flags = RTF_UP; 474f7a39160SGleb Smirnoff 475f7a39160SGleb Smirnoff if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 476f7a39160SGleb Smirnoff flags |= RTF_HOST; 477f7a39160SGleb Smirnoff 478f7a39160SGleb Smirnoff error = in_addprefix(ia, flags); 479f7a39160SGleb Smirnoff if (error) 4805af464bbSSteven Hartland goto fail1; 481df8bae1dSRodney W. Grimes } 482df8bae1dSRodney W. Grimes 483588885f2SRobert Watson /* 484f7a39160SGleb Smirnoff * Add a loopback route to self. 485588885f2SRobert Watson */ 486f7a39160SGleb Smirnoff if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 && 487c1f7c3f5SGleb Smirnoff ia->ia_addr.sin_addr.s_addr != INADDR_ANY && 488c1f7c3f5SGleb Smirnoff !((ifp->if_flags & IFF_POINTOPOINT) && 489c1f7c3f5SGleb Smirnoff ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) { 490f7a39160SGleb Smirnoff struct in_ifaddr *eia; 491df8bae1dSRodney W. Grimes 492f7a39160SGleb Smirnoff eia = in_localip_more(ia); 493f7a39160SGleb Smirnoff 494f7a39160SGleb Smirnoff if (eia == NULL) { 495f7a39160SGleb Smirnoff error = ifa_add_loopback_route((struct ifaddr *)ia, 496f7a39160SGleb Smirnoff (struct sockaddr *)&ia->ia_addr); 497f7a39160SGleb Smirnoff if (error) 4985af464bbSSteven Hartland goto fail2; 499f7a39160SGleb Smirnoff } else 500f7a39160SGleb Smirnoff ifa_free(&eia->ia_ifa); 501588885f2SRobert Watson } 502df8bae1dSRodney W. Grimes 503f7a39160SGleb Smirnoff if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) { 504f7a39160SGleb Smirnoff struct in_addr allhosts_addr; 505f7a39160SGleb Smirnoff struct in_ifinfo *ii; 506df8bae1dSRodney W. Grimes 507c75aa354SBruce M Simpson ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 508f7a39160SGleb Smirnoff allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 509df8bae1dSRodney W. Grimes 510f7a39160SGleb Smirnoff error = in_joingroup(ifp, &allhosts_addr, NULL, 511f7a39160SGleb Smirnoff &ii->ii_allhosts); 512f7a39160SGleb Smirnoff } 513f7a39160SGleb Smirnoff 514f7a39160SGleb Smirnoff EVENTHANDLER_INVOKE(ifaddr_event, ifp); 515f7a39160SGleb Smirnoff 516f7a39160SGleb Smirnoff return (error); 517f7a39160SGleb Smirnoff 5185af464bbSSteven Hartland fail2: 519f7a39160SGleb Smirnoff if (vhid == 0) 520f7a39160SGleb Smirnoff (void )in_scrubprefix(ia, LLE_STATIC); 521f7a39160SGleb Smirnoff 5225af464bbSSteven Hartland fail1: 523f7a39160SGleb Smirnoff if (ia->ia_ifa.ifa_carp) 524f7a39160SGleb Smirnoff (*carp_detach_p)(&ia->ia_ifa); 525f7a39160SGleb Smirnoff 526f7a39160SGleb Smirnoff IF_ADDR_WLOCK(ifp); 527f7a39160SGleb Smirnoff TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 528f7a39160SGleb Smirnoff IF_ADDR_WUNLOCK(ifp); 529a49b317cSAlexander V. Chernikov ifa_free(&ia->ia_ifa); /* if_addrhead */ 530f7a39160SGleb Smirnoff 531f7a39160SGleb Smirnoff IN_IFADDR_WLOCK(); 532f7a39160SGleb Smirnoff TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 533f7a39160SGleb Smirnoff LIST_REMOVE(ia, ia_hash); 534f7a39160SGleb Smirnoff IN_IFADDR_WUNLOCK(); 535a49b317cSAlexander V. Chernikov ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 536f7a39160SGleb Smirnoff 537f7a39160SGleb Smirnoff return (error); 538f7a39160SGleb Smirnoff } 539f7a39160SGleb Smirnoff 540f7a39160SGleb Smirnoff static int 541f7a39160SGleb Smirnoff in_difaddr_ioctl(caddr_t data, struct ifnet *ifp, struct thread *td) 542f7a39160SGleb Smirnoff { 543f7a39160SGleb Smirnoff const struct ifreq *ifr = (struct ifreq *)data; 5446224cd89SNathan Whitehorn const struct sockaddr_in *addr = (const struct sockaddr_in *) 5456224cd89SNathan Whitehorn &ifr->ifr_addr; 546f7a39160SGleb Smirnoff struct ifaddr *ifa; 547f7a39160SGleb Smirnoff struct in_ifaddr *ia; 548f7a39160SGleb Smirnoff bool deleteAny, iaIsLast; 549f7a39160SGleb Smirnoff int error; 550f7a39160SGleb Smirnoff 551f7a39160SGleb Smirnoff if (td != NULL) { 552f7a39160SGleb Smirnoff error = priv_check(td, PRIV_NET_DELIFADDR); 553f7a39160SGleb Smirnoff if (error) 554f7a39160SGleb Smirnoff return (error); 555f7a39160SGleb Smirnoff } 556f7a39160SGleb Smirnoff 557f7a39160SGleb Smirnoff if (addr->sin_len != sizeof(struct sockaddr_in) || 558f7a39160SGleb Smirnoff addr->sin_family != AF_INET) 559f7a39160SGleb Smirnoff deleteAny = true; 560f7a39160SGleb Smirnoff else 561f7a39160SGleb Smirnoff deleteAny = false; 562f7a39160SGleb Smirnoff 563f7a39160SGleb Smirnoff iaIsLast = true; 564f7a39160SGleb Smirnoff ia = NULL; 565f7a39160SGleb Smirnoff IF_ADDR_WLOCK(ifp); 566f7a39160SGleb Smirnoff TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 5679706c950SGleb Smirnoff struct in_ifaddr *it; 568f7a39160SGleb Smirnoff 5699706c950SGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET) 570f7a39160SGleb Smirnoff continue; 571f7a39160SGleb Smirnoff 5729706c950SGleb Smirnoff it = (struct in_ifaddr *)ifa; 573f7a39160SGleb Smirnoff if (deleteAny && ia == NULL && (td == NULL || 574f7a39160SGleb Smirnoff prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0)) 575f7a39160SGleb Smirnoff ia = it; 576f7a39160SGleb Smirnoff 577f7a39160SGleb Smirnoff if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 578f7a39160SGleb Smirnoff (td == NULL || prison_check_ip4(td->td_ucred, 579f7a39160SGleb Smirnoff &addr->sin_addr) == 0)) 580f7a39160SGleb Smirnoff ia = it; 581f7a39160SGleb Smirnoff 582f7a39160SGleb Smirnoff if (it != ia) 583f7a39160SGleb Smirnoff iaIsLast = false; 584f7a39160SGleb Smirnoff } 585f7a39160SGleb Smirnoff 586f7a39160SGleb Smirnoff if (ia == NULL) { 587f7a39160SGleb Smirnoff IF_ADDR_WUNLOCK(ifp); 588f7a39160SGleb Smirnoff return (EADDRNOTAVAIL); 589f7a39160SGleb Smirnoff } 590f7a39160SGleb Smirnoff 591f7a39160SGleb Smirnoff TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 592f7a39160SGleb Smirnoff IF_ADDR_WUNLOCK(ifp); 593f7a39160SGleb Smirnoff ifa_free(&ia->ia_ifa); /* if_addrhead */ 594f7a39160SGleb Smirnoff 595f7a39160SGleb Smirnoff IN_IFADDR_WLOCK(); 596f7a39160SGleb Smirnoff TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 597f7a39160SGleb Smirnoff LIST_REMOVE(ia, ia_hash); 598f7a39160SGleb Smirnoff IN_IFADDR_WUNLOCK(); 599f7a39160SGleb Smirnoff 600089cdfadSRuslan Ermilov /* 601237bf7f7SGleb Smirnoff * in_scrubprefix() kills the interface route. 602089cdfadSRuslan Ermilov */ 603237bf7f7SGleb Smirnoff in_scrubprefix(ia, LLE_STATIC); 604588885f2SRobert Watson 605c655b7c4SDavid Greenman /* 606089cdfadSRuslan Ermilov * in_ifadown gets rid of all the rest of 607089cdfadSRuslan Ermilov * the routes. This is not quite the right 608089cdfadSRuslan Ermilov * thing to do, but at least if we are running 609089cdfadSRuslan Ermilov * a routing process they will come back. 610089cdfadSRuslan Ermilov */ 61191854268SRuslan Ermilov in_ifadown(&ia->ia_ifa, 1); 6120f02fdacSBrian Somers 61308b68b0eSGleb Smirnoff if (ia->ia_ifa.ifa_carp) 61408b68b0eSGleb Smirnoff (*carp_detach_p)(&ia->ia_ifa); 61508b68b0eSGleb Smirnoff 616f7e083afSBruce M Simpson /* 617f7e083afSBruce M Simpson * If this is the last IPv4 address configured on this 618f7e083afSBruce M Simpson * interface, leave the all-hosts group. 619d10910e6SBruce M Simpson * No state-change report need be transmitted. 620f7e083afSBruce M Simpson */ 621f7a39160SGleb Smirnoff if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) { 622f7a39160SGleb Smirnoff struct in_ifinfo *ii; 623f7a39160SGleb Smirnoff 624c75aa354SBruce M Simpson ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 625f7e083afSBruce M Simpson IN_MULTI_LOCK(); 626d10910e6SBruce M Simpson if (ii->ii_allhosts) { 6276d00fd9cSGleb Smirnoff (void)in_leavegroup_locked(ii->ii_allhosts, NULL); 628d10910e6SBruce M Simpson ii->ii_allhosts = NULL; 629d10910e6SBruce M Simpson } 630f7e083afSBruce M Simpson IN_MULTI_UNLOCK(); 631f7a39160SGleb Smirnoff } 6326d00fd9cSGleb Smirnoff 633f7a39160SGleb Smirnoff EVENTHANDLER_INVOKE(ifaddr_event, ifp); 634a49b317cSAlexander V. Chernikov ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 635f7a39160SGleb Smirnoff 636f7a39160SGleb Smirnoff return (0); 637df8bae1dSRodney W. Grimes } 638df8bae1dSRodney W. Grimes 63948321abeSMax Laier #define rtinitflags(x) \ 64048321abeSMax Laier ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 64148321abeSMax Laier ? RTF_HOST : 0) 642ccbb9c35SQing Li 643ccbb9c35SQing Li /* 644fbdd20a1SMatt Jacob * Check if we have a route for the given prefix already or add one accordingly. 64548321abeSMax Laier */ 64608b68b0eSGleb Smirnoff int 647f2565d68SRobert Watson in_addprefix(struct in_ifaddr *target, int flags) 64848321abeSMax Laier { 64948321abeSMax Laier struct in_ifaddr *ia; 650bfb26eecSGleb Smirnoff struct in_addr prefix, mask, p, m; 6510cfee0c2SAlan Somers int error; 65248321abeSMax Laier 653fbdd20a1SMatt Jacob if ((flags & RTF_HOST) != 0) { 65448321abeSMax Laier prefix = target->ia_dstaddr.sin_addr; 655fbdd20a1SMatt Jacob mask.s_addr = 0; 656fbdd20a1SMatt Jacob } else { 65748321abeSMax Laier prefix = target->ia_addr.sin_addr; 65848321abeSMax Laier mask = target->ia_sockmask.sin_addr; 65948321abeSMax Laier prefix.s_addr &= mask.s_addr; 66048321abeSMax Laier } 66148321abeSMax Laier 6622d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 6630cfee0c2SAlan Somers /* Look for an existing address with the same prefix, mask, and fib */ 664603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 665bfb26eecSGleb Smirnoff if (rtinitflags(ia)) { 66672366606SKevin Lo p = ia->ia_dstaddr.sin_addr; 66748321abeSMax Laier 66848321abeSMax Laier if (prefix.s_addr != p.s_addr) 66948321abeSMax Laier continue; 670bfb26eecSGleb Smirnoff } else { 671bfb26eecSGleb Smirnoff p = ia->ia_addr.sin_addr; 672bfb26eecSGleb Smirnoff m = ia->ia_sockmask.sin_addr; 673bfb26eecSGleb Smirnoff p.s_addr &= m.s_addr; 674bfb26eecSGleb Smirnoff 675bfb26eecSGleb Smirnoff if (prefix.s_addr != p.s_addr || 676bfb26eecSGleb Smirnoff mask.s_addr != m.s_addr) 677bfb26eecSGleb Smirnoff continue; 678bfb26eecSGleb Smirnoff } 6790cfee0c2SAlan Somers if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib) 6800cfee0c2SAlan Somers continue; 68148321abeSMax Laier 68248321abeSMax Laier /* 68348321abeSMax Laier * If we got a matching prefix route inserted by other 68448321abeSMax Laier * interface address, we are done here. 68548321abeSMax Laier */ 6861ae95409SGleb Smirnoff if (ia->ia_flags & IFA_ROUTE) { 687c7ea0aa6SQing Li #ifdef RADIX_MPATH 688c7ea0aa6SQing Li if (ia->ia_addr.sin_addr.s_addr == 68942db1b87SBjoern A. Zeeb target->ia_addr.sin_addr.s_addr) { 69042db1b87SBjoern A. Zeeb IN_IFADDR_RUNLOCK(); 691c7ea0aa6SQing Li return (EEXIST); 69242db1b87SBjoern A. Zeeb } else 693c7ea0aa6SQing Li break; 694c7ea0aa6SQing Li #endif 69508b68b0eSGleb Smirnoff if (V_nosameprefix) { 6962d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 6971ae95409SGleb Smirnoff return (EEXIST); 6982d9cfabaSRobert Watson } else { 6990cfee0c2SAlan Somers int fibnum; 7000cfee0c2SAlan Somers 701cc45ae40SHiroki Sato fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS : 7020cfee0c2SAlan Somers target->ia_ifp->if_fib; 703d375edc9SAlexander V. Chernikov rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum); 7042d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 7051ae95409SGleb Smirnoff return (0); 7061ae95409SGleb Smirnoff } 70748321abeSMax Laier } 7082d9cfabaSRobert Watson } 7092d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 71048321abeSMax Laier 71148321abeSMax Laier /* 71248321abeSMax Laier * No-one seem to have this prefix route, so we try to insert it. 71348321abeSMax Laier */ 71448321abeSMax Laier error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 71548321abeSMax Laier if (!error) 71648321abeSMax Laier target->ia_flags |= IFA_ROUTE; 717460473a0SBjoern A. Zeeb return (error); 71848321abeSMax Laier } 71948321abeSMax Laier 72048321abeSMax Laier /* 72148321abeSMax Laier * If there is no other address in the system that can serve a route to the 72248321abeSMax Laier * same prefix, remove the route. Hand over the route to the new address 72348321abeSMax Laier * otherwise. 72448321abeSMax Laier */ 72508b68b0eSGleb Smirnoff int 7265b84dc78SQing Li in_scrubprefix(struct in_ifaddr *target, u_int flags) 72748321abeSMax Laier { 72848321abeSMax Laier struct in_ifaddr *ia; 72955174c34SGleb Smirnoff struct in_addr prefix, mask, p, m; 7307278b62aSAlan Somers int error = 0; 731c9d763bfSQing Li struct sockaddr_in prefix0, mask0; 73248321abeSMax Laier 733df813b7eSQing Li /* 734df813b7eSQing Li * Remove the loopback route to the interface address. 735df813b7eSQing Li */ 73692fac994SQing Li if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) && 737c7ab6602SQing Li !(target->ia_ifp->if_flags & IFF_LOOPBACK) && 738f7a39160SGleb Smirnoff (flags & LLE_STATIC)) { 739f7a39160SGleb Smirnoff struct in_ifaddr *eia; 740c7ab6602SQing Li 741f7a39160SGleb Smirnoff eia = in_localip_more(target); 742f7a39160SGleb Smirnoff 743f7a39160SGleb Smirnoff if (eia != NULL) { 7447278b62aSAlan Somers int fibnum = target->ia_ifp->if_fib; 7457278b62aSAlan Somers 746f7a39160SGleb Smirnoff error = ifa_switch_loopback_route((struct ifaddr *)eia, 7470489b891SAlan Somers (struct sockaddr *)&target->ia_addr, fibnum); 748f7a39160SGleb Smirnoff ifa_free(&eia->ia_ifa); 749f7a39160SGleb Smirnoff } else { 7509bb7d0f4SQing Li error = ifa_del_loopback_route((struct ifaddr *)target, 7519bb7d0f4SQing Li (struct sockaddr *)&target->ia_addr); 7525b84dc78SQing Li } 753f7a39160SGleb Smirnoff 754f7a39160SGleb Smirnoff if (!(target->ia_ifp->if_flags & IFF_NOARP)) 755fa3cfd39SQing Li /* remove arp cache */ 756f7a39160SGleb Smirnoff arp_ifscrub(target->ia_ifp, 757f7a39160SGleb Smirnoff IA_SIN(target)->sin_addr.s_addr); 758ebc90701SQing Li } 759ebc90701SQing Li 76055174c34SGleb Smirnoff if (rtinitflags(target)) { 76148321abeSMax Laier prefix = target->ia_dstaddr.sin_addr; 76255174c34SGleb Smirnoff mask.s_addr = 0; 76355174c34SGleb Smirnoff } else { 76448321abeSMax Laier prefix = target->ia_addr.sin_addr; 76548321abeSMax Laier mask = target->ia_sockmask.sin_addr; 76648321abeSMax Laier prefix.s_addr &= mask.s_addr; 76748321abeSMax Laier } 76848321abeSMax Laier 769ccbb9c35SQing Li if ((target->ia_flags & IFA_ROUTE) == 0) { 7707278b62aSAlan Somers int fibnum; 7717278b62aSAlan Somers 772cc45ae40SHiroki Sato fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS : 7737278b62aSAlan Somers target->ia_ifp->if_fib; 774d375edc9SAlexander V. Chernikov rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum); 775ccbb9c35SQing Li return (0); 776ccbb9c35SQing Li } 777ccbb9c35SQing Li 7782d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 779603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 78055174c34SGleb Smirnoff if (rtinitflags(ia)) { 78148321abeSMax Laier p = ia->ia_dstaddr.sin_addr; 78255174c34SGleb Smirnoff 78355174c34SGleb Smirnoff if (prefix.s_addr != p.s_addr) 78455174c34SGleb Smirnoff continue; 78555174c34SGleb Smirnoff } else { 78648321abeSMax Laier p = ia->ia_addr.sin_addr; 78755174c34SGleb Smirnoff m = ia->ia_sockmask.sin_addr; 78855174c34SGleb Smirnoff p.s_addr &= m.s_addr; 78955174c34SGleb Smirnoff 79055174c34SGleb Smirnoff if (prefix.s_addr != p.s_addr || 79155174c34SGleb Smirnoff mask.s_addr != m.s_addr) 79255174c34SGleb Smirnoff continue; 79348321abeSMax Laier } 79448321abeSMax Laier 79555174c34SGleb Smirnoff if ((ia->ia_ifp->if_flags & IFF_UP) == 0) 79648321abeSMax Laier continue; 79748321abeSMax Laier 79848321abeSMax Laier /* 79948321abeSMax Laier * If we got a matching prefix address, move IFA_ROUTE and 80048321abeSMax Laier * the route itself to it. Make sure that routing daemons 80148321abeSMax Laier * get a heads-up. 80248321abeSMax Laier */ 80308b68b0eSGleb Smirnoff if ((ia->ia_flags & IFA_ROUTE) == 0) { 80479d51435SSergey Kandaurov ifa_ref(&ia->ia_ifa); 8052d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 80692322284SQing Li error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, 80748321abeSMax Laier rtinitflags(target)); 80892322284SQing Li if (error == 0) 80948321abeSMax Laier target->ia_flags &= ~IFA_ROUTE; 81092322284SQing Li else 81192322284SQing Li log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n", 81292322284SQing Li error); 81348321abeSMax Laier error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 81448321abeSMax Laier rtinitflags(ia) | RTF_UP); 81548321abeSMax Laier if (error == 0) 81648321abeSMax Laier ia->ia_flags |= IFA_ROUTE; 81792322284SQing Li else 81892322284SQing Li log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n", 81992322284SQing Li error); 82079d51435SSergey Kandaurov ifa_free(&ia->ia_ifa); 821460473a0SBjoern A. Zeeb return (error); 82248321abeSMax Laier } 82348321abeSMax Laier } 8242d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 82548321abeSMax Laier 82648321abeSMax Laier /* 827c9d763bfSQing Li * remove all L2 entries on the given prefix 828c9d763bfSQing Li */ 829c9d763bfSQing Li bzero(&prefix0, sizeof(prefix0)); 830c9d763bfSQing Li prefix0.sin_len = sizeof(prefix0); 831c9d763bfSQing Li prefix0.sin_family = AF_INET; 832c9d763bfSQing Li prefix0.sin_addr.s_addr = target->ia_subnet; 833c9d763bfSQing Li bzero(&mask0, sizeof(mask0)); 834c9d763bfSQing Li mask0.sin_len = sizeof(mask0); 835c9d763bfSQing Li mask0.sin_family = AF_INET; 836c9d763bfSQing Li mask0.sin_addr.s_addr = target->ia_subnetmask; 837c9d763bfSQing Li lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0, 8385b84dc78SQing Li (struct sockaddr *)&mask0, flags); 839c9d763bfSQing Li 840c9d763bfSQing Li /* 84148321abeSMax Laier * As no-one seem to have this prefix, we can remove the route. 84248321abeSMax Laier */ 84392322284SQing Li error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 84492322284SQing Li if (error == 0) 84548321abeSMax Laier target->ia_flags &= ~IFA_ROUTE; 84692322284SQing Li else 84792322284SQing Li log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error); 84892322284SQing Li return (error); 84948321abeSMax Laier } 85048321abeSMax Laier 85148321abeSMax Laier #undef rtinitflags 852df8bae1dSRodney W. Grimes 853df8bae1dSRodney W. Grimes /* 854df8bae1dSRodney W. Grimes * Return 1 if the address might be a local broadcast address. 855df8bae1dSRodney W. Grimes */ 85626f9a767SRodney W. Grimes int 857f2565d68SRobert Watson in_broadcast(struct in_addr in, struct ifnet *ifp) 858df8bae1dSRodney W. Grimes { 859df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 860df8bae1dSRodney W. Grimes u_long t; 861df8bae1dSRodney W. Grimes 862df8bae1dSRodney W. Grimes if (in.s_addr == INADDR_BROADCAST || 863df8bae1dSRodney W. Grimes in.s_addr == INADDR_ANY) 864460473a0SBjoern A. Zeeb return (1); 865df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 866460473a0SBjoern A. Zeeb return (0); 867df8bae1dSRodney W. Grimes t = ntohl(in.s_addr); 868df8bae1dSRodney W. Grimes /* 869df8bae1dSRodney W. Grimes * Look through the list of addresses for a match 870df8bae1dSRodney W. Grimes * with a broadcast address. 871df8bae1dSRodney W. Grimes */ 872df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa) 873462b86feSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 874df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family == AF_INET && 875df8bae1dSRodney W. Grimes (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 876df8bae1dSRodney W. Grimes /* 87753883e0cSGleb Smirnoff * Check for old-style (host 0) broadcast, but 87853883e0cSGleb Smirnoff * taking into account that RFC 3021 obsoletes it. 879df8bae1dSRodney W. Grimes */ 88053883e0cSGleb Smirnoff (ia->ia_subnetmask != IN_RFC3021_MASK && 88153883e0cSGleb Smirnoff t == ia->ia_subnet)) && 8828dd27fd6SGuido van Rooij /* 8838dd27fd6SGuido van Rooij * Check for an all one subnetmask. These 8848dd27fd6SGuido van Rooij * only exist when an interface gets a secondary 8858dd27fd6SGuido van Rooij * address. 8868dd27fd6SGuido van Rooij */ 8878dd27fd6SGuido van Rooij ia->ia_subnetmask != (u_long)0xffffffff) 888460473a0SBjoern A. Zeeb return (1); 889df8bae1dSRodney W. Grimes return (0); 890df8bae1dSRodney W. Grimes #undef ia 891df8bae1dSRodney W. Grimes } 892ec002feeSBruce M Simpson 893df8bae1dSRodney W. Grimes /* 894b1c53bc9SRobert Watson * On interface removal, clean up IPv4 data structures hung off of the ifnet. 895b1c53bc9SRobert Watson */ 896b1c53bc9SRobert Watson void 897f2565d68SRobert Watson in_ifdetach(struct ifnet *ifp) 898b1c53bc9SRobert Watson { 899b1c53bc9SRobert Watson 900603724d3SBjoern A. Zeeb in_pcbpurgeif0(&V_ripcbinfo, ifp); 901603724d3SBjoern A. Zeeb in_pcbpurgeif0(&V_udbinfo, ifp); 902e06e816fSKevin Lo in_pcbpurgeif0(&V_ulitecbinfo, ifp); 903ec002feeSBruce M Simpson in_purgemaddrs(ifp); 904b1c53bc9SRobert Watson } 9056e6b3f7cSQing Li 906d10910e6SBruce M Simpson /* 907d10910e6SBruce M Simpson * Delete all IPv4 multicast address records, and associated link-layer 908d10910e6SBruce M Simpson * multicast address records, associated with ifp. 909d10910e6SBruce M Simpson * XXX It looks like domifdetach runs AFTER the link layer cleanup. 91056663a40SBruce M Simpson * XXX This should not race with ifma_protospec being set during 91156663a40SBruce M Simpson * a new allocation, if it does, we have bigger problems. 912d10910e6SBruce M Simpson */ 913d10910e6SBruce M Simpson static void 914d10910e6SBruce M Simpson in_purgemaddrs(struct ifnet *ifp) 915d10910e6SBruce M Simpson { 916d10910e6SBruce M Simpson LIST_HEAD(,in_multi) purgeinms; 917d10910e6SBruce M Simpson struct in_multi *inm, *tinm; 918d10910e6SBruce M Simpson struct ifmultiaddr *ifma; 919d10910e6SBruce M Simpson 920d10910e6SBruce M Simpson LIST_INIT(&purgeinms); 921d10910e6SBruce M Simpson IN_MULTI_LOCK(); 922d10910e6SBruce M Simpson 923d10910e6SBruce M Simpson /* 924d10910e6SBruce M Simpson * Extract list of in_multi associated with the detaching ifp 925d10910e6SBruce M Simpson * which the PF_INET layer is about to release. 926d10910e6SBruce M Simpson * We need to do this as IF_ADDR_LOCK() may be re-acquired 927d10910e6SBruce M Simpson * by code further down. 928d10910e6SBruce M Simpson */ 929137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 930d10910e6SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 93156663a40SBruce M Simpson if (ifma->ifma_addr->sa_family != AF_INET || 93256663a40SBruce M Simpson ifma->ifma_protospec == NULL) 933d10910e6SBruce M Simpson continue; 93456663a40SBruce M Simpson #if 0 93556663a40SBruce M Simpson KASSERT(ifma->ifma_protospec != NULL, 93656663a40SBruce M Simpson ("%s: ifma_protospec is NULL", __func__)); 93756663a40SBruce M Simpson #endif 938d10910e6SBruce M Simpson inm = (struct in_multi *)ifma->ifma_protospec; 939d10910e6SBruce M Simpson LIST_INSERT_HEAD(&purgeinms, inm, inm_link); 940d10910e6SBruce M Simpson } 941137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 942d10910e6SBruce M Simpson 943d10910e6SBruce M Simpson LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) { 944d10910e6SBruce M Simpson LIST_REMOVE(inm, inm_link); 94507cde5e9SRobert Watson inm_release_locked(inm); 946d10910e6SBruce M Simpson } 947d10910e6SBruce M Simpson igmp_ifdetach(ifp); 948d10910e6SBruce M Simpson 949d10910e6SBruce M Simpson IN_MULTI_UNLOCK(); 950d10910e6SBruce M Simpson } 951d10910e6SBruce M Simpson 9526e6b3f7cSQing Li struct in_llentry { 9536e6b3f7cSQing Li struct llentry base; 9546e6b3f7cSQing Li struct sockaddr_in l3_addr4; 9556e6b3f7cSQing Li }; 9566e6b3f7cSQing Li 957a93cda78SKip Macy /* 958a93cda78SKip Macy * Deletes an address from the address table. 959a93cda78SKip Macy * This function is called by the timer functions 960a93cda78SKip Macy * such as arptimer() and nd6_llinfo_timer(), and 961a93cda78SKip Macy * the caller does the locking. 962a93cda78SKip Macy */ 963a93cda78SKip Macy static void 964a93cda78SKip Macy in_lltable_free(struct lltable *llt, struct llentry *lle) 965a93cda78SKip Macy { 966a93cda78SKip Macy LLE_WUNLOCK(lle); 967a93cda78SKip Macy LLE_LOCK_DESTROY(lle); 968a93cda78SKip Macy free(lle, M_LLTABLE); 969a93cda78SKip Macy } 970a93cda78SKip Macy 9716e6b3f7cSQing Li static struct llentry * 9726e6b3f7cSQing Li in_lltable_new(const struct sockaddr *l3addr, u_int flags) 9736e6b3f7cSQing Li { 9746e6b3f7cSQing Li struct in_llentry *lle; 9756e6b3f7cSQing Li 97690b357f6SGleb Smirnoff lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO); 9776e6b3f7cSQing Li if (lle == NULL) /* NB: caller generates msg */ 9786e6b3f7cSQing Li return NULL; 9796e6b3f7cSQing Li 9806e6b3f7cSQing Li /* 9816e6b3f7cSQing Li * For IPv4 this will trigger "arpresolve" to generate 9826e6b3f7cSQing Li * an ARP request. 9836e6b3f7cSQing Li */ 984a98c06f1SGleb Smirnoff lle->base.la_expire = time_uptime; /* mark expired */ 9856e6b3f7cSQing Li lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 9866e6b3f7cSQing Li lle->base.lle_refcnt = 1; 987a93cda78SKip Macy lle->base.lle_free = in_lltable_free; 9886e6b3f7cSQing Li LLE_LOCK_INIT(&lle->base); 9892575fbb8SRandall Stewart callout_init(&lle->base.la_timer, 1); 990ea537929SGleb Smirnoff 991ea537929SGleb Smirnoff return (&lle->base); 9926e6b3f7cSQing Li } 9936e6b3f7cSQing Li 994c9d763bfSQing Li #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 995c9d763bfSQing Li (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 ) 996c9d763bfSQing Li 997c9d763bfSQing Li static void 998ea50c13eSGleb Smirnoff in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix, 999ea50c13eSGleb Smirnoff const struct sockaddr *mask, u_int flags) 1000c9d763bfSQing Li { 1001c9d763bfSQing Li const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix; 1002c9d763bfSQing Li const struct sockaddr_in *msk = (const struct sockaddr_in *)mask; 1003c9d763bfSQing Li struct llentry *lle, *next; 1004ea50c13eSGleb Smirnoff int i; 1005e162ea60SGeorge V. Neville-Neil size_t pkts_dropped; 1006c9d763bfSQing Li 1007ea537929SGleb Smirnoff IF_AFDATA_WLOCK(llt->llt_ifp); 1008c9d763bfSQing Li for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 1009c9d763bfSQing Li LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 10105b84dc78SQing Li /* 10115b84dc78SQing Li * (flags & LLE_STATIC) means deleting all entries 1012ea50c13eSGleb Smirnoff * including static ARP entries. 10135b84dc78SQing Li */ 1014ea50c13eSGleb Smirnoff if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)), 1015ea50c13eSGleb Smirnoff pfx, msk) && ((flags & LLE_STATIC) || 1016ea50c13eSGleb Smirnoff !(lle->la_flags & LLE_STATIC))) { 1017c9d763bfSQing Li LLE_WLOCK(lle); 1018ea537929SGleb Smirnoff if (callout_stop(&lle->la_timer)) 1019becba438SBjoern A. Zeeb LLE_REMREF(lle); 1020e162ea60SGeorge V. Neville-Neil pkts_dropped = llentry_free(lle); 1021e162ea60SGeorge V. Neville-Neil ARPSTAT_ADD(dropped, pkts_dropped); 1022c9d763bfSQing Li } 1023c9d763bfSQing Li } 1024c9d763bfSQing Li } 1025ea537929SGleb Smirnoff IF_AFDATA_WUNLOCK(llt->llt_ifp); 1026c9d763bfSQing Li } 1027c9d763bfSQing Li 1028c9d763bfSQing Li 10296e6b3f7cSQing Li static int 1030c7ab6602SQing Li in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr) 10316e6b3f7cSQing Li { 10326e6b3f7cSQing Li struct rtentry *rt; 10336e6b3f7cSQing Li 10346e6b3f7cSQing Li KASSERT(l3addr->sa_family == AF_INET, 10356e6b3f7cSQing Li ("sin_family %d", l3addr->sa_family)); 10366e6b3f7cSQing Li 1037743c072aSAlan Somers /* XXX rtalloc1_fib should take a const param */ 1038743c072aSAlan Somers rt = rtalloc1_fib(__DECONST(struct sockaddr *, l3addr), 0, 0, 1039743c072aSAlan Somers ifp->if_fib); 104013e255faSMarko Zec 10416cf8e330SQing Li if (rt == NULL) 10426cf8e330SQing Li return (EINVAL); 10436cf8e330SQing Li 104413e255faSMarko Zec /* 104513e255faSMarko Zec * If the gateway for an existing host route matches the target L3 10466cf8e330SQing Li * address, which is a special route inserted by some implementation 10476cf8e330SQing Li * such as MANET, and the interface is of the correct type, then 10486cf8e330SQing Li * allow for ARP to proceed. 104913e255faSMarko Zec */ 1050db92413eSQing Li if (rt->rt_flags & RTF_GATEWAY) { 105115d25219SQing Li if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp || 105215d25219SQing Li rt->rt_ifp->if_type != IFT_ETHER || 1053ea50c13eSGleb Smirnoff (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 || 105415d25219SQing Li memcmp(rt->rt_gateway->sa_data, l3addr->sa_data, 105515d25219SQing Li sizeof(in_addr_t)) != 0) { 1056db92413eSQing Li RTFREE_LOCKED(rt); 1057db92413eSQing Li return (EINVAL); 1058db92413eSQing Li } 105915d25219SQing Li } 1060db92413eSQing Li 1061db92413eSQing Li /* 1062db92413eSQing Li * Make sure that at least the destination address is covered 1063db92413eSQing Li * by the route. This is for handling the case where 2 or more 1064db92413eSQing Li * interfaces have the same prefix. An incoming packet arrives 1065db92413eSQing Li * on one interface and the corresponding outgoing packet leaves 1066db92413eSQing Li * another interface. 1067db92413eSQing Li */ 1068b3664a14SQing Li if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) { 106915d25219SQing Li const char *sa, *mask, *addr, *lim; 1070db92413eSQing Li int len; 1071db92413eSQing Li 107215d25219SQing Li mask = (const char *)rt_mask(rt); 1073b3664a14SQing Li /* 1074b3664a14SQing Li * Just being extra cautious to avoid some custom 1075b3664a14SQing Li * code getting into trouble. 1076b3664a14SQing Li */ 1077b3664a14SQing Li if (mask == NULL) { 1078b3664a14SQing Li RTFREE_LOCKED(rt); 1079b3664a14SQing Li return (EINVAL); 1080b3664a14SQing Li } 1081b3664a14SQing Li 1082b3664a14SQing Li sa = (const char *)rt_key(rt); 108315d25219SQing Li addr = (const char *)l3addr; 108415d25219SQing Li len = ((const struct sockaddr_in *)l3addr)->sin_len; 1085db92413eSQing Li lim = addr + len; 1086db92413eSQing Li 1087db92413eSQing Li for ( ; addr < lim; sa++, mask++, addr++) { 1088db92413eSQing Li if ((*sa ^ *addr) & *mask) { 10896cb2b4e7SQing Li #ifdef DIAGNOSTIC 10906e6b3f7cSQing Li log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 10916e6b3f7cSQing Li inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1092b4a22c36SQing Li #endif 109315d25219SQing Li RTFREE_LOCKED(rt); 109415d25219SQing Li return (EINVAL); 10956e6b3f7cSQing Li } 1096db92413eSQing Li } 1097db92413eSQing Li } 1098db92413eSQing Li 10996e6b3f7cSQing Li RTFREE_LOCKED(rt); 110015d25219SQing Li return (0); 11016e6b3f7cSQing Li } 11026e6b3f7cSQing Li 11036e6b3f7cSQing Li /* 11046e6b3f7cSQing Li * Return NULL if not found or marked for deletion. 11056e6b3f7cSQing Li * If found return lle read locked. 11066e6b3f7cSQing Li */ 11076e6b3f7cSQing Li static struct llentry * 11086e6b3f7cSQing Li in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 11096e6b3f7cSQing Li { 11106e6b3f7cSQing Li const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 11116e6b3f7cSQing Li struct ifnet *ifp = llt->llt_ifp; 11126e6b3f7cSQing Li struct llentry *lle; 11136e6b3f7cSQing Li struct llentries *lleh; 11146e6b3f7cSQing Li u_int hashkey; 11156e6b3f7cSQing Li 11166e6b3f7cSQing Li IF_AFDATA_LOCK_ASSERT(ifp); 11176e6b3f7cSQing Li KASSERT(l3addr->sa_family == AF_INET, 11186e6b3f7cSQing Li ("sin_family %d", l3addr->sa_family)); 11196e6b3f7cSQing Li 11206e6b3f7cSQing Li hashkey = sin->sin_addr.s_addr; 11216e6b3f7cSQing Li lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 11226e6b3f7cSQing Li LIST_FOREACH(lle, lleh, lle_next) { 1123ea50c13eSGleb Smirnoff struct sockaddr_in *sa2 = satosin(L3_ADDR(lle)); 11246e6b3f7cSQing Li if (lle->la_flags & LLE_DELETED) 11256e6b3f7cSQing Li continue; 1126dc495497SQing Li if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr) 11276e6b3f7cSQing Li break; 11286e6b3f7cSQing Li } 11296e6b3f7cSQing Li if (lle == NULL) { 11306cb2b4e7SQing Li #ifdef DIAGNOSTIC 11316e6b3f7cSQing Li if (flags & LLE_DELETE) 11326e6b3f7cSQing Li log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 11336e6b3f7cSQing Li #endif 11346e6b3f7cSQing Li if (!(flags & LLE_CREATE)) 11356e6b3f7cSQing Li return (NULL); 1136e2d14d93SAndrey V. Elsukov IF_AFDATA_WLOCK_ASSERT(ifp); 11376e6b3f7cSQing Li /* 11386e6b3f7cSQing Li * A route that covers the given address must have 11396e6b3f7cSQing Li * been installed 1st because we are doing a resolution, 11406e6b3f7cSQing Li * verify this. 11416e6b3f7cSQing Li */ 11426e6b3f7cSQing Li if (!(flags & LLE_IFADDR) && 1143c7ab6602SQing Li in_lltable_rtcheck(ifp, flags, l3addr) != 0) 11446e6b3f7cSQing Li goto done; 11456e6b3f7cSQing Li 11466e6b3f7cSQing Li lle = in_lltable_new(l3addr, flags); 11476e6b3f7cSQing Li if (lle == NULL) { 11486e6b3f7cSQing Li log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 11496e6b3f7cSQing Li goto done; 11506e6b3f7cSQing Li } 11516e6b3f7cSQing Li lle->la_flags = flags & ~LLE_CREATE; 11526e6b3f7cSQing Li if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 11536e6b3f7cSQing Li bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 11546e6b3f7cSQing Li lle->la_flags |= (LLE_VALID | LLE_STATIC); 11556e6b3f7cSQing Li } 11566e6b3f7cSQing Li 11576e6b3f7cSQing Li lle->lle_tbl = llt; 11586e6b3f7cSQing Li lle->lle_head = lleh; 1159ea537929SGleb Smirnoff lle->la_flags |= LLE_LINKED; 11606e6b3f7cSQing Li LIST_INSERT_HEAD(lleh, lle, lle_next); 11616e6b3f7cSQing Li } else if (flags & LLE_DELETE) { 11626e6b3f7cSQing Li if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 11636e6b3f7cSQing Li LLE_WLOCK(lle); 1164ea537929SGleb Smirnoff lle->la_flags |= LLE_DELETED; 116509fe6320SNavdeep Parhar EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED); 11666cb2b4e7SQing Li #ifdef DIAGNOSTIC 11676e6b3f7cSQing Li log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 11686e6b3f7cSQing Li #endif 11691571132fSOleg Bulyzhin if ((lle->la_flags & 11701571132fSOleg Bulyzhin (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC) 11711571132fSOleg Bulyzhin llentry_free(lle); 11721571132fSOleg Bulyzhin else 11731571132fSOleg Bulyzhin LLE_WUNLOCK(lle); 11746e6b3f7cSQing Li } 11756e6b3f7cSQing Li lle = (void *)-1; 11766e6b3f7cSQing Li 11776e6b3f7cSQing Li } 117842d866ddSBjoern A. Zeeb if (LLE_IS_VALID(lle)) { 11796e6b3f7cSQing Li if (flags & LLE_EXCLUSIVE) 11806e6b3f7cSQing Li LLE_WLOCK(lle); 11816e6b3f7cSQing Li else 11826e6b3f7cSQing Li LLE_RLOCK(lle); 11836e6b3f7cSQing Li } 11846e6b3f7cSQing Li done: 11856e6b3f7cSQing Li return (lle); 11866e6b3f7cSQing Li } 11876e6b3f7cSQing Li 11886e6b3f7cSQing Li static int 11896e6b3f7cSQing Li in_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 11906e6b3f7cSQing Li { 11916e6b3f7cSQing Li #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 11926e6b3f7cSQing Li struct ifnet *ifp = llt->llt_ifp; 11936e6b3f7cSQing Li struct llentry *lle; 11946e6b3f7cSQing Li /* XXX stack use */ 11956e6b3f7cSQing Li struct { 11966e6b3f7cSQing Li struct rt_msghdr rtm; 11979711a168SGleb Smirnoff struct sockaddr_in sin; 11986e6b3f7cSQing Li struct sockaddr_dl sdl; 11996e6b3f7cSQing Li } arpc; 12006e6b3f7cSQing Li int error, i; 12016e6b3f7cSQing Li 1202dc56e98fSRobert Watson LLTABLE_LOCK_ASSERT(); 12036e6b3f7cSQing Li 12046e6b3f7cSQing Li error = 0; 12056e6b3f7cSQing Li for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 12066e6b3f7cSQing Li LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 12076e6b3f7cSQing Li struct sockaddr_dl *sdl; 12086e6b3f7cSQing Li 12096e6b3f7cSQing Li /* skip deleted entries */ 121093704ac5SQing Li if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) 12116e6b3f7cSQing Li continue; 1212813dd6aeSBjoern A. Zeeb /* Skip if jailed and not a valid IP of the prison. */ 1213b89e82ddSJamie Gritton if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 1214813dd6aeSBjoern A. Zeeb continue; 12156e6b3f7cSQing Li /* 12166e6b3f7cSQing Li * produce a msg made of: 12176e6b3f7cSQing Li * struct rt_msghdr; 12189711a168SGleb Smirnoff * struct sockaddr_in; (IPv4) 12196e6b3f7cSQing Li * struct sockaddr_dl; 12206e6b3f7cSQing Li */ 12216e6b3f7cSQing Li bzero(&arpc, sizeof(arpc)); 12226e6b3f7cSQing Li arpc.rtm.rtm_msglen = sizeof(arpc); 1223c0e9a8a1SHartmut Brandt arpc.rtm.rtm_version = RTM_VERSION; 1224c0e9a8a1SHartmut Brandt arpc.rtm.rtm_type = RTM_GET; 1225c0e9a8a1SHartmut Brandt arpc.rtm.rtm_flags = RTF_UP; 1226c0e9a8a1SHartmut Brandt arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 12276e6b3f7cSQing Li arpc.sin.sin_family = AF_INET; 12286e6b3f7cSQing Li arpc.sin.sin_len = sizeof(arpc.sin); 12296e6b3f7cSQing Li arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 12306e6b3f7cSQing Li 12316e6b3f7cSQing Li /* publish */ 12329711a168SGleb Smirnoff if (lle->la_flags & LLE_PUB) 12336e6b3f7cSQing Li arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 12346e6b3f7cSQing Li 12356e6b3f7cSQing Li sdl = &arpc.sdl; 12366e6b3f7cSQing Li sdl->sdl_family = AF_LINK; 12376e6b3f7cSQing Li sdl->sdl_len = sizeof(*sdl); 12386e6b3f7cSQing Li sdl->sdl_index = ifp->if_index; 12396e6b3f7cSQing Li sdl->sdl_type = ifp->if_type; 124093704ac5SQing Li if ((lle->la_flags & LLE_VALID) == LLE_VALID) { 124193704ac5SQing Li sdl->sdl_alen = ifp->if_addrlen; 12426e6b3f7cSQing Li bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 124393704ac5SQing Li } else { 124493704ac5SQing Li sdl->sdl_alen = 0; 124593704ac5SQing Li bzero(LLADDR(sdl), ifp->if_addrlen); 124693704ac5SQing Li } 12476e6b3f7cSQing Li 12486e6b3f7cSQing Li arpc.rtm.rtm_rmx.rmx_expire = 12496e6b3f7cSQing Li lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 12508eca593cSQing Li arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 12516e6b3f7cSQing Li if (lle->la_flags & LLE_STATIC) 12526e6b3f7cSQing Li arpc.rtm.rtm_flags |= RTF_STATIC; 12536e6b3f7cSQing Li arpc.rtm.rtm_index = ifp->if_index; 12546e6b3f7cSQing Li error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 12556e6b3f7cSQing Li if (error) 12566e6b3f7cSQing Li break; 12576e6b3f7cSQing Li } 12586e6b3f7cSQing Li } 12596e6b3f7cSQing Li return error; 12606e6b3f7cSQing Li #undef SIN 12616e6b3f7cSQing Li } 12626e6b3f7cSQing Li 12636e6b3f7cSQing Li void * 12646e6b3f7cSQing Li in_domifattach(struct ifnet *ifp) 12656e6b3f7cSQing Li { 1266d10910e6SBruce M Simpson struct in_ifinfo *ii; 1267d10910e6SBruce M Simpson struct lltable *llt; 12686e6b3f7cSQing Li 1269d10910e6SBruce M Simpson ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1270d10910e6SBruce M Simpson 1271d10910e6SBruce M Simpson llt = lltable_init(ifp, AF_INET); 12726e6b3f7cSQing Li if (llt != NULL) { 1273c9d763bfSQing Li llt->llt_prefix_free = in_lltable_prefix_free; 12746e6b3f7cSQing Li llt->llt_lookup = in_lltable_lookup; 12756e6b3f7cSQing Li llt->llt_dump = in_lltable_dump; 12766e6b3f7cSQing Li } 1277d10910e6SBruce M Simpson ii->ii_llt = llt; 1278d10910e6SBruce M Simpson 1279d10910e6SBruce M Simpson ii->ii_igmp = igmp_domifattach(ifp); 1280d10910e6SBruce M Simpson 1281d10910e6SBruce M Simpson return ii; 12826e6b3f7cSQing Li } 12836e6b3f7cSQing Li 12846e6b3f7cSQing Li void 1285d10910e6SBruce M Simpson in_domifdetach(struct ifnet *ifp, void *aux) 12866e6b3f7cSQing Li { 1287d10910e6SBruce M Simpson struct in_ifinfo *ii = (struct in_ifinfo *)aux; 12886e6b3f7cSQing Li 1289d10910e6SBruce M Simpson igmp_domifdetach(ifp); 1290d10910e6SBruce M Simpson lltable_free(ii->ii_llt); 1291d10910e6SBruce M Simpson free(ii, M_IFADDR); 12926e6b3f7cSQing Li } 1293