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