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 341158dfb7SGarrett Wollman * $Id: if.c,v 1.38 1996/12/13 21:28:37 wollman 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> 46df8bae1dSRodney W. Grimes #include <sys/ioctl.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 { 380df8bae1dSRodney W. Grimes 381df8bae1dSRodney W. Grimes ifp->if_flags |= IFF_UP; 382a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 383df8bae1dSRodney W. Grimes #ifdef notyet 3849448326fSPoul-Henning Kamp register struct ifaddr *ifa; 385df8bae1dSRodney W. Grimes /* this has no effect on IP, and will kill all iso connections XXX */ 386df8bae1dSRodney W. Grimes for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 387df8bae1dSRodney W. Grimes pfctlinput(PRC_IFUP, ifa->ifa_addr); 388df8bae1dSRodney W. Grimes #endif 389df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 390df8bae1dSRodney W. Grimes } 391df8bae1dSRodney W. Grimes 392df8bae1dSRodney W. Grimes /* 393df8bae1dSRodney W. Grimes * Flush an interface queue. 394df8bae1dSRodney W. Grimes */ 3953bda9f9bSPoul-Henning Kamp static void 396df8bae1dSRodney W. Grimes if_qflush(ifq) 397df8bae1dSRodney W. Grimes register struct ifqueue *ifq; 398df8bae1dSRodney W. Grimes { 399df8bae1dSRodney W. Grimes register struct mbuf *m, *n; 400df8bae1dSRodney W. Grimes 401df8bae1dSRodney W. Grimes n = ifq->ifq_head; 4029448326fSPoul-Henning Kamp while ((m = n) != 0) { 403df8bae1dSRodney W. Grimes n = m->m_act; 404df8bae1dSRodney W. Grimes m_freem(m); 405df8bae1dSRodney W. Grimes } 406df8bae1dSRodney W. Grimes ifq->ifq_head = 0; 407df8bae1dSRodney W. Grimes ifq->ifq_tail = 0; 408df8bae1dSRodney W. Grimes ifq->ifq_len = 0; 409df8bae1dSRodney W. Grimes } 410df8bae1dSRodney W. Grimes 411df8bae1dSRodney W. Grimes /* 412df8bae1dSRodney W. Grimes * Handle interface watchdog timer routines. Called 413df8bae1dSRodney W. Grimes * from softclock, we decrement timers (if set) and 414df8bae1dSRodney W. Grimes * call the appropriate interface routine on expiration. 415df8bae1dSRodney W. Grimes */ 4163bda9f9bSPoul-Henning Kamp static void 417df8bae1dSRodney W. Grimes if_slowtimo(arg) 418df8bae1dSRodney W. Grimes void *arg; 419df8bae1dSRodney W. Grimes { 420df8bae1dSRodney W. Grimes register struct ifnet *ifp; 421df8bae1dSRodney W. Grimes int s = splimp(); 422df8bae1dSRodney W. Grimes 42329412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 424df8bae1dSRodney W. Grimes if (ifp->if_timer == 0 || --ifp->if_timer) 425df8bae1dSRodney W. Grimes continue; 426df8bae1dSRodney W. Grimes if (ifp->if_watchdog) 4274a5f1499SDavid Greenman (*ifp->if_watchdog)(ifp); 428df8bae1dSRodney W. Grimes } 429df8bae1dSRodney W. Grimes splx(s); 430df8bae1dSRodney W. Grimes timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 431df8bae1dSRodney W. Grimes } 432df8bae1dSRodney W. Grimes 433df8bae1dSRodney W. Grimes /* 434df8bae1dSRodney W. Grimes * Map interface name to 435df8bae1dSRodney W. Grimes * interface structure pointer. 436df8bae1dSRodney W. Grimes */ 437df8bae1dSRodney W. Grimes struct ifnet * 438df8bae1dSRodney W. Grimes ifunit(name) 439df8bae1dSRodney W. Grimes register char *name; 440df8bae1dSRodney W. Grimes { 441df8bae1dSRodney W. Grimes register char *cp; 442df8bae1dSRodney W. Grimes register struct ifnet *ifp; 443df8bae1dSRodney W. Grimes int unit; 444df8bae1dSRodney W. Grimes unsigned len; 445df8bae1dSRodney W. Grimes char *ep, c; 446df8bae1dSRodney W. Grimes 447df8bae1dSRodney W. Grimes for (cp = name; cp < name + IFNAMSIZ && *cp; cp++) 448df8bae1dSRodney W. Grimes if (*cp >= '0' && *cp <= '9') 449df8bae1dSRodney W. Grimes break; 450df8bae1dSRodney W. Grimes if (*cp == '\0' || cp == name + IFNAMSIZ) 451df8bae1dSRodney W. Grimes return ((struct ifnet *)0); 452df8bae1dSRodney W. Grimes /* 453df8bae1dSRodney W. Grimes * Save first char of unit, and pointer to it, 454df8bae1dSRodney W. Grimes * so we can put a null there to avoid matching 455df8bae1dSRodney W. Grimes * initial substrings of interface names. 456df8bae1dSRodney W. Grimes */ 457df8bae1dSRodney W. Grimes len = cp - name + 1; 458df8bae1dSRodney W. Grimes c = *cp; 459df8bae1dSRodney W. Grimes ep = cp; 460df8bae1dSRodney W. Grimes for (unit = 0; *cp >= '0' && *cp <= '9'; ) 461df8bae1dSRodney W. Grimes unit = unit * 10 + *cp++ - '0'; 46258639ed3SGarrett Wollman if (*cp != '\0') 46358639ed3SGarrett Wollman return 0; /* no trailing garbage allowed */ 464df8bae1dSRodney W. Grimes *ep = 0; 46529412182SGarrett Wollman for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { 466df8bae1dSRodney W. Grimes if (bcmp(ifp->if_name, name, len)) 467df8bae1dSRodney W. Grimes continue; 468df8bae1dSRodney W. Grimes if (unit == ifp->if_unit) 469df8bae1dSRodney W. Grimes break; 470df8bae1dSRodney W. Grimes } 471df8bae1dSRodney W. Grimes *ep = c; 472df8bae1dSRodney W. Grimes return (ifp); 473df8bae1dSRodney W. Grimes } 474df8bae1dSRodney W. Grimes 475df8bae1dSRodney W. Grimes /* 476df8bae1dSRodney W. Grimes * Interface ioctls. 477df8bae1dSRodney W. Grimes */ 478df8bae1dSRodney W. Grimes int 479df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p) 480df8bae1dSRodney W. Grimes struct socket *so; 481df8bae1dSRodney W. Grimes int cmd; 482df8bae1dSRodney W. Grimes caddr_t data; 483df8bae1dSRodney W. Grimes struct proc *p; 484df8bae1dSRodney W. Grimes { 485df8bae1dSRodney W. Grimes register struct ifnet *ifp; 486df8bae1dSRodney W. Grimes register struct ifreq *ifr; 487df8bae1dSRodney W. Grimes int error; 488df8bae1dSRodney W. Grimes 489df8bae1dSRodney W. Grimes switch (cmd) { 490df8bae1dSRodney W. Grimes 491df8bae1dSRodney W. Grimes case SIOCGIFCONF: 492df8bae1dSRodney W. Grimes case OSIOCGIFCONF: 493df8bae1dSRodney W. Grimes return (ifconf(cmd, data)); 494df8bae1dSRodney W. Grimes } 495df8bae1dSRodney W. Grimes ifr = (struct ifreq *)data; 496df8bae1dSRodney W. Grimes ifp = ifunit(ifr->ifr_name); 497df8bae1dSRodney W. Grimes if (ifp == 0) 498df8bae1dSRodney W. Grimes return (ENXIO); 499df8bae1dSRodney W. Grimes switch (cmd) { 500df8bae1dSRodney W. Grimes 501df8bae1dSRodney W. Grimes case SIOCGIFFLAGS: 502df8bae1dSRodney W. Grimes ifr->ifr_flags = ifp->if_flags; 503df8bae1dSRodney W. Grimes break; 504df8bae1dSRodney W. Grimes 505df8bae1dSRodney W. Grimes case SIOCGIFMETRIC: 506df8bae1dSRodney W. Grimes ifr->ifr_metric = ifp->if_metric; 507df8bae1dSRodney W. Grimes break; 508df8bae1dSRodney W. Grimes 509a7028af7SDavid Greenman case SIOCGIFMTU: 510a7028af7SDavid Greenman ifr->ifr_mtu = ifp->if_mtu; 511a7028af7SDavid Greenman break; 512a7028af7SDavid Greenman 513074c4a4eSGarrett Wollman case SIOCGIFPHYS: 514074c4a4eSGarrett Wollman ifr->ifr_phys = ifp->if_physical; 515074c4a4eSGarrett Wollman break; 516074c4a4eSGarrett Wollman 517df8bae1dSRodney W. Grimes case SIOCSIFFLAGS: 5189448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5199448326fSPoul-Henning Kamp if (error) 520df8bae1dSRodney W. Grimes return (error); 521df8bae1dSRodney W. Grimes if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 522df8bae1dSRodney W. Grimes int s = splimp(); 523df8bae1dSRodney W. Grimes if_down(ifp); 524df8bae1dSRodney W. Grimes splx(s); 525df8bae1dSRodney W. Grimes } 526df8bae1dSRodney W. Grimes if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 527df8bae1dSRodney W. Grimes int s = splimp(); 528df8bae1dSRodney W. Grimes if_up(ifp); 529df8bae1dSRodney W. Grimes splx(s); 530df8bae1dSRodney W. Grimes } 531df8bae1dSRodney W. Grimes ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 532df8bae1dSRodney W. Grimes (ifr->ifr_flags &~ IFF_CANTCHANGE); 533df8bae1dSRodney W. Grimes if (ifp->if_ioctl) 534df8bae1dSRodney W. Grimes (void) (*ifp->if_ioctl)(ifp, cmd, data); 535a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 536df8bae1dSRodney W. Grimes break; 537df8bae1dSRodney W. Grimes 538df8bae1dSRodney W. Grimes case SIOCSIFMETRIC: 5399448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5409448326fSPoul-Henning Kamp if (error) 541df8bae1dSRodney W. Grimes return (error); 542df8bae1dSRodney W. Grimes ifp->if_metric = ifr->ifr_metric; 543a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 544df8bae1dSRodney W. Grimes break; 545df8bae1dSRodney W. Grimes 546074c4a4eSGarrett Wollman case SIOCSIFPHYS: 547074c4a4eSGarrett Wollman error = suser(p->p_ucred, &p->p_acflag); 548e39a0280SGary Palmer if (error) 549e39a0280SGary Palmer return error; 550e39a0280SGary Palmer if (!ifp->if_ioctl) 551e39a0280SGary Palmer return EOPNOTSUPP; 552e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 553e39a0280SGary Palmer if (error == 0) 554a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 555e39a0280SGary Palmer return(error); 556074c4a4eSGarrett Wollman 557a7028af7SDavid Greenman case SIOCSIFMTU: 5589448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5599448326fSPoul-Henning Kamp if (error) 560a7028af7SDavid Greenman return (error); 561a7028af7SDavid Greenman if (ifp->if_ioctl == NULL) 562a7028af7SDavid Greenman return (EOPNOTSUPP); 56366025569SDavid Greenman /* 56466025569SDavid Greenman * 72 was chosen below because it is the size of a TCP/IP 56566025569SDavid Greenman * header (40) + the minimum mss (32). 56666025569SDavid Greenman */ 56766025569SDavid Greenman if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535) 56875ee03cbSDavid Greenman return (EINVAL); 569e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 570e39a0280SGary Palmer if (error == 0) 571a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 572e39a0280SGary Palmer return(error); 573a7028af7SDavid Greenman 574df8bae1dSRodney W. Grimes case SIOCADDMULTI: 575df8bae1dSRodney W. Grimes case SIOCDELMULTI: 5769448326fSPoul-Henning Kamp error = suser(p->p_ucred, &p->p_acflag); 5779448326fSPoul-Henning Kamp if (error) 578df8bae1dSRodney W. Grimes return (error); 579df8bae1dSRodney W. Grimes if (ifp->if_ioctl == NULL) 580df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 581e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 582e39a0280SGary Palmer if (error == 0 ) 583a614bfe0SGary Palmer microtime(&ifp->if_lastchange); 584e39a0280SGary Palmer return(error); 585df8bae1dSRodney W. Grimes 586df8bae1dSRodney W. Grimes default: 587df8bae1dSRodney W. Grimes if (so->so_proto == 0) 588df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 589df8bae1dSRodney W. Grimes #ifndef COMPAT_43 5902c37256eSGarrett Wollman return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 5912c37256eSGarrett Wollman data, 5922c37256eSGarrett Wollman ifp)); 593df8bae1dSRodney W. Grimes #else 594df8bae1dSRodney W. Grimes { 595df8bae1dSRodney W. Grimes int ocmd = cmd; 596df8bae1dSRodney W. Grimes 597df8bae1dSRodney W. Grimes switch (cmd) { 598df8bae1dSRodney W. Grimes 599df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 600df8bae1dSRodney W. Grimes case SIOCSIFADDR: 601df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 602df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 603df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN 604df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_family == 0 && 605df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len < 16) { 606df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 607df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 608df8bae1dSRodney W. Grimes } 609df8bae1dSRodney W. Grimes #else 610df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_len == 0) 611df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 612df8bae1dSRodney W. Grimes #endif 613df8bae1dSRodney W. Grimes break; 614df8bae1dSRodney W. Grimes 615df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 616df8bae1dSRodney W. Grimes cmd = SIOCGIFADDR; 617df8bae1dSRodney W. Grimes break; 618df8bae1dSRodney W. Grimes 619df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 620df8bae1dSRodney W. Grimes cmd = SIOCGIFDSTADDR; 621df8bae1dSRodney W. Grimes break; 622df8bae1dSRodney W. Grimes 623df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 624df8bae1dSRodney W. Grimes cmd = SIOCGIFBRDADDR; 625df8bae1dSRodney W. Grimes break; 626df8bae1dSRodney W. Grimes 627df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 628df8bae1dSRodney W. Grimes cmd = SIOCGIFNETMASK; 629df8bae1dSRodney W. Grimes } 6302c37256eSGarrett Wollman error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 6312c37256eSGarrett Wollman cmd, 6322c37256eSGarrett Wollman data, 6332c37256eSGarrett Wollman ifp)); 634df8bae1dSRodney W. Grimes switch (ocmd) { 635df8bae1dSRodney W. Grimes 636df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 637df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 638df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 639df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 640df8bae1dSRodney W. Grimes *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 641df8bae1dSRodney W. Grimes } 642df8bae1dSRodney W. Grimes return (error); 643df8bae1dSRodney W. Grimes 644df8bae1dSRodney W. Grimes } 645df8bae1dSRodney W. Grimes #endif 646df8bae1dSRodney W. Grimes } 647df8bae1dSRodney W. Grimes return (0); 648df8bae1dSRodney W. Grimes } 649df8bae1dSRodney W. Grimes 650df8bae1dSRodney W. Grimes /* 651963e4c2aSGarrett Wollman * Set/clear promiscuous mode on interface ifp based on the truth value 652963e4c2aSGarrett Wollman * of pswitch. The calls are reference counted so that only the first 653963e4c2aSGarrett Wollman * "on" request actually has an effect, as does the final "off" request. 654963e4c2aSGarrett Wollman * Results are undefined if the "off" and "on" requests are not matched. 655963e4c2aSGarrett Wollman */ 656963e4c2aSGarrett Wollman int 657963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch) 658963e4c2aSGarrett Wollman struct ifnet *ifp; 659963e4c2aSGarrett Wollman int pswitch; 660963e4c2aSGarrett Wollman { 661963e4c2aSGarrett Wollman struct ifreq ifr; 662963e4c2aSGarrett Wollman 663963e4c2aSGarrett Wollman if (pswitch) { 664963e4c2aSGarrett Wollman /* 665963e4c2aSGarrett Wollman * If the device is not configured up, we cannot put it in 666963e4c2aSGarrett Wollman * promiscuous mode. 667963e4c2aSGarrett Wollman */ 668963e4c2aSGarrett Wollman if ((ifp->if_flags & IFF_UP) == 0) 669963e4c2aSGarrett Wollman return (ENETDOWN); 670963e4c2aSGarrett Wollman if (ifp->if_pcount++ != 0) 671963e4c2aSGarrett Wollman return (0); 672963e4c2aSGarrett Wollman ifp->if_flags |= IFF_PROMISC; 673e9a30d00SGarrett Wollman log(LOG_INFO, "%s%d: promiscuous mode enabled\n", 674963e4c2aSGarrett Wollman ifp->if_name, ifp->if_unit); 675963e4c2aSGarrett Wollman } else { 676963e4c2aSGarrett Wollman if (--ifp->if_pcount > 0) 677963e4c2aSGarrett Wollman return (0); 678963e4c2aSGarrett Wollman ifp->if_flags &= ~IFF_PROMISC; 679963e4c2aSGarrett Wollman } 680963e4c2aSGarrett Wollman ifr.ifr_flags = ifp->if_flags; 681963e4c2aSGarrett Wollman return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr)); 682963e4c2aSGarrett Wollman } 683963e4c2aSGarrett Wollman 684963e4c2aSGarrett Wollman /* 685df8bae1dSRodney W. Grimes * Return interface configuration 686df8bae1dSRodney W. Grimes * of system. List may be used 687df8bae1dSRodney W. Grimes * in later ioctl's (above) to get 688df8bae1dSRodney W. Grimes * other information. 689df8bae1dSRodney W. Grimes */ 690df8bae1dSRodney W. Grimes /*ARGSUSED*/ 6913bda9f9bSPoul-Henning Kamp static int 692df8bae1dSRodney W. Grimes ifconf(cmd, data) 693df8bae1dSRodney W. Grimes int cmd; 694df8bae1dSRodney W. Grimes caddr_t data; 695df8bae1dSRodney W. Grimes { 696df8bae1dSRodney W. Grimes register struct ifconf *ifc = (struct ifconf *)data; 69729412182SGarrett Wollman register struct ifnet *ifp = ifnet.tqh_first; 698df8bae1dSRodney W. Grimes register struct ifaddr *ifa; 699df8bae1dSRodney W. Grimes struct ifreq ifr, *ifrp; 700df8bae1dSRodney W. Grimes int space = ifc->ifc_len, error = 0; 701df8bae1dSRodney W. Grimes 702df8bae1dSRodney W. Grimes ifrp = ifc->ifc_req; 70329412182SGarrett Wollman for (; space > sizeof (ifr) && ifp; ifp = ifp->if_link.tqe_next) { 7041ce9bf88SPoul-Henning Kamp char workbuf[64]; 7051ce9bf88SPoul-Henning Kamp int ifnlen; 7062624cf89SGarrett Wollman 7071ce9bf88SPoul-Henning Kamp ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit); 7081ce9bf88SPoul-Henning Kamp if(ifnlen + 1 > sizeof ifr.ifr_name) { 7092624cf89SGarrett Wollman error = ENAMETOOLONG; 7102624cf89SGarrett Wollman } else { 7111ce9bf88SPoul-Henning Kamp strcpy(ifr.ifr_name, workbuf); 7122624cf89SGarrett Wollman } 7132624cf89SGarrett Wollman 71459562606SGarrett Wollman if ((ifa = ifp->if_addrhead.tqh_first) == 0) { 715df8bae1dSRodney W. Grimes bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 716df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 717df8bae1dSRodney W. Grimes sizeof (ifr)); 718df8bae1dSRodney W. Grimes if (error) 719df8bae1dSRodney W. Grimes break; 720df8bae1dSRodney W. Grimes space -= sizeof (ifr), ifrp++; 721df8bae1dSRodney W. Grimes } else 72259562606SGarrett Wollman for ( ; space > sizeof (ifr) && ifa; 72359562606SGarrett Wollman ifa = ifa->ifa_link.tqe_next) { 724df8bae1dSRodney W. Grimes register struct sockaddr *sa = ifa->ifa_addr; 725df8bae1dSRodney W. Grimes #ifdef COMPAT_43 726df8bae1dSRodney W. Grimes if (cmd == OSIOCGIFCONF) { 727df8bae1dSRodney W. Grimes struct osockaddr *osa = 728df8bae1dSRodney W. Grimes (struct osockaddr *)&ifr.ifr_addr; 729df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 730df8bae1dSRodney W. Grimes osa->sa_family = sa->sa_family; 731df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 732df8bae1dSRodney W. Grimes sizeof (ifr)); 733df8bae1dSRodney W. Grimes ifrp++; 734df8bae1dSRodney W. Grimes } else 735df8bae1dSRodney W. Grimes #endif 736df8bae1dSRodney W. Grimes if (sa->sa_len <= sizeof(*sa)) { 737df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 738df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 739df8bae1dSRodney W. Grimes sizeof (ifr)); 740df8bae1dSRodney W. Grimes ifrp++; 741df8bae1dSRodney W. Grimes } else { 742df8bae1dSRodney W. Grimes space -= sa->sa_len - sizeof(*sa); 743df8bae1dSRodney W. Grimes if (space < sizeof (ifr)) 744df8bae1dSRodney W. Grimes break; 745df8bae1dSRodney W. Grimes error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 746df8bae1dSRodney W. Grimes sizeof (ifr.ifr_name)); 747df8bae1dSRodney W. Grimes if (error == 0) 748df8bae1dSRodney W. Grimes error = copyout((caddr_t)sa, 749df8bae1dSRodney W. Grimes (caddr_t)&ifrp->ifr_addr, sa->sa_len); 750df8bae1dSRodney W. Grimes ifrp = (struct ifreq *) 751df8bae1dSRodney W. Grimes (sa->sa_len + (caddr_t)&ifrp->ifr_addr); 752df8bae1dSRodney W. Grimes } 753df8bae1dSRodney W. Grimes if (error) 754df8bae1dSRodney W. Grimes break; 755df8bae1dSRodney W. Grimes space -= sizeof (ifr); 756df8bae1dSRodney W. Grimes } 757df8bae1dSRodney W. Grimes } 758df8bae1dSRodney W. Grimes ifc->ifc_len -= space; 759df8bae1dSRodney W. Grimes return (error); 760df8bae1dSRodney W. Grimes } 761df8bae1dSRodney W. Grimes 7621158dfb7SGarrett Wollman /* 7631158dfb7SGarrett Wollman * Just like if_promisc(), but for all-multicast-reception mode. 7641158dfb7SGarrett Wollman */ 7651158dfb7SGarrett Wollman int 7661158dfb7SGarrett Wollman if_allmulti(ifp, onswitch) 7671158dfb7SGarrett Wollman struct ifnet *ifp; 7681158dfb7SGarrett Wollman int onswitch; 7691158dfb7SGarrett Wollman { 7701158dfb7SGarrett Wollman int error = 0; 7711158dfb7SGarrett Wollman int s = splimp(); 7721158dfb7SGarrett Wollman 7731158dfb7SGarrett Wollman if (onswitch) { 7741158dfb7SGarrett Wollman if (ifp->if_amcount++ == 0) { 7751158dfb7SGarrett Wollman ifp->if_flags |= IFF_ALLMULTI; 7761158dfb7SGarrett Wollman error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 7771158dfb7SGarrett Wollman } 7781158dfb7SGarrett Wollman } else { 7791158dfb7SGarrett Wollman if (ifp->if_amcount > 1) { 7801158dfb7SGarrett Wollman ifp->if_amcount--; 7811158dfb7SGarrett Wollman } else { 7821158dfb7SGarrett Wollman ifp->if_amcount = 0; 7831158dfb7SGarrett Wollman ifp->if_flags &= ~IFF_ALLMULTI; 7841158dfb7SGarrett Wollman error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 7851158dfb7SGarrett Wollman } 7861158dfb7SGarrett Wollman } 7871158dfb7SGarrett Wollman splx(s); 7881158dfb7SGarrett Wollman return error; 7891158dfb7SGarrett Wollman } 7901158dfb7SGarrett Wollman 7911158dfb7SGarrett Wollman /* 7921158dfb7SGarrett Wollman * Add a multicast listenership to the interface in question. 7931158dfb7SGarrett Wollman * The link layer provides a routine which converts 7941158dfb7SGarrett Wollman */ 7951158dfb7SGarrett Wollman int 7961158dfb7SGarrett Wollman if_addmulti(ifp, sa) 7971158dfb7SGarrett Wollman struct ifnet *ifp; /* interface to manipulate */ 7981158dfb7SGarrett Wollman struct sockaddr *sa; /* address to add */ 7991158dfb7SGarrett Wollman { 8001158dfb7SGarrett Wollman struct sockaddr *llsa, *dupsa; 8011158dfb7SGarrett Wollman int error, s; 8021158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 8031158dfb7SGarrett Wollman 8041158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 8051158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) { 8061158dfb7SGarrett Wollman if (equal(sa, ifma->ifma_addr)) 8071158dfb7SGarrett Wollman break; 8081158dfb7SGarrett Wollman } 8091158dfb7SGarrett Wollman 8101158dfb7SGarrett Wollman if (ifma) { 8111158dfb7SGarrett Wollman ifma->ifma_refcount++; 8121158dfb7SGarrett Wollman return 0; 8131158dfb7SGarrett Wollman } 8141158dfb7SGarrett Wollman 8151158dfb7SGarrett Wollman /* 8161158dfb7SGarrett Wollman * Give the link layer a chance to accept/reject it, and also 8171158dfb7SGarrett Wollman * find out which AF_LINK address this maps to, if it isn't one 8181158dfb7SGarrett Wollman * already. 8191158dfb7SGarrett Wollman */ 8201158dfb7SGarrett Wollman if (ifp->if_resolvemulti) { 8211158dfb7SGarrett Wollman error = ifp->if_resolvemulti(ifp, &llsa, sa); 8221158dfb7SGarrett Wollman if (error) return error; 8231158dfb7SGarrett Wollman } else { 8241158dfb7SGarrett Wollman llsa = 0; 8251158dfb7SGarrett Wollman } 8261158dfb7SGarrett Wollman 8271158dfb7SGarrett Wollman MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); 8281158dfb7SGarrett Wollman MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); 8291158dfb7SGarrett Wollman bcopy(sa, dupsa, sa->sa_len); 8301158dfb7SGarrett Wollman 8311158dfb7SGarrett Wollman ifma->ifma_addr = dupsa; 8321158dfb7SGarrett Wollman ifma->ifma_lladdr = llsa; 8331158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 8341158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 8351158dfb7SGarrett Wollman /* 8361158dfb7SGarrett Wollman * Some network interfaces can scan the address list at 8371158dfb7SGarrett Wollman * interrupt time; lock them out. 8381158dfb7SGarrett Wollman */ 8391158dfb7SGarrett Wollman s = splimp(); 8401158dfb7SGarrett Wollman LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 8411158dfb7SGarrett Wollman splx(s); 8421158dfb7SGarrett Wollman 8431158dfb7SGarrett Wollman if (llsa != 0) { 8441158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 8451158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) { 8461158dfb7SGarrett Wollman if (equal(ifma->ifma_addr, llsa)) 8471158dfb7SGarrett Wollman break; 8481158dfb7SGarrett Wollman } 8491158dfb7SGarrett Wollman if (ifma) { 8501158dfb7SGarrett Wollman ifma->ifma_refcount++; 8511158dfb7SGarrett Wollman } else { 8521158dfb7SGarrett Wollman MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, 8531158dfb7SGarrett Wollman M_IFMADDR, M_WAITOK); 8541158dfb7SGarrett Wollman ifma->ifma_addr = llsa; 8551158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 8561158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 8571158dfb7SGarrett Wollman } 8581158dfb7SGarrett Wollman s = splimp(); 8591158dfb7SGarrett Wollman LIST_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 8601158dfb7SGarrett Wollman splx(s); 8611158dfb7SGarrett Wollman } 8621158dfb7SGarrett Wollman /* 8631158dfb7SGarrett Wollman * We are certain we have added something, so call down to the 8641158dfb7SGarrett Wollman * interface to let them know about it. 8651158dfb7SGarrett Wollman */ 8661158dfb7SGarrett Wollman s = splimp(); 8671158dfb7SGarrett Wollman ifp->if_ioctl(ifp, SIOCADDMULTI, 0); 8681158dfb7SGarrett Wollman splx(s); 8691158dfb7SGarrett Wollman 8701158dfb7SGarrett Wollman return 0; 8711158dfb7SGarrett Wollman } 8721158dfb7SGarrett Wollman 8731158dfb7SGarrett Wollman /* 8741158dfb7SGarrett Wollman * Remove a reference to a multicast address on this interface. Yell 8751158dfb7SGarrett Wollman * if the request does not match an existing membership. 8761158dfb7SGarrett Wollman */ 8771158dfb7SGarrett Wollman int 8781158dfb7SGarrett Wollman if_delmulti(ifp, sa) 8791158dfb7SGarrett Wollman struct ifnet *ifp; 8801158dfb7SGarrett Wollman struct sockaddr *sa; 8811158dfb7SGarrett Wollman { 8821158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 8831158dfb7SGarrett Wollman int s; 8841158dfb7SGarrett Wollman 8851158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 8861158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) 8871158dfb7SGarrett Wollman if (equal(sa, ifma->ifma_addr)) 8881158dfb7SGarrett Wollman break; 8891158dfb7SGarrett Wollman if (ifma == 0) 8901158dfb7SGarrett Wollman return ENOENT; 8911158dfb7SGarrett Wollman 8921158dfb7SGarrett Wollman if (ifma->ifma_refcount > 1) { 8931158dfb7SGarrett Wollman ifma->ifma_refcount--; 8941158dfb7SGarrett Wollman return 0; 8951158dfb7SGarrett Wollman } 8961158dfb7SGarrett Wollman 8971158dfb7SGarrett Wollman sa = ifma->ifma_lladdr; 8981158dfb7SGarrett Wollman s = splimp(); 8991158dfb7SGarrett Wollman LIST_REMOVE(ifma, ifma_link); 9001158dfb7SGarrett Wollman splx(s); 9011158dfb7SGarrett Wollman free(ifma->ifma_addr, M_IFMADDR); 9021158dfb7SGarrett Wollman free(ifma, M_IFMADDR); 9031158dfb7SGarrett Wollman if (sa == 0) 9041158dfb7SGarrett Wollman return 0; 9051158dfb7SGarrett Wollman 9061158dfb7SGarrett Wollman /* 9071158dfb7SGarrett Wollman * Now look for the link-layer address which corresponds to 9081158dfb7SGarrett Wollman * this network address. It had been squirreled away in 9091158dfb7SGarrett Wollman * ifma->ifma_lladdr for this purpose (so we don't have 9101158dfb7SGarrett Wollman * to call ifp->if_resolvemulti() again), and we saved that 9111158dfb7SGarrett Wollman * value in sa above. If some nasty deleted the 9121158dfb7SGarrett Wollman * link-layer address out from underneath us, we can deal because 9131158dfb7SGarrett Wollman * the address we stored was is not the same as the one which was 9141158dfb7SGarrett Wollman * in the record for the link-layer address. (So we don't complain 9151158dfb7SGarrett Wollman * in that case.) 9161158dfb7SGarrett Wollman */ 9171158dfb7SGarrett Wollman for (ifma = ifp->if_multiaddrs.lh_first; ifma; 9181158dfb7SGarrett Wollman ifma = ifma->ifma_link.le_next) 9191158dfb7SGarrett Wollman if (equal(sa, ifma->ifma_addr)) 9201158dfb7SGarrett Wollman break; 9211158dfb7SGarrett Wollman if (ifma == 0) 9221158dfb7SGarrett Wollman return 0; 9231158dfb7SGarrett Wollman 9241158dfb7SGarrett Wollman if (ifma->ifma_refcount > 1) { 9251158dfb7SGarrett Wollman ifma->ifma_refcount--; 9261158dfb7SGarrett Wollman return 0; 9271158dfb7SGarrett Wollman } 9281158dfb7SGarrett Wollman 9291158dfb7SGarrett Wollman s = splimp(); 9301158dfb7SGarrett Wollman LIST_REMOVE(ifma, ifma_link); 9311158dfb7SGarrett Wollman splx(s); 9321158dfb7SGarrett Wollman free(ifma->ifma_addr, M_IFMADDR); 9331158dfb7SGarrett Wollman free(sa, M_IFMADDR); 9341158dfb7SGarrett Wollman free(ifma, M_IFMADDR); 9351158dfb7SGarrett Wollman 9361158dfb7SGarrett Wollman return 0; 9371158dfb7SGarrett Wollman } 9381158dfb7SGarrett Wollman 939602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 9402c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 941