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