1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 332180b925SGarrett Wollman * @(#)in.c 8.4 (Berkeley) 1/9/95 34c655b7c4SDavid Greenman * $Id: in.c,v 1.23 1996/03/15 17:08:07 fenner Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 382ee45d7dSDavid Greenman #include <sys/queue.h> 3926f9a767SRodney W. Grimes #include <sys/systm.h> 40df8bae1dSRodney W. Grimes #include <sys/ioctl.h> 41df8bae1dSRodney W. Grimes #include <sys/errno.h> 42df8bae1dSRodney W. Grimes #include <sys/malloc.h> 43df8bae1dSRodney W. Grimes #include <sys/socket.h> 44df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 45f6d24a78SPoul-Henning Kamp #include <sys/kernel.h> 46f6d24a78SPoul-Henning Kamp #include <sys/sysctl.h> 47df8bae1dSRodney W. Grimes 48df8bae1dSRodney W. Grimes #include <net/if.h> 49df8bae1dSRodney W. Grimes #include <net/route.h> 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 52df8bae1dSRodney W. Grimes #include <netinet/in.h> 53df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 54df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 55df8bae1dSRodney W. Grimes 56c70f4510SPoul-Henning Kamp #include <netinet/igmp_var.h> 57c70f4510SPoul-Henning Kamp 58df8bae1dSRodney W. Grimes /* 59ffa5b11aSGarrett Wollman * This structure is used to keep track of in_multi chains which belong to 60ffa5b11aSGarrett Wollman * deleted interface addresses. 61ffa5b11aSGarrett Wollman */ 62ffa5b11aSGarrett Wollman static LIST_HEAD(, multi_kludge) in_mk; /* XXX BSS initialization */ 63ffa5b11aSGarrett Wollman 64ffa5b11aSGarrett Wollman struct multi_kludge { 65ffa5b11aSGarrett Wollman LIST_ENTRY(multi_kludge) mk_entry; 66ffa5b11aSGarrett Wollman struct ifnet *mk_ifp; 67ffa5b11aSGarrett Wollman struct in_multihead mk_head; 68ffa5b11aSGarrett Wollman }; 69ffa5b11aSGarrett Wollman 700312fbe9SPoul-Henning Kamp static void in_socktrim __P((struct sockaddr_in *)); 710312fbe9SPoul-Henning Kamp static int in_ifinit __P((struct ifnet *, 720312fbe9SPoul-Henning Kamp struct in_ifaddr *, struct sockaddr_in *, int)); 730312fbe9SPoul-Henning Kamp static void in_ifscrub __P((struct ifnet *, struct in_ifaddr *)); 74df8bae1dSRodney W. Grimes 75f6d24a78SPoul-Henning Kamp static int subnetsarelocal = 1; 76f6d24a78SPoul-Henning Kamp SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW, 77f6d24a78SPoul-Henning Kamp &subnetsarelocal, 0, ""); 78df8bae1dSRodney W. Grimes /* 79df8bae1dSRodney W. Grimes * Return 1 if an internet address is for a ``local'' host 80df8bae1dSRodney W. Grimes * (one to which we have a connection). If subnetsarelocal 81df8bae1dSRodney W. Grimes * is true, this includes other subnets of the local net. 82df8bae1dSRodney W. Grimes * Otherwise, it includes only the directly-connected (sub)nets. 83df8bae1dSRodney W. Grimes */ 8426f9a767SRodney W. Grimes int 85df8bae1dSRodney W. Grimes in_localaddr(in) 86df8bae1dSRodney W. Grimes struct in_addr in; 87df8bae1dSRodney W. Grimes { 88df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 89df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 90df8bae1dSRodney W. Grimes 91df8bae1dSRodney W. Grimes if (subnetsarelocal) { 92df8bae1dSRodney W. Grimes for (ia = in_ifaddr; ia; ia = ia->ia_next) 93df8bae1dSRodney W. Grimes if ((i & ia->ia_netmask) == ia->ia_net) 94df8bae1dSRodney W. Grimes return (1); 95df8bae1dSRodney W. Grimes } else { 96df8bae1dSRodney W. Grimes for (ia = in_ifaddr; ia; ia = ia->ia_next) 97df8bae1dSRodney W. Grimes if ((i & ia->ia_subnetmask) == ia->ia_subnet) 98df8bae1dSRodney W. Grimes return (1); 99df8bae1dSRodney W. Grimes } 100df8bae1dSRodney W. Grimes return (0); 101df8bae1dSRodney W. Grimes } 102df8bae1dSRodney W. Grimes 103df8bae1dSRodney W. Grimes /* 104df8bae1dSRodney W. Grimes * Determine whether an IP address is in a reserved set of addresses 105df8bae1dSRodney W. Grimes * that may not be forwarded, or whether datagrams to that destination 106df8bae1dSRodney W. Grimes * may be forwarded. 107df8bae1dSRodney W. Grimes */ 10826f9a767SRodney W. Grimes int 109df8bae1dSRodney W. Grimes in_canforward(in) 110df8bae1dSRodney W. Grimes struct in_addr in; 111df8bae1dSRodney W. Grimes { 112df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 113df8bae1dSRodney W. Grimes register u_long net; 114df8bae1dSRodney W. Grimes 115df8bae1dSRodney W. Grimes if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) 116df8bae1dSRodney W. Grimes return (0); 117df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) { 118df8bae1dSRodney W. Grimes net = i & IN_CLASSA_NET; 119df8bae1dSRodney W. Grimes if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 120df8bae1dSRodney W. Grimes return (0); 121df8bae1dSRodney W. Grimes } 122df8bae1dSRodney W. Grimes return (1); 123df8bae1dSRodney W. Grimes } 124df8bae1dSRodney W. Grimes 125df8bae1dSRodney W. Grimes /* 126df8bae1dSRodney W. Grimes * Trim a mask in a sockaddr 127df8bae1dSRodney W. Grimes */ 1280312fbe9SPoul-Henning Kamp static void 129df8bae1dSRodney W. Grimes in_socktrim(ap) 130df8bae1dSRodney W. Grimes struct sockaddr_in *ap; 131df8bae1dSRodney W. Grimes { 132df8bae1dSRodney W. Grimes register char *cplim = (char *) &ap->sin_addr; 133df8bae1dSRodney W. Grimes register char *cp = (char *) (&ap->sin_addr + 1); 134df8bae1dSRodney W. Grimes 135df8bae1dSRodney W. Grimes ap->sin_len = 0; 136df00058dSGarrett Wollman while (--cp >= cplim) 137df8bae1dSRodney W. Grimes if (*cp) { 138df8bae1dSRodney W. Grimes (ap)->sin_len = cp - (char *) (ap) + 1; 139df8bae1dSRodney W. Grimes break; 140df8bae1dSRodney W. Grimes } 141df8bae1dSRodney W. Grimes } 142df8bae1dSRodney W. Grimes 143f6d24a78SPoul-Henning Kamp static int in_interfaces; /* number of external internet interfaces */ 144df8bae1dSRodney W. Grimes 145df8bae1dSRodney W. Grimes /* 146df8bae1dSRodney W. Grimes * Generic internet control operations (ioctl's). 147df8bae1dSRodney W. Grimes * Ifp is 0 if not an interface-specific ioctl. 148df8bae1dSRodney W. Grimes */ 149df8bae1dSRodney W. Grimes /* ARGSUSED */ 15026f9a767SRodney W. Grimes int 151df8bae1dSRodney W. Grimes in_control(so, cmd, data, ifp) 152df8bae1dSRodney W. Grimes struct socket *so; 1532180b925SGarrett Wollman u_long cmd; 154df8bae1dSRodney W. Grimes caddr_t data; 155df8bae1dSRodney W. Grimes register struct ifnet *ifp; 156df8bae1dSRodney W. Grimes { 157df8bae1dSRodney W. Grimes register struct ifreq *ifr = (struct ifreq *)data; 158ac0aa473SBill Fenner register struct in_ifaddr *ia = 0, *iap; 159df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 160df8bae1dSRodney W. Grimes struct in_ifaddr *oia; 161df8bae1dSRodney W. Grimes struct in_aliasreq *ifra = (struct in_aliasreq *)data; 162df8bae1dSRodney W. Grimes struct sockaddr_in oldaddr; 163c655b7c4SDavid Greenman int error, hostIsNew, maskIsNew, s; 164df8bae1dSRodney W. Grimes u_long i; 165ffa5b11aSGarrett Wollman struct multi_kludge *mk; 166df8bae1dSRodney W. Grimes 167df8bae1dSRodney W. Grimes /* 168df8bae1dSRodney W. Grimes * Find address for this interface, if it exists. 169ac0aa473SBill Fenner * 170ac0aa473SBill Fenner * If an alias address was specified, find that one instead of 171ac0aa473SBill Fenner * the first one on the interface. 172df8bae1dSRodney W. Grimes */ 173df8bae1dSRodney W. Grimes if (ifp) 174ac0aa473SBill Fenner for (iap = in_ifaddr; iap; iap = iap->ia_next) 175ac0aa473SBill Fenner if (iap->ia_ifp == ifp) { 176ac0aa473SBill Fenner if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr == 177ac0aa473SBill Fenner iap->ia_addr.sin_addr.s_addr) { 178ac0aa473SBill Fenner ia = iap; 179df8bae1dSRodney W. Grimes break; 180ac0aa473SBill Fenner } else if (ia == NULL) { 181ac0aa473SBill Fenner ia = iap; 182ac0aa473SBill Fenner if (ifr->ifr_addr.sa_family != AF_INET) 183ac0aa473SBill Fenner break; 184ac0aa473SBill Fenner } 185ac0aa473SBill Fenner } 186df8bae1dSRodney W. Grimes 187df8bae1dSRodney W. Grimes switch (cmd) { 188df8bae1dSRodney W. Grimes 189df8bae1dSRodney W. Grimes case SIOCAIFADDR: 190df8bae1dSRodney W. Grimes case SIOCDIFADDR: 1911067217dSGarrett Wollman if (ifra->ifra_addr.sin_family == AF_INET) { 192df8bae1dSRodney W. Grimes for (oia = ia; ia; ia = ia->ia_next) { 193df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp && 194df8bae1dSRodney W. Grimes ia->ia_addr.sin_addr.s_addr == 195df8bae1dSRodney W. Grimes ifra->ifra_addr.sin_addr.s_addr) 196df8bae1dSRodney W. Grimes break; 197df8bae1dSRodney W. Grimes } 1981067217dSGarrett Wollman if ((ifp->if_flags & IFF_POINTOPOINT) 1991067217dSGarrett Wollman && (cmd == SIOCAIFADDR) 2001067217dSGarrett Wollman && (ifra->ifra_dstaddr.sin_addr.s_addr 2011067217dSGarrett Wollman == INADDR_ANY)) { 202357b78a9SGarrett Wollman return EDESTADDRREQ; 2031067217dSGarrett Wollman } 2041067217dSGarrett Wollman } 205df8bae1dSRodney W. Grimes if (cmd == SIOCDIFADDR && ia == 0) 206df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 207df8bae1dSRodney W. Grimes /* FALLTHROUGH */ 208df8bae1dSRodney W. Grimes case SIOCSIFADDR: 209df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 210df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 211df8bae1dSRodney W. Grimes if ((so->so_state & SS_PRIV) == 0) 212df8bae1dSRodney W. Grimes return (EPERM); 213df8bae1dSRodney W. Grimes 214df8bae1dSRodney W. Grimes if (ifp == 0) 215df8bae1dSRodney W. Grimes panic("in_control"); 216df8bae1dSRodney W. Grimes if (ia == (struct in_ifaddr *)0) { 217df8bae1dSRodney W. Grimes oia = (struct in_ifaddr *) 218df8bae1dSRodney W. Grimes malloc(sizeof *oia, M_IFADDR, M_WAITOK); 219df8bae1dSRodney W. Grimes if (oia == (struct in_ifaddr *)NULL) 220df8bae1dSRodney W. Grimes return (ENOBUFS); 221df8bae1dSRodney W. Grimes bzero((caddr_t)oia, sizeof *oia); 222623ae52eSPoul-Henning Kamp ia = in_ifaddr; 223c655b7c4SDavid Greenman /* 224c655b7c4SDavid Greenman * Protect from ipintr() traversing address list 225c655b7c4SDavid Greenman * while we're modifying it. 226c655b7c4SDavid Greenman */ 227c655b7c4SDavid Greenman s = splnet(); 228c655b7c4SDavid Greenman 229623ae52eSPoul-Henning Kamp if (ia) { 230df8bae1dSRodney W. Grimes for ( ; ia->ia_next; ia = ia->ia_next) 231df8bae1dSRodney W. Grimes continue; 232df8bae1dSRodney W. Grimes ia->ia_next = oia; 233df8bae1dSRodney W. Grimes } else 234df8bae1dSRodney W. Grimes in_ifaddr = oia; 235df8bae1dSRodney W. Grimes ia = oia; 236623ae52eSPoul-Henning Kamp ifa = ifp->if_addrlist; 237623ae52eSPoul-Henning Kamp if (ifa) { 238df8bae1dSRodney W. Grimes for ( ; ifa->ifa_next; ifa = ifa->ifa_next) 239df8bae1dSRodney W. Grimes continue; 240df8bae1dSRodney W. Grimes ifa->ifa_next = (struct ifaddr *) ia; 241df8bae1dSRodney W. Grimes } else 242df8bae1dSRodney W. Grimes ifp->if_addrlist = (struct ifaddr *) ia; 243df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 244df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr 245df8bae1dSRodney W. Grimes = (struct sockaddr *)&ia->ia_dstaddr; 246df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_netmask 247df8bae1dSRodney W. Grimes = (struct sockaddr *)&ia->ia_sockmask; 248df8bae1dSRodney W. Grimes ia->ia_sockmask.sin_len = 8; 249df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 250df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 251df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_family = AF_INET; 252df8bae1dSRodney W. Grimes } 253df8bae1dSRodney W. Grimes ia->ia_ifp = ifp; 254f5fea3ddSPaul Traina if (!(ifp->if_flags & IFF_LOOPBACK)) 255df8bae1dSRodney W. Grimes in_interfaces++; 256c655b7c4SDavid Greenman splx(s); 257df8bae1dSRodney W. Grimes } 258df8bae1dSRodney W. Grimes break; 259df8bae1dSRodney W. Grimes 260df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 261df8bae1dSRodney W. Grimes if ((so->so_state & SS_PRIV) == 0) 262df8bae1dSRodney W. Grimes return (EPERM); 263df8bae1dSRodney W. Grimes /* FALLTHROUGH */ 264df8bae1dSRodney W. Grimes 265df8bae1dSRodney W. Grimes case SIOCGIFADDR: 266df8bae1dSRodney W. Grimes case SIOCGIFNETMASK: 267df8bae1dSRodney W. Grimes case SIOCGIFDSTADDR: 268df8bae1dSRodney W. Grimes case SIOCGIFBRDADDR: 269df8bae1dSRodney W. Grimes if (ia == (struct in_ifaddr *)0) 270df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 271df8bae1dSRodney W. Grimes break; 272df8bae1dSRodney W. Grimes } 273df8bae1dSRodney W. Grimes switch (cmd) { 274df8bae1dSRodney W. Grimes 275df8bae1dSRodney W. Grimes case SIOCGIFADDR: 276df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 277df8bae1dSRodney W. Grimes break; 278df8bae1dSRodney W. Grimes 279df8bae1dSRodney W. Grimes case SIOCGIFBRDADDR: 280df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 281df8bae1dSRodney W. Grimes return (EINVAL); 282df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 283df8bae1dSRodney W. Grimes break; 284df8bae1dSRodney W. Grimes 285df8bae1dSRodney W. Grimes case SIOCGIFDSTADDR: 286df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 287df8bae1dSRodney W. Grimes return (EINVAL); 288df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 289df8bae1dSRodney W. Grimes break; 290df8bae1dSRodney W. Grimes 291df8bae1dSRodney W. Grimes case SIOCGIFNETMASK: 292df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 293df8bae1dSRodney W. Grimes break; 294df8bae1dSRodney W. Grimes 295df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 296df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 297df8bae1dSRodney W. Grimes return (EINVAL); 298df8bae1dSRodney W. Grimes oldaddr = ia->ia_dstaddr; 299df8bae1dSRodney W. Grimes ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 300df8bae1dSRodney W. Grimes if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 301df8bae1dSRodney W. Grimes (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 302df8bae1dSRodney W. Grimes ia->ia_dstaddr = oldaddr; 303df8bae1dSRodney W. Grimes return (error); 304df8bae1dSRodney W. Grimes } 305df8bae1dSRodney W. Grimes if (ia->ia_flags & IFA_ROUTE) { 306df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 307df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 308df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr = 309df8bae1dSRodney W. Grimes (struct sockaddr *)&ia->ia_dstaddr; 310df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 311df8bae1dSRodney W. Grimes } 312df8bae1dSRodney W. Grimes break; 313df8bae1dSRodney W. Grimes 314df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 315df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 316df8bae1dSRodney W. Grimes return (EINVAL); 317df8bae1dSRodney W. Grimes ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 318df8bae1dSRodney W. Grimes break; 319df8bae1dSRodney W. Grimes 320df8bae1dSRodney W. Grimes case SIOCSIFADDR: 321df8bae1dSRodney W. Grimes return (in_ifinit(ifp, ia, 322df8bae1dSRodney W. Grimes (struct sockaddr_in *) &ifr->ifr_addr, 1)); 323df8bae1dSRodney W. Grimes 324df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 325df8bae1dSRodney W. Grimes i = ifra->ifra_addr.sin_addr.s_addr; 326df8bae1dSRodney W. Grimes ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i); 327df8bae1dSRodney W. Grimes break; 328df8bae1dSRodney W. Grimes 329df8bae1dSRodney W. Grimes case SIOCAIFADDR: 330df8bae1dSRodney W. Grimes maskIsNew = 0; 331df8bae1dSRodney W. Grimes hostIsNew = 1; 332df8bae1dSRodney W. Grimes error = 0; 333df8bae1dSRodney W. Grimes if (ia->ia_addr.sin_family == AF_INET) { 334df8bae1dSRodney W. Grimes if (ifra->ifra_addr.sin_len == 0) { 335df8bae1dSRodney W. Grimes ifra->ifra_addr = ia->ia_addr; 336df8bae1dSRodney W. Grimes hostIsNew = 0; 337df8bae1dSRodney W. Grimes } else if (ifra->ifra_addr.sin_addr.s_addr == 338df8bae1dSRodney W. Grimes ia->ia_addr.sin_addr.s_addr) 339df8bae1dSRodney W. Grimes hostIsNew = 0; 340df8bae1dSRodney W. Grimes } 341df8bae1dSRodney W. Grimes if (ifra->ifra_mask.sin_len) { 342df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 343df8bae1dSRodney W. Grimes ia->ia_sockmask = ifra->ifra_mask; 344df8bae1dSRodney W. Grimes ia->ia_subnetmask = 345df8bae1dSRodney W. Grimes ntohl(ia->ia_sockmask.sin_addr.s_addr); 346df8bae1dSRodney W. Grimes maskIsNew = 1; 347df8bae1dSRodney W. Grimes } 348df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) && 349df8bae1dSRodney W. Grimes (ifra->ifra_dstaddr.sin_family == AF_INET)) { 350df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 351df8bae1dSRodney W. Grimes ia->ia_dstaddr = ifra->ifra_dstaddr; 352df8bae1dSRodney W. Grimes maskIsNew = 1; /* We lie; but the effect's the same */ 353df8bae1dSRodney W. Grimes } 354df8bae1dSRodney W. Grimes if (ifra->ifra_addr.sin_family == AF_INET && 355df8bae1dSRodney W. Grimes (hostIsNew || maskIsNew)) 356df8bae1dSRodney W. Grimes error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 357df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) && 358df8bae1dSRodney W. Grimes (ifra->ifra_broadaddr.sin_family == AF_INET)) 359df8bae1dSRodney W. Grimes ia->ia_broadaddr = ifra->ifra_broadaddr; 360df8bae1dSRodney W. Grimes return (error); 361df8bae1dSRodney W. Grimes 362df8bae1dSRodney W. Grimes case SIOCDIFADDR: 363ffa5b11aSGarrett Wollman mk = malloc(sizeof *mk, M_IPMADDR, M_WAITOK); 364ffa5b11aSGarrett Wollman if (!mk) 365ffa5b11aSGarrett Wollman return ENOBUFS; 366ffa5b11aSGarrett Wollman 367df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 368c655b7c4SDavid Greenman /* 369c655b7c4SDavid Greenman * Protect from ipintr() traversing address list 370c655b7c4SDavid Greenman * while we're modifying it. 371c655b7c4SDavid Greenman */ 372c655b7c4SDavid Greenman s = splnet(); 373c655b7c4SDavid Greenman 374df8bae1dSRodney W. Grimes if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia) 375df8bae1dSRodney W. Grimes ifp->if_addrlist = ifa->ifa_next; 376df8bae1dSRodney W. Grimes else { 377df8bae1dSRodney W. Grimes while (ifa->ifa_next && 378df8bae1dSRodney W. Grimes (ifa->ifa_next != (struct ifaddr *)ia)) 379df8bae1dSRodney W. Grimes ifa = ifa->ifa_next; 380df8bae1dSRodney W. Grimes if (ifa->ifa_next) 381df8bae1dSRodney W. Grimes ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next; 382df8bae1dSRodney W. Grimes else 383df8bae1dSRodney W. Grimes printf("Couldn't unlink inifaddr from ifp\n"); 384df8bae1dSRodney W. Grimes } 385df8bae1dSRodney W. Grimes oia = ia; 386df8bae1dSRodney W. Grimes if (oia == (ia = in_ifaddr)) 387df8bae1dSRodney W. Grimes in_ifaddr = ia->ia_next; 388df8bae1dSRodney W. Grimes else { 389df8bae1dSRodney W. Grimes while (ia->ia_next && (ia->ia_next != oia)) 390df8bae1dSRodney W. Grimes ia = ia->ia_next; 391df8bae1dSRodney W. Grimes if (ia->ia_next) 392df8bae1dSRodney W. Grimes ia->ia_next = oia->ia_next; 393df8bae1dSRodney W. Grimes else 394df8bae1dSRodney W. Grimes printf("Didn't unlink inifadr from list\n"); 395df8bae1dSRodney W. Grimes } 396ffa5b11aSGarrett Wollman 397ffa5b11aSGarrett Wollman if (!oia->ia_multiaddrs.lh_first) { 398ffa5b11aSGarrett Wollman IFAFREE(&oia->ia_ifa); 399ffa5b11aSGarrett Wollman FREE(mk, M_IPMADDR); 400c655b7c4SDavid Greenman splx(s); 401ffa5b11aSGarrett Wollman break; 402ffa5b11aSGarrett Wollman } 403ffa5b11aSGarrett Wollman 404ffa5b11aSGarrett Wollman /* 405ffa5b11aSGarrett Wollman * Multicast address kludge: 406ffa5b11aSGarrett Wollman * If there were any multicast addresses attached to this 407ffa5b11aSGarrett Wollman * interface address, either move them to another address 408ffa5b11aSGarrett Wollman * on this interface, or save them until such time as this 409ffa5b11aSGarrett Wollman * interface is reconfigured for IP. 410ffa5b11aSGarrett Wollman */ 411ffa5b11aSGarrett Wollman IFP_TO_IA(oia->ia_ifp, ia); 412ffa5b11aSGarrett Wollman if (ia) { /* there is another address */ 413ffa5b11aSGarrett Wollman struct in_multi *inm; 414ffa5b11aSGarrett Wollman for(inm = oia->ia_multiaddrs.lh_first; inm; 415ffa5b11aSGarrett Wollman inm = inm->inm_entry.le_next) { 416ffa5b11aSGarrett Wollman IFAFREE(&inm->inm_ia->ia_ifa); 417ffa5b11aSGarrett Wollman ia->ia_ifa.ifa_refcnt++; 418ffa5b11aSGarrett Wollman inm->inm_ia = ia; 419ffa5b11aSGarrett Wollman LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, 420ffa5b11aSGarrett Wollman inm_entry); 421ffa5b11aSGarrett Wollman } 422ffa5b11aSGarrett Wollman FREE(mk, M_IPMADDR); 423ffa5b11aSGarrett Wollman } else { /* last address on this if deleted, save */ 424ffa5b11aSGarrett Wollman struct in_multi *inm; 425ffa5b11aSGarrett Wollman 426ffa5b11aSGarrett Wollman LIST_INIT(&mk->mk_head); 427ffa5b11aSGarrett Wollman mk->mk_ifp = ifp; 428ffa5b11aSGarrett Wollman 429ffa5b11aSGarrett Wollman for(inm = oia->ia_multiaddrs.lh_first; inm; 430ffa5b11aSGarrett Wollman inm = inm->inm_entry.le_next) { 431ffa5b11aSGarrett Wollman LIST_INSERT_HEAD(&mk->mk_head, inm, inm_entry); 432ffa5b11aSGarrett Wollman } 433ffa5b11aSGarrett Wollman 434ffa5b11aSGarrett Wollman if (mk->mk_head.lh_first) { 435ffa5b11aSGarrett Wollman LIST_INSERT_HEAD(&in_mk, mk, mk_entry); 436ffa5b11aSGarrett Wollman } else { 437ffa5b11aSGarrett Wollman FREE(mk, M_IPMADDR); 438ffa5b11aSGarrett Wollman } 439ffa5b11aSGarrett Wollman } 440ffa5b11aSGarrett Wollman 441df8bae1dSRodney W. Grimes IFAFREE((&oia->ia_ifa)); 442c655b7c4SDavid Greenman splx(s); 443df8bae1dSRodney W. Grimes break; 444df8bae1dSRodney W. Grimes 445df8bae1dSRodney W. Grimes default: 446df8bae1dSRodney W. Grimes if (ifp == 0 || ifp->if_ioctl == 0) 447df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 448df8bae1dSRodney W. Grimes return ((*ifp->if_ioctl)(ifp, cmd, data)); 449df8bae1dSRodney W. Grimes } 450df8bae1dSRodney W. Grimes return (0); 451df8bae1dSRodney W. Grimes } 452df8bae1dSRodney W. Grimes 453df8bae1dSRodney W. Grimes /* 454df8bae1dSRodney W. Grimes * Delete any existing route for an interface. 455df8bae1dSRodney W. Grimes */ 4560312fbe9SPoul-Henning Kamp static void 457df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia) 458df8bae1dSRodney W. Grimes register struct ifnet *ifp; 459df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 460df8bae1dSRodney W. Grimes { 461df8bae1dSRodney W. Grimes 462df8bae1dSRodney W. Grimes if ((ia->ia_flags & IFA_ROUTE) == 0) 463df8bae1dSRodney W. Grimes return; 464df8bae1dSRodney W. Grimes if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 465df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 466df8bae1dSRodney W. Grimes else 467df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 468df8bae1dSRodney W. Grimes ia->ia_flags &= ~IFA_ROUTE; 469df8bae1dSRodney W. Grimes } 470df8bae1dSRodney W. Grimes 471df8bae1dSRodney W. Grimes /* 472df8bae1dSRodney W. Grimes * Initialize an interface's internet address 473df8bae1dSRodney W. Grimes * and routing table entry. 474df8bae1dSRodney W. Grimes */ 4750312fbe9SPoul-Henning Kamp static int 476df8bae1dSRodney W. Grimes in_ifinit(ifp, ia, sin, scrub) 477df8bae1dSRodney W. Grimes register struct ifnet *ifp; 478df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 479df8bae1dSRodney W. Grimes struct sockaddr_in *sin; 480df8bae1dSRodney W. Grimes int scrub; 481df8bae1dSRodney W. Grimes { 482df8bae1dSRodney W. Grimes register u_long i = ntohl(sin->sin_addr.s_addr); 483df8bae1dSRodney W. Grimes struct sockaddr_in oldaddr; 484f23b4c91SGarrett Wollman int s = splimp(), flags = RTF_UP, error; 485ffa5b11aSGarrett Wollman struct multi_kludge *mk; 486df8bae1dSRodney W. Grimes 487df8bae1dSRodney W. Grimes oldaddr = ia->ia_addr; 488df8bae1dSRodney W. Grimes ia->ia_addr = *sin; 489df8bae1dSRodney W. Grimes /* 490df8bae1dSRodney W. Grimes * Give the interface a chance to initialize 491df8bae1dSRodney W. Grimes * if this is its first address, 492df8bae1dSRodney W. Grimes * and to validate the address if necessary. 493df8bae1dSRodney W. Grimes */ 494df8bae1dSRodney W. Grimes if (ifp->if_ioctl && 495df8bae1dSRodney W. Grimes (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 496df8bae1dSRodney W. Grimes splx(s); 497df8bae1dSRodney W. Grimes ia->ia_addr = oldaddr; 498df8bae1dSRodney W. Grimes return (error); 499df8bae1dSRodney W. Grimes } 500df8bae1dSRodney W. Grimes splx(s); 501df8bae1dSRodney W. Grimes if (scrub) { 502df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 503df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 504df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 505df8bae1dSRodney W. Grimes } 506df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) 507df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSA_NET; 508df8bae1dSRodney W. Grimes else if (IN_CLASSB(i)) 509df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSB_NET; 510df8bae1dSRodney W. Grimes else 511df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSC_NET; 512df8bae1dSRodney W. Grimes /* 513df8bae1dSRodney W. Grimes * The subnet mask usually includes at least the standard network part, 514df8bae1dSRodney W. Grimes * but may may be smaller in the case of supernetting. 515df8bae1dSRodney W. Grimes * If it is set, we believe it. 516df8bae1dSRodney W. Grimes */ 517df8bae1dSRodney W. Grimes if (ia->ia_subnetmask == 0) { 518df8bae1dSRodney W. Grimes ia->ia_subnetmask = ia->ia_netmask; 519df8bae1dSRodney W. Grimes ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 520df8bae1dSRodney W. Grimes } else 521df8bae1dSRodney W. Grimes ia->ia_netmask &= ia->ia_subnetmask; 522df8bae1dSRodney W. Grimes ia->ia_net = i & ia->ia_netmask; 523df8bae1dSRodney W. Grimes ia->ia_subnet = i & ia->ia_subnetmask; 524df8bae1dSRodney W. Grimes in_socktrim(&ia->ia_sockmask); 525df8bae1dSRodney W. Grimes /* 526df8bae1dSRodney W. Grimes * Add route for the network. 527df8bae1dSRodney W. Grimes */ 528df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_metric = ifp->if_metric; 529df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 530df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_addr.s_addr = 531df8bae1dSRodney W. Grimes htonl(ia->ia_subnet | ~ia->ia_subnetmask); 532df8bae1dSRodney W. Grimes ia->ia_netbroadcast.s_addr = 533df8bae1dSRodney W. Grimes htonl(ia->ia_net | ~ ia->ia_netmask); 534df8bae1dSRodney W. Grimes } else if (ifp->if_flags & IFF_LOOPBACK) { 535df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 536df8bae1dSRodney W. Grimes flags |= RTF_HOST; 537df8bae1dSRodney W. Grimes } else if (ifp->if_flags & IFF_POINTOPOINT) { 538df8bae1dSRodney W. Grimes if (ia->ia_dstaddr.sin_family != AF_INET) 539df8bae1dSRodney W. Grimes return (0); 540df8bae1dSRodney W. Grimes flags |= RTF_HOST; 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) 543df8bae1dSRodney W. Grimes ia->ia_flags |= IFA_ROUTE; 544ffa5b11aSGarrett Wollman 545ffa5b11aSGarrett Wollman LIST_INIT(&ia->ia_multiaddrs); 546df8bae1dSRodney W. Grimes /* 547df8bae1dSRodney W. Grimes * If the interface supports multicast, join the "all hosts" 548df8bae1dSRodney W. Grimes * multicast group on that interface. 549df8bae1dSRodney W. Grimes */ 550df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_MULTICAST) { 551df8bae1dSRodney W. Grimes struct in_addr addr; 552df8bae1dSRodney W. Grimes 553ffa5b11aSGarrett Wollman /* 554ffa5b11aSGarrett Wollman * Continuation of multicast address hack: 555ffa5b11aSGarrett Wollman * If there was a multicast group list previously saved 556ffa5b11aSGarrett Wollman * for this interface, then we re-attach it to the first 557ffa5b11aSGarrett Wollman * address configured on the i/f. 558ffa5b11aSGarrett Wollman */ 559ffa5b11aSGarrett Wollman for(mk = in_mk.lh_first; mk; mk = mk->mk_entry.le_next) { 560ffa5b11aSGarrett Wollman if(mk->mk_ifp == ifp) { 561ffa5b11aSGarrett Wollman struct in_multi *inm; 562ffa5b11aSGarrett Wollman 563ffa5b11aSGarrett Wollman for(inm = mk->mk_head.lh_first; inm; 564ffa5b11aSGarrett Wollman inm = inm->inm_entry.le_next) { 565ffa5b11aSGarrett Wollman IFAFREE(&inm->inm_ia->ia_ifa); 566ffa5b11aSGarrett Wollman ia->ia_ifa.ifa_refcnt++; 567ffa5b11aSGarrett Wollman inm->inm_ia = ia; 568ffa5b11aSGarrett Wollman LIST_INSERT_HEAD(&ia->ia_multiaddrs, 569ffa5b11aSGarrett Wollman inm, inm_entry); 570ffa5b11aSGarrett Wollman } 571ffa5b11aSGarrett Wollman LIST_REMOVE(mk, mk_entry); 572ffa5b11aSGarrett Wollman free(mk, M_IPMADDR); 573ffa5b11aSGarrett Wollman break; 574ffa5b11aSGarrett Wollman } 575ffa5b11aSGarrett Wollman } 576ffa5b11aSGarrett Wollman 577df8bae1dSRodney W. Grimes addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 578df8bae1dSRodney W. Grimes in_addmulti(&addr, ifp); 579df8bae1dSRodney W. Grimes } 580df8bae1dSRodney W. Grimes return (error); 581df8bae1dSRodney W. Grimes } 582df8bae1dSRodney W. Grimes 583df8bae1dSRodney W. Grimes 584df8bae1dSRodney W. Grimes /* 585df8bae1dSRodney W. Grimes * Return 1 if the address might be a local broadcast address. 586df8bae1dSRodney W. Grimes */ 58726f9a767SRodney W. Grimes int 588df8bae1dSRodney W. Grimes in_broadcast(in, ifp) 589df8bae1dSRodney W. Grimes struct in_addr in; 590df8bae1dSRodney W. Grimes struct ifnet *ifp; 591df8bae1dSRodney W. Grimes { 592df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 593df8bae1dSRodney W. Grimes u_long t; 594df8bae1dSRodney W. Grimes 595df8bae1dSRodney W. Grimes if (in.s_addr == INADDR_BROADCAST || 596df8bae1dSRodney W. Grimes in.s_addr == INADDR_ANY) 597df8bae1dSRodney W. Grimes return 1; 598df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 599df8bae1dSRodney W. Grimes return 0; 600df8bae1dSRodney W. Grimes t = ntohl(in.s_addr); 601df8bae1dSRodney W. Grimes /* 602df8bae1dSRodney W. Grimes * Look through the list of addresses for a match 603df8bae1dSRodney W. Grimes * with a broadcast address. 604df8bae1dSRodney W. Grimes */ 605df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa) 606df8bae1dSRodney W. Grimes for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 607df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family == AF_INET && 608df8bae1dSRodney W. Grimes (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 609df8bae1dSRodney W. Grimes in.s_addr == ia->ia_netbroadcast.s_addr || 610df8bae1dSRodney W. Grimes /* 611df8bae1dSRodney W. Grimes * Check for old-style (host 0) broadcast. 612df8bae1dSRodney W. Grimes */ 6138dd27fd6SGuido van Rooij t == ia->ia_subnet || t == ia->ia_net) && 6148dd27fd6SGuido van Rooij /* 6158dd27fd6SGuido van Rooij * Check for an all one subnetmask. These 6168dd27fd6SGuido van Rooij * only exist when an interface gets a secondary 6178dd27fd6SGuido van Rooij * address. 6188dd27fd6SGuido van Rooij */ 6198dd27fd6SGuido van Rooij ia->ia_subnetmask != (u_long)0xffffffff) 620df8bae1dSRodney W. Grimes return 1; 621df8bae1dSRodney W. Grimes return (0); 622df8bae1dSRodney W. Grimes #undef ia 623df8bae1dSRodney W. Grimes } 624df8bae1dSRodney W. Grimes /* 625df8bae1dSRodney W. Grimes * Add an address to the list of IP multicast addresses for a given interface. 626df8bae1dSRodney W. Grimes */ 627df8bae1dSRodney W. Grimes struct in_multi * 628df8bae1dSRodney W. Grimes in_addmulti(ap, ifp) 629df8bae1dSRodney W. Grimes register struct in_addr *ap; 630df8bae1dSRodney W. Grimes register struct ifnet *ifp; 631df8bae1dSRodney W. Grimes { 632df8bae1dSRodney W. Grimes register struct in_multi *inm; 633df8bae1dSRodney W. Grimes struct ifreq ifr; 634df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 635df8bae1dSRodney W. Grimes int s = splnet(); 636df8bae1dSRodney W. Grimes 637df8bae1dSRodney W. Grimes /* 638df8bae1dSRodney W. Grimes * See if address already in list. 639df8bae1dSRodney W. Grimes */ 640df8bae1dSRodney W. Grimes IN_LOOKUP_MULTI(*ap, ifp, inm); 641df8bae1dSRodney W. Grimes if (inm != NULL) { 642df8bae1dSRodney W. Grimes /* 643df8bae1dSRodney W. Grimes * Found it; just increment the reference count. 644df8bae1dSRodney W. Grimes */ 645df8bae1dSRodney W. Grimes ++inm->inm_refcount; 646df8bae1dSRodney W. Grimes } 647df8bae1dSRodney W. Grimes else { 648df8bae1dSRodney W. Grimes /* 649df8bae1dSRodney W. Grimes * New address; allocate a new multicast record 650df8bae1dSRodney W. Grimes * and link it into the interface's multicast list. 651df8bae1dSRodney W. Grimes */ 652df8bae1dSRodney W. Grimes inm = (struct in_multi *)malloc(sizeof(*inm), 653df8bae1dSRodney W. Grimes M_IPMADDR, M_NOWAIT); 654df8bae1dSRodney W. Grimes if (inm == NULL) { 655df8bae1dSRodney W. Grimes splx(s); 656df8bae1dSRodney W. Grimes return (NULL); 657df8bae1dSRodney W. Grimes } 658df8bae1dSRodney W. Grimes inm->inm_addr = *ap; 659df8bae1dSRodney W. Grimes inm->inm_ifp = ifp; 660df8bae1dSRodney W. Grimes inm->inm_refcount = 1; 661df8bae1dSRodney W. Grimes IFP_TO_IA(ifp, ia); 662df8bae1dSRodney W. Grimes if (ia == NULL) { 663df8bae1dSRodney W. Grimes free(inm, M_IPMADDR); 664df8bae1dSRodney W. Grimes splx(s); 665df8bae1dSRodney W. Grimes return (NULL); 666df8bae1dSRodney W. Grimes } 667df8bae1dSRodney W. Grimes inm->inm_ia = ia; 668ffa5b11aSGarrett Wollman ia->ia_ifa.ifa_refcnt++; /* gain a reference */ 669ffa5b11aSGarrett Wollman LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_entry); 670ffa5b11aSGarrett Wollman 671df8bae1dSRodney W. Grimes /* 672df8bae1dSRodney W. Grimes * Ask the network driver to update its multicast reception 673df8bae1dSRodney W. Grimes * filter appropriately for the new address. 674df8bae1dSRodney W. Grimes */ 675df8bae1dSRodney W. Grimes ((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET; 676df8bae1dSRodney W. Grimes ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr = *ap; 677df8bae1dSRodney W. Grimes if ((ifp->if_ioctl == NULL) || 678df8bae1dSRodney W. Grimes (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) { 679ffa5b11aSGarrett Wollman LIST_REMOVE(inm, inm_entry); 680ffa5b11aSGarrett Wollman IFAFREE(&ia->ia_ifa); /* release reference */ 681df8bae1dSRodney W. Grimes free(inm, M_IPMADDR); 682df8bae1dSRodney W. Grimes splx(s); 683df8bae1dSRodney W. Grimes return (NULL); 684df8bae1dSRodney W. Grimes } 685df8bae1dSRodney W. Grimes /* 686df8bae1dSRodney W. Grimes * Let IGMP know that we have joined a new IP multicast group. 687df8bae1dSRodney W. Grimes */ 688df8bae1dSRodney W. Grimes igmp_joingroup(inm); 689df8bae1dSRodney W. Grimes } 690df8bae1dSRodney W. Grimes splx(s); 691df8bae1dSRodney W. Grimes return (inm); 692df8bae1dSRodney W. Grimes } 693df8bae1dSRodney W. Grimes 694df8bae1dSRodney W. Grimes /* 695df8bae1dSRodney W. Grimes * Delete a multicast address record. 696df8bae1dSRodney W. Grimes */ 69726f9a767SRodney W. Grimes void 698df8bae1dSRodney W. Grimes in_delmulti(inm) 699df8bae1dSRodney W. Grimes register struct in_multi *inm; 700df8bae1dSRodney W. Grimes { 701df8bae1dSRodney W. Grimes struct ifreq ifr; 702df8bae1dSRodney W. Grimes int s = splnet(); 703df8bae1dSRodney W. Grimes 704df8bae1dSRodney W. Grimes if (--inm->inm_refcount == 0) { 705df8bae1dSRodney W. Grimes /* 706df8bae1dSRodney W. Grimes * No remaining claims to this record; let IGMP know that 707df8bae1dSRodney W. Grimes * we are leaving the multicast group. 708df8bae1dSRodney W. Grimes */ 709df8bae1dSRodney W. Grimes igmp_leavegroup(inm); 710df8bae1dSRodney W. Grimes /* 711df8bae1dSRodney W. Grimes * Unlink from list. 712df8bae1dSRodney W. Grimes */ 713ffa5b11aSGarrett Wollman LIST_REMOVE(inm, inm_entry); 714ffa5b11aSGarrett Wollman IFAFREE(&inm->inm_ia->ia_ifa); /* release reference */ 715ffa5b11aSGarrett Wollman 716df8bae1dSRodney W. Grimes /* 717df8bae1dSRodney W. Grimes * Notify the network driver to update its multicast reception 718df8bae1dSRodney W. Grimes * filter. 719df8bae1dSRodney W. Grimes */ 720df8bae1dSRodney W. Grimes ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET; 721df8bae1dSRodney W. Grimes ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr = 722df8bae1dSRodney W. Grimes inm->inm_addr; 723df8bae1dSRodney W. Grimes (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI, 724df8bae1dSRodney W. Grimes (caddr_t)&ifr); 725df8bae1dSRodney W. Grimes free(inm, M_IPMADDR); 726df8bae1dSRodney W. Grimes } 727df8bae1dSRodney W. Grimes splx(s); 728df8bae1dSRodney W. Grimes } 729