xref: /freebsd/sys/net/if.c (revision 3740e2ad04ffcfe39240739e63bef6b361ce4db3)
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
343740e2adSDavid Greenman  * $Id: if.c,v 1.15 1995/05/30 08:07:58 rgrimes 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 			if (ifp->if_flags & IFF_POINTOPOINT) {
243b2af64fdSDavid Greenman 				if (equal(addr, ifa->ifa_addr))
244b2af64fdSDavid Greenman  					return (ifa);
2453740e2adSDavid Greenman 			} else {
246df8bae1dSRodney W. Grimes 				cp = addr_data;
247df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
248df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
249df8bae1dSRodney W. Grimes 				cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
250df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
251df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
252df8bae1dSRodney W. Grimes 						goto next;
253df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
254df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
255df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
256df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
257df8bae1dSRodney W. Grimes 			}
258b2af64fdSDavid Greenman 		}
259b2af64fdSDavid Greenman 	}
260df8bae1dSRodney W. Grimes 	return (ifa_maybe);
261df8bae1dSRodney W. Grimes }
262df8bae1dSRodney W. Grimes 
263df8bae1dSRodney W. Grimes /*
264df8bae1dSRodney W. Grimes  * Find an interface using a specific address family
265df8bae1dSRodney W. Grimes  */
266df8bae1dSRodney W. Grimes struct ifaddr *
267df8bae1dSRodney W. Grimes ifa_ifwithaf(af)
268df8bae1dSRodney W. Grimes 	register int af;
269df8bae1dSRodney W. Grimes {
270df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
271df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
272df8bae1dSRodney W. Grimes 
273df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
274df8bae1dSRodney W. Grimes 	    for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
275df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == af)
276df8bae1dSRodney W. Grimes 			return (ifa);
277df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
278df8bae1dSRodney W. Grimes }
279df8bae1dSRodney W. Grimes 
280df8bae1dSRodney W. Grimes /*
281df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
282df8bae1dSRodney W. Grimes  * a given address.
283df8bae1dSRodney W. Grimes  */
284df8bae1dSRodney W. Grimes struct ifaddr *
285df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
286df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
287df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
288df8bae1dSRodney W. Grimes {
289df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
290df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
291df8bae1dSRodney W. Grimes 	register char *cplim;
292df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
293df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
294df8bae1dSRodney W. Grimes 
295df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
296df8bae1dSRodney W. Grimes 		return (0);
297df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
298df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
299df8bae1dSRodney W. Grimes 			continue;
300df8bae1dSRodney W. Grimes 		ifa_maybe = ifa;
301df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
302df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
303df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
304df8bae1dSRodney W. Grimes 				return (ifa);
305df8bae1dSRodney W. Grimes 			continue;
306df8bae1dSRodney W. Grimes 		}
307b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
308b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
309b2af64fdSDavid Greenman 				return (ifa);
3103740e2adSDavid Greenman 		} else {
311df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
312df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
313df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
314df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
315df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
316df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
317df8bae1dSRodney W. Grimes 					break;
318df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
319df8bae1dSRodney W. Grimes 				return (ifa);
320df8bae1dSRodney W. Grimes 		}
321b2af64fdSDavid Greenman 	}
322df8bae1dSRodney W. Grimes 	return (ifa_maybe);
323df8bae1dSRodney W. Grimes }
324df8bae1dSRodney W. Grimes 
325df8bae1dSRodney W. Grimes #include <net/route.h>
326df8bae1dSRodney W. Grimes 
327df8bae1dSRodney W. Grimes /*
328df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
329df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
330df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
331df8bae1dSRodney W. Grimes  */
332df8bae1dSRodney W. Grimes void
333df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa)
334df8bae1dSRodney W. Grimes 	int cmd;
335df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
336df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
337df8bae1dSRodney W. Grimes {
338df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
339df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
340df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
341df8bae1dSRodney W. Grimes 
342df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
343df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
344df8bae1dSRodney W. Grimes 		return;
3459448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
3469448326fSPoul-Henning Kamp 	if (ifa) {
347df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
348df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
349df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
350df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
351df8bae1dSRodney W. Grimes 			ifa->ifa_rtrequest(cmd, rt, sa);
352df8bae1dSRodney W. Grimes 	}
353df8bae1dSRodney W. Grimes }
354df8bae1dSRodney W. Grimes 
355df8bae1dSRodney W. Grimes /*
356df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
357df8bae1dSRodney W. Grimes  * the transition.
358df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
359df8bae1dSRodney W. Grimes  */
360df8bae1dSRodney W. Grimes void
361df8bae1dSRodney W. Grimes if_down(ifp)
362df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
363df8bae1dSRodney W. Grimes {
364df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
365df8bae1dSRodney W. Grimes 
366df8bae1dSRodney W. Grimes 	ifp->if_flags &= ~IFF_UP;
367df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
368df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
369df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
370df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
371df8bae1dSRodney W. Grimes }
372df8bae1dSRodney W. Grimes 
373df8bae1dSRodney W. Grimes /*
374df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
375df8bae1dSRodney W. Grimes  * the transition.
376df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
377df8bae1dSRodney W. Grimes  */
378df8bae1dSRodney W. Grimes void
379df8bae1dSRodney W. Grimes if_up(ifp)
380df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
381df8bae1dSRodney W. Grimes {
382df8bae1dSRodney W. Grimes 
383df8bae1dSRodney W. Grimes 	ifp->if_flags |= IFF_UP;
384df8bae1dSRodney W. Grimes #ifdef notyet
3859448326fSPoul-Henning Kamp 	register struct ifaddr *ifa;
386df8bae1dSRodney W. Grimes 	/* this has no effect on IP, and will kill all iso connections XXX */
387df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
388df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
389df8bae1dSRodney W. Grimes #endif
390df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
391df8bae1dSRodney W. Grimes }
392df8bae1dSRodney W. Grimes 
393df8bae1dSRodney W. Grimes /*
394df8bae1dSRodney W. Grimes  * Flush an interface queue.
395df8bae1dSRodney W. Grimes  */
396df8bae1dSRodney W. Grimes void
397df8bae1dSRodney W. Grimes if_qflush(ifq)
398df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
399df8bae1dSRodney W. Grimes {
400df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
401df8bae1dSRodney W. Grimes 
402df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
4039448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
404df8bae1dSRodney W. Grimes 		n = m->m_act;
405df8bae1dSRodney W. Grimes 		m_freem(m);
406df8bae1dSRodney W. Grimes 	}
407df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
408df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
409df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
410df8bae1dSRodney W. Grimes }
411df8bae1dSRodney W. Grimes 
412df8bae1dSRodney W. Grimes /*
413df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
414df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
415df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
416df8bae1dSRodney W. Grimes  */
417df8bae1dSRodney W. Grimes void
418df8bae1dSRodney W. Grimes if_slowtimo(arg)
419df8bae1dSRodney W. Grimes 	void *arg;
420df8bae1dSRodney W. Grimes {
421df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
422df8bae1dSRodney W. Grimes 	int s = splimp();
423df8bae1dSRodney W. Grimes 
424df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
425df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
426df8bae1dSRodney W. Grimes 			continue;
427df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
428df8bae1dSRodney W. Grimes 			(*ifp->if_watchdog)(ifp->if_unit);
429df8bae1dSRodney W. Grimes 	}
430df8bae1dSRodney W. Grimes 	splx(s);
431df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
432df8bae1dSRodney W. Grimes }
433df8bae1dSRodney W. Grimes 
434df8bae1dSRodney W. Grimes /*
435df8bae1dSRodney W. Grimes  * Map interface name to
436df8bae1dSRodney W. Grimes  * interface structure pointer.
437df8bae1dSRodney W. Grimes  */
438df8bae1dSRodney W. Grimes struct ifnet *
439df8bae1dSRodney W. Grimes ifunit(name)
440df8bae1dSRodney W. Grimes 	register char *name;
441df8bae1dSRodney W. Grimes {
442df8bae1dSRodney W. Grimes 	register char *cp;
443df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
444df8bae1dSRodney W. Grimes 	int unit;
445df8bae1dSRodney W. Grimes 	unsigned len;
446df8bae1dSRodney W. Grimes 	char *ep, c;
447df8bae1dSRodney W. Grimes 
448df8bae1dSRodney W. Grimes 	for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
449df8bae1dSRodney W. Grimes 		if (*cp >= '0' && *cp <= '9')
450df8bae1dSRodney W. Grimes 			break;
451df8bae1dSRodney W. Grimes 	if (*cp == '\0' || cp == name + IFNAMSIZ)
452df8bae1dSRodney W. Grimes 		return ((struct ifnet *)0);
453df8bae1dSRodney W. Grimes 	/*
454df8bae1dSRodney W. Grimes 	 * Save first char of unit, and pointer to it,
455df8bae1dSRodney W. Grimes 	 * so we can put a null there to avoid matching
456df8bae1dSRodney W. Grimes 	 * initial substrings of interface names.
457df8bae1dSRodney W. Grimes 	 */
458df8bae1dSRodney W. Grimes 	len = cp - name + 1;
459df8bae1dSRodney W. Grimes 	c = *cp;
460df8bae1dSRodney W. Grimes 	ep = cp;
461df8bae1dSRodney W. Grimes 	for (unit = 0; *cp >= '0' && *cp <= '9'; )
462df8bae1dSRodney W. Grimes 		unit = unit * 10 + *cp++ - '0';
463df8bae1dSRodney W. Grimes 	*ep = 0;
464df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
465df8bae1dSRodney W. Grimes 		if (bcmp(ifp->if_name, name, len))
466df8bae1dSRodney W. Grimes 			continue;
467df8bae1dSRodney W. Grimes 		if (unit == ifp->if_unit)
468df8bae1dSRodney W. Grimes 			break;
469df8bae1dSRodney W. Grimes 	}
470df8bae1dSRodney W. Grimes 	*ep = c;
471df8bae1dSRodney W. Grimes 	return (ifp);
472df8bae1dSRodney W. Grimes }
473df8bae1dSRodney W. Grimes 
474df8bae1dSRodney W. Grimes /*
475df8bae1dSRodney W. Grimes  * Interface ioctls.
476df8bae1dSRodney W. Grimes  */
477df8bae1dSRodney W. Grimes int
478df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p)
479df8bae1dSRodney W. Grimes 	struct socket *so;
480df8bae1dSRodney W. Grimes 	int cmd;
481df8bae1dSRodney W. Grimes 	caddr_t data;
482df8bae1dSRodney W. Grimes 	struct proc *p;
483df8bae1dSRodney W. Grimes {
484df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
485df8bae1dSRodney W. Grimes 	register struct ifreq *ifr;
486df8bae1dSRodney W. Grimes 	int error;
487df8bae1dSRodney W. Grimes 
488df8bae1dSRodney W. Grimes 	switch (cmd) {
489df8bae1dSRodney W. Grimes 
490df8bae1dSRodney W. Grimes 	case SIOCGIFCONF:
491df8bae1dSRodney W. Grimes 	case OSIOCGIFCONF:
492df8bae1dSRodney W. Grimes 		return (ifconf(cmd, data));
493df8bae1dSRodney W. Grimes 	}
494df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
495df8bae1dSRodney W. Grimes 	ifp = ifunit(ifr->ifr_name);
496df8bae1dSRodney W. Grimes 	if (ifp == 0)
497df8bae1dSRodney W. Grimes 		return (ENXIO);
498df8bae1dSRodney W. Grimes 	switch (cmd) {
499df8bae1dSRodney W. Grimes 
500df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
501df8bae1dSRodney W. Grimes 		ifr->ifr_flags = ifp->if_flags;
502df8bae1dSRodney W. Grimes 		break;
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
505df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
506df8bae1dSRodney W. Grimes 		break;
507df8bae1dSRodney W. Grimes 
508a7028af7SDavid Greenman 	case SIOCGIFMTU:
509a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
510a7028af7SDavid Greenman 		break;
511a7028af7SDavid Greenman 
512074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
513074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
514074c4a4eSGarrett Wollman 		break;
515074c4a4eSGarrett Wollman 
516df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
5179448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5189448326fSPoul-Henning Kamp 		if (error)
519df8bae1dSRodney W. Grimes 			return (error);
520df8bae1dSRodney W. Grimes 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
521df8bae1dSRodney W. Grimes 			int s = splimp();
522df8bae1dSRodney W. Grimes 			if_down(ifp);
523df8bae1dSRodney W. Grimes 			splx(s);
524df8bae1dSRodney W. Grimes 		}
525df8bae1dSRodney W. Grimes 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
526df8bae1dSRodney W. Grimes 			int s = splimp();
527df8bae1dSRodney W. Grimes 			if_up(ifp);
528df8bae1dSRodney W. Grimes 			splx(s);
529df8bae1dSRodney W. Grimes 		}
530df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
531df8bae1dSRodney W. Grimes 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
532df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
533df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
534df8bae1dSRodney W. Grimes 		break;
535df8bae1dSRodney W. Grimes 
536df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
5379448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5389448326fSPoul-Henning Kamp 		if (error)
539df8bae1dSRodney W. Grimes 			return (error);
540df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
541df8bae1dSRodney W. Grimes 		break;
542df8bae1dSRodney W. Grimes 
543074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
544074c4a4eSGarrett Wollman 		error = suser(p->p_ucred, &p->p_acflag);
545074c4a4eSGarrett Wollman 		if (error) return error;
546074c4a4eSGarrett Wollman 
547074c4a4eSGarrett Wollman 		if (!ifp->if_ioctl) return EOPNOTSUPP;
548074c4a4eSGarrett Wollman 		return ifp->if_ioctl(ifp, cmd, data);
549074c4a4eSGarrett Wollman 
550a7028af7SDavid Greenman 	case SIOCSIFMTU:
5519448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5529448326fSPoul-Henning Kamp 		if (error)
553a7028af7SDavid Greenman 			return (error);
554a7028af7SDavid Greenman 		if (ifp->if_ioctl == NULL)
555a7028af7SDavid Greenman 			return (EOPNOTSUPP);
55666025569SDavid Greenman 		/*
55766025569SDavid Greenman 		 * 72 was chosen below because it is the size of a TCP/IP
55866025569SDavid Greenman 		 * header (40) + the minimum mss (32).
55966025569SDavid Greenman 		 */
56066025569SDavid Greenman 		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
56175ee03cbSDavid Greenman 			return (EINVAL);
562a7028af7SDavid Greenman 		return ((*ifp->if_ioctl)(ifp, cmd, data));
563a7028af7SDavid Greenman 
564df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
565df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
5669448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5679448326fSPoul-Henning Kamp 		if (error)
568df8bae1dSRodney W. Grimes 			return (error);
569df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl == NULL)
570df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
571df8bae1dSRodney W. Grimes 		return ((*ifp->if_ioctl)(ifp, cmd, data));
572df8bae1dSRodney W. Grimes 
573df8bae1dSRodney W. Grimes 	default:
574df8bae1dSRodney W. Grimes 		if (so->so_proto == 0)
575df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
576df8bae1dSRodney W. Grimes #ifndef COMPAT_43
577df8bae1dSRodney W. Grimes 		return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
578df8bae1dSRodney W. Grimes 			cmd, data, ifp));
579df8bae1dSRodney W. Grimes #else
580df8bae1dSRodney W. Grimes 	    {
581df8bae1dSRodney W. Grimes 		int ocmd = cmd;
582df8bae1dSRodney W. Grimes 
583df8bae1dSRodney W. Grimes 		switch (cmd) {
584df8bae1dSRodney W. Grimes 
585df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
586df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
587df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
588df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
589df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
590df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
591df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
592df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
593df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
594df8bae1dSRodney W. Grimes 			}
595df8bae1dSRodney W. Grimes #else
596df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
597df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
598df8bae1dSRodney W. Grimes #endif
599df8bae1dSRodney W. Grimes 			break;
600df8bae1dSRodney W. Grimes 
601df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
602df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
603df8bae1dSRodney W. Grimes 			break;
604df8bae1dSRodney W. Grimes 
605df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
606df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
607df8bae1dSRodney W. Grimes 			break;
608df8bae1dSRodney W. Grimes 
609df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
610df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
611df8bae1dSRodney W. Grimes 			break;
612df8bae1dSRodney W. Grimes 
613df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
614df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
615df8bae1dSRodney W. Grimes 		}
616df8bae1dSRodney W. Grimes 		error =  ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
617df8bae1dSRodney W. Grimes 							    cmd, data, ifp));
618df8bae1dSRodney W. Grimes 		switch (ocmd) {
619df8bae1dSRodney W. Grimes 
620df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
621df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
622df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
623df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
624df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
625df8bae1dSRodney W. Grimes 		}
626df8bae1dSRodney W. Grimes 		return (error);
627df8bae1dSRodney W. Grimes 
628df8bae1dSRodney W. Grimes 	    }
629df8bae1dSRodney W. Grimes #endif
630df8bae1dSRodney W. Grimes 	}
631df8bae1dSRodney W. Grimes 	return (0);
632df8bae1dSRodney W. Grimes }
633df8bae1dSRodney W. Grimes 
634df8bae1dSRodney W. Grimes /*
635df8bae1dSRodney W. Grimes  * Return interface configuration
636df8bae1dSRodney W. Grimes  * of system.  List may be used
637df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
638df8bae1dSRodney W. Grimes  * other information.
639df8bae1dSRodney W. Grimes  */
640df8bae1dSRodney W. Grimes /*ARGSUSED*/
641df8bae1dSRodney W. Grimes int
642df8bae1dSRodney W. Grimes ifconf(cmd, data)
643df8bae1dSRodney W. Grimes 	int cmd;
644df8bae1dSRodney W. Grimes 	caddr_t data;
645df8bae1dSRodney W. Grimes {
646df8bae1dSRodney W. Grimes 	register struct ifconf *ifc = (struct ifconf *)data;
647df8bae1dSRodney W. Grimes 	register struct ifnet *ifp = ifnet;
648df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
649df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
650df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
651df8bae1dSRodney W. Grimes 
652df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
653df8bae1dSRodney W. Grimes 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) {
6542624cf89SGarrett Wollman 		char workbuf[12], *unitname;
6552624cf89SGarrett Wollman 		int unitlen, ifnlen;
6562624cf89SGarrett Wollman 
6572624cf89SGarrett Wollman 		unitname = sprint_d(ifp->if_unit, workbuf, sizeof workbuf);
6582624cf89SGarrett Wollman 		unitlen = strlen(unitname);
6592624cf89SGarrett Wollman 		ifnlen = strlen(ifp->if_name);
6602624cf89SGarrett Wollman 		if(unitlen + ifnlen + 1 > sizeof ifr.ifr_name) {
6612624cf89SGarrett Wollman 			error = ENAMETOOLONG;
6622624cf89SGarrett Wollman 		} else {
6632624cf89SGarrett Wollman 			strcpy(ifr.ifr_name, ifp->if_name);
6642624cf89SGarrett Wollman 			strcpy(&ifr.ifr_name[ifnlen], unitname);
6652624cf89SGarrett Wollman 		}
6662624cf89SGarrett Wollman 
667df8bae1dSRodney W. Grimes 		if ((ifa = ifp->if_addrlist) == 0) {
668df8bae1dSRodney W. Grimes 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
669df8bae1dSRodney W. Grimes 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
670df8bae1dSRodney W. Grimes 			    sizeof (ifr));
671df8bae1dSRodney W. Grimes 			if (error)
672df8bae1dSRodney W. Grimes 				break;
673df8bae1dSRodney W. Grimes 			space -= sizeof (ifr), ifrp++;
674df8bae1dSRodney W. Grimes 		} else
675df8bae1dSRodney W. Grimes 		    for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) {
676df8bae1dSRodney W. Grimes 			register struct sockaddr *sa = ifa->ifa_addr;
677df8bae1dSRodney W. Grimes #ifdef COMPAT_43
678df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
679df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
680df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
681df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
682df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
683df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
684df8bae1dSRodney W. Grimes 						sizeof (ifr));
685df8bae1dSRodney W. Grimes 				ifrp++;
686df8bae1dSRodney W. Grimes 			} else
687df8bae1dSRodney W. Grimes #endif
688df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
689df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
690df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
691df8bae1dSRodney W. Grimes 						sizeof (ifr));
692df8bae1dSRodney W. Grimes 				ifrp++;
693df8bae1dSRodney W. Grimes 			} else {
694df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
695df8bae1dSRodney W. Grimes 				if (space < sizeof (ifr))
696df8bae1dSRodney W. Grimes 					break;
697df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
698df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
699df8bae1dSRodney W. Grimes 				if (error == 0)
700df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
701df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
702df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
703df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
704df8bae1dSRodney W. Grimes 			}
705df8bae1dSRodney W. Grimes 			if (error)
706df8bae1dSRodney W. Grimes 				break;
707df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
708df8bae1dSRodney W. Grimes 		}
709df8bae1dSRodney W. Grimes 	}
710df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
711df8bae1dSRodney W. Grimes 	return (error);
712df8bae1dSRodney W. Grimes }
713df8bae1dSRodney W. Grimes 
714df8bae1dSRodney W. Grimes static char *
715df8bae1dSRodney W. Grimes sprint_d(n, buf, buflen)
716df8bae1dSRodney W. Grimes 	u_int n;
717df8bae1dSRodney W. Grimes 	char *buf;
718df8bae1dSRodney W. Grimes 	int buflen;
719df8bae1dSRodney W. Grimes {
720df8bae1dSRodney W. Grimes 	register char *cp = buf + buflen - 1;
721df8bae1dSRodney W. Grimes 
722df8bae1dSRodney W. Grimes 	*cp = 0;
723df8bae1dSRodney W. Grimes 	do {
724df8bae1dSRodney W. Grimes 		cp--;
725df8bae1dSRodney W. Grimes 		*cp = "0123456789"[n % 10];
726df8bae1dSRodney W. Grimes 		n /= 10;
727df8bae1dSRodney W. Grimes 	} while (n != 0);
728df8bae1dSRodney W. Grimes 	return (cp);
729df8bae1dSRodney W. Grimes }
730