if.c revision 1.323 1 /* $NetBSD: if.c,v 1.323 2016/02/09 08:32:12 ozaki-r Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2008 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 /*
62 * Copyright (c) 1980, 1986, 1993
63 * The Regents of the University of California. All rights reserved.
64 *
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
67 * are met:
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. Neither the name of the University nor the names of its contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 *
89 * @(#)if.c 8.5 (Berkeley) 1/9/95
90 */
91
92 #include <sys/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.323 2016/02/09 08:32:12 ozaki-r Exp $");
94
95 #if defined(_KERNEL_OPT)
96 #include "opt_inet.h"
97
98 #include "opt_atalk.h"
99 #include "opt_natm.h"
100 #include "opt_wlan.h"
101 #include "opt_net_mpsafe.h"
102 #endif
103
104 #include <sys/param.h>
105 #include <sys/mbuf.h>
106 #include <sys/systm.h>
107 #include <sys/callout.h>
108 #include <sys/proc.h>
109 #include <sys/socket.h>
110 #include <sys/socketvar.h>
111 #include <sys/domain.h>
112 #include <sys/protosw.h>
113 #include <sys/kernel.h>
114 #include <sys/ioctl.h>
115 #include <sys/sysctl.h>
116 #include <sys/syslog.h>
117 #include <sys/kauth.h>
118 #include <sys/kmem.h>
119 #include <sys/xcall.h>
120 #include <sys/cpu.h>
121 #include <sys/intr.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 #include <sys/module.h>
134 #ifdef NETATALK
135 #include <netatalk/at_extern.h>
136 #include <netatalk/at.h>
137 #endif
138 #include <net/pfil.h>
139 #include <netinet/in.h>
140 #include <netinet/in_var.h>
141
142 #ifdef INET6
143 #include <netinet6/in6_var.h>
144 #include <netinet6/nd6.h>
145 #endif
146
147 #include "ether.h"
148 #include "fddi.h"
149 #include "token.h"
150
151 #include "carp.h"
152 #if NCARP > 0
153 #include <netinet/ip_carp.h>
154 #endif
155
156 #include <compat/sys/sockio.h>
157 #include <compat/sys/socket.h>
158
159 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
160 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
161
162 /*
163 * Global list of interfaces.
164 */
165 struct ifnet_head ifnet_list;
166 static ifnet_t ** ifindex2ifnet = NULL;
167
168 static u_int if_index = 1;
169 static size_t if_indexlim = 0;
170 static uint64_t index_gen;
171 static kmutex_t index_gen_mtx;
172 static kmutex_t if_clone_mtx;
173
174 struct ifnet *lo0ifp;
175 int ifqmaxlen = IFQ_MAXLEN;
176
177 static int if_rt_walktree(struct rtentry *, void *);
178
179 static struct if_clone *if_clone_lookup(const char *, int *);
180
181 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
182 static int if_cloners_count;
183
184 /* Packet filtering hook for interfaces. */
185 pfil_head_t * if_pfil;
186
187 static kauth_listener_t if_listener;
188
189 static int doifioctl(struct socket *, u_long, void *, struct lwp *);
190 static int ifioctl_attach(struct ifnet *);
191 static void ifioctl_detach(struct ifnet *);
192 static void ifnet_lock_enter(struct ifnet_lock *);
193 static void ifnet_lock_exit(struct ifnet_lock *);
194 static void if_detach_queues(struct ifnet *, struct ifqueue *);
195 static void sysctl_sndq_setup(struct sysctllog **, const char *,
196 struct ifaltq *);
197 static void if_slowtimo(void *);
198 static void if_free_sadl(struct ifnet *);
199 static void if_attachdomain1(struct ifnet *);
200 static int ifconf(u_long, void *);
201 static int if_clone_create(const char *);
202 static int if_clone_destroy(const char *);
203
204 struct if_percpuq {
205 struct ifnet *ipq_ifp;
206 void *ipq_si;
207 struct percpu *ipq_ifqs; /* struct ifqueue */
208 };
209
210 static struct mbuf *if_percpuq_dequeue(struct if_percpuq *);
211
212 #if defined(INET) || defined(INET6)
213 static void sysctl_net_pktq_setup(struct sysctllog **, int);
214 #endif
215
216 static int
217 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
218 void *arg0, void *arg1, void *arg2, void *arg3)
219 {
220 int result;
221 enum kauth_network_req req;
222
223 result = KAUTH_RESULT_DEFER;
224 req = (enum kauth_network_req)arg1;
225
226 if (action != KAUTH_NETWORK_INTERFACE)
227 return result;
228
229 if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
230 (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
231 result = KAUTH_RESULT_ALLOW;
232
233 return result;
234 }
235
236 /*
237 * Network interface utility routines.
238 *
239 * Routines with ifa_ifwith* names take sockaddr *'s as
240 * parameters.
241 */
242 void
243 ifinit(void)
244 {
245 #if defined(INET)
246 sysctl_net_pktq_setup(NULL, PF_INET);
247 #endif
248 #ifdef INET6
249 if (in6_present)
250 sysctl_net_pktq_setup(NULL, PF_INET6);
251 #endif
252
253 if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
254 if_listener_cb, NULL);
255
256 /* interfaces are available, inform socket code */
257 ifioctl = doifioctl;
258 }
259
260 /*
261 * XXX Initialization before configure().
262 * XXX hack to get pfil_add_hook working in autoconf.
263 */
264 void
265 ifinit1(void)
266 {
267 mutex_init(&index_gen_mtx, MUTEX_DEFAULT, IPL_NONE);
268 mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE);
269 TAILQ_INIT(&ifnet_list);
270 if_indexlim = 8;
271
272 if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL);
273 KASSERT(if_pfil != NULL);
274
275 #if NETHER > 0 || NFDDI > 0 || defined(NETATALK) || NTOKEN > 0 || defined(WLAN)
276 etherinit();
277 #endif
278 }
279
280 ifnet_t *
281 if_alloc(u_char type)
282 {
283 return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP);
284 }
285
286 void
287 if_free(ifnet_t *ifp)
288 {
289 kmem_free(ifp, sizeof(ifnet_t));
290 }
291
292 void
293 if_initname(struct ifnet *ifp, const char *name, int unit)
294 {
295 (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
296 "%s%d", name, unit);
297 }
298
299 /*
300 * Null routines used while an interface is going away. These routines
301 * just return an error.
302 */
303
304 int
305 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
306 const struct sockaddr *so, struct rtentry *rt)
307 {
308
309 return ENXIO;
310 }
311
312 void
313 if_nullinput(struct ifnet *ifp, struct mbuf *m)
314 {
315
316 /* Nothing. */
317 }
318
319 void
320 if_nullstart(struct ifnet *ifp)
321 {
322
323 /* Nothing. */
324 }
325
326 int
327 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
328 {
329
330 /* Wake ifioctl_detach(), who may wait for all threads to
331 * quit the critical section.
332 */
333 cv_signal(&ifp->if_ioctl_lock->il_emptied);
334 return ENXIO;
335 }
336
337 int
338 if_nullinit(struct ifnet *ifp)
339 {
340
341 return ENXIO;
342 }
343
344 void
345 if_nullstop(struct ifnet *ifp, int disable)
346 {
347
348 /* Nothing. */
349 }
350
351 void
352 if_nullslowtimo(struct ifnet *ifp)
353 {
354
355 /* Nothing. */
356 }
357
358 void
359 if_nulldrain(struct ifnet *ifp)
360 {
361
362 /* Nothing. */
363 }
364
365 void
366 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
367 {
368 struct ifaddr *ifa;
369 struct sockaddr_dl *sdl;
370
371 ifp->if_addrlen = addrlen;
372 if_alloc_sadl(ifp);
373 ifa = ifp->if_dl;
374 sdl = satosdl(ifa->ifa_addr);
375
376 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
377 if (factory) {
378 ifp->if_hwdl = ifp->if_dl;
379 ifaref(ifp->if_hwdl);
380 }
381 /* TBD routing socket */
382 }
383
384 struct ifaddr *
385 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
386 {
387 unsigned socksize, ifasize;
388 int addrlen, namelen;
389 struct sockaddr_dl *mask, *sdl;
390 struct ifaddr *ifa;
391
392 namelen = strlen(ifp->if_xname);
393 addrlen = ifp->if_addrlen;
394 socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
395 ifasize = sizeof(*ifa) + 2 * socksize;
396 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO);
397
398 sdl = (struct sockaddr_dl *)(ifa + 1);
399 mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
400
401 sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
402 ifp->if_xname, namelen, NULL, addrlen);
403 mask->sdl_len = sockaddr_dl_measure(namelen, 0);
404 memset(&mask->sdl_data[0], 0xff, namelen);
405 ifa->ifa_rtrequest = link_rtrequest;
406 ifa->ifa_addr = (struct sockaddr *)sdl;
407 ifa->ifa_netmask = (struct sockaddr *)mask;
408
409 *sdlp = sdl;
410
411 return ifa;
412 }
413
414 static void
415 if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
416 {
417 const struct sockaddr_dl *sdl;
418
419 ifp->if_dl = ifa;
420 ifaref(ifa);
421 sdl = satosdl(ifa->ifa_addr);
422 ifp->if_sadl = sdl;
423 }
424
425 /*
426 * Allocate the link level name for the specified interface. This
427 * is an attachment helper. It must be called after ifp->if_addrlen
428 * is initialized, which may not be the case when if_attach() is
429 * called.
430 */
431 void
432 if_alloc_sadl(struct ifnet *ifp)
433 {
434 struct ifaddr *ifa;
435 const struct sockaddr_dl *sdl;
436
437 /*
438 * If the interface already has a link name, release it
439 * now. This is useful for interfaces that can change
440 * link types, and thus switch link names often.
441 */
442 if (ifp->if_sadl != NULL)
443 if_free_sadl(ifp);
444
445 ifa = if_dl_create(ifp, &sdl);
446
447 ifa_insert(ifp, ifa);
448 if_sadl_setrefs(ifp, ifa);
449 }
450
451 static void
452 if_deactivate_sadl(struct ifnet *ifp)
453 {
454 struct ifaddr *ifa;
455
456 KASSERT(ifp->if_dl != NULL);
457
458 ifa = ifp->if_dl;
459
460 ifp->if_sadl = NULL;
461
462 ifp->if_dl = NULL;
463 ifafree(ifa);
464 }
465
466 void
467 if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa,
468 const struct sockaddr_dl *sdl)
469 {
470 int s;
471
472 s = splnet();
473
474 if_deactivate_sadl(ifp);
475
476 if_sadl_setrefs(ifp, ifa);
477 IFADDR_FOREACH(ifa, ifp)
478 rtinit(ifa, RTM_LLINFO_UPD, 0);
479 splx(s);
480 }
481
482 /*
483 * Free the link level name for the specified interface. This is
484 * a detach helper. This is called from if_detach().
485 */
486 static void
487 if_free_sadl(struct ifnet *ifp)
488 {
489 struct ifaddr *ifa;
490 int s;
491
492 ifa = ifp->if_dl;
493 if (ifa == NULL) {
494 KASSERT(ifp->if_sadl == NULL);
495 return;
496 }
497
498 KASSERT(ifp->if_sadl != NULL);
499
500 s = splnet();
501 rtinit(ifa, RTM_DELETE, 0);
502 ifa_remove(ifp, ifa);
503 if_deactivate_sadl(ifp);
504 if (ifp->if_hwdl == ifa) {
505 ifafree(ifa);
506 ifp->if_hwdl = NULL;
507 }
508 splx(s);
509 }
510
511 static void
512 if_getindex(ifnet_t *ifp)
513 {
514 bool hitlimit = false;
515
516 mutex_enter(&index_gen_mtx);
517 ifp->if_index_gen = index_gen++;
518 mutex_exit(&index_gen_mtx);
519
520 ifp->if_index = if_index;
521 if (ifindex2ifnet == NULL) {
522 if_index++;
523 goto skip;
524 }
525 while (if_byindex(ifp->if_index)) {
526 /*
527 * If we hit USHRT_MAX, we skip back to 0 since
528 * there are a number of places where the value
529 * of if_index or if_index itself is compared
530 * to or stored in an unsigned short. By
531 * jumping back, we won't botch those assignments
532 * or comparisons.
533 */
534 if (++if_index == 0) {
535 if_index = 1;
536 } else if (if_index == USHRT_MAX) {
537 /*
538 * However, if we have to jump back to
539 * zero *twice* without finding an empty
540 * slot in ifindex2ifnet[], then there
541 * there are too many (>65535) interfaces.
542 */
543 if (hitlimit) {
544 panic("too many interfaces");
545 }
546 hitlimit = true;
547 if_index = 1;
548 }
549 ifp->if_index = if_index;
550 }
551 skip:
552 /*
553 * ifindex2ifnet is indexed by if_index. Since if_index will
554 * grow dynamically, it should grow too.
555 */
556 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
557 size_t m, n, oldlim;
558 void *q;
559
560 oldlim = if_indexlim;
561 while (ifp->if_index >= if_indexlim)
562 if_indexlim <<= 1;
563
564 /* grow ifindex2ifnet */
565 m = oldlim * sizeof(struct ifnet *);
566 n = if_indexlim * sizeof(struct ifnet *);
567 q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO);
568 if (ifindex2ifnet != NULL) {
569 memcpy(q, ifindex2ifnet, m);
570 free(ifindex2ifnet, M_IFADDR);
571 }
572 ifindex2ifnet = (struct ifnet **)q;
573 }
574 ifindex2ifnet[ifp->if_index] = ifp;
575 }
576
577 /*
578 * Initialize an interface and assign an index for it.
579 *
580 * It must be called prior to a device specific attach routine
581 * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl,
582 * and be followed by if_register:
583 *
584 * if_initialize(ifp);
585 * ether_ifattach(ifp, enaddr);
586 * if_register(ifp);
587 */
588 void
589 if_initialize(ifnet_t *ifp)
590 {
591 KASSERT(if_indexlim > 0);
592 TAILQ_INIT(&ifp->if_addrlist);
593
594 /*
595 * Link level name is allocated later by a separate call to
596 * if_alloc_sadl().
597 */
598
599 if (ifp->if_snd.ifq_maxlen == 0)
600 ifp->if_snd.ifq_maxlen = ifqmaxlen;
601
602 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
603
604 ifp->if_link_state = LINK_STATE_UNKNOWN;
605
606 ifp->if_capenable = 0;
607 ifp->if_csum_flags_tx = 0;
608 ifp->if_csum_flags_rx = 0;
609
610 #ifdef ALTQ
611 ifp->if_snd.altq_type = 0;
612 ifp->if_snd.altq_disc = NULL;
613 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
614 ifp->if_snd.altq_tbr = NULL;
615 ifp->if_snd.altq_ifp = ifp;
616 #endif
617
618 #ifdef NET_MPSAFE
619 ifp->if_snd.ifq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
620 #else
621 ifp->if_snd.ifq_lock = NULL;
622 #endif
623
624 ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp);
625 (void)pfil_run_hooks(if_pfil,
626 (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET);
627
628 IF_AFDATA_LOCK_INIT(ifp);
629
630 if_getindex(ifp);
631 }
632
633 /*
634 * Register an interface to the list of "active" interfaces.
635 */
636 void
637 if_register(ifnet_t *ifp)
638 {
639 if (ifioctl_attach(ifp) != 0)
640 panic("%s: ifioctl_attach() failed", __func__);
641
642 sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
643
644 if (!STAILQ_EMPTY(&domains))
645 if_attachdomain1(ifp);
646
647 /* Announce the interface. */
648 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
649
650 if (ifp->if_slowtimo != NULL) {
651 ifp->if_slowtimo_ch =
652 kmem_zalloc(sizeof(*ifp->if_slowtimo_ch), KM_SLEEP);
653 callout_init(ifp->if_slowtimo_ch, 0);
654 callout_setfunc(ifp->if_slowtimo_ch, if_slowtimo, ifp);
655 if_slowtimo(ifp);
656 }
657
658 TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list);
659 }
660
661 /*
662 * The if_percpuq framework
663 *
664 * It allows network device drivers to execute the network stack
665 * in softint (so called softint-based if_input). It utilizes
666 * softint and percpu ifqueue. It doesn't distribute any packets
667 * between CPUs, unlike pktqueue(9).
668 *
669 * Currently we support two options for device drivers to apply the framework:
670 * - Use it implicitly with less changes
671 * - If you use if_attach in driver's _attach function and if_input in
672 * driver's Rx interrupt handler, a packet is queued and a softint handles
673 * the packet implicitly
674 * - Use it explicitly in each driver (recommended)
675 * - You can use if_percpuq_* directly in your driver
676 * - In this case, you need to allocate struct if_percpuq in driver's softc
677 * - See wm(4) as a reference implementation
678 */
679
680 static void
681 if_percpuq_softint(void *arg)
682 {
683 struct if_percpuq *ipq = arg;
684 struct ifnet *ifp = ipq->ipq_ifp;
685 struct mbuf *m;
686
687 while ((m = if_percpuq_dequeue(ipq)) != NULL)
688 ifp->_if_input(ifp, m);
689 }
690
691 static void
692 if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
693 {
694 struct ifqueue *const ifq = p;
695
696 memset(ifq, 0, sizeof(*ifq));
697 ifq->ifq_maxlen = IFQ_MAXLEN;
698 }
699
700 struct if_percpuq *
701 if_percpuq_create(struct ifnet *ifp)
702 {
703 struct if_percpuq *ipq;
704
705 ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP);
706 if (ipq == NULL)
707 panic("kmem_zalloc failed");
708
709 ipq->ipq_ifp = ifp;
710 ipq->ipq_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,
711 if_percpuq_softint, ipq);
712 ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue));
713 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL);
714
715 return ipq;
716 }
717
718 static struct mbuf *
719 if_percpuq_dequeue(struct if_percpuq *ipq)
720 {
721 struct mbuf *m;
722 struct ifqueue *ifq;
723 int s;
724
725 s = splnet();
726 ifq = percpu_getref(ipq->ipq_ifqs);
727 IF_DEQUEUE(ifq, m);
728 percpu_putref(ipq->ipq_ifqs);
729 splx(s);
730
731 return m;
732 }
733
734 static void
735 if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
736 {
737 struct ifqueue *const ifq = p;
738
739 IF_PURGE(ifq);
740 }
741
742 void
743 if_percpuq_destroy(struct if_percpuq *ipq)
744 {
745
746 /* if_detach may already destroy it */
747 if (ipq == NULL)
748 return;
749
750 softint_disestablish(ipq->ipq_si);
751 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL);
752 percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue));
753 }
754
755 void
756 if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m)
757 {
758 struct ifqueue *ifq;
759 int s;
760
761 KASSERT(ipq != NULL);
762
763 s = splnet();
764 ifq = percpu_getref(ipq->ipq_ifqs);
765 if (IF_QFULL(ifq)) {
766 IF_DROP(ifq);
767 m_freem(m);
768 goto out;
769 }
770 IF_ENQUEUE(ifq, m);
771 percpu_putref(ipq->ipq_ifqs);
772
773 softint_schedule(ipq->ipq_si);
774 out:
775 splx(s);
776 }
777
778 /*
779 * The common interface input routine that is called by device drivers,
780 * which should be used only when the driver's rx handler already runs
781 * in softint.
782 */
783 void
784 if_input(struct ifnet *ifp, struct mbuf *m)
785 {
786
787 KASSERT(ifp->if_percpuq == NULL);
788 KASSERT(!cpu_intr_p());
789
790 ifp->_if_input(ifp, m);
791 }
792
793 /*
794 * DEPRECATED. Use if_initialize and if_register instead.
795 * See the above comment of if_initialize.
796 *
797 * Note that it implicitly enables if_percpuq to make drivers easy to
798 * migrate softinet-based if_input without much changes. If you don't
799 * want to enable it, use if_initialize instead.
800 */
801 void
802 if_attach(ifnet_t *ifp)
803 {
804
805 if_initialize(ifp);
806 ifp->if_percpuq = if_percpuq_create(ifp);
807 if_register(ifp);
808 }
809
810 void
811 if_attachdomain(void)
812 {
813 struct ifnet *ifp;
814 int s;
815
816 s = splnet();
817 IFNET_FOREACH(ifp)
818 if_attachdomain1(ifp);
819 splx(s);
820 }
821
822 static void
823 if_attachdomain1(struct ifnet *ifp)
824 {
825 struct domain *dp;
826 int s;
827
828 s = splnet();
829
830 /* address family dependent data region */
831 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
832 DOMAIN_FOREACH(dp) {
833 if (dp->dom_ifattach != NULL)
834 ifp->if_afdata[dp->dom_family] =
835 (*dp->dom_ifattach)(ifp);
836 }
837
838 splx(s);
839 }
840
841 /*
842 * Deactivate an interface. This points all of the procedure
843 * handles at error stubs. May be called from interrupt context.
844 */
845 void
846 if_deactivate(struct ifnet *ifp)
847 {
848 int s;
849
850 s = splnet();
851
852 ifp->if_output = if_nulloutput;
853 ifp->_if_input = if_nullinput;
854 ifp->if_start = if_nullstart;
855 ifp->if_ioctl = if_nullioctl;
856 ifp->if_init = if_nullinit;
857 ifp->if_stop = if_nullstop;
858 ifp->if_slowtimo = if_nullslowtimo;
859 ifp->if_drain = if_nulldrain;
860
861 /* No more packets may be enqueued. */
862 ifp->if_snd.ifq_maxlen = 0;
863
864 splx(s);
865 }
866
867 void
868 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
869 {
870 struct ifaddr *ifa, *nifa;
871
872 IFADDR_FOREACH_SAFE(ifa, ifp, nifa) {
873 if (ifa->ifa_addr->sa_family != family)
874 continue;
875 (*purgeaddr)(ifa);
876 }
877 }
878
879 /*
880 * Detach an interface from the list of "active" interfaces,
881 * freeing any resources as we go along.
882 *
883 * NOTE: This routine must be called with a valid thread context,
884 * as it may block.
885 */
886 void
887 if_detach(struct ifnet *ifp)
888 {
889 struct socket so;
890 struct ifaddr *ifa;
891 #ifdef IFAREF_DEBUG
892 struct ifaddr *last_ifa = NULL;
893 #endif
894 struct domain *dp;
895 const struct protosw *pr;
896 int s, i, family, purged;
897 uint64_t xc;
898
899 /*
900 * XXX It's kind of lame that we have to have the
901 * XXX socket structure...
902 */
903 memset(&so, 0, sizeof(so));
904
905 s = splnet();
906
907 ifindex2ifnet[ifp->if_index] = NULL;
908 TAILQ_REMOVE(&ifnet_list, ifp, if_list);
909
910 if (ifp->if_slowtimo != NULL) {
911 ifp->if_slowtimo = NULL;
912 callout_halt(ifp->if_slowtimo_ch, NULL);
913 callout_destroy(ifp->if_slowtimo_ch);
914 kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch));
915 }
916
917 /*
918 * Do an if_down() to give protocols a chance to do something.
919 */
920 if_down(ifp);
921
922 #ifdef ALTQ
923 if (ALTQ_IS_ENABLED(&ifp->if_snd))
924 altq_disable(&ifp->if_snd);
925 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
926 altq_detach(&ifp->if_snd);
927 #endif
928
929 if (ifp->if_snd.ifq_lock)
930 mutex_obj_free(ifp->if_snd.ifq_lock);
931
932 sysctl_teardown(&ifp->if_sysctl_log);
933
934 #if NCARP > 0
935 /* Remove the interface from any carp group it is a part of. */
936 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
937 carp_ifdetach(ifp);
938 #endif
939
940 /*
941 * Rip all the addresses off the interface. This should make
942 * all of the routes go away.
943 *
944 * pr_usrreq calls can remove an arbitrary number of ifaddrs
945 * from the list, including our "cursor", ifa. For safety,
946 * and to honor the TAILQ abstraction, I just restart the
947 * loop after each removal. Note that the loop will exit
948 * when all of the remaining ifaddrs belong to the AF_LINK
949 * family. I am counting on the historical fact that at
950 * least one pr_usrreq in each address domain removes at
951 * least one ifaddr.
952 */
953 again:
954 IFADDR_FOREACH(ifa, ifp) {
955 family = ifa->ifa_addr->sa_family;
956 #ifdef IFAREF_DEBUG
957 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
958 ifa, family, ifa->ifa_refcnt);
959 if (last_ifa != NULL && ifa == last_ifa)
960 panic("if_detach: loop detected");
961 last_ifa = ifa;
962 #endif
963 if (family == AF_LINK)
964 continue;
965 dp = pffinddomain(family);
966 #ifdef DIAGNOSTIC
967 if (dp == NULL)
968 panic("if_detach: no domain for AF %d",
969 family);
970 #endif
971 /*
972 * XXX These PURGEIF calls are redundant with the
973 * purge-all-families calls below, but are left in for
974 * now both to make a smaller change, and to avoid
975 * unplanned interactions with clearing of
976 * ifp->if_addrlist.
977 */
978 purged = 0;
979 for (pr = dp->dom_protosw;
980 pr < dp->dom_protoswNPROTOSW; pr++) {
981 so.so_proto = pr;
982 if (pr->pr_usrreqs) {
983 (void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
984 purged = 1;
985 }
986 }
987 if (purged == 0) {
988 /*
989 * XXX What's really the best thing to do
990 * XXX here? --thorpej (at) NetBSD.org
991 */
992 printf("if_detach: WARNING: AF %d not purged\n",
993 family);
994 ifa_remove(ifp, ifa);
995 }
996 goto again;
997 }
998
999 if_free_sadl(ifp);
1000
1001 /* Walk the routing table looking for stragglers. */
1002 for (i = 0; i <= AF_MAX; i++) {
1003 while (rt_walktree(i, if_rt_walktree, ifp) == ERESTART)
1004 continue;
1005 }
1006
1007 DOMAIN_FOREACH(dp) {
1008 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
1009 {
1010 void *p = ifp->if_afdata[dp->dom_family];
1011 if (p) {
1012 ifp->if_afdata[dp->dom_family] = NULL;
1013 (*dp->dom_ifdetach)(ifp, p);
1014 }
1015 }
1016
1017 /*
1018 * One would expect multicast memberships (INET and
1019 * INET6) on UDP sockets to be purged by the PURGEIF
1020 * calls above, but if all addresses were removed from
1021 * the interface prior to destruction, the calls will
1022 * not be made (e.g. ppp, for which pppd(8) generally
1023 * removes addresses before destroying the interface).
1024 * Because there is no invariant that multicast
1025 * memberships only exist for interfaces with IPv4
1026 * addresses, we must call PURGEIF regardless of
1027 * addresses. (Protocols which might store ifnet
1028 * pointers are marked with PR_PURGEIF.)
1029 */
1030 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
1031 so.so_proto = pr;
1032 if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
1033 (void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1034 }
1035 }
1036
1037 (void)pfil_run_hooks(if_pfil,
1038 (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
1039 (void)pfil_head_destroy(ifp->if_pfil);
1040
1041 /* Announce that the interface is gone. */
1042 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1043
1044 ifioctl_detach(ifp);
1045
1046 IF_AFDATA_LOCK_DESTROY(ifp);
1047
1048 /*
1049 * remove packets that came from ifp, from software interrupt queues.
1050 */
1051 DOMAIN_FOREACH(dp) {
1052 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
1053 struct ifqueue *iq = dp->dom_ifqueues[i];
1054 if (iq == NULL)
1055 break;
1056 dp->dom_ifqueues[i] = NULL;
1057 if_detach_queues(ifp, iq);
1058 }
1059 }
1060
1061 /*
1062 * IP queues have to be processed separately: net-queue barrier
1063 * ensures that the packets are dequeued while a cross-call will
1064 * ensure that the interrupts have completed. FIXME: not quite..
1065 */
1066 #ifdef INET
1067 pktq_barrier(ip_pktq);
1068 #endif
1069 #ifdef INET6
1070 if (in6_present)
1071 pktq_barrier(ip6_pktq);
1072 #endif
1073 xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
1074 xc_wait(xc);
1075
1076 if (ifp->if_percpuq != NULL) {
1077 if_percpuq_destroy(ifp->if_percpuq);
1078 ifp->if_percpuq = NULL;
1079 }
1080
1081 splx(s);
1082 }
1083
1084 static void
1085 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
1086 {
1087 struct mbuf *m, *prev, *next;
1088
1089 prev = NULL;
1090 for (m = q->ifq_head; m != NULL; m = next) {
1091 KASSERT((m->m_flags & M_PKTHDR) != 0);
1092
1093 next = m->m_nextpkt;
1094 if (m->m_pkthdr.rcvif != ifp) {
1095 prev = m;
1096 continue;
1097 }
1098
1099 if (prev != NULL)
1100 prev->m_nextpkt = m->m_nextpkt;
1101 else
1102 q->ifq_head = m->m_nextpkt;
1103 if (q->ifq_tail == m)
1104 q->ifq_tail = prev;
1105 q->ifq_len--;
1106
1107 m->m_nextpkt = NULL;
1108 m_freem(m);
1109 IF_DROP(q);
1110 }
1111 }
1112
1113 /*
1114 * Callback for a radix tree walk to delete all references to an
1115 * ifnet.
1116 */
1117 static int
1118 if_rt_walktree(struct rtentry *rt, void *v)
1119 {
1120 struct ifnet *ifp = (struct ifnet *)v;
1121 int error;
1122 struct rtentry *retrt;
1123
1124 if (rt->rt_ifp != ifp)
1125 return 0;
1126
1127 /* Delete the entry. */
1128 error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
1129 rt_mask(rt), rt->rt_flags, &retrt);
1130 if (error == 0) {
1131 KASSERT(retrt == rt);
1132 KASSERT((retrt->rt_flags & RTF_UP) == 0);
1133 retrt->rt_ifp = NULL;
1134 rtfree(retrt);
1135 } else {
1136 printf("%s: warning: unable to delete rtentry @ %p, "
1137 "error = %d\n", ifp->if_xname, rt, error);
1138 }
1139 return ERESTART;
1140 }
1141
1142 /*
1143 * Create a clone network interface.
1144 */
1145 static int
1146 if_clone_create(const char *name)
1147 {
1148 struct if_clone *ifc;
1149 int unit;
1150
1151 ifc = if_clone_lookup(name, &unit);
1152 if (ifc == NULL)
1153 return EINVAL;
1154
1155 if (ifunit(name) != NULL)
1156 return EEXIST;
1157
1158 return (*ifc->ifc_create)(ifc, unit);
1159 }
1160
1161 /*
1162 * Destroy a clone network interface.
1163 */
1164 static int
1165 if_clone_destroy(const char *name)
1166 {
1167 struct if_clone *ifc;
1168 struct ifnet *ifp;
1169
1170 ifc = if_clone_lookup(name, NULL);
1171 if (ifc == NULL)
1172 return EINVAL;
1173
1174 ifp = ifunit(name);
1175 if (ifp == NULL)
1176 return ENXIO;
1177
1178 if (ifc->ifc_destroy == NULL)
1179 return EOPNOTSUPP;
1180
1181 return (*ifc->ifc_destroy)(ifp);
1182 }
1183
1184 /*
1185 * Look up a network interface cloner.
1186 */
1187 static struct if_clone *
1188 if_clone_lookup(const char *name, int *unitp)
1189 {
1190 struct if_clone *ifc;
1191 const char *cp;
1192 char *dp, ifname[IFNAMSIZ + 3];
1193 int unit;
1194
1195 strcpy(ifname, "if_");
1196 /* separate interface name from unit */
1197 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
1198 *cp && (*cp < '0' || *cp > '9');)
1199 *dp++ = *cp++;
1200
1201 if (cp == name || cp - name == IFNAMSIZ || !*cp)
1202 return NULL; /* No name or unit number */
1203 *dp++ = '\0';
1204
1205 again:
1206 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1207 if (strcmp(ifname + 3, ifc->ifc_name) == 0)
1208 break;
1209 }
1210
1211 if (ifc == NULL) {
1212 if (*ifname == '\0' ||
1213 module_autoload(ifname, MODULE_CLASS_DRIVER))
1214 return NULL;
1215 *ifname = '\0';
1216 goto again;
1217 }
1218
1219 unit = 0;
1220 while (cp - name < IFNAMSIZ && *cp) {
1221 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1222 /* Bogus unit number. */
1223 return NULL;
1224 }
1225 unit = (unit * 10) + (*cp++ - '0');
1226 }
1227
1228 if (unitp != NULL)
1229 *unitp = unit;
1230 return ifc;
1231 }
1232
1233 /*
1234 * Register a network interface cloner.
1235 */
1236 void
1237 if_clone_attach(struct if_clone *ifc)
1238 {
1239
1240 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1241 if_cloners_count++;
1242 }
1243
1244 /*
1245 * Unregister a network interface cloner.
1246 */
1247 void
1248 if_clone_detach(struct if_clone *ifc)
1249 {
1250
1251 LIST_REMOVE(ifc, ifc_list);
1252 if_cloners_count--;
1253 }
1254
1255 /*
1256 * Provide list of interface cloners to userspace.
1257 */
1258 int
1259 if_clone_list(int buf_count, char *buffer, int *total)
1260 {
1261 char outbuf[IFNAMSIZ], *dst;
1262 struct if_clone *ifc;
1263 int count, error = 0;
1264
1265 *total = if_cloners_count;
1266 if ((dst = buffer) == NULL) {
1267 /* Just asking how many there are. */
1268 return 0;
1269 }
1270
1271 if (buf_count < 0)
1272 return EINVAL;
1273
1274 count = (if_cloners_count < buf_count) ?
1275 if_cloners_count : buf_count;
1276
1277 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
1278 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1279 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1280 if (outbuf[sizeof(outbuf) - 1] != '\0')
1281 return ENAMETOOLONG;
1282 error = copyout(outbuf, dst, sizeof(outbuf));
1283 if (error != 0)
1284 break;
1285 }
1286
1287 return error;
1288 }
1289
1290 void
1291 ifaref(struct ifaddr *ifa)
1292 {
1293 ifa->ifa_refcnt++;
1294 }
1295
1296 void
1297 ifafree(struct ifaddr *ifa)
1298 {
1299 KASSERT(ifa != NULL);
1300 KASSERT(ifa->ifa_refcnt > 0);
1301
1302 if (--ifa->ifa_refcnt == 0) {
1303 free(ifa, M_IFADDR);
1304 }
1305 }
1306
1307 void
1308 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
1309 {
1310 ifa->ifa_ifp = ifp;
1311 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1312 ifaref(ifa);
1313 }
1314
1315 void
1316 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
1317 {
1318 KASSERT(ifa->ifa_ifp == ifp);
1319 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1320 ifafree(ifa);
1321 }
1322
1323 static inline int
1324 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
1325 {
1326 return sockaddr_cmp(sa1, sa2) == 0;
1327 }
1328
1329 /*
1330 * Locate an interface based on a complete address.
1331 */
1332 /*ARGSUSED*/
1333 struct ifaddr *
1334 ifa_ifwithaddr(const struct sockaddr *addr)
1335 {
1336 struct ifnet *ifp;
1337 struct ifaddr *ifa;
1338
1339 IFNET_FOREACH(ifp) {
1340 if (ifp->if_output == if_nulloutput)
1341 continue;
1342 IFADDR_FOREACH(ifa, ifp) {
1343 if (ifa->ifa_addr->sa_family != addr->sa_family)
1344 continue;
1345 if (equal(addr, ifa->ifa_addr))
1346 return ifa;
1347 if ((ifp->if_flags & IFF_BROADCAST) &&
1348 ifa->ifa_broadaddr &&
1349 /* IP6 doesn't have broadcast */
1350 ifa->ifa_broadaddr->sa_len != 0 &&
1351 equal(ifa->ifa_broadaddr, addr))
1352 return ifa;
1353 }
1354 }
1355 return NULL;
1356 }
1357
1358 /*
1359 * Locate the point to point interface with a given destination address.
1360 */
1361 /*ARGSUSED*/
1362 struct ifaddr *
1363 ifa_ifwithdstaddr(const struct sockaddr *addr)
1364 {
1365 struct ifnet *ifp;
1366 struct ifaddr *ifa;
1367
1368 IFNET_FOREACH(ifp) {
1369 if (ifp->if_output == if_nulloutput)
1370 continue;
1371 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1372 continue;
1373 IFADDR_FOREACH(ifa, ifp) {
1374 if (ifa->ifa_addr->sa_family != addr->sa_family ||
1375 ifa->ifa_dstaddr == NULL)
1376 continue;
1377 if (equal(addr, ifa->ifa_dstaddr))
1378 return ifa;
1379 }
1380 }
1381 return NULL;
1382 }
1383
1384 /*
1385 * Find an interface on a specific network. If many, choice
1386 * is most specific found.
1387 */
1388 struct ifaddr *
1389 ifa_ifwithnet(const struct sockaddr *addr)
1390 {
1391 struct ifnet *ifp;
1392 struct ifaddr *ifa;
1393 const struct sockaddr_dl *sdl;
1394 struct ifaddr *ifa_maybe = 0;
1395 u_int af = addr->sa_family;
1396 const char *addr_data = addr->sa_data, *cplim;
1397
1398 if (af == AF_LINK) {
1399 sdl = satocsdl(addr);
1400 if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1401 ifindex2ifnet[sdl->sdl_index] &&
1402 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput) {
1403 return ifindex2ifnet[sdl->sdl_index]->if_dl;
1404 }
1405 }
1406 #ifdef NETATALK
1407 if (af == AF_APPLETALK) {
1408 const struct sockaddr_at *sat, *sat2;
1409 sat = (const struct sockaddr_at *)addr;
1410 IFNET_FOREACH(ifp) {
1411 if (ifp->if_output == if_nulloutput)
1412 continue;
1413 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1414 if (ifa == NULL)
1415 continue;
1416 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1417 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1418 return ifa; /* exact match */
1419 if (ifa_maybe == NULL) {
1420 /* else keep the if with the right range */
1421 ifa_maybe = ifa;
1422 }
1423 }
1424 return ifa_maybe;
1425 }
1426 #endif
1427 IFNET_FOREACH(ifp) {
1428 if (ifp->if_output == if_nulloutput)
1429 continue;
1430 IFADDR_FOREACH(ifa, ifp) {
1431 const char *cp, *cp2, *cp3;
1432
1433 if (ifa->ifa_addr->sa_family != af ||
1434 ifa->ifa_netmask == NULL)
1435 next: continue;
1436 cp = addr_data;
1437 cp2 = ifa->ifa_addr->sa_data;
1438 cp3 = ifa->ifa_netmask->sa_data;
1439 cplim = (const char *)ifa->ifa_netmask +
1440 ifa->ifa_netmask->sa_len;
1441 while (cp3 < cplim) {
1442 if ((*cp++ ^ *cp2++) & *cp3++) {
1443 /* want to continue for() loop */
1444 goto next;
1445 }
1446 }
1447 if (ifa_maybe == NULL ||
1448 rn_refines((void *)ifa->ifa_netmask,
1449 (void *)ifa_maybe->ifa_netmask))
1450 ifa_maybe = ifa;
1451 }
1452 }
1453 return ifa_maybe;
1454 }
1455
1456 /*
1457 * Find the interface of the addresss.
1458 */
1459 struct ifaddr *
1460 ifa_ifwithladdr(const struct sockaddr *addr)
1461 {
1462 struct ifaddr *ia;
1463
1464 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1465 (ia = ifa_ifwithnet(addr)))
1466 return ia;
1467 return NULL;
1468 }
1469
1470 /*
1471 * Find an interface using a specific address family
1472 */
1473 struct ifaddr *
1474 ifa_ifwithaf(int af)
1475 {
1476 struct ifnet *ifp;
1477 struct ifaddr *ifa;
1478
1479 IFNET_FOREACH(ifp) {
1480 if (ifp->if_output == if_nulloutput)
1481 continue;
1482 IFADDR_FOREACH(ifa, ifp) {
1483 if (ifa->ifa_addr->sa_family == af)
1484 return ifa;
1485 }
1486 }
1487 return NULL;
1488 }
1489
1490 /*
1491 * Find an interface address specific to an interface best matching
1492 * a given address.
1493 */
1494 struct ifaddr *
1495 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1496 {
1497 struct ifaddr *ifa;
1498 const char *cp, *cp2, *cp3;
1499 const char *cplim;
1500 struct ifaddr *ifa_maybe = 0;
1501 u_int af = addr->sa_family;
1502
1503 if (ifp->if_output == if_nulloutput)
1504 return NULL;
1505
1506 if (af >= AF_MAX)
1507 return NULL;
1508
1509 IFADDR_FOREACH(ifa, ifp) {
1510 if (ifa->ifa_addr->sa_family != af)
1511 continue;
1512 ifa_maybe = ifa;
1513 if (ifa->ifa_netmask == NULL) {
1514 if (equal(addr, ifa->ifa_addr) ||
1515 (ifa->ifa_dstaddr &&
1516 equal(addr, ifa->ifa_dstaddr)))
1517 return ifa;
1518 continue;
1519 }
1520 cp = addr->sa_data;
1521 cp2 = ifa->ifa_addr->sa_data;
1522 cp3 = ifa->ifa_netmask->sa_data;
1523 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1524 for (; cp3 < cplim; cp3++) {
1525 if ((*cp++ ^ *cp2++) & *cp3)
1526 break;
1527 }
1528 if (cp3 == cplim)
1529 return ifa;
1530 }
1531 return ifa_maybe;
1532 }
1533
1534 /*
1535 * Default action when installing a route with a Link Level gateway.
1536 * Lookup an appropriate real ifa to point to.
1537 * This should be moved to /sys/net/link.c eventually.
1538 */
1539 void
1540 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
1541 {
1542 struct ifaddr *ifa;
1543 const struct sockaddr *dst;
1544 struct ifnet *ifp;
1545
1546 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1547 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
1548 return;
1549 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1550 rt_replace_ifa(rt, ifa);
1551 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1552 ifa->ifa_rtrequest(cmd, rt, info);
1553 }
1554 }
1555
1556 /*
1557 * Handle a change in the interface link state.
1558 * XXX: We should listen to the routing socket in-kernel rather
1559 * than calling in6_if_link_* functions directly from here.
1560 */
1561 void
1562 if_link_state_change(struct ifnet *ifp, int link_state)
1563 {
1564 int s;
1565 int old_link_state;
1566 struct domain *dp;
1567
1568 s = splnet();
1569 if (ifp->if_link_state == link_state) {
1570 splx(s);
1571 return;
1572 }
1573
1574 old_link_state = ifp->if_link_state;
1575 ifp->if_link_state = link_state;
1576 #ifdef DEBUG
1577 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
1578 link_state == LINK_STATE_UP ? "UP" :
1579 link_state == LINK_STATE_DOWN ? "DOWN" :
1580 "UNKNOWN",
1581 old_link_state == LINK_STATE_UP ? "UP" :
1582 old_link_state == LINK_STATE_DOWN ? "DOWN" :
1583 "UNKNOWN");
1584 #endif
1585
1586 /*
1587 * When going from UNKNOWN to UP, we need to mark existing
1588 * addresses as tentative and restart DAD as we may have
1589 * erroneously not found a duplicate.
1590 *
1591 * This needs to happen before rt_ifmsg to avoid a race where
1592 * listeners would have an address and expect it to work right
1593 * away.
1594 */
1595 if (link_state == LINK_STATE_UP &&
1596 old_link_state == LINK_STATE_UNKNOWN)
1597 {
1598 DOMAIN_FOREACH(dp) {
1599 if (dp->dom_if_link_state_change != NULL)
1600 dp->dom_if_link_state_change(ifp,
1601 LINK_STATE_DOWN);
1602 }
1603 }
1604
1605 /* Notify that the link state has changed. */
1606 rt_ifmsg(ifp);
1607
1608 #if NCARP > 0
1609 if (ifp->if_carp)
1610 carp_carpdev_state(ifp);
1611 #endif
1612
1613 DOMAIN_FOREACH(dp) {
1614 if (dp->dom_if_link_state_change != NULL)
1615 dp->dom_if_link_state_change(ifp, link_state);
1616 }
1617
1618 splx(s);
1619 }
1620
1621 /*
1622 * Default action when installing a local route on a point-to-point
1623 * interface.
1624 */
1625 void
1626 p2p_rtrequest(int req, struct rtentry *rt,
1627 __unused const struct rt_addrinfo *info)
1628 {
1629 struct ifnet *ifp = rt->rt_ifp;
1630 struct ifaddr *ifa, *lo0ifa;
1631
1632 switch (req) {
1633 case RTM_ADD:
1634 if ((rt->rt_flags & RTF_LOCAL) == 0)
1635 break;
1636
1637 IFADDR_FOREACH(ifa, ifp) {
1638 if (equal(rt_getkey(rt), ifa->ifa_addr))
1639 break;
1640 }
1641 if (ifa == NULL)
1642 break;
1643
1644 /*
1645 * Ensure lo0 has an address of the same family.
1646 */
1647 IFADDR_FOREACH(lo0ifa, lo0ifp) {
1648 if (lo0ifa->ifa_addr->sa_family ==
1649 ifa->ifa_addr->sa_family)
1650 break;
1651 }
1652 if (lo0ifa == NULL)
1653 break;
1654
1655 rt->rt_ifp = lo0ifp;
1656 rt->rt_flags &= ~RTF_LLINFO;
1657
1658 /*
1659 * Make sure to set rt->rt_ifa to the interface
1660 * address we are using, otherwise we will have trouble
1661 * with source address selection.
1662 */
1663 if (ifa != rt->rt_ifa)
1664 rt_replace_ifa(rt, ifa);
1665 break;
1666 case RTM_DELETE:
1667 case RTM_RESOLVE:
1668 default:
1669 break;
1670 }
1671 }
1672
1673 /*
1674 * Mark an interface down and notify protocols of
1675 * the transition.
1676 * NOTE: must be called at splsoftnet or equivalent.
1677 */
1678 void
1679 if_down(struct ifnet *ifp)
1680 {
1681 struct ifaddr *ifa;
1682 struct domain *dp;
1683
1684 ifp->if_flags &= ~IFF_UP;
1685 nanotime(&ifp->if_lastchange);
1686 IFADDR_FOREACH(ifa, ifp)
1687 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1688 IFQ_PURGE(&ifp->if_snd);
1689 #if NCARP > 0
1690 if (ifp->if_carp)
1691 carp_carpdev_state(ifp);
1692 #endif
1693 rt_ifmsg(ifp);
1694 DOMAIN_FOREACH(dp) {
1695 if (dp->dom_if_down)
1696 dp->dom_if_down(ifp);
1697 }
1698 }
1699
1700 /*
1701 * Mark an interface up and notify protocols of
1702 * the transition.
1703 * NOTE: must be called at splsoftnet or equivalent.
1704 */
1705 void
1706 if_up(struct ifnet *ifp)
1707 {
1708 #ifdef notyet
1709 struct ifaddr *ifa;
1710 #endif
1711 struct domain *dp;
1712
1713 ifp->if_flags |= IFF_UP;
1714 nanotime(&ifp->if_lastchange);
1715 #ifdef notyet
1716 /* this has no effect on IP, and will kill all ISO connections XXX */
1717 IFADDR_FOREACH(ifa, ifp)
1718 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1719 #endif
1720 #if NCARP > 0
1721 if (ifp->if_carp)
1722 carp_carpdev_state(ifp);
1723 #endif
1724 rt_ifmsg(ifp);
1725 DOMAIN_FOREACH(dp) {
1726 if (dp->dom_if_up)
1727 dp->dom_if_up(ifp);
1728 }
1729 }
1730
1731 /*
1732 * Handle interface slowtimo timer routine. Called
1733 * from softclock, we decrement timer (if set) and
1734 * call the appropriate interface routine on expiration.
1735 */
1736 static void
1737 if_slowtimo(void *arg)
1738 {
1739 void (*slowtimo)(struct ifnet *);
1740 struct ifnet *ifp = arg;
1741 int s;
1742
1743 slowtimo = ifp->if_slowtimo;
1744 if (__predict_false(slowtimo == NULL))
1745 return;
1746
1747 s = splnet();
1748 if (ifp->if_timer != 0 && --ifp->if_timer == 0)
1749 (*slowtimo)(ifp);
1750
1751 splx(s);
1752
1753 if (__predict_true(ifp->if_slowtimo != NULL))
1754 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
1755 }
1756
1757 /*
1758 * Set/clear promiscuous mode on interface ifp based on the truth value
1759 * of pswitch. The calls are reference counted so that only the first
1760 * "on" request actually has an effect, as does the final "off" request.
1761 * Results are undefined if the "off" and "on" requests are not matched.
1762 */
1763 int
1764 ifpromisc(struct ifnet *ifp, int pswitch)
1765 {
1766 int pcount, ret;
1767 short nflags;
1768
1769 pcount = ifp->if_pcount;
1770 if (pswitch) {
1771 /*
1772 * Allow the device to be "placed" into promiscuous
1773 * mode even if it is not configured up. It will
1774 * consult IFF_PROMISC when it is brought up.
1775 */
1776 if (ifp->if_pcount++ != 0)
1777 return 0;
1778 nflags = ifp->if_flags | IFF_PROMISC;
1779 } else {
1780 if (--ifp->if_pcount > 0)
1781 return 0;
1782 nflags = ifp->if_flags & ~IFF_PROMISC;
1783 }
1784 ret = if_flags_set(ifp, nflags);
1785 /* Restore interface state if not successful. */
1786 if (ret != 0) {
1787 ifp->if_pcount = pcount;
1788 }
1789 return ret;
1790 }
1791
1792 /*
1793 * Map interface name to
1794 * interface structure pointer.
1795 */
1796 struct ifnet *
1797 ifunit(const char *name)
1798 {
1799 struct ifnet *ifp;
1800 const char *cp = name;
1801 u_int unit = 0;
1802 u_int i;
1803
1804 /*
1805 * If the entire name is a number, treat it as an ifindex.
1806 */
1807 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1808 unit = unit * 10 + (*cp - '0');
1809 }
1810
1811 /*
1812 * If the number took all of the name, then it's a valid ifindex.
1813 */
1814 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1815 if (unit >= if_indexlim)
1816 return NULL;
1817 ifp = ifindex2ifnet[unit];
1818 if (ifp == NULL || ifp->if_output == if_nulloutput)
1819 return NULL;
1820 return ifp;
1821 }
1822
1823 IFNET_FOREACH(ifp) {
1824 if (ifp->if_output == if_nulloutput)
1825 continue;
1826 if (strcmp(ifp->if_xname, name) == 0)
1827 return ifp;
1828 }
1829 return NULL;
1830 }
1831
1832 ifnet_t *
1833 if_byindex(u_int idx)
1834 {
1835 return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL;
1836 }
1837
1838 /* common */
1839 int
1840 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
1841 {
1842 int s;
1843 struct ifreq *ifr;
1844 struct ifcapreq *ifcr;
1845 struct ifdatareq *ifdr;
1846
1847 switch (cmd) {
1848 case SIOCSIFCAP:
1849 ifcr = data;
1850 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1851 return EINVAL;
1852
1853 if (ifcr->ifcr_capenable == ifp->if_capenable)
1854 return 0;
1855
1856 ifp->if_capenable = ifcr->ifcr_capenable;
1857
1858 /* Pre-compute the checksum flags mask. */
1859 ifp->if_csum_flags_tx = 0;
1860 ifp->if_csum_flags_rx = 0;
1861 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1862 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1863 }
1864 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1865 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1866 }
1867
1868 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1869 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1870 }
1871 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1872 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1873 }
1874
1875 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1876 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1877 }
1878 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1879 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1880 }
1881
1882 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1883 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1884 }
1885 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1886 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1887 }
1888
1889 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1890 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1891 }
1892 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1893 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1894 }
1895 if (ifp->if_flags & IFF_UP)
1896 return ENETRESET;
1897 return 0;
1898 case SIOCSIFFLAGS:
1899 ifr = data;
1900 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1901 s = splnet();
1902 if_down(ifp);
1903 splx(s);
1904 }
1905 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1906 s = splnet();
1907 if_up(ifp);
1908 splx(s);
1909 }
1910 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1911 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1912 break;
1913 case SIOCGIFFLAGS:
1914 ifr = data;
1915 ifr->ifr_flags = ifp->if_flags;
1916 break;
1917
1918 case SIOCGIFMETRIC:
1919 ifr = data;
1920 ifr->ifr_metric = ifp->if_metric;
1921 break;
1922
1923 case SIOCGIFMTU:
1924 ifr = data;
1925 ifr->ifr_mtu = ifp->if_mtu;
1926 break;
1927
1928 case SIOCGIFDLT:
1929 ifr = data;
1930 ifr->ifr_dlt = ifp->if_dlt;
1931 break;
1932
1933 case SIOCGIFCAP:
1934 ifcr = data;
1935 ifcr->ifcr_capabilities = ifp->if_capabilities;
1936 ifcr->ifcr_capenable = ifp->if_capenable;
1937 break;
1938
1939 case SIOCSIFMETRIC:
1940 ifr = data;
1941 ifp->if_metric = ifr->ifr_metric;
1942 break;
1943
1944 case SIOCGIFDATA:
1945 ifdr = data;
1946 ifdr->ifdr_data = ifp->if_data;
1947 break;
1948
1949 case SIOCGIFINDEX:
1950 ifr = data;
1951 ifr->ifr_index = ifp->if_index;
1952 break;
1953
1954 case SIOCZIFDATA:
1955 ifdr = data;
1956 ifdr->ifdr_data = ifp->if_data;
1957 /*
1958 * Assumes that the volatile counters that can be
1959 * zero'ed are at the end of if_data.
1960 */
1961 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1962 offsetof(struct if_data, ifi_ipackets));
1963 /*
1964 * The memset() clears to the bottm of if_data. In the area,
1965 * if_lastchange is included. Please be careful if new entry
1966 * will be added into if_data or rewite this.
1967 *
1968 * And also, update if_lastchnage.
1969 */
1970 getnanotime(&ifp->if_lastchange);
1971 break;
1972 case SIOCSIFMTU:
1973 ifr = data;
1974 if (ifp->if_mtu == ifr->ifr_mtu)
1975 break;
1976 ifp->if_mtu = ifr->ifr_mtu;
1977 /*
1978 * If the link MTU changed, do network layer specific procedure.
1979 */
1980 #ifdef INET6
1981 if (in6_present)
1982 nd6_setmtu(ifp);
1983 #endif
1984 return ENETRESET;
1985 default:
1986 return ENOTTY;
1987 }
1988 return 0;
1989 }
1990
1991 int
1992 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
1993 {
1994 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
1995 struct ifaddr *ifa;
1996 const struct sockaddr *any, *sa;
1997 union {
1998 struct sockaddr sa;
1999 struct sockaddr_storage ss;
2000 } u, v;
2001
2002 switch (cmd) {
2003 case SIOCSIFADDRPREF:
2004 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
2005 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
2006 NULL) != 0)
2007 return EPERM;
2008 case SIOCGIFADDRPREF:
2009 break;
2010 default:
2011 return EOPNOTSUPP;
2012 }
2013
2014 /* sanity checks */
2015 if (data == NULL || ifp == NULL) {
2016 panic("invalid argument to %s", __func__);
2017 /*NOTREACHED*/
2018 }
2019
2020 /* address must be specified on ADD and DELETE */
2021 sa = sstocsa(&ifap->ifap_addr);
2022 if (sa->sa_family != sofamily(so))
2023 return EINVAL;
2024 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
2025 return EINVAL;
2026
2027 sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
2028
2029 IFADDR_FOREACH(ifa, ifp) {
2030 if (ifa->ifa_addr->sa_family != sa->sa_family)
2031 continue;
2032 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
2033 if (sockaddr_cmp(&u.sa, &v.sa) == 0)
2034 break;
2035 }
2036 if (ifa == NULL)
2037 return EADDRNOTAVAIL;
2038
2039 switch (cmd) {
2040 case SIOCSIFADDRPREF:
2041 ifa->ifa_preference = ifap->ifap_preference;
2042 return 0;
2043 case SIOCGIFADDRPREF:
2044 /* fill in the if_laddrreq structure */
2045 (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
2046 sizeof(ifap->ifap_addr), ifa->ifa_addr);
2047 ifap->ifap_preference = ifa->ifa_preference;
2048 return 0;
2049 default:
2050 return EOPNOTSUPP;
2051 }
2052 }
2053
2054 static void
2055 ifnet_lock_enter(struct ifnet_lock *il)
2056 {
2057 uint64_t *nenter;
2058
2059 /* Before trying to acquire the mutex, increase the count of threads
2060 * who have entered or who wait to enter the critical section.
2061 * Avoid one costly locked memory transaction by keeping a count for
2062 * each CPU.
2063 */
2064 nenter = percpu_getref(il->il_nenter);
2065 (*nenter)++;
2066 percpu_putref(il->il_nenter);
2067 mutex_enter(&il->il_lock);
2068 }
2069
2070 static void
2071 ifnet_lock_exit(struct ifnet_lock *il)
2072 {
2073 /* Increase the count of threads who have exited the critical
2074 * section. Increase while we still hold the lock.
2075 */
2076 il->il_nexit++;
2077 mutex_exit(&il->il_lock);
2078 }
2079
2080 /*
2081 * Interface ioctls.
2082 */
2083 static int
2084 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
2085 {
2086 struct ifnet *ifp;
2087 struct ifreq *ifr;
2088 int error = 0;
2089 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
2090 u_long ocmd = cmd;
2091 #endif
2092 short oif_flags;
2093 #ifdef COMPAT_OIFREQ
2094 struct ifreq ifrb;
2095 struct oifreq *oifr = NULL;
2096 #endif
2097 int r;
2098
2099 switch (cmd) {
2100 #ifdef COMPAT_OIFREQ
2101 case OSIOCGIFCONF:
2102 case OOSIOCGIFCONF:
2103 return compat_ifconf(cmd, data);
2104 #endif
2105 #ifdef COMPAT_OIFDATA
2106 case OSIOCGIFDATA:
2107 case OSIOCZIFDATA:
2108 return compat_ifdatareq(l, cmd, data);
2109 #endif
2110 case SIOCGIFCONF:
2111 return ifconf(cmd, data);
2112 case SIOCINITIFADDR:
2113 return EPERM;
2114 }
2115
2116 #ifdef COMPAT_OIFREQ
2117 cmd = compat_cvtcmd(cmd);
2118 if (cmd != ocmd) {
2119 oifr = data;
2120 data = ifr = &ifrb;
2121 ifreqo2n(oifr, ifr);
2122 } else
2123 #endif
2124 ifr = data;
2125
2126 ifp = ifunit(ifr->ifr_name);
2127
2128 switch (cmd) {
2129 case SIOCIFCREATE:
2130 case SIOCIFDESTROY:
2131 if (l != NULL) {
2132 error = kauth_authorize_network(l->l_cred,
2133 KAUTH_NETWORK_INTERFACE,
2134 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
2135 (void *)cmd, NULL);
2136 if (error != 0)
2137 return error;
2138 }
2139 mutex_enter(&if_clone_mtx);
2140 r = (cmd == SIOCIFCREATE) ?
2141 if_clone_create(ifr->ifr_name) :
2142 if_clone_destroy(ifr->ifr_name);
2143 mutex_exit(&if_clone_mtx);
2144 return r;
2145
2146 case SIOCIFGCLONERS:
2147 {
2148 struct if_clonereq *req = (struct if_clonereq *)data;
2149 return if_clone_list(req->ifcr_count, req->ifcr_buffer,
2150 &req->ifcr_total);
2151 }
2152 }
2153
2154 if (ifp == NULL)
2155 return ENXIO;
2156
2157 switch (cmd) {
2158 case SIOCALIFADDR:
2159 case SIOCDLIFADDR:
2160 case SIOCSIFADDRPREF:
2161 case SIOCSIFFLAGS:
2162 case SIOCSIFCAP:
2163 case SIOCSIFMETRIC:
2164 case SIOCZIFDATA:
2165 case SIOCSIFMTU:
2166 case SIOCSIFPHYADDR:
2167 case SIOCDIFPHYADDR:
2168 #ifdef INET6
2169 case SIOCSIFPHYADDR_IN6:
2170 #endif
2171 case SIOCSLIFPHYADDR:
2172 case SIOCADDMULTI:
2173 case SIOCDELMULTI:
2174 case SIOCSIFMEDIA:
2175 case SIOCSDRVSPEC:
2176 case SIOCG80211:
2177 case SIOCS80211:
2178 case SIOCS80211NWID:
2179 case SIOCS80211NWKEY:
2180 case SIOCS80211POWER:
2181 case SIOCS80211BSSID:
2182 case SIOCS80211CHANNEL:
2183 case SIOCSLINKSTR:
2184 if (l != NULL) {
2185 error = kauth_authorize_network(l->l_cred,
2186 KAUTH_NETWORK_INTERFACE,
2187 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
2188 (void *)cmd, NULL);
2189 if (error != 0)
2190 return error;
2191 }
2192 }
2193
2194 oif_flags = ifp->if_flags;
2195
2196 ifnet_lock_enter(ifp->if_ioctl_lock);
2197 error = (*ifp->if_ioctl)(ifp, cmd, data);
2198 if (error != ENOTTY)
2199 ;
2200 else if (so->so_proto == NULL)
2201 error = EOPNOTSUPP;
2202 else {
2203 #ifdef COMPAT_OSOCK
2204 error = compat_ifioctl(so, ocmd, cmd, data, l);
2205 #else
2206 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
2207 cmd, data, ifp);
2208 #endif
2209 }
2210
2211 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
2212 if ((ifp->if_flags & IFF_UP) != 0) {
2213 int s = splnet();
2214 if_up(ifp);
2215 splx(s);
2216 }
2217 }
2218 #ifdef COMPAT_OIFREQ
2219 if (cmd != ocmd)
2220 ifreqn2o(oifr, ifr);
2221 #endif
2222
2223 ifnet_lock_exit(ifp->if_ioctl_lock);
2224 return error;
2225 }
2226
2227 /* This callback adds to the sum in `arg' the number of
2228 * threads on `ci' who have entered or who wait to enter the
2229 * critical section.
2230 */
2231 static void
2232 ifnet_lock_sum(void *p, void *arg, struct cpu_info *ci)
2233 {
2234 uint64_t *sum = arg, *nenter = p;
2235
2236 *sum += *nenter;
2237 }
2238
2239 /* Return the number of threads who have entered or who wait
2240 * to enter the critical section on all CPUs.
2241 */
2242 static uint64_t
2243 ifnet_lock_entrances(struct ifnet_lock *il)
2244 {
2245 uint64_t sum = 0;
2246
2247 percpu_foreach(il->il_nenter, ifnet_lock_sum, &sum);
2248
2249 return sum;
2250 }
2251
2252 static int
2253 ifioctl_attach(struct ifnet *ifp)
2254 {
2255 struct ifnet_lock *il;
2256
2257 /* If the driver has not supplied its own if_ioctl, then
2258 * supply the default.
2259 */
2260 if (ifp->if_ioctl == NULL)
2261 ifp->if_ioctl = ifioctl_common;
2262
2263 /* Create an ifnet_lock for synchronizing ifioctls. */
2264 if ((il = kmem_zalloc(sizeof(*il), KM_SLEEP)) == NULL)
2265 return ENOMEM;
2266
2267 il->il_nenter = percpu_alloc(sizeof(uint64_t));
2268 if (il->il_nenter == NULL) {
2269 kmem_free(il, sizeof(*il));
2270 return ENOMEM;
2271 }
2272
2273 mutex_init(&il->il_lock, MUTEX_DEFAULT, IPL_NONE);
2274 cv_init(&il->il_emptied, ifp->if_xname);
2275
2276 ifp->if_ioctl_lock = il;
2277
2278 return 0;
2279 }
2280
2281 /*
2282 * This must not be called until after `ifp' has been withdrawn from the
2283 * ifnet tables so that ifioctl() cannot get a handle on it by calling
2284 * ifunit().
2285 */
2286 static void
2287 ifioctl_detach(struct ifnet *ifp)
2288 {
2289 struct ifnet_lock *il;
2290
2291 il = ifp->if_ioctl_lock;
2292 mutex_enter(&il->il_lock);
2293 /* Install if_nullioctl to make sure that any thread that
2294 * subsequently enters the critical section will quit it
2295 * immediately and signal the condition variable that we
2296 * wait on, below.
2297 */
2298 ifp->if_ioctl = if_nullioctl;
2299 /* Sleep while threads are still in the critical section or
2300 * wait to enter it.
2301 */
2302 while (ifnet_lock_entrances(il) != il->il_nexit)
2303 cv_wait(&il->il_emptied, &il->il_lock);
2304 /* At this point, we are the only thread still in the critical
2305 * section, and no new thread can get a handle on the ifioctl
2306 * lock, so it is safe to free its memory.
2307 */
2308 mutex_exit(&il->il_lock);
2309 ifp->if_ioctl_lock = NULL;
2310 percpu_free(il->il_nenter, sizeof(uint64_t));
2311 il->il_nenter = NULL;
2312 cv_destroy(&il->il_emptied);
2313 mutex_destroy(&il->il_lock);
2314 kmem_free(il, sizeof(*il));
2315 }
2316
2317 /*
2318 * Return interface configuration
2319 * of system. List may be used
2320 * in later ioctl's (above) to get
2321 * other information.
2322 *
2323 * Each record is a struct ifreq. Before the addition of
2324 * sockaddr_storage, the API rule was that sockaddr flavors that did
2325 * not fit would extend beyond the struct ifreq, with the next struct
2326 * ifreq starting sa_len beyond the struct sockaddr. Because the
2327 * union in struct ifreq includes struct sockaddr_storage, every kind
2328 * of sockaddr must fit. Thus, there are no longer any overlength
2329 * records.
2330 *
2331 * Records are added to the user buffer if they fit, and ifc_len is
2332 * adjusted to the length that was written. Thus, the user is only
2333 * assured of getting the complete list if ifc_len on return is at
2334 * least sizeof(struct ifreq) less than it was on entry.
2335 *
2336 * If the user buffer pointer is NULL, this routine copies no data and
2337 * returns the amount of space that would be needed.
2338 *
2339 * Invariants:
2340 * ifrp points to the next part of the user's buffer to be used. If
2341 * ifrp != NULL, space holds the number of bytes remaining that we may
2342 * write at ifrp. Otherwise, space holds the number of bytes that
2343 * would have been written had there been adequate space.
2344 */
2345 /*ARGSUSED*/
2346 static int
2347 ifconf(u_long cmd, void *data)
2348 {
2349 struct ifconf *ifc = (struct ifconf *)data;
2350 struct ifnet *ifp;
2351 struct ifaddr *ifa;
2352 struct ifreq ifr, *ifrp = NULL;
2353 int space = 0, error = 0;
2354 const int sz = (int)sizeof(struct ifreq);
2355 const bool docopy = ifc->ifc_req != NULL;
2356
2357 if (docopy) {
2358 space = ifc->ifc_len;
2359 ifrp = ifc->ifc_req;
2360 }
2361
2362 IFNET_FOREACH(ifp) {
2363 (void)strncpy(ifr.ifr_name, ifp->if_xname,
2364 sizeof(ifr.ifr_name));
2365 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
2366 return ENAMETOOLONG;
2367 if (IFADDR_EMPTY(ifp)) {
2368 /* Interface with no addresses - send zero sockaddr. */
2369 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2370 if (!docopy) {
2371 space += sz;
2372 continue;
2373 }
2374 if (space >= sz) {
2375 error = copyout(&ifr, ifrp, sz);
2376 if (error != 0)
2377 return error;
2378 ifrp++;
2379 space -= sz;
2380 }
2381 }
2382
2383 IFADDR_FOREACH(ifa, ifp) {
2384 struct sockaddr *sa = ifa->ifa_addr;
2385 /* all sockaddrs must fit in sockaddr_storage */
2386 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
2387
2388 if (!docopy) {
2389 space += sz;
2390 continue;
2391 }
2392 memcpy(&ifr.ifr_space, sa, sa->sa_len);
2393 if (space >= sz) {
2394 error = copyout(&ifr, ifrp, sz);
2395 if (error != 0)
2396 return (error);
2397 ifrp++; space -= sz;
2398 }
2399 }
2400 }
2401 if (docopy) {
2402 KASSERT(0 <= space && space <= ifc->ifc_len);
2403 ifc->ifc_len -= space;
2404 } else {
2405 KASSERT(space >= 0);
2406 ifc->ifc_len = space;
2407 }
2408 return (0);
2409 }
2410
2411 int
2412 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
2413 {
2414 uint8_t len;
2415 #ifdef COMPAT_OIFREQ
2416 struct ifreq ifrb;
2417 struct oifreq *oifr = NULL;
2418 u_long ocmd = cmd;
2419 cmd = compat_cvtcmd(cmd);
2420 if (cmd != ocmd) {
2421 oifr = (struct oifreq *)(void *)ifr;
2422 ifr = &ifrb;
2423 ifreqo2n(oifr, ifr);
2424 len = sizeof(oifr->ifr_addr);
2425 } else
2426 #endif
2427 len = sizeof(ifr->ifr_ifru.ifru_space);
2428
2429 if (len < sa->sa_len)
2430 return EFBIG;
2431
2432 memset(&ifr->ifr_addr, 0, len);
2433 sockaddr_copy(&ifr->ifr_addr, len, sa);
2434
2435 #ifdef COMPAT_OIFREQ
2436 if (cmd != ocmd)
2437 ifreqn2o(oifr, ifr);
2438 #endif
2439 return 0;
2440 }
2441
2442 /*
2443 * Queue message on interface, and start output if interface
2444 * not yet active.
2445 */
2446 int
2447 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
2448 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
2449 {
2450 int len = m->m_pkthdr.len;
2451 int mflags = m->m_flags;
2452 int s = splnet();
2453 int error;
2454
2455 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
2456 if (error != 0)
2457 goto out;
2458 ifp->if_obytes += len;
2459 if (mflags & M_MCAST)
2460 ifp->if_omcasts++;
2461 if ((ifp->if_flags & IFF_OACTIVE) == 0)
2462 (*ifp->if_start)(ifp);
2463 out:
2464 splx(s);
2465 return error;
2466 }
2467
2468 /*
2469 * Queue message on interface, possibly using a second fast queue
2470 */
2471 int
2472 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
2473 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
2474 {
2475 int error = 0;
2476
2477 if (ifq != NULL
2478 #ifdef ALTQ
2479 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
2480 #endif
2481 ) {
2482 if (IF_QFULL(ifq)) {
2483 IF_DROP(&ifp->if_snd);
2484 m_freem(m);
2485 if (error == 0)
2486 error = ENOBUFS;
2487 } else
2488 IF_ENQUEUE(ifq, m);
2489 } else
2490 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
2491 if (error != 0) {
2492 ++ifp->if_oerrors;
2493 return error;
2494 }
2495 return 0;
2496 }
2497
2498 int
2499 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
2500 {
2501 int rc;
2502
2503 if (ifp->if_initaddr != NULL)
2504 rc = (*ifp->if_initaddr)(ifp, ifa, src);
2505 else if (src ||
2506 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
2507 rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
2508
2509 return rc;
2510 }
2511
2512 int
2513 if_do_dad(struct ifnet *ifp)
2514 {
2515 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2516 return 0;
2517
2518 switch (ifp->if_type) {
2519 case IFT_FAITH:
2520 /*
2521 * These interfaces do not have the IFF_LOOPBACK flag,
2522 * but loop packets back. We do not have to do DAD on such
2523 * interfaces. We should even omit it, because loop-backed
2524 * responses would confuse the DAD procedure.
2525 */
2526 return 0;
2527 default:
2528 /*
2529 * Our DAD routine requires the interface up and running.
2530 * However, some interfaces can be up before the RUNNING
2531 * status. Additionaly, users may try to assign addresses
2532 * before the interface becomes up (or running).
2533 * We simply skip DAD in such a case as a work around.
2534 * XXX: we should rather mark "tentative" on such addresses,
2535 * and do DAD after the interface becomes ready.
2536 */
2537 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2538 (IFF_UP|IFF_RUNNING))
2539 return 0;
2540
2541 return 1;
2542 }
2543 }
2544
2545 int
2546 if_flags_set(ifnet_t *ifp, const short flags)
2547 {
2548 int rc;
2549
2550 if (ifp->if_setflags != NULL)
2551 rc = (*ifp->if_setflags)(ifp, flags);
2552 else {
2553 short cantflags, chgdflags;
2554 struct ifreq ifr;
2555
2556 chgdflags = ifp->if_flags ^ flags;
2557 cantflags = chgdflags & IFF_CANTCHANGE;
2558
2559 if (cantflags != 0)
2560 ifp->if_flags ^= cantflags;
2561
2562 /* Traditionally, we do not call if_ioctl after
2563 * setting/clearing only IFF_PROMISC if the interface
2564 * isn't IFF_UP. Uphold that tradition.
2565 */
2566 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
2567 return 0;
2568
2569 memset(&ifr, 0, sizeof(ifr));
2570
2571 ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
2572 rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
2573
2574 if (rc != 0 && cantflags != 0)
2575 ifp->if_flags ^= cantflags;
2576 }
2577
2578 return rc;
2579 }
2580
2581 int
2582 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
2583 {
2584 int rc;
2585 struct ifreq ifr;
2586
2587 if (ifp->if_mcastop != NULL)
2588 rc = (*ifp->if_mcastop)(ifp, cmd, sa);
2589 else {
2590 ifreq_setaddr(cmd, &ifr, sa);
2591 rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
2592 }
2593
2594 return rc;
2595 }
2596
2597 static void
2598 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
2599 struct ifaltq *ifq)
2600 {
2601 const struct sysctlnode *cnode, *rnode;
2602
2603 if (sysctl_createv(clog, 0, NULL, &rnode,
2604 CTLFLAG_PERMANENT,
2605 CTLTYPE_NODE, "interfaces",
2606 SYSCTL_DESCR("Per-interface controls"),
2607 NULL, 0, NULL, 0,
2608 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
2609 goto bad;
2610
2611 if (sysctl_createv(clog, 0, &rnode, &rnode,
2612 CTLFLAG_PERMANENT,
2613 CTLTYPE_NODE, ifname,
2614 SYSCTL_DESCR("Interface controls"),
2615 NULL, 0, NULL, 0,
2616 CTL_CREATE, CTL_EOL) != 0)
2617 goto bad;
2618
2619 if (sysctl_createv(clog, 0, &rnode, &rnode,
2620 CTLFLAG_PERMANENT,
2621 CTLTYPE_NODE, "sndq",
2622 SYSCTL_DESCR("Interface output queue controls"),
2623 NULL, 0, NULL, 0,
2624 CTL_CREATE, CTL_EOL) != 0)
2625 goto bad;
2626
2627 if (sysctl_createv(clog, 0, &rnode, &cnode,
2628 CTLFLAG_PERMANENT,
2629 CTLTYPE_INT, "len",
2630 SYSCTL_DESCR("Current output queue length"),
2631 NULL, 0, &ifq->ifq_len, 0,
2632 CTL_CREATE, CTL_EOL) != 0)
2633 goto bad;
2634
2635 if (sysctl_createv(clog, 0, &rnode, &cnode,
2636 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2637 CTLTYPE_INT, "maxlen",
2638 SYSCTL_DESCR("Maximum allowed output queue length"),
2639 NULL, 0, &ifq->ifq_maxlen, 0,
2640 CTL_CREATE, CTL_EOL) != 0)
2641 goto bad;
2642
2643 if (sysctl_createv(clog, 0, &rnode, &cnode,
2644 CTLFLAG_PERMANENT,
2645 CTLTYPE_INT, "drops",
2646 SYSCTL_DESCR("Packets dropped due to full output queue"),
2647 NULL, 0, &ifq->ifq_drops, 0,
2648 CTL_CREATE, CTL_EOL) != 0)
2649 goto bad;
2650
2651 return;
2652 bad:
2653 printf("%s: could not attach sysctl nodes\n", ifname);
2654 return;
2655 }
2656
2657 #if defined(INET) || defined(INET6)
2658
2659 #define SYSCTL_NET_PKTQ(q, cn, c) \
2660 static int \
2661 sysctl_net_##q##_##cn(SYSCTLFN_ARGS) \
2662 { \
2663 return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c); \
2664 }
2665
2666 #if defined(INET)
2667 static int
2668 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
2669 {
2670 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
2671 }
2672 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
2673 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
2674 #endif
2675
2676 #if defined(INET6)
2677 static int
2678 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
2679 {
2680 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
2681 }
2682 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
2683 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
2684 #endif
2685
2686 static void
2687 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
2688 {
2689 sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
2690 const char *pfname = NULL, *ipname = NULL;
2691 int ipn = 0, qid = 0;
2692
2693 switch (pf) {
2694 #if defined(INET)
2695 case PF_INET:
2696 len_func = sysctl_net_ip_pktq_items;
2697 maxlen_func = sysctl_net_ip_pktq_maxlen;
2698 drops_func = sysctl_net_ip_pktq_drops;
2699 pfname = "inet", ipn = IPPROTO_IP;
2700 ipname = "ip", qid = IPCTL_IFQ;
2701 break;
2702 #endif
2703 #if defined(INET6)
2704 case PF_INET6:
2705 len_func = sysctl_net_ip6_pktq_items;
2706 maxlen_func = sysctl_net_ip6_pktq_maxlen;
2707 drops_func = sysctl_net_ip6_pktq_drops;
2708 pfname = "inet6", ipn = IPPROTO_IPV6;
2709 ipname = "ip6", qid = IPV6CTL_IFQ;
2710 break;
2711 #endif
2712 default:
2713 KASSERT(false);
2714 }
2715
2716 sysctl_createv(clog, 0, NULL, NULL,
2717 CTLFLAG_PERMANENT,
2718 CTLTYPE_NODE, pfname, NULL,
2719 NULL, 0, NULL, 0,
2720 CTL_NET, pf, CTL_EOL);
2721 sysctl_createv(clog, 0, NULL, NULL,
2722 CTLFLAG_PERMANENT,
2723 CTLTYPE_NODE, ipname, NULL,
2724 NULL, 0, NULL, 0,
2725 CTL_NET, pf, ipn, CTL_EOL);
2726 sysctl_createv(clog, 0, NULL, NULL,
2727 CTLFLAG_PERMANENT,
2728 CTLTYPE_NODE, "ifq",
2729 SYSCTL_DESCR("Protocol input queue controls"),
2730 NULL, 0, NULL, 0,
2731 CTL_NET, pf, ipn, qid, CTL_EOL);
2732
2733 sysctl_createv(clog, 0, NULL, NULL,
2734 CTLFLAG_PERMANENT,
2735 CTLTYPE_INT, "len",
2736 SYSCTL_DESCR("Current input queue length"),
2737 len_func, 0, NULL, 0,
2738 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
2739 sysctl_createv(clog, 0, NULL, NULL,
2740 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2741 CTLTYPE_INT, "maxlen",
2742 SYSCTL_DESCR("Maximum allowed input queue length"),
2743 maxlen_func, 0, NULL, 0,
2744 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
2745 sysctl_createv(clog, 0, NULL, NULL,
2746 CTLFLAG_PERMANENT,
2747 CTLTYPE_INT, "drops",
2748 SYSCTL_DESCR("Packets dropped due to full input queue"),
2749 drops_func, 0, NULL, 0,
2750 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
2751 }
2752 #endif /* INET || INET6 */
2753
2754 static int
2755 if_sdl_sysctl(SYSCTLFN_ARGS)
2756 {
2757 struct ifnet *ifp;
2758 const struct sockaddr_dl *sdl;
2759
2760 if (namelen != 1)
2761 return EINVAL;
2762
2763 ifp = if_byindex(name[0]);
2764 if (ifp == NULL)
2765 return ENODEV;
2766
2767 sdl = ifp->if_sadl;
2768 if (sdl == NULL) {
2769 *oldlenp = 0;
2770 return 0;
2771 }
2772
2773 if (oldp == NULL) {
2774 *oldlenp = sdl->sdl_alen;
2775 return 0;
2776 }
2777
2778 if (*oldlenp >= sdl->sdl_alen)
2779 *oldlenp = sdl->sdl_alen;
2780 return sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
2781 }
2782
2783 SYSCTL_SETUP(sysctl_net_sdl_setup, "sysctl net.sdl subtree setup")
2784 {
2785 const struct sysctlnode *rnode = NULL;
2786
2787 sysctl_createv(clog, 0, NULL, &rnode,
2788 CTLFLAG_PERMANENT,
2789 CTLTYPE_NODE, "sdl",
2790 SYSCTL_DESCR("Get active link-layer address"),
2791 if_sdl_sysctl, 0, NULL, 0,
2792 CTL_NET, CTL_CREATE, CTL_EOL);
2793 }
2794