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 * 33df8bae1dSRodney W. Grimes * @(#)in.c 8.2 (Berkeley) 11/15/93 34df8bae1dSRodney W. Grimes */ 35df8bae1dSRodney W. Grimes 36df8bae1dSRodney W. Grimes #include <sys/param.h> 3726f9a767SRodney W. Grimes #include <sys/systm.h> 38df8bae1dSRodney W. Grimes #include <sys/ioctl.h> 39df8bae1dSRodney W. Grimes #include <sys/errno.h> 40df8bae1dSRodney W. Grimes #include <sys/malloc.h> 41df8bae1dSRodney W. Grimes #include <sys/socket.h> 42df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 43df8bae1dSRodney W. Grimes 44df8bae1dSRodney W. Grimes #include <net/if.h> 45df8bae1dSRodney W. Grimes #include <net/route.h> 46df8bae1dSRodney W. Grimes 47df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 48df8bae1dSRodney W. Grimes #include <netinet/in.h> 49df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 50df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 51df8bae1dSRodney W. Grimes 52df8bae1dSRodney W. Grimes #ifdef INET 53df8bae1dSRodney W. Grimes /* 54df8bae1dSRodney W. Grimes * Return the network number from an internet address. 55df8bae1dSRodney W. Grimes */ 56df8bae1dSRodney W. Grimes u_long 57df8bae1dSRodney W. Grimes in_netof(in) 58df8bae1dSRodney W. Grimes struct in_addr in; 59df8bae1dSRodney W. Grimes { 60df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 61df8bae1dSRodney W. Grimes register u_long net; 62df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) 65df8bae1dSRodney W. Grimes net = i & IN_CLASSA_NET; 66df8bae1dSRodney W. Grimes else if (IN_CLASSB(i)) 67df8bae1dSRodney W. Grimes net = i & IN_CLASSB_NET; 68df8bae1dSRodney W. Grimes else if (IN_CLASSC(i)) 69df8bae1dSRodney W. Grimes net = i & IN_CLASSC_NET; 70df8bae1dSRodney W. Grimes else if (IN_CLASSD(i)) 71df8bae1dSRodney W. Grimes net = i & IN_CLASSD_NET; 72df8bae1dSRodney W. Grimes else 73df8bae1dSRodney W. Grimes return (0); 74df8bae1dSRodney W. Grimes 75df8bae1dSRodney W. Grimes /* 76df8bae1dSRodney W. Grimes * Check whether network is a subnet; 77df8bae1dSRodney W. Grimes * if so, return subnet number. 78df8bae1dSRodney W. Grimes */ 79df8bae1dSRodney W. Grimes for (ia = in_ifaddr; ia; ia = ia->ia_next) 80df8bae1dSRodney W. Grimes if (net == ia->ia_net) 81df8bae1dSRodney W. Grimes return (i & ia->ia_subnetmask); 82df8bae1dSRodney W. Grimes return (net); 83df8bae1dSRodney W. Grimes } 84df8bae1dSRodney W. Grimes 85df8bae1dSRodney W. Grimes #ifndef SUBNETSARELOCAL 86df8bae1dSRodney W. Grimes #define SUBNETSARELOCAL 1 87df8bae1dSRodney W. Grimes #endif 88df8bae1dSRodney W. Grimes int subnetsarelocal = SUBNETSARELOCAL; 89df8bae1dSRodney W. Grimes /* 90df8bae1dSRodney W. Grimes * Return 1 if an internet address is for a ``local'' host 91df8bae1dSRodney W. Grimes * (one to which we have a connection). If subnetsarelocal 92df8bae1dSRodney W. Grimes * is true, this includes other subnets of the local net. 93df8bae1dSRodney W. Grimes * Otherwise, it includes only the directly-connected (sub)nets. 94df8bae1dSRodney W. Grimes */ 9526f9a767SRodney W. Grimes int 96df8bae1dSRodney W. Grimes in_localaddr(in) 97df8bae1dSRodney W. Grimes struct in_addr in; 98df8bae1dSRodney W. Grimes { 99df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 100df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 101df8bae1dSRodney W. Grimes 102df8bae1dSRodney W. Grimes if (subnetsarelocal) { 103df8bae1dSRodney W. Grimes for (ia = in_ifaddr; ia; ia = ia->ia_next) 104df8bae1dSRodney W. Grimes if ((i & ia->ia_netmask) == ia->ia_net) 105df8bae1dSRodney W. Grimes return (1); 106df8bae1dSRodney W. Grimes } else { 107df8bae1dSRodney W. Grimes for (ia = in_ifaddr; ia; ia = ia->ia_next) 108df8bae1dSRodney W. Grimes if ((i & ia->ia_subnetmask) == ia->ia_subnet) 109df8bae1dSRodney W. Grimes return (1); 110df8bae1dSRodney W. Grimes } 111df8bae1dSRodney W. Grimes return (0); 112df8bae1dSRodney W. Grimes } 113df8bae1dSRodney W. Grimes 114df8bae1dSRodney W. Grimes /* 115df8bae1dSRodney W. Grimes * Determine whether an IP address is in a reserved set of addresses 116df8bae1dSRodney W. Grimes * that may not be forwarded, or whether datagrams to that destination 117df8bae1dSRodney W. Grimes * may be forwarded. 118df8bae1dSRodney W. Grimes */ 11926f9a767SRodney W. Grimes int 120df8bae1dSRodney W. Grimes in_canforward(in) 121df8bae1dSRodney W. Grimes struct in_addr in; 122df8bae1dSRodney W. Grimes { 123df8bae1dSRodney W. Grimes register u_long i = ntohl(in.s_addr); 124df8bae1dSRodney W. Grimes register u_long net; 125df8bae1dSRodney W. Grimes 126df8bae1dSRodney W. Grimes if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) 127df8bae1dSRodney W. Grimes return (0); 128df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) { 129df8bae1dSRodney W. Grimes net = i & IN_CLASSA_NET; 130df8bae1dSRodney W. Grimes if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 131df8bae1dSRodney W. Grimes return (0); 132df8bae1dSRodney W. Grimes } 133df8bae1dSRodney W. Grimes return (1); 134df8bae1dSRodney W. Grimes } 135df8bae1dSRodney W. Grimes 136df8bae1dSRodney W. Grimes /* 137df8bae1dSRodney W. Grimes * Trim a mask in a sockaddr 138df8bae1dSRodney W. Grimes */ 139df8bae1dSRodney W. Grimes void 140df8bae1dSRodney W. Grimes in_socktrim(ap) 141df8bae1dSRodney W. Grimes struct sockaddr_in *ap; 142df8bae1dSRodney W. Grimes { 143df8bae1dSRodney W. Grimes register char *cplim = (char *) &ap->sin_addr; 144df8bae1dSRodney W. Grimes register char *cp = (char *) (&ap->sin_addr + 1); 145df8bae1dSRodney W. Grimes 146df8bae1dSRodney W. Grimes ap->sin_len = 0; 147df8bae1dSRodney W. Grimes while (--cp > cplim) 148df8bae1dSRodney W. Grimes if (*cp) { 149df8bae1dSRodney W. Grimes (ap)->sin_len = cp - (char *) (ap) + 1; 150df8bae1dSRodney W. Grimes break; 151df8bae1dSRodney W. Grimes } 152df8bae1dSRodney W. Grimes } 153df8bae1dSRodney W. Grimes 154df8bae1dSRodney W. Grimes int in_interfaces; /* number of external internet interfaces */ 155df8bae1dSRodney W. Grimes extern struct ifnet loif; 156df8bae1dSRodney W. Grimes 157df8bae1dSRodney W. Grimes /* 158df8bae1dSRodney W. Grimes * Generic internet control operations (ioctl's). 159df8bae1dSRodney W. Grimes * Ifp is 0 if not an interface-specific ioctl. 160df8bae1dSRodney W. Grimes */ 161df8bae1dSRodney W. Grimes /* ARGSUSED */ 16226f9a767SRodney W. Grimes int 163df8bae1dSRodney W. Grimes in_control(so, cmd, data, ifp) 164df8bae1dSRodney W. Grimes struct socket *so; 165df8bae1dSRodney W. Grimes int cmd; 166df8bae1dSRodney W. Grimes caddr_t data; 167df8bae1dSRodney W. Grimes register struct ifnet *ifp; 168df8bae1dSRodney W. Grimes { 169df8bae1dSRodney W. Grimes register struct ifreq *ifr = (struct ifreq *)data; 170df8bae1dSRodney W. Grimes register struct in_ifaddr *ia = 0; 171df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 172df8bae1dSRodney W. Grimes struct in_ifaddr *oia; 173df8bae1dSRodney W. Grimes struct in_aliasreq *ifra = (struct in_aliasreq *)data; 174df8bae1dSRodney W. Grimes struct sockaddr_in oldaddr; 175df8bae1dSRodney W. Grimes int error, hostIsNew, maskIsNew; 176df8bae1dSRodney W. Grimes u_long i; 177df8bae1dSRodney W. Grimes 178df8bae1dSRodney W. Grimes /* 179df8bae1dSRodney W. Grimes * Find address for this interface, if it exists. 180df8bae1dSRodney W. Grimes */ 181df8bae1dSRodney W. Grimes if (ifp) 182df8bae1dSRodney W. Grimes for (ia = in_ifaddr; ia; ia = ia->ia_next) 183df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp) 184df8bae1dSRodney W. Grimes break; 185df8bae1dSRodney W. Grimes 186df8bae1dSRodney W. Grimes switch (cmd) { 187df8bae1dSRodney W. Grimes 188df8bae1dSRodney W. Grimes case SIOCAIFADDR: 189df8bae1dSRodney W. Grimes case SIOCDIFADDR: 190df8bae1dSRodney W. Grimes if (ifra->ifra_addr.sin_family == AF_INET) 191df8bae1dSRodney W. Grimes for (oia = ia; ia; ia = ia->ia_next) { 192df8bae1dSRodney W. Grimes if (ia->ia_ifp == ifp && 193df8bae1dSRodney W. Grimes ia->ia_addr.sin_addr.s_addr == 194df8bae1dSRodney W. Grimes ifra->ifra_addr.sin_addr.s_addr) 195df8bae1dSRodney W. Grimes break; 196df8bae1dSRodney W. Grimes } 197df8bae1dSRodney W. Grimes if (cmd == SIOCDIFADDR && ia == 0) 198df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 199df8bae1dSRodney W. Grimes /* FALLTHROUGH */ 200df8bae1dSRodney W. Grimes case SIOCSIFADDR: 201df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 202df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 203df8bae1dSRodney W. Grimes if ((so->so_state & SS_PRIV) == 0) 204df8bae1dSRodney W. Grimes return (EPERM); 205df8bae1dSRodney W. Grimes 206df8bae1dSRodney W. Grimes if (ifp == 0) 207df8bae1dSRodney W. Grimes panic("in_control"); 208df8bae1dSRodney W. Grimes if (ia == (struct in_ifaddr *)0) { 209df8bae1dSRodney W. Grimes oia = (struct in_ifaddr *) 210df8bae1dSRodney W. Grimes malloc(sizeof *oia, M_IFADDR, M_WAITOK); 211df8bae1dSRodney W. Grimes if (oia == (struct in_ifaddr *)NULL) 212df8bae1dSRodney W. Grimes return (ENOBUFS); 213df8bae1dSRodney W. Grimes bzero((caddr_t)oia, sizeof *oia); 214df8bae1dSRodney W. Grimes if (ia = in_ifaddr) { 215df8bae1dSRodney W. Grimes for ( ; ia->ia_next; ia = ia->ia_next) 216df8bae1dSRodney W. Grimes continue; 217df8bae1dSRodney W. Grimes ia->ia_next = oia; 218df8bae1dSRodney W. Grimes } else 219df8bae1dSRodney W. Grimes in_ifaddr = oia; 220df8bae1dSRodney W. Grimes ia = oia; 221df8bae1dSRodney W. Grimes if (ifa = ifp->if_addrlist) { 222df8bae1dSRodney W. Grimes for ( ; ifa->ifa_next; ifa = ifa->ifa_next) 223df8bae1dSRodney W. Grimes continue; 224df8bae1dSRodney W. Grimes ifa->ifa_next = (struct ifaddr *) ia; 225df8bae1dSRodney W. Grimes } else 226df8bae1dSRodney W. Grimes ifp->if_addrlist = (struct ifaddr *) ia; 227df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 228df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr 229df8bae1dSRodney W. Grimes = (struct sockaddr *)&ia->ia_dstaddr; 230df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_netmask 231df8bae1dSRodney W. Grimes = (struct sockaddr *)&ia->ia_sockmask; 232df8bae1dSRodney W. Grimes ia->ia_sockmask.sin_len = 8; 233df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 234df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 235df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_family = AF_INET; 236df8bae1dSRodney W. Grimes } 237df8bae1dSRodney W. Grimes ia->ia_ifp = ifp; 238df8bae1dSRodney W. Grimes if (ifp != &loif) 239df8bae1dSRodney W. Grimes in_interfaces++; 240df8bae1dSRodney W. Grimes } 241df8bae1dSRodney W. Grimes break; 242df8bae1dSRodney W. Grimes 243df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 244df8bae1dSRodney W. Grimes if ((so->so_state & SS_PRIV) == 0) 245df8bae1dSRodney W. Grimes return (EPERM); 246df8bae1dSRodney W. Grimes /* FALLTHROUGH */ 247df8bae1dSRodney W. Grimes 248df8bae1dSRodney W. Grimes case SIOCGIFADDR: 249df8bae1dSRodney W. Grimes case SIOCGIFNETMASK: 250df8bae1dSRodney W. Grimes case SIOCGIFDSTADDR: 251df8bae1dSRodney W. Grimes case SIOCGIFBRDADDR: 252df8bae1dSRodney W. Grimes if (ia == (struct in_ifaddr *)0) 253df8bae1dSRodney W. Grimes return (EADDRNOTAVAIL); 254df8bae1dSRodney W. Grimes break; 255df8bae1dSRodney W. Grimes } 256df8bae1dSRodney W. Grimes switch (cmd) { 257df8bae1dSRodney W. Grimes 258df8bae1dSRodney W. Grimes case SIOCGIFADDR: 259df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 260df8bae1dSRodney W. Grimes break; 261df8bae1dSRodney W. Grimes 262df8bae1dSRodney W. Grimes case SIOCGIFBRDADDR: 263df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 264df8bae1dSRodney W. Grimes return (EINVAL); 265df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 266df8bae1dSRodney W. Grimes break; 267df8bae1dSRodney W. Grimes 268df8bae1dSRodney W. Grimes case SIOCGIFDSTADDR: 269df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 270df8bae1dSRodney W. Grimes return (EINVAL); 271df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 272df8bae1dSRodney W. Grimes break; 273df8bae1dSRodney W. Grimes 274df8bae1dSRodney W. Grimes case SIOCGIFNETMASK: 275df8bae1dSRodney W. Grimes *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 276df8bae1dSRodney W. Grimes break; 277df8bae1dSRodney W. Grimes 278df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 279df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 280df8bae1dSRodney W. Grimes return (EINVAL); 281df8bae1dSRodney W. Grimes oldaddr = ia->ia_dstaddr; 282df8bae1dSRodney W. Grimes ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 283df8bae1dSRodney W. Grimes if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 284df8bae1dSRodney W. Grimes (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 285df8bae1dSRodney W. Grimes ia->ia_dstaddr = oldaddr; 286df8bae1dSRodney W. Grimes return (error); 287df8bae1dSRodney W. Grimes } 288df8bae1dSRodney W. Grimes if (ia->ia_flags & IFA_ROUTE) { 289df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 290df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 291df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr = 292df8bae1dSRodney W. Grimes (struct sockaddr *)&ia->ia_dstaddr; 293df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 294df8bae1dSRodney W. Grimes } 295df8bae1dSRodney W. Grimes break; 296df8bae1dSRodney W. Grimes 297df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 298df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 299df8bae1dSRodney W. Grimes return (EINVAL); 300df8bae1dSRodney W. Grimes ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 301df8bae1dSRodney W. Grimes break; 302df8bae1dSRodney W. Grimes 303df8bae1dSRodney W. Grimes case SIOCSIFADDR: 304df8bae1dSRodney W. Grimes return (in_ifinit(ifp, ia, 305df8bae1dSRodney W. Grimes (struct sockaddr_in *) &ifr->ifr_addr, 1)); 306df8bae1dSRodney W. Grimes 307df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 308df8bae1dSRodney W. Grimes i = ifra->ifra_addr.sin_addr.s_addr; 309df8bae1dSRodney W. Grimes ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i); 310df8bae1dSRodney W. Grimes break; 311df8bae1dSRodney W. Grimes 312df8bae1dSRodney W. Grimes case SIOCAIFADDR: 313df8bae1dSRodney W. Grimes maskIsNew = 0; 314df8bae1dSRodney W. Grimes hostIsNew = 1; 315df8bae1dSRodney W. Grimes error = 0; 316df8bae1dSRodney W. Grimes if (ia->ia_addr.sin_family == AF_INET) { 317df8bae1dSRodney W. Grimes if (ifra->ifra_addr.sin_len == 0) { 318df8bae1dSRodney W. Grimes ifra->ifra_addr = ia->ia_addr; 319df8bae1dSRodney W. Grimes hostIsNew = 0; 320df8bae1dSRodney W. Grimes } else if (ifra->ifra_addr.sin_addr.s_addr == 321df8bae1dSRodney W. Grimes ia->ia_addr.sin_addr.s_addr) 322df8bae1dSRodney W. Grimes hostIsNew = 0; 323df8bae1dSRodney W. Grimes } 324df8bae1dSRodney W. Grimes if (ifra->ifra_mask.sin_len) { 325df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 326df8bae1dSRodney W. Grimes ia->ia_sockmask = ifra->ifra_mask; 327df8bae1dSRodney W. Grimes ia->ia_subnetmask = 328df8bae1dSRodney W. Grimes ntohl(ia->ia_sockmask.sin_addr.s_addr); 329df8bae1dSRodney W. Grimes maskIsNew = 1; 330df8bae1dSRodney W. Grimes } 331df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_POINTOPOINT) && 332df8bae1dSRodney W. Grimes (ifra->ifra_dstaddr.sin_family == AF_INET)) { 333df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 334df8bae1dSRodney W. Grimes ia->ia_dstaddr = ifra->ifra_dstaddr; 335df8bae1dSRodney W. Grimes maskIsNew = 1; /* We lie; but the effect's the same */ 336df8bae1dSRodney W. Grimes } 337df8bae1dSRodney W. Grimes if (ifra->ifra_addr.sin_family == AF_INET && 338df8bae1dSRodney W. Grimes (hostIsNew || maskIsNew)) 339df8bae1dSRodney W. Grimes error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 340df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) && 341df8bae1dSRodney W. Grimes (ifra->ifra_broadaddr.sin_family == AF_INET)) 342df8bae1dSRodney W. Grimes ia->ia_broadaddr = ifra->ifra_broadaddr; 343df8bae1dSRodney W. Grimes return (error); 344df8bae1dSRodney W. Grimes 345df8bae1dSRodney W. Grimes case SIOCDIFADDR: 346df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 347df8bae1dSRodney W. Grimes if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia) 348df8bae1dSRodney W. Grimes ifp->if_addrlist = ifa->ifa_next; 349df8bae1dSRodney W. Grimes else { 350df8bae1dSRodney W. Grimes while (ifa->ifa_next && 351df8bae1dSRodney W. Grimes (ifa->ifa_next != (struct ifaddr *)ia)) 352df8bae1dSRodney W. Grimes ifa = ifa->ifa_next; 353df8bae1dSRodney W. Grimes if (ifa->ifa_next) 354df8bae1dSRodney W. Grimes ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next; 355df8bae1dSRodney W. Grimes else 356df8bae1dSRodney W. Grimes printf("Couldn't unlink inifaddr from ifp\n"); 357df8bae1dSRodney W. Grimes } 358df8bae1dSRodney W. Grimes oia = ia; 359df8bae1dSRodney W. Grimes if (oia == (ia = in_ifaddr)) 360df8bae1dSRodney W. Grimes in_ifaddr = ia->ia_next; 361df8bae1dSRodney W. Grimes else { 362df8bae1dSRodney W. Grimes while (ia->ia_next && (ia->ia_next != oia)) 363df8bae1dSRodney W. Grimes ia = ia->ia_next; 364df8bae1dSRodney W. Grimes if (ia->ia_next) 365df8bae1dSRodney W. Grimes ia->ia_next = oia->ia_next; 366df8bae1dSRodney W. Grimes else 367df8bae1dSRodney W. Grimes printf("Didn't unlink inifadr from list\n"); 368df8bae1dSRodney W. Grimes } 369df8bae1dSRodney W. Grimes IFAFREE((&oia->ia_ifa)); 370df8bae1dSRodney W. Grimes break; 371df8bae1dSRodney W. Grimes 372df8bae1dSRodney W. Grimes default: 373df8bae1dSRodney W. Grimes if (ifp == 0 || ifp->if_ioctl == 0) 374df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 375df8bae1dSRodney W. Grimes return ((*ifp->if_ioctl)(ifp, cmd, data)); 376df8bae1dSRodney W. Grimes } 377df8bae1dSRodney W. Grimes return (0); 378df8bae1dSRodney W. Grimes } 379df8bae1dSRodney W. Grimes 380df8bae1dSRodney W. Grimes /* 381df8bae1dSRodney W. Grimes * Delete any existing route for an interface. 382df8bae1dSRodney W. Grimes */ 383df8bae1dSRodney W. Grimes void 384df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia) 385df8bae1dSRodney W. Grimes register struct ifnet *ifp; 386df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 387df8bae1dSRodney W. Grimes { 388df8bae1dSRodney W. Grimes 389df8bae1dSRodney W. Grimes if ((ia->ia_flags & IFA_ROUTE) == 0) 390df8bae1dSRodney W. Grimes return; 391df8bae1dSRodney W. Grimes if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 392df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 393df8bae1dSRodney W. Grimes else 394df8bae1dSRodney W. Grimes rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 395df8bae1dSRodney W. Grimes ia->ia_flags &= ~IFA_ROUTE; 396df8bae1dSRodney W. Grimes } 397df8bae1dSRodney W. Grimes 398df8bae1dSRodney W. Grimes /* 399df8bae1dSRodney W. Grimes * Initialize an interface's internet address 400df8bae1dSRodney W. Grimes * and routing table entry. 401df8bae1dSRodney W. Grimes */ 40226f9a767SRodney W. Grimes int 403df8bae1dSRodney W. Grimes in_ifinit(ifp, ia, sin, scrub) 404df8bae1dSRodney W. Grimes register struct ifnet *ifp; 405df8bae1dSRodney W. Grimes register struct in_ifaddr *ia; 406df8bae1dSRodney W. Grimes struct sockaddr_in *sin; 407df8bae1dSRodney W. Grimes int scrub; 408df8bae1dSRodney W. Grimes { 409df8bae1dSRodney W. Grimes register u_long i = ntohl(sin->sin_addr.s_addr); 410df8bae1dSRodney W. Grimes struct sockaddr_in oldaddr; 411df8bae1dSRodney W. Grimes int s = splimp(), flags = RTF_UP, error, ether_output(); 412df8bae1dSRodney W. Grimes 413df8bae1dSRodney W. Grimes oldaddr = ia->ia_addr; 414df8bae1dSRodney W. Grimes ia->ia_addr = *sin; 415df8bae1dSRodney W. Grimes /* 416df8bae1dSRodney W. Grimes * Give the interface a chance to initialize 417df8bae1dSRodney W. Grimes * if this is its first address, 418df8bae1dSRodney W. Grimes * and to validate the address if necessary. 419df8bae1dSRodney W. Grimes */ 420df8bae1dSRodney W. Grimes if (ifp->if_ioctl && 421df8bae1dSRodney W. Grimes (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 422df8bae1dSRodney W. Grimes splx(s); 423df8bae1dSRodney W. Grimes ia->ia_addr = oldaddr; 424df8bae1dSRodney W. Grimes return (error); 425df8bae1dSRodney W. Grimes } 426df8bae1dSRodney W. Grimes if (ifp->if_output == ether_output) { /* XXX: Another Kludge */ 427df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_rtrequest = arp_rtrequest; 428df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_flags |= RTF_CLONING; 429df8bae1dSRodney W. Grimes } 430df8bae1dSRodney W. Grimes splx(s); 431df8bae1dSRodney W. Grimes if (scrub) { 432df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 433df8bae1dSRodney W. Grimes in_ifscrub(ifp, ia); 434df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 435df8bae1dSRodney W. Grimes } 436df8bae1dSRodney W. Grimes if (IN_CLASSA(i)) 437df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSA_NET; 438df8bae1dSRodney W. Grimes else if (IN_CLASSB(i)) 439df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSB_NET; 440df8bae1dSRodney W. Grimes else 441df8bae1dSRodney W. Grimes ia->ia_netmask = IN_CLASSC_NET; 442df8bae1dSRodney W. Grimes /* 443df8bae1dSRodney W. Grimes * The subnet mask usually includes at least the standard network part, 444df8bae1dSRodney W. Grimes * but may may be smaller in the case of supernetting. 445df8bae1dSRodney W. Grimes * If it is set, we believe it. 446df8bae1dSRodney W. Grimes */ 447df8bae1dSRodney W. Grimes if (ia->ia_subnetmask == 0) { 448df8bae1dSRodney W. Grimes ia->ia_subnetmask = ia->ia_netmask; 449df8bae1dSRodney W. Grimes ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 450df8bae1dSRodney W. Grimes } else 451df8bae1dSRodney W. Grimes ia->ia_netmask &= ia->ia_subnetmask; 452df8bae1dSRodney W. Grimes ia->ia_net = i & ia->ia_netmask; 453df8bae1dSRodney W. Grimes ia->ia_subnet = i & ia->ia_subnetmask; 454df8bae1dSRodney W. Grimes in_socktrim(&ia->ia_sockmask); 455df8bae1dSRodney W. Grimes /* 456df8bae1dSRodney W. Grimes * Add route for the network. 457df8bae1dSRodney W. Grimes */ 458df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_metric = ifp->if_metric; 459df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_BROADCAST) { 460df8bae1dSRodney W. Grimes ia->ia_broadaddr.sin_addr.s_addr = 461df8bae1dSRodney W. Grimes htonl(ia->ia_subnet | ~ia->ia_subnetmask); 462df8bae1dSRodney W. Grimes ia->ia_netbroadcast.s_addr = 463df8bae1dSRodney W. Grimes htonl(ia->ia_net | ~ ia->ia_netmask); 464df8bae1dSRodney W. Grimes } else if (ifp->if_flags & IFF_LOOPBACK) { 465df8bae1dSRodney W. Grimes ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 466df8bae1dSRodney W. Grimes flags |= RTF_HOST; 467df8bae1dSRodney W. Grimes } else if (ifp->if_flags & IFF_POINTOPOINT) { 468df8bae1dSRodney W. Grimes if (ia->ia_dstaddr.sin_family != AF_INET) 469df8bae1dSRodney W. Grimes return (0); 470df8bae1dSRodney W. Grimes flags |= RTF_HOST; 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) 473df8bae1dSRodney W. Grimes ia->ia_flags |= IFA_ROUTE; 474df8bae1dSRodney W. Grimes /* 475df8bae1dSRodney W. Grimes * If the interface supports multicast, join the "all hosts" 476df8bae1dSRodney W. Grimes * multicast group on that interface. 477df8bae1dSRodney W. Grimes */ 478df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_MULTICAST) { 479df8bae1dSRodney W. Grimes struct in_addr addr; 480df8bae1dSRodney W. Grimes 481df8bae1dSRodney W. Grimes addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 482df8bae1dSRodney W. Grimes in_addmulti(&addr, ifp); 483df8bae1dSRodney W. Grimes } 484df8bae1dSRodney W. Grimes return (error); 485df8bae1dSRodney W. Grimes } 486df8bae1dSRodney W. Grimes 487df8bae1dSRodney W. Grimes 488df8bae1dSRodney W. Grimes /* 489df8bae1dSRodney W. Grimes * Return 1 if the address might be a local broadcast address. 490df8bae1dSRodney W. Grimes */ 49126f9a767SRodney W. Grimes int 492df8bae1dSRodney W. Grimes in_broadcast(in, ifp) 493df8bae1dSRodney W. Grimes struct in_addr in; 494df8bae1dSRodney W. Grimes struct ifnet *ifp; 495df8bae1dSRodney W. Grimes { 496df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 497df8bae1dSRodney W. Grimes u_long t; 498df8bae1dSRodney W. Grimes 499df8bae1dSRodney W. Grimes if (in.s_addr == INADDR_BROADCAST || 500df8bae1dSRodney W. Grimes in.s_addr == INADDR_ANY) 501df8bae1dSRodney W. Grimes return 1; 502df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) == 0) 503df8bae1dSRodney W. Grimes return 0; 504df8bae1dSRodney W. Grimes t = ntohl(in.s_addr); 505df8bae1dSRodney W. Grimes /* 506df8bae1dSRodney W. Grimes * Look through the list of addresses for a match 507df8bae1dSRodney W. Grimes * with a broadcast address. 508df8bae1dSRodney W. Grimes */ 509df8bae1dSRodney W. Grimes #define ia ((struct in_ifaddr *)ifa) 510df8bae1dSRodney W. Grimes for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 511df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family == AF_INET && 512df8bae1dSRodney W. Grimes (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 513df8bae1dSRodney W. Grimes in.s_addr == ia->ia_netbroadcast.s_addr || 514df8bae1dSRodney W. Grimes /* 515df8bae1dSRodney W. Grimes * Check for old-style (host 0) broadcast. 516df8bae1dSRodney W. Grimes */ 517df8bae1dSRodney W. Grimes t == ia->ia_subnet || t == ia->ia_net)) 518df8bae1dSRodney W. Grimes return 1; 519df8bae1dSRodney W. Grimes return (0); 520df8bae1dSRodney W. Grimes #undef ia 521df8bae1dSRodney W. Grimes } 522df8bae1dSRodney W. Grimes /* 523df8bae1dSRodney W. Grimes * Add an address to the list of IP multicast addresses for a given interface. 524df8bae1dSRodney W. Grimes */ 525df8bae1dSRodney W. Grimes struct in_multi * 526df8bae1dSRodney W. Grimes in_addmulti(ap, ifp) 527df8bae1dSRodney W. Grimes register struct in_addr *ap; 528df8bae1dSRodney W. Grimes register struct ifnet *ifp; 529df8bae1dSRodney W. Grimes { 530df8bae1dSRodney W. Grimes register struct in_multi *inm; 531df8bae1dSRodney W. Grimes struct ifreq ifr; 532df8bae1dSRodney W. Grimes struct in_ifaddr *ia; 533df8bae1dSRodney W. Grimes int s = splnet(); 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes /* 536df8bae1dSRodney W. Grimes * See if address already in list. 537df8bae1dSRodney W. Grimes */ 538df8bae1dSRodney W. Grimes IN_LOOKUP_MULTI(*ap, ifp, inm); 539df8bae1dSRodney W. Grimes if (inm != NULL) { 540df8bae1dSRodney W. Grimes /* 541df8bae1dSRodney W. Grimes * Found it; just increment the reference count. 542df8bae1dSRodney W. Grimes */ 543df8bae1dSRodney W. Grimes ++inm->inm_refcount; 544df8bae1dSRodney W. Grimes } 545df8bae1dSRodney W. Grimes else { 546df8bae1dSRodney W. Grimes /* 547df8bae1dSRodney W. Grimes * New address; allocate a new multicast record 548df8bae1dSRodney W. Grimes * and link it into the interface's multicast list. 549df8bae1dSRodney W. Grimes */ 550df8bae1dSRodney W. Grimes inm = (struct in_multi *)malloc(sizeof(*inm), 551df8bae1dSRodney W. Grimes M_IPMADDR, M_NOWAIT); 552df8bae1dSRodney W. Grimes if (inm == NULL) { 553df8bae1dSRodney W. Grimes splx(s); 554df8bae1dSRodney W. Grimes return (NULL); 555df8bae1dSRodney W. Grimes } 556df8bae1dSRodney W. Grimes inm->inm_addr = *ap; 557df8bae1dSRodney W. Grimes inm->inm_ifp = ifp; 558df8bae1dSRodney W. Grimes inm->inm_refcount = 1; 559df8bae1dSRodney W. Grimes IFP_TO_IA(ifp, ia); 560df8bae1dSRodney W. Grimes if (ia == NULL) { 561df8bae1dSRodney W. Grimes free(inm, M_IPMADDR); 562df8bae1dSRodney W. Grimes splx(s); 563df8bae1dSRodney W. Grimes return (NULL); 564df8bae1dSRodney W. Grimes } 565df8bae1dSRodney W. Grimes inm->inm_ia = ia; 566df8bae1dSRodney W. Grimes inm->inm_next = ia->ia_multiaddrs; 567df8bae1dSRodney W. Grimes ia->ia_multiaddrs = inm; 568df8bae1dSRodney W. Grimes /* 569df8bae1dSRodney W. Grimes * Ask the network driver to update its multicast reception 570df8bae1dSRodney W. Grimes * filter appropriately for the new address. 571df8bae1dSRodney W. Grimes */ 572df8bae1dSRodney W. Grimes ((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET; 573df8bae1dSRodney W. Grimes ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr = *ap; 574df8bae1dSRodney W. Grimes if ((ifp->if_ioctl == NULL) || 575df8bae1dSRodney W. Grimes (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) { 576df8bae1dSRodney W. Grimes ia->ia_multiaddrs = inm->inm_next; 577df8bae1dSRodney W. Grimes free(inm, M_IPMADDR); 578df8bae1dSRodney W. Grimes splx(s); 579df8bae1dSRodney W. Grimes return (NULL); 580df8bae1dSRodney W. Grimes } 581df8bae1dSRodney W. Grimes /* 582df8bae1dSRodney W. Grimes * Let IGMP know that we have joined a new IP multicast group. 583df8bae1dSRodney W. Grimes */ 584df8bae1dSRodney W. Grimes igmp_joingroup(inm); 585df8bae1dSRodney W. Grimes } 586df8bae1dSRodney W. Grimes splx(s); 587df8bae1dSRodney W. Grimes return (inm); 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes 590df8bae1dSRodney W. Grimes /* 591df8bae1dSRodney W. Grimes * Delete a multicast address record. 592df8bae1dSRodney W. Grimes */ 59326f9a767SRodney W. Grimes void 594df8bae1dSRodney W. Grimes in_delmulti(inm) 595df8bae1dSRodney W. Grimes register struct in_multi *inm; 596df8bae1dSRodney W. Grimes { 597df8bae1dSRodney W. Grimes register struct in_multi **p; 598df8bae1dSRodney W. Grimes struct ifreq ifr; 599df8bae1dSRodney W. Grimes int s = splnet(); 600df8bae1dSRodney W. Grimes 601df8bae1dSRodney W. Grimes if (--inm->inm_refcount == 0) { 602df8bae1dSRodney W. Grimes /* 603df8bae1dSRodney W. Grimes * No remaining claims to this record; let IGMP know that 604df8bae1dSRodney W. Grimes * we are leaving the multicast group. 605df8bae1dSRodney W. Grimes */ 606df8bae1dSRodney W. Grimes igmp_leavegroup(inm); 607df8bae1dSRodney W. Grimes /* 608df8bae1dSRodney W. Grimes * Unlink from list. 609df8bae1dSRodney W. Grimes */ 610df8bae1dSRodney W. Grimes for (p = &inm->inm_ia->ia_multiaddrs; 611df8bae1dSRodney W. Grimes *p != inm; 612df8bae1dSRodney W. Grimes p = &(*p)->inm_next) 613df8bae1dSRodney W. Grimes continue; 614df8bae1dSRodney W. Grimes *p = (*p)->inm_next; 615df8bae1dSRodney W. Grimes /* 616df8bae1dSRodney W. Grimes * Notify the network driver to update its multicast reception 617df8bae1dSRodney W. Grimes * filter. 618df8bae1dSRodney W. Grimes */ 619df8bae1dSRodney W. Grimes ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET; 620df8bae1dSRodney W. Grimes ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr = 621df8bae1dSRodney W. Grimes inm->inm_addr; 622df8bae1dSRodney W. Grimes (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI, 623df8bae1dSRodney W. Grimes (caddr_t)&ifr); 624df8bae1dSRodney W. Grimes free(inm, M_IPMADDR); 625df8bae1dSRodney W. Grimes } 626df8bae1dSRodney W. Grimes splx(s); 627df8bae1dSRodney W. Grimes } 628df8bae1dSRodney W. Grimes #endif 629