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