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 3457af7922SJulian Elischer * $Id: if.c,v 1.48 1997/05/03 21:07:13 peter 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 238df8bae1dSRodney W. Grimes if (af == AF_LINK) { 239df8bae1dSRodney W. Grimes register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 240df8bae1dSRodney W. Grimes if (sdl->sdl_index && sdl->sdl_index <= if_index) 241df8bae1dSRodney W. Grimes return (ifnet_addrs[sdl->sdl_index - 1]); 242df8bae1dSRodney W. Grimes } 24329412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 24459562606SGarrett Wollman for (ifa = ifp->if_addrhead.tqh_first; ifa; 24559562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) { 246df8bae1dSRodney W. Grimes register char *cp, *cp2, *cp3; 247df8bae1dSRodney W. Grimes 248523a02aaSDavid Greenman if (ifa->ifa_addr->sa_family != af) 249df8bae1dSRodney W. Grimes next: continue; 250b2af64fdSDavid Greenman if (ifp->if_flags & IFF_POINTOPOINT) { 251fcd6781aSGarrett Wollman if (ifa->ifa_dstaddr != 0 252fcd6781aSGarrett Wollman && equal(addr, ifa->ifa_dstaddr)) 253b2af64fdSDavid Greenman return (ifa); 2543740e2adSDavid Greenman } else { 255523a02aaSDavid Greenman if (ifa->ifa_netmask == 0) 256523a02aaSDavid Greenman continue; 257df8bae1dSRodney W. Grimes cp = addr_data; 258df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 259df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 260df8bae1dSRodney W. Grimes cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 261df8bae1dSRodney W. Grimes while (cp3 < cplim) 262df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3++) 263df8bae1dSRodney W. Grimes goto next; 264df8bae1dSRodney W. Grimes if (ifa_maybe == 0 || 265df8bae1dSRodney W. Grimes rn_refines((caddr_t)ifa->ifa_netmask, 266df8bae1dSRodney W. Grimes (caddr_t)ifa_maybe->ifa_netmask)) 267df8bae1dSRodney W. Grimes ifa_maybe = ifa; 268df8bae1dSRodney W. Grimes } 269b2af64fdSDavid Greenman } 270b2af64fdSDavid Greenman } 271df8bae1dSRodney W. Grimes return (ifa_maybe); 272df8bae1dSRodney W. Grimes } 273df8bae1dSRodney W. Grimes 274df8bae1dSRodney W. Grimes /* 275df8bae1dSRodney W. Grimes * Find an interface address specific to an interface best matching 276df8bae1dSRodney W. Grimes * a given address. 277df8bae1dSRodney W. Grimes */ 278df8bae1dSRodney W. Grimes struct ifaddr * 279df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp) 280df8bae1dSRodney W. Grimes struct sockaddr *addr; 281df8bae1dSRodney W. Grimes register struct ifnet *ifp; 282df8bae1dSRodney W. Grimes { 283df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 284df8bae1dSRodney W. Grimes register char *cp, *cp2, *cp3; 285df8bae1dSRodney W. Grimes register char *cplim; 286df8bae1dSRodney W. Grimes struct ifaddr *ifa_maybe = 0; 287df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 288df8bae1dSRodney W. Grimes 289df8bae1dSRodney W. Grimes if (af >= AF_MAX) 290df8bae1dSRodney W. Grimes return (0); 29159562606SGarrett Wollman for (ifa = ifp->if_addrhead.tqh_first; ifa; 29259562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) { 293df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != af) 294df8bae1dSRodney W. Grimes continue; 295381dd1d2SJulian Elischer if (ifa_maybe == 0) 296df8bae1dSRodney W. Grimes ifa_maybe = ifa; 297df8bae1dSRodney W. Grimes if (ifa->ifa_netmask == 0) { 298df8bae1dSRodney W. Grimes if (equal(addr, ifa->ifa_addr) || 299df8bae1dSRodney W. Grimes (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 300df8bae1dSRodney W. Grimes return (ifa); 301df8bae1dSRodney W. Grimes continue; 302df8bae1dSRodney W. Grimes } 303b2af64fdSDavid Greenman if (ifp->if_flags & IFF_POINTOPOINT) { 304b2af64fdSDavid Greenman if (equal(addr, ifa->ifa_dstaddr)) 305b2af64fdSDavid Greenman return (ifa); 3063740e2adSDavid Greenman } else { 307df8bae1dSRodney W. Grimes cp = addr->sa_data; 308df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 309df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 310df8bae1dSRodney W. Grimes cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 311df8bae1dSRodney W. Grimes for (; cp3 < cplim; cp3++) 312df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3) 313df8bae1dSRodney W. Grimes break; 314df8bae1dSRodney W. Grimes if (cp3 == cplim) 315df8bae1dSRodney W. Grimes return (ifa); 316df8bae1dSRodney W. Grimes } 317b2af64fdSDavid Greenman } 318df8bae1dSRodney W. Grimes return (ifa_maybe); 319df8bae1dSRodney W. Grimes } 320df8bae1dSRodney W. Grimes 321df8bae1dSRodney W. Grimes #include <net/route.h> 322df8bae1dSRodney W. Grimes 323df8bae1dSRodney W. Grimes /* 324df8bae1dSRodney W. Grimes * Default action when installing a route with a Link Level gateway. 325df8bae1dSRodney W. Grimes * Lookup an appropriate real ifa to point to. 326df8bae1dSRodney W. Grimes * This should be moved to /sys/net/link.c eventually. 327df8bae1dSRodney W. Grimes */ 3283bda9f9bSPoul-Henning Kamp static void 329df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa) 330df8bae1dSRodney W. Grimes int cmd; 331df8bae1dSRodney W. Grimes register struct rtentry *rt; 332df8bae1dSRodney W. Grimes struct sockaddr *sa; 333df8bae1dSRodney W. Grimes { 334df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 335df8bae1dSRodney W. Grimes struct sockaddr *dst; 336df8bae1dSRodney W. Grimes struct ifnet *ifp; 337df8bae1dSRodney W. Grimes 338df8bae1dSRodney W. Grimes if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 339df8bae1dSRodney W. Grimes ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 340df8bae1dSRodney W. Grimes return; 3419448326fSPoul-Henning Kamp ifa = ifaof_ifpforaddr(dst, ifp); 3429448326fSPoul-Henning Kamp if (ifa) { 343df8bae1dSRodney W. Grimes IFAFREE(rt->rt_ifa); 344df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 345df8bae1dSRodney W. Grimes ifa->ifa_refcnt++; 346df8bae1dSRodney W. Grimes if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 347df8bae1dSRodney W. Grimes ifa->ifa_rtrequest(cmd, rt, sa); 348df8bae1dSRodney W. Grimes } 349df8bae1dSRodney W. Grimes } 350df8bae1dSRodney W. Grimes 351df8bae1dSRodney W. Grimes /* 352df8bae1dSRodney W. Grimes * Mark an interface down and notify protocols of 353df8bae1dSRodney W. Grimes * the transition. 354df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 355df8bae1dSRodney W. Grimes */ 356df8bae1dSRodney W. Grimes void 357df8bae1dSRodney W. Grimes if_down(ifp) 358df8bae1dSRodney W. Grimes register struct ifnet *ifp; 359df8bae1dSRodney W. Grimes { 360df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 361df8bae1dSRodney W. Grimes 362df8bae1dSRodney W. Grimes ifp->if_flags &= ~IFF_UP; 363a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 36459562606SGarrett Wollman for (ifa = ifp->if_addrhead.tqh_first; ifa; 36559562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) 366df8bae1dSRodney W. Grimes pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 367df8bae1dSRodney W. Grimes if_qflush(&ifp->if_snd); 368df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 369df8bae1dSRodney W. Grimes } 370df8bae1dSRodney W. Grimes 371df8bae1dSRodney W. Grimes /* 372df8bae1dSRodney W. Grimes * Mark an interface up and notify protocols of 373df8bae1dSRodney W. Grimes * the transition. 374df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 375df8bae1dSRodney W. Grimes */ 376df8bae1dSRodney W. Grimes void 377df8bae1dSRodney W. Grimes if_up(ifp) 378df8bae1dSRodney W. Grimes register struct ifnet *ifp; 379df8bae1dSRodney W. Grimes { 380176395b2SGarrett Wollman register struct ifaddr *ifa; 381df8bae1dSRodney W. Grimes 382df8bae1dSRodney W. Grimes ifp->if_flags |= IFF_UP; 383a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 384176395b2SGarrett Wollman for (ifa = ifp->if_addrhead.tqh_first; ifa; 385176395b2SGarrett Wollman ifa = ifa->ifa_link.tqe_next) 386df8bae1dSRodney W. Grimes pfctlinput(PRC_IFUP, ifa->ifa_addr); 387df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 388df8bae1dSRodney W. Grimes } 389df8bae1dSRodney W. Grimes 390df8bae1dSRodney W. Grimes /* 391df8bae1dSRodney W. Grimes * Flush an interface queue. 392df8bae1dSRodney W. Grimes */ 3933bda9f9bSPoul-Henning Kamp static void 394df8bae1dSRodney W. Grimes if_qflush(ifq) 395df8bae1dSRodney W. Grimes register struct ifqueue *ifq; 396df8bae1dSRodney W. Grimes { 397df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 398df8bae1dSRodney W. Grimes 399df8bae1dSRodney W. Grimes n = ifq->ifq_head; 4009448326fSPoul-Henning Kamp while ((m = n) != 0) { 401df8bae1dSRodney W. Grimes n = m->m_act; 402df8bae1dSRodney W. Grimes m_freem(m); 403df8bae1dSRodney W. Grimes } 404df8bae1dSRodney W. Grimes ifq->ifq_head = 0; 405df8bae1dSRodney W. Grimes ifq->ifq_tail = 0; 406df8bae1dSRodney W. Grimes ifq->ifq_len = 0; 407df8bae1dSRodney W. Grimes } 408df8bae1dSRodney W. Grimes 409df8bae1dSRodney W. Grimes /* 410df8bae1dSRodney W. Grimes * Handle interface watchdog timer routines. Called 411df8bae1dSRodney W. Grimes * from softclock, we decrement timers (if set) and 412df8bae1dSRodney W. Grimes * call the appropriate interface routine on expiration. 413df8bae1dSRodney W. Grimes */ 4143bda9f9bSPoul-Henning Kamp static void 415df8bae1dSRodney W. Grimes if_slowtimo(arg) 416df8bae1dSRodney W. Grimes void *arg; 417df8bae1dSRodney W. Grimes { 418df8bae1dSRodney W. Grimes register struct ifnet *ifp; 419df8bae1dSRodney W. Grimes int s = splimp(); 420df8bae1dSRodney W. Grimes 42129412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 422df8bae1dSRodney W. Grimes if (ifp->if_timer == 0 || --ifp->if_timer) 423df8bae1dSRodney W. Grimes continue; 424df8bae1dSRodney W. Grimes if (ifp->if_watchdog) 4254a5f1499SDavid Greenman (*ifp->if_watchdog)(ifp); 426df8bae1dSRodney W. Grimes } 427df8bae1dSRodney W. Grimes splx(s); 428df8bae1dSRodney W. Grimes timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 429df8bae1dSRodney W. Grimes } 430df8bae1dSRodney W. Grimes 431df8bae1dSRodney W. Grimes /* 432df8bae1dSRodney W. Grimes * Map interface name to 433df8bae1dSRodney W. Grimes * interface structure pointer. 434df8bae1dSRodney W. Grimes */ 435df8bae1dSRodney W. Grimes struct ifnet * 436df8bae1dSRodney W. Grimes ifunit(name) 437df8bae1dSRodney W. Grimes register char *name; 438df8bae1dSRodney W. Grimes { 439df8bae1dSRodney W. Grimes register char *cp; 440df8bae1dSRodney W. Grimes register struct ifnet *ifp; 441df8bae1dSRodney W. Grimes int unit; 442df8bae1dSRodney W. Grimes unsigned len; 443df8bae1dSRodney W. Grimes char *ep, c; 444df8bae1dSRodney W. Grimes 445df8bae1dSRodney W. Grimes for (cp = name; cp < name + IFNAMSIZ && *cp; cp++) 446df8bae1dSRodney W. Grimes if (*cp >= '0' && *cp <= '9') 447df8bae1dSRodney W. Grimes break; 448df8bae1dSRodney W. Grimes if (*cp == '\0' || cp == name + IFNAMSIZ) 449df8bae1dSRodney W. Grimes return ((struct ifnet *)0); 450df8bae1dSRodney W. Grimes /* 451df8bae1dSRodney W. Grimes * Save first char of unit, and pointer to it, 452df8bae1dSRodney W. Grimes * so we can put a null there to avoid matching 453df8bae1dSRodney W. Grimes * initial substrings of interface names. 454df8bae1dSRodney W. Grimes */ 455df8bae1dSRodney W. Grimes len = cp - name + 1; 456df8bae1dSRodney W. Grimes c = *cp; 457df8bae1dSRodney W. Grimes ep = cp; 458df8bae1dSRodney W. Grimes for (unit = 0; *cp >= '0' && *cp <= '9'; ) 459df8bae1dSRodney W. Grimes unit = unit * 10 + *cp++ - '0'; 46058639ed3SGarrett Wollman if (*cp != '\0') 46158639ed3SGarrett Wollman return 0; /* no trailing garbage allowed */ 462df8bae1dSRodney W. Grimes *ep = 0; 46329412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 464df8bae1dSRodney W. Grimes if (bcmp(ifp->if_name, name, len)) 465df8bae1dSRodney W. Grimes continue; 466df8bae1dSRodney W. Grimes if (unit == ifp->if_unit) 467df8bae1dSRodney W. Grimes break; 468df8bae1dSRodney W. Grimes } 469df8bae1dSRodney W. Grimes *ep = c; 470df8bae1dSRodney W. Grimes return (ifp); 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes 473df8bae1dSRodney W. Grimes /* 474df8bae1dSRodney W. Grimes * Interface ioctls. 475df8bae1dSRodney W. Grimes */ 476df8bae1dSRodney W. Grimes int 477df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p) 478df8bae1dSRodney W. Grimes struct socket *so; 479df8bae1dSRodney W. Grimes int cmd; 480df8bae1dSRodney W. Grimes caddr_t data; 481df8bae1dSRodney W. Grimes struct proc *p; 482df8bae1dSRodney W. Grimes { 483df8bae1dSRodney W. Grimes register struct ifnet *ifp; 484df8bae1dSRodney W. Grimes register struct ifreq *ifr; 485df8bae1dSRodney W. Grimes int error; 486df8bae1dSRodney W. Grimes 487df8bae1dSRodney W. Grimes switch (cmd) { 488df8bae1dSRodney W. Grimes 489df8bae1dSRodney W. Grimes case SIOCGIFCONF: 490df8bae1dSRodney W. Grimes case OSIOCGIFCONF: 491df8bae1dSRodney W. Grimes return (ifconf(cmd, data)); 492df8bae1dSRodney W. Grimes } 493df8bae1dSRodney W. Grimes ifr = (struct ifreq *)data; 494df8bae1dSRodney W. Grimes ifp = ifunit(ifr->ifr_name); 495df8bae1dSRodney W. Grimes if (ifp == 0) 496df8bae1dSRodney W. Grimes return (ENXIO); 497df8bae1dSRodney W. Grimes switch (cmd) { 498df8bae1dSRodney W. Grimes 499df8bae1dSRodney W. Grimes case SIOCGIFFLAGS: 500df8bae1dSRodney W. Grimes ifr->ifr_flags = ifp->if_flags; 501df8bae1dSRodney W. Grimes break; 502df8bae1dSRodney W. Grimes 503df8bae1dSRodney W. Grimes case SIOCGIFMETRIC: 504df8bae1dSRodney W. Grimes ifr->ifr_metric = ifp->if_metric; 505df8bae1dSRodney W. Grimes break; 506df8bae1dSRodney W. Grimes 507a7028af7SDavid Greenman case SIOCGIFMTU: 508a7028af7SDavid Greenman ifr->ifr_mtu = ifp->if_mtu; 509a7028af7SDavid Greenman break; 510a7028af7SDavid Greenman 511074c4a4eSGarrett Wollman case SIOCGIFPHYS: 512074c4a4eSGarrett Wollman ifr->ifr_phys = ifp->if_physical; 513074c4a4eSGarrett Wollman break; 514074c4a4eSGarrett Wollman 515df8bae1dSRodney W. Grimes case SIOCSIFFLAGS: 5169448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5179448326fSPoul-Henning Kamp if (error) 518df8bae1dSRodney W. Grimes return (error); 519df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 520df8bae1dSRodney W. Grimes int s = splimp(); 521df8bae1dSRodney W. Grimes if_down(ifp); 522df8bae1dSRodney W. Grimes splx(s); 523df8bae1dSRodney W. Grimes } 524df8bae1dSRodney W. Grimes if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 525df8bae1dSRodney W. Grimes int s = splimp(); 526df8bae1dSRodney W. Grimes if_up(ifp); 527df8bae1dSRodney W. Grimes splx(s); 528df8bae1dSRodney W. Grimes } 529df8bae1dSRodney W. Grimes ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 530df8bae1dSRodney W. Grimes (ifr->ifr_flags &~ IFF_CANTCHANGE); 531df8bae1dSRodney W. Grimes if (ifp->if_ioctl) 532df8bae1dSRodney W. Grimes (void) (*ifp->if_ioctl)(ifp, cmd, data); 533a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 534df8bae1dSRodney W. Grimes break; 535df8bae1dSRodney W. Grimes 536df8bae1dSRodney W. Grimes case SIOCSIFMETRIC: 5379448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5389448326fSPoul-Henning Kamp if (error) 539df8bae1dSRodney W. Grimes return (error); 540df8bae1dSRodney W. Grimes ifp->if_metric = ifr->ifr_metric; 541a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 542df8bae1dSRodney W. Grimes break; 543df8bae1dSRodney W. Grimes 544074c4a4eSGarrett Wollman case SIOCSIFPHYS: 545074c4a4eSGarrett Wollman error = suser(p->p_ucred, &p->p_acflag); 546e39a0280SGary Palmer if (error) 547e39a0280SGary Palmer return error; 548e39a0280SGary Palmer if (!ifp->if_ioctl) 549e39a0280SGary Palmer return EOPNOTSUPP; 550e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 551e39a0280SGary Palmer if (error == 0) 552a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 553e39a0280SGary Palmer return(error); 554074c4a4eSGarrett Wollman 555a7028af7SDavid Greenman case SIOCSIFMTU: 5569448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5579448326fSPoul-Henning Kamp if (error) 558a7028af7SDavid Greenman return (error); 559a7028af7SDavid Greenman if (ifp->if_ioctl == NULL) 560a7028af7SDavid Greenman return (EOPNOTSUPP); 56166025569SDavid Greenman /* 56266025569SDavid Greenman * 72 was chosen below because it is the size of a TCP/IP 56366025569SDavid Greenman * header (40) + the minimum mss (32). 56466025569SDavid Greenman */ 56566025569SDavid Greenman if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535) 56675ee03cbSDavid Greenman return (EINVAL); 567e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 568e39a0280SGary Palmer if (error == 0) 569a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 570e39a0280SGary Palmer return(error); 571a7028af7SDavid Greenman 572df8bae1dSRodney W. Grimes case SIOCADDMULTI: 573df8bae1dSRodney W. Grimes case SIOCDELMULTI: 5749448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5759448326fSPoul-Henning Kamp if (error) 576df8bae1dSRodney W. Grimes return (error); 577477180fbSGarrett Wollman 578477180fbSGarrett Wollman /* Don't allow group membership on non-multicast interfaces. */ 579477180fbSGarrett Wollman if ((ifp->if_flags & IFF_MULTICAST) == 0) 580477180fbSGarrett Wollman return EOPNOTSUPP; 581477180fbSGarrett Wollman 582477180fbSGarrett Wollman /* Don't let users screw up protocols' entries. */ 583477180fbSGarrett Wollman if (ifr->ifr_addr.sa_family != AF_LINK) 584477180fbSGarrett Wollman return EINVAL; 585477180fbSGarrett Wollman 586477180fbSGarrett Wollman if (cmd == SIOCADDMULTI) { 587477180fbSGarrett Wollman struct ifmultiaddr *ifma; 588477180fbSGarrett Wollman error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 589477180fbSGarrett Wollman } else { 590477180fbSGarrett Wollman error = if_delmulti(ifp, &ifr->ifr_addr); 591477180fbSGarrett Wollman } 592e39a0280SGary Palmer if (error == 0) 593a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 594477180fbSGarrett Wollman return error; 595df8bae1dSRodney W. Grimes 596a912e453SPeter Wemm case SIOCSIFMEDIA: 597a912e453SPeter Wemm error = suser(p->p_ucred, &p->p_acflag); 598a912e453SPeter Wemm if (error) 599a912e453SPeter Wemm return (error); 600a912e453SPeter Wemm if (ifp->if_ioctl == 0) 601a912e453SPeter Wemm return (EOPNOTSUPP); 602a912e453SPeter Wemm error = (*ifp->if_ioctl)(ifp, cmd, data); 603a912e453SPeter Wemm if (error == 0) 604a912e453SPeter Wemm microtime(&ifp->if_lastchange); 605a912e453SPeter Wemm return error; 606a912e453SPeter Wemm 607a912e453SPeter Wemm case SIOCGIFMEDIA: 608a912e453SPeter Wemm if (ifp->if_ioctl == 0) 609a912e453SPeter Wemm return (EOPNOTSUPP); 610a912e453SPeter Wemm return ((*ifp->if_ioctl)(ifp, cmd, data)); 611a912e453SPeter Wemm 612df8bae1dSRodney W. Grimes default: 613df8bae1dSRodney W. Grimes if (so->so_proto == 0) 614df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 615df8bae1dSRodney W. Grimes #ifndef COMPAT_43 6162c37256eSGarrett Wollman return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 6172c37256eSGarrett Wollman data, 6182c37256eSGarrett Wollman ifp)); 619df8bae1dSRodney W. Grimes #else 620df8bae1dSRodney W. Grimes { 621df8bae1dSRodney W. Grimes int ocmd = cmd; 622df8bae1dSRodney W. Grimes 623df8bae1dSRodney W. Grimes switch (cmd) { 624df8bae1dSRodney W. Grimes 625df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 626df8bae1dSRodney W. Grimes case SIOCSIFADDR: 627df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 628df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 629df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN 630df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_family == 0 && 631df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len < 16) { 632df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 633df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 634df8bae1dSRodney W. Grimes } 635df8bae1dSRodney W. Grimes #else 636df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_len == 0) 637df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 638df8bae1dSRodney W. Grimes #endif 639df8bae1dSRodney W. Grimes break; 640df8bae1dSRodney W. Grimes 641df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 642df8bae1dSRodney W. Grimes cmd = SIOCGIFADDR; 643df8bae1dSRodney W. Grimes break; 644df8bae1dSRodney W. Grimes 645df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 646df8bae1dSRodney W. Grimes cmd = SIOCGIFDSTADDR; 647df8bae1dSRodney W. Grimes break; 648df8bae1dSRodney W. Grimes 649df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 650df8bae1dSRodney W. Grimes cmd = SIOCGIFBRDADDR; 651df8bae1dSRodney W. Grimes break; 652df8bae1dSRodney W. Grimes 653df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 654df8bae1dSRodney W. Grimes cmd = SIOCGIFNETMASK; 655df8bae1dSRodney W. Grimes } 6562c37256eSGarrett Wollman error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 6572c37256eSGarrett Wollman cmd, 6582c37256eSGarrett Wollman data, 659a29f300eSGarrett Wollman ifp, p)); 660df8bae1dSRodney W. Grimes switch (ocmd) { 661df8bae1dSRodney W. Grimes 662df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 663df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 664df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 665df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 666df8bae1dSRodney W. Grimes *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 667df8bae1dSRodney W. Grimes } 668df8bae1dSRodney W. Grimes return (error); 669df8bae1dSRodney W. Grimes 670df8bae1dSRodney W. Grimes } 671df8bae1dSRodney W. Grimes #endif 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes return (0); 674df8bae1dSRodney W. Grimes } 675df8bae1dSRodney W. Grimes 676df8bae1dSRodney W. Grimes /* 677963e4c2aSGarrett Wollman * Set/clear promiscuous mode on interface ifp based on the truth value 678963e4c2aSGarrett Wollman * of pswitch. The calls are reference counted so that only the first 679963e4c2aSGarrett Wollman * "on" request actually has an effect, as does the final "off" request. 680963e4c2aSGarrett Wollman * Results are undefined if the "off" and "on" requests are not matched. 681963e4c2aSGarrett Wollman */ 682963e4c2aSGarrett Wollman int 683963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch) 684963e4c2aSGarrett Wollman struct ifnet *ifp; 685963e4c2aSGarrett Wollman int pswitch; 686963e4c2aSGarrett Wollman { 687963e4c2aSGarrett Wollman struct ifreq ifr; 6884a26224cSGarrett Wollman int error; 689963e4c2aSGarrett Wollman 690963e4c2aSGarrett Wollman if (pswitch) { 691963e4c2aSGarrett Wollman /* 692963e4c2aSGarrett Wollman * If the device is not configured up, we cannot put it in 693963e4c2aSGarrett Wollman * promiscuous mode. 694963e4c2aSGarrett Wollman */ 695963e4c2aSGarrett Wollman if ((ifp->if_flags & IFF_UP) == 0) 696963e4c2aSGarrett Wollman return (ENETDOWN); 697963e4c2aSGarrett Wollman if (ifp->if_pcount++ != 0) 698963e4c2aSGarrett Wollman return (0); 699963e4c2aSGarrett Wollman ifp->if_flags |= IFF_PROMISC; 700e9a30d00SGarrett Wollman log(LOG_INFO, "%s%d: promiscuous mode enabled\n", 701963e4c2aSGarrett Wollman ifp->if_name, ifp->if_unit); 702963e4c2aSGarrett Wollman } else { 703963e4c2aSGarrett Wollman if (--ifp->if_pcount > 0) 704963e4c2aSGarrett Wollman return (0); 705963e4c2aSGarrett Wollman ifp->if_flags &= ~IFF_PROMISC; 706963e4c2aSGarrett Wollman } 707963e4c2aSGarrett Wollman ifr.ifr_flags = ifp->if_flags; 7084a26224cSGarrett Wollman error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 7094a26224cSGarrett Wollman if (error == 0) 7104a26224cSGarrett Wollman rt_ifmsg(ifp); 7114a26224cSGarrett Wollman return error; 712963e4c2aSGarrett Wollman } 713963e4c2aSGarrett Wollman 714963e4c2aSGarrett Wollman /* 715df8bae1dSRodney W. Grimes * Return interface configuration 716df8bae1dSRodney W. Grimes * of system. List may be used 717df8bae1dSRodney W. Grimes * in later ioctl's (above) to get 718df8bae1dSRodney W. Grimes * other information. 719df8bae1dSRodney W. Grimes */ 720df8bae1dSRodney W. Grimes /*ARGSUSED*/ 7213bda9f9bSPoul-Henning Kamp static int 722df8bae1dSRodney W. Grimes ifconf(cmd, data) 723df8bae1dSRodney W. Grimes int cmd; 724df8bae1dSRodney W. Grimes caddr_t data; 725df8bae1dSRodney W. Grimes { 726df8bae1dSRodney W. Grimes register struct ifconf *ifc = (struct ifconf *)data; 72729412182SGarrett Wollman register struct ifnet *ifp = ifnet.tqh_first; 728df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 729df8bae1dSRodney W. Grimes struct ifreq ifr, *ifrp; 730df8bae1dSRodney W. Grimes int space = ifc->ifc_len, error = 0; 731df8bae1dSRodney W. Grimes 732df8bae1dSRodney W. Grimes ifrp = ifc->ifc_req; 73329412182SGarrett Wollman for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) { 7341ce9bf88SPoul-Henning Kamp char workbuf[64]; 7351ce9bf88SPoul-Henning Kamp int ifnlen; 7362624cf89SGarrett Wollman 7371ce9bf88SPoul-Henning Kamp ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit); 7381ce9bf88SPoul-Henning Kamp if(ifnlen + 1 > sizeof ifr.ifr_name) { 7392624cf89SGarrett Wollman error = ENAMETOOLONG; 7402624cf89SGarrett Wollman } else { 7411ce9bf88SPoul-Henning Kamp strcpy(ifr.ifr_name, workbuf); 7422624cf89SGarrett Wollman } 7432624cf89SGarrett Wollman 74459562606SGarrett Wollman if ((ifa = ifp->if_addrhead.tqh_first) == 0) { 745df8bae1dSRodney W. Grimes bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 746df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 747df8bae1dSRodney W. Grimes sizeof (ifr)); 748df8bae1dSRodney W. Grimes if (error) 749df8bae1dSRodney W. Grimes break; 750df8bae1dSRodney W. Grimes space -= sizeof (ifr), ifrp++; 751df8bae1dSRodney W. Grimes } else 75259562606SGarrett Wollman for ( ; space > sizeof (ifr) && ifa; 75359562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) { 754df8bae1dSRodney W. Grimes register struct sockaddr *sa = ifa->ifa_addr; 755df8bae1dSRodney W. Grimes #ifdef COMPAT_43 756df8bae1dSRodney W. Grimes if (cmd == OSIOCGIFCONF) { 757df8bae1dSRodney W. Grimes struct osockaddr *osa = 758df8bae1dSRodney W. Grimes (struct osockaddr *)&ifr.ifr_addr; 759df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 760df8bae1dSRodney W. Grimes osa->sa_family = sa->sa_family; 761df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 762df8bae1dSRodney W. Grimes sizeof (ifr)); 763df8bae1dSRodney W. Grimes ifrp++; 764df8bae1dSRodney W. Grimes } else 765df8bae1dSRodney W. Grimes #endif 766df8bae1dSRodney W. Grimes if (sa->sa_len <= sizeof(*sa)) { 767df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 768df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 769df8bae1dSRodney W. Grimes sizeof (ifr)); 770df8bae1dSRodney W. Grimes ifrp++; 771df8bae1dSRodney W. Grimes } else { 772df8bae1dSRodney W. Grimes space -= sa->sa_len - sizeof(*sa); 773df8bae1dSRodney W. Grimes if (space < sizeof (ifr)) 774df8bae1dSRodney W. Grimes break; 775df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 776df8bae1dSRodney W. Grimes sizeof (ifr.ifr_name)); 777df8bae1dSRodney W. Grimes if (error == 0) 778df8bae1dSRodney W. Grimes error = copyout((caddr_t)sa, 779df8bae1dSRodney W. Grimes (caddr_t)&ifrp->ifr_addr, sa->sa_len); 780df8bae1dSRodney W. Grimes ifrp = (struct ifreq *) 781df8bae1dSRodney W. Grimes (sa->sa_len + (caddr_t)&ifrp->ifr_addr); 782df8bae1dSRodney W. Grimes } 783df8bae1dSRodney W. Grimes if (error) 784df8bae1dSRodney W. Grimes break; 785df8bae1dSRodney W. Grimes space -= sizeof (ifr); 786df8bae1dSRodney W. Grimes } 787df8bae1dSRodney W. Grimes } 788df8bae1dSRodney W. Grimes ifc->ifc_len -= space; 789df8bae1dSRodney W. Grimes return (error); 790df8bae1dSRodney W. Grimes } 791df8bae1dSRodney W. Grimes 7921158dfb7SGarrett Wollman /* 7931158dfb7SGarrett Wollman * Just like if_promisc(), but for all-multicast-reception mode. 7941158dfb7SGarrett Wollman */ 7951158dfb7SGarrett Wollman int 7961158dfb7SGarrett Wollman if_allmulti(ifp, onswitch) 7971158dfb7SGarrett Wollman struct ifnet *ifp; 7981158dfb7SGarrett Wollman int onswitch; 7991158dfb7SGarrett Wollman { 8001158dfb7SGarrett Wollman int error = 0; 8011158dfb7SGarrett Wollman int s = splimp(); 8021158dfb7SGarrett Wollman 8031158dfb7SGarrett Wollman if (onswitch) { 8041158dfb7SGarrett Wollman if (ifp->if_amcount++ == 0) { 8051158dfb7SGarrett Wollman ifp->if_flags |= IFF_ALLMULTI; 8061158dfb7SGarrett Wollman error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 8071158dfb7SGarrett Wollman } 8081158dfb7SGarrett Wollman } else { 8091158dfb7SGarrett Wollman if (ifp->if_amcount > 1) { 8101158dfb7SGarrett Wollman ifp->if_amcount--; 8111158dfb7SGarrett Wollman } else { 8121158dfb7SGarrett Wollman ifp->if_amcount = 0; 8131158dfb7SGarrett Wollman ifp->if_flags &= ~IFF_ALLMULTI; 8141158dfb7SGarrett Wollman error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 8151158dfb7SGarrett Wollman } 8161158dfb7SGarrett Wollman } 8171158dfb7SGarrett Wollman splx(s); 8184a26224cSGarrett Wollman 8194a26224cSGarrett Wollman if (error == 0) 8204a26224cSGarrett Wollman rt_ifmsg(ifp); 8211158dfb7SGarrett Wollman return error; 8221158dfb7SGarrett Wollman } 8231158dfb7SGarrett Wollman 8241158dfb7SGarrett Wollman /* 8251158dfb7SGarrett Wollman * Add a multicast listenership to the interface in question. 8261158dfb7SGarrett Wollman * The link layer provides a routine which converts 8271158dfb7SGarrett Wollman */ 8281158dfb7SGarrett Wollman int 829373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma) 8301158dfb7SGarrett Wollman struct ifnet *ifp; /* interface to manipulate */ 8311158dfb7SGarrett Wollman struct sockaddr *sa; /* address to add */ 832b2053118SGarrett Wollman struct ifmultiaddr **retifma; 8331158dfb7SGarrett Wollman { 8341158dfb7SGarrett Wollman struct sockaddr *llsa, *dupsa; 8351158dfb7SGarrett Wollman int error, s; 8361158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 8371158dfb7SGarrett Wollman 83857af7922SJulian Elischer /* 83957af7922SJulian Elischer * If the matching multicast address already exists 84057af7922SJulian Elischer * then don't add a new one, just add a reference 84157af7922SJulian Elischer */ 8421158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 8431158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) { 84457af7922SJulian Elischer if (equal(sa, ifma->ifma_addr)) { 8451158dfb7SGarrett Wollman ifma->ifma_refcount++; 84657af7922SJulian Elischer if (retifma) 84757af7922SJulian Elischer *retifma = ifma; 8481158dfb7SGarrett Wollman return 0; 8491158dfb7SGarrett Wollman } 85057af7922SJulian Elischer } 8511158dfb7SGarrett Wollman 8521158dfb7SGarrett Wollman /* 8531158dfb7SGarrett Wollman * Give the link layer a chance to accept/reject it, and also 8541158dfb7SGarrett Wollman * find out which AF_LINK address this maps to, if it isn't one 8551158dfb7SGarrett Wollman * already. 8561158dfb7SGarrett Wollman */ 8571158dfb7SGarrett Wollman if (ifp->if_resolvemulti) { 8581158dfb7SGarrett Wollman error = ifp->if_resolvemulti(ifp, &llsa, sa); 8591158dfb7SGarrett Wollman if (error) return error; 8601158dfb7SGarrett Wollman } else { 8611158dfb7SGarrett Wollman llsa = 0; 8621158dfb7SGarrett Wollman } 8631158dfb7SGarrett Wollman 8641158dfb7SGarrett Wollman MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); 8651158dfb7SGarrett Wollman MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); 8661158dfb7SGarrett Wollman bcopy(sa, dupsa, sa->sa_len); 8671158dfb7SGarrett Wollman 8681158dfb7SGarrett Wollman ifma->ifma_addr = dupsa; 8691158dfb7SGarrett Wollman ifma->ifma_lladdr = llsa; 8701158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 8711158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 872373f88edSGarrett Wollman ifma->ifma_protospec = 0; 873477180fbSGarrett Wollman rt_newmaddrmsg(RTM_NEWMADDR, ifma); 874373f88edSGarrett Wollman 8751158dfb7SGarrett Wollman /* 8761158dfb7SGarrett Wollman * Some network interfaces can scan the address list at 8771158dfb7SGarrett Wollman * interrupt time; lock them out. 8781158dfb7SGarrett Wollman */ 8791158dfb7SGarrett Wollman s = splimp(); 8801158dfb7SGarrett Wollman LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 8811158dfb7SGarrett Wollman splx(s); 882373f88edSGarrett Wollman *retifma = ifma; 8831158dfb7SGarrett Wollman 8841158dfb7SGarrett Wollman if (llsa != 0) { 8851158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 8861158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) { 8871158dfb7SGarrett Wollman if (equal(ifma->ifma_addr, llsa)) 8881158dfb7SGarrett Wollman break; 8891158dfb7SGarrett Wollman } 8901158dfb7SGarrett Wollman if (ifma) { 8911158dfb7SGarrett Wollman ifma->ifma_refcount++; 8921158dfb7SGarrett Wollman } else { 8931158dfb7SGarrett Wollman MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, 8941158dfb7SGarrett Wollman M_IFMADDR, M_WAITOK); 895477180fbSGarrett Wollman MALLOC(dupsa, struct sockaddr *, llsa->sa_len, 896477180fbSGarrett Wollman M_IFMADDR, M_WAITOK); 897477180fbSGarrett Wollman bcopy(llsa, dupsa, llsa->sa_len); 898477180fbSGarrett Wollman ifma->ifma_addr = dupsa; 8991158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 9001158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 9011158dfb7SGarrett Wollman s = splimp(); 9021158dfb7SGarrett Wollman LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 9031158dfb7SGarrett Wollman splx(s); 9041158dfb7SGarrett Wollman } 90557af7922SJulian Elischer } 9061158dfb7SGarrett Wollman /* 9071158dfb7SGarrett Wollman * We are certain we have added something, so call down to the 9081158dfb7SGarrett Wollman * interface to let them know about it. 9091158dfb7SGarrett Wollman */ 9101158dfb7SGarrett Wollman s = splimp(); 9111158dfb7SGarrett Wollman ifp->if_ioctl(ifp, SIOCADDMULTI, 0); 9121158dfb7SGarrett Wollman splx(s); 9131158dfb7SGarrett Wollman 9141158dfb7SGarrett Wollman return 0; 9151158dfb7SGarrett Wollman } 9161158dfb7SGarrett Wollman 9171158dfb7SGarrett Wollman /* 9181158dfb7SGarrett Wollman * Remove a reference to a multicast address on this interface. Yell 9191158dfb7SGarrett Wollman * if the request does not match an existing membership. 9201158dfb7SGarrett Wollman */ 9211158dfb7SGarrett Wollman int 9221158dfb7SGarrett Wollman if_delmulti(ifp, sa) 9231158dfb7SGarrett Wollman struct ifnet *ifp; 9241158dfb7SGarrett Wollman struct sockaddr *sa; 9251158dfb7SGarrett Wollman { 9261158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 9271158dfb7SGarrett Wollman int s; 9281158dfb7SGarrett Wollman 9291158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 9301158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) 9311158dfb7SGarrett Wollman if (equal(sa, ifma->ifma_addr)) 9321158dfb7SGarrett Wollman break; 9331158dfb7SGarrett Wollman if (ifma == 0) 9341158dfb7SGarrett Wollman return ENOENT; 9351158dfb7SGarrett Wollman 9361158dfb7SGarrett Wollman if (ifma->ifma_refcount > 1) { 9371158dfb7SGarrett Wollman ifma->ifma_refcount--; 9381158dfb7SGarrett Wollman return 0; 9391158dfb7SGarrett Wollman } 9401158dfb7SGarrett Wollman 941477180fbSGarrett Wollman rt_newmaddrmsg(RTM_DELMADDR, ifma); 9421158dfb7SGarrett Wollman sa = ifma->ifma_lladdr; 9431158dfb7SGarrett Wollman s = splimp(); 9441158dfb7SGarrett Wollman LIST_REMOVE(ifma, ifma_link); 9451158dfb7SGarrett Wollman splx(s); 9461158dfb7SGarrett Wollman free(ifma->ifma_addr, M_IFMADDR); 9471158dfb7SGarrett Wollman free(ifma, M_IFMADDR); 9481158dfb7SGarrett Wollman if (sa == 0) 9491158dfb7SGarrett Wollman return 0; 9501158dfb7SGarrett Wollman 9511158dfb7SGarrett Wollman /* 9521158dfb7SGarrett Wollman * Now look for the link-layer address which corresponds to 9531158dfb7SGarrett Wollman * this network address. It had been squirreled away in 9541158dfb7SGarrett Wollman * ifma->ifma_lladdr for this purpose (so we don't have 9551158dfb7SGarrett Wollman * to call ifp->if_resolvemulti() again), and we saved that 9561158dfb7SGarrett Wollman * value in sa above. If some nasty deleted the 9571158dfb7SGarrett Wollman * link-layer address out from underneath us, we can deal because 9581158dfb7SGarrett Wollman * the address we stored was is not the same as the one which was 9591158dfb7SGarrett Wollman * in the record for the link-layer address. (So we don't complain 9601158dfb7SGarrett Wollman * in that case.) 9611158dfb7SGarrett Wollman */ 9621158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 9631158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) 9641158dfb7SGarrett Wollman if (equal(sa, ifma->ifma_addr)) 9651158dfb7SGarrett Wollman break; 9661158dfb7SGarrett Wollman if (ifma == 0) 9671158dfb7SGarrett Wollman return 0; 9681158dfb7SGarrett Wollman 9691158dfb7SGarrett Wollman if (ifma->ifma_refcount > 1) { 9701158dfb7SGarrett Wollman ifma->ifma_refcount--; 9711158dfb7SGarrett Wollman return 0; 9721158dfb7SGarrett Wollman } 9731158dfb7SGarrett Wollman 9741158dfb7SGarrett Wollman s = splimp(); 9751158dfb7SGarrett Wollman LIST_REMOVE(ifma, ifma_link); 9761158dfb7SGarrett Wollman splx(s); 9771158dfb7SGarrett Wollman free(ifma->ifma_addr, M_IFMADDR); 9781158dfb7SGarrett Wollman free(sa, M_IFMADDR); 9791158dfb7SGarrett Wollman free(ifma, M_IFMADDR); 9801158dfb7SGarrett Wollman 9811158dfb7SGarrett Wollman return 0; 9821158dfb7SGarrett Wollman } 9831158dfb7SGarrett Wollman 984373f88edSGarrett Wollman struct ifmultiaddr * 985373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp) 986373f88edSGarrett Wollman struct sockaddr *sa; 987373f88edSGarrett Wollman struct ifnet *ifp; 988373f88edSGarrett Wollman { 989373f88edSGarrett Wollman struct ifmultiaddr *ifma; 990373f88edSGarrett Wollman 991373f88edSGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 992373f88edSGarrett Wollman ifma = ifma->ifma_link.le_next) 993373f88edSGarrett Wollman if (equal(ifma->ifma_addr, sa)) 994373f88edSGarrett Wollman break; 995373f88edSGarrett Wollman 996373f88edSGarrett Wollman return ifma; 997373f88edSGarrett Wollman } 998373f88edSGarrett Wollman 999602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 10002c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 1001