xref: /freebsd/sys/net/if.c (revision e39a0280cb07f5cb2a01c8dda54c7bd3f0a2003a)
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
34e39a0280SGary Palmer  * $Id: if.c,v 1.30 1996/06/05 17:12:41 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;
7073c2ab46SDavid Greenman struct	ifnet *ifnet;
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 
87df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_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 
933bda9f9bSPoul-Henning Kamp static int if_index = 0;
943bda9f9bSPoul-Henning Kamp static 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 ifnet **p = &ifnet;
109df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
110df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
111df8bae1dSRodney W. Grimes 	static int if_indexlim = 8;
112f23b4c91SGarrett Wollman 
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes 	while (*p)
115df8bae1dSRodney W. Grimes 		p = &((*p)->if_next);
116df8bae1dSRodney W. Grimes 	*p = ifp;
117df8bae1dSRodney W. Grimes 	ifp->if_index = ++if_index;
118e39a0280SGary Palmer 	ifp->if_lastchange = time;
119df8bae1dSRodney W. Grimes 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
120df8bae1dSRodney W. Grimes 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
121df8bae1dSRodney W. Grimes 		struct ifaddr **q = (struct ifaddr **)
122df8bae1dSRodney W. Grimes 					malloc(n, M_IFADDR, M_WAITOK);
12373c2ab46SDavid Greenman 		bzero((caddr_t)q, n);
124df8bae1dSRodney W. Grimes 		if (ifnet_addrs) {
125df8bae1dSRodney W. Grimes 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
126df8bae1dSRodney W. Grimes 			free((caddr_t)ifnet_addrs, M_IFADDR);
127df8bae1dSRodney W. Grimes 		}
128df8bae1dSRodney W. Grimes 		ifnet_addrs = q;
129df8bae1dSRodney W. Grimes 	}
130df8bae1dSRodney W. Grimes 	/*
131df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
132df8bae1dSRodney W. Grimes 	 */
1331ce9bf88SPoul-Henning Kamp 	namelen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
134df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
1351ce9bf88SPoul-Henning Kamp 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
136df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
137df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
138df8bae1dSRodney W. Grimes 	socksize = ROUNDUP(socksize);
139df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
140df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
141df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
1429448326fSPoul-Henning Kamp 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
1439448326fSPoul-Henning Kamp 	if (ifa) {
144df8bae1dSRodney W. Grimes 		bzero((caddr_t)ifa, ifasize);
145df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
146df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
147df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
1481ce9bf88SPoul-Henning Kamp 		bcopy(workbuf, sdl->sdl_data, namelen);
1491ce9bf88SPoul-Henning Kamp 		sdl->sdl_nlen = namelen;
150df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
151df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
152df8bae1dSRodney W. Grimes 		ifnet_addrs[if_index - 1] = ifa;
153df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
154df8bae1dSRodney W. Grimes 		ifa->ifa_next = ifp->if_addrlist;
155df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
156df8bae1dSRodney W. Grimes 		ifp->if_addrlist = ifa;
157df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
1581ce9bf88SPoul-Henning Kamp 
159df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
160df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
161df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
162df8bae1dSRodney W. Grimes 		while (namelen != 0)
163df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
164df8bae1dSRodney W. Grimes 	}
165a9ad85b0SGarrett Wollman 	/*
166a9ad85b0SGarrett Wollman 	 * If they provided a slow input queue, initialize it.
167a9ad85b0SGarrett Wollman 	 */
168a9ad85b0SGarrett Wollman 	if (ifp->if_poll_slowq) {
169a9ad85b0SGarrett Wollman 		struct ifqueue *ifq = ifp->if_poll_slowq;
170a9ad85b0SGarrett Wollman 
171a9ad85b0SGarrett Wollman 		bzero(ifq, sizeof *ifq);
172a9ad85b0SGarrett Wollman 		ifq->ifq_maxlen = ifqmaxlen;
173a9ad85b0SGarrett Wollman #ifdef POLLING
174a9ad85b0SGarrett Wollman 		ifq->if_poll_recv = if_poll_recv_slow;
175a9ad85b0SGarrett Wollman #endif
176a9ad85b0SGarrett Wollman 	}
177df8bae1dSRodney W. Grimes }
178df8bae1dSRodney W. Grimes /*
179df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
180df8bae1dSRodney W. Grimes  */
181df8bae1dSRodney W. Grimes /*ARGSUSED*/
182df8bae1dSRodney W. Grimes struct ifaddr *
183df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
184df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
185df8bae1dSRodney W. Grimes {
186df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
187df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
188df8bae1dSRodney W. Grimes 
189df8bae1dSRodney W. Grimes #define	equal(a1, a2) \
190df8bae1dSRodney W. Grimes   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
191df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
192df8bae1dSRodney W. Grimes 	    for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
193df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != addr->sa_family)
194df8bae1dSRodney W. Grimes 			continue;
195df8bae1dSRodney W. Grimes 		if (equal(addr, ifa->ifa_addr))
196df8bae1dSRodney W. Grimes 			return (ifa);
197df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
198df8bae1dSRodney W. Grimes 		    equal(ifa->ifa_broadaddr, addr))
199df8bae1dSRodney W. Grimes 			return (ifa);
200df8bae1dSRodney W. Grimes 	}
201df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
202df8bae1dSRodney W. Grimes }
203df8bae1dSRodney W. Grimes /*
204df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
205df8bae1dSRodney W. Grimes  */
206df8bae1dSRodney W. Grimes /*ARGSUSED*/
207df8bae1dSRodney W. Grimes struct ifaddr *
208df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
209df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
210df8bae1dSRodney W. Grimes {
211df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
212df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
213df8bae1dSRodney W. Grimes 
214df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
215df8bae1dSRodney W. Grimes 	    if (ifp->if_flags & IFF_POINTOPOINT)
216df8bae1dSRodney W. Grimes 		for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
217df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
218df8bae1dSRodney W. Grimes 				continue;
21955088a1cSDavid Greenman 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
220df8bae1dSRodney W. Grimes 				return (ifa);
221df8bae1dSRodney W. Grimes 	}
222df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
223df8bae1dSRodney W. Grimes }
224df8bae1dSRodney W. Grimes 
225df8bae1dSRodney W. Grimes /*
226df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
227df8bae1dSRodney W. Grimes  * is most specific found.
228df8bae1dSRodney W. Grimes  */
229df8bae1dSRodney W. Grimes struct ifaddr *
230df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
231df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
232df8bae1dSRodney W. Grimes {
233df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
234df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
235df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
236df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
237df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
238df8bae1dSRodney W. Grimes 
239df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
240df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
241df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
242df8bae1dSRodney W. Grimes 		return (ifnet_addrs[sdl->sdl_index - 1]);
243df8bae1dSRodney W. Grimes 	}
244b2af64fdSDavid Greenman 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
245df8bae1dSRodney W. Grimes 		for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_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) {
251523a02aaSDavid Greenman 				if (equal(addr, ifa->ifa_dstaddr))
252b2af64fdSDavid Greenman  					return (ifa);
2533740e2adSDavid Greenman 			} else {
254523a02aaSDavid Greenman 				if (ifa->ifa_netmask == 0)
255523a02aaSDavid Greenman 					continue;
256df8bae1dSRodney W. Grimes 				cp = addr_data;
257df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
258df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
259df8bae1dSRodney W. Grimes 				cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
260df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
261df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
262df8bae1dSRodney W. Grimes 						goto next;
263df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
264df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
265df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
266df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
267df8bae1dSRodney W. Grimes 			}
268b2af64fdSDavid Greenman 		}
269b2af64fdSDavid Greenman 	}
270df8bae1dSRodney W. Grimes 	return (ifa_maybe);
271df8bae1dSRodney W. Grimes }
272df8bae1dSRodney W. Grimes 
273df8bae1dSRodney W. Grimes /*
274df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
275df8bae1dSRodney W. Grimes  * a given address.
276df8bae1dSRodney W. Grimes  */
277df8bae1dSRodney W. Grimes struct ifaddr *
278df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
279df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
280df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
281df8bae1dSRodney W. Grimes {
282df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
283df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
284df8bae1dSRodney W. Grimes 	register char *cplim;
285df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
286df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
287df8bae1dSRodney W. Grimes 
288df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
289df8bae1dSRodney W. Grimes 		return (0);
290df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
291df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
292df8bae1dSRodney W. Grimes 			continue;
293df8bae1dSRodney W. Grimes 		ifa_maybe = ifa;
294df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
295df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
296df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
297df8bae1dSRodney W. Grimes 				return (ifa);
298df8bae1dSRodney W. Grimes 			continue;
299df8bae1dSRodney W. Grimes 		}
300b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
301b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
302b2af64fdSDavid Greenman 				return (ifa);
3033740e2adSDavid Greenman 		} else {
304df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
305df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
306df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
307df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
308df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
309df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
310df8bae1dSRodney W. Grimes 					break;
311df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
312df8bae1dSRodney W. Grimes 				return (ifa);
313df8bae1dSRodney W. Grimes 		}
314b2af64fdSDavid Greenman 	}
315df8bae1dSRodney W. Grimes 	return (ifa_maybe);
316df8bae1dSRodney W. Grimes }
317df8bae1dSRodney W. Grimes 
318df8bae1dSRodney W. Grimes #include <net/route.h>
319df8bae1dSRodney W. Grimes 
320df8bae1dSRodney W. Grimes /*
321df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
322df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
323df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
324df8bae1dSRodney W. Grimes  */
3253bda9f9bSPoul-Henning Kamp static void
326df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa)
327df8bae1dSRodney W. Grimes 	int cmd;
328df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
329df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
330df8bae1dSRodney W. Grimes {
331df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
332df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
333df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
334df8bae1dSRodney W. Grimes 
335df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
336df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
337df8bae1dSRodney W. Grimes 		return;
3389448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
3399448326fSPoul-Henning Kamp 	if (ifa) {
340df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
341df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
342df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
343df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
344df8bae1dSRodney W. Grimes 			ifa->ifa_rtrequest(cmd, rt, sa);
345df8bae1dSRodney W. Grimes 	}
346df8bae1dSRodney W. Grimes }
347df8bae1dSRodney W. Grimes 
348df8bae1dSRodney W. Grimes /*
349df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
350df8bae1dSRodney W. Grimes  * the transition.
351df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
352df8bae1dSRodney W. Grimes  */
353df8bae1dSRodney W. Grimes void
354df8bae1dSRodney W. Grimes if_down(ifp)
355df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
356df8bae1dSRodney W. Grimes {
357df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
358df8bae1dSRodney W. Grimes 
359df8bae1dSRodney W. Grimes 	ifp->if_flags &= ~IFF_UP;
360e39a0280SGary Palmer 	ifp->if_lastchange = time;
361df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
362df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
363df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
364df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
365df8bae1dSRodney W. Grimes }
366df8bae1dSRodney W. Grimes 
367df8bae1dSRodney W. Grimes /*
368df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
369df8bae1dSRodney W. Grimes  * the transition.
370df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
371df8bae1dSRodney W. Grimes  */
372df8bae1dSRodney W. Grimes void
373df8bae1dSRodney W. Grimes if_up(ifp)
374df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
375df8bae1dSRodney W. Grimes {
376df8bae1dSRodney W. Grimes 
377df8bae1dSRodney W. Grimes 	ifp->if_flags |= IFF_UP;
378e39a0280SGary Palmer 	ifp->if_lastchange = time;
379df8bae1dSRodney W. Grimes #ifdef notyet
3809448326fSPoul-Henning Kamp 	register struct ifaddr *ifa;
381df8bae1dSRodney W. Grimes 	/* this has no effect on IP, and will kill all iso connections XXX */
382df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
383df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
384df8bae1dSRodney W. Grimes #endif
385df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
386df8bae1dSRodney W. Grimes }
387df8bae1dSRodney W. Grimes 
388df8bae1dSRodney W. Grimes /*
389df8bae1dSRodney W. Grimes  * Flush an interface queue.
390df8bae1dSRodney W. Grimes  */
3913bda9f9bSPoul-Henning Kamp static void
392df8bae1dSRodney W. Grimes if_qflush(ifq)
393df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
394df8bae1dSRodney W. Grimes {
395df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
3989448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
399df8bae1dSRodney W. Grimes 		n = m->m_act;
400df8bae1dSRodney W. Grimes 		m_freem(m);
401df8bae1dSRodney W. Grimes 	}
402df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
403df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
404df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
405df8bae1dSRodney W. Grimes }
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes /*
408df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
409df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
410df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
411df8bae1dSRodney W. Grimes  */
4123bda9f9bSPoul-Henning Kamp static void
413df8bae1dSRodney W. Grimes if_slowtimo(arg)
414df8bae1dSRodney W. Grimes 	void *arg;
415df8bae1dSRodney W. Grimes {
416df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
417df8bae1dSRodney W. Grimes 	int s = splimp();
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
420df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
421df8bae1dSRodney W. Grimes 			continue;
422df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
4234a5f1499SDavid Greenman 			(*ifp->if_watchdog)(ifp);
424df8bae1dSRodney W. Grimes 	}
425df8bae1dSRodney W. Grimes 	splx(s);
426df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
427df8bae1dSRodney W. Grimes }
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes /*
430df8bae1dSRodney W. Grimes  * Map interface name to
431df8bae1dSRodney W. Grimes  * interface structure pointer.
432df8bae1dSRodney W. Grimes  */
433df8bae1dSRodney W. Grimes struct ifnet *
434df8bae1dSRodney W. Grimes ifunit(name)
435df8bae1dSRodney W. Grimes 	register char *name;
436df8bae1dSRodney W. Grimes {
437df8bae1dSRodney W. Grimes 	register char *cp;
438df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
439df8bae1dSRodney W. Grimes 	int unit;
440df8bae1dSRodney W. Grimes 	unsigned len;
441df8bae1dSRodney W. Grimes 	char *ep, c;
442df8bae1dSRodney W. Grimes 
443df8bae1dSRodney W. Grimes 	for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
444df8bae1dSRodney W. Grimes 		if (*cp >= '0' && *cp <= '9')
445df8bae1dSRodney W. Grimes 			break;
446df8bae1dSRodney W. Grimes 	if (*cp == '\0' || cp == name + IFNAMSIZ)
447df8bae1dSRodney W. Grimes 		return ((struct ifnet *)0);
448df8bae1dSRodney W. Grimes 	/*
449df8bae1dSRodney W. Grimes 	 * Save first char of unit, and pointer to it,
450df8bae1dSRodney W. Grimes 	 * so we can put a null there to avoid matching
451df8bae1dSRodney W. Grimes 	 * initial substrings of interface names.
452df8bae1dSRodney W. Grimes 	 */
453df8bae1dSRodney W. Grimes 	len = cp - name + 1;
454df8bae1dSRodney W. Grimes 	c = *cp;
455df8bae1dSRodney W. Grimes 	ep = cp;
456df8bae1dSRodney W. Grimes 	for (unit = 0; *cp >= '0' && *cp <= '9'; )
457df8bae1dSRodney W. Grimes 		unit = unit * 10 + *cp++ - '0';
45858639ed3SGarrett Wollman 	if (*cp != '\0')
45958639ed3SGarrett Wollman 		return 0;	/* no trailing garbage allowed */
460df8bae1dSRodney W. Grimes 	*ep = 0;
461df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
462df8bae1dSRodney W. Grimes 		if (bcmp(ifp->if_name, name, len))
463df8bae1dSRodney W. Grimes 			continue;
464df8bae1dSRodney W. Grimes 		if (unit == ifp->if_unit)
465df8bae1dSRodney W. Grimes 			break;
466df8bae1dSRodney W. Grimes 	}
467df8bae1dSRodney W. Grimes 	*ep = c;
468df8bae1dSRodney W. Grimes 	return (ifp);
469df8bae1dSRodney W. Grimes }
470df8bae1dSRodney W. Grimes 
471df8bae1dSRodney W. Grimes /*
472df8bae1dSRodney W. Grimes  * Interface ioctls.
473df8bae1dSRodney W. Grimes  */
474df8bae1dSRodney W. Grimes int
475df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p)
476df8bae1dSRodney W. Grimes 	struct socket *so;
477df8bae1dSRodney W. Grimes 	int cmd;
478df8bae1dSRodney W. Grimes 	caddr_t data;
479df8bae1dSRodney W. Grimes 	struct proc *p;
480df8bae1dSRodney W. Grimes {
481df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
482df8bae1dSRodney W. Grimes 	register struct ifreq *ifr;
483df8bae1dSRodney W. Grimes 	int error;
484df8bae1dSRodney W. Grimes 
485df8bae1dSRodney W. Grimes 	switch (cmd) {
486df8bae1dSRodney W. Grimes 
487df8bae1dSRodney W. Grimes 	case SIOCGIFCONF:
488df8bae1dSRodney W. Grimes 	case OSIOCGIFCONF:
489df8bae1dSRodney W. Grimes 		return (ifconf(cmd, data));
490df8bae1dSRodney W. Grimes 	}
491df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
492df8bae1dSRodney W. Grimes 	ifp = ifunit(ifr->ifr_name);
493df8bae1dSRodney W. Grimes 	if (ifp == 0)
494df8bae1dSRodney W. Grimes 		return (ENXIO);
495df8bae1dSRodney W. Grimes 	switch (cmd) {
496df8bae1dSRodney W. Grimes 
497df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
498df8bae1dSRodney W. Grimes 		ifr->ifr_flags = ifp->if_flags;
499df8bae1dSRodney W. Grimes 		break;
500df8bae1dSRodney W. Grimes 
501df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
502df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
503df8bae1dSRodney W. Grimes 		break;
504df8bae1dSRodney W. Grimes 
505a7028af7SDavid Greenman 	case SIOCGIFMTU:
506a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
507a7028af7SDavid Greenman 		break;
508a7028af7SDavid Greenman 
509074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
510074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
511074c4a4eSGarrett Wollman 		break;
512074c4a4eSGarrett Wollman 
513df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
5149448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5159448326fSPoul-Henning Kamp 		if (error)
516df8bae1dSRodney W. Grimes 			return (error);
517df8bae1dSRodney W. Grimes 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
518df8bae1dSRodney W. Grimes 			int s = splimp();
519df8bae1dSRodney W. Grimes 			if_down(ifp);
520df8bae1dSRodney W. Grimes 			splx(s);
521df8bae1dSRodney W. Grimes 		}
522df8bae1dSRodney W. Grimes 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
523df8bae1dSRodney W. Grimes 			int s = splimp();
524df8bae1dSRodney W. Grimes 			if_up(ifp);
525df8bae1dSRodney W. Grimes 			splx(s);
526df8bae1dSRodney W. Grimes 		}
527df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
528df8bae1dSRodney W. Grimes 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
529df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
530df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
531e39a0280SGary Palmer 		ifp->if_lastchange = time;
532df8bae1dSRodney W. Grimes 		break;
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
5359448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5369448326fSPoul-Henning Kamp 		if (error)
537df8bae1dSRodney W. Grimes 			return (error);
538df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
539e39a0280SGary Palmer 		ifp->if_lastchange = time;
540df8bae1dSRodney W. Grimes 		break;
541df8bae1dSRodney W. Grimes 
542074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
543074c4a4eSGarrett Wollman 		error = suser(p->p_ucred, &p->p_acflag);
544e39a0280SGary Palmer 		if (error)
545e39a0280SGary Palmer 		        return error;
546e39a0280SGary Palmer 		if (!ifp->if_ioctl)
547e39a0280SGary Palmer 		        return EOPNOTSUPP;
548e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
549e39a0280SGary Palmer 		if (error == 0)
550e39a0280SGary Palmer 			ifp->if_lastchange = time;
551e39a0280SGary Palmer 		return(error);
552074c4a4eSGarrett Wollman 
553a7028af7SDavid Greenman 	case SIOCSIFMTU:
5549448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5559448326fSPoul-Henning Kamp 		if (error)
556a7028af7SDavid Greenman 			return (error);
557a7028af7SDavid Greenman 		if (ifp->if_ioctl == NULL)
558a7028af7SDavid Greenman 			return (EOPNOTSUPP);
55966025569SDavid Greenman 		/*
56066025569SDavid Greenman 		 * 72 was chosen below because it is the size of a TCP/IP
56166025569SDavid Greenman 		 * header (40) + the minimum mss (32).
56266025569SDavid Greenman 		 */
56366025569SDavid Greenman 		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
56475ee03cbSDavid Greenman 			return (EINVAL);
565e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
566e39a0280SGary Palmer 		if (error == 0)
567e39a0280SGary Palmer 			ifp->if_lastchange = time;
568e39a0280SGary Palmer 		return(error);
569a7028af7SDavid Greenman 
570df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
571df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
5729448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5739448326fSPoul-Henning Kamp 		if (error)
574df8bae1dSRodney W. Grimes 			return (error);
575df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl == NULL)
576df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
577e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
578e39a0280SGary Palmer 		if (error == 0 )
579e39a0280SGary Palmer 		    	ifp->if_lastchange = time;
580e39a0280SGary Palmer 		return(error);
581df8bae1dSRodney W. Grimes 
582df8bae1dSRodney W. Grimes 	default:
583df8bae1dSRodney W. Grimes 		if (so->so_proto == 0)
584df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
585df8bae1dSRodney W. Grimes #ifndef COMPAT_43
586df8bae1dSRodney W. Grimes 		return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
58727501cb6SBruce Evans 			(struct mbuf *)cmd, (struct mbuf *)data,
58827501cb6SBruce Evans 			(struct mbuf *)ifp));
589df8bae1dSRodney W. Grimes #else
590df8bae1dSRodney W. Grimes 	    {
591df8bae1dSRodney W. Grimes 		int ocmd = cmd;
592df8bae1dSRodney W. Grimes 
593df8bae1dSRodney W. Grimes 		switch (cmd) {
594df8bae1dSRodney W. Grimes 
595df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
596df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
597df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
598df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
599df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
600df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
601df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
602df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
603df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
604df8bae1dSRodney W. Grimes 			}
605df8bae1dSRodney W. Grimes #else
606df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
607df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
608df8bae1dSRodney W. Grimes #endif
609df8bae1dSRodney W. Grimes 			break;
610df8bae1dSRodney W. Grimes 
611df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
612df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
613df8bae1dSRodney W. Grimes 			break;
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
616df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
617df8bae1dSRodney W. Grimes 			break;
618df8bae1dSRodney W. Grimes 
619df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
620df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
621df8bae1dSRodney W. Grimes 			break;
622df8bae1dSRodney W. Grimes 
623df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
624df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
625df8bae1dSRodney W. Grimes 		}
626df8bae1dSRodney W. Grimes 		error =  ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
62727501cb6SBruce Evans 			  /*
62827501cb6SBruce Evans 			   * XXX callees reverse the following bogus casts,
62927501cb6SBruce Evans 			   * but it would be easier to use a separate
63027501cb6SBruce Evans 			   * interface that is guaranteed to work.
63127501cb6SBruce Evans 			   */
63227501cb6SBruce Evans 			  (struct mbuf *)cmd, (struct mbuf *)data,
63327501cb6SBruce Evans 			  (struct mbuf *)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;
697df8bae1dSRodney W. Grimes 	register struct ifnet *ifp = ifnet;
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;
703df8bae1dSRodney W. Grimes 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_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 
714df8bae1dSRodney W. Grimes 		if ((ifa = ifp->if_addrlist) == 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
722df8bae1dSRodney W. Grimes 		    for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) {
723df8bae1dSRodney W. Grimes 			register struct sockaddr *sa = ifa->ifa_addr;
724df8bae1dSRodney W. Grimes #ifdef COMPAT_43
725df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
726df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
727df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
728df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
729df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
730df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
731df8bae1dSRodney W. Grimes 						sizeof (ifr));
732df8bae1dSRodney W. Grimes 				ifrp++;
733df8bae1dSRodney W. Grimes 			} else
734df8bae1dSRodney W. Grimes #endif
735df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
736df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
737df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
738df8bae1dSRodney W. Grimes 						sizeof (ifr));
739df8bae1dSRodney W. Grimes 				ifrp++;
740df8bae1dSRodney W. Grimes 			} else {
741df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
742df8bae1dSRodney W. Grimes 				if (space < sizeof (ifr))
743df8bae1dSRodney W. Grimes 					break;
744df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
745df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
746df8bae1dSRodney W. Grimes 				if (error == 0)
747df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
748df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
749df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
750df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
751df8bae1dSRodney W. Grimes 			}
752df8bae1dSRodney W. Grimes 			if (error)
753df8bae1dSRodney W. Grimes 				break;
754df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
755df8bae1dSRodney W. Grimes 		}
756df8bae1dSRodney W. Grimes 	}
757df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
758df8bae1dSRodney W. Grimes 	return (error);
759df8bae1dSRodney W. Grimes }
760df8bae1dSRodney W. Grimes 
761602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
762