if.c revision 1.44 1 1.44 fvdl /* $NetBSD: if.c,v 1.44 1998/03/01 02:25:04 fvdl Exp $ */
2 1.16 cgd
3 1.1 cgd /*
4 1.15 mycroft * Copyright (c) 1980, 1986, 1993
5 1.15 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.44 fvdl * @(#)if.c 8.5 (Berkeley) 1/9/95
36 1.1 cgd */
37 1.1 cgd
38 1.8 mycroft #include <sys/param.h>
39 1.8 mycroft #include <sys/mbuf.h>
40 1.8 mycroft #include <sys/systm.h>
41 1.15 mycroft #include <sys/proc.h>
42 1.8 mycroft #include <sys/socket.h>
43 1.8 mycroft #include <sys/socketvar.h>
44 1.8 mycroft #include <sys/protosw.h>
45 1.8 mycroft #include <sys/kernel.h>
46 1.8 mycroft #include <sys/ioctl.h>
47 1.1 cgd
48 1.8 mycroft #include <net/if.h>
49 1.8 mycroft #include <net/if_dl.h>
50 1.8 mycroft #include <net/if_types.h>
51 1.24 christos #include <net/radix.h>
52 1.1 cgd
53 1.1 cgd int ifqmaxlen = IFQ_MAXLEN;
54 1.15 mycroft void if_slowtimo __P((void *arg));
55 1.4 andrew
56 1.1 cgd /*
57 1.1 cgd * Network interface utility routines.
58 1.1 cgd *
59 1.1 cgd * Routines with ifa_ifwith* names take sockaddr *'s as
60 1.1 cgd * parameters.
61 1.1 cgd */
62 1.4 andrew void
63 1.1 cgd ifinit()
64 1.1 cgd {
65 1.1 cgd
66 1.4 andrew if_slowtimo(NULL);
67 1.1 cgd }
68 1.1 cgd
69 1.1 cgd int if_index = 0;
70 1.1 cgd struct ifaddr **ifnet_addrs;
71 1.1 cgd
72 1.1 cgd /*
73 1.1 cgd * Attach an interface to the
74 1.1 cgd * list of "active" interfaces.
75 1.1 cgd */
76 1.15 mycroft void
77 1.1 cgd if_attach(ifp)
78 1.1 cgd struct ifnet *ifp;
79 1.1 cgd {
80 1.1 cgd unsigned socksize, ifasize;
81 1.34 thorpej int namelen, masklen;
82 1.1 cgd register struct sockaddr_dl *sdl;
83 1.1 cgd register struct ifaddr *ifa;
84 1.1 cgd static int if_indexlim = 8;
85 1.1 cgd
86 1.22 mycroft if (if_index == 0)
87 1.22 mycroft TAILQ_INIT(&ifnet);
88 1.22 mycroft TAILQ_INIT(&ifp->if_addrlist);
89 1.21 mycroft TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
90 1.1 cgd ifp->if_index = ++if_index;
91 1.1 cgd if (ifnet_addrs == 0 || if_index >= if_indexlim) {
92 1.1 cgd unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
93 1.1 cgd struct ifaddr **q = (struct ifaddr **)
94 1.1 cgd malloc(n, M_IFADDR, M_WAITOK);
95 1.1 cgd if (ifnet_addrs) {
96 1.1 cgd bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
97 1.1 cgd free((caddr_t)ifnet_addrs, M_IFADDR);
98 1.1 cgd }
99 1.1 cgd ifnet_addrs = q;
100 1.1 cgd }
101 1.1 cgd /*
102 1.1 cgd * create a Link Level name for this device
103 1.1 cgd */
104 1.34 thorpej namelen = strlen(ifp->if_xname);
105 1.43 thorpej masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
106 1.15 mycroft socksize = masklen + ifp->if_addrlen;
107 1.1 cgd #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
108 1.1 cgd if (socksize < sizeof(*sdl))
109 1.1 cgd socksize = sizeof(*sdl);
110 1.20 cgd socksize = ROUNDUP(socksize);
111 1.1 cgd ifasize = sizeof(*ifa) + 2 * socksize;
112 1.1 cgd ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
113 1.1 cgd bzero((caddr_t)ifa, ifasize);
114 1.1 cgd sdl = (struct sockaddr_dl *)(ifa + 1);
115 1.1 cgd sdl->sdl_len = socksize;
116 1.1 cgd sdl->sdl_family = AF_LINK;
117 1.34 thorpej bcopy(ifp->if_xname, sdl->sdl_data, namelen);
118 1.34 thorpej sdl->sdl_nlen = namelen;
119 1.1 cgd sdl->sdl_index = ifp->if_index;
120 1.15 mycroft sdl->sdl_type = ifp->if_type;
121 1.15 mycroft ifnet_addrs[if_index - 1] = ifa;
122 1.15 mycroft ifa->ifa_ifp = ifp;
123 1.15 mycroft ifa->ifa_rtrequest = link_rtrequest;
124 1.21 mycroft TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
125 1.15 mycroft ifa->ifa_addr = (struct sockaddr *)sdl;
126 1.38 is ifp->if_sadl = sdl;
127 1.1 cgd sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
128 1.1 cgd ifa->ifa_netmask = (struct sockaddr *)sdl;
129 1.15 mycroft sdl->sdl_len = masklen;
130 1.1 cgd while (namelen != 0)
131 1.1 cgd sdl->sdl_data[--namelen] = 0xff;
132 1.40 thorpej if (ifp->if_snd.ifq_maxlen == 0)
133 1.40 thorpej ifp->if_snd.ifq_maxlen = ifqmaxlen;
134 1.42 is ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
135 1.1 cgd }
136 1.1 cgd /*
137 1.1 cgd * Locate an interface based on a complete address.
138 1.1 cgd */
139 1.1 cgd /*ARGSUSED*/
140 1.1 cgd struct ifaddr *
141 1.1 cgd ifa_ifwithaddr(addr)
142 1.1 cgd register struct sockaddr *addr;
143 1.1 cgd {
144 1.1 cgd register struct ifnet *ifp;
145 1.1 cgd register struct ifaddr *ifa;
146 1.1 cgd
147 1.1 cgd #define equal(a1, a2) \
148 1.1 cgd (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
149 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
150 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
151 1.1 cgd if (ifa->ifa_addr->sa_family != addr->sa_family)
152 1.1 cgd continue;
153 1.1 cgd if (equal(addr, ifa->ifa_addr))
154 1.1 cgd return (ifa);
155 1.1 cgd if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr &&
156 1.1 cgd equal(ifa->ifa_broadaddr, addr))
157 1.1 cgd return (ifa);
158 1.1 cgd }
159 1.1 cgd return ((struct ifaddr *)0);
160 1.1 cgd }
161 1.1 cgd /*
162 1.1 cgd * Locate the point to point interface with a given destination address.
163 1.1 cgd */
164 1.1 cgd /*ARGSUSED*/
165 1.1 cgd struct ifaddr *
166 1.1 cgd ifa_ifwithdstaddr(addr)
167 1.1 cgd register struct sockaddr *addr;
168 1.1 cgd {
169 1.1 cgd register struct ifnet *ifp;
170 1.1 cgd register struct ifaddr *ifa;
171 1.1 cgd
172 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
173 1.1 cgd if (ifp->if_flags & IFF_POINTOPOINT)
174 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
175 1.30 mrg if (ifa->ifa_addr->sa_family != addr->sa_family ||
176 1.30 mrg ifa->ifa_dstaddr == NULL)
177 1.1 cgd continue;
178 1.1 cgd if (equal(addr, ifa->ifa_dstaddr))
179 1.1 cgd return (ifa);
180 1.1 cgd }
181 1.1 cgd return ((struct ifaddr *)0);
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd /*
185 1.1 cgd * Find an interface on a specific network. If many, choice
186 1.15 mycroft * is most specific found.
187 1.1 cgd */
188 1.1 cgd struct ifaddr *
189 1.1 cgd ifa_ifwithnet(addr)
190 1.1 cgd struct sockaddr *addr;
191 1.1 cgd {
192 1.1 cgd register struct ifnet *ifp;
193 1.1 cgd register struct ifaddr *ifa;
194 1.15 mycroft struct ifaddr *ifa_maybe = 0;
195 1.1 cgd u_int af = addr->sa_family;
196 1.15 mycroft char *addr_data = addr->sa_data, *cplim;
197 1.1 cgd
198 1.1 cgd if (af == AF_LINK) {
199 1.1 cgd register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
200 1.1 cgd if (sdl->sdl_index && sdl->sdl_index <= if_index)
201 1.1 cgd return (ifnet_addrs[sdl->sdl_index - 1]);
202 1.1 cgd }
203 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
204 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
205 1.15 mycroft register char *cp, *cp2, *cp3;
206 1.15 mycroft
207 1.15 mycroft if (ifa->ifa_addr->sa_family != af ||
208 1.15 mycroft ifa->ifa_netmask == 0)
209 1.32 mrg next: continue;
210 1.15 mycroft cp = addr_data;
211 1.15 mycroft cp2 = ifa->ifa_addr->sa_data;
212 1.15 mycroft cp3 = ifa->ifa_netmask->sa_data;
213 1.15 mycroft cplim = (char *)ifa->ifa_netmask +
214 1.15 mycroft ifa->ifa_netmask->sa_len;
215 1.15 mycroft while (cp3 < cplim)
216 1.15 mycroft if ((*cp++ ^ *cp2++) & *cp3++)
217 1.32 mrg /* want to continue for() loop */
218 1.32 mrg goto next;
219 1.15 mycroft if (ifa_maybe == 0 ||
220 1.15 mycroft rn_refines((caddr_t)ifa->ifa_netmask,
221 1.15 mycroft (caddr_t)ifa_maybe->ifa_netmask))
222 1.15 mycroft ifa_maybe = ifa;
223 1.15 mycroft }
224 1.15 mycroft return (ifa_maybe);
225 1.26 mrg }
226 1.26 mrg /*
227 1.26 mrg * Find the interface of the addresss.
228 1.26 mrg */
229 1.26 mrg struct ifaddr *
230 1.26 mrg ifa_ifwithladdr(addr)
231 1.26 mrg struct sockaddr *addr;
232 1.26 mrg {
233 1.26 mrg struct ifaddr *ia;
234 1.26 mrg
235 1.26 mrg if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr))
236 1.26 mrg || (ia = ifa_ifwithnet(addr)))
237 1.26 mrg return (ia);
238 1.26 mrg return (NULL);
239 1.1 cgd }
240 1.1 cgd
241 1.1 cgd /*
242 1.1 cgd * Find an interface using a specific address family
243 1.1 cgd */
244 1.1 cgd struct ifaddr *
245 1.1 cgd ifa_ifwithaf(af)
246 1.1 cgd register int af;
247 1.1 cgd {
248 1.1 cgd register struct ifnet *ifp;
249 1.1 cgd register struct ifaddr *ifa;
250 1.1 cgd
251 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
252 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next)
253 1.21 mycroft if (ifa->ifa_addr->sa_family == af)
254 1.21 mycroft return (ifa);
255 1.1 cgd return ((struct ifaddr *)0);
256 1.1 cgd }
257 1.1 cgd
258 1.1 cgd /*
259 1.1 cgd * Find an interface address specific to an interface best matching
260 1.1 cgd * a given address.
261 1.1 cgd */
262 1.1 cgd struct ifaddr *
263 1.1 cgd ifaof_ifpforaddr(addr, ifp)
264 1.1 cgd struct sockaddr *addr;
265 1.1 cgd register struct ifnet *ifp;
266 1.1 cgd {
267 1.1 cgd register struct ifaddr *ifa;
268 1.1 cgd register char *cp, *cp2, *cp3;
269 1.1 cgd register char *cplim;
270 1.1 cgd struct ifaddr *ifa_maybe = 0;
271 1.1 cgd u_int af = addr->sa_family;
272 1.1 cgd
273 1.1 cgd if (af >= AF_MAX)
274 1.1 cgd return (0);
275 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
276 1.1 cgd if (ifa->ifa_addr->sa_family != af)
277 1.1 cgd continue;
278 1.1 cgd ifa_maybe = ifa;
279 1.1 cgd if (ifa->ifa_netmask == 0) {
280 1.1 cgd if (equal(addr, ifa->ifa_addr) ||
281 1.1 cgd (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
282 1.1 cgd return (ifa);
283 1.1 cgd continue;
284 1.1 cgd }
285 1.1 cgd cp = addr->sa_data;
286 1.1 cgd cp2 = ifa->ifa_addr->sa_data;
287 1.1 cgd cp3 = ifa->ifa_netmask->sa_data;
288 1.1 cgd cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
289 1.1 cgd for (; cp3 < cplim; cp3++)
290 1.1 cgd if ((*cp++ ^ *cp2++) & *cp3)
291 1.1 cgd break;
292 1.1 cgd if (cp3 == cplim)
293 1.1 cgd return (ifa);
294 1.1 cgd }
295 1.1 cgd return (ifa_maybe);
296 1.1 cgd }
297 1.9 mycroft
298 1.15 mycroft #include <net/route.h>
299 1.15 mycroft
300 1.1 cgd /*
301 1.1 cgd * Default action when installing a route with a Link Level gateway.
302 1.1 cgd * Lookup an appropriate real ifa to point to.
303 1.1 cgd * This should be moved to /sys/net/link.c eventually.
304 1.1 cgd */
305 1.15 mycroft void
306 1.1 cgd link_rtrequest(cmd, rt, sa)
307 1.15 mycroft int cmd;
308 1.15 mycroft register struct rtentry *rt;
309 1.15 mycroft struct sockaddr *sa;
310 1.1 cgd {
311 1.1 cgd register struct ifaddr *ifa;
312 1.1 cgd struct sockaddr *dst;
313 1.15 mycroft struct ifnet *ifp;
314 1.1 cgd
315 1.1 cgd if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
316 1.1 cgd ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
317 1.1 cgd return;
318 1.24 christos if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
319 1.15 mycroft IFAFREE(rt->rt_ifa);
320 1.1 cgd rt->rt_ifa = ifa;
321 1.15 mycroft ifa->ifa_refcnt++;
322 1.1 cgd if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
323 1.1 cgd ifa->ifa_rtrequest(cmd, rt, sa);
324 1.1 cgd }
325 1.1 cgd }
326 1.1 cgd
327 1.1 cgd /*
328 1.1 cgd * Mark an interface down and notify protocols of
329 1.1 cgd * the transition.
330 1.23 mycroft * NOTE: must be called at splsoftnet or equivalent.
331 1.1 cgd */
332 1.15 mycroft void
333 1.1 cgd if_down(ifp)
334 1.1 cgd register struct ifnet *ifp;
335 1.1 cgd {
336 1.1 cgd register struct ifaddr *ifa;
337 1.1 cgd
338 1.1 cgd ifp->if_flags &= ~IFF_UP;
339 1.21 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; ifa = ifa->ifa_list.tqe_next)
340 1.1 cgd pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
341 1.1 cgd if_qflush(&ifp->if_snd);
342 1.15 mycroft rt_ifmsg(ifp);
343 1.15 mycroft }
344 1.15 mycroft
345 1.15 mycroft /*
346 1.15 mycroft * Mark an interface up and notify protocols of
347 1.15 mycroft * the transition.
348 1.23 mycroft * NOTE: must be called at splsoftnet or equivalent.
349 1.15 mycroft */
350 1.15 mycroft void
351 1.15 mycroft if_up(ifp)
352 1.15 mycroft register struct ifnet *ifp;
353 1.15 mycroft {
354 1.24 christos #ifdef notyet
355 1.15 mycroft register struct ifaddr *ifa;
356 1.24 christos #endif
357 1.15 mycroft
358 1.15 mycroft ifp->if_flags |= IFF_UP;
359 1.15 mycroft #ifdef notyet
360 1.15 mycroft /* this has no effect on IP, and will kill all ISO connections XXX */
361 1.24 christos for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
362 1.24 christos ifa = ifa->ifa_list.tqe_next)
363 1.15 mycroft pfctlinput(PRC_IFUP, ifa->ifa_addr);
364 1.15 mycroft #endif
365 1.15 mycroft rt_ifmsg(ifp);
366 1.1 cgd }
367 1.1 cgd
368 1.1 cgd /*
369 1.1 cgd * Flush an interface queue.
370 1.1 cgd */
371 1.15 mycroft void
372 1.1 cgd if_qflush(ifq)
373 1.1 cgd register struct ifqueue *ifq;
374 1.1 cgd {
375 1.1 cgd register struct mbuf *m, *n;
376 1.1 cgd
377 1.1 cgd n = ifq->ifq_head;
378 1.24 christos while ((m = n) != NULL) {
379 1.1 cgd n = m->m_act;
380 1.1 cgd m_freem(m);
381 1.1 cgd }
382 1.1 cgd ifq->ifq_head = 0;
383 1.1 cgd ifq->ifq_tail = 0;
384 1.1 cgd ifq->ifq_len = 0;
385 1.1 cgd }
386 1.1 cgd
387 1.1 cgd /*
388 1.1 cgd * Handle interface watchdog timer routines. Called
389 1.1 cgd * from softclock, we decrement timers (if set) and
390 1.1 cgd * call the appropriate interface routine on expiration.
391 1.1 cgd */
392 1.4 andrew void
393 1.13 cgd if_slowtimo(arg)
394 1.13 cgd void *arg;
395 1.1 cgd {
396 1.1 cgd register struct ifnet *ifp;
397 1.1 cgd int s = splimp();
398 1.1 cgd
399 1.21 mycroft for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next) {
400 1.1 cgd if (ifp->if_timer == 0 || --ifp->if_timer)
401 1.1 cgd continue;
402 1.1 cgd if (ifp->if_watchdog)
403 1.34 thorpej (*ifp->if_watchdog)(ifp);
404 1.1 cgd }
405 1.1 cgd splx(s);
406 1.14 mycroft timeout(if_slowtimo, NULL, hz / IFNET_SLOWHZ);
407 1.1 cgd }
408 1.1 cgd
409 1.1 cgd /*
410 1.1 cgd * Map interface name to
411 1.1 cgd * interface structure pointer.
412 1.1 cgd */
413 1.1 cgd struct ifnet *
414 1.1 cgd ifunit(name)
415 1.1 cgd register char *name;
416 1.1 cgd {
417 1.1 cgd register struct ifnet *ifp;
418 1.34 thorpej
419 1.34 thorpej for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
420 1.35 thorpej if (strcmp(ifp->if_xname, name) == 0)
421 1.34 thorpej return (ifp);
422 1.34 thorpej
423 1.34 thorpej return (NULL);
424 1.1 cgd }
425 1.1 cgd
426 1.1 cgd /*
427 1.1 cgd * Interface ioctls.
428 1.1 cgd */
429 1.15 mycroft int
430 1.1 cgd ifioctl(so, cmd, data, p)
431 1.1 cgd struct socket *so;
432 1.18 cgd u_long cmd;
433 1.1 cgd caddr_t data;
434 1.1 cgd struct proc *p;
435 1.1 cgd {
436 1.1 cgd register struct ifnet *ifp;
437 1.1 cgd register struct ifreq *ifr;
438 1.1 cgd int error;
439 1.1 cgd
440 1.1 cgd switch (cmd) {
441 1.1 cgd
442 1.1 cgd case SIOCGIFCONF:
443 1.1 cgd case OSIOCGIFCONF:
444 1.1 cgd return (ifconf(cmd, data));
445 1.1 cgd }
446 1.1 cgd ifr = (struct ifreq *)data;
447 1.1 cgd ifp = ifunit(ifr->ifr_name);
448 1.1 cgd if (ifp == 0)
449 1.1 cgd return (ENXIO);
450 1.1 cgd switch (cmd) {
451 1.1 cgd
452 1.1 cgd case SIOCGIFFLAGS:
453 1.1 cgd ifr->ifr_flags = ifp->if_flags;
454 1.1 cgd break;
455 1.1 cgd
456 1.1 cgd case SIOCGIFMETRIC:
457 1.1 cgd ifr->ifr_metric = ifp->if_metric;
458 1.1 cgd break;
459 1.7 hpeyerl
460 1.36 mycroft case SIOCGIFMTU:
461 1.37 cgd ifr->ifr_mtu = ifp->if_mtu;
462 1.37 cgd break;
463 1.36 mycroft
464 1.1 cgd case SIOCSIFFLAGS:
465 1.24 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
466 1.1 cgd return (error);
467 1.1 cgd if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
468 1.1 cgd int s = splimp();
469 1.1 cgd if_down(ifp);
470 1.1 cgd splx(s);
471 1.1 cgd }
472 1.15 mycroft if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
473 1.15 mycroft int s = splimp();
474 1.15 mycroft if_up(ifp);
475 1.15 mycroft splx(s);
476 1.15 mycroft }
477 1.1 cgd ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
478 1.1 cgd (ifr->ifr_flags &~ IFF_CANTCHANGE);
479 1.1 cgd if (ifp->if_ioctl)
480 1.1 cgd (void) (*ifp->if_ioctl)(ifp, cmd, data);
481 1.1 cgd break;
482 1.1 cgd
483 1.1 cgd case SIOCSIFMETRIC:
484 1.24 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
485 1.1 cgd return (error);
486 1.1 cgd ifp->if_metric = ifr->ifr_metric;
487 1.5 deraadt break;
488 1.5 deraadt
489 1.36 mycroft case SIOCSIFMTU:
490 1.15 mycroft case SIOCADDMULTI:
491 1.15 mycroft case SIOCDELMULTI:
492 1.39 thorpej case SIOCSIFMEDIA:
493 1.24 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
494 1.15 mycroft return (error);
495 1.39 thorpej /* FALLTHROUGH */
496 1.39 thorpej case SIOCGIFMEDIA:
497 1.15 mycroft if (ifp->if_ioctl == 0)
498 1.5 deraadt return (EOPNOTSUPP);
499 1.5 deraadt return ((*ifp->if_ioctl)(ifp, cmd, data));
500 1.1 cgd
501 1.1 cgd default:
502 1.1 cgd if (so->so_proto == 0)
503 1.1 cgd return (EOPNOTSUPP);
504 1.33 christos #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4)
505 1.1 cgd return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
506 1.36 mycroft (struct mbuf *)cmd, (struct mbuf *)data,
507 1.36 mycroft (struct mbuf *)ifp, p));
508 1.1 cgd #else
509 1.1 cgd {
510 1.1 cgd int ocmd = cmd;
511 1.1 cgd
512 1.1 cgd switch (cmd) {
513 1.1 cgd
514 1.28 mycroft case SIOCSIFADDR:
515 1.1 cgd case SIOCSIFDSTADDR:
516 1.1 cgd case SIOCSIFBRDADDR:
517 1.1 cgd case SIOCSIFNETMASK:
518 1.1 cgd #if BYTE_ORDER != BIG_ENDIAN
519 1.1 cgd if (ifr->ifr_addr.sa_family == 0 &&
520 1.1 cgd ifr->ifr_addr.sa_len < 16) {
521 1.1 cgd ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
522 1.1 cgd ifr->ifr_addr.sa_len = 16;
523 1.1 cgd }
524 1.1 cgd #else
525 1.1 cgd if (ifr->ifr_addr.sa_len == 0)
526 1.1 cgd ifr->ifr_addr.sa_len = 16;
527 1.1 cgd #endif
528 1.1 cgd break;
529 1.1 cgd
530 1.1 cgd case OSIOCGIFADDR:
531 1.1 cgd cmd = SIOCGIFADDR;
532 1.1 cgd break;
533 1.1 cgd
534 1.1 cgd case OSIOCGIFDSTADDR:
535 1.1 cgd cmd = SIOCGIFDSTADDR;
536 1.1 cgd break;
537 1.1 cgd
538 1.1 cgd case OSIOCGIFBRDADDR:
539 1.1 cgd cmd = SIOCGIFBRDADDR;
540 1.1 cgd break;
541 1.1 cgd
542 1.1 cgd case OSIOCGIFNETMASK:
543 1.1 cgd cmd = SIOCGIFNETMASK;
544 1.1 cgd }
545 1.24 christos error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
546 1.36 mycroft (struct mbuf *)cmd, (struct mbuf *)data,
547 1.36 mycroft (struct mbuf *)ifp, p));
548 1.1 cgd switch (ocmd) {
549 1.1 cgd
550 1.1 cgd case OSIOCGIFADDR:
551 1.1 cgd case OSIOCGIFDSTADDR:
552 1.1 cgd case OSIOCGIFBRDADDR:
553 1.1 cgd case OSIOCGIFNETMASK:
554 1.20 cgd *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
555 1.1 cgd }
556 1.1 cgd return (error);
557 1.1 cgd
558 1.1 cgd }
559 1.1 cgd #endif
560 1.1 cgd }
561 1.1 cgd return (0);
562 1.1 cgd }
563 1.1 cgd
564 1.1 cgd /*
565 1.1 cgd * Return interface configuration
566 1.1 cgd * of system. List may be used
567 1.1 cgd * in later ioctl's (above) to get
568 1.1 cgd * other information.
569 1.1 cgd */
570 1.1 cgd /*ARGSUSED*/
571 1.15 mycroft int
572 1.1 cgd ifconf(cmd, data)
573 1.19 mycroft u_long cmd;
574 1.1 cgd caddr_t data;
575 1.1 cgd {
576 1.1 cgd register struct ifconf *ifc = (struct ifconf *)data;
577 1.21 mycroft register struct ifnet *ifp;
578 1.1 cgd register struct ifaddr *ifa;
579 1.1 cgd struct ifreq ifr, *ifrp;
580 1.1 cgd int space = ifc->ifc_len, error = 0;
581 1.1 cgd
582 1.1 cgd ifrp = ifc->ifc_req;
583 1.21 mycroft for (ifp = ifnet.tqh_first;
584 1.33 christos space >= sizeof (ifr) && ifp != 0; ifp = ifp->if_list.tqe_next) {
585 1.34 thorpej bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
586 1.21 mycroft if ((ifa = ifp->if_addrlist.tqh_first) == 0) {
587 1.1 cgd bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
588 1.15 mycroft error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
589 1.15 mycroft sizeof(ifr));
590 1.1 cgd if (error)
591 1.1 cgd break;
592 1.1 cgd space -= sizeof (ifr), ifrp++;
593 1.1 cgd } else
594 1.33 christos for (; space >= sizeof (ifr) && ifa != 0; ifa = ifa->ifa_list.tqe_next) {
595 1.1 cgd register struct sockaddr *sa = ifa->ifa_addr;
596 1.33 christos #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4)
597 1.1 cgd if (cmd == OSIOCGIFCONF) {
598 1.1 cgd struct osockaddr *osa =
599 1.1 cgd (struct osockaddr *)&ifr.ifr_addr;
600 1.1 cgd ifr.ifr_addr = *sa;
601 1.1 cgd osa->sa_family = sa->sa_family;
602 1.1 cgd error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
603 1.1 cgd sizeof (ifr));
604 1.1 cgd ifrp++;
605 1.1 cgd } else
606 1.1 cgd #endif
607 1.1 cgd if (sa->sa_len <= sizeof(*sa)) {
608 1.1 cgd ifr.ifr_addr = *sa;
609 1.1 cgd error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
610 1.1 cgd sizeof (ifr));
611 1.1 cgd ifrp++;
612 1.1 cgd } else {
613 1.1 cgd space -= sa->sa_len - sizeof(*sa);
614 1.1 cgd if (space < sizeof (ifr))
615 1.1 cgd break;
616 1.1 cgd error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
617 1.1 cgd sizeof (ifr.ifr_name));
618 1.1 cgd if (error == 0)
619 1.1 cgd error = copyout((caddr_t)sa,
620 1.1 cgd (caddr_t)&ifrp->ifr_addr, sa->sa_len);
621 1.1 cgd ifrp = (struct ifreq *)
622 1.1 cgd (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
623 1.1 cgd }
624 1.1 cgd if (error)
625 1.1 cgd break;
626 1.1 cgd space -= sizeof (ifr);
627 1.1 cgd }
628 1.1 cgd }
629 1.1 cgd ifc->ifc_len -= space;
630 1.1 cgd return (error);
631 1.1 cgd }
632