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