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