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 714d77a549SAlfred Perlstein static int in_mask2len(struct in_addr *); 724d77a549SAlfred Perlstein static void in_len2mask(struct in_addr *, int); 734d77a549SAlfred Perlstein static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 744d77a549SAlfred Perlstein struct ifnet *, struct thread *); 75f7a39160SGleb Smirnoff static int in_aifaddr_ioctl(caddr_t, struct ifnet *, struct thread *); 76f7a39160SGleb Smirnoff static int in_difaddr_ioctl(caddr_t, struct ifnet *, struct thread *); 776a800098SYoshinobu Inoue 784d77a549SAlfred Perlstein static void in_socktrim(struct sockaddr_in *); 79ec002feeSBruce M Simpson static void in_purgemaddrs(struct ifnet *); 80df8bae1dSRodney W. Grimes 8108b68b0eSGleb Smirnoff static VNET_DEFINE(int, nosameprefix); 8208b68b0eSGleb Smirnoff #define V_nosameprefix VNET(nosameprefix) 8308b68b0eSGleb Smirnoff SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_RW, 8408b68b0eSGleb Smirnoff &VNET_NAME(nosameprefix), 0, 851ae95409SGleb Smirnoff "Refuse to create same prefixes on different interfaces"); 86477180fbSGarrett Wollman 8782cea7e6SBjoern A. Zeeb VNET_DECLARE(struct inpcbinfo, ripcbinfo); 8882cea7e6SBjoern A. Zeeb #define V_ripcbinfo VNET(ripcbinfo) 8982cea7e6SBjoern A. Zeeb 90f7a39160SGleb Smirnoff static struct sx in_control_sx; 91f7a39160SGleb Smirnoff SX_SYSINIT(in_control_sx, &in_control_sx, "in_control"); 92f7a39160SGleb Smirnoff 93df8bae1dSRodney W. Grimes /* 94df8bae1dSRodney W. Grimes * Return 1 if an internet address is for a ``local'' host 95b365d954SGleb Smirnoff * (one to which we have a connection). 96df8bae1dSRodney W. Grimes */ 9726f9a767SRodney W. Grimes int 98f2565d68SRobert Watson in_localaddr(struct in_addr in) 99df8bae1dSRodney W. Grimes { 100df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 101df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 102df8bae1dSRodney W. Grimes 1032d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 1042d9cfabaSRobert Watson TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1052d9cfabaSRobert Watson if ((i & ia->ia_subnetmask) == ia->ia_subnet) { 1062d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 1072d9cfabaSRobert Watson return (1); 1082d9cfabaSRobert Watson } 1092d9cfabaSRobert Watson } 1102d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 111df8bae1dSRodney W. Grimes return (0); 112df8bae1dSRodney W. Grimes } 113df8bae1dSRodney W. Grimes 114df8bae1dSRodney W. Grimes /* 1152eccc90bSAndre Oppermann * Return 1 if an internet address is for the local host and configured 1162eccc90bSAndre Oppermann * on one of its interfaces. 1172eccc90bSAndre Oppermann */ 1182eccc90bSAndre Oppermann int 119f2565d68SRobert Watson in_localip(struct in_addr in) 1202eccc90bSAndre Oppermann { 1212eccc90bSAndre Oppermann struct in_ifaddr *ia; 1222eccc90bSAndre Oppermann 1232d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 1242eccc90bSAndre Oppermann LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 1252d9cfabaSRobert Watson if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) { 1262d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 127460473a0SBjoern A. Zeeb return (1); 1282eccc90bSAndre Oppermann } 1292d9cfabaSRobert Watson } 1302d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 131460473a0SBjoern A. Zeeb return (0); 1322eccc90bSAndre Oppermann } 1332eccc90bSAndre Oppermann 1342eccc90bSAndre Oppermann /* 135f7a39160SGleb Smirnoff * Return a reference to the interface address which is different to 136f7a39160SGleb Smirnoff * the supplied one but with same IP address value. 137f7a39160SGleb Smirnoff */ 138f7a39160SGleb Smirnoff static struct in_ifaddr * 139f7a39160SGleb Smirnoff in_localip_more(struct in_ifaddr *ia) 140f7a39160SGleb Smirnoff { 141f7a39160SGleb Smirnoff in_addr_t in = IA_SIN(ia)->sin_addr.s_addr; 142f7a39160SGleb Smirnoff struct in_ifaddr *it; 143f7a39160SGleb Smirnoff 144f7a39160SGleb Smirnoff IN_IFADDR_RLOCK(); 145f7a39160SGleb Smirnoff LIST_FOREACH(it, INADDR_HASH(in), ia_hash) { 146f7a39160SGleb Smirnoff if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) { 147f7a39160SGleb Smirnoff ifa_ref(&it->ia_ifa); 148f7a39160SGleb Smirnoff IN_IFADDR_RUNLOCK(); 149f7a39160SGleb Smirnoff return (it); 150f7a39160SGleb Smirnoff } 151f7a39160SGleb Smirnoff } 152f7a39160SGleb Smirnoff IN_IFADDR_RUNLOCK(); 153f7a39160SGleb Smirnoff 154f7a39160SGleb Smirnoff return (NULL); 155f7a39160SGleb Smirnoff } 156f7a39160SGleb Smirnoff 157f7a39160SGleb Smirnoff /* 158df8bae1dSRodney W. Grimes * Determine whether an IP address is in a reserved set of addresses 159df8bae1dSRodney W. Grimes * that may not be forwarded, or whether datagrams to that destination 160df8bae1dSRodney W. Grimes * may be forwarded. 161df8bae1dSRodney W. Grimes */ 16226f9a767SRodney W. Grimes int 163f2565d68SRobert Watson in_canforward(struct in_addr in) 164df8bae1dSRodney W. Grimes { 165df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 166df8bae1dSRodney W. Grimes register u_long net; 167df8bae1dSRodney W. Grimes 168f8429ca2SBruce M Simpson if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i)) 169df8bae1dSRodney W. Grimes return (0); 170df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) { 171df8bae1dSRodney W. Grimes net = i & IN_CLASSA_NET; 172df8bae1dSRodney W. Grimes if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 173df8bae1dSRodney W. Grimes return (0); 174df8bae1dSRodney W. Grimes } 175df8bae1dSRodney W. Grimes return (1); 176df8bae1dSRodney W. Grimes } 177df8bae1dSRodney W. Grimes 178df8bae1dSRodney W. Grimes /* 179df8bae1dSRodney W. Grimes * Trim a mask in a sockaddr 180df8bae1dSRodney W. Grimes */ 1810312fbe9SPoul-Henning Kamp static void 182f2565d68SRobert Watson in_socktrim(struct sockaddr_in *ap) 183df8bae1dSRodney W. Grimes { 184df8bae1dSRodney W. Grimes register char *cplim = (char *) &ap->sin_addr; 185df8bae1dSRodney W. Grimes register char *cp = (char *) (&ap->sin_addr + 1); 186df8bae1dSRodney W. Grimes 187df8bae1dSRodney W. Grimes ap->sin_len = 0; 188df00058dSGarrett Wollman while (--cp >= cplim) 189df8bae1dSRodney W. Grimes if (*cp) { 190df8bae1dSRodney W. Grimes (ap)->sin_len = cp - (char *) (ap) + 1; 191df8bae1dSRodney W. Grimes break; 192df8bae1dSRodney W. Grimes } 193df8bae1dSRodney W. Grimes } 194df8bae1dSRodney W. Grimes 1956a800098SYoshinobu Inoue static int 1966a800098SYoshinobu Inoue in_mask2len(mask) 1976a800098SYoshinobu Inoue struct in_addr *mask; 1986a800098SYoshinobu Inoue { 1996a800098SYoshinobu Inoue int x, y; 2006a800098SYoshinobu Inoue u_char *p; 2016a800098SYoshinobu Inoue 2026a800098SYoshinobu Inoue p = (u_char *)mask; 2036a800098SYoshinobu Inoue for (x = 0; x < sizeof(*mask); x++) { 2046a800098SYoshinobu Inoue if (p[x] != 0xff) 2056a800098SYoshinobu Inoue break; 2066a800098SYoshinobu Inoue } 2076a800098SYoshinobu Inoue y = 0; 2086a800098SYoshinobu Inoue if (x < sizeof(*mask)) { 2096a800098SYoshinobu Inoue for (y = 0; y < 8; y++) { 2106a800098SYoshinobu Inoue if ((p[x] & (0x80 >> y)) == 0) 2116a800098SYoshinobu Inoue break; 2126a800098SYoshinobu Inoue } 2136a800098SYoshinobu Inoue } 214460473a0SBjoern A. Zeeb return (x * 8 + y); 2156a800098SYoshinobu Inoue } 2166a800098SYoshinobu Inoue 2176a800098SYoshinobu Inoue static void 218f2565d68SRobert Watson in_len2mask(struct in_addr *mask, int len) 2196a800098SYoshinobu Inoue { 2206a800098SYoshinobu Inoue int i; 2216a800098SYoshinobu Inoue u_char *p; 2226a800098SYoshinobu Inoue 2236a800098SYoshinobu Inoue p = (u_char *)mask; 2246a800098SYoshinobu Inoue bzero(mask, sizeof(*mask)); 2256a800098SYoshinobu Inoue for (i = 0; i < len / 8; i++) 2266a800098SYoshinobu Inoue p[i] = 0xff; 2276a800098SYoshinobu Inoue if (len % 8) 2286a800098SYoshinobu Inoue p[i] = (0xff00 >> (len % 8)) & 0xff; 2296a800098SYoshinobu Inoue } 2306a800098SYoshinobu Inoue 231df8bae1dSRodney W. Grimes /* 232df8bae1dSRodney W. Grimes * Generic internet control operations (ioctl's). 233df8bae1dSRodney W. Grimes */ 23426f9a767SRodney W. Grimes int 235f2565d68SRobert Watson in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 236f2565d68SRobert Watson struct thread *td) 237df8bae1dSRodney W. Grimes { 238f7a39160SGleb Smirnoff struct ifreq *ifr = (struct ifreq *)data; 239f7a39160SGleb Smirnoff struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr; 240f7a39160SGleb Smirnoff struct in_ifaddr *ia; 241f7a39160SGleb Smirnoff int error; 242f7a39160SGleb Smirnoff 243f7a39160SGleb Smirnoff if (ifp == NULL) 244f7a39160SGleb Smirnoff return (EADDRNOTAVAIL); 24571212473SGleb Smirnoff 24671212473SGleb Smirnoff /* 247f7a39160SGleb Smirnoff * Filter out 4 ioctls we implement directly. Forward the rest 248f7a39160SGleb Smirnoff * to specific functions and ifp->if_ioctl(). 249bbb3fb61SRobert Watson */ 2506a800098SYoshinobu Inoue switch (cmd) { 251bbb3fb61SRobert Watson case SIOCGIFADDR: 252bbb3fb61SRobert Watson case SIOCGIFBRDADDR: 253bbb3fb61SRobert Watson case SIOCGIFDSTADDR: 254bbb3fb61SRobert Watson case SIOCGIFNETMASK: 255f7a39160SGleb Smirnoff break; 2566d00fd9cSGleb Smirnoff case SIOCDIFADDR: 257f7a39160SGleb Smirnoff sx_xlock(&in_control_sx); 258f7a39160SGleb Smirnoff error = in_difaddr_ioctl(data, ifp, td); 259f7a39160SGleb Smirnoff sx_xunlock(&in_control_sx); 260f7a39160SGleb Smirnoff return (error); 2616d00fd9cSGleb Smirnoff case SIOCAIFADDR: 262f7a39160SGleb Smirnoff sx_xlock(&in_control_sx); 263f7a39160SGleb Smirnoff error = in_aifaddr_ioctl(data, ifp, td); 264f7a39160SGleb Smirnoff sx_xunlock(&in_control_sx); 265f7a39160SGleb Smirnoff return (error); 266f7a39160SGleb Smirnoff case SIOCALIFADDR: 267f7a39160SGleb Smirnoff case SIOCDLIFADDR: 268f7a39160SGleb Smirnoff case SIOCGLIFADDR: 269f7a39160SGleb Smirnoff return (in_lifaddr_ioctl(so, cmd, data, ifp, td)); 270bbb3fb61SRobert Watson case SIOCSIFADDR: 271bbb3fb61SRobert Watson case SIOCSIFBRDADDR: 272bbb3fb61SRobert Watson case SIOCSIFDSTADDR: 273bbb3fb61SRobert Watson case SIOCSIFNETMASK: 27456cf9dc1SGleb Smirnoff /* We no longer support that old commands. */ 2756d00fd9cSGleb Smirnoff return (EINVAL); 276bbb3fb61SRobert Watson default: 277f7a39160SGleb Smirnoff if (ifp->if_ioctl == NULL) 278bbb3fb61SRobert Watson return (EOPNOTSUPP); 279bbb3fb61SRobert Watson return ((*ifp->if_ioctl)(ifp, cmd, data)); 2806a800098SYoshinobu Inoue } 2816a800098SYoshinobu Inoue 282cf7b18f1SRobert Watson /* 283df8bae1dSRodney W. Grimes * Find address for this interface, if it exists. 284df8bae1dSRodney W. Grimes */ 2852d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 286f7a39160SGleb Smirnoff LIST_FOREACH(ia, INADDR_HASH(addr->sin_addr.s_addr), ia_hash) { 287f7a39160SGleb Smirnoff if (ia->ia_ifp == ifp && 288f7a39160SGleb Smirnoff ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 289f7a39160SGleb Smirnoff prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) 290df8bae1dSRodney W. Grimes break; 291ca925d9cSJonathan Lemon } 292f7a39160SGleb Smirnoff 293bbb3fb61SRobert Watson if (ia == NULL) { 294f7a39160SGleb Smirnoff IN_IFADDR_RUNLOCK(); 295f7a39160SGleb Smirnoff return (EADDRNOTAVAIL); 296ac0aa473SBill Fenner } 297df8bae1dSRodney W. Grimes 298588885f2SRobert Watson error = 0; 299df8bae1dSRodney W. Grimes switch (cmd) { 300f7a39160SGleb Smirnoff case SIOCGIFADDR: 301f7a39160SGleb Smirnoff *addr = ia->ia_addr; 302f7a39160SGleb Smirnoff break; 3038c0fec80SRobert Watson 304f7a39160SGleb Smirnoff case SIOCGIFBRDADDR: 305f7a39160SGleb Smirnoff if ((ifp->if_flags & IFF_BROADCAST) == 0) { 306f7a39160SGleb Smirnoff error = EINVAL; 307df8bae1dSRodney W. Grimes break; 308df8bae1dSRodney W. Grimes } 309f7a39160SGleb Smirnoff *addr = ia->ia_broadaddr; 310f7a39160SGleb Smirnoff break; 311f7a39160SGleb Smirnoff 312f7a39160SGleb Smirnoff case SIOCGIFDSTADDR: 313f7a39160SGleb Smirnoff if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 314f7a39160SGleb Smirnoff error = EINVAL; 315f7a39160SGleb Smirnoff break; 316f7a39160SGleb Smirnoff } 317f7a39160SGleb Smirnoff *addr = ia->ia_dstaddr; 318f7a39160SGleb Smirnoff break; 319f7a39160SGleb Smirnoff 320f7a39160SGleb Smirnoff case SIOCGIFNETMASK: 321f7a39160SGleb Smirnoff *addr = ia->ia_sockmask; 322f7a39160SGleb Smirnoff break; 323f7a39160SGleb Smirnoff } 324f7a39160SGleb Smirnoff 3252d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 326f7a39160SGleb Smirnoff 327f7a39160SGleb Smirnoff return (error); 3281067217dSGarrett Wollman } 329f7a39160SGleb Smirnoff 330f7a39160SGleb Smirnoff static int 331f7a39160SGleb Smirnoff in_aifaddr_ioctl(caddr_t data, struct ifnet *ifp, struct thread *td) 332f7a39160SGleb Smirnoff { 333f7a39160SGleb Smirnoff const struct in_aliasreq *ifra = (struct in_aliasreq *)data; 334f7a39160SGleb Smirnoff const struct sockaddr_in *addr = &ifra->ifra_addr; 335f7a39160SGleb Smirnoff const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr; 336f7a39160SGleb Smirnoff const struct sockaddr_in *mask = &ifra->ifra_mask; 337f7a39160SGleb Smirnoff const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr; 338f7a39160SGleb Smirnoff const int vhid = ifra->ifra_vhid; 339f7a39160SGleb Smirnoff struct ifaddr *ifa; 340f7a39160SGleb Smirnoff struct in_ifaddr *ia; 341f7a39160SGleb Smirnoff bool iaIsFirst; 342f7a39160SGleb Smirnoff int error = 0; 343f7a39160SGleb Smirnoff 344f7a39160SGleb Smirnoff error = priv_check(td, PRIV_NET_ADDIFADDR); 345f7a39160SGleb Smirnoff if (error) 346f7a39160SGleb Smirnoff return (error); 347f7a39160SGleb Smirnoff 348f7a39160SGleb Smirnoff /* 349f7a39160SGleb Smirnoff * ifra_addr must be present and be of INET family. 350f7a39160SGleb Smirnoff * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional. 351f7a39160SGleb Smirnoff */ 352f7a39160SGleb Smirnoff if (addr->sin_len != sizeof(struct sockaddr_in) || 353f7a39160SGleb Smirnoff addr->sin_family != AF_INET) 354f7a39160SGleb Smirnoff return (EINVAL); 355f7a39160SGleb Smirnoff if (broadaddr->sin_len != 0 && 356f7a39160SGleb Smirnoff (broadaddr->sin_len != sizeof(struct sockaddr_in) || 357f7a39160SGleb Smirnoff broadaddr->sin_family != AF_INET)) 358f7a39160SGleb Smirnoff return (EINVAL); 359f7a39160SGleb Smirnoff if (mask->sin_len != 0 && 360f7a39160SGleb Smirnoff (mask->sin_len != sizeof(struct sockaddr_in) || 361f7a39160SGleb Smirnoff mask->sin_family != AF_INET)) 362f7a39160SGleb Smirnoff return (EINVAL); 363f7a39160SGleb Smirnoff if ((ifp->if_flags & IFF_POINTOPOINT) && 364f7a39160SGleb Smirnoff (dstaddr->sin_len != sizeof(struct sockaddr_in) || 365f7a39160SGleb Smirnoff dstaddr->sin_addr.s_addr == INADDR_ANY)) 366f7a39160SGleb Smirnoff return (EDESTADDRREQ); 367f7a39160SGleb Smirnoff if (vhid > 0 && carp_attach_p == NULL) 368f7a39160SGleb Smirnoff return (EPROTONOSUPPORT); 369f7a39160SGleb Smirnoff 370f7a39160SGleb Smirnoff /* 371f7a39160SGleb Smirnoff * See whether address already exist. 372f7a39160SGleb Smirnoff */ 373f7a39160SGleb Smirnoff iaIsFirst = true; 374f7a39160SGleb Smirnoff ia = NULL; 375f7a39160SGleb Smirnoff IF_ADDR_RLOCK(ifp); 376f7a39160SGleb Smirnoff TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 377f7a39160SGleb Smirnoff struct in_ifaddr *it = ifatoia(ifa); 378f7a39160SGleb Smirnoff 379f7a39160SGleb Smirnoff if (it->ia_addr.sin_family != AF_INET) 380f7a39160SGleb Smirnoff continue; 381f7a39160SGleb Smirnoff 382f7a39160SGleb Smirnoff iaIsFirst = false; 383f7a39160SGleb Smirnoff if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 384f7a39160SGleb Smirnoff prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) 385f7a39160SGleb Smirnoff ia = it; 3861067217dSGarrett Wollman } 387f7a39160SGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 388f7a39160SGleb Smirnoff 389f7a39160SGleb Smirnoff if (ia != NULL) 390f7a39160SGleb Smirnoff (void )in_difaddr_ioctl(data, ifp, td); 391f7a39160SGleb Smirnoff 39246758960SGleb Smirnoff ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK); 39346758960SGleb Smirnoff ia = (struct in_ifaddr *)ifa; 39459562606SGarrett Wollman ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 39559562606SGarrett Wollman ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 39659562606SGarrett Wollman ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 39719fc74fbSJeffrey Hsu 398f7a39160SGleb Smirnoff ia->ia_ifp = ifp; 399f7a39160SGleb Smirnoff ia->ia_ifa.ifa_metric = ifp->if_metric; 400f7a39160SGleb Smirnoff ia->ia_addr = *addr; 401f7a39160SGleb Smirnoff if (mask->sin_len != 0) { 402f7a39160SGleb Smirnoff ia->ia_sockmask = *mask; 403f7a39160SGleb Smirnoff ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 404f7a39160SGleb Smirnoff } else { 405f7a39160SGleb Smirnoff in_addr_t i = ntohl(addr->sin_addr.s_addr); 406f7a39160SGleb Smirnoff 407f7a39160SGleb Smirnoff /* 408f7a39160SGleb Smirnoff * Be compatible with network classes, if netmask isn't 409f7a39160SGleb Smirnoff * supplied, guess it based on classes. 410f7a39160SGleb Smirnoff */ 411f7a39160SGleb Smirnoff if (IN_CLASSA(i)) 412f7a39160SGleb Smirnoff ia->ia_subnetmask = IN_CLASSA_NET; 413f7a39160SGleb Smirnoff else if (IN_CLASSB(i)) 414f7a39160SGleb Smirnoff ia->ia_subnetmask = IN_CLASSB_NET; 415f7a39160SGleb Smirnoff else 416f7a39160SGleb Smirnoff ia->ia_subnetmask = IN_CLASSC_NET; 417f7a39160SGleb Smirnoff ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 418f7a39160SGleb Smirnoff } 419f7a39160SGleb Smirnoff ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask; 420f7a39160SGleb Smirnoff in_socktrim(&ia->ia_sockmask); 421f7a39160SGleb Smirnoff 422df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 423f7a39160SGleb Smirnoff if (broadaddr->sin_len != 0) { 424f7a39160SGleb Smirnoff ia->ia_broadaddr = *broadaddr; 425f7a39160SGleb Smirnoff } else if (ia->ia_subnetmask == IN_RFC3021_MASK) { 426f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST; 427f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 428f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_family = AF_INET; 429f7a39160SGleb Smirnoff } else { 430f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_addr.s_addr = 431f7a39160SGleb Smirnoff htonl(ia->ia_subnet | ~ia->ia_subnetmask); 432f7a39160SGleb Smirnoff ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 433df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_family = AF_INET; 434df8bae1dSRodney W. Grimes } 435f7a39160SGleb Smirnoff } 436f7a39160SGleb Smirnoff 437f7a39160SGleb Smirnoff if (ifp->if_flags & IFF_POINTOPOINT) 438f7a39160SGleb Smirnoff ia->ia_dstaddr = *dstaddr; 439f7a39160SGleb Smirnoff 440f7a39160SGleb Smirnoff /* XXXGL: rtinit() needs this strange assignment. */ 441f7a39160SGleb Smirnoff if (ifp->if_flags & IFF_LOOPBACK) 442f7a39160SGleb Smirnoff ia->ia_dstaddr = ia->ia_addr; 443f3d30eb2SGleb Smirnoff 4448c0fec80SRobert Watson ifa_ref(ifa); /* if_addrhead */ 445137f91e8SJohn Baldwin IF_ADDR_WLOCK(ifp); 4468021456aSRobert Watson TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 447137f91e8SJohn Baldwin IF_ADDR_WUNLOCK(ifp); 448f7a39160SGleb Smirnoff 4498c0fec80SRobert Watson ifa_ref(ifa); /* in_ifaddrhead */ 4502d9cfabaSRobert Watson IN_IFADDR_WLOCK(); 451603724d3SBjoern A. Zeeb TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 452f7a39160SGleb Smirnoff LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 4532d9cfabaSRobert Watson IN_IFADDR_WUNLOCK(); 454df8bae1dSRodney W. Grimes 455f7a39160SGleb Smirnoff if (vhid != 0) 456f7a39160SGleb Smirnoff error = (*carp_attach_p)(&ia->ia_ifa, vhid); 457f7a39160SGleb Smirnoff if (error) 458f7a39160SGleb Smirnoff goto fail1; 459f7a39160SGleb Smirnoff 460f7a39160SGleb Smirnoff /* 461f7a39160SGleb Smirnoff * Give the interface a chance to initialize 462f7a39160SGleb Smirnoff * if this is its first address, 463f7a39160SGleb Smirnoff * and to validate the address if necessary. 464f7a39160SGleb Smirnoff */ 465f7a39160SGleb Smirnoff if (ifp->if_ioctl != NULL) 466f7a39160SGleb Smirnoff error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 467f7a39160SGleb Smirnoff if (error) 468f7a39160SGleb Smirnoff goto fail2; 469f7a39160SGleb Smirnoff 470f7a39160SGleb Smirnoff /* 471f7a39160SGleb Smirnoff * Add route for the network. 472f7a39160SGleb Smirnoff */ 473f7a39160SGleb Smirnoff if (vhid == 0) { 474f7a39160SGleb Smirnoff int flags = RTF_UP; 475f7a39160SGleb Smirnoff 476f7a39160SGleb Smirnoff if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 477f7a39160SGleb Smirnoff flags |= RTF_HOST; 478f7a39160SGleb Smirnoff 479f7a39160SGleb Smirnoff error = in_addprefix(ia, flags); 480f7a39160SGleb Smirnoff if (error) 481f7a39160SGleb Smirnoff goto fail2; 482df8bae1dSRodney W. Grimes } 483df8bae1dSRodney W. Grimes 484588885f2SRobert Watson /* 485f7a39160SGleb Smirnoff * Add a loopback route to self. 486588885f2SRobert Watson */ 487f7a39160SGleb Smirnoff if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 && 488f7a39160SGleb Smirnoff ia->ia_addr.sin_addr.s_addr != INADDR_ANY) { 489f7a39160SGleb Smirnoff struct in_ifaddr *eia; 490df8bae1dSRodney W. Grimes 491f7a39160SGleb Smirnoff eia = in_localip_more(ia); 492f7a39160SGleb Smirnoff 493f7a39160SGleb Smirnoff if (eia == NULL) { 494f7a39160SGleb Smirnoff error = ifa_add_loopback_route((struct ifaddr *)ia, 495f7a39160SGleb Smirnoff (struct sockaddr *)&ia->ia_addr); 496f7a39160SGleb Smirnoff if (error) 497f7a39160SGleb Smirnoff goto fail3; 498f7a39160SGleb Smirnoff } else 499f7a39160SGleb Smirnoff ifa_free(&eia->ia_ifa); 500588885f2SRobert Watson } 501df8bae1dSRodney W. Grimes 502f7a39160SGleb Smirnoff if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) { 503f7a39160SGleb Smirnoff struct in_addr allhosts_addr; 504f7a39160SGleb Smirnoff struct in_ifinfo *ii; 505df8bae1dSRodney W. Grimes 506c75aa354SBruce M Simpson ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 507f7a39160SGleb Smirnoff allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 508df8bae1dSRodney W. Grimes 509f7a39160SGleb Smirnoff error = in_joingroup(ifp, &allhosts_addr, NULL, 510f7a39160SGleb Smirnoff &ii->ii_allhosts); 511f7a39160SGleb Smirnoff } 512f7a39160SGleb Smirnoff 513f7a39160SGleb Smirnoff EVENTHANDLER_INVOKE(ifaddr_event, ifp); 514f7a39160SGleb Smirnoff 515f7a39160SGleb Smirnoff return (error); 516f7a39160SGleb Smirnoff 517f7a39160SGleb Smirnoff fail3: 518f7a39160SGleb Smirnoff if (vhid == 0) 519f7a39160SGleb Smirnoff (void )in_scrubprefix(ia, LLE_STATIC); 520f7a39160SGleb Smirnoff 521f7a39160SGleb Smirnoff fail2: 522f7a39160SGleb Smirnoff if (ia->ia_ifa.ifa_carp) 523f7a39160SGleb Smirnoff (*carp_detach_p)(&ia->ia_ifa); 524f7a39160SGleb Smirnoff 525f7a39160SGleb Smirnoff fail1: 526f7a39160SGleb Smirnoff IF_ADDR_WLOCK(ifp); 527f7a39160SGleb Smirnoff TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 528f7a39160SGleb Smirnoff IF_ADDR_WUNLOCK(ifp); 529f7a39160SGleb Smirnoff ifa_free(&ia->ia_ifa); 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(); 535f7a39160SGleb Smirnoff ifa_free(&ia->ia_ifa); 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; 544*6224cd89SNathan Whitehorn const struct sockaddr_in *addr = (const struct sockaddr_in *) 545*6224cd89SNathan 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) { 567f7a39160SGleb Smirnoff struct in_ifaddr *it = ifatoia(ifa); 568f7a39160SGleb Smirnoff 569f7a39160SGleb Smirnoff if (it->ia_addr.sin_family != AF_INET) 570f7a39160SGleb Smirnoff continue; 571f7a39160SGleb Smirnoff 572f7a39160SGleb Smirnoff if (deleteAny && ia == NULL && (td == NULL || 573f7a39160SGleb Smirnoff prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0)) 574f7a39160SGleb Smirnoff ia = it; 575f7a39160SGleb Smirnoff 576f7a39160SGleb Smirnoff if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 577f7a39160SGleb Smirnoff (td == NULL || prison_check_ip4(td->td_ucred, 578f7a39160SGleb Smirnoff &addr->sin_addr) == 0)) 579f7a39160SGleb Smirnoff ia = it; 580f7a39160SGleb Smirnoff 581f7a39160SGleb Smirnoff if (it != ia) 582f7a39160SGleb Smirnoff iaIsLast = false; 583f7a39160SGleb Smirnoff } 584f7a39160SGleb Smirnoff 585f7a39160SGleb Smirnoff if (ia == NULL) { 586f7a39160SGleb Smirnoff IF_ADDR_WUNLOCK(ifp); 587f7a39160SGleb Smirnoff return (EADDRNOTAVAIL); 588f7a39160SGleb Smirnoff } 589f7a39160SGleb Smirnoff 590f7a39160SGleb Smirnoff TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 591f7a39160SGleb Smirnoff IF_ADDR_WUNLOCK(ifp); 592f7a39160SGleb Smirnoff ifa_free(&ia->ia_ifa); /* if_addrhead */ 593f7a39160SGleb Smirnoff 594f7a39160SGleb Smirnoff IN_IFADDR_WLOCK(); 595f7a39160SGleb Smirnoff TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 596f7a39160SGleb Smirnoff LIST_REMOVE(ia, ia_hash); 597f7a39160SGleb Smirnoff IN_IFADDR_WUNLOCK(); 598f7a39160SGleb Smirnoff ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 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); 634f7a39160SGleb Smirnoff 635f7a39160SGleb Smirnoff return (0); 636df8bae1dSRodney W. Grimes } 637df8bae1dSRodney W. Grimes 638df8bae1dSRodney W. Grimes /* 6396a800098SYoshinobu Inoue * SIOC[GAD]LIFADDR. 6406a800098SYoshinobu Inoue * SIOCGLIFADDR: get first address. (?!?) 6416a800098SYoshinobu Inoue * SIOCGLIFADDR with IFLR_PREFIX: 6426a800098SYoshinobu Inoue * get first address that matches the specified prefix. 6436a800098SYoshinobu Inoue * SIOCALIFADDR: add the specified address. 6446a800098SYoshinobu Inoue * SIOCALIFADDR with IFLR_PREFIX: 6456a800098SYoshinobu Inoue * EINVAL since we can't deduce hostid part of the address. 6466a800098SYoshinobu Inoue * SIOCDLIFADDR: delete the specified address. 6476a800098SYoshinobu Inoue * SIOCDLIFADDR with IFLR_PREFIX: 6486a800098SYoshinobu Inoue * delete the first address that matches the specified prefix. 6496a800098SYoshinobu Inoue * return values: 6506a800098SYoshinobu Inoue * EINVAL on invalid parameters 6516a800098SYoshinobu Inoue * EADDRNOTAVAIL on prefix match failed/specified address not found 6526a800098SYoshinobu Inoue * other values may be returned from in_ioctl() 6536a800098SYoshinobu Inoue */ 6546a800098SYoshinobu Inoue static int 655f2565d68SRobert Watson in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 656f2565d68SRobert Watson struct ifnet *ifp, struct thread *td) 6576a800098SYoshinobu Inoue { 6586a800098SYoshinobu Inoue struct if_laddrreq *iflr = (struct if_laddrreq *)data; 6596a800098SYoshinobu Inoue struct ifaddr *ifa; 660f7a39160SGleb Smirnoff int error; 6616a800098SYoshinobu Inoue 662f7a39160SGleb Smirnoff switch (cmd) { 663f7a39160SGleb Smirnoff case SIOCALIFADDR: 664f7a39160SGleb Smirnoff if (td != NULL) { 665f7a39160SGleb Smirnoff error = priv_check(td, PRIV_NET_ADDIFADDR); 666f7a39160SGleb Smirnoff if (error) 667f7a39160SGleb Smirnoff return (error); 668f7a39160SGleb Smirnoff } 669f7a39160SGleb Smirnoff break; 670f7a39160SGleb Smirnoff case SIOCDLIFADDR: 671f7a39160SGleb Smirnoff if (td != NULL) { 672f7a39160SGleb Smirnoff error = priv_check(td, PRIV_NET_DELIFADDR); 673f7a39160SGleb Smirnoff if (error) 674f7a39160SGleb Smirnoff return (error); 675f7a39160SGleb Smirnoff } 676f7a39160SGleb Smirnoff break; 6776a800098SYoshinobu Inoue } 6786a800098SYoshinobu Inoue 6796a800098SYoshinobu Inoue switch (cmd) { 6806a800098SYoshinobu Inoue case SIOCGLIFADDR: 6816a800098SYoshinobu Inoue /* address must be specified on GET with IFLR_PREFIX */ 6826a800098SYoshinobu Inoue if ((iflr->flags & IFLR_PREFIX) == 0) 6836a800098SYoshinobu Inoue break; 6846a800098SYoshinobu Inoue /*FALLTHROUGH*/ 6856a800098SYoshinobu Inoue case SIOCALIFADDR: 6866a800098SYoshinobu Inoue case SIOCDLIFADDR: 6876a800098SYoshinobu Inoue /* address must be specified on ADD and DELETE */ 6885d60ed0eSYoshinobu Inoue if (iflr->addr.ss_family != AF_INET) 689460473a0SBjoern A. Zeeb return (EINVAL); 6905d60ed0eSYoshinobu Inoue if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 691460473a0SBjoern A. Zeeb return (EINVAL); 6926a800098SYoshinobu Inoue /* XXX need improvement */ 6935d60ed0eSYoshinobu Inoue if (iflr->dstaddr.ss_family 6945d60ed0eSYoshinobu Inoue && iflr->dstaddr.ss_family != AF_INET) 695460473a0SBjoern A. Zeeb return (EINVAL); 6965d60ed0eSYoshinobu Inoue if (iflr->dstaddr.ss_family 6975d60ed0eSYoshinobu Inoue && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 698460473a0SBjoern A. Zeeb return (EINVAL); 6996a800098SYoshinobu Inoue break; 7006a800098SYoshinobu Inoue default: /*shouldn't happen*/ 701460473a0SBjoern A. Zeeb return (EOPNOTSUPP); 7026a800098SYoshinobu Inoue } 7036a800098SYoshinobu Inoue if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 704460473a0SBjoern A. Zeeb return (EINVAL); 7056a800098SYoshinobu Inoue 7066a800098SYoshinobu Inoue switch (cmd) { 7076a800098SYoshinobu Inoue case SIOCALIFADDR: 7086a800098SYoshinobu Inoue { 7096a800098SYoshinobu Inoue struct in_aliasreq ifra; 7106a800098SYoshinobu Inoue 7116a800098SYoshinobu Inoue if (iflr->flags & IFLR_PREFIX) 712460473a0SBjoern A. Zeeb return (EINVAL); 7136a800098SYoshinobu Inoue 7140823c29bSJohn Baldwin /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */ 7156a800098SYoshinobu Inoue bzero(&ifra, sizeof(ifra)); 7166a800098SYoshinobu Inoue bcopy(iflr->iflr_name, ifra.ifra_name, 7176a800098SYoshinobu Inoue sizeof(ifra.ifra_name)); 7186a800098SYoshinobu Inoue 7195d60ed0eSYoshinobu Inoue bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 7206a800098SYoshinobu Inoue 7215d60ed0eSYoshinobu Inoue if (iflr->dstaddr.ss_family) { /*XXX*/ 7226a800098SYoshinobu Inoue bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 7235d60ed0eSYoshinobu Inoue iflr->dstaddr.ss_len); 7246a800098SYoshinobu Inoue } 7256a800098SYoshinobu Inoue 7266a800098SYoshinobu Inoue ifra.ifra_mask.sin_family = AF_INET; 7276a800098SYoshinobu Inoue ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 7286a800098SYoshinobu Inoue in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 7296a800098SYoshinobu Inoue 730460473a0SBjoern A. Zeeb return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td)); 7316a800098SYoshinobu Inoue } 7326a800098SYoshinobu Inoue case SIOCGLIFADDR: 7336a800098SYoshinobu Inoue case SIOCDLIFADDR: 7346a800098SYoshinobu Inoue { 7356a800098SYoshinobu Inoue struct in_ifaddr *ia; 7366a800098SYoshinobu Inoue struct in_addr mask, candidate, match; 7376a800098SYoshinobu Inoue struct sockaddr_in *sin; 7386a800098SYoshinobu Inoue 7396a800098SYoshinobu Inoue bzero(&mask, sizeof(mask)); 740fbdd20a1SMatt Jacob bzero(&match, sizeof(match)); 7416a800098SYoshinobu Inoue if (iflr->flags & IFLR_PREFIX) { 7426a800098SYoshinobu Inoue /* lookup a prefix rather than address. */ 7436a800098SYoshinobu Inoue in_len2mask(&mask, iflr->prefixlen); 7446a800098SYoshinobu Inoue 7456a800098SYoshinobu Inoue sin = (struct sockaddr_in *)&iflr->addr; 7466a800098SYoshinobu Inoue match.s_addr = sin->sin_addr.s_addr; 7476a800098SYoshinobu Inoue match.s_addr &= mask.s_addr; 7486a800098SYoshinobu Inoue 7496a800098SYoshinobu Inoue /* if you set extra bits, that's wrong */ 7506a800098SYoshinobu Inoue if (match.s_addr != sin->sin_addr.s_addr) 751460473a0SBjoern A. Zeeb return (EINVAL); 7526a800098SYoshinobu Inoue 7536a800098SYoshinobu Inoue } else { 7546a800098SYoshinobu Inoue /* on getting an address, take the 1st match */ 7556a800098SYoshinobu Inoue /* on deleting an address, do exact match */ 756fbdd20a1SMatt Jacob if (cmd != SIOCGLIFADDR) { 7576a800098SYoshinobu Inoue in_len2mask(&mask, 32); 7586a800098SYoshinobu Inoue sin = (struct sockaddr_in *)&iflr->addr; 7596a800098SYoshinobu Inoue match.s_addr = sin->sin_addr.s_addr; 7606a800098SYoshinobu Inoue } 7616a800098SYoshinobu Inoue } 7626a800098SYoshinobu Inoue 763137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 7646a800098SYoshinobu Inoue TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 7650823c29bSJohn Baldwin if (ifa->ifa_addr->sa_family != AF_INET) 7666a800098SYoshinobu Inoue continue; 767fbdd20a1SMatt Jacob if (match.s_addr == 0) 7686a800098SYoshinobu Inoue break; 7690f188ebbSJohn Baldwin sin = (struct sockaddr_in *)&ifa->ifa_addr; 7700f188ebbSJohn Baldwin candidate.s_addr = sin->sin_addr.s_addr; 7716a800098SYoshinobu Inoue candidate.s_addr &= mask.s_addr; 7726a800098SYoshinobu Inoue if (candidate.s_addr == match.s_addr) 7736a800098SYoshinobu Inoue break; 7746a800098SYoshinobu Inoue } 77556d6e129SJohn Baldwin if (ifa != NULL) 77656d6e129SJohn Baldwin ifa_ref(ifa); 777137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 778460473a0SBjoern A. Zeeb if (ifa == NULL) 779460473a0SBjoern A. Zeeb return (EADDRNOTAVAIL); 7806a800098SYoshinobu Inoue ia = (struct in_ifaddr *)ifa; 7816a800098SYoshinobu Inoue 7826a800098SYoshinobu Inoue if (cmd == SIOCGLIFADDR) { 7836a800098SYoshinobu Inoue /* fill in the if_laddrreq structure */ 7846a800098SYoshinobu Inoue bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 7856a800098SYoshinobu Inoue 7866a800098SYoshinobu Inoue if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 7876a800098SYoshinobu Inoue bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 7886a800098SYoshinobu Inoue ia->ia_dstaddr.sin_len); 7896a800098SYoshinobu Inoue } else 7906a800098SYoshinobu Inoue bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 7916a800098SYoshinobu Inoue 7926a800098SYoshinobu Inoue iflr->prefixlen = 7936a800098SYoshinobu Inoue in_mask2len(&ia->ia_sockmask.sin_addr); 7946a800098SYoshinobu Inoue 7956a800098SYoshinobu Inoue iflr->flags = 0; /*XXX*/ 79656d6e129SJohn Baldwin ifa_free(ifa); 7976a800098SYoshinobu Inoue 798460473a0SBjoern A. Zeeb return (0); 7996a800098SYoshinobu Inoue } else { 8006a800098SYoshinobu Inoue struct in_aliasreq ifra; 8016a800098SYoshinobu Inoue 8020823c29bSJohn Baldwin /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */ 8036a800098SYoshinobu Inoue bzero(&ifra, sizeof(ifra)); 8046a800098SYoshinobu Inoue bcopy(iflr->iflr_name, ifra.ifra_name, 8056a800098SYoshinobu Inoue sizeof(ifra.ifra_name)); 8066a800098SYoshinobu Inoue 8076a800098SYoshinobu Inoue bcopy(&ia->ia_addr, &ifra.ifra_addr, 8086a800098SYoshinobu Inoue ia->ia_addr.sin_len); 8096a800098SYoshinobu Inoue if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 8106a800098SYoshinobu Inoue bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 8116a800098SYoshinobu Inoue ia->ia_dstaddr.sin_len); 8126a800098SYoshinobu Inoue } 8136a800098SYoshinobu Inoue bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 8146a800098SYoshinobu Inoue ia->ia_sockmask.sin_len); 81556d6e129SJohn Baldwin ifa_free(ifa); 8166a800098SYoshinobu Inoue 817460473a0SBjoern A. Zeeb return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 818460473a0SBjoern A. Zeeb ifp, td)); 8196a800098SYoshinobu Inoue } 8206a800098SYoshinobu Inoue } 8216a800098SYoshinobu Inoue } 8226a800098SYoshinobu Inoue 823460473a0SBjoern A. Zeeb return (EOPNOTSUPP); /*just for safety*/ 8246a800098SYoshinobu Inoue } 8256a800098SYoshinobu Inoue 82648321abeSMax Laier #define rtinitflags(x) \ 82748321abeSMax Laier ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 82848321abeSMax Laier ? RTF_HOST : 0) 829ccbb9c35SQing Li 830ccbb9c35SQing Li /* 831ccbb9c35SQing Li * Generate a routing message when inserting or deleting 832ccbb9c35SQing Li * an interface address alias. 833ccbb9c35SQing Li */ 834ccbb9c35SQing Li static void in_addralias_rtmsg(int cmd, struct in_addr *prefix, 835ccbb9c35SQing Li struct in_ifaddr *target) 836ccbb9c35SQing Li { 837ccbb9c35SQing Li struct route pfx_ro; 838ccbb9c35SQing Li struct sockaddr_in *pfx_addr; 839ccbb9c35SQing Li struct rtentry msg_rt; 840ccbb9c35SQing Li 841ccbb9c35SQing Li /* QL: XXX 842ccbb9c35SQing Li * This is a bit questionable because there is no 843ccbb9c35SQing Li * additional route entry added/deleted for an address 844ccbb9c35SQing Li * alias. Therefore this route report is inaccurate. 845ccbb9c35SQing Li */ 846ccbb9c35SQing Li bzero(&pfx_ro, sizeof(pfx_ro)); 847ccbb9c35SQing Li pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst); 848ccbb9c35SQing Li pfx_addr->sin_len = sizeof(*pfx_addr); 849ccbb9c35SQing Li pfx_addr->sin_family = AF_INET; 850ccbb9c35SQing Li pfx_addr->sin_addr = *prefix; 851ccbb9c35SQing Li rtalloc_ign_fib(&pfx_ro, 0, 0); 852ccbb9c35SQing Li if (pfx_ro.ro_rt != NULL) { 853ccbb9c35SQing Li msg_rt = *pfx_ro.ro_rt; 854ccbb9c35SQing Li 855ccbb9c35SQing Li /* QL: XXX 856ccbb9c35SQing Li * Point the gateway to the new interface 857ccbb9c35SQing Li * address as if a new prefix route entry has 858ccbb9c35SQing Li * been added through the new address alias. 859ccbb9c35SQing Li * All other parts of the rtentry is accurate, 860ccbb9c35SQing Li * e.g., rt_key, rt_mask, rt_ifp etc. 861ccbb9c35SQing Li */ 862ea50c13eSGleb Smirnoff msg_rt.rt_gateway = (struct sockaddr *)&target->ia_addr; 863ea50c13eSGleb Smirnoff rt_newaddrmsg(cmd, (struct ifaddr *)target, 0, &msg_rt); 864ccbb9c35SQing Li RTFREE(pfx_ro.ro_rt); 865ccbb9c35SQing Li } 866ccbb9c35SQing Li return; 867ccbb9c35SQing Li } 868ccbb9c35SQing Li 86948321abeSMax Laier /* 870fbdd20a1SMatt Jacob * Check if we have a route for the given prefix already or add one accordingly. 87148321abeSMax Laier */ 87208b68b0eSGleb Smirnoff int 873f2565d68SRobert Watson in_addprefix(struct in_ifaddr *target, int flags) 87448321abeSMax Laier { 87548321abeSMax Laier struct in_ifaddr *ia; 876bfb26eecSGleb Smirnoff struct in_addr prefix, mask, p, m; 87748321abeSMax Laier int error; 87848321abeSMax Laier 879fbdd20a1SMatt Jacob if ((flags & RTF_HOST) != 0) { 88048321abeSMax Laier prefix = target->ia_dstaddr.sin_addr; 881fbdd20a1SMatt Jacob mask.s_addr = 0; 882fbdd20a1SMatt Jacob } else { 88348321abeSMax Laier prefix = target->ia_addr.sin_addr; 88448321abeSMax Laier mask = target->ia_sockmask.sin_addr; 88548321abeSMax Laier prefix.s_addr &= mask.s_addr; 88648321abeSMax Laier } 88748321abeSMax Laier 8882d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 889603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 890bfb26eecSGleb Smirnoff if (rtinitflags(ia)) { 89172366606SKevin Lo p = ia->ia_dstaddr.sin_addr; 89248321abeSMax Laier 89348321abeSMax Laier if (prefix.s_addr != p.s_addr) 89448321abeSMax Laier continue; 895bfb26eecSGleb Smirnoff } else { 896bfb26eecSGleb Smirnoff p = ia->ia_addr.sin_addr; 897bfb26eecSGleb Smirnoff m = ia->ia_sockmask.sin_addr; 898bfb26eecSGleb Smirnoff p.s_addr &= m.s_addr; 899bfb26eecSGleb Smirnoff 900bfb26eecSGleb Smirnoff if (prefix.s_addr != p.s_addr || 901bfb26eecSGleb Smirnoff mask.s_addr != m.s_addr) 902bfb26eecSGleb Smirnoff continue; 903bfb26eecSGleb Smirnoff } 90448321abeSMax Laier 90548321abeSMax Laier /* 90648321abeSMax Laier * If we got a matching prefix route inserted by other 90748321abeSMax Laier * interface address, we are done here. 90848321abeSMax Laier */ 9091ae95409SGleb Smirnoff if (ia->ia_flags & IFA_ROUTE) { 910c7ea0aa6SQing Li #ifdef RADIX_MPATH 911c7ea0aa6SQing Li if (ia->ia_addr.sin_addr.s_addr == 91242db1b87SBjoern A. Zeeb target->ia_addr.sin_addr.s_addr) { 91342db1b87SBjoern A. Zeeb IN_IFADDR_RUNLOCK(); 914c7ea0aa6SQing Li return (EEXIST); 91542db1b87SBjoern A. Zeeb } else 916c7ea0aa6SQing Li break; 917c7ea0aa6SQing Li #endif 91808b68b0eSGleb Smirnoff if (V_nosameprefix) { 9192d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 9201ae95409SGleb Smirnoff return (EEXIST); 9212d9cfabaSRobert Watson } else { 922ccbb9c35SQing Li in_addralias_rtmsg(RTM_ADD, &prefix, target); 9232d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 9241ae95409SGleb Smirnoff return (0); 9251ae95409SGleb Smirnoff } 92648321abeSMax Laier } 9272d9cfabaSRobert Watson } 9282d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 92948321abeSMax Laier 93048321abeSMax Laier /* 93148321abeSMax Laier * No-one seem to have this prefix route, so we try to insert it. 93248321abeSMax Laier */ 93348321abeSMax Laier error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 93448321abeSMax Laier if (!error) 93548321abeSMax Laier target->ia_flags |= IFA_ROUTE; 936460473a0SBjoern A. Zeeb return (error); 93748321abeSMax Laier } 93848321abeSMax Laier 93948321abeSMax Laier /* 94048321abeSMax Laier * If there is no other address in the system that can serve a route to the 94148321abeSMax Laier * same prefix, remove the route. Hand over the route to the new address 94248321abeSMax Laier * otherwise. 94348321abeSMax Laier */ 94408b68b0eSGleb Smirnoff int 9455b84dc78SQing Li in_scrubprefix(struct in_ifaddr *target, u_int flags) 94648321abeSMax Laier { 94748321abeSMax Laier struct in_ifaddr *ia; 94855174c34SGleb Smirnoff struct in_addr prefix, mask, p, m; 949c7ab6602SQing Li int error = 0; 950c9d763bfSQing Li struct sockaddr_in prefix0, mask0; 95148321abeSMax Laier 952df813b7eSQing Li /* 953df813b7eSQing Li * Remove the loopback route to the interface address. 954df813b7eSQing Li */ 95592fac994SQing Li if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) && 956c7ab6602SQing Li !(target->ia_ifp->if_flags & IFF_LOOPBACK) && 957f7a39160SGleb Smirnoff (flags & LLE_STATIC)) { 958f7a39160SGleb Smirnoff struct in_ifaddr *eia; 959c7ab6602SQing Li 960f7a39160SGleb Smirnoff eia = in_localip_more(target); 961f7a39160SGleb Smirnoff 962f7a39160SGleb Smirnoff if (eia != NULL) { 963f7a39160SGleb Smirnoff error = ifa_switch_loopback_route((struct ifaddr *)eia, 964f7a39160SGleb Smirnoff (struct sockaddr *)&target->ia_addr); 965f7a39160SGleb Smirnoff ifa_free(&eia->ia_ifa); 966f7a39160SGleb Smirnoff } else { 9679bb7d0f4SQing Li error = ifa_del_loopback_route((struct ifaddr *)target, 9689bb7d0f4SQing Li (struct sockaddr *)&target->ia_addr); 9695b84dc78SQing Li } 970f7a39160SGleb Smirnoff 971f7a39160SGleb Smirnoff if (!(target->ia_ifp->if_flags & IFF_NOARP)) 972fa3cfd39SQing Li /* remove arp cache */ 973f7a39160SGleb Smirnoff arp_ifscrub(target->ia_ifp, 974f7a39160SGleb Smirnoff IA_SIN(target)->sin_addr.s_addr); 975ebc90701SQing Li } 976ebc90701SQing Li 97755174c34SGleb Smirnoff if (rtinitflags(target)) { 97848321abeSMax Laier prefix = target->ia_dstaddr.sin_addr; 97955174c34SGleb Smirnoff mask.s_addr = 0; 98055174c34SGleb Smirnoff } else { 98148321abeSMax Laier prefix = target->ia_addr.sin_addr; 98248321abeSMax Laier mask = target->ia_sockmask.sin_addr; 98348321abeSMax Laier prefix.s_addr &= mask.s_addr; 98448321abeSMax Laier } 98548321abeSMax Laier 986ccbb9c35SQing Li if ((target->ia_flags & IFA_ROUTE) == 0) { 987ccbb9c35SQing Li in_addralias_rtmsg(RTM_DELETE, &prefix, target); 988ccbb9c35SQing Li return (0); 989ccbb9c35SQing Li } 990ccbb9c35SQing Li 9912d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 992603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 99355174c34SGleb Smirnoff if (rtinitflags(ia)) { 99448321abeSMax Laier p = ia->ia_dstaddr.sin_addr; 99555174c34SGleb Smirnoff 99655174c34SGleb Smirnoff if (prefix.s_addr != p.s_addr) 99755174c34SGleb Smirnoff continue; 99855174c34SGleb Smirnoff } else { 99948321abeSMax Laier p = ia->ia_addr.sin_addr; 100055174c34SGleb Smirnoff m = ia->ia_sockmask.sin_addr; 100155174c34SGleb Smirnoff p.s_addr &= m.s_addr; 100255174c34SGleb Smirnoff 100355174c34SGleb Smirnoff if (prefix.s_addr != p.s_addr || 100455174c34SGleb Smirnoff mask.s_addr != m.s_addr) 100555174c34SGleb Smirnoff continue; 100648321abeSMax Laier } 100748321abeSMax Laier 100855174c34SGleb Smirnoff if ((ia->ia_ifp->if_flags & IFF_UP) == 0) 100948321abeSMax Laier continue; 101048321abeSMax Laier 101148321abeSMax Laier /* 101248321abeSMax Laier * If we got a matching prefix address, move IFA_ROUTE and 101348321abeSMax Laier * the route itself to it. Make sure that routing daemons 101448321abeSMax Laier * get a heads-up. 101548321abeSMax Laier */ 101608b68b0eSGleb Smirnoff if ((ia->ia_flags & IFA_ROUTE) == 0) { 101779d51435SSergey Kandaurov ifa_ref(&ia->ia_ifa); 10182d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 101992322284SQing Li error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, 102048321abeSMax Laier rtinitflags(target)); 102192322284SQing Li if (error == 0) 102248321abeSMax Laier target->ia_flags &= ~IFA_ROUTE; 102392322284SQing Li else 102492322284SQing Li log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n", 102592322284SQing Li error); 102648321abeSMax Laier error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 102748321abeSMax Laier rtinitflags(ia) | RTF_UP); 102848321abeSMax Laier if (error == 0) 102948321abeSMax Laier ia->ia_flags |= IFA_ROUTE; 103092322284SQing Li else 103192322284SQing Li log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n", 103292322284SQing Li error); 103379d51435SSergey Kandaurov ifa_free(&ia->ia_ifa); 1034460473a0SBjoern A. Zeeb return (error); 103548321abeSMax Laier } 103648321abeSMax Laier } 10372d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 103848321abeSMax Laier 103948321abeSMax Laier /* 1040c9d763bfSQing Li * remove all L2 entries on the given prefix 1041c9d763bfSQing Li */ 1042c9d763bfSQing Li bzero(&prefix0, sizeof(prefix0)); 1043c9d763bfSQing Li prefix0.sin_len = sizeof(prefix0); 1044c9d763bfSQing Li prefix0.sin_family = AF_INET; 1045c9d763bfSQing Li prefix0.sin_addr.s_addr = target->ia_subnet; 1046c9d763bfSQing Li bzero(&mask0, sizeof(mask0)); 1047c9d763bfSQing Li mask0.sin_len = sizeof(mask0); 1048c9d763bfSQing Li mask0.sin_family = AF_INET; 1049c9d763bfSQing Li mask0.sin_addr.s_addr = target->ia_subnetmask; 1050c9d763bfSQing Li lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0, 10515b84dc78SQing Li (struct sockaddr *)&mask0, flags); 1052c9d763bfSQing Li 1053c9d763bfSQing Li /* 105448321abeSMax Laier * As no-one seem to have this prefix, we can remove the route. 105548321abeSMax Laier */ 105692322284SQing Li error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 105792322284SQing Li if (error == 0) 105848321abeSMax Laier target->ia_flags &= ~IFA_ROUTE; 105992322284SQing Li else 106092322284SQing Li log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error); 106192322284SQing Li return (error); 106248321abeSMax Laier } 106348321abeSMax Laier 106448321abeSMax Laier #undef rtinitflags 1065df8bae1dSRodney W. Grimes 1066df8bae1dSRodney W. Grimes /* 1067df8bae1dSRodney W. Grimes * Return 1 if the address might be a local broadcast address. 1068df8bae1dSRodney W. Grimes */ 106926f9a767SRodney W. Grimes int 1070f2565d68SRobert Watson in_broadcast(struct in_addr in, struct ifnet *ifp) 1071df8bae1dSRodney W. Grimes { 1072df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 1073df8bae1dSRodney W. Grimes u_long t; 1074df8bae1dSRodney W. Grimes 1075df8bae1dSRodney W. Grimes if (in.s_addr == INADDR_BROADCAST || 1076df8bae1dSRodney W. Grimes in.s_addr == INADDR_ANY) 1077460473a0SBjoern A. Zeeb return (1); 1078df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 1079460473a0SBjoern A. Zeeb return (0); 1080df8bae1dSRodney W. Grimes t = ntohl(in.s_addr); 1081df8bae1dSRodney W. Grimes /* 1082df8bae1dSRodney W. Grimes * Look through the list of addresses for a match 1083df8bae1dSRodney W. Grimes * with a broadcast address. 1084df8bae1dSRodney W. Grimes */ 1085df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa) 1086462b86feSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1087df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family == AF_INET && 1088df8bae1dSRodney W. Grimes (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 1089df8bae1dSRodney W. Grimes /* 109053883e0cSGleb Smirnoff * Check for old-style (host 0) broadcast, but 109153883e0cSGleb Smirnoff * taking into account that RFC 3021 obsoletes it. 1092df8bae1dSRodney W. Grimes */ 109353883e0cSGleb Smirnoff (ia->ia_subnetmask != IN_RFC3021_MASK && 109453883e0cSGleb Smirnoff t == ia->ia_subnet)) && 10958dd27fd6SGuido van Rooij /* 10968dd27fd6SGuido van Rooij * Check for an all one subnetmask. These 10978dd27fd6SGuido van Rooij * only exist when an interface gets a secondary 10988dd27fd6SGuido van Rooij * address. 10998dd27fd6SGuido van Rooij */ 11008dd27fd6SGuido van Rooij ia->ia_subnetmask != (u_long)0xffffffff) 1101460473a0SBjoern A. Zeeb return (1); 1102df8bae1dSRodney W. Grimes return (0); 1103df8bae1dSRodney W. Grimes #undef ia 1104df8bae1dSRodney W. Grimes } 1105ec002feeSBruce M Simpson 1106df8bae1dSRodney W. Grimes /* 1107b1c53bc9SRobert Watson * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1108b1c53bc9SRobert Watson */ 1109b1c53bc9SRobert Watson void 1110f2565d68SRobert Watson in_ifdetach(struct ifnet *ifp) 1111b1c53bc9SRobert Watson { 1112b1c53bc9SRobert Watson 1113603724d3SBjoern A. Zeeb in_pcbpurgeif0(&V_ripcbinfo, ifp); 1114603724d3SBjoern A. Zeeb in_pcbpurgeif0(&V_udbinfo, ifp); 1115ec002feeSBruce M Simpson in_purgemaddrs(ifp); 1116b1c53bc9SRobert Watson } 11176e6b3f7cSQing Li 1118d10910e6SBruce M Simpson /* 1119d10910e6SBruce M Simpson * Delete all IPv4 multicast address records, and associated link-layer 1120d10910e6SBruce M Simpson * multicast address records, associated with ifp. 1121d10910e6SBruce M Simpson * XXX It looks like domifdetach runs AFTER the link layer cleanup. 112256663a40SBruce M Simpson * XXX This should not race with ifma_protospec being set during 112356663a40SBruce M Simpson * a new allocation, if it does, we have bigger problems. 1124d10910e6SBruce M Simpson */ 1125d10910e6SBruce M Simpson static void 1126d10910e6SBruce M Simpson in_purgemaddrs(struct ifnet *ifp) 1127d10910e6SBruce M Simpson { 1128d10910e6SBruce M Simpson LIST_HEAD(,in_multi) purgeinms; 1129d10910e6SBruce M Simpson struct in_multi *inm, *tinm; 1130d10910e6SBruce M Simpson struct ifmultiaddr *ifma; 1131d10910e6SBruce M Simpson 1132d10910e6SBruce M Simpson LIST_INIT(&purgeinms); 1133d10910e6SBruce M Simpson IN_MULTI_LOCK(); 1134d10910e6SBruce M Simpson 1135d10910e6SBruce M Simpson /* 1136d10910e6SBruce M Simpson * Extract list of in_multi associated with the detaching ifp 1137d10910e6SBruce M Simpson * which the PF_INET layer is about to release. 1138d10910e6SBruce M Simpson * We need to do this as IF_ADDR_LOCK() may be re-acquired 1139d10910e6SBruce M Simpson * by code further down. 1140d10910e6SBruce M Simpson */ 1141137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 1142d10910e6SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 114356663a40SBruce M Simpson if (ifma->ifma_addr->sa_family != AF_INET || 114456663a40SBruce M Simpson ifma->ifma_protospec == NULL) 1145d10910e6SBruce M Simpson continue; 114656663a40SBruce M Simpson #if 0 114756663a40SBruce M Simpson KASSERT(ifma->ifma_protospec != NULL, 114856663a40SBruce M Simpson ("%s: ifma_protospec is NULL", __func__)); 114956663a40SBruce M Simpson #endif 1150d10910e6SBruce M Simpson inm = (struct in_multi *)ifma->ifma_protospec; 1151d10910e6SBruce M Simpson LIST_INSERT_HEAD(&purgeinms, inm, inm_link); 1152d10910e6SBruce M Simpson } 1153137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 1154d10910e6SBruce M Simpson 1155d10910e6SBruce M Simpson LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) { 1156d10910e6SBruce M Simpson LIST_REMOVE(inm, inm_link); 115707cde5e9SRobert Watson inm_release_locked(inm); 1158d10910e6SBruce M Simpson } 1159d10910e6SBruce M Simpson igmp_ifdetach(ifp); 1160d10910e6SBruce M Simpson 1161d10910e6SBruce M Simpson IN_MULTI_UNLOCK(); 1162d10910e6SBruce M Simpson } 1163d10910e6SBruce M Simpson 11646e6b3f7cSQing Li struct in_llentry { 11656e6b3f7cSQing Li struct llentry base; 11666e6b3f7cSQing Li struct sockaddr_in l3_addr4; 11676e6b3f7cSQing Li }; 11686e6b3f7cSQing Li 1169a93cda78SKip Macy /* 1170a93cda78SKip Macy * Deletes an address from the address table. 1171a93cda78SKip Macy * This function is called by the timer functions 1172a93cda78SKip Macy * such as arptimer() and nd6_llinfo_timer(), and 1173a93cda78SKip Macy * the caller does the locking. 1174a93cda78SKip Macy */ 1175a93cda78SKip Macy static void 1176a93cda78SKip Macy in_lltable_free(struct lltable *llt, struct llentry *lle) 1177a93cda78SKip Macy { 1178a93cda78SKip Macy LLE_WUNLOCK(lle); 1179a93cda78SKip Macy LLE_LOCK_DESTROY(lle); 1180a93cda78SKip Macy free(lle, M_LLTABLE); 1181a93cda78SKip Macy } 1182a93cda78SKip Macy 11836e6b3f7cSQing Li static struct llentry * 11846e6b3f7cSQing Li in_lltable_new(const struct sockaddr *l3addr, u_int flags) 11856e6b3f7cSQing Li { 11866e6b3f7cSQing Li struct in_llentry *lle; 11876e6b3f7cSQing Li 118890b357f6SGleb Smirnoff lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO); 11896e6b3f7cSQing Li if (lle == NULL) /* NB: caller generates msg */ 11906e6b3f7cSQing Li return NULL; 11916e6b3f7cSQing Li 11926e6b3f7cSQing Li /* 11936e6b3f7cSQing Li * For IPv4 this will trigger "arpresolve" to generate 11946e6b3f7cSQing Li * an ARP request. 11956e6b3f7cSQing Li */ 1196a98c06f1SGleb Smirnoff lle->base.la_expire = time_uptime; /* mark expired */ 11976e6b3f7cSQing Li lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 11986e6b3f7cSQing Li lle->base.lle_refcnt = 1; 1199a93cda78SKip Macy lle->base.lle_free = in_lltable_free; 12006e6b3f7cSQing Li LLE_LOCK_INIT(&lle->base); 1201ea537929SGleb Smirnoff callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock, 1202ea537929SGleb Smirnoff CALLOUT_RETURNUNLOCKED); 1203ea537929SGleb Smirnoff 1204ea537929SGleb Smirnoff return (&lle->base); 12056e6b3f7cSQing Li } 12066e6b3f7cSQing Li 1207c9d763bfSQing Li #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 1208c9d763bfSQing Li (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 ) 1209c9d763bfSQing Li 1210c9d763bfSQing Li static void 1211ea50c13eSGleb Smirnoff in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix, 1212ea50c13eSGleb Smirnoff const struct sockaddr *mask, u_int flags) 1213c9d763bfSQing Li { 1214c9d763bfSQing Li const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix; 1215c9d763bfSQing Li const struct sockaddr_in *msk = (const struct sockaddr_in *)mask; 1216c9d763bfSQing Li struct llentry *lle, *next; 1217ea50c13eSGleb Smirnoff int i; 1218e162ea60SGeorge V. Neville-Neil size_t pkts_dropped; 1219c9d763bfSQing Li 1220ea537929SGleb Smirnoff IF_AFDATA_WLOCK(llt->llt_ifp); 1221c9d763bfSQing Li for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 1222c9d763bfSQing Li LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 12235b84dc78SQing Li /* 12245b84dc78SQing Li * (flags & LLE_STATIC) means deleting all entries 1225ea50c13eSGleb Smirnoff * including static ARP entries. 12265b84dc78SQing Li */ 1227ea50c13eSGleb Smirnoff if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)), 1228ea50c13eSGleb Smirnoff pfx, msk) && ((flags & LLE_STATIC) || 1229ea50c13eSGleb Smirnoff !(lle->la_flags & LLE_STATIC))) { 1230c9d763bfSQing Li LLE_WLOCK(lle); 1231ea537929SGleb Smirnoff if (callout_stop(&lle->la_timer)) 1232becba438SBjoern A. Zeeb LLE_REMREF(lle); 1233e162ea60SGeorge V. Neville-Neil pkts_dropped = llentry_free(lle); 1234e162ea60SGeorge V. Neville-Neil ARPSTAT_ADD(dropped, pkts_dropped); 1235c9d763bfSQing Li } 1236c9d763bfSQing Li } 1237c9d763bfSQing Li } 1238ea537929SGleb Smirnoff IF_AFDATA_WUNLOCK(llt->llt_ifp); 1239c9d763bfSQing Li } 1240c9d763bfSQing Li 1241c9d763bfSQing Li 12426e6b3f7cSQing Li static int 1243c7ab6602SQing Li in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr) 12446e6b3f7cSQing Li { 12456e6b3f7cSQing Li struct rtentry *rt; 12466e6b3f7cSQing Li 12476e6b3f7cSQing Li KASSERT(l3addr->sa_family == AF_INET, 12486e6b3f7cSQing Li ("sin_family %d", l3addr->sa_family)); 12496e6b3f7cSQing Li 12506e6b3f7cSQing Li /* XXX rtalloc1 should take a const param */ 12516e6b3f7cSQing Li rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 125213e255faSMarko Zec 12536cf8e330SQing Li if (rt == NULL) 12546cf8e330SQing Li return (EINVAL); 12556cf8e330SQing Li 125613e255faSMarko Zec /* 125713e255faSMarko Zec * If the gateway for an existing host route matches the target L3 12586cf8e330SQing Li * address, which is a special route inserted by some implementation 12596cf8e330SQing Li * such as MANET, and the interface is of the correct type, then 12606cf8e330SQing Li * allow for ARP to proceed. 126113e255faSMarko Zec */ 1262db92413eSQing Li if (rt->rt_flags & RTF_GATEWAY) { 126315d25219SQing Li if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp || 126415d25219SQing Li rt->rt_ifp->if_type != IFT_ETHER || 1265ea50c13eSGleb Smirnoff (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 || 126615d25219SQing Li memcmp(rt->rt_gateway->sa_data, l3addr->sa_data, 126715d25219SQing Li sizeof(in_addr_t)) != 0) { 1268db92413eSQing Li RTFREE_LOCKED(rt); 1269db92413eSQing Li return (EINVAL); 1270db92413eSQing Li } 127115d25219SQing Li } 1272db92413eSQing Li 1273db92413eSQing Li /* 1274db92413eSQing Li * Make sure that at least the destination address is covered 1275db92413eSQing Li * by the route. This is for handling the case where 2 or more 1276db92413eSQing Li * interfaces have the same prefix. An incoming packet arrives 1277db92413eSQing Li * on one interface and the corresponding outgoing packet leaves 1278db92413eSQing Li * another interface. 1279db92413eSQing Li */ 1280b3664a14SQing Li if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) { 128115d25219SQing Li const char *sa, *mask, *addr, *lim; 1282db92413eSQing Li int len; 1283db92413eSQing Li 128415d25219SQing Li mask = (const char *)rt_mask(rt); 1285b3664a14SQing Li /* 1286b3664a14SQing Li * Just being extra cautious to avoid some custom 1287b3664a14SQing Li * code getting into trouble. 1288b3664a14SQing Li */ 1289b3664a14SQing Li if (mask == NULL) { 1290b3664a14SQing Li RTFREE_LOCKED(rt); 1291b3664a14SQing Li return (EINVAL); 1292b3664a14SQing Li } 1293b3664a14SQing Li 1294b3664a14SQing Li sa = (const char *)rt_key(rt); 129515d25219SQing Li addr = (const char *)l3addr; 129615d25219SQing Li len = ((const struct sockaddr_in *)l3addr)->sin_len; 1297db92413eSQing Li lim = addr + len; 1298db92413eSQing Li 1299db92413eSQing Li for ( ; addr < lim; sa++, mask++, addr++) { 1300db92413eSQing Li if ((*sa ^ *addr) & *mask) { 13016cb2b4e7SQing Li #ifdef DIAGNOSTIC 13026e6b3f7cSQing Li log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 13036e6b3f7cSQing Li inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1304b4a22c36SQing Li #endif 130515d25219SQing Li RTFREE_LOCKED(rt); 130615d25219SQing Li return (EINVAL); 13076e6b3f7cSQing Li } 1308db92413eSQing Li } 1309db92413eSQing Li } 1310db92413eSQing Li 13116e6b3f7cSQing Li RTFREE_LOCKED(rt); 131215d25219SQing Li return (0); 13136e6b3f7cSQing Li } 13146e6b3f7cSQing Li 13156e6b3f7cSQing Li /* 13166e6b3f7cSQing Li * Return NULL if not found or marked for deletion. 13176e6b3f7cSQing Li * If found return lle read locked. 13186e6b3f7cSQing Li */ 13196e6b3f7cSQing Li static struct llentry * 13206e6b3f7cSQing Li in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 13216e6b3f7cSQing Li { 13226e6b3f7cSQing Li const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 13236e6b3f7cSQing Li struct ifnet *ifp = llt->llt_ifp; 13246e6b3f7cSQing Li struct llentry *lle; 13256e6b3f7cSQing Li struct llentries *lleh; 13266e6b3f7cSQing Li u_int hashkey; 13276e6b3f7cSQing Li 13286e6b3f7cSQing Li IF_AFDATA_LOCK_ASSERT(ifp); 13296e6b3f7cSQing Li KASSERT(l3addr->sa_family == AF_INET, 13306e6b3f7cSQing Li ("sin_family %d", l3addr->sa_family)); 13316e6b3f7cSQing Li 13326e6b3f7cSQing Li hashkey = sin->sin_addr.s_addr; 13336e6b3f7cSQing Li lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 13346e6b3f7cSQing Li LIST_FOREACH(lle, lleh, lle_next) { 1335ea50c13eSGleb Smirnoff struct sockaddr_in *sa2 = satosin(L3_ADDR(lle)); 13366e6b3f7cSQing Li if (lle->la_flags & LLE_DELETED) 13376e6b3f7cSQing Li continue; 1338dc495497SQing Li if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr) 13396e6b3f7cSQing Li break; 13406e6b3f7cSQing Li } 13416e6b3f7cSQing Li if (lle == NULL) { 13426cb2b4e7SQing Li #ifdef DIAGNOSTIC 13436e6b3f7cSQing Li if (flags & LLE_DELETE) 13446e6b3f7cSQing Li log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 13456e6b3f7cSQing Li #endif 13466e6b3f7cSQing Li if (!(flags & LLE_CREATE)) 13476e6b3f7cSQing Li return (NULL); 13486e6b3f7cSQing Li /* 13496e6b3f7cSQing Li * A route that covers the given address must have 13506e6b3f7cSQing Li * been installed 1st because we are doing a resolution, 13516e6b3f7cSQing Li * verify this. 13526e6b3f7cSQing Li */ 13536e6b3f7cSQing Li if (!(flags & LLE_IFADDR) && 1354c7ab6602SQing Li in_lltable_rtcheck(ifp, flags, l3addr) != 0) 13556e6b3f7cSQing Li goto done; 13566e6b3f7cSQing Li 13576e6b3f7cSQing Li lle = in_lltable_new(l3addr, flags); 13586e6b3f7cSQing Li if (lle == NULL) { 13596e6b3f7cSQing Li log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 13606e6b3f7cSQing Li goto done; 13616e6b3f7cSQing Li } 13626e6b3f7cSQing Li lle->la_flags = flags & ~LLE_CREATE; 13636e6b3f7cSQing Li if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 13646e6b3f7cSQing Li bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 13656e6b3f7cSQing Li lle->la_flags |= (LLE_VALID | LLE_STATIC); 13666e6b3f7cSQing Li } 13676e6b3f7cSQing Li 13686e6b3f7cSQing Li lle->lle_tbl = llt; 13696e6b3f7cSQing Li lle->lle_head = lleh; 1370ea537929SGleb Smirnoff lle->la_flags |= LLE_LINKED; 13716e6b3f7cSQing Li LIST_INSERT_HEAD(lleh, lle, lle_next); 13726e6b3f7cSQing Li } else if (flags & LLE_DELETE) { 13736e6b3f7cSQing Li if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 13746e6b3f7cSQing Li LLE_WLOCK(lle); 1375ea537929SGleb Smirnoff lle->la_flags |= LLE_DELETED; 137609fe6320SNavdeep Parhar EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED); 13776cb2b4e7SQing Li #ifdef DIAGNOSTIC 13786e6b3f7cSQing Li log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 13796e6b3f7cSQing Li #endif 13801571132fSOleg Bulyzhin if ((lle->la_flags & 13811571132fSOleg Bulyzhin (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC) 13821571132fSOleg Bulyzhin llentry_free(lle); 13831571132fSOleg Bulyzhin else 13841571132fSOleg Bulyzhin LLE_WUNLOCK(lle); 13856e6b3f7cSQing Li } 13866e6b3f7cSQing Li lle = (void *)-1; 13876e6b3f7cSQing Li 13886e6b3f7cSQing Li } 138942d866ddSBjoern A. Zeeb if (LLE_IS_VALID(lle)) { 13906e6b3f7cSQing Li if (flags & LLE_EXCLUSIVE) 13916e6b3f7cSQing Li LLE_WLOCK(lle); 13926e6b3f7cSQing Li else 13936e6b3f7cSQing Li LLE_RLOCK(lle); 13946e6b3f7cSQing Li } 13956e6b3f7cSQing Li done: 13966e6b3f7cSQing Li return (lle); 13976e6b3f7cSQing Li } 13986e6b3f7cSQing Li 13996e6b3f7cSQing Li static int 14006e6b3f7cSQing Li in_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 14016e6b3f7cSQing Li { 14026e6b3f7cSQing Li #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 14036e6b3f7cSQing Li struct ifnet *ifp = llt->llt_ifp; 14046e6b3f7cSQing Li struct llentry *lle; 14056e6b3f7cSQing Li /* XXX stack use */ 14066e6b3f7cSQing Li struct { 14076e6b3f7cSQing Li struct rt_msghdr rtm; 14089711a168SGleb Smirnoff struct sockaddr_in sin; 14096e6b3f7cSQing Li struct sockaddr_dl sdl; 14106e6b3f7cSQing Li } arpc; 14116e6b3f7cSQing Li int error, i; 14126e6b3f7cSQing Li 1413dc56e98fSRobert Watson LLTABLE_LOCK_ASSERT(); 14146e6b3f7cSQing Li 14156e6b3f7cSQing Li error = 0; 14166e6b3f7cSQing Li for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 14176e6b3f7cSQing Li LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 14186e6b3f7cSQing Li struct sockaddr_dl *sdl; 14196e6b3f7cSQing Li 14206e6b3f7cSQing Li /* skip deleted entries */ 142193704ac5SQing Li if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) 14226e6b3f7cSQing Li continue; 1423813dd6aeSBjoern A. Zeeb /* Skip if jailed and not a valid IP of the prison. */ 1424b89e82ddSJamie Gritton if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 1425813dd6aeSBjoern A. Zeeb continue; 14266e6b3f7cSQing Li /* 14276e6b3f7cSQing Li * produce a msg made of: 14286e6b3f7cSQing Li * struct rt_msghdr; 14299711a168SGleb Smirnoff * struct sockaddr_in; (IPv4) 14306e6b3f7cSQing Li * struct sockaddr_dl; 14316e6b3f7cSQing Li */ 14326e6b3f7cSQing Li bzero(&arpc, sizeof(arpc)); 14336e6b3f7cSQing Li arpc.rtm.rtm_msglen = sizeof(arpc); 1434c0e9a8a1SHartmut Brandt arpc.rtm.rtm_version = RTM_VERSION; 1435c0e9a8a1SHartmut Brandt arpc.rtm.rtm_type = RTM_GET; 1436c0e9a8a1SHartmut Brandt arpc.rtm.rtm_flags = RTF_UP; 1437c0e9a8a1SHartmut Brandt arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 14386e6b3f7cSQing Li arpc.sin.sin_family = AF_INET; 14396e6b3f7cSQing Li arpc.sin.sin_len = sizeof(arpc.sin); 14406e6b3f7cSQing Li arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 14416e6b3f7cSQing Li 14426e6b3f7cSQing Li /* publish */ 14439711a168SGleb Smirnoff if (lle->la_flags & LLE_PUB) 14446e6b3f7cSQing Li arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 14456e6b3f7cSQing Li 14466e6b3f7cSQing Li sdl = &arpc.sdl; 14476e6b3f7cSQing Li sdl->sdl_family = AF_LINK; 14486e6b3f7cSQing Li sdl->sdl_len = sizeof(*sdl); 14496e6b3f7cSQing Li sdl->sdl_index = ifp->if_index; 14506e6b3f7cSQing Li sdl->sdl_type = ifp->if_type; 145193704ac5SQing Li if ((lle->la_flags & LLE_VALID) == LLE_VALID) { 145293704ac5SQing Li sdl->sdl_alen = ifp->if_addrlen; 14536e6b3f7cSQing Li bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 145493704ac5SQing Li } else { 145593704ac5SQing Li sdl->sdl_alen = 0; 145693704ac5SQing Li bzero(LLADDR(sdl), ifp->if_addrlen); 145793704ac5SQing Li } 14586e6b3f7cSQing Li 14596e6b3f7cSQing Li arpc.rtm.rtm_rmx.rmx_expire = 14606e6b3f7cSQing Li lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 14618eca593cSQing Li arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 14626e6b3f7cSQing Li if (lle->la_flags & LLE_STATIC) 14636e6b3f7cSQing Li arpc.rtm.rtm_flags |= RTF_STATIC; 14646e6b3f7cSQing Li arpc.rtm.rtm_index = ifp->if_index; 14656e6b3f7cSQing Li error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 14666e6b3f7cSQing Li if (error) 14676e6b3f7cSQing Li break; 14686e6b3f7cSQing Li } 14696e6b3f7cSQing Li } 14706e6b3f7cSQing Li return error; 14716e6b3f7cSQing Li #undef SIN 14726e6b3f7cSQing Li } 14736e6b3f7cSQing Li 14746e6b3f7cSQing Li void * 14756e6b3f7cSQing Li in_domifattach(struct ifnet *ifp) 14766e6b3f7cSQing Li { 1477d10910e6SBruce M Simpson struct in_ifinfo *ii; 1478d10910e6SBruce M Simpson struct lltable *llt; 14796e6b3f7cSQing Li 1480d10910e6SBruce M Simpson ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1481d10910e6SBruce M Simpson 1482d10910e6SBruce M Simpson llt = lltable_init(ifp, AF_INET); 14836e6b3f7cSQing Li if (llt != NULL) { 1484c9d763bfSQing Li llt->llt_prefix_free = in_lltable_prefix_free; 14856e6b3f7cSQing Li llt->llt_lookup = in_lltable_lookup; 14866e6b3f7cSQing Li llt->llt_dump = in_lltable_dump; 14876e6b3f7cSQing Li } 1488d10910e6SBruce M Simpson ii->ii_llt = llt; 1489d10910e6SBruce M Simpson 1490d10910e6SBruce M Simpson ii->ii_igmp = igmp_domifattach(ifp); 1491d10910e6SBruce M Simpson 1492d10910e6SBruce M Simpson return ii; 14936e6b3f7cSQing Li } 14946e6b3f7cSQing Li 14956e6b3f7cSQing Li void 1496d10910e6SBruce M Simpson in_domifdetach(struct ifnet *ifp, void *aux) 14976e6b3f7cSQing Li { 1498d10910e6SBruce M Simpson struct in_ifinfo *ii = (struct in_ifinfo *)aux; 14996e6b3f7cSQing Li 1500d10910e6SBruce M Simpson igmp_domifdetach(ifp); 1501d10910e6SBruce M Simpson lltable_free(ii->ii_llt); 1502d10910e6SBruce M Simpson free(ii, M_IFADDR); 15036e6b3f7cSQing Li } 1504