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