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 31c3aac50fSPeter Wemm * $FreeBSD$ 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 34df8bae1dSRodney W. Grimes #include <sys/param.h> 3526f9a767SRodney W. Grimes #include <sys/systm.h> 3651a53488SBruce Evans #include <sys/sockio.h> 37df8bae1dSRodney W. Grimes #include <sys/malloc.h> 38df8bae1dSRodney W. Grimes #include <sys/socket.h> 39f6d24a78SPoul-Henning Kamp #include <sys/kernel.h> 40f6d24a78SPoul-Henning Kamp #include <sys/sysctl.h> 41df8bae1dSRodney W. Grimes 42df8bae1dSRodney W. Grimes #include <net/if.h> 436a800098SYoshinobu Inoue #include <net/if_types.h> 44df8bae1dSRodney W. Grimes #include <net/route.h> 45df8bae1dSRodney W. Grimes 46df8bae1dSRodney W. Grimes #include <netinet/in.h> 47df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 48e43cc4aeSHajimu UMEMOTO #include <netinet/in_pcb.h> 49df8bae1dSRodney W. Grimes 50c70f4510SPoul-Henning Kamp #include <netinet/igmp_var.h> 51c70f4510SPoul-Henning Kamp 52a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address"); 5355166637SPoul-Henning Kamp 544d77a549SAlfred Perlstein static int in_mask2len(struct in_addr *); 554d77a549SAlfred Perlstein static void in_len2mask(struct in_addr *, int); 564d77a549SAlfred Perlstein static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 574d77a549SAlfred Perlstein struct ifnet *, struct thread *); 586a800098SYoshinobu Inoue 5948321abeSMax Laier static int in_addprefix(struct in_ifaddr *, int); 6048321abeSMax Laier static int in_scrubprefix(struct in_ifaddr *); 614d77a549SAlfred Perlstein static void in_socktrim(struct sockaddr_in *); 624d77a549SAlfred Perlstein static int in_ifinit(struct ifnet *, 634d77a549SAlfred Perlstein struct in_ifaddr *, struct sockaddr_in *, int); 64df8bae1dSRodney W. Grimes 65f8731310SGarrett Wollman static int subnetsarelocal = 0; 66f6d24a78SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW, 673b95e134SRuslan Ermilov &subnetsarelocal, 0, "Treat all subnets as directly connected"); 68477180fbSGarrett Wollman 69477180fbSGarrett Wollman struct in_multihead in_multihead; /* XXX BSS initialization */ 70477180fbSGarrett Wollman 71e43cc4aeSHajimu UMEMOTO extern struct inpcbinfo ripcbinfo; 72e43cc4aeSHajimu UMEMOTO extern struct inpcbinfo udbinfo; 73e43cc4aeSHajimu UMEMOTO 74df8bae1dSRodney W. Grimes /* 75df8bae1dSRodney W. Grimes * Return 1 if an internet address is for a ``local'' host 76df8bae1dSRodney W. Grimes * (one to which we have a connection). If subnetsarelocal 77df8bae1dSRodney W. Grimes * is true, this includes other subnets of the local net. 78df8bae1dSRodney W. Grimes * Otherwise, it includes only the directly-connected (sub)nets. 79df8bae1dSRodney W. Grimes */ 8026f9a767SRodney W. Grimes int 81df8bae1dSRodney W. Grimes in_localaddr(in) 82df8bae1dSRodney W. Grimes struct in_addr in; 83df8bae1dSRodney W. Grimes { 84df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 85df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 86df8bae1dSRodney W. Grimes 87df8bae1dSRodney W. Grimes if (subnetsarelocal) { 88462b86feSPoul-Henning Kamp TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 89df8bae1dSRodney W. Grimes if ((i & ia->ia_netmask) == ia->ia_net) 90df8bae1dSRodney W. Grimes return (1); 91df8bae1dSRodney W. Grimes } else { 9237d40066SPoul-Henning Kamp TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 93df8bae1dSRodney W. Grimes if ((i & ia->ia_subnetmask) == ia->ia_subnet) 94df8bae1dSRodney W. Grimes return (1); 95df8bae1dSRodney W. Grimes } 96df8bae1dSRodney W. Grimes return (0); 97df8bae1dSRodney W. Grimes } 98df8bae1dSRodney W. Grimes 99df8bae1dSRodney W. Grimes /* 1002eccc90bSAndre Oppermann * Return 1 if an internet address is for the local host and configured 1012eccc90bSAndre Oppermann * on one of its interfaces. 1022eccc90bSAndre Oppermann */ 1032eccc90bSAndre Oppermann int 1042eccc90bSAndre Oppermann in_localip(in) 1052eccc90bSAndre Oppermann struct in_addr in; 1062eccc90bSAndre Oppermann { 1072eccc90bSAndre Oppermann struct in_ifaddr *ia; 1082eccc90bSAndre Oppermann 1092eccc90bSAndre Oppermann LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 1102eccc90bSAndre Oppermann if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) 1112eccc90bSAndre Oppermann return 1; 1122eccc90bSAndre Oppermann } 1132eccc90bSAndre Oppermann return 0; 1142eccc90bSAndre Oppermann } 1152eccc90bSAndre Oppermann 1162eccc90bSAndre Oppermann /* 117df8bae1dSRodney W. Grimes * Determine whether an IP address is in a reserved set of addresses 118df8bae1dSRodney W. Grimes * that may not be forwarded, or whether datagrams to that destination 119df8bae1dSRodney W. Grimes * may be forwarded. 120df8bae1dSRodney W. Grimes */ 12126f9a767SRodney W. Grimes int 122df8bae1dSRodney W. Grimes in_canforward(in) 123df8bae1dSRodney W. Grimes struct in_addr in; 124df8bae1dSRodney W. Grimes { 125df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 126df8bae1dSRodney W. Grimes register u_long net; 127df8bae1dSRodney W. Grimes 128df8bae1dSRodney W. Grimes if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) 129df8bae1dSRodney W. Grimes return (0); 130df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) { 131df8bae1dSRodney W. Grimes net = i & IN_CLASSA_NET; 132df8bae1dSRodney W. Grimes if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 133df8bae1dSRodney W. Grimes return (0); 134df8bae1dSRodney W. Grimes } 135df8bae1dSRodney W. Grimes return (1); 136df8bae1dSRodney W. Grimes } 137df8bae1dSRodney W. Grimes 138df8bae1dSRodney W. Grimes /* 139df8bae1dSRodney W. Grimes * Trim a mask in a sockaddr 140df8bae1dSRodney W. Grimes */ 1410312fbe9SPoul-Henning Kamp static void 142df8bae1dSRodney W. Grimes in_socktrim(ap) 143df8bae1dSRodney W. Grimes struct sockaddr_in *ap; 144df8bae1dSRodney W. Grimes { 145df8bae1dSRodney W. Grimes register char *cplim = (char *) &ap->sin_addr; 146df8bae1dSRodney W. Grimes register char *cp = (char *) (&ap->sin_addr + 1); 147df8bae1dSRodney W. Grimes 148df8bae1dSRodney W. Grimes ap->sin_len = 0; 149df00058dSGarrett Wollman while (--cp >= cplim) 150df8bae1dSRodney W. Grimes if (*cp) { 151df8bae1dSRodney W. Grimes (ap)->sin_len = cp - (char *) (ap) + 1; 152df8bae1dSRodney W. Grimes break; 153df8bae1dSRodney W. Grimes } 154df8bae1dSRodney W. Grimes } 155df8bae1dSRodney W. Grimes 1566a800098SYoshinobu Inoue static int 1576a800098SYoshinobu Inoue in_mask2len(mask) 1586a800098SYoshinobu Inoue struct in_addr *mask; 1596a800098SYoshinobu Inoue { 1606a800098SYoshinobu Inoue int x, y; 1616a800098SYoshinobu Inoue u_char *p; 1626a800098SYoshinobu Inoue 1636a800098SYoshinobu Inoue p = (u_char *)mask; 1646a800098SYoshinobu Inoue for (x = 0; x < sizeof(*mask); x++) { 1656a800098SYoshinobu Inoue if (p[x] != 0xff) 1666a800098SYoshinobu Inoue break; 1676a800098SYoshinobu Inoue } 1686a800098SYoshinobu Inoue y = 0; 1696a800098SYoshinobu Inoue if (x < sizeof(*mask)) { 1706a800098SYoshinobu Inoue for (y = 0; y < 8; y++) { 1716a800098SYoshinobu Inoue if ((p[x] & (0x80 >> y)) == 0) 1726a800098SYoshinobu Inoue break; 1736a800098SYoshinobu Inoue } 1746a800098SYoshinobu Inoue } 1756a800098SYoshinobu Inoue return x * 8 + y; 1766a800098SYoshinobu Inoue } 1776a800098SYoshinobu Inoue 1786a800098SYoshinobu Inoue static void 1796a800098SYoshinobu Inoue in_len2mask(mask, len) 1806a800098SYoshinobu Inoue struct in_addr *mask; 1816a800098SYoshinobu Inoue int len; 1826a800098SYoshinobu Inoue { 1836a800098SYoshinobu Inoue int i; 1846a800098SYoshinobu Inoue u_char *p; 1856a800098SYoshinobu Inoue 1866a800098SYoshinobu Inoue p = (u_char *)mask; 1876a800098SYoshinobu Inoue bzero(mask, sizeof(*mask)); 1886a800098SYoshinobu Inoue for (i = 0; i < len / 8; i++) 1896a800098SYoshinobu Inoue p[i] = 0xff; 1906a800098SYoshinobu Inoue if (len % 8) 1916a800098SYoshinobu Inoue p[i] = (0xff00 >> (len % 8)) & 0xff; 1926a800098SYoshinobu Inoue } 1936a800098SYoshinobu Inoue 194df8bae1dSRodney W. Grimes /* 195df8bae1dSRodney W. Grimes * Generic internet control operations (ioctl's). 196df8bae1dSRodney W. Grimes * Ifp is 0 if not an interface-specific ioctl. 197df8bae1dSRodney W. Grimes */ 198df8bae1dSRodney W. Grimes /* ARGSUSED */ 19926f9a767SRodney W. Grimes int 200b40ce416SJulian Elischer in_control(so, cmd, data, ifp, td) 201df8bae1dSRodney W. Grimes struct socket *so; 202ecbb00a2SDoug Rabson u_long cmd; 203df8bae1dSRodney W. Grimes caddr_t data; 204df8bae1dSRodney W. Grimes register struct ifnet *ifp; 205b40ce416SJulian Elischer struct thread *td; 206df8bae1dSRodney W. Grimes { 207df8bae1dSRodney W. Grimes register struct ifreq *ifr = (struct ifreq *)data; 208ac0aa473SBill Fenner register struct in_ifaddr *ia = 0, *iap; 209df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 210ca925d9cSJonathan Lemon struct in_addr dst; 211df8bae1dSRodney W. Grimes struct in_ifaddr *oia; 212df8bae1dSRodney W. Grimes struct in_aliasreq *ifra = (struct in_aliasreq *)data; 213df8bae1dSRodney W. Grimes struct sockaddr_in oldaddr; 2140f02fdacSBrian Somers int error, hostIsNew, iaIsNew, maskIsNew, s; 2150f02fdacSBrian Somers 2160f02fdacSBrian Somers iaIsNew = 0; 217df8bae1dSRodney W. Grimes 2186a800098SYoshinobu Inoue switch (cmd) { 2196a800098SYoshinobu Inoue case SIOCALIFADDR: 2206a800098SYoshinobu Inoue case SIOCDLIFADDR: 22144731cabSJohn Baldwin if (td && (error = suser(td)) != 0) 2226a800098SYoshinobu Inoue return error; 2236a800098SYoshinobu Inoue /*fall through*/ 2246a800098SYoshinobu Inoue case SIOCGLIFADDR: 2256a800098SYoshinobu Inoue if (!ifp) 2266a800098SYoshinobu Inoue return EINVAL; 227b40ce416SJulian Elischer return in_lifaddr_ioctl(so, cmd, data, ifp, td); 2286a800098SYoshinobu Inoue } 2296a800098SYoshinobu Inoue 230df8bae1dSRodney W. Grimes /* 231df8bae1dSRodney W. Grimes * Find address for this interface, if it exists. 232ac0aa473SBill Fenner * 233ac0aa473SBill Fenner * If an alias address was specified, find that one instead of 234ca925d9cSJonathan Lemon * the first one on the interface, if possible. 235df8bae1dSRodney W. Grimes */ 236ca925d9cSJonathan Lemon if (ifp) { 237ca925d9cSJonathan Lemon dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 238ca925d9cSJonathan Lemon LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) 239ca925d9cSJonathan Lemon if (iap->ia_ifp == ifp && 240ca925d9cSJonathan Lemon iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 241ac0aa473SBill Fenner ia = iap; 242df8bae1dSRodney W. Grimes break; 243ca925d9cSJonathan Lemon } 244ca925d9cSJonathan Lemon if (ia == NULL) 245ca925d9cSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 246ca925d9cSJonathan Lemon iap = ifatoia(ifa); 247ca925d9cSJonathan Lemon if (iap->ia_addr.sin_family == AF_INET) { 248ac0aa473SBill Fenner ia = iap; 249ac0aa473SBill Fenner break; 250ac0aa473SBill Fenner } 251ac0aa473SBill Fenner } 252ca925d9cSJonathan Lemon } 253df8bae1dSRodney W. Grimes 254df8bae1dSRodney W. Grimes switch (cmd) { 255df8bae1dSRodney W. Grimes 256df8bae1dSRodney W. Grimes case SIOCAIFADDR: 257df8bae1dSRodney W. Grimes case SIOCDIFADDR: 2586572231dSEivind Eklund if (ifp == 0) 2596572231dSEivind Eklund return (EADDRNOTAVAIL); 2601067217dSGarrett Wollman if (ifra->ifra_addr.sin_family == AF_INET) { 261fc2ffbe6SPoul-Henning Kamp for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) { 262df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp && 263df8bae1dSRodney W. Grimes ia->ia_addr.sin_addr.s_addr == 264df8bae1dSRodney W. Grimes ifra->ifra_addr.sin_addr.s_addr) 265df8bae1dSRodney W. Grimes break; 266df8bae1dSRodney W. Grimes } 2671067217dSGarrett Wollman if ((ifp->if_flags & IFF_POINTOPOINT) 2681067217dSGarrett Wollman && (cmd == SIOCAIFADDR) 2691067217dSGarrett Wollman && (ifra->ifra_dstaddr.sin_addr.s_addr 2701067217dSGarrett Wollman == INADDR_ANY)) { 271357b78a9SGarrett Wollman return EDESTADDRREQ; 2721067217dSGarrett Wollman } 2731067217dSGarrett Wollman } 274df8bae1dSRodney W. Grimes if (cmd == SIOCDIFADDR && ia == 0) 275df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 276df8bae1dSRodney W. Grimes /* FALLTHROUGH */ 277df8bae1dSRodney W. Grimes case SIOCSIFADDR: 278df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 279df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 28044731cabSJohn Baldwin if (td && (error = suser(td)) != 0) 281a29f300eSGarrett Wollman return error; 282df8bae1dSRodney W. Grimes 283df8bae1dSRodney W. Grimes if (ifp == 0) 2846572231dSEivind Eklund return (EADDRNOTAVAIL); 285df8bae1dSRodney W. Grimes if (ia == (struct in_ifaddr *)0) { 28659562606SGarrett Wollman ia = (struct in_ifaddr *) 287a163d034SWarner Losh malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO); 28859562606SGarrett Wollman if (ia == (struct in_ifaddr *)NULL) 289df8bae1dSRodney W. Grimes return (ENOBUFS); 290c655b7c4SDavid Greenman /* 291c655b7c4SDavid Greenman * Protect from ipintr() traversing address list 292c655b7c4SDavid Greenman * while we're modifying it. 293c655b7c4SDavid Greenman */ 294c655b7c4SDavid Greenman s = splnet(); 29559562606SGarrett Wollman TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link); 29659562606SGarrett Wollman 29719fc74fbSJeffrey Hsu ifa = &ia->ia_ifa; 29819fc74fbSJeffrey Hsu IFA_LOCK_INIT(ifa); 29959562606SGarrett Wollman ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 30059562606SGarrett Wollman ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 30159562606SGarrett Wollman ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 30219fc74fbSJeffrey Hsu ifa->ifa_refcnt = 1; 30319fc74fbSJeffrey Hsu TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 30419fc74fbSJeffrey Hsu 305df8bae1dSRodney W. Grimes ia->ia_sockmask.sin_len = 8; 306bc183b3fSDag-Erling Smørgrav ia->ia_sockmask.sin_family = AF_INET; 307df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 308df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 309df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_family = AF_INET; 310df8bae1dSRodney W. Grimes } 311df8bae1dSRodney W. Grimes ia->ia_ifp = ifp; 312c655b7c4SDavid Greenman splx(s); 3130f02fdacSBrian Somers iaIsNew = 1; 314df8bae1dSRodney W. Grimes } 315df8bae1dSRodney W. Grimes break; 316df8bae1dSRodney W. Grimes 317df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 31844731cabSJohn Baldwin if (td && (error = suser(td)) != 0) 319a29f300eSGarrett Wollman return error; 320df8bae1dSRodney W. Grimes /* FALLTHROUGH */ 321df8bae1dSRodney W. Grimes 322df8bae1dSRodney W. Grimes case SIOCGIFADDR: 323df8bae1dSRodney W. Grimes case SIOCGIFNETMASK: 324df8bae1dSRodney W. Grimes case SIOCGIFDSTADDR: 325df8bae1dSRodney W. Grimes case SIOCGIFBRDADDR: 326df8bae1dSRodney W. Grimes if (ia == (struct in_ifaddr *)0) 327df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 328df8bae1dSRodney W. Grimes break; 329df8bae1dSRodney W. Grimes } 330df8bae1dSRodney W. Grimes switch (cmd) { 331df8bae1dSRodney W. Grimes 332df8bae1dSRodney W. Grimes case SIOCGIFADDR: 333df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 3340f02fdacSBrian Somers return (0); 335df8bae1dSRodney W. Grimes 336df8bae1dSRodney W. Grimes case SIOCGIFBRDADDR: 337df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 338df8bae1dSRodney W. Grimes return (EINVAL); 339df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 3400f02fdacSBrian Somers return (0); 341df8bae1dSRodney W. Grimes 342df8bae1dSRodney W. Grimes case SIOCGIFDSTADDR: 343df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 344df8bae1dSRodney W. Grimes return (EINVAL); 345df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 3460f02fdacSBrian Somers return (0); 347df8bae1dSRodney W. Grimes 348df8bae1dSRodney W. Grimes case SIOCGIFNETMASK: 349df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 3500f02fdacSBrian Somers return (0); 351df8bae1dSRodney W. Grimes 352df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 353df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 354df8bae1dSRodney W. Grimes return (EINVAL); 355df8bae1dSRodney W. Grimes oldaddr = ia->ia_dstaddr; 356df8bae1dSRodney W. Grimes ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 357df8bae1dSRodney W. Grimes if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 358df8bae1dSRodney W. Grimes (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 359df8bae1dSRodney W. Grimes ia->ia_dstaddr = oldaddr; 360df8bae1dSRodney W. Grimes return (error); 361df8bae1dSRodney W. Grimes } 362df8bae1dSRodney W. Grimes if (ia->ia_flags & IFA_ROUTE) { 363df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 364df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 365df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr = 366df8bae1dSRodney W. Grimes (struct sockaddr *)&ia->ia_dstaddr; 367df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 368df8bae1dSRodney W. Grimes } 3690f02fdacSBrian Somers return (0); 370df8bae1dSRodney W. Grimes 371df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 372df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 373df8bae1dSRodney W. Grimes return (EINVAL); 374df8bae1dSRodney W. Grimes ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 3750f02fdacSBrian Somers return (0); 376df8bae1dSRodney W. Grimes 377df8bae1dSRodney W. Grimes case SIOCSIFADDR: 3780f02fdacSBrian Somers error = in_ifinit(ifp, ia, 3790f02fdacSBrian Somers (struct sockaddr_in *) &ifr->ifr_addr, 1); 3800f02fdacSBrian Somers if (error != 0 && iaIsNew) 3810f02fdacSBrian Somers break; 38225a4adceSMax Laier if (error == 0) 38325a4adceSMax Laier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 3840f02fdacSBrian Somers return (0); 385df8bae1dSRodney W. Grimes 386df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 387bc183b3fSDag-Erling Smørgrav ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; 388bc183b3fSDag-Erling Smørgrav ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 3890f02fdacSBrian Somers return (0); 390df8bae1dSRodney W. Grimes 391df8bae1dSRodney W. Grimes case SIOCAIFADDR: 392df8bae1dSRodney W. Grimes maskIsNew = 0; 393df8bae1dSRodney W. Grimes hostIsNew = 1; 394df8bae1dSRodney W. Grimes error = 0; 395df8bae1dSRodney W. Grimes if (ia->ia_addr.sin_family == AF_INET) { 396df8bae1dSRodney W. Grimes if (ifra->ifra_addr.sin_len == 0) { 397df8bae1dSRodney W. Grimes ifra->ifra_addr = ia->ia_addr; 398df8bae1dSRodney W. Grimes hostIsNew = 0; 399df8bae1dSRodney W. Grimes } else if (ifra->ifra_addr.sin_addr.s_addr == 400df8bae1dSRodney W. Grimes ia->ia_addr.sin_addr.s_addr) 401df8bae1dSRodney W. Grimes hostIsNew = 0; 402df8bae1dSRodney W. Grimes } 403df8bae1dSRodney W. Grimes if (ifra->ifra_mask.sin_len) { 404df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 405df8bae1dSRodney W. Grimes ia->ia_sockmask = ifra->ifra_mask; 406bc183b3fSDag-Erling Smørgrav ia->ia_sockmask.sin_family = AF_INET; 407df8bae1dSRodney W. Grimes ia->ia_subnetmask = 408df8bae1dSRodney W. Grimes ntohl(ia->ia_sockmask.sin_addr.s_addr); 409df8bae1dSRodney W. Grimes maskIsNew = 1; 410df8bae1dSRodney W. Grimes } 411df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) && 412df8bae1dSRodney W. Grimes (ifra->ifra_dstaddr.sin_family == AF_INET)) { 413df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 414df8bae1dSRodney W. Grimes ia->ia_dstaddr = ifra->ifra_dstaddr; 415df8bae1dSRodney W. Grimes maskIsNew = 1; /* We lie; but the effect's the same */ 416df8bae1dSRodney W. Grimes } 417df8bae1dSRodney W. Grimes if (ifra->ifra_addr.sin_family == AF_INET && 418df8bae1dSRodney W. Grimes (hostIsNew || maskIsNew)) 419df8bae1dSRodney W. Grimes error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 4200f02fdacSBrian Somers if (error != 0 && iaIsNew) 4210f02fdacSBrian Somers break; 4220f02fdacSBrian Somers 423df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) && 424df8bae1dSRodney W. Grimes (ifra->ifra_broadaddr.sin_family == AF_INET)) 425df8bae1dSRodney W. Grimes ia->ia_broadaddr = ifra->ifra_broadaddr; 42625a4adceSMax Laier if (error == 0) 42725a4adceSMax Laier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 428df8bae1dSRodney W. Grimes return (error); 429df8bae1dSRodney W. Grimes 430df8bae1dSRodney W. Grimes case SIOCDIFADDR: 431089cdfadSRuslan Ermilov /* 432089cdfadSRuslan Ermilov * in_ifscrub kills the interface route. 433089cdfadSRuslan Ermilov */ 434df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 435c655b7c4SDavid Greenman /* 436089cdfadSRuslan Ermilov * in_ifadown gets rid of all the rest of 437089cdfadSRuslan Ermilov * the routes. This is not quite the right 438089cdfadSRuslan Ermilov * thing to do, but at least if we are running 439089cdfadSRuslan Ermilov * a routing process they will come back. 440089cdfadSRuslan Ermilov */ 44191854268SRuslan Ermilov in_ifadown(&ia->ia_ifa, 1); 442e43cc4aeSHajimu UMEMOTO /* 443e43cc4aeSHajimu UMEMOTO * XXX horrible hack to detect that we are being called 444e43cc4aeSHajimu UMEMOTO * from if_detach() 445e43cc4aeSHajimu UMEMOTO */ 4469ce78778SSam Leffler if (ifaddr_byindex(ifp->if_index) == NULL) { 447f76fcf6dSJeffrey Hsu in_pcbpurgeif0(&ripcbinfo, ifp); 448f76fcf6dSJeffrey Hsu in_pcbpurgeif0(&udbinfo, ifp); 449e43cc4aeSHajimu UMEMOTO } 45025a4adceSMax Laier EVENTHANDLER_INVOKE(ifaddr_event, ifp); 4510f02fdacSBrian Somers error = 0; 452df8bae1dSRodney W. Grimes break; 453df8bae1dSRodney W. Grimes 454df8bae1dSRodney W. Grimes default: 455df8bae1dSRodney W. Grimes if (ifp == 0 || ifp->if_ioctl == 0) 456df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 457df8bae1dSRodney W. Grimes return ((*ifp->if_ioctl)(ifp, cmd, data)); 458df8bae1dSRodney W. Grimes } 4590f02fdacSBrian Somers 4600f02fdacSBrian Somers /* 4610f02fdacSBrian Somers * Protect from ipintr() traversing address list while we're modifying 4620f02fdacSBrian Somers * it. 4630f02fdacSBrian Somers */ 4640f02fdacSBrian Somers s = splnet(); 4650f02fdacSBrian Somers TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 4660f02fdacSBrian Somers TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link); 4670f02fdacSBrian Somers LIST_REMOVE(ia, ia_hash); 4680f02fdacSBrian Somers IFAFREE(&ia->ia_ifa); 4690f02fdacSBrian Somers splx(s); 4700f02fdacSBrian Somers 4710f02fdacSBrian Somers return (error); 472df8bae1dSRodney W. Grimes } 473df8bae1dSRodney W. Grimes 474df8bae1dSRodney W. Grimes /* 4756a800098SYoshinobu Inoue * SIOC[GAD]LIFADDR. 4766a800098SYoshinobu Inoue * SIOCGLIFADDR: get first address. (?!?) 4776a800098SYoshinobu Inoue * SIOCGLIFADDR with IFLR_PREFIX: 4786a800098SYoshinobu Inoue * get first address that matches the specified prefix. 4796a800098SYoshinobu Inoue * SIOCALIFADDR: add the specified address. 4806a800098SYoshinobu Inoue * SIOCALIFADDR with IFLR_PREFIX: 4816a800098SYoshinobu Inoue * EINVAL since we can't deduce hostid part of the address. 4826a800098SYoshinobu Inoue * SIOCDLIFADDR: delete the specified address. 4836a800098SYoshinobu Inoue * SIOCDLIFADDR with IFLR_PREFIX: 4846a800098SYoshinobu Inoue * delete the first address that matches the specified prefix. 4856a800098SYoshinobu Inoue * return values: 4866a800098SYoshinobu Inoue * EINVAL on invalid parameters 4876a800098SYoshinobu Inoue * EADDRNOTAVAIL on prefix match failed/specified address not found 4886a800098SYoshinobu Inoue * other values may be returned from in_ioctl() 4896a800098SYoshinobu Inoue */ 4906a800098SYoshinobu Inoue static int 491b40ce416SJulian Elischer in_lifaddr_ioctl(so, cmd, data, ifp, td) 4926a800098SYoshinobu Inoue struct socket *so; 4936a800098SYoshinobu Inoue u_long cmd; 4946a800098SYoshinobu Inoue caddr_t data; 4956a800098SYoshinobu Inoue struct ifnet *ifp; 496b40ce416SJulian Elischer struct thread *td; 4976a800098SYoshinobu Inoue { 4986a800098SYoshinobu Inoue struct if_laddrreq *iflr = (struct if_laddrreq *)data; 4996a800098SYoshinobu Inoue struct ifaddr *ifa; 5006a800098SYoshinobu Inoue 5016a800098SYoshinobu Inoue /* sanity checks */ 5026a800098SYoshinobu Inoue if (!data || !ifp) { 5036a800098SYoshinobu Inoue panic("invalid argument to in_lifaddr_ioctl"); 5046a800098SYoshinobu Inoue /*NOTRECHED*/ 5056a800098SYoshinobu Inoue } 5066a800098SYoshinobu Inoue 5076a800098SYoshinobu Inoue switch (cmd) { 5086a800098SYoshinobu Inoue case SIOCGLIFADDR: 5096a800098SYoshinobu Inoue /* address must be specified on GET with IFLR_PREFIX */ 5106a800098SYoshinobu Inoue if ((iflr->flags & IFLR_PREFIX) == 0) 5116a800098SYoshinobu Inoue break; 5126a800098SYoshinobu Inoue /*FALLTHROUGH*/ 5136a800098SYoshinobu Inoue case SIOCALIFADDR: 5146a800098SYoshinobu Inoue case SIOCDLIFADDR: 5156a800098SYoshinobu Inoue /* address must be specified on ADD and DELETE */ 5165d60ed0eSYoshinobu Inoue if (iflr->addr.ss_family != AF_INET) 5176a800098SYoshinobu Inoue return EINVAL; 5185d60ed0eSYoshinobu Inoue if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 5196a800098SYoshinobu Inoue return EINVAL; 5206a800098SYoshinobu Inoue /* XXX need improvement */ 5215d60ed0eSYoshinobu Inoue if (iflr->dstaddr.ss_family 5225d60ed0eSYoshinobu Inoue && iflr->dstaddr.ss_family != AF_INET) 5236a800098SYoshinobu Inoue return EINVAL; 5245d60ed0eSYoshinobu Inoue if (iflr->dstaddr.ss_family 5255d60ed0eSYoshinobu Inoue && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 5266a800098SYoshinobu Inoue return EINVAL; 5276a800098SYoshinobu Inoue break; 5286a800098SYoshinobu Inoue default: /*shouldn't happen*/ 5296a800098SYoshinobu Inoue return EOPNOTSUPP; 5306a800098SYoshinobu Inoue } 5316a800098SYoshinobu Inoue if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 5326a800098SYoshinobu Inoue return EINVAL; 5336a800098SYoshinobu Inoue 5346a800098SYoshinobu Inoue switch (cmd) { 5356a800098SYoshinobu Inoue case SIOCALIFADDR: 5366a800098SYoshinobu Inoue { 5376a800098SYoshinobu Inoue struct in_aliasreq ifra; 5386a800098SYoshinobu Inoue 5396a800098SYoshinobu Inoue if (iflr->flags & IFLR_PREFIX) 5406a800098SYoshinobu Inoue return EINVAL; 5416a800098SYoshinobu Inoue 5426a800098SYoshinobu Inoue /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 5436a800098SYoshinobu Inoue bzero(&ifra, sizeof(ifra)); 5446a800098SYoshinobu Inoue bcopy(iflr->iflr_name, ifra.ifra_name, 5456a800098SYoshinobu Inoue sizeof(ifra.ifra_name)); 5466a800098SYoshinobu Inoue 5475d60ed0eSYoshinobu Inoue bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 5486a800098SYoshinobu Inoue 5495d60ed0eSYoshinobu Inoue if (iflr->dstaddr.ss_family) { /*XXX*/ 5506a800098SYoshinobu Inoue bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 5515d60ed0eSYoshinobu Inoue iflr->dstaddr.ss_len); 5526a800098SYoshinobu Inoue } 5536a800098SYoshinobu Inoue 5546a800098SYoshinobu Inoue ifra.ifra_mask.sin_family = AF_INET; 5556a800098SYoshinobu Inoue ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 5566a800098SYoshinobu Inoue in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 5576a800098SYoshinobu Inoue 558b40ce416SJulian Elischer return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td); 5596a800098SYoshinobu Inoue } 5606a800098SYoshinobu Inoue case SIOCGLIFADDR: 5616a800098SYoshinobu Inoue case SIOCDLIFADDR: 5626a800098SYoshinobu Inoue { 5636a800098SYoshinobu Inoue struct in_ifaddr *ia; 5646a800098SYoshinobu Inoue struct in_addr mask, candidate, match; 5656a800098SYoshinobu Inoue struct sockaddr_in *sin; 5666a800098SYoshinobu Inoue int cmp; 5676a800098SYoshinobu Inoue 5686a800098SYoshinobu Inoue bzero(&mask, sizeof(mask)); 5696a800098SYoshinobu Inoue if (iflr->flags & IFLR_PREFIX) { 5706a800098SYoshinobu Inoue /* lookup a prefix rather than address. */ 5716a800098SYoshinobu Inoue in_len2mask(&mask, iflr->prefixlen); 5726a800098SYoshinobu Inoue 5736a800098SYoshinobu Inoue sin = (struct sockaddr_in *)&iflr->addr; 5746a800098SYoshinobu Inoue match.s_addr = sin->sin_addr.s_addr; 5756a800098SYoshinobu Inoue match.s_addr &= mask.s_addr; 5766a800098SYoshinobu Inoue 5776a800098SYoshinobu Inoue /* if you set extra bits, that's wrong */ 5786a800098SYoshinobu Inoue if (match.s_addr != sin->sin_addr.s_addr) 5796a800098SYoshinobu Inoue return EINVAL; 5806a800098SYoshinobu Inoue 5816a800098SYoshinobu Inoue cmp = 1; 5826a800098SYoshinobu Inoue } else { 5836a800098SYoshinobu Inoue if (cmd == SIOCGLIFADDR) { 5846a800098SYoshinobu Inoue /* on getting an address, take the 1st match */ 5856a800098SYoshinobu Inoue cmp = 0; /*XXX*/ 5866a800098SYoshinobu Inoue } else { 5876a800098SYoshinobu Inoue /* on deleting an address, do exact match */ 5886a800098SYoshinobu Inoue in_len2mask(&mask, 32); 5896a800098SYoshinobu Inoue sin = (struct sockaddr_in *)&iflr->addr; 5906a800098SYoshinobu Inoue match.s_addr = sin->sin_addr.s_addr; 5916a800098SYoshinobu Inoue 5926a800098SYoshinobu Inoue cmp = 1; 5936a800098SYoshinobu Inoue } 5946a800098SYoshinobu Inoue } 5956a800098SYoshinobu Inoue 5966a800098SYoshinobu Inoue TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 5976a800098SYoshinobu Inoue if (ifa->ifa_addr->sa_family != AF_INET6) 5986a800098SYoshinobu Inoue continue; 5996a800098SYoshinobu Inoue if (!cmp) 6006a800098SYoshinobu Inoue break; 6016a800098SYoshinobu Inoue candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 6026a800098SYoshinobu Inoue candidate.s_addr &= mask.s_addr; 6036a800098SYoshinobu Inoue if (candidate.s_addr == match.s_addr) 6046a800098SYoshinobu Inoue break; 6056a800098SYoshinobu Inoue } 6066a800098SYoshinobu Inoue if (!ifa) 6076a800098SYoshinobu Inoue return EADDRNOTAVAIL; 6086a800098SYoshinobu Inoue ia = (struct in_ifaddr *)ifa; 6096a800098SYoshinobu Inoue 6106a800098SYoshinobu Inoue if (cmd == SIOCGLIFADDR) { 6116a800098SYoshinobu Inoue /* fill in the if_laddrreq structure */ 6126a800098SYoshinobu Inoue bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 6136a800098SYoshinobu Inoue 6146a800098SYoshinobu Inoue if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 6156a800098SYoshinobu Inoue bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 6166a800098SYoshinobu Inoue ia->ia_dstaddr.sin_len); 6176a800098SYoshinobu Inoue } else 6186a800098SYoshinobu Inoue bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 6196a800098SYoshinobu Inoue 6206a800098SYoshinobu Inoue iflr->prefixlen = 6216a800098SYoshinobu Inoue in_mask2len(&ia->ia_sockmask.sin_addr); 6226a800098SYoshinobu Inoue 6236a800098SYoshinobu Inoue iflr->flags = 0; /*XXX*/ 6246a800098SYoshinobu Inoue 6256a800098SYoshinobu Inoue return 0; 6266a800098SYoshinobu Inoue } else { 6276a800098SYoshinobu Inoue struct in_aliasreq ifra; 6286a800098SYoshinobu Inoue 6296a800098SYoshinobu Inoue /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 6306a800098SYoshinobu Inoue bzero(&ifra, sizeof(ifra)); 6316a800098SYoshinobu Inoue bcopy(iflr->iflr_name, ifra.ifra_name, 6326a800098SYoshinobu Inoue sizeof(ifra.ifra_name)); 6336a800098SYoshinobu Inoue 6346a800098SYoshinobu Inoue bcopy(&ia->ia_addr, &ifra.ifra_addr, 6356a800098SYoshinobu Inoue ia->ia_addr.sin_len); 6366a800098SYoshinobu Inoue if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 6376a800098SYoshinobu Inoue bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 6386a800098SYoshinobu Inoue ia->ia_dstaddr.sin_len); 6396a800098SYoshinobu Inoue } 6406a800098SYoshinobu Inoue bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 6416a800098SYoshinobu Inoue ia->ia_sockmask.sin_len); 6426a800098SYoshinobu Inoue 6436a800098SYoshinobu Inoue return in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 644b40ce416SJulian Elischer ifp, td); 6456a800098SYoshinobu Inoue } 6466a800098SYoshinobu Inoue } 6476a800098SYoshinobu Inoue } 6486a800098SYoshinobu Inoue 6496a800098SYoshinobu Inoue return EOPNOTSUPP; /*just for safety*/ 6506a800098SYoshinobu Inoue } 6516a800098SYoshinobu Inoue 6526a800098SYoshinobu Inoue /* 653df8bae1dSRodney W. Grimes * Delete any existing route for an interface. 654df8bae1dSRodney W. Grimes */ 65539191c8eSGarrett Wollman void 656df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia) 657df8bae1dSRodney W. Grimes register struct ifnet *ifp; 658df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 659df8bae1dSRodney W. Grimes { 66048321abeSMax Laier in_scrubprefix(ia); 661df8bae1dSRodney W. Grimes } 662df8bae1dSRodney W. Grimes 663df8bae1dSRodney W. Grimes /* 664df8bae1dSRodney W. Grimes * Initialize an interface's internet address 665df8bae1dSRodney W. Grimes * and routing table entry. 666df8bae1dSRodney W. Grimes */ 6670312fbe9SPoul-Henning Kamp static int 668df8bae1dSRodney W. Grimes in_ifinit(ifp, ia, sin, scrub) 669df8bae1dSRodney W. Grimes register struct ifnet *ifp; 670df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 671df8bae1dSRodney W. Grimes struct sockaddr_in *sin; 672df8bae1dSRodney W. Grimes int scrub; 673df8bae1dSRodney W. Grimes { 674df8bae1dSRodney W. Grimes register u_long i = ntohl(sin->sin_addr.s_addr); 675df8bae1dSRodney W. Grimes struct sockaddr_in oldaddr; 6765a43847dSBrian Somers int s = splimp(), flags = RTF_UP, error = 0; 677df8bae1dSRodney W. Grimes 678df8bae1dSRodney W. Grimes oldaddr = ia->ia_addr; 6792754d95dSSUZUKI Shinsuke if (oldaddr.sin_family == AF_INET) 6802754d95dSSUZUKI Shinsuke LIST_REMOVE(ia, ia_hash); 681df8bae1dSRodney W. Grimes ia->ia_addr = *sin; 6822754d95dSSUZUKI Shinsuke if (ia->ia_addr.sin_family == AF_INET) 6832754d95dSSUZUKI Shinsuke LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 6842754d95dSSUZUKI Shinsuke ia, ia_hash); 685df8bae1dSRodney W. Grimes /* 686df8bae1dSRodney W. Grimes * Give the interface a chance to initialize 687df8bae1dSRodney W. Grimes * if this is its first address, 688df8bae1dSRodney W. Grimes * and to validate the address if necessary. 689df8bae1dSRodney W. Grimes */ 690df8bae1dSRodney W. Grimes if (ifp->if_ioctl && 691df8bae1dSRodney W. Grimes (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 692df8bae1dSRodney W. Grimes splx(s); 6932754d95dSSUZUKI Shinsuke /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 694df8bae1dSRodney W. Grimes ia->ia_addr = oldaddr; 69522c819a7SJonathan Lemon if (ia->ia_addr.sin_family == AF_INET) 69622c819a7SJonathan Lemon LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 69722c819a7SJonathan Lemon ia, ia_hash); 6982754d95dSSUZUKI Shinsuke return (error); 6992754d95dSSUZUKI Shinsuke } 700df8bae1dSRodney W. Grimes splx(s); 701df8bae1dSRodney W. Grimes if (scrub) { 702df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 703df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 704df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 705df8bae1dSRodney W. Grimes } 706df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) 707df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSA_NET; 708df8bae1dSRodney W. Grimes else if (IN_CLASSB(i)) 709df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSB_NET; 710df8bae1dSRodney W. Grimes else 711df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSC_NET; 712df8bae1dSRodney W. Grimes /* 713df8bae1dSRodney W. Grimes * The subnet mask usually includes at least the standard network part, 714df8bae1dSRodney W. Grimes * but may may be smaller in the case of supernetting. 715df8bae1dSRodney W. Grimes * If it is set, we believe it. 716df8bae1dSRodney W. Grimes */ 717df8bae1dSRodney W. Grimes if (ia->ia_subnetmask == 0) { 718df8bae1dSRodney W. Grimes ia->ia_subnetmask = ia->ia_netmask; 719df8bae1dSRodney W. Grimes ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 720df8bae1dSRodney W. Grimes } else 721df8bae1dSRodney W. Grimes ia->ia_netmask &= ia->ia_subnetmask; 722df8bae1dSRodney W. Grimes ia->ia_net = i & ia->ia_netmask; 723df8bae1dSRodney W. Grimes ia->ia_subnet = i & ia->ia_subnetmask; 724df8bae1dSRodney W. Grimes in_socktrim(&ia->ia_sockmask); 725df8bae1dSRodney W. Grimes /* 726df8bae1dSRodney W. Grimes * Add route for the network. 727df8bae1dSRodney W. Grimes */ 728df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_metric = ifp->if_metric; 729df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 730df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_addr.s_addr = 731df8bae1dSRodney W. Grimes htonl(ia->ia_subnet | ~ia->ia_subnetmask); 732df8bae1dSRodney W. Grimes ia->ia_netbroadcast.s_addr = 733df8bae1dSRodney W. Grimes htonl(ia->ia_net | ~ ia->ia_netmask); 734df8bae1dSRodney W. Grimes } else if (ifp->if_flags & IFF_LOOPBACK) { 7359a6a6eebSMax Laier ia->ia_dstaddr = ia->ia_addr; 736df8bae1dSRodney W. Grimes flags |= RTF_HOST; 737df8bae1dSRodney W. Grimes } else if (ifp->if_flags & IFF_POINTOPOINT) { 738df8bae1dSRodney W. Grimes if (ia->ia_dstaddr.sin_family != AF_INET) 739df8bae1dSRodney W. Grimes return (0); 740df8bae1dSRodney W. Grimes flags |= RTF_HOST; 741df8bae1dSRodney W. Grimes } 74248321abeSMax Laier if ((error = in_addprefix(ia, flags)) != 0) 7430f02fdacSBrian Somers return (error); 7440f02fdacSBrian Somers 745df8bae1dSRodney W. Grimes /* 746df8bae1dSRodney W. Grimes * If the interface supports multicast, join the "all hosts" 747df8bae1dSRodney W. Grimes * multicast group on that interface. 748df8bae1dSRodney W. Grimes */ 749df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_MULTICAST) { 750df8bae1dSRodney W. Grimes struct in_addr addr; 751df8bae1dSRodney W. Grimes 752df8bae1dSRodney W. Grimes addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 753df8bae1dSRodney W. Grimes in_addmulti(&addr, ifp); 754df8bae1dSRodney W. Grimes } 755df8bae1dSRodney W. Grimes return (error); 756df8bae1dSRodney W. Grimes } 757df8bae1dSRodney W. Grimes 75848321abeSMax Laier #define rtinitflags(x) \ 75948321abeSMax Laier ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 76048321abeSMax Laier ? RTF_HOST : 0) 76148321abeSMax Laier /* 76248321abeSMax Laier * Check if we have a route for the given prefix already or add a one 76348321abeSMax Laier * accordingly. 76448321abeSMax Laier */ 76548321abeSMax Laier static int 76648321abeSMax Laier in_addprefix(target, flags) 76748321abeSMax Laier struct in_ifaddr *target; 76848321abeSMax Laier int flags; 76948321abeSMax Laier { 77048321abeSMax Laier struct in_ifaddr *ia; 77148321abeSMax Laier struct in_addr prefix, mask, p; 77248321abeSMax Laier int error; 77348321abeSMax Laier 77448321abeSMax Laier if ((flags & RTF_HOST) != 0) 77548321abeSMax Laier prefix = target->ia_dstaddr.sin_addr; 77648321abeSMax Laier else { 77748321abeSMax Laier prefix = target->ia_addr.sin_addr; 77848321abeSMax Laier mask = target->ia_sockmask.sin_addr; 77948321abeSMax Laier prefix.s_addr &= mask.s_addr; 78048321abeSMax Laier } 78148321abeSMax Laier 78248321abeSMax Laier TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 78348321abeSMax Laier if (rtinitflags(ia)) 78448321abeSMax Laier p = ia->ia_dstaddr.sin_addr; 78548321abeSMax Laier else { 78648321abeSMax Laier p = ia->ia_addr.sin_addr; 78748321abeSMax Laier p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 78848321abeSMax Laier } 78948321abeSMax Laier 79048321abeSMax Laier if (prefix.s_addr != p.s_addr) 79148321abeSMax Laier continue; 79248321abeSMax Laier 79348321abeSMax Laier /* 79448321abeSMax Laier * If we got a matching prefix route inserted by other 79548321abeSMax Laier * interface address, we are done here. 79648321abeSMax Laier */ 79748321abeSMax Laier if (ia->ia_flags & IFA_ROUTE) 79848321abeSMax Laier return 0; 79948321abeSMax Laier } 80048321abeSMax Laier 80148321abeSMax Laier /* 80248321abeSMax Laier * No-one seem to have this prefix route, so we try to insert it. 80348321abeSMax Laier */ 80448321abeSMax Laier error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 80548321abeSMax Laier if (!error) 80648321abeSMax Laier target->ia_flags |= IFA_ROUTE; 80748321abeSMax Laier return error; 80848321abeSMax Laier } 80948321abeSMax Laier 81048321abeSMax Laier /* 81148321abeSMax Laier * If there is no other address in the system that can serve a route to the 81248321abeSMax Laier * same prefix, remove the route. Hand over the route to the new address 81348321abeSMax Laier * otherwise. 81448321abeSMax Laier */ 81548321abeSMax Laier static int 81648321abeSMax Laier in_scrubprefix(target) 81748321abeSMax Laier struct in_ifaddr *target; 81848321abeSMax Laier { 81948321abeSMax Laier struct in_ifaddr *ia; 82048321abeSMax Laier struct in_addr prefix, mask, p; 82148321abeSMax Laier int error; 82248321abeSMax Laier 82348321abeSMax Laier if ((target->ia_flags & IFA_ROUTE) == 0) 82448321abeSMax Laier return 0; 82548321abeSMax Laier 82648321abeSMax Laier if (rtinitflags(target)) 82748321abeSMax Laier prefix = target->ia_dstaddr.sin_addr; 82848321abeSMax Laier else { 82948321abeSMax Laier prefix = target->ia_addr.sin_addr; 83048321abeSMax Laier mask = target->ia_sockmask.sin_addr; 83148321abeSMax Laier prefix.s_addr &= mask.s_addr; 83248321abeSMax Laier } 83348321abeSMax Laier 83448321abeSMax Laier TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 83548321abeSMax Laier if (rtinitflags(ia)) 83648321abeSMax Laier p = ia->ia_dstaddr.sin_addr; 83748321abeSMax Laier else { 83848321abeSMax Laier p = ia->ia_addr.sin_addr; 83948321abeSMax Laier p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 84048321abeSMax Laier } 84148321abeSMax Laier 84248321abeSMax Laier if (prefix.s_addr != p.s_addr) 84348321abeSMax Laier continue; 84448321abeSMax Laier 84548321abeSMax Laier /* 84648321abeSMax Laier * If we got a matching prefix address, move IFA_ROUTE and 84748321abeSMax Laier * the route itself to it. Make sure that routing daemons 84848321abeSMax Laier * get a heads-up. 84948321abeSMax Laier */ 85048321abeSMax Laier if ((ia->ia_flags & IFA_ROUTE) == 0) { 85148321abeSMax Laier rtinit(&(target->ia_ifa), (int)RTM_DELETE, 85248321abeSMax Laier rtinitflags(target)); 85348321abeSMax Laier target->ia_flags &= ~IFA_ROUTE; 85448321abeSMax Laier 85548321abeSMax Laier error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 85648321abeSMax Laier rtinitflags(ia) | RTF_UP); 85748321abeSMax Laier if (error == 0) 85848321abeSMax Laier ia->ia_flags |= IFA_ROUTE; 85948321abeSMax Laier return error; 86048321abeSMax Laier } 86148321abeSMax Laier } 86248321abeSMax Laier 86348321abeSMax Laier /* 86448321abeSMax Laier * As no-one seem to have this prefix, we can remove the route. 86548321abeSMax Laier */ 86648321abeSMax Laier rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 86748321abeSMax Laier target->ia_flags &= ~IFA_ROUTE; 86848321abeSMax Laier return 0; 86948321abeSMax Laier } 87048321abeSMax Laier 87148321abeSMax Laier #undef rtinitflags 872df8bae1dSRodney W. Grimes 873df8bae1dSRodney W. Grimes /* 874df8bae1dSRodney W. Grimes * Return 1 if the address might be a local broadcast address. 875df8bae1dSRodney W. Grimes */ 87626f9a767SRodney W. Grimes int 877df8bae1dSRodney W. Grimes in_broadcast(in, ifp) 878df8bae1dSRodney W. Grimes struct in_addr in; 879df8bae1dSRodney W. Grimes struct ifnet *ifp; 880df8bae1dSRodney W. Grimes { 881df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 882df8bae1dSRodney W. Grimes u_long t; 883df8bae1dSRodney W. Grimes 884df8bae1dSRodney W. Grimes if (in.s_addr == INADDR_BROADCAST || 885df8bae1dSRodney W. Grimes in.s_addr == INADDR_ANY) 886df8bae1dSRodney W. Grimes return 1; 887df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 888df8bae1dSRodney W. Grimes return 0; 889df8bae1dSRodney W. Grimes t = ntohl(in.s_addr); 890df8bae1dSRodney W. Grimes /* 891df8bae1dSRodney W. Grimes * Look through the list of addresses for a match 892df8bae1dSRodney W. Grimes * with a broadcast address. 893df8bae1dSRodney W. Grimes */ 894df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa) 895462b86feSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 896df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family == AF_INET && 897df8bae1dSRodney W. Grimes (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 898df8bae1dSRodney W. Grimes in.s_addr == ia->ia_netbroadcast.s_addr || 899df8bae1dSRodney W. Grimes /* 900df8bae1dSRodney W. Grimes * Check for old-style (host 0) broadcast. 901df8bae1dSRodney W. Grimes */ 9028dd27fd6SGuido van Rooij t == ia->ia_subnet || t == ia->ia_net) && 9038dd27fd6SGuido van Rooij /* 9048dd27fd6SGuido van Rooij * Check for an all one subnetmask. These 9058dd27fd6SGuido van Rooij * only exist when an interface gets a secondary 9068dd27fd6SGuido van Rooij * address. 9078dd27fd6SGuido van Rooij */ 9088dd27fd6SGuido van Rooij ia->ia_subnetmask != (u_long)0xffffffff) 909df8bae1dSRodney W. Grimes return 1; 910df8bae1dSRodney W. Grimes return (0); 911df8bae1dSRodney W. Grimes #undef ia 912df8bae1dSRodney W. Grimes } 913df8bae1dSRodney W. Grimes /* 914df8bae1dSRodney W. Grimes * Add an address to the list of IP multicast addresses for a given interface. 915df8bae1dSRodney W. Grimes */ 916df8bae1dSRodney W. Grimes struct in_multi * 917df8bae1dSRodney W. Grimes in_addmulti(ap, ifp) 918df8bae1dSRodney W. Grimes register struct in_addr *ap; 919df8bae1dSRodney W. Grimes register struct ifnet *ifp; 920df8bae1dSRodney W. Grimes { 921df8bae1dSRodney W. Grimes register struct in_multi *inm; 922477180fbSGarrett Wollman int error; 923477180fbSGarrett Wollman struct sockaddr_in sin; 924477180fbSGarrett Wollman struct ifmultiaddr *ifma; 925df8bae1dSRodney W. Grimes int s = splnet(); 926df8bae1dSRodney W. Grimes 927df8bae1dSRodney W. Grimes /* 928477180fbSGarrett Wollman * Call generic routine to add membership or increment 929477180fbSGarrett Wollman * refcount. It wants addresses in the form of a sockaddr, 930477180fbSGarrett Wollman * so we build one here (being careful to zero the unused bytes). 931df8bae1dSRodney W. Grimes */ 932477180fbSGarrett Wollman bzero(&sin, sizeof sin); 933477180fbSGarrett Wollman sin.sin_family = AF_INET; 934477180fbSGarrett Wollman sin.sin_len = sizeof sin; 935477180fbSGarrett Wollman sin.sin_addr = *ap; 936477180fbSGarrett Wollman error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma); 937477180fbSGarrett Wollman if (error) { 938477180fbSGarrett Wollman splx(s); 939477180fbSGarrett Wollman return 0; 940df8bae1dSRodney W. Grimes } 941477180fbSGarrett Wollman 942df8bae1dSRodney W. Grimes /* 943477180fbSGarrett Wollman * If ifma->ifma_protospec is null, then if_addmulti() created 944477180fbSGarrett Wollman * a new record. Otherwise, we are done. 945df8bae1dSRodney W. Grimes */ 9464153a3a3SBruce Evans if (ifma->ifma_protospec != 0) { 9474153a3a3SBruce Evans splx(s); 948477180fbSGarrett Wollman return ifma->ifma_protospec; 9494153a3a3SBruce Evans } 950477180fbSGarrett Wollman 951a163d034SWarner Losh /* XXX - if_addmulti uses M_WAITOK. Can this really be called 952477180fbSGarrett Wollman at interrupt time? If so, need to fix if_addmulti. XXX */ 9537cc0979fSDavid Malone inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR, 9547cc0979fSDavid Malone M_NOWAIT | M_ZERO); 955df8bae1dSRodney W. Grimes if (inm == NULL) { 956df8bae1dSRodney W. Grimes splx(s); 957df8bae1dSRodney W. Grimes return (NULL); 958df8bae1dSRodney W. Grimes } 959477180fbSGarrett Wollman 960df8bae1dSRodney W. Grimes inm->inm_addr = *ap; 961df8bae1dSRodney W. Grimes inm->inm_ifp = ifp; 962477180fbSGarrett Wollman inm->inm_ifma = ifma; 963477180fbSGarrett Wollman ifma->ifma_protospec = inm; 964477180fbSGarrett Wollman LIST_INSERT_HEAD(&in_multihead, inm, inm_link); 965ffa5b11aSGarrett Wollman 966df8bae1dSRodney W. Grimes /* 967df8bae1dSRodney W. Grimes * Let IGMP know that we have joined a new IP multicast group. 968df8bae1dSRodney W. Grimes */ 969df8bae1dSRodney W. Grimes igmp_joingroup(inm); 970df8bae1dSRodney W. Grimes splx(s); 971df8bae1dSRodney W. Grimes return (inm); 972df8bae1dSRodney W. Grimes } 973df8bae1dSRodney W. Grimes 974df8bae1dSRodney W. Grimes /* 975df8bae1dSRodney W. Grimes * Delete a multicast address record. 976df8bae1dSRodney W. Grimes */ 97726f9a767SRodney W. Grimes void 978df8bae1dSRodney W. Grimes in_delmulti(inm) 979df8bae1dSRodney W. Grimes register struct in_multi *inm; 980df8bae1dSRodney W. Grimes { 981477180fbSGarrett Wollman struct ifmultiaddr *ifma = inm->inm_ifma; 98288a5354eSLuigi Rizzo struct in_multi my_inm; 983df8bae1dSRodney W. Grimes int s = splnet(); 984df8bae1dSRodney W. Grimes 98588a5354eSLuigi Rizzo my_inm.inm_ifp = NULL ; /* don't send the leave msg */ 986477180fbSGarrett Wollman if (ifma->ifma_refcount == 1) { 987df8bae1dSRodney W. Grimes /* 988df8bae1dSRodney W. Grimes * No remaining claims to this record; let IGMP know that 989df8bae1dSRodney W. Grimes * we are leaving the multicast group. 99088a5354eSLuigi Rizzo * But do it after the if_delmulti() which might reset 99188a5354eSLuigi Rizzo * the interface and nuke the packet. 992df8bae1dSRodney W. Grimes */ 99388a5354eSLuigi Rizzo my_inm = *inm ; 994477180fbSGarrett Wollman ifma->ifma_protospec = 0; 995477180fbSGarrett Wollman LIST_REMOVE(inm, inm_link); 996df8bae1dSRodney W. Grimes free(inm, M_IPMADDR); 997df8bae1dSRodney W. Grimes } 998477180fbSGarrett Wollman /* XXX - should be separate API for when we have an ifma? */ 999477180fbSGarrett Wollman if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); 100088a5354eSLuigi Rizzo if (my_inm.inm_ifp != NULL) 100188a5354eSLuigi Rizzo igmp_leavegroup(&my_inm); 1002df8bae1dSRodney W. Grimes splx(s); 1003df8bae1dSRodney W. Grimes } 1004