if.c revision 1.347 1 /* $NetBSD: if.c,v 1.347 2016/06/27 10:09:02 knakahara Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William Studenmund and Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 /*
62 * Copyright (c) 1980, 1986, 1993
63 * The Regents of the University of California. All rights reserved.
64 *
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
67 * are met:
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. Neither the name of the University nor the names of its contributors
74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission.
76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE.
88 *
89 * @(#)if.c 8.5 (Berkeley) 1/9/95
90 */
91
92 #include <sys/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.347 2016/06/27 10:09:02 knakahara 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 void
1010 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *))
1011 {
1012 struct ifaddr *ifa, *nifa;
1013
1014 IFADDR_FOREACH_SAFE(ifa, ifp, nifa) {
1015 if (ifa->ifa_addr->sa_family != family)
1016 continue;
1017 (*purgeaddr)(ifa);
1018 }
1019 }
1020
1021 /*
1022 * Detach an interface from the list of "active" interfaces,
1023 * freeing any resources as we go along.
1024 *
1025 * NOTE: This routine must be called with a valid thread context,
1026 * as it may block.
1027 */
1028 void
1029 if_detach(struct ifnet *ifp)
1030 {
1031 struct socket so;
1032 struct ifaddr *ifa;
1033 #ifdef IFAREF_DEBUG
1034 struct ifaddr *last_ifa = NULL;
1035 #endif
1036 struct domain *dp;
1037 const struct protosw *pr;
1038 int s, i, family, purged;
1039 uint64_t xc;
1040
1041 /*
1042 * XXX It's kind of lame that we have to have the
1043 * XXX socket structure...
1044 */
1045 memset(&so, 0, sizeof(so));
1046
1047 s = splnet();
1048
1049 sysctl_teardown(&ifp->if_sysctl_log);
1050 mutex_enter(ifp->if_ioctl_lock);
1051 ifp->if_ioctl = if_nullioctl;
1052 mutex_exit(ifp->if_ioctl_lock);
1053
1054 IFNET_LOCK();
1055 ifindex2ifnet[ifp->if_index] = NULL;
1056 TAILQ_REMOVE(&ifnet_list, ifp, if_list);
1057 IFNET_WRITER_REMOVE(ifp);
1058 pserialize_perform(ifnet_psz);
1059 IFNET_UNLOCK();
1060
1061 mutex_obj_free(ifp->if_ioctl_lock);
1062 ifp->if_ioctl_lock = NULL;
1063
1064 if (ifp->if_slowtimo != NULL) {
1065 ifp->if_slowtimo = NULL;
1066 callout_halt(ifp->if_slowtimo_ch, NULL);
1067 callout_destroy(ifp->if_slowtimo_ch);
1068 kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch));
1069 }
1070
1071 /*
1072 * Do an if_down() to give protocols a chance to do something.
1073 */
1074 if_down(ifp);
1075
1076 #ifdef ALTQ
1077 if (ALTQ_IS_ENABLED(&ifp->if_snd))
1078 altq_disable(&ifp->if_snd);
1079 if (ALTQ_IS_ATTACHED(&ifp->if_snd))
1080 altq_detach(&ifp->if_snd);
1081 #endif
1082
1083 if (ifp->if_snd.ifq_lock)
1084 mutex_obj_free(ifp->if_snd.ifq_lock);
1085
1086 #if NCARP > 0
1087 /* Remove the interface from any carp group it is a part of. */
1088 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP)
1089 carp_ifdetach(ifp);
1090 #endif
1091
1092 /*
1093 * Rip all the addresses off the interface. This should make
1094 * all of the routes go away.
1095 *
1096 * pr_usrreq calls can remove an arbitrary number of ifaddrs
1097 * from the list, including our "cursor", ifa. For safety,
1098 * and to honor the TAILQ abstraction, I just restart the
1099 * loop after each removal. Note that the loop will exit
1100 * when all of the remaining ifaddrs belong to the AF_LINK
1101 * family. I am counting on the historical fact that at
1102 * least one pr_usrreq in each address domain removes at
1103 * least one ifaddr.
1104 */
1105 again:
1106 IFADDR_FOREACH(ifa, ifp) {
1107 family = ifa->ifa_addr->sa_family;
1108 #ifdef IFAREF_DEBUG
1109 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
1110 ifa, family, ifa->ifa_refcnt);
1111 if (last_ifa != NULL && ifa == last_ifa)
1112 panic("if_detach: loop detected");
1113 last_ifa = ifa;
1114 #endif
1115 if (family == AF_LINK)
1116 continue;
1117 dp = pffinddomain(family);
1118 #ifdef DIAGNOSTIC
1119 if (dp == NULL)
1120 panic("if_detach: no domain for AF %d",
1121 family);
1122 #endif
1123 /*
1124 * XXX These PURGEIF calls are redundant with the
1125 * purge-all-families calls below, but are left in for
1126 * now both to make a smaller change, and to avoid
1127 * unplanned interactions with clearing of
1128 * ifp->if_addrlist.
1129 */
1130 purged = 0;
1131 for (pr = dp->dom_protosw;
1132 pr < dp->dom_protoswNPROTOSW; pr++) {
1133 so.so_proto = pr;
1134 if (pr->pr_usrreqs) {
1135 (void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1136 purged = 1;
1137 }
1138 }
1139 if (purged == 0) {
1140 /*
1141 * XXX What's really the best thing to do
1142 * XXX here? --thorpej (at) NetBSD.org
1143 */
1144 printf("if_detach: WARNING: AF %d not purged\n",
1145 family);
1146 ifa_remove(ifp, ifa);
1147 }
1148 goto again;
1149 }
1150
1151 if_free_sadl(ifp);
1152
1153 /* Walk the routing table looking for stragglers. */
1154 for (i = 0; i <= AF_MAX; i++) {
1155 while (rt_walktree(i, if_rt_walktree, ifp) == ERESTART)
1156 continue;
1157 }
1158
1159 DOMAIN_FOREACH(dp) {
1160 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family])
1161 {
1162 void *p = ifp->if_afdata[dp->dom_family];
1163 if (p) {
1164 ifp->if_afdata[dp->dom_family] = NULL;
1165 (*dp->dom_ifdetach)(ifp, p);
1166 }
1167 }
1168
1169 /*
1170 * One would expect multicast memberships (INET and
1171 * INET6) on UDP sockets to be purged by the PURGEIF
1172 * calls above, but if all addresses were removed from
1173 * the interface prior to destruction, the calls will
1174 * not be made (e.g. ppp, for which pppd(8) generally
1175 * removes addresses before destroying the interface).
1176 * Because there is no invariant that multicast
1177 * memberships only exist for interfaces with IPv4
1178 * addresses, we must call PURGEIF regardless of
1179 * addresses. (Protocols which might store ifnet
1180 * pointers are marked with PR_PURGEIF.)
1181 */
1182 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
1183 so.so_proto = pr;
1184 if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF)
1185 (void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp);
1186 }
1187 }
1188
1189 (void)pfil_run_hooks(if_pfil,
1190 (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET);
1191 (void)pfil_head_destroy(ifp->if_pfil);
1192
1193 /* Announce that the interface is gone. */
1194 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1195
1196 IF_AFDATA_LOCK_DESTROY(ifp);
1197
1198 if (if_is_link_state_changeable(ifp)) {
1199 softint_disestablish(ifp->if_link_si);
1200 ifp->if_link_si = NULL;
1201 }
1202
1203 /*
1204 * remove packets that came from ifp, from software interrupt queues.
1205 */
1206 DOMAIN_FOREACH(dp) {
1207 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) {
1208 struct ifqueue *iq = dp->dom_ifqueues[i];
1209 if (iq == NULL)
1210 break;
1211 dp->dom_ifqueues[i] = NULL;
1212 if_detach_queues(ifp, iq);
1213 }
1214 }
1215
1216 /*
1217 * IP queues have to be processed separately: net-queue barrier
1218 * ensures that the packets are dequeued while a cross-call will
1219 * ensure that the interrupts have completed. FIXME: not quite..
1220 */
1221 #ifdef INET
1222 pktq_barrier(ip_pktq);
1223 #endif
1224 #ifdef INET6
1225 if (in6_present)
1226 pktq_barrier(ip6_pktq);
1227 #endif
1228 xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
1229 xc_wait(xc);
1230
1231 /* Wait for all readers to drain before freeing. */
1232 psref_target_destroy(&ifp->if_psref, ifnet_psref_class);
1233 PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry);
1234
1235 if (ifp->if_percpuq != NULL) {
1236 if_percpuq_destroy(ifp->if_percpuq);
1237 ifp->if_percpuq = NULL;
1238 }
1239
1240 splx(s);
1241 }
1242
1243 static void
1244 if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
1245 {
1246 struct mbuf *m, *prev, *next;
1247
1248 prev = NULL;
1249 for (m = q->ifq_head; m != NULL; m = next) {
1250 KASSERT((m->m_flags & M_PKTHDR) != 0);
1251
1252 next = m->m_nextpkt;
1253 if (m->m_pkthdr.rcvif_index != ifp->if_index) {
1254 prev = m;
1255 continue;
1256 }
1257
1258 if (prev != NULL)
1259 prev->m_nextpkt = m->m_nextpkt;
1260 else
1261 q->ifq_head = m->m_nextpkt;
1262 if (q->ifq_tail == m)
1263 q->ifq_tail = prev;
1264 q->ifq_len--;
1265
1266 m->m_nextpkt = NULL;
1267 m_freem(m);
1268 IF_DROP(q);
1269 }
1270 }
1271
1272 /*
1273 * Callback for a radix tree walk to delete all references to an
1274 * ifnet.
1275 */
1276 static int
1277 if_rt_walktree(struct rtentry *rt, void *v)
1278 {
1279 struct ifnet *ifp = (struct ifnet *)v;
1280 int error;
1281 struct rtentry *retrt;
1282
1283 if (rt->rt_ifp != ifp)
1284 return 0;
1285
1286 /* Delete the entry. */
1287 error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway,
1288 rt_mask(rt), rt->rt_flags, &retrt);
1289 if (error == 0) {
1290 KASSERT(retrt == rt);
1291 KASSERT((retrt->rt_flags & RTF_UP) == 0);
1292 retrt->rt_ifp = NULL;
1293 rtfree(retrt);
1294 } else {
1295 printf("%s: warning: unable to delete rtentry @ %p, "
1296 "error = %d\n", ifp->if_xname, rt, error);
1297 }
1298 return ERESTART;
1299 }
1300
1301 /*
1302 * Create a clone network interface.
1303 */
1304 static int
1305 if_clone_create(const char *name)
1306 {
1307 struct if_clone *ifc;
1308 int unit;
1309 struct ifnet *ifp;
1310 struct psref psref;
1311
1312 ifc = if_clone_lookup(name, &unit);
1313 if (ifc == NULL)
1314 return EINVAL;
1315
1316 ifp = if_get(name, &psref);
1317 if (ifp != NULL) {
1318 if_put(ifp, &psref);
1319 return EEXIST;
1320 }
1321
1322 return (*ifc->ifc_create)(ifc, unit);
1323 }
1324
1325 /*
1326 * Destroy a clone network interface.
1327 */
1328 static int
1329 if_clone_destroy(const char *name)
1330 {
1331 struct if_clone *ifc;
1332 struct ifnet *ifp;
1333 struct psref psref;
1334
1335 ifc = if_clone_lookup(name, NULL);
1336 if (ifc == NULL)
1337 return EINVAL;
1338
1339 if (ifc->ifc_destroy == NULL)
1340 return EOPNOTSUPP;
1341
1342 ifp = if_get(name, &psref);
1343 if (ifp == NULL)
1344 return ENXIO;
1345
1346 /* We have to disable ioctls here */
1347 mutex_enter(ifp->if_ioctl_lock);
1348 ifp->if_ioctl = if_nullioctl;
1349 mutex_exit(ifp->if_ioctl_lock);
1350
1351 /*
1352 * We cannot call ifc_destroy with holding ifp.
1353 * Releasing ifp here is safe thanks to if_clone_mtx.
1354 */
1355 if_put(ifp, &psref);
1356
1357 return (*ifc->ifc_destroy)(ifp);
1358 }
1359
1360 /*
1361 * Look up a network interface cloner.
1362 */
1363 static struct if_clone *
1364 if_clone_lookup(const char *name, int *unitp)
1365 {
1366 struct if_clone *ifc;
1367 const char *cp;
1368 char *dp, ifname[IFNAMSIZ + 3];
1369 int unit;
1370
1371 strcpy(ifname, "if_");
1372 /* separate interface name from unit */
1373 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
1374 *cp && (*cp < '0' || *cp > '9');)
1375 *dp++ = *cp++;
1376
1377 if (cp == name || cp - name == IFNAMSIZ || !*cp)
1378 return NULL; /* No name or unit number */
1379 *dp++ = '\0';
1380
1381 again:
1382 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1383 if (strcmp(ifname + 3, ifc->ifc_name) == 0)
1384 break;
1385 }
1386
1387 if (ifc == NULL) {
1388 if (*ifname == '\0' ||
1389 module_autoload(ifname, MODULE_CLASS_DRIVER))
1390 return NULL;
1391 *ifname = '\0';
1392 goto again;
1393 }
1394
1395 unit = 0;
1396 while (cp - name < IFNAMSIZ && *cp) {
1397 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1398 /* Bogus unit number. */
1399 return NULL;
1400 }
1401 unit = (unit * 10) + (*cp++ - '0');
1402 }
1403
1404 if (unitp != NULL)
1405 *unitp = unit;
1406 return ifc;
1407 }
1408
1409 /*
1410 * Register a network interface cloner.
1411 */
1412 void
1413 if_clone_attach(struct if_clone *ifc)
1414 {
1415
1416 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1417 if_cloners_count++;
1418 }
1419
1420 /*
1421 * Unregister a network interface cloner.
1422 */
1423 void
1424 if_clone_detach(struct if_clone *ifc)
1425 {
1426
1427 LIST_REMOVE(ifc, ifc_list);
1428 if_cloners_count--;
1429 }
1430
1431 /*
1432 * Provide list of interface cloners to userspace.
1433 */
1434 int
1435 if_clone_list(int buf_count, char *buffer, int *total)
1436 {
1437 char outbuf[IFNAMSIZ], *dst;
1438 struct if_clone *ifc;
1439 int count, error = 0;
1440
1441 *total = if_cloners_count;
1442 if ((dst = buffer) == NULL) {
1443 /* Just asking how many there are. */
1444 return 0;
1445 }
1446
1447 if (buf_count < 0)
1448 return EINVAL;
1449
1450 count = (if_cloners_count < buf_count) ?
1451 if_cloners_count : buf_count;
1452
1453 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
1454 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1455 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1456 if (outbuf[sizeof(outbuf) - 1] != '\0')
1457 return ENAMETOOLONG;
1458 error = copyout(outbuf, dst, sizeof(outbuf));
1459 if (error != 0)
1460 break;
1461 }
1462
1463 return error;
1464 }
1465
1466 void
1467 ifaref(struct ifaddr *ifa)
1468 {
1469 ifa->ifa_refcnt++;
1470 }
1471
1472 void
1473 ifafree(struct ifaddr *ifa)
1474 {
1475 KASSERT(ifa != NULL);
1476 KASSERT(ifa->ifa_refcnt > 0);
1477
1478 if (--ifa->ifa_refcnt == 0) {
1479 free(ifa, M_IFADDR);
1480 }
1481 }
1482
1483 void
1484 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
1485 {
1486 ifa->ifa_ifp = ifp;
1487 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1488 ifaref(ifa);
1489 }
1490
1491 void
1492 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
1493 {
1494 KASSERT(ifa->ifa_ifp == ifp);
1495 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1496 ifafree(ifa);
1497 }
1498
1499 static inline int
1500 equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
1501 {
1502 return sockaddr_cmp(sa1, sa2) == 0;
1503 }
1504
1505 /*
1506 * Locate an interface based on a complete address.
1507 */
1508 /*ARGSUSED*/
1509 struct ifaddr *
1510 ifa_ifwithaddr(const struct sockaddr *addr)
1511 {
1512 struct ifnet *ifp;
1513 struct ifaddr *ifa;
1514 int s;
1515
1516 s = pserialize_read_enter();
1517 IFNET_READER_FOREACH(ifp) {
1518 if (ifp->if_output == if_nulloutput)
1519 continue;
1520 IFADDR_FOREACH(ifa, ifp) {
1521 if (ifa->ifa_addr->sa_family != addr->sa_family)
1522 continue;
1523 if (equal(addr, ifa->ifa_addr))
1524 return ifa;
1525 if ((ifp->if_flags & IFF_BROADCAST) &&
1526 ifa->ifa_broadaddr &&
1527 /* IP6 doesn't have broadcast */
1528 ifa->ifa_broadaddr->sa_len != 0 &&
1529 equal(ifa->ifa_broadaddr, addr))
1530 return ifa;
1531 }
1532 }
1533 pserialize_read_exit(s);
1534 return NULL;
1535 }
1536
1537 /*
1538 * Locate the point to point interface with a given destination address.
1539 */
1540 /*ARGSUSED*/
1541 struct ifaddr *
1542 ifa_ifwithdstaddr(const struct sockaddr *addr)
1543 {
1544 struct ifnet *ifp;
1545 struct ifaddr *ifa;
1546 int s;
1547
1548 s = pserialize_read_enter();
1549 IFNET_READER_FOREACH(ifp) {
1550 if (ifp->if_output == if_nulloutput)
1551 continue;
1552 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1553 continue;
1554 IFADDR_FOREACH(ifa, ifp) {
1555 if (ifa->ifa_addr->sa_family != addr->sa_family ||
1556 ifa->ifa_dstaddr == NULL)
1557 continue;
1558 if (equal(addr, ifa->ifa_dstaddr))
1559 return ifa;
1560 }
1561 }
1562 pserialize_read_exit(s);
1563 return NULL;
1564 }
1565
1566 /*
1567 * Find an interface on a specific network. If many, choice
1568 * is most specific found.
1569 */
1570 struct ifaddr *
1571 ifa_ifwithnet(const struct sockaddr *addr)
1572 {
1573 struct ifnet *ifp;
1574 struct ifaddr *ifa;
1575 const struct sockaddr_dl *sdl;
1576 struct ifaddr *ifa_maybe = 0;
1577 u_int af = addr->sa_family;
1578 const char *addr_data = addr->sa_data, *cplim;
1579 int s;
1580
1581 if (af == AF_LINK) {
1582 sdl = satocsdl(addr);
1583 if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1584 ifindex2ifnet[sdl->sdl_index] &&
1585 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput) {
1586 return ifindex2ifnet[sdl->sdl_index]->if_dl;
1587 }
1588 }
1589 #ifdef NETATALK
1590 if (af == AF_APPLETALK) {
1591 const struct sockaddr_at *sat, *sat2;
1592 sat = (const struct sockaddr_at *)addr;
1593 s = pserialize_read_enter();
1594 IFNET_READER_FOREACH(ifp) {
1595 if (ifp->if_output == if_nulloutput)
1596 continue;
1597 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
1598 if (ifa == NULL)
1599 continue;
1600 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
1601 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
1602 return ifa; /* exact match */
1603 if (ifa_maybe == NULL) {
1604 /* else keep the if with the right range */
1605 ifa_maybe = ifa;
1606 }
1607 }
1608 pserialize_read_exit(s);
1609 return ifa_maybe;
1610 }
1611 #endif
1612 s = pserialize_read_enter();
1613 IFNET_READER_FOREACH(ifp) {
1614 if (ifp->if_output == if_nulloutput)
1615 continue;
1616 IFADDR_FOREACH(ifa, ifp) {
1617 const char *cp, *cp2, *cp3;
1618
1619 if (ifa->ifa_addr->sa_family != af ||
1620 ifa->ifa_netmask == NULL)
1621 next: continue;
1622 cp = addr_data;
1623 cp2 = ifa->ifa_addr->sa_data;
1624 cp3 = ifa->ifa_netmask->sa_data;
1625 cplim = (const char *)ifa->ifa_netmask +
1626 ifa->ifa_netmask->sa_len;
1627 while (cp3 < cplim) {
1628 if ((*cp++ ^ *cp2++) & *cp3++) {
1629 /* want to continue for() loop */
1630 goto next;
1631 }
1632 }
1633 if (ifa_maybe == NULL ||
1634 rt_refines(ifa->ifa_netmask,
1635 ifa_maybe->ifa_netmask))
1636 ifa_maybe = ifa;
1637 }
1638 }
1639 pserialize_read_exit(s);
1640 return ifa_maybe;
1641 }
1642
1643 /*
1644 * Find the interface of the addresss.
1645 */
1646 struct ifaddr *
1647 ifa_ifwithladdr(const struct sockaddr *addr)
1648 {
1649 struct ifaddr *ia;
1650
1651 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
1652 (ia = ifa_ifwithnet(addr)))
1653 return ia;
1654 return NULL;
1655 }
1656
1657 /*
1658 * Find an interface using a specific address family
1659 */
1660 struct ifaddr *
1661 ifa_ifwithaf(int af)
1662 {
1663 struct ifnet *ifp;
1664 struct ifaddr *ifa = NULL;
1665 int s;
1666
1667 s = pserialize_read_enter();
1668 IFNET_READER_FOREACH(ifp) {
1669 if (ifp->if_output == if_nulloutput)
1670 continue;
1671 IFADDR_FOREACH(ifa, ifp) {
1672 if (ifa->ifa_addr->sa_family == af)
1673 goto out;
1674 }
1675 }
1676 out:
1677 pserialize_read_exit(s);
1678 return ifa;
1679 }
1680
1681 /*
1682 * Find an interface address specific to an interface best matching
1683 * a given address.
1684 */
1685 struct ifaddr *
1686 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
1687 {
1688 struct ifaddr *ifa;
1689 const char *cp, *cp2, *cp3;
1690 const char *cplim;
1691 struct ifaddr *ifa_maybe = 0;
1692 u_int af = addr->sa_family;
1693
1694 if (ifp->if_output == if_nulloutput)
1695 return NULL;
1696
1697 if (af >= AF_MAX)
1698 return NULL;
1699
1700 IFADDR_FOREACH(ifa, ifp) {
1701 if (ifa->ifa_addr->sa_family != af)
1702 continue;
1703 ifa_maybe = ifa;
1704 if (ifa->ifa_netmask == NULL) {
1705 if (equal(addr, ifa->ifa_addr) ||
1706 (ifa->ifa_dstaddr &&
1707 equal(addr, ifa->ifa_dstaddr)))
1708 return ifa;
1709 continue;
1710 }
1711 cp = addr->sa_data;
1712 cp2 = ifa->ifa_addr->sa_data;
1713 cp3 = ifa->ifa_netmask->sa_data;
1714 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1715 for (; cp3 < cplim; cp3++) {
1716 if ((*cp++ ^ *cp2++) & *cp3)
1717 break;
1718 }
1719 if (cp3 == cplim)
1720 return ifa;
1721 }
1722 return ifa_maybe;
1723 }
1724
1725 /*
1726 * Default action when installing a route with a Link Level gateway.
1727 * Lookup an appropriate real ifa to point to.
1728 * This should be moved to /sys/net/link.c eventually.
1729 */
1730 void
1731 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
1732 {
1733 struct ifaddr *ifa;
1734 const struct sockaddr *dst;
1735 struct ifnet *ifp;
1736
1737 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1738 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL)
1739 return;
1740 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) {
1741 rt_replace_ifa(rt, ifa);
1742 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1743 ifa->ifa_rtrequest(cmd, rt, info);
1744 }
1745 }
1746
1747 /*
1748 * bitmask macros to manage a densely packed link_state change queue.
1749 * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
1750 * LINK_STATE_UP(2) we need 2 bits for each state change.
1751 * As a state change to store is 0, treat all bits set as an unset item.
1752 */
1753 #define LQ_ITEM_BITS 2
1754 #define LQ_ITEM_MASK ((1 << LQ_ITEM_BITS) - 1)
1755 #define LQ_MASK(i) (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
1756 #define LINK_STATE_UNSET LQ_ITEM_MASK
1757 #define LQ_ITEM(q, i) (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
1758 #define LQ_STORE(q, i, v) \
1759 do { \
1760 (q) &= ~LQ_MASK((i)); \
1761 (q) |= (v) << (i) * LQ_ITEM_BITS; \
1762 } while (0 /* CONSTCOND */)
1763 #define LQ_MAX(q) ((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
1764 #define LQ_POP(q, v) \
1765 do { \
1766 (v) = LQ_ITEM((q), 0); \
1767 (q) >>= LQ_ITEM_BITS; \
1768 (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \
1769 } while (0 /* CONSTCOND */)
1770 #define LQ_PUSH(q, v) \
1771 do { \
1772 (q) >>= LQ_ITEM_BITS; \
1773 (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \
1774 } while (0 /* CONSTCOND */)
1775 #define LQ_FIND_UNSET(q, i) \
1776 for ((i) = 0; i < LQ_MAX((q)); (i)++) { \
1777 if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET) \
1778 break; \
1779 }
1780 /*
1781 * Handle a change in the interface link state and
1782 * queue notifications.
1783 */
1784 void
1785 if_link_state_change(struct ifnet *ifp, int link_state)
1786 {
1787 int s, idx;
1788
1789 KASSERTMSG(if_is_link_state_changeable(ifp),
1790 "%s: IFEF_NO_LINK_STATE_CHANGE must not be set, but if_extflags=0x%x",
1791 ifp->if_xname, ifp->if_extflags);
1792
1793 /* Ensure change is to a valid state */
1794 switch (link_state) {
1795 case LINK_STATE_UNKNOWN: /* FALLTHROUGH */
1796 case LINK_STATE_DOWN: /* FALLTHROUGH */
1797 case LINK_STATE_UP:
1798 break;
1799 default:
1800 #ifdef DEBUG
1801 printf("%s: invalid link state %d\n",
1802 ifp->if_xname, link_state);
1803 #endif
1804 return;
1805 }
1806
1807 s = splnet();
1808
1809 /* Find the last unset event in the queue. */
1810 LQ_FIND_UNSET(ifp->if_link_queue, idx);
1811
1812 /*
1813 * Ensure link_state doesn't match the last event in the queue.
1814 * ifp->if_link_state is not checked and set here because
1815 * that would present an inconsistent picture to the system.
1816 */
1817 if (idx != 0 &&
1818 LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
1819 goto out;
1820
1821 /* Handle queue overflow. */
1822 if (idx == LQ_MAX(ifp->if_link_queue)) {
1823 uint8_t lost;
1824
1825 /*
1826 * The DOWN state must be protected from being pushed off
1827 * the queue to ensure that userland will always be
1828 * in a sane state.
1829 * Because DOWN is protected, there is no need to protect
1830 * UNKNOWN.
1831 * It should be invalid to change from any other state to
1832 * UNKNOWN anyway ...
1833 */
1834 lost = LQ_ITEM(ifp->if_link_queue, 0);
1835 LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
1836 if (lost == LINK_STATE_DOWN) {
1837 lost = LQ_ITEM(ifp->if_link_queue, 0);
1838 LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
1839 }
1840 printf("%s: lost link state change %s\n",
1841 ifp->if_xname,
1842 lost == LINK_STATE_UP ? "UP" :
1843 lost == LINK_STATE_DOWN ? "DOWN" :
1844 "UNKNOWN");
1845 } else
1846 LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
1847
1848 softint_schedule(ifp->if_link_si);
1849
1850 out:
1851 splx(s);
1852 }
1853
1854 /*
1855 * Handle interface link state change notifications.
1856 * Must be called at splnet().
1857 */
1858 static void
1859 if_link_state_change0(struct ifnet *ifp, int link_state)
1860 {
1861 struct domain *dp;
1862
1863 /* Ensure the change is still valid. */
1864 if (ifp->if_link_state == link_state)
1865 return;
1866
1867 #ifdef DEBUG
1868 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
1869 link_state == LINK_STATE_UP ? "UP" :
1870 link_state == LINK_STATE_DOWN ? "DOWN" :
1871 "UNKNOWN",
1872 ifp->if_link_state == LINK_STATE_UP ? "UP" :
1873 ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
1874 "UNKNOWN");
1875 #endif
1876
1877 /*
1878 * When going from UNKNOWN to UP, we need to mark existing
1879 * addresses as tentative and restart DAD as we may have
1880 * erroneously not found a duplicate.
1881 *
1882 * This needs to happen before rt_ifmsg to avoid a race where
1883 * listeners would have an address and expect it to work right
1884 * away.
1885 */
1886 if (link_state == LINK_STATE_UP &&
1887 ifp->if_link_state == LINK_STATE_UNKNOWN)
1888 {
1889 DOMAIN_FOREACH(dp) {
1890 if (dp->dom_if_link_state_change != NULL)
1891 dp->dom_if_link_state_change(ifp,
1892 LINK_STATE_DOWN);
1893 }
1894 }
1895
1896 ifp->if_link_state = link_state;
1897
1898 /* Notify that the link state has changed. */
1899 rt_ifmsg(ifp);
1900
1901 #if NCARP > 0
1902 if (ifp->if_carp)
1903 carp_carpdev_state(ifp);
1904 #endif
1905
1906 DOMAIN_FOREACH(dp) {
1907 if (dp->dom_if_link_state_change != NULL)
1908 dp->dom_if_link_state_change(ifp, link_state);
1909 }
1910 }
1911
1912 /*
1913 * Process the interface link state change queue.
1914 */
1915 static void
1916 if_link_state_change_si(void *arg)
1917 {
1918 struct ifnet *ifp = arg;
1919 int s;
1920 uint8_t state;
1921
1922 s = splnet();
1923
1924 /* Pop a link state change from the queue and process it. */
1925 LQ_POP(ifp->if_link_queue, state);
1926 if_link_state_change0(ifp, state);
1927
1928 /* If there is a link state change to come, schedule it. */
1929 if (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET)
1930 softint_schedule(ifp->if_link_si);
1931
1932 splx(s);
1933 }
1934
1935 /*
1936 * Default action when installing a local route on a point-to-point
1937 * interface.
1938 */
1939 void
1940 p2p_rtrequest(int req, struct rtentry *rt,
1941 __unused const struct rt_addrinfo *info)
1942 {
1943 struct ifnet *ifp = rt->rt_ifp;
1944 struct ifaddr *ifa, *lo0ifa;
1945
1946 switch (req) {
1947 case RTM_ADD:
1948 if ((rt->rt_flags & RTF_LOCAL) == 0)
1949 break;
1950
1951 IFADDR_FOREACH(ifa, ifp) {
1952 if (equal(rt_getkey(rt), ifa->ifa_addr))
1953 break;
1954 }
1955 if (ifa == NULL)
1956 break;
1957
1958 /*
1959 * Ensure lo0 has an address of the same family.
1960 */
1961 IFADDR_FOREACH(lo0ifa, lo0ifp) {
1962 if (lo0ifa->ifa_addr->sa_family ==
1963 ifa->ifa_addr->sa_family)
1964 break;
1965 }
1966 if (lo0ifa == NULL)
1967 break;
1968
1969 rt->rt_ifp = lo0ifp;
1970
1971 /*
1972 * Make sure to set rt->rt_ifa to the interface
1973 * address we are using, otherwise we will have trouble
1974 * with source address selection.
1975 */
1976 if (ifa != rt->rt_ifa)
1977 rt_replace_ifa(rt, ifa);
1978 break;
1979 case RTM_DELETE:
1980 default:
1981 break;
1982 }
1983 }
1984
1985 /*
1986 * Mark an interface down and notify protocols of
1987 * the transition.
1988 * NOTE: must be called at splsoftnet or equivalent.
1989 */
1990 void
1991 if_down(struct ifnet *ifp)
1992 {
1993 struct ifaddr *ifa;
1994 struct domain *dp;
1995
1996 ifp->if_flags &= ~IFF_UP;
1997 nanotime(&ifp->if_lastchange);
1998 IFADDR_FOREACH(ifa, ifp)
1999 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2000 IFQ_PURGE(&ifp->if_snd);
2001 #if NCARP > 0
2002 if (ifp->if_carp)
2003 carp_carpdev_state(ifp);
2004 #endif
2005 rt_ifmsg(ifp);
2006 DOMAIN_FOREACH(dp) {
2007 if (dp->dom_if_down)
2008 dp->dom_if_down(ifp);
2009 }
2010 }
2011
2012 /*
2013 * Mark an interface up and notify protocols of
2014 * the transition.
2015 * NOTE: must be called at splsoftnet or equivalent.
2016 */
2017 void
2018 if_up(struct ifnet *ifp)
2019 {
2020 #ifdef notyet
2021 struct ifaddr *ifa;
2022 #endif
2023 struct domain *dp;
2024
2025 ifp->if_flags |= IFF_UP;
2026 nanotime(&ifp->if_lastchange);
2027 #ifdef notyet
2028 /* this has no effect on IP, and will kill all ISO connections XXX */
2029 IFADDR_FOREACH(ifa, ifp)
2030 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2031 #endif
2032 #if NCARP > 0
2033 if (ifp->if_carp)
2034 carp_carpdev_state(ifp);
2035 #endif
2036 rt_ifmsg(ifp);
2037 DOMAIN_FOREACH(dp) {
2038 if (dp->dom_if_up)
2039 dp->dom_if_up(ifp);
2040 }
2041 }
2042
2043 /*
2044 * Handle interface slowtimo timer routine. Called
2045 * from softclock, we decrement timer (if set) and
2046 * call the appropriate interface routine on expiration.
2047 */
2048 static void
2049 if_slowtimo(void *arg)
2050 {
2051 void (*slowtimo)(struct ifnet *);
2052 struct ifnet *ifp = arg;
2053 int s;
2054
2055 slowtimo = ifp->if_slowtimo;
2056 if (__predict_false(slowtimo == NULL))
2057 return;
2058
2059 s = splnet();
2060 if (ifp->if_timer != 0 && --ifp->if_timer == 0)
2061 (*slowtimo)(ifp);
2062
2063 splx(s);
2064
2065 if (__predict_true(ifp->if_slowtimo != NULL))
2066 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
2067 }
2068
2069 /*
2070 * Set/clear promiscuous mode on interface ifp based on the truth value
2071 * of pswitch. The calls are reference counted so that only the first
2072 * "on" request actually has an effect, as does the final "off" request.
2073 * Results are undefined if the "off" and "on" requests are not matched.
2074 */
2075 int
2076 ifpromisc(struct ifnet *ifp, int pswitch)
2077 {
2078 int pcount, ret;
2079 short nflags;
2080
2081 pcount = ifp->if_pcount;
2082 if (pswitch) {
2083 /*
2084 * Allow the device to be "placed" into promiscuous
2085 * mode even if it is not configured up. It will
2086 * consult IFF_PROMISC when it is brought up.
2087 */
2088 if (ifp->if_pcount++ != 0)
2089 return 0;
2090 nflags = ifp->if_flags | IFF_PROMISC;
2091 } else {
2092 if (--ifp->if_pcount > 0)
2093 return 0;
2094 nflags = ifp->if_flags & ~IFF_PROMISC;
2095 }
2096 ret = if_flags_set(ifp, nflags);
2097 /* Restore interface state if not successful. */
2098 if (ret != 0) {
2099 ifp->if_pcount = pcount;
2100 }
2101 return ret;
2102 }
2103
2104 /*
2105 * Map interface name to
2106 * interface structure pointer.
2107 */
2108 struct ifnet *
2109 ifunit(const char *name)
2110 {
2111 struct ifnet *ifp;
2112 const char *cp = name;
2113 u_int unit = 0;
2114 u_int i;
2115 int s;
2116
2117 /*
2118 * If the entire name is a number, treat it as an ifindex.
2119 */
2120 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2121 unit = unit * 10 + (*cp - '0');
2122 }
2123
2124 /*
2125 * If the number took all of the name, then it's a valid ifindex.
2126 */
2127 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
2128 if (unit >= if_indexlim)
2129 return NULL;
2130 ifp = ifindex2ifnet[unit];
2131 if (ifp == NULL || ifp->if_output == if_nulloutput)
2132 return NULL;
2133 return ifp;
2134 }
2135
2136 ifp = NULL;
2137 s = pserialize_read_enter();
2138 IFNET_READER_FOREACH(ifp) {
2139 if (ifp->if_output == if_nulloutput)
2140 continue;
2141 if (strcmp(ifp->if_xname, name) == 0)
2142 goto out;
2143 }
2144 out:
2145 pserialize_read_exit(s);
2146 return ifp;
2147 }
2148
2149 /*
2150 * Get a reference of an ifnet object by an interface name.
2151 * The returned reference is protected by psref(9). The caller
2152 * must release a returned reference by if_put after use.
2153 */
2154 struct ifnet *
2155 if_get(const char *name, struct psref *psref)
2156 {
2157 struct ifnet *ifp;
2158 const char *cp = name;
2159 u_int unit = 0;
2160 u_int i;
2161 int s;
2162
2163 /*
2164 * If the entire name is a number, treat it as an ifindex.
2165 */
2166 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2167 unit = unit * 10 + (*cp - '0');
2168 }
2169
2170 /*
2171 * If the number took all of the name, then it's a valid ifindex.
2172 */
2173 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
2174 if (unit >= if_indexlim)
2175 return NULL;
2176 ifp = ifindex2ifnet[unit];
2177 if (ifp == NULL || ifp->if_output == if_nulloutput)
2178 return NULL;
2179 return ifp;
2180 }
2181
2182 ifp = NULL;
2183 s = pserialize_read_enter();
2184 IFNET_READER_FOREACH(ifp) {
2185 if (ifp->if_output == if_nulloutput)
2186 continue;
2187 if (strcmp(ifp->if_xname, name) == 0) {
2188 psref_acquire(psref, &ifp->if_psref,
2189 ifnet_psref_class);
2190 goto out;
2191 }
2192 }
2193 out:
2194 pserialize_read_exit(s);
2195 return ifp;
2196 }
2197
2198 /*
2199 * Release a reference of an ifnet object given by if_get or
2200 * if_get_byindex.
2201 */
2202 void
2203 if_put(const struct ifnet *ifp, struct psref *psref)
2204 {
2205
2206 if (ifp == NULL)
2207 return;
2208
2209 psref_release(psref, &ifp->if_psref, ifnet_psref_class);
2210 }
2211
2212 ifnet_t *
2213 if_byindex(u_int idx)
2214 {
2215 return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL;
2216 }
2217
2218 /*
2219 * Get a reference of an ifnet object by an interface index.
2220 * The returned reference is protected by psref(9). The caller
2221 * must release a returned reference by if_put after use.
2222 */
2223 ifnet_t *
2224 if_get_byindex(u_int idx, struct psref *psref)
2225 {
2226 ifnet_t *ifp;
2227 int s;
2228
2229 s = pserialize_read_enter();
2230 ifp = (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
2231 if (__predict_true(ifp != NULL))
2232 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
2233 pserialize_read_exit(s);
2234
2235 return ifp;
2236 }
2237
2238 /*
2239 * XXX it's safe only if the passed ifp is guaranteed to not be freed,
2240 * for example the ifp is already held or some other object is held which
2241 * guarantes the ifp to not be freed indirectly.
2242 */
2243 void
2244 if_acquire_NOMPSAFE(struct ifnet *ifp, struct psref *psref)
2245 {
2246
2247 KASSERT(ifp->if_index != 0);
2248 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
2249 }
2250
2251 bool
2252 if_held(struct ifnet *ifp)
2253 {
2254
2255 return psref_held(&ifp->if_psref, ifnet_psref_class);
2256 }
2257
2258
2259 /* common */
2260 int
2261 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
2262 {
2263 int s;
2264 struct ifreq *ifr;
2265 struct ifcapreq *ifcr;
2266 struct ifdatareq *ifdr;
2267
2268 switch (cmd) {
2269 case SIOCSIFCAP:
2270 ifcr = data;
2271 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
2272 return EINVAL;
2273
2274 if (ifcr->ifcr_capenable == ifp->if_capenable)
2275 return 0;
2276
2277 ifp->if_capenable = ifcr->ifcr_capenable;
2278
2279 /* Pre-compute the checksum flags mask. */
2280 ifp->if_csum_flags_tx = 0;
2281 ifp->if_csum_flags_rx = 0;
2282 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
2283 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
2284 }
2285 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
2286 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
2287 }
2288
2289 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
2290 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
2291 }
2292 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
2293 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
2294 }
2295
2296 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
2297 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
2298 }
2299 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
2300 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
2301 }
2302
2303 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
2304 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
2305 }
2306 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
2307 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
2308 }
2309
2310 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
2311 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
2312 }
2313 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
2314 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
2315 }
2316 if (ifp->if_flags & IFF_UP)
2317 return ENETRESET;
2318 return 0;
2319 case SIOCSIFFLAGS:
2320 ifr = data;
2321 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
2322 s = splnet();
2323 if_down(ifp);
2324 splx(s);
2325 }
2326 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
2327 s = splnet();
2328 if_up(ifp);
2329 splx(s);
2330 }
2331 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2332 (ifr->ifr_flags &~ IFF_CANTCHANGE);
2333 break;
2334 case SIOCGIFFLAGS:
2335 ifr = data;
2336 ifr->ifr_flags = ifp->if_flags;
2337 break;
2338
2339 case SIOCGIFMETRIC:
2340 ifr = data;
2341 ifr->ifr_metric = ifp->if_metric;
2342 break;
2343
2344 case SIOCGIFMTU:
2345 ifr = data;
2346 ifr->ifr_mtu = ifp->if_mtu;
2347 break;
2348
2349 case SIOCGIFDLT:
2350 ifr = data;
2351 ifr->ifr_dlt = ifp->if_dlt;
2352 break;
2353
2354 case SIOCGIFCAP:
2355 ifcr = data;
2356 ifcr->ifcr_capabilities = ifp->if_capabilities;
2357 ifcr->ifcr_capenable = ifp->if_capenable;
2358 break;
2359
2360 case SIOCSIFMETRIC:
2361 ifr = data;
2362 ifp->if_metric = ifr->ifr_metric;
2363 break;
2364
2365 case SIOCGIFDATA:
2366 ifdr = data;
2367 ifdr->ifdr_data = ifp->if_data;
2368 break;
2369
2370 case SIOCGIFINDEX:
2371 ifr = data;
2372 ifr->ifr_index = ifp->if_index;
2373 break;
2374
2375 case SIOCZIFDATA:
2376 ifdr = data;
2377 ifdr->ifdr_data = ifp->if_data;
2378 /*
2379 * Assumes that the volatile counters that can be
2380 * zero'ed are at the end of if_data.
2381 */
2382 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
2383 offsetof(struct if_data, ifi_ipackets));
2384 /*
2385 * The memset() clears to the bottm of if_data. In the area,
2386 * if_lastchange is included. Please be careful if new entry
2387 * will be added into if_data or rewite this.
2388 *
2389 * And also, update if_lastchnage.
2390 */
2391 getnanotime(&ifp->if_lastchange);
2392 break;
2393 case SIOCSIFMTU:
2394 ifr = data;
2395 if (ifp->if_mtu == ifr->ifr_mtu)
2396 break;
2397 ifp->if_mtu = ifr->ifr_mtu;
2398 /*
2399 * If the link MTU changed, do network layer specific procedure.
2400 */
2401 #ifdef INET6
2402 if (in6_present)
2403 nd6_setmtu(ifp);
2404 #endif
2405 return ENETRESET;
2406 default:
2407 return ENOTTY;
2408 }
2409 return 0;
2410 }
2411
2412 int
2413 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
2414 {
2415 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
2416 struct ifaddr *ifa;
2417 const struct sockaddr *any, *sa;
2418 union {
2419 struct sockaddr sa;
2420 struct sockaddr_storage ss;
2421 } u, v;
2422
2423 switch (cmd) {
2424 case SIOCSIFADDRPREF:
2425 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
2426 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
2427 NULL) != 0)
2428 return EPERM;
2429 case SIOCGIFADDRPREF:
2430 break;
2431 default:
2432 return EOPNOTSUPP;
2433 }
2434
2435 /* sanity checks */
2436 if (data == NULL || ifp == NULL) {
2437 panic("invalid argument to %s", __func__);
2438 /*NOTREACHED*/
2439 }
2440
2441 /* address must be specified on ADD and DELETE */
2442 sa = sstocsa(&ifap->ifap_addr);
2443 if (sa->sa_family != sofamily(so))
2444 return EINVAL;
2445 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
2446 return EINVAL;
2447
2448 sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
2449
2450 IFADDR_FOREACH(ifa, ifp) {
2451 if (ifa->ifa_addr->sa_family != sa->sa_family)
2452 continue;
2453 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
2454 if (sockaddr_cmp(&u.sa, &v.sa) == 0)
2455 break;
2456 }
2457 if (ifa == NULL)
2458 return EADDRNOTAVAIL;
2459
2460 switch (cmd) {
2461 case SIOCSIFADDRPREF:
2462 ifa->ifa_preference = ifap->ifap_preference;
2463 return 0;
2464 case SIOCGIFADDRPREF:
2465 /* fill in the if_laddrreq structure */
2466 (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
2467 sizeof(ifap->ifap_addr), ifa->ifa_addr);
2468 ifap->ifap_preference = ifa->ifa_preference;
2469 return 0;
2470 default:
2471 return EOPNOTSUPP;
2472 }
2473 }
2474
2475 /*
2476 * Interface ioctls.
2477 */
2478 static int
2479 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
2480 {
2481 struct ifnet *ifp;
2482 struct ifreq *ifr;
2483 int error = 0;
2484 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
2485 u_long ocmd = cmd;
2486 #endif
2487 short oif_flags;
2488 #ifdef COMPAT_OIFREQ
2489 struct ifreq ifrb;
2490 struct oifreq *oifr = NULL;
2491 #endif
2492 int r;
2493 struct psref psref;
2494 int bound;
2495
2496 switch (cmd) {
2497 #ifdef COMPAT_OIFREQ
2498 case OSIOCGIFCONF:
2499 case OOSIOCGIFCONF:
2500 return compat_ifconf(cmd, data);
2501 #endif
2502 #ifdef COMPAT_OIFDATA
2503 case OSIOCGIFDATA:
2504 case OSIOCZIFDATA:
2505 return compat_ifdatareq(l, cmd, data);
2506 #endif
2507 case SIOCGIFCONF:
2508 return ifconf(cmd, data);
2509 case SIOCINITIFADDR:
2510 return EPERM;
2511 }
2512
2513 #ifdef COMPAT_OIFREQ
2514 cmd = compat_cvtcmd(cmd);
2515 if (cmd != ocmd) {
2516 oifr = data;
2517 data = ifr = &ifrb;
2518 ifreqo2n(oifr, ifr);
2519 } else
2520 #endif
2521 ifr = data;
2522
2523 switch (cmd) {
2524 case SIOCIFCREATE:
2525 case SIOCIFDESTROY:
2526 bound = curlwp_bind();
2527 if (l != NULL) {
2528 ifp = if_get(ifr->ifr_name, &psref);
2529 error = kauth_authorize_network(l->l_cred,
2530 KAUTH_NETWORK_INTERFACE,
2531 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
2532 (void *)cmd, NULL);
2533 if (ifp != NULL)
2534 if_put(ifp, &psref);
2535 if (error != 0) {
2536 curlwp_bindx(bound);
2537 return error;
2538 }
2539 }
2540 mutex_enter(&if_clone_mtx);
2541 r = (cmd == SIOCIFCREATE) ?
2542 if_clone_create(ifr->ifr_name) :
2543 if_clone_destroy(ifr->ifr_name);
2544 mutex_exit(&if_clone_mtx);
2545 curlwp_bindx(bound);
2546 return r;
2547
2548 case SIOCIFGCLONERS:
2549 {
2550 struct if_clonereq *req = (struct if_clonereq *)data;
2551 return if_clone_list(req->ifcr_count, req->ifcr_buffer,
2552 &req->ifcr_total);
2553 }
2554 }
2555
2556 bound = curlwp_bind();
2557 ifp = if_get(ifr->ifr_name, &psref);
2558 if (ifp == NULL) {
2559 curlwp_bindx(bound);
2560 return ENXIO;
2561 }
2562
2563 switch (cmd) {
2564 case SIOCALIFADDR:
2565 case SIOCDLIFADDR:
2566 case SIOCSIFADDRPREF:
2567 case SIOCSIFFLAGS:
2568 case SIOCSIFCAP:
2569 case SIOCSIFMETRIC:
2570 case SIOCZIFDATA:
2571 case SIOCSIFMTU:
2572 case SIOCSIFPHYADDR:
2573 case SIOCDIFPHYADDR:
2574 #ifdef INET6
2575 case SIOCSIFPHYADDR_IN6:
2576 #endif
2577 case SIOCSLIFPHYADDR:
2578 case SIOCADDMULTI:
2579 case SIOCDELMULTI:
2580 case SIOCSIFMEDIA:
2581 case SIOCSDRVSPEC:
2582 case SIOCG80211:
2583 case SIOCS80211:
2584 case SIOCS80211NWID:
2585 case SIOCS80211NWKEY:
2586 case SIOCS80211POWER:
2587 case SIOCS80211BSSID:
2588 case SIOCS80211CHANNEL:
2589 case SIOCSLINKSTR:
2590 if (l != NULL) {
2591 error = kauth_authorize_network(l->l_cred,
2592 KAUTH_NETWORK_INTERFACE,
2593 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
2594 (void *)cmd, NULL);
2595 if (error != 0)
2596 goto out;
2597 }
2598 }
2599
2600 oif_flags = ifp->if_flags;
2601
2602 mutex_enter(ifp->if_ioctl_lock);
2603
2604 error = (*ifp->if_ioctl)(ifp, cmd, data);
2605 if (error != ENOTTY)
2606 ;
2607 else if (so->so_proto == NULL)
2608 error = EOPNOTSUPP;
2609 else {
2610 #ifdef COMPAT_OSOCK
2611 error = compat_ifioctl(so, ocmd, cmd, data, l);
2612 #else
2613 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
2614 cmd, data, ifp);
2615 #endif
2616 }
2617
2618 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
2619 if ((ifp->if_flags & IFF_UP) != 0) {
2620 int s = splnet();
2621 if_up(ifp);
2622 splx(s);
2623 }
2624 }
2625 #ifdef COMPAT_OIFREQ
2626 if (cmd != ocmd)
2627 ifreqn2o(oifr, ifr);
2628 #endif
2629
2630 mutex_exit(ifp->if_ioctl_lock);
2631 out:
2632 if_put(ifp, &psref);
2633 curlwp_bindx(bound);
2634 return error;
2635 }
2636
2637 /*
2638 * Return interface configuration
2639 * of system. List may be used
2640 * in later ioctl's (above) to get
2641 * other information.
2642 *
2643 * Each record is a struct ifreq. Before the addition of
2644 * sockaddr_storage, the API rule was that sockaddr flavors that did
2645 * not fit would extend beyond the struct ifreq, with the next struct
2646 * ifreq starting sa_len beyond the struct sockaddr. Because the
2647 * union in struct ifreq includes struct sockaddr_storage, every kind
2648 * of sockaddr must fit. Thus, there are no longer any overlength
2649 * records.
2650 *
2651 * Records are added to the user buffer if they fit, and ifc_len is
2652 * adjusted to the length that was written. Thus, the user is only
2653 * assured of getting the complete list if ifc_len on return is at
2654 * least sizeof(struct ifreq) less than it was on entry.
2655 *
2656 * If the user buffer pointer is NULL, this routine copies no data and
2657 * returns the amount of space that would be needed.
2658 *
2659 * Invariants:
2660 * ifrp points to the next part of the user's buffer to be used. If
2661 * ifrp != NULL, space holds the number of bytes remaining that we may
2662 * write at ifrp. Otherwise, space holds the number of bytes that
2663 * would have been written had there been adequate space.
2664 */
2665 /*ARGSUSED*/
2666 static int
2667 ifconf(u_long cmd, void *data)
2668 {
2669 struct ifconf *ifc = (struct ifconf *)data;
2670 struct ifnet *ifp;
2671 struct ifaddr *ifa;
2672 struct ifreq ifr, *ifrp = NULL;
2673 int space = 0, error = 0;
2674 const int sz = (int)sizeof(struct ifreq);
2675 const bool docopy = ifc->ifc_req != NULL;
2676 int s;
2677 int bound;
2678 struct psref psref;
2679
2680 if (docopy) {
2681 space = ifc->ifc_len;
2682 ifrp = ifc->ifc_req;
2683 }
2684
2685 bound = curlwp_bind();
2686 s = pserialize_read_enter();
2687 IFNET_READER_FOREACH(ifp) {
2688 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
2689 pserialize_read_exit(s);
2690
2691 (void)strncpy(ifr.ifr_name, ifp->if_xname,
2692 sizeof(ifr.ifr_name));
2693 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
2694 error = ENAMETOOLONG;
2695 goto release_exit;
2696 }
2697 if (IFADDR_EMPTY(ifp)) {
2698 /* Interface with no addresses - send zero sockaddr. */
2699 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2700 if (!docopy) {
2701 space += sz;
2702 continue;
2703 }
2704 if (space >= sz) {
2705 error = copyout(&ifr, ifrp, sz);
2706 if (error != 0)
2707 goto release_exit;
2708 ifrp++;
2709 space -= sz;
2710 }
2711 }
2712
2713 IFADDR_FOREACH(ifa, ifp) {
2714 struct sockaddr *sa = ifa->ifa_addr;
2715 /* all sockaddrs must fit in sockaddr_storage */
2716 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
2717
2718 if (!docopy) {
2719 space += sz;
2720 continue;
2721 }
2722 memcpy(&ifr.ifr_space, sa, sa->sa_len);
2723 if (space >= sz) {
2724 error = copyout(&ifr, ifrp, sz);
2725 if (error != 0)
2726 goto release_exit;
2727 ifrp++; space -= sz;
2728 }
2729 }
2730
2731 s = pserialize_read_enter();
2732 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
2733 }
2734 pserialize_read_exit(s);
2735 curlwp_bindx(bound);
2736
2737 if (docopy) {
2738 KASSERT(0 <= space && space <= ifc->ifc_len);
2739 ifc->ifc_len -= space;
2740 } else {
2741 KASSERT(space >= 0);
2742 ifc->ifc_len = space;
2743 }
2744 return (0);
2745
2746 release_exit:
2747 psref_release(&psref, &ifp->if_psref, ifnet_psref_class);
2748 curlwp_bindx(bound);
2749 return error;
2750 }
2751
2752 int
2753 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
2754 {
2755 uint8_t len;
2756 #ifdef COMPAT_OIFREQ
2757 struct ifreq ifrb;
2758 struct oifreq *oifr = NULL;
2759 u_long ocmd = cmd;
2760 cmd = compat_cvtcmd(cmd);
2761 if (cmd != ocmd) {
2762 oifr = (struct oifreq *)(void *)ifr;
2763 ifr = &ifrb;
2764 ifreqo2n(oifr, ifr);
2765 len = sizeof(oifr->ifr_addr);
2766 } else
2767 #endif
2768 len = sizeof(ifr->ifr_ifru.ifru_space);
2769
2770 if (len < sa->sa_len)
2771 return EFBIG;
2772
2773 memset(&ifr->ifr_addr, 0, len);
2774 sockaddr_copy(&ifr->ifr_addr, len, sa);
2775
2776 #ifdef COMPAT_OIFREQ
2777 if (cmd != ocmd)
2778 ifreqn2o(oifr, ifr);
2779 #endif
2780 return 0;
2781 }
2782
2783 /*
2784 * wrapper function for the drivers which doesn't have if_transmit().
2785 */
2786 static int
2787 if_transmit(struct ifnet *ifp, struct mbuf *m)
2788 {
2789 int s, error;
2790
2791 s = splnet();
2792
2793 /*
2794 * If NET_MPSAFE is not defined , IFQ_LOCK() is nop.
2795 * use KERNEL_LOCK instead of ifq_lock.
2796 */
2797 #ifndef NET_MPSAFE
2798 KERNEL_LOCK(1, NULL);
2799 #endif
2800 IFQ_ENQUEUE(&ifp->if_snd, m, error);
2801 #ifndef NET_MPSAFE
2802 KERNEL_UNLOCK_ONE(NULL);
2803 #endif
2804 if (error != 0) {
2805 /* mbuf is already freed */
2806 goto out;
2807 }
2808
2809 ifp->if_obytes += m->m_pkthdr.len;;
2810 if (m->m_flags & M_MCAST)
2811 ifp->if_omcasts++;
2812
2813 if ((ifp->if_flags & IFF_OACTIVE) == 0)
2814 if_start_lock(ifp);
2815 out:
2816 splx(s);
2817
2818 return error;
2819 }
2820
2821 int
2822 if_transmit_lock(struct ifnet *ifp, struct mbuf *m)
2823 {
2824 int error;
2825
2826 #ifdef ALTQ
2827 KERNEL_LOCK(1, NULL);
2828 if (ALTQ_IS_ENABLED(&ifp->if_snd)) {
2829 error = if_transmit(ifp, m);
2830 KERNEL_UNLOCK_ONE(NULL);
2831 } else {
2832 KERNEL_UNLOCK_ONE(NULL);
2833 error = (*ifp->if_transmit)(ifp, m);
2834 }
2835 #else /* !ALTQ */
2836 error = (*ifp->if_transmit)(ifp, m);
2837 #endif /* !ALTQ */
2838
2839 return error;
2840 }
2841
2842 /*
2843 * Queue message on interface, and start output if interface
2844 * not yet active.
2845 */
2846 int
2847 ifq_enqueue(struct ifnet *ifp, struct mbuf *m)
2848 {
2849
2850 return if_transmit_lock(ifp, m);
2851 }
2852
2853 /*
2854 * Queue message on interface, possibly using a second fast queue
2855 */
2856 int
2857 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m)
2858 {
2859 int error = 0;
2860
2861 if (ifq != NULL
2862 #ifdef ALTQ
2863 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
2864 #endif
2865 ) {
2866 if (IF_QFULL(ifq)) {
2867 IF_DROP(&ifp->if_snd);
2868 m_freem(m);
2869 if (error == 0)
2870 error = ENOBUFS;
2871 } else
2872 IF_ENQUEUE(ifq, m);
2873 } else
2874 IFQ_ENQUEUE(&ifp->if_snd, m, error);
2875 if (error != 0) {
2876 ++ifp->if_oerrors;
2877 return error;
2878 }
2879 return 0;
2880 }
2881
2882 int
2883 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
2884 {
2885 int rc;
2886
2887 if (ifp->if_initaddr != NULL)
2888 rc = (*ifp->if_initaddr)(ifp, ifa, src);
2889 else if (src ||
2890 /* FIXME: may not hold if_ioctl_lock */
2891 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
2892 rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
2893
2894 return rc;
2895 }
2896
2897 int
2898 if_do_dad(struct ifnet *ifp)
2899 {
2900 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2901 return 0;
2902
2903 switch (ifp->if_type) {
2904 case IFT_FAITH:
2905 /*
2906 * These interfaces do not have the IFF_LOOPBACK flag,
2907 * but loop packets back. We do not have to do DAD on such
2908 * interfaces. We should even omit it, because loop-backed
2909 * responses would confuse the DAD procedure.
2910 */
2911 return 0;
2912 default:
2913 /*
2914 * Our DAD routine requires the interface up and running.
2915 * However, some interfaces can be up before the RUNNING
2916 * status. Additionaly, users may try to assign addresses
2917 * before the interface becomes up (or running).
2918 * We simply skip DAD in such a case as a work around.
2919 * XXX: we should rather mark "tentative" on such addresses,
2920 * and do DAD after the interface becomes ready.
2921 */
2922 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2923 (IFF_UP|IFF_RUNNING))
2924 return 0;
2925
2926 return 1;
2927 }
2928 }
2929
2930 int
2931 if_flags_set(ifnet_t *ifp, const short flags)
2932 {
2933 int rc;
2934
2935 if (ifp->if_setflags != NULL)
2936 rc = (*ifp->if_setflags)(ifp, flags);
2937 else {
2938 short cantflags, chgdflags;
2939 struct ifreq ifr;
2940
2941 chgdflags = ifp->if_flags ^ flags;
2942 cantflags = chgdflags & IFF_CANTCHANGE;
2943
2944 if (cantflags != 0)
2945 ifp->if_flags ^= cantflags;
2946
2947 /* Traditionally, we do not call if_ioctl after
2948 * setting/clearing only IFF_PROMISC if the interface
2949 * isn't IFF_UP. Uphold that tradition.
2950 */
2951 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
2952 return 0;
2953
2954 memset(&ifr, 0, sizeof(ifr));
2955
2956 ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
2957 /* FIXME: may not hold if_ioctl_lock */
2958 rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
2959
2960 if (rc != 0 && cantflags != 0)
2961 ifp->if_flags ^= cantflags;
2962 }
2963
2964 return rc;
2965 }
2966
2967 int
2968 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
2969 {
2970 int rc;
2971 struct ifreq ifr;
2972
2973 if (ifp->if_mcastop != NULL)
2974 rc = (*ifp->if_mcastop)(ifp, cmd, sa);
2975 else {
2976 ifreq_setaddr(cmd, &ifr, sa);
2977 rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
2978 }
2979
2980 return rc;
2981 }
2982
2983 static void
2984 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
2985 struct ifaltq *ifq)
2986 {
2987 const struct sysctlnode *cnode, *rnode;
2988
2989 if (sysctl_createv(clog, 0, NULL, &rnode,
2990 CTLFLAG_PERMANENT,
2991 CTLTYPE_NODE, "interfaces",
2992 SYSCTL_DESCR("Per-interface controls"),
2993 NULL, 0, NULL, 0,
2994 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
2995 goto bad;
2996
2997 if (sysctl_createv(clog, 0, &rnode, &rnode,
2998 CTLFLAG_PERMANENT,
2999 CTLTYPE_NODE, ifname,
3000 SYSCTL_DESCR("Interface controls"),
3001 NULL, 0, NULL, 0,
3002 CTL_CREATE, CTL_EOL) != 0)
3003 goto bad;
3004
3005 if (sysctl_createv(clog, 0, &rnode, &rnode,
3006 CTLFLAG_PERMANENT,
3007 CTLTYPE_NODE, "sndq",
3008 SYSCTL_DESCR("Interface output queue controls"),
3009 NULL, 0, NULL, 0,
3010 CTL_CREATE, CTL_EOL) != 0)
3011 goto bad;
3012
3013 if (sysctl_createv(clog, 0, &rnode, &cnode,
3014 CTLFLAG_PERMANENT,
3015 CTLTYPE_INT, "len",
3016 SYSCTL_DESCR("Current output queue length"),
3017 NULL, 0, &ifq->ifq_len, 0,
3018 CTL_CREATE, CTL_EOL) != 0)
3019 goto bad;
3020
3021 if (sysctl_createv(clog, 0, &rnode, &cnode,
3022 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3023 CTLTYPE_INT, "maxlen",
3024 SYSCTL_DESCR("Maximum allowed output queue length"),
3025 NULL, 0, &ifq->ifq_maxlen, 0,
3026 CTL_CREATE, CTL_EOL) != 0)
3027 goto bad;
3028
3029 if (sysctl_createv(clog, 0, &rnode, &cnode,
3030 CTLFLAG_PERMANENT,
3031 CTLTYPE_INT, "drops",
3032 SYSCTL_DESCR("Packets dropped due to full output queue"),
3033 NULL, 0, &ifq->ifq_drops, 0,
3034 CTL_CREATE, CTL_EOL) != 0)
3035 goto bad;
3036
3037 return;
3038 bad:
3039 printf("%s: could not attach sysctl nodes\n", ifname);
3040 return;
3041 }
3042
3043 #if defined(INET) || defined(INET6)
3044
3045 #define SYSCTL_NET_PKTQ(q, cn, c) \
3046 static int \
3047 sysctl_net_##q##_##cn(SYSCTLFN_ARGS) \
3048 { \
3049 return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c); \
3050 }
3051
3052 #if defined(INET)
3053 static int
3054 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
3055 {
3056 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
3057 }
3058 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
3059 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
3060 #endif
3061
3062 #if defined(INET6)
3063 static int
3064 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
3065 {
3066 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
3067 }
3068 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
3069 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
3070 #endif
3071
3072 static void
3073 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
3074 {
3075 sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
3076 const char *pfname = NULL, *ipname = NULL;
3077 int ipn = 0, qid = 0;
3078
3079 switch (pf) {
3080 #if defined(INET)
3081 case PF_INET:
3082 len_func = sysctl_net_ip_pktq_items;
3083 maxlen_func = sysctl_net_ip_pktq_maxlen;
3084 drops_func = sysctl_net_ip_pktq_drops;
3085 pfname = "inet", ipn = IPPROTO_IP;
3086 ipname = "ip", qid = IPCTL_IFQ;
3087 break;
3088 #endif
3089 #if defined(INET6)
3090 case PF_INET6:
3091 len_func = sysctl_net_ip6_pktq_items;
3092 maxlen_func = sysctl_net_ip6_pktq_maxlen;
3093 drops_func = sysctl_net_ip6_pktq_drops;
3094 pfname = "inet6", ipn = IPPROTO_IPV6;
3095 ipname = "ip6", qid = IPV6CTL_IFQ;
3096 break;
3097 #endif
3098 default:
3099 KASSERT(false);
3100 }
3101
3102 sysctl_createv(clog, 0, NULL, NULL,
3103 CTLFLAG_PERMANENT,
3104 CTLTYPE_NODE, pfname, NULL,
3105 NULL, 0, NULL, 0,
3106 CTL_NET, pf, CTL_EOL);
3107 sysctl_createv(clog, 0, NULL, NULL,
3108 CTLFLAG_PERMANENT,
3109 CTLTYPE_NODE, ipname, NULL,
3110 NULL, 0, NULL, 0,
3111 CTL_NET, pf, ipn, CTL_EOL);
3112 sysctl_createv(clog, 0, NULL, NULL,
3113 CTLFLAG_PERMANENT,
3114 CTLTYPE_NODE, "ifq",
3115 SYSCTL_DESCR("Protocol input queue controls"),
3116 NULL, 0, NULL, 0,
3117 CTL_NET, pf, ipn, qid, CTL_EOL);
3118
3119 sysctl_createv(clog, 0, NULL, NULL,
3120 CTLFLAG_PERMANENT,
3121 CTLTYPE_INT, "len",
3122 SYSCTL_DESCR("Current input queue length"),
3123 len_func, 0, NULL, 0,
3124 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
3125 sysctl_createv(clog, 0, NULL, NULL,
3126 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3127 CTLTYPE_INT, "maxlen",
3128 SYSCTL_DESCR("Maximum allowed input queue length"),
3129 maxlen_func, 0, NULL, 0,
3130 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
3131 sysctl_createv(clog, 0, NULL, NULL,
3132 CTLFLAG_PERMANENT,
3133 CTLTYPE_INT, "drops",
3134 SYSCTL_DESCR("Packets dropped due to full input queue"),
3135 drops_func, 0, NULL, 0,
3136 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
3137 }
3138 #endif /* INET || INET6 */
3139
3140 static int
3141 if_sdl_sysctl(SYSCTLFN_ARGS)
3142 {
3143 struct ifnet *ifp;
3144 const struct sockaddr_dl *sdl;
3145 struct psref psref;
3146 int error = 0;
3147 int bound;
3148
3149 if (namelen != 1)
3150 return EINVAL;
3151
3152 bound = curlwp_bind();
3153 ifp = if_get_byindex(name[0], &psref);
3154 if (ifp == NULL) {
3155 error = ENODEV;
3156 goto out0;
3157 }
3158
3159 sdl = ifp->if_sadl;
3160 if (sdl == NULL) {
3161 *oldlenp = 0;
3162 goto out1;
3163 }
3164
3165 if (oldp == NULL) {
3166 *oldlenp = sdl->sdl_alen;
3167 goto out1;
3168 }
3169
3170 if (*oldlenp >= sdl->sdl_alen)
3171 *oldlenp = sdl->sdl_alen;
3172 error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
3173 out1:
3174 if_put(ifp, &psref);
3175 out0:
3176 curlwp_bindx(bound);
3177 return error;
3178 }
3179
3180 SYSCTL_SETUP(sysctl_net_sdl_setup, "sysctl net.sdl subtree setup")
3181 {
3182 const struct sysctlnode *rnode = NULL;
3183
3184 sysctl_createv(clog, 0, NULL, &rnode,
3185 CTLFLAG_PERMANENT,
3186 CTLTYPE_NODE, "sdl",
3187 SYSCTL_DESCR("Get active link-layer address"),
3188 if_sdl_sysctl, 0, NULL, 0,
3189 CTL_NET, CTL_CREATE, CTL_EOL);
3190 }
3191