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