if.c revision 1.192 1 /* $NetBSD: if.c,v 1.192 2007/06/09 03:07:21 dyoung 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.192 2007/06/09 03:07:21 dyoung 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 rtentry *, 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 int s, i, family, purged;
552
553 /*
554 * XXX It's kind of lame that we have to have the
555 * XXX socket structure...
556 */
557 memset(&so, 0, sizeof(so));
558
559 s = splnet();
560
561 /*
562 * Do an if_down() to give protocols a chance to do something.
563 */
564 if_down(ifp);
565
566 #ifdef ALTQ
567 if (ALTQ_IS_ENABLED(&ifp->if_snd))
568 altq_disable(&ifp->if_snd);
569 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
570 altq_detach(&ifp->if_snd);
571 #endif
572
573
574 #if NCARP > 0
575 /* Remove the interface from any carp group it is a part of. */
576 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
577 carp_ifdetach(ifp);
578 #endif
579
580 /*
581 * Rip all the addresses off the interface. This should make
582 * all of the routes go away.
583 *
584 * pr_usrreq calls can remove an arbitrary number of ifaddrs
585 * from the list, including our "cursor", ifa. For safety,
586 * and to honor the TAILQ abstraction, I just restart the
587 * loop after each removal. Note that the loop will exit
588 * when all of the remaining ifaddrs belong to the AF_LINK
589 * family. I am counting on the historical fact that at
590 * least one pr_usrreq in each address domain removes at
591 * least one ifaddr.
592 */
593 again:
594 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
595 family = ifa->ifa_addr->sa_family;
596 #ifdef IFAREF_DEBUG
597 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
598 ifa, family, ifa->ifa_refcnt);
599 if (last_ifa != NULL && ifa == last_ifa)
600 panic("if_detach: loop detected");
601 last_ifa = ifa;
602 #endif
603 if (family == AF_LINK)
604 continue;
605 dp = pffinddomain(family);
606 #ifdef DIAGNOSTIC
607 if (dp == NULL)
608 panic("if_detach: no domain for AF %d",
609 family);
610 #endif
611 /*
612 * XXX These PURGEIF calls are redundant with the
613 * purge-all-families calls below, but are left in for
614 * now both to make a smaller change, and to avoid
615 * unplanned interactions with clearing of
616 * ifp->if_addrlist.
617 */
618 purged = 0;
619 for (pr = dp->dom_protosw;
620 pr < dp->dom_protoswNPROTOSW; pr++) {
621 so.so_proto = pr;
622 if (pr->pr_usrreq != NULL) {
623 (void) (*pr->pr_usrreq)(&so,
624 PRU_PURGEIF, NULL, NULL,
625 (struct mbuf *) ifp, curlwp);
626 purged = 1;
627 }
628 }
629 if (purged == 0) {
630 /*
631 * XXX What's really the best thing to do
632 * XXX here? --thorpej (at) NetBSD.org
633 */
634 printf("if_detach: WARNING: AF %d not purged\n",
635 family);
636 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
637 }
638 goto again;
639 }
640
641 if_free_sadl(ifp);
642
643 /* Walk the routing table looking for stragglers. */
644 for (i = 0; i <= AF_MAX; i++)
645 (void)rt_walktree(i, if_rt_walktree, ifp);
646
647 DOMAIN_FOREACH(dp) {
648 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
649 (*dp->dom_ifdetach)(ifp,
650 ifp->if_afdata[dp->dom_family]);
651
652 /*
653 * One would expect multicast memberships (INET and
654 * INET6) on UDP sockets to be purged by the PURGEIF
655 * calls above, but if all addresses were removed from
656 * the interface prior to destruction, the calls will
657 * not be made (e.g. ppp, for which pppd(8) generally
658 * removes addresses before destroying the interface).
659 * Because there is no invariant that multicast
660 * memberships only exist for interfaces with IPv4
661 * addresses, we must call PURGEIF regardless of
662 * addresses. (Protocols which might store ifnet
663 * pointers are marked with PR_PURGEIF.)
664 */
665 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
666 so.so_proto = pr;
667 if (pr->pr_usrreq != NULL && pr->pr_flags & PR_PURGEIF)
668 (void)(*pr->pr_usrreq)(&so, PRU_PURGEIF, NULL,
669 NULL, (struct mbuf *)ifp, curlwp);
670 }
671 }
672
673 #ifdef PFIL_HOOKS
674 (void)pfil_run_hooks(&if_pfil,
675 (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
676 (void)pfil_head_unregister(&ifp->if_pfil);
677 #endif
678
679 /* Announce that the interface is gone. */
680 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
681
682 ifindex2ifnet[ifp->if_index] = NULL;
683
684 TAILQ_REMOVE(&ifnet, ifp, if_list);
685
686 /*
687 * remove packets that came from ifp, from software interrupt queues.
688 */
689 DOMAIN_FOREACH(dp) {
690 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
691 if (dp->dom_ifqueues[i] == NULL)
692 break;
693 if_detach_queues(ifp, dp->dom_ifqueues[i]);
694 }
695 }
696
697 splx(s);
698 }
699
700 static void
701 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
702 {
703 struct mbuf *m, *prev, *next;
704
705 prev = NULL;
706 for (m = q->ifq_head; m != NULL; m = next) {
707 next = m->m_nextpkt;
708 #ifdef DIAGNOSTIC
709 if ((m->m_flags & M_PKTHDR) == 0) {
710 prev = m;
711 continue;
712 }
713 #endif
714 if (m->m_pkthdr.rcvif != ifp) {
715 prev = m;
716 continue;
717 }
718
719 if (prev != NULL)
720 prev->m_nextpkt = m->m_nextpkt;
721 else
722 q->ifq_head = m->m_nextpkt;
723 if (q->ifq_tail == m)
724 q->ifq_tail = prev;
725 q->ifq_len--;
726
727 m->m_nextpkt = NULL;
728 m_freem(m);
729 IF_DROP(q);
730 }
731 }
732
733 /*
734 * Callback for a radix tree walk to delete all references to an
735 * ifnet.
736 */
737 static int
738 if_rt_walktree(struct rtentry *rt, void *v)
739 {
740 struct ifnet *ifp = (struct ifnet *)v;
741 int error;
742
743 if (rt->rt_ifp != ifp)
744 return 0;
745
746 /* Delete the entry. */
747 ++rt->rt_refcnt;
748 error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
749 rt_mask(rt), rt->rt_flags, NULL);
750 KASSERT((rt->rt_flags & RTF_UP) == 0);
751 rt->rt_ifp = NULL;
752 RTFREE(rt);
753 if (error != 0)
754 printf("%s: warning: unable to delete rtentry @ %p, "
755 "error = %d\n", ifp->if_xname, rt, error);
756 return 0;
757 }
758
759 /*
760 * Create a clone network interface.
761 */
762 int
763 if_clone_create(const char *name)
764 {
765 struct if_clone *ifc;
766 int unit;
767
768 ifc = if_clone_lookup(name, &unit);
769 if (ifc == NULL)
770 return EINVAL;
771
772 if (ifunit(name) != NULL)
773 return EEXIST;
774
775 return (*ifc->ifc_create)(ifc, unit);
776 }
777
778 /*
779 * Destroy a clone network interface.
780 */
781 int
782 if_clone_destroy(const char *name)
783 {
784 struct if_clone *ifc;
785 struct ifnet *ifp;
786
787 ifc = if_clone_lookup(name, NULL);
788 if (ifc == NULL)
789 return EINVAL;
790
791 ifp = ifunit(name);
792 if (ifp == NULL)
793 return ENXIO;
794
795 if (ifc->ifc_destroy == NULL)
796 return EOPNOTSUPP;
797
798 return (*ifc->ifc_destroy)(ifp);
799 }
800
801 /*
802 * Look up a network interface cloner.
803 */
804 static struct if_clone *
805 if_clone_lookup(const char *name, int *unitp)
806 {
807 struct if_clone *ifc;
808 const char *cp;
809 int unit;
810
811 /* separate interface name from unit */
812 for (cp = name;
813 cp - name < IFNAMSIZ && *cp && (*cp < '0' || *cp > '9');
814 cp++)
815 continue;
816
817 if (cp == name || cp - name == IFNAMSIZ || !*cp)
818 return NULL; /* No name or unit number */
819
820 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
821 if (strlen(ifc->ifc_name) == cp - name &&
822 strncmp(name, ifc->ifc_name, cp - name) == 0)
823 break;
824 }
825
826 if (ifc == NULL)
827 return NULL;
828
829 unit = 0;
830 while (cp - name < IFNAMSIZ && *cp) {
831 if (*cp < '0' || *cp > '9' || unit > INT_MAX / 10) {
832 /* Bogus unit number. */
833 return NULL;
834 }
835 unit = (unit * 10) + (*cp++ - '0');
836 }
837
838 if (unitp != NULL)
839 *unitp = unit;
840 return ifc;
841 }
842
843 /*
844 * Register a network interface cloner.
845 */
846 void
847 if_clone_attach(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(struct if_clone *ifc)
859 {
860
861 LIST_REMOVE(ifc, ifc_list);
862 if_cloners_count--;
863 }
864
865 /*
866 * Provide list of interface cloners to userspace.
867 */
868 static int
869 if_clone_list(struct if_clonereq *ifcr)
870 {
871 char outbuf[IFNAMSIZ], *dst;
872 struct if_clone *ifc;
873 int count, error = 0;
874
875 ifcr->ifcr_total = if_cloners_count;
876 if ((dst = ifcr->ifcr_buffer) == NULL) {
877 /* Just asking how many there are. */
878 return 0;
879 }
880
881 if (ifcr->ifcr_count < 0)
882 return EINVAL;
883
884 count = (if_cloners_count < ifcr->ifcr_count) ?
885 if_cloners_count : ifcr->ifcr_count;
886
887 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
888 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
889 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
890 if (outbuf[sizeof(outbuf) - 1] != '\0')
891 return ENAMETOOLONG;
892 error = copyout(outbuf, dst, sizeof(outbuf));
893 if (error != 0)
894 break;
895 }
896
897 return error;
898 }
899
900 /*
901 * Locate an interface based on a complete address.
902 */
903 /*ARGSUSED*/
904 struct ifaddr *
905 ifa_ifwithaddr(const struct sockaddr *addr)
906 {
907 struct ifnet *ifp;
908 struct ifaddr *ifa;
909
910 #define equal(a1, a2) \
911 (memcmp((a1), (a2), ((const struct sockaddr *)(a1))->sa_len) == 0)
912
913 IFNET_FOREACH(ifp) {
914 if (ifp->if_output == if_nulloutput)
915 continue;
916 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
917 if (ifa->ifa_addr->sa_family != addr->sa_family)
918 continue;
919 if (equal(addr, ifa->ifa_addr))
920 return ifa;
921 if ((ifp->if_flags & IFF_BROADCAST) &&
922 ifa->ifa_broadaddr &&
923 /* IP6 doesn't have broadcast */
924 ifa->ifa_broadaddr->sa_len != 0 &&
925 equal(ifa->ifa_broadaddr, addr))
926 return ifa;
927 }
928 }
929 return NULL;
930 }
931
932 /*
933 * Locate the point to point interface with a given destination address.
934 */
935 /*ARGSUSED*/
936 struct ifaddr *
937 ifa_ifwithdstaddr(const struct sockaddr *addr)
938 {
939 struct ifnet *ifp;
940 struct ifaddr *ifa;
941
942 IFNET_FOREACH(ifp) {
943 if (ifp->if_output == if_nulloutput)
944 continue;
945 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
946 continue;
947 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
948 if (ifa->ifa_addr->sa_family != addr->sa_family ||
949 ifa->ifa_dstaddr == NULL)
950 continue;
951 if (equal(addr, ifa->ifa_dstaddr))
952 return ifa;
953 }
954 }
955 return NULL;
956 }
957
958 /*
959 * Find an interface on a specific network. If many, choice
960 * is most specific found.
961 */
962 struct ifaddr *
963 ifa_ifwithnet(const struct sockaddr *addr)
964 {
965 struct ifnet *ifp;
966 struct ifaddr *ifa;
967 const struct sockaddr_dl *sdl;
968 struct ifaddr *ifa_maybe = 0;
969 u_int af = addr->sa_family;
970 const char *addr_data = addr->sa_data, *cplim;
971
972 if (af == AF_LINK) {
973 sdl = (const struct sockaddr_dl *)addr;
974 if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
975 ifindex2ifnet[sdl->sdl_index] &&
976 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput)
977 return ifnet_addrs[sdl->sdl_index];
978 }
979 #ifdef NETATALK
980 if (af == AF_APPLETALK) {
981 const struct sockaddr_at *sat, *sat2;
982 sat = (const struct sockaddr_at *)addr;
983 IFNET_FOREACH(ifp) {
984 if (ifp->if_output == if_nulloutput)
985 continue;
986 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
987 if (ifa == NULL)
988 continue;
989 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
990 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
991 return ifa; /* exact match */
992 if (ifa_maybe == NULL) {
993 /* else keep the if with the right range */
994 ifa_maybe = ifa;
995 }
996 }
997 return ifa_maybe;
998 }
999 #endif
1000 IFNET_FOREACH(ifp) {
1001 if (ifp->if_output == if_nulloutput)
1002 continue;
1003 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1004 const char *cp, *cp2, *cp3;
1005
1006 if (ifa->ifa_addr->sa_family != af ||
1007 ifa->ifa_netmask == NULL)
1008 next: continue;
1009 cp = addr_data;
1010 cp2 = ifa->ifa_addr->sa_data;
1011 cp3 = ifa->ifa_netmask->sa_data;
1012 cplim = (const char *)ifa->ifa_netmask +
1013 ifa->ifa_netmask->sa_len;
1014 while (cp3 < cplim) {
1015 if ((*cp++ ^ *cp2++) & *cp3++) {
1016 /* want to continue for() loop */
1017 goto next;
1018 }
1019 }
1020 if (ifa_maybe == NULL ||
1021 rn_refines((void *)ifa->ifa_netmask,
1022 (void *)ifa_maybe->ifa_netmask))
1023 ifa_maybe = ifa;
1024 }
1025 }
1026 return ifa_maybe;
1027 }
1028
1029 /*
1030 * Find the interface of the addresss.
1031 */
1032 struct ifaddr *
1033 ifa_ifwithladdr(const struct sockaddr *addr)
1034 {
1035 struct ifaddr *ia;
1036
1037 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1038 (ia = ifa_ifwithnet(addr)))
1039 return ia;
1040 return NULL;
1041 }
1042
1043 /*
1044 * Find an interface using a specific address family
1045 */
1046 struct ifaddr *
1047 ifa_ifwithaf(int af)
1048 {
1049 struct ifnet *ifp;
1050 struct ifaddr *ifa;
1051
1052 IFNET_FOREACH(ifp) {
1053 if (ifp->if_output == if_nulloutput)
1054 continue;
1055 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1056 if (ifa->ifa_addr->sa_family == af)
1057 return ifa;
1058 }
1059 }
1060 return NULL;
1061 }
1062
1063 /*
1064 * Find an interface address specific to an interface best matching
1065 * a given address.
1066 */
1067 struct ifaddr *
1068 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1069 {
1070 struct ifaddr *ifa;
1071 const char *cp, *cp2, *cp3;
1072 const char *cplim;
1073 struct ifaddr *ifa_maybe = 0;
1074 u_int af = addr->sa_family;
1075
1076 if (ifp->if_output == if_nulloutput)
1077 return NULL;
1078
1079 if (af >= AF_MAX)
1080 return NULL;
1081
1082 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1083 if (ifa->ifa_addr->sa_family != af)
1084 continue;
1085 ifa_maybe = ifa;
1086 if (ifa->ifa_netmask == NULL) {
1087 if (equal(addr, ifa->ifa_addr) ||
1088 (ifa->ifa_dstaddr &&
1089 equal(addr, ifa->ifa_dstaddr)))
1090 return ifa;
1091 continue;
1092 }
1093 cp = addr->sa_data;
1094 cp2 = ifa->ifa_addr->sa_data;
1095 cp3 = ifa->ifa_netmask->sa_data;
1096 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1097 for (; cp3 < cplim; cp3++) {
1098 if ((*cp++ ^ *cp2++) & *cp3)
1099 break;
1100 }
1101 if (cp3 == cplim)
1102 return ifa;
1103 }
1104 return ifa_maybe;
1105 }
1106
1107 /*
1108 * Default action when installing a route with a Link Level gateway.
1109 * Lookup an appropriate real ifa to point to.
1110 * This should be moved to /sys/net/link.c eventually.
1111 */
1112 void
1113 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1114 {
1115 struct ifaddr *ifa;
1116 struct sockaddr *dst;
1117 struct ifnet *ifp;
1118
1119 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == NULL) ||
1120 ((ifp = ifa->ifa_ifp) == NULL) || ((dst = rt_key(rt)) == NULL))
1121 return;
1122 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1123 rt_replace_ifa(rt, ifa);
1124 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1125 ifa->ifa_rtrequest(cmd, rt, info);
1126 }
1127 }
1128
1129 /*
1130 * Handle a change in the interface link state.
1131 */
1132 void
1133 if_link_state_change(struct ifnet *ifp, int link_state)
1134 {
1135 if (ifp->if_link_state == link_state)
1136 return;
1137 ifp->if_link_state = link_state;
1138 /* Notify that the link state has changed. */
1139 rt_ifmsg(ifp);
1140 #if NCARP > 0
1141 if (ifp->if_carp)
1142 carp_carpdev_state(ifp);
1143 #endif
1144 }
1145
1146 /*
1147 * Mark an interface down and notify protocols of
1148 * the transition.
1149 * NOTE: must be called at splsoftnet or equivalent.
1150 */
1151 void
1152 if_down(struct ifnet *ifp)
1153 {
1154 struct ifaddr *ifa;
1155
1156 ifp->if_flags &= ~IFF_UP;
1157 microtime(&ifp->if_lastchange);
1158 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1159 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1160 IFQ_PURGE(&ifp->if_snd);
1161 #if NCARP > 0
1162 if (ifp->if_carp)
1163 carp_carpdev_state(ifp);
1164 #endif
1165 rt_ifmsg(ifp);
1166 }
1167
1168 /*
1169 * Mark an interface up and notify protocols of
1170 * the transition.
1171 * NOTE: must be called at splsoftnet or equivalent.
1172 */
1173 void
1174 if_up(struct ifnet *ifp)
1175 {
1176 #ifdef notyet
1177 struct ifaddr *ifa;
1178 #endif
1179
1180 ifp->if_flags |= IFF_UP;
1181 microtime(&ifp->if_lastchange);
1182 #ifdef notyet
1183 /* this has no effect on IP, and will kill all ISO connections XXX */
1184 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
1185 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1186 #endif
1187 #if NCARP > 0
1188 if (ifp->if_carp)
1189 carp_carpdev_state(ifp);
1190 #endif
1191 rt_ifmsg(ifp);
1192 #ifdef INET6
1193 in6_if_up(ifp);
1194 #endif
1195 }
1196
1197 /*
1198 * Handle interface watchdog timer routines. Called
1199 * from softclock, we decrement timers (if set) and
1200 * call the appropriate interface routine on expiration.
1201 */
1202 void
1203 if_slowtimo(void *arg)
1204 {
1205 struct ifnet *ifp;
1206 int s = splnet();
1207
1208 IFNET_FOREACH(ifp) {
1209 if (ifp->if_timer == 0 || --ifp->if_timer)
1210 continue;
1211 if (ifp->if_watchdog != NULL)
1212 (*ifp->if_watchdog)(ifp);
1213 }
1214 splx(s);
1215 callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1216 }
1217
1218 /*
1219 * Set/clear promiscuous mode on interface ifp based on the truth value
1220 * of pswitch. The calls are reference counted so that only the first
1221 * "on" request actually has an effect, as does the final "off" request.
1222 * Results are undefined if the "off" and "on" requests are not matched.
1223 */
1224 int
1225 ifpromisc(struct ifnet *ifp, int pswitch)
1226 {
1227 int pcount, ret;
1228 short flags;
1229 struct ifreq ifr;
1230
1231 pcount = ifp->if_pcount;
1232 flags = ifp->if_flags;
1233 if (pswitch) {
1234 /*
1235 * Allow the device to be "placed" into promiscuous
1236 * mode even if it is not configured up. It will
1237 * consult IFF_PROMISC when it is is brought up.
1238 */
1239 if (ifp->if_pcount++ != 0)
1240 return 0;
1241 ifp->if_flags |= IFF_PROMISC;
1242 if ((ifp->if_flags & IFF_UP) == 0)
1243 return 0;
1244 } else {
1245 if (--ifp->if_pcount > 0)
1246 return 0;
1247 ifp->if_flags &= ~IFF_PROMISC;
1248 /*
1249 * If the device is not configured up, we should not need to
1250 * turn off promiscuous mode (device should have turned it
1251 * off when interface went down; and will look at IFF_PROMISC
1252 * again next time interface comes up).
1253 */
1254 if ((ifp->if_flags & IFF_UP) == 0)
1255 return 0;
1256 }
1257 memset(&ifr, 0, sizeof(ifr));
1258 ifr.ifr_flags = ifp->if_flags;
1259 ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (void *) &ifr);
1260 /* Restore interface state if not successful. */
1261 if (ret != 0) {
1262 ifp->if_pcount = pcount;
1263 ifp->if_flags = flags;
1264 }
1265 return ret;
1266 }
1267
1268 /*
1269 * Map interface name to
1270 * interface structure pointer.
1271 */
1272 struct ifnet *
1273 ifunit(const char *name)
1274 {
1275 struct ifnet *ifp;
1276 const char *cp = name;
1277 u_int unit = 0;
1278 u_int i;
1279
1280 /*
1281 * If the entire name is a number, treat it as an ifindex.
1282 */
1283 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1284 unit = unit * 10 + (*cp - '0');
1285 }
1286
1287 /*
1288 * If the number took all of the name, then it's a valid ifindex.
1289 */
1290 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1291 if (unit >= if_indexlim)
1292 return NULL;
1293 ifp = ifindex2ifnet[unit];
1294 if (ifp == NULL || ifp->if_output == if_nulloutput)
1295 return NULL;
1296 return ifp;
1297 }
1298
1299 IFNET_FOREACH(ifp) {
1300 if (ifp->if_output == if_nulloutput)
1301 continue;
1302 if (strcmp(ifp->if_xname, name) == 0)
1303 return ifp;
1304 }
1305 return NULL;
1306 }
1307
1308 /*
1309 * Interface ioctls.
1310 */
1311 int
1312 ifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1313 {
1314 struct ifnet *ifp;
1315 struct ifreq *ifr;
1316 struct ifcapreq *ifcr;
1317 struct ifdatareq *ifdr;
1318 int s, error = 0;
1319 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
1320 u_long ocmd = cmd;
1321 #endif
1322 short oif_flags;
1323 #ifdef COMPAT_OIFREQ
1324 struct ifreq ifrb;
1325 struct oifreq *oifr = NULL;
1326 #endif
1327
1328 switch (cmd) {
1329 #ifdef COMPAT_OIFREQ
1330 case OSIOCGIFCONF:
1331 case OOSIOCGIFCONF:
1332 return compat_ifconf(cmd, data);
1333 #endif
1334 case SIOCGIFCONF:
1335 return ifconf(cmd, data);
1336 }
1337
1338 #ifdef COMPAT_OIFREQ
1339 cmd = cvtcmd(cmd);
1340 if (cmd != ocmd) {
1341 oifr = data;
1342 data = ifr = &ifrb;
1343 ifreqo2n(oifr, ifr);
1344 } else
1345 #endif
1346 ifr = data;
1347 ifcr = data;
1348 ifdr = data;
1349
1350 ifp = ifunit(ifr->ifr_name);
1351
1352 switch (cmd) {
1353 case SIOCIFCREATE:
1354 case SIOCIFDESTROY:
1355 if (l != NULL) {
1356 error = kauth_authorize_network(l->l_cred,
1357 KAUTH_NETWORK_INTERFACE,
1358 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1359 (void *)cmd, NULL);
1360 if (error != 0)
1361 return error;
1362 }
1363 return (cmd == SIOCIFCREATE) ?
1364 if_clone_create(ifr->ifr_name) :
1365 if_clone_destroy(ifr->ifr_name);
1366
1367 case SIOCIFGCLONERS:
1368 return if_clone_list((struct if_clonereq *)data);
1369 }
1370
1371 if (ifp == NULL)
1372 return ENXIO;
1373
1374 switch (cmd) {
1375 case SIOCSIFFLAGS:
1376 case SIOCSIFCAP:
1377 case SIOCSIFMETRIC:
1378 case SIOCZIFDATA:
1379 case SIOCSIFMTU:
1380 case SIOCSIFPHYADDR:
1381 case SIOCDIFPHYADDR:
1382 #ifdef INET6
1383 case SIOCSIFPHYADDR_IN6:
1384 #endif
1385 case SIOCSLIFPHYADDR:
1386 case SIOCADDMULTI:
1387 case SIOCDELMULTI:
1388 case SIOCSIFMEDIA:
1389 case SIOCSDRVSPEC:
1390 case SIOCS80211NWID:
1391 case SIOCS80211NWKEY:
1392 case SIOCS80211POWER:
1393 case SIOCS80211BSSID:
1394 case SIOCS80211CHANNEL:
1395 if (l != NULL) {
1396 error = kauth_authorize_network(l->l_cred,
1397 KAUTH_NETWORK_INTERFACE,
1398 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1399 (void *)cmd, NULL);
1400 if (error != 0)
1401 return error;
1402 }
1403 }
1404
1405 oif_flags = ifp->if_flags;
1406 switch (cmd) {
1407
1408 case SIOCGIFFLAGS:
1409 ifr->ifr_flags = ifp->if_flags;
1410 break;
1411
1412 case SIOCGIFMETRIC:
1413 ifr->ifr_metric = ifp->if_metric;
1414 break;
1415
1416 case SIOCGIFMTU:
1417 ifr->ifr_mtu = ifp->if_mtu;
1418 break;
1419
1420 case SIOCGIFDLT:
1421 ifr->ifr_dlt = ifp->if_dlt;
1422 break;
1423
1424 case SIOCSIFFLAGS:
1425 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1426 s = splnet();
1427 if_down(ifp);
1428 splx(s);
1429 }
1430 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1431 s = splnet();
1432 if_up(ifp);
1433 splx(s);
1434 }
1435 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1436 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1437 if (ifp->if_ioctl)
1438 (void)(*ifp->if_ioctl)(ifp, cmd, data);
1439 break;
1440
1441 case SIOCGIFCAP:
1442 ifcr->ifcr_capabilities = ifp->if_capabilities;
1443 ifcr->ifcr_capenable = ifp->if_capenable;
1444 break;
1445
1446 case SIOCSIFCAP:
1447 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1448 return EINVAL;
1449 if (ifp->if_ioctl == NULL)
1450 return EOPNOTSUPP;
1451
1452 /* Must prevent race with packet reception here. */
1453 s = splnet();
1454 if (ifcr->ifcr_capenable != ifp->if_capenable) {
1455 struct ifreq ifrq;
1456
1457 ifrq.ifr_flags = ifp->if_flags;
1458 ifp->if_capenable = ifcr->ifcr_capenable;
1459
1460 /* Pre-compute the checksum flags mask. */
1461 ifp->if_csum_flags_tx = 0;
1462 ifp->if_csum_flags_rx = 0;
1463 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1464 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1465 }
1466 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1467 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1468 }
1469
1470 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1471 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1472 }
1473 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1474 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1475 }
1476
1477 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1478 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1479 }
1480 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1481 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1482 }
1483
1484 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1485 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1486 }
1487 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1488 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1489 }
1490
1491 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1492 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1493 }
1494 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1495 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1496 }
1497
1498 /*
1499 * Only kick the interface if it's up. If it's
1500 * not up now, it will notice the cap enables
1501 * when it is brought up later.
1502 */
1503 if (ifp->if_flags & IFF_UP)
1504 (void)(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS,
1505 (void *)&ifrq);
1506 }
1507 splx(s);
1508 break;
1509
1510 case SIOCSIFMETRIC:
1511 ifp->if_metric = ifr->ifr_metric;
1512 break;
1513
1514 case SIOCGIFDATA:
1515 ifdr->ifdr_data = ifp->if_data;
1516 break;
1517
1518 case SIOCZIFDATA:
1519 ifdr->ifdr_data = ifp->if_data;
1520 /*
1521 * Assumes that the volatile counters that can be
1522 * zero'ed are at the end of if_data.
1523 */
1524 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1525 offsetof(struct if_data, ifi_ipackets));
1526 break;
1527
1528 case SIOCSIFMTU:
1529 {
1530 u_long oldmtu = ifp->if_mtu;
1531
1532 if (ifp->if_ioctl == NULL)
1533 return EOPNOTSUPP;
1534 error = (*ifp->if_ioctl)(ifp, cmd, data);
1535
1536 /*
1537 * If the link MTU changed, do network layer specific procedure.
1538 */
1539 if (ifp->if_mtu != oldmtu) {
1540 #ifdef INET6
1541 nd6_setmtu(ifp);
1542 #endif
1543 }
1544 break;
1545 }
1546 case SIOCSIFPHYADDR:
1547 case SIOCDIFPHYADDR:
1548 #ifdef INET6
1549 case SIOCSIFPHYADDR_IN6:
1550 #endif
1551 case SIOCSLIFPHYADDR:
1552 case SIOCADDMULTI:
1553 case SIOCDELMULTI:
1554 case SIOCSIFMEDIA:
1555 case SIOCGIFPSRCADDR:
1556 case SIOCGIFPDSTADDR:
1557 case SIOCGLIFPHYADDR:
1558 case SIOCGIFMEDIA:
1559 if (ifp->if_ioctl == NULL)
1560 return EOPNOTSUPP;
1561 error = (*ifp->if_ioctl)(ifp, cmd, data);
1562 break;
1563
1564 case SIOCSDRVSPEC:
1565 case SIOCS80211NWID:
1566 case SIOCS80211NWKEY:
1567 case SIOCS80211POWER:
1568 case SIOCS80211BSSID:
1569 case SIOCS80211CHANNEL:
1570 default:
1571 if (so->so_proto == NULL)
1572 return EOPNOTSUPP;
1573 #ifdef COMPAT_OSOCK
1574 error = compat_ifioctl(so, ocmd, cmd, data, l);
1575 #else
1576 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
1577 (struct mbuf *)cmd, (struct mbuf *)data,
1578 (struct mbuf *)ifp, l));
1579 #endif
1580 break;
1581 }
1582
1583 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
1584 #ifdef INET6
1585 if ((ifp->if_flags & IFF_UP) != 0) {
1586 s = splnet();
1587 in6_if_up(ifp);
1588 splx(s);
1589 }
1590 #endif
1591 }
1592 #ifdef COMPAT_OIFREQ
1593 if (cmd != ocmd)
1594 ifreqn2o(oifr, ifr);
1595 #endif
1596
1597 return error;
1598 }
1599
1600 /*
1601 * Return interface configuration
1602 * of system. List may be used
1603 * in later ioctl's (above) to get
1604 * other information.
1605 */
1606 /*ARGSUSED*/
1607 int
1608 ifconf(u_long cmd, void *data)
1609 {
1610 struct ifconf *ifc = (struct ifconf *)data;
1611 struct ifnet *ifp;
1612 struct ifaddr *ifa;
1613 struct ifreq ifr, *ifrp;
1614 int space, error = 0;
1615 const int sz = offsetof(struct ifreq, ifr_ifru) +
1616 sizeof(struct sockaddr);
1617
1618 if ((ifrp = ifc->ifc_req) == NULL)
1619 space = 0;
1620 else
1621 space = ifc->ifc_len;
1622 IFNET_FOREACH(ifp) {
1623 (void)strncpy(ifr.ifr_name, ifp->if_xname,
1624 sizeof(ifr.ifr_name));
1625 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
1626 return ENAMETOOLONG;
1627 if (TAILQ_EMPTY(&ifp->if_addrlist)) {
1628 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
1629 if (space >= sz) {
1630 error = copyout(&ifr, ifrp, sz);
1631 if (error != 0)
1632 return (error);
1633 ifrp++;
1634 }
1635 space -= sizeof(struct ifreq);
1636 continue;
1637 }
1638
1639 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1640 struct sockaddr *sa = ifa->ifa_addr;
1641 if (sa->sa_len <= sizeof(*sa)) {
1642 ifr.ifr_addr = *sa;
1643 if (space >= sz) {
1644 error = copyout(&ifr, ifrp, sz);
1645 ifrp++;
1646 }
1647 space -= sizeof(struct ifreq);
1648 } else {
1649 space -= sa->sa_len - sizeof(*sa) + sz;
1650 if (space < 0)
1651 continue;
1652 error = copyout(&ifr, ifrp,
1653 sizeof(ifr.ifr_name));
1654 if (error == 0)
1655 error = copyout(sa,
1656 &ifrp->ifr_addr, sa->sa_len);
1657 ifrp = (struct ifreq *)
1658 (sa->sa_len + (char *)&ifrp->ifr_addr);
1659 }
1660 if (error != 0)
1661 return (error);
1662 }
1663 }
1664 if (ifrp != NULL)
1665 ifc->ifc_len -= space;
1666 else
1667 ifc->ifc_len = -space;
1668 return (0);
1669 }
1670
1671 /*
1672 * Queue message on interface, and start output if interface
1673 * not yet active.
1674 */
1675 int
1676 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
1677 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1678 {
1679 int len = m->m_pkthdr.len;
1680 int mflags = m->m_flags;
1681 int s = splnet();
1682 int error;
1683
1684 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1685 if (error != 0)
1686 goto out;
1687 ifp->if_obytes += len;
1688 if (mflags & M_MCAST)
1689 ifp->if_omcasts++;
1690 if ((ifp->if_flags & IFF_OACTIVE) == 0)
1691 (*ifp->if_start)(ifp);
1692 out:
1693 splx(s);
1694 return error;
1695 }
1696
1697 /*
1698 * Queue message on interface, possibly using a second fast queue
1699 */
1700 int
1701 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
1702 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
1703 {
1704 int error = 0;
1705
1706 if (ifq != NULL
1707 #ifdef ALTQ
1708 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
1709 #endif
1710 ) {
1711 if (IF_QFULL(ifq)) {
1712 IF_DROP(&ifp->if_snd);
1713 m_freem(m);
1714 if (error == 0)
1715 error = ENOBUFS;
1716 } else
1717 IF_ENQUEUE(ifq, m);
1718 } else
1719 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
1720 if (error != 0) {
1721 ++ifp->if_oerrors;
1722 return error;
1723 }
1724 return 0;
1725 }
1726
1727
1728 #if defined(INET) || defined(INET6)
1729 static void
1730 sysctl_net_ifq_setup(struct sysctllog **clog,
1731 int pf, const char *pfname,
1732 int ipn, const char *ipname,
1733 int qid, struct ifqueue *ifq)
1734 {
1735
1736 sysctl_createv(clog, 0, NULL, NULL,
1737 CTLFLAG_PERMANENT,
1738 CTLTYPE_NODE, "net", NULL,
1739 NULL, 0, NULL, 0,
1740 CTL_NET, CTL_EOL);
1741 sysctl_createv(clog, 0, NULL, NULL,
1742 CTLFLAG_PERMANENT,
1743 CTLTYPE_NODE, pfname, NULL,
1744 NULL, 0, NULL, 0,
1745 CTL_NET, pf, CTL_EOL);
1746 sysctl_createv(clog, 0, NULL, NULL,
1747 CTLFLAG_PERMANENT,
1748 CTLTYPE_NODE, ipname, NULL,
1749 NULL, 0, NULL, 0,
1750 CTL_NET, pf, ipn, CTL_EOL);
1751 sysctl_createv(clog, 0, NULL, NULL,
1752 CTLFLAG_PERMANENT,
1753 CTLTYPE_NODE, "ifq",
1754 SYSCTL_DESCR("Protocol input queue controls"),
1755 NULL, 0, NULL, 0,
1756 CTL_NET, pf, ipn, qid, CTL_EOL);
1757
1758 sysctl_createv(clog, 0, NULL, NULL,
1759 CTLFLAG_PERMANENT,
1760 CTLTYPE_INT, "len",
1761 SYSCTL_DESCR("Current input queue length"),
1762 NULL, 0, &ifq->ifq_len, 0,
1763 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
1764 sysctl_createv(clog, 0, NULL, NULL,
1765 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1766 CTLTYPE_INT, "maxlen",
1767 SYSCTL_DESCR("Maximum allowed input queue length"),
1768 NULL, 0, &ifq->ifq_maxlen, 0,
1769 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
1770 #ifdef notyet
1771 sysctl_createv(clog, 0, NULL, NULL,
1772 CTLFLAG_PERMANENT,
1773 CTLTYPE_INT, "peak",
1774 SYSCTL_DESCR("Highest input queue length"),
1775 NULL, 0, &ifq->ifq_peak, 0,
1776 CTL_NET, pf, ipn, qid, IFQCTL_PEAK, CTL_EOL);
1777 #endif
1778 sysctl_createv(clog, 0, NULL, NULL,
1779 CTLFLAG_PERMANENT,
1780 CTLTYPE_INT, "drops",
1781 SYSCTL_DESCR("Packets dropped due to full input queue"),
1782 NULL, 0, &ifq->ifq_drops, 0,
1783 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
1784 }
1785
1786 #ifdef INET
1787 SYSCTL_SETUP(sysctl_net_inet_ip_ifq_setup,
1788 "sysctl net.inet.ip.ifq subtree setup")
1789 {
1790 extern struct ifqueue ipintrq;
1791
1792 sysctl_net_ifq_setup(clog, PF_INET, "inet", IPPROTO_IP, "ip",
1793 IPCTL_IFQ, &ipintrq);
1794 }
1795 #endif /* INET */
1796
1797 #ifdef INET6
1798 SYSCTL_SETUP(sysctl_net_inet6_ip6_ifq_setup,
1799 "sysctl net.inet6.ip6.ifq subtree setup")
1800 {
1801 extern struct ifqueue ip6intrq;
1802
1803 sysctl_net_ifq_setup(clog, PF_INET6, "inet6", IPPROTO_IPV6, "ip6",
1804 IPV6CTL_IFQ, &ip6intrq);
1805 }
1806 #endif /* INET6 */
1807 #endif /* INET || INET6 */
1808