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