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