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