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