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