if.c revision 1.434 1 /* $NetBSD: if.c,v 1.434 2018/08/27 04:53:24 ozaki-r Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William Studenmund and Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 /*
62 * Copyright (c) 1980, 1986, 1993
63 * The Regents of the University of California. All rights reserved.
64 *
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
67 * are met:
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. Neither the name of the University nor the names of its contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 *
89 * @(#)if.c 8.5 (Berkeley) 1/9/95
90 */
91
92 #include <sys/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.434 2018/08/27 04:53:24 ozaki-r 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 ifp->if_transmit == if_transmit ||
1046 ALTQ_IS_ENABLED(&ifp->if_snd);
1047 }
1048
1049 /*
1050 * Schedule deferred if_start.
1051 */
1052 void
1053 if_schedule_deferred_start(struct ifnet *ifp)
1054 {
1055
1056 KASSERT(ifp->if_deferred_start != NULL);
1057
1058 if (if_snd_is_used(ifp) && IFQ_IS_EMPTY(&ifp->if_snd))
1059 return;
1060
1061 softint_schedule(ifp->if_deferred_start->ids_si);
1062 }
1063
1064 /*
1065 * Create an instance of deferred if_start. A driver should call the function
1066 * only if the driver needs deferred if_start. Drivers can setup their own
1067 * deferred if_start function via 2nd argument.
1068 */
1069 void
1070 if_deferred_start_init(struct ifnet *ifp, void (*func)(struct ifnet *))
1071 {
1072 struct if_deferred_start *ids;
1073 u_int flags = SOFTINT_NET;
1074
1075 flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0;
1076
1077 ids = kmem_zalloc(sizeof(*ids), KM_SLEEP);
1078 ids->ids_ifp = ifp;
1079 ids->ids_si = softint_establish(flags, if_deferred_start_softint, ids);
1080 if (func != NULL)
1081 ids->ids_if_start = func;
1082 else
1083 ids->ids_if_start = if_deferred_start_common;
1084
1085 ifp->if_deferred_start = ids;
1086 }
1087
1088 static void
1089 if_deferred_start_destroy(struct ifnet *ifp)
1090 {
1091
1092 if (ifp->if_deferred_start == NULL)
1093 return;
1094
1095 softint_disestablish(ifp->if_deferred_start->ids_si);
1096 kmem_free(ifp->if_deferred_start, sizeof(*ifp->if_deferred_start));
1097 ifp->if_deferred_start = NULL;
1098 }
1099
1100 /*
1101 * The common interface input routine that is called by device drivers,
1102 * which should be used only when the driver's rx handler already runs
1103 * in softint.
1104 */
1105 void
1106 if_input(struct ifnet *ifp, struct mbuf *m)
1107 {
1108
1109 KASSERT(ifp->if_percpuq == NULL);
1110 KASSERT(!cpu_intr_p());
1111
1112 ifp->if_ipackets++;
1113 bpf_mtap(ifp, m, BPF_D_IN);
1114
1115 ifp->_if_input(ifp, m);
1116 }
1117
1118 /*
1119 * DEPRECATED. Use if_initialize and if_register instead.
1120 * See the above comment of if_initialize.
1121 *
1122 * Note that it implicitly enables if_percpuq to make drivers easy to
1123 * migrate softint-based if_input without much changes. If you don't
1124 * want to enable it, use if_initialize instead.
1125 */
1126 int
1127 if_attach(ifnet_t *ifp)
1128 {
1129 int rv;
1130
1131 rv = if_initialize(ifp);
1132 if (rv != 0)
1133 return rv;
1134
1135 ifp->if_percpuq = if_percpuq_create(ifp);
1136 if_register(ifp);
1137
1138 return 0;
1139 }
1140
1141 void
1142 if_attachdomain(void)
1143 {
1144 struct ifnet *ifp;
1145 int s;
1146 int bound = curlwp_bind();
1147
1148 s = pserialize_read_enter();
1149 IFNET_READER_FOREACH(ifp) {
1150 struct psref psref;
1151 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
1152 pserialize_read_exit(s);
1153 if_attachdomain1(ifp);
1154 s = pserialize_read_enter();
1155 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
1156 }
1157 pserialize_read_exit(s);
1158 curlwp_bindx(bound);
1159 }
1160
1161 static void
1162 if_attachdomain1(struct ifnet *ifp)
1163 {
1164 struct domain *dp;
1165 int s;
1166
1167 s = splsoftnet();
1168
1169 /* address family dependent data region */
1170 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata));
1171 DOMAIN_FOREACH(dp) {
1172 if (dp->dom_ifattach != NULL)
1173 ifp->if_afdata[dp->dom_family] =
1174 (*dp->dom_ifattach)(ifp);
1175 }
1176
1177 splx(s);
1178 }
1179
1180 /*
1181 * Deactivate an interface. This points all of the procedure
1182 * handles at error stubs. May be called from interrupt context.
1183 */
1184 void
1185 if_deactivate(struct ifnet *ifp)
1186 {
1187 int s;
1188
1189 s = splsoftnet();
1190
1191 ifp->if_output = if_nulloutput;
1192 ifp->_if_input = if_nullinput;
1193 ifp->if_start = if_nullstart;
1194 ifp->if_transmit = if_nulltransmit;
1195 ifp->if_ioctl = if_nullioctl;
1196 ifp->if_init = if_nullinit;
1197 ifp->if_stop = if_nullstop;
1198 ifp->if_slowtimo = if_nullslowtimo;
1199 ifp->if_drain = if_nulldrain;
1200
1201 /* No more packets may be enqueued. */
1202 ifp->if_snd.ifq_maxlen = 0;
1203
1204 splx(s);
1205 }
1206
1207 bool
1208 if_is_deactivated(const struct ifnet *ifp)
1209 {
1210
1211 return ifp->if_output == if_nulloutput;
1212 }
1213
1214 void
1215 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
1216 {
1217 struct ifaddr *ifa, *nifa;
1218 int s;
1219
1220 s = pserialize_read_enter();
1221 for (ifa = IFADDR_READER_FIRST(ifp); ifa; ifa = nifa) {
1222 nifa = IFADDR_READER_NEXT(ifa);
1223 if (ifa->ifa_addr->sa_family != family)
1224 continue;
1225 pserialize_read_exit(s);
1226
1227 (*purgeaddr)(ifa);
1228
1229 s = pserialize_read_enter();
1230 }
1231 pserialize_read_exit(s);
1232 }
1233
1234 #ifdef IFAREF_DEBUG
1235 static struct ifaddr **ifa_list;
1236 static int ifa_list_size;
1237
1238 /* Depends on only one if_attach runs at once */
1239 static void
1240 if_build_ifa_list(struct ifnet *ifp)
1241 {
1242 struct ifaddr *ifa;
1243 int i;
1244
1245 KASSERT(ifa_list == NULL);
1246 KASSERT(ifa_list_size == 0);
1247
1248 IFADDR_READER_FOREACH(ifa, ifp)
1249 ifa_list_size++;
1250
1251 ifa_list = kmem_alloc(sizeof(*ifa) * ifa_list_size, KM_SLEEP);
1252 i = 0;
1253 IFADDR_READER_FOREACH(ifa, ifp) {
1254 ifa_list[i++] = ifa;
1255 ifaref(ifa);
1256 }
1257 }
1258
1259 static void
1260 if_check_and_free_ifa_list(struct ifnet *ifp)
1261 {
1262 int i;
1263 struct ifaddr *ifa;
1264
1265 if (ifa_list == NULL)
1266 return;
1267
1268 for (i = 0; i < ifa_list_size; i++) {
1269 char buf[64];
1270
1271 ifa = ifa_list[i];
1272 sockaddr_format(ifa->ifa_addr, buf, sizeof(buf));
1273 if (ifa->ifa_refcnt > 1) {
1274 log(LOG_WARNING,
1275 "ifa(%s) still referenced (refcnt=%d)\n",
1276 buf, ifa->ifa_refcnt - 1);
1277 } else
1278 log(LOG_DEBUG,
1279 "ifa(%s) not referenced (refcnt=%d)\n",
1280 buf, ifa->ifa_refcnt - 1);
1281 ifafree(ifa);
1282 }
1283
1284 kmem_free(ifa_list, sizeof(*ifa) * ifa_list_size);
1285 ifa_list = NULL;
1286 ifa_list_size = 0;
1287 }
1288 #endif
1289
1290 /*
1291 * Detach an interface from the list of "active" interfaces,
1292 * freeing any resources as we go along.
1293 *
1294 * NOTE: This routine must be called with a valid thread context,
1295 * as it may block.
1296 */
1297 void
1298 if_detach(struct ifnet *ifp)
1299 {
1300 struct socket so;
1301 struct ifaddr *ifa;
1302 #ifdef IFAREF_DEBUG
1303 struct ifaddr *last_ifa = NULL;
1304 #endif
1305 struct domain *dp;
1306 const struct protosw *pr;
1307 int s, i, family, purged;
1308 uint64_t xc;
1309
1310 #ifdef IFAREF_DEBUG
1311 if_build_ifa_list(ifp);
1312 #endif
1313 /*
1314 * XXX It's kind of lame that we have to have the
1315 * XXX socket structure...
1316 */
1317 memset(&so, 0, sizeof(so));
1318
1319 s = splnet();
1320
1321 sysctl_teardown(&ifp->if_sysctl_log);
1322 IFNET_LOCK(ifp);
1323 if_deactivate(ifp);
1324 IFNET_UNLOCK(ifp);
1325
1326 if (ifp->if_slowtimo != NULL && ifp->if_slowtimo_ch != NULL) {
1327 ifp->if_slowtimo = NULL;
1328 callout_halt(ifp->if_slowtimo_ch, NULL);
1329 callout_destroy(ifp->if_slowtimo_ch);
1330 kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch));
1331 }
1332 if_deferred_start_destroy(ifp);
1333
1334 /*
1335 * Do an if_down() to give protocols a chance to do something.
1336 */
1337 if_down_deactivated(ifp);
1338
1339 #ifdef ALTQ
1340 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1341 altq_disable(&ifp->if_snd);
1342 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1343 altq_detach(&ifp->if_snd);
1344 #endif
1345
1346 #if NCARP > 0
1347 /* Remove the interface from any carp group it is a part of. */
1348 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
1349 carp_ifdetach(ifp);
1350 #endif
1351
1352 /*
1353 * Rip all the addresses off the interface. This should make
1354 * all of the routes go away.
1355 *
1356 * pr_usrreq calls can remove an arbitrary number of ifaddrs
1357 * from the list, including our "cursor", ifa. For safety,
1358 * and to honor the TAILQ abstraction, I just restart the
1359 * loop after each removal. Note that the loop will exit
1360 * when all of the remaining ifaddrs belong to the AF_LINK
1361 * family. I am counting on the historical fact that at
1362 * least one pr_usrreq in each address domain removes at
1363 * least one ifaddr.
1364 */
1365 again:
1366 /*
1367 * At this point, no other one tries to remove ifa in the list,
1368 * so we don't need to take a lock or psref. Avoid using
1369 * IFADDR_READER_FOREACH to pass over an inspection of contract
1370 * violations of pserialize.
1371 */
1372 IFADDR_WRITER_FOREACH(ifa, ifp) {
1373 family = ifa->ifa_addr->sa_family;
1374 #ifdef IFAREF_DEBUG
1375 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
1376 ifa, family, ifa->ifa_refcnt);
1377 if (last_ifa != NULL && ifa == last_ifa)
1378 panic("if_detach: loop detected");
1379 last_ifa = ifa;
1380 #endif
1381 if (family == AF_LINK)
1382 continue;
1383 dp = pffinddomain(family);
1384 KASSERTMSG(dp != NULL, "no domain for AF %d", family);
1385 /*
1386 * XXX These PURGEIF calls are redundant with the
1387 * purge-all-families calls below, but are left in for
1388 * now both to make a smaller change, and to avoid
1389 * unplanned interactions with clearing of
1390 * ifp->if_addrlist.
1391 */
1392 purged = 0;
1393 for (pr = dp->dom_protosw;
1394 pr < dp->dom_protoswNPROTOSW; pr++) {
1395 so.so_proto = pr;
1396 if (pr->pr_usrreqs) {
1397 (void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1398 purged = 1;
1399 }
1400 }
1401 if (purged == 0) {
1402 /*
1403 * XXX What's really the best thing to do
1404 * XXX here? --thorpej (at) NetBSD.org
1405 */
1406 printf("if_detach: WARNING: AF %d not purged\n",
1407 family);
1408 ifa_remove(ifp, ifa);
1409 }
1410 goto again;
1411 }
1412
1413 if_free_sadl(ifp, 1);
1414
1415 restart:
1416 IFADDR_WRITER_FOREACH(ifa, ifp) {
1417 family = ifa->ifa_addr->sa_family;
1418 KASSERT(family == AF_LINK);
1419 ifa_remove(ifp, ifa);
1420 goto restart;
1421 }
1422
1423 /* Delete stray routes from the routing table. */
1424 for (i = 0; i <= AF_MAX; i++)
1425 rt_delete_matched_entries(i, if_delroute_matcher, ifp);
1426
1427 DOMAIN_FOREACH(dp) {
1428 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
1429 {
1430 void *p = ifp->if_afdata[dp->dom_family];
1431 if (p) {
1432 ifp->if_afdata[dp->dom_family] = NULL;
1433 (*dp->dom_ifdetach)(ifp, p);
1434 }
1435 }
1436
1437 /*
1438 * One would expect multicast memberships (INET and
1439 * INET6) on UDP sockets to be purged by the PURGEIF
1440 * calls above, but if all addresses were removed from
1441 * the interface prior to destruction, the calls will
1442 * not be made (e.g. ppp, for which pppd(8) generally
1443 * removes addresses before destroying the interface).
1444 * Because there is no invariant that multicast
1445 * memberships only exist for interfaces with IPv4
1446 * addresses, we must call PURGEIF regardless of
1447 * addresses. (Protocols which might store ifnet
1448 * pointers are marked with PR_PURGEIF.)
1449 */
1450 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
1451 so.so_proto = pr;
1452 if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
1453 (void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1454 }
1455 }
1456
1457 /* Wait for all readers to drain before freeing. */
1458 IFNET_GLOBAL_LOCK();
1459 ifindex2ifnet[ifp->if_index] = NULL;
1460 TAILQ_REMOVE(&ifnet_list, ifp, if_list);
1461 IFNET_WRITER_REMOVE(ifp);
1462 pserialize_perform(ifnet_psz);
1463 IFNET_GLOBAL_UNLOCK();
1464
1465 psref_target_destroy(&ifp->if_psref, ifnet_psref_class);
1466 PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry);
1467
1468 pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp);
1469 (void)pfil_head_destroy(ifp->if_pfil);
1470
1471 /* Announce that the interface is gone. */
1472 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1473
1474 IF_AFDATA_LOCK_DESTROY(ifp);
1475
1476 if (if_is_link_state_changeable(ifp)) {
1477 softint_disestablish(ifp->if_link_si);
1478 ifp->if_link_si = NULL;
1479 }
1480
1481 /*
1482 * remove packets that came from ifp, from software interrupt queues.
1483 */
1484 DOMAIN_FOREACH(dp) {
1485 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
1486 struct ifqueue *iq = dp->dom_ifqueues[i];
1487 if (iq == NULL)
1488 break;
1489 dp->dom_ifqueues[i] = NULL;
1490 if_detach_queues(ifp, iq);
1491 }
1492 }
1493
1494 /*
1495 * IP queues have to be processed separately: net-queue barrier
1496 * ensures that the packets are dequeued while a cross-call will
1497 * ensure that the interrupts have completed. FIXME: not quite..
1498 */
1499 #ifdef INET
1500 pktq_barrier(ip_pktq);
1501 #endif
1502 #ifdef INET6
1503 if (in6_present)
1504 pktq_barrier(ip6_pktq);
1505 #endif
1506 xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
1507 xc_wait(xc);
1508
1509 if (ifp->if_percpuq != NULL) {
1510 if_percpuq_destroy(ifp->if_percpuq);
1511 ifp->if_percpuq = NULL;
1512 }
1513
1514 mutex_obj_free(ifp->if_ioctl_lock);
1515 ifp->if_ioctl_lock = NULL;
1516 mutex_obj_free(ifp->if_snd.ifq_lock);
1517
1518 splx(s);
1519
1520 #ifdef IFAREF_DEBUG
1521 if_check_and_free_ifa_list(ifp);
1522 #endif
1523 }
1524
1525 static void
1526 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
1527 {
1528 struct mbuf *m, *prev, *next;
1529
1530 prev = NULL;
1531 for (m = q->ifq_head; m != NULL; m = next) {
1532 KASSERT((m->m_flags & M_PKTHDR) != 0);
1533
1534 next = m->m_nextpkt;
1535 if (m->m_pkthdr.rcvif_index != ifp->if_index) {
1536 prev = m;
1537 continue;
1538 }
1539
1540 if (prev != NULL)
1541 prev->m_nextpkt = m->m_nextpkt;
1542 else
1543 q->ifq_head = m->m_nextpkt;
1544 if (q->ifq_tail == m)
1545 q->ifq_tail = prev;
1546 q->ifq_len--;
1547
1548 m->m_nextpkt = NULL;
1549 m_freem(m);
1550 IF_DROP(q);
1551 }
1552 }
1553
1554 /*
1555 * Callback for a radix tree walk to delete all references to an
1556 * ifnet.
1557 */
1558 static int
1559 if_delroute_matcher(struct rtentry *rt, void *v)
1560 {
1561 struct ifnet *ifp = (struct ifnet *)v;
1562
1563 if (rt->rt_ifp == ifp)
1564 return 1;
1565 else
1566 return 0;
1567 }
1568
1569 /*
1570 * Create a clone network interface.
1571 */
1572 static int
1573 if_clone_create(const char *name)
1574 {
1575 struct if_clone *ifc;
1576 int unit;
1577 struct ifnet *ifp;
1578 struct psref psref;
1579
1580 KASSERT(mutex_owned(&if_clone_mtx));
1581
1582 ifc = if_clone_lookup(name, &unit);
1583 if (ifc == NULL)
1584 return EINVAL;
1585
1586 ifp = if_get(name, &psref);
1587 if (ifp != NULL) {
1588 if_put(ifp, &psref);
1589 return EEXIST;
1590 }
1591
1592 return (*ifc->ifc_create)(ifc, unit);
1593 }
1594
1595 /*
1596 * Destroy a clone network interface.
1597 */
1598 static int
1599 if_clone_destroy(const char *name)
1600 {
1601 struct if_clone *ifc;
1602 struct ifnet *ifp;
1603 struct psref psref;
1604
1605 KASSERT(mutex_owned(&if_clone_mtx));
1606
1607 ifc = if_clone_lookup(name, NULL);
1608 if (ifc == NULL)
1609 return EINVAL;
1610
1611 if (ifc->ifc_destroy == NULL)
1612 return EOPNOTSUPP;
1613
1614 ifp = if_get(name, &psref);
1615 if (ifp == NULL)
1616 return ENXIO;
1617
1618 /* We have to disable ioctls here */
1619 IFNET_LOCK(ifp);
1620 ifp->if_ioctl = if_nullioctl;
1621 IFNET_UNLOCK(ifp);
1622
1623 /*
1624 * We cannot call ifc_destroy with holding ifp.
1625 * Releasing ifp here is safe thanks to if_clone_mtx.
1626 */
1627 if_put(ifp, &psref);
1628
1629 return (*ifc->ifc_destroy)(ifp);
1630 }
1631
1632 static bool
1633 if_is_unit(const char *name)
1634 {
1635
1636 while(*name != '\0') {
1637 if (*name < '0' || *name > '9')
1638 return false;
1639 name++;
1640 }
1641
1642 return true;
1643 }
1644
1645 /*
1646 * Look up a network interface cloner.
1647 */
1648 static struct if_clone *
1649 if_clone_lookup(const char *name, int *unitp)
1650 {
1651 struct if_clone *ifc;
1652 const char *cp;
1653 char *dp, ifname[IFNAMSIZ + 3];
1654 int unit;
1655
1656 KASSERT(mutex_owned(&if_clone_mtx));
1657
1658 strcpy(ifname, "if_");
1659 /* separate interface name from unit */
1660 /* TODO: search unit number from backward */
1661 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
1662 *cp && !if_is_unit(cp);)
1663 *dp++ = *cp++;
1664
1665 if (cp == name || cp - name == IFNAMSIZ || !*cp)
1666 return NULL; /* No name or unit number */
1667 *dp++ = '\0';
1668
1669 again:
1670 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1671 if (strcmp(ifname + 3, ifc->ifc_name) == 0)
1672 break;
1673 }
1674
1675 if (ifc == NULL) {
1676 int error;
1677 if (*ifname == '\0')
1678 return NULL;
1679 mutex_exit(&if_clone_mtx);
1680 error = module_autoload(ifname, MODULE_CLASS_DRIVER);
1681 mutex_enter(&if_clone_mtx);
1682 if (error)
1683 return NULL;
1684 *ifname = '\0';
1685 goto again;
1686 }
1687
1688 unit = 0;
1689 while (cp - name < IFNAMSIZ && *cp) {
1690 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1691 /* Bogus unit number. */
1692 return NULL;
1693 }
1694 unit = (unit * 10) + (*cp++ - '0');
1695 }
1696
1697 if (unitp != NULL)
1698 *unitp = unit;
1699 return ifc;
1700 }
1701
1702 /*
1703 * Register a network interface cloner.
1704 */
1705 void
1706 if_clone_attach(struct if_clone *ifc)
1707 {
1708
1709 mutex_enter(&if_clone_mtx);
1710 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1711 if_cloners_count++;
1712 mutex_exit(&if_clone_mtx);
1713 }
1714
1715 /*
1716 * Unregister a network interface cloner.
1717 */
1718 void
1719 if_clone_detach(struct if_clone *ifc)
1720 {
1721
1722 mutex_enter(&if_clone_mtx);
1723 LIST_REMOVE(ifc, ifc_list);
1724 if_cloners_count--;
1725 mutex_exit(&if_clone_mtx);
1726 }
1727
1728 /*
1729 * Provide list of interface cloners to userspace.
1730 */
1731 int
1732 if_clone_list(int buf_count, char *buffer, int *total)
1733 {
1734 char outbuf[IFNAMSIZ], *dst;
1735 struct if_clone *ifc;
1736 int count, error = 0;
1737
1738 mutex_enter(&if_clone_mtx);
1739 *total = if_cloners_count;
1740 if ((dst = buffer) == NULL) {
1741 /* Just asking how many there are. */
1742 goto out;
1743 }
1744
1745 if (buf_count < 0) {
1746 error = EINVAL;
1747 goto out;
1748 }
1749
1750 count = (if_cloners_count < buf_count) ?
1751 if_cloners_count : buf_count;
1752
1753 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
1754 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1755 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1756 if (outbuf[sizeof(outbuf) - 1] != '\0') {
1757 error = ENAMETOOLONG;
1758 goto out;
1759 }
1760 error = copyout(outbuf, dst, sizeof(outbuf));
1761 if (error != 0)
1762 break;
1763 }
1764
1765 out:
1766 mutex_exit(&if_clone_mtx);
1767 return error;
1768 }
1769
1770 void
1771 ifa_psref_init(struct ifaddr *ifa)
1772 {
1773
1774 psref_target_init(&ifa->ifa_psref, ifa_psref_class);
1775 }
1776
1777 void
1778 ifaref(struct ifaddr *ifa)
1779 {
1780 KASSERT(!ISSET(ifa->ifa_flags, IFA_DESTROYING));
1781 ifa->ifa_refcnt++;
1782 }
1783
1784 void
1785 ifafree(struct ifaddr *ifa)
1786 {
1787 KASSERT(ifa != NULL);
1788 KASSERT(ifa->ifa_refcnt > 0);
1789
1790 if (--ifa->ifa_refcnt == 0) {
1791 free(ifa, M_IFADDR);
1792 }
1793 }
1794
1795 bool
1796 ifa_is_destroying(struct ifaddr *ifa)
1797 {
1798
1799 return ISSET(ifa->ifa_flags, IFA_DESTROYING);
1800 }
1801
1802 void
1803 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
1804 {
1805
1806 ifa->ifa_ifp = ifp;
1807
1808 /*
1809 * Check MP-safety for IFEF_MPSAFE drivers.
1810 * Check !IFF_RUNNING for initialization routines that normally don't
1811 * take IFNET_LOCK but it's safe because there is no competitor.
1812 * XXX there are false positive cases because IFF_RUNNING can be off on
1813 * if_stop.
1814 */
1815 KASSERT(!if_is_mpsafe(ifp) || !ISSET(ifp->if_flags, IFF_RUNNING) ||
1816 IFNET_LOCKED(ifp));
1817
1818 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1819 IFADDR_ENTRY_INIT(ifa);
1820 IFADDR_WRITER_INSERT_TAIL(ifp, ifa);
1821
1822 ifaref(ifa);
1823 }
1824
1825 void
1826 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
1827 {
1828
1829 KASSERT(ifa->ifa_ifp == ifp);
1830 /*
1831 * Check MP-safety for IFEF_MPSAFE drivers.
1832 * if_is_deactivated indicates ifa_remove is called form if_detach
1833 * where is safe even if IFNET_LOCK isn't held.
1834 */
1835 KASSERT(!if_is_mpsafe(ifp) || if_is_deactivated(ifp) || IFNET_LOCKED(ifp));
1836
1837 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1838 IFADDR_WRITER_REMOVE(ifa);
1839 #ifdef NET_MPSAFE
1840 IFNET_GLOBAL_LOCK();
1841 pserialize_perform(ifnet_psz);
1842 IFNET_GLOBAL_UNLOCK();
1843 #endif
1844
1845 #ifdef NET_MPSAFE
1846 psref_target_destroy(&ifa->ifa_psref, ifa_psref_class);
1847 #endif
1848 IFADDR_ENTRY_DESTROY(ifa);
1849 ifafree(ifa);
1850 }
1851
1852 void
1853 ifa_acquire(struct ifaddr *ifa, struct psref *psref)
1854 {
1855
1856 psref_acquire(psref, &ifa->ifa_psref, ifa_psref_class);
1857 }
1858
1859 void
1860 ifa_release(struct ifaddr *ifa, struct psref *psref)
1861 {
1862
1863 if (ifa == NULL)
1864 return;
1865
1866 psref_release(psref, &ifa->ifa_psref, ifa_psref_class);
1867 }
1868
1869 bool
1870 ifa_held(struct ifaddr *ifa)
1871 {
1872
1873 return psref_held(&ifa->ifa_psref, ifa_psref_class);
1874 }
1875
1876 static inline int
1877 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
1878 {
1879 return sockaddr_cmp(sa1, sa2) == 0;
1880 }
1881
1882 /*
1883 * Locate an interface based on a complete address.
1884 */
1885 /*ARGSUSED*/
1886 struct ifaddr *
1887 ifa_ifwithaddr(const struct sockaddr *addr)
1888 {
1889 struct ifnet *ifp;
1890 struct ifaddr *ifa;
1891
1892 IFNET_READER_FOREACH(ifp) {
1893 if (if_is_deactivated(ifp))
1894 continue;
1895 IFADDR_READER_FOREACH(ifa, ifp) {
1896 if (ifa->ifa_addr->sa_family != addr->sa_family)
1897 continue;
1898 if (equal(addr, ifa->ifa_addr))
1899 return ifa;
1900 if ((ifp->if_flags & IFF_BROADCAST) &&
1901 ifa->ifa_broadaddr &&
1902 /* IP6 doesn't have broadcast */
1903 ifa->ifa_broadaddr->sa_len != 0 &&
1904 equal(ifa->ifa_broadaddr, addr))
1905 return ifa;
1906 }
1907 }
1908 return NULL;
1909 }
1910
1911 struct ifaddr *
1912 ifa_ifwithaddr_psref(const struct sockaddr *addr, struct psref *psref)
1913 {
1914 struct ifaddr *ifa;
1915 int s = pserialize_read_enter();
1916
1917 ifa = ifa_ifwithaddr(addr);
1918 if (ifa != NULL)
1919 ifa_acquire(ifa, psref);
1920 pserialize_read_exit(s);
1921
1922 return ifa;
1923 }
1924
1925 /*
1926 * Locate the point to point interface with a given destination address.
1927 */
1928 /*ARGSUSED*/
1929 struct ifaddr *
1930 ifa_ifwithdstaddr(const struct sockaddr *addr)
1931 {
1932 struct ifnet *ifp;
1933 struct ifaddr *ifa;
1934
1935 IFNET_READER_FOREACH(ifp) {
1936 if (if_is_deactivated(ifp))
1937 continue;
1938 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1939 continue;
1940 IFADDR_READER_FOREACH(ifa, ifp) {
1941 if (ifa->ifa_addr->sa_family != addr->sa_family ||
1942 ifa->ifa_dstaddr == NULL)
1943 continue;
1944 if (equal(addr, ifa->ifa_dstaddr))
1945 return ifa;
1946 }
1947 }
1948
1949 return NULL;
1950 }
1951
1952 struct ifaddr *
1953 ifa_ifwithdstaddr_psref(const struct sockaddr *addr, struct psref *psref)
1954 {
1955 struct ifaddr *ifa;
1956 int s;
1957
1958 s = pserialize_read_enter();
1959 ifa = ifa_ifwithdstaddr(addr);
1960 if (ifa != NULL)
1961 ifa_acquire(ifa, psref);
1962 pserialize_read_exit(s);
1963
1964 return ifa;
1965 }
1966
1967 /*
1968 * Find an interface on a specific network. If many, choice
1969 * is most specific found.
1970 */
1971 struct ifaddr *
1972 ifa_ifwithnet(const struct sockaddr *addr)
1973 {
1974 struct ifnet *ifp;
1975 struct ifaddr *ifa, *ifa_maybe = NULL;
1976 const struct sockaddr_dl *sdl;
1977 u_int af = addr->sa_family;
1978 const char *addr_data = addr->sa_data, *cplim;
1979
1980 if (af == AF_LINK) {
1981 sdl = satocsdl(addr);
1982 if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1983 ifindex2ifnet[sdl->sdl_index] &&
1984 !if_is_deactivated(ifindex2ifnet[sdl->sdl_index])) {
1985 return ifindex2ifnet[sdl->sdl_index]->if_dl;
1986 }
1987 }
1988 #ifdef NETATALK
1989 if (af == AF_APPLETALK) {
1990 const struct sockaddr_at *sat, *sat2;
1991 sat = (const struct sockaddr_at *)addr;
1992 IFNET_READER_FOREACH(ifp) {
1993 if (if_is_deactivated(ifp))
1994 continue;
1995 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1996 if (ifa == NULL)
1997 continue;
1998 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1999 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
2000 return ifa; /* exact match */
2001 if (ifa_maybe == NULL) {
2002 /* else keep the if with the right range */
2003 ifa_maybe = ifa;
2004 }
2005 }
2006 return ifa_maybe;
2007 }
2008 #endif
2009 IFNET_READER_FOREACH(ifp) {
2010 if (if_is_deactivated(ifp))
2011 continue;
2012 IFADDR_READER_FOREACH(ifa, ifp) {
2013 const char *cp, *cp2, *cp3;
2014
2015 if (ifa->ifa_addr->sa_family != af ||
2016 ifa->ifa_netmask == NULL)
2017 next: continue;
2018 cp = addr_data;
2019 cp2 = ifa->ifa_addr->sa_data;
2020 cp3 = ifa->ifa_netmask->sa_data;
2021 cplim = (const char *)ifa->ifa_netmask +
2022 ifa->ifa_netmask->sa_len;
2023 while (cp3 < cplim) {
2024 if ((*cp++ ^ *cp2++) & *cp3++) {
2025 /* want to continue for() loop */
2026 goto next;
2027 }
2028 }
2029 if (ifa_maybe == NULL ||
2030 rt_refines(ifa->ifa_netmask,
2031 ifa_maybe->ifa_netmask))
2032 ifa_maybe = ifa;
2033 }
2034 }
2035 return ifa_maybe;
2036 }
2037
2038 struct ifaddr *
2039 ifa_ifwithnet_psref(const struct sockaddr *addr, struct psref *psref)
2040 {
2041 struct ifaddr *ifa;
2042 int s;
2043
2044 s = pserialize_read_enter();
2045 ifa = ifa_ifwithnet(addr);
2046 if (ifa != NULL)
2047 ifa_acquire(ifa, psref);
2048 pserialize_read_exit(s);
2049
2050 return ifa;
2051 }
2052
2053 /*
2054 * Find the interface of the addresss.
2055 */
2056 struct ifaddr *
2057 ifa_ifwithladdr(const struct sockaddr *addr)
2058 {
2059 struct ifaddr *ia;
2060
2061 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
2062 (ia = ifa_ifwithnet(addr)))
2063 return ia;
2064 return NULL;
2065 }
2066
2067 struct ifaddr *
2068 ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref)
2069 {
2070 struct ifaddr *ifa;
2071 int s;
2072
2073 s = pserialize_read_enter();
2074 ifa = ifa_ifwithladdr(addr);
2075 if (ifa != NULL)
2076 ifa_acquire(ifa, psref);
2077 pserialize_read_exit(s);
2078
2079 return ifa;
2080 }
2081
2082 /*
2083 * Find an interface using a specific address family
2084 */
2085 struct ifaddr *
2086 ifa_ifwithaf(int af)
2087 {
2088 struct ifnet *ifp;
2089 struct ifaddr *ifa = NULL;
2090 int s;
2091
2092 s = pserialize_read_enter();
2093 IFNET_READER_FOREACH(ifp) {
2094 if (if_is_deactivated(ifp))
2095 continue;
2096 IFADDR_READER_FOREACH(ifa, ifp) {
2097 if (ifa->ifa_addr->sa_family == af)
2098 goto out;
2099 }
2100 }
2101 out:
2102 pserialize_read_exit(s);
2103 return ifa;
2104 }
2105
2106 /*
2107 * Find an interface address specific to an interface best matching
2108 * a given address.
2109 */
2110 struct ifaddr *
2111 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2112 {
2113 struct ifaddr *ifa;
2114 const char *cp, *cp2, *cp3;
2115 const char *cplim;
2116 struct ifaddr *ifa_maybe = 0;
2117 u_int af = addr->sa_family;
2118
2119 if (if_is_deactivated(ifp))
2120 return NULL;
2121
2122 if (af >= AF_MAX)
2123 return NULL;
2124
2125 IFADDR_READER_FOREACH(ifa, ifp) {
2126 if (ifa->ifa_addr->sa_family != af)
2127 continue;
2128 ifa_maybe = ifa;
2129 if (ifa->ifa_netmask == NULL) {
2130 if (equal(addr, ifa->ifa_addr) ||
2131 (ifa->ifa_dstaddr &&
2132 equal(addr, ifa->ifa_dstaddr)))
2133 return ifa;
2134 continue;
2135 }
2136 cp = addr->sa_data;
2137 cp2 = ifa->ifa_addr->sa_data;
2138 cp3 = ifa->ifa_netmask->sa_data;
2139 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2140 for (; cp3 < cplim; cp3++) {
2141 if ((*cp++ ^ *cp2++) & *cp3)
2142 break;
2143 }
2144 if (cp3 == cplim)
2145 return ifa;
2146 }
2147 return ifa_maybe;
2148 }
2149
2150 struct ifaddr *
2151 ifaof_ifpforaddr_psref(const struct sockaddr *addr, struct ifnet *ifp,
2152 struct psref *psref)
2153 {
2154 struct ifaddr *ifa;
2155 int s;
2156
2157 s = pserialize_read_enter();
2158 ifa = ifaof_ifpforaddr(addr, ifp);
2159 if (ifa != NULL)
2160 ifa_acquire(ifa, psref);
2161 pserialize_read_exit(s);
2162
2163 return ifa;
2164 }
2165
2166 /*
2167 * Default action when installing a route with a Link Level gateway.
2168 * Lookup an appropriate real ifa to point to.
2169 * This should be moved to /sys/net/link.c eventually.
2170 */
2171 void
2172 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
2173 {
2174 struct ifaddr *ifa;
2175 const struct sockaddr *dst;
2176 struct ifnet *ifp;
2177 struct psref psref;
2178
2179 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
2180 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
2181 return;
2182 if ((ifa = ifaof_ifpforaddr_psref(dst, ifp, &psref)) != NULL) {
2183 rt_replace_ifa(rt, ifa);
2184 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2185 ifa->ifa_rtrequest(cmd, rt, info);
2186 ifa_release(ifa, &psref);
2187 }
2188 }
2189
2190 /*
2191 * bitmask macros to manage a densely packed link_state change queue.
2192 * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
2193 * LINK_STATE_UP(2) we need 2 bits for each state change.
2194 * As a state change to store is 0, treat all bits set as an unset item.
2195 */
2196 #define LQ_ITEM_BITS 2
2197 #define LQ_ITEM_MASK ((1 << LQ_ITEM_BITS) - 1)
2198 #define LQ_MASK(i) (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
2199 #define LINK_STATE_UNSET LQ_ITEM_MASK
2200 #define LQ_ITEM(q, i) (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
2201 #define LQ_STORE(q, i, v) \
2202 do { \
2203 (q) &= ~LQ_MASK((i)); \
2204 (q) |= (v) << (i) * LQ_ITEM_BITS; \
2205 } while (0 /* CONSTCOND */)
2206 #define LQ_MAX(q) ((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
2207 #define LQ_POP(q, v) \
2208 do { \
2209 (v) = LQ_ITEM((q), 0); \
2210 (q) >>= LQ_ITEM_BITS; \
2211 (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \
2212 } while (0 /* CONSTCOND */)
2213 #define LQ_PUSH(q, v) \
2214 do { \
2215 (q) >>= LQ_ITEM_BITS; \
2216 (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \
2217 } while (0 /* CONSTCOND */)
2218 #define LQ_FIND_UNSET(q, i) \
2219 for ((i) = 0; i < LQ_MAX((q)); (i)++) { \
2220 if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET) \
2221 break; \
2222 }
2223
2224 /*
2225 * XXX reusing (ifp)->if_snd->ifq_lock rather than having another spin mutex
2226 * for each ifnet. It doesn't matter because:
2227 * - if IFEF_MPSAFE is enabled, if_snd isn't used and lock contentions on
2228 * ifq_lock don't happen
2229 * - if IFEF_MPSAFE is disabled, there is no lock contention on ifq_lock
2230 * because if_snd, if_link_state_change and if_link_state_change_softint
2231 * are all called with KERNEL_LOCK
2232 */
2233 #define IF_LINK_STATE_CHANGE_LOCK(ifp) \
2234 mutex_enter((ifp)->if_snd.ifq_lock)
2235 #define IF_LINK_STATE_CHANGE_UNLOCK(ifp) \
2236 mutex_exit((ifp)->if_snd.ifq_lock)
2237
2238 /*
2239 * Handle a change in the interface link state and
2240 * queue notifications.
2241 */
2242 void
2243 if_link_state_change(struct ifnet *ifp, int link_state)
2244 {
2245 int idx;
2246
2247 KASSERTMSG(if_is_link_state_changeable(ifp),
2248 "%s: IFEF_NO_LINK_STATE_CHANGE must not be set, but if_extflags=0x%x",
2249 ifp->if_xname, ifp->if_extflags);
2250
2251 /* Ensure change is to a valid state */
2252 switch (link_state) {
2253 case LINK_STATE_UNKNOWN: /* FALLTHROUGH */
2254 case LINK_STATE_DOWN: /* FALLTHROUGH */
2255 case LINK_STATE_UP:
2256 break;
2257 default:
2258 #ifdef DEBUG
2259 printf("%s: invalid link state %d\n",
2260 ifp->if_xname, link_state);
2261 #endif
2262 return;
2263 }
2264
2265 IF_LINK_STATE_CHANGE_LOCK(ifp);
2266
2267 /* Find the last unset event in the queue. */
2268 LQ_FIND_UNSET(ifp->if_link_queue, idx);
2269
2270 /*
2271 * Ensure link_state doesn't match the last event in the queue.
2272 * ifp->if_link_state is not checked and set here because
2273 * that would present an inconsistent picture to the system.
2274 */
2275 if (idx != 0 &&
2276 LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
2277 goto out;
2278
2279 /* Handle queue overflow. */
2280 if (idx == LQ_MAX(ifp->if_link_queue)) {
2281 uint8_t lost;
2282
2283 /*
2284 * The DOWN state must be protected from being pushed off
2285 * the queue to ensure that userland will always be
2286 * in a sane state.
2287 * Because DOWN is protected, there is no need to protect
2288 * UNKNOWN.
2289 * It should be invalid to change from any other state to
2290 * UNKNOWN anyway ...
2291 */
2292 lost = LQ_ITEM(ifp->if_link_queue, 0);
2293 LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
2294 if (lost == LINK_STATE_DOWN) {
2295 lost = LQ_ITEM(ifp->if_link_queue, 0);
2296 LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
2297 }
2298 printf("%s: lost link state change %s\n",
2299 ifp->if_xname,
2300 lost == LINK_STATE_UP ? "UP" :
2301 lost == LINK_STATE_DOWN ? "DOWN" :
2302 "UNKNOWN");
2303 } else
2304 LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
2305
2306 softint_schedule(ifp->if_link_si);
2307
2308 out:
2309 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2310 }
2311
2312 /*
2313 * Handle interface link state change notifications.
2314 */
2315 void
2316 if_link_state_change_softint(struct ifnet *ifp, int link_state)
2317 {
2318 struct domain *dp;
2319 int s = splnet();
2320 bool notify;
2321
2322 KASSERT(!cpu_intr_p());
2323
2324 IF_LINK_STATE_CHANGE_LOCK(ifp);
2325
2326 /* Ensure the change is still valid. */
2327 if (ifp->if_link_state == link_state) {
2328 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2329 splx(s);
2330 return;
2331 }
2332
2333 #ifdef DEBUG
2334 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
2335 link_state == LINK_STATE_UP ? "UP" :
2336 link_state == LINK_STATE_DOWN ? "DOWN" :
2337 "UNKNOWN",
2338 ifp->if_link_state == LINK_STATE_UP ? "UP" :
2339 ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
2340 "UNKNOWN");
2341 #endif
2342
2343 /*
2344 * When going from UNKNOWN to UP, we need to mark existing
2345 * addresses as tentative and restart DAD as we may have
2346 * erroneously not found a duplicate.
2347 *
2348 * This needs to happen before rt_ifmsg to avoid a race where
2349 * listeners would have an address and expect it to work right
2350 * away.
2351 */
2352 notify = (link_state == LINK_STATE_UP &&
2353 ifp->if_link_state == LINK_STATE_UNKNOWN);
2354 ifp->if_link_state = link_state;
2355 /* The following routines may sleep so release the spin mutex */
2356 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2357
2358 KERNEL_LOCK_UNLESS_NET_MPSAFE();
2359 if (notify) {
2360 DOMAIN_FOREACH(dp) {
2361 if (dp->dom_if_link_state_change != NULL)
2362 dp->dom_if_link_state_change(ifp,
2363 LINK_STATE_DOWN);
2364 }
2365 }
2366
2367 /* Notify that the link state has changed. */
2368 rt_ifmsg(ifp);
2369
2370 #if NCARP > 0
2371 if (ifp->if_carp)
2372 carp_carpdev_state(ifp);
2373 #endif
2374
2375 DOMAIN_FOREACH(dp) {
2376 if (dp->dom_if_link_state_change != NULL)
2377 dp->dom_if_link_state_change(ifp, link_state);
2378 }
2379 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2380 splx(s);
2381 }
2382
2383 /*
2384 * Process the interface link state change queue.
2385 */
2386 static void
2387 if_link_state_change_si(void *arg)
2388 {
2389 struct ifnet *ifp = arg;
2390 int s;
2391 uint8_t state;
2392 bool schedule;
2393
2394 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
2395 s = splnet();
2396
2397 /* Pop a link state change from the queue and process it. */
2398 IF_LINK_STATE_CHANGE_LOCK(ifp);
2399 LQ_POP(ifp->if_link_queue, state);
2400 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2401
2402 if_link_state_change_softint(ifp, state);
2403
2404 /* If there is a link state change to come, schedule it. */
2405 IF_LINK_STATE_CHANGE_LOCK(ifp);
2406 schedule = (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET);
2407 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2408 if (schedule)
2409 softint_schedule(ifp->if_link_si);
2410
2411 splx(s);
2412 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2413 }
2414
2415 /*
2416 * Default action when installing a local route on a point-to-point
2417 * interface.
2418 */
2419 void
2420 p2p_rtrequest(int req, struct rtentry *rt,
2421 __unused const struct rt_addrinfo *info)
2422 {
2423 struct ifnet *ifp = rt->rt_ifp;
2424 struct ifaddr *ifa, *lo0ifa;
2425 int s = pserialize_read_enter();
2426
2427 switch (req) {
2428 case RTM_ADD:
2429 if ((rt->rt_flags & RTF_LOCAL) == 0)
2430 break;
2431
2432 rt->rt_ifp = lo0ifp;
2433
2434 IFADDR_READER_FOREACH(ifa, ifp) {
2435 if (equal(rt_getkey(rt), ifa->ifa_addr))
2436 break;
2437 }
2438 if (ifa == NULL)
2439 break;
2440
2441 /*
2442 * Ensure lo0 has an address of the same family.
2443 */
2444 IFADDR_READER_FOREACH(lo0ifa, lo0ifp) {
2445 if (lo0ifa->ifa_addr->sa_family ==
2446 ifa->ifa_addr->sa_family)
2447 break;
2448 }
2449 if (lo0ifa == NULL)
2450 break;
2451
2452 /*
2453 * Make sure to set rt->rt_ifa to the interface
2454 * address we are using, otherwise we will have trouble
2455 * with source address selection.
2456 */
2457 if (ifa != rt->rt_ifa)
2458 rt_replace_ifa(rt, ifa);
2459 break;
2460 case RTM_DELETE:
2461 default:
2462 break;
2463 }
2464 pserialize_read_exit(s);
2465 }
2466
2467 static void
2468 _if_down(struct ifnet *ifp)
2469 {
2470 struct ifaddr *ifa;
2471 struct domain *dp;
2472 int s, bound;
2473 struct psref psref;
2474
2475 ifp->if_flags &= ~IFF_UP;
2476 nanotime(&ifp->if_lastchange);
2477
2478 bound = curlwp_bind();
2479 s = pserialize_read_enter();
2480 IFADDR_READER_FOREACH(ifa, ifp) {
2481 ifa_acquire(ifa, &psref);
2482 pserialize_read_exit(s);
2483
2484 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2485
2486 s = pserialize_read_enter();
2487 ifa_release(ifa, &psref);
2488 }
2489 pserialize_read_exit(s);
2490 curlwp_bindx(bound);
2491
2492 IFQ_PURGE(&ifp->if_snd);
2493 #if NCARP > 0
2494 if (ifp->if_carp)
2495 carp_carpdev_state(ifp);
2496 #endif
2497 rt_ifmsg(ifp);
2498 DOMAIN_FOREACH(dp) {
2499 if (dp->dom_if_down)
2500 dp->dom_if_down(ifp);
2501 }
2502 }
2503
2504 static void
2505 if_down_deactivated(struct ifnet *ifp)
2506 {
2507
2508 KASSERT(if_is_deactivated(ifp));
2509 _if_down(ifp);
2510 }
2511
2512 void
2513 if_down_locked(struct ifnet *ifp)
2514 {
2515
2516 KASSERT(IFNET_LOCKED(ifp));
2517 _if_down(ifp);
2518 }
2519
2520 /*
2521 * Mark an interface down and notify protocols of
2522 * the transition.
2523 * NOTE: must be called at splsoftnet or equivalent.
2524 */
2525 void
2526 if_down(struct ifnet *ifp)
2527 {
2528
2529 IFNET_LOCK(ifp);
2530 if_down_locked(ifp);
2531 IFNET_UNLOCK(ifp);
2532 }
2533
2534 /*
2535 * Must be called with holding if_ioctl_lock.
2536 */
2537 static void
2538 if_up_locked(struct ifnet *ifp)
2539 {
2540 #ifdef notyet
2541 struct ifaddr *ifa;
2542 #endif
2543 struct domain *dp;
2544
2545 KASSERT(IFNET_LOCKED(ifp));
2546
2547 KASSERT(!if_is_deactivated(ifp));
2548 ifp->if_flags |= IFF_UP;
2549 nanotime(&ifp->if_lastchange);
2550 #ifdef notyet
2551 /* this has no effect on IP, and will kill all ISO connections XXX */
2552 IFADDR_READER_FOREACH(ifa, ifp)
2553 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2554 #endif
2555 #if NCARP > 0
2556 if (ifp->if_carp)
2557 carp_carpdev_state(ifp);
2558 #endif
2559 rt_ifmsg(ifp);
2560 DOMAIN_FOREACH(dp) {
2561 if (dp->dom_if_up)
2562 dp->dom_if_up(ifp);
2563 }
2564 }
2565
2566 /*
2567 * Handle interface slowtimo timer routine. Called
2568 * from softclock, we decrement timer (if set) and
2569 * call the appropriate interface routine on expiration.
2570 */
2571 static void
2572 if_slowtimo(void *arg)
2573 {
2574 void (*slowtimo)(struct ifnet *);
2575 struct ifnet *ifp = arg;
2576 int s;
2577
2578 slowtimo = ifp->if_slowtimo;
2579 if (__predict_false(slowtimo == NULL))
2580 return;
2581
2582 s = splnet();
2583 if (ifp->if_timer != 0 && --ifp->if_timer == 0)
2584 (*slowtimo)(ifp);
2585
2586 splx(s);
2587
2588 if (__predict_true(ifp->if_slowtimo != NULL))
2589 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
2590 }
2591
2592 /*
2593 * Mark an interface up and notify protocols of
2594 * the transition.
2595 * NOTE: must be called at splsoftnet or equivalent.
2596 */
2597 void
2598 if_up(struct ifnet *ifp)
2599 {
2600
2601 IFNET_LOCK(ifp);
2602 if_up_locked(ifp);
2603 IFNET_UNLOCK(ifp);
2604 }
2605
2606 /*
2607 * Set/clear promiscuous mode on interface ifp based on the truth value
2608 * of pswitch. The calls are reference counted so that only the first
2609 * "on" request actually has an effect, as does the final "off" request.
2610 * Results are undefined if the "off" and "on" requests are not matched.
2611 */
2612 int
2613 ifpromisc_locked(struct ifnet *ifp, int pswitch)
2614 {
2615 int pcount, ret = 0;
2616 short nflags;
2617
2618 KASSERT(IFNET_LOCKED(ifp));
2619
2620 pcount = ifp->if_pcount;
2621 if (pswitch) {
2622 /*
2623 * Allow the device to be "placed" into promiscuous
2624 * mode even if it is not configured up. It will
2625 * consult IFF_PROMISC when it is brought up.
2626 */
2627 if (ifp->if_pcount++ != 0)
2628 goto out;
2629 nflags = ifp->if_flags | IFF_PROMISC;
2630 } else {
2631 if (--ifp->if_pcount > 0)
2632 goto out;
2633 nflags = ifp->if_flags & ~IFF_PROMISC;
2634 }
2635 ret = if_flags_set(ifp, nflags);
2636 /* Restore interface state if not successful. */
2637 if (ret != 0) {
2638 ifp->if_pcount = pcount;
2639 }
2640 out:
2641 return ret;
2642 }
2643
2644 int
2645 ifpromisc(struct ifnet *ifp, int pswitch)
2646 {
2647 int e;
2648
2649 IFNET_LOCK(ifp);
2650 e = ifpromisc_locked(ifp, pswitch);
2651 IFNET_UNLOCK(ifp);
2652
2653 return e;
2654 }
2655
2656 /*
2657 * Map interface name to
2658 * interface structure pointer.
2659 */
2660 struct ifnet *
2661 ifunit(const char *name)
2662 {
2663 struct ifnet *ifp;
2664 const char *cp = name;
2665 u_int unit = 0;
2666 u_int i;
2667 int s;
2668
2669 /*
2670 * If the entire name is a number, treat it as an ifindex.
2671 */
2672 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2673 unit = unit * 10 + (*cp - '0');
2674 }
2675
2676 /*
2677 * If the number took all of the name, then it's a valid ifindex.
2678 */
2679 if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
2680 return if_byindex(unit);
2681
2682 ifp = NULL;
2683 s = pserialize_read_enter();
2684 IFNET_READER_FOREACH(ifp) {
2685 if (if_is_deactivated(ifp))
2686 continue;
2687 if (strcmp(ifp->if_xname, name) == 0)
2688 goto out;
2689 }
2690 out:
2691 pserialize_read_exit(s);
2692 return ifp;
2693 }
2694
2695 /*
2696 * Get a reference of an ifnet object by an interface name.
2697 * The returned reference is protected by psref(9). The caller
2698 * must release a returned reference by if_put after use.
2699 */
2700 struct ifnet *
2701 if_get(const char *name, struct psref *psref)
2702 {
2703 struct ifnet *ifp;
2704 const char *cp = name;
2705 u_int unit = 0;
2706 u_int i;
2707 int s;
2708
2709 /*
2710 * If the entire name is a number, treat it as an ifindex.
2711 */
2712 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2713 unit = unit * 10 + (*cp - '0');
2714 }
2715
2716 /*
2717 * If the number took all of the name, then it's a valid ifindex.
2718 */
2719 if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
2720 return if_get_byindex(unit, psref);
2721
2722 ifp = NULL;
2723 s = pserialize_read_enter();
2724 IFNET_READER_FOREACH(ifp) {
2725 if (if_is_deactivated(ifp))
2726 continue;
2727 if (strcmp(ifp->if_xname, name) == 0) {
2728 psref_acquire(psref, &ifp->if_psref,
2729 ifnet_psref_class);
2730 goto out;
2731 }
2732 }
2733 out:
2734 pserialize_read_exit(s);
2735 return ifp;
2736 }
2737
2738 /*
2739 * Release a reference of an ifnet object given by if_get, if_get_byindex
2740 * or if_get_bylla.
2741 */
2742 void
2743 if_put(const struct ifnet *ifp, struct psref *psref)
2744 {
2745
2746 if (ifp == NULL)
2747 return;
2748
2749 psref_release(psref, &ifp->if_psref, ifnet_psref_class);
2750 }
2751
2752 /*
2753 * Return ifp having idx. Return NULL if not found. Normally if_byindex
2754 * should be used.
2755 */
2756 ifnet_t *
2757 _if_byindex(u_int idx)
2758 {
2759
2760 return (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
2761 }
2762
2763 /*
2764 * Return ifp having idx. Return NULL if not found or the found ifp is
2765 * already deactivated.
2766 */
2767 ifnet_t *
2768 if_byindex(u_int idx)
2769 {
2770 ifnet_t *ifp;
2771
2772 ifp = _if_byindex(idx);
2773 if (ifp != NULL && if_is_deactivated(ifp))
2774 ifp = NULL;
2775 return ifp;
2776 }
2777
2778 /*
2779 * Get a reference of an ifnet object by an interface index.
2780 * The returned reference is protected by psref(9). The caller
2781 * must release a returned reference by if_put after use.
2782 */
2783 ifnet_t *
2784 if_get_byindex(u_int idx, struct psref *psref)
2785 {
2786 ifnet_t *ifp;
2787 int s;
2788
2789 s = pserialize_read_enter();
2790 ifp = if_byindex(idx);
2791 if (__predict_true(ifp != NULL))
2792 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
2793 pserialize_read_exit(s);
2794
2795 return ifp;
2796 }
2797
2798 ifnet_t *
2799 if_get_bylla(const void *lla, unsigned char lla_len, struct psref *psref)
2800 {
2801 ifnet_t *ifp;
2802 int s;
2803
2804 s = pserialize_read_enter();
2805 IFNET_READER_FOREACH(ifp) {
2806 if (if_is_deactivated(ifp))
2807 continue;
2808 if (ifp->if_addrlen != lla_len)
2809 continue;
2810 if (memcmp(lla, CLLADDR(ifp->if_sadl), lla_len) == 0) {
2811 psref_acquire(psref, &ifp->if_psref,
2812 ifnet_psref_class);
2813 break;
2814 }
2815 }
2816 pserialize_read_exit(s);
2817
2818 return ifp;
2819 }
2820
2821 /*
2822 * Note that it's safe only if the passed ifp is guaranteed to not be freed,
2823 * for example using pserialize or the ifp is already held or some other
2824 * object is held which guarantes the ifp to not be freed indirectly.
2825 */
2826 void
2827 if_acquire(struct ifnet *ifp, struct psref *psref)
2828 {
2829
2830 KASSERT(ifp->if_index != 0);
2831 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
2832 }
2833
2834 bool
2835 if_held(struct ifnet *ifp)
2836 {
2837
2838 return psref_held(&ifp->if_psref, ifnet_psref_class);
2839 }
2840
2841 /*
2842 * Some tunnel interfaces can nest, e.g. IPv4 over IPv4 gif(4) tunnel over IPv4.
2843 * Check the tunnel nesting count.
2844 * Return > 0, if tunnel nesting count is more than limit.
2845 * Return 0, if tunnel nesting count is equal or less than limit.
2846 */
2847 int
2848 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, int limit)
2849 {
2850 struct m_tag *mtag;
2851 int *count;
2852
2853 mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO, NULL);
2854 if (mtag != NULL) {
2855 count = (int *)(mtag + 1);
2856 if (++(*count) > limit) {
2857 log(LOG_NOTICE,
2858 "%s: recursively called too many times(%d)\n",
2859 ifp->if_xname, *count);
2860 return EIO;
2861 }
2862 } else {
2863 mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count),
2864 M_NOWAIT);
2865 if (mtag != NULL) {
2866 m_tag_prepend(m, mtag);
2867 count = (int *)(mtag + 1);
2868 *count = 0;
2869 } else {
2870 log(LOG_DEBUG,
2871 "%s: m_tag_get() failed, recursion calls are not prevented.\n",
2872 ifp->if_xname);
2873 }
2874 }
2875
2876 return 0;
2877 }
2878
2879 /* common */
2880 int
2881 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
2882 {
2883 int s;
2884 struct ifreq *ifr;
2885 struct ifcapreq *ifcr;
2886 struct ifdatareq *ifdr;
2887
2888 switch (cmd) {
2889 case SIOCSIFCAP:
2890 ifcr = data;
2891 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
2892 return EINVAL;
2893
2894 if (ifcr->ifcr_capenable == ifp->if_capenable)
2895 return 0;
2896
2897 ifp->if_capenable = ifcr->ifcr_capenable;
2898
2899 /* Pre-compute the checksum flags mask. */
2900 ifp->if_csum_flags_tx = 0;
2901 ifp->if_csum_flags_rx = 0;
2902 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
2903 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
2904 }
2905 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
2906 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
2907 }
2908
2909 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
2910 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
2911 }
2912 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
2913 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
2914 }
2915
2916 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
2917 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
2918 }
2919 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
2920 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
2921 }
2922
2923 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
2924 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
2925 }
2926 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
2927 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
2928 }
2929
2930 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
2931 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
2932 }
2933 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
2934 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
2935 }
2936 if (ifp->if_flags & IFF_UP)
2937 return ENETRESET;
2938 return 0;
2939 case SIOCSIFFLAGS:
2940 ifr = data;
2941 /*
2942 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here, but if_up
2943 * and if_down aren't MP-safe yet, so we must hold the lock.
2944 */
2945 KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
2946 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
2947 s = splsoftnet();
2948 if_down_locked(ifp);
2949 splx(s);
2950 }
2951 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
2952 s = splsoftnet();
2953 if_up_locked(ifp);
2954 splx(s);
2955 }
2956 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
2957 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2958 (ifr->ifr_flags &~ IFF_CANTCHANGE);
2959 break;
2960 case SIOCGIFFLAGS:
2961 ifr = data;
2962 ifr->ifr_flags = ifp->if_flags;
2963 break;
2964
2965 case SIOCGIFMETRIC:
2966 ifr = data;
2967 ifr->ifr_metric = ifp->if_metric;
2968 break;
2969
2970 case SIOCGIFMTU:
2971 ifr = data;
2972 ifr->ifr_mtu = ifp->if_mtu;
2973 break;
2974
2975 case SIOCGIFDLT:
2976 ifr = data;
2977 ifr->ifr_dlt = ifp->if_dlt;
2978 break;
2979
2980 case SIOCGIFCAP:
2981 ifcr = data;
2982 ifcr->ifcr_capabilities = ifp->if_capabilities;
2983 ifcr->ifcr_capenable = ifp->if_capenable;
2984 break;
2985
2986 case SIOCSIFMETRIC:
2987 ifr = data;
2988 ifp->if_metric = ifr->ifr_metric;
2989 break;
2990
2991 case SIOCGIFDATA:
2992 ifdr = data;
2993 ifdr->ifdr_data = ifp->if_data;
2994 break;
2995
2996 case SIOCGIFINDEX:
2997 ifr = data;
2998 ifr->ifr_index = ifp->if_index;
2999 break;
3000
3001 case SIOCZIFDATA:
3002 ifdr = data;
3003 ifdr->ifdr_data = ifp->if_data;
3004 /*
3005 * Assumes that the volatile counters that can be
3006 * zero'ed are at the end of if_data.
3007 */
3008 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
3009 offsetof(struct if_data, ifi_ipackets));
3010 /*
3011 * The memset() clears to the bottm of if_data. In the area,
3012 * if_lastchange is included. Please be careful if new entry
3013 * will be added into if_data or rewite this.
3014 *
3015 * And also, update if_lastchnage.
3016 */
3017 getnanotime(&ifp->if_lastchange);
3018 break;
3019 case SIOCSIFMTU:
3020 ifr = data;
3021 if (ifp->if_mtu == ifr->ifr_mtu)
3022 break;
3023 ifp->if_mtu = ifr->ifr_mtu;
3024 /*
3025 * If the link MTU changed, do network layer specific procedure.
3026 */
3027 #ifdef INET6
3028 KERNEL_LOCK_UNLESS_NET_MPSAFE();
3029 if (in6_present)
3030 nd6_setmtu(ifp);
3031 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
3032 #endif
3033 return ENETRESET;
3034 default:
3035 return ENOTTY;
3036 }
3037 return 0;
3038 }
3039
3040 int
3041 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
3042 {
3043 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
3044 struct ifaddr *ifa;
3045 const struct sockaddr *any, *sa;
3046 union {
3047 struct sockaddr sa;
3048 struct sockaddr_storage ss;
3049 } u, v;
3050 int s, error = 0;
3051
3052 switch (cmd) {
3053 case SIOCSIFADDRPREF:
3054 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
3055 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
3056 NULL) != 0)
3057 return EPERM;
3058 case SIOCGIFADDRPREF:
3059 break;
3060 default:
3061 return EOPNOTSUPP;
3062 }
3063
3064 /* sanity checks */
3065 if (data == NULL || ifp == NULL) {
3066 panic("invalid argument to %s", __func__);
3067 /*NOTREACHED*/
3068 }
3069
3070 /* address must be specified on ADD and DELETE */
3071 sa = sstocsa(&ifap->ifap_addr);
3072 if (sa->sa_family != sofamily(so))
3073 return EINVAL;
3074 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
3075 return EINVAL;
3076
3077 sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
3078
3079 s = pserialize_read_enter();
3080 IFADDR_READER_FOREACH(ifa, ifp) {
3081 if (ifa->ifa_addr->sa_family != sa->sa_family)
3082 continue;
3083 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
3084 if (sockaddr_cmp(&u.sa, &v.sa) == 0)
3085 break;
3086 }
3087 if (ifa == NULL) {
3088 error = EADDRNOTAVAIL;
3089 goto out;
3090 }
3091
3092 switch (cmd) {
3093 case SIOCSIFADDRPREF:
3094 ifa->ifa_preference = ifap->ifap_preference;
3095 goto out;
3096 case SIOCGIFADDRPREF:
3097 /* fill in the if_laddrreq structure */
3098 (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
3099 sizeof(ifap->ifap_addr), ifa->ifa_addr);
3100 ifap->ifap_preference = ifa->ifa_preference;
3101 goto out;
3102 default:
3103 error = EOPNOTSUPP;
3104 }
3105 out:
3106 pserialize_read_exit(s);
3107 return error;
3108 }
3109
3110 /*
3111 * Interface ioctls.
3112 */
3113 static int
3114 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
3115 {
3116 struct ifnet *ifp;
3117 struct ifreq *ifr;
3118 int error = 0;
3119 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
3120 u_long ocmd = cmd;
3121 #endif
3122 short oif_flags;
3123 #ifdef COMPAT_OIFREQ
3124 struct ifreq ifrb;
3125 struct oifreq *oifr = NULL;
3126 #endif
3127 int r;
3128 struct psref psref;
3129 int bound;
3130
3131 switch (cmd) {
3132 case SIOCGIFCONF:
3133 return ifconf(cmd, data);
3134 case SIOCINITIFADDR:
3135 return EPERM;
3136 default:
3137 error = (*vec_compat_ifconf)(l, cmd, data);
3138 if (error != ENOSYS)
3139 return error;
3140 error = (*vec_compat_ifdatareq)(l, cmd, data);
3141 if (error != ENOSYS)
3142 return error;
3143 break;
3144 }
3145
3146 ifr = data;
3147 #ifdef COMPAT_OIFREQ
3148 if (vec_compat_cvtcmd) {
3149 cmd = (*vec_compat_cvtcmd)(cmd);
3150 if (cmd != ocmd) {
3151 oifr = data;
3152 data = ifr = &ifrb;
3153 ifreqo2n(oifr, ifr);
3154 }
3155 }
3156 #endif
3157
3158 switch (cmd) {
3159 case SIOCIFCREATE:
3160 case SIOCIFDESTROY:
3161 bound = curlwp_bind();
3162 if (l != NULL) {
3163 ifp = if_get(ifr->ifr_name, &psref);
3164 error = kauth_authorize_network(l->l_cred,
3165 KAUTH_NETWORK_INTERFACE,
3166 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
3167 (void *)cmd, NULL);
3168 if (ifp != NULL)
3169 if_put(ifp, &psref);
3170 if (error != 0) {
3171 curlwp_bindx(bound);
3172 return error;
3173 }
3174 }
3175 KERNEL_LOCK_UNLESS_NET_MPSAFE();
3176 mutex_enter(&if_clone_mtx);
3177 r = (cmd == SIOCIFCREATE) ?
3178 if_clone_create(ifr->ifr_name) :
3179 if_clone_destroy(ifr->ifr_name);
3180 mutex_exit(&if_clone_mtx);
3181 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
3182 curlwp_bindx(bound);
3183 return r;
3184
3185 case SIOCIFGCLONERS:
3186 {
3187 struct if_clonereq *req = (struct if_clonereq *)data;
3188 return if_clone_list(req->ifcr_count, req->ifcr_buffer,
3189 &req->ifcr_total);
3190 }
3191 }
3192
3193 bound = curlwp_bind();
3194 ifp = if_get(ifr->ifr_name, &psref);
3195 if (ifp == NULL) {
3196 curlwp_bindx(bound);
3197 return ENXIO;
3198 }
3199
3200 switch (cmd) {
3201 case SIOCALIFADDR:
3202 case SIOCDLIFADDR:
3203 case SIOCSIFADDRPREF:
3204 case SIOCSIFFLAGS:
3205 case SIOCSIFCAP:
3206 case SIOCSIFMETRIC:
3207 case SIOCZIFDATA:
3208 case SIOCSIFMTU:
3209 case SIOCSIFPHYADDR:
3210 case SIOCDIFPHYADDR:
3211 #ifdef INET6
3212 case SIOCSIFPHYADDR_IN6:
3213 #endif
3214 case SIOCSLIFPHYADDR:
3215 case SIOCADDMULTI:
3216 case SIOCDELMULTI:
3217 case SIOCSIFMEDIA:
3218 case SIOCSDRVSPEC:
3219 case SIOCG80211:
3220 case SIOCS80211:
3221 case SIOCS80211NWID:
3222 case SIOCS80211NWKEY:
3223 case SIOCS80211POWER:
3224 case SIOCS80211BSSID:
3225 case SIOCS80211CHANNEL:
3226 case SIOCSLINKSTR:
3227 if (l != NULL) {
3228 error = kauth_authorize_network(l->l_cred,
3229 KAUTH_NETWORK_INTERFACE,
3230 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
3231 (void *)cmd, NULL);
3232 if (error != 0)
3233 goto out;
3234 }
3235 }
3236
3237 oif_flags = ifp->if_flags;
3238
3239 KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp);
3240 IFNET_LOCK(ifp);
3241
3242 error = (*ifp->if_ioctl)(ifp, cmd, data);
3243 if (error != ENOTTY)
3244 ;
3245 else if (so->so_proto == NULL)
3246 error = EOPNOTSUPP;
3247 else {
3248 KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
3249 #ifdef COMPAT_OSOCK
3250 if (vec_compat_ifioctl != NULL)
3251 error = (*vec_compat_ifioctl)(so, ocmd, cmd, data, l);
3252 else
3253 #endif
3254 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
3255 cmd, data, ifp);
3256 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
3257 }
3258
3259 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
3260 if ((ifp->if_flags & IFF_UP) != 0) {
3261 int s = splsoftnet();
3262 if_up_locked(ifp);
3263 splx(s);
3264 }
3265 }
3266 #ifdef COMPAT_OIFREQ
3267 if (cmd != ocmd)
3268 ifreqn2o(oifr, ifr);
3269 #endif
3270
3271 IFNET_UNLOCK(ifp);
3272 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp);
3273 out:
3274 if_put(ifp, &psref);
3275 curlwp_bindx(bound);
3276 return error;
3277 }
3278
3279 /*
3280 * Return interface configuration
3281 * of system. List may be used
3282 * in later ioctl's (above) to get
3283 * other information.
3284 *
3285 * Each record is a struct ifreq. Before the addition of
3286 * sockaddr_storage, the API rule was that sockaddr flavors that did
3287 * not fit would extend beyond the struct ifreq, with the next struct
3288 * ifreq starting sa_len beyond the struct sockaddr. Because the
3289 * union in struct ifreq includes struct sockaddr_storage, every kind
3290 * of sockaddr must fit. Thus, there are no longer any overlength
3291 * records.
3292 *
3293 * Records are added to the user buffer if they fit, and ifc_len is
3294 * adjusted to the length that was written. Thus, the user is only
3295 * assured of getting the complete list if ifc_len on return is at
3296 * least sizeof(struct ifreq) less than it was on entry.
3297 *
3298 * If the user buffer pointer is NULL, this routine copies no data and
3299 * returns the amount of space that would be needed.
3300 *
3301 * Invariants:
3302 * ifrp points to the next part of the user's buffer to be used. If
3303 * ifrp != NULL, space holds the number of bytes remaining that we may
3304 * write at ifrp. Otherwise, space holds the number of bytes that
3305 * would have been written had there been adequate space.
3306 */
3307 /*ARGSUSED*/
3308 static int
3309 ifconf(u_long cmd, void *data)
3310 {
3311 struct ifconf *ifc = (struct ifconf *)data;
3312 struct ifnet *ifp;
3313 struct ifaddr *ifa;
3314 struct ifreq ifr, *ifrp = NULL;
3315 int space = 0, error = 0;
3316 const int sz = (int)sizeof(struct ifreq);
3317 const bool docopy = ifc->ifc_req != NULL;
3318 int s;
3319 int bound;
3320 struct psref psref;
3321
3322 if (docopy) {
3323 space = ifc->ifc_len;
3324 ifrp = ifc->ifc_req;
3325 }
3326
3327 bound = curlwp_bind();
3328 s = pserialize_read_enter();
3329 IFNET_READER_FOREACH(ifp) {
3330 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
3331 pserialize_read_exit(s);
3332
3333 (void)strncpy(ifr.ifr_name, ifp->if_xname,
3334 sizeof(ifr.ifr_name));
3335 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
3336 error = ENAMETOOLONG;
3337 goto release_exit;
3338 }
3339 if (IFADDR_READER_EMPTY(ifp)) {
3340 /* Interface with no addresses - send zero sockaddr. */
3341 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
3342 if (!docopy) {
3343 space += sz;
3344 goto next;
3345 }
3346 if (space >= sz) {
3347 error = copyout(&ifr, ifrp, sz);
3348 if (error != 0)
3349 goto release_exit;
3350 ifrp++;
3351 space -= sz;
3352 }
3353 }
3354
3355 s = pserialize_read_enter();
3356 IFADDR_READER_FOREACH(ifa, ifp) {
3357 struct sockaddr *sa = ifa->ifa_addr;
3358 /* all sockaddrs must fit in sockaddr_storage */
3359 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
3360
3361 if (!docopy) {
3362 space += sz;
3363 continue;
3364 }
3365 memcpy(&ifr.ifr_space, sa, sa->sa_len);
3366 pserialize_read_exit(s);
3367
3368 if (space >= sz) {
3369 error = copyout(&ifr, ifrp, sz);
3370 if (error != 0)
3371 goto release_exit;
3372 ifrp++; space -= sz;
3373 }
3374 s = pserialize_read_enter();
3375 }
3376 pserialize_read_exit(s);
3377
3378 next:
3379 s = pserialize_read_enter();
3380 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
3381 }
3382 pserialize_read_exit(s);
3383 curlwp_bindx(bound);
3384
3385 if (docopy) {
3386 KASSERT(0 <= space && space <= ifc->ifc_len);
3387 ifc->ifc_len -= space;
3388 } else {
3389 KASSERT(space >= 0);
3390 ifc->ifc_len = space;
3391 }
3392 return (0);
3393
3394 release_exit:
3395 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
3396 curlwp_bindx(bound);
3397 return error;
3398 }
3399
3400 int
3401 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
3402 {
3403 uint8_t len = sizeof(ifr->ifr_ifru.ifru_space);
3404 #ifdef COMPAT_OIFREQ
3405 struct ifreq ifrb;
3406 struct oifreq *oifr = NULL;
3407 u_long ocmd = cmd;
3408
3409 if (vec_compat_cvtcmd) {
3410 cmd = (*vec_compat_cvtcmd)(cmd);
3411 if (cmd != ocmd) {
3412 oifr = (struct oifreq *)(void *)ifr;
3413 ifr = &ifrb;
3414 ifreqo2n(oifr, ifr);
3415 len = sizeof(oifr->ifr_addr);
3416 }
3417 }
3418 #endif
3419 if (len < sa->sa_len)
3420 return EFBIG;
3421
3422 memset(&ifr->ifr_addr, 0, len);
3423 sockaddr_copy(&ifr->ifr_addr, len, sa);
3424
3425 #ifdef COMPAT_OIFREQ
3426 if (cmd != ocmd)
3427 ifreqn2o(oifr, ifr);
3428 #endif
3429 return 0;
3430 }
3431
3432 /*
3433 * wrapper function for the drivers which doesn't have if_transmit().
3434 */
3435 static int
3436 if_transmit(struct ifnet *ifp, struct mbuf *m)
3437 {
3438 int s, error;
3439 size_t pktlen = m->m_pkthdr.len;
3440 bool mcast = (m->m_flags & M_MCAST) != 0;
3441
3442 s = splnet();
3443
3444 IFQ_ENQUEUE(&ifp->if_snd, m, error);
3445 if (error != 0) {
3446 /* mbuf is already freed */
3447 goto out;
3448 }
3449
3450 ifp->if_obytes += pktlen;
3451 if (mcast)
3452 ifp->if_omcasts++;
3453
3454 if ((ifp->if_flags & IFF_OACTIVE) == 0)
3455 if_start_lock(ifp);
3456 out:
3457 splx(s);
3458
3459 return error;
3460 }
3461
3462 int
3463 if_transmit_lock(struct ifnet *ifp, struct mbuf *m)
3464 {
3465 int error;
3466
3467 #ifdef ALTQ
3468 KERNEL_LOCK(1, NULL);
3469 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
3470 error = if_transmit(ifp, m);
3471 KERNEL_UNLOCK_ONE(NULL);
3472 } else {
3473 KERNEL_UNLOCK_ONE(NULL);
3474 error = (*ifp->if_transmit)(ifp, m);
3475 /* mbuf is alredy freed */
3476 }
3477 #else /* !ALTQ */
3478 error = (*ifp->if_transmit)(ifp, m);
3479 /* mbuf is alredy freed */
3480 #endif /* !ALTQ */
3481
3482 return error;
3483 }
3484
3485 /*
3486 * Queue message on interface, and start output if interface
3487 * not yet active.
3488 */
3489 int
3490 ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
3491 {
3492
3493 return if_transmit_lock(ifp, m);
3494 }
3495
3496 /*
3497 * Queue message on interface, possibly using a second fast queue
3498 */
3499 int
3500 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
3501 {
3502 int error = 0;
3503
3504 if (ifq != NULL
3505 #ifdef ALTQ
3506 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
3507 #endif
3508 ) {
3509 if (IF_QFULL(ifq)) {
3510 IF_DROP(&ifp->if_snd);
3511 m_freem(m);
3512 if (error == 0)
3513 error = ENOBUFS;
3514 } else
3515 IF_ENQUEUE(ifq, m);
3516 } else
3517 IFQ_ENQUEUE(&ifp->if_snd, m, error);
3518 if (error != 0) {
3519 ++ifp->if_oerrors;
3520 return error;
3521 }
3522 return 0;
3523 }
3524
3525 int
3526 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
3527 {
3528 int rc;
3529
3530 KASSERT(IFNET_LOCKED(ifp));
3531 if (ifp->if_initaddr != NULL)
3532 rc = (*ifp->if_initaddr)(ifp, ifa, src);
3533 else if (src ||
3534 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
3535 rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
3536
3537 return rc;
3538 }
3539
3540 int
3541 if_do_dad(struct ifnet *ifp)
3542 {
3543 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
3544 return 0;
3545
3546 switch (ifp->if_type) {
3547 case IFT_FAITH:
3548 /*
3549 * These interfaces do not have the IFF_LOOPBACK flag,
3550 * but loop packets back. We do not have to do DAD on such
3551 * interfaces. We should even omit it, because loop-backed
3552 * responses would confuse the DAD procedure.
3553 */
3554 return 0;
3555 default:
3556 /*
3557 * Our DAD routine requires the interface up and running.
3558 * However, some interfaces can be up before the RUNNING
3559 * status. Additionaly, users may try to assign addresses
3560 * before the interface becomes up (or running).
3561 * We simply skip DAD in such a case as a work around.
3562 * XXX: we should rather mark "tentative" on such addresses,
3563 * and do DAD after the interface becomes ready.
3564 */
3565 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
3566 (IFF_UP|IFF_RUNNING))
3567 return 0;
3568
3569 return 1;
3570 }
3571 }
3572
3573 int
3574 if_flags_set(ifnet_t *ifp, const short flags)
3575 {
3576 int rc;
3577
3578 KASSERT(IFNET_LOCKED(ifp));
3579
3580 if (ifp->if_setflags != NULL)
3581 rc = (*ifp->if_setflags)(ifp, flags);
3582 else {
3583 short cantflags, chgdflags;
3584 struct ifreq ifr;
3585
3586 chgdflags = ifp->if_flags ^ flags;
3587 cantflags = chgdflags & IFF_CANTCHANGE;
3588
3589 if (cantflags != 0)
3590 ifp->if_flags ^= cantflags;
3591
3592 /* Traditionally, we do not call if_ioctl after
3593 * setting/clearing only IFF_PROMISC if the interface
3594 * isn't IFF_UP. Uphold that tradition.
3595 */
3596 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
3597 return 0;
3598
3599 memset(&ifr, 0, sizeof(ifr));
3600
3601 ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
3602 rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
3603
3604 if (rc != 0 && cantflags != 0)
3605 ifp->if_flags ^= cantflags;
3606 }
3607
3608 return rc;
3609 }
3610
3611 int
3612 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
3613 {
3614 int rc;
3615 struct ifreq ifr;
3616
3617 /* There remain some paths that don't hold IFNET_LOCK yet */
3618 #ifdef NET_MPSAFE
3619 /* CARP and MROUTING still don't deal with the lock yet */
3620 #if (!defined(NCARP) || (NCARP == 0)) && !defined(MROUTING)
3621 KASSERT(IFNET_LOCKED(ifp));
3622 #endif
3623 #endif
3624 if (ifp->if_mcastop != NULL)
3625 rc = (*ifp->if_mcastop)(ifp, cmd, sa);
3626 else {
3627 ifreq_setaddr(cmd, &ifr, sa);
3628 rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
3629 }
3630
3631 return rc;
3632 }
3633
3634 static void
3635 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
3636 struct ifaltq *ifq)
3637 {
3638 const struct sysctlnode *cnode, *rnode;
3639
3640 if (sysctl_createv(clog, 0, NULL, &rnode,
3641 CTLFLAG_PERMANENT,
3642 CTLTYPE_NODE, "interfaces",
3643 SYSCTL_DESCR("Per-interface controls"),
3644 NULL, 0, NULL, 0,
3645 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
3646 goto bad;
3647
3648 if (sysctl_createv(clog, 0, &rnode, &rnode,
3649 CTLFLAG_PERMANENT,
3650 CTLTYPE_NODE, ifname,
3651 SYSCTL_DESCR("Interface controls"),
3652 NULL, 0, NULL, 0,
3653 CTL_CREATE, CTL_EOL) != 0)
3654 goto bad;
3655
3656 if (sysctl_createv(clog, 0, &rnode, &rnode,
3657 CTLFLAG_PERMANENT,
3658 CTLTYPE_NODE, "sndq",
3659 SYSCTL_DESCR("Interface output queue controls"),
3660 NULL, 0, NULL, 0,
3661 CTL_CREATE, CTL_EOL) != 0)
3662 goto bad;
3663
3664 if (sysctl_createv(clog, 0, &rnode, &cnode,
3665 CTLFLAG_PERMANENT,
3666 CTLTYPE_INT, "len",
3667 SYSCTL_DESCR("Current output queue length"),
3668 NULL, 0, &ifq->ifq_len, 0,
3669 CTL_CREATE, CTL_EOL) != 0)
3670 goto bad;
3671
3672 if (sysctl_createv(clog, 0, &rnode, &cnode,
3673 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3674 CTLTYPE_INT, "maxlen",
3675 SYSCTL_DESCR("Maximum allowed output queue length"),
3676 NULL, 0, &ifq->ifq_maxlen, 0,
3677 CTL_CREATE, CTL_EOL) != 0)
3678 goto bad;
3679
3680 if (sysctl_createv(clog, 0, &rnode, &cnode,
3681 CTLFLAG_PERMANENT,
3682 CTLTYPE_INT, "drops",
3683 SYSCTL_DESCR("Packets dropped due to full output queue"),
3684 NULL, 0, &ifq->ifq_drops, 0,
3685 CTL_CREATE, CTL_EOL) != 0)
3686 goto bad;
3687
3688 return;
3689 bad:
3690 printf("%s: could not attach sysctl nodes\n", ifname);
3691 return;
3692 }
3693
3694 #if defined(INET) || defined(INET6)
3695
3696 #define SYSCTL_NET_PKTQ(q, cn, c) \
3697 static int \
3698 sysctl_net_##q##_##cn(SYSCTLFN_ARGS) \
3699 { \
3700 return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c); \
3701 }
3702
3703 #if defined(INET)
3704 static int
3705 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
3706 {
3707 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
3708 }
3709 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
3710 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
3711 #endif
3712
3713 #if defined(INET6)
3714 static int
3715 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
3716 {
3717 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
3718 }
3719 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
3720 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
3721 #endif
3722
3723 static void
3724 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
3725 {
3726 sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
3727 const char *pfname = NULL, *ipname = NULL;
3728 int ipn = 0, qid = 0;
3729
3730 switch (pf) {
3731 #if defined(INET)
3732 case PF_INET:
3733 len_func = sysctl_net_ip_pktq_items;
3734 maxlen_func = sysctl_net_ip_pktq_maxlen;
3735 drops_func = sysctl_net_ip_pktq_drops;
3736 pfname = "inet", ipn = IPPROTO_IP;
3737 ipname = "ip", qid = IPCTL_IFQ;
3738 break;
3739 #endif
3740 #if defined(INET6)
3741 case PF_INET6:
3742 len_func = sysctl_net_ip6_pktq_items;
3743 maxlen_func = sysctl_net_ip6_pktq_maxlen;
3744 drops_func = sysctl_net_ip6_pktq_drops;
3745 pfname = "inet6", ipn = IPPROTO_IPV6;
3746 ipname = "ip6", qid = IPV6CTL_IFQ;
3747 break;
3748 #endif
3749 default:
3750 KASSERT(false);
3751 }
3752
3753 sysctl_createv(clog, 0, NULL, NULL,
3754 CTLFLAG_PERMANENT,
3755 CTLTYPE_NODE, pfname, NULL,
3756 NULL, 0, NULL, 0,
3757 CTL_NET, pf, CTL_EOL);
3758 sysctl_createv(clog, 0, NULL, NULL,
3759 CTLFLAG_PERMANENT,
3760 CTLTYPE_NODE, ipname, NULL,
3761 NULL, 0, NULL, 0,
3762 CTL_NET, pf, ipn, CTL_EOL);
3763 sysctl_createv(clog, 0, NULL, NULL,
3764 CTLFLAG_PERMANENT,
3765 CTLTYPE_NODE, "ifq",
3766 SYSCTL_DESCR("Protocol input queue controls"),
3767 NULL, 0, NULL, 0,
3768 CTL_NET, pf, ipn, qid, CTL_EOL);
3769
3770 sysctl_createv(clog, 0, NULL, NULL,
3771 CTLFLAG_PERMANENT,
3772 CTLTYPE_QUAD, "len",
3773 SYSCTL_DESCR("Current input queue length"),
3774 len_func, 0, NULL, 0,
3775 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
3776 sysctl_createv(clog, 0, NULL, NULL,
3777 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3778 CTLTYPE_INT, "maxlen",
3779 SYSCTL_DESCR("Maximum allowed input queue length"),
3780 maxlen_func, 0, NULL, 0,
3781 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
3782 sysctl_createv(clog, 0, NULL, NULL,
3783 CTLFLAG_PERMANENT,
3784 CTLTYPE_QUAD, "drops",
3785 SYSCTL_DESCR("Packets dropped due to full input queue"),
3786 drops_func, 0, NULL, 0,
3787 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
3788 }
3789 #endif /* INET || INET6 */
3790
3791 static int
3792 if_sdl_sysctl(SYSCTLFN_ARGS)
3793 {
3794 struct ifnet *ifp;
3795 const struct sockaddr_dl *sdl;
3796 struct psref psref;
3797 int error = 0;
3798 int bound;
3799
3800 if (namelen != 1)
3801 return EINVAL;
3802
3803 bound = curlwp_bind();
3804 ifp = if_get_byindex(name[0], &psref);
3805 if (ifp == NULL) {
3806 error = ENODEV;
3807 goto out0;
3808 }
3809
3810 sdl = ifp->if_sadl;
3811 if (sdl == NULL) {
3812 *oldlenp = 0;
3813 goto out1;
3814 }
3815
3816 if (oldp == NULL) {
3817 *oldlenp = sdl->sdl_alen;
3818 goto out1;
3819 }
3820
3821 if (*oldlenp >= sdl->sdl_alen)
3822 *oldlenp = sdl->sdl_alen;
3823 error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
3824 out1:
3825 if_put(ifp, &psref);
3826 out0:
3827 curlwp_bindx(bound);
3828 return error;
3829 }
3830
3831 static void
3832 if_sysctl_setup(struct sysctllog **clog)
3833 {
3834 const struct sysctlnode *rnode = NULL;
3835
3836 sysctl_createv(clog, 0, NULL, &rnode,
3837 CTLFLAG_PERMANENT,
3838 CTLTYPE_NODE, "sdl",
3839 SYSCTL_DESCR("Get active link-layer address"),
3840 if_sdl_sysctl, 0, NULL, 0,
3841 CTL_NET, CTL_CREATE, CTL_EOL);
3842
3843 #if defined(INET)
3844 sysctl_net_pktq_setup(NULL, PF_INET);
3845 #endif
3846 #ifdef INET6
3847 if (in6_present)
3848 sysctl_net_pktq_setup(NULL, PF_INET6);
3849 #endif
3850 }
3851