if.c revision 1.392 1 /* $NetBSD: if.c,v 1.392 2017/04/06 09:20:07 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.392 2017/04/06 09:20:07 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 #include <netinet/ip_encap.h>
142 #include <net/bpf.h>
143
144 #ifdef INET6
145 #include <netinet6/in6_var.h>
146 #include <netinet6/nd6.h>
147 #endif
148
149 #include "ether.h"
150 #include "fddi.h"
151 #include "token.h"
152
153 #include "carp.h"
154 #if NCARP > 0
155 #include <netinet/ip_carp.h>
156 #endif
157
158 #include <compat/sys/sockio.h>
159 #include <compat/sys/socket.h>
160
161 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
162 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
163
164 /*
165 * Global list of interfaces.
166 */
167 /* DEPRECATED. Remove it once kvm(3) users disappeared */
168 struct ifnet_head ifnet_list;
169
170 struct pslist_head ifnet_pslist;
171 static ifnet_t ** ifindex2ifnet = NULL;
172 static u_int if_index = 1;
173 static size_t if_indexlim = 0;
174 static uint64_t index_gen;
175 /* Mutex to protect the above objects. */
176 kmutex_t ifnet_mtx __cacheline_aligned;
177 static struct psref_class *ifnet_psref_class __read_mostly;
178 static pserialize_t ifnet_psz;
179
180 static kmutex_t if_clone_mtx;
181
182 struct ifnet *lo0ifp;
183 int ifqmaxlen = IFQ_MAXLEN;
184
185 struct psref_class *ifa_psref_class __read_mostly;
186
187 static int if_delroute_matcher(struct rtentry *, void *);
188
189 static bool if_is_unit(const char *);
190 static struct if_clone *if_clone_lookup(const char *, int *);
191
192 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
193 static int if_cloners_count;
194
195 /* Packet filtering hook for interfaces. */
196 pfil_head_t * if_pfil __read_mostly;
197
198 static kauth_listener_t if_listener;
199
200 static int doifioctl(struct socket *, u_long, void *, struct lwp *);
201 static void if_detach_queues(struct ifnet *, struct ifqueue *);
202 static void sysctl_sndq_setup(struct sysctllog **, const char *,
203 struct ifaltq *);
204 static void if_slowtimo(void *);
205 static void if_free_sadl(struct ifnet *);
206 static void if_attachdomain1(struct ifnet *);
207 static int ifconf(u_long, void *);
208 static int if_transmit(struct ifnet *, struct mbuf *);
209 static int if_clone_create(const char *);
210 static int if_clone_destroy(const char *);
211 static void if_link_state_change_si(void *);
212
213 struct if_percpuq {
214 struct ifnet *ipq_ifp;
215 void *ipq_si;
216 struct percpu *ipq_ifqs; /* struct ifqueue */
217 };
218
219 static struct mbuf *if_percpuq_dequeue(struct if_percpuq *);
220
221 static void if_percpuq_drops(void *, void *, struct cpu_info *);
222 static int sysctl_percpuq_drops_handler(SYSCTLFN_PROTO);
223 static void sysctl_percpuq_setup(struct sysctllog **, const char *,
224 struct if_percpuq *);
225
226 struct if_deferred_start {
227 struct ifnet *ids_ifp;
228 void (*ids_if_start)(struct ifnet *);
229 void *ids_si;
230 };
231
232 static void if_deferred_start_softint(void *);
233 static void if_deferred_start_common(struct ifnet *);
234 static void if_deferred_start_destroy(struct ifnet *);
235
236 #if defined(INET) || defined(INET6)
237 static void sysctl_net_pktq_setup(struct sysctllog **, int);
238 #endif
239
240 static void if_sysctl_setup(struct sysctllog **);
241
242 /*
243 * Pointer to stub or real compat_cvtcmd() depending on presence of
244 * the compat module
245 */
246 u_long stub_compat_cvtcmd(u_long);
247 u_long (*vec_compat_cvtcmd)(u_long) = stub_compat_cvtcmd;
248
249 /* Similarly, pointer to compat_ifioctl() if it is present */
250
251 int (*vec_compat_ifioctl)(struct socket *, u_long, u_long, void *,
252 struct lwp *) = NULL;
253
254 /* The stub version of compat_cvtcmd() */
255 u_long stub_compat_cvtcmd(u_long cmd)
256 {
257
258 return cmd;
259 }
260
261 static int
262 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
263 void *arg0, void *arg1, void *arg2, void *arg3)
264 {
265 int result;
266 enum kauth_network_req req;
267
268 result = KAUTH_RESULT_DEFER;
269 req = (enum kauth_network_req)arg1;
270
271 if (action != KAUTH_NETWORK_INTERFACE)
272 return result;
273
274 if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
275 (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
276 result = KAUTH_RESULT_ALLOW;
277
278 return result;
279 }
280
281 /*
282 * Network interface utility routines.
283 *
284 * Routines with ifa_ifwith* names take sockaddr *'s as
285 * parameters.
286 */
287 void
288 ifinit(void)
289 {
290
291 if_sysctl_setup(NULL);
292
293 #if (defined(INET) || defined(INET6))
294 encapinit();
295 #endif
296
297 if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
298 if_listener_cb, NULL);
299
300 /* interfaces are available, inform socket code */
301 ifioctl = doifioctl;
302 }
303
304 /*
305 * XXX Initialization before configure().
306 * XXX hack to get pfil_add_hook working in autoconf.
307 */
308 void
309 ifinit1(void)
310 {
311 mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE);
312
313 TAILQ_INIT(&ifnet_list);
314 mutex_init(&ifnet_mtx, MUTEX_DEFAULT, IPL_NONE);
315 ifnet_psz = pserialize_create();
316 ifnet_psref_class = psref_class_create("ifnet", IPL_SOFTNET);
317 ifa_psref_class = psref_class_create("ifa", IPL_SOFTNET);
318 PSLIST_INIT(&ifnet_pslist);
319
320 if_indexlim = 8;
321
322 if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL);
323 KASSERT(if_pfil != NULL);
324
325 #if NETHER > 0 || NFDDI > 0 || defined(NETATALK) || NTOKEN > 0 || defined(WLAN)
326 etherinit();
327 #endif
328 }
329
330 ifnet_t *
331 if_alloc(u_char type)
332 {
333 return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP);
334 }
335
336 void
337 if_free(ifnet_t *ifp)
338 {
339 kmem_free(ifp, sizeof(ifnet_t));
340 }
341
342 void
343 if_initname(struct ifnet *ifp, const char *name, int unit)
344 {
345 (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
346 "%s%d", name, unit);
347 }
348
349 /*
350 * Null routines used while an interface is going away. These routines
351 * just return an error.
352 */
353
354 int
355 if_nulloutput(struct ifnet *ifp, struct mbuf *m,
356 const struct sockaddr *so, const struct rtentry *rt)
357 {
358
359 return ENXIO;
360 }
361
362 void
363 if_nullinput(struct ifnet *ifp, struct mbuf *m)
364 {
365
366 /* Nothing. */
367 }
368
369 void
370 if_nullstart(struct ifnet *ifp)
371 {
372
373 /* Nothing. */
374 }
375
376 int
377 if_nulltransmit(struct ifnet *ifp, struct mbuf *m)
378 {
379
380 m_freem(m);
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 LIST_INIT(&ifp->if_multiaddrs);
723
724 IFNET_LOCK();
725 if_getindex(ifp);
726 IFNET_UNLOCK();
727 }
728
729 /*
730 * Register an interface to the list of "active" interfaces.
731 */
732 void
733 if_register(ifnet_t *ifp)
734 {
735 /*
736 * If the driver has not supplied its own if_ioctl, then
737 * supply the default.
738 */
739 if (ifp->if_ioctl == NULL)
740 ifp->if_ioctl = ifioctl_common;
741
742 sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
743
744 if (!STAILQ_EMPTY(&domains))
745 if_attachdomain1(ifp);
746
747 /* Announce the interface. */
748 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
749
750 if (ifp->if_slowtimo != NULL) {
751 ifp->if_slowtimo_ch =
752 kmem_zalloc(sizeof(*ifp->if_slowtimo_ch), KM_SLEEP);
753 callout_init(ifp->if_slowtimo_ch, 0);
754 callout_setfunc(ifp->if_slowtimo_ch, if_slowtimo, ifp);
755 if_slowtimo(ifp);
756 }
757
758 if (ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit)
759 ifp->if_transmit = if_transmit;
760
761 IFNET_LOCK();
762 TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list);
763 IFNET_WRITER_INSERT_TAIL(ifp);
764 IFNET_UNLOCK();
765 }
766
767 /*
768 * The if_percpuq framework
769 *
770 * It allows network device drivers to execute the network stack
771 * in softint (so called softint-based if_input). It utilizes
772 * softint and percpu ifqueue. It doesn't distribute any packets
773 * between CPUs, unlike pktqueue(9).
774 *
775 * Currently we support two options for device drivers to apply the framework:
776 * - Use it implicitly with less changes
777 * - If you use if_attach in driver's _attach function and if_input in
778 * driver's Rx interrupt handler, a packet is queued and a softint handles
779 * the packet implicitly
780 * - Use it explicitly in each driver (recommended)
781 * - You can use if_percpuq_* directly in your driver
782 * - In this case, you need to allocate struct if_percpuq in driver's softc
783 * - See wm(4) as a reference implementation
784 */
785
786 static void
787 if_percpuq_softint(void *arg)
788 {
789 struct if_percpuq *ipq = arg;
790 struct ifnet *ifp = ipq->ipq_ifp;
791 struct mbuf *m;
792
793 while ((m = if_percpuq_dequeue(ipq)) != NULL) {
794 ifp->if_ipackets++;
795 bpf_mtap(ifp, m);
796
797 ifp->_if_input(ifp, m);
798 }
799 }
800
801 static void
802 if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
803 {
804 struct ifqueue *const ifq = p;
805
806 memset(ifq, 0, sizeof(*ifq));
807 ifq->ifq_maxlen = IFQ_MAXLEN;
808 }
809
810 struct if_percpuq *
811 if_percpuq_create(struct ifnet *ifp)
812 {
813 struct if_percpuq *ipq;
814
815 ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP);
816 if (ipq == NULL)
817 panic("kmem_zalloc failed");
818
819 ipq->ipq_ifp = ifp;
820 ipq->ipq_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,
821 if_percpuq_softint, ipq);
822 ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue));
823 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL);
824
825 sysctl_percpuq_setup(&ifp->if_sysctl_log, ifp->if_xname, ipq);
826
827 return ipq;
828 }
829
830 static struct mbuf *
831 if_percpuq_dequeue(struct if_percpuq *ipq)
832 {
833 struct mbuf *m;
834 struct ifqueue *ifq;
835 int s;
836
837 s = splnet();
838 ifq = percpu_getref(ipq->ipq_ifqs);
839 IF_DEQUEUE(ifq, m);
840 percpu_putref(ipq->ipq_ifqs);
841 splx(s);
842
843 return m;
844 }
845
846 static void
847 if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
848 {
849 struct ifqueue *const ifq = p;
850
851 IF_PURGE(ifq);
852 }
853
854 void
855 if_percpuq_destroy(struct if_percpuq *ipq)
856 {
857
858 /* if_detach may already destroy it */
859 if (ipq == NULL)
860 return;
861
862 softint_disestablish(ipq->ipq_si);
863 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL);
864 percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue));
865 kmem_free(ipq, sizeof(*ipq));
866 }
867
868 void
869 if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m)
870 {
871 struct ifqueue *ifq;
872 int s;
873
874 KASSERT(ipq != NULL);
875
876 s = splnet();
877 ifq = percpu_getref(ipq->ipq_ifqs);
878 if (IF_QFULL(ifq)) {
879 IF_DROP(ifq);
880 percpu_putref(ipq->ipq_ifqs);
881 m_freem(m);
882 goto out;
883 }
884 IF_ENQUEUE(ifq, m);
885 percpu_putref(ipq->ipq_ifqs);
886
887 softint_schedule(ipq->ipq_si);
888 out:
889 splx(s);
890 }
891
892 static void
893 if_percpuq_drops(void *p, void *arg, struct cpu_info *ci __unused)
894 {
895 struct ifqueue *const ifq = p;
896 int *sum = arg;
897
898 *sum += ifq->ifq_drops;
899 }
900
901 static int
902 sysctl_percpuq_drops_handler(SYSCTLFN_ARGS)
903 {
904 struct sysctlnode node;
905 struct if_percpuq *ipq;
906 int sum = 0;
907 int error;
908
909 node = *rnode;
910 ipq = node.sysctl_data;
911
912 percpu_foreach(ipq->ipq_ifqs, if_percpuq_drops, &sum);
913
914 node.sysctl_data = ∑
915 error = sysctl_lookup(SYSCTLFN_CALL(&node));
916 if (error != 0 || newp == NULL)
917 return error;
918
919 return 0;
920 }
921
922 static void
923 sysctl_percpuq_setup(struct sysctllog **clog, const char* ifname,
924 struct if_percpuq *ipq)
925 {
926 const struct sysctlnode *cnode, *rnode;
927
928 if (sysctl_createv(clog, 0, NULL, &rnode,
929 CTLFLAG_PERMANENT,
930 CTLTYPE_NODE, "interfaces",
931 SYSCTL_DESCR("Per-interface controls"),
932 NULL, 0, NULL, 0,
933 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
934 goto bad;
935
936 if (sysctl_createv(clog, 0, &rnode, &rnode,
937 CTLFLAG_PERMANENT,
938 CTLTYPE_NODE, ifname,
939 SYSCTL_DESCR("Interface controls"),
940 NULL, 0, NULL, 0,
941 CTL_CREATE, CTL_EOL) != 0)
942 goto bad;
943
944 if (sysctl_createv(clog, 0, &rnode, &rnode,
945 CTLFLAG_PERMANENT,
946 CTLTYPE_NODE, "rcvq",
947 SYSCTL_DESCR("Interface input queue controls"),
948 NULL, 0, NULL, 0,
949 CTL_CREATE, CTL_EOL) != 0)
950 goto bad;
951
952 #ifdef NOTYET
953 /* XXX Should show each per-CPU queue length? */
954 if (sysctl_createv(clog, 0, &rnode, &rnode,
955 CTLFLAG_PERMANENT,
956 CTLTYPE_INT, "len",
957 SYSCTL_DESCR("Current input queue length"),
958 sysctl_percpuq_len, 0, NULL, 0,
959 CTL_CREATE, CTL_EOL) != 0)
960 goto bad;
961
962 if (sysctl_createv(clog, 0, &rnode, &cnode,
963 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
964 CTLTYPE_INT, "maxlen",
965 SYSCTL_DESCR("Maximum allowed input queue length"),
966 sysctl_percpuq_maxlen_handler, 0, (void *)ipq, 0,
967 CTL_CREATE, CTL_EOL) != 0)
968 goto bad;
969 #endif
970
971 if (sysctl_createv(clog, 0, &rnode, &cnode,
972 CTLFLAG_PERMANENT,
973 CTLTYPE_INT, "drops",
974 SYSCTL_DESCR("Total packets dropped due to full input queue"),
975 sysctl_percpuq_drops_handler, 0, (void *)ipq, 0,
976 CTL_CREATE, CTL_EOL) != 0)
977 goto bad;
978
979 return;
980 bad:
981 printf("%s: could not attach sysctl nodes\n", ifname);
982 return;
983 }
984
985 /*
986 * The deferred if_start framework
987 *
988 * The common APIs to defer if_start to softint when if_start is requested
989 * from a device driver running in hardware interrupt context.
990 */
991 /*
992 * Call ifp->if_start (or equivalent) in a dedicated softint for
993 * deferred if_start.
994 */
995 static void
996 if_deferred_start_softint(void *arg)
997 {
998 struct if_deferred_start *ids = arg;
999 struct ifnet *ifp = ids->ids_ifp;
1000
1001 ids->ids_if_start(ifp);
1002 }
1003
1004 /*
1005 * The default callback function for deferred if_start.
1006 */
1007 static void
1008 if_deferred_start_common(struct ifnet *ifp)
1009 {
1010 int s;
1011
1012 s = splnet();
1013 if_start_lock(ifp);
1014 splx(s);
1015 }
1016
1017 static inline bool
1018 if_snd_is_used(struct ifnet *ifp)
1019 {
1020
1021 return ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit ||
1022 ALTQ_IS_ENABLED(&ifp->if_snd);
1023 }
1024
1025 /*
1026 * Schedule deferred if_start.
1027 */
1028 void
1029 if_schedule_deferred_start(struct ifnet *ifp)
1030 {
1031
1032 KASSERT(ifp->if_deferred_start != NULL);
1033
1034 if (if_snd_is_used(ifp) && IFQ_IS_EMPTY(&ifp->if_snd))
1035 return;
1036
1037 softint_schedule(ifp->if_deferred_start->ids_si);
1038 }
1039
1040 /*
1041 * Create an instance of deferred if_start. A driver should call the function
1042 * only if the driver needs deferred if_start. Drivers can setup their own
1043 * deferred if_start function via 2nd argument.
1044 */
1045 void
1046 if_deferred_start_init(struct ifnet *ifp, void (*func)(struct ifnet *))
1047 {
1048 struct if_deferred_start *ids;
1049
1050 ids = kmem_zalloc(sizeof(*ids), KM_SLEEP);
1051 if (ids == NULL)
1052 panic("kmem_zalloc failed");
1053
1054 ids->ids_ifp = ifp;
1055 ids->ids_si = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,
1056 if_deferred_start_softint, ids);
1057 if (func != NULL)
1058 ids->ids_if_start = func;
1059 else
1060 ids->ids_if_start = if_deferred_start_common;
1061
1062 ifp->if_deferred_start = ids;
1063 }
1064
1065 static void
1066 if_deferred_start_destroy(struct ifnet *ifp)
1067 {
1068
1069 if (ifp->if_deferred_start == NULL)
1070 return;
1071
1072 softint_disestablish(ifp->if_deferred_start->ids_si);
1073 kmem_free(ifp->if_deferred_start, sizeof(*ifp->if_deferred_start));
1074 ifp->if_deferred_start = NULL;
1075 }
1076
1077 /*
1078 * The common interface input routine that is called by device drivers,
1079 * which should be used only when the driver's rx handler already runs
1080 * in softint.
1081 */
1082 void
1083 if_input(struct ifnet *ifp, struct mbuf *m)
1084 {
1085
1086 KASSERT(ifp->if_percpuq == NULL);
1087 KASSERT(!cpu_intr_p());
1088
1089 ifp->if_ipackets++;
1090 bpf_mtap(ifp, m);
1091
1092 ifp->_if_input(ifp, m);
1093 }
1094
1095 /*
1096 * DEPRECATED. Use if_initialize and if_register instead.
1097 * See the above comment of if_initialize.
1098 *
1099 * Note that it implicitly enables if_percpuq to make drivers easy to
1100 * migrate softint-based if_input without much changes. If you don't
1101 * want to enable it, use if_initialize instead.
1102 */
1103 void
1104 if_attach(ifnet_t *ifp)
1105 {
1106
1107 if_initialize(ifp);
1108 ifp->if_percpuq = if_percpuq_create(ifp);
1109 if_register(ifp);
1110 }
1111
1112 void
1113 if_attachdomain(void)
1114 {
1115 struct ifnet *ifp;
1116 int s;
1117 int bound = curlwp_bind();
1118
1119 s = pserialize_read_enter();
1120 IFNET_READER_FOREACH(ifp) {
1121 struct psref psref;
1122 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
1123 pserialize_read_exit(s);
1124 if_attachdomain1(ifp);
1125 s = pserialize_read_enter();
1126 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1127 }
1128 pserialize_read_exit(s);
1129 curlwp_bindx(bound);
1130 }
1131
1132 static void
1133 if_attachdomain1(struct ifnet *ifp)
1134 {
1135 struct domain *dp;
1136 int s;
1137
1138 s = splsoftnet();
1139
1140 /* address family dependent data region */
1141 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
1142 DOMAIN_FOREACH(dp) {
1143 if (dp->dom_ifattach != NULL)
1144 ifp->if_afdata[dp->dom_family] =
1145 (*dp->dom_ifattach)(ifp);
1146 }
1147
1148 splx(s);
1149 }
1150
1151 /*
1152 * Deactivate an interface. This points all of the procedure
1153 * handles at error stubs. May be called from interrupt context.
1154 */
1155 void
1156 if_deactivate(struct ifnet *ifp)
1157 {
1158 int s;
1159
1160 s = splsoftnet();
1161
1162 ifp->if_output = if_nulloutput;
1163 ifp->_if_input = if_nullinput;
1164 ifp->if_start = if_nullstart;
1165 ifp->if_transmit = if_nulltransmit;
1166 ifp->if_ioctl = if_nullioctl;
1167 ifp->if_init = if_nullinit;
1168 ifp->if_stop = if_nullstop;
1169 ifp->if_slowtimo = if_nullslowtimo;
1170 ifp->if_drain = if_nulldrain;
1171
1172 /* No more packets may be enqueued. */
1173 ifp->if_snd.ifq_maxlen = 0;
1174
1175 splx(s);
1176 }
1177
1178 bool
1179 if_is_deactivated(const struct ifnet *ifp)
1180 {
1181
1182 return ifp->if_output == if_nulloutput;
1183 }
1184
1185 void
1186 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
1187 {
1188 struct ifaddr *ifa, *nifa;
1189 int s;
1190
1191 s = pserialize_read_enter();
1192 for (ifa = IFADDR_READER_FIRST(ifp); ifa; ifa = nifa) {
1193 nifa = IFADDR_READER_NEXT(ifa);
1194 if (ifa->ifa_addr->sa_family != family)
1195 continue;
1196 pserialize_read_exit(s);
1197
1198 (*purgeaddr)(ifa);
1199
1200 s = pserialize_read_enter();
1201 }
1202 pserialize_read_exit(s);
1203 }
1204
1205 #ifdef IFAREF_DEBUG
1206 static struct ifaddr **ifa_list;
1207 static int ifa_list_size;
1208
1209 /* Depends on only one if_attach runs at once */
1210 static void
1211 if_build_ifa_list(struct ifnet *ifp)
1212 {
1213 struct ifaddr *ifa;
1214 int i;
1215
1216 KASSERT(ifa_list == NULL);
1217 KASSERT(ifa_list_size == 0);
1218
1219 IFADDR_READER_FOREACH(ifa, ifp)
1220 ifa_list_size++;
1221
1222 ifa_list = kmem_alloc(sizeof(*ifa) * ifa_list_size, KM_SLEEP);
1223 if (ifa_list == NULL)
1224 return;
1225
1226 i = 0;
1227 IFADDR_READER_FOREACH(ifa, ifp) {
1228 ifa_list[i++] = ifa;
1229 ifaref(ifa);
1230 }
1231 }
1232
1233 static void
1234 if_check_and_free_ifa_list(struct ifnet *ifp)
1235 {
1236 int i;
1237 struct ifaddr *ifa;
1238
1239 if (ifa_list == NULL)
1240 return;
1241
1242 for (i = 0; i < ifa_list_size; i++) {
1243 char buf[64];
1244
1245 ifa = ifa_list[i];
1246 sockaddr_format(ifa->ifa_addr, buf, sizeof(buf));
1247 if (ifa->ifa_refcnt > 1) {
1248 log(LOG_WARNING,
1249 "ifa(%s) still referenced (refcnt=%d)\n",
1250 buf, ifa->ifa_refcnt - 1);
1251 } else
1252 log(LOG_DEBUG,
1253 "ifa(%s) not referenced (refcnt=%d)\n",
1254 buf, ifa->ifa_refcnt - 1);
1255 ifafree(ifa);
1256 }
1257
1258 kmem_free(ifa_list, sizeof(*ifa) * ifa_list_size);
1259 ifa_list = NULL;
1260 ifa_list_size = 0;
1261 }
1262 #endif
1263
1264 /*
1265 * Detach an interface from the list of "active" interfaces,
1266 * freeing any resources as we go along.
1267 *
1268 * NOTE: This routine must be called with a valid thread context,
1269 * as it may block.
1270 */
1271 void
1272 if_detach(struct ifnet *ifp)
1273 {
1274 struct socket so;
1275 struct ifaddr *ifa;
1276 #ifdef IFAREF_DEBUG
1277 struct ifaddr *last_ifa = NULL;
1278 #endif
1279 struct domain *dp;
1280 const struct protosw *pr;
1281 int s, i, family, purged;
1282 uint64_t xc;
1283
1284 #ifdef IFAREF_DEBUG
1285 if_build_ifa_list(ifp);
1286 #endif
1287 /*
1288 * XXX It's kind of lame that we have to have the
1289 * XXX socket structure...
1290 */
1291 memset(&so, 0, sizeof(so));
1292
1293 s = splnet();
1294
1295 sysctl_teardown(&ifp->if_sysctl_log);
1296 mutex_enter(ifp->if_ioctl_lock);
1297 if_deactivate(ifp);
1298 mutex_exit(ifp->if_ioctl_lock);
1299
1300 IFNET_LOCK();
1301 ifindex2ifnet[ifp->if_index] = NULL;
1302 TAILQ_REMOVE(&ifnet_list, ifp, if_list);
1303 IFNET_WRITER_REMOVE(ifp);
1304 pserialize_perform(ifnet_psz);
1305 IFNET_UNLOCK();
1306
1307 /* Wait for all readers to drain before freeing. */
1308 psref_target_destroy(&ifp->if_psref, ifnet_psref_class);
1309 PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry);
1310
1311 mutex_obj_free(ifp->if_ioctl_lock);
1312 ifp->if_ioctl_lock = NULL;
1313
1314 if (ifp->if_slowtimo != NULL && ifp->if_slowtimo_ch != NULL) {
1315 ifp->if_slowtimo = NULL;
1316 callout_halt(ifp->if_slowtimo_ch, NULL);
1317 callout_destroy(ifp->if_slowtimo_ch);
1318 kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch));
1319 }
1320 if_deferred_start_destroy(ifp);
1321
1322 /*
1323 * Do an if_down() to give protocols a chance to do something.
1324 */
1325 if_down(ifp);
1326
1327 #ifdef ALTQ
1328 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1329 altq_disable(&ifp->if_snd);
1330 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1331 altq_detach(&ifp->if_snd);
1332 #endif
1333
1334 mutex_obj_free(ifp->if_snd.ifq_lock);
1335
1336 #if NCARP > 0
1337 /* Remove the interface from any carp group it is a part of. */
1338 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
1339 carp_ifdetach(ifp);
1340 #endif
1341
1342 /*
1343 * Rip all the addresses off the interface. This should make
1344 * all of the routes go away.
1345 *
1346 * pr_usrreq calls can remove an arbitrary number of ifaddrs
1347 * from the list, including our "cursor", ifa. For safety,
1348 * and to honor the TAILQ abstraction, I just restart the
1349 * loop after each removal. Note that the loop will exit
1350 * when all of the remaining ifaddrs belong to the AF_LINK
1351 * family. I am counting on the historical fact that at
1352 * least one pr_usrreq in each address domain removes at
1353 * least one ifaddr.
1354 */
1355 again:
1356 /*
1357 * At this point, no other one tries to remove ifa in the list,
1358 * so we don't need to take a lock or psref.
1359 */
1360 IFADDR_READER_FOREACH(ifa, ifp) {
1361 family = ifa->ifa_addr->sa_family;
1362 #ifdef IFAREF_DEBUG
1363 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
1364 ifa, family, ifa->ifa_refcnt);
1365 if (last_ifa != NULL && ifa == last_ifa)
1366 panic("if_detach: loop detected");
1367 last_ifa = ifa;
1368 #endif
1369 if (family == AF_LINK)
1370 continue;
1371 dp = pffinddomain(family);
1372 KASSERTMSG(dp != NULL, "no domain for AF %d", family);
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 static bool
1598 if_is_unit(const char *name)
1599 {
1600
1601 while(*name != '\0') {
1602 if (*name < '0' || *name > '9')
1603 return false;
1604 name++;
1605 }
1606
1607 return true;
1608 }
1609
1610 /*
1611 * Look up a network interface cloner.
1612 */
1613 static struct if_clone *
1614 if_clone_lookup(const char *name, int *unitp)
1615 {
1616 struct if_clone *ifc;
1617 const char *cp;
1618 char *dp, ifname[IFNAMSIZ + 3];
1619 int unit;
1620
1621 KASSERT(mutex_owned(&if_clone_mtx));
1622
1623 strcpy(ifname, "if_");
1624 /* separate interface name from unit */
1625 /* TODO: search unit number from backward */
1626 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
1627 *cp && !if_is_unit(cp);)
1628 *dp++ = *cp++;
1629
1630 if (cp == name || cp - name == IFNAMSIZ || !*cp)
1631 return NULL; /* No name or unit number */
1632 *dp++ = '\0';
1633
1634 again:
1635 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1636 if (strcmp(ifname + 3, ifc->ifc_name) == 0)
1637 break;
1638 }
1639
1640 if (ifc == NULL) {
1641 int error;
1642 if (*ifname == '\0')
1643 return NULL;
1644 mutex_exit(&if_clone_mtx);
1645 error = module_autoload(ifname, MODULE_CLASS_DRIVER);
1646 mutex_enter(&if_clone_mtx);
1647 if (error)
1648 return NULL;
1649 *ifname = '\0';
1650 goto again;
1651 }
1652
1653 unit = 0;
1654 while (cp - name < IFNAMSIZ && *cp) {
1655 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1656 /* Bogus unit number. */
1657 return NULL;
1658 }
1659 unit = (unit * 10) + (*cp++ - '0');
1660 }
1661
1662 if (unitp != NULL)
1663 *unitp = unit;
1664 return ifc;
1665 }
1666
1667 /*
1668 * Register a network interface cloner.
1669 */
1670 void
1671 if_clone_attach(struct if_clone *ifc)
1672 {
1673
1674 mutex_enter(&if_clone_mtx);
1675 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1676 if_cloners_count++;
1677 mutex_exit(&if_clone_mtx);
1678 }
1679
1680 /*
1681 * Unregister a network interface cloner.
1682 */
1683 void
1684 if_clone_detach(struct if_clone *ifc)
1685 {
1686
1687 mutex_enter(&if_clone_mtx);
1688 LIST_REMOVE(ifc, ifc_list);
1689 if_cloners_count--;
1690 mutex_exit(&if_clone_mtx);
1691 }
1692
1693 /*
1694 * Provide list of interface cloners to userspace.
1695 */
1696 int
1697 if_clone_list(int buf_count, char *buffer, int *total)
1698 {
1699 char outbuf[IFNAMSIZ], *dst;
1700 struct if_clone *ifc;
1701 int count, error = 0;
1702
1703 mutex_enter(&if_clone_mtx);
1704 *total = if_cloners_count;
1705 if ((dst = buffer) == NULL) {
1706 /* Just asking how many there are. */
1707 goto out;
1708 }
1709
1710 if (buf_count < 0) {
1711 error = EINVAL;
1712 goto out;
1713 }
1714
1715 count = (if_cloners_count < buf_count) ?
1716 if_cloners_count : buf_count;
1717
1718 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
1719 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1720 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1721 if (outbuf[sizeof(outbuf) - 1] != '\0') {
1722 error = ENAMETOOLONG;
1723 goto out;
1724 }
1725 error = copyout(outbuf, dst, sizeof(outbuf));
1726 if (error != 0)
1727 break;
1728 }
1729
1730 out:
1731 mutex_exit(&if_clone_mtx);
1732 return error;
1733 }
1734
1735 void
1736 ifa_psref_init(struct ifaddr *ifa)
1737 {
1738
1739 psref_target_init(&ifa->ifa_psref, ifa_psref_class);
1740 }
1741
1742 void
1743 ifaref(struct ifaddr *ifa)
1744 {
1745 KASSERT(!ISSET(ifa->ifa_flags, IFA_DESTROYING));
1746 ifa->ifa_refcnt++;
1747 }
1748
1749 void
1750 ifafree(struct ifaddr *ifa)
1751 {
1752 KASSERT(ifa != NULL);
1753 KASSERT(ifa->ifa_refcnt > 0);
1754
1755 if (--ifa->ifa_refcnt == 0) {
1756 free(ifa, M_IFADDR);
1757 }
1758 }
1759
1760 bool
1761 ifa_is_destroying(struct ifaddr *ifa)
1762 {
1763
1764 return ISSET(ifa->ifa_flags, IFA_DESTROYING);
1765 }
1766
1767 void
1768 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
1769 {
1770
1771 ifa->ifa_ifp = ifp;
1772
1773 IFNET_LOCK();
1774 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1775 IFADDR_ENTRY_INIT(ifa);
1776 IFADDR_WRITER_INSERT_TAIL(ifp, ifa);
1777 IFNET_UNLOCK();
1778
1779 ifaref(ifa);
1780 }
1781
1782 void
1783 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
1784 {
1785
1786 KASSERT(ifa->ifa_ifp == ifp);
1787
1788 IFNET_LOCK();
1789 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1790 IFADDR_WRITER_REMOVE(ifa);
1791 #ifdef NET_MPSAFE
1792 pserialize_perform(ifnet_psz);
1793 #endif
1794 IFNET_UNLOCK();
1795
1796 #ifdef NET_MPSAFE
1797 psref_target_destroy(&ifa->ifa_psref, ifa_psref_class);
1798 #endif
1799 IFADDR_ENTRY_DESTROY(ifa);
1800 ifafree(ifa);
1801 }
1802
1803 void
1804 ifa_acquire(struct ifaddr *ifa, struct psref *psref)
1805 {
1806
1807 psref_acquire(psref, &ifa->ifa_psref, ifa_psref_class);
1808 }
1809
1810 void
1811 ifa_release(struct ifaddr *ifa, struct psref *psref)
1812 {
1813
1814 if (ifa == NULL)
1815 return;
1816
1817 psref_release(psref, &ifa->ifa_psref, ifa_psref_class);
1818 }
1819
1820 bool
1821 ifa_held(struct ifaddr *ifa)
1822 {
1823
1824 return psref_held(&ifa->ifa_psref, ifa_psref_class);
1825 }
1826
1827 static inline int
1828 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
1829 {
1830 return sockaddr_cmp(sa1, sa2) == 0;
1831 }
1832
1833 /*
1834 * Locate an interface based on a complete address.
1835 */
1836 /*ARGSUSED*/
1837 struct ifaddr *
1838 ifa_ifwithaddr(const struct sockaddr *addr)
1839 {
1840 struct ifnet *ifp;
1841 struct ifaddr *ifa;
1842
1843 IFNET_READER_FOREACH(ifp) {
1844 if (if_is_deactivated(ifp))
1845 continue;
1846 IFADDR_READER_FOREACH(ifa, ifp) {
1847 if (ifa->ifa_addr->sa_family != addr->sa_family)
1848 continue;
1849 if (equal(addr, ifa->ifa_addr))
1850 return ifa;
1851 if ((ifp->if_flags & IFF_BROADCAST) &&
1852 ifa->ifa_broadaddr &&
1853 /* IP6 doesn't have broadcast */
1854 ifa->ifa_broadaddr->sa_len != 0 &&
1855 equal(ifa->ifa_broadaddr, addr))
1856 return ifa;
1857 }
1858 }
1859 return NULL;
1860 }
1861
1862 struct ifaddr *
1863 ifa_ifwithaddr_psref(const struct sockaddr *addr, struct psref *psref)
1864 {
1865 struct ifaddr *ifa;
1866 int s = pserialize_read_enter();
1867
1868 ifa = ifa_ifwithaddr(addr);
1869 if (ifa != NULL)
1870 ifa_acquire(ifa, psref);
1871 pserialize_read_exit(s);
1872
1873 return ifa;
1874 }
1875
1876 /*
1877 * Locate the point to point interface with a given destination address.
1878 */
1879 /*ARGSUSED*/
1880 struct ifaddr *
1881 ifa_ifwithdstaddr(const struct sockaddr *addr)
1882 {
1883 struct ifnet *ifp;
1884 struct ifaddr *ifa;
1885
1886 IFNET_READER_FOREACH(ifp) {
1887 if (if_is_deactivated(ifp))
1888 continue;
1889 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1890 continue;
1891 IFADDR_READER_FOREACH(ifa, ifp) {
1892 if (ifa->ifa_addr->sa_family != addr->sa_family ||
1893 ifa->ifa_dstaddr == NULL)
1894 continue;
1895 if (equal(addr, ifa->ifa_dstaddr))
1896 return ifa;
1897 }
1898 }
1899
1900 return NULL;
1901 }
1902
1903 struct ifaddr *
1904 ifa_ifwithdstaddr_psref(const struct sockaddr *addr, struct psref *psref)
1905 {
1906 struct ifaddr *ifa;
1907 int s;
1908
1909 s = pserialize_read_enter();
1910 ifa = ifa_ifwithdstaddr(addr);
1911 if (ifa != NULL)
1912 ifa_acquire(ifa, psref);
1913 pserialize_read_exit(s);
1914
1915 return ifa;
1916 }
1917
1918 /*
1919 * Find an interface on a specific network. If many, choice
1920 * is most specific found.
1921 */
1922 struct ifaddr *
1923 ifa_ifwithnet(const struct sockaddr *addr)
1924 {
1925 struct ifnet *ifp;
1926 struct ifaddr *ifa, *ifa_maybe = NULL;
1927 const struct sockaddr_dl *sdl;
1928 u_int af = addr->sa_family;
1929 const char *addr_data = addr->sa_data, *cplim;
1930
1931 if (af == AF_LINK) {
1932 sdl = satocsdl(addr);
1933 if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1934 ifindex2ifnet[sdl->sdl_index] &&
1935 !if_is_deactivated(ifindex2ifnet[sdl->sdl_index])) {
1936 return ifindex2ifnet[sdl->sdl_index]->if_dl;
1937 }
1938 }
1939 #ifdef NETATALK
1940 if (af == AF_APPLETALK) {
1941 const struct sockaddr_at *sat, *sat2;
1942 sat = (const struct sockaddr_at *)addr;
1943 IFNET_READER_FOREACH(ifp) {
1944 if (if_is_deactivated(ifp))
1945 continue;
1946 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1947 if (ifa == NULL)
1948 continue;
1949 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1950 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1951 return ifa; /* exact match */
1952 if (ifa_maybe == NULL) {
1953 /* else keep the if with the right range */
1954 ifa_maybe = ifa;
1955 }
1956 }
1957 return ifa_maybe;
1958 }
1959 #endif
1960 IFNET_READER_FOREACH(ifp) {
1961 if (if_is_deactivated(ifp))
1962 continue;
1963 IFADDR_READER_FOREACH(ifa, ifp) {
1964 const char *cp, *cp2, *cp3;
1965
1966 if (ifa->ifa_addr->sa_family != af ||
1967 ifa->ifa_netmask == NULL)
1968 next: continue;
1969 cp = addr_data;
1970 cp2 = ifa->ifa_addr->sa_data;
1971 cp3 = ifa->ifa_netmask->sa_data;
1972 cplim = (const char *)ifa->ifa_netmask +
1973 ifa->ifa_netmask->sa_len;
1974 while (cp3 < cplim) {
1975 if ((*cp++ ^ *cp2++) & *cp3++) {
1976 /* want to continue for() loop */
1977 goto next;
1978 }
1979 }
1980 if (ifa_maybe == NULL ||
1981 rt_refines(ifa->ifa_netmask,
1982 ifa_maybe->ifa_netmask))
1983 ifa_maybe = ifa;
1984 }
1985 }
1986 return ifa_maybe;
1987 }
1988
1989 struct ifaddr *
1990 ifa_ifwithnet_psref(const struct sockaddr *addr, struct psref *psref)
1991 {
1992 struct ifaddr *ifa;
1993 int s;
1994
1995 s = pserialize_read_enter();
1996 ifa = ifa_ifwithnet(addr);
1997 if (ifa != NULL)
1998 ifa_acquire(ifa, psref);
1999 pserialize_read_exit(s);
2000
2001 return ifa;
2002 }
2003
2004 /*
2005 * Find the interface of the addresss.
2006 */
2007 struct ifaddr *
2008 ifa_ifwithladdr(const struct sockaddr *addr)
2009 {
2010 struct ifaddr *ia;
2011
2012 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
2013 (ia = ifa_ifwithnet(addr)))
2014 return ia;
2015 return NULL;
2016 }
2017
2018 struct ifaddr *
2019 ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref)
2020 {
2021 struct ifaddr *ifa;
2022 int s;
2023
2024 s = pserialize_read_enter();
2025 ifa = ifa_ifwithladdr(addr);
2026 if (ifa != NULL)
2027 ifa_acquire(ifa, psref);
2028 pserialize_read_exit(s);
2029
2030 return ifa;
2031 }
2032
2033 /*
2034 * Find an interface using a specific address family
2035 */
2036 struct ifaddr *
2037 ifa_ifwithaf(int af)
2038 {
2039 struct ifnet *ifp;
2040 struct ifaddr *ifa = NULL;
2041 int s;
2042
2043 s = pserialize_read_enter();
2044 IFNET_READER_FOREACH(ifp) {
2045 if (if_is_deactivated(ifp))
2046 continue;
2047 IFADDR_READER_FOREACH(ifa, ifp) {
2048 if (ifa->ifa_addr->sa_family == af)
2049 goto out;
2050 }
2051 }
2052 out:
2053 pserialize_read_exit(s);
2054 return ifa;
2055 }
2056
2057 /*
2058 * Find an interface address specific to an interface best matching
2059 * a given address.
2060 */
2061 struct ifaddr *
2062 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2063 {
2064 struct ifaddr *ifa;
2065 const char *cp, *cp2, *cp3;
2066 const char *cplim;
2067 struct ifaddr *ifa_maybe = 0;
2068 u_int af = addr->sa_family;
2069
2070 if (if_is_deactivated(ifp))
2071 return NULL;
2072
2073 if (af >= AF_MAX)
2074 return NULL;
2075
2076 IFADDR_READER_FOREACH(ifa, ifp) {
2077 if (ifa->ifa_addr->sa_family != af)
2078 continue;
2079 ifa_maybe = ifa;
2080 if (ifa->ifa_netmask == NULL) {
2081 if (equal(addr, ifa->ifa_addr) ||
2082 (ifa->ifa_dstaddr &&
2083 equal(addr, ifa->ifa_dstaddr)))
2084 return ifa;
2085 continue;
2086 }
2087 cp = addr->sa_data;
2088 cp2 = ifa->ifa_addr->sa_data;
2089 cp3 = ifa->ifa_netmask->sa_data;
2090 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2091 for (; cp3 < cplim; cp3++) {
2092 if ((*cp++ ^ *cp2++) & *cp3)
2093 break;
2094 }
2095 if (cp3 == cplim)
2096 return ifa;
2097 }
2098 return ifa_maybe;
2099 }
2100
2101 struct ifaddr *
2102 ifaof_ifpforaddr_psref(const struct sockaddr *addr, struct ifnet *ifp,
2103 struct psref *psref)
2104 {
2105 struct ifaddr *ifa;
2106 int s;
2107
2108 s = pserialize_read_enter();
2109 ifa = ifaof_ifpforaddr(addr, ifp);
2110 if (ifa != NULL)
2111 ifa_acquire(ifa, psref);
2112 pserialize_read_exit(s);
2113
2114 return ifa;
2115 }
2116
2117 /*
2118 * Default action when installing a route with a Link Level gateway.
2119 * Lookup an appropriate real ifa to point to.
2120 * This should be moved to /sys/net/link.c eventually.
2121 */
2122 void
2123 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
2124 {
2125 struct ifaddr *ifa;
2126 const struct sockaddr *dst;
2127 struct ifnet *ifp;
2128 struct psref psref;
2129
2130 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
2131 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
2132 return;
2133 if ((ifa = ifaof_ifpforaddr_psref(dst, ifp, &psref)) != NULL) {
2134 rt_replace_ifa(rt, ifa);
2135 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2136 ifa->ifa_rtrequest(cmd, rt, info);
2137 ifa_release(ifa, &psref);
2138 }
2139 }
2140
2141 /*
2142 * bitmask macros to manage a densely packed link_state change queue.
2143 * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
2144 * LINK_STATE_UP(2) we need 2 bits for each state change.
2145 * As a state change to store is 0, treat all bits set as an unset item.
2146 */
2147 #define LQ_ITEM_BITS 2
2148 #define LQ_ITEM_MASK ((1 << LQ_ITEM_BITS) - 1)
2149 #define LQ_MASK(i) (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
2150 #define LINK_STATE_UNSET LQ_ITEM_MASK
2151 #define LQ_ITEM(q, i) (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
2152 #define LQ_STORE(q, i, v) \
2153 do { \
2154 (q) &= ~LQ_MASK((i)); \
2155 (q) |= (v) << (i) * LQ_ITEM_BITS; \
2156 } while (0 /* CONSTCOND */)
2157 #define LQ_MAX(q) ((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
2158 #define LQ_POP(q, v) \
2159 do { \
2160 (v) = LQ_ITEM((q), 0); \
2161 (q) >>= LQ_ITEM_BITS; \
2162 (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \
2163 } while (0 /* CONSTCOND */)
2164 #define LQ_PUSH(q, v) \
2165 do { \
2166 (q) >>= LQ_ITEM_BITS; \
2167 (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \
2168 } while (0 /* CONSTCOND */)
2169 #define LQ_FIND_UNSET(q, i) \
2170 for ((i) = 0; i < LQ_MAX((q)); (i)++) { \
2171 if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET) \
2172 break; \
2173 }
2174 /*
2175 * Handle a change in the interface link state and
2176 * queue notifications.
2177 */
2178 void
2179 if_link_state_change(struct ifnet *ifp, int link_state)
2180 {
2181 int s, idx;
2182
2183 KASSERTMSG(if_is_link_state_changeable(ifp),
2184 "%s: IFEF_NO_LINK_STATE_CHANGE must not be set, but if_extflags=0x%x",
2185 ifp->if_xname, ifp->if_extflags);
2186
2187 /* Ensure change is to a valid state */
2188 switch (link_state) {
2189 case LINK_STATE_UNKNOWN: /* FALLTHROUGH */
2190 case LINK_STATE_DOWN: /* FALLTHROUGH */
2191 case LINK_STATE_UP:
2192 break;
2193 default:
2194 #ifdef DEBUG
2195 printf("%s: invalid link state %d\n",
2196 ifp->if_xname, link_state);
2197 #endif
2198 return;
2199 }
2200
2201 s = splnet();
2202
2203 /* Find the last unset event in the queue. */
2204 LQ_FIND_UNSET(ifp->if_link_queue, idx);
2205
2206 /*
2207 * Ensure link_state doesn't match the last event in the queue.
2208 * ifp->if_link_state is not checked and set here because
2209 * that would present an inconsistent picture to the system.
2210 */
2211 if (idx != 0 &&
2212 LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
2213 goto out;
2214
2215 /* Handle queue overflow. */
2216 if (idx == LQ_MAX(ifp->if_link_queue)) {
2217 uint8_t lost;
2218
2219 /*
2220 * The DOWN state must be protected from being pushed off
2221 * the queue to ensure that userland will always be
2222 * in a sane state.
2223 * Because DOWN is protected, there is no need to protect
2224 * UNKNOWN.
2225 * It should be invalid to change from any other state to
2226 * UNKNOWN anyway ...
2227 */
2228 lost = LQ_ITEM(ifp->if_link_queue, 0);
2229 LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
2230 if (lost == LINK_STATE_DOWN) {
2231 lost = LQ_ITEM(ifp->if_link_queue, 0);
2232 LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
2233 }
2234 printf("%s: lost link state change %s\n",
2235 ifp->if_xname,
2236 lost == LINK_STATE_UP ? "UP" :
2237 lost == LINK_STATE_DOWN ? "DOWN" :
2238 "UNKNOWN");
2239 } else
2240 LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
2241
2242 softint_schedule(ifp->if_link_si);
2243
2244 out:
2245 splx(s);
2246 }
2247
2248 /*
2249 * Handle interface link state change notifications.
2250 * Must be called at splnet().
2251 */
2252 static void
2253 if_link_state_change0(struct ifnet *ifp, int link_state)
2254 {
2255 struct domain *dp;
2256
2257 /* Ensure the change is still valid. */
2258 if (ifp->if_link_state == link_state)
2259 return;
2260
2261 #ifdef DEBUG
2262 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
2263 link_state == LINK_STATE_UP ? "UP" :
2264 link_state == LINK_STATE_DOWN ? "DOWN" :
2265 "UNKNOWN",
2266 ifp->if_link_state == LINK_STATE_UP ? "UP" :
2267 ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
2268 "UNKNOWN");
2269 #endif
2270
2271 /*
2272 * When going from UNKNOWN to UP, we need to mark existing
2273 * addresses as tentative and restart DAD as we may have
2274 * erroneously not found a duplicate.
2275 *
2276 * This needs to happen before rt_ifmsg to avoid a race where
2277 * listeners would have an address and expect it to work right
2278 * away.
2279 */
2280 if (link_state == LINK_STATE_UP &&
2281 ifp->if_link_state == LINK_STATE_UNKNOWN)
2282 {
2283 DOMAIN_FOREACH(dp) {
2284 if (dp->dom_if_link_state_change != NULL)
2285 dp->dom_if_link_state_change(ifp,
2286 LINK_STATE_DOWN);
2287 }
2288 }
2289
2290 ifp->if_link_state = link_state;
2291
2292 /* Notify that the link state has changed. */
2293 rt_ifmsg(ifp);
2294
2295 #if NCARP > 0
2296 if (ifp->if_carp)
2297 carp_carpdev_state(ifp);
2298 #endif
2299
2300 DOMAIN_FOREACH(dp) {
2301 if (dp->dom_if_link_state_change != NULL)
2302 dp->dom_if_link_state_change(ifp, link_state);
2303 }
2304 }
2305
2306 /*
2307 * Process the interface link state change queue.
2308 */
2309 static void
2310 if_link_state_change_si(void *arg)
2311 {
2312 struct ifnet *ifp = arg;
2313 int s;
2314 uint8_t state;
2315
2316 #ifndef NET_MPSAFE
2317 mutex_enter(softnet_lock);
2318 KERNEL_LOCK(1, NULL);
2319 #endif
2320 s = splnet();
2321
2322 /* Pop a link state change from the queue and process it. */
2323 LQ_POP(ifp->if_link_queue, state);
2324 if_link_state_change0(ifp, state);
2325
2326 /* If there is a link state change to come, schedule it. */
2327 if (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET)
2328 softint_schedule(ifp->if_link_si);
2329
2330 splx(s);
2331 #ifndef NET_MPSAFE
2332 KERNEL_UNLOCK_ONE(NULL);
2333 mutex_exit(softnet_lock);
2334 #endif
2335 }
2336
2337 /*
2338 * Default action when installing a local route on a point-to-point
2339 * interface.
2340 */
2341 void
2342 p2p_rtrequest(int req, struct rtentry *rt,
2343 __unused const struct rt_addrinfo *info)
2344 {
2345 struct ifnet *ifp = rt->rt_ifp;
2346 struct ifaddr *ifa, *lo0ifa;
2347 int s = pserialize_read_enter();
2348
2349 switch (req) {
2350 case RTM_ADD:
2351 if ((rt->rt_flags & RTF_LOCAL) == 0)
2352 break;
2353
2354 rt->rt_ifp = lo0ifp;
2355
2356 IFADDR_READER_FOREACH(ifa, ifp) {
2357 if (equal(rt_getkey(rt), ifa->ifa_addr))
2358 break;
2359 }
2360 if (ifa == NULL)
2361 break;
2362
2363 /*
2364 * Ensure lo0 has an address of the same family.
2365 */
2366 IFADDR_READER_FOREACH(lo0ifa, lo0ifp) {
2367 if (lo0ifa->ifa_addr->sa_family ==
2368 ifa->ifa_addr->sa_family)
2369 break;
2370 }
2371 if (lo0ifa == NULL)
2372 break;
2373
2374 /*
2375 * Make sure to set rt->rt_ifa to the interface
2376 * address we are using, otherwise we will have trouble
2377 * with source address selection.
2378 */
2379 if (ifa != rt->rt_ifa)
2380 rt_replace_ifa(rt, ifa);
2381 break;
2382 case RTM_DELETE:
2383 default:
2384 break;
2385 }
2386 pserialize_read_exit(s);
2387 }
2388
2389 /*
2390 * Mark an interface down and notify protocols of
2391 * the transition.
2392 * NOTE: must be called at splsoftnet or equivalent.
2393 */
2394 void
2395 if_down(struct ifnet *ifp)
2396 {
2397 struct ifaddr *ifa;
2398 struct domain *dp;
2399 int s, bound;
2400 struct psref psref;
2401
2402 ifp->if_flags &= ~IFF_UP;
2403 nanotime(&ifp->if_lastchange);
2404
2405 bound = curlwp_bind();
2406 s = pserialize_read_enter();
2407 IFADDR_READER_FOREACH(ifa, ifp) {
2408 ifa_acquire(ifa, &psref);
2409 pserialize_read_exit(s);
2410
2411 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2412
2413 s = pserialize_read_enter();
2414 ifa_release(ifa, &psref);
2415 }
2416 pserialize_read_exit(s);
2417 curlwp_bindx(bound);
2418
2419 IFQ_PURGE(&ifp->if_snd);
2420 #if NCARP > 0
2421 if (ifp->if_carp)
2422 carp_carpdev_state(ifp);
2423 #endif
2424 rt_ifmsg(ifp);
2425 DOMAIN_FOREACH(dp) {
2426 if (dp->dom_if_down)
2427 dp->dom_if_down(ifp);
2428 }
2429 }
2430
2431 /*
2432 * Mark an interface up and notify protocols of
2433 * the transition.
2434 * NOTE: must be called at splsoftnet or equivalent.
2435 */
2436 void
2437 if_up(struct ifnet *ifp)
2438 {
2439 #ifdef notyet
2440 struct ifaddr *ifa;
2441 #endif
2442 struct domain *dp;
2443
2444 ifp->if_flags |= IFF_UP;
2445 nanotime(&ifp->if_lastchange);
2446 #ifdef notyet
2447 /* this has no effect on IP, and will kill all ISO connections XXX */
2448 IFADDR_READER_FOREACH(ifa, ifp)
2449 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2450 #endif
2451 #if NCARP > 0
2452 if (ifp->if_carp)
2453 carp_carpdev_state(ifp);
2454 #endif
2455 rt_ifmsg(ifp);
2456 DOMAIN_FOREACH(dp) {
2457 if (dp->dom_if_up)
2458 dp->dom_if_up(ifp);
2459 }
2460 }
2461
2462 /*
2463 * Handle interface slowtimo timer routine. Called
2464 * from softclock, we decrement timer (if set) and
2465 * call the appropriate interface routine on expiration.
2466 */
2467 static void
2468 if_slowtimo(void *arg)
2469 {
2470 void (*slowtimo)(struct ifnet *);
2471 struct ifnet *ifp = arg;
2472 int s;
2473
2474 slowtimo = ifp->if_slowtimo;
2475 if (__predict_false(slowtimo == NULL))
2476 return;
2477
2478 s = splnet();
2479 if (ifp->if_timer != 0 && --ifp->if_timer == 0)
2480 (*slowtimo)(ifp);
2481
2482 splx(s);
2483
2484 if (__predict_true(ifp->if_slowtimo != NULL))
2485 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
2486 }
2487
2488 /*
2489 * Set/clear promiscuous mode on interface ifp based on the truth value
2490 * of pswitch. The calls are reference counted so that only the first
2491 * "on" request actually has an effect, as does the final "off" request.
2492 * Results are undefined if the "off" and "on" requests are not matched.
2493 */
2494 int
2495 ifpromisc(struct ifnet *ifp, int pswitch)
2496 {
2497 int pcount, ret;
2498 short nflags;
2499
2500 pcount = ifp->if_pcount;
2501 if (pswitch) {
2502 /*
2503 * Allow the device to be "placed" into promiscuous
2504 * mode even if it is not configured up. It will
2505 * consult IFF_PROMISC when it is brought up.
2506 */
2507 if (ifp->if_pcount++ != 0)
2508 return 0;
2509 nflags = ifp->if_flags | IFF_PROMISC;
2510 } else {
2511 if (--ifp->if_pcount > 0)
2512 return 0;
2513 nflags = ifp->if_flags & ~IFF_PROMISC;
2514 }
2515 ret = if_flags_set(ifp, nflags);
2516 /* Restore interface state if not successful. */
2517 if (ret != 0) {
2518 ifp->if_pcount = pcount;
2519 }
2520 return ret;
2521 }
2522
2523 /*
2524 * Map interface name to
2525 * interface structure pointer.
2526 */
2527 struct ifnet *
2528 ifunit(const char *name)
2529 {
2530 struct ifnet *ifp;
2531 const char *cp = name;
2532 u_int unit = 0;
2533 u_int i;
2534 int s;
2535
2536 /*
2537 * If the entire name is a number, treat it as an ifindex.
2538 */
2539 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2540 unit = unit * 10 + (*cp - '0');
2541 }
2542
2543 /*
2544 * If the number took all of the name, then it's a valid ifindex.
2545 */
2546 if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
2547 return if_byindex(unit);
2548
2549 ifp = NULL;
2550 s = pserialize_read_enter();
2551 IFNET_READER_FOREACH(ifp) {
2552 if (if_is_deactivated(ifp))
2553 continue;
2554 if (strcmp(ifp->if_xname, name) == 0)
2555 goto out;
2556 }
2557 out:
2558 pserialize_read_exit(s);
2559 return ifp;
2560 }
2561
2562 /*
2563 * Get a reference of an ifnet object by an interface name.
2564 * The returned reference is protected by psref(9). The caller
2565 * must release a returned reference by if_put after use.
2566 */
2567 struct ifnet *
2568 if_get(const char *name, struct psref *psref)
2569 {
2570 struct ifnet *ifp;
2571 const char *cp = name;
2572 u_int unit = 0;
2573 u_int i;
2574 int s;
2575
2576 /*
2577 * If the entire name is a number, treat it as an ifindex.
2578 */
2579 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2580 unit = unit * 10 + (*cp - '0');
2581 }
2582
2583 /*
2584 * If the number took all of the name, then it's a valid ifindex.
2585 */
2586 if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
2587 return if_get_byindex(unit, psref);
2588
2589 ifp = NULL;
2590 s = pserialize_read_enter();
2591 IFNET_READER_FOREACH(ifp) {
2592 if (if_is_deactivated(ifp))
2593 continue;
2594 if (strcmp(ifp->if_xname, name) == 0) {
2595 psref_acquire(psref, &ifp->if_psref,
2596 ifnet_psref_class);
2597 goto out;
2598 }
2599 }
2600 out:
2601 pserialize_read_exit(s);
2602 return ifp;
2603 }
2604
2605 /*
2606 * Release a reference of an ifnet object given by if_get or
2607 * if_get_byindex.
2608 */
2609 void
2610 if_put(const struct ifnet *ifp, struct psref *psref)
2611 {
2612
2613 if (ifp == NULL)
2614 return;
2615
2616 psref_release(psref, &ifp->if_psref, ifnet_psref_class);
2617 }
2618
2619 ifnet_t *
2620 if_byindex(u_int idx)
2621 {
2622 ifnet_t *ifp;
2623
2624 ifp = (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
2625 if (ifp != NULL && if_is_deactivated(ifp))
2626 ifp = NULL;
2627 return ifp;
2628 }
2629
2630 /*
2631 * Get a reference of an ifnet object by an interface index.
2632 * The returned reference is protected by psref(9). The caller
2633 * must release a returned reference by if_put after use.
2634 */
2635 ifnet_t *
2636 if_get_byindex(u_int idx, struct psref *psref)
2637 {
2638 ifnet_t *ifp;
2639 int s;
2640
2641 s = pserialize_read_enter();
2642 ifp = if_byindex(idx);
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 * Note that it's safe only if the passed ifp is guaranteed to not be freed,
2652 * for example using pserialize or the ifp is already held or some other
2653 * object is held which guarantes the ifp to not be freed indirectly.
2654 */
2655 void
2656 if_acquire(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 size_t pktlen = m->m_pkthdr.len;
3211 bool mcast = (m->m_flags & M_MCAST) != 0;
3212
3213 s = splnet();
3214
3215 IFQ_ENQUEUE(&ifp->if_snd, m, error);
3216 if (error != 0) {
3217 /* mbuf is already freed */
3218 goto out;
3219 }
3220
3221 ifp->if_obytes += pktlen;
3222 if (mcast)
3223 ifp->if_omcasts++;
3224
3225 if ((ifp->if_flags & IFF_OACTIVE) == 0)
3226 if_start_lock(ifp);
3227 out:
3228 splx(s);
3229
3230 return error;
3231 }
3232
3233 int
3234 if_transmit_lock(struct ifnet *ifp, struct mbuf *m)
3235 {
3236 int error;
3237
3238 #ifdef ALTQ
3239 KERNEL_LOCK(1, NULL);
3240 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
3241 error = if_transmit(ifp, m);
3242 KERNEL_UNLOCK_ONE(NULL);
3243 } else {
3244 KERNEL_UNLOCK_ONE(NULL);
3245 error = (*ifp->if_transmit)(ifp, m);
3246 /* mbuf is alredy freed */
3247 }
3248 #else /* !ALTQ */
3249 error = (*ifp->if_transmit)(ifp, m);
3250 /* mbuf is alredy freed */
3251 #endif /* !ALTQ */
3252
3253 return error;
3254 }
3255
3256 /*
3257 * Queue message on interface, and start output if interface
3258 * not yet active.
3259 */
3260 int
3261 ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
3262 {
3263
3264 return if_transmit_lock(ifp, m);
3265 }
3266
3267 /*
3268 * Queue message on interface, possibly using a second fast queue
3269 */
3270 int
3271 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
3272 {
3273 int error = 0;
3274
3275 if (ifq != NULL
3276 #ifdef ALTQ
3277 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
3278 #endif
3279 ) {
3280 if (IF_QFULL(ifq)) {
3281 IF_DROP(&ifp->if_snd);
3282 m_freem(m);
3283 if (error == 0)
3284 error = ENOBUFS;
3285 } else
3286 IF_ENQUEUE(ifq, m);
3287 } else
3288 IFQ_ENQUEUE(&ifp->if_snd, m, error);
3289 if (error != 0) {
3290 ++ifp->if_oerrors;
3291 return error;
3292 }
3293 return 0;
3294 }
3295
3296 int
3297 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
3298 {
3299 int rc;
3300
3301 if (ifp->if_initaddr != NULL)
3302 rc = (*ifp->if_initaddr)(ifp, ifa, src);
3303 else if (src ||
3304 /* FIXME: may not hold if_ioctl_lock */
3305 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
3306 rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
3307
3308 return rc;
3309 }
3310
3311 int
3312 if_do_dad(struct ifnet *ifp)
3313 {
3314 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
3315 return 0;
3316
3317 switch (ifp->if_type) {
3318 case IFT_FAITH:
3319 /*
3320 * These interfaces do not have the IFF_LOOPBACK flag,
3321 * but loop packets back. We do not have to do DAD on such
3322 * interfaces. We should even omit it, because loop-backed
3323 * responses would confuse the DAD procedure.
3324 */
3325 return 0;
3326 default:
3327 /*
3328 * Our DAD routine requires the interface up and running.
3329 * However, some interfaces can be up before the RUNNING
3330 * status. Additionaly, users may try to assign addresses
3331 * before the interface becomes up (or running).
3332 * We simply skip DAD in such a case as a work around.
3333 * XXX: we should rather mark "tentative" on such addresses,
3334 * and do DAD after the interface becomes ready.
3335 */
3336 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
3337 (IFF_UP|IFF_RUNNING))
3338 return 0;
3339
3340 return 1;
3341 }
3342 }
3343
3344 int
3345 if_flags_set(ifnet_t *ifp, const short flags)
3346 {
3347 int rc;
3348
3349 if (ifp->if_setflags != NULL)
3350 rc = (*ifp->if_setflags)(ifp, flags);
3351 else {
3352 short cantflags, chgdflags;
3353 struct ifreq ifr;
3354
3355 chgdflags = ifp->if_flags ^ flags;
3356 cantflags = chgdflags & IFF_CANTCHANGE;
3357
3358 if (cantflags != 0)
3359 ifp->if_flags ^= cantflags;
3360
3361 /* Traditionally, we do not call if_ioctl after
3362 * setting/clearing only IFF_PROMISC if the interface
3363 * isn't IFF_UP. Uphold that tradition.
3364 */
3365 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
3366 return 0;
3367
3368 memset(&ifr, 0, sizeof(ifr));
3369
3370 ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
3371 /* FIXME: may not hold if_ioctl_lock */
3372 rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
3373
3374 if (rc != 0 && cantflags != 0)
3375 ifp->if_flags ^= cantflags;
3376 }
3377
3378 return rc;
3379 }
3380
3381 int
3382 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
3383 {
3384 int rc;
3385 struct ifreq ifr;
3386
3387 if (ifp->if_mcastop != NULL)
3388 rc = (*ifp->if_mcastop)(ifp, cmd, sa);
3389 else {
3390 ifreq_setaddr(cmd, &ifr, sa);
3391 rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
3392 }
3393
3394 return rc;
3395 }
3396
3397 static void
3398 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
3399 struct ifaltq *ifq)
3400 {
3401 const struct sysctlnode *cnode, *rnode;
3402
3403 if (sysctl_createv(clog, 0, NULL, &rnode,
3404 CTLFLAG_PERMANENT,
3405 CTLTYPE_NODE, "interfaces",
3406 SYSCTL_DESCR("Per-interface controls"),
3407 NULL, 0, NULL, 0,
3408 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
3409 goto bad;
3410
3411 if (sysctl_createv(clog, 0, &rnode, &rnode,
3412 CTLFLAG_PERMANENT,
3413 CTLTYPE_NODE, ifname,
3414 SYSCTL_DESCR("Interface controls"),
3415 NULL, 0, NULL, 0,
3416 CTL_CREATE, CTL_EOL) != 0)
3417 goto bad;
3418
3419 if (sysctl_createv(clog, 0, &rnode, &rnode,
3420 CTLFLAG_PERMANENT,
3421 CTLTYPE_NODE, "sndq",
3422 SYSCTL_DESCR("Interface output queue controls"),
3423 NULL, 0, NULL, 0,
3424 CTL_CREATE, CTL_EOL) != 0)
3425 goto bad;
3426
3427 if (sysctl_createv(clog, 0, &rnode, &cnode,
3428 CTLFLAG_PERMANENT,
3429 CTLTYPE_INT, "len",
3430 SYSCTL_DESCR("Current output queue length"),
3431 NULL, 0, &ifq->ifq_len, 0,
3432 CTL_CREATE, CTL_EOL) != 0)
3433 goto bad;
3434
3435 if (sysctl_createv(clog, 0, &rnode, &cnode,
3436 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3437 CTLTYPE_INT, "maxlen",
3438 SYSCTL_DESCR("Maximum allowed output queue length"),
3439 NULL, 0, &ifq->ifq_maxlen, 0,
3440 CTL_CREATE, CTL_EOL) != 0)
3441 goto bad;
3442
3443 if (sysctl_createv(clog, 0, &rnode, &cnode,
3444 CTLFLAG_PERMANENT,
3445 CTLTYPE_INT, "drops",
3446 SYSCTL_DESCR("Packets dropped due to full output queue"),
3447 NULL, 0, &ifq->ifq_drops, 0,
3448 CTL_CREATE, CTL_EOL) != 0)
3449 goto bad;
3450
3451 return;
3452 bad:
3453 printf("%s: could not attach sysctl nodes\n", ifname);
3454 return;
3455 }
3456
3457 #if defined(INET) || defined(INET6)
3458
3459 #define SYSCTL_NET_PKTQ(q, cn, c) \
3460 static int \
3461 sysctl_net_##q##_##cn(SYSCTLFN_ARGS) \
3462 { \
3463 return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c); \
3464 }
3465
3466 #if defined(INET)
3467 static int
3468 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
3469 {
3470 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
3471 }
3472 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
3473 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
3474 #endif
3475
3476 #if defined(INET6)
3477 static int
3478 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
3479 {
3480 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
3481 }
3482 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
3483 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
3484 #endif
3485
3486 static void
3487 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
3488 {
3489 sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
3490 const char *pfname = NULL, *ipname = NULL;
3491 int ipn = 0, qid = 0;
3492
3493 switch (pf) {
3494 #if defined(INET)
3495 case PF_INET:
3496 len_func = sysctl_net_ip_pktq_items;
3497 maxlen_func = sysctl_net_ip_pktq_maxlen;
3498 drops_func = sysctl_net_ip_pktq_drops;
3499 pfname = "inet", ipn = IPPROTO_IP;
3500 ipname = "ip", qid = IPCTL_IFQ;
3501 break;
3502 #endif
3503 #if defined(INET6)
3504 case PF_INET6:
3505 len_func = sysctl_net_ip6_pktq_items;
3506 maxlen_func = sysctl_net_ip6_pktq_maxlen;
3507 drops_func = sysctl_net_ip6_pktq_drops;
3508 pfname = "inet6", ipn = IPPROTO_IPV6;
3509 ipname = "ip6", qid = IPV6CTL_IFQ;
3510 break;
3511 #endif
3512 default:
3513 KASSERT(false);
3514 }
3515
3516 sysctl_createv(clog, 0, NULL, NULL,
3517 CTLFLAG_PERMANENT,
3518 CTLTYPE_NODE, pfname, NULL,
3519 NULL, 0, NULL, 0,
3520 CTL_NET, pf, CTL_EOL);
3521 sysctl_createv(clog, 0, NULL, NULL,
3522 CTLFLAG_PERMANENT,
3523 CTLTYPE_NODE, ipname, NULL,
3524 NULL, 0, NULL, 0,
3525 CTL_NET, pf, ipn, CTL_EOL);
3526 sysctl_createv(clog, 0, NULL, NULL,
3527 CTLFLAG_PERMANENT,
3528 CTLTYPE_NODE, "ifq",
3529 SYSCTL_DESCR("Protocol input queue controls"),
3530 NULL, 0, NULL, 0,
3531 CTL_NET, pf, ipn, qid, CTL_EOL);
3532
3533 sysctl_createv(clog, 0, NULL, NULL,
3534 CTLFLAG_PERMANENT,
3535 CTLTYPE_INT, "len",
3536 SYSCTL_DESCR("Current input queue length"),
3537 len_func, 0, NULL, 0,
3538 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
3539 sysctl_createv(clog, 0, NULL, NULL,
3540 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3541 CTLTYPE_INT, "maxlen",
3542 SYSCTL_DESCR("Maximum allowed input queue length"),
3543 maxlen_func, 0, NULL, 0,
3544 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
3545 sysctl_createv(clog, 0, NULL, NULL,
3546 CTLFLAG_PERMANENT,
3547 CTLTYPE_INT, "drops",
3548 SYSCTL_DESCR("Packets dropped due to full input queue"),
3549 drops_func, 0, NULL, 0,
3550 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
3551 }
3552 #endif /* INET || INET6 */
3553
3554 static int
3555 if_sdl_sysctl(SYSCTLFN_ARGS)
3556 {
3557 struct ifnet *ifp;
3558 const struct sockaddr_dl *sdl;
3559 struct psref psref;
3560 int error = 0;
3561 int bound;
3562
3563 if (namelen != 1)
3564 return EINVAL;
3565
3566 bound = curlwp_bind();
3567 ifp = if_get_byindex(name[0], &psref);
3568 if (ifp == NULL) {
3569 error = ENODEV;
3570 goto out0;
3571 }
3572
3573 sdl = ifp->if_sadl;
3574 if (sdl == NULL) {
3575 *oldlenp = 0;
3576 goto out1;
3577 }
3578
3579 if (oldp == NULL) {
3580 *oldlenp = sdl->sdl_alen;
3581 goto out1;
3582 }
3583
3584 if (*oldlenp >= sdl->sdl_alen)
3585 *oldlenp = sdl->sdl_alen;
3586 error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
3587 out1:
3588 if_put(ifp, &psref);
3589 out0:
3590 curlwp_bindx(bound);
3591 return error;
3592 }
3593
3594 static void
3595 if_sysctl_setup(struct sysctllog **clog)
3596 {
3597 const struct sysctlnode *rnode = NULL;
3598
3599 sysctl_createv(clog, 0, NULL, &rnode,
3600 CTLFLAG_PERMANENT,
3601 CTLTYPE_NODE, "sdl",
3602 SYSCTL_DESCR("Get active link-layer address"),
3603 if_sdl_sysctl, 0, NULL, 0,
3604 CTL_NET, CTL_CREATE, CTL_EOL);
3605
3606 #if defined(INET)
3607 sysctl_net_pktq_setup(NULL, PF_INET);
3608 #endif
3609 #ifdef INET6
3610 if (in6_present)
3611 sysctl_net_pktq_setup(NULL, PF_INET6);
3612 #endif
3613 }
3614