xref: /freebsd/sys/net/if.c (revision 074c4a4e2ee4029e924580f689ec868500c8a75b)
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
34074c4a4eSGarrett Wollman  * $Id: if.c,v 1.10 1994/11/10 18:49:23 guido 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;
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes /*
57df8bae1dSRodney W. Grimes  * Network interface utility routines.
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * Routines with ifa_ifwith* names take sockaddr *'s as
60df8bae1dSRodney W. Grimes  * parameters.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes void
63df8bae1dSRodney W. Grimes ifinit()
64df8bae1dSRodney W. Grimes {
65df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
68df8bae1dSRodney W. Grimes 		if (ifp->if_snd.ifq_maxlen == 0)
69df8bae1dSRodney W. Grimes 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
70df8bae1dSRodney W. Grimes 	if_slowtimo(0);
71df8bae1dSRodney W. Grimes }
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #ifdef vax
74df8bae1dSRodney W. Grimes /*
75df8bae1dSRodney W. Grimes  * Call each interface on a Unibus reset.
76df8bae1dSRodney W. Grimes  */
77df8bae1dSRodney W. Grimes void
78df8bae1dSRodney W. Grimes ifubareset(uban)
79df8bae1dSRodney W. Grimes 	int uban;
80df8bae1dSRodney W. Grimes {
81df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
82df8bae1dSRodney W. Grimes 
83df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
84df8bae1dSRodney W. Grimes 		if (ifp->if_reset)
85df8bae1dSRodney W. Grimes 			(*ifp->if_reset)(ifp->if_unit, uban);
86df8bae1dSRodney W. Grimes }
87df8bae1dSRodney W. Grimes #endif
88df8bae1dSRodney W. Grimes 
89df8bae1dSRodney W. Grimes int if_index = 0;
90df8bae1dSRodney W. Grimes struct ifaddr **ifnet_addrs;
91df8bae1dSRodney W. Grimes static char *sprint_d __P((u_int, char *, int));
92df8bae1dSRodney W. Grimes 
93df8bae1dSRodney W. Grimes /*
94df8bae1dSRodney W. Grimes  * Attach an interface to the
95df8bae1dSRodney W. Grimes  * list of "active" interfaces.
96df8bae1dSRodney W. Grimes  */
97df8bae1dSRodney W. Grimes void
98df8bae1dSRodney W. Grimes if_attach(ifp)
99df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
100df8bae1dSRodney W. Grimes {
101df8bae1dSRodney W. Grimes 	unsigned socksize, ifasize;
102f23b4c91SGarrett Wollman 	int namelen, unitlen, masklen;
103df8bae1dSRodney W. Grimes 	char workbuf[12], *unitname;
104df8bae1dSRodney W. Grimes 	register struct ifnet **p = &ifnet;
105df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
106df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
107df8bae1dSRodney W. Grimes 	static int if_indexlim = 8;
108f23b4c91SGarrett Wollman 
109df8bae1dSRodney W. Grimes 
110df8bae1dSRodney W. Grimes 	while (*p)
111df8bae1dSRodney W. Grimes 		p = &((*p)->if_next);
112df8bae1dSRodney W. Grimes 	*p = ifp;
113df8bae1dSRodney W. Grimes 	ifp->if_index = ++if_index;
114df8bae1dSRodney W. Grimes 	if (ifnet_addrs == 0 || if_index >= if_indexlim) {
115df8bae1dSRodney W. Grimes 		unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
116df8bae1dSRodney W. Grimes 		struct ifaddr **q = (struct ifaddr **)
117df8bae1dSRodney W. Grimes 					malloc(n, M_IFADDR, M_WAITOK);
118df8bae1dSRodney W. Grimes 		if (ifnet_addrs) {
119df8bae1dSRodney W. Grimes 			bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
120df8bae1dSRodney W. Grimes 			free((caddr_t)ifnet_addrs, M_IFADDR);
121df8bae1dSRodney W. Grimes 		}
122df8bae1dSRodney W. Grimes 		ifnet_addrs = q;
123df8bae1dSRodney W. Grimes 	}
124df8bae1dSRodney W. Grimes 	/*
125df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
126df8bae1dSRodney W. Grimes 	 */
127df8bae1dSRodney W. Grimes 	unitname = sprint_d((u_int)ifp->if_unit, workbuf, sizeof(workbuf));
128df8bae1dSRodney W. Grimes 	namelen = strlen(ifp->if_name);
129df8bae1dSRodney W. Grimes 	unitlen = strlen(unitname);
130df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
131df8bae1dSRodney W. Grimes 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) +
132df8bae1dSRodney W. Grimes 			       unitlen + namelen;
133df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
134df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
135df8bae1dSRodney W. Grimes 	socksize = ROUNDUP(socksize);
136df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
137df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
138df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
1399448326fSPoul-Henning Kamp 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
1409448326fSPoul-Henning Kamp 	if (ifa) {
141df8bae1dSRodney W. Grimes 		bzero((caddr_t)ifa, ifasize);
142df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
143df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
144df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
145df8bae1dSRodney W. Grimes 		bcopy(ifp->if_name, sdl->sdl_data, namelen);
146df8bae1dSRodney W. Grimes 		bcopy(unitname, namelen + (caddr_t)sdl->sdl_data, unitlen);
147df8bae1dSRodney W. Grimes 		sdl->sdl_nlen = (namelen += unitlen);
148df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
149df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
150df8bae1dSRodney W. Grimes 		ifnet_addrs[if_index - 1] = ifa;
151df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
152df8bae1dSRodney W. Grimes 		ifa->ifa_next = ifp->if_addrlist;
153df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
154df8bae1dSRodney W. Grimes 		ifp->if_addrlist = ifa;
155df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
156df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
157df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
158df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
159df8bae1dSRodney W. Grimes 		while (namelen != 0)
160df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
161df8bae1dSRodney W. Grimes 	}
162df8bae1dSRodney W. Grimes 	/* XXX -- Temporary fix before changing 10 ethernet drivers */
163fe95e21fSPoul-Henning Kamp #if NETHER > 0
164df8bae1dSRodney W. Grimes 	if (ifp->if_output == ether_output)
165df8bae1dSRodney W. Grimes 		ether_ifattach(ifp);
166fe95e21fSPoul-Henning Kamp #endif
167df8bae1dSRodney W. Grimes }
168df8bae1dSRodney W. Grimes /*
169df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
170df8bae1dSRodney W. Grimes  */
171df8bae1dSRodney W. Grimes /*ARGSUSED*/
172df8bae1dSRodney W. Grimes struct ifaddr *
173df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
174df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
175df8bae1dSRodney W. Grimes {
176df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
177df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
178df8bae1dSRodney W. Grimes 
179df8bae1dSRodney W. Grimes #define	equal(a1, a2) \
180df8bae1dSRodney W. Grimes   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
181df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
182df8bae1dSRodney W. Grimes 	    for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
183df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != addr->sa_family)
184df8bae1dSRodney W. Grimes 			continue;
185df8bae1dSRodney W. Grimes 		if (equal(addr, ifa->ifa_addr))
186df8bae1dSRodney W. Grimes 			return (ifa);
187df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
188df8bae1dSRodney W. Grimes 		    equal(ifa->ifa_broadaddr, addr))
189df8bae1dSRodney W. Grimes 			return (ifa);
190df8bae1dSRodney W. Grimes 	}
191df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
192df8bae1dSRodney W. Grimes }
193df8bae1dSRodney W. Grimes /*
194df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
195df8bae1dSRodney W. Grimes  */
196df8bae1dSRodney W. Grimes /*ARGSUSED*/
197df8bae1dSRodney W. Grimes struct ifaddr *
198df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
199df8bae1dSRodney W. Grimes 	register struct sockaddr *addr;
200df8bae1dSRodney W. Grimes {
201df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
202df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
203df8bae1dSRodney W. Grimes 
204df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
205df8bae1dSRodney W. Grimes 	    if (ifp->if_flags & IFF_POINTOPOINT)
206df8bae1dSRodney W. Grimes 		for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
207df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
208df8bae1dSRodney W. Grimes 				continue;
209df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_dstaddr))
210df8bae1dSRodney W. Grimes 				return (ifa);
211df8bae1dSRodney W. Grimes 	}
212df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
213df8bae1dSRodney W. Grimes }
214df8bae1dSRodney W. Grimes 
215df8bae1dSRodney W. Grimes /*
216df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
217df8bae1dSRodney W. Grimes  * is most specific found.
218df8bae1dSRodney W. Grimes  */
219df8bae1dSRodney W. Grimes struct ifaddr *
220df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
221df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
222df8bae1dSRodney W. Grimes {
223df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
224df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
225df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
226df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
227df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
228df8bae1dSRodney W. Grimes 
229df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
230df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
231df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
232df8bae1dSRodney W. Grimes 		return (ifnet_addrs[sdl->sdl_index - 1]);
233df8bae1dSRodney W. Grimes 	}
234df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
235df8bae1dSRodney W. Grimes 	    for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
236df8bae1dSRodney W. Grimes 		register char *cp, *cp2, *cp3;
237df8bae1dSRodney W. Grimes 
238df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af || ifa->ifa_netmask == 0)
239df8bae1dSRodney W. Grimes 			next: continue;
240df8bae1dSRodney W. Grimes 		cp = addr_data;
241df8bae1dSRodney W. Grimes 		cp2 = ifa->ifa_addr->sa_data;
242df8bae1dSRodney W. Grimes 		cp3 = ifa->ifa_netmask->sa_data;
243df8bae1dSRodney W. Grimes 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
244df8bae1dSRodney W. Grimes 		while (cp3 < cplim)
245df8bae1dSRodney W. Grimes 			if ((*cp++ ^ *cp2++) & *cp3++)
246df8bae1dSRodney W. Grimes 				goto next;
247df8bae1dSRodney W. Grimes 		if (ifa_maybe == 0 ||
248df8bae1dSRodney W. Grimes 		    rn_refines((caddr_t)ifa->ifa_netmask,
249df8bae1dSRodney W. Grimes 		    (caddr_t)ifa_maybe->ifa_netmask))
250df8bae1dSRodney W. Grimes 			ifa_maybe = ifa;
251df8bae1dSRodney W. Grimes 	    }
252df8bae1dSRodney W. Grimes 	return (ifa_maybe);
253df8bae1dSRodney W. Grimes }
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes /*
256df8bae1dSRodney W. Grimes  * Find an interface using a specific address family
257df8bae1dSRodney W. Grimes  */
258df8bae1dSRodney W. Grimes struct ifaddr *
259df8bae1dSRodney W. Grimes ifa_ifwithaf(af)
260df8bae1dSRodney W. Grimes 	register int af;
261df8bae1dSRodney W. Grimes {
262df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
263df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
266df8bae1dSRodney W. Grimes 	    for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
267df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family == af)
268df8bae1dSRodney W. Grimes 			return (ifa);
269df8bae1dSRodney W. Grimes 	return ((struct ifaddr *)0);
270df8bae1dSRodney W. Grimes }
271df8bae1dSRodney W. Grimes 
272df8bae1dSRodney W. Grimes /*
273df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
274df8bae1dSRodney W. Grimes  * a given address.
275df8bae1dSRodney W. Grimes  */
276df8bae1dSRodney W. Grimes struct ifaddr *
277df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
278df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
279df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
280df8bae1dSRodney W. Grimes {
281df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
282df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
283df8bae1dSRodney W. Grimes 	register char *cplim;
284df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
285df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
286df8bae1dSRodney W. Grimes 
287df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
288df8bae1dSRodney W. Grimes 		return (0);
289df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) {
290df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
291df8bae1dSRodney W. Grimes 			continue;
292df8bae1dSRodney W. Grimes 		ifa_maybe = ifa;
293df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
294df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
295df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
296df8bae1dSRodney W. Grimes 				return (ifa);
297df8bae1dSRodney W. Grimes 			continue;
298df8bae1dSRodney W. Grimes 		}
299df8bae1dSRodney W. Grimes 		cp = addr->sa_data;
300df8bae1dSRodney W. Grimes 		cp2 = ifa->ifa_addr->sa_data;
301df8bae1dSRodney W. Grimes 		cp3 = ifa->ifa_netmask->sa_data;
302df8bae1dSRodney W. Grimes 		cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
303df8bae1dSRodney W. Grimes 		for (; cp3 < cplim; cp3++)
304df8bae1dSRodney W. Grimes 			if ((*cp++ ^ *cp2++) & *cp3)
305df8bae1dSRodney W. Grimes 				break;
306df8bae1dSRodney W. Grimes 		if (cp3 == cplim)
307df8bae1dSRodney W. Grimes 			return (ifa);
308df8bae1dSRodney W. Grimes 	}
309df8bae1dSRodney W. Grimes 	return (ifa_maybe);
310df8bae1dSRodney W. Grimes }
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes #include <net/route.h>
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes /*
315df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
316df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
317df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
318df8bae1dSRodney W. Grimes  */
319df8bae1dSRodney W. Grimes void
320df8bae1dSRodney W. Grimes link_rtrequest(cmd, rt, sa)
321df8bae1dSRodney W. Grimes 	int cmd;
322df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
323df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
324df8bae1dSRodney W. Grimes {
325df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
326df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
327df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
328df8bae1dSRodney W. Grimes 
329df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
330df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
331df8bae1dSRodney W. Grimes 		return;
3329448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
3339448326fSPoul-Henning Kamp 	if (ifa) {
334df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
335df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
336df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
337df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
338df8bae1dSRodney W. Grimes 			ifa->ifa_rtrequest(cmd, rt, sa);
339df8bae1dSRodney W. Grimes 	}
340df8bae1dSRodney W. Grimes }
341df8bae1dSRodney W. Grimes 
342df8bae1dSRodney W. Grimes /*
343df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
344df8bae1dSRodney W. Grimes  * the transition.
345df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
346df8bae1dSRodney W. Grimes  */
347df8bae1dSRodney W. Grimes void
348df8bae1dSRodney W. Grimes if_down(ifp)
349df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
350df8bae1dSRodney W. Grimes {
351df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
352df8bae1dSRodney W. Grimes 
353df8bae1dSRodney W. Grimes 	ifp->if_flags &= ~IFF_UP;
354df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
355df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
356df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
357df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
358df8bae1dSRodney W. Grimes }
359df8bae1dSRodney W. Grimes 
360df8bae1dSRodney W. Grimes /*
361df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
362df8bae1dSRodney W. Grimes  * the transition.
363df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
364df8bae1dSRodney W. Grimes  */
365df8bae1dSRodney W. Grimes void
366df8bae1dSRodney W. Grimes if_up(ifp)
367df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
368df8bae1dSRodney W. Grimes {
369df8bae1dSRodney W. Grimes 
370df8bae1dSRodney W. Grimes 	ifp->if_flags |= IFF_UP;
371df8bae1dSRodney W. Grimes #ifdef notyet
3729448326fSPoul-Henning Kamp 	register struct ifaddr *ifa;
373df8bae1dSRodney W. Grimes 	/* this has no effect on IP, and will kill all iso connections XXX */
374df8bae1dSRodney W. Grimes 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
375df8bae1dSRodney W. Grimes 		pfctlinput(PRC_IFUP, ifa->ifa_addr);
376df8bae1dSRodney W. Grimes #endif
377df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
378df8bae1dSRodney W. Grimes }
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes /*
381df8bae1dSRodney W. Grimes  * Flush an interface queue.
382df8bae1dSRodney W. Grimes  */
383df8bae1dSRodney W. Grimes void
384df8bae1dSRodney W. Grimes if_qflush(ifq)
385df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
386df8bae1dSRodney W. Grimes {
387df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
388df8bae1dSRodney W. Grimes 
389df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
3909448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
391df8bae1dSRodney W. Grimes 		n = m->m_act;
392df8bae1dSRodney W. Grimes 		m_freem(m);
393df8bae1dSRodney W. Grimes 	}
394df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
395df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
396df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
397df8bae1dSRodney W. Grimes }
398df8bae1dSRodney W. Grimes 
399df8bae1dSRodney W. Grimes /*
400df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
401df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
402df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
403df8bae1dSRodney W. Grimes  */
404df8bae1dSRodney W. Grimes void
405df8bae1dSRodney W. Grimes if_slowtimo(arg)
406df8bae1dSRodney W. Grimes 	void *arg;
407df8bae1dSRodney W. Grimes {
408df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
409df8bae1dSRodney W. Grimes 	int s = splimp();
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
412df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
413df8bae1dSRodney W. Grimes 			continue;
414df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
415df8bae1dSRodney W. Grimes 			(*ifp->if_watchdog)(ifp->if_unit);
416df8bae1dSRodney W. Grimes 	}
417df8bae1dSRodney W. Grimes 	splx(s);
418df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
419df8bae1dSRodney W. Grimes }
420df8bae1dSRodney W. Grimes 
421df8bae1dSRodney W. Grimes /*
422df8bae1dSRodney W. Grimes  * Map interface name to
423df8bae1dSRodney W. Grimes  * interface structure pointer.
424df8bae1dSRodney W. Grimes  */
425df8bae1dSRodney W. Grimes struct ifnet *
426df8bae1dSRodney W. Grimes ifunit(name)
427df8bae1dSRodney W. Grimes 	register char *name;
428df8bae1dSRodney W. Grimes {
429df8bae1dSRodney W. Grimes 	register char *cp;
430df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
431df8bae1dSRodney W. Grimes 	int unit;
432df8bae1dSRodney W. Grimes 	unsigned len;
433df8bae1dSRodney W. Grimes 	char *ep, c;
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes 	for (cp = name; cp < name + IFNAMSIZ && *cp; cp++)
436df8bae1dSRodney W. Grimes 		if (*cp >= '0' && *cp <= '9')
437df8bae1dSRodney W. Grimes 			break;
438df8bae1dSRodney W. Grimes 	if (*cp == '\0' || cp == name + IFNAMSIZ)
439df8bae1dSRodney W. Grimes 		return ((struct ifnet *)0);
440df8bae1dSRodney W. Grimes 	/*
441df8bae1dSRodney W. Grimes 	 * Save first char of unit, and pointer to it,
442df8bae1dSRodney W. Grimes 	 * so we can put a null there to avoid matching
443df8bae1dSRodney W. Grimes 	 * initial substrings of interface names.
444df8bae1dSRodney W. Grimes 	 */
445df8bae1dSRodney W. Grimes 	len = cp - name + 1;
446df8bae1dSRodney W. Grimes 	c = *cp;
447df8bae1dSRodney W. Grimes 	ep = cp;
448df8bae1dSRodney W. Grimes 	for (unit = 0; *cp >= '0' && *cp <= '9'; )
449df8bae1dSRodney W. Grimes 		unit = unit * 10 + *cp++ - '0';
450df8bae1dSRodney W. Grimes 	*ep = 0;
451df8bae1dSRodney W. Grimes 	for (ifp = ifnet; ifp; ifp = ifp->if_next) {
452df8bae1dSRodney W. Grimes 		if (bcmp(ifp->if_name, name, len))
453df8bae1dSRodney W. Grimes 			continue;
454df8bae1dSRodney W. Grimes 		if (unit == ifp->if_unit)
455df8bae1dSRodney W. Grimes 			break;
456df8bae1dSRodney W. Grimes 	}
457df8bae1dSRodney W. Grimes 	*ep = c;
458df8bae1dSRodney W. Grimes 	return (ifp);
459df8bae1dSRodney W. Grimes }
460df8bae1dSRodney W. Grimes 
461df8bae1dSRodney W. Grimes /*
462df8bae1dSRodney W. Grimes  * Interface ioctls.
463df8bae1dSRodney W. Grimes  */
464df8bae1dSRodney W. Grimes int
465df8bae1dSRodney W. Grimes ifioctl(so, cmd, data, p)
466df8bae1dSRodney W. Grimes 	struct socket *so;
467df8bae1dSRodney W. Grimes 	int cmd;
468df8bae1dSRodney W. Grimes 	caddr_t data;
469df8bae1dSRodney W. Grimes 	struct proc *p;
470df8bae1dSRodney W. Grimes {
471df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
472df8bae1dSRodney W. Grimes 	register struct ifreq *ifr;
473df8bae1dSRodney W. Grimes 	int error;
474df8bae1dSRodney W. Grimes 
475df8bae1dSRodney W. Grimes 	switch (cmd) {
476df8bae1dSRodney W. Grimes 
477df8bae1dSRodney W. Grimes 	case SIOCGIFCONF:
478df8bae1dSRodney W. Grimes 	case OSIOCGIFCONF:
479df8bae1dSRodney W. Grimes 		return (ifconf(cmd, data));
480df8bae1dSRodney W. Grimes 	}
481df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
482df8bae1dSRodney W. Grimes 	ifp = ifunit(ifr->ifr_name);
483df8bae1dSRodney W. Grimes 	if (ifp == 0)
484df8bae1dSRodney W. Grimes 		return (ENXIO);
485df8bae1dSRodney W. Grimes 	switch (cmd) {
486df8bae1dSRodney W. Grimes 
487df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
488df8bae1dSRodney W. Grimes 		ifr->ifr_flags = ifp->if_flags;
489df8bae1dSRodney W. Grimes 		break;
490df8bae1dSRodney W. Grimes 
491df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
492df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
493df8bae1dSRodney W. Grimes 		break;
494df8bae1dSRodney W. Grimes 
495a7028af7SDavid Greenman 	case SIOCGIFMTU:
496a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
497a7028af7SDavid Greenman 		break;
498a7028af7SDavid Greenman 
499074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
500074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
501074c4a4eSGarrett Wollman 		break;
502074c4a4eSGarrett Wollman 
503df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
5049448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5059448326fSPoul-Henning Kamp 		if (error)
506df8bae1dSRodney W. Grimes 			return (error);
507df8bae1dSRodney W. Grimes 		if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
508df8bae1dSRodney W. Grimes 			int s = splimp();
509df8bae1dSRodney W. Grimes 			if_down(ifp);
510df8bae1dSRodney W. Grimes 			splx(s);
511df8bae1dSRodney W. Grimes 		}
512df8bae1dSRodney W. Grimes 		if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
513df8bae1dSRodney W. Grimes 			int s = splimp();
514df8bae1dSRodney W. Grimes 			if_up(ifp);
515df8bae1dSRodney W. Grimes 			splx(s);
516df8bae1dSRodney W. Grimes 		}
517df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
518df8bae1dSRodney W. Grimes 			(ifr->ifr_flags &~ IFF_CANTCHANGE);
519df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
520df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
521df8bae1dSRodney W. Grimes 		break;
522df8bae1dSRodney W. Grimes 
523df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
5249448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5259448326fSPoul-Henning Kamp 		if (error)
526df8bae1dSRodney W. Grimes 			return (error);
527df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
528df8bae1dSRodney W. Grimes 		break;
529df8bae1dSRodney W. Grimes 
530074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
531074c4a4eSGarrett Wollman 		error = suser(p->p_ucred, &p->p_acflag);
532074c4a4eSGarrett Wollman 		if (error) return error;
533074c4a4eSGarrett Wollman 
534074c4a4eSGarrett Wollman 		if (!ifp->if_ioctl) return EOPNOTSUPP;
535074c4a4eSGarrett Wollman 		return ifp->if_ioctl(ifp, cmd, data);
536074c4a4eSGarrett Wollman 
537a7028af7SDavid Greenman 	case SIOCSIFMTU:
5389448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5399448326fSPoul-Henning Kamp 		if (error)
540a7028af7SDavid Greenman 			return (error);
541a7028af7SDavid Greenman 		if (ifp->if_ioctl == NULL)
542a7028af7SDavid Greenman 			return (EOPNOTSUPP);
54366025569SDavid Greenman 		/*
54466025569SDavid Greenman 		 * 72 was chosen below because it is the size of a TCP/IP
54566025569SDavid Greenman 		 * header (40) + the minimum mss (32).
54666025569SDavid Greenman 		 */
54766025569SDavid Greenman 		if (ifr->ifr_mtu < 72 || ifr->ifr_mtu > 65535)
54875ee03cbSDavid Greenman 			return (EINVAL);
549a7028af7SDavid Greenman 		return ((*ifp->if_ioctl)(ifp, cmd, data));
550a7028af7SDavid Greenman 
551df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
552df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
5539448326fSPoul-Henning Kamp 		error = suser(p->p_ucred, &p->p_acflag);
5549448326fSPoul-Henning Kamp 		if (error)
555df8bae1dSRodney W. Grimes 			return (error);
556df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl == NULL)
557df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
558df8bae1dSRodney W. Grimes 		return ((*ifp->if_ioctl)(ifp, cmd, data));
559df8bae1dSRodney W. Grimes 
560df8bae1dSRodney W. Grimes 	default:
561df8bae1dSRodney W. Grimes 		if (so->so_proto == 0)
562df8bae1dSRodney W. Grimes 			return (EOPNOTSUPP);
563df8bae1dSRodney W. Grimes #ifndef COMPAT_43
564df8bae1dSRodney W. Grimes 		return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
565df8bae1dSRodney W. Grimes 			cmd, data, ifp));
566df8bae1dSRodney W. Grimes #else
567df8bae1dSRodney W. Grimes 	    {
568df8bae1dSRodney W. Grimes 		int ocmd = cmd;
569df8bae1dSRodney W. Grimes 
570df8bae1dSRodney W. Grimes 		switch (cmd) {
571df8bae1dSRodney W. Grimes 
572df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
573df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
574df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
575df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
576df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
577df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
578df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
579df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
580df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
581df8bae1dSRodney W. Grimes 			}
582df8bae1dSRodney W. Grimes #else
583df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
584df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
585df8bae1dSRodney W. Grimes #endif
586df8bae1dSRodney W. Grimes 			break;
587df8bae1dSRodney W. Grimes 
588df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
589df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
590df8bae1dSRodney W. Grimes 			break;
591df8bae1dSRodney W. Grimes 
592df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
593df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
594df8bae1dSRodney W. Grimes 			break;
595df8bae1dSRodney W. Grimes 
596df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
597df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
598df8bae1dSRodney W. Grimes 			break;
599df8bae1dSRodney W. Grimes 
600df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
601df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
602df8bae1dSRodney W. Grimes 		}
603df8bae1dSRodney W. Grimes 		error =  ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
604df8bae1dSRodney W. Grimes 							    cmd, data, ifp));
605df8bae1dSRodney W. Grimes 		switch (ocmd) {
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
608df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
609df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
610df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
611df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
612df8bae1dSRodney W. Grimes 		}
613df8bae1dSRodney W. Grimes 		return (error);
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes 	    }
616df8bae1dSRodney W. Grimes #endif
617df8bae1dSRodney W. Grimes 	}
618df8bae1dSRodney W. Grimes 	return (0);
619df8bae1dSRodney W. Grimes }
620df8bae1dSRodney W. Grimes 
621df8bae1dSRodney W. Grimes /*
622df8bae1dSRodney W. Grimes  * Return interface configuration
623df8bae1dSRodney W. Grimes  * of system.  List may be used
624df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
625df8bae1dSRodney W. Grimes  * other information.
626df8bae1dSRodney W. Grimes  */
627df8bae1dSRodney W. Grimes /*ARGSUSED*/
628df8bae1dSRodney W. Grimes int
629df8bae1dSRodney W. Grimes ifconf(cmd, data)
630df8bae1dSRodney W. Grimes 	int cmd;
631df8bae1dSRodney W. Grimes 	caddr_t data;
632df8bae1dSRodney W. Grimes {
633df8bae1dSRodney W. Grimes 	register struct ifconf *ifc = (struct ifconf *)data;
634df8bae1dSRodney W. Grimes 	register struct ifnet *ifp = ifnet;
635df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
636df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
637df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
640df8bae1dSRodney W. Grimes 	for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) {
6412624cf89SGarrett Wollman 		char workbuf[12], *unitname;
6422624cf89SGarrett Wollman 		int unitlen, ifnlen;
6432624cf89SGarrett Wollman 
6442624cf89SGarrett Wollman 		unitname = sprint_d(ifp->if_unit, workbuf, sizeof workbuf);
6452624cf89SGarrett Wollman 		unitlen = strlen(unitname);
6462624cf89SGarrett Wollman 		ifnlen = strlen(ifp->if_name);
6472624cf89SGarrett Wollman 		if(unitlen + ifnlen + 1 > sizeof ifr.ifr_name) {
6482624cf89SGarrett Wollman 			error = ENAMETOOLONG;
6492624cf89SGarrett Wollman 		} else {
6502624cf89SGarrett Wollman 			strcpy(ifr.ifr_name, ifp->if_name);
6512624cf89SGarrett Wollman 			strcpy(&ifr.ifr_name[ifnlen], unitname);
6522624cf89SGarrett Wollman 		}
6532624cf89SGarrett Wollman 
654df8bae1dSRodney W. Grimes 		if ((ifa = ifp->if_addrlist) == 0) {
655df8bae1dSRodney W. Grimes 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
656df8bae1dSRodney W. Grimes 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
657df8bae1dSRodney W. Grimes 			    sizeof (ifr));
658df8bae1dSRodney W. Grimes 			if (error)
659df8bae1dSRodney W. Grimes 				break;
660df8bae1dSRodney W. Grimes 			space -= sizeof (ifr), ifrp++;
661df8bae1dSRodney W. Grimes 		} else
662df8bae1dSRodney W. Grimes 		    for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) {
663df8bae1dSRodney W. Grimes 			register struct sockaddr *sa = ifa->ifa_addr;
664df8bae1dSRodney W. Grimes #ifdef COMPAT_43
665df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
666df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
667df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
668df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
669df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
670df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
671df8bae1dSRodney W. Grimes 						sizeof (ifr));
672df8bae1dSRodney W. Grimes 				ifrp++;
673df8bae1dSRodney W. Grimes 			} else
674df8bae1dSRodney W. Grimes #endif
675df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
676df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
677df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
678df8bae1dSRodney W. Grimes 						sizeof (ifr));
679df8bae1dSRodney W. Grimes 				ifrp++;
680df8bae1dSRodney W. Grimes 			} else {
681df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
682df8bae1dSRodney W. Grimes 				if (space < sizeof (ifr))
683df8bae1dSRodney W. Grimes 					break;
684df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
685df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
686df8bae1dSRodney W. Grimes 				if (error == 0)
687df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
688df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
689df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
690df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
691df8bae1dSRodney W. Grimes 			}
692df8bae1dSRodney W. Grimes 			if (error)
693df8bae1dSRodney W. Grimes 				break;
694df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
695df8bae1dSRodney W. Grimes 		}
696df8bae1dSRodney W. Grimes 	}
697df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
698df8bae1dSRodney W. Grimes 	return (error);
699df8bae1dSRodney W. Grimes }
700df8bae1dSRodney W. Grimes 
701df8bae1dSRodney W. Grimes static char *
702df8bae1dSRodney W. Grimes sprint_d(n, buf, buflen)
703df8bae1dSRodney W. Grimes 	u_int n;
704df8bae1dSRodney W. Grimes 	char *buf;
705df8bae1dSRodney W. Grimes 	int buflen;
706df8bae1dSRodney W. Grimes {
707df8bae1dSRodney W. Grimes 	register char *cp = buf + buflen - 1;
708df8bae1dSRodney W. Grimes 
709df8bae1dSRodney W. Grimes 	*cp = 0;
710df8bae1dSRodney W. Grimes 	do {
711df8bae1dSRodney W. Grimes 		cp--;
712df8bae1dSRodney W. Grimes 		*cp = "0123456789"[n % 10];
713df8bae1dSRodney W. Grimes 		n /= 10;
714df8bae1dSRodney W. Grimes 	} while (n != 0);
715df8bae1dSRodney W. Grimes 	return (cp);
716df8bae1dSRodney W. Grimes }
717