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