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