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