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