if.c revision 1.147 1 /* $NetBSD: if.c,v 1.147 2004/10/07 20:06:58 tron Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William Studnemund and Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the project nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68 /*
69 * Copyright (c) 1980, 1986, 1993
70 * The Regents of the University of California. All rights reserved.
71 *
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
74 * are met:
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. Neither the name of the University nor the names of its contributors
81 * may be used to endorse or promote products derived from this software
82 * without specific prior written permission.
83 *
84 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 * SUCH DAMAGE.
95 *
96 * @(#)if.c 8.5 (Berkeley) 1/9/95
97 */
98
99 #include <sys/cdefs.h>
100 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.147 2004/10/07 20:06:58 tron Exp $");
101
102 #include "opt_inet.h"
103
104 #include "opt_compat_linux.h"
105 #include "opt_compat_svr4.h"
106 #include "opt_compat_ultrix.h"
107 #include "opt_compat_43.h"
108 #include "opt_atalk.h"
109 #include "opt_ccitt.h"
110 #include "opt_natm.h"
111 #include "opt_pfil_hooks.h"
112
113 #include <sys/param.h>
114 #include <sys/mbuf.h>
115 #include <sys/systm.h>
116 #include <sys/callout.h>
117 #include <sys/proc.h>
118 #include <sys/socket.h>
119 #include <sys/socketvar.h>
120 #include <sys/domain.h>
121 #include <sys/protosw.h>
122 #include <sys/kernel.h>
123 #include <sys/ioctl.h>
124 #include <sys/sysctl.h>
125
126 #include <net/if.h>
127 #include <net/if_dl.h>
128 #include <net/if_ether.h>
129 #include <net/if_media.h>
130 #include <net80211/ieee80211.h>
131 #include <net80211/ieee80211_ioctl.h>
132 #include <net/if_types.h>
133 #include <net/radix.h>
134 #include <net/route.h>
135 #include <net/netisr.h>
136 #ifdef NETATALK
137 #include <netatalk/at_extern.h>
138 #include <netatalk/at.h>
139 #endif
140 #include <net/pfil.h>
141
142 #ifdef INET6
143 #include <netinet/in.h>
144 #include <netinet6/in6_var.h>
145 #include <netinet6/nd6.h>
146 #endif
147
148 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
149 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
150
151 int ifqmaxlen = IFQ_MAXLEN;
152 struct callout if_slowtimo_ch;
153
154 int netisr; /* scheduling bits for network */
155
156 int if_rt_walktree __P((struct radix_node *, void *));
157
158 struct if_clone *if_clone_lookup __P((const char *, int *));
159 int if_clone_list __P((struct if_clonereq *));
160
161 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
162 int if_cloners_count;
163
164 #ifdef PFIL_HOOKS
165 struct pfil_head if_pfil; /* packet filtering hook for interfaces */
166 #endif
167
168 #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \
169 defined(ISO) || defined(CCITT) || defined(NATM)
170 static void if_detach_queues __P((struct ifnet *, struct ifqueue *));
171 #endif
172
173 /*
174 * Network interface utility routines.
175 *
176 * Routines with ifa_ifwith* names take sockaddr *'s as
177 * parameters.
178 */
179 void
180 ifinit()
181 {
182
183 callout_init(&if_slowtimo_ch);
184 if_slowtimo(NULL);
185 #ifdef PFIL_HOOKS
186 if_pfil.ph_type = PFIL_TYPE_IFNET;
187 if_pfil.ph_ifnet = NULL;
188 if (pfil_head_register(&if_pfil) != 0)
189 printf("WARNING: unable to register pfil hook\n");
190 #endif
191 }
192
193 /*
194 * Null routines used while an interface is going away. These routines
195 * just return an error.
196 */
197
198 int
199 if_nulloutput(ifp, m, so, rt)
200 struct ifnet *ifp;
201 struct mbuf *m;
202 struct sockaddr *so;
203 struct rtentry *rt;
204 {
205
206 return (ENXIO);
207 }
208
209 void
210 if_nullinput(ifp, m)
211 struct ifnet *ifp;
212 struct mbuf *m;
213 {
214
215 /* Nothing. */
216 }
217
218 void
219 if_nullstart(ifp)
220 struct ifnet *ifp;
221 {
222
223 /* Nothing. */
224 }
225
226 int
227 if_nullioctl(ifp, cmd, data)
228 struct ifnet *ifp;
229 u_long cmd;
230 caddr_t data;
231 {
232
233 return (ENXIO);
234 }
235
236 int
237 if_nullinit(ifp)
238 struct ifnet *ifp;
239 {
240
241 return (ENXIO);
242 }
243
244 void
245 if_nullstop(ifp, disable)
246 struct ifnet *ifp;
247 int disable;
248 {
249
250 /* Nothing. */
251 }
252
253 void
254 if_nullwatchdog(ifp)
255 struct ifnet *ifp;
256 {
257
258 /* Nothing. */
259 }
260
261 void
262 if_nulldrain(ifp)
263 struct ifnet *ifp;
264 {
265
266 /* Nothing. */
267 }
268
269 static u_int if_index = 1;
270 struct ifnet_head ifnet;
271 size_t if_indexlim = 0;
272 struct ifaddr **ifnet_addrs = NULL;
273 struct ifnet **ifindex2ifnet = NULL;
274
275 /*
276 * Allocate the link level name for the specified interface. This
277 * is an attachment helper. It must be called after ifp->if_addrlen
278 * is initialized, which may not be the case when if_attach() is
279 * called.
280 */
281 void
282 if_alloc_sadl(struct ifnet *ifp)
283 {
284 unsigned socksize, ifasize;
285 int namelen, masklen;
286 struct sockaddr_dl *sdl;
287 struct ifaddr *ifa;
288
289 /*
290 * If the interface already has a link name, release it
291 * now. This is useful for interfaces that can change
292 * link types, and thus switch link names often.
293 */
294 if (ifp->if_sadl != NULL)
295 if_free_sadl(ifp);
296
297 namelen = strlen(ifp->if_xname);
298 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
299 socksize = masklen + ifp->if_addrlen;
300 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
301 if (socksize < sizeof(*sdl))
302 socksize = sizeof(*sdl);
303 socksize = ROUNDUP(socksize);
304 ifasize = sizeof(*ifa) + 2 * socksize;
305 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK);
306 memset((caddr_t)ifa, 0, ifasize);
307 sdl = (struct sockaddr_dl *)(ifa + 1);
308 sdl->sdl_len = socksize;
309 sdl->sdl_family = AF_LINK;
310 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
311 sdl->sdl_nlen = namelen;
312 sdl->sdl_alen = ifp->if_addrlen;
313 sdl->sdl_index = ifp->if_index;
314 sdl->sdl_type = ifp->if_type;
315 ifnet_addrs[ifp->if_index] = ifa;
316 IFAREF(ifa);
317 ifa->ifa_ifp = ifp;
318 ifa->ifa_rtrequest = link_rtrequest;
319 TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list);
320 IFAREF(ifa);
321 ifa->ifa_addr = (struct sockaddr *)sdl;
322 ifp->if_sadl = sdl;
323 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
324 ifa->ifa_netmask = (struct sockaddr *)sdl;
325 sdl->sdl_len = masklen;
326 while (namelen != 0)
327 sdl->sdl_data[--namelen] = 0xff;
328 }
329
330 /*
331 * Free the link level name for the specified interface. This is
332 * a detach helper. This is called from if_detach() or from
333 * link layer type specific detach functions.
334 */
335 void
336 if_free_sadl(struct ifnet *ifp)
337 {
338 struct ifaddr *ifa;
339 int s;
340
341 ifa = ifnet_addrs[ifp->if_index];
342 if (ifa == NULL) {
343 KASSERT(ifp->if_sadl == NULL);
344 return;
345 }
346
347 KASSERT(ifp->if_sadl != NULL);
348
349 s = splnet();
350 rtinit(ifa, RTM_DELETE, 0);
351 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
352 IFAFREE(ifa);
353
354 ifp->if_sadl = NULL;
355
356 ifnet_addrs[ifp->if_index] = NULL;
357 IFAFREE(ifa);
358 splx(s);
359 }
360
361 /*
362 * Attach an interface to the
363 * list of "active" interfaces.
364 */
365 void
366 if_attach(ifp)
367 struct ifnet *ifp;
368 {
369 int indexlim = 0;
370
371 if (if_indexlim == 0) {
372 TAILQ_INIT(&ifnet);
373 if_indexlim = 8;
374 }
375 TAILQ_INIT(&ifp->if_addrlist);
376 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list);
377 ifp->if_index = if_index;
378 if (ifindex2ifnet == 0)
379 if_index++;
380 else
381 while (ifp->if_index < if_indexlim &&
382 ifindex2ifnet[ifp->if_index] != NULL) {
383 ++if_index;
384 if (if_index == 0)
385 if_index = 1;
386 /*
387 * If we hit USHRT_MAX, we skip back to 0 since
388 * there are a number of places where the value
389 * of if_index or if_index itself is compared
390 * to or stored in an unsigned short. By
391 * jumping back, we won't botch those assignments
392 * or comparisons.
393 */
394 else if (if_index == USHRT_MAX) {
395 /*
396 * However, if we have to jump back to
397 * zero *twice* without finding an empty
398 * slot in ifindex2ifnet[], then there
399 * there are too many (>65535) interfaces.
400 */
401 if (indexlim++)
402 panic("too many interfaces");
403 else
404 if_index = 1;
405 }
406 ifp->if_index = if_index;
407 }
408
409 /*
410 * We have some arrays that should be indexed by if_index.
411 * since if_index will grow dynamically, they should grow too.
412 * struct ifadd **ifnet_addrs
413 * struct ifnet **ifindex2ifnet
414 */
415 if (ifnet_addrs == 0 || ifindex2ifnet == 0 ||
416 ifp->if_index >= if_indexlim) {
417 size_t m, n, oldlim;
418 caddr_t q;
419
420 oldlim = if_indexlim;
421 while (ifp->if_index >= if_indexlim)
422 if_indexlim <<= 1;
423
424 /* grow ifnet_addrs */
425 m = oldlim * sizeof(struct ifaddr *);
426 n = if_indexlim * sizeof(struct ifaddr *);
427 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
428 memset(q, 0, n);
429 if (ifnet_addrs) {
430 bcopy((caddr_t)ifnet_addrs, q, m);
431 free((caddr_t)ifnet_addrs, M_IFADDR);
432 }
433 ifnet_addrs = (struct ifaddr **)q;
434
435 /* grow ifindex2ifnet */
436 m = oldlim * sizeof(struct ifnet *);
437 n = if_indexlim * sizeof(struct ifnet *);
438 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
439 memset(q, 0, n);
440 if (ifindex2ifnet) {
441 bcopy((caddr_t)ifindex2ifnet, q, m);
442 free((caddr_t)ifindex2ifnet, M_IFADDR);
443 }
444 ifindex2ifnet = (struct ifnet **)q;
445 }
446
447 ifindex2ifnet[ifp->if_index] = ifp;
448
449 /*
450 * Link level name is allocated later by a separate call to
451 * if_alloc_sadl().
452 */
453
454 if (ifp->if_snd.ifq_maxlen == 0)
455 ifp->if_snd.ifq_maxlen = ifqmaxlen;
456 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
457
458 ifp->if_link_state = LINK_STATE_UNKNOWN;
459
460 ifp->if_capenable = 0;
461 ifp->if_csum_flags_tx = 0;
462 ifp->if_csum_flags_rx = 0;
463
464 #ifdef ALTQ
465 ifp->if_snd.altq_type = 0;
466 ifp->if_snd.altq_disc = NULL;
467 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
468 ifp->if_snd.altq_tbr = NULL;
469 ifp->if_snd.altq_ifp = ifp;
470 #endif
471
472 #ifdef PFIL_HOOKS
473 ifp->if_pfil.ph_type = PFIL_TYPE_IFNET;
474 ifp->if_pfil.ph_ifnet = ifp;
475 if (pfil_head_register(&ifp->if_pfil) != 0)
476 printf("%s: WARNING: unable to register pfil hook\n",
477 ifp->if_xname);
478 (void)pfil_run_hooks(&if_pfil,
479 (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
480 #endif
481
482 if (domains)
483 if_attachdomain1(ifp);
484
485 /* Announce the interface. */
486 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
487 }
488
489 void
490 if_attachdomain()
491 {
492 struct ifnet *ifp;
493 int s;
494
495 s = splnet();
496 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
497 if_attachdomain1(ifp);
498 splx(s);
499 }
500
501 void
502 if_attachdomain1(ifp)
503 struct ifnet *ifp;
504 {
505 struct domain *dp;
506 int s;
507
508 s = splnet();
509
510 /* address family dependent data region */
511 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
512 for (dp = domains; dp; dp = dp->dom_next) {
513 if (dp->dom_ifattach)
514 ifp->if_afdata[dp->dom_family] =
515 (*dp->dom_ifattach)(ifp);
516 }
517
518 splx(s);
519 }
520
521 /*
522 * Deactivate an interface. This points all of the procedure
523 * handles at error stubs. May be called from interrupt context.
524 */
525 void
526 if_deactivate(ifp)
527 struct ifnet *ifp;
528 {
529 int s;
530
531 s = splnet();
532
533 ifp->if_output = if_nulloutput;
534 ifp->if_input = if_nullinput;
535 ifp->if_start = if_nullstart;
536 ifp->if_ioctl = if_nullioctl;
537 ifp->if_init = if_nullinit;
538 ifp->if_stop = if_nullstop;
539 ifp->if_watchdog = if_nullwatchdog;
540 ifp->if_drain = if_nulldrain;
541
542 /* No more packets may be enqueued. */
543 ifp->if_snd.ifq_maxlen = 0;
544
545 splx(s);
546 }
547
548 /*
549 * Detach an interface from the list of "active" interfaces,
550 * freeing any resources as we go along.
551 *
552 * NOTE: This routine must be called with a valid thread context,
553 * as it may block.
554 */
555 void
556 if_detach(ifp)
557 struct ifnet *ifp;
558 {
559 struct socket so;
560 struct ifaddr *ifa, **ifap;
561 #ifdef IFAREF_DEBUG
562 struct ifaddr *last_ifa = NULL;
563 #endif
564 struct domain *dp;
565 const struct protosw *pr;
566 struct radix_node_head *rnh;
567 int s, i, family, purged;
568
569 /*
570 * XXX It's kind of lame that we have to have the
571 * XXX socket structure...
572 */
573 memset(&so, 0, sizeof(so));
574
575 s = splnet();
576
577 /*
578 * Do an if_down() to give protocols a chance to do something.
579 */
580 if_down(ifp);
581
582 #ifdef ALTQ
583 if (ALTQ_IS_ENABLED(&ifp->if_snd))
584 altq_disable(&ifp->if_snd);
585 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
586 altq_detach(&ifp->if_snd);
587 #endif
588
589 #ifdef PFIL_HOOKS
590 (void)pfil_run_hooks(&if_pfil,
591 (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
592 (void)pfil_head_unregister(&ifp->if_pfil);
593 #endif
594
595 /*
596 * Rip all the addresses off the interface. This should make
597 * all of the routes go away.
598 */
599 ifap = &TAILQ_FIRST(&ifp->if_addrlist); /* XXX abstraction violation */
600 while ((ifa = *ifap)) {
601 family = ifa->ifa_addr->sa_family;
602 #ifdef IFAREF_DEBUG
603 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
604 ifa, family, ifa->ifa_refcnt);
605 if (last_ifa != NULL && ifa == last_ifa)
606 panic("if_detach: loop detected");
607 last_ifa = ifa;
608 #endif
609 if (family == AF_LINK) {
610 ifap = &TAILQ_NEXT(ifa, ifa_list);
611 continue;
612 }
613 dp = pffinddomain(family);
614 #ifdef DIAGNOSTIC
615 if (dp == NULL)
616 panic("if_detach: no domain for AF %d",
617 family);
618 #endif
619 purged = 0;
620 for (pr = dp->dom_protosw;
621 pr < dp->dom_protoswNPROTOSW; pr++) {
622 so.so_proto = pr;
623 if (pr->pr_usrreq != NULL) {
624 (void) (*pr->pr_usrreq)(&so,
625 PRU_PURGEIF, NULL, NULL,
626 (struct mbuf *) ifp, curproc);
627 purged = 1;
628 }
629 }
630 if (purged == 0) {
631 /*
632 * XXX What's really the best thing to do
633 * XXX here? --thorpej (at) NetBSD.org
634 */
635 printf("if_detach: WARNING: AF %d not purged\n",
636 family);
637 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
638 }
639 }
640
641 if_free_sadl(ifp);
642
643 /* Walk the routing table looking for straglers. */
644 for (i = 0; i <= AF_MAX; i++) {
645 if ((rnh = rt_tables[i]) != NULL)
646 (void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp);
647 }
648
649 for (dp = domains; dp; dp = dp->dom_next) {
650 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
651 (*dp->dom_ifdetach)(ifp,
652 ifp->if_afdata[dp->dom_family]);
653 }
654
655 /* Announce that the interface is gone. */
656 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
657
658 ifindex2ifnet[ifp->if_index] = NULL;
659
660 TAILQ_REMOVE(&ifnet, ifp, if_list);
661
662 /*
663 * remove packets came from ifp, from software interrupt queues.
664 * net/netisr_dispatch.h is not usable, as some of them use
665 * strange queue names.
666 */
667 #define IF_DETACH_QUEUES(x) \
668 do { \
669 extern struct ifqueue x; \
670 if_detach_queues(ifp, & x); \
671 } while (/*CONSTCOND*/ 0)
672 #ifdef INET
673 #if NARP > 0
674 IF_DETACH_QUEUES(arpintrq);
675 #endif
676 IF_DETACH_QUEUES(ipintrq);
677 #endif
678 #ifdef INET6
679 IF_DETACH_QUEUES(ip6intrq);
680 #endif
681 #ifdef NETATALK
682 IF_DETACH_QUEUES(atintrq1);
683 IF_DETACH_QUEUES(atintrq2);
684 #endif
685 #ifdef NS
686 IF_DETACH_QUEUES(nsintrq);
687 #endif
688 #ifdef ISO
689 IF_DETACH_QUEUES(clnlintrq);
690 #endif
691 #ifdef CCITT
692 IF_DETACH_QUEUES(llcintrq);
693 IF_DETACH_QUEUES(hdintrq);
694 #endif
695 #ifdef NATM
696 IF_DETACH_QUEUES(natmintrq);
697 #endif
698 #ifdef DECNET
699 IF_DETACH_QUEUES(decnetintrq);
700 #endif
701 #undef IF_DETACH_QUEUES
702
703 splx(s);
704 }
705
706 #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \
707 defined(ISO) || defined(CCITT) || defined(NATM) || defined(DECNET)
708 static void
709 if_detach_queues(ifp, q)
710 struct ifnet *ifp;
711 struct ifqueue *q;
712 {
713 struct mbuf *m, *prev, *next;
714
715 prev = NULL;
716 for (m = q->ifq_head; m; m = next) {
717 next = m->m_nextpkt;
718 #ifdef DIAGNOSTIC
719 if ((m->m_flags & M_PKTHDR) == 0) {
720 prev = m;
721 continue;
722 }
723 #endif
724 if (m->m_pkthdr.rcvif != ifp) {
725 prev = m;
726 continue;
727 }
728
729 if (prev)
730 prev->m_nextpkt = m->m_nextpkt;
731 else
732 q->ifq_head = m->m_nextpkt;
733 if (q->ifq_tail == m)
734 q->ifq_tail = prev;
735 q->ifq_len--;
736
737 m->m_nextpkt = NULL;
738 m_freem(m);
739 IF_DROP(q);
740 }
741 }
742 #endif /* defined(INET) || ... */
743
744 /*
745 * Callback for a radix tree walk to delete all references to an
746 * ifnet.
747 */
748 int
749 if_rt_walktree(rn, v)
750 struct radix_node *rn;
751 void *v;
752 {
753 struct ifnet *ifp = (struct ifnet *)v;
754 struct rtentry *rt = (struct rtentry *)rn;
755 int error;
756
757 if (rt->rt_ifp == ifp) {
758 /* Delete the entry. */
759 error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
760 rt_mask(rt), rt->rt_flags, NULL);
761 if (error)
762 printf("%s: warning: unable to delete rtentry @ %p, "
763 "error = %d\n", ifp->if_xname, rt, error);
764 }
765 return (0);
766 }
767
768 /*
769 * Create a clone network interface.
770 */
771 int
772 if_clone_create(name)
773 const char *name;
774 {
775 struct if_clone *ifc;
776 int unit;
777
778 ifc = if_clone_lookup(name, &unit);
779 if (ifc == NULL)
780 return (EINVAL);
781
782 if (ifunit(name) != NULL)
783 return (EEXIST);
784
785 return ((*ifc->ifc_create)(ifc, unit));
786 }
787
788 /*
789 * Destroy a clone network interface.
790 */
791 int
792 if_clone_destroy(name)
793 const char *name;
794 {
795 struct if_clone *ifc;
796 struct ifnet *ifp;
797
798 ifc = if_clone_lookup(name, NULL);
799 if (ifc == NULL)
800 return (EINVAL);
801
802 ifp = ifunit(name);
803 if (ifp == NULL)
804 return (ENXIO);
805
806 if (ifc->ifc_destroy == NULL)
807 return (EOPNOTSUPP);
808
809 (*ifc->ifc_destroy)(ifp);
810 return (0);
811 }
812
813 /*
814 * Look up a network interface cloner.
815 */
816 struct if_clone *
817 if_clone_lookup(name, unitp)
818 const char *name;
819 int *unitp;
820 {
821 struct if_clone *ifc;
822 const char *cp;
823 int unit;
824
825 /* separate interface name from unit */
826 for (cp = name;
827 cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
828 cp++)
829 continue;
830
831 if (cp == name || cp - name == IFNAMSIZ || !*cp)
832 return (NULL); /* No name or unit number */
833
834 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
835 if (strlen(ifc->ifc_name) == cp - name &&
836 !strncmp(name, ifc->ifc_name, cp - name))
837 break;
838 }
839
840 if (ifc == NULL)
841 return (NULL);
842
843 unit = 0;
844 while (cp - name < IFNAMSIZ && *cp) {
845 if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
846 /* Bogus unit number. */
847 return (NULL);
848 }
849 unit = (unit * 10) + (*cp++ - '0');
850 }
851
852 if (unitp != NULL)
853 *unitp = unit;
854 return (ifc);
855 }
856
857 /*
858 * Register a network interface cloner.
859 */
860 void
861 if_clone_attach(ifc)
862 struct if_clone *ifc;
863 {
864
865 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
866 if_cloners_count++;
867 }
868
869 /*
870 * Unregister a network interface cloner.
871 */
872 void
873 if_clone_detach(ifc)
874 struct if_clone *ifc;
875 {
876
877 LIST_REMOVE(ifc, ifc_list);
878 if_cloners_count--;
879 }
880
881 /*
882 * Provide list of interface cloners to userspace.
883 */
884 int
885 if_clone_list(ifcr)
886 struct if_clonereq *ifcr;
887 {
888 char outbuf[IFNAMSIZ], *dst;
889 struct if_clone *ifc;
890 int count, error = 0;
891
892 ifcr->ifcr_total = if_cloners_count;
893 if ((dst = ifcr->ifcr_buffer) == NULL) {
894 /* Just asking how many there are. */
895 return (0);
896 }
897
898 if (ifcr->ifcr_count < 0)
899 return (EINVAL);
900
901 count = (if_cloners_count < ifcr->ifcr_count) ?
902 if_cloners_count : ifcr->ifcr_count;
903
904 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
905 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
906 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
907 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */
908 error = copyout(outbuf, dst, IFNAMSIZ);
909 if (error)
910 break;
911 }
912
913 return (error);
914 }
915
916 /*
917 * Locate an interface based on a complete address.
918 */
919 /*ARGSUSED*/
920 struct ifaddr *
921 ifa_ifwithaddr(addr)
922 const struct sockaddr *addr;
923 {
924 struct ifnet *ifp;
925 struct ifaddr *ifa;
926
927 #define equal(a1, a2) \
928 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
929
930 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
931 ifp = TAILQ_NEXT(ifp, if_list)) {
932 if (ifp->if_output == if_nulloutput)
933 continue;
934 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
935 ifa = TAILQ_NEXT(ifa, ifa_list)) {
936 if (ifa->ifa_addr->sa_family != addr->sa_family)
937 continue;
938 if (equal(addr, ifa->ifa_addr))
939 return (ifa);
940 if ((ifp->if_flags & IFF_BROADCAST) &&
941 ifa->ifa_broadaddr &&
942 /* IP6 doesn't have broadcast */
943 ifa->ifa_broadaddr->sa_len != 0 &&
944 equal(ifa->ifa_broadaddr, addr))
945 return (ifa);
946 }
947 }
948 return (NULL);
949 }
950
951 /*
952 * Locate the point to point interface with a given destination address.
953 */
954 /*ARGSUSED*/
955 struct ifaddr *
956 ifa_ifwithdstaddr(addr)
957 const struct sockaddr *addr;
958 {
959 struct ifnet *ifp;
960 struct ifaddr *ifa;
961
962 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
963 ifp = TAILQ_NEXT(ifp, if_list)) {
964 if (ifp->if_output == if_nulloutput)
965 continue;
966 if (ifp->if_flags & IFF_POINTOPOINT) {
967 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
968 ifa = TAILQ_NEXT(ifa, ifa_list)) {
969 if (ifa->ifa_addr->sa_family !=
970 addr->sa_family ||
971 ifa->ifa_dstaddr == NULL)
972 continue;
973 if (equal(addr, ifa->ifa_dstaddr))
974 return (ifa);
975 }
976 }
977 }
978 return (NULL);
979 }
980
981 /*
982 * Find an interface on a specific network. If many, choice
983 * is most specific found.
984 */
985 struct ifaddr *
986 ifa_ifwithnet(addr)
987 const struct sockaddr *addr;
988 {
989 struct ifnet *ifp;
990 struct ifaddr *ifa;
991 const struct sockaddr_dl *sdl;
992 struct ifaddr *ifa_maybe = 0;
993 u_int af = addr->sa_family;
994 char *addr_data = addr->sa_data, *cplim;
995
996 if (af == AF_LINK) {
997 sdl = (struct sockaddr_dl *)addr;
998 if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
999 ifindex2ifnet[sdl->sdl_index] &&
1000 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
1001 return (ifnet_addrs[sdl->sdl_index]);
1002 }
1003 #ifdef NETATALK
1004 if (af == AF_APPLETALK) {
1005 const struct sockaddr_at *sat, *sat2;
1006 sat = (struct sockaddr_at *)addr;
1007 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1008 ifp = TAILQ_NEXT(ifp, if_list)) {
1009 if (ifp->if_output == if_nulloutput)
1010 continue;
1011 ifa = at_ifawithnet((struct sockaddr_at *)addr, ifp);
1012 if (ifa == NULL)
1013 continue;
1014 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1015 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1016 return (ifa); /* exact match */
1017 if (ifa_maybe == NULL) {
1018 /* else keep the if with the right range */
1019 ifa_maybe = ifa;
1020 }
1021 }
1022 return (ifa_maybe);
1023 }
1024 #endif
1025 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1026 ifp = TAILQ_NEXT(ifp, if_list)) {
1027 if (ifp->if_output == if_nulloutput)
1028 continue;
1029 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1030 ifa = TAILQ_NEXT(ifa, ifa_list)) {
1031 char *cp, *cp2, *cp3;
1032
1033 if (ifa->ifa_addr->sa_family != af ||
1034 ifa->ifa_netmask == 0)
1035 next: continue;
1036 cp = addr_data;
1037 cp2 = ifa->ifa_addr->sa_data;
1038 cp3 = ifa->ifa_netmask->sa_data;
1039 cplim = (char *)ifa->ifa_netmask +
1040 ifa->ifa_netmask->sa_len;
1041 while (cp3 < cplim) {
1042 if ((*cp++ ^ *cp2++) & *cp3++) {
1043 /* want to continue for() loop */
1044 goto next;
1045 }
1046 }
1047 if (ifa_maybe == 0 ||
1048 rn_refines((caddr_t)ifa->ifa_netmask,
1049 (caddr_t)ifa_maybe->ifa_netmask))
1050 ifa_maybe = ifa;
1051 }
1052 }
1053 return (ifa_maybe);
1054 }
1055
1056 /*
1057 * Find the interface of the addresss.
1058 */
1059 struct ifaddr *
1060 ifa_ifwithladdr(addr)
1061 const struct sockaddr *addr;
1062 {
1063 struct ifaddr *ia;
1064
1065 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1066 (ia = ifa_ifwithnet(addr)))
1067 return (ia);
1068 return (NULL);
1069 }
1070
1071 /*
1072 * Find an interface using a specific address family
1073 */
1074 struct ifaddr *
1075 ifa_ifwithaf(af)
1076 int af;
1077 {
1078 struct ifnet *ifp;
1079 struct ifaddr *ifa;
1080
1081 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1082 ifp = TAILQ_NEXT(ifp, if_list)) {
1083 if (ifp->if_output == if_nulloutput)
1084 continue;
1085 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1086 ifa = TAILQ_NEXT(ifa, ifa_list)) {
1087 if (ifa->ifa_addr->sa_family == af)
1088 return (ifa);
1089 }
1090 }
1091 return (NULL);
1092 }
1093
1094 /*
1095 * Find an interface address specific to an interface best matching
1096 * a given address.
1097 */
1098 struct ifaddr *
1099 ifaof_ifpforaddr(addr, ifp)
1100 const struct sockaddr *addr;
1101 struct ifnet *ifp;
1102 {
1103 struct ifaddr *ifa;
1104 const char *cp, *cp2, *cp3;
1105 const char *cplim;
1106 struct ifaddr *ifa_maybe = 0;
1107 u_int af = addr->sa_family;
1108
1109 if (ifp->if_output == if_nulloutput)
1110 return (NULL);
1111
1112 if (af >= AF_MAX)
1113 return (NULL);
1114
1115 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1116 ifa = TAILQ_NEXT(ifa, ifa_list)) {
1117 if (ifa->ifa_addr->sa_family != af)
1118 continue;
1119 ifa_maybe = ifa;
1120 if (ifa->ifa_netmask == 0) {
1121 if (equal(addr, ifa->ifa_addr) ||
1122 (ifa->ifa_dstaddr &&
1123 equal(addr, ifa->ifa_dstaddr)))
1124 return (ifa);
1125 continue;
1126 }
1127 cp = addr->sa_data;
1128 cp2 = ifa->ifa_addr->sa_data;
1129 cp3 = ifa->ifa_netmask->sa_data;
1130 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1131 for (; cp3 < cplim; cp3++) {
1132 if ((*cp++ ^ *cp2++) & *cp3)
1133 break;
1134 }
1135 if (cp3 == cplim)
1136 return (ifa);
1137 }
1138 return (ifa_maybe);
1139 }
1140
1141 /*
1142 * Default action when installing a route with a Link Level gateway.
1143 * Lookup an appropriate real ifa to point to.
1144 * This should be moved to /sys/net/link.c eventually.
1145 */
1146 void
1147 link_rtrequest(cmd, rt, info)
1148 int cmd;
1149 struct rtentry *rt;
1150 struct rt_addrinfo *info;
1151 {
1152 struct ifaddr *ifa;
1153 struct sockaddr *dst;
1154 struct ifnet *ifp;
1155
1156 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1157 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1158 return;
1159 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1160 IFAFREE(rt->rt_ifa);
1161 rt->rt_ifa = ifa;
1162 IFAREF(ifa);
1163 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1164 ifa->ifa_rtrequest(cmd, rt, info);
1165 }
1166 }
1167
1168 /*
1169 * Mark an interface down and notify protocols of
1170 * the transition.
1171 * NOTE: must be called at splsoftnet or equivalent.
1172 */
1173 void
1174 if_down(ifp)
1175 struct ifnet *ifp;
1176 {
1177 struct ifaddr *ifa;
1178
1179 ifp->if_flags &= ~IFF_UP;
1180 microtime(&ifp->if_lastchange);
1181 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1182 ifa = TAILQ_NEXT(ifa, ifa_list))
1183 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1184 IFQ_PURGE(&ifp->if_snd);
1185 rt_ifmsg(ifp);
1186 }
1187
1188 /*
1189 * Mark an interface up and notify protocols of
1190 * the transition.
1191 * NOTE: must be called at splsoftnet or equivalent.
1192 */
1193 void
1194 if_up(ifp)
1195 struct ifnet *ifp;
1196 {
1197 #ifdef notyet
1198 struct ifaddr *ifa;
1199 #endif
1200
1201 ifp->if_flags |= IFF_UP;
1202 microtime(&ifp->if_lastchange);
1203 #ifdef notyet
1204 /* this has no effect on IP, and will kill all ISO connections XXX */
1205 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL;
1206 ifa = TAILQ_NEXT(ifa, ifa_list))
1207 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1208 #endif
1209 rt_ifmsg(ifp);
1210 #ifdef INET6
1211 in6_if_up(ifp);
1212 #endif
1213 }
1214
1215 /*
1216 * Handle interface watchdog timer routines. Called
1217 * from softclock, we decrement timers (if set) and
1218 * call the appropriate interface routine on expiration.
1219 */
1220 void
1221 if_slowtimo(arg)
1222 void *arg;
1223 {
1224 struct ifnet *ifp;
1225 int s = splnet();
1226
1227 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1228 ifp = TAILQ_NEXT(ifp, if_list)) {
1229 if (ifp->if_timer == 0 || --ifp->if_timer)
1230 continue;
1231 if (ifp->if_watchdog)
1232 (*ifp->if_watchdog)(ifp);
1233 }
1234 splx(s);
1235 callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ,
1236 if_slowtimo, NULL);
1237 }
1238
1239 /*
1240 * Set/clear promiscuous mode on interface ifp based on the truth value
1241 * of pswitch. The calls are reference counted so that only the first
1242 * "on" request actually has an effect, as does the final "off" request.
1243 * Results are undefined if the "off" and "on" requests are not matched.
1244 */
1245 int
1246 ifpromisc(ifp, pswitch)
1247 struct ifnet *ifp;
1248 int pswitch;
1249 {
1250 int pcount, ret;
1251 short flags;
1252 struct ifreq ifr;
1253
1254 pcount = ifp->if_pcount;
1255 flags = ifp->if_flags;
1256 if (pswitch) {
1257 /*
1258 * Allow the device to be "placed" into promiscuous
1259 * mode even if it is not configured up. It will
1260 * consult IFF_PROMISC when it is is brought up.
1261 */
1262 if (ifp->if_pcount++ != 0)
1263 return (0);
1264 ifp->if_flags |= IFF_PROMISC;
1265 if ((ifp->if_flags & IFF_UP) == 0)
1266 return (0);
1267 } else {
1268 if (--ifp->if_pcount > 0)
1269 return (0);
1270 ifp->if_flags &= ~IFF_PROMISC;
1271 /*
1272 * If the device is not configured up, we should not need to
1273 * turn off promiscuous mode (device should have turned it
1274 * off when interface went down; and will look at IFF_PROMISC
1275 * again next time interface comes up).
1276 */
1277 if ((ifp->if_flags & IFF_UP) == 0)
1278 return (0);
1279 }
1280 memset(&ifr, 0, sizeof(ifr));
1281 ifr.ifr_flags = ifp->if_flags;
1282 ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr);
1283 /* Restore interface state if not successful. */
1284 if (ret != 0) {
1285 ifp->if_pcount = pcount;
1286 ifp->if_flags = flags;
1287 }
1288 return (ret);
1289 }
1290
1291 /*
1292 * Map interface name to
1293 * interface structure pointer.
1294 */
1295 struct ifnet *
1296 ifunit(name)
1297 const char *name;
1298 {
1299 struct ifnet *ifp;
1300 const char *cp = name;
1301 u_int unit = 0;
1302 u_int i;
1303
1304 /*
1305 * If the entire name is a number, treat it as an ifindex.
1306 */
1307 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1308 unit = unit * 10 + (*cp - '0');
1309 }
1310
1311 /*
1312 * If the number took all of the name, then it's a valid ifindex.
1313 */
1314 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1315 if (unit >= if_indexlim)
1316 return (NULL);
1317 ifp = ifindex2ifnet[unit];
1318 if (ifp == NULL || ifp->if_output == if_nulloutput)
1319 return (NULL);
1320 return (ifp);
1321 }
1322
1323 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL;
1324 ifp = TAILQ_NEXT(ifp, if_list)) {
1325 if (ifp->if_output == if_nulloutput)
1326 continue;
1327 if (strcmp(ifp->if_xname, name) == 0)
1328 return (ifp);
1329 }
1330 return (NULL);
1331 }
1332
1333 /*
1334 * Interface ioctls.
1335 */
1336 int
1337 ifioctl(so, cmd, data, p)
1338 struct socket *so;
1339 u_long cmd;
1340 caddr_t data;
1341 struct proc *p;
1342 {
1343 struct ifnet *ifp;
1344 struct ifreq *ifr;
1345 struct ifcapreq *ifcr;
1346 struct ifdatareq *ifdr;
1347 int s, error = 0;
1348 short oif_flags;
1349 int prived_error;
1350
1351 if (p)
1352 prived_error = suser(p->p_ucred, &p->p_acflag);
1353 else
1354 prived_error = 0;
1355
1356 switch (cmd) {
1357
1358 case SIOCGIFCONF:
1359 case OSIOCGIFCONF:
1360 return (ifconf(cmd, data));
1361 }
1362 ifr = (struct ifreq *)data;
1363 ifcr = (struct ifcapreq *)data;
1364 ifdr = (struct ifdatareq *)data;
1365
1366 switch (cmd) {
1367 case SIOCIFCREATE:
1368 case SIOCIFDESTROY:
1369 if (prived_error)
1370 return (prived_error);
1371 return ((cmd == SIOCIFCREATE) ?
1372 if_clone_create(ifr->ifr_name) :
1373 if_clone_destroy(ifr->ifr_name));
1374
1375 case SIOCIFGCLONERS:
1376 return (if_clone_list((struct if_clonereq *)data));
1377 }
1378
1379 ifp = ifunit(ifr->ifr_name);
1380 if (ifp == 0)
1381 return (ENXIO);
1382 oif_flags = ifp->if_flags;
1383 switch (cmd) {
1384
1385 case SIOCGIFFLAGS:
1386 ifr->ifr_flags = ifp->if_flags;
1387 break;
1388
1389 case SIOCGIFMETRIC:
1390 ifr->ifr_metric = ifp->if_metric;
1391 break;
1392
1393 case SIOCGIFMTU:
1394 ifr->ifr_mtu = ifp->if_mtu;
1395 break;
1396
1397 case SIOCGIFDLT:
1398 ifr->ifr_dlt = ifp->if_dlt;
1399 break;
1400
1401 case SIOCSIFFLAGS:
1402 if (prived_error != 0)
1403 return (prived_error);
1404 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1405 s = splnet();
1406 if_down(ifp);
1407 splx(s);
1408 }
1409 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1410 s = splnet();
1411 if_up(ifp);
1412 splx(s);
1413 }
1414 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1415 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1416 if (ifp->if_ioctl)
1417 (void) (*ifp->if_ioctl)(ifp, cmd, data);
1418 break;
1419
1420 case SIOCGIFCAP:
1421 ifcr->ifcr_capabilities = ifp->if_capabilities;
1422 ifcr->ifcr_capenable = ifp->if_capenable;
1423 break;
1424
1425 case SIOCSIFCAP:
1426 if (prived_error != 0)
1427 return (prived_error);
1428 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1429 return (EINVAL);
1430 if (ifp->if_ioctl == NULL)
1431 return (EOPNOTSUPP);
1432
1433 /* Must prevent race with packet reception here. */
1434 s = splnet();
1435 if (ifcr->ifcr_capenable != ifp->if_capenable) {
1436 struct ifreq ifrq;
1437
1438 ifrq.ifr_flags = ifp->if_flags;
1439 ifp->if_capenable = ifcr->ifcr_capenable;
1440
1441 /* Pre-compute the checksum flags mask. */
1442 ifp->if_csum_flags_tx = 0;
1443 ifp->if_csum_flags_rx = 0;
1444 if (ifp->if_capenable & IFCAP_CSUM_IPv4) {
1445 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1446 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1447 }
1448
1449 if (ifp->if_capenable & IFCAP_CSUM_TCPv4) {
1450 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1451 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1452 } else if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx)
1453 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1454
1455 if (ifp->if_capenable & IFCAP_CSUM_UDPv4) {
1456 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1457 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1458 } else if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx)
1459 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1460
1461 if (ifp->if_capenable & IFCAP_CSUM_TCPv6) {
1462 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1463 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1464 }
1465
1466 if (ifp->if_capenable & IFCAP_CSUM_UDPv6) {
1467 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1468 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1469 }
1470
1471 /*
1472 * Only kick the interface if it's up. If it's
1473 * not up now, it will notice the cap enables
1474 * when it is brought up later.
1475 */
1476 if (ifp->if_flags & IFF_UP)
1477 (void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
1478 (caddr_t) &ifrq);
1479 }
1480 splx(s);
1481 break;
1482
1483 case SIOCSIFMETRIC:
1484 if (prived_error != 0)
1485 return (prived_error);
1486 ifp->if_metric = ifr->ifr_metric;
1487 break;
1488
1489 case SIOCGIFDATA:
1490 ifdr->ifdr_data = ifp->if_data;
1491 break;
1492
1493 case SIOCZIFDATA:
1494 if (prived_error != 0)
1495 return (prived_error);
1496 ifdr->ifdr_data = ifp->if_data;
1497 /*
1498 * Assumes that the volatile counters that can be
1499 * zero'ed are at the end of if_data.
1500 */
1501 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1502 offsetof(struct if_data, ifi_ipackets));
1503 break;
1504
1505 case SIOCSIFMTU:
1506 {
1507 u_long oldmtu = ifp->if_mtu;
1508
1509 if (prived_error)
1510 return (prived_error);
1511 if (ifp->if_ioctl == NULL)
1512 return (EOPNOTSUPP);
1513 error = (*ifp->if_ioctl)(ifp, cmd, data);
1514
1515 /*
1516 * If the link MTU changed, do network layer specific procedure.
1517 */
1518 if (ifp->if_mtu != oldmtu) {
1519 #ifdef INET6
1520 nd6_setmtu(ifp);
1521 #endif
1522 }
1523 break;
1524 }
1525 case SIOCSIFPHYADDR:
1526 case SIOCDIFPHYADDR:
1527 #ifdef INET6
1528 case SIOCSIFPHYADDR_IN6:
1529 #endif
1530 case SIOCSLIFPHYADDR:
1531 case SIOCADDMULTI:
1532 case SIOCDELMULTI:
1533 case SIOCSIFMEDIA:
1534 if (prived_error != 0)
1535 return (prived_error);
1536 /* FALLTHROUGH */
1537 case SIOCGIFPSRCADDR:
1538 case SIOCGIFPDSTADDR:
1539 case SIOCGLIFPHYADDR:
1540 case SIOCGIFMEDIA:
1541 if (ifp->if_ioctl == 0)
1542 return (EOPNOTSUPP);
1543 error = (*ifp->if_ioctl)(ifp, cmd, data);
1544 break;
1545
1546 case SIOCSDRVSPEC:
1547 case SIOCS80211NWID:
1548 case SIOCS80211NWKEY:
1549 case SIOCS80211POWER:
1550 case SIOCS80211BSSID:
1551 case SIOCS80211CHANNEL:
1552 /* XXX: need to pass proc pointer through to driver... */
1553 if (prived_error != 0)
1554 return (prived_error);
1555 /* FALLTHROUGH */
1556 default:
1557 if (so->so_proto == 0)
1558 return (EOPNOTSUPP);
1559 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4) && !defined(COMPAT_ULTRIX) && !defined(LKM)
1560 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1561 (struct mbuf *)cmd, (struct mbuf *)data,
1562 (struct mbuf *)ifp, p));
1563 #else
1564 {
1565 int ocmd = cmd;
1566
1567 switch (cmd) {
1568
1569 case SIOCSIFADDR:
1570 case SIOCSIFDSTADDR:
1571 case SIOCSIFBRDADDR:
1572 case SIOCSIFNETMASK:
1573 #if BYTE_ORDER != BIG_ENDIAN
1574 if (ifr->ifr_addr.sa_family == 0 &&
1575 ifr->ifr_addr.sa_len < 16) {
1576 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1577 ifr->ifr_addr.sa_len = 16;
1578 }
1579 #else
1580 if (ifr->ifr_addr.sa_len == 0)
1581 ifr->ifr_addr.sa_len = 16;
1582 #endif
1583 break;
1584
1585 case OSIOCGIFADDR:
1586 cmd = SIOCGIFADDR;
1587 break;
1588
1589 case OSIOCGIFDSTADDR:
1590 cmd = SIOCGIFDSTADDR;
1591 break;
1592
1593 case OSIOCGIFBRDADDR:
1594 cmd = SIOCGIFBRDADDR;
1595 break;
1596
1597 case OSIOCGIFNETMASK:
1598 cmd = SIOCGIFNETMASK;
1599 }
1600
1601 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1602 (struct mbuf *)cmd, (struct mbuf *)data,
1603 (struct mbuf *)ifp, p));
1604
1605 switch (ocmd) {
1606 case OSIOCGIFADDR:
1607 case OSIOCGIFDSTADDR:
1608 case OSIOCGIFBRDADDR:
1609 case OSIOCGIFNETMASK:
1610 *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1611 }
1612 }
1613 #endif /* COMPAT_43 */
1614 break;
1615 }
1616
1617 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1618 #ifdef INET6
1619 if ((ifp->if_flags & IFF_UP) != 0) {
1620 s = splnet();
1621 in6_if_up(ifp);
1622 splx(s);
1623 }
1624 #endif
1625 }
1626
1627 return (error);
1628 }
1629
1630 /*
1631 * Return interface configuration
1632 * of system. List may be used
1633 * in later ioctl's (above) to get
1634 * other information.
1635 */
1636 /*ARGSUSED*/
1637 int
1638 ifconf(cmd, data)
1639 u_long cmd;
1640 caddr_t data;
1641 {
1642 struct ifconf *ifc = (struct ifconf *)data;
1643 struct ifnet *ifp;
1644 struct ifaddr *ifa;
1645 struct ifreq ifr, *ifrp;
1646 int space = ifc->ifc_len, error = 0;
1647 const int sz = (int)sizeof(ifr);
1648 int sign;
1649
1650 if ((ifrp = ifc->ifc_req) == NULL) {
1651 space = 0;
1652 sign = -1;
1653 } else {
1654 sign = 1;
1655 }
1656 TAILQ_FOREACH(ifp, &ifnet, if_list) {
1657 bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ);
1658 if ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) == 0) {
1659 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1660 if (ifrp != NULL && space >= sz) {
1661 error = copyout(&ifr, ifrp, sz);
1662 if (error)
1663 break;
1664 ifrp++;
1665 }
1666 space -= sizeof(ifr) * sign;
1667 continue;
1668 }
1669
1670 for (; ifa != 0; ifa = TAILQ_NEXT(ifa, ifa_list)) {
1671 struct sockaddr *sa = ifa->ifa_addr;
1672 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) || defined(COMPAT_ULTRIX)
1673 if (cmd == OSIOCGIFCONF) {
1674 struct osockaddr *osa =
1675 (struct osockaddr *)&ifr.ifr_addr;
1676 /*
1677 * If it does not fit, we don't bother with it
1678 */
1679 if (sa->sa_len > sizeof(*osa))
1680 continue;
1681 ifr.ifr_addr = *sa;
1682 osa->sa_family = sa->sa_family;
1683 if (ifrp != NULL && space >= sz) {
1684 error = copyout(&ifr, ifrp, sz);
1685 ifrp++;
1686 }
1687 } else
1688 #endif
1689 if (sa->sa_len <= sizeof(*sa)) {
1690 ifr.ifr_addr = *sa;
1691 if (ifrp != NULL && space >= sz) {
1692 error = copyout(&ifr, ifrp, sz);
1693 ifrp++;
1694 }
1695 } else {
1696 space -= (sa->sa_len - sizeof(*sa)) * sign;
1697 if (ifrp != NULL && space >= sz) {
1698 error = copyout(&ifr, ifrp,
1699 sizeof(ifr.ifr_name));
1700 if (error == 0) {
1701 error = copyout(sa,
1702 &ifrp->ifr_addr,
1703 sa->sa_len);
1704 }
1705 ifrp = (struct ifreq *)
1706 (sa->sa_len +
1707 (caddr_t)&ifrp->ifr_addr);
1708 }
1709 }
1710 if (error)
1711 break;
1712 space -= sz * sign;
1713 }
1714 }
1715 if (ifrp != NULL)
1716 ifc->ifc_len -= space;
1717 else
1718 ifc->ifc_len = space;
1719 return (error);
1720 }
1721
1722 #if defined(INET) || defined(INET6)
1723 static void
1724 sysctl_net_ifq_setup(struct sysctllog **clog,
1725 int pf, const char *pfname,
1726 int ipn, const char *ipname,
1727 int qid, struct ifqueue *ifq)
1728 {
1729
1730 sysctl_createv(clog, 0, NULL, NULL,
1731 CTLFLAG_PERMANENT,
1732 CTLTYPE_NODE, "net", NULL,
1733 NULL, 0, NULL, 0,
1734 CTL_NET, CTL_EOL);
1735 sysctl_createv(clog, 0, NULL, NULL,
1736 CTLFLAG_PERMANENT,
1737 CTLTYPE_NODE, pfname, NULL,
1738 NULL, 0, NULL, 0,
1739 CTL_NET, pf, CTL_EOL);
1740 sysctl_createv(clog, 0, NULL, NULL,
1741 CTLFLAG_PERMANENT,
1742 CTLTYPE_NODE, ipname, NULL,
1743 NULL, 0, NULL, 0,
1744 CTL_NET, pf, ipn, CTL_EOL);
1745 sysctl_createv(clog, 0, NULL, NULL,
1746 CTLFLAG_PERMANENT,
1747 CTLTYPE_NODE, "ifq",
1748 SYSCTL_DESCR("Protocol input queue controls"),
1749 NULL, 0, NULL, 0,
1750 CTL_NET, pf, ipn, qid, CTL_EOL);
1751
1752 sysctl_createv(clog, 0, NULL, NULL,
1753 CTLFLAG_PERMANENT,
1754 CTLTYPE_INT, "len",
1755 SYSCTL_DESCR("Current input queue length"),
1756 NULL, 0, &ifq->ifq_len, 0,
1757 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1758 sysctl_createv(clog, 0, NULL, NULL,
1759 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1760 CTLTYPE_INT, "maxlen",
1761 SYSCTL_DESCR("Maximum allowed input queue length"),
1762 NULL, 0, &ifq->ifq_maxlen, 0,
1763 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1764 #ifdef notyet
1765 sysctl_createv(clog, 0, NULL, NULL,
1766 CTLFLAG_PERMANENT,
1767 CTLTYPE_INT, "peak",
1768 SYSCTL_DESCR("Highest input queue length"),
1769 NULL, 0, &ifq->ifq_peak, 0,
1770 CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
1771 #endif
1772 sysctl_createv(clog, 0, NULL, NULL,
1773 CTLFLAG_PERMANENT,
1774 CTLTYPE_INT, "drops",
1775 SYSCTL_DESCR("Packets dropped due to full input queue"),
1776 NULL, 0, &ifq->ifq_drops, 0,
1777 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
1778 }
1779
1780 #ifdef INET
1781 SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
1782 "sysctl net.inet.ip.ifq subtree setup")
1783 {
1784 extern struct ifqueue ipintrq;
1785
1786 sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
1787 IPCTL_IFQ, &ipintrq);
1788 }
1789 #endif /* INET */
1790
1791 #ifdef INET6
1792 SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
1793 "sysctl net.inet6.ip6.ifq subtree setup")
1794 {
1795 extern struct ifqueue ip6intrq;
1796
1797 sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
1798 IPV6CTL_IFQ, &ip6intrq);
1799 }
1800 #endif /* INET6 */
1801 #endif /* INET || INET6 */
1802