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