if.c revision 1.522 1 /* $NetBSD: if.c,v 1.522 2022/09/02 04:34:58 thorpej 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.522 2022/09/02 04:34:58 thorpej Exp $");
94
95 #if defined(_KERNEL_OPT)
96 #include "opt_inet.h"
97 #include "opt_ipsec.h"
98 #include "opt_atalk.h"
99 #include "opt_wlan.h"
100 #include "opt_net_mpsafe.h"
101 #include "opt_mrouting.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 #include <sys/module_hook.h>
123 #include <sys/compat_stub.h>
124 #include <sys/msan.h>
125 #include <sys/hook.h>
126
127 #include <net/if.h>
128 #include <net/if_dl.h>
129 #include <net/if_ether.h>
130 #include <net/if_media.h>
131 #include <net80211/ieee80211.h>
132 #include <net80211/ieee80211_ioctl.h>
133 #include <net/if_types.h>
134 #include <net/route.h>
135 #include <net/netisr.h>
136 #include <sys/module.h>
137 #ifdef NETATALK
138 #include <netatalk/at_extern.h>
139 #include <netatalk/at.h>
140 #endif
141 #include <net/pfil.h>
142 #include <netinet/in.h>
143 #include <netinet/in_var.h>
144 #include <netinet/ip_encap.h>
145 #include <net/bpf.h>
146
147 #ifdef INET6
148 #include <netinet6/in6_var.h>
149 #include <netinet6/nd6.h>
150 #endif
151
152 #include "ether.h"
153
154 #include "bridge.h"
155 #if NBRIDGE > 0
156 #include <net/if_bridgevar.h>
157 #endif
158
159 #include "carp.h"
160 #if NCARP > 0
161 #include <netinet/ip_carp.h>
162 #endif
163
164 #include <compat/sys/sockio.h>
165
166 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
167 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
168
169 /*
170 * XXX reusing (ifp)->if_snd->ifq_lock rather than having another spin mutex
171 * for each ifnet. It doesn't matter because:
172 * - if IFEF_MPSAFE is enabled, if_snd isn't used and lock contentions on
173 * ifq_lock don't happen
174 * - if IFEF_MPSAFE is disabled, there is no lock contention on ifq_lock
175 * because if_snd, if_link_state_change and if_link_state_change_process
176 * are all called with KERNEL_LOCK
177 */
178 #define IF_LINK_STATE_CHANGE_LOCK(ifp) \
179 mutex_enter((ifp)->if_snd.ifq_lock)
180 #define IF_LINK_STATE_CHANGE_UNLOCK(ifp) \
181 mutex_exit((ifp)->if_snd.ifq_lock)
182
183 /*
184 * Global list of interfaces.
185 */
186 /* DEPRECATED. Remove it once kvm(3) users disappeared */
187 struct ifnet_head ifnet_list;
188
189 struct pslist_head ifnet_pslist;
190 static ifnet_t ** ifindex2ifnet = NULL;
191 static u_int if_index = 1;
192 static size_t if_indexlim = 0;
193 static uint64_t index_gen;
194 /* Mutex to protect the above objects. */
195 kmutex_t ifnet_mtx __cacheline_aligned;
196 static struct psref_class *ifnet_psref_class __read_mostly;
197 static pserialize_t ifnet_psz;
198 static struct workqueue *ifnet_link_state_wq __read_mostly;
199
200 static struct workqueue *if_slowtimo_wq __read_mostly;
201
202 static kmutex_t if_clone_mtx;
203
204 struct ifnet *lo0ifp;
205 int ifqmaxlen = IFQ_MAXLEN;
206
207 struct psref_class *ifa_psref_class __read_mostly;
208
209 static int if_delroute_matcher(struct rtentry *, void *);
210
211 static bool if_is_unit(const char *);
212 static struct if_clone *if_clone_lookup(const char *, int *);
213
214 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
215 static int if_cloners_count;
216
217 /* Packet filtering hook for interfaces. */
218 pfil_head_t * if_pfil __read_mostly;
219
220 static kauth_listener_t if_listener;
221
222 static int doifioctl(struct socket *, u_long, void *, struct lwp *);
223 static void if_detach_queues(struct ifnet *, struct ifqueue *);
224 static void sysctl_sndq_setup(struct sysctllog **, const char *,
225 struct ifaltq *);
226 static void if_slowtimo_intr(void *);
227 static void if_slowtimo_work(struct work *, void *);
228 static int sysctl_if_watchdog(SYSCTLFN_PROTO);
229 static void sysctl_watchdog_setup(struct ifnet *);
230 static void if_attachdomain1(struct ifnet *);
231 static int ifconf(u_long, void *);
232 static int if_transmit(struct ifnet *, struct mbuf *);
233 static int if_clone_create(const char *);
234 static int if_clone_destroy(const char *);
235 static void if_link_state_change_work(struct work *, void *);
236 static void if_up_locked(struct ifnet *);
237 static void _if_down(struct ifnet *);
238 static void if_down_deactivated(struct ifnet *);
239
240 struct if_percpuq {
241 struct ifnet *ipq_ifp;
242 void *ipq_si;
243 struct percpu *ipq_ifqs; /* struct ifqueue */
244 };
245
246 static struct mbuf *if_percpuq_dequeue(struct if_percpuq *);
247
248 static void if_percpuq_drops(void *, void *, struct cpu_info *);
249 static int sysctl_percpuq_drops_handler(SYSCTLFN_PROTO);
250 static void sysctl_percpuq_setup(struct sysctllog **, const char *,
251 struct if_percpuq *);
252
253 struct if_deferred_start {
254 struct ifnet *ids_ifp;
255 void (*ids_if_start)(struct ifnet *);
256 void *ids_si;
257 };
258
259 static void if_deferred_start_softint(void *);
260 static void if_deferred_start_common(struct ifnet *);
261 static void if_deferred_start_destroy(struct ifnet *);
262
263 struct if_slowtimo_data {
264 kmutex_t isd_lock;
265 struct callout isd_ch;
266 struct work isd_work;
267 struct ifnet *isd_ifp;
268 bool isd_queued;
269 bool isd_dying;
270 bool isd_trigger;
271 };
272
273 /*
274 * Hook for if_vlan - needed by if_agr
275 */
276 struct if_vlan_vlan_input_hook_t if_vlan_vlan_input_hook;
277
278 static void if_sysctl_setup(struct sysctllog **);
279
280 static int
281 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
282 void *arg0, void *arg1, void *arg2, void *arg3)
283 {
284 int result;
285 enum kauth_network_req req;
286
287 result = KAUTH_RESULT_DEFER;
288 req = (enum kauth_network_req)(uintptr_t)arg1;
289
290 if (action != KAUTH_NETWORK_INTERFACE)
291 return result;
292
293 if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
294 (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
295 result = KAUTH_RESULT_ALLOW;
296
297 return result;
298 }
299
300 /*
301 * Network interface utility routines.
302 *
303 * Routines with ifa_ifwith* names take sockaddr *'s as
304 * parameters.
305 */
306 void
307 ifinit(void)
308 {
309
310 #if (defined(INET) || defined(INET6))
311 encapinit();
312 #endif
313
314 if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
315 if_listener_cb, NULL);
316
317 /* interfaces are available, inform socket code */
318 ifioctl = doifioctl;
319 }
320
321 /*
322 * XXX Initialization before configure().
323 * XXX hack to get pfil_add_hook working in autoconf.
324 */
325 void
326 ifinit1(void)
327 {
328 int error __diagused;
329
330 #ifdef NET_MPSAFE
331 printf("NET_MPSAFE enabled\n");
332 #endif
333
334 mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE);
335
336 TAILQ_INIT(&ifnet_list);
337 mutex_init(&ifnet_mtx, MUTEX_DEFAULT, IPL_NONE);
338 ifnet_psz = pserialize_create();
339 ifnet_psref_class = psref_class_create("ifnet", IPL_SOFTNET);
340 ifa_psref_class = psref_class_create("ifa", IPL_SOFTNET);
341 error = workqueue_create(&ifnet_link_state_wq, "iflnkst",
342 if_link_state_change_work, NULL, PRI_SOFTNET, IPL_NET,
343 WQ_MPSAFE);
344 KASSERT(error == 0);
345 PSLIST_INIT(&ifnet_pslist);
346
347 error = workqueue_create(&if_slowtimo_wq, "ifwdog",
348 if_slowtimo_work, NULL, PRI_SOFTNET, IPL_SOFTCLOCK, WQ_MPSAFE);
349 KASSERTMSG(error == 0, "error=%d", error);
350
351 if_indexlim = 8;
352
353 if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL);
354 KASSERT(if_pfil != NULL);
355
356 #if NETHER > 0 || defined(NETATALK) || defined(WLAN)
357 etherinit();
358 #endif
359 }
360
361 /* XXX must be after domaininit() */
362 void
363 ifinit_post(void)
364 {
365
366 if_sysctl_setup(NULL);
367 }
368
369 ifnet_t *
370 if_alloc(u_char type)
371 {
372 return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP);
373 }
374
375 void
376 if_free(ifnet_t *ifp)
377 {
378 kmem_free(ifp, sizeof(ifnet_t));
379 }
380
381 void
382 if_initname(struct ifnet *ifp, const char *name, int unit)
383 {
384 (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
385 "%s%d", name, unit);
386 }
387
388 /*
389 * Null routines used while an interface is going away. These routines
390 * just return an error.
391 */
392
393 int
394 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
395 const struct sockaddr *so, const struct rtentry *rt)
396 {
397
398 return ENXIO;
399 }
400
401 void
402 if_nullinput(struct ifnet *ifp, struct mbuf *m)
403 {
404
405 /* Nothing. */
406 }
407
408 void
409 if_nullstart(struct ifnet *ifp)
410 {
411
412 /* Nothing. */
413 }
414
415 int
416 if_nulltransmit(struct ifnet *ifp, struct mbuf *m)
417 {
418
419 m_freem(m);
420 return ENXIO;
421 }
422
423 int
424 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
425 {
426
427 return ENXIO;
428 }
429
430 int
431 if_nullinit(struct ifnet *ifp)
432 {
433
434 return ENXIO;
435 }
436
437 void
438 if_nullstop(struct ifnet *ifp, int disable)
439 {
440
441 /* Nothing. */
442 }
443
444 void
445 if_nullslowtimo(struct ifnet *ifp)
446 {
447
448 /* Nothing. */
449 }
450
451 void
452 if_nulldrain(struct ifnet *ifp)
453 {
454
455 /* Nothing. */
456 }
457
458 void
459 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
460 {
461 struct ifaddr *ifa;
462 struct sockaddr_dl *sdl;
463
464 ifp->if_addrlen = addrlen;
465 if_alloc_sadl(ifp);
466 ifa = ifp->if_dl;
467 sdl = satosdl(ifa->ifa_addr);
468
469 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
470 if (factory) {
471 KASSERT(ifp->if_hwdl == NULL);
472 ifp->if_hwdl = ifp->if_dl;
473 ifaref(ifp->if_hwdl);
474 }
475 /* TBD routing socket */
476 }
477
478 struct ifaddr *
479 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
480 {
481 unsigned socksize, ifasize;
482 int addrlen, namelen;
483 struct sockaddr_dl *mask, *sdl;
484 struct ifaddr *ifa;
485
486 namelen = strlen(ifp->if_xname);
487 addrlen = ifp->if_addrlen;
488 socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
489 ifasize = sizeof(*ifa) + 2 * socksize;
490 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
491
492 sdl = (struct sockaddr_dl *)(ifa + 1);
493 mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
494
495 sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
496 ifp->if_xname, namelen, NULL, addrlen);
497 mask->sdl_family = AF_LINK;
498 mask->sdl_len = sockaddr_dl_measure(namelen, 0);
499 memset(&mask->sdl_data[0], 0xff, namelen);
500 ifa->ifa_rtrequest = link_rtrequest;
501 ifa->ifa_addr = (struct sockaddr *)sdl;
502 ifa->ifa_netmask = (struct sockaddr *)mask;
503 ifa_psref_init(ifa);
504
505 *sdlp = sdl;
506
507 return ifa;
508 }
509
510 static void
511 if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
512 {
513 const struct sockaddr_dl *sdl;
514
515 ifp->if_dl = ifa;
516 ifaref(ifa);
517 sdl = satosdl(ifa->ifa_addr);
518 ifp->if_sadl = sdl;
519 }
520
521 /*
522 * Allocate the link level name for the specified interface. This
523 * is an attachment helper. It must be called after ifp->if_addrlen
524 * is initialized, which may not be the case when if_attach() is
525 * called.
526 */
527 void
528 if_alloc_sadl(struct ifnet *ifp)
529 {
530 struct ifaddr *ifa;
531 const struct sockaddr_dl *sdl;
532
533 /*
534 * If the interface already has a link name, release it
535 * now. This is useful for interfaces that can change
536 * link types, and thus switch link names often.
537 */
538 if (ifp->if_sadl != NULL)
539 if_free_sadl(ifp, 0);
540
541 ifa = if_dl_create(ifp, &sdl);
542
543 ifa_insert(ifp, ifa);
544 if_sadl_setrefs(ifp, ifa);
545 }
546
547 static void
548 if_deactivate_sadl(struct ifnet *ifp)
549 {
550 struct ifaddr *ifa;
551
552 KASSERT(ifp->if_dl != NULL);
553
554 ifa = ifp->if_dl;
555
556 ifp->if_sadl = NULL;
557
558 ifp->if_dl = NULL;
559 ifafree(ifa);
560 }
561
562 static void
563 if_replace_sadl(struct ifnet *ifp, struct ifaddr *ifa)
564 {
565 struct ifaddr *old;
566
567 KASSERT(ifp->if_dl != NULL);
568
569 old = ifp->if_dl;
570
571 ifaref(ifa);
572 /* XXX Update if_dl and if_sadl atomically */
573 ifp->if_dl = ifa;
574 ifp->if_sadl = satosdl(ifa->ifa_addr);
575
576 ifafree(old);
577 }
578
579 void
580 if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa0,
581 const struct sockaddr_dl *sdl)
582 {
583 struct ifaddr *ifa;
584 const int bound = curlwp_bind();
585
586 KASSERT(ifa_held(ifa0));
587
588 const int s = splsoftnet();
589
590 if_replace_sadl(ifp, ifa0);
591
592 int ss = pserialize_read_enter();
593 IFADDR_READER_FOREACH(ifa, ifp) {
594 struct psref psref;
595 ifa_acquire(ifa, &psref);
596 pserialize_read_exit(ss);
597
598 rtinit(ifa, RTM_LLINFO_UPD, 0);
599
600 ss = pserialize_read_enter();
601 ifa_release(ifa, &psref);
602 }
603 pserialize_read_exit(ss);
604
605 splx(s);
606 curlwp_bindx(bound);
607 }
608
609 /*
610 * Free the link level name for the specified interface. This is
611 * a detach helper. This is called from if_detach().
612 */
613 void
614 if_free_sadl(struct ifnet *ifp, int factory)
615 {
616 struct ifaddr *ifa;
617
618 if (factory && ifp->if_hwdl != NULL) {
619 ifa = ifp->if_hwdl;
620 ifp->if_hwdl = NULL;
621 ifafree(ifa);
622 }
623
624 ifa = ifp->if_dl;
625 if (ifa == NULL) {
626 KASSERT(ifp->if_sadl == NULL);
627 return;
628 }
629
630 KASSERT(ifp->if_sadl != NULL);
631
632 const int s = splsoftnet();
633 KASSERT(ifa->ifa_addr->sa_family == AF_LINK);
634 ifa_remove(ifp, ifa);
635 if_deactivate_sadl(ifp);
636 splx(s);
637 }
638
639 static void
640 if_getindex(ifnet_t *ifp)
641 {
642 bool hitlimit = false;
643 char xnamebuf[HOOKNAMSIZ];
644
645 ifp->if_index_gen = index_gen++;
646 snprintf(xnamebuf, sizeof(xnamebuf),
647 "%s-lshk", ifp->if_xname);
648 ifp->if_linkstate_hooks = simplehook_create(IPL_NET,
649 xnamebuf);
650
651 ifp->if_index = if_index;
652 if (ifindex2ifnet == NULL) {
653 if_index++;
654 goto skip;
655 }
656 while (if_byindex(ifp->if_index)) {
657 /*
658 * If we hit USHRT_MAX, we skip back to 0 since
659 * there are a number of places where the value
660 * of if_index or if_index itself is compared
661 * to or stored in an unsigned short. By
662 * jumping back, we won't botch those assignments
663 * or comparisons.
664 */
665 if (++if_index == 0) {
666 if_index = 1;
667 } else if (if_index == USHRT_MAX) {
668 /*
669 * However, if we have to jump back to
670 * zero *twice* without finding an empty
671 * slot in ifindex2ifnet[], then there
672 * there are too many (>65535) interfaces.
673 */
674 if (hitlimit) {
675 panic("too many interfaces");
676 }
677 hitlimit = true;
678 if_index = 1;
679 }
680 ifp->if_index = if_index;
681 }
682 skip:
683 /*
684 * ifindex2ifnet is indexed by if_index. Since if_index will
685 * grow dynamically, it should grow too.
686 */
687 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
688 size_t m, n, oldlim;
689 void *q;
690
691 oldlim = if_indexlim;
692 while (ifp->if_index >= if_indexlim)
693 if_indexlim <<= 1;
694
695 /* grow ifindex2ifnet */
696 m = oldlim * sizeof(struct ifnet *);
697 n = if_indexlim * sizeof(struct ifnet *);
698 q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
699 if (ifindex2ifnet != NULL) {
700 memcpy(q, ifindex2ifnet, m);
701 free(ifindex2ifnet, M_IFADDR);
702 }
703 ifindex2ifnet = (struct ifnet **)q;
704 }
705 ifindex2ifnet[ifp->if_index] = ifp;
706 }
707
708 /*
709 * Initialize an interface and assign an index for it.
710 *
711 * It must be called prior to a device specific attach routine
712 * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl,
713 * and be followed by if_register:
714 *
715 * if_initialize(ifp);
716 * ether_ifattach(ifp, enaddr);
717 * if_register(ifp);
718 */
719 void
720 if_initialize(ifnet_t *ifp)
721 {
722
723 KASSERT(if_indexlim > 0);
724 TAILQ_INIT(&ifp->if_addrlist);
725
726 /*
727 * Link level name is allocated later by a separate call to
728 * if_alloc_sadl().
729 */
730
731 if (ifp->if_snd.ifq_maxlen == 0)
732 ifp->if_snd.ifq_maxlen = ifqmaxlen;
733
734 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
735
736 ifp->if_link_state = LINK_STATE_UNKNOWN;
737 ifp->if_link_queue = -1; /* all bits set, see link_state_change() */
738 ifp->if_link_scheduled = false;
739
740 ifp->if_capenable = 0;
741 ifp->if_csum_flags_tx = 0;
742 ifp->if_csum_flags_rx = 0;
743
744 #ifdef ALTQ
745 ifp->if_snd.altq_type = 0;
746 ifp->if_snd.altq_disc = NULL;
747 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
748 ifp->if_snd.altq_tbr = NULL;
749 ifp->if_snd.altq_ifp = ifp;
750 #endif
751
752 IFQ_LOCK_INIT(&ifp->if_snd);
753
754 ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp);
755 pfil_run_ifhooks(if_pfil, PFIL_IFNET_ATTACH, ifp);
756
757 IF_AFDATA_LOCK_INIT(ifp);
758
759 PSLIST_ENTRY_INIT(ifp, if_pslist_entry);
760 PSLIST_INIT(&ifp->if_addr_pslist);
761 psref_target_init(&ifp->if_psref, ifnet_psref_class);
762 ifp->if_ioctl_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
763 LIST_INIT(&ifp->if_multiaddrs);
764 if_stats_init(ifp);
765
766 IFNET_GLOBAL_LOCK();
767 if_getindex(ifp);
768 IFNET_GLOBAL_UNLOCK();
769 }
770
771 /*
772 * Register an interface to the list of "active" interfaces.
773 */
774 void
775 if_register(ifnet_t *ifp)
776 {
777 /*
778 * If the driver has not supplied its own if_ioctl or if_stop,
779 * then supply the default.
780 */
781 if (ifp->if_ioctl == NULL)
782 ifp->if_ioctl = ifioctl_common;
783 if (ifp->if_stop == NULL)
784 ifp->if_stop = if_nullstop;
785
786 sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
787
788 if (!STAILQ_EMPTY(&domains))
789 if_attachdomain1(ifp);
790
791 /* Announce the interface. */
792 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
793
794 if (ifp->if_slowtimo != NULL) {
795 struct if_slowtimo_data *isd;
796
797 isd = kmem_zalloc(sizeof(*isd), KM_SLEEP);
798 mutex_init(&isd->isd_lock, MUTEX_DEFAULT, IPL_SOFTCLOCK);
799 callout_init(&isd->isd_ch, CALLOUT_MPSAFE);
800 callout_setfunc(&isd->isd_ch, if_slowtimo_intr, ifp);
801 isd->isd_ifp = ifp;
802
803 ifp->if_slowtimo_data = isd;
804
805 if_slowtimo_intr(ifp);
806
807 sysctl_watchdog_setup(ifp);
808 }
809
810 if (ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit)
811 ifp->if_transmit = if_transmit;
812
813 IFNET_GLOBAL_LOCK();
814 TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list);
815 IFNET_WRITER_INSERT_TAIL(ifp);
816 IFNET_GLOBAL_UNLOCK();
817 }
818
819 /*
820 * The if_percpuq framework
821 *
822 * It allows network device drivers to execute the network stack
823 * in softint (so called softint-based if_input). It utilizes
824 * softint and percpu ifqueue. It doesn't distribute any packets
825 * between CPUs, unlike pktqueue(9).
826 *
827 * Currently we support two options for device drivers to apply the framework:
828 * - Use it implicitly with less changes
829 * - If you use if_attach in driver's _attach function and if_input in
830 * driver's Rx interrupt handler, a packet is queued and a softint handles
831 * the packet implicitly
832 * - Use it explicitly in each driver (recommended)
833 * - You can use if_percpuq_* directly in your driver
834 * - In this case, you need to allocate struct if_percpuq in driver's softc
835 * - See wm(4) as a reference implementation
836 */
837
838 static void
839 if_percpuq_softint(void *arg)
840 {
841 struct if_percpuq *ipq = arg;
842 struct ifnet *ifp = ipq->ipq_ifp;
843 struct mbuf *m;
844
845 while ((m = if_percpuq_dequeue(ipq)) != NULL) {
846 if_statinc(ifp, if_ipackets);
847 bpf_mtap(ifp, m, BPF_D_IN);
848
849 ifp->_if_input(ifp, m);
850 }
851 }
852
853 static void
854 if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
855 {
856 struct ifqueue *const ifq = p;
857
858 memset(ifq, 0, sizeof(*ifq));
859 ifq->ifq_maxlen = IFQ_MAXLEN;
860 }
861
862 struct if_percpuq *
863 if_percpuq_create(struct ifnet *ifp)
864 {
865 struct if_percpuq *ipq;
866 u_int flags = SOFTINT_NET;
867
868 flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0;
869
870 ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP);
871 ipq->ipq_ifp = ifp;
872 ipq->ipq_si = softint_establish(flags, if_percpuq_softint, ipq);
873 ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue));
874 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL);
875
876 sysctl_percpuq_setup(&ifp->if_sysctl_log, ifp->if_xname, ipq);
877
878 return ipq;
879 }
880
881 static struct mbuf *
882 if_percpuq_dequeue(struct if_percpuq *ipq)
883 {
884 struct mbuf *m;
885 struct ifqueue *ifq;
886
887 const int s = splnet();
888 ifq = percpu_getref(ipq->ipq_ifqs);
889 IF_DEQUEUE(ifq, m);
890 percpu_putref(ipq->ipq_ifqs);
891 splx(s);
892
893 return m;
894 }
895
896 static void
897 if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
898 {
899 struct ifqueue *const ifq = p;
900
901 IF_PURGE(ifq);
902 }
903
904 void
905 if_percpuq_destroy(struct if_percpuq *ipq)
906 {
907
908 /* if_detach may already destroy it */
909 if (ipq == NULL)
910 return;
911
912 softint_disestablish(ipq->ipq_si);
913 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL);
914 percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue));
915 kmem_free(ipq, sizeof(*ipq));
916 }
917
918 void
919 if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m)
920 {
921 struct ifqueue *ifq;
922
923 KASSERT(ipq != NULL);
924
925 const int s = splnet();
926 ifq = percpu_getref(ipq->ipq_ifqs);
927 if (IF_QFULL(ifq)) {
928 IF_DROP(ifq);
929 percpu_putref(ipq->ipq_ifqs);
930 m_freem(m);
931 goto out;
932 }
933 IF_ENQUEUE(ifq, m);
934 percpu_putref(ipq->ipq_ifqs);
935
936 softint_schedule(ipq->ipq_si);
937 out:
938 splx(s);
939 }
940
941 static void
942 if_percpuq_drops(void *p, void *arg, struct cpu_info *ci __unused)
943 {
944 struct ifqueue *const ifq = p;
945 int *sum = arg;
946
947 *sum += ifq->ifq_drops;
948 }
949
950 static int
951 sysctl_percpuq_drops_handler(SYSCTLFN_ARGS)
952 {
953 struct sysctlnode node;
954 struct if_percpuq *ipq;
955 int sum = 0;
956 int error;
957
958 node = *rnode;
959 ipq = node.sysctl_data;
960
961 percpu_foreach(ipq->ipq_ifqs, if_percpuq_drops, &sum);
962
963 node.sysctl_data = ∑
964 error = sysctl_lookup(SYSCTLFN_CALL(&node));
965 if (error != 0 || newp == NULL)
966 return error;
967
968 return 0;
969 }
970
971 static void
972 sysctl_percpuq_setup(struct sysctllog **clog, const char* ifname,
973 struct if_percpuq *ipq)
974 {
975 const struct sysctlnode *cnode, *rnode;
976
977 if (sysctl_createv(clog, 0, NULL, &rnode,
978 CTLFLAG_PERMANENT,
979 CTLTYPE_NODE, "interfaces",
980 SYSCTL_DESCR("Per-interface controls"),
981 NULL, 0, NULL, 0,
982 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
983 goto bad;
984
985 if (sysctl_createv(clog, 0, &rnode, &rnode,
986 CTLFLAG_PERMANENT,
987 CTLTYPE_NODE, ifname,
988 SYSCTL_DESCR("Interface controls"),
989 NULL, 0, NULL, 0,
990 CTL_CREATE, CTL_EOL) != 0)
991 goto bad;
992
993 if (sysctl_createv(clog, 0, &rnode, &rnode,
994 CTLFLAG_PERMANENT,
995 CTLTYPE_NODE, "rcvq",
996 SYSCTL_DESCR("Interface input queue controls"),
997 NULL, 0, NULL, 0,
998 CTL_CREATE, CTL_EOL) != 0)
999 goto bad;
1000
1001 #ifdef NOTYET
1002 /* XXX Should show each per-CPU queue length? */
1003 if (sysctl_createv(clog, 0, &rnode, &rnode,
1004 CTLFLAG_PERMANENT,
1005 CTLTYPE_INT, "len",
1006 SYSCTL_DESCR("Current input queue length"),
1007 sysctl_percpuq_len, 0, NULL, 0,
1008 CTL_CREATE, CTL_EOL) != 0)
1009 goto bad;
1010
1011 if (sysctl_createv(clog, 0, &rnode, &cnode,
1012 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
1013 CTLTYPE_INT, "maxlen",
1014 SYSCTL_DESCR("Maximum allowed input queue length"),
1015 sysctl_percpuq_maxlen_handler, 0, (void *)ipq, 0,
1016 CTL_CREATE, CTL_EOL) != 0)
1017 goto bad;
1018 #endif
1019
1020 if (sysctl_createv(clog, 0, &rnode, &cnode,
1021 CTLFLAG_PERMANENT,
1022 CTLTYPE_INT, "drops",
1023 SYSCTL_DESCR("Total packets dropped due to full input queue"),
1024 sysctl_percpuq_drops_handler, 0, (void *)ipq, 0,
1025 CTL_CREATE, CTL_EOL) != 0)
1026 goto bad;
1027
1028 return;
1029 bad:
1030 printf("%s: could not attach sysctl nodes\n", ifname);
1031 return;
1032 }
1033
1034 /*
1035 * The deferred if_start framework
1036 *
1037 * The common APIs to defer if_start to softint when if_start is requested
1038 * from a device driver running in hardware interrupt context.
1039 */
1040 /*
1041 * Call ifp->if_start (or equivalent) in a dedicated softint for
1042 * deferred if_start.
1043 */
1044 static void
1045 if_deferred_start_softint(void *arg)
1046 {
1047 struct if_deferred_start *ids = arg;
1048 struct ifnet *ifp = ids->ids_ifp;
1049
1050 ids->ids_if_start(ifp);
1051 }
1052
1053 /*
1054 * The default callback function for deferred if_start.
1055 */
1056 static void
1057 if_deferred_start_common(struct ifnet *ifp)
1058 {
1059 const int s = splnet();
1060 if_start_lock(ifp);
1061 splx(s);
1062 }
1063
1064 static inline bool
1065 if_snd_is_used(struct ifnet *ifp)
1066 {
1067
1068 return ALTQ_IS_ENABLED(&ifp->if_snd) ||
1069 ifp->if_transmit == if_transmit ||
1070 ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit;
1071 }
1072
1073 /*
1074 * Schedule deferred if_start.
1075 */
1076 void
1077 if_schedule_deferred_start(struct ifnet *ifp)
1078 {
1079
1080 KASSERT(ifp->if_deferred_start != NULL);
1081
1082 if (if_snd_is_used(ifp) && IFQ_IS_EMPTY(&ifp->if_snd))
1083 return;
1084
1085 softint_schedule(ifp->if_deferred_start->ids_si);
1086 }
1087
1088 /*
1089 * Create an instance of deferred if_start. A driver should call the function
1090 * only if the driver needs deferred if_start. Drivers can setup their own
1091 * deferred if_start function via 2nd argument.
1092 */
1093 void
1094 if_deferred_start_init(struct ifnet *ifp, void (*func)(struct ifnet *))
1095 {
1096 struct if_deferred_start *ids;
1097 u_int flags = SOFTINT_NET;
1098
1099 flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0;
1100
1101 ids = kmem_zalloc(sizeof(*ids), KM_SLEEP);
1102 ids->ids_ifp = ifp;
1103 ids->ids_si = softint_establish(flags, if_deferred_start_softint, ids);
1104 if (func != NULL)
1105 ids->ids_if_start = func;
1106 else
1107 ids->ids_if_start = if_deferred_start_common;
1108
1109 ifp->if_deferred_start = ids;
1110 }
1111
1112 static void
1113 if_deferred_start_destroy(struct ifnet *ifp)
1114 {
1115
1116 if (ifp->if_deferred_start == NULL)
1117 return;
1118
1119 softint_disestablish(ifp->if_deferred_start->ids_si);
1120 kmem_free(ifp->if_deferred_start, sizeof(*ifp->if_deferred_start));
1121 ifp->if_deferred_start = NULL;
1122 }
1123
1124 /*
1125 * The common interface input routine that is called by device drivers,
1126 * which should be used only when the driver's rx handler already runs
1127 * in softint.
1128 */
1129 void
1130 if_input(struct ifnet *ifp, struct mbuf *m)
1131 {
1132
1133 KASSERT(ifp->if_percpuq == NULL);
1134 KASSERT(!cpu_intr_p());
1135
1136 if_statinc(ifp, if_ipackets);
1137 bpf_mtap(ifp, m, BPF_D_IN);
1138
1139 ifp->_if_input(ifp, m);
1140 }
1141
1142 /*
1143 * DEPRECATED. Use if_initialize and if_register instead.
1144 * See the above comment of if_initialize.
1145 *
1146 * Note that it implicitly enables if_percpuq to make drivers easy to
1147 * migrate softint-based if_input without much changes. If you don't
1148 * want to enable it, use if_initialize instead.
1149 */
1150 void
1151 if_attach(ifnet_t *ifp)
1152 {
1153
1154 if_initialize(ifp);
1155 ifp->if_percpuq = if_percpuq_create(ifp);
1156 if_register(ifp);
1157 }
1158
1159 void
1160 if_attachdomain(void)
1161 {
1162 struct ifnet *ifp;
1163 const int bound = curlwp_bind();
1164
1165 int s = pserialize_read_enter();
1166 IFNET_READER_FOREACH(ifp) {
1167 struct psref psref;
1168 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
1169 pserialize_read_exit(s);
1170 if_attachdomain1(ifp);
1171 s = pserialize_read_enter();
1172 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1173 }
1174 pserialize_read_exit(s);
1175 curlwp_bindx(bound);
1176 }
1177
1178 static void
1179 if_attachdomain1(struct ifnet *ifp)
1180 {
1181 struct domain *dp;
1182 const int s = splsoftnet();
1183
1184 /* address family dependent data region */
1185 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
1186 DOMAIN_FOREACH(dp) {
1187 if (dp->dom_ifattach != NULL)
1188 ifp->if_afdata[dp->dom_family] =
1189 (*dp->dom_ifattach)(ifp);
1190 }
1191
1192 splx(s);
1193 }
1194
1195 /*
1196 * Deactivate an interface. This points all of the procedure
1197 * handles at error stubs. May be called from interrupt context.
1198 */
1199 void
1200 if_deactivate(struct ifnet *ifp)
1201 {
1202 const int s = splsoftnet();
1203
1204 ifp->if_output = if_nulloutput;
1205 ifp->_if_input = if_nullinput;
1206 ifp->if_start = if_nullstart;
1207 ifp->if_transmit = if_nulltransmit;
1208 ifp->if_ioctl = if_nullioctl;
1209 ifp->if_init = if_nullinit;
1210 ifp->if_stop = if_nullstop;
1211 if (ifp->if_slowtimo)
1212 ifp->if_slowtimo = if_nullslowtimo;
1213 ifp->if_drain = if_nulldrain;
1214
1215 /* No more packets may be enqueued. */
1216 ifp->if_snd.ifq_maxlen = 0;
1217
1218 splx(s);
1219 }
1220
1221 bool
1222 if_is_deactivated(const struct ifnet *ifp)
1223 {
1224
1225 return ifp->if_output == if_nulloutput;
1226 }
1227
1228 void
1229 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
1230 {
1231 struct ifaddr *ifa, *nifa;
1232 int s;
1233
1234 s = pserialize_read_enter();
1235 for (ifa = IFADDR_READER_FIRST(ifp); ifa; ifa = nifa) {
1236 nifa = IFADDR_READER_NEXT(ifa);
1237 if (ifa->ifa_addr->sa_family != family)
1238 continue;
1239 pserialize_read_exit(s);
1240
1241 (*purgeaddr)(ifa);
1242
1243 s = pserialize_read_enter();
1244 }
1245 pserialize_read_exit(s);
1246 }
1247
1248 #ifdef IFAREF_DEBUG
1249 static struct ifaddr **ifa_list;
1250 static int ifa_list_size;
1251
1252 /* Depends on only one if_attach runs at once */
1253 static void
1254 if_build_ifa_list(struct ifnet *ifp)
1255 {
1256 struct ifaddr *ifa;
1257 int i;
1258
1259 KASSERT(ifa_list == NULL);
1260 KASSERT(ifa_list_size == 0);
1261
1262 IFADDR_READER_FOREACH(ifa, ifp)
1263 ifa_list_size++;
1264
1265 ifa_list = kmem_alloc(sizeof(*ifa) * ifa_list_size, KM_SLEEP);
1266 i = 0;
1267 IFADDR_READER_FOREACH(ifa, ifp) {
1268 ifa_list[i++] = ifa;
1269 ifaref(ifa);
1270 }
1271 }
1272
1273 static void
1274 if_check_and_free_ifa_list(struct ifnet *ifp)
1275 {
1276 int i;
1277 struct ifaddr *ifa;
1278
1279 if (ifa_list == NULL)
1280 return;
1281
1282 for (i = 0; i < ifa_list_size; i++) {
1283 char buf[64];
1284
1285 ifa = ifa_list[i];
1286 sockaddr_format(ifa->ifa_addr, buf, sizeof(buf));
1287 if (ifa->ifa_refcnt > 1) {
1288 log(LOG_WARNING,
1289 "ifa(%s) still referenced (refcnt=%d)\n",
1290 buf, ifa->ifa_refcnt - 1);
1291 } else
1292 log(LOG_DEBUG,
1293 "ifa(%s) not referenced (refcnt=%d)\n",
1294 buf, ifa->ifa_refcnt - 1);
1295 ifafree(ifa);
1296 }
1297
1298 kmem_free(ifa_list, sizeof(*ifa) * ifa_list_size);
1299 ifa_list = NULL;
1300 ifa_list_size = 0;
1301 }
1302 #endif
1303
1304 /*
1305 * Detach an interface from the list of "active" interfaces,
1306 * freeing any resources as we go along.
1307 *
1308 * NOTE: This routine must be called with a valid thread context,
1309 * as it may block.
1310 */
1311 void
1312 if_detach(struct ifnet *ifp)
1313 {
1314 struct socket so;
1315 struct ifaddr *ifa;
1316 #ifdef IFAREF_DEBUG
1317 struct ifaddr *last_ifa = NULL;
1318 #endif
1319 struct domain *dp;
1320 const struct protosw *pr;
1321 int i, family, purged;
1322
1323 #ifdef IFAREF_DEBUG
1324 if_build_ifa_list(ifp);
1325 #endif
1326 /*
1327 * XXX It's kind of lame that we have to have the
1328 * XXX socket structure...
1329 */
1330 memset(&so, 0, sizeof(so));
1331
1332 const int s = splnet();
1333
1334 sysctl_teardown(&ifp->if_sysctl_log);
1335
1336 IFNET_LOCK(ifp);
1337
1338 /*
1339 * Unset all queued link states and pretend a
1340 * link state change is scheduled.
1341 * This stops any more link state changes occurring for this
1342 * interface while it's being detached so it's safe
1343 * to drain the workqueue.
1344 */
1345 IF_LINK_STATE_CHANGE_LOCK(ifp);
1346 ifp->if_link_queue = -1; /* all bits set, see link_state_change() */
1347 ifp->if_link_scheduled = true;
1348 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
1349 workqueue_wait(ifnet_link_state_wq, &ifp->if_link_work);
1350
1351 if_deactivate(ifp);
1352 IFNET_UNLOCK(ifp);
1353
1354 /*
1355 * Unlink from the list and wait for all readers to leave
1356 * from pserialize read sections. Note that we can't do
1357 * psref_target_destroy here. See below.
1358 */
1359 IFNET_GLOBAL_LOCK();
1360 ifindex2ifnet[ifp->if_index] = NULL;
1361 TAILQ_REMOVE(&ifnet_list, ifp, if_list);
1362 IFNET_WRITER_REMOVE(ifp);
1363 pserialize_perform(ifnet_psz);
1364 IFNET_GLOBAL_UNLOCK();
1365
1366 if (ifp->if_slowtimo != NULL) {
1367 struct if_slowtimo_data *isd = ifp->if_slowtimo_data;
1368
1369 mutex_enter(&isd->isd_lock);
1370 isd->isd_dying = true;
1371 mutex_exit(&isd->isd_lock);
1372 callout_halt(&isd->isd_ch, NULL);
1373 workqueue_wait(if_slowtimo_wq, &isd->isd_work);
1374 callout_destroy(&isd->isd_ch);
1375 mutex_destroy(&isd->isd_lock);
1376 kmem_free(isd, sizeof(*isd));
1377
1378 ifp->if_slowtimo_data = NULL; /* paraonia */
1379 ifp->if_slowtimo = NULL; /* paranoia */
1380 }
1381 if_deferred_start_destroy(ifp);
1382
1383 /*
1384 * Do an if_down() to give protocols a chance to do something.
1385 */
1386 if_down_deactivated(ifp);
1387
1388 #ifdef ALTQ
1389 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1390 altq_disable(&ifp->if_snd);
1391 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1392 altq_detach(&ifp->if_snd);
1393 #endif
1394
1395 #if NCARP > 0
1396 /* Remove the interface from any carp group it is a part of. */
1397 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
1398 carp_ifdetach(ifp);
1399 #endif
1400
1401 /*
1402 * remove packets that came from ifp, from software interrupt queues.
1403 */
1404 DOMAIN_FOREACH(dp) {
1405 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
1406 struct ifqueue *iq = dp->dom_ifqueues[i];
1407 if (iq == NULL)
1408 break;
1409 dp->dom_ifqueues[i] = NULL;
1410 if_detach_queues(ifp, iq);
1411 }
1412 }
1413
1414 /*
1415 * IP queues have to be processed separately: net-queue barrier
1416 * ensures that the packets are dequeued while a cross-call will
1417 * ensure that the interrupts have completed. FIXME: not quite..
1418 */
1419 #ifdef INET
1420 pktq_barrier(ip_pktq);
1421 #endif
1422 #ifdef INET6
1423 if (in6_present)
1424 pktq_barrier(ip6_pktq);
1425 #endif
1426 xc_barrier(0);
1427
1428 /*
1429 * Rip all the addresses off the interface. This should make
1430 * all of the routes go away.
1431 *
1432 * pr_usrreq calls can remove an arbitrary number of ifaddrs
1433 * from the list, including our "cursor", ifa. For safety,
1434 * and to honor the TAILQ abstraction, I just restart the
1435 * loop after each removal. Note that the loop will exit
1436 * when all of the remaining ifaddrs belong to the AF_LINK
1437 * family. I am counting on the historical fact that at
1438 * least one pr_usrreq in each address domain removes at
1439 * least one ifaddr.
1440 */
1441 again:
1442 /*
1443 * At this point, no other one tries to remove ifa in the list,
1444 * so we don't need to take a lock or psref. Avoid using
1445 * IFADDR_READER_FOREACH to pass over an inspection of contract
1446 * violations of pserialize.
1447 */
1448 IFADDR_WRITER_FOREACH(ifa, ifp) {
1449 family = ifa->ifa_addr->sa_family;
1450 #ifdef IFAREF_DEBUG
1451 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
1452 ifa, family, ifa->ifa_refcnt);
1453 if (last_ifa != NULL && ifa == last_ifa)
1454 panic("if_detach: loop detected");
1455 last_ifa = ifa;
1456 #endif
1457 if (family == AF_LINK)
1458 continue;
1459 dp = pffinddomain(family);
1460 KASSERTMSG(dp != NULL, "no domain for AF %d", family);
1461 /*
1462 * XXX These PURGEIF calls are redundant with the
1463 * purge-all-families calls below, but are left in for
1464 * now both to make a smaller change, and to avoid
1465 * unplanned interactions with clearing of
1466 * ifp->if_addrlist.
1467 */
1468 purged = 0;
1469 for (pr = dp->dom_protosw;
1470 pr < dp->dom_protoswNPROTOSW; pr++) {
1471 so.so_proto = pr;
1472 if (pr->pr_usrreqs) {
1473 (void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1474 purged = 1;
1475 }
1476 }
1477 if (purged == 0) {
1478 /*
1479 * XXX What's really the best thing to do
1480 * XXX here? --thorpej (at) NetBSD.org
1481 */
1482 printf("if_detach: WARNING: AF %d not purged\n",
1483 family);
1484 ifa_remove(ifp, ifa);
1485 }
1486 goto again;
1487 }
1488
1489 if_free_sadl(ifp, 1);
1490
1491 restart:
1492 IFADDR_WRITER_FOREACH(ifa, ifp) {
1493 family = ifa->ifa_addr->sa_family;
1494 KASSERT(family == AF_LINK);
1495 ifa_remove(ifp, ifa);
1496 goto restart;
1497 }
1498
1499 /* Delete stray routes from the routing table. */
1500 for (i = 0; i <= AF_MAX; i++)
1501 rt_delete_matched_entries(i, if_delroute_matcher, ifp);
1502
1503 DOMAIN_FOREACH(dp) {
1504 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
1505 {
1506 void *p = ifp->if_afdata[dp->dom_family];
1507 if (p) {
1508 ifp->if_afdata[dp->dom_family] = NULL;
1509 (*dp->dom_ifdetach)(ifp, p);
1510 }
1511 }
1512
1513 /*
1514 * One would expect multicast memberships (INET and
1515 * INET6) on UDP sockets to be purged by the PURGEIF
1516 * calls above, but if all addresses were removed from
1517 * the interface prior to destruction, the calls will
1518 * not be made (e.g. ppp, for which pppd(8) generally
1519 * removes addresses before destroying the interface).
1520 * Because there is no invariant that multicast
1521 * memberships only exist for interfaces with IPv4
1522 * addresses, we must call PURGEIF regardless of
1523 * addresses. (Protocols which might store ifnet
1524 * pointers are marked with PR_PURGEIF.)
1525 */
1526 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
1527 so.so_proto = pr;
1528 if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
1529 (void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1530 }
1531 }
1532
1533 /*
1534 * Must be done after the above pr_purgeif because if_psref may be
1535 * still used in pr_purgeif.
1536 */
1537 psref_target_destroy(&ifp->if_psref, ifnet_psref_class);
1538 PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry);
1539
1540 pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp);
1541 (void)pfil_head_destroy(ifp->if_pfil);
1542
1543 /* Announce that the interface is gone. */
1544 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1545
1546 IF_AFDATA_LOCK_DESTROY(ifp);
1547
1548 if (ifp->if_percpuq != NULL) {
1549 if_percpuq_destroy(ifp->if_percpuq);
1550 ifp->if_percpuq = NULL;
1551 }
1552
1553 mutex_obj_free(ifp->if_ioctl_lock);
1554 ifp->if_ioctl_lock = NULL;
1555 mutex_obj_free(ifp->if_snd.ifq_lock);
1556 if_stats_fini(ifp);
1557 KASSERT(!simplehook_has_hooks(ifp->if_linkstate_hooks));
1558 simplehook_destroy(ifp->if_linkstate_hooks);
1559
1560 splx(s);
1561
1562 #ifdef IFAREF_DEBUG
1563 if_check_and_free_ifa_list(ifp);
1564 #endif
1565 }
1566
1567 static void
1568 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
1569 {
1570 struct mbuf *m, *prev, *next;
1571
1572 prev = NULL;
1573 for (m = q->ifq_head; m != NULL; m = next) {
1574 KASSERT((m->m_flags & M_PKTHDR) != 0);
1575
1576 next = m->m_nextpkt;
1577 if (m->m_pkthdr.rcvif_index != ifp->if_index) {
1578 prev = m;
1579 continue;
1580 }
1581
1582 if (prev != NULL)
1583 prev->m_nextpkt = m->m_nextpkt;
1584 else
1585 q->ifq_head = m->m_nextpkt;
1586 if (q->ifq_tail == m)
1587 q->ifq_tail = prev;
1588 q->ifq_len--;
1589
1590 m->m_nextpkt = NULL;
1591 m_freem(m);
1592 IF_DROP(q);
1593 }
1594 }
1595
1596 /*
1597 * Callback for a radix tree walk to delete all references to an
1598 * ifnet.
1599 */
1600 static int
1601 if_delroute_matcher(struct rtentry *rt, void *v)
1602 {
1603 struct ifnet *ifp = (struct ifnet *)v;
1604
1605 if (rt->rt_ifp == ifp)
1606 return 1;
1607 else
1608 return 0;
1609 }
1610
1611 /*
1612 * Create a clone network interface.
1613 */
1614 static int
1615 if_clone_create(const char *name)
1616 {
1617 struct if_clone *ifc;
1618 int unit;
1619 struct ifnet *ifp;
1620 struct psref psref;
1621
1622 KASSERT(mutex_owned(&if_clone_mtx));
1623
1624 ifc = if_clone_lookup(name, &unit);
1625 if (ifc == NULL)
1626 return EINVAL;
1627
1628 ifp = if_get(name, &psref);
1629 if (ifp != NULL) {
1630 if_put(ifp, &psref);
1631 return EEXIST;
1632 }
1633
1634 return (*ifc->ifc_create)(ifc, unit);
1635 }
1636
1637 /*
1638 * Destroy a clone network interface.
1639 */
1640 static int
1641 if_clone_destroy(const char *name)
1642 {
1643 struct if_clone *ifc;
1644 struct ifnet *ifp;
1645 struct psref psref;
1646 int error;
1647 int (*if_ioctlfn)(struct ifnet *, u_long, void *);
1648
1649 KASSERT(mutex_owned(&if_clone_mtx));
1650
1651 ifc = if_clone_lookup(name, NULL);
1652 if (ifc == NULL)
1653 return EINVAL;
1654
1655 if (ifc->ifc_destroy == NULL)
1656 return EOPNOTSUPP;
1657
1658 ifp = if_get(name, &psref);
1659 if (ifp == NULL)
1660 return ENXIO;
1661
1662 /* We have to disable ioctls here */
1663 IFNET_LOCK(ifp);
1664 if_ioctlfn = ifp->if_ioctl;
1665 ifp->if_ioctl = if_nullioctl;
1666 IFNET_UNLOCK(ifp);
1667
1668 /*
1669 * We cannot call ifc_destroy with holding ifp.
1670 * Releasing ifp here is safe thanks to if_clone_mtx.
1671 */
1672 if_put(ifp, &psref);
1673
1674 error = (*ifc->ifc_destroy)(ifp);
1675
1676 if (error != 0) {
1677 /* We have to restore if_ioctl on error */
1678 IFNET_LOCK(ifp);
1679 ifp->if_ioctl = if_ioctlfn;
1680 IFNET_UNLOCK(ifp);
1681 }
1682
1683 return error;
1684 }
1685
1686 static bool
1687 if_is_unit(const char *name)
1688 {
1689
1690 while (*name != '\0') {
1691 if (*name < '0' || *name > '9')
1692 return false;
1693 name++;
1694 }
1695
1696 return true;
1697 }
1698
1699 /*
1700 * Look up a network interface cloner.
1701 */
1702 static struct if_clone *
1703 if_clone_lookup(const char *name, int *unitp)
1704 {
1705 struct if_clone *ifc;
1706 const char *cp;
1707 char *dp, ifname[IFNAMSIZ + 3];
1708 int unit;
1709
1710 KASSERT(mutex_owned(&if_clone_mtx));
1711
1712 strcpy(ifname, "if_");
1713 /* separate interface name from unit */
1714 /* TODO: search unit number from backward */
1715 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
1716 *cp && !if_is_unit(cp);)
1717 *dp++ = *cp++;
1718
1719 if (cp == name || cp - name == IFNAMSIZ || !*cp)
1720 return NULL; /* No name or unit number */
1721 *dp++ = '\0';
1722
1723 again:
1724 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1725 if (strcmp(ifname + 3, ifc->ifc_name) == 0)
1726 break;
1727 }
1728
1729 if (ifc == NULL) {
1730 int error;
1731 if (*ifname == '\0')
1732 return NULL;
1733 mutex_exit(&if_clone_mtx);
1734 error = module_autoload(ifname, MODULE_CLASS_DRIVER);
1735 mutex_enter(&if_clone_mtx);
1736 if (error)
1737 return NULL;
1738 *ifname = '\0';
1739 goto again;
1740 }
1741
1742 unit = 0;
1743 while (cp - name < IFNAMSIZ && *cp) {
1744 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1745 /* Bogus unit number. */
1746 return NULL;
1747 }
1748 unit = (unit * 10) + (*cp++ - '0');
1749 }
1750
1751 if (unitp != NULL)
1752 *unitp = unit;
1753 return ifc;
1754 }
1755
1756 /*
1757 * Register a network interface cloner.
1758 */
1759 void
1760 if_clone_attach(struct if_clone *ifc)
1761 {
1762
1763 mutex_enter(&if_clone_mtx);
1764 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1765 if_cloners_count++;
1766 mutex_exit(&if_clone_mtx);
1767 }
1768
1769 /*
1770 * Unregister a network interface cloner.
1771 */
1772 void
1773 if_clone_detach(struct if_clone *ifc)
1774 {
1775
1776 mutex_enter(&if_clone_mtx);
1777 LIST_REMOVE(ifc, ifc_list);
1778 if_cloners_count--;
1779 mutex_exit(&if_clone_mtx);
1780 }
1781
1782 /*
1783 * Provide list of interface cloners to userspace.
1784 */
1785 int
1786 if_clone_list(int buf_count, char *buffer, int *total)
1787 {
1788 char outbuf[IFNAMSIZ], *dst;
1789 struct if_clone *ifc;
1790 int count, error = 0;
1791
1792 mutex_enter(&if_clone_mtx);
1793 *total = if_cloners_count;
1794 if ((dst = buffer) == NULL) {
1795 /* Just asking how many there are. */
1796 goto out;
1797 }
1798
1799 if (buf_count < 0) {
1800 error = EINVAL;
1801 goto out;
1802 }
1803
1804 count = (if_cloners_count < buf_count) ?
1805 if_cloners_count : buf_count;
1806
1807 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
1808 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1809 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1810 if (outbuf[sizeof(outbuf) - 1] != '\0') {
1811 error = ENAMETOOLONG;
1812 goto out;
1813 }
1814 error = copyout(outbuf, dst, sizeof(outbuf));
1815 if (error != 0)
1816 break;
1817 }
1818
1819 out:
1820 mutex_exit(&if_clone_mtx);
1821 return error;
1822 }
1823
1824 void
1825 ifa_psref_init(struct ifaddr *ifa)
1826 {
1827
1828 psref_target_init(&ifa->ifa_psref, ifa_psref_class);
1829 }
1830
1831 void
1832 ifaref(struct ifaddr *ifa)
1833 {
1834
1835 atomic_inc_uint(&ifa->ifa_refcnt);
1836 }
1837
1838 void
1839 ifafree(struct ifaddr *ifa)
1840 {
1841 KASSERT(ifa != NULL);
1842 KASSERTMSG(ifa->ifa_refcnt > 0, "ifa_refcnt=%d", ifa->ifa_refcnt);
1843
1844 #ifndef __HAVE_ATOMIC_AS_MEMBAR
1845 membar_release();
1846 #endif
1847 if (atomic_dec_uint_nv(&ifa->ifa_refcnt) != 0)
1848 return;
1849 #ifndef __HAVE_ATOMIC_AS_MEMBAR
1850 membar_acquire();
1851 #endif
1852 free(ifa, M_IFADDR);
1853 }
1854
1855 bool
1856 ifa_is_destroying(struct ifaddr *ifa)
1857 {
1858
1859 return ISSET(ifa->ifa_flags, IFA_DESTROYING);
1860 }
1861
1862 void
1863 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
1864 {
1865
1866 ifa->ifa_ifp = ifp;
1867
1868 /*
1869 * Check MP-safety for IFEF_MPSAFE drivers.
1870 * Check !IFF_RUNNING for initialization routines that normally don't
1871 * take IFNET_LOCK but it's safe because there is no competitor.
1872 * XXX there are false positive cases because IFF_RUNNING can be off on
1873 * if_stop.
1874 */
1875 KASSERT(!if_is_mpsafe(ifp) || !ISSET(ifp->if_flags, IFF_RUNNING) ||
1876 IFNET_LOCKED(ifp));
1877
1878 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1879 IFADDR_ENTRY_INIT(ifa);
1880 IFADDR_WRITER_INSERT_TAIL(ifp, ifa);
1881
1882 ifaref(ifa);
1883 }
1884
1885 void
1886 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
1887 {
1888
1889 KASSERT(ifa->ifa_ifp == ifp);
1890 /*
1891 * Check MP-safety for IFEF_MPSAFE drivers.
1892 * if_is_deactivated indicates ifa_remove is called from if_detach
1893 * where it is safe even if IFNET_LOCK isn't held.
1894 */
1895 KASSERT(!if_is_mpsafe(ifp) || if_is_deactivated(ifp) || IFNET_LOCKED(ifp));
1896
1897 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1898 IFADDR_WRITER_REMOVE(ifa);
1899 #ifdef NET_MPSAFE
1900 IFNET_GLOBAL_LOCK();
1901 pserialize_perform(ifnet_psz);
1902 IFNET_GLOBAL_UNLOCK();
1903 #endif
1904
1905 #ifdef NET_MPSAFE
1906 psref_target_destroy(&ifa->ifa_psref, ifa_psref_class);
1907 #endif
1908 IFADDR_ENTRY_DESTROY(ifa);
1909 ifafree(ifa);
1910 }
1911
1912 void
1913 ifa_acquire(struct ifaddr *ifa, struct psref *psref)
1914 {
1915
1916 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
1917 psref_acquire(psref, &ifa->ifa_psref, ifa_psref_class);
1918 }
1919
1920 void
1921 ifa_release(struct ifaddr *ifa, struct psref *psref)
1922 {
1923
1924 if (ifa == NULL)
1925 return;
1926
1927 psref_release(psref, &ifa->ifa_psref, ifa_psref_class);
1928 }
1929
1930 bool
1931 ifa_held(struct ifaddr *ifa)
1932 {
1933
1934 return psref_held(&ifa->ifa_psref, ifa_psref_class);
1935 }
1936
1937 static inline int
1938 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
1939 {
1940 return sockaddr_cmp(sa1, sa2) == 0;
1941 }
1942
1943 /*
1944 * Locate an interface based on a complete address.
1945 */
1946 /*ARGSUSED*/
1947 struct ifaddr *
1948 ifa_ifwithaddr(const struct sockaddr *addr)
1949 {
1950 struct ifnet *ifp;
1951 struct ifaddr *ifa;
1952
1953 IFNET_READER_FOREACH(ifp) {
1954 if (if_is_deactivated(ifp))
1955 continue;
1956 IFADDR_READER_FOREACH(ifa, ifp) {
1957 if (ifa->ifa_addr->sa_family != addr->sa_family)
1958 continue;
1959 if (equal(addr, ifa->ifa_addr))
1960 return ifa;
1961 if ((ifp->if_flags & IFF_BROADCAST) &&
1962 ifa->ifa_broadaddr &&
1963 /* IP6 doesn't have broadcast */
1964 ifa->ifa_broadaddr->sa_len != 0 &&
1965 equal(ifa->ifa_broadaddr, addr))
1966 return ifa;
1967 }
1968 }
1969 return NULL;
1970 }
1971
1972 struct ifaddr *
1973 ifa_ifwithaddr_psref(const struct sockaddr *addr, struct psref *psref)
1974 {
1975 struct ifaddr *ifa;
1976 int s = pserialize_read_enter();
1977
1978 ifa = ifa_ifwithaddr(addr);
1979 if (ifa != NULL)
1980 ifa_acquire(ifa, psref);
1981 pserialize_read_exit(s);
1982
1983 return ifa;
1984 }
1985
1986 /*
1987 * Locate the point to point interface with a given destination address.
1988 */
1989 /*ARGSUSED*/
1990 struct ifaddr *
1991 ifa_ifwithdstaddr(const struct sockaddr *addr)
1992 {
1993 struct ifnet *ifp;
1994 struct ifaddr *ifa;
1995
1996 IFNET_READER_FOREACH(ifp) {
1997 if (if_is_deactivated(ifp))
1998 continue;
1999 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
2000 continue;
2001 IFADDR_READER_FOREACH(ifa, ifp) {
2002 if (ifa->ifa_addr->sa_family != addr->sa_family ||
2003 ifa->ifa_dstaddr == NULL)
2004 continue;
2005 if (equal(addr, ifa->ifa_dstaddr))
2006 return ifa;
2007 }
2008 }
2009
2010 return NULL;
2011 }
2012
2013 struct ifaddr *
2014 ifa_ifwithdstaddr_psref(const struct sockaddr *addr, struct psref *psref)
2015 {
2016 struct ifaddr *ifa;
2017 int s;
2018
2019 s = pserialize_read_enter();
2020 ifa = ifa_ifwithdstaddr(addr);
2021 if (ifa != NULL)
2022 ifa_acquire(ifa, psref);
2023 pserialize_read_exit(s);
2024
2025 return ifa;
2026 }
2027
2028 /*
2029 * Find an interface on a specific network. If many, choice
2030 * is most specific found.
2031 */
2032 struct ifaddr *
2033 ifa_ifwithnet(const struct sockaddr *addr)
2034 {
2035 struct ifnet *ifp;
2036 struct ifaddr *ifa, *ifa_maybe = NULL;
2037 const struct sockaddr_dl *sdl;
2038 u_int af = addr->sa_family;
2039 const char *addr_data = addr->sa_data, *cplim;
2040
2041 if (af == AF_LINK) {
2042 sdl = satocsdl(addr);
2043 if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
2044 ifindex2ifnet[sdl->sdl_index] &&
2045 !if_is_deactivated(ifindex2ifnet[sdl->sdl_index])) {
2046 return ifindex2ifnet[sdl->sdl_index]->if_dl;
2047 }
2048 }
2049 #ifdef NETATALK
2050 if (af == AF_APPLETALK) {
2051 const struct sockaddr_at *sat, *sat2;
2052 sat = (const struct sockaddr_at *)addr;
2053 IFNET_READER_FOREACH(ifp) {
2054 if (if_is_deactivated(ifp))
2055 continue;
2056 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
2057 if (ifa == NULL)
2058 continue;
2059 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
2060 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
2061 return ifa; /* exact match */
2062 if (ifa_maybe == NULL) {
2063 /* else keep the if with the right range */
2064 ifa_maybe = ifa;
2065 }
2066 }
2067 return ifa_maybe;
2068 }
2069 #endif
2070 IFNET_READER_FOREACH(ifp) {
2071 if (if_is_deactivated(ifp))
2072 continue;
2073 IFADDR_READER_FOREACH(ifa, ifp) {
2074 const char *cp, *cp2, *cp3;
2075
2076 if (ifa->ifa_addr->sa_family != af ||
2077 ifa->ifa_netmask == NULL)
2078 next: continue;
2079 cp = addr_data;
2080 cp2 = ifa->ifa_addr->sa_data;
2081 cp3 = ifa->ifa_netmask->sa_data;
2082 cplim = (const char *)ifa->ifa_netmask +
2083 ifa->ifa_netmask->sa_len;
2084 while (cp3 < cplim) {
2085 if ((*cp++ ^ *cp2++) & *cp3++) {
2086 /* want to continue for() loop */
2087 goto next;
2088 }
2089 }
2090 if (ifa_maybe == NULL ||
2091 rt_refines(ifa->ifa_netmask,
2092 ifa_maybe->ifa_netmask))
2093 ifa_maybe = ifa;
2094 }
2095 }
2096 return ifa_maybe;
2097 }
2098
2099 struct ifaddr *
2100 ifa_ifwithnet_psref(const struct sockaddr *addr, struct psref *psref)
2101 {
2102 struct ifaddr *ifa;
2103 int s;
2104
2105 s = pserialize_read_enter();
2106 ifa = ifa_ifwithnet(addr);
2107 if (ifa != NULL)
2108 ifa_acquire(ifa, psref);
2109 pserialize_read_exit(s);
2110
2111 return ifa;
2112 }
2113
2114 /*
2115 * Find the interface of the address.
2116 */
2117 struct ifaddr *
2118 ifa_ifwithladdr(const struct sockaddr *addr)
2119 {
2120 struct ifaddr *ia;
2121
2122 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
2123 (ia = ifa_ifwithnet(addr)))
2124 return ia;
2125 return NULL;
2126 }
2127
2128 struct ifaddr *
2129 ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref)
2130 {
2131 struct ifaddr *ifa;
2132 int s;
2133
2134 s = pserialize_read_enter();
2135 ifa = ifa_ifwithladdr(addr);
2136 if (ifa != NULL)
2137 ifa_acquire(ifa, psref);
2138 pserialize_read_exit(s);
2139
2140 return ifa;
2141 }
2142
2143 /*
2144 * Find an interface using a specific address family
2145 */
2146 struct ifaddr *
2147 ifa_ifwithaf(int af)
2148 {
2149 struct ifnet *ifp;
2150 struct ifaddr *ifa = NULL;
2151 int s;
2152
2153 s = pserialize_read_enter();
2154 IFNET_READER_FOREACH(ifp) {
2155 if (if_is_deactivated(ifp))
2156 continue;
2157 IFADDR_READER_FOREACH(ifa, ifp) {
2158 if (ifa->ifa_addr->sa_family == af)
2159 goto out;
2160 }
2161 }
2162 out:
2163 pserialize_read_exit(s);
2164 return ifa;
2165 }
2166
2167 /*
2168 * Find an interface address specific to an interface best matching
2169 * a given address.
2170 */
2171 struct ifaddr *
2172 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2173 {
2174 struct ifaddr *ifa;
2175 const char *cp, *cp2, *cp3;
2176 const char *cplim;
2177 struct ifaddr *ifa_maybe = 0;
2178 u_int af = addr->sa_family;
2179
2180 if (if_is_deactivated(ifp))
2181 return NULL;
2182
2183 if (af >= AF_MAX)
2184 return NULL;
2185
2186 IFADDR_READER_FOREACH(ifa, ifp) {
2187 if (ifa->ifa_addr->sa_family != af)
2188 continue;
2189 ifa_maybe = ifa;
2190 if (ifa->ifa_netmask == NULL) {
2191 if (equal(addr, ifa->ifa_addr) ||
2192 (ifa->ifa_dstaddr &&
2193 equal(addr, ifa->ifa_dstaddr)))
2194 return ifa;
2195 continue;
2196 }
2197 cp = addr->sa_data;
2198 cp2 = ifa->ifa_addr->sa_data;
2199 cp3 = ifa->ifa_netmask->sa_data;
2200 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2201 for (; cp3 < cplim; cp3++) {
2202 if ((*cp++ ^ *cp2++) & *cp3)
2203 break;
2204 }
2205 if (cp3 == cplim)
2206 return ifa;
2207 }
2208 return ifa_maybe;
2209 }
2210
2211 struct ifaddr *
2212 ifaof_ifpforaddr_psref(const struct sockaddr *addr, struct ifnet *ifp,
2213 struct psref *psref)
2214 {
2215 struct ifaddr *ifa;
2216 int s;
2217
2218 s = pserialize_read_enter();
2219 ifa = ifaof_ifpforaddr(addr, ifp);
2220 if (ifa != NULL)
2221 ifa_acquire(ifa, psref);
2222 pserialize_read_exit(s);
2223
2224 return ifa;
2225 }
2226
2227 /*
2228 * Default action when installing a route with a Link Level gateway.
2229 * Lookup an appropriate real ifa to point to.
2230 * This should be moved to /sys/net/link.c eventually.
2231 */
2232 void
2233 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
2234 {
2235 struct ifaddr *ifa;
2236 const struct sockaddr *dst;
2237 struct ifnet *ifp;
2238 struct psref psref;
2239
2240 if (cmd != RTM_ADD || ISSET(info->rti_flags, RTF_DONTCHANGEIFA))
2241 return;
2242 ifp = rt->rt_ifa->ifa_ifp;
2243 dst = rt_getkey(rt);
2244 if ((ifa = ifaof_ifpforaddr_psref(dst, ifp, &psref)) != NULL) {
2245 rt_replace_ifa(rt, ifa);
2246 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2247 ifa->ifa_rtrequest(cmd, rt, info);
2248 ifa_release(ifa, &psref);
2249 }
2250 }
2251
2252 /*
2253 * bitmask macros to manage a densely packed link_state change queue.
2254 * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
2255 * LINK_STATE_UP(2) we need 2 bits for each state change.
2256 * As a state change to store is 0, treat all bits set as an unset item.
2257 */
2258 #define LQ_ITEM_BITS 2
2259 #define LQ_ITEM_MASK ((1 << LQ_ITEM_BITS) - 1)
2260 #define LQ_MASK(i) (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
2261 #define LINK_STATE_UNSET LQ_ITEM_MASK
2262 #define LQ_ITEM(q, i) (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
2263 #define LQ_STORE(q, i, v) \
2264 do { \
2265 (q) &= ~LQ_MASK((i)); \
2266 (q) |= (v) << (i) * LQ_ITEM_BITS; \
2267 } while (0 /* CONSTCOND */)
2268 #define LQ_MAX(q) ((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
2269 #define LQ_POP(q, v) \
2270 do { \
2271 (v) = LQ_ITEM((q), 0); \
2272 (q) >>= LQ_ITEM_BITS; \
2273 (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \
2274 } while (0 /* CONSTCOND */)
2275 #define LQ_PUSH(q, v) \
2276 do { \
2277 (q) >>= LQ_ITEM_BITS; \
2278 (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \
2279 } while (0 /* CONSTCOND */)
2280 #define LQ_FIND_UNSET(q, i) \
2281 for ((i) = 0; i < LQ_MAX((q)); (i)++) { \
2282 if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET) \
2283 break; \
2284 }
2285
2286 /*
2287 * Handle a change in the interface link state and
2288 * queue notifications.
2289 */
2290 void
2291 if_link_state_change(struct ifnet *ifp, int link_state)
2292 {
2293 int idx;
2294
2295 /* Ensure change is to a valid state */
2296 switch (link_state) {
2297 case LINK_STATE_UNKNOWN: /* FALLTHROUGH */
2298 case LINK_STATE_DOWN: /* FALLTHROUGH */
2299 case LINK_STATE_UP:
2300 break;
2301 default:
2302 #ifdef DEBUG
2303 printf("%s: invalid link state %d\n",
2304 ifp->if_xname, link_state);
2305 #endif
2306 return;
2307 }
2308
2309 IF_LINK_STATE_CHANGE_LOCK(ifp);
2310
2311 /* Find the last unset event in the queue. */
2312 LQ_FIND_UNSET(ifp->if_link_queue, idx);
2313
2314 if (idx == 0) {
2315 /*
2316 * There is no queue of link state changes.
2317 * As we have the lock we can safely compare against the
2318 * current link state and return if the same.
2319 * Otherwise, if scheduled is true then the interface is being
2320 * detached and the queue is being drained so we need
2321 * to avoid queuing more work.
2322 */
2323 if (ifp->if_link_state == link_state || ifp->if_link_scheduled)
2324 goto out;
2325 } else {
2326 /* Ensure link_state doesn't match the last queued state. */
2327 if (LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
2328 goto out;
2329 }
2330
2331 /* Handle queue overflow. */
2332 if (idx == LQ_MAX(ifp->if_link_queue)) {
2333 uint8_t lost;
2334
2335 /*
2336 * The DOWN state must be protected from being pushed off
2337 * the queue to ensure that userland will always be
2338 * in a sane state.
2339 * Because DOWN is protected, there is no need to protect
2340 * UNKNOWN.
2341 * It should be invalid to change from any other state to
2342 * UNKNOWN anyway ...
2343 */
2344 lost = LQ_ITEM(ifp->if_link_queue, 0);
2345 LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
2346 if (lost == LINK_STATE_DOWN) {
2347 lost = LQ_ITEM(ifp->if_link_queue, 0);
2348 LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
2349 }
2350 printf("%s: lost link state change %s\n",
2351 ifp->if_xname,
2352 lost == LINK_STATE_UP ? "UP" :
2353 lost == LINK_STATE_DOWN ? "DOWN" :
2354 "UNKNOWN");
2355 } else
2356 LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
2357
2358 if (ifp->if_link_scheduled)
2359 goto out;
2360
2361 ifp->if_link_scheduled = true;
2362 workqueue_enqueue(ifnet_link_state_wq, &ifp->if_link_work, NULL);
2363
2364 out:
2365 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2366 }
2367
2368 /*
2369 * Handle interface link state change notifications.
2370 */
2371 static void
2372 if_link_state_change_process(struct ifnet *ifp, int link_state)
2373 {
2374 struct domain *dp;
2375 const int s = splnet();
2376 bool notify;
2377
2378 KASSERT(!cpu_intr_p());
2379
2380 IF_LINK_STATE_CHANGE_LOCK(ifp);
2381
2382 /* Ensure the change is still valid. */
2383 if (ifp->if_link_state == link_state) {
2384 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2385 splx(s);
2386 return;
2387 }
2388
2389 #ifdef DEBUG
2390 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
2391 link_state == LINK_STATE_UP ? "UP" :
2392 link_state == LINK_STATE_DOWN ? "DOWN" :
2393 "UNKNOWN",
2394 ifp->if_link_state == LINK_STATE_UP ? "UP" :
2395 ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
2396 "UNKNOWN");
2397 #endif
2398
2399 /*
2400 * When going from UNKNOWN to UP, we need to mark existing
2401 * addresses as tentative and restart DAD as we may have
2402 * erroneously not found a duplicate.
2403 *
2404 * This needs to happen before rt_ifmsg to avoid a race where
2405 * listeners would have an address and expect it to work right
2406 * away.
2407 */
2408 notify = (link_state == LINK_STATE_UP &&
2409 ifp->if_link_state == LINK_STATE_UNKNOWN);
2410 ifp->if_link_state = link_state;
2411 /* The following routines may sleep so release the spin mutex */
2412 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2413
2414 KERNEL_LOCK_UNLESS_NET_MPSAFE();
2415 if (notify) {
2416 DOMAIN_FOREACH(dp) {
2417 if (dp->dom_if_link_state_change != NULL)
2418 dp->dom_if_link_state_change(ifp,
2419 LINK_STATE_DOWN);
2420 }
2421 }
2422
2423 /* Notify that the link state has changed. */
2424 rt_ifmsg(ifp);
2425
2426 simplehook_dohooks(ifp->if_linkstate_hooks);
2427
2428 DOMAIN_FOREACH(dp) {
2429 if (dp->dom_if_link_state_change != NULL)
2430 dp->dom_if_link_state_change(ifp, link_state);
2431 }
2432 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2433 splx(s);
2434 }
2435
2436 /*
2437 * Process the interface link state change queue.
2438 */
2439 static void
2440 if_link_state_change_work(struct work *work, void *arg)
2441 {
2442 struct ifnet *ifp = container_of(work, struct ifnet, if_link_work);
2443 uint8_t state;
2444
2445 KERNEL_LOCK_UNLESS_NET_MPSAFE();
2446 const int s = splnet();
2447
2448 /*
2449 * Pop a link state change from the queue and process it.
2450 * If there is nothing to process then if_detach() has been called.
2451 * We keep if_link_scheduled = true so the queue can safely drain
2452 * without more work being queued.
2453 */
2454 IF_LINK_STATE_CHANGE_LOCK(ifp);
2455 LQ_POP(ifp->if_link_queue, state);
2456 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2457 if (state == LINK_STATE_UNSET)
2458 goto out;
2459
2460 if_link_state_change_process(ifp, state);
2461
2462 /* If there is a link state change to come, schedule it. */
2463 IF_LINK_STATE_CHANGE_LOCK(ifp);
2464 if (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET) {
2465 ifp->if_link_scheduled = true;
2466 workqueue_enqueue(ifnet_link_state_wq, &ifp->if_link_work, NULL);
2467 } else
2468 ifp->if_link_scheduled = false;
2469 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2470
2471 out:
2472 splx(s);
2473 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2474 }
2475
2476 void *
2477 if_linkstate_change_establish(struct ifnet *ifp, void (*fn)(void *), void *arg)
2478 {
2479 khook_t *hk;
2480
2481 hk = simplehook_establish(ifp->if_linkstate_hooks, fn, arg);
2482
2483 return (void *)hk;
2484 }
2485
2486 void
2487 if_linkstate_change_disestablish(struct ifnet *ifp, void *vhook, kmutex_t *lock)
2488 {
2489
2490 simplehook_disestablish(ifp->if_linkstate_hooks, vhook, lock);
2491 }
2492
2493 /*
2494 * Used to mark addresses on an interface as DETATCHED or TENTATIVE
2495 * and thus start Duplicate Address Detection without changing the
2496 * real link state.
2497 */
2498 void
2499 if_domain_link_state_change(struct ifnet *ifp, int link_state)
2500 {
2501 struct domain *dp;
2502
2503 const int s = splnet();
2504 KERNEL_LOCK_UNLESS_NET_MPSAFE();
2505
2506 DOMAIN_FOREACH(dp) {
2507 if (dp->dom_if_link_state_change != NULL)
2508 dp->dom_if_link_state_change(ifp, link_state);
2509 }
2510
2511 splx(s);
2512 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2513 }
2514
2515 /*
2516 * Default action when installing a local route on a point-to-point
2517 * interface.
2518 */
2519 void
2520 p2p_rtrequest(int req, struct rtentry *rt,
2521 __unused const struct rt_addrinfo *info)
2522 {
2523 struct ifnet *ifp = rt->rt_ifp;
2524 struct ifaddr *ifa, *lo0ifa;
2525 int s = pserialize_read_enter();
2526
2527 switch (req) {
2528 case RTM_ADD:
2529 if ((rt->rt_flags & RTF_LOCAL) == 0)
2530 break;
2531
2532 rt->rt_ifp = lo0ifp;
2533
2534 if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA))
2535 break;
2536
2537 IFADDR_READER_FOREACH(ifa, ifp) {
2538 if (equal(rt_getkey(rt), ifa->ifa_addr))
2539 break;
2540 }
2541 if (ifa == NULL)
2542 break;
2543
2544 /*
2545 * Ensure lo0 has an address of the same family.
2546 */
2547 IFADDR_READER_FOREACH(lo0ifa, lo0ifp) {
2548 if (lo0ifa->ifa_addr->sa_family ==
2549 ifa->ifa_addr->sa_family)
2550 break;
2551 }
2552 if (lo0ifa == NULL)
2553 break;
2554
2555 /*
2556 * Make sure to set rt->rt_ifa to the interface
2557 * address we are using, otherwise we will have trouble
2558 * with source address selection.
2559 */
2560 if (ifa != rt->rt_ifa)
2561 rt_replace_ifa(rt, ifa);
2562 break;
2563 case RTM_DELETE:
2564 default:
2565 break;
2566 }
2567 pserialize_read_exit(s);
2568 }
2569
2570 static void
2571 _if_down(struct ifnet *ifp)
2572 {
2573 struct ifaddr *ifa;
2574 struct domain *dp;
2575 struct psref psref;
2576
2577 ifp->if_flags &= ~IFF_UP;
2578 nanotime(&ifp->if_lastchange);
2579
2580 const int bound = curlwp_bind();
2581 int s = pserialize_read_enter();
2582 IFADDR_READER_FOREACH(ifa, ifp) {
2583 ifa_acquire(ifa, &psref);
2584 pserialize_read_exit(s);
2585
2586 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2587
2588 s = pserialize_read_enter();
2589 ifa_release(ifa, &psref);
2590 }
2591 pserialize_read_exit(s);
2592 curlwp_bindx(bound);
2593
2594 IFQ_PURGE(&ifp->if_snd);
2595 #if NCARP > 0
2596 if (ifp->if_carp)
2597 carp_carpdev_state(ifp);
2598 #endif
2599 rt_ifmsg(ifp);
2600 DOMAIN_FOREACH(dp) {
2601 if (dp->dom_if_down)
2602 dp->dom_if_down(ifp);
2603 }
2604 }
2605
2606 static void
2607 if_down_deactivated(struct ifnet *ifp)
2608 {
2609
2610 KASSERT(if_is_deactivated(ifp));
2611 _if_down(ifp);
2612 }
2613
2614 void
2615 if_down_locked(struct ifnet *ifp)
2616 {
2617
2618 KASSERT(IFNET_LOCKED(ifp));
2619 _if_down(ifp);
2620 }
2621
2622 /*
2623 * Mark an interface down and notify protocols of
2624 * the transition.
2625 * NOTE: must be called at splsoftnet or equivalent.
2626 */
2627 void
2628 if_down(struct ifnet *ifp)
2629 {
2630
2631 IFNET_LOCK(ifp);
2632 if_down_locked(ifp);
2633 IFNET_UNLOCK(ifp);
2634 }
2635
2636 /*
2637 * Must be called with holding if_ioctl_lock.
2638 */
2639 static void
2640 if_up_locked(struct ifnet *ifp)
2641 {
2642 #ifdef notyet
2643 struct ifaddr *ifa;
2644 #endif
2645 struct domain *dp;
2646
2647 KASSERT(IFNET_LOCKED(ifp));
2648
2649 KASSERT(!if_is_deactivated(ifp));
2650 ifp->if_flags |= IFF_UP;
2651 nanotime(&ifp->if_lastchange);
2652 #ifdef notyet
2653 /* this has no effect on IP, and will kill all ISO connections XXX */
2654 IFADDR_READER_FOREACH(ifa, ifp)
2655 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2656 #endif
2657 #if NCARP > 0
2658 if (ifp->if_carp)
2659 carp_carpdev_state(ifp);
2660 #endif
2661 rt_ifmsg(ifp);
2662 DOMAIN_FOREACH(dp) {
2663 if (dp->dom_if_up)
2664 dp->dom_if_up(ifp);
2665 }
2666 }
2667
2668 /*
2669 * Handle interface slowtimo timer routine. Called
2670 * from softclock, we decrement timer (if set) and
2671 * call the appropriate interface routine on expiration.
2672 */
2673 static bool
2674 if_slowtimo_countdown(struct ifnet *ifp)
2675 {
2676 bool fire = false;
2677 const int s = splnet();
2678 KERNEL_LOCK(1, NULL);
2679 if (ifp->if_timer != 0 && --ifp->if_timer == 0)
2680 fire = true;
2681 KERNEL_UNLOCK_ONE(NULL);
2682 splx(s);
2683
2684 return fire;
2685 }
2686
2687 static void
2688 if_slowtimo_intr(void *arg)
2689 {
2690 struct ifnet *ifp = arg;
2691 struct if_slowtimo_data *isd = ifp->if_slowtimo_data;
2692
2693 mutex_enter(&isd->isd_lock);
2694 if (!isd->isd_dying) {
2695 if (isd->isd_trigger || if_slowtimo_countdown(ifp)) {
2696 if (!isd->isd_queued) {
2697 isd->isd_queued = true;
2698 workqueue_enqueue(if_slowtimo_wq,
2699 &isd->isd_work, NULL);
2700 }
2701 } else {
2702 callout_schedule(&isd->isd_ch, hz / IFNET_SLOWHZ);
2703 }
2704 }
2705 mutex_exit(&isd->isd_lock);
2706 }
2707
2708 static void
2709 if_slowtimo_work(struct work *work, void *arg)
2710 {
2711 struct if_slowtimo_data *isd =
2712 container_of(work, struct if_slowtimo_data, isd_work);
2713 struct ifnet *ifp = isd->isd_ifp;
2714 const int s = splnet();
2715 KERNEL_LOCK(1, NULL);
2716 (*ifp->if_slowtimo)(ifp);
2717 KERNEL_UNLOCK_ONE(NULL);
2718 splx(s);
2719
2720 mutex_enter(&isd->isd_lock);
2721 if (isd->isd_trigger) {
2722 isd->isd_trigger = false;
2723 printf("%s: watchdog triggered\n", ifp->if_xname);
2724 }
2725 isd->isd_queued = false;
2726 if (!isd->isd_dying)
2727 callout_schedule(&isd->isd_ch, hz / IFNET_SLOWHZ);
2728 mutex_exit(&isd->isd_lock);
2729 }
2730
2731 static int
2732 sysctl_if_watchdog(SYSCTLFN_ARGS)
2733 {
2734 struct sysctlnode node = *rnode;
2735 struct ifnet *ifp = node.sysctl_data;
2736 struct if_slowtimo_data *isd = ifp->if_slowtimo_data;
2737 int arg = 0;
2738 int error;
2739
2740 node.sysctl_data = &arg;
2741 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2742 if (error || newp == NULL)
2743 return error;
2744 if (arg) {
2745 mutex_enter(&isd->isd_lock);
2746 KASSERT(!isd->isd_dying);
2747 isd->isd_trigger = true;
2748 callout_schedule(&isd->isd_ch, 0);
2749 mutex_exit(&isd->isd_lock);
2750 }
2751
2752 return 0;
2753 }
2754
2755 static void
2756 sysctl_watchdog_setup(struct ifnet *ifp)
2757 {
2758 struct sysctllog **clog = &ifp->if_sysctl_log;
2759 const struct sysctlnode *rnode;
2760
2761 if (sysctl_createv(clog, 0, NULL, &rnode,
2762 CTLFLAG_PERMANENT, CTLTYPE_NODE, "interfaces",
2763 SYSCTL_DESCR("Per-interface controls"),
2764 NULL, 0, NULL, 0,
2765 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
2766 goto bad;
2767 if (sysctl_createv(clog, 0, &rnode, &rnode,
2768 CTLFLAG_PERMANENT, CTLTYPE_NODE, ifp->if_xname,
2769 SYSCTL_DESCR("Interface controls"),
2770 NULL, 0, NULL, 0,
2771 CTL_CREATE, CTL_EOL) != 0)
2772 goto bad;
2773 if (sysctl_createv(clog, 0, &rnode, &rnode,
2774 CTLFLAG_PERMANENT, CTLTYPE_NODE, "watchdog",
2775 SYSCTL_DESCR("Interface watchdog controls"),
2776 NULL, 0, NULL, 0,
2777 CTL_CREATE, CTL_EOL) != 0)
2778 goto bad;
2779 if (sysctl_createv(clog, 0, &rnode, NULL,
2780 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "trigger",
2781 SYSCTL_DESCR("Trigger watchdog timeout"),
2782 sysctl_if_watchdog, 0, (int *)ifp, 0,
2783 CTL_CREATE, CTL_EOL) != 0)
2784 goto bad;
2785
2786 return;
2787
2788 bad:
2789 printf("%s: could not attach sysctl watchdog nodes\n", ifp->if_xname);
2790 }
2791
2792 /*
2793 * Mark an interface up and notify protocols of
2794 * the transition.
2795 * NOTE: must be called at splsoftnet or equivalent.
2796 */
2797 void
2798 if_up(struct ifnet *ifp)
2799 {
2800
2801 IFNET_LOCK(ifp);
2802 if_up_locked(ifp);
2803 IFNET_UNLOCK(ifp);
2804 }
2805
2806 /*
2807 * Set/clear promiscuous mode on interface ifp based on the truth value
2808 * of pswitch. The calls are reference counted so that only the first
2809 * "on" request actually has an effect, as does the final "off" request.
2810 * Results are undefined if the "off" and "on" requests are not matched.
2811 */
2812 int
2813 ifpromisc_locked(struct ifnet *ifp, int pswitch)
2814 {
2815 int pcount, ret = 0;
2816 u_short nflags;
2817
2818 KASSERT(IFNET_LOCKED(ifp));
2819
2820 pcount = ifp->if_pcount;
2821 if (pswitch) {
2822 /*
2823 * Allow the device to be "placed" into promiscuous
2824 * mode even if it is not configured up. It will
2825 * consult IFF_PROMISC when it is brought up.
2826 */
2827 if (ifp->if_pcount++ != 0)
2828 goto out;
2829 nflags = ifp->if_flags | IFF_PROMISC;
2830 } else {
2831 if (--ifp->if_pcount > 0)
2832 goto out;
2833 nflags = ifp->if_flags & ~IFF_PROMISC;
2834 }
2835 ret = if_flags_set(ifp, nflags);
2836 /* Restore interface state if not successful. */
2837 if (ret != 0) {
2838 ifp->if_pcount = pcount;
2839 }
2840 out:
2841 return ret;
2842 }
2843
2844 int
2845 ifpromisc(struct ifnet *ifp, int pswitch)
2846 {
2847 int e;
2848
2849 IFNET_LOCK(ifp);
2850 e = ifpromisc_locked(ifp, pswitch);
2851 IFNET_UNLOCK(ifp);
2852
2853 return e;
2854 }
2855
2856 /*
2857 * if_ioctl(ifp, cmd, data)
2858 *
2859 * Apply an ioctl command to the interface. Returns 0 on success,
2860 * nonzero errno(3) number on failure.
2861 *
2862 * For SIOCADDMULTI/SIOCDELMULTI, caller need not hold locks -- it
2863 * is the driver's responsibility to take any internal locks.
2864 * (Kernel logic should generally invoke these only through
2865 * if_mcast_op.)
2866 *
2867 * For all other ioctls, caller must hold ifp->if_ioctl_lock,
2868 * a.k.a. IFNET_LOCK. May sleep.
2869 */
2870 int
2871 if_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2872 {
2873
2874 switch (cmd) {
2875 case SIOCADDMULTI:
2876 case SIOCDELMULTI:
2877 break;
2878 default:
2879 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
2880 }
2881
2882 return (*ifp->if_ioctl)(ifp, cmd, data);
2883 }
2884
2885 /*
2886 * if_init(ifp)
2887 *
2888 * Prepare the hardware underlying ifp to process packets
2889 * according to its current configuration. Returns 0 on success,
2890 * nonzero errno(3) number on failure.
2891 *
2892 * May sleep. Caller must hold ifp->if_ioctl_lock, a.k.a
2893 * IFNET_LOCK.
2894 */
2895 int
2896 if_init(struct ifnet *ifp)
2897 {
2898
2899 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
2900
2901 return (*ifp->if_init)(ifp);
2902 }
2903
2904 /*
2905 * if_stop(ifp, disable)
2906 *
2907 * Stop the hardware underlying ifp from processing packets.
2908 *
2909 * If disable is true, ... XXX(?)
2910 *
2911 * May sleep. Caller must hold ifp->if_ioctl_lock, a.k.a
2912 * IFNET_LOCK.
2913 */
2914 void
2915 if_stop(struct ifnet *ifp, int disable)
2916 {
2917
2918 KASSERTMSG(IFNET_LOCKED(ifp), "%s", ifp->if_xname);
2919
2920 (*ifp->if_stop)(ifp, disable);
2921 }
2922
2923 /*
2924 * Map interface name to
2925 * interface structure pointer.
2926 */
2927 struct ifnet *
2928 ifunit(const char *name)
2929 {
2930 struct ifnet *ifp;
2931 const char *cp = name;
2932 u_int unit = 0;
2933 u_int i;
2934
2935 /*
2936 * If the entire name is a number, treat it as an ifindex.
2937 */
2938 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2939 unit = unit * 10 + (*cp - '0');
2940 }
2941
2942 /*
2943 * If the number took all of the name, then it's a valid ifindex.
2944 */
2945 if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
2946 return if_byindex(unit);
2947
2948 ifp = NULL;
2949 const int s = pserialize_read_enter();
2950 IFNET_READER_FOREACH(ifp) {
2951 if (if_is_deactivated(ifp))
2952 continue;
2953 if (strcmp(ifp->if_xname, name) == 0)
2954 goto out;
2955 }
2956 out:
2957 pserialize_read_exit(s);
2958 return ifp;
2959 }
2960
2961 /*
2962 * Get a reference of an ifnet object by an interface name.
2963 * The returned reference is protected by psref(9). The caller
2964 * must release a returned reference by if_put after use.
2965 */
2966 struct ifnet *
2967 if_get(const char *name, struct psref *psref)
2968 {
2969 struct ifnet *ifp;
2970 const char *cp = name;
2971 u_int unit = 0;
2972 u_int i;
2973
2974 /*
2975 * If the entire name is a number, treat it as an ifindex.
2976 */
2977 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2978 unit = unit * 10 + (*cp - '0');
2979 }
2980
2981 /*
2982 * If the number took all of the name, then it's a valid ifindex.
2983 */
2984 if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
2985 return if_get_byindex(unit, psref);
2986
2987 ifp = NULL;
2988 const int s = pserialize_read_enter();
2989 IFNET_READER_FOREACH(ifp) {
2990 if (if_is_deactivated(ifp))
2991 continue;
2992 if (strcmp(ifp->if_xname, name) == 0) {
2993 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
2994 psref_acquire(psref, &ifp->if_psref,
2995 ifnet_psref_class);
2996 goto out;
2997 }
2998 }
2999 out:
3000 pserialize_read_exit(s);
3001 return ifp;
3002 }
3003
3004 /*
3005 * Release a reference of an ifnet object given by if_get, if_get_byindex
3006 * or if_get_bylla.
3007 */
3008 void
3009 if_put(const struct ifnet *ifp, struct psref *psref)
3010 {
3011
3012 if (ifp == NULL)
3013 return;
3014
3015 psref_release(psref, &ifp->if_psref, ifnet_psref_class);
3016 }
3017
3018 /*
3019 * Return ifp having idx. Return NULL if not found. Normally if_byindex
3020 * should be used.
3021 */
3022 ifnet_t *
3023 _if_byindex(u_int idx)
3024 {
3025
3026 return (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
3027 }
3028
3029 /*
3030 * Return ifp having idx. Return NULL if not found or the found ifp is
3031 * already deactivated.
3032 */
3033 ifnet_t *
3034 if_byindex(u_int idx)
3035 {
3036 ifnet_t *ifp;
3037
3038 ifp = _if_byindex(idx);
3039 if (ifp != NULL && if_is_deactivated(ifp))
3040 ifp = NULL;
3041 return ifp;
3042 }
3043
3044 /*
3045 * Get a reference of an ifnet object by an interface index.
3046 * The returned reference is protected by psref(9). The caller
3047 * must release a returned reference by if_put after use.
3048 */
3049 ifnet_t *
3050 if_get_byindex(u_int idx, struct psref *psref)
3051 {
3052 ifnet_t *ifp;
3053
3054 const int s = pserialize_read_enter();
3055 ifp = if_byindex(idx);
3056 if (__predict_true(ifp != NULL)) {
3057 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
3058 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
3059 }
3060 pserialize_read_exit(s);
3061
3062 return ifp;
3063 }
3064
3065 ifnet_t *
3066 if_get_bylla(const void *lla, unsigned char lla_len, struct psref *psref)
3067 {
3068 ifnet_t *ifp;
3069
3070 const int s = pserialize_read_enter();
3071 IFNET_READER_FOREACH(ifp) {
3072 if (if_is_deactivated(ifp))
3073 continue;
3074 if (ifp->if_addrlen != lla_len)
3075 continue;
3076 if (memcmp(lla, CLLADDR(ifp->if_sadl), lla_len) == 0) {
3077 psref_acquire(psref, &ifp->if_psref,
3078 ifnet_psref_class);
3079 break;
3080 }
3081 }
3082 pserialize_read_exit(s);
3083
3084 return ifp;
3085 }
3086
3087 /*
3088 * Note that it's safe only if the passed ifp is guaranteed to not be freed,
3089 * for example using pserialize or the ifp is already held or some other
3090 * object is held which guarantes the ifp to not be freed indirectly.
3091 */
3092 void
3093 if_acquire(struct ifnet *ifp, struct psref *psref)
3094 {
3095
3096 KASSERT(ifp->if_index != 0);
3097 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
3098 }
3099
3100 bool
3101 if_held(struct ifnet *ifp)
3102 {
3103
3104 return psref_held(&ifp->if_psref, ifnet_psref_class);
3105 }
3106
3107 /*
3108 * Some tunnel interfaces can nest, e.g. IPv4 over IPv4 gif(4) tunnel over IPv4.
3109 * Check the tunnel nesting count.
3110 * Return > 0, if tunnel nesting count is more than limit.
3111 * Return 0, if tunnel nesting count is equal or less than limit.
3112 */
3113 int
3114 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, int limit)
3115 {
3116 struct m_tag *mtag;
3117 int *count;
3118
3119 mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO);
3120 if (mtag != NULL) {
3121 count = (int *)(mtag + 1);
3122 if (++(*count) > limit) {
3123 log(LOG_NOTICE,
3124 "%s: recursively called too many times(%d)\n",
3125 ifp->if_xname, *count);
3126 return EIO;
3127 }
3128 } else {
3129 mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count),
3130 M_NOWAIT);
3131 if (mtag != NULL) {
3132 m_tag_prepend(m, mtag);
3133 count = (int *)(mtag + 1);
3134 *count = 0;
3135 } else {
3136 log(LOG_DEBUG,
3137 "%s: m_tag_get() failed, recursion calls are not prevented.\n",
3138 ifp->if_xname);
3139 }
3140 }
3141
3142 return 0;
3143 }
3144
3145 static void
3146 if_tunnel_ro_init_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
3147 {
3148 struct tunnel_ro *tro = p;
3149
3150 tro->tr_ro = kmem_zalloc(sizeof(*tro->tr_ro), KM_SLEEP);
3151 tro->tr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
3152 }
3153
3154 static void
3155 if_tunnel_ro_fini_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
3156 {
3157 struct tunnel_ro *tro = p;
3158
3159 rtcache_free(tro->tr_ro);
3160 kmem_free(tro->tr_ro, sizeof(*tro->tr_ro));
3161
3162 mutex_obj_free(tro->tr_lock);
3163 }
3164
3165 percpu_t *
3166 if_tunnel_alloc_ro_percpu(void)
3167 {
3168
3169 return percpu_create(sizeof(struct tunnel_ro),
3170 if_tunnel_ro_init_pc, if_tunnel_ro_fini_pc, NULL);
3171 }
3172
3173 void
3174 if_tunnel_free_ro_percpu(percpu_t *ro_percpu)
3175 {
3176
3177 percpu_free(ro_percpu, sizeof(struct tunnel_ro));
3178 }
3179
3180
3181 static void
3182 if_tunnel_rtcache_free_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
3183 {
3184 struct tunnel_ro *tro = p;
3185
3186 mutex_enter(tro->tr_lock);
3187 rtcache_free(tro->tr_ro);
3188 mutex_exit(tro->tr_lock);
3189 }
3190
3191 void if_tunnel_ro_percpu_rtcache_free(percpu_t *ro_percpu)
3192 {
3193
3194 percpu_foreach(ro_percpu, if_tunnel_rtcache_free_pc, NULL);
3195 }
3196
3197 void
3198 if_export_if_data(ifnet_t * const ifp, struct if_data *ifi, bool zero_stats)
3199 {
3200
3201 /* Collect the volatile stats first; this zeros *ifi. */
3202 if_stats_to_if_data(ifp, ifi, zero_stats);
3203
3204 ifi->ifi_type = ifp->if_type;
3205 ifi->ifi_addrlen = ifp->if_addrlen;
3206 ifi->ifi_hdrlen = ifp->if_hdrlen;
3207 ifi->ifi_link_state = ifp->if_link_state;
3208 ifi->ifi_mtu = ifp->if_mtu;
3209 ifi->ifi_metric = ifp->if_metric;
3210 ifi->ifi_baudrate = ifp->if_baudrate;
3211 ifi->ifi_lastchange = ifp->if_lastchange;
3212 }
3213
3214 /* common */
3215 int
3216 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
3217 {
3218 struct ifreq *ifr;
3219 struct ifcapreq *ifcr;
3220 struct ifdatareq *ifdr;
3221 unsigned short flags;
3222 char *descr;
3223 int error;
3224
3225 switch (cmd) {
3226 case SIOCSIFCAP:
3227 ifcr = data;
3228 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
3229 return EINVAL;
3230
3231 if (ifcr->ifcr_capenable == ifp->if_capenable)
3232 return 0;
3233
3234 ifp->if_capenable = ifcr->ifcr_capenable;
3235
3236 /* Pre-compute the checksum flags mask. */
3237 ifp->if_csum_flags_tx = 0;
3238 ifp->if_csum_flags_rx = 0;
3239 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx)
3240 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
3241 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx)
3242 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
3243
3244 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx)
3245 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
3246 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx)
3247 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
3248
3249 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx)
3250 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
3251 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx)
3252 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
3253
3254 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx)
3255 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
3256 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx)
3257 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
3258
3259 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx)
3260 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
3261 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx)
3262 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
3263
3264 if (ifp->if_capenable & IFCAP_TSOv4)
3265 ifp->if_csum_flags_tx |= M_CSUM_TSOv4;
3266 if (ifp->if_capenable & IFCAP_TSOv6)
3267 ifp->if_csum_flags_tx |= M_CSUM_TSOv6;
3268
3269 #if NBRIDGE > 0
3270 if (ifp->if_bridge != NULL)
3271 bridge_calc_csum_flags(ifp->if_bridge);
3272 #endif
3273
3274 if (ifp->if_flags & IFF_UP)
3275 return ENETRESET;
3276 return 0;
3277 case SIOCSIFFLAGS:
3278 ifr = data;
3279 /*
3280 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here, but if_up
3281 * and if_down aren't MP-safe yet, so we must hold the lock.
3282 */
3283 KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
3284 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
3285 const int s = splsoftnet();
3286 if_down_locked(ifp);
3287 splx(s);
3288 }
3289 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
3290 const int s = splsoftnet();
3291 if_up_locked(ifp);
3292 splx(s);
3293 }
3294 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
3295 flags = (ifp->if_flags & IFF_CANTCHANGE) |
3296 (ifr->ifr_flags &~ IFF_CANTCHANGE);
3297 if (ifp->if_flags != flags) {
3298 ifp->if_flags = flags;
3299 /* Notify that the flags have changed. */
3300 rt_ifmsg(ifp);
3301 }
3302 break;
3303 case SIOCGIFFLAGS:
3304 ifr = data;
3305 ifr->ifr_flags = ifp->if_flags;
3306 break;
3307
3308 case SIOCGIFMETRIC:
3309 ifr = data;
3310 ifr->ifr_metric = ifp->if_metric;
3311 break;
3312
3313 case SIOCGIFMTU:
3314 ifr = data;
3315 ifr->ifr_mtu = ifp->if_mtu;
3316 break;
3317
3318 case SIOCGIFDLT:
3319 ifr = data;
3320 ifr->ifr_dlt = ifp->if_dlt;
3321 break;
3322
3323 case SIOCGIFCAP:
3324 ifcr = data;
3325 ifcr->ifcr_capabilities = ifp->if_capabilities;
3326 ifcr->ifcr_capenable = ifp->if_capenable;
3327 break;
3328
3329 case SIOCSIFMETRIC:
3330 ifr = data;
3331 ifp->if_metric = ifr->ifr_metric;
3332 break;
3333
3334 case SIOCGIFDATA:
3335 ifdr = data;
3336 if_export_if_data(ifp, &ifdr->ifdr_data, false);
3337 break;
3338
3339 case SIOCGIFINDEX:
3340 ifr = data;
3341 ifr->ifr_index = ifp->if_index;
3342 break;
3343
3344 case SIOCZIFDATA:
3345 ifdr = data;
3346 if_export_if_data(ifp, &ifdr->ifdr_data, true);
3347 getnanotime(&ifp->if_lastchange);
3348 break;
3349 case SIOCSIFMTU:
3350 ifr = data;
3351 if (ifp->if_mtu == ifr->ifr_mtu)
3352 break;
3353 ifp->if_mtu = ifr->ifr_mtu;
3354 return ENETRESET;
3355 case SIOCSIFDESCR:
3356 error = kauth_authorize_network(kauth_cred_get(),
3357 KAUTH_NETWORK_INTERFACE,
3358 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
3359 NULL);
3360 if (error)
3361 return error;
3362
3363 ifr = data;
3364
3365 if (ifr->ifr_buflen > IFDESCRSIZE)
3366 return ENAMETOOLONG;
3367
3368 if (ifr->ifr_buf == NULL || ifr->ifr_buflen == 0) {
3369 /* unset description */
3370 descr = NULL;
3371 } else {
3372 descr = kmem_zalloc(IFDESCRSIZE, KM_SLEEP);
3373 /*
3374 * copy (IFDESCRSIZE - 1) bytes to ensure
3375 * terminating nul
3376 */
3377 error = copyin(ifr->ifr_buf, descr, IFDESCRSIZE - 1);
3378 if (error) {
3379 kmem_free(descr, IFDESCRSIZE);
3380 return error;
3381 }
3382 }
3383
3384 if (ifp->if_description != NULL)
3385 kmem_free(ifp->if_description, IFDESCRSIZE);
3386
3387 ifp->if_description = descr;
3388 break;
3389
3390 case SIOCGIFDESCR:
3391 ifr = data;
3392 descr = ifp->if_description;
3393
3394 if (descr == NULL)
3395 return ENOMSG;
3396
3397 if (ifr->ifr_buflen < IFDESCRSIZE)
3398 return EINVAL;
3399
3400 error = copyout(descr, ifr->ifr_buf, IFDESCRSIZE);
3401 if (error)
3402 return error;
3403 break;
3404
3405 default:
3406 return ENOTTY;
3407 }
3408 return 0;
3409 }
3410
3411 int
3412 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
3413 {
3414 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
3415 struct ifaddr *ifa;
3416 const struct sockaddr *any, *sa;
3417 union {
3418 struct sockaddr sa;
3419 struct sockaddr_storage ss;
3420 } u, v;
3421 int s, error = 0;
3422
3423 switch (cmd) {
3424 case SIOCSIFADDRPREF:
3425 error = kauth_authorize_network(kauth_cred_get(),
3426 KAUTH_NETWORK_INTERFACE,
3427 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
3428 NULL);
3429 if (error)
3430 return error;
3431 break;
3432 case SIOCGIFADDRPREF:
3433 break;
3434 default:
3435 return EOPNOTSUPP;
3436 }
3437
3438 /* sanity checks */
3439 if (data == NULL || ifp == NULL) {
3440 panic("invalid argument to %s", __func__);
3441 /*NOTREACHED*/
3442 }
3443
3444 /* address must be specified on ADD and DELETE */
3445 sa = sstocsa(&ifap->ifap_addr);
3446 if (sa->sa_family != sofamily(so))
3447 return EINVAL;
3448 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
3449 return EINVAL;
3450
3451 sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
3452
3453 s = pserialize_read_enter();
3454 IFADDR_READER_FOREACH(ifa, ifp) {
3455 if (ifa->ifa_addr->sa_family != sa->sa_family)
3456 continue;
3457 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
3458 if (sockaddr_cmp(&u.sa, &v.sa) == 0)
3459 break;
3460 }
3461 if (ifa == NULL) {
3462 error = EADDRNOTAVAIL;
3463 goto out;
3464 }
3465
3466 switch (cmd) {
3467 case SIOCSIFADDRPREF:
3468 ifa->ifa_preference = ifap->ifap_preference;
3469 goto out;
3470 case SIOCGIFADDRPREF:
3471 /* fill in the if_laddrreq structure */
3472 (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
3473 sizeof(ifap->ifap_addr), ifa->ifa_addr);
3474 ifap->ifap_preference = ifa->ifa_preference;
3475 goto out;
3476 default:
3477 error = EOPNOTSUPP;
3478 }
3479 out:
3480 pserialize_read_exit(s);
3481 return error;
3482 }
3483
3484 /*
3485 * Interface ioctls.
3486 */
3487 static int
3488 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
3489 {
3490 struct ifnet *ifp;
3491 struct ifreq *ifr;
3492 int error = 0;
3493 u_long ocmd = cmd;
3494 u_short oif_flags;
3495 struct ifreq ifrb;
3496 struct oifreq *oifr = NULL;
3497 int r;
3498 struct psref psref;
3499 bool do_if43_post = false;
3500 bool do_ifm80_post = false;
3501
3502 switch (cmd) {
3503 case SIOCGIFCONF:
3504 return ifconf(cmd, data);
3505 case SIOCINITIFADDR:
3506 return EPERM;
3507 default:
3508 MODULE_HOOK_CALL(uipc_syscalls_40_hook, (cmd, data), enosys(),
3509 error);
3510 if (error != ENOSYS)
3511 return error;
3512 MODULE_HOOK_CALL(uipc_syscalls_50_hook, (l, cmd, data),
3513 enosys(), error);
3514 if (error != ENOSYS)
3515 return error;
3516 error = 0;
3517 break;
3518 }
3519
3520 ifr = data;
3521 /* Pre-conversion */
3522 MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), error);
3523 if (cmd != ocmd) {
3524 oifr = data;
3525 data = ifr = &ifrb;
3526 IFREQO2N_43(oifr, ifr);
3527 do_if43_post = true;
3528 }
3529 MODULE_HOOK_CALL(ifmedia_80_pre_hook, (ifr, &cmd, &do_ifm80_post),
3530 enosys(), error);
3531
3532 switch (cmd) {
3533 case SIOCIFCREATE:
3534 case SIOCIFDESTROY: {
3535 const int bound = curlwp_bind();
3536 if (l != NULL) {
3537 ifp = if_get(ifr->ifr_name, &psref);
3538 error = kauth_authorize_network(l->l_cred,
3539 KAUTH_NETWORK_INTERFACE,
3540 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
3541 KAUTH_ARG(cmd), NULL);
3542 if (ifp != NULL)
3543 if_put(ifp, &psref);
3544 if (error != 0) {
3545 curlwp_bindx(bound);
3546 return error;
3547 }
3548 }
3549 KERNEL_LOCK_UNLESS_NET_MPSAFE();
3550 mutex_enter(&if_clone_mtx);
3551 r = (cmd == SIOCIFCREATE) ?
3552 if_clone_create(ifr->ifr_name) :
3553 if_clone_destroy(ifr->ifr_name);
3554 mutex_exit(&if_clone_mtx);
3555 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
3556 curlwp_bindx(bound);
3557 return r;
3558 }
3559 case SIOCIFGCLONERS: {
3560 struct if_clonereq *req = (struct if_clonereq *)data;
3561 return if_clone_list(req->ifcr_count, req->ifcr_buffer,
3562 &req->ifcr_total);
3563 }
3564 }
3565
3566 if ((cmd & IOC_IN) == 0 || IOCPARM_LEN(cmd) < sizeof(ifr->ifr_name))
3567 return EINVAL;
3568
3569 const int bound = curlwp_bind();
3570 ifp = if_get(ifr->ifr_name, &psref);
3571 if (ifp == NULL) {
3572 curlwp_bindx(bound);
3573 return ENXIO;
3574 }
3575
3576 switch (cmd) {
3577 case SIOCALIFADDR:
3578 case SIOCDLIFADDR:
3579 case SIOCSIFADDRPREF:
3580 case SIOCSIFFLAGS:
3581 case SIOCSIFCAP:
3582 case SIOCSIFMETRIC:
3583 case SIOCZIFDATA:
3584 case SIOCSIFMTU:
3585 case SIOCSIFPHYADDR:
3586 case SIOCDIFPHYADDR:
3587 #ifdef INET6
3588 case SIOCSIFPHYADDR_IN6:
3589 #endif
3590 case SIOCSLIFPHYADDR:
3591 case SIOCADDMULTI:
3592 case SIOCDELMULTI:
3593 case SIOCSETHERCAP:
3594 case SIOCSIFMEDIA:
3595 case SIOCSDRVSPEC:
3596 case SIOCG80211:
3597 case SIOCS80211:
3598 case SIOCS80211NWID:
3599 case SIOCS80211NWKEY:
3600 case SIOCS80211POWER:
3601 case SIOCS80211BSSID:
3602 case SIOCS80211CHANNEL:
3603 case SIOCSLINKSTR:
3604 if (l != NULL) {
3605 error = kauth_authorize_network(l->l_cred,
3606 KAUTH_NETWORK_INTERFACE,
3607 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
3608 KAUTH_ARG(cmd), NULL);
3609 if (error != 0)
3610 goto out;
3611 }
3612 }
3613
3614 oif_flags = ifp->if_flags;
3615
3616 KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp);
3617 IFNET_LOCK(ifp);
3618
3619 error = if_ioctl(ifp, cmd, data);
3620 if (error != ENOTTY)
3621 ;
3622 else if (so->so_proto == NULL)
3623 error = EOPNOTSUPP;
3624 else {
3625 KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
3626 MODULE_HOOK_CALL(if_ifioctl_43_hook,
3627 (so, ocmd, cmd, data, l), enosys(), error);
3628 if (error == ENOSYS)
3629 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
3630 cmd, data, ifp);
3631 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
3632 }
3633
3634 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
3635 if ((ifp->if_flags & IFF_UP) != 0) {
3636 const int s = splsoftnet();
3637 if_up_locked(ifp);
3638 splx(s);
3639 }
3640 }
3641
3642 /* Post-conversion */
3643 if (do_ifm80_post && (error == 0))
3644 MODULE_HOOK_CALL(ifmedia_80_post_hook, (ifr, cmd),
3645 enosys(), error);
3646 if (do_if43_post)
3647 IFREQN2O_43(oifr, ifr);
3648
3649 IFNET_UNLOCK(ifp);
3650 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp);
3651 out:
3652 if_put(ifp, &psref);
3653 curlwp_bindx(bound);
3654 return error;
3655 }
3656
3657 /*
3658 * Return interface configuration
3659 * of system. List may be used
3660 * in later ioctl's (above) to get
3661 * other information.
3662 *
3663 * Each record is a struct ifreq. Before the addition of
3664 * sockaddr_storage, the API rule was that sockaddr flavors that did
3665 * not fit would extend beyond the struct ifreq, with the next struct
3666 * ifreq starting sa_len beyond the struct sockaddr. Because the
3667 * union in struct ifreq includes struct sockaddr_storage, every kind
3668 * of sockaddr must fit. Thus, there are no longer any overlength
3669 * records.
3670 *
3671 * Records are added to the user buffer if they fit, and ifc_len is
3672 * adjusted to the length that was written. Thus, the user is only
3673 * assured of getting the complete list if ifc_len on return is at
3674 * least sizeof(struct ifreq) less than it was on entry.
3675 *
3676 * If the user buffer pointer is NULL, this routine copies no data and
3677 * returns the amount of space that would be needed.
3678 *
3679 * Invariants:
3680 * ifrp points to the next part of the user's buffer to be used. If
3681 * ifrp != NULL, space holds the number of bytes remaining that we may
3682 * write at ifrp. Otherwise, space holds the number of bytes that
3683 * would have been written had there been adequate space.
3684 */
3685 /*ARGSUSED*/
3686 static int
3687 ifconf(u_long cmd, void *data)
3688 {
3689 struct ifconf *ifc = (struct ifconf *)data;
3690 struct ifnet *ifp;
3691 struct ifaddr *ifa;
3692 struct ifreq ifr, *ifrp = NULL;
3693 int space = 0, error = 0;
3694 const int sz = (int)sizeof(struct ifreq);
3695 const bool docopy = ifc->ifc_req != NULL;
3696 struct psref psref;
3697
3698 if (docopy) {
3699 if (ifc->ifc_len < 0)
3700 return EINVAL;
3701
3702 space = ifc->ifc_len;
3703 ifrp = ifc->ifc_req;
3704 }
3705 memset(&ifr, 0, sizeof(ifr));
3706
3707 const int bound = curlwp_bind();
3708 int s = pserialize_read_enter();
3709 IFNET_READER_FOREACH(ifp) {
3710 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
3711 pserialize_read_exit(s);
3712
3713 (void)strncpy(ifr.ifr_name, ifp->if_xname,
3714 sizeof(ifr.ifr_name));
3715 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
3716 error = ENAMETOOLONG;
3717 goto release_exit;
3718 }
3719 if (IFADDR_READER_EMPTY(ifp)) {
3720 /* Interface with no addresses - send zero sockaddr. */
3721 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
3722 if (!docopy) {
3723 space += sz;
3724 goto next;
3725 }
3726 if (space >= sz) {
3727 error = copyout(&ifr, ifrp, sz);
3728 if (error != 0)
3729 goto release_exit;
3730 ifrp++;
3731 space -= sz;
3732 }
3733 }
3734
3735 s = pserialize_read_enter();
3736 IFADDR_READER_FOREACH(ifa, ifp) {
3737 struct sockaddr *sa = ifa->ifa_addr;
3738 /* all sockaddrs must fit in sockaddr_storage */
3739 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
3740
3741 if (!docopy) {
3742 space += sz;
3743 continue;
3744 }
3745 memcpy(&ifr.ifr_space, sa, sa->sa_len);
3746 pserialize_read_exit(s);
3747
3748 if (space >= sz) {
3749 error = copyout(&ifr, ifrp, sz);
3750 if (error != 0)
3751 goto release_exit;
3752 ifrp++; space -= sz;
3753 }
3754 s = pserialize_read_enter();
3755 }
3756 pserialize_read_exit(s);
3757
3758 next:
3759 s = pserialize_read_enter();
3760 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
3761 }
3762 pserialize_read_exit(s);
3763 curlwp_bindx(bound);
3764
3765 if (docopy) {
3766 KASSERT(0 <= space && space <= ifc->ifc_len);
3767 ifc->ifc_len -= space;
3768 } else {
3769 KASSERT(space >= 0);
3770 ifc->ifc_len = space;
3771 }
3772 return (0);
3773
3774 release_exit:
3775 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
3776 curlwp_bindx(bound);
3777 return error;
3778 }
3779
3780 int
3781 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
3782 {
3783 uint8_t len = sizeof(ifr->ifr_ifru.ifru_space);
3784 struct ifreq ifrb;
3785 struct oifreq *oifr = NULL;
3786 u_long ocmd = cmd;
3787 int hook;
3788
3789 MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), hook);
3790 if (hook != ENOSYS) {
3791 if (cmd != ocmd) {
3792 oifr = (struct oifreq *)(void *)ifr;
3793 ifr = &ifrb;
3794 IFREQO2N_43(oifr, ifr);
3795 len = sizeof(oifr->ifr_addr);
3796 }
3797 }
3798
3799 if (len < sa->sa_len)
3800 return EFBIG;
3801
3802 memset(&ifr->ifr_addr, 0, len);
3803 sockaddr_copy(&ifr->ifr_addr, len, sa);
3804
3805 if (cmd != ocmd)
3806 IFREQN2O_43(oifr, ifr);
3807 return 0;
3808 }
3809
3810 /*
3811 * wrapper function for the drivers which doesn't have if_transmit().
3812 */
3813 static int
3814 if_transmit(struct ifnet *ifp, struct mbuf *m)
3815 {
3816 int error;
3817 size_t pktlen = m->m_pkthdr.len;
3818 bool mcast = (m->m_flags & M_MCAST) != 0;
3819
3820 const int s = splnet();
3821
3822 IFQ_ENQUEUE(&ifp->if_snd, m, error);
3823 if (error != 0) {
3824 /* mbuf is already freed */
3825 goto out;
3826 }
3827
3828 net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
3829 if_statadd_ref(nsr, if_obytes, pktlen);
3830 if (mcast)
3831 if_statinc_ref(nsr, if_omcasts);
3832 IF_STAT_PUTREF(ifp);
3833
3834 if ((ifp->if_flags & IFF_OACTIVE) == 0)
3835 if_start_lock(ifp);
3836 out:
3837 splx(s);
3838
3839 return error;
3840 }
3841
3842 int
3843 if_transmit_lock(struct ifnet *ifp, struct mbuf *m)
3844 {
3845 int error;
3846
3847 kmsan_check_mbuf(m);
3848
3849 #ifdef ALTQ
3850 KERNEL_LOCK(1, NULL);
3851 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
3852 error = if_transmit(ifp, m);
3853 KERNEL_UNLOCK_ONE(NULL);
3854 } else {
3855 KERNEL_UNLOCK_ONE(NULL);
3856 error = (*ifp->if_transmit)(ifp, m);
3857 /* mbuf is already freed */
3858 }
3859 #else /* !ALTQ */
3860 error = (*ifp->if_transmit)(ifp, m);
3861 /* mbuf is already freed */
3862 #endif /* !ALTQ */
3863
3864 return error;
3865 }
3866
3867 /*
3868 * Queue message on interface, and start output if interface
3869 * not yet active.
3870 */
3871 int
3872 ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
3873 {
3874
3875 return if_transmit_lock(ifp, m);
3876 }
3877
3878 /*
3879 * Queue message on interface, possibly using a second fast queue
3880 */
3881 int
3882 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
3883 {
3884 int error = 0;
3885
3886 if (ifq != NULL
3887 #ifdef ALTQ
3888 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
3889 #endif
3890 ) {
3891 if (IF_QFULL(ifq)) {
3892 IF_DROP(&ifp->if_snd);
3893 m_freem(m);
3894 if (error == 0)
3895 error = ENOBUFS;
3896 } else
3897 IF_ENQUEUE(ifq, m);
3898 } else
3899 IFQ_ENQUEUE(&ifp->if_snd, m, error);
3900 if (error != 0) {
3901 if_statinc(ifp, if_oerrors);
3902 return error;
3903 }
3904 return 0;
3905 }
3906
3907 int
3908 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
3909 {
3910 int rc;
3911
3912 KASSERT(IFNET_LOCKED(ifp));
3913 if (ifp->if_initaddr != NULL)
3914 rc = (*ifp->if_initaddr)(ifp, ifa, src);
3915 else if (src ||
3916 (rc = if_ioctl(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
3917 rc = if_ioctl(ifp, SIOCINITIFADDR, ifa);
3918
3919 return rc;
3920 }
3921
3922 int
3923 if_do_dad(struct ifnet *ifp)
3924 {
3925 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
3926 return 0;
3927
3928 switch (ifp->if_type) {
3929 case IFT_FAITH:
3930 /*
3931 * These interfaces do not have the IFF_LOOPBACK flag,
3932 * but loop packets back. We do not have to do DAD on such
3933 * interfaces. We should even omit it, because loop-backed
3934 * responses would confuse the DAD procedure.
3935 */
3936 return 0;
3937 default:
3938 /*
3939 * Our DAD routine requires the interface up and running.
3940 * However, some interfaces can be up before the RUNNING
3941 * status. Additionally, users may try to assign addresses
3942 * before the interface becomes up (or running).
3943 * We simply skip DAD in such a case as a work around.
3944 * XXX: we should rather mark "tentative" on such addresses,
3945 * and do DAD after the interface becomes ready.
3946 */
3947 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
3948 (IFF_UP | IFF_RUNNING))
3949 return 0;
3950
3951 return 1;
3952 }
3953 }
3954
3955 /*
3956 * if_flags_set(ifp, flags)
3957 *
3958 * Ask ifp to change ifp->if_flags to flags, as if with the
3959 * SIOCSIFFLAGS ioctl command.
3960 *
3961 * May sleep. Caller must hold ifp->if_ioctl_lock, a.k.a
3962 * IFNET_LOCK.
3963 */
3964 int
3965 if_flags_set(ifnet_t *ifp, const u_short flags)
3966 {
3967 int rc;
3968
3969 KASSERT(IFNET_LOCKED(ifp));
3970
3971 if (ifp->if_setflags != NULL)
3972 rc = (*ifp->if_setflags)(ifp, flags);
3973 else {
3974 u_short cantflags, chgdflags;
3975 struct ifreq ifr;
3976
3977 chgdflags = ifp->if_flags ^ flags;
3978 cantflags = chgdflags & IFF_CANTCHANGE;
3979
3980 if (cantflags != 0)
3981 ifp->if_flags ^= cantflags;
3982
3983 /*
3984 * Traditionally, we do not call if_ioctl after
3985 * setting/clearing only IFF_PROMISC if the interface
3986 * isn't IFF_UP. Uphold that tradition.
3987 */
3988 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
3989 return 0;
3990
3991 memset(&ifr, 0, sizeof(ifr));
3992
3993 ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
3994 rc = if_ioctl(ifp, SIOCSIFFLAGS, &ifr);
3995
3996 if (rc != 0 && cantflags != 0)
3997 ifp->if_flags ^= cantflags;
3998 }
3999
4000 return rc;
4001 }
4002
4003 /*
4004 * if_mcast_op(ifp, cmd, sa)
4005 *
4006 * Apply a multicast command, SIOCADDMULTI/SIOCDELMULTI, to the
4007 * interface. Returns 0 on success, nonzero errno(3) number on
4008 * failure.
4009 *
4010 * May sleep.
4011 *
4012 * Use this, not if_ioctl, for the multicast commands.
4013 */
4014 int
4015 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
4016 {
4017 int rc;
4018 struct ifreq ifr;
4019
4020 switch (cmd) {
4021 case SIOCADDMULTI:
4022 case SIOCDELMULTI:
4023 break;
4024 default:
4025 panic("invalid ifnet multicast command: 0x%lx", cmd);
4026 }
4027
4028 ifreq_setaddr(cmd, &ifr, sa);
4029 rc = if_ioctl(ifp, cmd, &ifr);
4030
4031 return rc;
4032 }
4033
4034 static void
4035 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
4036 struct ifaltq *ifq)
4037 {
4038 const struct sysctlnode *cnode, *rnode;
4039
4040 if (sysctl_createv(clog, 0, NULL, &rnode,
4041 CTLFLAG_PERMANENT,
4042 CTLTYPE_NODE, "interfaces",
4043 SYSCTL_DESCR("Per-interface controls"),
4044 NULL, 0, NULL, 0,
4045 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
4046 goto bad;
4047
4048 if (sysctl_createv(clog, 0, &rnode, &rnode,
4049 CTLFLAG_PERMANENT,
4050 CTLTYPE_NODE, ifname,
4051 SYSCTL_DESCR("Interface controls"),
4052 NULL, 0, NULL, 0,
4053 CTL_CREATE, CTL_EOL) != 0)
4054 goto bad;
4055
4056 if (sysctl_createv(clog, 0, &rnode, &rnode,
4057 CTLFLAG_PERMANENT,
4058 CTLTYPE_NODE, "sndq",
4059 SYSCTL_DESCR("Interface output queue controls"),
4060 NULL, 0, NULL, 0,
4061 CTL_CREATE, CTL_EOL) != 0)
4062 goto bad;
4063
4064 if (sysctl_createv(clog, 0, &rnode, &cnode,
4065 CTLFLAG_PERMANENT,
4066 CTLTYPE_INT, "len",
4067 SYSCTL_DESCR("Current output queue length"),
4068 NULL, 0, &ifq->ifq_len, 0,
4069 CTL_CREATE, CTL_EOL) != 0)
4070 goto bad;
4071
4072 if (sysctl_createv(clog, 0, &rnode, &cnode,
4073 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
4074 CTLTYPE_INT, "maxlen",
4075 SYSCTL_DESCR("Maximum allowed output queue length"),
4076 NULL, 0, &ifq->ifq_maxlen, 0,
4077 CTL_CREATE, CTL_EOL) != 0)
4078 goto bad;
4079
4080 if (sysctl_createv(clog, 0, &rnode, &cnode,
4081 CTLFLAG_PERMANENT,
4082 CTLTYPE_INT, "drops",
4083 SYSCTL_DESCR("Packets dropped due to full output queue"),
4084 NULL, 0, &ifq->ifq_drops, 0,
4085 CTL_CREATE, CTL_EOL) != 0)
4086 goto bad;
4087
4088 return;
4089 bad:
4090 printf("%s: could not attach sysctl nodes\n", ifname);
4091 return;
4092 }
4093
4094 static int
4095 if_sdl_sysctl(SYSCTLFN_ARGS)
4096 {
4097 struct ifnet *ifp;
4098 const struct sockaddr_dl *sdl;
4099 struct psref psref;
4100 int error = 0;
4101
4102 if (namelen != 1)
4103 return EINVAL;
4104
4105 const int bound = curlwp_bind();
4106 ifp = if_get_byindex(name[0], &psref);
4107 if (ifp == NULL) {
4108 error = ENODEV;
4109 goto out0;
4110 }
4111
4112 sdl = ifp->if_sadl;
4113 if (sdl == NULL) {
4114 *oldlenp = 0;
4115 goto out1;
4116 }
4117
4118 if (oldp == NULL) {
4119 *oldlenp = sdl->sdl_alen;
4120 goto out1;
4121 }
4122
4123 if (*oldlenp >= sdl->sdl_alen)
4124 *oldlenp = sdl->sdl_alen;
4125 error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
4126 out1:
4127 if_put(ifp, &psref);
4128 out0:
4129 curlwp_bindx(bound);
4130 return error;
4131 }
4132
4133 static void
4134 if_sysctl_setup(struct sysctllog **clog)
4135 {
4136 const struct sysctlnode *rnode = NULL;
4137
4138 sysctl_createv(clog, 0, NULL, &rnode,
4139 CTLFLAG_PERMANENT,
4140 CTLTYPE_NODE, "sdl",
4141 SYSCTL_DESCR("Get active link-layer address"),
4142 if_sdl_sysctl, 0, NULL, 0,
4143 CTL_NET, CTL_CREATE, CTL_EOL);
4144 }
4145