xref: /freebsd/sys/net/if.c (revision 1ce9bf88c347ae279c1ae9eb425f3feae53dad50)
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
341ce9bf88SPoul-Henning Kamp  * $Id: if.c,v 1.25 1995/12/20 21:53:38 wollman Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
39df8bae1dSRodney W. Grimes #include <sys/systm.h>
40df8bae1dSRodney W. Grimes #include <sys/proc.h>
41df8bae1dSRodney W. Grimes #include <sys/socket.h>
42df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
43df8bae1dSRodney W. Grimes #include <sys/protosw.h>
44df8bae1dSRodney W. Grimes #include <sys/kernel.h>
45df8bae1dSRodney W. Grimes #include <sys/ioctl.h>
462624cf89SGarrett Wollman #include <sys/errno.h>
47963e4c2aSGarrett Wollman #include <sys/syslog.h>
48602d513cSGarrett Wollman #include <sys/sysctl.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <net/if.h>
51df8bae1dSRodney W. Grimes #include <net/if_dl.h>
52df8bae1dSRodney W. Grimes #include <net/if_types.h>
539448326fSPoul-Henning Kamp #include <net/radix.h>
54fe95e21fSPoul-Henning Kamp #include <ether.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;
118df8bae1dSRodney W. Grimes 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
119df8bae1dSRodney W. Grimes 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
120df8bae1dSRodney W. Grimes 		struct ifaddr **q = (struct ifaddr **)
121df8bae1dSRodney W. Grimes 					malloc(n, M_IFADDR, M_WAITOK);
12273c2ab46SDavid Greenman 		bzero((caddr_t)q, n);
123df8bae1dSRodney W. Grimes 		if (ifnet_addrs) {
124df8bae1dSRodney W. Grimes 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
125df8bae1dSRodney W. Grimes 			free((caddr_t)ifnet_addrs, M_IFADDR);
126df8bae1dSRodney W. Grimes 		}
127df8bae1dSRodney W. Grimes 		ifnet_addrs = q;
128df8bae1dSRodney W. Grimes 	}
129df8bae1dSRodney W. Grimes 	/*
130df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
131df8bae1dSRodney W. Grimes 	 */
1321ce9bf88SPoul-Henning Kamp 	namelen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
133df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
1341ce9bf88SPoul-Henning Kamp 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
135df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
136df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
137df8bae1dSRodney W. Grimes 	socksize = ROUNDUP(socksize);
138df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
139df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
140df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
1419448326fSPoul-Henning Kamp 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
1429448326fSPoul-Henning Kamp 	if (ifa) {
143df8bae1dSRodney W. Grimes 		bzero((caddr_t)ifa, ifasize);
144df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
145df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
146df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
1471ce9bf88SPoul-Henning Kamp 		bcopy(workbuf, sdl->sdl_data, namelen);
1481ce9bf88SPoul-Henning Kamp 		sdl->sdl_nlen = namelen;
149df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
150df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
151df8bae1dSRodney W. Grimes 		ifnet_addrs[if_index - 1] = ifa;
152df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
153df8bae1dSRodney W. Grimes 		ifa->ifa_next = ifp->if_addrlist;
154df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
155df8bae1dSRodney W. Grimes 		ifp->if_addrlist = ifa;
156df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
1571ce9bf88SPoul-Henning Kamp 
158df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
159df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
160df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
161df8bae1dSRodney W. Grimes 		while (namelen != 0)
162df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
163df8bae1dSRodney W. Grimes 	}
164df8bae1dSRodney W. Grimes 	/* XXX -- Temporary fix before changing 10 ethernet drivers */
165fe95e21fSPoul-Henning Kamp #if NETHER > 0
166df8bae1dSRodney W. Grimes 	if (ifp->if_output == ether_output)
167df8bae1dSRodney W. Grimes 		ether_ifattach(ifp);
168fe95e21fSPoul-Henning Kamp #endif
169df8bae1dSRodney W. Grimes }
170df8bae1dSRodney W. Grimes /*
171df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
172df8bae1dSRodney W. Grimes  */
173df8bae1dSRodney W. Grimes /*ARGSUSED*/
174df8bae1dSRodney W. Grimes struct ifaddr *
175df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
176df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
177df8bae1dSRodney W. Grimes {
178df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
179df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes #define	equal(a1, a2) \
182df8bae1dSRodney W. Grimes   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
183df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
184df8bae1dSRodney W. Grimes 	    for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
185df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != addr->sa_family)
186df8bae1dSRodney W. Grimes 			continue;
187df8bae1dSRodney W. Grimes 		if (equal(addr, ifa->ifa_addr))
188df8bae1dSRodney W. Grimes 			return (ifa);
189df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
190df8bae1dSRodney W. Grimes 		    equal(ifa->ifa_broadaddr, addr))
191df8bae1dSRodney W. Grimes 			return (ifa);
192df8bae1dSRodney W. Grimes 	}
193df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
194df8bae1dSRodney W. Grimes }
195df8bae1dSRodney W. Grimes /*
196df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
197df8bae1dSRodney W. Grimes  */
198df8bae1dSRodney W. Grimes /*ARGSUSED*/
199df8bae1dSRodney W. Grimes struct ifaddr *
200df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
201df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
202df8bae1dSRodney W. Grimes {
203df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
204df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
205df8bae1dSRodney W. Grimes 
206df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
207df8bae1dSRodney W. Grimes 	    if (ifp->if_flags & IFF_POINTOPOINT)
208df8bae1dSRodney W. Grimes 		for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
209df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
210df8bae1dSRodney W. Grimes 				continue;
21155088a1cSDavid Greenman 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
212df8bae1dSRodney W. Grimes 				return (ifa);
213df8bae1dSRodney W. Grimes 	}
214df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
215df8bae1dSRodney W. Grimes }
216df8bae1dSRodney W. Grimes 
217df8bae1dSRodney W. Grimes /*
218df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
219df8bae1dSRodney W. Grimes  * is most specific found.
220df8bae1dSRodney W. Grimes  */
221df8bae1dSRodney W. Grimes struct ifaddr *
222df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
223df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
224df8bae1dSRodney W. Grimes {
225df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
226df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
227df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
228df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
229df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
230df8bae1dSRodney W. Grimes 
231df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
232df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
233df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
234df8bae1dSRodney W. Grimes 		return (ifnet_addrs[sdl->sdl_index - 1]);
235df8bae1dSRodney W. Grimes 	}
236b2af64fdSDavid Greenman 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
237df8bae1dSRodney W. Grimes 		for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
238df8bae1dSRodney W. Grimes 			register char *cp, *cp2, *cp3;
239df8bae1dSRodney W. Grimes 
240523a02aaSDavid Greenman 			if (ifa->ifa_addr->sa_family != af)
241df8bae1dSRodney W. Grimes 				next: continue;
242b2af64fdSDavid Greenman 			if (ifp->if_flags & IFF_POINTOPOINT) {
243523a02aaSDavid Greenman 				if (equal(addr, ifa->ifa_dstaddr))
244b2af64fdSDavid Greenman  					return (ifa);
2453740e2adSDavid Greenman 			} else {
246523a02aaSDavid Greenman 				if (ifa->ifa_netmask == 0)
247523a02aaSDavid Greenman 					continue;
248df8bae1dSRodney W. Grimes 				cp = addr_data;
249df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
250df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
251df8bae1dSRodney W. Grimes 				cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
252df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
253df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
254df8bae1dSRodney W. Grimes 						goto next;
255df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
256df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
257df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
258df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
259df8bae1dSRodney W. Grimes 			}
260b2af64fdSDavid Greenman 		}
261b2af64fdSDavid Greenman 	}
262df8bae1dSRodney W. Grimes 	return (ifa_maybe);
263df8bae1dSRodney W. Grimes }
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes /*
266df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
267df8bae1dSRodney W. Grimes  * a given address.
268df8bae1dSRodney W. Grimes  */
269df8bae1dSRodney W. Grimes struct ifaddr *
270df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
271df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
272df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
273df8bae1dSRodney W. Grimes {
274df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
275df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
276df8bae1dSRodney W. Grimes 	register char *cplim;
277df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
278df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
279df8bae1dSRodney W. Grimes 
280df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
281df8bae1dSRodney W. Grimes 		return (0);
282df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
283df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
284df8bae1dSRodney W. Grimes 			continue;
285df8bae1dSRodney W. Grimes 		ifa_maybe = ifa;
286df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
287df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
288df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
289df8bae1dSRodney W. Grimes 				return (ifa);
290df8bae1dSRodney W. Grimes 			continue;
291df8bae1dSRodney W. Grimes 		}
292b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
293b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
294b2af64fdSDavid Greenman 				return (ifa);
2953740e2adSDavid Greenman 		} else {
296df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
297df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
298df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
299df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
300df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
301df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
302df8bae1dSRodney W. Grimes 					break;
303df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
304df8bae1dSRodney W. Grimes 				return (ifa);
305df8bae1dSRodney W. Grimes 		}
306b2af64fdSDavid Greenman 	}
307df8bae1dSRodney W. Grimes 	return (ifa_maybe);
308df8bae1dSRodney W. Grimes }
309df8bae1dSRodney W. Grimes 
310df8bae1dSRodney W. Grimes #include <net/route.h>
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes /*
313df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
314df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
315df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
316df8bae1dSRodney W. Grimes  */
3173bda9f9bSPoul-Henning Kamp static void
318df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa)
319df8bae1dSRodney W. Grimes 	int cmd;
320df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
321df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
322df8bae1dSRodney W. Grimes {
323df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
324df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
325df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
326df8bae1dSRodney W. Grimes 
327df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
328df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
329df8bae1dSRodney W. Grimes 		return;
3309448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
3319448326fSPoul-Henning Kamp 	if (ifa) {
332df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
333df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
334df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
335df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
336df8bae1dSRodney W. Grimes 			ifa->ifa_rtrequest(cmd, rt, sa);
337df8bae1dSRodney W. Grimes 	}
338df8bae1dSRodney W. Grimes }
339df8bae1dSRodney W. Grimes 
340df8bae1dSRodney W. Grimes /*
341df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
342df8bae1dSRodney W. Grimes  * the transition.
343df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
344df8bae1dSRodney W. Grimes  */
345df8bae1dSRodney W. Grimes void
346df8bae1dSRodney W. Grimes if_down(ifp)
347df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
348df8bae1dSRodney W. Grimes {
349df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
350df8bae1dSRodney W. Grimes 
351df8bae1dSRodney W. Grimes 	ifp->if_flags &= ~IFF_UP;
352df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
353df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
354df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
355df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
356df8bae1dSRodney W. Grimes }
357df8bae1dSRodney W. Grimes 
358df8bae1dSRodney W. Grimes /*
359df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
360df8bae1dSRodney W. Grimes  * the transition.
361df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
362df8bae1dSRodney W. Grimes  */
363df8bae1dSRodney W. Grimes void
364df8bae1dSRodney W. Grimes if_up(ifp)
365df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
366df8bae1dSRodney W. Grimes {
367df8bae1dSRodney W. Grimes 
368df8bae1dSRodney W. Grimes 	ifp->if_flags |= IFF_UP;
369df8bae1dSRodney W. Grimes #ifdef notyet
3709448326fSPoul-Henning Kamp 	register struct ifaddr *ifa;
371df8bae1dSRodney W. Grimes 	/* this has no effect on IP, and will kill all iso connections XXX */
372df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
373df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
374df8bae1dSRodney W. Grimes #endif
375df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
376df8bae1dSRodney W. Grimes }
377df8bae1dSRodney W. Grimes 
378df8bae1dSRodney W. Grimes /*
379df8bae1dSRodney W. Grimes  * Flush an interface queue.
380df8bae1dSRodney W. Grimes  */
3813bda9f9bSPoul-Henning Kamp static void
382df8bae1dSRodney W. Grimes if_qflush(ifq)
383df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
384df8bae1dSRodney W. Grimes {
385df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
3889448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
389df8bae1dSRodney W. Grimes 		n = m->m_act;
390df8bae1dSRodney W. Grimes 		m_freem(m);
391df8bae1dSRodney W. Grimes 	}
392df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
393df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
394df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
395df8bae1dSRodney W. Grimes }
396df8bae1dSRodney W. Grimes 
397df8bae1dSRodney W. Grimes /*
398df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
399df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
400df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
401df8bae1dSRodney W. Grimes  */
4023bda9f9bSPoul-Henning Kamp static void
403df8bae1dSRodney W. Grimes if_slowtimo(arg)
404df8bae1dSRodney W. Grimes 	void *arg;
405df8bae1dSRodney W. Grimes {
406df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
407df8bae1dSRodney W. Grimes 	int s = splimp();
408df8bae1dSRodney W. Grimes 
409df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
410df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
411df8bae1dSRodney W. Grimes 			continue;
412df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
4134a5f1499SDavid Greenman 			(*ifp->if_watchdog)(ifp);
414df8bae1dSRodney W. Grimes 	}
415df8bae1dSRodney W. Grimes 	splx(s);
416df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
417df8bae1dSRodney W. Grimes }
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes /*
420df8bae1dSRodney W. Grimes  * Map interface name to
421df8bae1dSRodney W. Grimes  * interface structure pointer.
422df8bae1dSRodney W. Grimes  */
423df8bae1dSRodney W. Grimes struct ifnet *
424df8bae1dSRodney W. Grimes ifunit(name)
425df8bae1dSRodney W. Grimes 	register char *name;
426df8bae1dSRodney W. Grimes {
427df8bae1dSRodney W. Grimes 	register char *cp;
428df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
429df8bae1dSRodney W. Grimes 	int unit;
430df8bae1dSRodney W. Grimes 	unsigned len;
431df8bae1dSRodney W. Grimes 	char *ep, c;
432df8bae1dSRodney W. Grimes 
433df8bae1dSRodney W. Grimes 	for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
434df8bae1dSRodney W. Grimes 		if (*cp >= '0' && *cp <= '9')
435df8bae1dSRodney W. Grimes 			break;
436df8bae1dSRodney W. Grimes 	if (*cp == '\0' || cp == name + IFNAMSIZ)
437df8bae1dSRodney W. Grimes 		return ((struct ifnet *)0);
438df8bae1dSRodney W. Grimes 	/*
439df8bae1dSRodney W. Grimes 	 * Save first char of unit, and pointer to it,
440df8bae1dSRodney W. Grimes 	 * so we can put a null there to avoid matching
441df8bae1dSRodney W. Grimes 	 * initial substrings of interface names.
442df8bae1dSRodney W. Grimes 	 */
443df8bae1dSRodney W. Grimes 	len = cp - name + 1;
444df8bae1dSRodney W. Grimes 	c = *cp;
445df8bae1dSRodney W. Grimes 	ep = cp;
446df8bae1dSRodney W. Grimes 	for (unit = 0; *cp >= '0' && *cp <= '9'; )
447df8bae1dSRodney W. Grimes 		unit = unit * 10 + *cp++ - '0';
448df8bae1dSRodney W. Grimes 	*ep = 0;
449df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
450df8bae1dSRodney W. Grimes 		if (bcmp(ifp->if_name, name, len))
451df8bae1dSRodney W. Grimes 			continue;
452df8bae1dSRodney W. Grimes 		if (unit == ifp->if_unit)
453df8bae1dSRodney W. Grimes 			break;
454df8bae1dSRodney W. Grimes 	}
455df8bae1dSRodney W. Grimes 	*ep = c;
456df8bae1dSRodney W. Grimes 	return (ifp);
457df8bae1dSRodney W. Grimes }
458df8bae1dSRodney W. Grimes 
459df8bae1dSRodney W. Grimes /*
460df8bae1dSRodney W. Grimes  * Interface ioctls.
461df8bae1dSRodney W. Grimes  */
462df8bae1dSRodney W. Grimes int
463df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p)
464df8bae1dSRodney W. Grimes 	struct socket *so;
465df8bae1dSRodney W. Grimes 	int cmd;
466df8bae1dSRodney W. Grimes 	caddr_t data;
467df8bae1dSRodney W. Grimes 	struct proc *p;
468df8bae1dSRodney W. Grimes {
469df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
470df8bae1dSRodney W. Grimes 	register struct ifreq *ifr;
471df8bae1dSRodney W. Grimes 	int error;
472df8bae1dSRodney W. Grimes 
473df8bae1dSRodney W. Grimes 	switch (cmd) {
474df8bae1dSRodney W. Grimes 
475df8bae1dSRodney W. Grimes 	case SIOCGIFCONF:
476df8bae1dSRodney W. Grimes 	case OSIOCGIFCONF:
477df8bae1dSRodney W. Grimes 		return (ifconf(cmd, data));
478df8bae1dSRodney W. Grimes 	}
479df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
480df8bae1dSRodney W. Grimes 	ifp = ifunit(ifr->ifr_name);
481df8bae1dSRodney W. Grimes 	if (ifp == 0)
482df8bae1dSRodney W. Grimes 		return (ENXIO);
483df8bae1dSRodney W. Grimes 	switch (cmd) {
484df8bae1dSRodney W. Grimes 
485df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
486df8bae1dSRodney W. Grimes 		ifr->ifr_flags = ifp->if_flags;
487df8bae1dSRodney W. Grimes 		break;
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
490df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
491df8bae1dSRodney W. Grimes 		break;
492df8bae1dSRodney W. Grimes 
493a7028af7SDavid Greenman 	case SIOCGIFMTU:
494a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
495a7028af7SDavid Greenman 		break;
496a7028af7SDavid Greenman 
497074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
498074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
499074c4a4eSGarrett Wollman 		break;
500074c4a4eSGarrett Wollman 
501df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
5029448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5039448326fSPoul-Henning Kamp 		if (error)
504df8bae1dSRodney W. Grimes 			return (error);
505df8bae1dSRodney W. Grimes 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
506df8bae1dSRodney W. Grimes 			int s = splimp();
507df8bae1dSRodney W. Grimes 			if_down(ifp);
508df8bae1dSRodney W. Grimes 			splx(s);
509df8bae1dSRodney W. Grimes 		}
510df8bae1dSRodney W. Grimes 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
511df8bae1dSRodney W. Grimes 			int s = splimp();
512df8bae1dSRodney W. Grimes 			if_up(ifp);
513df8bae1dSRodney W. Grimes 			splx(s);
514df8bae1dSRodney W. Grimes 		}
515df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
516df8bae1dSRodney W. Grimes 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
517df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
518df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
519df8bae1dSRodney W. Grimes 		break;
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
5229448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5239448326fSPoul-Henning Kamp 		if (error)
524df8bae1dSRodney W. Grimes 			return (error);
525df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
526df8bae1dSRodney W. Grimes 		break;
527df8bae1dSRodney W. Grimes 
528074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
529074c4a4eSGarrett Wollman 		error = suser(p->p_ucred, &p->p_acflag);
530074c4a4eSGarrett Wollman 		if (error) return error;
531074c4a4eSGarrett Wollman 
532074c4a4eSGarrett Wollman 		if (!ifp->if_ioctl) return EOPNOTSUPP;
533074c4a4eSGarrett Wollman 		return ifp->if_ioctl(ifp, cmd, data);
534074c4a4eSGarrett Wollman 
535a7028af7SDavid Greenman 	case SIOCSIFMTU:
5369448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5379448326fSPoul-Henning Kamp 		if (error)
538a7028af7SDavid Greenman 			return (error);
539a7028af7SDavid Greenman 		if (ifp->if_ioctl == NULL)
540a7028af7SDavid Greenman 			return (EOPNOTSUPP);
54166025569SDavid Greenman 		/*
54266025569SDavid Greenman 		 * 72 was chosen below because it is the size of a TCP/IP
54366025569SDavid Greenman 		 * header (40) + the minimum mss (32).
54466025569SDavid Greenman 		 */
54566025569SDavid Greenman 		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
54675ee03cbSDavid Greenman 			return (EINVAL);
547a7028af7SDavid Greenman 		return ((*ifp->if_ioctl)(ifp, cmd, data));
548a7028af7SDavid Greenman 
549df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
550df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
5519448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5529448326fSPoul-Henning Kamp 		if (error)
553df8bae1dSRodney W. Grimes 			return (error);
554df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl == NULL)
555df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
556df8bae1dSRodney W. Grimes 		return ((*ifp->if_ioctl)(ifp, cmd, data));
557df8bae1dSRodney W. Grimes 
558df8bae1dSRodney W. Grimes 	default:
559df8bae1dSRodney W. Grimes 		if (so->so_proto == 0)
560df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
561df8bae1dSRodney W. Grimes #ifndef COMPAT_43
562df8bae1dSRodney W. Grimes 		return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
56327501cb6SBruce Evans 			(struct mbuf *)cmd, (struct mbuf *)data,
56427501cb6SBruce Evans 			(struct mbuf *)ifp));
565df8bae1dSRodney W. Grimes #else
566df8bae1dSRodney W. Grimes 	    {
567df8bae1dSRodney W. Grimes 		int ocmd = cmd;
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes 		switch (cmd) {
570df8bae1dSRodney W. Grimes 
571df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
572df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
573df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
574df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
575df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
576df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
577df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
578df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
579df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
580df8bae1dSRodney W. Grimes 			}
581df8bae1dSRodney W. Grimes #else
582df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
583df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
584df8bae1dSRodney W. Grimes #endif
585df8bae1dSRodney W. Grimes 			break;
586df8bae1dSRodney W. Grimes 
587df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
588df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
589df8bae1dSRodney W. Grimes 			break;
590df8bae1dSRodney W. Grimes 
591df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
592df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
593df8bae1dSRodney W. Grimes 			break;
594df8bae1dSRodney W. Grimes 
595df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
596df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
597df8bae1dSRodney W. Grimes 			break;
598df8bae1dSRodney W. Grimes 
599df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
600df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
601df8bae1dSRodney W. Grimes 		}
602df8bae1dSRodney W. Grimes 		error =  ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
60327501cb6SBruce Evans 			  /*
60427501cb6SBruce Evans 			   * XXX callees reverse the following bogus casts,
60527501cb6SBruce Evans 			   * but it would be easier to use a separate
60627501cb6SBruce Evans 			   * interface that is guaranteed to work.
60727501cb6SBruce Evans 			   */
60827501cb6SBruce Evans 			  (struct mbuf *)cmd, (struct mbuf *)data,
60927501cb6SBruce Evans 			  (struct mbuf *)ifp));
610df8bae1dSRodney W. Grimes 		switch (ocmd) {
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
613df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
614df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
615df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
616df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
617df8bae1dSRodney W. Grimes 		}
618df8bae1dSRodney W. Grimes 		return (error);
619df8bae1dSRodney W. Grimes 
620df8bae1dSRodney W. Grimes 	    }
621df8bae1dSRodney W. Grimes #endif
622df8bae1dSRodney W. Grimes 	}
623df8bae1dSRodney W. Grimes 	return (0);
624df8bae1dSRodney W. Grimes }
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes /*
627963e4c2aSGarrett Wollman  * Set/clear promiscuous mode on interface ifp based on the truth value
628963e4c2aSGarrett Wollman  * of pswitch.  The calls are reference counted so that only the first
629963e4c2aSGarrett Wollman  * "on" request actually has an effect, as does the final "off" request.
630963e4c2aSGarrett Wollman  * Results are undefined if the "off" and "on" requests are not matched.
631963e4c2aSGarrett Wollman  */
632963e4c2aSGarrett Wollman int
633963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch)
634963e4c2aSGarrett Wollman 	struct ifnet *ifp;
635963e4c2aSGarrett Wollman 	int pswitch;
636963e4c2aSGarrett Wollman {
637963e4c2aSGarrett Wollman 	struct ifreq ifr;
638963e4c2aSGarrett Wollman 
639963e4c2aSGarrett Wollman 	if (pswitch) {
640963e4c2aSGarrett Wollman 		/*
641963e4c2aSGarrett Wollman 		 * If the device is not configured up, we cannot put it in
642963e4c2aSGarrett Wollman 		 * promiscuous mode.
643963e4c2aSGarrett Wollman 		 */
644963e4c2aSGarrett Wollman 		if ((ifp->if_flags & IFF_UP) == 0)
645963e4c2aSGarrett Wollman 			return (ENETDOWN);
646963e4c2aSGarrett Wollman 		if (ifp->if_pcount++ != 0)
647963e4c2aSGarrett Wollman 			return (0);
648963e4c2aSGarrett Wollman 		ifp->if_flags |= IFF_PROMISC;
649e9a30d00SGarrett Wollman 		log(LOG_INFO, "%s%d: promiscuous mode enabled\n",
650963e4c2aSGarrett Wollman 		    ifp->if_name, ifp->if_unit);
651963e4c2aSGarrett Wollman 	} else {
652963e4c2aSGarrett Wollman 		if (--ifp->if_pcount > 0)
653963e4c2aSGarrett Wollman 			return (0);
654963e4c2aSGarrett Wollman 		ifp->if_flags &= ~IFF_PROMISC;
655963e4c2aSGarrett Wollman 	}
656963e4c2aSGarrett Wollman 	ifr.ifr_flags = ifp->if_flags;
657963e4c2aSGarrett Wollman 	return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr));
658963e4c2aSGarrett Wollman }
659963e4c2aSGarrett Wollman 
660963e4c2aSGarrett Wollman /*
661df8bae1dSRodney W. Grimes  * Return interface configuration
662df8bae1dSRodney W. Grimes  * of system.  List may be used
663df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
664df8bae1dSRodney W. Grimes  * other information.
665df8bae1dSRodney W. Grimes  */
666df8bae1dSRodney W. Grimes /*ARGSUSED*/
6673bda9f9bSPoul-Henning Kamp static int
668df8bae1dSRodney W. Grimes ifconf(cmd, data)
669df8bae1dSRodney W. Grimes 	int cmd;
670df8bae1dSRodney W. Grimes 	caddr_t data;
671df8bae1dSRodney W. Grimes {
672df8bae1dSRodney W. Grimes 	register struct ifconf *ifc = (struct ifconf *)data;
673df8bae1dSRodney W. Grimes 	register struct ifnet *ifp = ifnet;
674df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
675df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
676df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
677df8bae1dSRodney W. Grimes 
678df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
679df8bae1dSRodney W. Grimes 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) {
6801ce9bf88SPoul-Henning Kamp 		char workbuf[64];
6811ce9bf88SPoul-Henning Kamp 		int ifnlen;
6822624cf89SGarrett Wollman 
6831ce9bf88SPoul-Henning Kamp 		ifnlen = sprintf(workbuf, "%s%d", ifp->if_name, ifp->if_unit);
6841ce9bf88SPoul-Henning Kamp 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
6852624cf89SGarrett Wollman 			error = ENAMETOOLONG;
6862624cf89SGarrett Wollman 		} else {
6871ce9bf88SPoul-Henning Kamp 			strcpy(ifr.ifr_name, workbuf);
6882624cf89SGarrett Wollman 		}
6892624cf89SGarrett Wollman 
690df8bae1dSRodney W. Grimes 		if ((ifa = ifp->if_addrlist) == 0) {
691df8bae1dSRodney W. Grimes 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
692df8bae1dSRodney W. Grimes 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
693df8bae1dSRodney W. Grimes 			    sizeof (ifr));
694df8bae1dSRodney W. Grimes 			if (error)
695df8bae1dSRodney W. Grimes 				break;
696df8bae1dSRodney W. Grimes 			space -= sizeof (ifr), ifrp++;
697df8bae1dSRodney W. Grimes 		} else
698df8bae1dSRodney W. Grimes 		    for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) {
699df8bae1dSRodney W. Grimes 			register struct sockaddr *sa = ifa->ifa_addr;
700df8bae1dSRodney W. Grimes #ifdef COMPAT_43
701df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
702df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
703df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
704df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
705df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
706df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
707df8bae1dSRodney W. Grimes 						sizeof (ifr));
708df8bae1dSRodney W. Grimes 				ifrp++;
709df8bae1dSRodney W. Grimes 			} else
710df8bae1dSRodney W. Grimes #endif
711df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
712df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
713df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
714df8bae1dSRodney W. Grimes 						sizeof (ifr));
715df8bae1dSRodney W. Grimes 				ifrp++;
716df8bae1dSRodney W. Grimes 			} else {
717df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
718df8bae1dSRodney W. Grimes 				if (space < sizeof (ifr))
719df8bae1dSRodney W. Grimes 					break;
720df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
721df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
722df8bae1dSRodney W. Grimes 				if (error == 0)
723df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
724df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
725df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
726df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
727df8bae1dSRodney W. Grimes 			}
728df8bae1dSRodney W. Grimes 			if (error)
729df8bae1dSRodney W. Grimes 				break;
730df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
731df8bae1dSRodney W. Grimes 		}
732df8bae1dSRodney W. Grimes 	}
733df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
734df8bae1dSRodney W. Grimes 	return (error);
735df8bae1dSRodney W. Grimes }
736df8bae1dSRodney W. Grimes 
737602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
738