1 /* $NetBSD: if.c,v 1.240 2009/10/26 16:41:35 cegger Exp $ */
4 * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William Studenmund and Jason R. Thorpe.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
62 * Copyright (c) 1980, 1986, 1993
63 * The Regents of the University of California. All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
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.
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
89 * @(#)if.c 8.5 (Berkeley) 1/9/95
92 #include <sys/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.240 2009/10/26 16:41:35 cegger Exp $");
97 #include "opt_atalk.h"
99 #include "opt_pfil_hooks.h"
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>
117 #include <net/if_dl.h>
118 #include <net/if_ether.h>
119 #include <net/if_media.h>
120 #include <net80211/ieee80211.h>
121 #include <net80211/ieee80211_ioctl.h>
122 #include <net/if_types.h>
123 #include <net/radix.h>
124 #include <net/route.h>
125 #include <net/netisr.h>
127 #include <netatalk/at_extern.h>
128 #include <netatalk/at.h>
130 #include <net/pfil.h>
133 #include <netinet/in.h>
134 #include <netinet6/in6_var.h>
135 #include <netinet6/nd6.h>
140 #include <netinet/ip_carp.h>
143 #include <compat/sys/sockio.h>
144 #include <compat/sys/socket.h>
146 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
147 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
149 int ifqmaxlen
= IFQ_MAXLEN
;
150 callout_t if_slowtimo_ch
;
152 int netisr
; /* scheduling bits for network */
154 static int if_rt_walktree(struct rtentry
*, void *);
156 static struct if_clone
*if_clone_lookup(const char *, int *);
157 static int if_clone_list(struct if_clonereq
*);
159 static LIST_HEAD(, if_clone
) if_cloners
= LIST_HEAD_INITIALIZER(if_cloners
);
160 static int if_cloners_count
;
162 static uint64_t index_gen
;
163 static kmutex_t index_gen_mtx
;
166 struct pfil_head if_pfil
; /* packet filtering hook for interfaces */
169 static kauth_listener_t if_listener
;
171 static void if_detach_queues(struct ifnet
*, struct ifqueue
*);
172 static void sysctl_sndq_setup(struct sysctllog
**, const char *,
175 #if defined(INET) || defined(INET6)
176 static void sysctl_net_ifq_setup(struct sysctllog
**, int, const char *,
177 int, const char *, int, struct ifqueue
*);
181 if_listener_cb(kauth_cred_t cred
, kauth_action_t action
, void *cookie
,
182 void *arg0
, void *arg1
, void *arg2
, void *arg3
)
185 enum kauth_network_req req
;
187 result
= KAUTH_RESULT_DEFER
;
188 req
= (enum kauth_network_req
)arg1
;
190 if (action
!= KAUTH_NETWORK_INTERFACE
)
193 if ((req
== KAUTH_REQ_NETWORK_INTERFACE_GET
) ||
194 (req
== KAUTH_REQ_NETWORK_INTERFACE_SET
))
195 result
= KAUTH_RESULT_ALLOW
;
201 * Network interface utility routines.
203 * Routines with ifa_ifwith* names take sockaddr *'s as
210 {extern struct ifqueue ipintrq
;
211 sysctl_net_ifq_setup(NULL
, PF_INET
, "inet", IPPROTO_IP
, "ip",
212 IPCTL_IFQ
, &ipintrq
);}
215 {extern struct ifqueue ip6intrq
;
216 sysctl_net_ifq_setup(NULL
, PF_INET6
, "inet6", IPPROTO_IPV6
, "ip6",
217 IPV6CTL_IFQ
, &ip6intrq
);}
220 callout_init(&if_slowtimo_ch
, 0);
223 if_listener
= kauth_listen_scope(KAUTH_SCOPE_NETWORK
,
224 if_listener_cb
, NULL
);
228 * XXX Initialization before configure().
229 * XXX hack to get pfil_add_hook working in autoconf.
235 mutex_init(&index_gen_mtx
, MUTEX_DEFAULT
, IPL_NONE
);
238 if_pfil
.ph_type
= PFIL_TYPE_IFNET
;
239 if_pfil
.ph_ifnet
= NULL
;
240 if (pfil_head_register(&if_pfil
) != 0)
241 printf("WARNING: unable to register pfil hook\n");
246 if_alloc(u_char type
)
248 return malloc(sizeof(struct ifnet
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
252 if_initname(struct ifnet
*ifp
, const char *name
, int unit
)
254 (void)snprintf(ifp
->if_xname
, sizeof(ifp
->if_xname
),
259 * Null routines used while an interface is going away. These routines
260 * just return an error.
264 if_nulloutput(struct ifnet
*ifp
, struct mbuf
*m
,
265 const struct sockaddr
*so
, struct rtentry
*rt
)
272 if_nullinput(struct ifnet
*ifp
, struct mbuf
*m
)
279 if_nullstart(struct ifnet
*ifp
)
286 if_nullioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
293 if_nullinit(struct ifnet
*ifp
)
300 if_nullstop(struct ifnet
*ifp
, int disable
)
307 if_nullwatchdog(struct ifnet
*ifp
)
314 if_nulldrain(struct ifnet
*ifp
)
320 static u_int if_index
= 1;
321 struct ifnet_head ifnet
;
322 size_t if_indexlim
= 0;
323 struct ifaddr
**ifnet_addrs
= NULL
;
324 struct ifnet
**ifindex2ifnet
= NULL
;
325 struct ifnet
*lo0ifp
;
328 if_set_sadl(struct ifnet
*ifp
, const void *lla
, u_char addrlen
, bool factory
)
331 struct sockaddr_dl
*sdl
;
333 ifp
->if_addrlen
= addrlen
;
336 sdl
= satosdl(ifa
->ifa_addr
);
338 (void)sockaddr_dl_setaddr(sdl
, sdl
->sdl_len
, lla
, ifp
->if_addrlen
);
340 ifp
->if_hwdl
= ifp
->if_dl
;
341 IFAREF(ifp
->if_hwdl
);
343 /* TBD routing socket */
347 if_dl_create(const struct ifnet
*ifp
, const struct sockaddr_dl
**sdlp
)
349 unsigned socksize
, ifasize
;
350 int addrlen
, namelen
;
351 struct sockaddr_dl
*mask
, *sdl
;
354 namelen
= strlen(ifp
->if_xname
);
355 addrlen
= ifp
->if_addrlen
;
356 socksize
= roundup(sockaddr_dl_measure(namelen
, addrlen
), sizeof(long));
357 ifasize
= sizeof(*ifa
) + 2 * socksize
;
358 ifa
= (struct ifaddr
*)malloc(ifasize
, M_IFADDR
, M_WAITOK
|M_ZERO
);
360 sdl
= (struct sockaddr_dl
*)(ifa
+ 1);
361 mask
= (struct sockaddr_dl
*)(socksize
+ (char *)sdl
);
363 sockaddr_dl_init(sdl
, socksize
, ifp
->if_index
, ifp
->if_type
,
364 ifp
->if_xname
, namelen
, NULL
, addrlen
);
365 mask
->sdl_len
= sockaddr_dl_measure(namelen
, 0);
366 memset(&mask
->sdl_data
[0], 0xff, namelen
);
367 ifa
->ifa_rtrequest
= link_rtrequest
;
368 ifa
->ifa_addr
= (struct sockaddr
*)sdl
;
369 ifa
->ifa_netmask
= (struct sockaddr
*)mask
;
377 if_sadl_setrefs(struct ifnet
*ifp
, struct ifaddr
*ifa
)
379 const struct sockaddr_dl
*sdl
;
380 ifnet_addrs
[ifp
->if_index
] = ifa
;
384 sdl
= satosdl(ifa
->ifa_addr
);
389 * Allocate the link level name for the specified interface. This
390 * is an attachment helper. It must be called after ifp->if_addrlen
391 * is initialized, which may not be the case when if_attach() is
395 if_alloc_sadl(struct ifnet
*ifp
)
398 const struct sockaddr_dl
*sdl
;
401 * If the interface already has a link name, release it
402 * now. This is useful for interfaces that can change
403 * link types, and thus switch link names often.
405 if (ifp
->if_sadl
!= NULL
)
408 ifa
= if_dl_create(ifp
, &sdl
);
410 ifa_insert(ifp
, ifa
);
411 if_sadl_setrefs(ifp
, ifa
);
415 if_deactivate_sadl(struct ifnet
*ifp
)
419 KASSERT(ifp
->if_dl
!= NULL
);
425 ifnet_addrs
[ifp
->if_index
] = NULL
;
432 if_activate_sadl(struct ifnet
*ifp
, struct ifaddr
*ifa
,
433 const struct sockaddr_dl
*sdl
)
439 if_deactivate_sadl(ifp
);
441 if_sadl_setrefs(ifp
, ifa
);
442 IFADDR_FOREACH(ifa
, ifp
)
443 rtinit(ifa
, RTM_LLINFO_UPD
, 0);
448 * Free the link level name for the specified interface. This is
449 * a detach helper. This is called from if_detach() or from
450 * link layer type specific detach functions.
453 if_free_sadl(struct ifnet
*ifp
)
458 ifa
= ifnet_addrs
[ifp
->if_index
];
460 KASSERT(ifp
->if_sadl
== NULL
);
461 KASSERT(ifp
->if_dl
== NULL
);
465 KASSERT(ifp
->if_sadl
!= NULL
);
466 KASSERT(ifp
->if_dl
!= NULL
);
469 rtinit(ifa
, RTM_DELETE
, 0);
470 ifa_remove(ifp
, ifa
);
471 if_deactivate_sadl(ifp
);
472 if (ifp
->if_hwdl
== ifa
) {
480 * Attach an interface to the
481 * list of "active" interfaces.
484 if_attach(struct ifnet
*ifp
)
488 if (if_indexlim
== 0) {
492 TAILQ_INIT(&ifp
->if_addrlist
);
493 TAILQ_INSERT_TAIL(&ifnet
, ifp
, if_list
);
494 if (ifp
->if_ioctl
== NULL
)
495 ifp
->if_ioctl
= ifioctl_common
;
497 mutex_enter(&index_gen_mtx
);
498 ifp
->if_index_gen
= index_gen
++;
499 mutex_exit(&index_gen_mtx
);
501 ifp
->if_index
= if_index
;
502 if (ifindex2ifnet
== NULL
)
505 while (ifp
->if_index
< if_indexlim
&&
506 ifindex2ifnet
[ifp
->if_index
] != NULL
) {
511 * If we hit USHRT_MAX, we skip back to 0 since
512 * there are a number of places where the value
513 * of if_index or if_index itself is compared
514 * to or stored in an unsigned short. By
515 * jumping back, we won't botch those assignments
518 else if (if_index
== USHRT_MAX
) {
520 * However, if we have to jump back to
521 * zero *twice* without finding an empty
522 * slot in ifindex2ifnet[], then there
523 * there are too many (>65535) interfaces.
526 panic("too many interfaces");
530 ifp
->if_index
= if_index
;
534 * We have some arrays that should be indexed by if_index.
535 * since if_index will grow dynamically, they should grow too.
536 * struct ifadd **ifnet_addrs
537 * struct ifnet **ifindex2ifnet
539 if (ifnet_addrs
== NULL
|| ifindex2ifnet
== NULL
||
540 ifp
->if_index
>= if_indexlim
) {
544 oldlim
= if_indexlim
;
545 while (ifp
->if_index
>= if_indexlim
)
548 /* grow ifnet_addrs */
549 m
= oldlim
* sizeof(struct ifaddr
*);
550 n
= if_indexlim
* sizeof(struct ifaddr
*);
551 q
= malloc(n
, M_IFADDR
, M_WAITOK
|M_ZERO
);
552 if (ifnet_addrs
!= NULL
) {
553 memcpy(q
, ifnet_addrs
, m
);
554 free(ifnet_addrs
, M_IFADDR
);
556 ifnet_addrs
= (struct ifaddr
**)q
;
558 /* grow ifindex2ifnet */
559 m
= oldlim
* sizeof(struct ifnet
*);
560 n
= if_indexlim
* sizeof(struct ifnet
*);
561 q
= malloc(n
, M_IFADDR
, M_WAITOK
|M_ZERO
);
562 if (ifindex2ifnet
!= NULL
) {
563 memcpy(q
, ifindex2ifnet
, m
);
564 free(ifindex2ifnet
, M_IFADDR
);
566 ifindex2ifnet
= (struct ifnet
**)q
;
569 ifindex2ifnet
[ifp
->if_index
] = ifp
;
572 * Link level name is allocated later by a separate call to
576 if (ifp
->if_snd
.ifq_maxlen
== 0)
577 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
579 sysctl_sndq_setup(&ifp
->if_sysctl_log
, ifp
->if_xname
, &ifp
->if_snd
);
581 ifp
->if_broadcastaddr
= 0; /* reliably crash if used uninitialized */
583 ifp
->if_link_state
= LINK_STATE_UNKNOWN
;
585 ifp
->if_capenable
= 0;
586 ifp
->if_csum_flags_tx
= 0;
587 ifp
->if_csum_flags_rx
= 0;
590 ifp
->if_snd
.altq_type
= 0;
591 ifp
->if_snd
.altq_disc
= NULL
;
592 ifp
->if_snd
.altq_flags
&= ALTQF_CANTCHANGE
;
593 ifp
->if_snd
.altq_tbr
= NULL
;
594 ifp
->if_snd
.altq_ifp
= ifp
;
598 ifp
->if_pfil
.ph_type
= PFIL_TYPE_IFNET
;
599 ifp
->if_pfil
.ph_ifnet
= ifp
;
600 if (pfil_head_register(&ifp
->if_pfil
) != 0)
601 printf("%s: WARNING: unable to register pfil hook\n",
603 (void)pfil_run_hooks(&if_pfil
,
604 (struct mbuf
**)PFIL_IFNET_ATTACH
, ifp
, PFIL_IFNET
);
607 if (!STAILQ_EMPTY(&domains
))
608 if_attachdomain1(ifp
);
610 /* Announce the interface. */
611 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
615 if_attachdomain(void)
622 if_attachdomain1(ifp
);
627 if_attachdomain1(struct ifnet
*ifp
)
634 /* address family dependent data region */
635 memset(ifp
->if_afdata
, 0, sizeof(ifp
->if_afdata
));
637 if (dp
->dom_ifattach
!= NULL
)
638 ifp
->if_afdata
[dp
->dom_family
] =
639 (*dp
->dom_ifattach
)(ifp
);
646 * Deactivate an interface. This points all of the procedure
647 * handles at error stubs. May be called from interrupt context.
650 if_deactivate(struct ifnet
*ifp
)
656 ifp
->if_output
= if_nulloutput
;
657 ifp
->if_input
= if_nullinput
;
658 ifp
->if_start
= if_nullstart
;
659 ifp
->if_ioctl
= if_nullioctl
;
660 ifp
->if_init
= if_nullinit
;
661 ifp
->if_stop
= if_nullstop
;
662 ifp
->if_watchdog
= if_nullwatchdog
;
663 ifp
->if_drain
= if_nulldrain
;
665 /* No more packets may be enqueued. */
666 ifp
->if_snd
.ifq_maxlen
= 0;
672 if_purgeaddrs(struct ifnet
*ifp
, int family
, void (*purgeaddr
)(struct ifaddr
*))
674 struct ifaddr
*ifa
, *nifa
;
676 for (ifa
= IFADDR_FIRST(ifp
); ifa
!= NULL
; ifa
= nifa
) {
677 nifa
= IFADDR_NEXT(ifa
);
678 if (ifa
->ifa_addr
->sa_family
!= family
)
685 * Detach an interface from the list of "active" interfaces,
686 * freeing any resources as we go along.
688 * NOTE: This routine must be called with a valid thread context,
692 if_detach(struct ifnet
*ifp
)
697 struct ifaddr
*last_ifa
= NULL
;
700 const struct protosw
*pr
;
701 int s
, i
, family
, purged
;
704 * XXX It's kind of lame that we have to have the
705 * XXX socket structure...
707 memset(&so
, 0, sizeof(so
));
712 * Do an if_down() to give protocols a chance to do something.
717 if (ALTQ_IS_ENABLED(&ifp
->if_snd
))
718 altq_disable(&ifp
->if_snd
);
719 if (ALTQ_IS_ATTACHED(&ifp
->if_snd
))
720 altq_detach(&ifp
->if_snd
);
723 sysctl_teardown(&ifp
->if_sysctl_log
);
726 /* Remove the interface from any carp group it is a part of. */
727 if (ifp
->if_carp
!= NULL
&& ifp
->if_type
!= IFT_CARP
)
732 * Rip all the addresses off the interface. This should make
733 * all of the routes go away.
735 * pr_usrreq calls can remove an arbitrary number of ifaddrs
736 * from the list, including our "cursor", ifa. For safety,
737 * and to honor the TAILQ abstraction, I just restart the
738 * loop after each removal. Note that the loop will exit
739 * when all of the remaining ifaddrs belong to the AF_LINK
740 * family. I am counting on the historical fact that at
741 * least one pr_usrreq in each address domain removes at
745 IFADDR_FOREACH(ifa
, ifp
) {
746 family
= ifa
->ifa_addr
->sa_family
;
748 printf("if_detach: ifaddr %p, family %d, refcnt %d\n",
749 ifa
, family
, ifa
->ifa_refcnt
);
750 if (last_ifa
!= NULL
&& ifa
== last_ifa
)
751 panic("if_detach: loop detected");
754 if (family
== AF_LINK
)
756 dp
= pffinddomain(family
);
759 panic("if_detach: no domain for AF %d",
763 * XXX These PURGEIF calls are redundant with the
764 * purge-all-families calls below, but are left in for
765 * now both to make a smaller change, and to avoid
766 * unplanned interactions with clearing of
770 for (pr
= dp
->dom_protosw
;
771 pr
< dp
->dom_protoswNPROTOSW
; pr
++) {
773 if (pr
->pr_usrreq
!= NULL
) {
774 (void) (*pr
->pr_usrreq
)(&so
,
775 PRU_PURGEIF
, NULL
, NULL
,
776 (struct mbuf
*) ifp
, curlwp
);
782 * XXX What's really the best thing to do
783 * XXX here? --thorpej@NetBSD.org
785 printf("if_detach: WARNING: AF %d not purged\n",
787 ifa_remove(ifp
, ifa
);
794 /* Walk the routing table looking for stragglers. */
795 for (i
= 0; i
<= AF_MAX
; i
++)
796 (void)rt_walktree(i
, if_rt_walktree
, ifp
);
799 if (dp
->dom_ifdetach
!= NULL
&& ifp
->if_afdata
[dp
->dom_family
])
800 (*dp
->dom_ifdetach
)(ifp
,
801 ifp
->if_afdata
[dp
->dom_family
]);
804 * One would expect multicast memberships (INET and
805 * INET6) on UDP sockets to be purged by the PURGEIF
806 * calls above, but if all addresses were removed from
807 * the interface prior to destruction, the calls will
808 * not be made (e.g. ppp, for which pppd(8) generally
809 * removes addresses before destroying the interface).
810 * Because there is no invariant that multicast
811 * memberships only exist for interfaces with IPv4
812 * addresses, we must call PURGEIF regardless of
813 * addresses. (Protocols which might store ifnet
814 * pointers are marked with PR_PURGEIF.)
816 for (pr
= dp
->dom_protosw
; pr
< dp
->dom_protoswNPROTOSW
; pr
++) {
818 if (pr
->pr_usrreq
!= NULL
&& pr
->pr_flags
& PR_PURGEIF
)
819 (void)(*pr
->pr_usrreq
)(&so
, PRU_PURGEIF
, NULL
,
820 NULL
, (struct mbuf
*)ifp
, curlwp
);
825 (void)pfil_run_hooks(&if_pfil
,
826 (struct mbuf
**)PFIL_IFNET_DETACH
, ifp
, PFIL_IFNET
);
827 (void)pfil_head_unregister(&ifp
->if_pfil
);
830 /* Announce that the interface is gone. */
831 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
833 ifindex2ifnet
[ifp
->if_index
] = NULL
;
835 TAILQ_REMOVE(&ifnet
, ifp
, if_list
);
838 * remove packets that came from ifp, from software interrupt queues.
841 for (i
= 0; i
< __arraycount(dp
->dom_ifqueues
); i
++) {
842 if (dp
->dom_ifqueues
[i
] == NULL
)
844 if_detach_queues(ifp
, dp
->dom_ifqueues
[i
]);
852 if_detach_queues(struct ifnet
*ifp
, struct ifqueue
*q
)
854 struct mbuf
*m
, *prev
, *next
;
857 for (m
= q
->ifq_head
; m
!= NULL
; m
= next
) {
860 if ((m
->m_flags
& M_PKTHDR
) == 0) {
865 if (m
->m_pkthdr
.rcvif
!= ifp
) {
871 prev
->m_nextpkt
= m
->m_nextpkt
;
873 q
->ifq_head
= m
->m_nextpkt
;
874 if (q
->ifq_tail
== m
)
885 * Callback for a radix tree walk to delete all references to an
889 if_rt_walktree(struct rtentry
*rt
, void *v
)
891 struct ifnet
*ifp
= (struct ifnet
*)v
;
894 if (rt
->rt_ifp
!= ifp
)
897 /* Delete the entry. */
899 error
= rtrequest(RTM_DELETE
, rt_getkey(rt
), rt
->rt_gateway
,
900 rt_mask(rt
), rt
->rt_flags
, NULL
);
901 KASSERT((rt
->rt_flags
& RTF_UP
) == 0);
905 printf("%s: warning: unable to delete rtentry @ %p, "
906 "error = %d\n", ifp
->if_xname
, rt
, error
);
911 * Create a clone network interface.
914 if_clone_create(const char *name
)
916 struct if_clone
*ifc
;
919 ifc
= if_clone_lookup(name
, &unit
);
923 if (ifunit(name
) != NULL
)
926 return (*ifc
->ifc_create
)(ifc
, unit
);
930 * Destroy a clone network interface.
933 if_clone_destroy(const char *name
)
935 struct if_clone
*ifc
;
938 ifc
= if_clone_lookup(name
, NULL
);
946 if (ifc
->ifc_destroy
== NULL
)
949 return (*ifc
->ifc_destroy
)(ifp
);
953 * Look up a network interface cloner.
955 static struct if_clone
*
956 if_clone_lookup(const char *name
, int *unitp
)
958 struct if_clone
*ifc
;
962 /* separate interface name from unit */
964 cp
- name
< IFNAMSIZ
&& *cp
&& (*cp
< '0' || *cp
> '9');
968 if (cp
== name
|| cp
- name
== IFNAMSIZ
|| !*cp
)
969 return NULL
; /* No name or unit number */
971 LIST_FOREACH(ifc
, &if_cloners
, ifc_list
) {
972 if (strlen(ifc
->ifc_name
) == cp
- name
&&
973 strncmp(name
, ifc
->ifc_name
, cp
- name
) == 0)
981 while (cp
- name
< IFNAMSIZ
&& *cp
) {
982 if (*cp
< '0' || *cp
> '9' || unit
> INT_MAX
/ 10) {
983 /* Bogus unit number. */
986 unit
= (unit
* 10) + (*cp
++ - '0');
995 * Register a network interface cloner.
998 if_clone_attach(struct if_clone
*ifc
)
1001 LIST_INSERT_HEAD(&if_cloners
, ifc
, ifc_list
);
1006 * Unregister a network interface cloner.
1009 if_clone_detach(struct if_clone
*ifc
)
1012 LIST_REMOVE(ifc
, ifc_list
);
1017 * Provide list of interface cloners to userspace.
1020 if_clone_list(struct if_clonereq
*ifcr
)
1022 char outbuf
[IFNAMSIZ
], *dst
;
1023 struct if_clone
*ifc
;
1024 int count
, error
= 0;
1026 ifcr
->ifcr_total
= if_cloners_count
;
1027 if ((dst
= ifcr
->ifcr_buffer
) == NULL
) {
1028 /* Just asking how many there are. */
1032 if (ifcr
->ifcr_count
< 0)
1035 count
= (if_cloners_count
< ifcr
->ifcr_count
) ?
1036 if_cloners_count
: ifcr
->ifcr_count
;
1038 for (ifc
= LIST_FIRST(&if_cloners
); ifc
!= NULL
&& count
!= 0;
1039 ifc
= LIST_NEXT(ifc
, ifc_list
), count
--, dst
+= IFNAMSIZ
) {
1040 (void)strncpy(outbuf
, ifc
->ifc_name
, sizeof(outbuf
));
1041 if (outbuf
[sizeof(outbuf
) - 1] != '\0')
1042 return ENAMETOOLONG
;
1043 error
= copyout(outbuf
, dst
, sizeof(outbuf
));
1052 ifa_insert(struct ifnet
*ifp
, struct ifaddr
*ifa
)
1055 TAILQ_INSERT_TAIL(&ifp
->if_addrlist
, ifa
, ifa_list
);
1060 ifa_remove(struct ifnet
*ifp
, struct ifaddr
*ifa
)
1062 KASSERT(ifa
->ifa_ifp
== ifp
);
1063 TAILQ_REMOVE(&ifp
->if_addrlist
, ifa
, ifa_list
);
1068 equal(const struct sockaddr
*sa1
, const struct sockaddr
*sa2
)
1070 return sockaddr_cmp(sa1
, sa2
) == 0;
1074 * Locate an interface based on a complete address.
1078 ifa_ifwithaddr(const struct sockaddr
*addr
)
1083 IFNET_FOREACH(ifp
) {
1084 if (ifp
->if_output
== if_nulloutput
)
1086 IFADDR_FOREACH(ifa
, ifp
) {
1087 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
1089 if (equal(addr
, ifa
->ifa_addr
))
1091 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
1092 ifa
->ifa_broadaddr
&&
1093 /* IP6 doesn't have broadcast */
1094 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
1095 equal(ifa
->ifa_broadaddr
, addr
))
1103 * Locate the point to point interface with a given destination address.
1107 ifa_ifwithdstaddr(const struct sockaddr
*addr
)
1112 IFNET_FOREACH(ifp
) {
1113 if (ifp
->if_output
== if_nulloutput
)
1115 if ((ifp
->if_flags
& IFF_POINTOPOINT
) == 0)
1117 IFADDR_FOREACH(ifa
, ifp
) {
1118 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
||
1119 ifa
->ifa_dstaddr
== NULL
)
1121 if (equal(addr
, ifa
->ifa_dstaddr
))
1129 * Find an interface on a specific network. If many, choice
1130 * is most specific found.
1133 ifa_ifwithnet(const struct sockaddr
*addr
)
1137 const struct sockaddr_dl
*sdl
;
1138 struct ifaddr
*ifa_maybe
= 0;
1139 u_int af
= addr
->sa_family
;
1140 const char *addr_data
= addr
->sa_data
, *cplim
;
1142 if (af
== AF_LINK
) {
1143 sdl
= satocsdl(addr
);
1144 if (sdl
->sdl_index
&& sdl
->sdl_index
< if_indexlim
&&
1145 ifindex2ifnet
[sdl
->sdl_index
] &&
1146 ifindex2ifnet
[sdl
->sdl_index
]->if_output
!= if_nulloutput
)
1147 return ifnet_addrs
[sdl
->sdl_index
];
1150 if (af
== AF_APPLETALK
) {
1151 const struct sockaddr_at
*sat
, *sat2
;
1152 sat
= (const struct sockaddr_at
*)addr
;
1153 IFNET_FOREACH(ifp
) {
1154 if (ifp
->if_output
== if_nulloutput
)
1156 ifa
= at_ifawithnet((const struct sockaddr_at
*)addr
, ifp
);
1159 sat2
= (struct sockaddr_at
*)ifa
->ifa_addr
;
1160 if (sat2
->sat_addr
.s_net
== sat
->sat_addr
.s_net
)
1161 return ifa
; /* exact match */
1162 if (ifa_maybe
== NULL
) {
1163 /* else keep the if with the right range */
1170 IFNET_FOREACH(ifp
) {
1171 if (ifp
->if_output
== if_nulloutput
)
1173 IFADDR_FOREACH(ifa
, ifp
) {
1174 const char *cp
, *cp2
, *cp3
;
1176 if (ifa
->ifa_addr
->sa_family
!= af
||
1177 ifa
->ifa_netmask
== NULL
)
1180 cp2
= ifa
->ifa_addr
->sa_data
;
1181 cp3
= ifa
->ifa_netmask
->sa_data
;
1182 cplim
= (const char *)ifa
->ifa_netmask
+
1183 ifa
->ifa_netmask
->sa_len
;
1184 while (cp3
< cplim
) {
1185 if ((*cp
++ ^ *cp2
++) & *cp3
++) {
1186 /* want to continue for() loop */
1190 if (ifa_maybe
== NULL
||
1191 rn_refines((void *)ifa
->ifa_netmask
,
1192 (void *)ifa_maybe
->ifa_netmask
))
1200 * Find the interface of the addresss.
1203 ifa_ifwithladdr(const struct sockaddr
*addr
)
1207 if ((ia
= ifa_ifwithaddr(addr
)) || (ia
= ifa_ifwithdstaddr(addr
)) ||
1208 (ia
= ifa_ifwithnet(addr
)))
1214 * Find an interface using a specific address family
1217 ifa_ifwithaf(int af
)
1222 IFNET_FOREACH(ifp
) {
1223 if (ifp
->if_output
== if_nulloutput
)
1225 IFADDR_FOREACH(ifa
, ifp
) {
1226 if (ifa
->ifa_addr
->sa_family
== af
)
1234 * Find an interface address specific to an interface best matching
1238 ifaof_ifpforaddr(const struct sockaddr
*addr
, struct ifnet
*ifp
)
1241 const char *cp
, *cp2
, *cp3
;
1243 struct ifaddr
*ifa_maybe
= 0;
1244 u_int af
= addr
->sa_family
;
1246 if (ifp
->if_output
== if_nulloutput
)
1252 IFADDR_FOREACH(ifa
, ifp
) {
1253 if (ifa
->ifa_addr
->sa_family
!= af
)
1256 if (ifa
->ifa_netmask
== NULL
) {
1257 if (equal(addr
, ifa
->ifa_addr
) ||
1258 (ifa
->ifa_dstaddr
&&
1259 equal(addr
, ifa
->ifa_dstaddr
)))
1264 cp2
= ifa
->ifa_addr
->sa_data
;
1265 cp3
= ifa
->ifa_netmask
->sa_data
;
1266 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
1267 for (; cp3
< cplim
; cp3
++) {
1268 if ((*cp
++ ^ *cp2
++) & *cp3
)
1278 * Default action when installing a route with a Link Level gateway.
1279 * Lookup an appropriate real ifa to point to.
1280 * This should be moved to /sys/net/link.c eventually.
1283 link_rtrequest(int cmd
, struct rtentry
*rt
, const struct rt_addrinfo
*info
)
1286 const struct sockaddr
*dst
;
1289 if (cmd
!= RTM_ADD
|| (ifa
= rt
->rt_ifa
) == NULL
||
1290 (ifp
= ifa
->ifa_ifp
) == NULL
|| (dst
= rt_getkey(rt
)) == NULL
)
1292 if ((ifa
= ifaof_ifpforaddr(dst
, ifp
)) != NULL
) {
1293 rt_replace_ifa(rt
, ifa
);
1294 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
1295 ifa
->ifa_rtrequest(cmd
, rt
, info
);
1300 * Handle a change in the interface link state.
1303 if_link_state_change(struct ifnet
*ifp
, int link_state
)
1305 if (ifp
->if_link_state
== link_state
)
1307 ifp
->if_link_state
= link_state
;
1308 /* Notify that the link state has changed. */
1312 carp_carpdev_state(ifp
);
1317 * Mark an interface down and notify protocols of
1319 * NOTE: must be called at splsoftnet or equivalent.
1322 if_down(struct ifnet
*ifp
)
1326 ifp
->if_flags
&= ~IFF_UP
;
1327 nanotime(&ifp
->if_lastchange
);
1328 IFADDR_FOREACH(ifa
, ifp
)
1329 pfctlinput(PRC_IFDOWN
, ifa
->ifa_addr
);
1330 IFQ_PURGE(&ifp
->if_snd
);
1333 carp_carpdev_state(ifp
);
1339 * Mark an interface up and notify protocols of
1341 * NOTE: must be called at splsoftnet or equivalent.
1344 if_up(struct ifnet
*ifp
)
1350 ifp
->if_flags
|= IFF_UP
;
1351 nanotime(&ifp
->if_lastchange
);
1353 /* this has no effect on IP, and will kill all ISO connections XXX */
1354 IFADDR_FOREACH(ifa
, ifp
)
1355 pfctlinput(PRC_IFUP
, ifa
->ifa_addr
);
1359 carp_carpdev_state(ifp
);
1368 * Handle interface watchdog timer routines. Called
1369 * from softclock, we decrement timers (if set) and
1370 * call the appropriate interface routine on expiration.
1373 if_slowtimo(void *arg
)
1378 IFNET_FOREACH(ifp
) {
1379 if (ifp
->if_timer
== 0 || --ifp
->if_timer
)
1381 if (ifp
->if_watchdog
!= NULL
)
1382 (*ifp
->if_watchdog
)(ifp
);
1385 callout_reset(&if_slowtimo_ch
, hz
/ IFNET_SLOWHZ
, if_slowtimo
, NULL
);
1389 * Set/clear promiscuous mode on interface ifp based on the truth value
1390 * of pswitch. The calls are reference counted so that only the first
1391 * "on" request actually has an effect, as does the final "off" request.
1392 * Results are undefined if the "off" and "on" requests are not matched.
1395 ifpromisc(struct ifnet
*ifp
, int pswitch
)
1401 pcount
= ifp
->if_pcount
;
1402 flags
= ifp
->if_flags
;
1405 * Allow the device to be "placed" into promiscuous
1406 * mode even if it is not configured up. It will
1407 * consult IFF_PROMISC when it is is brought up.
1409 if (ifp
->if_pcount
++ != 0)
1411 ifp
->if_flags
|= IFF_PROMISC
;
1412 if ((ifp
->if_flags
& IFF_UP
) == 0)
1415 if (--ifp
->if_pcount
> 0)
1417 ifp
->if_flags
&= ~IFF_PROMISC
;
1419 * If the device is not configured up, we should not need to
1420 * turn off promiscuous mode (device should have turned it
1421 * off when interface went down; and will look at IFF_PROMISC
1422 * again next time interface comes up).
1424 if ((ifp
->if_flags
& IFF_UP
) == 0)
1427 memset(&ifr
, 0, sizeof(ifr
));
1428 ifr
.ifr_flags
= ifp
->if_flags
;
1429 ret
= (*ifp
->if_ioctl
)(ifp
, SIOCSIFFLAGS
, &ifr
);
1430 /* Restore interface state if not successful. */
1432 ifp
->if_pcount
= pcount
;
1433 ifp
->if_flags
= flags
;
1439 * Map interface name to
1440 * interface structure pointer.
1443 ifunit(const char *name
)
1446 const char *cp
= name
;
1451 * If the entire name is a number, treat it as an ifindex.
1453 for (i
= 0; i
< IFNAMSIZ
&& *cp
>= '0' && *cp
<= '9'; i
++, cp
++) {
1454 unit
= unit
* 10 + (*cp
- '0');
1458 * If the number took all of the name, then it's a valid ifindex.
1460 if (i
== IFNAMSIZ
|| (cp
!= name
&& *cp
== '\0')) {
1461 if (unit
>= if_indexlim
)
1463 ifp
= ifindex2ifnet
[unit
];
1464 if (ifp
== NULL
|| ifp
->if_output
== if_nulloutput
)
1469 IFNET_FOREACH(ifp
) {
1470 if (ifp
->if_output
== if_nulloutput
)
1472 if (strcmp(ifp
->if_xname
, name
) == 0)
1480 ifioctl_common(struct ifnet
*ifp
, u_long cmd
, void *data
)
1484 struct ifcapreq
*ifcr
;
1485 struct ifdatareq
*ifdr
;
1490 if ((ifcr
->ifcr_capenable
& ~ifp
->if_capabilities
) != 0)
1493 if (ifcr
->ifcr_capenable
== ifp
->if_capenable
)
1496 ifp
->if_capenable
= ifcr
->ifcr_capenable
;
1498 /* Pre-compute the checksum flags mask. */
1499 ifp
->if_csum_flags_tx
= 0;
1500 ifp
->if_csum_flags_rx
= 0;
1501 if (ifp
->if_capenable
& IFCAP_CSUM_IPv4_Tx
) {
1502 ifp
->if_csum_flags_tx
|= M_CSUM_IPv4
;
1504 if (ifp
->if_capenable
& IFCAP_CSUM_IPv4_Rx
) {
1505 ifp
->if_csum_flags_rx
|= M_CSUM_IPv4
;
1508 if (ifp
->if_capenable
& IFCAP_CSUM_TCPv4_Tx
) {
1509 ifp
->if_csum_flags_tx
|= M_CSUM_TCPv4
;
1511 if (ifp
->if_capenable
& IFCAP_CSUM_TCPv4_Rx
) {
1512 ifp
->if_csum_flags_rx
|= M_CSUM_TCPv4
;
1515 if (ifp
->if_capenable
& IFCAP_CSUM_UDPv4_Tx
) {
1516 ifp
->if_csum_flags_tx
|= M_CSUM_UDPv4
;
1518 if (ifp
->if_capenable
& IFCAP_CSUM_UDPv4_Rx
) {
1519 ifp
->if_csum_flags_rx
|= M_CSUM_UDPv4
;
1522 if (ifp
->if_capenable
& IFCAP_CSUM_TCPv6_Tx
) {
1523 ifp
->if_csum_flags_tx
|= M_CSUM_TCPv6
;
1525 if (ifp
->if_capenable
& IFCAP_CSUM_TCPv6_Rx
) {
1526 ifp
->if_csum_flags_rx
|= M_CSUM_TCPv6
;
1529 if (ifp
->if_capenable
& IFCAP_CSUM_UDPv6_Tx
) {
1530 ifp
->if_csum_flags_tx
|= M_CSUM_UDPv6
;
1532 if (ifp
->if_capenable
& IFCAP_CSUM_UDPv6_Rx
) {
1533 ifp
->if_csum_flags_rx
|= M_CSUM_UDPv6
;
1535 if (ifp
->if_flags
& IFF_UP
)
1540 if (ifp
->if_flags
& IFF_UP
&& (ifr
->ifr_flags
& IFF_UP
) == 0) {
1545 if (ifr
->ifr_flags
& IFF_UP
&& (ifp
->if_flags
& IFF_UP
) == 0) {
1550 ifp
->if_flags
= (ifp
->if_flags
& IFF_CANTCHANGE
) |
1551 (ifr
->ifr_flags
&~ IFF_CANTCHANGE
);
1555 ifr
->ifr_flags
= ifp
->if_flags
;
1560 ifr
->ifr_metric
= ifp
->if_metric
;
1565 ifr
->ifr_mtu
= ifp
->if_mtu
;
1570 ifr
->ifr_dlt
= ifp
->if_dlt
;
1575 ifcr
->ifcr_capabilities
= ifp
->if_capabilities
;
1576 ifcr
->ifcr_capenable
= ifp
->if_capenable
;
1581 ifp
->if_metric
= ifr
->ifr_metric
;
1586 ifdr
->ifdr_data
= ifp
->if_data
;
1591 ifdr
->ifdr_data
= ifp
->if_data
;
1593 * Assumes that the volatile counters that can be
1594 * zero'ed are at the end of if_data.
1596 memset(&ifp
->if_data
.ifi_ipackets
, 0, sizeof(ifp
->if_data
) -
1597 offsetof(struct if_data
, ifi_ipackets
));
1601 if (ifp
->if_mtu
== ifr
->ifr_mtu
)
1603 ifp
->if_mtu
= ifr
->ifr_mtu
;
1605 * If the link MTU changed, do network layer specific procedure.
1618 ifaddrpref_ioctl(struct socket
*so
, u_long cmd
, void *data
, struct ifnet
*ifp
,
1621 struct if_addrprefreq
*ifap
= (struct if_addrprefreq
*)data
;
1623 const struct sockaddr
*any
, *sa
;
1626 struct sockaddr_storage ss
;
1630 case SIOCSIFADDRPREF
:
1631 if (kauth_authorize_network(l
->l_cred
, KAUTH_NETWORK_INTERFACE
,
1632 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV
, ifp
, (void *)cmd
,
1635 case SIOCGIFADDRPREF
:
1642 if (data
== NULL
|| ifp
== NULL
) {
1643 panic("invalid argument to %s", __func__
);
1647 /* address must be specified on ADD and DELETE */
1648 sa
= sstocsa(&ifap
->ifap_addr
);
1649 if (sa
->sa_family
!= sofamily(so
))
1651 if ((any
= sockaddr_any(sa
)) == NULL
|| sa
->sa_len
!= any
->sa_len
)
1654 sockaddr_externalize(&v
.sa
, sizeof(v
.ss
), sa
);
1656 IFADDR_FOREACH(ifa
, ifp
) {
1657 if (ifa
->ifa_addr
->sa_family
!= sa
->sa_family
)
1659 sockaddr_externalize(&u
.sa
, sizeof(u
.ss
), ifa
->ifa_addr
);
1660 if (sockaddr_cmp(&u
.sa
, &v
.sa
) == 0)
1664 return EADDRNOTAVAIL
;
1667 case SIOCSIFADDRPREF
:
1668 ifa
->ifa_preference
= ifap
->ifap_preference
;
1670 case SIOCGIFADDRPREF
:
1671 /* fill in the if_laddrreq structure */
1672 (void)sockaddr_copy(sstosa(&ifap
->ifap_addr
),
1673 sizeof(ifap
->ifap_addr
), ifa
->ifa_addr
);
1674 ifap
->ifap_preference
= ifa
->ifa_preference
;
1685 ifioctl(struct socket
*so
, u_long cmd
, void *data
, struct lwp
*l
)
1689 struct ifcapreq
*ifcr
;
1690 struct ifdatareq
*ifdr
;
1692 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ)
1696 #ifdef COMPAT_OIFREQ
1698 struct oifreq
*oifr
= NULL
;
1702 #ifdef COMPAT_OIFREQ
1705 return compat_ifconf(cmd
, data
);
1707 #ifdef COMPAT_OIFDATA
1710 return compat_ifdatareq(l
, cmd
, data
);
1713 return ifconf(cmd
, data
);
1714 case SIOCINITIFADDR
:
1718 #ifdef COMPAT_OIFREQ
1719 cmd
= compat_cvtcmd(cmd
);
1723 ifreqo2n(oifr
, ifr
);
1730 ifp
= ifunit(ifr
->ifr_name
);
1736 error
= kauth_authorize_network(l
->l_cred
,
1737 KAUTH_NETWORK_INTERFACE
,
1738 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV
, ifp
,
1743 return (cmd
== SIOCIFCREATE
) ?
1744 if_clone_create(ifr
->ifr_name
) :
1745 if_clone_destroy(ifr
->ifr_name
);
1747 case SIOCIFGCLONERS
:
1748 return if_clone_list((struct if_clonereq
*)data
);
1757 case SIOCSIFADDRPREF
:
1763 case SIOCSIFPHYADDR
:
1764 case SIOCDIFPHYADDR
:
1766 case SIOCSIFPHYADDR_IN6
:
1768 case SIOCSLIFPHYADDR
:
1775 case SIOCS80211NWID
:
1776 case SIOCS80211NWKEY
:
1777 case SIOCS80211POWER
:
1778 case SIOCS80211BSSID
:
1779 case SIOCS80211CHANNEL
:
1781 error
= kauth_authorize_network(l
->l_cred
,
1782 KAUTH_NETWORK_INTERFACE
,
1783 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV
, ifp
,
1790 oif_flags
= ifp
->if_flags
;
1792 error
= (*ifp
->if_ioctl
)(ifp
, cmd
, data
);
1793 if (error
!= ENOTTY
)
1795 else if (so
->so_proto
== NULL
)
1799 error
= compat_ifioctl(so
, ocmd
, cmd
, data
, l
);
1801 error
= (*so
->so_proto
->pr_usrreq
)(so
, PRU_CONTROL
,
1802 (struct mbuf
*)cmd
, (struct mbuf
*)data
,
1803 (struct mbuf
*)ifp
, l
);
1807 if (((oif_flags
^ ifp
->if_flags
) & IFF_UP
) != 0) {
1809 if ((ifp
->if_flags
& IFF_UP
) != 0) {
1816 #ifdef COMPAT_OIFREQ
1818 ifreqn2o(oifr
, ifr
);
1825 * Return interface configuration
1826 * of system. List may be used
1827 * in later ioctl's (above) to get
1828 * other information.
1830 * Each record is a struct ifreq. Before the addition of
1831 * sockaddr_storage, the API rule was that sockaddr flavors that did
1832 * not fit would extend beyond the struct ifreq, with the next struct
1833 * ifreq starting sa_len beyond the struct sockaddr. Because the
1834 * union in struct ifreq includes struct sockaddr_storage, every kind
1835 * of sockaddr must fit. Thus, there are no longer any overlength
1838 * Records are added to the user buffer if they fit, and ifc_len is
1839 * adjusted to the length that was written. Thus, the user is only
1840 * assured of getting the complete list if ifc_len on return is at
1841 * least sizeof(struct ifreq) less than it was on entry.
1843 * If the user buffer pointer is NULL, this routine copies no data and
1844 * returns the amount of space that would be needed.
1847 * ifrp points to the next part of the user's buffer to be used. If
1848 * ifrp != NULL, space holds the number of bytes remaining that we may
1849 * write at ifrp. Otherwise, space holds the number of bytes that
1850 * would have been written had there been adequate space.
1854 ifconf(u_long cmd
, void *data
)
1856 struct ifconf
*ifc
= (struct ifconf
*)data
;
1859 struct ifreq ifr
, *ifrp
;
1860 int space
, error
= 0;
1861 const int sz
= (int)sizeof(struct ifreq
);
1863 if ((ifrp
= ifc
->ifc_req
) == NULL
)
1866 space
= ifc
->ifc_len
;
1867 IFNET_FOREACH(ifp
) {
1868 (void)strncpy(ifr
.ifr_name
, ifp
->if_xname
,
1869 sizeof(ifr
.ifr_name
));
1870 if (ifr
.ifr_name
[sizeof(ifr
.ifr_name
) - 1] != '\0')
1871 return ENAMETOOLONG
;
1872 if (IFADDR_EMPTY(ifp
)) {
1873 /* Interface with no addresses - send zero sockaddr. */
1874 memset(&ifr
.ifr_addr
, 0, sizeof(ifr
.ifr_addr
));
1880 error
= copyout(&ifr
, ifrp
, sz
);
1888 IFADDR_FOREACH(ifa
, ifp
) {
1889 struct sockaddr
*sa
= ifa
->ifa_addr
;
1890 /* all sockaddrs must fit in sockaddr_storage */
1891 KASSERT(sa
->sa_len
<= sizeof(ifr
.ifr_ifru
));
1897 memcpy(&ifr
.ifr_space
, sa
, sa
->sa_len
);
1899 error
= copyout(&ifr
, ifrp
, sz
);
1902 ifrp
++; space
-= sz
;
1907 KASSERT(0 <= space
&& space
<= ifc
->ifc_len
);
1908 ifc
->ifc_len
-= space
;
1910 KASSERT(space
>= 0);
1911 ifc
->ifc_len
= space
;
1917 ifreq_setaddr(const u_long cmd
, struct ifreq
*ifr
, const struct sockaddr
*sa
)
1922 if ((ncmd
= compat_cvtcmd(cmd
)) != cmd
)
1923 len
= sizeof(ifr
->ifr_addr
);
1925 len
= sizeof(ifr
->ifr_ifru
.ifru_space
);
1926 if (len
< sa
->sa_len
)
1928 memset(&ifr
->ifr_addr
, 0, len
);
1929 sockaddr_copy(&ifr
->ifr_addr
, len
, sa
);
1934 * Queue message on interface, and start output if interface
1938 ifq_enqueue(struct ifnet
*ifp
, struct mbuf
*m
1939 ALTQ_COMMA
ALTQ_DECL(struct altq_pktattr
*pktattr
))
1941 int len
= m
->m_pkthdr
.len
;
1942 int mflags
= m
->m_flags
;
1946 IFQ_ENQUEUE(&ifp
->if_snd
, m
, pktattr
, error
);
1949 ifp
->if_obytes
+= len
;
1950 if (mflags
& M_MCAST
)
1952 if ((ifp
->if_flags
& IFF_OACTIVE
) == 0)
1953 (*ifp
->if_start
)(ifp
);
1960 * Queue message on interface, possibly using a second fast queue
1963 ifq_enqueue2(struct ifnet
*ifp
, struct ifqueue
*ifq
, struct mbuf
*m
1964 ALTQ_COMMA
ALTQ_DECL(struct altq_pktattr
*pktattr
))
1970 && ALTQ_IS_ENABLED(&ifp
->if_snd
) == 0
1973 if (IF_QFULL(ifq
)) {
1974 IF_DROP(&ifp
->if_snd
);
1981 IFQ_ENQUEUE(&ifp
->if_snd
, m
, pktattr
, error
);
1991 sysctl_sndq_setup(struct sysctllog
**clog
, const char *ifname
,
1994 const struct sysctlnode
*cnode
, *rnode
;
1996 if (sysctl_createv(clog
, 0, NULL
, &rnode
,
1998 CTLTYPE_NODE
, "net", NULL
,
2000 CTL_NET
, CTL_EOL
) != 0)
2003 if (sysctl_createv(clog
, 0, &rnode
, &rnode
,
2005 CTLTYPE_NODE
, "interfaces",
2006 SYSCTL_DESCR("Per-interface controls"),
2008 CTL_CREATE
, CTL_EOL
) != 0)
2011 if (sysctl_createv(clog
, 0, &rnode
, &rnode
,
2013 CTLTYPE_NODE
, ifname
,
2014 SYSCTL_DESCR("Interface controls"),
2016 CTL_CREATE
, CTL_EOL
) != 0)
2019 if (sysctl_createv(clog
, 0, &rnode
, &rnode
,
2021 CTLTYPE_NODE
, "sndq",
2022 SYSCTL_DESCR("Interface output queue controls"),
2024 CTL_CREATE
, CTL_EOL
) != 0)
2027 if (sysctl_createv(clog
, 0, &rnode
, &cnode
,
2030 SYSCTL_DESCR("Current output queue length"),
2031 NULL
, 0, &ifq
->ifq_len
, 0,
2032 CTL_CREATE
, CTL_EOL
) != 0)
2035 if (sysctl_createv(clog
, 0, &rnode
, &cnode
,
2036 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
2037 CTLTYPE_INT
, "maxlen",
2038 SYSCTL_DESCR("Maximum allowed output queue length"),
2039 NULL
, 0, &ifq
->ifq_maxlen
, 0,
2040 CTL_CREATE
, CTL_EOL
) != 0)
2043 if (sysctl_createv(clog
, 0, &rnode
, &cnode
,
2045 CTLTYPE_INT
, "drops",
2046 SYSCTL_DESCR("Packets dropped due to full output queue"),
2047 NULL
, 0, &ifq
->ifq_drops
, 0,
2048 CTL_CREATE
, CTL_EOL
) != 0)
2053 printf("%s: could not attach sysctl nodes\n", ifname
);
2057 #if defined(INET) || defined(INET6)
2059 sysctl_net_ifq_setup(struct sysctllog
**clog
,
2060 int pf
, const char *pfname
,
2061 int ipn
, const char *ipname
,
2062 int qid
, struct ifqueue
*ifq
)
2065 sysctl_createv(clog
, 0, NULL
, NULL
,
2067 CTLTYPE_NODE
, "net", NULL
,
2070 sysctl_createv(clog
, 0, NULL
, NULL
,
2072 CTLTYPE_NODE
, pfname
, NULL
,
2074 CTL_NET
, pf
, CTL_EOL
);
2075 sysctl_createv(clog
, 0, NULL
, NULL
,
2077 CTLTYPE_NODE
, ipname
, NULL
,
2079 CTL_NET
, pf
, ipn
, CTL_EOL
);
2080 sysctl_createv(clog
, 0, NULL
, NULL
,
2082 CTLTYPE_NODE
, "ifq",
2083 SYSCTL_DESCR("Protocol input queue controls"),
2085 CTL_NET
, pf
, ipn
, qid
, CTL_EOL
);
2087 sysctl_createv(clog
, 0, NULL
, NULL
,
2090 SYSCTL_DESCR("Current input queue length"),
2091 NULL
, 0, &ifq
->ifq_len
, 0,
2092 CTL_NET
, pf
, ipn
, qid
, IFQCTL_LEN
, CTL_EOL
);
2093 sysctl_createv(clog
, 0, NULL
, NULL
,
2094 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
2095 CTLTYPE_INT
, "maxlen",
2096 SYSCTL_DESCR("Maximum allowed input queue length"),
2097 NULL
, 0, &ifq
->ifq_maxlen
, 0,
2098 CTL_NET
, pf
, ipn
, qid
, IFQCTL_MAXLEN
, CTL_EOL
);
2100 sysctl_createv(clog
, 0, NULL
, NULL
,
2102 CTLTYPE_INT
, "peak",
2103 SYSCTL_DESCR("Highest input queue length"),
2104 NULL
, 0, &ifq
->ifq_peak
, 0,
2105 CTL_NET
, pf
, ipn
, qid
, IFQCTL_PEAK
, CTL_EOL
);
2107 sysctl_createv(clog
, 0, NULL
, NULL
,
2109 CTLTYPE_INT
, "drops",
2110 SYSCTL_DESCR("Packets dropped due to full input queue"),
2111 NULL
, 0, &ifq
->ifq_drops
, 0,
2112 CTL_NET
, pf
, ipn
, qid
, IFQCTL_DROPS
, CTL_EOL
);
2114 #endif /* INET || INET6 */