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) 7908b68b0eSGleb Smirnoff SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, no_same_prefix, 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 /* 131f7a39160SGleb Smirnoff * Return a reference to the interface address which is different to 132f7a39160SGleb Smirnoff * the supplied one but with same IP address value. 133f7a39160SGleb Smirnoff */ 134f7a39160SGleb Smirnoff static struct in_ifaddr * 135f7a39160SGleb Smirnoff in_localip_more(struct in_ifaddr *ia) 136f7a39160SGleb Smirnoff { 137f7a39160SGleb Smirnoff in_addr_t in = IA_SIN(ia)->sin_addr.s_addr; 138f7a39160SGleb Smirnoff struct in_ifaddr *it; 139f7a39160SGleb Smirnoff 140f7a39160SGleb Smirnoff IN_IFADDR_RLOCK(); 141f7a39160SGleb Smirnoff LIST_FOREACH(it, INADDR_HASH(in), ia_hash) { 142f7a39160SGleb Smirnoff if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) { 143f7a39160SGleb Smirnoff ifa_ref(&it->ia_ifa); 144f7a39160SGleb Smirnoff IN_IFADDR_RUNLOCK(); 145f7a39160SGleb Smirnoff return (it); 146f7a39160SGleb Smirnoff } 147f7a39160SGleb Smirnoff } 148f7a39160SGleb Smirnoff IN_IFADDR_RUNLOCK(); 149f7a39160SGleb Smirnoff 150f7a39160SGleb Smirnoff return (NULL); 151f7a39160SGleb Smirnoff } 152f7a39160SGleb Smirnoff 153f7a39160SGleb Smirnoff /* 154df8bae1dSRodney W. Grimes * Determine whether an IP address is in a reserved set of addresses 155df8bae1dSRodney W. Grimes * that may not be forwarded, or whether datagrams to that destination 156df8bae1dSRodney W. Grimes * may be forwarded. 157df8bae1dSRodney W. Grimes */ 15826f9a767SRodney W. Grimes int 159f2565d68SRobert Watson in_canforward(struct in_addr in) 160df8bae1dSRodney W. Grimes { 161df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 162df8bae1dSRodney W. Grimes register u_long net; 163df8bae1dSRodney W. Grimes 164f8429ca2SBruce M Simpson if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i)) 165df8bae1dSRodney W. Grimes return (0); 166df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) { 167df8bae1dSRodney W. Grimes net = i & IN_CLASSA_NET; 168df8bae1dSRodney W. Grimes if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 169df8bae1dSRodney W. Grimes return (0); 170df8bae1dSRodney W. Grimes } 171df8bae1dSRodney W. Grimes return (1); 172df8bae1dSRodney W. Grimes } 173df8bae1dSRodney W. Grimes 174df8bae1dSRodney W. Grimes /* 175df8bae1dSRodney W. Grimes * Trim a mask in a sockaddr 176df8bae1dSRodney W. Grimes */ 1770312fbe9SPoul-Henning Kamp static void 178f2565d68SRobert Watson in_socktrim(struct sockaddr_in *ap) 179df8bae1dSRodney W. Grimes { 180df8bae1dSRodney W. Grimes register char *cplim = (char *) &ap->sin_addr; 181df8bae1dSRodney W. Grimes register char *cp = (char *) (&ap->sin_addr + 1); 182df8bae1dSRodney W. Grimes 183df8bae1dSRodney W. Grimes ap->sin_len = 0; 184df00058dSGarrett Wollman while (--cp >= cplim) 185df8bae1dSRodney W. Grimes if (*cp) { 186df8bae1dSRodney W. Grimes (ap)->sin_len = cp - (char *) (ap) + 1; 187df8bae1dSRodney W. Grimes break; 188df8bae1dSRodney W. Grimes } 189df8bae1dSRodney W. Grimes } 190df8bae1dSRodney W. Grimes 191df8bae1dSRodney W. Grimes /* 192df8bae1dSRodney W. Grimes * Generic internet control operations (ioctl's). 193df8bae1dSRodney W. Grimes */ 19426f9a767SRodney W. Grimes int 195f2565d68SRobert Watson in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 196f2565d68SRobert Watson struct thread *td) 197df8bae1dSRodney W. Grimes { 198f7a39160SGleb Smirnoff struct ifreq *ifr = (struct ifreq *)data; 199f7a39160SGleb Smirnoff struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr; 200821b5cafSGleb Smirnoff struct ifaddr *ifa; 201f7a39160SGleb Smirnoff struct in_ifaddr *ia; 202f7a39160SGleb Smirnoff int error; 203f7a39160SGleb Smirnoff 204f7a39160SGleb Smirnoff if (ifp == NULL) 205f7a39160SGleb Smirnoff return (EADDRNOTAVAIL); 20671212473SGleb Smirnoff 20771212473SGleb Smirnoff /* 208f7a39160SGleb Smirnoff * Filter out 4 ioctls we implement directly. Forward the rest 209f7a39160SGleb Smirnoff * to specific functions and ifp->if_ioctl(). 210bbb3fb61SRobert Watson */ 2116a800098SYoshinobu Inoue switch (cmd) { 212bbb3fb61SRobert Watson case SIOCGIFADDR: 213bbb3fb61SRobert Watson case SIOCGIFBRDADDR: 214bbb3fb61SRobert Watson case SIOCGIFDSTADDR: 215bbb3fb61SRobert Watson case SIOCGIFNETMASK: 216f7a39160SGleb Smirnoff break; 2176d00fd9cSGleb Smirnoff case SIOCDIFADDR: 218f7a39160SGleb Smirnoff sx_xlock(&in_control_sx); 219f7a39160SGleb Smirnoff error = in_difaddr_ioctl(data, ifp, td); 220f7a39160SGleb Smirnoff sx_xunlock(&in_control_sx); 221f7a39160SGleb Smirnoff return (error); 22277b89ad8SGleb Smirnoff case OSIOCAIFADDR: /* 9.x compat */ 2236d00fd9cSGleb Smirnoff case SIOCAIFADDR: 224f7a39160SGleb Smirnoff sx_xlock(&in_control_sx); 22577b89ad8SGleb Smirnoff error = in_aifaddr_ioctl(cmd, data, ifp, td); 226f7a39160SGleb Smirnoff sx_xunlock(&in_control_sx); 227f7a39160SGleb Smirnoff return (error); 228bbb3fb61SRobert Watson case SIOCSIFADDR: 229bbb3fb61SRobert Watson case SIOCSIFBRDADDR: 230bbb3fb61SRobert Watson case SIOCSIFDSTADDR: 231bbb3fb61SRobert Watson case SIOCSIFNETMASK: 23256cf9dc1SGleb Smirnoff /* We no longer support that old commands. */ 2336d00fd9cSGleb Smirnoff return (EINVAL); 234bbb3fb61SRobert Watson default: 235f7a39160SGleb Smirnoff if (ifp->if_ioctl == NULL) 236bbb3fb61SRobert Watson return (EOPNOTSUPP); 237bbb3fb61SRobert Watson return ((*ifp->if_ioctl)(ifp, cmd, data)); 2386a800098SYoshinobu Inoue } 2396a800098SYoshinobu Inoue 240821b5cafSGleb Smirnoff if (addr->sin_addr.s_addr != INADDR_ANY && 241821b5cafSGleb Smirnoff prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0) 242821b5cafSGleb Smirnoff return (EADDRNOTAVAIL); 243821b5cafSGleb Smirnoff 244cf7b18f1SRobert Watson /* 245821b5cafSGleb Smirnoff * For SIOCGIFADDR, pick the first address. For the rest of 246821b5cafSGleb Smirnoff * ioctls, try to find specified address. 247df8bae1dSRodney W. Grimes */ 248821b5cafSGleb Smirnoff IF_ADDR_RLOCK(ifp); 249821b5cafSGleb Smirnoff TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2509706c950SGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET) 2519706c950SGleb Smirnoff continue; 252821b5cafSGleb Smirnoff ia = (struct in_ifaddr *)ifa; 253821b5cafSGleb Smirnoff if (cmd == SIOCGIFADDR || addr->sin_addr.s_addr == INADDR_ANY) 254821b5cafSGleb Smirnoff break; 255821b5cafSGleb Smirnoff if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr) 256df8bae1dSRodney W. Grimes break; 257ca925d9cSJonathan Lemon } 258f7a39160SGleb Smirnoff 259821b5cafSGleb Smirnoff if (ifa == NULL) { 260821b5cafSGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 261f7a39160SGleb Smirnoff return (EADDRNOTAVAIL); 262ac0aa473SBill Fenner } 263df8bae1dSRodney W. Grimes 264588885f2SRobert Watson error = 0; 265df8bae1dSRodney W. Grimes switch (cmd) { 266f7a39160SGleb Smirnoff case SIOCGIFADDR: 267f7a39160SGleb Smirnoff *addr = ia->ia_addr; 268f7a39160SGleb Smirnoff break; 2698c0fec80SRobert Watson 270f7a39160SGleb Smirnoff case SIOCGIFBRDADDR: 271f7a39160SGleb Smirnoff if ((ifp->if_flags & IFF_BROADCAST) == 0) { 272f7a39160SGleb Smirnoff error = EINVAL; 273df8bae1dSRodney W. Grimes break; 274df8bae1dSRodney W. Grimes } 275f7a39160SGleb Smirnoff *addr = ia->ia_broadaddr; 276f7a39160SGleb Smirnoff break; 277f7a39160SGleb Smirnoff 278f7a39160SGleb Smirnoff case SIOCGIFDSTADDR: 279f7a39160SGleb Smirnoff if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 280f7a39160SGleb Smirnoff error = EINVAL; 281f7a39160SGleb Smirnoff break; 282f7a39160SGleb Smirnoff } 283f7a39160SGleb Smirnoff *addr = ia->ia_dstaddr; 284f7a39160SGleb Smirnoff break; 285f7a39160SGleb Smirnoff 286f7a39160SGleb Smirnoff case SIOCGIFNETMASK: 287f7a39160SGleb Smirnoff *addr = ia->ia_sockmask; 288f7a39160SGleb Smirnoff break; 289f7a39160SGleb Smirnoff } 290f7a39160SGleb Smirnoff 291821b5cafSGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 292f7a39160SGleb Smirnoff 293f7a39160SGleb Smirnoff return (error); 2941067217dSGarrett Wollman } 295f7a39160SGleb Smirnoff 296f7a39160SGleb Smirnoff static int 29777b89ad8SGleb Smirnoff in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td) 298f7a39160SGleb Smirnoff { 299f7a39160SGleb Smirnoff const struct in_aliasreq *ifra = (struct in_aliasreq *)data; 300f7a39160SGleb Smirnoff const struct sockaddr_in *addr = &ifra->ifra_addr; 301f7a39160SGleb Smirnoff const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr; 302f7a39160SGleb Smirnoff const struct sockaddr_in *mask = &ifra->ifra_mask; 303f7a39160SGleb Smirnoff const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr; 30477b89ad8SGleb Smirnoff const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0; 305f7a39160SGleb Smirnoff struct ifaddr *ifa; 306f7a39160SGleb Smirnoff struct in_ifaddr *ia; 307f7a39160SGleb Smirnoff bool iaIsFirst; 308f7a39160SGleb Smirnoff int error = 0; 309f7a39160SGleb Smirnoff 310f7a39160SGleb Smirnoff error = priv_check(td, PRIV_NET_ADDIFADDR); 311f7a39160SGleb Smirnoff if (error) 312f7a39160SGleb Smirnoff return (error); 313f7a39160SGleb Smirnoff 314f7a39160SGleb Smirnoff /* 315f7a39160SGleb Smirnoff * ifra_addr must be present and be of INET family. 316f7a39160SGleb Smirnoff * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional. 317f7a39160SGleb Smirnoff */ 318f7a39160SGleb Smirnoff if (addr->sin_len != sizeof(struct sockaddr_in) || 319f7a39160SGleb Smirnoff addr->sin_family != AF_INET) 320f7a39160SGleb Smirnoff return (EINVAL); 321f7a39160SGleb Smirnoff if (broadaddr->sin_len != 0 && 322f7a39160SGleb Smirnoff (broadaddr->sin_len != sizeof(struct sockaddr_in) || 323f7a39160SGleb Smirnoff broadaddr->sin_family != AF_INET)) 324f7a39160SGleb Smirnoff return (EINVAL); 325f7a39160SGleb Smirnoff if (mask->sin_len != 0 && 326f7a39160SGleb Smirnoff (mask->sin_len != sizeof(struct sockaddr_in) || 327f7a39160SGleb Smirnoff mask->sin_family != AF_INET)) 328f7a39160SGleb Smirnoff return (EINVAL); 329f7a39160SGleb Smirnoff if ((ifp->if_flags & IFF_POINTOPOINT) && 330f7a39160SGleb Smirnoff (dstaddr->sin_len != sizeof(struct sockaddr_in) || 331f7a39160SGleb Smirnoff dstaddr->sin_addr.s_addr == INADDR_ANY)) 332f7a39160SGleb Smirnoff return (EDESTADDRREQ); 333f7a39160SGleb Smirnoff if (vhid > 0 && carp_attach_p == NULL) 334f7a39160SGleb Smirnoff return (EPROTONOSUPPORT); 335f7a39160SGleb Smirnoff 336f7a39160SGleb Smirnoff /* 337f7a39160SGleb Smirnoff * See whether address already exist. 338f7a39160SGleb Smirnoff */ 339f7a39160SGleb Smirnoff iaIsFirst = true; 340f7a39160SGleb Smirnoff ia = NULL; 341f7a39160SGleb Smirnoff IF_ADDR_RLOCK(ifp); 342f7a39160SGleb Smirnoff TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3439706c950SGleb Smirnoff struct in_ifaddr *it; 344f7a39160SGleb Smirnoff 3459706c950SGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET) 346f7a39160SGleb Smirnoff continue; 347f7a39160SGleb Smirnoff 3489706c950SGleb Smirnoff it = (struct in_ifaddr *)ifa; 349f7a39160SGleb Smirnoff iaIsFirst = false; 350f7a39160SGleb Smirnoff if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 351f7a39160SGleb Smirnoff prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) 352f7a39160SGleb Smirnoff ia = it; 3531067217dSGarrett Wollman } 354f7a39160SGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 355f7a39160SGleb Smirnoff 356f7a39160SGleb Smirnoff if (ia != NULL) 357f7a39160SGleb Smirnoff (void )in_difaddr_ioctl(data, ifp, td); 358f7a39160SGleb Smirnoff 35946758960SGleb Smirnoff ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK); 36046758960SGleb Smirnoff ia = (struct in_ifaddr *)ifa; 36159562606SGarrett Wollman ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 36259562606SGarrett Wollman ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 36359562606SGarrett Wollman ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 36419fc74fbSJeffrey Hsu 365f7a39160SGleb Smirnoff ia->ia_ifp = ifp; 366f7a39160SGleb Smirnoff ia->ia_addr = *addr; 367f7a39160SGleb Smirnoff if (mask->sin_len != 0) { 368f7a39160SGleb Smirnoff ia->ia_sockmask = *mask; 369f7a39160SGleb Smirnoff ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 370f7a39160SGleb Smirnoff } else { 371f7a39160SGleb Smirnoff in_addr_t i = ntohl(addr->sin_addr.s_addr); 372f7a39160SGleb Smirnoff 373f7a39160SGleb Smirnoff /* 374f7a39160SGleb Smirnoff * Be compatible with network classes, if netmask isn't 375f7a39160SGleb Smirnoff * supplied, guess it based on classes. 376f7a39160SGleb Smirnoff */ 377f7a39160SGleb Smirnoff if (IN_CLASSA(i)) 378f7a39160SGleb Smirnoff ia->ia_subnetmask = IN_CLASSA_NET; 379f7a39160SGleb Smirnoff else if (IN_CLASSB(i)) 380f7a39160SGleb Smirnoff ia->ia_subnetmask = IN_CLASSB_NET; 381f7a39160SGleb Smirnoff else 382f7a39160SGleb Smirnoff ia->ia_subnetmask = IN_CLASSC_NET; 383f7a39160SGleb Smirnoff ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 384f7a39160SGleb Smirnoff } 385f7a39160SGleb Smirnoff ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask; 386f7a39160SGleb Smirnoff in_socktrim(&ia->ia_sockmask); 387f7a39160SGleb Smirnoff 388df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 389f7a39160SGleb Smirnoff if (broadaddr->sin_len != 0) { 390f7a39160SGleb Smirnoff ia->ia_broadaddr = *broadaddr; 391f7a39160SGleb Smirnoff } else if (ia->ia_subnetmask == IN_RFC3021_MASK) { 392f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST; 393f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 394f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_family = AF_INET; 395f7a39160SGleb Smirnoff } else { 396f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_addr.s_addr = 397f7a39160SGleb Smirnoff htonl(ia->ia_subnet | ~ia->ia_subnetmask); 398f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 399df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_family = AF_INET; 400df8bae1dSRodney W. Grimes } 401f7a39160SGleb Smirnoff } 402f7a39160SGleb Smirnoff 403f7a39160SGleb Smirnoff if (ifp->if_flags & IFF_POINTOPOINT) 404f7a39160SGleb Smirnoff ia->ia_dstaddr = *dstaddr; 405f7a39160SGleb Smirnoff 406f7a39160SGleb Smirnoff /* XXXGL: rtinit() needs this strange assignment. */ 407f7a39160SGleb Smirnoff if (ifp->if_flags & IFF_LOOPBACK) 408f7a39160SGleb Smirnoff ia->ia_dstaddr = ia->ia_addr; 409f3d30eb2SGleb Smirnoff 410a49b317cSAlexander V. Chernikov /* if_addrhead is already referenced by ifa_alloc() */ 411137f91e8SJohn Baldwin IF_ADDR_WLOCK(ifp); 4128021456aSRobert Watson TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 413137f91e8SJohn Baldwin IF_ADDR_WUNLOCK(ifp); 414f7a39160SGleb Smirnoff 4158c0fec80SRobert Watson ifa_ref(ifa); /* in_ifaddrhead */ 4162d9cfabaSRobert Watson IN_IFADDR_WLOCK(); 417603724d3SBjoern A. Zeeb TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 418f7a39160SGleb Smirnoff LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 4192d9cfabaSRobert Watson IN_IFADDR_WUNLOCK(); 420df8bae1dSRodney W. Grimes 421*d34165f7SSteven Hartland if (vhid != 0) { 422f7a39160SGleb Smirnoff error = (*carp_attach_p)(&ia->ia_ifa, vhid); 423f7a39160SGleb Smirnoff if (error) 424f7a39160SGleb Smirnoff goto fail1; 425*d34165f7SSteven Hartland } 426f7a39160SGleb Smirnoff 427f7a39160SGleb Smirnoff /* 428f7a39160SGleb Smirnoff * Give the interface a chance to initialize 429f7a39160SGleb Smirnoff * if this is its first address, 430f7a39160SGleb Smirnoff * and to validate the address if necessary. 431f7a39160SGleb Smirnoff */ 432*d34165f7SSteven Hartland if (ifp->if_ioctl != NULL) { 433f7a39160SGleb Smirnoff error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 434f7a39160SGleb Smirnoff if (error) 435f7a39160SGleb Smirnoff goto fail2; 436*d34165f7SSteven Hartland } 437f7a39160SGleb Smirnoff 438f7a39160SGleb Smirnoff /* 439f7a39160SGleb Smirnoff * Add route for the network. 440f7a39160SGleb Smirnoff */ 441f7a39160SGleb Smirnoff if (vhid == 0) { 442f7a39160SGleb Smirnoff int flags = RTF_UP; 443f7a39160SGleb Smirnoff 444f7a39160SGleb Smirnoff if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 445f7a39160SGleb Smirnoff flags |= RTF_HOST; 446f7a39160SGleb Smirnoff 447f7a39160SGleb Smirnoff error = in_addprefix(ia, flags); 448f7a39160SGleb Smirnoff if (error) 449f7a39160SGleb Smirnoff goto fail2; 450df8bae1dSRodney W. Grimes } 451df8bae1dSRodney W. Grimes 452588885f2SRobert Watson /* 453f7a39160SGleb Smirnoff * Add a loopback route to self. 454588885f2SRobert Watson */ 455f7a39160SGleb Smirnoff if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 && 456c1f7c3f5SGleb Smirnoff ia->ia_addr.sin_addr.s_addr != INADDR_ANY && 457c1f7c3f5SGleb Smirnoff !((ifp->if_flags & IFF_POINTOPOINT) && 458c1f7c3f5SGleb Smirnoff ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) { 459f7a39160SGleb Smirnoff struct in_ifaddr *eia; 460df8bae1dSRodney W. Grimes 461f7a39160SGleb Smirnoff eia = in_localip_more(ia); 462f7a39160SGleb Smirnoff 463f7a39160SGleb Smirnoff if (eia == NULL) { 464f7a39160SGleb Smirnoff error = ifa_add_loopback_route((struct ifaddr *)ia, 465f7a39160SGleb Smirnoff (struct sockaddr *)&ia->ia_addr); 466f7a39160SGleb Smirnoff if (error) 467f7a39160SGleb Smirnoff goto fail3; 468f7a39160SGleb Smirnoff } else 469f7a39160SGleb Smirnoff ifa_free(&eia->ia_ifa); 470588885f2SRobert Watson } 471df8bae1dSRodney W. Grimes 472f7a39160SGleb Smirnoff if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) { 473f7a39160SGleb Smirnoff struct in_addr allhosts_addr; 474f7a39160SGleb Smirnoff struct in_ifinfo *ii; 475df8bae1dSRodney W. Grimes 476c75aa354SBruce M Simpson ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 477f7a39160SGleb Smirnoff allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 478df8bae1dSRodney W. Grimes 479f7a39160SGleb Smirnoff error = in_joingroup(ifp, &allhosts_addr, NULL, 480f7a39160SGleb Smirnoff &ii->ii_allhosts); 481f7a39160SGleb Smirnoff } 482f7a39160SGleb Smirnoff 483f7a39160SGleb Smirnoff EVENTHANDLER_INVOKE(ifaddr_event, ifp); 484f7a39160SGleb Smirnoff 485f7a39160SGleb Smirnoff return (error); 486f7a39160SGleb Smirnoff 487f7a39160SGleb Smirnoff fail3: 488f7a39160SGleb Smirnoff if (vhid == 0) 489f7a39160SGleb Smirnoff (void )in_scrubprefix(ia, LLE_STATIC); 490f7a39160SGleb Smirnoff 491f7a39160SGleb Smirnoff fail2: 492f7a39160SGleb Smirnoff if (ia->ia_ifa.ifa_carp) 493f7a39160SGleb Smirnoff (*carp_detach_p)(&ia->ia_ifa); 494f7a39160SGleb Smirnoff 495f7a39160SGleb Smirnoff fail1: 496f7a39160SGleb Smirnoff IF_ADDR_WLOCK(ifp); 497f7a39160SGleb Smirnoff TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 498f7a39160SGleb Smirnoff IF_ADDR_WUNLOCK(ifp); 499a49b317cSAlexander V. Chernikov ifa_free(&ia->ia_ifa); /* if_addrhead */ 500f7a39160SGleb Smirnoff 501f7a39160SGleb Smirnoff IN_IFADDR_WLOCK(); 502f7a39160SGleb Smirnoff TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 503f7a39160SGleb Smirnoff LIST_REMOVE(ia, ia_hash); 504f7a39160SGleb Smirnoff IN_IFADDR_WUNLOCK(); 505a49b317cSAlexander V. Chernikov ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 506f7a39160SGleb Smirnoff 507f7a39160SGleb Smirnoff return (error); 508f7a39160SGleb Smirnoff } 509f7a39160SGleb Smirnoff 510f7a39160SGleb Smirnoff static int 511f7a39160SGleb Smirnoff in_difaddr_ioctl(caddr_t data, struct ifnet *ifp, struct thread *td) 512f7a39160SGleb Smirnoff { 513f7a39160SGleb Smirnoff const struct ifreq *ifr = (struct ifreq *)data; 5146224cd89SNathan Whitehorn const struct sockaddr_in *addr = (const struct sockaddr_in *) 5156224cd89SNathan Whitehorn &ifr->ifr_addr; 516f7a39160SGleb Smirnoff struct ifaddr *ifa; 517f7a39160SGleb Smirnoff struct in_ifaddr *ia; 518f7a39160SGleb Smirnoff bool deleteAny, iaIsLast; 519f7a39160SGleb Smirnoff int error; 520f7a39160SGleb Smirnoff 521f7a39160SGleb Smirnoff if (td != NULL) { 522f7a39160SGleb Smirnoff error = priv_check(td, PRIV_NET_DELIFADDR); 523f7a39160SGleb Smirnoff if (error) 524f7a39160SGleb Smirnoff return (error); 525f7a39160SGleb Smirnoff } 526f7a39160SGleb Smirnoff 527f7a39160SGleb Smirnoff if (addr->sin_len != sizeof(struct sockaddr_in) || 528f7a39160SGleb Smirnoff addr->sin_family != AF_INET) 529f7a39160SGleb Smirnoff deleteAny = true; 530f7a39160SGleb Smirnoff else 531f7a39160SGleb Smirnoff deleteAny = false; 532f7a39160SGleb Smirnoff 533f7a39160SGleb Smirnoff iaIsLast = true; 534f7a39160SGleb Smirnoff ia = NULL; 535f7a39160SGleb Smirnoff IF_ADDR_WLOCK(ifp); 536f7a39160SGleb Smirnoff TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 5379706c950SGleb Smirnoff struct in_ifaddr *it; 538f7a39160SGleb Smirnoff 5399706c950SGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET) 540f7a39160SGleb Smirnoff continue; 541f7a39160SGleb Smirnoff 5429706c950SGleb Smirnoff it = (struct in_ifaddr *)ifa; 543f7a39160SGleb Smirnoff if (deleteAny && ia == NULL && (td == NULL || 544f7a39160SGleb Smirnoff prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0)) 545f7a39160SGleb Smirnoff ia = it; 546f7a39160SGleb Smirnoff 547f7a39160SGleb Smirnoff if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 548f7a39160SGleb Smirnoff (td == NULL || prison_check_ip4(td->td_ucred, 549f7a39160SGleb Smirnoff &addr->sin_addr) == 0)) 550f7a39160SGleb Smirnoff ia = it; 551f7a39160SGleb Smirnoff 552f7a39160SGleb Smirnoff if (it != ia) 553f7a39160SGleb Smirnoff iaIsLast = false; 554f7a39160SGleb Smirnoff } 555f7a39160SGleb Smirnoff 556f7a39160SGleb Smirnoff if (ia == NULL) { 557f7a39160SGleb Smirnoff IF_ADDR_WUNLOCK(ifp); 558f7a39160SGleb Smirnoff return (EADDRNOTAVAIL); 559f7a39160SGleb Smirnoff } 560f7a39160SGleb Smirnoff 561f7a39160SGleb Smirnoff TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 562f7a39160SGleb Smirnoff IF_ADDR_WUNLOCK(ifp); 563f7a39160SGleb Smirnoff ifa_free(&ia->ia_ifa); /* if_addrhead */ 564f7a39160SGleb Smirnoff 565f7a39160SGleb Smirnoff IN_IFADDR_WLOCK(); 566f7a39160SGleb Smirnoff TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 567f7a39160SGleb Smirnoff LIST_REMOVE(ia, ia_hash); 568f7a39160SGleb Smirnoff IN_IFADDR_WUNLOCK(); 569f7a39160SGleb Smirnoff 570089cdfadSRuslan Ermilov /* 571237bf7f7SGleb Smirnoff * in_scrubprefix() kills the interface route. 572089cdfadSRuslan Ermilov */ 573237bf7f7SGleb Smirnoff in_scrubprefix(ia, LLE_STATIC); 574588885f2SRobert Watson 575c655b7c4SDavid Greenman /* 576089cdfadSRuslan Ermilov * in_ifadown gets rid of all the rest of 577089cdfadSRuslan Ermilov * the routes. This is not quite the right 578089cdfadSRuslan Ermilov * thing to do, but at least if we are running 579089cdfadSRuslan Ermilov * a routing process they will come back. 580089cdfadSRuslan Ermilov */ 58191854268SRuslan Ermilov in_ifadown(&ia->ia_ifa, 1); 5820f02fdacSBrian Somers 58308b68b0eSGleb Smirnoff if (ia->ia_ifa.ifa_carp) 58408b68b0eSGleb Smirnoff (*carp_detach_p)(&ia->ia_ifa); 58508b68b0eSGleb Smirnoff 586f7e083afSBruce M Simpson /* 587f7e083afSBruce M Simpson * If this is the last IPv4 address configured on this 588f7e083afSBruce M Simpson * interface, leave the all-hosts group. 589d10910e6SBruce M Simpson * No state-change report need be transmitted. 590f7e083afSBruce M Simpson */ 591f7a39160SGleb Smirnoff if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) { 592f7a39160SGleb Smirnoff struct in_ifinfo *ii; 593f7a39160SGleb Smirnoff 594c75aa354SBruce M Simpson ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 595f7e083afSBruce M Simpson IN_MULTI_LOCK(); 596d10910e6SBruce M Simpson if (ii->ii_allhosts) { 5976d00fd9cSGleb Smirnoff (void)in_leavegroup_locked(ii->ii_allhosts, NULL); 598d10910e6SBruce M Simpson ii->ii_allhosts = NULL; 599d10910e6SBruce M Simpson } 600f7e083afSBruce M Simpson IN_MULTI_UNLOCK(); 601f7a39160SGleb Smirnoff } 6026d00fd9cSGleb Smirnoff 603f7a39160SGleb Smirnoff EVENTHANDLER_INVOKE(ifaddr_event, ifp); 604a49b317cSAlexander V. Chernikov ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 605f7a39160SGleb Smirnoff 606f7a39160SGleb Smirnoff return (0); 607df8bae1dSRodney W. Grimes } 608df8bae1dSRodney W. Grimes 60948321abeSMax Laier #define rtinitflags(x) \ 61048321abeSMax Laier ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 61148321abeSMax Laier ? RTF_HOST : 0) 612ccbb9c35SQing Li 613ccbb9c35SQing Li /* 614fbdd20a1SMatt Jacob * Check if we have a route for the given prefix already or add one accordingly. 61548321abeSMax Laier */ 61608b68b0eSGleb Smirnoff int 617f2565d68SRobert Watson in_addprefix(struct in_ifaddr *target, int flags) 61848321abeSMax Laier { 61948321abeSMax Laier struct in_ifaddr *ia; 620bfb26eecSGleb Smirnoff struct in_addr prefix, mask, p, m; 6210cfee0c2SAlan Somers int error; 62248321abeSMax Laier 623fbdd20a1SMatt Jacob if ((flags & RTF_HOST) != 0) { 62448321abeSMax Laier prefix = target->ia_dstaddr.sin_addr; 625fbdd20a1SMatt Jacob mask.s_addr = 0; 626fbdd20a1SMatt Jacob } else { 62748321abeSMax Laier prefix = target->ia_addr.sin_addr; 62848321abeSMax Laier mask = target->ia_sockmask.sin_addr; 62948321abeSMax Laier prefix.s_addr &= mask.s_addr; 63048321abeSMax Laier } 63148321abeSMax Laier 6322d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 6330cfee0c2SAlan Somers /* Look for an existing address with the same prefix, mask, and fib */ 634603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 635bfb26eecSGleb Smirnoff if (rtinitflags(ia)) { 63672366606SKevin Lo p = ia->ia_dstaddr.sin_addr; 63748321abeSMax Laier 63848321abeSMax Laier if (prefix.s_addr != p.s_addr) 63948321abeSMax Laier continue; 640bfb26eecSGleb Smirnoff } else { 641bfb26eecSGleb Smirnoff p = ia->ia_addr.sin_addr; 642bfb26eecSGleb Smirnoff m = ia->ia_sockmask.sin_addr; 643bfb26eecSGleb Smirnoff p.s_addr &= m.s_addr; 644bfb26eecSGleb Smirnoff 645bfb26eecSGleb Smirnoff if (prefix.s_addr != p.s_addr || 646bfb26eecSGleb Smirnoff mask.s_addr != m.s_addr) 647bfb26eecSGleb Smirnoff continue; 648bfb26eecSGleb Smirnoff } 6490cfee0c2SAlan Somers if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib) 6500cfee0c2SAlan Somers continue; 65148321abeSMax Laier 65248321abeSMax Laier /* 65348321abeSMax Laier * If we got a matching prefix route inserted by other 65448321abeSMax Laier * interface address, we are done here. 65548321abeSMax Laier */ 6561ae95409SGleb Smirnoff if (ia->ia_flags & IFA_ROUTE) { 657c7ea0aa6SQing Li #ifdef RADIX_MPATH 658c7ea0aa6SQing Li if (ia->ia_addr.sin_addr.s_addr == 65942db1b87SBjoern A. Zeeb target->ia_addr.sin_addr.s_addr) { 66042db1b87SBjoern A. Zeeb IN_IFADDR_RUNLOCK(); 661c7ea0aa6SQing Li return (EEXIST); 66242db1b87SBjoern A. Zeeb } else 663c7ea0aa6SQing Li break; 664c7ea0aa6SQing Li #endif 66508b68b0eSGleb Smirnoff if (V_nosameprefix) { 6662d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 6671ae95409SGleb Smirnoff return (EEXIST); 6682d9cfabaSRobert Watson } else { 6690cfee0c2SAlan Somers int fibnum; 6700cfee0c2SAlan Somers 6710cfee0c2SAlan Somers fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS : 6720cfee0c2SAlan Somers target->ia_ifp->if_fib; 673d375edc9SAlexander V. Chernikov rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum); 6742d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 6751ae95409SGleb Smirnoff return (0); 6761ae95409SGleb Smirnoff } 67748321abeSMax Laier } 6782d9cfabaSRobert Watson } 6792d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 68048321abeSMax Laier 68148321abeSMax Laier /* 68248321abeSMax Laier * No-one seem to have this prefix route, so we try to insert it. 68348321abeSMax Laier */ 68448321abeSMax Laier error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 68548321abeSMax Laier if (!error) 68648321abeSMax Laier target->ia_flags |= IFA_ROUTE; 687460473a0SBjoern A. Zeeb return (error); 68848321abeSMax Laier } 68948321abeSMax Laier 69048321abeSMax Laier /* 69148321abeSMax Laier * If there is no other address in the system that can serve a route to the 69248321abeSMax Laier * same prefix, remove the route. Hand over the route to the new address 69348321abeSMax Laier * otherwise. 69448321abeSMax Laier */ 69508b68b0eSGleb Smirnoff int 6965b84dc78SQing Li in_scrubprefix(struct in_ifaddr *target, u_int flags) 69748321abeSMax Laier { 69848321abeSMax Laier struct in_ifaddr *ia; 69955174c34SGleb Smirnoff struct in_addr prefix, mask, p, m; 7007278b62aSAlan Somers int error = 0; 701c9d763bfSQing Li struct sockaddr_in prefix0, mask0; 70248321abeSMax Laier 703df813b7eSQing Li /* 704df813b7eSQing Li * Remove the loopback route to the interface address. 705df813b7eSQing Li */ 70692fac994SQing Li if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) && 707c7ab6602SQing Li !(target->ia_ifp->if_flags & IFF_LOOPBACK) && 708f7a39160SGleb Smirnoff (flags & LLE_STATIC)) { 709f7a39160SGleb Smirnoff struct in_ifaddr *eia; 710c7ab6602SQing Li 711f7a39160SGleb Smirnoff eia = in_localip_more(target); 712f7a39160SGleb Smirnoff 713f7a39160SGleb Smirnoff if (eia != NULL) { 7147278b62aSAlan Somers int fibnum = target->ia_ifp->if_fib; 7157278b62aSAlan Somers 716f7a39160SGleb Smirnoff error = ifa_switch_loopback_route((struct ifaddr *)eia, 7170489b891SAlan Somers (struct sockaddr *)&target->ia_addr, fibnum); 718f7a39160SGleb Smirnoff ifa_free(&eia->ia_ifa); 719f7a39160SGleb Smirnoff } else { 7209bb7d0f4SQing Li error = ifa_del_loopback_route((struct ifaddr *)target, 7219bb7d0f4SQing Li (struct sockaddr *)&target->ia_addr); 7225b84dc78SQing Li } 723f7a39160SGleb Smirnoff 724f7a39160SGleb Smirnoff if (!(target->ia_ifp->if_flags & IFF_NOARP)) 725fa3cfd39SQing Li /* remove arp cache */ 726f7a39160SGleb Smirnoff arp_ifscrub(target->ia_ifp, 727f7a39160SGleb Smirnoff IA_SIN(target)->sin_addr.s_addr); 728ebc90701SQing Li } 729ebc90701SQing Li 73055174c34SGleb Smirnoff if (rtinitflags(target)) { 73148321abeSMax Laier prefix = target->ia_dstaddr.sin_addr; 73255174c34SGleb Smirnoff mask.s_addr = 0; 73355174c34SGleb Smirnoff } else { 73448321abeSMax Laier prefix = target->ia_addr.sin_addr; 73548321abeSMax Laier mask = target->ia_sockmask.sin_addr; 73648321abeSMax Laier prefix.s_addr &= mask.s_addr; 73748321abeSMax Laier } 73848321abeSMax Laier 739ccbb9c35SQing Li if ((target->ia_flags & IFA_ROUTE) == 0) { 7407278b62aSAlan Somers int fibnum; 7417278b62aSAlan Somers 7427278b62aSAlan Somers fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS : 7437278b62aSAlan Somers target->ia_ifp->if_fib; 744d375edc9SAlexander V. Chernikov rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum); 745ccbb9c35SQing Li return (0); 746ccbb9c35SQing Li } 747ccbb9c35SQing Li 7482d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 749603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 75055174c34SGleb Smirnoff if (rtinitflags(ia)) { 75148321abeSMax Laier p = ia->ia_dstaddr.sin_addr; 75255174c34SGleb Smirnoff 75355174c34SGleb Smirnoff if (prefix.s_addr != p.s_addr) 75455174c34SGleb Smirnoff continue; 75555174c34SGleb Smirnoff } else { 75648321abeSMax Laier p = ia->ia_addr.sin_addr; 75755174c34SGleb Smirnoff m = ia->ia_sockmask.sin_addr; 75855174c34SGleb Smirnoff p.s_addr &= m.s_addr; 75955174c34SGleb Smirnoff 76055174c34SGleb Smirnoff if (prefix.s_addr != p.s_addr || 76155174c34SGleb Smirnoff mask.s_addr != m.s_addr) 76255174c34SGleb Smirnoff continue; 76348321abeSMax Laier } 76448321abeSMax Laier 76555174c34SGleb Smirnoff if ((ia->ia_ifp->if_flags & IFF_UP) == 0) 76648321abeSMax Laier continue; 76748321abeSMax Laier 76848321abeSMax Laier /* 76948321abeSMax Laier * If we got a matching prefix address, move IFA_ROUTE and 77048321abeSMax Laier * the route itself to it. Make sure that routing daemons 77148321abeSMax Laier * get a heads-up. 77248321abeSMax Laier */ 77308b68b0eSGleb Smirnoff if ((ia->ia_flags & IFA_ROUTE) == 0) { 77479d51435SSergey Kandaurov ifa_ref(&ia->ia_ifa); 7752d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 77692322284SQing Li error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, 77748321abeSMax Laier rtinitflags(target)); 77892322284SQing Li if (error == 0) 77948321abeSMax Laier target->ia_flags &= ~IFA_ROUTE; 78092322284SQing Li else 78192322284SQing Li log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n", 78292322284SQing Li error); 78348321abeSMax Laier error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 78448321abeSMax Laier rtinitflags(ia) | RTF_UP); 78548321abeSMax Laier if (error == 0) 78648321abeSMax Laier ia->ia_flags |= IFA_ROUTE; 78792322284SQing Li else 78892322284SQing Li log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n", 78992322284SQing Li error); 79079d51435SSergey Kandaurov ifa_free(&ia->ia_ifa); 791460473a0SBjoern A. Zeeb return (error); 79248321abeSMax Laier } 79348321abeSMax Laier } 7942d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 79548321abeSMax Laier 79648321abeSMax Laier /* 797c9d763bfSQing Li * remove all L2 entries on the given prefix 798c9d763bfSQing Li */ 799c9d763bfSQing Li bzero(&prefix0, sizeof(prefix0)); 800c9d763bfSQing Li prefix0.sin_len = sizeof(prefix0); 801c9d763bfSQing Li prefix0.sin_family = AF_INET; 802c9d763bfSQing Li prefix0.sin_addr.s_addr = target->ia_subnet; 803c9d763bfSQing Li bzero(&mask0, sizeof(mask0)); 804c9d763bfSQing Li mask0.sin_len = sizeof(mask0); 805c9d763bfSQing Li mask0.sin_family = AF_INET; 806c9d763bfSQing Li mask0.sin_addr.s_addr = target->ia_subnetmask; 807c9d763bfSQing Li lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0, 8085b84dc78SQing Li (struct sockaddr *)&mask0, flags); 809c9d763bfSQing Li 810c9d763bfSQing Li /* 81148321abeSMax Laier * As no-one seem to have this prefix, we can remove the route. 81248321abeSMax Laier */ 81392322284SQing Li error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 81492322284SQing Li if (error == 0) 81548321abeSMax Laier target->ia_flags &= ~IFA_ROUTE; 81692322284SQing Li else 81792322284SQing Li log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error); 81892322284SQing Li return (error); 81948321abeSMax Laier } 82048321abeSMax Laier 82148321abeSMax Laier #undef rtinitflags 822df8bae1dSRodney W. Grimes 823df8bae1dSRodney W. Grimes /* 824df8bae1dSRodney W. Grimes * Return 1 if the address might be a local broadcast address. 825df8bae1dSRodney W. Grimes */ 82626f9a767SRodney W. Grimes int 827f2565d68SRobert Watson in_broadcast(struct in_addr in, struct ifnet *ifp) 828df8bae1dSRodney W. Grimes { 829df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 830df8bae1dSRodney W. Grimes u_long t; 831df8bae1dSRodney W. Grimes 832df8bae1dSRodney W. Grimes if (in.s_addr == INADDR_BROADCAST || 833df8bae1dSRodney W. Grimes in.s_addr == INADDR_ANY) 834460473a0SBjoern A. Zeeb return (1); 835df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 836460473a0SBjoern A. Zeeb return (0); 837df8bae1dSRodney W. Grimes t = ntohl(in.s_addr); 838df8bae1dSRodney W. Grimes /* 839df8bae1dSRodney W. Grimes * Look through the list of addresses for a match 840df8bae1dSRodney W. Grimes * with a broadcast address. 841df8bae1dSRodney W. Grimes */ 842df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa) 843462b86feSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 844df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family == AF_INET && 845df8bae1dSRodney W. Grimes (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 846df8bae1dSRodney W. Grimes /* 84753883e0cSGleb Smirnoff * Check for old-style (host 0) broadcast, but 84853883e0cSGleb Smirnoff * taking into account that RFC 3021 obsoletes it. 849df8bae1dSRodney W. Grimes */ 85053883e0cSGleb Smirnoff (ia->ia_subnetmask != IN_RFC3021_MASK && 85153883e0cSGleb Smirnoff t == ia->ia_subnet)) && 8528dd27fd6SGuido van Rooij /* 8538dd27fd6SGuido van Rooij * Check for an all one subnetmask. These 8548dd27fd6SGuido van Rooij * only exist when an interface gets a secondary 8558dd27fd6SGuido van Rooij * address. 8568dd27fd6SGuido van Rooij */ 8578dd27fd6SGuido van Rooij ia->ia_subnetmask != (u_long)0xffffffff) 858460473a0SBjoern A. Zeeb return (1); 859df8bae1dSRodney W. Grimes return (0); 860df8bae1dSRodney W. Grimes #undef ia 861df8bae1dSRodney W. Grimes } 862ec002feeSBruce M Simpson 863df8bae1dSRodney W. Grimes /* 864b1c53bc9SRobert Watson * On interface removal, clean up IPv4 data structures hung off of the ifnet. 865b1c53bc9SRobert Watson */ 866b1c53bc9SRobert Watson void 867f2565d68SRobert Watson in_ifdetach(struct ifnet *ifp) 868b1c53bc9SRobert Watson { 869b1c53bc9SRobert Watson 870603724d3SBjoern A. Zeeb in_pcbpurgeif0(&V_ripcbinfo, ifp); 871603724d3SBjoern A. Zeeb in_pcbpurgeif0(&V_udbinfo, ifp); 872e06e816fSKevin Lo in_pcbpurgeif0(&V_ulitecbinfo, ifp); 873ec002feeSBruce M Simpson in_purgemaddrs(ifp); 874b1c53bc9SRobert Watson } 8756e6b3f7cSQing Li 876d10910e6SBruce M Simpson /* 877d10910e6SBruce M Simpson * Delete all IPv4 multicast address records, and associated link-layer 878d10910e6SBruce M Simpson * multicast address records, associated with ifp. 879d10910e6SBruce M Simpson * XXX It looks like domifdetach runs AFTER the link layer cleanup. 88056663a40SBruce M Simpson * XXX This should not race with ifma_protospec being set during 88156663a40SBruce M Simpson * a new allocation, if it does, we have bigger problems. 882d10910e6SBruce M Simpson */ 883d10910e6SBruce M Simpson static void 884d10910e6SBruce M Simpson in_purgemaddrs(struct ifnet *ifp) 885d10910e6SBruce M Simpson { 886d10910e6SBruce M Simpson LIST_HEAD(,in_multi) purgeinms; 887d10910e6SBruce M Simpson struct in_multi *inm, *tinm; 888d10910e6SBruce M Simpson struct ifmultiaddr *ifma; 889d10910e6SBruce M Simpson 890d10910e6SBruce M Simpson LIST_INIT(&purgeinms); 891d10910e6SBruce M Simpson IN_MULTI_LOCK(); 892d10910e6SBruce M Simpson 893d10910e6SBruce M Simpson /* 894d10910e6SBruce M Simpson * Extract list of in_multi associated with the detaching ifp 895d10910e6SBruce M Simpson * which the PF_INET layer is about to release. 896d10910e6SBruce M Simpson * We need to do this as IF_ADDR_LOCK() may be re-acquired 897d10910e6SBruce M Simpson * by code further down. 898d10910e6SBruce M Simpson */ 899137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 900d10910e6SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 90156663a40SBruce M Simpson if (ifma->ifma_addr->sa_family != AF_INET || 90256663a40SBruce M Simpson ifma->ifma_protospec == NULL) 903d10910e6SBruce M Simpson continue; 90456663a40SBruce M Simpson #if 0 90556663a40SBruce M Simpson KASSERT(ifma->ifma_protospec != NULL, 90656663a40SBruce M Simpson ("%s: ifma_protospec is NULL", __func__)); 90756663a40SBruce M Simpson #endif 908d10910e6SBruce M Simpson inm = (struct in_multi *)ifma->ifma_protospec; 909d10910e6SBruce M Simpson LIST_INSERT_HEAD(&purgeinms, inm, inm_link); 910d10910e6SBruce M Simpson } 911137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 912d10910e6SBruce M Simpson 913d10910e6SBruce M Simpson LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) { 914d10910e6SBruce M Simpson LIST_REMOVE(inm, inm_link); 91507cde5e9SRobert Watson inm_release_locked(inm); 916d10910e6SBruce M Simpson } 917d10910e6SBruce M Simpson igmp_ifdetach(ifp); 918d10910e6SBruce M Simpson 919d10910e6SBruce M Simpson IN_MULTI_UNLOCK(); 920d10910e6SBruce M Simpson } 921d10910e6SBruce M Simpson 9226e6b3f7cSQing Li struct in_llentry { 9236e6b3f7cSQing Li struct llentry base; 9246e6b3f7cSQing Li struct sockaddr_in l3_addr4; 9256e6b3f7cSQing Li }; 9266e6b3f7cSQing Li 927a93cda78SKip Macy /* 928a93cda78SKip Macy * Deletes an address from the address table. 929a93cda78SKip Macy * This function is called by the timer functions 930a93cda78SKip Macy * such as arptimer() and nd6_llinfo_timer(), and 931a93cda78SKip Macy * the caller does the locking. 932a93cda78SKip Macy */ 933a93cda78SKip Macy static void 934a93cda78SKip Macy in_lltable_free(struct lltable *llt, struct llentry *lle) 935a93cda78SKip Macy { 936a93cda78SKip Macy LLE_WUNLOCK(lle); 937a93cda78SKip Macy LLE_LOCK_DESTROY(lle); 938a93cda78SKip Macy free(lle, M_LLTABLE); 939a93cda78SKip Macy } 940a93cda78SKip Macy 9416e6b3f7cSQing Li static struct llentry * 9426e6b3f7cSQing Li in_lltable_new(const struct sockaddr *l3addr, u_int flags) 9436e6b3f7cSQing Li { 9446e6b3f7cSQing Li struct in_llentry *lle; 9456e6b3f7cSQing Li 94690b357f6SGleb Smirnoff lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO); 9476e6b3f7cSQing Li if (lle == NULL) /* NB: caller generates msg */ 9486e6b3f7cSQing Li return NULL; 9496e6b3f7cSQing Li 9506e6b3f7cSQing Li /* 9516e6b3f7cSQing Li * For IPv4 this will trigger "arpresolve" to generate 9526e6b3f7cSQing Li * an ARP request. 9536e6b3f7cSQing Li */ 954a98c06f1SGleb Smirnoff lle->base.la_expire = time_uptime; /* mark expired */ 9556e6b3f7cSQing Li lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 9566e6b3f7cSQing Li lle->base.lle_refcnt = 1; 957a93cda78SKip Macy lle->base.lle_free = in_lltable_free; 9586e6b3f7cSQing Li LLE_LOCK_INIT(&lle->base); 959ea537929SGleb Smirnoff callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock, 960ea537929SGleb Smirnoff CALLOUT_RETURNUNLOCKED); 961ea537929SGleb Smirnoff 962ea537929SGleb Smirnoff return (&lle->base); 9636e6b3f7cSQing Li } 9646e6b3f7cSQing Li 965c9d763bfSQing Li #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 966c9d763bfSQing Li (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 ) 967c9d763bfSQing Li 968c9d763bfSQing Li static void 969ea50c13eSGleb Smirnoff in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix, 970ea50c13eSGleb Smirnoff const struct sockaddr *mask, u_int flags) 971c9d763bfSQing Li { 972c9d763bfSQing Li const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix; 973c9d763bfSQing Li const struct sockaddr_in *msk = (const struct sockaddr_in *)mask; 974c9d763bfSQing Li struct llentry *lle, *next; 975ea50c13eSGleb Smirnoff int i; 976e162ea60SGeorge V. Neville-Neil size_t pkts_dropped; 977c9d763bfSQing Li 978ea537929SGleb Smirnoff IF_AFDATA_WLOCK(llt->llt_ifp); 979c9d763bfSQing Li for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 980c9d763bfSQing Li LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 9815b84dc78SQing Li /* 9825b84dc78SQing Li * (flags & LLE_STATIC) means deleting all entries 983ea50c13eSGleb Smirnoff * including static ARP entries. 9845b84dc78SQing Li */ 985ea50c13eSGleb Smirnoff if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)), 986ea50c13eSGleb Smirnoff pfx, msk) && ((flags & LLE_STATIC) || 987ea50c13eSGleb Smirnoff !(lle->la_flags & LLE_STATIC))) { 988c9d763bfSQing Li LLE_WLOCK(lle); 989ea537929SGleb Smirnoff if (callout_stop(&lle->la_timer)) 990becba438SBjoern A. Zeeb LLE_REMREF(lle); 991e162ea60SGeorge V. Neville-Neil pkts_dropped = llentry_free(lle); 992e162ea60SGeorge V. Neville-Neil ARPSTAT_ADD(dropped, pkts_dropped); 993c9d763bfSQing Li } 994c9d763bfSQing Li } 995c9d763bfSQing Li } 996ea537929SGleb Smirnoff IF_AFDATA_WUNLOCK(llt->llt_ifp); 997c9d763bfSQing Li } 998c9d763bfSQing Li 999c9d763bfSQing Li 10006e6b3f7cSQing Li static int 1001c7ab6602SQing Li in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr) 10026e6b3f7cSQing Li { 10036e6b3f7cSQing Li struct rtentry *rt; 10046e6b3f7cSQing Li 10056e6b3f7cSQing Li KASSERT(l3addr->sa_family == AF_INET, 10066e6b3f7cSQing Li ("sin_family %d", l3addr->sa_family)); 10076e6b3f7cSQing Li 1008743c072aSAlan Somers /* XXX rtalloc1_fib should take a const param */ 1009743c072aSAlan Somers rt = rtalloc1_fib(__DECONST(struct sockaddr *, l3addr), 0, 0, 1010743c072aSAlan Somers ifp->if_fib); 101113e255faSMarko Zec 10126cf8e330SQing Li if (rt == NULL) 10136cf8e330SQing Li return (EINVAL); 10146cf8e330SQing Li 101513e255faSMarko Zec /* 101613e255faSMarko Zec * If the gateway for an existing host route matches the target L3 10176cf8e330SQing Li * address, which is a special route inserted by some implementation 10186cf8e330SQing Li * such as MANET, and the interface is of the correct type, then 10196cf8e330SQing Li * allow for ARP to proceed. 102013e255faSMarko Zec */ 1021db92413eSQing Li if (rt->rt_flags & RTF_GATEWAY) { 102215d25219SQing Li if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp || 102315d25219SQing Li rt->rt_ifp->if_type != IFT_ETHER || 1024ea50c13eSGleb Smirnoff (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 || 102515d25219SQing Li memcmp(rt->rt_gateway->sa_data, l3addr->sa_data, 102615d25219SQing Li sizeof(in_addr_t)) != 0) { 1027db92413eSQing Li RTFREE_LOCKED(rt); 1028db92413eSQing Li return (EINVAL); 1029db92413eSQing Li } 103015d25219SQing Li } 1031db92413eSQing Li 1032db92413eSQing Li /* 1033db92413eSQing Li * Make sure that at least the destination address is covered 1034db92413eSQing Li * by the route. This is for handling the case where 2 or more 1035db92413eSQing Li * interfaces have the same prefix. An incoming packet arrives 1036db92413eSQing Li * on one interface and the corresponding outgoing packet leaves 1037db92413eSQing Li * another interface. 1038db92413eSQing Li */ 1039b3664a14SQing Li if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) { 104015d25219SQing Li const char *sa, *mask, *addr, *lim; 1041db92413eSQing Li int len; 1042db92413eSQing Li 104315d25219SQing Li mask = (const char *)rt_mask(rt); 1044b3664a14SQing Li /* 1045b3664a14SQing Li * Just being extra cautious to avoid some custom 1046b3664a14SQing Li * code getting into trouble. 1047b3664a14SQing Li */ 1048b3664a14SQing Li if (mask == NULL) { 1049b3664a14SQing Li RTFREE_LOCKED(rt); 1050b3664a14SQing Li return (EINVAL); 1051b3664a14SQing Li } 1052b3664a14SQing Li 1053b3664a14SQing Li sa = (const char *)rt_key(rt); 105415d25219SQing Li addr = (const char *)l3addr; 105515d25219SQing Li len = ((const struct sockaddr_in *)l3addr)->sin_len; 1056db92413eSQing Li lim = addr + len; 1057db92413eSQing Li 1058db92413eSQing Li for ( ; addr < lim; sa++, mask++, addr++) { 1059db92413eSQing Li if ((*sa ^ *addr) & *mask) { 10606cb2b4e7SQing Li #ifdef DIAGNOSTIC 10616e6b3f7cSQing Li log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 10626e6b3f7cSQing Li inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1063b4a22c36SQing Li #endif 106415d25219SQing Li RTFREE_LOCKED(rt); 106515d25219SQing Li return (EINVAL); 10666e6b3f7cSQing Li } 1067db92413eSQing Li } 1068db92413eSQing Li } 1069db92413eSQing Li 10706e6b3f7cSQing Li RTFREE_LOCKED(rt); 107115d25219SQing Li return (0); 10726e6b3f7cSQing Li } 10736e6b3f7cSQing Li 10746e6b3f7cSQing Li /* 10756e6b3f7cSQing Li * Return NULL if not found or marked for deletion. 10766e6b3f7cSQing Li * If found return lle read locked. 10776e6b3f7cSQing Li */ 10786e6b3f7cSQing Li static struct llentry * 10796e6b3f7cSQing Li in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 10806e6b3f7cSQing Li { 10816e6b3f7cSQing Li const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 10826e6b3f7cSQing Li struct ifnet *ifp = llt->llt_ifp; 10836e6b3f7cSQing Li struct llentry *lle; 10846e6b3f7cSQing Li struct llentries *lleh; 10856e6b3f7cSQing Li u_int hashkey; 10866e6b3f7cSQing Li 10876e6b3f7cSQing Li IF_AFDATA_LOCK_ASSERT(ifp); 10886e6b3f7cSQing Li KASSERT(l3addr->sa_family == AF_INET, 10896e6b3f7cSQing Li ("sin_family %d", l3addr->sa_family)); 10906e6b3f7cSQing Li 10916e6b3f7cSQing Li hashkey = sin->sin_addr.s_addr; 10926e6b3f7cSQing Li lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 10936e6b3f7cSQing Li LIST_FOREACH(lle, lleh, lle_next) { 1094ea50c13eSGleb Smirnoff struct sockaddr_in *sa2 = satosin(L3_ADDR(lle)); 10956e6b3f7cSQing Li if (lle->la_flags & LLE_DELETED) 10966e6b3f7cSQing Li continue; 1097dc495497SQing Li if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr) 10986e6b3f7cSQing Li break; 10996e6b3f7cSQing Li } 11006e6b3f7cSQing Li if (lle == NULL) { 11016cb2b4e7SQing Li #ifdef DIAGNOSTIC 11026e6b3f7cSQing Li if (flags & LLE_DELETE) 11036e6b3f7cSQing Li log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 11046e6b3f7cSQing Li #endif 11056e6b3f7cSQing Li if (!(flags & LLE_CREATE)) 11066e6b3f7cSQing Li return (NULL); 1107e2d14d93SAndrey V. Elsukov IF_AFDATA_WLOCK_ASSERT(ifp); 11086e6b3f7cSQing Li /* 11096e6b3f7cSQing Li * A route that covers the given address must have 11106e6b3f7cSQing Li * been installed 1st because we are doing a resolution, 11116e6b3f7cSQing Li * verify this. 11126e6b3f7cSQing Li */ 11136e6b3f7cSQing Li if (!(flags & LLE_IFADDR) && 1114c7ab6602SQing Li in_lltable_rtcheck(ifp, flags, l3addr) != 0) 11156e6b3f7cSQing Li goto done; 11166e6b3f7cSQing Li 11176e6b3f7cSQing Li lle = in_lltable_new(l3addr, flags); 11186e6b3f7cSQing Li if (lle == NULL) { 11196e6b3f7cSQing Li log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 11206e6b3f7cSQing Li goto done; 11216e6b3f7cSQing Li } 11226e6b3f7cSQing Li lle->la_flags = flags & ~LLE_CREATE; 11236e6b3f7cSQing Li if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 11246e6b3f7cSQing Li bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 11256e6b3f7cSQing Li lle->la_flags |= (LLE_VALID | LLE_STATIC); 11266e6b3f7cSQing Li } 11276e6b3f7cSQing Li 11286e6b3f7cSQing Li lle->lle_tbl = llt; 11296e6b3f7cSQing Li lle->lle_head = lleh; 1130ea537929SGleb Smirnoff lle->la_flags |= LLE_LINKED; 11316e6b3f7cSQing Li LIST_INSERT_HEAD(lleh, lle, lle_next); 11326e6b3f7cSQing Li } else if (flags & LLE_DELETE) { 11336e6b3f7cSQing Li if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 11346e6b3f7cSQing Li LLE_WLOCK(lle); 1135ea537929SGleb Smirnoff lle->la_flags |= LLE_DELETED; 113609fe6320SNavdeep Parhar EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED); 11376cb2b4e7SQing Li #ifdef DIAGNOSTIC 11386e6b3f7cSQing Li log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 11396e6b3f7cSQing Li #endif 11401571132fSOleg Bulyzhin if ((lle->la_flags & 11411571132fSOleg Bulyzhin (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC) 11421571132fSOleg Bulyzhin llentry_free(lle); 11431571132fSOleg Bulyzhin else 11441571132fSOleg Bulyzhin LLE_WUNLOCK(lle); 11456e6b3f7cSQing Li } 11466e6b3f7cSQing Li lle = (void *)-1; 11476e6b3f7cSQing Li 11486e6b3f7cSQing Li } 114942d866ddSBjoern A. Zeeb if (LLE_IS_VALID(lle)) { 11506e6b3f7cSQing Li if (flags & LLE_EXCLUSIVE) 11516e6b3f7cSQing Li LLE_WLOCK(lle); 11526e6b3f7cSQing Li else 11536e6b3f7cSQing Li LLE_RLOCK(lle); 11546e6b3f7cSQing Li } 11556e6b3f7cSQing Li done: 11566e6b3f7cSQing Li return (lle); 11576e6b3f7cSQing Li } 11586e6b3f7cSQing Li 11596e6b3f7cSQing Li static int 11606e6b3f7cSQing Li in_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 11616e6b3f7cSQing Li { 11626e6b3f7cSQing Li #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 11636e6b3f7cSQing Li struct ifnet *ifp = llt->llt_ifp; 11646e6b3f7cSQing Li struct llentry *lle; 11656e6b3f7cSQing Li /* XXX stack use */ 11666e6b3f7cSQing Li struct { 11676e6b3f7cSQing Li struct rt_msghdr rtm; 11689711a168SGleb Smirnoff struct sockaddr_in sin; 11696e6b3f7cSQing Li struct sockaddr_dl sdl; 11706e6b3f7cSQing Li } arpc; 11716e6b3f7cSQing Li int error, i; 11726e6b3f7cSQing Li 1173dc56e98fSRobert Watson LLTABLE_LOCK_ASSERT(); 11746e6b3f7cSQing Li 11756e6b3f7cSQing Li error = 0; 11766e6b3f7cSQing Li for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 11776e6b3f7cSQing Li LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 11786e6b3f7cSQing Li struct sockaddr_dl *sdl; 11796e6b3f7cSQing Li 11806e6b3f7cSQing Li /* skip deleted entries */ 118193704ac5SQing Li if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) 11826e6b3f7cSQing Li continue; 1183813dd6aeSBjoern A. Zeeb /* Skip if jailed and not a valid IP of the prison. */ 1184b89e82ddSJamie Gritton if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 1185813dd6aeSBjoern A. Zeeb continue; 11866e6b3f7cSQing Li /* 11876e6b3f7cSQing Li * produce a msg made of: 11886e6b3f7cSQing Li * struct rt_msghdr; 11899711a168SGleb Smirnoff * struct sockaddr_in; (IPv4) 11906e6b3f7cSQing Li * struct sockaddr_dl; 11916e6b3f7cSQing Li */ 11926e6b3f7cSQing Li bzero(&arpc, sizeof(arpc)); 11936e6b3f7cSQing Li arpc.rtm.rtm_msglen = sizeof(arpc); 1194c0e9a8a1SHartmut Brandt arpc.rtm.rtm_version = RTM_VERSION; 1195c0e9a8a1SHartmut Brandt arpc.rtm.rtm_type = RTM_GET; 1196c0e9a8a1SHartmut Brandt arpc.rtm.rtm_flags = RTF_UP; 1197c0e9a8a1SHartmut Brandt arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 11986e6b3f7cSQing Li arpc.sin.sin_family = AF_INET; 11996e6b3f7cSQing Li arpc.sin.sin_len = sizeof(arpc.sin); 12006e6b3f7cSQing Li arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 12016e6b3f7cSQing Li 12026e6b3f7cSQing Li /* publish */ 12039711a168SGleb Smirnoff if (lle->la_flags & LLE_PUB) 12046e6b3f7cSQing Li arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 12056e6b3f7cSQing Li 12066e6b3f7cSQing Li sdl = &arpc.sdl; 12076e6b3f7cSQing Li sdl->sdl_family = AF_LINK; 12086e6b3f7cSQing Li sdl->sdl_len = sizeof(*sdl); 12096e6b3f7cSQing Li sdl->sdl_index = ifp->if_index; 12106e6b3f7cSQing Li sdl->sdl_type = ifp->if_type; 121193704ac5SQing Li if ((lle->la_flags & LLE_VALID) == LLE_VALID) { 121293704ac5SQing Li sdl->sdl_alen = ifp->if_addrlen; 12136e6b3f7cSQing Li bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 121493704ac5SQing Li } else { 121593704ac5SQing Li sdl->sdl_alen = 0; 121693704ac5SQing Li bzero(LLADDR(sdl), ifp->if_addrlen); 121793704ac5SQing Li } 12186e6b3f7cSQing Li 12196e6b3f7cSQing Li arpc.rtm.rtm_rmx.rmx_expire = 12206e6b3f7cSQing Li lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 12218eca593cSQing Li arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 12226e6b3f7cSQing Li if (lle->la_flags & LLE_STATIC) 12236e6b3f7cSQing Li arpc.rtm.rtm_flags |= RTF_STATIC; 12246e6b3f7cSQing Li arpc.rtm.rtm_index = ifp->if_index; 12256e6b3f7cSQing Li error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 12266e6b3f7cSQing Li if (error) 12276e6b3f7cSQing Li break; 12286e6b3f7cSQing Li } 12296e6b3f7cSQing Li } 12306e6b3f7cSQing Li return error; 12316e6b3f7cSQing Li #undef SIN 12326e6b3f7cSQing Li } 12336e6b3f7cSQing Li 12346e6b3f7cSQing Li void * 12356e6b3f7cSQing Li in_domifattach(struct ifnet *ifp) 12366e6b3f7cSQing Li { 1237d10910e6SBruce M Simpson struct in_ifinfo *ii; 1238d10910e6SBruce M Simpson struct lltable *llt; 12396e6b3f7cSQing Li 1240d10910e6SBruce M Simpson ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1241d10910e6SBruce M Simpson 1242d10910e6SBruce M Simpson llt = lltable_init(ifp, AF_INET); 12436e6b3f7cSQing Li if (llt != NULL) { 1244c9d763bfSQing Li llt->llt_prefix_free = in_lltable_prefix_free; 12456e6b3f7cSQing Li llt->llt_lookup = in_lltable_lookup; 12466e6b3f7cSQing Li llt->llt_dump = in_lltable_dump; 12476e6b3f7cSQing Li } 1248d10910e6SBruce M Simpson ii->ii_llt = llt; 1249d10910e6SBruce M Simpson 1250d10910e6SBruce M Simpson ii->ii_igmp = igmp_domifattach(ifp); 1251d10910e6SBruce M Simpson 1252d10910e6SBruce M Simpson return ii; 12536e6b3f7cSQing Li } 12546e6b3f7cSQing Li 12556e6b3f7cSQing Li void 1256d10910e6SBruce M Simpson in_domifdetach(struct ifnet *ifp, void *aux) 12576e6b3f7cSQing Li { 1258d10910e6SBruce M Simpson struct in_ifinfo *ii = (struct in_ifinfo *)aux; 12596e6b3f7cSQing Li 1260d10910e6SBruce M Simpson igmp_domifdetach(ifp); 1261d10910e6SBruce M Simpson lltable_free(ii->ii_llt); 1262d10910e6SBruce M Simpson free(ii, M_IFADDR); 12636e6b3f7cSQing Li } 1264