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