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 3650bb1704SGleb Smirnoff #include "opt_carp.h" 3750bb1704SGleb Smirnoff 38df8bae1dSRodney W. Grimes #include <sys/param.h> 3926f9a767SRodney W. Grimes #include <sys/systm.h> 4051a53488SBruce Evans #include <sys/sockio.h> 41df8bae1dSRodney W. Grimes #include <sys/malloc.h> 42acd3428bSRobert Watson #include <sys/priv.h> 43df8bae1dSRodney W. Grimes #include <sys/socket.h> 445ce0eb7fSBjoern A. Zeeb #include <sys/jail.h> 45f6d24a78SPoul-Henning Kamp #include <sys/kernel.h> 465ce0eb7fSBjoern A. Zeeb #include <sys/proc.h> 47f6d24a78SPoul-Henning Kamp #include <sys/sysctl.h> 48603724d3SBjoern A. Zeeb #include <sys/vimage.h> 49df8bae1dSRodney W. Grimes 50df8bae1dSRodney W. Grimes #include <net/if.h> 516e6b3f7cSQing Li #include <net/if_llatbl.h> 526a800098SYoshinobu Inoue #include <net/if_types.h> 53df8bae1dSRodney W. Grimes #include <net/route.h> 54df8bae1dSRodney W. Grimes 55df8bae1dSRodney W. Grimes #include <netinet/in.h> 56df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 57e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h> 5871498f30SBruce M Simpson #include <netinet/ip_var.h> 594b79449eSBjoern A. Zeeb #include <netinet/vinet.h> 60d10910e6SBruce M Simpson #include <netinet/igmp_var.h> 6155166637SPoul-Henning Kamp 624d77a549SAlfred Perlstein static int in_mask2len(struct in_addr *); 634d77a549SAlfred Perlstein static void in_len2mask(struct in_addr *, int); 644d77a549SAlfred Perlstein static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 654d77a549SAlfred Perlstein struct ifnet *, struct thread *); 666a800098SYoshinobu Inoue 6748321abeSMax Laier static int in_addprefix(struct in_ifaddr *, int); 6848321abeSMax Laier static int in_scrubprefix(struct in_ifaddr *); 694d77a549SAlfred Perlstein static void in_socktrim(struct sockaddr_in *); 704d77a549SAlfred Perlstein static int in_ifinit(struct ifnet *, 714d77a549SAlfred Perlstein struct in_ifaddr *, struct sockaddr_in *, int); 72ec002feeSBruce M Simpson static void in_purgemaddrs(struct ifnet *); 73df8bae1dSRodney W. Grimes 7444e33a07SMarko Zec #ifdef VIMAGE_GLOBALS 7544e33a07SMarko Zec static int subnetsarelocal; 7644e33a07SMarko Zec static int sameprefixcarponly; 7744e33a07SMarko Zec extern struct inpcbinfo ripcbinfo; 7844e33a07SMarko Zec #endif 7944e33a07SMarko Zec 808b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, subnets_are_local, 818b615593SMarko Zec CTLFLAG_RW, subnetsarelocal, 0, 828b615593SMarko Zec "Treat all subnets as directly connected"); 838b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, same_prefix_carp_only, 848b615593SMarko Zec CTLFLAG_RW, sameprefixcarponly, 0, 851ae95409SGleb Smirnoff "Refuse to create same prefixes on different interfaces"); 86477180fbSGarrett Wollman 87df8bae1dSRodney W. Grimes /* 88df8bae1dSRodney W. Grimes * Return 1 if an internet address is for a ``local'' host 89df8bae1dSRodney W. Grimes * (one to which we have a connection). If subnetsarelocal 90df8bae1dSRodney W. Grimes * is true, this includes other subnets of the local net. 91df8bae1dSRodney W. Grimes * Otherwise, it includes only the directly-connected (sub)nets. 92df8bae1dSRodney W. Grimes */ 9326f9a767SRodney W. Grimes int 94f2565d68SRobert Watson in_localaddr(struct in_addr in) 95df8bae1dSRodney W. Grimes { 968b615593SMarko Zec INIT_VNET_INET(curvnet); 97df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 98df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 99df8bae1dSRodney W. Grimes 100603724d3SBjoern A. Zeeb if (V_subnetsarelocal) { 101603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 102df8bae1dSRodney W. Grimes if ((i & ia->ia_netmask) == ia->ia_net) 103df8bae1dSRodney W. Grimes return (1); 104df8bae1dSRodney W. Grimes } else { 105603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) 106df8bae1dSRodney W. Grimes if ((i & ia->ia_subnetmask) == ia->ia_subnet) 107df8bae1dSRodney W. Grimes return (1); 108df8bae1dSRodney W. Grimes } 109df8bae1dSRodney W. Grimes return (0); 110df8bae1dSRodney W. Grimes } 111df8bae1dSRodney W. Grimes 112df8bae1dSRodney W. Grimes /* 1132eccc90bSAndre Oppermann * Return 1 if an internet address is for the local host and configured 1142eccc90bSAndre Oppermann * on one of its interfaces. 1152eccc90bSAndre Oppermann */ 1162eccc90bSAndre Oppermann int 117f2565d68SRobert Watson in_localip(struct in_addr in) 1182eccc90bSAndre Oppermann { 1198b615593SMarko Zec INIT_VNET_INET(curvnet); 1202eccc90bSAndre Oppermann struct in_ifaddr *ia; 1212eccc90bSAndre Oppermann 1222eccc90bSAndre Oppermann LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 1232eccc90bSAndre Oppermann if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) 124460473a0SBjoern A. Zeeb return (1); 1252eccc90bSAndre Oppermann } 126460473a0SBjoern A. Zeeb return (0); 1272eccc90bSAndre Oppermann } 1282eccc90bSAndre Oppermann 1292eccc90bSAndre Oppermann /* 130df8bae1dSRodney W. Grimes * Determine whether an IP address is in a reserved set of addresses 131df8bae1dSRodney W. Grimes * that may not be forwarded, or whether datagrams to that destination 132df8bae1dSRodney W. Grimes * may be forwarded. 133df8bae1dSRodney W. Grimes */ 13426f9a767SRodney W. Grimes int 135f2565d68SRobert Watson in_canforward(struct in_addr in) 136df8bae1dSRodney W. Grimes { 137df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 138df8bae1dSRodney W. Grimes register u_long net; 139df8bae1dSRodney W. Grimes 140f8429ca2SBruce M Simpson if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i)) 141df8bae1dSRodney W. Grimes return (0); 142df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) { 143df8bae1dSRodney W. Grimes net = i & IN_CLASSA_NET; 144df8bae1dSRodney W. Grimes if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 145df8bae1dSRodney W. Grimes return (0); 146df8bae1dSRodney W. Grimes } 147df8bae1dSRodney W. Grimes return (1); 148df8bae1dSRodney W. Grimes } 149df8bae1dSRodney W. Grimes 150df8bae1dSRodney W. Grimes /* 151df8bae1dSRodney W. Grimes * Trim a mask in a sockaddr 152df8bae1dSRodney W. Grimes */ 1530312fbe9SPoul-Henning Kamp static void 154f2565d68SRobert Watson in_socktrim(struct sockaddr_in *ap) 155df8bae1dSRodney W. Grimes { 156df8bae1dSRodney W. Grimes register char *cplim = (char *) &ap->sin_addr; 157df8bae1dSRodney W. Grimes register char *cp = (char *) (&ap->sin_addr + 1); 158df8bae1dSRodney W. Grimes 159df8bae1dSRodney W. Grimes ap->sin_len = 0; 160df00058dSGarrett Wollman while (--cp >= cplim) 161df8bae1dSRodney W. Grimes if (*cp) { 162df8bae1dSRodney W. Grimes (ap)->sin_len = cp - (char *) (ap) + 1; 163df8bae1dSRodney W. Grimes break; 164df8bae1dSRodney W. Grimes } 165df8bae1dSRodney W. Grimes } 166df8bae1dSRodney W. Grimes 1676a800098SYoshinobu Inoue static int 1686a800098SYoshinobu Inoue in_mask2len(mask) 1696a800098SYoshinobu Inoue struct in_addr *mask; 1706a800098SYoshinobu Inoue { 1716a800098SYoshinobu Inoue int x, y; 1726a800098SYoshinobu Inoue u_char *p; 1736a800098SYoshinobu Inoue 1746a800098SYoshinobu Inoue p = (u_char *)mask; 1756a800098SYoshinobu Inoue for (x = 0; x < sizeof(*mask); x++) { 1766a800098SYoshinobu Inoue if (p[x] != 0xff) 1776a800098SYoshinobu Inoue break; 1786a800098SYoshinobu Inoue } 1796a800098SYoshinobu Inoue y = 0; 1806a800098SYoshinobu Inoue if (x < sizeof(*mask)) { 1816a800098SYoshinobu Inoue for (y = 0; y < 8; y++) { 1826a800098SYoshinobu Inoue if ((p[x] & (0x80 >> y)) == 0) 1836a800098SYoshinobu Inoue break; 1846a800098SYoshinobu Inoue } 1856a800098SYoshinobu Inoue } 186460473a0SBjoern A. Zeeb return (x * 8 + y); 1876a800098SYoshinobu Inoue } 1886a800098SYoshinobu Inoue 1896a800098SYoshinobu Inoue static void 190f2565d68SRobert Watson in_len2mask(struct in_addr *mask, int len) 1916a800098SYoshinobu Inoue { 1926a800098SYoshinobu Inoue int i; 1936a800098SYoshinobu Inoue u_char *p; 1946a800098SYoshinobu Inoue 1956a800098SYoshinobu Inoue p = (u_char *)mask; 1966a800098SYoshinobu Inoue bzero(mask, sizeof(*mask)); 1976a800098SYoshinobu Inoue for (i = 0; i < len / 8; i++) 1986a800098SYoshinobu Inoue p[i] = 0xff; 1996a800098SYoshinobu Inoue if (len % 8) 2006a800098SYoshinobu Inoue p[i] = (0xff00 >> (len % 8)) & 0xff; 2016a800098SYoshinobu Inoue } 2026a800098SYoshinobu Inoue 203df8bae1dSRodney W. Grimes /* 204df8bae1dSRodney W. Grimes * Generic internet control operations (ioctl's). 205bbb3fb61SRobert Watson * 206bbb3fb61SRobert Watson * ifp is NULL if not an interface-specific ioctl. 207df8bae1dSRodney W. Grimes */ 208df8bae1dSRodney W. Grimes /* ARGSUSED */ 20926f9a767SRodney W. Grimes int 210f2565d68SRobert Watson in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 211f2565d68SRobert Watson struct thread *td) 212df8bae1dSRodney W. Grimes { 2138b615593SMarko Zec INIT_VNET_INET(curvnet); /* both so and ifp can be NULL here! */ 214df8bae1dSRodney W. Grimes register struct ifreq *ifr = (struct ifreq *)data; 215460473a0SBjoern A. Zeeb register struct in_ifaddr *ia, *iap; 216df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 217f7e083afSBruce M Simpson struct in_addr allhosts_addr; 218ca925d9cSJonathan Lemon struct in_addr dst; 219df8bae1dSRodney W. Grimes struct in_ifaddr *oia; 220d10910e6SBruce M Simpson struct in_ifinfo *ii; 221df8bae1dSRodney W. Grimes struct in_aliasreq *ifra = (struct in_aliasreq *)data; 222df8bae1dSRodney W. Grimes struct sockaddr_in oldaddr; 2230f02fdacSBrian Somers int error, hostIsNew, iaIsNew, maskIsNew, s; 224f7e083afSBruce M Simpson int iaIsFirst; 2250f02fdacSBrian Somers 226460473a0SBjoern A. Zeeb ia = NULL; 227f7e083afSBruce M Simpson iaIsFirst = 0; 2280f02fdacSBrian Somers iaIsNew = 0; 229f7e083afSBruce M Simpson allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 230df8bae1dSRodney W. Grimes 231bbb3fb61SRobert Watson /* 232bbb3fb61SRobert Watson * Filter out ioctls we implement directly; forward the rest on to 233bbb3fb61SRobert Watson * in_lifaddr_ioctl() and ifp->if_ioctl(). 234bbb3fb61SRobert Watson */ 2356a800098SYoshinobu Inoue switch (cmd) { 236bbb3fb61SRobert Watson case SIOCAIFADDR: 237bbb3fb61SRobert Watson case SIOCDIFADDR: 238bbb3fb61SRobert Watson case SIOCGIFADDR: 239bbb3fb61SRobert Watson case SIOCGIFBRDADDR: 240bbb3fb61SRobert Watson case SIOCGIFDSTADDR: 241bbb3fb61SRobert Watson case SIOCGIFNETMASK: 242bbb3fb61SRobert Watson case SIOCSIFADDR: 243bbb3fb61SRobert Watson case SIOCSIFBRDADDR: 244bbb3fb61SRobert Watson case SIOCSIFDSTADDR: 245bbb3fb61SRobert Watson case SIOCSIFNETMASK: 246bbb3fb61SRobert Watson break; 247bbb3fb61SRobert Watson 2486a800098SYoshinobu Inoue case SIOCALIFADDR: 249acd3428bSRobert Watson if (td != NULL) { 250acd3428bSRobert Watson error = priv_check(td, PRIV_NET_ADDIFADDR); 251acd3428bSRobert Watson if (error) 252acd3428bSRobert Watson return (error); 253acd3428bSRobert Watson } 254460473a0SBjoern A. Zeeb if (ifp == NULL) 255460473a0SBjoern A. Zeeb return (EINVAL); 256acd3428bSRobert Watson return in_lifaddr_ioctl(so, cmd, data, ifp, td); 257acd3428bSRobert Watson 2586a800098SYoshinobu Inoue case SIOCDLIFADDR: 259acd3428bSRobert Watson if (td != NULL) { 260acd3428bSRobert Watson error = priv_check(td, PRIV_NET_DELIFADDR); 261acd3428bSRobert Watson if (error) 262acd3428bSRobert Watson return (error); 263acd3428bSRobert Watson } 264460473a0SBjoern A. Zeeb if (ifp == NULL) 265460473a0SBjoern A. Zeeb return (EINVAL); 266acd3428bSRobert Watson return in_lifaddr_ioctl(so, cmd, data, ifp, td); 267acd3428bSRobert Watson 2686a800098SYoshinobu Inoue case SIOCGLIFADDR: 269460473a0SBjoern A. Zeeb if (ifp == NULL) 270460473a0SBjoern A. Zeeb return (EINVAL); 271b40ce416SJulian Elischer return in_lifaddr_ioctl(so, cmd, data, ifp, td); 272bbb3fb61SRobert Watson 273bbb3fb61SRobert Watson default: 274bbb3fb61SRobert Watson if (ifp == NULL || ifp->if_ioctl == NULL) 275bbb3fb61SRobert Watson return (EOPNOTSUPP); 276bbb3fb61SRobert Watson return ((*ifp->if_ioctl)(ifp, cmd, data)); 2776a800098SYoshinobu Inoue } 2786a800098SYoshinobu Inoue 279bbb3fb61SRobert Watson if (ifp == NULL) 280bbb3fb61SRobert Watson return (EADDRNOTAVAIL); 281bbb3fb61SRobert Watson 282df8bae1dSRodney W. Grimes /* 283cf7b18f1SRobert Watson * Security checks before we get involved in any work. 284cf7b18f1SRobert Watson */ 285cf7b18f1SRobert Watson switch (cmd) { 286cf7b18f1SRobert Watson case SIOCAIFADDR: 287cf7b18f1SRobert Watson case SIOCSIFADDR: 288cf7b18f1SRobert Watson case SIOCSIFBRDADDR: 289cf7b18f1SRobert Watson case SIOCSIFNETMASK: 290cf7b18f1SRobert Watson case SIOCSIFDSTADDR: 291cf7b18f1SRobert Watson if (td != NULL) { 292cf7b18f1SRobert Watson error = priv_check(td, PRIV_NET_ADDIFADDR); 293cf7b18f1SRobert Watson if (error) 294cf7b18f1SRobert Watson return (error); 295cf7b18f1SRobert Watson } 296cf7b18f1SRobert Watson break; 297cf7b18f1SRobert Watson 298cf7b18f1SRobert Watson case SIOCDIFADDR: 299cf7b18f1SRobert Watson if (td != NULL) { 300cf7b18f1SRobert Watson error = priv_check(td, PRIV_NET_DELIFADDR); 301cf7b18f1SRobert Watson if (error) 302cf7b18f1SRobert Watson return (error); 303cf7b18f1SRobert Watson } 304cf7b18f1SRobert Watson break; 305cf7b18f1SRobert Watson } 306cf7b18f1SRobert Watson 307cf7b18f1SRobert Watson /* 308df8bae1dSRodney W. Grimes * Find address for this interface, if it exists. 309ac0aa473SBill Fenner * 310bbb3fb61SRobert Watson * If an alias address was specified, find that one instead of the 311bbb3fb61SRobert Watson * first one on the interface, if possible. 312df8bae1dSRodney W. Grimes */ 313ca925d9cSJonathan Lemon dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 314bbb3fb61SRobert Watson LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) { 315ca925d9cSJonathan Lemon if (iap->ia_ifp == ifp && 316ca925d9cSJonathan Lemon iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 317bbb3fb61SRobert Watson if (td == NULL || prison_check_ip4(td->td_ucred, 318bbb3fb61SRobert Watson &dst) == 0) 319ac0aa473SBill Fenner ia = iap; 320df8bae1dSRodney W. Grimes break; 321ca925d9cSJonathan Lemon } 322bbb3fb61SRobert Watson } 323bbb3fb61SRobert Watson if (ia == NULL) { 324ca925d9cSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 325ca925d9cSJonathan Lemon iap = ifatoia(ifa); 326ca925d9cSJonathan Lemon if (iap->ia_addr.sin_family == AF_INET) { 3275ce0eb7fSBjoern A. Zeeb if (td != NULL && 328b89e82ddSJamie Gritton prison_check_ip4(td->td_ucred, 329b89e82ddSJamie Gritton &iap->ia_addr.sin_addr) != 0) 3305ce0eb7fSBjoern A. Zeeb continue; 331ac0aa473SBill Fenner ia = iap; 332ac0aa473SBill Fenner break; 333ac0aa473SBill Fenner } 334ac0aa473SBill Fenner } 335bbb3fb61SRobert Watson } 336f7e083afSBruce M Simpson if (ia == NULL) 337f7e083afSBruce M Simpson iaIsFirst = 1; 338df8bae1dSRodney W. Grimes 339df8bae1dSRodney W. Grimes switch (cmd) { 340df8bae1dSRodney W. Grimes case SIOCAIFADDR: 341df8bae1dSRodney W. Grimes case SIOCDIFADDR: 3421067217dSGarrett Wollman if (ifra->ifra_addr.sin_family == AF_INET) { 343fc2ffbe6SPoul-Henning Kamp for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) { 344df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp && 345df8bae1dSRodney W. Grimes ia->ia_addr.sin_addr.s_addr == 346df8bae1dSRodney W. Grimes ifra->ifra_addr.sin_addr.s_addr) 347df8bae1dSRodney W. Grimes break; 348df8bae1dSRodney W. Grimes } 3491067217dSGarrett Wollman if ((ifp->if_flags & IFF_POINTOPOINT) 3501067217dSGarrett Wollman && (cmd == SIOCAIFADDR) 3511067217dSGarrett Wollman && (ifra->ifra_dstaddr.sin_addr.s_addr 3521067217dSGarrett Wollman == INADDR_ANY)) { 353460473a0SBjoern A. Zeeb return (EDESTADDRREQ); 3541067217dSGarrett Wollman } 3551067217dSGarrett Wollman } 356460473a0SBjoern A. Zeeb if (cmd == SIOCDIFADDR && ia == NULL) 357df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 358df8bae1dSRodney W. Grimes /* FALLTHROUGH */ 359df8bae1dSRodney W. Grimes case SIOCSIFADDR: 360df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 361df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 362460473a0SBjoern A. Zeeb if (ia == NULL) { 36359562606SGarrett Wollman ia = (struct in_ifaddr *) 364a163d034SWarner Losh malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO); 365460473a0SBjoern A. Zeeb if (ia == NULL) 366df8bae1dSRodney W. Grimes return (ENOBUFS); 367c655b7c4SDavid Greenman /* 368c655b7c4SDavid Greenman * Protect from ipintr() traversing address list 369c655b7c4SDavid Greenman * while we're modifying it. 370c655b7c4SDavid Greenman */ 37119fc74fbSJeffrey Hsu ifa = &ia->ia_ifa; 37219fc74fbSJeffrey Hsu IFA_LOCK_INIT(ifa); 37359562606SGarrett Wollman ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 37459562606SGarrett Wollman ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 37559562606SGarrett Wollman ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 37619fc74fbSJeffrey Hsu ifa->ifa_refcnt = 1; 37719fc74fbSJeffrey Hsu 378df8bae1dSRodney W. Grimes ia->ia_sockmask.sin_len = 8; 379bc183b3fSDag-Erling Smørgrav ia->ia_sockmask.sin_family = AF_INET; 380df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 381df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 382df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_family = AF_INET; 383df8bae1dSRodney W. Grimes } 384df8bae1dSRodney W. Grimes ia->ia_ifp = ifp; 385f3d30eb2SGleb Smirnoff 3868021456aSRobert Watson IF_ADDR_LOCK(ifp); 3878021456aSRobert Watson TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 3888021456aSRobert Watson IF_ADDR_UNLOCK(ifp); 3898021456aSRobert Watson s = splnet(); 390603724d3SBjoern A. Zeeb TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 391c655b7c4SDavid Greenman splx(s); 3920f02fdacSBrian Somers iaIsNew = 1; 393df8bae1dSRodney W. Grimes } 394df8bae1dSRodney W. Grimes break; 395df8bae1dSRodney W. Grimes 396df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 397df8bae1dSRodney W. Grimes case SIOCGIFADDR: 398df8bae1dSRodney W. Grimes case SIOCGIFNETMASK: 399df8bae1dSRodney W. Grimes case SIOCGIFDSTADDR: 400df8bae1dSRodney W. Grimes case SIOCGIFBRDADDR: 401460473a0SBjoern A. Zeeb if (ia == NULL) 402df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 403df8bae1dSRodney W. Grimes break; 404df8bae1dSRodney W. Grimes } 405df8bae1dSRodney W. Grimes switch (cmd) { 406df8bae1dSRodney W. Grimes 407df8bae1dSRodney W. Grimes case SIOCGIFADDR: 408df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 4090f02fdacSBrian Somers return (0); 410df8bae1dSRodney W. Grimes 411df8bae1dSRodney W. Grimes case SIOCGIFBRDADDR: 412df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 413df8bae1dSRodney W. Grimes return (EINVAL); 414df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 4150f02fdacSBrian Somers return (0); 416df8bae1dSRodney W. Grimes 417df8bae1dSRodney W. Grimes case SIOCGIFDSTADDR: 418df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 419df8bae1dSRodney W. Grimes return (EINVAL); 420df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 4210f02fdacSBrian Somers return (0); 422df8bae1dSRodney W. Grimes 423df8bae1dSRodney W. Grimes case SIOCGIFNETMASK: 424df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 4250f02fdacSBrian Somers return (0); 426df8bae1dSRodney W. Grimes 427df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 428df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 429df8bae1dSRodney W. Grimes return (EINVAL); 430df8bae1dSRodney W. Grimes oldaddr = ia->ia_dstaddr; 431df8bae1dSRodney W. Grimes ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 432460473a0SBjoern A. Zeeb if (ifp->if_ioctl != NULL) { 433ba5da2a0SIan Dowse error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, 434ba5da2a0SIan Dowse (caddr_t)ia); 435ba5da2a0SIan Dowse if (error) { 436df8bae1dSRodney W. Grimes ia->ia_dstaddr = oldaddr; 437df8bae1dSRodney W. Grimes return (error); 438df8bae1dSRodney W. Grimes } 439ba5da2a0SIan Dowse } 440df8bae1dSRodney W. Grimes if (ia->ia_flags & IFA_ROUTE) { 441df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 442df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 443df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr = 444df8bae1dSRodney W. Grimes (struct sockaddr *)&ia->ia_dstaddr; 445df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 446df8bae1dSRodney W. Grimes } 4470f02fdacSBrian Somers return (0); 448df8bae1dSRodney W. Grimes 449df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 450df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 451df8bae1dSRodney W. Grimes return (EINVAL); 452df8bae1dSRodney W. Grimes ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 4530f02fdacSBrian Somers return (0); 454df8bae1dSRodney W. Grimes 455df8bae1dSRodney W. Grimes case SIOCSIFADDR: 4560f02fdacSBrian Somers error = in_ifinit(ifp, ia, 4570f02fdacSBrian Somers (struct sockaddr_in *) &ifr->ifr_addr, 1); 4580f02fdacSBrian Somers if (error != 0 && iaIsNew) 4590f02fdacSBrian Somers break; 460f7e083afSBruce M Simpson if (error == 0) { 461c75aa354SBruce M Simpson ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 462d10910e6SBruce M Simpson if (iaIsFirst && 463d10910e6SBruce M Simpson (ifp->if_flags & IFF_MULTICAST) != 0) { 464d10910e6SBruce M Simpson error = in_joingroup(ifp, &allhosts_addr, 465d10910e6SBruce M Simpson NULL, &ii->ii_allhosts); 466d10910e6SBruce M Simpson } 46725a4adceSMax Laier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 468f7e083afSBruce M Simpson } 4690f02fdacSBrian Somers return (0); 470df8bae1dSRodney W. Grimes 471df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 472bc183b3fSDag-Erling Smørgrav ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; 473bc183b3fSDag-Erling Smørgrav ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 4740f02fdacSBrian Somers return (0); 475df8bae1dSRodney W. Grimes 476df8bae1dSRodney W. Grimes case SIOCAIFADDR: 477df8bae1dSRodney W. Grimes maskIsNew = 0; 478df8bae1dSRodney W. Grimes hostIsNew = 1; 479df8bae1dSRodney W. Grimes error = 0; 480df8bae1dSRodney W. Grimes if (ia->ia_addr.sin_family == AF_INET) { 481df8bae1dSRodney W. Grimes if (ifra->ifra_addr.sin_len == 0) { 482df8bae1dSRodney W. Grimes ifra->ifra_addr = ia->ia_addr; 483df8bae1dSRodney W. Grimes hostIsNew = 0; 484df8bae1dSRodney W. Grimes } else if (ifra->ifra_addr.sin_addr.s_addr == 485df8bae1dSRodney W. Grimes ia->ia_addr.sin_addr.s_addr) 486df8bae1dSRodney W. Grimes hostIsNew = 0; 487df8bae1dSRodney W. Grimes } 488df8bae1dSRodney W. Grimes if (ifra->ifra_mask.sin_len) { 489df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 490df8bae1dSRodney W. Grimes ia->ia_sockmask = ifra->ifra_mask; 491bc183b3fSDag-Erling Smørgrav ia->ia_sockmask.sin_family = AF_INET; 492df8bae1dSRodney W. Grimes ia->ia_subnetmask = 493df8bae1dSRodney W. Grimes ntohl(ia->ia_sockmask.sin_addr.s_addr); 494df8bae1dSRodney W. Grimes maskIsNew = 1; 495df8bae1dSRodney W. Grimes } 496df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) && 497df8bae1dSRodney W. Grimes (ifra->ifra_dstaddr.sin_family == AF_INET)) { 498df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 499df8bae1dSRodney W. Grimes ia->ia_dstaddr = ifra->ifra_dstaddr; 500df8bae1dSRodney W. Grimes maskIsNew = 1; /* We lie; but the effect's the same */ 501df8bae1dSRodney W. Grimes } 502df8bae1dSRodney W. Grimes if (ifra->ifra_addr.sin_family == AF_INET && 503df8bae1dSRodney W. Grimes (hostIsNew || maskIsNew)) 504df8bae1dSRodney W. Grimes error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 5050f02fdacSBrian Somers if (error != 0 && iaIsNew) 5060f02fdacSBrian Somers break; 5070f02fdacSBrian Somers 508df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) && 509df8bae1dSRodney W. Grimes (ifra->ifra_broadaddr.sin_family == AF_INET)) 510df8bae1dSRodney W. Grimes ia->ia_broadaddr = ifra->ifra_broadaddr; 511f7e083afSBruce M Simpson if (error == 0) { 512c75aa354SBruce M Simpson ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 513d10910e6SBruce M Simpson if (iaIsFirst && 514d10910e6SBruce M Simpson (ifp->if_flags & IFF_MULTICAST) != 0) { 515d10910e6SBruce M Simpson error = in_joingroup(ifp, &allhosts_addr, 516d10910e6SBruce M Simpson NULL, &ii->ii_allhosts); 517d10910e6SBruce M Simpson } 51825a4adceSMax Laier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 519f7e083afSBruce M Simpson } 520df8bae1dSRodney W. Grimes return (error); 521df8bae1dSRodney W. Grimes 522df8bae1dSRodney W. Grimes case SIOCDIFADDR: 523089cdfadSRuslan Ermilov /* 524089cdfadSRuslan Ermilov * in_ifscrub kills the interface route. 525089cdfadSRuslan Ermilov */ 526df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 527c655b7c4SDavid Greenman /* 528089cdfadSRuslan Ermilov * in_ifadown gets rid of all the rest of 529089cdfadSRuslan Ermilov * the routes. This is not quite the right 530089cdfadSRuslan Ermilov * thing to do, but at least if we are running 531089cdfadSRuslan Ermilov * a routing process they will come back. 532089cdfadSRuslan Ermilov */ 53391854268SRuslan Ermilov in_ifadown(&ia->ia_ifa, 1); 53425a4adceSMax Laier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 5350f02fdacSBrian Somers error = 0; 536df8bae1dSRodney W. Grimes break; 537df8bae1dSRodney W. Grimes 538df8bae1dSRodney W. Grimes default: 539bbb3fb61SRobert Watson panic("in_control: unsupported ioctl"); 540df8bae1dSRodney W. Grimes } 5410f02fdacSBrian Somers 5420f02fdacSBrian Somers /* 5430f02fdacSBrian Somers * Protect from ipintr() traversing address list while we're modifying 5440f02fdacSBrian Somers * it. 5450f02fdacSBrian Somers */ 5468021456aSRobert Watson IF_ADDR_LOCK(ifp); 5470f02fdacSBrian Somers TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 5488021456aSRobert Watson IF_ADDR_UNLOCK(ifp); 5498021456aSRobert Watson s = splnet(); 550603724d3SBjoern A. Zeeb TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 551f7e083afSBruce M Simpson if (ia->ia_addr.sin_family == AF_INET) { 5520f02fdacSBrian Somers LIST_REMOVE(ia, ia_hash); 553f7e083afSBruce M Simpson /* 554f7e083afSBruce M Simpson * If this is the last IPv4 address configured on this 555f7e083afSBruce M Simpson * interface, leave the all-hosts group. 556d10910e6SBruce M Simpson * No state-change report need be transmitted. 557f7e083afSBruce M Simpson */ 558f7e083afSBruce M Simpson oia = NULL; 559f7e083afSBruce M Simpson IFP_TO_IA(ifp, oia); 560f7e083afSBruce M Simpson if (oia == NULL) { 561c75aa354SBruce M Simpson ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 562f7e083afSBruce M Simpson IN_MULTI_LOCK(); 563d10910e6SBruce M Simpson if (ii->ii_allhosts) { 564d10910e6SBruce M Simpson (void)in_leavegroup_locked(ii->ii_allhosts, 565d10910e6SBruce M Simpson NULL); 566d10910e6SBruce M Simpson ii->ii_allhosts = NULL; 567d10910e6SBruce M Simpson } 568f7e083afSBruce M Simpson IN_MULTI_UNLOCK(); 569f7e083afSBruce M Simpson } 570f7e083afSBruce M Simpson } 5710f02fdacSBrian Somers IFAFREE(&ia->ia_ifa); 5720f02fdacSBrian Somers splx(s); 5730f02fdacSBrian Somers 5740f02fdacSBrian Somers return (error); 575df8bae1dSRodney W. Grimes } 576df8bae1dSRodney W. Grimes 577df8bae1dSRodney W. Grimes /* 5786a800098SYoshinobu Inoue * SIOC[GAD]LIFADDR. 5796a800098SYoshinobu Inoue * SIOCGLIFADDR: get first address. (?!?) 5806a800098SYoshinobu Inoue * SIOCGLIFADDR with IFLR_PREFIX: 5816a800098SYoshinobu Inoue * get first address that matches the specified prefix. 5826a800098SYoshinobu Inoue * SIOCALIFADDR: add the specified address. 5836a800098SYoshinobu Inoue * SIOCALIFADDR with IFLR_PREFIX: 5846a800098SYoshinobu Inoue * EINVAL since we can't deduce hostid part of the address. 5856a800098SYoshinobu Inoue * SIOCDLIFADDR: delete the specified address. 5866a800098SYoshinobu Inoue * SIOCDLIFADDR with IFLR_PREFIX: 5876a800098SYoshinobu Inoue * delete the first address that matches the specified prefix. 5886a800098SYoshinobu Inoue * return values: 5896a800098SYoshinobu Inoue * EINVAL on invalid parameters 5906a800098SYoshinobu Inoue * EADDRNOTAVAIL on prefix match failed/specified address not found 5916a800098SYoshinobu Inoue * other values may be returned from in_ioctl() 5926a800098SYoshinobu Inoue */ 5936a800098SYoshinobu Inoue static int 594f2565d68SRobert Watson in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 595f2565d68SRobert Watson struct ifnet *ifp, struct thread *td) 5966a800098SYoshinobu Inoue { 5976a800098SYoshinobu Inoue struct if_laddrreq *iflr = (struct if_laddrreq *)data; 5986a800098SYoshinobu Inoue struct ifaddr *ifa; 5996a800098SYoshinobu Inoue 6006a800098SYoshinobu Inoue /* sanity checks */ 601460473a0SBjoern A. Zeeb if (data == NULL || ifp == NULL) { 6026a800098SYoshinobu Inoue panic("invalid argument to in_lifaddr_ioctl"); 6036a800098SYoshinobu Inoue /*NOTRECHED*/ 6046a800098SYoshinobu Inoue } 6056a800098SYoshinobu Inoue 6066a800098SYoshinobu Inoue switch (cmd) { 6076a800098SYoshinobu Inoue case SIOCGLIFADDR: 6086a800098SYoshinobu Inoue /* address must be specified on GET with IFLR_PREFIX */ 6096a800098SYoshinobu Inoue if ((iflr->flags & IFLR_PREFIX) == 0) 6106a800098SYoshinobu Inoue break; 6116a800098SYoshinobu Inoue /*FALLTHROUGH*/ 6126a800098SYoshinobu Inoue case SIOCALIFADDR: 6136a800098SYoshinobu Inoue case SIOCDLIFADDR: 6146a800098SYoshinobu Inoue /* address must be specified on ADD and DELETE */ 6155d60ed0eSYoshinobu Inoue if (iflr->addr.ss_family != AF_INET) 616460473a0SBjoern A. Zeeb return (EINVAL); 6175d60ed0eSYoshinobu Inoue if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 618460473a0SBjoern A. Zeeb return (EINVAL); 6196a800098SYoshinobu Inoue /* XXX need improvement */ 6205d60ed0eSYoshinobu Inoue if (iflr->dstaddr.ss_family 6215d60ed0eSYoshinobu Inoue && iflr->dstaddr.ss_family != AF_INET) 622460473a0SBjoern A. Zeeb return (EINVAL); 6235d60ed0eSYoshinobu Inoue if (iflr->dstaddr.ss_family 6245d60ed0eSYoshinobu Inoue && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 625460473a0SBjoern A. Zeeb return (EINVAL); 6266a800098SYoshinobu Inoue break; 6276a800098SYoshinobu Inoue default: /*shouldn't happen*/ 628460473a0SBjoern A. Zeeb return (EOPNOTSUPP); 6296a800098SYoshinobu Inoue } 6306a800098SYoshinobu Inoue if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 631460473a0SBjoern A. Zeeb return (EINVAL); 6326a800098SYoshinobu Inoue 6336a800098SYoshinobu Inoue switch (cmd) { 6346a800098SYoshinobu Inoue case SIOCALIFADDR: 6356a800098SYoshinobu Inoue { 6366a800098SYoshinobu Inoue struct in_aliasreq ifra; 6376a800098SYoshinobu Inoue 6386a800098SYoshinobu Inoue if (iflr->flags & IFLR_PREFIX) 639460473a0SBjoern A. Zeeb return (EINVAL); 6406a800098SYoshinobu Inoue 6416a800098SYoshinobu Inoue /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 6426a800098SYoshinobu Inoue bzero(&ifra, sizeof(ifra)); 6436a800098SYoshinobu Inoue bcopy(iflr->iflr_name, ifra.ifra_name, 6446a800098SYoshinobu Inoue sizeof(ifra.ifra_name)); 6456a800098SYoshinobu Inoue 6465d60ed0eSYoshinobu Inoue bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 6476a800098SYoshinobu Inoue 6485d60ed0eSYoshinobu Inoue if (iflr->dstaddr.ss_family) { /*XXX*/ 6496a800098SYoshinobu Inoue bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 6505d60ed0eSYoshinobu Inoue iflr->dstaddr.ss_len); 6516a800098SYoshinobu Inoue } 6526a800098SYoshinobu Inoue 6536a800098SYoshinobu Inoue ifra.ifra_mask.sin_family = AF_INET; 6546a800098SYoshinobu Inoue ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 6556a800098SYoshinobu Inoue in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 6566a800098SYoshinobu Inoue 657460473a0SBjoern A. Zeeb return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td)); 6586a800098SYoshinobu Inoue } 6596a800098SYoshinobu Inoue case SIOCGLIFADDR: 6606a800098SYoshinobu Inoue case SIOCDLIFADDR: 6616a800098SYoshinobu Inoue { 6626a800098SYoshinobu Inoue struct in_ifaddr *ia; 6636a800098SYoshinobu Inoue struct in_addr mask, candidate, match; 6646a800098SYoshinobu Inoue struct sockaddr_in *sin; 6656a800098SYoshinobu Inoue 6666a800098SYoshinobu Inoue bzero(&mask, sizeof(mask)); 667fbdd20a1SMatt Jacob bzero(&match, sizeof(match)); 6686a800098SYoshinobu Inoue if (iflr->flags & IFLR_PREFIX) { 6696a800098SYoshinobu Inoue /* lookup a prefix rather than address. */ 6706a800098SYoshinobu Inoue in_len2mask(&mask, iflr->prefixlen); 6716a800098SYoshinobu Inoue 6726a800098SYoshinobu Inoue sin = (struct sockaddr_in *)&iflr->addr; 6736a800098SYoshinobu Inoue match.s_addr = sin->sin_addr.s_addr; 6746a800098SYoshinobu Inoue match.s_addr &= mask.s_addr; 6756a800098SYoshinobu Inoue 6766a800098SYoshinobu Inoue /* if you set extra bits, that's wrong */ 6776a800098SYoshinobu Inoue if (match.s_addr != sin->sin_addr.s_addr) 678460473a0SBjoern A. Zeeb return (EINVAL); 6796a800098SYoshinobu Inoue 6806a800098SYoshinobu Inoue } else { 6816a800098SYoshinobu Inoue /* on getting an address, take the 1st match */ 6826a800098SYoshinobu Inoue /* on deleting an address, do exact match */ 683fbdd20a1SMatt Jacob if (cmd != SIOCGLIFADDR) { 6846a800098SYoshinobu Inoue in_len2mask(&mask, 32); 6856a800098SYoshinobu Inoue sin = (struct sockaddr_in *)&iflr->addr; 6866a800098SYoshinobu Inoue match.s_addr = sin->sin_addr.s_addr; 6876a800098SYoshinobu Inoue } 6886a800098SYoshinobu Inoue } 6896a800098SYoshinobu Inoue 6906a800098SYoshinobu Inoue TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 6916a800098SYoshinobu Inoue if (ifa->ifa_addr->sa_family != AF_INET6) 6926a800098SYoshinobu Inoue continue; 693fbdd20a1SMatt Jacob if (match.s_addr == 0) 6946a800098SYoshinobu Inoue break; 6956a800098SYoshinobu Inoue candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 6966a800098SYoshinobu Inoue candidate.s_addr &= mask.s_addr; 6976a800098SYoshinobu Inoue if (candidate.s_addr == match.s_addr) 6986a800098SYoshinobu Inoue break; 6996a800098SYoshinobu Inoue } 700460473a0SBjoern A. Zeeb if (ifa == NULL) 701460473a0SBjoern A. Zeeb return (EADDRNOTAVAIL); 7026a800098SYoshinobu Inoue ia = (struct in_ifaddr *)ifa; 7036a800098SYoshinobu Inoue 7046a800098SYoshinobu Inoue if (cmd == SIOCGLIFADDR) { 7056a800098SYoshinobu Inoue /* fill in the if_laddrreq structure */ 7066a800098SYoshinobu Inoue bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 7076a800098SYoshinobu Inoue 7086a800098SYoshinobu Inoue if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 7096a800098SYoshinobu Inoue bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 7106a800098SYoshinobu Inoue ia->ia_dstaddr.sin_len); 7116a800098SYoshinobu Inoue } else 7126a800098SYoshinobu Inoue bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 7136a800098SYoshinobu Inoue 7146a800098SYoshinobu Inoue iflr->prefixlen = 7156a800098SYoshinobu Inoue in_mask2len(&ia->ia_sockmask.sin_addr); 7166a800098SYoshinobu Inoue 7176a800098SYoshinobu Inoue iflr->flags = 0; /*XXX*/ 7186a800098SYoshinobu Inoue 719460473a0SBjoern A. Zeeb return (0); 7206a800098SYoshinobu Inoue } else { 7216a800098SYoshinobu Inoue struct in_aliasreq ifra; 7226a800098SYoshinobu Inoue 7236a800098SYoshinobu Inoue /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 7246a800098SYoshinobu Inoue bzero(&ifra, sizeof(ifra)); 7256a800098SYoshinobu Inoue bcopy(iflr->iflr_name, ifra.ifra_name, 7266a800098SYoshinobu Inoue sizeof(ifra.ifra_name)); 7276a800098SYoshinobu Inoue 7286a800098SYoshinobu Inoue bcopy(&ia->ia_addr, &ifra.ifra_addr, 7296a800098SYoshinobu Inoue ia->ia_addr.sin_len); 7306a800098SYoshinobu Inoue if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 7316a800098SYoshinobu Inoue bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 7326a800098SYoshinobu Inoue ia->ia_dstaddr.sin_len); 7336a800098SYoshinobu Inoue } 7346a800098SYoshinobu Inoue bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 7356a800098SYoshinobu Inoue ia->ia_sockmask.sin_len); 7366a800098SYoshinobu Inoue 737460473a0SBjoern A. Zeeb return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 738460473a0SBjoern A. Zeeb ifp, td)); 7396a800098SYoshinobu Inoue } 7406a800098SYoshinobu Inoue } 7416a800098SYoshinobu Inoue } 7426a800098SYoshinobu Inoue 743460473a0SBjoern A. Zeeb return (EOPNOTSUPP); /*just for safety*/ 7446a800098SYoshinobu Inoue } 7456a800098SYoshinobu Inoue 7466a800098SYoshinobu Inoue /* 747df8bae1dSRodney W. Grimes * Delete any existing route for an interface. 748df8bae1dSRodney W. Grimes */ 74939191c8eSGarrett Wollman void 750f2565d68SRobert Watson in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia) 751df8bae1dSRodney W. Grimes { 752f2565d68SRobert Watson 75348321abeSMax Laier in_scrubprefix(ia); 754df8bae1dSRodney W. Grimes } 755df8bae1dSRodney W. Grimes 756df8bae1dSRodney W. Grimes /* 757df8bae1dSRodney W. Grimes * Initialize an interface's internet address 758df8bae1dSRodney W. Grimes * and routing table entry. 759df8bae1dSRodney W. Grimes */ 7600312fbe9SPoul-Henning Kamp static int 761f2565d68SRobert Watson in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, 762f2565d68SRobert Watson int scrub) 763df8bae1dSRodney W. Grimes { 7648b615593SMarko Zec INIT_VNET_INET(ifp->if_vnet); 765df8bae1dSRodney W. Grimes register u_long i = ntohl(sin->sin_addr.s_addr); 766df8bae1dSRodney W. Grimes struct sockaddr_in oldaddr; 7675a43847dSBrian Somers int s = splimp(), flags = RTF_UP, error = 0; 768df8bae1dSRodney W. Grimes 769df8bae1dSRodney W. Grimes oldaddr = ia->ia_addr; 7702754d95dSSUZUKI Shinsuke if (oldaddr.sin_family == AF_INET) 7712754d95dSSUZUKI Shinsuke LIST_REMOVE(ia, ia_hash); 772df8bae1dSRodney W. Grimes ia->ia_addr = *sin; 7732754d95dSSUZUKI Shinsuke if (ia->ia_addr.sin_family == AF_INET) 7742754d95dSSUZUKI Shinsuke LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 7752754d95dSSUZUKI Shinsuke ia, ia_hash); 776df8bae1dSRodney W. Grimes /* 777df8bae1dSRodney W. Grimes * Give the interface a chance to initialize 778df8bae1dSRodney W. Grimes * if this is its first address, 779df8bae1dSRodney W. Grimes * and to validate the address if necessary. 780df8bae1dSRodney W. Grimes */ 781460473a0SBjoern A. Zeeb if (ifp->if_ioctl != NULL) { 782ba5da2a0SIan Dowse error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 783ba5da2a0SIan Dowse if (error) { 784df8bae1dSRodney W. Grimes splx(s); 7852754d95dSSUZUKI Shinsuke /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 786df8bae1dSRodney W. Grimes ia->ia_addr = oldaddr; 78722c819a7SJonathan Lemon if (ia->ia_addr.sin_family == AF_INET) 788ba5da2a0SIan Dowse LIST_INSERT_HEAD(INADDR_HASH( 789ba5da2a0SIan Dowse ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 790cf77b848SOleksandr Tymoshenko else 791cf77b848SOleksandr Tymoshenko /* 792cf77b848SOleksandr Tymoshenko * If oldaddr family is not AF_INET (e.g. 793cf77b848SOleksandr Tymoshenko * interface has been just created) in_control 794cf77b848SOleksandr Tymoshenko * does not call LIST_REMOVE, and we end up 795cf77b848SOleksandr Tymoshenko * with bogus ia entries in hash 796cf77b848SOleksandr Tymoshenko */ 797cf77b848SOleksandr Tymoshenko LIST_REMOVE(ia, ia_hash); 7982754d95dSSUZUKI Shinsuke return (error); 7992754d95dSSUZUKI Shinsuke } 800ba5da2a0SIan Dowse } 801df8bae1dSRodney W. Grimes splx(s); 802df8bae1dSRodney W. Grimes if (scrub) { 803df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 804df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 805df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 806df8bae1dSRodney W. Grimes } 807df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) 808df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSA_NET; 809df8bae1dSRodney W. Grimes else if (IN_CLASSB(i)) 810df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSB_NET; 811df8bae1dSRodney W. Grimes else 812df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSC_NET; 813df8bae1dSRodney W. Grimes /* 814df8bae1dSRodney W. Grimes * The subnet mask usually includes at least the standard network part, 815df8bae1dSRodney W. Grimes * but may may be smaller in the case of supernetting. 816df8bae1dSRodney W. Grimes * If it is set, we believe it. 817df8bae1dSRodney W. Grimes */ 818df8bae1dSRodney W. Grimes if (ia->ia_subnetmask == 0) { 819df8bae1dSRodney W. Grimes ia->ia_subnetmask = ia->ia_netmask; 820df8bae1dSRodney W. Grimes ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 821df8bae1dSRodney W. Grimes } else 822df8bae1dSRodney W. Grimes ia->ia_netmask &= ia->ia_subnetmask; 823df8bae1dSRodney W. Grimes ia->ia_net = i & ia->ia_netmask; 824df8bae1dSRodney W. Grimes ia->ia_subnet = i & ia->ia_subnetmask; 825df8bae1dSRodney W. Grimes in_socktrim(&ia->ia_sockmask); 82650bb1704SGleb Smirnoff #ifdef DEV_CARP 82750bb1704SGleb Smirnoff /* 82850bb1704SGleb Smirnoff * XXX: carp(4) does not have interface route 82950bb1704SGleb Smirnoff */ 83050bb1704SGleb Smirnoff if (ifp->if_type == IFT_CARP) 83150bb1704SGleb Smirnoff return (0); 83250bb1704SGleb Smirnoff #endif 833df8bae1dSRodney W. Grimes /* 834df8bae1dSRodney W. Grimes * Add route for the network. 835df8bae1dSRodney W. Grimes */ 836df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_metric = ifp->if_metric; 837df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 838df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_addr.s_addr = 839df8bae1dSRodney W. Grimes htonl(ia->ia_subnet | ~ia->ia_subnetmask); 840df8bae1dSRodney W. Grimes ia->ia_netbroadcast.s_addr = 841df8bae1dSRodney W. Grimes htonl(ia->ia_net | ~ ia->ia_netmask); 842df8bae1dSRodney W. Grimes } else if (ifp->if_flags & IFF_LOOPBACK) { 8439a6a6eebSMax Laier ia->ia_dstaddr = ia->ia_addr; 844df8bae1dSRodney W. Grimes flags |= RTF_HOST; 845df8bae1dSRodney W. Grimes } else if (ifp->if_flags & IFF_POINTOPOINT) { 846df8bae1dSRodney W. Grimes if (ia->ia_dstaddr.sin_family != AF_INET) 847df8bae1dSRodney W. Grimes return (0); 848df8bae1dSRodney W. Grimes flags |= RTF_HOST; 849df8bae1dSRodney W. Grimes } 85048321abeSMax Laier if ((error = in_addprefix(ia, flags)) != 0) 8510f02fdacSBrian Somers return (error); 8520f02fdacSBrian Somers 853df8bae1dSRodney W. Grimes return (error); 854df8bae1dSRodney W. Grimes } 855df8bae1dSRodney W. Grimes 85648321abeSMax Laier #define rtinitflags(x) \ 85748321abeSMax Laier ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 85848321abeSMax Laier ? RTF_HOST : 0) 85948321abeSMax Laier /* 860fbdd20a1SMatt Jacob * Check if we have a route for the given prefix already or add one accordingly. 86148321abeSMax Laier */ 86248321abeSMax Laier static int 863f2565d68SRobert Watson in_addprefix(struct in_ifaddr *target, int flags) 86448321abeSMax Laier { 8658b615593SMarko Zec INIT_VNET_INET(curvnet); 86648321abeSMax Laier struct in_ifaddr *ia; 867bfb26eecSGleb Smirnoff struct in_addr prefix, mask, p, m; 86848321abeSMax Laier int error; 86948321abeSMax Laier 870fbdd20a1SMatt Jacob if ((flags & RTF_HOST) != 0) { 87148321abeSMax Laier prefix = target->ia_dstaddr.sin_addr; 872fbdd20a1SMatt Jacob mask.s_addr = 0; 873fbdd20a1SMatt Jacob } else { 87448321abeSMax Laier prefix = target->ia_addr.sin_addr; 87548321abeSMax Laier mask = target->ia_sockmask.sin_addr; 87648321abeSMax Laier prefix.s_addr &= mask.s_addr; 87748321abeSMax Laier } 87848321abeSMax Laier 879603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 880bfb26eecSGleb Smirnoff if (rtinitflags(ia)) { 88148321abeSMax Laier p = ia->ia_addr.sin_addr; 88248321abeSMax Laier 88348321abeSMax Laier if (prefix.s_addr != p.s_addr) 88448321abeSMax Laier continue; 885bfb26eecSGleb Smirnoff } else { 886bfb26eecSGleb Smirnoff p = ia->ia_addr.sin_addr; 887bfb26eecSGleb Smirnoff m = ia->ia_sockmask.sin_addr; 888bfb26eecSGleb Smirnoff p.s_addr &= m.s_addr; 889bfb26eecSGleb Smirnoff 890bfb26eecSGleb Smirnoff if (prefix.s_addr != p.s_addr || 891bfb26eecSGleb Smirnoff mask.s_addr != m.s_addr) 892bfb26eecSGleb Smirnoff continue; 893bfb26eecSGleb Smirnoff } 89448321abeSMax Laier 89548321abeSMax Laier /* 89648321abeSMax Laier * If we got a matching prefix route inserted by other 89748321abeSMax Laier * interface address, we are done here. 89848321abeSMax Laier */ 8991ae95409SGleb Smirnoff if (ia->ia_flags & IFA_ROUTE) { 900603724d3SBjoern A. Zeeb if (V_sameprefixcarponly && 9011ae95409SGleb Smirnoff target->ia_ifp->if_type != IFT_CARP && 9021ae95409SGleb Smirnoff ia->ia_ifp->if_type != IFT_CARP) 9031ae95409SGleb Smirnoff return (EEXIST); 9041ae95409SGleb Smirnoff else 9051ae95409SGleb Smirnoff return (0); 9061ae95409SGleb Smirnoff } 90748321abeSMax Laier } 90848321abeSMax Laier 90948321abeSMax Laier /* 91048321abeSMax Laier * No-one seem to have this prefix route, so we try to insert it. 91148321abeSMax Laier */ 91248321abeSMax Laier error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 91348321abeSMax Laier if (!error) 91448321abeSMax Laier target->ia_flags |= IFA_ROUTE; 915460473a0SBjoern A. Zeeb return (error); 91648321abeSMax Laier } 91748321abeSMax Laier 9186e6b3f7cSQing Li extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr); 9196e6b3f7cSQing Li 92048321abeSMax Laier /* 92148321abeSMax Laier * If there is no other address in the system that can serve a route to the 92248321abeSMax Laier * same prefix, remove the route. Hand over the route to the new address 92348321abeSMax Laier * otherwise. 92448321abeSMax Laier */ 92548321abeSMax Laier static int 926f2565d68SRobert Watson in_scrubprefix(struct in_ifaddr *target) 92748321abeSMax Laier { 9288b615593SMarko Zec INIT_VNET_INET(curvnet); 92948321abeSMax Laier struct in_ifaddr *ia; 93048321abeSMax Laier struct in_addr prefix, mask, p; 93148321abeSMax Laier int error; 93248321abeSMax Laier 93348321abeSMax Laier if ((target->ia_flags & IFA_ROUTE) == 0) 934460473a0SBjoern A. Zeeb return (0); 93548321abeSMax Laier 93648321abeSMax Laier if (rtinitflags(target)) 93748321abeSMax Laier prefix = target->ia_dstaddr.sin_addr; 93848321abeSMax Laier else { 93948321abeSMax Laier prefix = target->ia_addr.sin_addr; 94048321abeSMax Laier mask = target->ia_sockmask.sin_addr; 94148321abeSMax Laier prefix.s_addr &= mask.s_addr; 9426e6b3f7cSQing Li /* remove arp cache */ 9436e6b3f7cSQing Li arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr); 94448321abeSMax Laier } 94548321abeSMax Laier 946603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 94748321abeSMax Laier if (rtinitflags(ia)) 94848321abeSMax Laier p = ia->ia_dstaddr.sin_addr; 94948321abeSMax Laier else { 95048321abeSMax Laier p = ia->ia_addr.sin_addr; 95148321abeSMax Laier p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 95248321abeSMax Laier } 95348321abeSMax Laier 95448321abeSMax Laier if (prefix.s_addr != p.s_addr) 95548321abeSMax Laier continue; 95648321abeSMax Laier 95748321abeSMax Laier /* 95848321abeSMax Laier * If we got a matching prefix address, move IFA_ROUTE and 95948321abeSMax Laier * the route itself to it. Make sure that routing daemons 96048321abeSMax Laier * get a heads-up. 96150bb1704SGleb Smirnoff * 96250bb1704SGleb Smirnoff * XXX: a special case for carp(4) interface 96348321abeSMax Laier */ 96450bb1704SGleb Smirnoff if ((ia->ia_flags & IFA_ROUTE) == 0 96550bb1704SGleb Smirnoff #ifdef DEV_CARP 96650bb1704SGleb Smirnoff && (ia->ia_ifp->if_type != IFT_CARP) 96750bb1704SGleb Smirnoff #endif 96850bb1704SGleb Smirnoff ) { 96948321abeSMax Laier rtinit(&(target->ia_ifa), (int)RTM_DELETE, 97048321abeSMax Laier rtinitflags(target)); 97148321abeSMax Laier target->ia_flags &= ~IFA_ROUTE; 97248321abeSMax Laier 97348321abeSMax Laier error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 97448321abeSMax Laier rtinitflags(ia) | RTF_UP); 97548321abeSMax Laier if (error == 0) 97648321abeSMax Laier ia->ia_flags |= IFA_ROUTE; 977460473a0SBjoern A. Zeeb return (error); 97848321abeSMax Laier } 97948321abeSMax Laier } 98048321abeSMax Laier 98148321abeSMax Laier /* 98248321abeSMax Laier * As no-one seem to have this prefix, we can remove the route. 98348321abeSMax Laier */ 98448321abeSMax Laier rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 98548321abeSMax Laier target->ia_flags &= ~IFA_ROUTE; 986460473a0SBjoern A. Zeeb return (0); 98748321abeSMax Laier } 98848321abeSMax Laier 98948321abeSMax Laier #undef rtinitflags 990df8bae1dSRodney W. Grimes 991df8bae1dSRodney W. Grimes /* 992df8bae1dSRodney W. Grimes * Return 1 if the address might be a local broadcast address. 993df8bae1dSRodney W. Grimes */ 99426f9a767SRodney W. Grimes int 995f2565d68SRobert Watson in_broadcast(struct in_addr in, struct ifnet *ifp) 996df8bae1dSRodney W. Grimes { 997df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 998df8bae1dSRodney W. Grimes u_long t; 999df8bae1dSRodney W. Grimes 1000df8bae1dSRodney W. Grimes if (in.s_addr == INADDR_BROADCAST || 1001df8bae1dSRodney W. Grimes in.s_addr == INADDR_ANY) 1002460473a0SBjoern A. Zeeb return (1); 1003df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 1004460473a0SBjoern A. Zeeb return (0); 1005df8bae1dSRodney W. Grimes t = ntohl(in.s_addr); 1006df8bae1dSRodney W. Grimes /* 1007df8bae1dSRodney W. Grimes * Look through the list of addresses for a match 1008df8bae1dSRodney W. Grimes * with a broadcast address. 1009df8bae1dSRodney W. Grimes */ 1010df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa) 1011462b86feSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1012df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family == AF_INET && 1013df8bae1dSRodney W. Grimes (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 1014df8bae1dSRodney W. Grimes in.s_addr == ia->ia_netbroadcast.s_addr || 1015df8bae1dSRodney W. Grimes /* 1016df8bae1dSRodney W. Grimes * Check for old-style (host 0) broadcast. 1017df8bae1dSRodney W. Grimes */ 10188dd27fd6SGuido van Rooij t == ia->ia_subnet || t == ia->ia_net) && 10198dd27fd6SGuido van Rooij /* 10208dd27fd6SGuido van Rooij * Check for an all one subnetmask. These 10218dd27fd6SGuido van Rooij * only exist when an interface gets a secondary 10228dd27fd6SGuido van Rooij * address. 10238dd27fd6SGuido van Rooij */ 10248dd27fd6SGuido van Rooij ia->ia_subnetmask != (u_long)0xffffffff) 1025460473a0SBjoern A. Zeeb return (1); 1026df8bae1dSRodney W. Grimes return (0); 1027df8bae1dSRodney W. Grimes #undef ia 1028df8bae1dSRodney W. Grimes } 1029ec002feeSBruce M Simpson 1030df8bae1dSRodney W. Grimes /* 1031b1c53bc9SRobert Watson * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1032b1c53bc9SRobert Watson */ 1033b1c53bc9SRobert Watson void 1034f2565d68SRobert Watson in_ifdetach(struct ifnet *ifp) 1035b1c53bc9SRobert Watson { 10368b615593SMarko Zec INIT_VNET_INET(ifp->if_vnet); 1037b1c53bc9SRobert Watson 1038603724d3SBjoern A. Zeeb in_pcbpurgeif0(&V_ripcbinfo, ifp); 1039603724d3SBjoern A. Zeeb in_pcbpurgeif0(&V_udbinfo, ifp); 1040ec002feeSBruce M Simpson in_purgemaddrs(ifp); 1041b1c53bc9SRobert Watson } 10426e6b3f7cSQing Li 1043d10910e6SBruce M Simpson /* 1044d10910e6SBruce M Simpson * Delete all IPv4 multicast address records, and associated link-layer 1045d10910e6SBruce M Simpson * multicast address records, associated with ifp. 1046d10910e6SBruce M Simpson * XXX It looks like domifdetach runs AFTER the link layer cleanup. 104756663a40SBruce M Simpson * XXX This should not race with ifma_protospec being set during 104856663a40SBruce M Simpson * a new allocation, if it does, we have bigger problems. 1049d10910e6SBruce M Simpson */ 1050d10910e6SBruce M Simpson static void 1051d10910e6SBruce M Simpson in_purgemaddrs(struct ifnet *ifp) 1052d10910e6SBruce M Simpson { 1053d10910e6SBruce M Simpson INIT_VNET_INET(ifp->if_vnet); 1054d10910e6SBruce M Simpson LIST_HEAD(,in_multi) purgeinms; 1055d10910e6SBruce M Simpson struct in_multi *inm, *tinm; 1056d10910e6SBruce M Simpson struct ifmultiaddr *ifma; 1057d10910e6SBruce M Simpson 1058d10910e6SBruce M Simpson LIST_INIT(&purgeinms); 1059d10910e6SBruce M Simpson IN_MULTI_LOCK(); 1060d10910e6SBruce M Simpson 1061d10910e6SBruce M Simpson /* 1062d10910e6SBruce M Simpson * Extract list of in_multi associated with the detaching ifp 1063d10910e6SBruce M Simpson * which the PF_INET layer is about to release. 1064d10910e6SBruce M Simpson * We need to do this as IF_ADDR_LOCK() may be re-acquired 1065d10910e6SBruce M Simpson * by code further down. 1066d10910e6SBruce M Simpson */ 1067d10910e6SBruce M Simpson IF_ADDR_LOCK(ifp); 1068d10910e6SBruce M Simpson TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 106956663a40SBruce M Simpson if (ifma->ifma_addr->sa_family != AF_INET || 107056663a40SBruce M Simpson ifma->ifma_protospec == NULL) 1071d10910e6SBruce M Simpson continue; 107256663a40SBruce M Simpson #if 0 107356663a40SBruce M Simpson KASSERT(ifma->ifma_protospec != NULL, 107456663a40SBruce M Simpson ("%s: ifma_protospec is NULL", __func__)); 107556663a40SBruce M Simpson #endif 1076d10910e6SBruce M Simpson inm = (struct in_multi *)ifma->ifma_protospec; 1077d10910e6SBruce M Simpson LIST_INSERT_HEAD(&purgeinms, inm, inm_link); 1078d10910e6SBruce M Simpson } 1079d10910e6SBruce M Simpson IF_ADDR_UNLOCK(ifp); 1080d10910e6SBruce M Simpson 1081d10910e6SBruce M Simpson LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) { 1082d10910e6SBruce M Simpson inm_release_locked(inm); 1083d10910e6SBruce M Simpson LIST_REMOVE(inm, inm_link); 1084d10910e6SBruce M Simpson } 1085d10910e6SBruce M Simpson igmp_ifdetach(ifp); 1086d10910e6SBruce M Simpson 1087d10910e6SBruce M Simpson IN_MULTI_UNLOCK(); 1088d10910e6SBruce M Simpson } 1089d10910e6SBruce M Simpson 10906e6b3f7cSQing Li #include <sys/syslog.h> 10916e6b3f7cSQing Li #include <net/if_dl.h> 10926e6b3f7cSQing Li #include <netinet/if_ether.h> 10936e6b3f7cSQing Li 10946e6b3f7cSQing Li struct in_llentry { 10956e6b3f7cSQing Li struct llentry base; 10966e6b3f7cSQing Li struct sockaddr_in l3_addr4; 10976e6b3f7cSQing Li }; 10986e6b3f7cSQing Li 10996e6b3f7cSQing Li static struct llentry * 11006e6b3f7cSQing Li in_lltable_new(const struct sockaddr *l3addr, u_int flags) 11016e6b3f7cSQing Li { 11026e6b3f7cSQing Li struct in_llentry *lle; 11036e6b3f7cSQing Li 11046e6b3f7cSQing Li lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO); 11056e6b3f7cSQing Li if (lle == NULL) /* NB: caller generates msg */ 11066e6b3f7cSQing Li return NULL; 11076e6b3f7cSQing Li 11086e6b3f7cSQing Li callout_init(&lle->base.la_timer, CALLOUT_MPSAFE); 11096e6b3f7cSQing Li /* 11106e6b3f7cSQing Li * For IPv4 this will trigger "arpresolve" to generate 11116e6b3f7cSQing Li * an ARP request. 11126e6b3f7cSQing Li */ 11136e6b3f7cSQing Li lle->base.la_expire = time_second; /* mark expired */ 11146e6b3f7cSQing Li lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 11156e6b3f7cSQing Li lle->base.lle_refcnt = 1; 11166e6b3f7cSQing Li LLE_LOCK_INIT(&lle->base); 11176e6b3f7cSQing Li return &lle->base; 11186e6b3f7cSQing Li } 11196e6b3f7cSQing Li 11206e6b3f7cSQing Li /* 11216e6b3f7cSQing Li * Deletes an address from the address table. 11226e6b3f7cSQing Li * This function is called by the timer functions 11236e6b3f7cSQing Li * such as arptimer() and nd6_llinfo_timer(), and 11246e6b3f7cSQing Li * the caller does the locking. 11256e6b3f7cSQing Li */ 11266e6b3f7cSQing Li static void 11276e6b3f7cSQing Li in_lltable_free(struct lltable *llt, struct llentry *lle) 11286e6b3f7cSQing Li { 1129fbc2ca1bSKip Macy LLE_WUNLOCK(lle); 1130fbc2ca1bSKip Macy LLE_LOCK_DESTROY(lle); 11316e6b3f7cSQing Li free(lle, M_LLTABLE); 11326e6b3f7cSQing Li } 11336e6b3f7cSQing Li 11346e6b3f7cSQing Li static int 11356e6b3f7cSQing Li in_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr) 11366e6b3f7cSQing Li { 11376e6b3f7cSQing Li struct rtentry *rt; 11386e6b3f7cSQing Li 11396e6b3f7cSQing Li KASSERT(l3addr->sa_family == AF_INET, 11406e6b3f7cSQing Li ("sin_family %d", l3addr->sa_family)); 11416e6b3f7cSQing Li 11426e6b3f7cSQing Li /* XXX rtalloc1 should take a const param */ 11436e6b3f7cSQing Li rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 11446e6b3f7cSQing Li if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 11456e6b3f7cSQing Li log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 11466e6b3f7cSQing Li inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 11476e6b3f7cSQing Li if (rt != NULL) 11486e6b3f7cSQing Li RTFREE_LOCKED(rt); 11496e6b3f7cSQing Li return (EINVAL); 11506e6b3f7cSQing Li } 11516e6b3f7cSQing Li RTFREE_LOCKED(rt); 11526e6b3f7cSQing Li return 0; 11536e6b3f7cSQing Li } 11546e6b3f7cSQing Li 11556e6b3f7cSQing Li /* 11566e6b3f7cSQing Li * Return NULL if not found or marked for deletion. 11576e6b3f7cSQing Li * If found return lle read locked. 11586e6b3f7cSQing Li */ 11596e6b3f7cSQing Li static struct llentry * 11606e6b3f7cSQing Li in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 11616e6b3f7cSQing Li { 11626e6b3f7cSQing Li const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 11636e6b3f7cSQing Li struct ifnet *ifp = llt->llt_ifp; 11646e6b3f7cSQing Li struct llentry *lle; 11656e6b3f7cSQing Li struct llentries *lleh; 11666e6b3f7cSQing Li u_int hashkey; 11676e6b3f7cSQing Li 11686e6b3f7cSQing Li IF_AFDATA_LOCK_ASSERT(ifp); 11696e6b3f7cSQing Li KASSERT(l3addr->sa_family == AF_INET, 11706e6b3f7cSQing Li ("sin_family %d", l3addr->sa_family)); 11716e6b3f7cSQing Li 11726e6b3f7cSQing Li hashkey = sin->sin_addr.s_addr; 11736e6b3f7cSQing Li lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 11746e6b3f7cSQing Li LIST_FOREACH(lle, lleh, lle_next) { 1175dc495497SQing Li struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle); 11766e6b3f7cSQing Li if (lle->la_flags & LLE_DELETED) 11776e6b3f7cSQing Li continue; 1178dc495497SQing Li if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr) 11796e6b3f7cSQing Li break; 11806e6b3f7cSQing Li } 11816e6b3f7cSQing Li if (lle == NULL) { 11826e6b3f7cSQing Li #ifdef DIAGNOSTICS 11836e6b3f7cSQing Li if (flags & LLE_DELETE) 11846e6b3f7cSQing Li log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 11856e6b3f7cSQing Li #endif 11866e6b3f7cSQing Li if (!(flags & LLE_CREATE)) 11876e6b3f7cSQing Li return (NULL); 11886e6b3f7cSQing Li /* 11896e6b3f7cSQing Li * A route that covers the given address must have 11906e6b3f7cSQing Li * been installed 1st because we are doing a resolution, 11916e6b3f7cSQing Li * verify this. 11926e6b3f7cSQing Li */ 11936e6b3f7cSQing Li if (!(flags & LLE_IFADDR) && 11946e6b3f7cSQing Li in_lltable_rtcheck(ifp, l3addr) != 0) 11956e6b3f7cSQing Li goto done; 11966e6b3f7cSQing Li 11976e6b3f7cSQing Li lle = in_lltable_new(l3addr, flags); 11986e6b3f7cSQing Li if (lle == NULL) { 11996e6b3f7cSQing Li log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 12006e6b3f7cSQing Li goto done; 12016e6b3f7cSQing Li } 12026e6b3f7cSQing Li lle->la_flags = flags & ~LLE_CREATE; 12036e6b3f7cSQing Li if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 12046e6b3f7cSQing Li bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 12056e6b3f7cSQing Li lle->la_flags |= (LLE_VALID | LLE_STATIC); 12066e6b3f7cSQing Li } 12076e6b3f7cSQing Li 12086e6b3f7cSQing Li lle->lle_tbl = llt; 12096e6b3f7cSQing Li lle->lle_head = lleh; 12106e6b3f7cSQing Li LIST_INSERT_HEAD(lleh, lle, lle_next); 12116e6b3f7cSQing Li } else if (flags & LLE_DELETE) { 12126e6b3f7cSQing Li if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 12136e6b3f7cSQing Li LLE_WLOCK(lle); 12146e6b3f7cSQing Li lle->la_flags = LLE_DELETED; 12156e6b3f7cSQing Li LLE_WUNLOCK(lle); 12166e6b3f7cSQing Li #ifdef DIAGNOSTICS 12176e6b3f7cSQing Li log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 12186e6b3f7cSQing Li #endif 12196e6b3f7cSQing Li } 12206e6b3f7cSQing Li lle = (void *)-1; 12216e6b3f7cSQing Li 12226e6b3f7cSQing Li } 122342d866ddSBjoern A. Zeeb if (LLE_IS_VALID(lle)) { 12246e6b3f7cSQing Li if (flags & LLE_EXCLUSIVE) 12256e6b3f7cSQing Li LLE_WLOCK(lle); 12266e6b3f7cSQing Li else 12276e6b3f7cSQing Li LLE_RLOCK(lle); 12286e6b3f7cSQing Li } 12296e6b3f7cSQing Li done: 12306e6b3f7cSQing Li return (lle); 12316e6b3f7cSQing Li } 12326e6b3f7cSQing Li 12336e6b3f7cSQing Li static int 12346e6b3f7cSQing Li in_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 12356e6b3f7cSQing Li { 12366e6b3f7cSQing Li #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 12376e6b3f7cSQing Li struct ifnet *ifp = llt->llt_ifp; 12386e6b3f7cSQing Li struct llentry *lle; 12396e6b3f7cSQing Li /* XXX stack use */ 12406e6b3f7cSQing Li struct { 12416e6b3f7cSQing Li struct rt_msghdr rtm; 12426e6b3f7cSQing Li struct sockaddr_inarp sin; 12436e6b3f7cSQing Li struct sockaddr_dl sdl; 12446e6b3f7cSQing Li } arpc; 12456e6b3f7cSQing Li int error, i; 12466e6b3f7cSQing Li 12476e6b3f7cSQing Li /* XXXXX 12486e6b3f7cSQing Li * current IFNET_RLOCK() is mapped to IFNET_WLOCK() 12496e6b3f7cSQing Li * so it is okay to use this ASSERT, change it when 12506e6b3f7cSQing Li * IFNET lock is finalized 12516e6b3f7cSQing Li */ 12526e6b3f7cSQing Li IFNET_WLOCK_ASSERT(); 12536e6b3f7cSQing Li 12546e6b3f7cSQing Li error = 0; 12556e6b3f7cSQing Li for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 12566e6b3f7cSQing Li LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 12576e6b3f7cSQing Li struct sockaddr_dl *sdl; 12586e6b3f7cSQing Li 12596e6b3f7cSQing Li /* skip deleted entries */ 12606e6b3f7cSQing Li if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID) 12616e6b3f7cSQing Li continue; 1262813dd6aeSBjoern A. Zeeb /* Skip if jailed and not a valid IP of the prison. */ 1263b89e82ddSJamie Gritton if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 1264813dd6aeSBjoern A. Zeeb continue; 12656e6b3f7cSQing Li /* 12666e6b3f7cSQing Li * produce a msg made of: 12676e6b3f7cSQing Li * struct rt_msghdr; 12686e6b3f7cSQing Li * struct sockaddr_inarp; (IPv4) 12696e6b3f7cSQing Li * struct sockaddr_dl; 12706e6b3f7cSQing Li */ 12716e6b3f7cSQing Li bzero(&arpc, sizeof(arpc)); 12726e6b3f7cSQing Li arpc.rtm.rtm_msglen = sizeof(arpc); 1273c0e9a8a1SHartmut Brandt arpc.rtm.rtm_version = RTM_VERSION; 1274c0e9a8a1SHartmut Brandt arpc.rtm.rtm_type = RTM_GET; 1275c0e9a8a1SHartmut Brandt arpc.rtm.rtm_flags = RTF_UP; 1276c0e9a8a1SHartmut Brandt arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 12776e6b3f7cSQing Li arpc.sin.sin_family = AF_INET; 12786e6b3f7cSQing Li arpc.sin.sin_len = sizeof(arpc.sin); 12796e6b3f7cSQing Li arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 12806e6b3f7cSQing Li 12816e6b3f7cSQing Li /* publish */ 12826e6b3f7cSQing Li if (lle->la_flags & LLE_PUB) { 12836e6b3f7cSQing Li arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 12846e6b3f7cSQing Li /* proxy only */ 12856e6b3f7cSQing Li if (lle->la_flags & LLE_PROXY) 12866e6b3f7cSQing Li arpc.sin.sin_other = SIN_PROXY; 12876e6b3f7cSQing Li } 12886e6b3f7cSQing Li 12896e6b3f7cSQing Li sdl = &arpc.sdl; 12906e6b3f7cSQing Li sdl->sdl_family = AF_LINK; 12916e6b3f7cSQing Li sdl->sdl_len = sizeof(*sdl); 12926e6b3f7cSQing Li sdl->sdl_alen = ifp->if_addrlen; 12936e6b3f7cSQing Li sdl->sdl_index = ifp->if_index; 12946e6b3f7cSQing Li sdl->sdl_type = ifp->if_type; 12956e6b3f7cSQing Li bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 12966e6b3f7cSQing Li 12976e6b3f7cSQing Li arpc.rtm.rtm_rmx.rmx_expire = 12986e6b3f7cSQing Li lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 12998eca593cSQing Li arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 13006e6b3f7cSQing Li if (lle->la_flags & LLE_STATIC) 13016e6b3f7cSQing Li arpc.rtm.rtm_flags |= RTF_STATIC; 13026e6b3f7cSQing Li arpc.rtm.rtm_index = ifp->if_index; 13036e6b3f7cSQing Li error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 13046e6b3f7cSQing Li if (error) 13056e6b3f7cSQing Li break; 13066e6b3f7cSQing Li } 13076e6b3f7cSQing Li } 13086e6b3f7cSQing Li return error; 13096e6b3f7cSQing Li #undef SIN 13106e6b3f7cSQing Li } 13116e6b3f7cSQing Li 13126e6b3f7cSQing Li void * 13136e6b3f7cSQing Li in_domifattach(struct ifnet *ifp) 13146e6b3f7cSQing Li { 1315d10910e6SBruce M Simpson struct in_ifinfo *ii; 1316d10910e6SBruce M Simpson struct lltable *llt; 13176e6b3f7cSQing Li 1318d10910e6SBruce M Simpson ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1319d10910e6SBruce M Simpson 1320d10910e6SBruce M Simpson llt = lltable_init(ifp, AF_INET); 13216e6b3f7cSQing Li if (llt != NULL) { 13226e6b3f7cSQing Li llt->llt_new = in_lltable_new; 13236e6b3f7cSQing Li llt->llt_free = in_lltable_free; 13246e6b3f7cSQing Li llt->llt_rtcheck = in_lltable_rtcheck; 13256e6b3f7cSQing Li llt->llt_lookup = in_lltable_lookup; 13266e6b3f7cSQing Li llt->llt_dump = in_lltable_dump; 13276e6b3f7cSQing Li } 1328d10910e6SBruce M Simpson ii->ii_llt = llt; 1329d10910e6SBruce M Simpson 1330d10910e6SBruce M Simpson ii->ii_igmp = igmp_domifattach(ifp); 1331d10910e6SBruce M Simpson 1332d10910e6SBruce M Simpson return ii; 13336e6b3f7cSQing Li } 13346e6b3f7cSQing Li 13356e6b3f7cSQing Li void 1336d10910e6SBruce M Simpson in_domifdetach(struct ifnet *ifp, void *aux) 13376e6b3f7cSQing Li { 1338d10910e6SBruce M Simpson struct in_ifinfo *ii = (struct in_ifinfo *)aux; 13396e6b3f7cSQing Li 1340d10910e6SBruce M Simpson igmp_domifdetach(ifp); 1341d10910e6SBruce M Simpson lltable_free(ii->ii_llt); 1342d10910e6SBruce M Simpson free(ii, M_IFADDR); 13436e6b3f7cSQing Li } 1344