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