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