if.c revision 1.433 1 /* $NetBSD: if.c,v 1.433 2018/08/10 10:31:01 knakahara Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William Studenmund and Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 /*
62 * Copyright (c) 1980, 1986, 1993
63 * The Regents of the University of California. All rights reserved.
64 *
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
67 * are met:
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. Neither the name of the University nor the names of its contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 *
89 * @(#)if.c 8.5 (Berkeley) 1/9/95
90 */
91
92 #include <sys/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.433 2018/08/10 10:31:01 knakahara 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 return;
2330 }
2331
2332 #ifdef DEBUG
2333 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
2334 link_state == LINK_STATE_UP ? "UP" :
2335 link_state == LINK_STATE_DOWN ? "DOWN" :
2336 "UNKNOWN",
2337 ifp->if_link_state == LINK_STATE_UP ? "UP" :
2338 ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
2339 "UNKNOWN");
2340 #endif
2341
2342 /*
2343 * When going from UNKNOWN to UP, we need to mark existing
2344 * addresses as tentative and restart DAD as we may have
2345 * erroneously not found a duplicate.
2346 *
2347 * This needs to happen before rt_ifmsg to avoid a race where
2348 * listeners would have an address and expect it to work right
2349 * away.
2350 */
2351 notify = (link_state == LINK_STATE_UP &&
2352 ifp->if_link_state == LINK_STATE_UNKNOWN);
2353 ifp->if_link_state = link_state;
2354 /* The following routines may sleep so release the spin mutex */
2355 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2356
2357 KERNEL_LOCK_UNLESS_NET_MPSAFE();
2358 if (notify) {
2359 DOMAIN_FOREACH(dp) {
2360 if (dp->dom_if_link_state_change != NULL)
2361 dp->dom_if_link_state_change(ifp,
2362 LINK_STATE_DOWN);
2363 }
2364 }
2365
2366 /* Notify that the link state has changed. */
2367 rt_ifmsg(ifp);
2368
2369 #if NCARP > 0
2370 if (ifp->if_carp)
2371 carp_carpdev_state(ifp);
2372 #endif
2373
2374 DOMAIN_FOREACH(dp) {
2375 if (dp->dom_if_link_state_change != NULL)
2376 dp->dom_if_link_state_change(ifp, link_state);
2377 }
2378 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2379 splx(s);
2380 }
2381
2382 /*
2383 * Process the interface link state change queue.
2384 */
2385 static void
2386 if_link_state_change_si(void *arg)
2387 {
2388 struct ifnet *ifp = arg;
2389 int s;
2390 uint8_t state;
2391 bool schedule;
2392
2393 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
2394 s = splnet();
2395
2396 /* Pop a link state change from the queue and process it. */
2397 IF_LINK_STATE_CHANGE_LOCK(ifp);
2398 LQ_POP(ifp->if_link_queue, state);
2399 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2400
2401 if_link_state_change_softint(ifp, state);
2402
2403 /* If there is a link state change to come, schedule it. */
2404 IF_LINK_STATE_CHANGE_LOCK(ifp);
2405 schedule = (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET);
2406 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2407 if (schedule)
2408 softint_schedule(ifp->if_link_si);
2409
2410 splx(s);
2411 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2412 }
2413
2414 /*
2415 * Default action when installing a local route on a point-to-point
2416 * interface.
2417 */
2418 void
2419 p2p_rtrequest(int req, struct rtentry *rt,
2420 __unused const struct rt_addrinfo *info)
2421 {
2422 struct ifnet *ifp = rt->rt_ifp;
2423 struct ifaddr *ifa, *lo0ifa;
2424 int s = pserialize_read_enter();
2425
2426 switch (req) {
2427 case RTM_ADD:
2428 if ((rt->rt_flags & RTF_LOCAL) == 0)
2429 break;
2430
2431 rt->rt_ifp = lo0ifp;
2432
2433 IFADDR_READER_FOREACH(ifa, ifp) {
2434 if (equal(rt_getkey(rt), ifa->ifa_addr))
2435 break;
2436 }
2437 if (ifa == NULL)
2438 break;
2439
2440 /*
2441 * Ensure lo0 has an address of the same family.
2442 */
2443 IFADDR_READER_FOREACH(lo0ifa, lo0ifp) {
2444 if (lo0ifa->ifa_addr->sa_family ==
2445 ifa->ifa_addr->sa_family)
2446 break;
2447 }
2448 if (lo0ifa == NULL)
2449 break;
2450
2451 /*
2452 * Make sure to set rt->rt_ifa to the interface
2453 * address we are using, otherwise we will have trouble
2454 * with source address selection.
2455 */
2456 if (ifa != rt->rt_ifa)
2457 rt_replace_ifa(rt, ifa);
2458 break;
2459 case RTM_DELETE:
2460 default:
2461 break;
2462 }
2463 pserialize_read_exit(s);
2464 }
2465
2466 static void
2467 _if_down(struct ifnet *ifp)
2468 {
2469 struct ifaddr *ifa;
2470 struct domain *dp;
2471 int s, bound;
2472 struct psref psref;
2473
2474 ifp->if_flags &= ~IFF_UP;
2475 nanotime(&ifp->if_lastchange);
2476
2477 bound = curlwp_bind();
2478 s = pserialize_read_enter();
2479 IFADDR_READER_FOREACH(ifa, ifp) {
2480 ifa_acquire(ifa, &psref);
2481 pserialize_read_exit(s);
2482
2483 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2484
2485 s = pserialize_read_enter();
2486 ifa_release(ifa, &psref);
2487 }
2488 pserialize_read_exit(s);
2489 curlwp_bindx(bound);
2490
2491 IFQ_PURGE(&ifp->if_snd);
2492 #if NCARP > 0
2493 if (ifp->if_carp)
2494 carp_carpdev_state(ifp);
2495 #endif
2496 rt_ifmsg(ifp);
2497 DOMAIN_FOREACH(dp) {
2498 if (dp->dom_if_down)
2499 dp->dom_if_down(ifp);
2500 }
2501 }
2502
2503 static void
2504 if_down_deactivated(struct ifnet *ifp)
2505 {
2506
2507 KASSERT(if_is_deactivated(ifp));
2508 _if_down(ifp);
2509 }
2510
2511 void
2512 if_down_locked(struct ifnet *ifp)
2513 {
2514
2515 KASSERT(IFNET_LOCKED(ifp));
2516 _if_down(ifp);
2517 }
2518
2519 /*
2520 * Mark an interface down and notify protocols of
2521 * the transition.
2522 * NOTE: must be called at splsoftnet or equivalent.
2523 */
2524 void
2525 if_down(struct ifnet *ifp)
2526 {
2527
2528 IFNET_LOCK(ifp);
2529 if_down_locked(ifp);
2530 IFNET_UNLOCK(ifp);
2531 }
2532
2533 /*
2534 * Must be called with holding if_ioctl_lock.
2535 */
2536 static void
2537 if_up_locked(struct ifnet *ifp)
2538 {
2539 #ifdef notyet
2540 struct ifaddr *ifa;
2541 #endif
2542 struct domain *dp;
2543
2544 KASSERT(IFNET_LOCKED(ifp));
2545
2546 KASSERT(!if_is_deactivated(ifp));
2547 ifp->if_flags |= IFF_UP;
2548 nanotime(&ifp->if_lastchange);
2549 #ifdef notyet
2550 /* this has no effect on IP, and will kill all ISO connections XXX */
2551 IFADDR_READER_FOREACH(ifa, ifp)
2552 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2553 #endif
2554 #if NCARP > 0
2555 if (ifp->if_carp)
2556 carp_carpdev_state(ifp);
2557 #endif
2558 rt_ifmsg(ifp);
2559 DOMAIN_FOREACH(dp) {
2560 if (dp->dom_if_up)
2561 dp->dom_if_up(ifp);
2562 }
2563 }
2564
2565 /*
2566 * Handle interface slowtimo timer routine. Called
2567 * from softclock, we decrement timer (if set) and
2568 * call the appropriate interface routine on expiration.
2569 */
2570 static void
2571 if_slowtimo(void *arg)
2572 {
2573 void (*slowtimo)(struct ifnet *);
2574 struct ifnet *ifp = arg;
2575 int s;
2576
2577 slowtimo = ifp->if_slowtimo;
2578 if (__predict_false(slowtimo == NULL))
2579 return;
2580
2581 s = splnet();
2582 if (ifp->if_timer != 0 && --ifp->if_timer == 0)
2583 (*slowtimo)(ifp);
2584
2585 splx(s);
2586
2587 if (__predict_true(ifp->if_slowtimo != NULL))
2588 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
2589 }
2590
2591 /*
2592 * Mark an interface up and notify protocols of
2593 * the transition.
2594 * NOTE: must be called at splsoftnet or equivalent.
2595 */
2596 void
2597 if_up(struct ifnet *ifp)
2598 {
2599
2600 IFNET_LOCK(ifp);
2601 if_up_locked(ifp);
2602 IFNET_UNLOCK(ifp);
2603 }
2604
2605 /*
2606 * Set/clear promiscuous mode on interface ifp based on the truth value
2607 * of pswitch. The calls are reference counted so that only the first
2608 * "on" request actually has an effect, as does the final "off" request.
2609 * Results are undefined if the "off" and "on" requests are not matched.
2610 */
2611 int
2612 ifpromisc_locked(struct ifnet *ifp, int pswitch)
2613 {
2614 int pcount, ret = 0;
2615 short nflags;
2616
2617 KASSERT(IFNET_LOCKED(ifp));
2618
2619 pcount = ifp->if_pcount;
2620 if (pswitch) {
2621 /*
2622 * Allow the device to be "placed" into promiscuous
2623 * mode even if it is not configured up. It will
2624 * consult IFF_PROMISC when it is brought up.
2625 */
2626 if (ifp->if_pcount++ != 0)
2627 goto out;
2628 nflags = ifp->if_flags | IFF_PROMISC;
2629 } else {
2630 if (--ifp->if_pcount > 0)
2631 goto out;
2632 nflags = ifp->if_flags & ~IFF_PROMISC;
2633 }
2634 ret = if_flags_set(ifp, nflags);
2635 /* Restore interface state if not successful. */
2636 if (ret != 0) {
2637 ifp->if_pcount = pcount;
2638 }
2639 out:
2640 return ret;
2641 }
2642
2643 int
2644 ifpromisc(struct ifnet *ifp, int pswitch)
2645 {
2646 int e;
2647
2648 IFNET_LOCK(ifp);
2649 e = ifpromisc_locked(ifp, pswitch);
2650 IFNET_UNLOCK(ifp);
2651
2652 return e;
2653 }
2654
2655 /*
2656 * Map interface name to
2657 * interface structure pointer.
2658 */
2659 struct ifnet *
2660 ifunit(const char *name)
2661 {
2662 struct ifnet *ifp;
2663 const char *cp = name;
2664 u_int unit = 0;
2665 u_int i;
2666 int s;
2667
2668 /*
2669 * If the entire name is a number, treat it as an ifindex.
2670 */
2671 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2672 unit = unit * 10 + (*cp - '0');
2673 }
2674
2675 /*
2676 * If the number took all of the name, then it's a valid ifindex.
2677 */
2678 if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
2679 return if_byindex(unit);
2680
2681 ifp = NULL;
2682 s = pserialize_read_enter();
2683 IFNET_READER_FOREACH(ifp) {
2684 if (if_is_deactivated(ifp))
2685 continue;
2686 if (strcmp(ifp->if_xname, name) == 0)
2687 goto out;
2688 }
2689 out:
2690 pserialize_read_exit(s);
2691 return ifp;
2692 }
2693
2694 /*
2695 * Get a reference of an ifnet object by an interface name.
2696 * The returned reference is protected by psref(9). The caller
2697 * must release a returned reference by if_put after use.
2698 */
2699 struct ifnet *
2700 if_get(const char *name, struct psref *psref)
2701 {
2702 struct ifnet *ifp;
2703 const char *cp = name;
2704 u_int unit = 0;
2705 u_int i;
2706 int s;
2707
2708 /*
2709 * If the entire name is a number, treat it as an ifindex.
2710 */
2711 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2712 unit = unit * 10 + (*cp - '0');
2713 }
2714
2715 /*
2716 * If the number took all of the name, then it's a valid ifindex.
2717 */
2718 if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
2719 return if_get_byindex(unit, psref);
2720
2721 ifp = NULL;
2722 s = pserialize_read_enter();
2723 IFNET_READER_FOREACH(ifp) {
2724 if (if_is_deactivated(ifp))
2725 continue;
2726 if (strcmp(ifp->if_xname, name) == 0) {
2727 psref_acquire(psref, &ifp->if_psref,
2728 ifnet_psref_class);
2729 goto out;
2730 }
2731 }
2732 out:
2733 pserialize_read_exit(s);
2734 return ifp;
2735 }
2736
2737 /*
2738 * Release a reference of an ifnet object given by if_get, if_get_byindex
2739 * or if_get_bylla.
2740 */
2741 void
2742 if_put(const struct ifnet *ifp, struct psref *psref)
2743 {
2744
2745 if (ifp == NULL)
2746 return;
2747
2748 psref_release(psref, &ifp->if_psref, ifnet_psref_class);
2749 }
2750
2751 /*
2752 * Return ifp having idx. Return NULL if not found. Normally if_byindex
2753 * should be used.
2754 */
2755 ifnet_t *
2756 _if_byindex(u_int idx)
2757 {
2758
2759 return (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
2760 }
2761
2762 /*
2763 * Return ifp having idx. Return NULL if not found or the found ifp is
2764 * already deactivated.
2765 */
2766 ifnet_t *
2767 if_byindex(u_int idx)
2768 {
2769 ifnet_t *ifp;
2770
2771 ifp = _if_byindex(idx);
2772 if (ifp != NULL && if_is_deactivated(ifp))
2773 ifp = NULL;
2774 return ifp;
2775 }
2776
2777 /*
2778 * Get a reference of an ifnet object by an interface index.
2779 * The returned reference is protected by psref(9). The caller
2780 * must release a returned reference by if_put after use.
2781 */
2782 ifnet_t *
2783 if_get_byindex(u_int idx, struct psref *psref)
2784 {
2785 ifnet_t *ifp;
2786 int s;
2787
2788 s = pserialize_read_enter();
2789 ifp = if_byindex(idx);
2790 if (__predict_true(ifp != NULL))
2791 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
2792 pserialize_read_exit(s);
2793
2794 return ifp;
2795 }
2796
2797 ifnet_t *
2798 if_get_bylla(const void *lla, unsigned char lla_len, struct psref *psref)
2799 {
2800 ifnet_t *ifp;
2801 int s;
2802
2803 s = pserialize_read_enter();
2804 IFNET_READER_FOREACH(ifp) {
2805 if (if_is_deactivated(ifp))
2806 continue;
2807 if (ifp->if_addrlen != lla_len)
2808 continue;
2809 if (memcmp(lla, CLLADDR(ifp->if_sadl), lla_len) == 0) {
2810 psref_acquire(psref, &ifp->if_psref,
2811 ifnet_psref_class);
2812 break;
2813 }
2814 }
2815 pserialize_read_exit(s);
2816
2817 return ifp;
2818 }
2819
2820 /*
2821 * Note that it's safe only if the passed ifp is guaranteed to not be freed,
2822 * for example using pserialize or the ifp is already held or some other
2823 * object is held which guarantes the ifp to not be freed indirectly.
2824 */
2825 void
2826 if_acquire(struct ifnet *ifp, struct psref *psref)
2827 {
2828
2829 KASSERT(ifp->if_index != 0);
2830 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
2831 }
2832
2833 bool
2834 if_held(struct ifnet *ifp)
2835 {
2836
2837 return psref_held(&ifp->if_psref, ifnet_psref_class);
2838 }
2839
2840 /*
2841 * Some tunnel interfaces can nest, e.g. IPv4 over IPv4 gif(4) tunnel over IPv4.
2842 * Check the tunnel nesting count.
2843 * Return > 0, if tunnel nesting count is more than limit.
2844 * Return 0, if tunnel nesting count is equal or less than limit.
2845 */
2846 int
2847 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, int limit)
2848 {
2849 struct m_tag *mtag;
2850 int *count;
2851
2852 mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO, NULL);
2853 if (mtag != NULL) {
2854 count = (int *)(mtag + 1);
2855 if (++(*count) > limit) {
2856 log(LOG_NOTICE,
2857 "%s: recursively called too many times(%d)\n",
2858 ifp->if_xname, *count);
2859 return EIO;
2860 }
2861 } else {
2862 mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count),
2863 M_NOWAIT);
2864 if (mtag != NULL) {
2865 m_tag_prepend(m, mtag);
2866 count = (int *)(mtag + 1);
2867 *count = 0;
2868 } else {
2869 log(LOG_DEBUG,
2870 "%s: m_tag_get() failed, recursion calls are not prevented.\n",
2871 ifp->if_xname);
2872 }
2873 }
2874
2875 return 0;
2876 }
2877
2878 /* common */
2879 int
2880 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
2881 {
2882 int s;
2883 struct ifreq *ifr;
2884 struct ifcapreq *ifcr;
2885 struct ifdatareq *ifdr;
2886
2887 switch (cmd) {
2888 case SIOCSIFCAP:
2889 ifcr = data;
2890 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
2891 return EINVAL;
2892
2893 if (ifcr->ifcr_capenable == ifp->if_capenable)
2894 return 0;
2895
2896 ifp->if_capenable = ifcr->ifcr_capenable;
2897
2898 /* Pre-compute the checksum flags mask. */
2899 ifp->if_csum_flags_tx = 0;
2900 ifp->if_csum_flags_rx = 0;
2901 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
2902 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
2903 }
2904 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
2905 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
2906 }
2907
2908 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
2909 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
2910 }
2911 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
2912 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
2913 }
2914
2915 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
2916 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
2917 }
2918 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
2919 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
2920 }
2921
2922 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
2923 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
2924 }
2925 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
2926 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
2927 }
2928
2929 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
2930 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
2931 }
2932 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
2933 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
2934 }
2935 if (ifp->if_flags & IFF_UP)
2936 return ENETRESET;
2937 return 0;
2938 case SIOCSIFFLAGS:
2939 ifr = data;
2940 /*
2941 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here, but if_up
2942 * and if_down aren't MP-safe yet, so we must hold the lock.
2943 */
2944 KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
2945 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
2946 s = splsoftnet();
2947 if_down_locked(ifp);
2948 splx(s);
2949 }
2950 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
2951 s = splsoftnet();
2952 if_up_locked(ifp);
2953 splx(s);
2954 }
2955 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
2956 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2957 (ifr->ifr_flags &~ IFF_CANTCHANGE);
2958 break;
2959 case SIOCGIFFLAGS:
2960 ifr = data;
2961 ifr->ifr_flags = ifp->if_flags;
2962 break;
2963
2964 case SIOCGIFMETRIC:
2965 ifr = data;
2966 ifr->ifr_metric = ifp->if_metric;
2967 break;
2968
2969 case SIOCGIFMTU:
2970 ifr = data;
2971 ifr->ifr_mtu = ifp->if_mtu;
2972 break;
2973
2974 case SIOCGIFDLT:
2975 ifr = data;
2976 ifr->ifr_dlt = ifp->if_dlt;
2977 break;
2978
2979 case SIOCGIFCAP:
2980 ifcr = data;
2981 ifcr->ifcr_capabilities = ifp->if_capabilities;
2982 ifcr->ifcr_capenable = ifp->if_capenable;
2983 break;
2984
2985 case SIOCSIFMETRIC:
2986 ifr = data;
2987 ifp->if_metric = ifr->ifr_metric;
2988 break;
2989
2990 case SIOCGIFDATA:
2991 ifdr = data;
2992 ifdr->ifdr_data = ifp->if_data;
2993 break;
2994
2995 case SIOCGIFINDEX:
2996 ifr = data;
2997 ifr->ifr_index = ifp->if_index;
2998 break;
2999
3000 case SIOCZIFDATA:
3001 ifdr = data;
3002 ifdr->ifdr_data = ifp->if_data;
3003 /*
3004 * Assumes that the volatile counters that can be
3005 * zero'ed are at the end of if_data.
3006 */
3007 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
3008 offsetof(struct if_data, ifi_ipackets));
3009 /*
3010 * The memset() clears to the bottm of if_data. In the area,
3011 * if_lastchange is included. Please be careful if new entry
3012 * will be added into if_data or rewite this.
3013 *
3014 * And also, update if_lastchnage.
3015 */
3016 getnanotime(&ifp->if_lastchange);
3017 break;
3018 case SIOCSIFMTU:
3019 ifr = data;
3020 if (ifp->if_mtu == ifr->ifr_mtu)
3021 break;
3022 ifp->if_mtu = ifr->ifr_mtu;
3023 /*
3024 * If the link MTU changed, do network layer specific procedure.
3025 */
3026 #ifdef INET6
3027 KERNEL_LOCK_UNLESS_NET_MPSAFE();
3028 if (in6_present)
3029 nd6_setmtu(ifp);
3030 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
3031 #endif
3032 return ENETRESET;
3033 default:
3034 return ENOTTY;
3035 }
3036 return 0;
3037 }
3038
3039 int
3040 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
3041 {
3042 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
3043 struct ifaddr *ifa;
3044 const struct sockaddr *any, *sa;
3045 union {
3046 struct sockaddr sa;
3047 struct sockaddr_storage ss;
3048 } u, v;
3049 int s, error = 0;
3050
3051 switch (cmd) {
3052 case SIOCSIFADDRPREF:
3053 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
3054 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
3055 NULL) != 0)
3056 return EPERM;
3057 case SIOCGIFADDRPREF:
3058 break;
3059 default:
3060 return EOPNOTSUPP;
3061 }
3062
3063 /* sanity checks */
3064 if (data == NULL || ifp == NULL) {
3065 panic("invalid argument to %s", __func__);
3066 /*NOTREACHED*/
3067 }
3068
3069 /* address must be specified on ADD and DELETE */
3070 sa = sstocsa(&ifap->ifap_addr);
3071 if (sa->sa_family != sofamily(so))
3072 return EINVAL;
3073 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
3074 return EINVAL;
3075
3076 sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
3077
3078 s = pserialize_read_enter();
3079 IFADDR_READER_FOREACH(ifa, ifp) {
3080 if (ifa->ifa_addr->sa_family != sa->sa_family)
3081 continue;
3082 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
3083 if (sockaddr_cmp(&u.sa, &v.sa) == 0)
3084 break;
3085 }
3086 if (ifa == NULL) {
3087 error = EADDRNOTAVAIL;
3088 goto out;
3089 }
3090
3091 switch (cmd) {
3092 case SIOCSIFADDRPREF:
3093 ifa->ifa_preference = ifap->ifap_preference;
3094 goto out;
3095 case SIOCGIFADDRPREF:
3096 /* fill in the if_laddrreq structure */
3097 (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
3098 sizeof(ifap->ifap_addr), ifa->ifa_addr);
3099 ifap->ifap_preference = ifa->ifa_preference;
3100 goto out;
3101 default:
3102 error = EOPNOTSUPP;
3103 }
3104 out:
3105 pserialize_read_exit(s);
3106 return error;
3107 }
3108
3109 /*
3110 * Interface ioctls.
3111 */
3112 static int
3113 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
3114 {
3115 struct ifnet *ifp;
3116 struct ifreq *ifr;
3117 int error = 0;
3118 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
3119 u_long ocmd = cmd;
3120 #endif
3121 short oif_flags;
3122 #ifdef COMPAT_OIFREQ
3123 struct ifreq ifrb;
3124 struct oifreq *oifr = NULL;
3125 #endif
3126 int r;
3127 struct psref psref;
3128 int bound;
3129
3130 switch (cmd) {
3131 case SIOCGIFCONF:
3132 return ifconf(cmd, data);
3133 case SIOCINITIFADDR:
3134 return EPERM;
3135 default:
3136 error = (*vec_compat_ifconf)(l, cmd, data);
3137 if (error != ENOSYS)
3138 return error;
3139 error = (*vec_compat_ifdatareq)(l, cmd, data);
3140 if (error != ENOSYS)
3141 return error;
3142 break;
3143 }
3144
3145 ifr = data;
3146 #ifdef COMPAT_OIFREQ
3147 if (vec_compat_cvtcmd) {
3148 cmd = (*vec_compat_cvtcmd)(cmd);
3149 if (cmd != ocmd) {
3150 oifr = data;
3151 data = ifr = &ifrb;
3152 ifreqo2n(oifr, ifr);
3153 }
3154 }
3155 #endif
3156
3157 switch (cmd) {
3158 case SIOCIFCREATE:
3159 case SIOCIFDESTROY:
3160 bound = curlwp_bind();
3161 if (l != NULL) {
3162 ifp = if_get(ifr->ifr_name, &psref);
3163 error = kauth_authorize_network(l->l_cred,
3164 KAUTH_NETWORK_INTERFACE,
3165 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
3166 (void *)cmd, NULL);
3167 if (ifp != NULL)
3168 if_put(ifp, &psref);
3169 if (error != 0) {
3170 curlwp_bindx(bound);
3171 return error;
3172 }
3173 }
3174 KERNEL_LOCK_UNLESS_NET_MPSAFE();
3175 mutex_enter(&if_clone_mtx);
3176 r = (cmd == SIOCIFCREATE) ?
3177 if_clone_create(ifr->ifr_name) :
3178 if_clone_destroy(ifr->ifr_name);
3179 mutex_exit(&if_clone_mtx);
3180 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
3181 curlwp_bindx(bound);
3182 return r;
3183
3184 case SIOCIFGCLONERS:
3185 {
3186 struct if_clonereq *req = (struct if_clonereq *)data;
3187 return if_clone_list(req->ifcr_count, req->ifcr_buffer,
3188 &req->ifcr_total);
3189 }
3190 }
3191
3192 bound = curlwp_bind();
3193 ifp = if_get(ifr->ifr_name, &psref);
3194 if (ifp == NULL) {
3195 curlwp_bindx(bound);
3196 return ENXIO;
3197 }
3198
3199 switch (cmd) {
3200 case SIOCALIFADDR:
3201 case SIOCDLIFADDR:
3202 case SIOCSIFADDRPREF:
3203 case SIOCSIFFLAGS:
3204 case SIOCSIFCAP:
3205 case SIOCSIFMETRIC:
3206 case SIOCZIFDATA:
3207 case SIOCSIFMTU:
3208 case SIOCSIFPHYADDR:
3209 case SIOCDIFPHYADDR:
3210 #ifdef INET6
3211 case SIOCSIFPHYADDR_IN6:
3212 #endif
3213 case SIOCSLIFPHYADDR:
3214 case SIOCADDMULTI:
3215 case SIOCDELMULTI:
3216 case SIOCSIFMEDIA:
3217 case SIOCSDRVSPEC:
3218 case SIOCG80211:
3219 case SIOCS80211:
3220 case SIOCS80211NWID:
3221 case SIOCS80211NWKEY:
3222 case SIOCS80211POWER:
3223 case SIOCS80211BSSID:
3224 case SIOCS80211CHANNEL:
3225 case SIOCSLINKSTR:
3226 if (l != NULL) {
3227 error = kauth_authorize_network(l->l_cred,
3228 KAUTH_NETWORK_INTERFACE,
3229 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
3230 (void *)cmd, NULL);
3231 if (error != 0)
3232 goto out;
3233 }
3234 }
3235
3236 oif_flags = ifp->if_flags;
3237
3238 KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp);
3239 IFNET_LOCK(ifp);
3240
3241 error = (*ifp->if_ioctl)(ifp, cmd, data);
3242 if (error != ENOTTY)
3243 ;
3244 else if (so->so_proto == NULL)
3245 error = EOPNOTSUPP;
3246 else {
3247 KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
3248 #ifdef COMPAT_OSOCK
3249 if (vec_compat_ifioctl != NULL)
3250 error = (*vec_compat_ifioctl)(so, ocmd, cmd, data, l);
3251 else
3252 #endif
3253 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
3254 cmd, data, ifp);
3255 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
3256 }
3257
3258 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
3259 if ((ifp->if_flags & IFF_UP) != 0) {
3260 int s = splsoftnet();
3261 if_up_locked(ifp);
3262 splx(s);
3263 }
3264 }
3265 #ifdef COMPAT_OIFREQ
3266 if (cmd != ocmd)
3267 ifreqn2o(oifr, ifr);
3268 #endif
3269
3270 IFNET_UNLOCK(ifp);
3271 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp);
3272 out:
3273 if_put(ifp, &psref);
3274 curlwp_bindx(bound);
3275 return error;
3276 }
3277
3278 /*
3279 * Return interface configuration
3280 * of system. List may be used
3281 * in later ioctl's (above) to get
3282 * other information.
3283 *
3284 * Each record is a struct ifreq. Before the addition of
3285 * sockaddr_storage, the API rule was that sockaddr flavors that did
3286 * not fit would extend beyond the struct ifreq, with the next struct
3287 * ifreq starting sa_len beyond the struct sockaddr. Because the
3288 * union in struct ifreq includes struct sockaddr_storage, every kind
3289 * of sockaddr must fit. Thus, there are no longer any overlength
3290 * records.
3291 *
3292 * Records are added to the user buffer if they fit, and ifc_len is
3293 * adjusted to the length that was written. Thus, the user is only
3294 * assured of getting the complete list if ifc_len on return is at
3295 * least sizeof(struct ifreq) less than it was on entry.
3296 *
3297 * If the user buffer pointer is NULL, this routine copies no data and
3298 * returns the amount of space that would be needed.
3299 *
3300 * Invariants:
3301 * ifrp points to the next part of the user's buffer to be used. If
3302 * ifrp != NULL, space holds the number of bytes remaining that we may
3303 * write at ifrp. Otherwise, space holds the number of bytes that
3304 * would have been written had there been adequate space.
3305 */
3306 /*ARGSUSED*/
3307 static int
3308 ifconf(u_long cmd, void *data)
3309 {
3310 struct ifconf *ifc = (struct ifconf *)data;
3311 struct ifnet *ifp;
3312 struct ifaddr *ifa;
3313 struct ifreq ifr, *ifrp = NULL;
3314 int space = 0, error = 0;
3315 const int sz = (int)sizeof(struct ifreq);
3316 const bool docopy = ifc->ifc_req != NULL;
3317 int s;
3318 int bound;
3319 struct psref psref;
3320
3321 if (docopy) {
3322 space = ifc->ifc_len;
3323 ifrp = ifc->ifc_req;
3324 }
3325
3326 bound = curlwp_bind();
3327 s = pserialize_read_enter();
3328 IFNET_READER_FOREACH(ifp) {
3329 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
3330 pserialize_read_exit(s);
3331
3332 (void)strncpy(ifr.ifr_name, ifp->if_xname,
3333 sizeof(ifr.ifr_name));
3334 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
3335 error = ENAMETOOLONG;
3336 goto release_exit;
3337 }
3338 if (IFADDR_READER_EMPTY(ifp)) {
3339 /* Interface with no addresses - send zero sockaddr. */
3340 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
3341 if (!docopy) {
3342 space += sz;
3343 goto next;
3344 }
3345 if (space >= sz) {
3346 error = copyout(&ifr, ifrp, sz);
3347 if (error != 0)
3348 goto release_exit;
3349 ifrp++;
3350 space -= sz;
3351 }
3352 }
3353
3354 s = pserialize_read_enter();
3355 IFADDR_READER_FOREACH(ifa, ifp) {
3356 struct sockaddr *sa = ifa->ifa_addr;
3357 /* all sockaddrs must fit in sockaddr_storage */
3358 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
3359
3360 if (!docopy) {
3361 space += sz;
3362 continue;
3363 }
3364 memcpy(&ifr.ifr_space, sa, sa->sa_len);
3365 pserialize_read_exit(s);
3366
3367 if (space >= sz) {
3368 error = copyout(&ifr, ifrp, sz);
3369 if (error != 0)
3370 goto release_exit;
3371 ifrp++; space -= sz;
3372 }
3373 s = pserialize_read_enter();
3374 }
3375 pserialize_read_exit(s);
3376
3377 next:
3378 s = pserialize_read_enter();
3379 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
3380 }
3381 pserialize_read_exit(s);
3382 curlwp_bindx(bound);
3383
3384 if (docopy) {
3385 KASSERT(0 <= space && space <= ifc->ifc_len);
3386 ifc->ifc_len -= space;
3387 } else {
3388 KASSERT(space >= 0);
3389 ifc->ifc_len = space;
3390 }
3391 return (0);
3392
3393 release_exit:
3394 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
3395 curlwp_bindx(bound);
3396 return error;
3397 }
3398
3399 int
3400 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
3401 {
3402 uint8_t len = sizeof(ifr->ifr_ifru.ifru_space);
3403 #ifdef COMPAT_OIFREQ
3404 struct ifreq ifrb;
3405 struct oifreq *oifr = NULL;
3406 u_long ocmd = cmd;
3407
3408 if (vec_compat_cvtcmd) {
3409 cmd = (*vec_compat_cvtcmd)(cmd);
3410 if (cmd != ocmd) {
3411 oifr = (struct oifreq *)(void *)ifr;
3412 ifr = &ifrb;
3413 ifreqo2n(oifr, ifr);
3414 len = sizeof(oifr->ifr_addr);
3415 }
3416 }
3417 #endif
3418 if (len < sa->sa_len)
3419 return EFBIG;
3420
3421 memset(&ifr->ifr_addr, 0, len);
3422 sockaddr_copy(&ifr->ifr_addr, len, sa);
3423
3424 #ifdef COMPAT_OIFREQ
3425 if (cmd != ocmd)
3426 ifreqn2o(oifr, ifr);
3427 #endif
3428 return 0;
3429 }
3430
3431 /*
3432 * wrapper function for the drivers which doesn't have if_transmit().
3433 */
3434 static int
3435 if_transmit(struct ifnet *ifp, struct mbuf *m)
3436 {
3437 int s, error;
3438 size_t pktlen = m->m_pkthdr.len;
3439 bool mcast = (m->m_flags & M_MCAST) != 0;
3440
3441 s = splnet();
3442
3443 IFQ_ENQUEUE(&ifp->if_snd, m, error);
3444 if (error != 0) {
3445 /* mbuf is already freed */
3446 goto out;
3447 }
3448
3449 ifp->if_obytes += pktlen;
3450 if (mcast)
3451 ifp->if_omcasts++;
3452
3453 if ((ifp->if_flags & IFF_OACTIVE) == 0)
3454 if_start_lock(ifp);
3455 out:
3456 splx(s);
3457
3458 return error;
3459 }
3460
3461 int
3462 if_transmit_lock(struct ifnet *ifp, struct mbuf *m)
3463 {
3464 int error;
3465
3466 #ifdef ALTQ
3467 KERNEL_LOCK(1, NULL);
3468 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
3469 error = if_transmit(ifp, m);
3470 KERNEL_UNLOCK_ONE(NULL);
3471 } else {
3472 KERNEL_UNLOCK_ONE(NULL);
3473 error = (*ifp->if_transmit)(ifp, m);
3474 /* mbuf is alredy freed */
3475 }
3476 #else /* !ALTQ */
3477 error = (*ifp->if_transmit)(ifp, m);
3478 /* mbuf is alredy freed */
3479 #endif /* !ALTQ */
3480
3481 return error;
3482 }
3483
3484 /*
3485 * Queue message on interface, and start output if interface
3486 * not yet active.
3487 */
3488 int
3489 ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
3490 {
3491
3492 return if_transmit_lock(ifp, m);
3493 }
3494
3495 /*
3496 * Queue message on interface, possibly using a second fast queue
3497 */
3498 int
3499 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
3500 {
3501 int error = 0;
3502
3503 if (ifq != NULL
3504 #ifdef ALTQ
3505 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
3506 #endif
3507 ) {
3508 if (IF_QFULL(ifq)) {
3509 IF_DROP(&ifp->if_snd);
3510 m_freem(m);
3511 if (error == 0)
3512 error = ENOBUFS;
3513 } else
3514 IF_ENQUEUE(ifq, m);
3515 } else
3516 IFQ_ENQUEUE(&ifp->if_snd, m, error);
3517 if (error != 0) {
3518 ++ifp->if_oerrors;
3519 return error;
3520 }
3521 return 0;
3522 }
3523
3524 int
3525 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
3526 {
3527 int rc;
3528
3529 KASSERT(IFNET_LOCKED(ifp));
3530 if (ifp->if_initaddr != NULL)
3531 rc = (*ifp->if_initaddr)(ifp, ifa, src);
3532 else if (src ||
3533 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
3534 rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
3535
3536 return rc;
3537 }
3538
3539 int
3540 if_do_dad(struct ifnet *ifp)
3541 {
3542 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
3543 return 0;
3544
3545 switch (ifp->if_type) {
3546 case IFT_FAITH:
3547 /*
3548 * These interfaces do not have the IFF_LOOPBACK flag,
3549 * but loop packets back. We do not have to do DAD on such
3550 * interfaces. We should even omit it, because loop-backed
3551 * responses would confuse the DAD procedure.
3552 */
3553 return 0;
3554 default:
3555 /*
3556 * Our DAD routine requires the interface up and running.
3557 * However, some interfaces can be up before the RUNNING
3558 * status. Additionaly, users may try to assign addresses
3559 * before the interface becomes up (or running).
3560 * We simply skip DAD in such a case as a work around.
3561 * XXX: we should rather mark "tentative" on such addresses,
3562 * and do DAD after the interface becomes ready.
3563 */
3564 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
3565 (IFF_UP|IFF_RUNNING))
3566 return 0;
3567
3568 return 1;
3569 }
3570 }
3571
3572 int
3573 if_flags_set(ifnet_t *ifp, const short flags)
3574 {
3575 int rc;
3576
3577 KASSERT(IFNET_LOCKED(ifp));
3578
3579 if (ifp->if_setflags != NULL)
3580 rc = (*ifp->if_setflags)(ifp, flags);
3581 else {
3582 short cantflags, chgdflags;
3583 struct ifreq ifr;
3584
3585 chgdflags = ifp->if_flags ^ flags;
3586 cantflags = chgdflags & IFF_CANTCHANGE;
3587
3588 if (cantflags != 0)
3589 ifp->if_flags ^= cantflags;
3590
3591 /* Traditionally, we do not call if_ioctl after
3592 * setting/clearing only IFF_PROMISC if the interface
3593 * isn't IFF_UP. Uphold that tradition.
3594 */
3595 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
3596 return 0;
3597
3598 memset(&ifr, 0, sizeof(ifr));
3599
3600 ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
3601 rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
3602
3603 if (rc != 0 && cantflags != 0)
3604 ifp->if_flags ^= cantflags;
3605 }
3606
3607 return rc;
3608 }
3609
3610 int
3611 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
3612 {
3613 int rc;
3614 struct ifreq ifr;
3615
3616 /* There remain some paths that don't hold IFNET_LOCK yet */
3617 #ifdef NET_MPSAFE
3618 /* CARP and MROUTING still don't deal with the lock yet */
3619 #if (!defined(NCARP) || (NCARP == 0)) && !defined(MROUTING)
3620 KASSERT(IFNET_LOCKED(ifp));
3621 #endif
3622 #endif
3623 if (ifp->if_mcastop != NULL)
3624 rc = (*ifp->if_mcastop)(ifp, cmd, sa);
3625 else {
3626 ifreq_setaddr(cmd, &ifr, sa);
3627 rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
3628 }
3629
3630 return rc;
3631 }
3632
3633 static void
3634 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
3635 struct ifaltq *ifq)
3636 {
3637 const struct sysctlnode *cnode, *rnode;
3638
3639 if (sysctl_createv(clog, 0, NULL, &rnode,
3640 CTLFLAG_PERMANENT,
3641 CTLTYPE_NODE, "interfaces",
3642 SYSCTL_DESCR("Per-interface controls"),
3643 NULL, 0, NULL, 0,
3644 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
3645 goto bad;
3646
3647 if (sysctl_createv(clog, 0, &rnode, &rnode,
3648 CTLFLAG_PERMANENT,
3649 CTLTYPE_NODE, ifname,
3650 SYSCTL_DESCR("Interface controls"),
3651 NULL, 0, NULL, 0,
3652 CTL_CREATE, CTL_EOL) != 0)
3653 goto bad;
3654
3655 if (sysctl_createv(clog, 0, &rnode, &rnode,
3656 CTLFLAG_PERMANENT,
3657 CTLTYPE_NODE, "sndq",
3658 SYSCTL_DESCR("Interface output queue controls"),
3659 NULL, 0, NULL, 0,
3660 CTL_CREATE, CTL_EOL) != 0)
3661 goto bad;
3662
3663 if (sysctl_createv(clog, 0, &rnode, &cnode,
3664 CTLFLAG_PERMANENT,
3665 CTLTYPE_INT, "len",
3666 SYSCTL_DESCR("Current output queue length"),
3667 NULL, 0, &ifq->ifq_len, 0,
3668 CTL_CREATE, CTL_EOL) != 0)
3669 goto bad;
3670
3671 if (sysctl_createv(clog, 0, &rnode, &cnode,
3672 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3673 CTLTYPE_INT, "maxlen",
3674 SYSCTL_DESCR("Maximum allowed output queue length"),
3675 NULL, 0, &ifq->ifq_maxlen, 0,
3676 CTL_CREATE, CTL_EOL) != 0)
3677 goto bad;
3678
3679 if (sysctl_createv(clog, 0, &rnode, &cnode,
3680 CTLFLAG_PERMANENT,
3681 CTLTYPE_INT, "drops",
3682 SYSCTL_DESCR("Packets dropped due to full output queue"),
3683 NULL, 0, &ifq->ifq_drops, 0,
3684 CTL_CREATE, CTL_EOL) != 0)
3685 goto bad;
3686
3687 return;
3688 bad:
3689 printf("%s: could not attach sysctl nodes\n", ifname);
3690 return;
3691 }
3692
3693 #if defined(INET) || defined(INET6)
3694
3695 #define SYSCTL_NET_PKTQ(q, cn, c) \
3696 static int \
3697 sysctl_net_##q##_##cn(SYSCTLFN_ARGS) \
3698 { \
3699 return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c); \
3700 }
3701
3702 #if defined(INET)
3703 static int
3704 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
3705 {
3706 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
3707 }
3708 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
3709 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
3710 #endif
3711
3712 #if defined(INET6)
3713 static int
3714 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
3715 {
3716 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
3717 }
3718 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
3719 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
3720 #endif
3721
3722 static void
3723 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
3724 {
3725 sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
3726 const char *pfname = NULL, *ipname = NULL;
3727 int ipn = 0, qid = 0;
3728
3729 switch (pf) {
3730 #if defined(INET)
3731 case PF_INET:
3732 len_func = sysctl_net_ip_pktq_items;
3733 maxlen_func = sysctl_net_ip_pktq_maxlen;
3734 drops_func = sysctl_net_ip_pktq_drops;
3735 pfname = "inet", ipn = IPPROTO_IP;
3736 ipname = "ip", qid = IPCTL_IFQ;
3737 break;
3738 #endif
3739 #if defined(INET6)
3740 case PF_INET6:
3741 len_func = sysctl_net_ip6_pktq_items;
3742 maxlen_func = sysctl_net_ip6_pktq_maxlen;
3743 drops_func = sysctl_net_ip6_pktq_drops;
3744 pfname = "inet6", ipn = IPPROTO_IPV6;
3745 ipname = "ip6", qid = IPV6CTL_IFQ;
3746 break;
3747 #endif
3748 default:
3749 KASSERT(false);
3750 }
3751
3752 sysctl_createv(clog, 0, NULL, NULL,
3753 CTLFLAG_PERMANENT,
3754 CTLTYPE_NODE, pfname, NULL,
3755 NULL, 0, NULL, 0,
3756 CTL_NET, pf, CTL_EOL);
3757 sysctl_createv(clog, 0, NULL, NULL,
3758 CTLFLAG_PERMANENT,
3759 CTLTYPE_NODE, ipname, NULL,
3760 NULL, 0, NULL, 0,
3761 CTL_NET, pf, ipn, CTL_EOL);
3762 sysctl_createv(clog, 0, NULL, NULL,
3763 CTLFLAG_PERMANENT,
3764 CTLTYPE_NODE, "ifq",
3765 SYSCTL_DESCR("Protocol input queue controls"),
3766 NULL, 0, NULL, 0,
3767 CTL_NET, pf, ipn, qid, CTL_EOL);
3768
3769 sysctl_createv(clog, 0, NULL, NULL,
3770 CTLFLAG_PERMANENT,
3771 CTLTYPE_QUAD, "len",
3772 SYSCTL_DESCR("Current input queue length"),
3773 len_func, 0, NULL, 0,
3774 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
3775 sysctl_createv(clog, 0, NULL, NULL,
3776 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3777 CTLTYPE_INT, "maxlen",
3778 SYSCTL_DESCR("Maximum allowed input queue length"),
3779 maxlen_func, 0, NULL, 0,
3780 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
3781 sysctl_createv(clog, 0, NULL, NULL,
3782 CTLFLAG_PERMANENT,
3783 CTLTYPE_QUAD, "drops",
3784 SYSCTL_DESCR("Packets dropped due to full input queue"),
3785 drops_func, 0, NULL, 0,
3786 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
3787 }
3788 #endif /* INET || INET6 */
3789
3790 static int
3791 if_sdl_sysctl(SYSCTLFN_ARGS)
3792 {
3793 struct ifnet *ifp;
3794 const struct sockaddr_dl *sdl;
3795 struct psref psref;
3796 int error = 0;
3797 int bound;
3798
3799 if (namelen != 1)
3800 return EINVAL;
3801
3802 bound = curlwp_bind();
3803 ifp = if_get_byindex(name[0], &psref);
3804 if (ifp == NULL) {
3805 error = ENODEV;
3806 goto out0;
3807 }
3808
3809 sdl = ifp->if_sadl;
3810 if (sdl == NULL) {
3811 *oldlenp = 0;
3812 goto out1;
3813 }
3814
3815 if (oldp == NULL) {
3816 *oldlenp = sdl->sdl_alen;
3817 goto out1;
3818 }
3819
3820 if (*oldlenp >= sdl->sdl_alen)
3821 *oldlenp = sdl->sdl_alen;
3822 error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
3823 out1:
3824 if_put(ifp, &psref);
3825 out0:
3826 curlwp_bindx(bound);
3827 return error;
3828 }
3829
3830 static void
3831 if_sysctl_setup(struct sysctllog **clog)
3832 {
3833 const struct sysctlnode *rnode = NULL;
3834
3835 sysctl_createv(clog, 0, NULL, &rnode,
3836 CTLFLAG_PERMANENT,
3837 CTLTYPE_NODE, "sdl",
3838 SYSCTL_DESCR("Get active link-layer address"),
3839 if_sdl_sysctl, 0, NULL, 0,
3840 CTL_NET, CTL_CREATE, CTL_EOL);
3841
3842 #if defined(INET)
3843 sysctl_net_pktq_setup(NULL, PF_INET);
3844 #endif
3845 #ifdef INET6
3846 if (in6_present)
3847 sysctl_net_pktq_setup(NULL, PF_INET6);
3848 #endif
3849 }
3850