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