if.c revision 1.311 1 /* $NetBSD: if.c,v 1.311 2015/04/21 10:39:41 pooka 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.311 2015/04/21 10:39:41 pooka 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 #if defined(DEBUG) || defined(INET6)
1427 int old_link_state;
1428 #endif
1429
1430 s = splnet();
1431 if (ifp->if_link_state == link_state) {
1432 splx(s);
1433 return;
1434 }
1435
1436 #if defined(DEBUG) || defined(INET6)
1437 old_link_state = ifp->if_link_state;
1438 #endif
1439 ifp->if_link_state = link_state;
1440 #ifdef DEBUG
1441 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
1442 link_state == LINK_STATE_UP ? "UP" :
1443 link_state == LINK_STATE_DOWN ? "DOWN" :
1444 "UNKNOWN",
1445 old_link_state == LINK_STATE_UP ? "UP" :
1446 old_link_state == LINK_STATE_DOWN ? "DOWN" :
1447 "UNKNOWN");
1448 #endif
1449
1450 #ifdef INET6
1451 /*
1452 * When going from UNKNOWN to UP, we need to mark existing
1453 * IPv6 addresses as tentative and restart DAD as we may have
1454 * erroneously not found a duplicate.
1455 *
1456 * This needs to happen before rt_ifmsg to avoid a race where
1457 * listeners would have an address and expect it to work right
1458 * away.
1459 */
1460 if (in6_present && link_state == LINK_STATE_UP &&
1461 old_link_state == LINK_STATE_UNKNOWN)
1462 in6_if_link_down(ifp);
1463 #endif
1464
1465 /* Notify that the link state has changed. */
1466 rt_ifmsg(ifp);
1467
1468 #if NCARP > 0
1469 if (ifp->if_carp)
1470 carp_carpdev_state(ifp);
1471 #endif
1472
1473 #ifdef INET6
1474 if (in6_present) {
1475 if (link_state == LINK_STATE_DOWN)
1476 in6_if_link_down(ifp);
1477 else if (link_state == LINK_STATE_UP)
1478 in6_if_link_up(ifp);
1479 }
1480 #endif
1481
1482 splx(s);
1483 }
1484
1485 /*
1486 * Default action when installing a local route on a point-to-point
1487 * interface.
1488 */
1489 void
1490 p2p_rtrequest(int req, struct rtentry *rt,
1491 __unused const struct rt_addrinfo *info)
1492 {
1493 struct ifnet *ifp = rt->rt_ifp;
1494 struct ifaddr *ifa, *lo0ifa;
1495
1496 switch (req) {
1497 case RTM_ADD:
1498 if ((rt->rt_flags & RTF_LOCAL) == 0)
1499 break;
1500
1501 IFADDR_FOREACH(ifa, ifp) {
1502 if (equal(rt_getkey(rt), ifa->ifa_addr))
1503 break;
1504 }
1505 if (ifa == NULL)
1506 break;
1507
1508 /*
1509 * Ensure lo0 has an address of the same family.
1510 */
1511 IFADDR_FOREACH(lo0ifa, lo0ifp) {
1512 if (lo0ifa->ifa_addr->sa_family ==
1513 ifa->ifa_addr->sa_family)
1514 break;
1515 }
1516 if (lo0ifa == NULL)
1517 break;
1518
1519 rt->rt_ifp = lo0ifp;
1520 rt->rt_flags &= ~RTF_LLINFO;
1521
1522 /*
1523 * Make sure to set rt->rt_ifa to the interface
1524 * address we are using, otherwise we will have trouble
1525 * with source address selection.
1526 */
1527 if (ifa != rt->rt_ifa)
1528 rt_replace_ifa(rt, ifa);
1529 break;
1530 case RTM_DELETE:
1531 case RTM_RESOLVE:
1532 default:
1533 break;
1534 }
1535 }
1536
1537 /*
1538 * Mark an interface down and notify protocols of
1539 * the transition.
1540 * NOTE: must be called at splsoftnet or equivalent.
1541 */
1542 void
1543 if_down(struct ifnet *ifp)
1544 {
1545 struct ifaddr *ifa;
1546
1547 ifp->if_flags &= ~IFF_UP;
1548 nanotime(&ifp->if_lastchange);
1549 IFADDR_FOREACH(ifa, ifp)
1550 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1551 IFQ_PURGE(&ifp->if_snd);
1552 #if NCARP > 0
1553 if (ifp->if_carp)
1554 carp_carpdev_state(ifp);
1555 #endif
1556 rt_ifmsg(ifp);
1557 #ifdef INET6
1558 if (in6_present)
1559 in6_if_down(ifp);
1560 #endif
1561 }
1562
1563 /*
1564 * Mark an interface up and notify protocols of
1565 * the transition.
1566 * NOTE: must be called at splsoftnet or equivalent.
1567 */
1568 void
1569 if_up(struct ifnet *ifp)
1570 {
1571 #ifdef notyet
1572 struct ifaddr *ifa;
1573 #endif
1574
1575 ifp->if_flags |= IFF_UP;
1576 nanotime(&ifp->if_lastchange);
1577 #ifdef notyet
1578 /* this has no effect on IP, and will kill all ISO connections XXX */
1579 IFADDR_FOREACH(ifa, ifp)
1580 pfctlinput(PRC_IFUP, ifa->ifa_addr);
1581 #endif
1582 #if NCARP > 0
1583 if (ifp->if_carp)
1584 carp_carpdev_state(ifp);
1585 #endif
1586 rt_ifmsg(ifp);
1587 #ifdef INET6
1588 if (in6_present)
1589 in6_if_up(ifp);
1590 #endif
1591 }
1592
1593 /*
1594 * Handle interface slowtimo timer routine. Called
1595 * from softclock, we decrement timer (if set) and
1596 * call the appropriate interface routine on expiration.
1597 */
1598 static void
1599 if_slowtimo(void *arg)
1600 {
1601 void (*slowtimo)(struct ifnet *);
1602 struct ifnet *ifp = arg;
1603 int s;
1604
1605 slowtimo = ifp->if_slowtimo;
1606 if (__predict_false(slowtimo == NULL))
1607 return;
1608
1609 s = splnet();
1610 if (ifp->if_timer != 0 && --ifp->if_timer == 0)
1611 (*slowtimo)(ifp);
1612
1613 splx(s);
1614
1615 if (__predict_true(ifp->if_slowtimo != NULL))
1616 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
1617 }
1618
1619 /*
1620 * Set/clear promiscuous mode on interface ifp based on the truth value
1621 * of pswitch. The calls are reference counted so that only the first
1622 * "on" request actually has an effect, as does the final "off" request.
1623 * Results are undefined if the "off" and "on" requests are not matched.
1624 */
1625 int
1626 ifpromisc(struct ifnet *ifp, int pswitch)
1627 {
1628 int pcount, ret;
1629 short nflags;
1630
1631 pcount = ifp->if_pcount;
1632 if (pswitch) {
1633 /*
1634 * Allow the device to be "placed" into promiscuous
1635 * mode even if it is not configured up. It will
1636 * consult IFF_PROMISC when it is brought up.
1637 */
1638 if (ifp->if_pcount++ != 0)
1639 return 0;
1640 nflags = ifp->if_flags | IFF_PROMISC;
1641 } else {
1642 if (--ifp->if_pcount > 0)
1643 return 0;
1644 nflags = ifp->if_flags & ~IFF_PROMISC;
1645 }
1646 ret = if_flags_set(ifp, nflags);
1647 /* Restore interface state if not successful. */
1648 if (ret != 0) {
1649 ifp->if_pcount = pcount;
1650 }
1651 return ret;
1652 }
1653
1654 /*
1655 * Map interface name to
1656 * interface structure pointer.
1657 */
1658 struct ifnet *
1659 ifunit(const char *name)
1660 {
1661 struct ifnet *ifp;
1662 const char *cp = name;
1663 u_int unit = 0;
1664 u_int i;
1665
1666 /*
1667 * If the entire name is a number, treat it as an ifindex.
1668 */
1669 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
1670 unit = unit * 10 + (*cp - '0');
1671 }
1672
1673 /*
1674 * If the number took all of the name, then it's a valid ifindex.
1675 */
1676 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) {
1677 if (unit >= if_indexlim)
1678 return NULL;
1679 ifp = ifindex2ifnet[unit];
1680 if (ifp == NULL || ifp->if_output == if_nulloutput)
1681 return NULL;
1682 return ifp;
1683 }
1684
1685 IFNET_FOREACH(ifp) {
1686 if (ifp->if_output == if_nulloutput)
1687 continue;
1688 if (strcmp(ifp->if_xname, name) == 0)
1689 return ifp;
1690 }
1691 return NULL;
1692 }
1693
1694 ifnet_t *
1695 if_byindex(u_int idx)
1696 {
1697 return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL;
1698 }
1699
1700 /* common */
1701 int
1702 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
1703 {
1704 int s;
1705 struct ifreq *ifr;
1706 struct ifcapreq *ifcr;
1707 struct ifdatareq *ifdr;
1708
1709 switch (cmd) {
1710 case SIOCSIFCAP:
1711 ifcr = data;
1712 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
1713 return EINVAL;
1714
1715 if (ifcr->ifcr_capenable == ifp->if_capenable)
1716 return 0;
1717
1718 ifp->if_capenable = ifcr->ifcr_capenable;
1719
1720 /* Pre-compute the checksum flags mask. */
1721 ifp->if_csum_flags_tx = 0;
1722 ifp->if_csum_flags_rx = 0;
1723 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) {
1724 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
1725 }
1726 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) {
1727 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
1728 }
1729
1730 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) {
1731 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
1732 }
1733 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) {
1734 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
1735 }
1736
1737 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) {
1738 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
1739 }
1740 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) {
1741 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
1742 }
1743
1744 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) {
1745 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
1746 }
1747 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) {
1748 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
1749 }
1750
1751 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) {
1752 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
1753 }
1754 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) {
1755 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
1756 }
1757 if (ifp->if_flags & IFF_UP)
1758 return ENETRESET;
1759 return 0;
1760 case SIOCSIFFLAGS:
1761 ifr = data;
1762 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
1763 s = splnet();
1764 if_down(ifp);
1765 splx(s);
1766 }
1767 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
1768 s = splnet();
1769 if_up(ifp);
1770 splx(s);
1771 }
1772 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1773 (ifr->ifr_flags &~ IFF_CANTCHANGE);
1774 break;
1775 case SIOCGIFFLAGS:
1776 ifr = data;
1777 ifr->ifr_flags = ifp->if_flags;
1778 break;
1779
1780 case SIOCGIFMETRIC:
1781 ifr = data;
1782 ifr->ifr_metric = ifp->if_metric;
1783 break;
1784
1785 case SIOCGIFMTU:
1786 ifr = data;
1787 ifr->ifr_mtu = ifp->if_mtu;
1788 break;
1789
1790 case SIOCGIFDLT:
1791 ifr = data;
1792 ifr->ifr_dlt = ifp->if_dlt;
1793 break;
1794
1795 case SIOCGIFCAP:
1796 ifcr = data;
1797 ifcr->ifcr_capabilities = ifp->if_capabilities;
1798 ifcr->ifcr_capenable = ifp->if_capenable;
1799 break;
1800
1801 case SIOCSIFMETRIC:
1802 ifr = data;
1803 ifp->if_metric = ifr->ifr_metric;
1804 break;
1805
1806 case SIOCGIFDATA:
1807 ifdr = data;
1808 ifdr->ifdr_data = ifp->if_data;
1809 break;
1810
1811 case SIOCGIFINDEX:
1812 ifr = data;
1813 ifr->ifr_index = ifp->if_index;
1814 break;
1815
1816 case SIOCZIFDATA:
1817 ifdr = data;
1818 ifdr->ifdr_data = ifp->if_data;
1819 /*
1820 * Assumes that the volatile counters that can be
1821 * zero'ed are at the end of if_data.
1822 */
1823 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) -
1824 offsetof(struct if_data, ifi_ipackets));
1825 /*
1826 * The memset() clears to the bottm of if_data. In the area,
1827 * if_lastchange is included. Please be careful if new entry
1828 * will be added into if_data or rewite this.
1829 *
1830 * And also, update if_lastchnage.
1831 */
1832 getnanotime(&ifp->if_lastchange);
1833 break;
1834 case SIOCSIFMTU:
1835 ifr = data;
1836 if (ifp->if_mtu == ifr->ifr_mtu)
1837 break;
1838 ifp->if_mtu = ifr->ifr_mtu;
1839 /*
1840 * If the link MTU changed, do network layer specific procedure.
1841 */
1842 #ifdef INET6
1843 if (in6_present)
1844 nd6_setmtu(ifp);
1845 #endif
1846 return ENETRESET;
1847 default:
1848 return ENOTTY;
1849 }
1850 return 0;
1851 }
1852
1853 int
1854 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
1855 {
1856 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
1857 struct ifaddr *ifa;
1858 const struct sockaddr *any, *sa;
1859 union {
1860 struct sockaddr sa;
1861 struct sockaddr_storage ss;
1862 } u, v;
1863
1864 switch (cmd) {
1865 case SIOCSIFADDRPREF:
1866 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
1867 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1868 NULL) != 0)
1869 return EPERM;
1870 case SIOCGIFADDRPREF:
1871 break;
1872 default:
1873 return EOPNOTSUPP;
1874 }
1875
1876 /* sanity checks */
1877 if (data == NULL || ifp == NULL) {
1878 panic("invalid argument to %s", __func__);
1879 /*NOTREACHED*/
1880 }
1881
1882 /* address must be specified on ADD and DELETE */
1883 sa = sstocsa(&ifap->ifap_addr);
1884 if (sa->sa_family != sofamily(so))
1885 return EINVAL;
1886 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
1887 return EINVAL;
1888
1889 sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
1890
1891 IFADDR_FOREACH(ifa, ifp) {
1892 if (ifa->ifa_addr->sa_family != sa->sa_family)
1893 continue;
1894 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
1895 if (sockaddr_cmp(&u.sa, &v.sa) == 0)
1896 break;
1897 }
1898 if (ifa == NULL)
1899 return EADDRNOTAVAIL;
1900
1901 switch (cmd) {
1902 case SIOCSIFADDRPREF:
1903 ifa->ifa_preference = ifap->ifap_preference;
1904 return 0;
1905 case SIOCGIFADDRPREF:
1906 /* fill in the if_laddrreq structure */
1907 (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
1908 sizeof(ifap->ifap_addr), ifa->ifa_addr);
1909 ifap->ifap_preference = ifa->ifa_preference;
1910 return 0;
1911 default:
1912 return EOPNOTSUPP;
1913 }
1914 }
1915
1916 static void
1917 ifnet_lock_enter(struct ifnet_lock *il)
1918 {
1919 uint64_t *nenter;
1920
1921 /* Before trying to acquire the mutex, increase the count of threads
1922 * who have entered or who wait to enter the critical section.
1923 * Avoid one costly locked memory transaction by keeping a count for
1924 * each CPU.
1925 */
1926 nenter = percpu_getref(il->il_nenter);
1927 (*nenter)++;
1928 percpu_putref(il->il_nenter);
1929 mutex_enter(&il->il_lock);
1930 }
1931
1932 static void
1933 ifnet_lock_exit(struct ifnet_lock *il)
1934 {
1935 /* Increase the count of threads who have exited the critical
1936 * section. Increase while we still hold the lock.
1937 */
1938 il->il_nexit++;
1939 mutex_exit(&il->il_lock);
1940 }
1941
1942 /*
1943 * Interface ioctls.
1944 */
1945 static int
1946 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
1947 {
1948 struct ifnet *ifp;
1949 struct ifreq *ifr;
1950 int error = 0;
1951 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
1952 u_long ocmd = cmd;
1953 #endif
1954 short oif_flags;
1955 #ifdef COMPAT_OIFREQ
1956 struct ifreq ifrb;
1957 struct oifreq *oifr = NULL;
1958 #endif
1959 int r;
1960
1961 switch (cmd) {
1962 #ifdef COMPAT_OIFREQ
1963 case OSIOCGIFCONF:
1964 case OOSIOCGIFCONF:
1965 return compat_ifconf(cmd, data);
1966 #endif
1967 #ifdef COMPAT_OIFDATA
1968 case OSIOCGIFDATA:
1969 case OSIOCZIFDATA:
1970 return compat_ifdatareq(l, cmd, data);
1971 #endif
1972 case SIOCGIFCONF:
1973 return ifconf(cmd, data);
1974 case SIOCINITIFADDR:
1975 return EPERM;
1976 }
1977
1978 #ifdef COMPAT_OIFREQ
1979 cmd = compat_cvtcmd(cmd);
1980 if (cmd != ocmd) {
1981 oifr = data;
1982 data = ifr = &ifrb;
1983 ifreqo2n(oifr, ifr);
1984 } else
1985 #endif
1986 ifr = data;
1987
1988 ifp = ifunit(ifr->ifr_name);
1989
1990 switch (cmd) {
1991 case SIOCIFCREATE:
1992 case SIOCIFDESTROY:
1993 if (l != NULL) {
1994 error = kauth_authorize_network(l->l_cred,
1995 KAUTH_NETWORK_INTERFACE,
1996 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
1997 (void *)cmd, NULL);
1998 if (error != 0)
1999 return error;
2000 }
2001 mutex_enter(&if_clone_mtx);
2002 r = (cmd == SIOCIFCREATE) ?
2003 if_clone_create(ifr->ifr_name) :
2004 if_clone_destroy(ifr->ifr_name);
2005 mutex_exit(&if_clone_mtx);
2006 return r;
2007
2008 case SIOCIFGCLONERS:
2009 return if_clone_list((struct if_clonereq *)data);
2010 }
2011
2012 if (ifp == NULL)
2013 return ENXIO;
2014
2015 switch (cmd) {
2016 case SIOCALIFADDR:
2017 case SIOCDLIFADDR:
2018 case SIOCSIFADDRPREF:
2019 case SIOCSIFFLAGS:
2020 case SIOCSIFCAP:
2021 case SIOCSIFMETRIC:
2022 case SIOCZIFDATA:
2023 case SIOCSIFMTU:
2024 case SIOCSIFPHYADDR:
2025 case SIOCDIFPHYADDR:
2026 #ifdef INET6
2027 case SIOCSIFPHYADDR_IN6:
2028 #endif
2029 case SIOCSLIFPHYADDR:
2030 case SIOCADDMULTI:
2031 case SIOCDELMULTI:
2032 case SIOCSIFMEDIA:
2033 case SIOCSDRVSPEC:
2034 case SIOCG80211:
2035 case SIOCS80211:
2036 case SIOCS80211NWID:
2037 case SIOCS80211NWKEY:
2038 case SIOCS80211POWER:
2039 case SIOCS80211BSSID:
2040 case SIOCS80211CHANNEL:
2041 case SIOCSLINKSTR:
2042 if (l != NULL) {
2043 error = kauth_authorize_network(l->l_cred,
2044 KAUTH_NETWORK_INTERFACE,
2045 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
2046 (void *)cmd, NULL);
2047 if (error != 0)
2048 return error;
2049 }
2050 }
2051
2052 oif_flags = ifp->if_flags;
2053
2054 ifnet_lock_enter(ifp->if_ioctl_lock);
2055 error = (*ifp->if_ioctl)(ifp, cmd, data);
2056 if (error != ENOTTY)
2057 ;
2058 else if (so->so_proto == NULL)
2059 error = EOPNOTSUPP;
2060 else {
2061 #ifdef COMPAT_OSOCK
2062 error = compat_ifioctl(so, ocmd, cmd, data, l);
2063 #else
2064 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
2065 cmd, data, ifp);
2066 #endif
2067 }
2068
2069 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
2070 #ifdef INET6
2071 if (in6_present && (ifp->if_flags & IFF_UP) != 0) {
2072 int s = splnet();
2073 in6_if_up(ifp);
2074 splx(s);
2075 }
2076 #endif
2077 }
2078 #ifdef COMPAT_OIFREQ
2079 if (cmd != ocmd)
2080 ifreqn2o(oifr, ifr);
2081 #endif
2082
2083 ifnet_lock_exit(ifp->if_ioctl_lock);
2084 return error;
2085 }
2086
2087 /* This callback adds to the sum in `arg' the number of
2088 * threads on `ci' who have entered or who wait to enter the
2089 * critical section.
2090 */
2091 static void
2092 ifnet_lock_sum(void *p, void *arg, struct cpu_info *ci)
2093 {
2094 uint64_t *sum = arg, *nenter = p;
2095
2096 *sum += *nenter;
2097 }
2098
2099 /* Return the number of threads who have entered or who wait
2100 * to enter the critical section on all CPUs.
2101 */
2102 static uint64_t
2103 ifnet_lock_entrances(struct ifnet_lock *il)
2104 {
2105 uint64_t sum = 0;
2106
2107 percpu_foreach(il->il_nenter, ifnet_lock_sum, &sum);
2108
2109 return sum;
2110 }
2111
2112 static int
2113 ifioctl_attach(struct ifnet *ifp)
2114 {
2115 struct ifnet_lock *il;
2116
2117 /* If the driver has not supplied its own if_ioctl, then
2118 * supply the default.
2119 */
2120 if (ifp->if_ioctl == NULL)
2121 ifp->if_ioctl = ifioctl_common;
2122
2123 /* Create an ifnet_lock for synchronizing ifioctls. */
2124 if ((il = kmem_zalloc(sizeof(*il), KM_SLEEP)) == NULL)
2125 return ENOMEM;
2126
2127 il->il_nenter = percpu_alloc(sizeof(uint64_t));
2128 if (il->il_nenter == NULL) {
2129 kmem_free(il, sizeof(*il));
2130 return ENOMEM;
2131 }
2132
2133 mutex_init(&il->il_lock, MUTEX_DEFAULT, IPL_NONE);
2134 cv_init(&il->il_emptied, ifp->if_xname);
2135
2136 ifp->if_ioctl_lock = il;
2137
2138 return 0;
2139 }
2140
2141 /*
2142 * This must not be called until after `ifp' has been withdrawn from the
2143 * ifnet tables so that ifioctl() cannot get a handle on it by calling
2144 * ifunit().
2145 */
2146 static void
2147 ifioctl_detach(struct ifnet *ifp)
2148 {
2149 struct ifnet_lock *il;
2150
2151 il = ifp->if_ioctl_lock;
2152 mutex_enter(&il->il_lock);
2153 /* Install if_nullioctl to make sure that any thread that
2154 * subsequently enters the critical section will quit it
2155 * immediately and signal the condition variable that we
2156 * wait on, below.
2157 */
2158 ifp->if_ioctl = if_nullioctl;
2159 /* Sleep while threads are still in the critical section or
2160 * wait to enter it.
2161 */
2162 while (ifnet_lock_entrances(il) != il->il_nexit)
2163 cv_wait(&il->il_emptied, &il->il_lock);
2164 /* At this point, we are the only thread still in the critical
2165 * section, and no new thread can get a handle on the ifioctl
2166 * lock, so it is safe to free its memory.
2167 */
2168 mutex_exit(&il->il_lock);
2169 ifp->if_ioctl_lock = NULL;
2170 percpu_free(il->il_nenter, sizeof(uint64_t));
2171 il->il_nenter = NULL;
2172 cv_destroy(&il->il_emptied);
2173 mutex_destroy(&il->il_lock);
2174 kmem_free(il, sizeof(*il));
2175 }
2176
2177 /*
2178 * Return interface configuration
2179 * of system. List may be used
2180 * in later ioctl's (above) to get
2181 * other information.
2182 *
2183 * Each record is a struct ifreq. Before the addition of
2184 * sockaddr_storage, the API rule was that sockaddr flavors that did
2185 * not fit would extend beyond the struct ifreq, with the next struct
2186 * ifreq starting sa_len beyond the struct sockaddr. Because the
2187 * union in struct ifreq includes struct sockaddr_storage, every kind
2188 * of sockaddr must fit. Thus, there are no longer any overlength
2189 * records.
2190 *
2191 * Records are added to the user buffer if they fit, and ifc_len is
2192 * adjusted to the length that was written. Thus, the user is only
2193 * assured of getting the complete list if ifc_len on return is at
2194 * least sizeof(struct ifreq) less than it was on entry.
2195 *
2196 * If the user buffer pointer is NULL, this routine copies no data and
2197 * returns the amount of space that would be needed.
2198 *
2199 * Invariants:
2200 * ifrp points to the next part of the user's buffer to be used. If
2201 * ifrp != NULL, space holds the number of bytes remaining that we may
2202 * write at ifrp. Otherwise, space holds the number of bytes that
2203 * would have been written had there been adequate space.
2204 */
2205 /*ARGSUSED*/
2206 static int
2207 ifconf(u_long cmd, void *data)
2208 {
2209 struct ifconf *ifc = (struct ifconf *)data;
2210 struct ifnet *ifp;
2211 struct ifaddr *ifa;
2212 struct ifreq ifr, *ifrp = NULL;
2213 int space = 0, error = 0;
2214 const int sz = (int)sizeof(struct ifreq);
2215 const bool docopy = ifc->ifc_req != NULL;
2216
2217 if (docopy) {
2218 space = ifc->ifc_len;
2219 ifrp = ifc->ifc_req;
2220 }
2221
2222 IFNET_FOREACH(ifp) {
2223 (void)strncpy(ifr.ifr_name, ifp->if_xname,
2224 sizeof(ifr.ifr_name));
2225 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
2226 return ENAMETOOLONG;
2227 if (IFADDR_EMPTY(ifp)) {
2228 /* Interface with no addresses - send zero sockaddr. */
2229 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
2230 if (!docopy) {
2231 space += sz;
2232 continue;
2233 }
2234 if (space >= sz) {
2235 error = copyout(&ifr, ifrp, sz);
2236 if (error != 0)
2237 return error;
2238 ifrp++;
2239 space -= sz;
2240 }
2241 }
2242
2243 IFADDR_FOREACH(ifa, ifp) {
2244 struct sockaddr *sa = ifa->ifa_addr;
2245 /* all sockaddrs must fit in sockaddr_storage */
2246 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
2247
2248 if (!docopy) {
2249 space += sz;
2250 continue;
2251 }
2252 memcpy(&ifr.ifr_space, sa, sa->sa_len);
2253 if (space >= sz) {
2254 error = copyout(&ifr, ifrp, sz);
2255 if (error != 0)
2256 return (error);
2257 ifrp++; space -= sz;
2258 }
2259 }
2260 }
2261 if (docopy) {
2262 KASSERT(0 <= space && space <= ifc->ifc_len);
2263 ifc->ifc_len -= space;
2264 } else {
2265 KASSERT(space >= 0);
2266 ifc->ifc_len = space;
2267 }
2268 return (0);
2269 }
2270
2271 int
2272 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa)
2273 {
2274 uint8_t len;
2275 #ifdef COMPAT_OIFREQ
2276 struct ifreq ifrb;
2277 struct oifreq *oifr = NULL;
2278 u_long ocmd = cmd;
2279 cmd = compat_cvtcmd(cmd);
2280 if (cmd != ocmd) {
2281 oifr = (struct oifreq *)(void *)ifr;
2282 ifr = &ifrb;
2283 ifreqo2n(oifr, ifr);
2284 len = sizeof(oifr->ifr_addr);
2285 } else
2286 #endif
2287 len = sizeof(ifr->ifr_ifru.ifru_space);
2288
2289 if (len < sa->sa_len)
2290 return EFBIG;
2291
2292 memset(&ifr->ifr_addr, 0, len);
2293 sockaddr_copy(&ifr->ifr_addr, len, sa);
2294
2295 #ifdef COMPAT_OIFREQ
2296 if (cmd != ocmd)
2297 ifreqn2o(oifr, ifr);
2298 #endif
2299 return 0;
2300 }
2301
2302 /*
2303 * Queue message on interface, and start output if interface
2304 * not yet active.
2305 */
2306 int
2307 ifq_enqueue(struct ifnet *ifp, struct mbuf *m
2308 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
2309 {
2310 int len = m->m_pkthdr.len;
2311 int mflags = m->m_flags;
2312 int s = splnet();
2313 int error;
2314
2315 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
2316 if (error != 0)
2317 goto out;
2318 ifp->if_obytes += len;
2319 if (mflags & M_MCAST)
2320 ifp->if_omcasts++;
2321 if ((ifp->if_flags & IFF_OACTIVE) == 0)
2322 (*ifp->if_start)(ifp);
2323 out:
2324 splx(s);
2325 return error;
2326 }
2327
2328 /*
2329 * Queue message on interface, possibly using a second fast queue
2330 */
2331 int
2332 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m
2333 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr))
2334 {
2335 int error = 0;
2336
2337 if (ifq != NULL
2338 #ifdef ALTQ
2339 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0
2340 #endif
2341 ) {
2342 if (IF_QFULL(ifq)) {
2343 IF_DROP(&ifp->if_snd);
2344 m_freem(m);
2345 if (error == 0)
2346 error = ENOBUFS;
2347 } else
2348 IF_ENQUEUE(ifq, m);
2349 } else
2350 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error);
2351 if (error != 0) {
2352 ++ifp->if_oerrors;
2353 return error;
2354 }
2355 return 0;
2356 }
2357
2358 int
2359 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src)
2360 {
2361 int rc;
2362
2363 if (ifp->if_initaddr != NULL)
2364 rc = (*ifp->if_initaddr)(ifp, ifa, src);
2365 else if (src ||
2366 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY)
2367 rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa);
2368
2369 return rc;
2370 }
2371
2372 int
2373 if_do_dad(struct ifnet *ifp)
2374 {
2375 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
2376 return 0;
2377
2378 switch (ifp->if_type) {
2379 case IFT_FAITH:
2380 /*
2381 * These interfaces do not have the IFF_LOOPBACK flag,
2382 * but loop packets back. We do not have to do DAD on such
2383 * interfaces. We should even omit it, because loop-backed
2384 * responses would confuse the DAD procedure.
2385 */
2386 return 0;
2387 default:
2388 /*
2389 * Our DAD routine requires the interface up and running.
2390 * However, some interfaces can be up before the RUNNING
2391 * status. Additionaly, users may try to assign addresses
2392 * before the interface becomes up (or running).
2393 * We simply skip DAD in such a case as a work around.
2394 * XXX: we should rather mark "tentative" on such addresses,
2395 * and do DAD after the interface becomes ready.
2396 */
2397 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2398 (IFF_UP|IFF_RUNNING))
2399 return 0;
2400
2401 return 1;
2402 }
2403 }
2404
2405 int
2406 if_flags_set(ifnet_t *ifp, const short flags)
2407 {
2408 int rc;
2409
2410 if (ifp->if_setflags != NULL)
2411 rc = (*ifp->if_setflags)(ifp, flags);
2412 else {
2413 short cantflags, chgdflags;
2414 struct ifreq ifr;
2415
2416 chgdflags = ifp->if_flags ^ flags;
2417 cantflags = chgdflags & IFF_CANTCHANGE;
2418
2419 if (cantflags != 0)
2420 ifp->if_flags ^= cantflags;
2421
2422 /* Traditionally, we do not call if_ioctl after
2423 * setting/clearing only IFF_PROMISC if the interface
2424 * isn't IFF_UP. Uphold that tradition.
2425 */
2426 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0)
2427 return 0;
2428
2429 memset(&ifr, 0, sizeof(ifr));
2430
2431 ifr.ifr_flags = flags & ~IFF_CANTCHANGE;
2432 rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr);
2433
2434 if (rc != 0 && cantflags != 0)
2435 ifp->if_flags ^= cantflags;
2436 }
2437
2438 return rc;
2439 }
2440
2441 int
2442 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa)
2443 {
2444 int rc;
2445 struct ifreq ifr;
2446
2447 if (ifp->if_mcastop != NULL)
2448 rc = (*ifp->if_mcastop)(ifp, cmd, sa);
2449 else {
2450 ifreq_setaddr(cmd, &ifr, sa);
2451 rc = (*ifp->if_ioctl)(ifp, cmd, &ifr);
2452 }
2453
2454 return rc;
2455 }
2456
2457 static void
2458 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname,
2459 struct ifaltq *ifq)
2460 {
2461 const struct sysctlnode *cnode, *rnode;
2462
2463 if (sysctl_createv(clog, 0, NULL, &rnode,
2464 CTLFLAG_PERMANENT,
2465 CTLTYPE_NODE, "interfaces",
2466 SYSCTL_DESCR("Per-interface controls"),
2467 NULL, 0, NULL, 0,
2468 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
2469 goto bad;
2470
2471 if (sysctl_createv(clog, 0, &rnode, &rnode,
2472 CTLFLAG_PERMANENT,
2473 CTLTYPE_NODE, ifname,
2474 SYSCTL_DESCR("Interface controls"),
2475 NULL, 0, NULL, 0,
2476 CTL_CREATE, CTL_EOL) != 0)
2477 goto bad;
2478
2479 if (sysctl_createv(clog, 0, &rnode, &rnode,
2480 CTLFLAG_PERMANENT,
2481 CTLTYPE_NODE, "sndq",
2482 SYSCTL_DESCR("Interface output queue controls"),
2483 NULL, 0, NULL, 0,
2484 CTL_CREATE, CTL_EOL) != 0)
2485 goto bad;
2486
2487 if (sysctl_createv(clog, 0, &rnode, &cnode,
2488 CTLFLAG_PERMANENT,
2489 CTLTYPE_INT, "len",
2490 SYSCTL_DESCR("Current output queue length"),
2491 NULL, 0, &ifq->ifq_len, 0,
2492 CTL_CREATE, CTL_EOL) != 0)
2493 goto bad;
2494
2495 if (sysctl_createv(clog, 0, &rnode, &cnode,
2496 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2497 CTLTYPE_INT, "maxlen",
2498 SYSCTL_DESCR("Maximum allowed output queue length"),
2499 NULL, 0, &ifq->ifq_maxlen, 0,
2500 CTL_CREATE, CTL_EOL) != 0)
2501 goto bad;
2502
2503 if (sysctl_createv(clog, 0, &rnode, &cnode,
2504 CTLFLAG_PERMANENT,
2505 CTLTYPE_INT, "drops",
2506 SYSCTL_DESCR("Packets dropped due to full output queue"),
2507 NULL, 0, &ifq->ifq_drops, 0,
2508 CTL_CREATE, CTL_EOL) != 0)
2509 goto bad;
2510
2511 return;
2512 bad:
2513 printf("%s: could not attach sysctl nodes\n", ifname);
2514 return;
2515 }
2516
2517 #if defined(INET) || defined(INET6)
2518
2519 #define SYSCTL_NET_PKTQ(q, cn, c) \
2520 static int \
2521 sysctl_net_##q##_##cn(SYSCTLFN_ARGS) \
2522 { \
2523 return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c); \
2524 }
2525
2526 #if defined(INET)
2527 static int
2528 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS)
2529 {
2530 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq);
2531 }
2532 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS)
2533 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS)
2534 #endif
2535
2536 #if defined(INET6)
2537 static int
2538 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS)
2539 {
2540 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq);
2541 }
2542 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS)
2543 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS)
2544 #endif
2545
2546 static void
2547 sysctl_net_pktq_setup(struct sysctllog **clog, int pf)
2548 {
2549 sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL;
2550 const char *pfname = NULL, *ipname = NULL;
2551 int ipn = 0, qid = 0;
2552
2553 switch (pf) {
2554 #if defined(INET)
2555 case PF_INET:
2556 len_func = sysctl_net_ip_pktq_items;
2557 maxlen_func = sysctl_net_ip_pktq_maxlen;
2558 drops_func = sysctl_net_ip_pktq_drops;
2559 pfname = "inet", ipn = IPPROTO_IP;
2560 ipname = "ip", qid = IPCTL_IFQ;
2561 break;
2562 #endif
2563 #if defined(INET6)
2564 case PF_INET6:
2565 len_func = sysctl_net_ip6_pktq_items;
2566 maxlen_func = sysctl_net_ip6_pktq_maxlen;
2567 drops_func = sysctl_net_ip6_pktq_drops;
2568 pfname = "inet6", ipn = IPPROTO_IPV6;
2569 ipname = "ip6", qid = IPV6CTL_IFQ;
2570 break;
2571 #endif
2572 default:
2573 KASSERT(false);
2574 }
2575
2576 sysctl_createv(clog, 0, NULL, NULL,
2577 CTLFLAG_PERMANENT,
2578 CTLTYPE_NODE, pfname, NULL,
2579 NULL, 0, NULL, 0,
2580 CTL_NET, pf, CTL_EOL);
2581 sysctl_createv(clog, 0, NULL, NULL,
2582 CTLFLAG_PERMANENT,
2583 CTLTYPE_NODE, ipname, NULL,
2584 NULL, 0, NULL, 0,
2585 CTL_NET, pf, ipn, CTL_EOL);
2586 sysctl_createv(clog, 0, NULL, NULL,
2587 CTLFLAG_PERMANENT,
2588 CTLTYPE_NODE, "ifq",
2589 SYSCTL_DESCR("Protocol input queue controls"),
2590 NULL, 0, NULL, 0,
2591 CTL_NET, pf, ipn, qid, CTL_EOL);
2592
2593 sysctl_createv(clog, 0, NULL, NULL,
2594 CTLFLAG_PERMANENT,
2595 CTLTYPE_INT, "len",
2596 SYSCTL_DESCR("Current input queue length"),
2597 len_func, 0, NULL, 0,
2598 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL);
2599 sysctl_createv(clog, 0, NULL, NULL,
2600 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2601 CTLTYPE_INT, "maxlen",
2602 SYSCTL_DESCR("Maximum allowed input queue length"),
2603 maxlen_func, 0, NULL, 0,
2604 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL);
2605 sysctl_createv(clog, 0, NULL, NULL,
2606 CTLFLAG_PERMANENT,
2607 CTLTYPE_INT, "drops",
2608 SYSCTL_DESCR("Packets dropped due to full input queue"),
2609 drops_func, 0, NULL, 0,
2610 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
2611 }
2612 #endif /* INET || INET6 */
2613
2614 static int
2615 if_sdl_sysctl(SYSCTLFN_ARGS)
2616 {
2617 struct ifnet *ifp;
2618 const struct sockaddr_dl *sdl;
2619
2620 if (namelen != 1)
2621 return EINVAL;
2622
2623 ifp = if_byindex(name[0]);
2624 if (ifp == NULL)
2625 return ENODEV;
2626
2627 sdl = ifp->if_sadl;
2628 if (sdl == NULL) {
2629 *oldlenp = 0;
2630 return 0;
2631 }
2632
2633 if (oldp == NULL) {
2634 *oldlenp = sdl->sdl_alen;
2635 return 0;
2636 }
2637
2638 if (*oldlenp >= sdl->sdl_alen)
2639 *oldlenp = sdl->sdl_alen;
2640 return sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
2641 }
2642
2643 SYSCTL_SETUP(sysctl_net_sdl_setup, "sysctl net.sdl subtree setup")
2644 {
2645 const struct sysctlnode *rnode = NULL;
2646
2647 sysctl_createv(clog, 0, NULL, &rnode,
2648 CTLFLAG_PERMANENT,
2649 CTLTYPE_NODE, "sdl",
2650 SYSCTL_DESCR("Get active link-layer address"),
2651 if_sdl_sysctl, 0, NULL, 0,
2652 CTL_NET, CTL_CREATE, CTL_EOL);
2653 }
2654