xref: /freebsd/sys/net/if.c (revision b2af64fd039ed18a2834634e676a275a59071ed3)
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
34b2af64fdSDavid Greenman  * $Id: if.c,v 1.13 1995/02/24 11:47:31 davidg 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>
47df8bae1dSRodney W. Grimes 
48df8bae1dSRodney W. Grimes #include <net/if.h>
49df8bae1dSRodney W. Grimes #include <net/if_dl.h>
50df8bae1dSRodney W. Grimes #include <net/if_types.h>
519448326fSPoul-Henning Kamp #include <net/radix.h>
52fe95e21fSPoul-Henning Kamp #include <ether.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes int	ifqmaxlen = IFQ_MAXLEN;
5573c2ab46SDavid Greenman struct	ifnet *ifnet;
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes /*
58df8bae1dSRodney W. Grimes  * Network interface utility routines.
59df8bae1dSRodney W. Grimes  *
60df8bae1dSRodney W. Grimes  * Routines with ifa_ifwith* names take sockaddr *'s as
61df8bae1dSRodney W. Grimes  * parameters.
62df8bae1dSRodney W. Grimes  */
63df8bae1dSRodney W. Grimes void
64df8bae1dSRodney W. Grimes ifinit()
65df8bae1dSRodney W. Grimes {
66df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
69df8bae1dSRodney W. Grimes 		if (ifp->if_snd.ifq_maxlen == 0)
70df8bae1dSRodney W. Grimes 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
71df8bae1dSRodney W. Grimes 	if_slowtimo(0);
72df8bae1dSRodney W. Grimes }
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #ifdef vax
75df8bae1dSRodney W. Grimes /*
76df8bae1dSRodney W. Grimes  * Call each interface on a Unibus reset.
77df8bae1dSRodney W. Grimes  */
78df8bae1dSRodney W. Grimes void
79df8bae1dSRodney W. Grimes ifubareset(uban)
80df8bae1dSRodney W. Grimes 	int uban;
81df8bae1dSRodney W. Grimes {
82df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
83df8bae1dSRodney W. Grimes 
84df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
85df8bae1dSRodney W. Grimes 		if (ifp->if_reset)
86df8bae1dSRodney W. Grimes 			(*ifp->if_reset)(ifp->if_unit, uban);
87df8bae1dSRodney W. Grimes }
88df8bae1dSRodney W. Grimes #endif
89df8bae1dSRodney W. Grimes 
90df8bae1dSRodney W. Grimes int if_index = 0;
91df8bae1dSRodney W. Grimes struct ifaddr **ifnet_addrs;
92df8bae1dSRodney W. Grimes static char *sprint_d __P((u_int, char *, int));
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes /*
95df8bae1dSRodney W. Grimes  * Attach an interface to the
96df8bae1dSRodney W. Grimes  * list of "active" interfaces.
97df8bae1dSRodney W. Grimes  */
98df8bae1dSRodney W. Grimes void
99df8bae1dSRodney W. Grimes if_attach(ifp)
100df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
101df8bae1dSRodney W. Grimes {
102df8bae1dSRodney W. Grimes 	unsigned socksize, ifasize;
103f23b4c91SGarrett Wollman 	int namelen, unitlen, masklen;
104df8bae1dSRodney W. Grimes 	char workbuf[12], *unitname;
105df8bae1dSRodney W. Grimes 	register struct ifnet **p = &ifnet;
106df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
107df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
108df8bae1dSRodney W. Grimes 	static int if_indexlim = 8;
109f23b4c91SGarrett Wollman 
110df8bae1dSRodney W. Grimes 
111df8bae1dSRodney W. Grimes 	while (*p)
112df8bae1dSRodney W. Grimes 		p = &((*p)->if_next);
113df8bae1dSRodney W. Grimes 	*p = ifp;
114df8bae1dSRodney W. Grimes 	ifp->if_index = ++if_index;
115df8bae1dSRodney W. Grimes 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
116df8bae1dSRodney W. Grimes 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
117df8bae1dSRodney W. Grimes 		struct ifaddr **q = (struct ifaddr **)
118df8bae1dSRodney W. Grimes 					malloc(n, M_IFADDR, M_WAITOK);
11973c2ab46SDavid Greenman 		bzero((caddr_t)q, n);
120df8bae1dSRodney W. Grimes 		if (ifnet_addrs) {
121df8bae1dSRodney W. Grimes 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
122df8bae1dSRodney W. Grimes 			free((caddr_t)ifnet_addrs, M_IFADDR);
123df8bae1dSRodney W. Grimes 		}
124df8bae1dSRodney W. Grimes 		ifnet_addrs = q;
125df8bae1dSRodney W. Grimes 	}
126df8bae1dSRodney W. Grimes 	/*
127df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
128df8bae1dSRodney W. Grimes 	 */
129df8bae1dSRodney W. Grimes 	unitname = sprint_d((u_int)ifp->if_unit, workbuf, sizeof(workbuf));
130df8bae1dSRodney W. Grimes 	namelen = strlen(ifp->if_name);
131df8bae1dSRodney W. Grimes 	unitlen = strlen(unitname);
132df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
133df8bae1dSRodney W. Grimes 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) +
134df8bae1dSRodney W. Grimes 			       unitlen + 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;
147df8bae1dSRodney W. Grimes 		bcopy(ifp->if_name, sdl->sdl_data, namelen);
148df8bae1dSRodney W. Grimes 		bcopy(unitname, namelen + (caddr_t)sdl->sdl_data, unitlen);
149df8bae1dSRodney W. Grimes 		sdl->sdl_nlen = (namelen += unitlen);
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;
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 
240df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != af || ifa->ifa_netmask == 0)
241df8bae1dSRodney W. Grimes 				next: continue;
242b2af64fdSDavid Greenman #ifdef P2P_LOCALADDR_SHARE
243b2af64fdSDavid Greenman 			if (ifp->if_flags & IFF_POINTOPOINT) {
244b2af64fdSDavid Greenman 				if (equal(addr, ifa->ifa_addr))
245b2af64fdSDavid Greenman  					return (ifa);
246b2af64fdSDavid Greenman 			} else
247b2af64fdSDavid Greenman #endif /* P2P_LOCALADDR_SHARE */
248b2af64fdSDavid Greenman 			{
249df8bae1dSRodney W. Grimes 				cp = addr_data;
250df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
251df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
252df8bae1dSRodney W. Grimes 				cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
253df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
254df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
255df8bae1dSRodney W. Grimes 						goto next;
256df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
257df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
258df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
259df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
260df8bae1dSRodney W. Grimes 			}
261b2af64fdSDavid Greenman 		}
262b2af64fdSDavid Greenman 	}
263df8bae1dSRodney W. Grimes 	return (ifa_maybe);
264df8bae1dSRodney W. Grimes }
265df8bae1dSRodney W. Grimes 
266df8bae1dSRodney W. Grimes /*
267df8bae1dSRodney W. Grimes  * Find an interface using a specific address family
268df8bae1dSRodney W. Grimes  */
269df8bae1dSRodney W. Grimes struct ifaddr *
270df8bae1dSRodney W. Grimes ifa_ifwithaf(af)
271df8bae1dSRodney W. Grimes 	register int af;
272df8bae1dSRodney W. Grimes {
273df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
274df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
275df8bae1dSRodney W. Grimes 
276df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
277df8bae1dSRodney W. Grimes 	    for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
278df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == af)
279df8bae1dSRodney W. Grimes 			return (ifa);
280df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
281df8bae1dSRodney W. Grimes }
282df8bae1dSRodney W. Grimes 
283df8bae1dSRodney W. Grimes /*
284df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
285df8bae1dSRodney W. Grimes  * a given address.
286df8bae1dSRodney W. Grimes  */
287df8bae1dSRodney W. Grimes struct ifaddr *
288df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
289df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
290df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
291df8bae1dSRodney W. Grimes {
292df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
293df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
294df8bae1dSRodney W. Grimes 	register char *cplim;
295df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
296df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
299df8bae1dSRodney W. Grimes 		return (0);
300df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
301df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
302df8bae1dSRodney W. Grimes 			continue;
303df8bae1dSRodney W. Grimes 		ifa_maybe = ifa;
304df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
305df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
306df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
307df8bae1dSRodney W. Grimes 				return (ifa);
308df8bae1dSRodney W. Grimes 			continue;
309df8bae1dSRodney W. Grimes 		}
310b2af64fdSDavid Greenman #ifdef P2P_LOCALADDR_SHARE
311b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
312b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
313b2af64fdSDavid Greenman 				return (ifa);
314b2af64fdSDavid Greenman 		} else
315b2af64fdSDavid Greenman #endif /* P2P_LOCALADDR_SHARE */
316b2af64fdSDavid Greenman 		{
317df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
318df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
319df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
320df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
321df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
322df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
323df8bae1dSRodney W. Grimes 					break;
324df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
325df8bae1dSRodney W. Grimes 				return (ifa);
326df8bae1dSRodney W. Grimes 		}
327b2af64fdSDavid Greenman 	}
328df8bae1dSRodney W. Grimes 	return (ifa_maybe);
329df8bae1dSRodney W. Grimes }
330df8bae1dSRodney W. Grimes 
331df8bae1dSRodney W. Grimes #include <net/route.h>
332df8bae1dSRodney W. Grimes 
333df8bae1dSRodney W. Grimes /*
334df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
335df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
336df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
337df8bae1dSRodney W. Grimes  */
338df8bae1dSRodney W. Grimes void
339df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa)
340df8bae1dSRodney W. Grimes 	int cmd;
341df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
342df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
343df8bae1dSRodney W. Grimes {
344df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
345df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
346df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
347df8bae1dSRodney W. Grimes 
348df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
349df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
350df8bae1dSRodney W. Grimes 		return;
3519448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
3529448326fSPoul-Henning Kamp 	if (ifa) {
353df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
354df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
355df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
356df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
357df8bae1dSRodney W. Grimes 			ifa->ifa_rtrequest(cmd, rt, sa);
358df8bae1dSRodney W. Grimes 	}
359df8bae1dSRodney W. Grimes }
360df8bae1dSRodney W. Grimes 
361df8bae1dSRodney W. Grimes /*
362df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
363df8bae1dSRodney W. Grimes  * the transition.
364df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
365df8bae1dSRodney W. Grimes  */
366df8bae1dSRodney W. Grimes void
367df8bae1dSRodney W. Grimes if_down(ifp)
368df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
369df8bae1dSRodney W. Grimes {
370df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
371df8bae1dSRodney W. Grimes 
372df8bae1dSRodney W. Grimes 	ifp->if_flags &= ~IFF_UP;
373df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
374df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
375df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
376df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
377df8bae1dSRodney W. Grimes }
378df8bae1dSRodney W. Grimes 
379df8bae1dSRodney W. Grimes /*
380df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
381df8bae1dSRodney W. Grimes  * the transition.
382df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
383df8bae1dSRodney W. Grimes  */
384df8bae1dSRodney W. Grimes void
385df8bae1dSRodney W. Grimes if_up(ifp)
386df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
387df8bae1dSRodney W. Grimes {
388df8bae1dSRodney W. Grimes 
389df8bae1dSRodney W. Grimes 	ifp->if_flags |= IFF_UP;
390df8bae1dSRodney W. Grimes #ifdef notyet
3919448326fSPoul-Henning Kamp 	register struct ifaddr *ifa;
392df8bae1dSRodney W. Grimes 	/* this has no effect on IP, and will kill all iso connections XXX */
393df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
394df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
395df8bae1dSRodney W. Grimes #endif
396df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
397df8bae1dSRodney W. Grimes }
398df8bae1dSRodney W. Grimes 
399df8bae1dSRodney W. Grimes /*
400df8bae1dSRodney W. Grimes  * Flush an interface queue.
401df8bae1dSRodney W. Grimes  */
402df8bae1dSRodney W. Grimes void
403df8bae1dSRodney W. Grimes if_qflush(ifq)
404df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
405df8bae1dSRodney W. Grimes {
406df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
4099448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
410df8bae1dSRodney W. Grimes 		n = m->m_act;
411df8bae1dSRodney W. Grimes 		m_freem(m);
412df8bae1dSRodney W. Grimes 	}
413df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
414df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
415df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
416df8bae1dSRodney W. Grimes }
417df8bae1dSRodney W. Grimes 
418df8bae1dSRodney W. Grimes /*
419df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
420df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
421df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
422df8bae1dSRodney W. Grimes  */
423df8bae1dSRodney W. Grimes void
424df8bae1dSRodney W. Grimes if_slowtimo(arg)
425df8bae1dSRodney W. Grimes 	void *arg;
426df8bae1dSRodney W. Grimes {
427df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
428df8bae1dSRodney W. Grimes 	int s = splimp();
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
431df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
432df8bae1dSRodney W. Grimes 			continue;
433df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
434df8bae1dSRodney W. Grimes 			(*ifp->if_watchdog)(ifp->if_unit);
435df8bae1dSRodney W. Grimes 	}
436df8bae1dSRodney W. Grimes 	splx(s);
437df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
438df8bae1dSRodney W. Grimes }
439df8bae1dSRodney W. Grimes 
440df8bae1dSRodney W. Grimes /*
441df8bae1dSRodney W. Grimes  * Map interface name to
442df8bae1dSRodney W. Grimes  * interface structure pointer.
443df8bae1dSRodney W. Grimes  */
444df8bae1dSRodney W. Grimes struct ifnet *
445df8bae1dSRodney W. Grimes ifunit(name)
446df8bae1dSRodney W. Grimes 	register char *name;
447df8bae1dSRodney W. Grimes {
448df8bae1dSRodney W. Grimes 	register char *cp;
449df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
450df8bae1dSRodney W. Grimes 	int unit;
451df8bae1dSRodney W. Grimes 	unsigned len;
452df8bae1dSRodney W. Grimes 	char *ep, c;
453df8bae1dSRodney W. Grimes 
454df8bae1dSRodney W. Grimes 	for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
455df8bae1dSRodney W. Grimes 		if (*cp >= '0' && *cp <= '9')
456df8bae1dSRodney W. Grimes 			break;
457df8bae1dSRodney W. Grimes 	if (*cp == '\0' || cp == name + IFNAMSIZ)
458df8bae1dSRodney W. Grimes 		return ((struct ifnet *)0);
459df8bae1dSRodney W. Grimes 	/*
460df8bae1dSRodney W. Grimes 	 * Save first char of unit, and pointer to it,
461df8bae1dSRodney W. Grimes 	 * so we can put a null there to avoid matching
462df8bae1dSRodney W. Grimes 	 * initial substrings of interface names.
463df8bae1dSRodney W. Grimes 	 */
464df8bae1dSRodney W. Grimes 	len = cp - name + 1;
465df8bae1dSRodney W. Grimes 	c = *cp;
466df8bae1dSRodney W. Grimes 	ep = cp;
467df8bae1dSRodney W. Grimes 	for (unit = 0; *cp >= '0' && *cp <= '9'; )
468df8bae1dSRodney W. Grimes 		unit = unit * 10 + *cp++ - '0';
469df8bae1dSRodney W. Grimes 	*ep = 0;
470df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
471df8bae1dSRodney W. Grimes 		if (bcmp(ifp->if_name, name, len))
472df8bae1dSRodney W. Grimes 			continue;
473df8bae1dSRodney W. Grimes 		if (unit == ifp->if_unit)
474df8bae1dSRodney W. Grimes 			break;
475df8bae1dSRodney W. Grimes 	}
476df8bae1dSRodney W. Grimes 	*ep = c;
477df8bae1dSRodney W. Grimes 	return (ifp);
478df8bae1dSRodney W. Grimes }
479df8bae1dSRodney W. Grimes 
480df8bae1dSRodney W. Grimes /*
481df8bae1dSRodney W. Grimes  * Interface ioctls.
482df8bae1dSRodney W. Grimes  */
483df8bae1dSRodney W. Grimes int
484df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p)
485df8bae1dSRodney W. Grimes 	struct socket *so;
486df8bae1dSRodney W. Grimes 	int cmd;
487df8bae1dSRodney W. Grimes 	caddr_t data;
488df8bae1dSRodney W. Grimes 	struct proc *p;
489df8bae1dSRodney W. Grimes {
490df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
491df8bae1dSRodney W. Grimes 	register struct ifreq *ifr;
492df8bae1dSRodney W. Grimes 	int error;
493df8bae1dSRodney W. Grimes 
494df8bae1dSRodney W. Grimes 	switch (cmd) {
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes 	case SIOCGIFCONF:
497df8bae1dSRodney W. Grimes 	case OSIOCGIFCONF:
498df8bae1dSRodney W. Grimes 		return (ifconf(cmd, data));
499df8bae1dSRodney W. Grimes 	}
500df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
501df8bae1dSRodney W. Grimes 	ifp = ifunit(ifr->ifr_name);
502df8bae1dSRodney W. Grimes 	if (ifp == 0)
503df8bae1dSRodney W. Grimes 		return (ENXIO);
504df8bae1dSRodney W. Grimes 	switch (cmd) {
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
507df8bae1dSRodney W. Grimes 		ifr->ifr_flags = ifp->if_flags;
508df8bae1dSRodney W. Grimes 		break;
509df8bae1dSRodney W. Grimes 
510df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
511df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
512df8bae1dSRodney W. Grimes 		break;
513df8bae1dSRodney W. Grimes 
514a7028af7SDavid Greenman 	case SIOCGIFMTU:
515a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
516a7028af7SDavid Greenman 		break;
517a7028af7SDavid Greenman 
518074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
519074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
520074c4a4eSGarrett Wollman 		break;
521074c4a4eSGarrett Wollman 
522df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
5239448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5249448326fSPoul-Henning Kamp 		if (error)
525df8bae1dSRodney W. Grimes 			return (error);
526df8bae1dSRodney W. Grimes 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
527df8bae1dSRodney W. Grimes 			int s = splimp();
528df8bae1dSRodney W. Grimes 			if_down(ifp);
529df8bae1dSRodney W. Grimes 			splx(s);
530df8bae1dSRodney W. Grimes 		}
531df8bae1dSRodney W. Grimes 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
532df8bae1dSRodney W. Grimes 			int s = splimp();
533df8bae1dSRodney W. Grimes 			if_up(ifp);
534df8bae1dSRodney W. Grimes 			splx(s);
535df8bae1dSRodney W. Grimes 		}
536df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
537df8bae1dSRodney W. Grimes 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
538df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
539df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
540df8bae1dSRodney W. Grimes 		break;
541df8bae1dSRodney W. Grimes 
542df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
5439448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5449448326fSPoul-Henning Kamp 		if (error)
545df8bae1dSRodney W. Grimes 			return (error);
546df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
547df8bae1dSRodney W. Grimes 		break;
548df8bae1dSRodney W. Grimes 
549074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
550074c4a4eSGarrett Wollman 		error = suser(p->p_ucred, &p->p_acflag);
551074c4a4eSGarrett Wollman 		if (error) return error;
552074c4a4eSGarrett Wollman 
553074c4a4eSGarrett Wollman 		if (!ifp->if_ioctl) return EOPNOTSUPP;
554074c4a4eSGarrett Wollman 		return ifp->if_ioctl(ifp, cmd, data);
555074c4a4eSGarrett Wollman 
556a7028af7SDavid Greenman 	case SIOCSIFMTU:
5579448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5589448326fSPoul-Henning Kamp 		if (error)
559a7028af7SDavid Greenman 			return (error);
560a7028af7SDavid Greenman 		if (ifp->if_ioctl == NULL)
561a7028af7SDavid Greenman 			return (EOPNOTSUPP);
56266025569SDavid Greenman 		/*
56366025569SDavid Greenman 		 * 72 was chosen below because it is the size of a TCP/IP
56466025569SDavid Greenman 		 * header (40) + the minimum mss (32).
56566025569SDavid Greenman 		 */
56666025569SDavid Greenman 		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
56775ee03cbSDavid Greenman 			return (EINVAL);
568a7028af7SDavid Greenman 		return ((*ifp->if_ioctl)(ifp, cmd, data));
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);
577df8bae1dSRodney W. Grimes 		return ((*ifp->if_ioctl)(ifp, cmd, data));
578df8bae1dSRodney W. Grimes 
579df8bae1dSRodney W. Grimes 	default:
580df8bae1dSRodney W. Grimes 		if (so->so_proto == 0)
581df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
582df8bae1dSRodney W. Grimes #ifndef COMPAT_43
583df8bae1dSRodney W. Grimes 		return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
584df8bae1dSRodney W. Grimes 			cmd, data, ifp));
585df8bae1dSRodney W. Grimes #else
586df8bae1dSRodney W. Grimes 	    {
587df8bae1dSRodney W. Grimes 		int ocmd = cmd;
588df8bae1dSRodney W. Grimes 
589df8bae1dSRodney W. Grimes 		switch (cmd) {
590df8bae1dSRodney W. Grimes 
591df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
592df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
593df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
594df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
595df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
596df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
597df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
598df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
599df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
600df8bae1dSRodney W. Grimes 			}
601df8bae1dSRodney W. Grimes #else
602df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
603df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
604df8bae1dSRodney W. Grimes #endif
605df8bae1dSRodney W. Grimes 			break;
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
608df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
609df8bae1dSRodney W. Grimes 			break;
610df8bae1dSRodney W. Grimes 
611df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
612df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
613df8bae1dSRodney W. Grimes 			break;
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
616df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
617df8bae1dSRodney W. Grimes 			break;
618df8bae1dSRodney W. Grimes 
619df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
620df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
621df8bae1dSRodney W. Grimes 		}
622df8bae1dSRodney W. Grimes 		error =  ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
623df8bae1dSRodney W. Grimes 							    cmd, data, ifp));
624df8bae1dSRodney W. Grimes 		switch (ocmd) {
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
627df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
628df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
629df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
630df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
631df8bae1dSRodney W. Grimes 		}
632df8bae1dSRodney W. Grimes 		return (error);
633df8bae1dSRodney W. Grimes 
634df8bae1dSRodney W. Grimes 	    }
635df8bae1dSRodney W. Grimes #endif
636df8bae1dSRodney W. Grimes 	}
637df8bae1dSRodney W. Grimes 	return (0);
638df8bae1dSRodney W. Grimes }
639df8bae1dSRodney W. Grimes 
640df8bae1dSRodney W. Grimes /*
641df8bae1dSRodney W. Grimes  * Return interface configuration
642df8bae1dSRodney W. Grimes  * of system.  List may be used
643df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
644df8bae1dSRodney W. Grimes  * other information.
645df8bae1dSRodney W. Grimes  */
646df8bae1dSRodney W. Grimes /*ARGSUSED*/
647df8bae1dSRodney W. Grimes int
648df8bae1dSRodney W. Grimes ifconf(cmd, data)
649df8bae1dSRodney W. Grimes 	int cmd;
650df8bae1dSRodney W. Grimes 	caddr_t data;
651df8bae1dSRodney W. Grimes {
652df8bae1dSRodney W. Grimes 	register struct ifconf *ifc = (struct ifconf *)data;
653df8bae1dSRodney W. Grimes 	register struct ifnet *ifp = ifnet;
654df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
655df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
656df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
659df8bae1dSRodney W. Grimes 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) {
6602624cf89SGarrett Wollman 		char workbuf[12], *unitname;
6612624cf89SGarrett Wollman 		int unitlen, ifnlen;
6622624cf89SGarrett Wollman 
6632624cf89SGarrett Wollman 		unitname = sprint_d(ifp->if_unit, workbuf, sizeof workbuf);
6642624cf89SGarrett Wollman 		unitlen = strlen(unitname);
6652624cf89SGarrett Wollman 		ifnlen = strlen(ifp->if_name);
6662624cf89SGarrett Wollman 		if(unitlen + ifnlen + 1 > sizeof ifr.ifr_name) {
6672624cf89SGarrett Wollman 			error = ENAMETOOLONG;
6682624cf89SGarrett Wollman 		} else {
6692624cf89SGarrett Wollman 			strcpy(ifr.ifr_name, ifp->if_name);
6702624cf89SGarrett Wollman 			strcpy(&ifr.ifr_name[ifnlen], unitname);
6712624cf89SGarrett Wollman 		}
6722624cf89SGarrett Wollman 
673df8bae1dSRodney W. Grimes 		if ((ifa = ifp->if_addrlist) == 0) {
674df8bae1dSRodney W. Grimes 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
675df8bae1dSRodney W. Grimes 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
676df8bae1dSRodney W. Grimes 			    sizeof (ifr));
677df8bae1dSRodney W. Grimes 			if (error)
678df8bae1dSRodney W. Grimes 				break;
679df8bae1dSRodney W. Grimes 			space -= sizeof (ifr), ifrp++;
680df8bae1dSRodney W. Grimes 		} else
681df8bae1dSRodney W. Grimes 		    for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) {
682df8bae1dSRodney W. Grimes 			register struct sockaddr *sa = ifa->ifa_addr;
683df8bae1dSRodney W. Grimes #ifdef COMPAT_43
684df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
685df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
686df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
687df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
688df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
689df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
690df8bae1dSRodney W. Grimes 						sizeof (ifr));
691df8bae1dSRodney W. Grimes 				ifrp++;
692df8bae1dSRodney W. Grimes 			} else
693df8bae1dSRodney W. Grimes #endif
694df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
695df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
696df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
697df8bae1dSRodney W. Grimes 						sizeof (ifr));
698df8bae1dSRodney W. Grimes 				ifrp++;
699df8bae1dSRodney W. Grimes 			} else {
700df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
701df8bae1dSRodney W. Grimes 				if (space < sizeof (ifr))
702df8bae1dSRodney W. Grimes 					break;
703df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
704df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
705df8bae1dSRodney W. Grimes 				if (error == 0)
706df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
707df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
708df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
709df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
710df8bae1dSRodney W. Grimes 			}
711df8bae1dSRodney W. Grimes 			if (error)
712df8bae1dSRodney W. Grimes 				break;
713df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
714df8bae1dSRodney W. Grimes 		}
715df8bae1dSRodney W. Grimes 	}
716df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
717df8bae1dSRodney W. Grimes 	return (error);
718df8bae1dSRodney W. Grimes }
719df8bae1dSRodney W. Grimes 
720df8bae1dSRodney W. Grimes static char *
721df8bae1dSRodney W. Grimes sprint_d(n, buf, buflen)
722df8bae1dSRodney W. Grimes 	u_int n;
723df8bae1dSRodney W. Grimes 	char *buf;
724df8bae1dSRodney W. Grimes 	int buflen;
725df8bae1dSRodney W. Grimes {
726df8bae1dSRodney W. Grimes 	register char *cp = buf + buflen - 1;
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes 	*cp = 0;
729df8bae1dSRodney W. Grimes 	do {
730df8bae1dSRodney W. Grimes 		cp--;
731df8bae1dSRodney W. Grimes 		*cp = "0123456789"[n % 10];
732df8bae1dSRodney W. Grimes 		n /= 10;
733df8bae1dSRodney W. Grimes 	} while (n != 0);
734df8bae1dSRodney W. Grimes 	return (cp);
735df8bae1dSRodney W. Grimes }
736