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