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