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