1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1980, 1986, 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 * @(#)if.c 8.3 (Berkeley) 1/4/94 347e2a6151SJulian Elischer * $Id: if.c,v 1.49 1997/07/07 17:36:06 julian Exp $ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 382ee45d7dSDavid Greenman #include <sys/queue.h> 39df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 40df8bae1dSRodney W. Grimes #include <sys/systm.h> 41df8bae1dSRodney W. Grimes #include <sys/proc.h> 42df8bae1dSRodney W. Grimes #include <sys/socket.h> 43df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 44df8bae1dSRodney W. Grimes #include <sys/protosw.h> 45df8bae1dSRodney W. Grimes #include <sys/kernel.h> 4651a53488SBruce Evans #include <sys/sockio.h> 472624cf89SGarrett Wollman #include <sys/errno.h> 48963e4c2aSGarrett Wollman #include <sys/syslog.h> 49602d513cSGarrett Wollman #include <sys/sysctl.h> 50df8bae1dSRodney W. Grimes 51df8bae1dSRodney W. Grimes #include <net/if.h> 52df8bae1dSRodney W. Grimes #include <net/if_dl.h> 53df8bae1dSRodney W. Grimes #include <net/if_types.h> 549448326fSPoul-Henning Kamp #include <net/radix.h> 55df8bae1dSRodney W. Grimes 562b14f991SJulian Elischer /* 572b14f991SJulian Elischer * System initialization 582b14f991SJulian Elischer */ 592b14f991SJulian Elischer 603bda9f9bSPoul-Henning Kamp static int ifconf __P((int, caddr_t)); 614590fd3aSDavid Greenman static void ifinit __P((void *)); 623bda9f9bSPoul-Henning Kamp static void if_qflush __P((struct ifqueue *)); 633bda9f9bSPoul-Henning Kamp static void if_slowtimo __P((void *)); 643bda9f9bSPoul-Henning Kamp static void link_rtrequest __P((int, struct rtentry *, struct sockaddr *)); 653bda9f9bSPoul-Henning Kamp 662b14f991SJulian Elischer SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL) 672b14f991SJulian Elischer 682b14f991SJulian Elischer 69df8bae1dSRodney W. Grimes int ifqmaxlen = IFQ_MAXLEN; 7029412182SGarrett Wollman struct ifnethead ifnet; /* depend on static init XXX */ 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes /* 73df8bae1dSRodney W. Grimes * Network interface utility routines. 74df8bae1dSRodney W. Grimes * 75df8bae1dSRodney W. Grimes * Routines with ifa_ifwith* names take sockaddr *'s as 76df8bae1dSRodney W. Grimes * parameters. 772b14f991SJulian Elischer * 782b14f991SJulian Elischer * This routine assumes that it will be called at splimp() or higher. 79df8bae1dSRodney W. Grimes */ 802b14f991SJulian Elischer /* ARGSUSED*/ 81df8bae1dSRodney W. Grimes void 8227501cb6SBruce Evans ifinit(dummy) 8327501cb6SBruce Evans void *dummy; 84df8bae1dSRodney W. Grimes { 85df8bae1dSRodney W. Grimes register struct ifnet *ifp; 86df8bae1dSRodney W. Grimes 8729412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) 88df8bae1dSRodney W. Grimes if (ifp->if_snd.ifq_maxlen == 0) 89df8bae1dSRodney W. Grimes ifp->if_snd.ifq_maxlen = ifqmaxlen; 90df8bae1dSRodney W. Grimes if_slowtimo(0); 91df8bae1dSRodney W. Grimes } 92df8bae1dSRodney W. Grimes 93bbd17bf8SGarrett Wollman int if_index = 0; 94bbd17bf8SGarrett Wollman struct ifaddr **ifnet_addrs; 953bda9f9bSPoul-Henning Kamp 96df8bae1dSRodney W. Grimes 97df8bae1dSRodney W. Grimes /* 98df8bae1dSRodney W. Grimes * Attach an interface to the 99df8bae1dSRodney W. Grimes * list of "active" interfaces. 100df8bae1dSRodney W. Grimes */ 101df8bae1dSRodney W. Grimes void 102df8bae1dSRodney W. Grimes if_attach(ifp) 103df8bae1dSRodney W. Grimes struct ifnet *ifp; 104df8bae1dSRodney W. Grimes { 105df8bae1dSRodney W. Grimes unsigned socksize, ifasize; 1061ce9bf88SPoul-Henning Kamp int namelen, masklen; 1071ce9bf88SPoul-Henning Kamp char workbuf[64]; 108df8bae1dSRodney W. Grimes register struct sockaddr_dl *sdl; 109df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 110df8bae1dSRodney W. Grimes static int if_indexlim = 8; 11129412182SGarrett Wollman static int inited; 112f23b4c91SGarrett Wollman 11329412182SGarrett Wollman if (!inited) { 11429412182SGarrett Wollman TAILQ_INIT(&ifnet); 11529412182SGarrett Wollman inited = 1; 11629412182SGarrett Wollman } 117df8bae1dSRodney W. Grimes 11829412182SGarrett Wollman TAILQ_INSERT_TAIL(&ifnet, ifp, if_link); 119df8bae1dSRodney W. Grimes ifp->if_index = ++if_index; 12059562606SGarrett Wollman /* 12159562606SGarrett Wollman * XXX - 12259562606SGarrett Wollman * The old code would work if the interface passed a pre-existing 12359562606SGarrett Wollman * chain of ifaddrs to this code. We don't trust our callers to 12459562606SGarrett Wollman * properly initialize the tailq, however, so we no longer allow 12559562606SGarrett Wollman * this unlikely case. 12659562606SGarrett Wollman */ 12759562606SGarrett Wollman TAILQ_INIT(&ifp->if_addrhead); 1281158dfb7SGarrett Wollman LIST_INIT(&ifp->if_multiaddrs); 129a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 130df8bae1dSRodney W. Grimes if (ifnet_addrs == 0 || if_index >= if_indexlim) { 131df8bae1dSRodney W. Grimes unsigned n = (if_indexlim <<= 1) * sizeof(ifa); 132df8bae1dSRodney W. Grimes struct ifaddr **q = (struct ifaddr **) 133df8bae1dSRodney W. Grimes malloc(n, M_IFADDR, M_WAITOK); 13473c2ab46SDavid Greenman bzero((caddr_t)q, n); 135df8bae1dSRodney W. Grimes if (ifnet_addrs) { 136df8bae1dSRodney W. Grimes bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2); 137df8bae1dSRodney W. Grimes free((caddr_t)ifnet_addrs, M_IFADDR); 138df8bae1dSRodney W. Grimes } 139df8bae1dSRodney W. Grimes ifnet_addrs = q; 140df8bae1dSRodney W. Grimes } 141df8bae1dSRodney W. Grimes /* 142df8bae1dSRodney W. Grimes * create a Link Level name for this device 143df8bae1dSRodney W. Grimes */ 1441ce9bf88SPoul-Henning Kamp namelen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit); 145df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) 1461ce9bf88SPoul-Henning Kamp masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 147df8bae1dSRodney W. Grimes socksize = masklen + ifp->if_addrlen; 148df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 149df8bae1dSRodney W. Grimes socksize = ROUNDUP(socksize); 150df8bae1dSRodney W. Grimes if (socksize < sizeof(*sdl)) 151df8bae1dSRodney W. Grimes socksize = sizeof(*sdl); 152df8bae1dSRodney W. Grimes ifasize = sizeof(*ifa) + 2 * socksize; 1539448326fSPoul-Henning Kamp ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK); 1549448326fSPoul-Henning Kamp if (ifa) { 155df8bae1dSRodney W. Grimes bzero((caddr_t)ifa, ifasize); 156df8bae1dSRodney W. Grimes sdl = (struct sockaddr_dl *)(ifa + 1); 157df8bae1dSRodney W. Grimes sdl->sdl_len = socksize; 158df8bae1dSRodney W. Grimes sdl->sdl_family = AF_LINK; 1591ce9bf88SPoul-Henning Kamp bcopy(workbuf, sdl->sdl_data, namelen); 1601ce9bf88SPoul-Henning Kamp sdl->sdl_nlen = namelen; 161df8bae1dSRodney W. Grimes sdl->sdl_index = ifp->if_index; 162df8bae1dSRodney W. Grimes sdl->sdl_type = ifp->if_type; 163df8bae1dSRodney W. Grimes ifnet_addrs[if_index - 1] = ifa; 164df8bae1dSRodney W. Grimes ifa->ifa_ifp = ifp; 165df8bae1dSRodney W. Grimes ifa->ifa_rtrequest = link_rtrequest; 166df8bae1dSRodney W. Grimes ifa->ifa_addr = (struct sockaddr *)sdl; 167df8bae1dSRodney W. Grimes sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 168df8bae1dSRodney W. Grimes ifa->ifa_netmask = (struct sockaddr *)sdl; 169df8bae1dSRodney W. Grimes sdl->sdl_len = masklen; 170df8bae1dSRodney W. Grimes while (namelen != 0) 171df8bae1dSRodney W. Grimes sdl->sdl_data[--namelen] = 0xff; 17259562606SGarrett Wollman TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 173df8bae1dSRodney W. Grimes } 174df8bae1dSRodney W. Grimes } 175df8bae1dSRodney W. Grimes /* 176df8bae1dSRodney W. Grimes * Locate an interface based on a complete address. 177df8bae1dSRodney W. Grimes */ 178df8bae1dSRodney W. Grimes /*ARGSUSED*/ 179df8bae1dSRodney W. Grimes struct ifaddr * 180df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr) 181df8bae1dSRodney W. Grimes register struct sockaddr *addr; 182df8bae1dSRodney W. Grimes { 183df8bae1dSRodney W. Grimes register struct ifnet *ifp; 184df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 185df8bae1dSRodney W. Grimes 186df8bae1dSRodney W. Grimes #define equal(a1, a2) \ 187df8bae1dSRodney W. Grimes (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) 18829412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) 18959562606SGarrett Wollman for (ifa = ifp->if_addrhead.tqh_first; ifa; 19059562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) { 191df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != addr->sa_family) 192df8bae1dSRodney W. Grimes continue; 193df8bae1dSRodney W. Grimes if (equal(addr, ifa->ifa_addr)) 194df8bae1dSRodney W. Grimes return (ifa); 195df8bae1dSRodney W. Grimes if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr && 196df8bae1dSRodney W. Grimes equal(ifa->ifa_broadaddr, addr)) 197df8bae1dSRodney W. Grimes return (ifa); 198df8bae1dSRodney W. Grimes } 199df8bae1dSRodney W. Grimes return ((struct ifaddr *)0); 200df8bae1dSRodney W. Grimes } 201df8bae1dSRodney W. Grimes /* 202df8bae1dSRodney W. Grimes * Locate the point to point interface with a given destination address. 203df8bae1dSRodney W. Grimes */ 204df8bae1dSRodney W. Grimes /*ARGSUSED*/ 205df8bae1dSRodney W. Grimes struct ifaddr * 206df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr) 207df8bae1dSRodney W. Grimes register struct sockaddr *addr; 208df8bae1dSRodney W. Grimes { 209df8bae1dSRodney W. Grimes register struct ifnet *ifp; 210df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 211df8bae1dSRodney W. Grimes 21229412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) 213df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_POINTOPOINT) 21459562606SGarrett Wollman for (ifa = ifp->if_addrhead.tqh_first; ifa; 21559562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) { 216df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != addr->sa_family) 217df8bae1dSRodney W. Grimes continue; 21855088a1cSDavid Greenman if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)) 219df8bae1dSRodney W. Grimes return (ifa); 220df8bae1dSRodney W. Grimes } 221df8bae1dSRodney W. Grimes return ((struct ifaddr *)0); 222df8bae1dSRodney W. Grimes } 223df8bae1dSRodney W. Grimes 224df8bae1dSRodney W. Grimes /* 225df8bae1dSRodney W. Grimes * Find an interface on a specific network. If many, choice 226df8bae1dSRodney W. Grimes * is most specific found. 227df8bae1dSRodney W. Grimes */ 228df8bae1dSRodney W. Grimes struct ifaddr * 229df8bae1dSRodney W. Grimes ifa_ifwithnet(addr) 230df8bae1dSRodney W. Grimes struct sockaddr *addr; 231df8bae1dSRodney W. Grimes { 232df8bae1dSRodney W. Grimes register struct ifnet *ifp; 233df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 234df8bae1dSRodney W. Grimes struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 235df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 236df8bae1dSRodney W. Grimes char *addr_data = addr->sa_data, *cplim; 237df8bae1dSRodney W. Grimes 2387e2a6151SJulian Elischer /* 2397e2a6151SJulian Elischer * AF_LINK addresses can be looked up directly by their index number, 2407e2a6151SJulian Elischer * so do that if we can. 2417e2a6151SJulian Elischer */ 242df8bae1dSRodney W. Grimes if (af == AF_LINK) { 243df8bae1dSRodney W. Grimes register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 244df8bae1dSRodney W. Grimes if (sdl->sdl_index && sdl->sdl_index <= if_index) 245df8bae1dSRodney W. Grimes return (ifnet_addrs[sdl->sdl_index - 1]); 246df8bae1dSRodney W. Grimes } 2477e2a6151SJulian Elischer 2487e2a6151SJulian Elischer /* 2497e2a6151SJulian Elischer * Scan though each interface, looking for ones that have 2507e2a6151SJulian Elischer * addresses in this address family. 2517e2a6151SJulian Elischer */ 25229412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 25359562606SGarrett Wollman for (ifa = ifp->if_addrhead.tqh_first; ifa; 25459562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) { 255df8bae1dSRodney W. Grimes register char *cp, *cp2, *cp3; 256df8bae1dSRodney W. Grimes 257523a02aaSDavid Greenman if (ifa->ifa_addr->sa_family != af) 258df8bae1dSRodney W. Grimes next: continue; 259b2af64fdSDavid Greenman if (ifp->if_flags & IFF_POINTOPOINT) { 2607e2a6151SJulian Elischer /* 2617e2a6151SJulian Elischer * This is a bit broken as it doesn't 2627e2a6151SJulian Elischer * take into account that the remote end may 2637e2a6151SJulian Elischer * be a single node in the network we are 2647e2a6151SJulian Elischer * looking for. 2657e2a6151SJulian Elischer * The trouble is that we don't know the 2667e2a6151SJulian Elischer * netmask for the remote end. 2677e2a6151SJulian Elischer */ 268fcd6781aSGarrett Wollman if (ifa->ifa_dstaddr != 0 269fcd6781aSGarrett Wollman && equal(addr, ifa->ifa_dstaddr)) 270b2af64fdSDavid Greenman return (ifa); 2713740e2adSDavid Greenman } else { 2727e2a6151SJulian Elischer /* 2737e2a6151SJulian Elischer * Scan all the bits in the ifa's address. 2747e2a6151SJulian Elischer * If a bit dissagrees with what we are 2757e2a6151SJulian Elischer * looking for, mask it with the netmask 2767e2a6151SJulian Elischer * to see if it really matters. 2777e2a6151SJulian Elischer * (A byte at a time) 2787e2a6151SJulian Elischer */ 279523a02aaSDavid Greenman if (ifa->ifa_netmask == 0) 280523a02aaSDavid Greenman continue; 281df8bae1dSRodney W. Grimes cp = addr_data; 282df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 283df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 2847e2a6151SJulian Elischer cplim = ifa->ifa_netmask->sa_len 2857e2a6151SJulian Elischer + (char *)ifa->ifa_netmask; 286df8bae1dSRodney W. Grimes while (cp3 < cplim) 287df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3++) 2887e2a6151SJulian Elischer goto next; /* next address! */ 2897e2a6151SJulian Elischer /* 2907e2a6151SJulian Elischer * If the netmask of what we just found 2917e2a6151SJulian Elischer * is more specific than what we had before 2927e2a6151SJulian Elischer * (if we had one) then remember the new one 2937e2a6151SJulian Elischer * before continuing to search 2947e2a6151SJulian Elischer * for an even better one. 2957e2a6151SJulian Elischer */ 296df8bae1dSRodney W. Grimes if (ifa_maybe == 0 || 297df8bae1dSRodney W. Grimes rn_refines((caddr_t)ifa->ifa_netmask, 298df8bae1dSRodney W. Grimes (caddr_t)ifa_maybe->ifa_netmask)) 299df8bae1dSRodney W. Grimes ifa_maybe = ifa; 300df8bae1dSRodney W. Grimes } 301b2af64fdSDavid Greenman } 302b2af64fdSDavid Greenman } 303df8bae1dSRodney W. Grimes return (ifa_maybe); 304df8bae1dSRodney W. Grimes } 305df8bae1dSRodney W. Grimes 306df8bae1dSRodney W. Grimes /* 307df8bae1dSRodney W. Grimes * Find an interface address specific to an interface best matching 308df8bae1dSRodney W. Grimes * a given address. 309df8bae1dSRodney W. Grimes */ 310df8bae1dSRodney W. Grimes struct ifaddr * 311df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp) 312df8bae1dSRodney W. Grimes struct sockaddr *addr; 313df8bae1dSRodney W. Grimes register struct ifnet *ifp; 314df8bae1dSRodney W. Grimes { 315df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 316df8bae1dSRodney W. Grimes register char *cp, *cp2, *cp3; 317df8bae1dSRodney W. Grimes register char *cplim; 318df8bae1dSRodney W. Grimes struct ifaddr *ifa_maybe = 0; 319df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 320df8bae1dSRodney W. Grimes 321df8bae1dSRodney W. Grimes if (af >= AF_MAX) 322df8bae1dSRodney W. Grimes return (0); 32359562606SGarrett Wollman for (ifa = ifp->if_addrhead.tqh_first; ifa; 32459562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) { 325df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != af) 326df8bae1dSRodney W. Grimes continue; 327381dd1d2SJulian Elischer if (ifa_maybe == 0) 328df8bae1dSRodney W. Grimes ifa_maybe = ifa; 329df8bae1dSRodney W. Grimes if (ifa->ifa_netmask == 0) { 330df8bae1dSRodney W. Grimes if (equal(addr, ifa->ifa_addr) || 331df8bae1dSRodney W. Grimes (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 332df8bae1dSRodney W. Grimes return (ifa); 333df8bae1dSRodney W. Grimes continue; 334df8bae1dSRodney W. Grimes } 335b2af64fdSDavid Greenman if (ifp->if_flags & IFF_POINTOPOINT) { 336b2af64fdSDavid Greenman if (equal(addr, ifa->ifa_dstaddr)) 337b2af64fdSDavid Greenman return (ifa); 3383740e2adSDavid Greenman } else { 339df8bae1dSRodney W. Grimes cp = addr->sa_data; 340df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 341df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 342df8bae1dSRodney W. Grimes cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 343df8bae1dSRodney W. Grimes for (; cp3 < cplim; cp3++) 344df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3) 345df8bae1dSRodney W. Grimes break; 346df8bae1dSRodney W. Grimes if (cp3 == cplim) 347df8bae1dSRodney W. Grimes return (ifa); 348df8bae1dSRodney W. Grimes } 349b2af64fdSDavid Greenman } 350df8bae1dSRodney W. Grimes return (ifa_maybe); 351df8bae1dSRodney W. Grimes } 352df8bae1dSRodney W. Grimes 353df8bae1dSRodney W. Grimes #include <net/route.h> 354df8bae1dSRodney W. Grimes 355df8bae1dSRodney W. Grimes /* 356df8bae1dSRodney W. Grimes * Default action when installing a route with a Link Level gateway. 357df8bae1dSRodney W. Grimes * Lookup an appropriate real ifa to point to. 358df8bae1dSRodney W. Grimes * This should be moved to /sys/net/link.c eventually. 359df8bae1dSRodney W. Grimes */ 3603bda9f9bSPoul-Henning Kamp static void 361df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa) 362df8bae1dSRodney W. Grimes int cmd; 363df8bae1dSRodney W. Grimes register struct rtentry *rt; 364df8bae1dSRodney W. Grimes struct sockaddr *sa; 365df8bae1dSRodney W. Grimes { 366df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 367df8bae1dSRodney W. Grimes struct sockaddr *dst; 368df8bae1dSRodney W. Grimes struct ifnet *ifp; 369df8bae1dSRodney W. Grimes 370df8bae1dSRodney W. Grimes if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 371df8bae1dSRodney W. Grimes ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 372df8bae1dSRodney W. Grimes return; 3739448326fSPoul-Henning Kamp ifa = ifaof_ifpforaddr(dst, ifp); 3749448326fSPoul-Henning Kamp if (ifa) { 375df8bae1dSRodney W. Grimes IFAFREE(rt->rt_ifa); 376df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 377df8bae1dSRodney W. Grimes ifa->ifa_refcnt++; 378df8bae1dSRodney W. Grimes if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 379df8bae1dSRodney W. Grimes ifa->ifa_rtrequest(cmd, rt, sa); 380df8bae1dSRodney W. Grimes } 381df8bae1dSRodney W. Grimes } 382df8bae1dSRodney W. Grimes 383df8bae1dSRodney W. Grimes /* 384df8bae1dSRodney W. Grimes * Mark an interface down and notify protocols of 385df8bae1dSRodney W. Grimes * the transition. 386df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 387df8bae1dSRodney W. Grimes */ 388df8bae1dSRodney W. Grimes void 389df8bae1dSRodney W. Grimes if_down(ifp) 390df8bae1dSRodney W. Grimes register struct ifnet *ifp; 391df8bae1dSRodney W. Grimes { 392df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 393df8bae1dSRodney W. Grimes 394df8bae1dSRodney W. Grimes ifp->if_flags &= ~IFF_UP; 395a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 39659562606SGarrett Wollman for (ifa = ifp->if_addrhead.tqh_first; ifa; 39759562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) 398df8bae1dSRodney W. Grimes pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 399df8bae1dSRodney W. Grimes if_qflush(&ifp->if_snd); 400df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 401df8bae1dSRodney W. Grimes } 402df8bae1dSRodney W. Grimes 403df8bae1dSRodney W. Grimes /* 404df8bae1dSRodney W. Grimes * Mark an interface up and notify protocols of 405df8bae1dSRodney W. Grimes * the transition. 406df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 407df8bae1dSRodney W. Grimes */ 408df8bae1dSRodney W. Grimes void 409df8bae1dSRodney W. Grimes if_up(ifp) 410df8bae1dSRodney W. Grimes register struct ifnet *ifp; 411df8bae1dSRodney W. Grimes { 412176395b2SGarrett Wollman register struct ifaddr *ifa; 413df8bae1dSRodney W. Grimes 414df8bae1dSRodney W. Grimes ifp->if_flags |= IFF_UP; 415a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 416176395b2SGarrett Wollman for (ifa = ifp->if_addrhead.tqh_first; ifa; 417176395b2SGarrett Wollman ifa = ifa->ifa_link.tqe_next) 418df8bae1dSRodney W. Grimes pfctlinput(PRC_IFUP, ifa->ifa_addr); 419df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 420df8bae1dSRodney W. Grimes } 421df8bae1dSRodney W. Grimes 422df8bae1dSRodney W. Grimes /* 423df8bae1dSRodney W. Grimes * Flush an interface queue. 424df8bae1dSRodney W. Grimes */ 4253bda9f9bSPoul-Henning Kamp static void 426df8bae1dSRodney W. Grimes if_qflush(ifq) 427df8bae1dSRodney W. Grimes register struct ifqueue *ifq; 428df8bae1dSRodney W. Grimes { 429df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 430df8bae1dSRodney W. Grimes 431df8bae1dSRodney W. Grimes n = ifq->ifq_head; 4329448326fSPoul-Henning Kamp while ((m = n) != 0) { 433df8bae1dSRodney W. Grimes n = m->m_act; 434df8bae1dSRodney W. Grimes m_freem(m); 435df8bae1dSRodney W. Grimes } 436df8bae1dSRodney W. Grimes ifq->ifq_head = 0; 437df8bae1dSRodney W. Grimes ifq->ifq_tail = 0; 438df8bae1dSRodney W. Grimes ifq->ifq_len = 0; 439df8bae1dSRodney W. Grimes } 440df8bae1dSRodney W. Grimes 441df8bae1dSRodney W. Grimes /* 442df8bae1dSRodney W. Grimes * Handle interface watchdog timer routines. Called 443df8bae1dSRodney W. Grimes * from softclock, we decrement timers (if set) and 444df8bae1dSRodney W. Grimes * call the appropriate interface routine on expiration. 445df8bae1dSRodney W. Grimes */ 4463bda9f9bSPoul-Henning Kamp static void 447df8bae1dSRodney W. Grimes if_slowtimo(arg) 448df8bae1dSRodney W. Grimes void *arg; 449df8bae1dSRodney W. Grimes { 450df8bae1dSRodney W. Grimes register struct ifnet *ifp; 451df8bae1dSRodney W. Grimes int s = splimp(); 452df8bae1dSRodney W. Grimes 45329412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 454df8bae1dSRodney W. Grimes if (ifp->if_timer == 0 || --ifp->if_timer) 455df8bae1dSRodney W. Grimes continue; 456df8bae1dSRodney W. Grimes if (ifp->if_watchdog) 4574a5f1499SDavid Greenman (*ifp->if_watchdog)(ifp); 458df8bae1dSRodney W. Grimes } 459df8bae1dSRodney W. Grimes splx(s); 460df8bae1dSRodney W. Grimes timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 461df8bae1dSRodney W. Grimes } 462df8bae1dSRodney W. Grimes 463df8bae1dSRodney W. Grimes /* 464df8bae1dSRodney W. Grimes * Map interface name to 465df8bae1dSRodney W. Grimes * interface structure pointer. 466df8bae1dSRodney W. Grimes */ 467df8bae1dSRodney W. Grimes struct ifnet * 468df8bae1dSRodney W. Grimes ifunit(name) 469df8bae1dSRodney W. Grimes register char *name; 470df8bae1dSRodney W. Grimes { 471df8bae1dSRodney W. Grimes register char *cp; 472df8bae1dSRodney W. Grimes register struct ifnet *ifp; 473df8bae1dSRodney W. Grimes int unit; 474df8bae1dSRodney W. Grimes unsigned len; 475df8bae1dSRodney W. Grimes char *ep, c; 476df8bae1dSRodney W. Grimes 477df8bae1dSRodney W. Grimes for (cp = name; cp < name + IFNAMSIZ && *cp; cp++) 478df8bae1dSRodney W. Grimes if (*cp >= '0' && *cp <= '9') 479df8bae1dSRodney W. Grimes break; 480df8bae1dSRodney W. Grimes if (*cp == '\0' || cp == name + IFNAMSIZ) 481df8bae1dSRodney W. Grimes return ((struct ifnet *)0); 482df8bae1dSRodney W. Grimes /* 483df8bae1dSRodney W. Grimes * Save first char of unit, and pointer to it, 484df8bae1dSRodney W. Grimes * so we can put a null there to avoid matching 485df8bae1dSRodney W. Grimes * initial substrings of interface names. 486df8bae1dSRodney W. Grimes */ 487df8bae1dSRodney W. Grimes len = cp - name + 1; 488df8bae1dSRodney W. Grimes c = *cp; 489df8bae1dSRodney W. Grimes ep = cp; 490df8bae1dSRodney W. Grimes for (unit = 0; *cp >= '0' && *cp <= '9'; ) 491df8bae1dSRodney W. Grimes unit = unit * 10 + *cp++ - '0'; 49258639ed3SGarrett Wollman if (*cp != '\0') 49358639ed3SGarrett Wollman return 0; /* no trailing garbage allowed */ 494df8bae1dSRodney W. Grimes *ep = 0; 49529412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 496df8bae1dSRodney W. Grimes if (bcmp(ifp->if_name, name, len)) 497df8bae1dSRodney W. Grimes continue; 498df8bae1dSRodney W. Grimes if (unit == ifp->if_unit) 499df8bae1dSRodney W. Grimes break; 500df8bae1dSRodney W. Grimes } 501df8bae1dSRodney W. Grimes *ep = c; 502df8bae1dSRodney W. Grimes return (ifp); 503df8bae1dSRodney W. Grimes } 504df8bae1dSRodney W. Grimes 505df8bae1dSRodney W. Grimes /* 506df8bae1dSRodney W. Grimes * Interface ioctls. 507df8bae1dSRodney W. Grimes */ 508df8bae1dSRodney W. Grimes int 509df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p) 510df8bae1dSRodney W. Grimes struct socket *so; 511df8bae1dSRodney W. Grimes int cmd; 512df8bae1dSRodney W. Grimes caddr_t data; 513df8bae1dSRodney W. Grimes struct proc *p; 514df8bae1dSRodney W. Grimes { 515df8bae1dSRodney W. Grimes register struct ifnet *ifp; 516df8bae1dSRodney W. Grimes register struct ifreq *ifr; 517df8bae1dSRodney W. Grimes int error; 518df8bae1dSRodney W. Grimes 519df8bae1dSRodney W. Grimes switch (cmd) { 520df8bae1dSRodney W. Grimes 521df8bae1dSRodney W. Grimes case SIOCGIFCONF: 522df8bae1dSRodney W. Grimes case OSIOCGIFCONF: 523df8bae1dSRodney W. Grimes return (ifconf(cmd, data)); 524df8bae1dSRodney W. Grimes } 525df8bae1dSRodney W. Grimes ifr = (struct ifreq *)data; 526df8bae1dSRodney W. Grimes ifp = ifunit(ifr->ifr_name); 527df8bae1dSRodney W. Grimes if (ifp == 0) 528df8bae1dSRodney W. Grimes return (ENXIO); 529df8bae1dSRodney W. Grimes switch (cmd) { 530df8bae1dSRodney W. Grimes 531df8bae1dSRodney W. Grimes case SIOCGIFFLAGS: 532df8bae1dSRodney W. Grimes ifr->ifr_flags = ifp->if_flags; 533df8bae1dSRodney W. Grimes break; 534df8bae1dSRodney W. Grimes 535df8bae1dSRodney W. Grimes case SIOCGIFMETRIC: 536df8bae1dSRodney W. Grimes ifr->ifr_metric = ifp->if_metric; 537df8bae1dSRodney W. Grimes break; 538df8bae1dSRodney W. Grimes 539a7028af7SDavid Greenman case SIOCGIFMTU: 540a7028af7SDavid Greenman ifr->ifr_mtu = ifp->if_mtu; 541a7028af7SDavid Greenman break; 542a7028af7SDavid Greenman 543074c4a4eSGarrett Wollman case SIOCGIFPHYS: 544074c4a4eSGarrett Wollman ifr->ifr_phys = ifp->if_physical; 545074c4a4eSGarrett Wollman break; 546074c4a4eSGarrett Wollman 547df8bae1dSRodney W. Grimes case SIOCSIFFLAGS: 5489448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5499448326fSPoul-Henning Kamp if (error) 550df8bae1dSRodney W. Grimes return (error); 551df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 552df8bae1dSRodney W. Grimes int s = splimp(); 553df8bae1dSRodney W. Grimes if_down(ifp); 554df8bae1dSRodney W. Grimes splx(s); 555df8bae1dSRodney W. Grimes } 556df8bae1dSRodney W. Grimes if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 557df8bae1dSRodney W. Grimes int s = splimp(); 558df8bae1dSRodney W. Grimes if_up(ifp); 559df8bae1dSRodney W. Grimes splx(s); 560df8bae1dSRodney W. Grimes } 561df8bae1dSRodney W. Grimes ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 562df8bae1dSRodney W. Grimes (ifr->ifr_flags &~ IFF_CANTCHANGE); 563df8bae1dSRodney W. Grimes if (ifp->if_ioctl) 564df8bae1dSRodney W. Grimes (void) (*ifp->if_ioctl)(ifp, cmd, data); 565a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 566df8bae1dSRodney W. Grimes break; 567df8bae1dSRodney W. Grimes 568df8bae1dSRodney W. Grimes case SIOCSIFMETRIC: 5699448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5709448326fSPoul-Henning Kamp if (error) 571df8bae1dSRodney W. Grimes return (error); 572df8bae1dSRodney W. Grimes ifp->if_metric = ifr->ifr_metric; 573a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 574df8bae1dSRodney W. Grimes break; 575df8bae1dSRodney W. Grimes 576074c4a4eSGarrett Wollman case SIOCSIFPHYS: 577074c4a4eSGarrett Wollman error = suser(p->p_ucred, &p->p_acflag); 578e39a0280SGary Palmer if (error) 579e39a0280SGary Palmer return error; 580e39a0280SGary Palmer if (!ifp->if_ioctl) 581e39a0280SGary Palmer return EOPNOTSUPP; 582e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 583e39a0280SGary Palmer if (error == 0) 584a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 585e39a0280SGary Palmer return(error); 586074c4a4eSGarrett Wollman 587a7028af7SDavid Greenman case SIOCSIFMTU: 5889448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5899448326fSPoul-Henning Kamp if (error) 590a7028af7SDavid Greenman return (error); 591a7028af7SDavid Greenman if (ifp->if_ioctl == NULL) 592a7028af7SDavid Greenman return (EOPNOTSUPP); 59366025569SDavid Greenman /* 59466025569SDavid Greenman * 72 was chosen below because it is the size of a TCP/IP 59566025569SDavid Greenman * header (40) + the minimum mss (32). 59666025569SDavid Greenman */ 59766025569SDavid Greenman if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535) 59875ee03cbSDavid Greenman return (EINVAL); 599e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 600e39a0280SGary Palmer if (error == 0) 601a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 602e39a0280SGary Palmer return(error); 603a7028af7SDavid Greenman 604df8bae1dSRodney W. Grimes case SIOCADDMULTI: 605df8bae1dSRodney W. Grimes case SIOCDELMULTI: 6069448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 6079448326fSPoul-Henning Kamp if (error) 608df8bae1dSRodney W. Grimes return (error); 609477180fbSGarrett Wollman 610477180fbSGarrett Wollman /* Don't allow group membership on non-multicast interfaces. */ 611477180fbSGarrett Wollman if ((ifp->if_flags & IFF_MULTICAST) == 0) 612477180fbSGarrett Wollman return EOPNOTSUPP; 613477180fbSGarrett Wollman 614477180fbSGarrett Wollman /* Don't let users screw up protocols' entries. */ 615477180fbSGarrett Wollman if (ifr->ifr_addr.sa_family != AF_LINK) 616477180fbSGarrett Wollman return EINVAL; 617477180fbSGarrett Wollman 618477180fbSGarrett Wollman if (cmd == SIOCADDMULTI) { 619477180fbSGarrett Wollman struct ifmultiaddr *ifma; 620477180fbSGarrett Wollman error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 621477180fbSGarrett Wollman } else { 622477180fbSGarrett Wollman error = if_delmulti(ifp, &ifr->ifr_addr); 623477180fbSGarrett Wollman } 624e39a0280SGary Palmer if (error == 0) 625a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 626477180fbSGarrett Wollman return error; 627df8bae1dSRodney W. Grimes 628a912e453SPeter Wemm case SIOCSIFMEDIA: 629a912e453SPeter Wemm error = suser(p->p_ucred, &p->p_acflag); 630a912e453SPeter Wemm if (error) 631a912e453SPeter Wemm return (error); 632a912e453SPeter Wemm if (ifp->if_ioctl == 0) 633a912e453SPeter Wemm return (EOPNOTSUPP); 634a912e453SPeter Wemm error = (*ifp->if_ioctl)(ifp, cmd, data); 635a912e453SPeter Wemm if (error == 0) 636a912e453SPeter Wemm microtime(&ifp->if_lastchange); 637a912e453SPeter Wemm return error; 638a912e453SPeter Wemm 639a912e453SPeter Wemm case SIOCGIFMEDIA: 640a912e453SPeter Wemm if (ifp->if_ioctl == 0) 641a912e453SPeter Wemm return (EOPNOTSUPP); 642a912e453SPeter Wemm return ((*ifp->if_ioctl)(ifp, cmd, data)); 643a912e453SPeter Wemm 644df8bae1dSRodney W. Grimes default: 645df8bae1dSRodney W. Grimes if (so->so_proto == 0) 646df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 647df8bae1dSRodney W. Grimes #ifndef COMPAT_43 6482c37256eSGarrett Wollman return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 6492c37256eSGarrett Wollman data, 6502c37256eSGarrett Wollman ifp)); 651df8bae1dSRodney W. Grimes #else 652df8bae1dSRodney W. Grimes { 653df8bae1dSRodney W. Grimes int ocmd = cmd; 654df8bae1dSRodney W. Grimes 655df8bae1dSRodney W. Grimes switch (cmd) { 656df8bae1dSRodney W. Grimes 657df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 658df8bae1dSRodney W. Grimes case SIOCSIFADDR: 659df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 660df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 661df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN 662df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_family == 0 && 663df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len < 16) { 664df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 665df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 666df8bae1dSRodney W. Grimes } 667df8bae1dSRodney W. Grimes #else 668df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_len == 0) 669df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 670df8bae1dSRodney W. Grimes #endif 671df8bae1dSRodney W. Grimes break; 672df8bae1dSRodney W. Grimes 673df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 674df8bae1dSRodney W. Grimes cmd = SIOCGIFADDR; 675df8bae1dSRodney W. Grimes break; 676df8bae1dSRodney W. Grimes 677df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 678df8bae1dSRodney W. Grimes cmd = SIOCGIFDSTADDR; 679df8bae1dSRodney W. Grimes break; 680df8bae1dSRodney W. Grimes 681df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 682df8bae1dSRodney W. Grimes cmd = SIOCGIFBRDADDR; 683df8bae1dSRodney W. Grimes break; 684df8bae1dSRodney W. Grimes 685df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 686df8bae1dSRodney W. Grimes cmd = SIOCGIFNETMASK; 687df8bae1dSRodney W. Grimes } 6882c37256eSGarrett Wollman error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 6892c37256eSGarrett Wollman cmd, 6902c37256eSGarrett Wollman data, 691a29f300eSGarrett Wollman ifp, p)); 692df8bae1dSRodney W. Grimes switch (ocmd) { 693df8bae1dSRodney W. Grimes 694df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 695df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 696df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 697df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 698df8bae1dSRodney W. Grimes *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 699df8bae1dSRodney W. Grimes } 700df8bae1dSRodney W. Grimes return (error); 701df8bae1dSRodney W. Grimes 702df8bae1dSRodney W. Grimes } 703df8bae1dSRodney W. Grimes #endif 704df8bae1dSRodney W. Grimes } 705df8bae1dSRodney W. Grimes return (0); 706df8bae1dSRodney W. Grimes } 707df8bae1dSRodney W. Grimes 708df8bae1dSRodney W. Grimes /* 709963e4c2aSGarrett Wollman * Set/clear promiscuous mode on interface ifp based on the truth value 710963e4c2aSGarrett Wollman * of pswitch. The calls are reference counted so that only the first 711963e4c2aSGarrett Wollman * "on" request actually has an effect, as does the final "off" request. 712963e4c2aSGarrett Wollman * Results are undefined if the "off" and "on" requests are not matched. 713963e4c2aSGarrett Wollman */ 714963e4c2aSGarrett Wollman int 715963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch) 716963e4c2aSGarrett Wollman struct ifnet *ifp; 717963e4c2aSGarrett Wollman int pswitch; 718963e4c2aSGarrett Wollman { 719963e4c2aSGarrett Wollman struct ifreq ifr; 7204a26224cSGarrett Wollman int error; 721963e4c2aSGarrett Wollman 722963e4c2aSGarrett Wollman if (pswitch) { 723963e4c2aSGarrett Wollman /* 724963e4c2aSGarrett Wollman * If the device is not configured up, we cannot put it in 725963e4c2aSGarrett Wollman * promiscuous mode. 726963e4c2aSGarrett Wollman */ 727963e4c2aSGarrett Wollman if ((ifp->if_flags & IFF_UP) == 0) 728963e4c2aSGarrett Wollman return (ENETDOWN); 729963e4c2aSGarrett Wollman if (ifp->if_pcount++ != 0) 730963e4c2aSGarrett Wollman return (0); 731963e4c2aSGarrett Wollman ifp->if_flags |= IFF_PROMISC; 732e9a30d00SGarrett Wollman log(LOG_INFO, "%s%d: promiscuous mode enabled\n", 733963e4c2aSGarrett Wollman ifp->if_name, ifp->if_unit); 734963e4c2aSGarrett Wollman } else { 735963e4c2aSGarrett Wollman if (--ifp->if_pcount > 0) 736963e4c2aSGarrett Wollman return (0); 737963e4c2aSGarrett Wollman ifp->if_flags &= ~IFF_PROMISC; 738963e4c2aSGarrett Wollman } 739963e4c2aSGarrett Wollman ifr.ifr_flags = ifp->if_flags; 7404a26224cSGarrett Wollman error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 7414a26224cSGarrett Wollman if (error == 0) 7424a26224cSGarrett Wollman rt_ifmsg(ifp); 7434a26224cSGarrett Wollman return error; 744963e4c2aSGarrett Wollman } 745963e4c2aSGarrett Wollman 746963e4c2aSGarrett Wollman /* 747df8bae1dSRodney W. Grimes * Return interface configuration 748df8bae1dSRodney W. Grimes * of system. List may be used 749df8bae1dSRodney W. Grimes * in later ioctl's (above) to get 750df8bae1dSRodney W. Grimes * other information. 751df8bae1dSRodney W. Grimes */ 752df8bae1dSRodney W. Grimes /*ARGSUSED*/ 7533bda9f9bSPoul-Henning Kamp static int 754df8bae1dSRodney W. Grimes ifconf(cmd, data) 755df8bae1dSRodney W. Grimes int cmd; 756df8bae1dSRodney W. Grimes caddr_t data; 757df8bae1dSRodney W. Grimes { 758df8bae1dSRodney W. Grimes register struct ifconf *ifc = (struct ifconf *)data; 75929412182SGarrett Wollman register struct ifnet *ifp = ifnet.tqh_first; 760df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 761df8bae1dSRodney W. Grimes struct ifreq ifr, *ifrp; 762df8bae1dSRodney W. Grimes int space = ifc->ifc_len, error = 0; 763df8bae1dSRodney W. Grimes 764df8bae1dSRodney W. Grimes ifrp = ifc->ifc_req; 76529412182SGarrett Wollman for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) { 7661ce9bf88SPoul-Henning Kamp char workbuf[64]; 7671ce9bf88SPoul-Henning Kamp int ifnlen; 7682624cf89SGarrett Wollman 7691ce9bf88SPoul-Henning Kamp ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit); 7701ce9bf88SPoul-Henning Kamp if(ifnlen + 1 > sizeof ifr.ifr_name) { 7712624cf89SGarrett Wollman error = ENAMETOOLONG; 7722624cf89SGarrett Wollman } else { 7731ce9bf88SPoul-Henning Kamp strcpy(ifr.ifr_name, workbuf); 7742624cf89SGarrett Wollman } 7752624cf89SGarrett Wollman 77659562606SGarrett Wollman if ((ifa = ifp->if_addrhead.tqh_first) == 0) { 777df8bae1dSRodney W. Grimes bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 778df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 779df8bae1dSRodney W. Grimes sizeof (ifr)); 780df8bae1dSRodney W. Grimes if (error) 781df8bae1dSRodney W. Grimes break; 782df8bae1dSRodney W. Grimes space -= sizeof (ifr), ifrp++; 783df8bae1dSRodney W. Grimes } else 78459562606SGarrett Wollman for ( ; space > sizeof (ifr) && ifa; 78559562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) { 786df8bae1dSRodney W. Grimes register struct sockaddr *sa = ifa->ifa_addr; 787df8bae1dSRodney W. Grimes #ifdef COMPAT_43 788df8bae1dSRodney W. Grimes if (cmd == OSIOCGIFCONF) { 789df8bae1dSRodney W. Grimes struct osockaddr *osa = 790df8bae1dSRodney W. Grimes (struct osockaddr *)&ifr.ifr_addr; 791df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 792df8bae1dSRodney W. Grimes osa->sa_family = sa->sa_family; 793df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 794df8bae1dSRodney W. Grimes sizeof (ifr)); 795df8bae1dSRodney W. Grimes ifrp++; 796df8bae1dSRodney W. Grimes } else 797df8bae1dSRodney W. Grimes #endif 798df8bae1dSRodney W. Grimes if (sa->sa_len <= sizeof(*sa)) { 799df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 800df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 801df8bae1dSRodney W. Grimes sizeof (ifr)); 802df8bae1dSRodney W. Grimes ifrp++; 803df8bae1dSRodney W. Grimes } else { 804df8bae1dSRodney W. Grimes space -= sa->sa_len - sizeof(*sa); 805df8bae1dSRodney W. Grimes if (space < sizeof (ifr)) 806df8bae1dSRodney W. Grimes break; 807df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 808df8bae1dSRodney W. Grimes sizeof (ifr.ifr_name)); 809df8bae1dSRodney W. Grimes if (error == 0) 810df8bae1dSRodney W. Grimes error = copyout((caddr_t)sa, 811df8bae1dSRodney W. Grimes (caddr_t)&ifrp->ifr_addr, sa->sa_len); 812df8bae1dSRodney W. Grimes ifrp = (struct ifreq *) 813df8bae1dSRodney W. Grimes (sa->sa_len + (caddr_t)&ifrp->ifr_addr); 814df8bae1dSRodney W. Grimes } 815df8bae1dSRodney W. Grimes if (error) 816df8bae1dSRodney W. Grimes break; 817df8bae1dSRodney W. Grimes space -= sizeof (ifr); 818df8bae1dSRodney W. Grimes } 819df8bae1dSRodney W. Grimes } 820df8bae1dSRodney W. Grimes ifc->ifc_len -= space; 821df8bae1dSRodney W. Grimes return (error); 822df8bae1dSRodney W. Grimes } 823df8bae1dSRodney W. Grimes 8241158dfb7SGarrett Wollman /* 8251158dfb7SGarrett Wollman * Just like if_promisc(), but for all-multicast-reception mode. 8261158dfb7SGarrett Wollman */ 8271158dfb7SGarrett Wollman int 8281158dfb7SGarrett Wollman if_allmulti(ifp, onswitch) 8291158dfb7SGarrett Wollman struct ifnet *ifp; 8301158dfb7SGarrett Wollman int onswitch; 8311158dfb7SGarrett Wollman { 8321158dfb7SGarrett Wollman int error = 0; 8331158dfb7SGarrett Wollman int s = splimp(); 8341158dfb7SGarrett Wollman 8351158dfb7SGarrett Wollman if (onswitch) { 8361158dfb7SGarrett Wollman if (ifp->if_amcount++ == 0) { 8371158dfb7SGarrett Wollman ifp->if_flags |= IFF_ALLMULTI; 8381158dfb7SGarrett Wollman error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 8391158dfb7SGarrett Wollman } 8401158dfb7SGarrett Wollman } else { 8411158dfb7SGarrett Wollman if (ifp->if_amcount > 1) { 8421158dfb7SGarrett Wollman ifp->if_amcount--; 8431158dfb7SGarrett Wollman } else { 8441158dfb7SGarrett Wollman ifp->if_amcount = 0; 8451158dfb7SGarrett Wollman ifp->if_flags &= ~IFF_ALLMULTI; 8461158dfb7SGarrett Wollman error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 8471158dfb7SGarrett Wollman } 8481158dfb7SGarrett Wollman } 8491158dfb7SGarrett Wollman splx(s); 8504a26224cSGarrett Wollman 8514a26224cSGarrett Wollman if (error == 0) 8524a26224cSGarrett Wollman rt_ifmsg(ifp); 8531158dfb7SGarrett Wollman return error; 8541158dfb7SGarrett Wollman } 8551158dfb7SGarrett Wollman 8561158dfb7SGarrett Wollman /* 8571158dfb7SGarrett Wollman * Add a multicast listenership to the interface in question. 8581158dfb7SGarrett Wollman * The link layer provides a routine which converts 8591158dfb7SGarrett Wollman */ 8601158dfb7SGarrett Wollman int 861373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma) 8621158dfb7SGarrett Wollman struct ifnet *ifp; /* interface to manipulate */ 8631158dfb7SGarrett Wollman struct sockaddr *sa; /* address to add */ 864b2053118SGarrett Wollman struct ifmultiaddr **retifma; 8651158dfb7SGarrett Wollman { 8661158dfb7SGarrett Wollman struct sockaddr *llsa, *dupsa; 8671158dfb7SGarrett Wollman int error, s; 8681158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 8691158dfb7SGarrett Wollman 87057af7922SJulian Elischer /* 87157af7922SJulian Elischer * If the matching multicast address already exists 87257af7922SJulian Elischer * then don't add a new one, just add a reference 87357af7922SJulian Elischer */ 8741158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 8751158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) { 87657af7922SJulian Elischer if (equal(sa, ifma->ifma_addr)) { 8771158dfb7SGarrett Wollman ifma->ifma_refcount++; 87857af7922SJulian Elischer if (retifma) 87957af7922SJulian Elischer *retifma = ifma; 8801158dfb7SGarrett Wollman return 0; 8811158dfb7SGarrett Wollman } 88257af7922SJulian Elischer } 8831158dfb7SGarrett Wollman 8841158dfb7SGarrett Wollman /* 8851158dfb7SGarrett Wollman * Give the link layer a chance to accept/reject it, and also 8861158dfb7SGarrett Wollman * find out which AF_LINK address this maps to, if it isn't one 8871158dfb7SGarrett Wollman * already. 8881158dfb7SGarrett Wollman */ 8891158dfb7SGarrett Wollman if (ifp->if_resolvemulti) { 8901158dfb7SGarrett Wollman error = ifp->if_resolvemulti(ifp, &llsa, sa); 8911158dfb7SGarrett Wollman if (error) return error; 8921158dfb7SGarrett Wollman } else { 8931158dfb7SGarrett Wollman llsa = 0; 8941158dfb7SGarrett Wollman } 8951158dfb7SGarrett Wollman 8961158dfb7SGarrett Wollman MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); 8971158dfb7SGarrett Wollman MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); 8981158dfb7SGarrett Wollman bcopy(sa, dupsa, sa->sa_len); 8991158dfb7SGarrett Wollman 9001158dfb7SGarrett Wollman ifma->ifma_addr = dupsa; 9011158dfb7SGarrett Wollman ifma->ifma_lladdr = llsa; 9021158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 9031158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 904373f88edSGarrett Wollman ifma->ifma_protospec = 0; 905477180fbSGarrett Wollman rt_newmaddrmsg(RTM_NEWMADDR, ifma); 906373f88edSGarrett Wollman 9071158dfb7SGarrett Wollman /* 9081158dfb7SGarrett Wollman * Some network interfaces can scan the address list at 9091158dfb7SGarrett Wollman * interrupt time; lock them out. 9101158dfb7SGarrett Wollman */ 9111158dfb7SGarrett Wollman s = splimp(); 9121158dfb7SGarrett Wollman LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 9131158dfb7SGarrett Wollman splx(s); 914373f88edSGarrett Wollman *retifma = ifma; 9151158dfb7SGarrett Wollman 9161158dfb7SGarrett Wollman if (llsa != 0) { 9171158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 9181158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) { 9191158dfb7SGarrett Wollman if (equal(ifma->ifma_addr, llsa)) 9201158dfb7SGarrett Wollman break; 9211158dfb7SGarrett Wollman } 9221158dfb7SGarrett Wollman if (ifma) { 9231158dfb7SGarrett Wollman ifma->ifma_refcount++; 9241158dfb7SGarrett Wollman } else { 9251158dfb7SGarrett Wollman MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, 9261158dfb7SGarrett Wollman M_IFMADDR, M_WAITOK); 927477180fbSGarrett Wollman MALLOC(dupsa, struct sockaddr *, llsa->sa_len, 928477180fbSGarrett Wollman M_IFMADDR, M_WAITOK); 929477180fbSGarrett Wollman bcopy(llsa, dupsa, llsa->sa_len); 930477180fbSGarrett Wollman ifma->ifma_addr = dupsa; 9311158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 9321158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 9331158dfb7SGarrett Wollman s = splimp(); 9341158dfb7SGarrett Wollman LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 9351158dfb7SGarrett Wollman splx(s); 9361158dfb7SGarrett Wollman } 93757af7922SJulian Elischer } 9381158dfb7SGarrett Wollman /* 9391158dfb7SGarrett Wollman * We are certain we have added something, so call down to the 9401158dfb7SGarrett Wollman * interface to let them know about it. 9411158dfb7SGarrett Wollman */ 9421158dfb7SGarrett Wollman s = splimp(); 9431158dfb7SGarrett Wollman ifp->if_ioctl(ifp, SIOCADDMULTI, 0); 9441158dfb7SGarrett Wollman splx(s); 9451158dfb7SGarrett Wollman 9461158dfb7SGarrett Wollman return 0; 9471158dfb7SGarrett Wollman } 9481158dfb7SGarrett Wollman 9491158dfb7SGarrett Wollman /* 9501158dfb7SGarrett Wollman * Remove a reference to a multicast address on this interface. Yell 9511158dfb7SGarrett Wollman * if the request does not match an existing membership. 9521158dfb7SGarrett Wollman */ 9531158dfb7SGarrett Wollman int 9541158dfb7SGarrett Wollman if_delmulti(ifp, sa) 9551158dfb7SGarrett Wollman struct ifnet *ifp; 9561158dfb7SGarrett Wollman struct sockaddr *sa; 9571158dfb7SGarrett Wollman { 9581158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 9591158dfb7SGarrett Wollman int s; 9601158dfb7SGarrett Wollman 9611158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 9621158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) 9631158dfb7SGarrett Wollman if (equal(sa, ifma->ifma_addr)) 9641158dfb7SGarrett Wollman break; 9651158dfb7SGarrett Wollman if (ifma == 0) 9661158dfb7SGarrett Wollman return ENOENT; 9671158dfb7SGarrett Wollman 9681158dfb7SGarrett Wollman if (ifma->ifma_refcount > 1) { 9691158dfb7SGarrett Wollman ifma->ifma_refcount--; 9701158dfb7SGarrett Wollman return 0; 9711158dfb7SGarrett Wollman } 9721158dfb7SGarrett Wollman 973477180fbSGarrett Wollman rt_newmaddrmsg(RTM_DELMADDR, ifma); 9741158dfb7SGarrett Wollman sa = ifma->ifma_lladdr; 9751158dfb7SGarrett Wollman s = splimp(); 9761158dfb7SGarrett Wollman LIST_REMOVE(ifma, ifma_link); 9771158dfb7SGarrett Wollman splx(s); 9781158dfb7SGarrett Wollman free(ifma->ifma_addr, M_IFMADDR); 9791158dfb7SGarrett Wollman free(ifma, M_IFMADDR); 9801158dfb7SGarrett Wollman if (sa == 0) 9811158dfb7SGarrett Wollman return 0; 9821158dfb7SGarrett Wollman 9831158dfb7SGarrett Wollman /* 9841158dfb7SGarrett Wollman * Now look for the link-layer address which corresponds to 9851158dfb7SGarrett Wollman * this network address. It had been squirreled away in 9861158dfb7SGarrett Wollman * ifma->ifma_lladdr for this purpose (so we don't have 9871158dfb7SGarrett Wollman * to call ifp->if_resolvemulti() again), and we saved that 9881158dfb7SGarrett Wollman * value in sa above. If some nasty deleted the 9891158dfb7SGarrett Wollman * link-layer address out from underneath us, we can deal because 9901158dfb7SGarrett Wollman * the address we stored was is not the same as the one which was 9911158dfb7SGarrett Wollman * in the record for the link-layer address. (So we don't complain 9921158dfb7SGarrett Wollman * in that case.) 9931158dfb7SGarrett Wollman */ 9941158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 9951158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) 9961158dfb7SGarrett Wollman if (equal(sa, ifma->ifma_addr)) 9971158dfb7SGarrett Wollman break; 9981158dfb7SGarrett Wollman if (ifma == 0) 9991158dfb7SGarrett Wollman return 0; 10001158dfb7SGarrett Wollman 10011158dfb7SGarrett Wollman if (ifma->ifma_refcount > 1) { 10021158dfb7SGarrett Wollman ifma->ifma_refcount--; 10031158dfb7SGarrett Wollman return 0; 10041158dfb7SGarrett Wollman } 10051158dfb7SGarrett Wollman 10061158dfb7SGarrett Wollman s = splimp(); 10071158dfb7SGarrett Wollman LIST_REMOVE(ifma, ifma_link); 10081158dfb7SGarrett Wollman splx(s); 10091158dfb7SGarrett Wollman free(ifma->ifma_addr, M_IFMADDR); 10101158dfb7SGarrett Wollman free(sa, M_IFMADDR); 10111158dfb7SGarrett Wollman free(ifma, M_IFMADDR); 10121158dfb7SGarrett Wollman 10131158dfb7SGarrett Wollman return 0; 10141158dfb7SGarrett Wollman } 10151158dfb7SGarrett Wollman 1016373f88edSGarrett Wollman struct ifmultiaddr * 1017373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp) 1018373f88edSGarrett Wollman struct sockaddr *sa; 1019373f88edSGarrett Wollman struct ifnet *ifp; 1020373f88edSGarrett Wollman { 1021373f88edSGarrett Wollman struct ifmultiaddr *ifma; 1022373f88edSGarrett Wollman 1023373f88edSGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 1024373f88edSGarrett Wollman ifma = ifma->ifma_link.le_next) 1025373f88edSGarrett Wollman if (equal(ifma->ifma_addr, sa)) 1026373f88edSGarrett Wollman break; 1027373f88edSGarrett Wollman 1028373f88edSGarrett Wollman return ifma; 1029373f88edSGarrett Wollman } 1030373f88edSGarrett Wollman 1031602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 10322c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 1033