Sat Apr 18 15:56:26 2020 UTC ()
In _if_down(), release the link state change lock before calling
workqueue_wait().  Add a comment explaining how the locking here
works.

PR kern/55018.


(thorpej)
diff -r1.473 -r1.474 src/sys/net/if.c

cvs diff -r1.473 -r1.474 src/sys/net/if.c (switch to unified diff)

--- src/sys/net/if.c 2020/02/21 00:26:23 1.473
+++ src/sys/net/if.c 2020/04/18 15:56:26 1.474
@@ -1,1092 +1,1092 @@ @@ -1,1092 +1,1092 @@
1/* $NetBSD: if.c,v 1.473 2020/02/21 00:26:23 joerg Exp $ */ 1/* $NetBSD: if.c,v 1.474 2020/04/18 15:56:26 thorpej Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by William Studenmund and Jason R. Thorpe. 8 * by William Studenmund and Jason R. Thorpe.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 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 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 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32/* 32/*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved. 34 * All rights reserved.
35 * 35 *
36 * Redistribution and use in source and binary forms, with or without 36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions 37 * modification, are permitted provided that the following conditions
38 * are met: 38 * are met:
39 * 1. Redistributions of source code must retain the above copyright 39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer. 40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright 41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the 42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution. 43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors 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 45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission. 46 * without specific prior written permission.
47 * 47 *
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 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 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE. 58 * SUCH DAMAGE.
59 */ 59 */
60 60
61/* 61/*
62 * Copyright (c) 1980, 1986, 1993 62 * Copyright (c) 1980, 1986, 1993
63 * The Regents of the University of California. All rights reserved. 63 * The Regents of the University of California. All rights reserved.
64 * 64 *
65 * Redistribution and use in source and binary forms, with or without 65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions 66 * modification, are permitted provided that the following conditions
67 * are met: 67 * are met:
68 * 1. Redistributions of source code must retain the above copyright 68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer. 69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright 70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the 71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution. 72 * documentation and/or other materials provided with the distribution.
73 * 3. Neither the name of the University nor the names of its contributors 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 74 * may be used to endorse or promote products derived from this software
75 * without specific prior written permission. 75 * without specific prior written permission.
76 * 76 *
77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 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 78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 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 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 86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * SUCH DAMAGE. 87 * SUCH DAMAGE.
88 * 88 *
89 * @(#)if.c 8.5 (Berkeley) 1/9/95 89 * @(#)if.c 8.5 (Berkeley) 1/9/95
90 */ 90 */
91 91
92#include <sys/cdefs.h> 92#include <sys/cdefs.h>
93__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.473 2020/02/21 00:26:23 joerg Exp $"); 93__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.474 2020/04/18 15:56:26 thorpej Exp $");
94 94
95#if defined(_KERNEL_OPT) 95#if defined(_KERNEL_OPT)
96#include "opt_inet.h" 96#include "opt_inet.h"
97#include "opt_ipsec.h" 97#include "opt_ipsec.h"
98#include "opt_atalk.h" 98#include "opt_atalk.h"
99#include "opt_wlan.h" 99#include "opt_wlan.h"
100#include "opt_net_mpsafe.h" 100#include "opt_net_mpsafe.h"
101#include "opt_mrouting.h" 101#include "opt_mrouting.h"
102#endif 102#endif
103 103
104#include <sys/param.h> 104#include <sys/param.h>
105#include <sys/mbuf.h> 105#include <sys/mbuf.h>
106#include <sys/systm.h> 106#include <sys/systm.h>
107#include <sys/callout.h> 107#include <sys/callout.h>
108#include <sys/proc.h> 108#include <sys/proc.h>
109#include <sys/socket.h> 109#include <sys/socket.h>
110#include <sys/socketvar.h> 110#include <sys/socketvar.h>
111#include <sys/domain.h> 111#include <sys/domain.h>
112#include <sys/protosw.h> 112#include <sys/protosw.h>
113#include <sys/kernel.h> 113#include <sys/kernel.h>
114#include <sys/ioctl.h> 114#include <sys/ioctl.h>
115#include <sys/sysctl.h> 115#include <sys/sysctl.h>
116#include <sys/syslog.h> 116#include <sys/syslog.h>
117#include <sys/kauth.h> 117#include <sys/kauth.h>
118#include <sys/kmem.h> 118#include <sys/kmem.h>
119#include <sys/xcall.h> 119#include <sys/xcall.h>
120#include <sys/cpu.h> 120#include <sys/cpu.h>
121#include <sys/intr.h> 121#include <sys/intr.h>
122#include <sys/module_hook.h> 122#include <sys/module_hook.h>
123#include <sys/compat_stub.h> 123#include <sys/compat_stub.h>
124#include <sys/msan.h> 124#include <sys/msan.h>
125 125
126#include <net/if.h> 126#include <net/if.h>
127#include <net/if_dl.h> 127#include <net/if_dl.h>
128#include <net/if_ether.h> 128#include <net/if_ether.h>
129#include <net/if_media.h> 129#include <net/if_media.h>
130#include <net80211/ieee80211.h> 130#include <net80211/ieee80211.h>
131#include <net80211/ieee80211_ioctl.h> 131#include <net80211/ieee80211_ioctl.h>
132#include <net/if_types.h> 132#include <net/if_types.h>
133#include <net/route.h> 133#include <net/route.h>
134#include <net/netisr.h> 134#include <net/netisr.h>
135#include <sys/module.h> 135#include <sys/module.h>
136#ifdef NETATALK 136#ifdef NETATALK
137#include <netatalk/at_extern.h> 137#include <netatalk/at_extern.h>
138#include <netatalk/at.h> 138#include <netatalk/at.h>
139#endif 139#endif
140#include <net/pfil.h> 140#include <net/pfil.h>
141#include <netinet/in.h> 141#include <netinet/in.h>
142#include <netinet/in_var.h> 142#include <netinet/in_var.h>
143#include <netinet/ip_encap.h> 143#include <netinet/ip_encap.h>
144#include <net/bpf.h> 144#include <net/bpf.h>
145 145
146#ifdef INET6 146#ifdef INET6
147#include <netinet6/in6_var.h> 147#include <netinet6/in6_var.h>
148#include <netinet6/nd6.h> 148#include <netinet6/nd6.h>
149#endif 149#endif
150 150
151#include "ether.h" 151#include "ether.h"
152 152
153#include "bridge.h" 153#include "bridge.h"
154#if NBRIDGE > 0 154#if NBRIDGE > 0
155#include <net/if_bridgevar.h> 155#include <net/if_bridgevar.h>
156#endif 156#endif
157 157
158#include "carp.h" 158#include "carp.h"
159#if NCARP > 0 159#if NCARP > 0
160#include <netinet/ip_carp.h> 160#include <netinet/ip_carp.h>
161#endif 161#endif
162 162
163#include <compat/sys/sockio.h> 163#include <compat/sys/sockio.h>
164 164
165MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 165MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
166MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 166MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
167 167
168/* 168/*
169 * Global list of interfaces. 169 * Global list of interfaces.
170 */ 170 */
171/* DEPRECATED. Remove it once kvm(3) users disappeared */ 171/* DEPRECATED. Remove it once kvm(3) users disappeared */
172struct ifnet_head ifnet_list; 172struct ifnet_head ifnet_list;
173 173
174struct pslist_head ifnet_pslist; 174struct pslist_head ifnet_pslist;
175static ifnet_t ** ifindex2ifnet = NULL; 175static ifnet_t ** ifindex2ifnet = NULL;
176static u_int if_index = 1; 176static u_int if_index = 1;
177static size_t if_indexlim = 0; 177static size_t if_indexlim = 0;
178static uint64_t index_gen; 178static uint64_t index_gen;
179/* Mutex to protect the above objects. */ 179/* Mutex to protect the above objects. */
180kmutex_t ifnet_mtx __cacheline_aligned; 180kmutex_t ifnet_mtx __cacheline_aligned;
181static struct psref_class *ifnet_psref_class __read_mostly; 181static struct psref_class *ifnet_psref_class __read_mostly;
182static pserialize_t ifnet_psz; 182static pserialize_t ifnet_psz;
183static struct workqueue *ifnet_link_state_wq __read_mostly; 183static struct workqueue *ifnet_link_state_wq __read_mostly;
184 184
185static kmutex_t if_clone_mtx; 185static kmutex_t if_clone_mtx;
186 186
187struct ifnet *lo0ifp; 187struct ifnet *lo0ifp;
188int ifqmaxlen = IFQ_MAXLEN; 188int ifqmaxlen = IFQ_MAXLEN;
189 189
190struct psref_class *ifa_psref_class __read_mostly; 190struct psref_class *ifa_psref_class __read_mostly;
191 191
192static int if_delroute_matcher(struct rtentry *, void *); 192static int if_delroute_matcher(struct rtentry *, void *);
193 193
194static bool if_is_unit(const char *); 194static bool if_is_unit(const char *);
195static struct if_clone *if_clone_lookup(const char *, int *); 195static struct if_clone *if_clone_lookup(const char *, int *);
196 196
197static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners); 197static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
198static int if_cloners_count; 198static int if_cloners_count;
199 199
200/* Packet filtering hook for interfaces. */ 200/* Packet filtering hook for interfaces. */
201pfil_head_t * if_pfil __read_mostly; 201pfil_head_t * if_pfil __read_mostly;
202 202
203static kauth_listener_t if_listener; 203static kauth_listener_t if_listener;
204 204
205static int doifioctl(struct socket *, u_long, void *, struct lwp *); 205static int doifioctl(struct socket *, u_long, void *, struct lwp *);
206static void if_detach_queues(struct ifnet *, struct ifqueue *); 206static void if_detach_queues(struct ifnet *, struct ifqueue *);
207static void sysctl_sndq_setup(struct sysctllog **, const char *, 207static void sysctl_sndq_setup(struct sysctllog **, const char *,
208 struct ifaltq *); 208 struct ifaltq *);
209static void if_slowtimo(void *); 209static void if_slowtimo(void *);
210static void if_attachdomain1(struct ifnet *); 210static void if_attachdomain1(struct ifnet *);
211static int ifconf(u_long, void *); 211static int ifconf(u_long, void *);
212static int if_transmit(struct ifnet *, struct mbuf *); 212static int if_transmit(struct ifnet *, struct mbuf *);
213static int if_clone_create(const char *); 213static int if_clone_create(const char *);
214static int if_clone_destroy(const char *); 214static int if_clone_destroy(const char *);
215static void if_link_state_change_work(struct work *, void *); 215static void if_link_state_change_work(struct work *, void *);
216static void if_up_locked(struct ifnet *); 216static void if_up_locked(struct ifnet *);
217static void _if_down(struct ifnet *); 217static void _if_down(struct ifnet *);
218static void if_down_deactivated(struct ifnet *); 218static void if_down_deactivated(struct ifnet *);
219 219
220struct if_percpuq { 220struct if_percpuq {
221 struct ifnet *ipq_ifp; 221 struct ifnet *ipq_ifp;
222 void *ipq_si; 222 void *ipq_si;
223 struct percpu *ipq_ifqs; /* struct ifqueue */ 223 struct percpu *ipq_ifqs; /* struct ifqueue */
224}; 224};
225 225
226static struct mbuf *if_percpuq_dequeue(struct if_percpuq *); 226static struct mbuf *if_percpuq_dequeue(struct if_percpuq *);
227 227
228static void if_percpuq_drops(void *, void *, struct cpu_info *); 228static void if_percpuq_drops(void *, void *, struct cpu_info *);
229static int sysctl_percpuq_drops_handler(SYSCTLFN_PROTO); 229static int sysctl_percpuq_drops_handler(SYSCTLFN_PROTO);
230static void sysctl_percpuq_setup(struct sysctllog **, const char *, 230static void sysctl_percpuq_setup(struct sysctllog **, const char *,
231 struct if_percpuq *); 231 struct if_percpuq *);
232 232
233struct if_deferred_start { 233struct if_deferred_start {
234 struct ifnet *ids_ifp; 234 struct ifnet *ids_ifp;
235 void (*ids_if_start)(struct ifnet *); 235 void (*ids_if_start)(struct ifnet *);
236 void *ids_si; 236 void *ids_si;
237}; 237};
238 238
239static void if_deferred_start_softint(void *); 239static void if_deferred_start_softint(void *);
240static void if_deferred_start_common(struct ifnet *); 240static void if_deferred_start_common(struct ifnet *);
241static void if_deferred_start_destroy(struct ifnet *); 241static void if_deferred_start_destroy(struct ifnet *);
242 242
243#if defined(INET) || defined(INET6) 243#if defined(INET) || defined(INET6)
244static void sysctl_net_pktq_setup(struct sysctllog **, int); 244static void sysctl_net_pktq_setup(struct sysctllog **, int);
245#endif 245#endif
246 246
247/* 247/*
248 * Hook for if_vlan - needed by if_agr 248 * Hook for if_vlan - needed by if_agr
249 */ 249 */
250struct if_vlan_vlan_input_hook_t if_vlan_vlan_input_hook; 250struct if_vlan_vlan_input_hook_t if_vlan_vlan_input_hook;
251 251
252static void if_sysctl_setup(struct sysctllog **); 252static void if_sysctl_setup(struct sysctllog **);
253 253
254static int 254static int
255if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 255if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
256 void *arg0, void *arg1, void *arg2, void *arg3) 256 void *arg0, void *arg1, void *arg2, void *arg3)
257{ 257{
258 int result; 258 int result;
259 enum kauth_network_req req; 259 enum kauth_network_req req;
260 260
261 result = KAUTH_RESULT_DEFER; 261 result = KAUTH_RESULT_DEFER;
262 req = (enum kauth_network_req)(uintptr_t)arg1; 262 req = (enum kauth_network_req)(uintptr_t)arg1;
263 263
264 if (action != KAUTH_NETWORK_INTERFACE) 264 if (action != KAUTH_NETWORK_INTERFACE)
265 return result; 265 return result;
266 266
267 if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) || 267 if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) ||
268 (req == KAUTH_REQ_NETWORK_INTERFACE_SET)) 268 (req == KAUTH_REQ_NETWORK_INTERFACE_SET))
269 result = KAUTH_RESULT_ALLOW; 269 result = KAUTH_RESULT_ALLOW;
270 270
271 return result; 271 return result;
272} 272}
273 273
274/* 274/*
275 * Network interface utility routines. 275 * Network interface utility routines.
276 * 276 *
277 * Routines with ifa_ifwith* names take sockaddr *'s as 277 * Routines with ifa_ifwith* names take sockaddr *'s as
278 * parameters. 278 * parameters.
279 */ 279 */
280void 280void
281ifinit(void) 281ifinit(void)
282{ 282{
283 283
284#if (defined(INET) || defined(INET6)) 284#if (defined(INET) || defined(INET6))
285 encapinit(); 285 encapinit();
286#endif 286#endif
287 287
288 if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK, 288 if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
289 if_listener_cb, NULL); 289 if_listener_cb, NULL);
290 290
291 /* interfaces are available, inform socket code */ 291 /* interfaces are available, inform socket code */
292 ifioctl = doifioctl; 292 ifioctl = doifioctl;
293} 293}
294 294
295/* 295/*
296 * XXX Initialization before configure(). 296 * XXX Initialization before configure().
297 * XXX hack to get pfil_add_hook working in autoconf. 297 * XXX hack to get pfil_add_hook working in autoconf.
298 */ 298 */
299void 299void
300ifinit1(void) 300ifinit1(void)
301{ 301{
302 int error __diagused; 302 int error __diagused;
303 303
304#ifdef NET_MPSAFE 304#ifdef NET_MPSAFE
305 printf("NET_MPSAFE enabled\n"); 305 printf("NET_MPSAFE enabled\n");
306#endif 306#endif
307 307
308 mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE); 308 mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE);
309 309
310 TAILQ_INIT(&ifnet_list); 310 TAILQ_INIT(&ifnet_list);
311 mutex_init(&ifnet_mtx, MUTEX_DEFAULT, IPL_NONE); 311 mutex_init(&ifnet_mtx, MUTEX_DEFAULT, IPL_NONE);
312 ifnet_psz = pserialize_create(); 312 ifnet_psz = pserialize_create();
313 ifnet_psref_class = psref_class_create("ifnet", IPL_SOFTNET); 313 ifnet_psref_class = psref_class_create("ifnet", IPL_SOFTNET);
314 ifa_psref_class = psref_class_create("ifa", IPL_SOFTNET); 314 ifa_psref_class = psref_class_create("ifa", IPL_SOFTNET);
315 error = workqueue_create(&ifnet_link_state_wq, "iflnkst", 315 error = workqueue_create(&ifnet_link_state_wq, "iflnkst",
316 if_link_state_change_work, NULL, PRI_SOFTNET, IPL_NET, 316 if_link_state_change_work, NULL, PRI_SOFTNET, IPL_NET,
317 WQ_MPSAFE); 317 WQ_MPSAFE);
318 KASSERT(error == 0); 318 KASSERT(error == 0);
319 PSLIST_INIT(&ifnet_pslist); 319 PSLIST_INIT(&ifnet_pslist);
320 320
321 if_indexlim = 8; 321 if_indexlim = 8;
322 322
323 if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL); 323 if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL);
324 KASSERT(if_pfil != NULL); 324 KASSERT(if_pfil != NULL);
325 325
326#if NETHER > 0 || defined(NETATALK) || defined(WLAN) 326#if NETHER > 0 || defined(NETATALK) || defined(WLAN)
327 etherinit(); 327 etherinit();
328#endif 328#endif
329} 329}
330 330
331/* XXX must be after domaininit() */ 331/* XXX must be after domaininit() */
332void 332void
333ifinit_post(void) 333ifinit_post(void)
334{ 334{
335 335
336 if_sysctl_setup(NULL); 336 if_sysctl_setup(NULL);
337} 337}
338 338
339ifnet_t * 339ifnet_t *
340if_alloc(u_char type) 340if_alloc(u_char type)
341{ 341{
342 return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP); 342 return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP);
343} 343}
344 344
345void 345void
346if_free(ifnet_t *ifp) 346if_free(ifnet_t *ifp)
347{ 347{
348 kmem_free(ifp, sizeof(ifnet_t)); 348 kmem_free(ifp, sizeof(ifnet_t));
349} 349}
350 350
351void 351void
352if_initname(struct ifnet *ifp, const char *name, int unit) 352if_initname(struct ifnet *ifp, const char *name, int unit)
353{ 353{
354 (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname), 354 (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname),
355 "%s%d", name, unit); 355 "%s%d", name, unit);
356} 356}
357 357
358/* 358/*
359 * Null routines used while an interface is going away. These routines 359 * Null routines used while an interface is going away. These routines
360 * just return an error. 360 * just return an error.
361 */ 361 */
362 362
363int 363int
364if_nulloutput(struct ifnet *ifp, struct mbuf *m, 364if_nulloutput(struct ifnet *ifp, struct mbuf *m,
365 const struct sockaddr *so, const struct rtentry *rt) 365 const struct sockaddr *so, const struct rtentry *rt)
366{ 366{
367 367
368 return ENXIO; 368 return ENXIO;
369} 369}
370 370
371void 371void
372if_nullinput(struct ifnet *ifp, struct mbuf *m) 372if_nullinput(struct ifnet *ifp, struct mbuf *m)
373{ 373{
374 374
375 /* Nothing. */ 375 /* Nothing. */
376} 376}
377 377
378void 378void
379if_nullstart(struct ifnet *ifp) 379if_nullstart(struct ifnet *ifp)
380{ 380{
381 381
382 /* Nothing. */ 382 /* Nothing. */
383} 383}
384 384
385int 385int
386if_nulltransmit(struct ifnet *ifp, struct mbuf *m) 386if_nulltransmit(struct ifnet *ifp, struct mbuf *m)
387{ 387{
388 388
389 m_freem(m); 389 m_freem(m);
390 return ENXIO; 390 return ENXIO;
391} 391}
392 392
393int 393int
394if_nullioctl(struct ifnet *ifp, u_long cmd, void *data) 394if_nullioctl(struct ifnet *ifp, u_long cmd, void *data)
395{ 395{
396 396
397 return ENXIO; 397 return ENXIO;
398} 398}
399 399
400int 400int
401if_nullinit(struct ifnet *ifp) 401if_nullinit(struct ifnet *ifp)
402{ 402{
403 403
404 return ENXIO; 404 return ENXIO;
405} 405}
406 406
407void 407void
408if_nullstop(struct ifnet *ifp, int disable) 408if_nullstop(struct ifnet *ifp, int disable)
409{ 409{
410 410
411 /* Nothing. */ 411 /* Nothing. */
412} 412}
413 413
414void 414void
415if_nullslowtimo(struct ifnet *ifp) 415if_nullslowtimo(struct ifnet *ifp)
416{ 416{
417 417
418 /* Nothing. */ 418 /* Nothing. */
419} 419}
420 420
421void 421void
422if_nulldrain(struct ifnet *ifp) 422if_nulldrain(struct ifnet *ifp)
423{ 423{
424 424
425 /* Nothing. */ 425 /* Nothing. */
426} 426}
427 427
428void 428void
429if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory) 429if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory)
430{ 430{
431 struct ifaddr *ifa; 431 struct ifaddr *ifa;
432 struct sockaddr_dl *sdl; 432 struct sockaddr_dl *sdl;
433 433
434 ifp->if_addrlen = addrlen; 434 ifp->if_addrlen = addrlen;
435 if_alloc_sadl(ifp); 435 if_alloc_sadl(ifp);
436 ifa = ifp->if_dl; 436 ifa = ifp->if_dl;
437 sdl = satosdl(ifa->ifa_addr); 437 sdl = satosdl(ifa->ifa_addr);
438 438
439 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen); 439 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen);
440 if (factory) { 440 if (factory) {
441 KASSERT(ifp->if_hwdl == NULL); 441 KASSERT(ifp->if_hwdl == NULL);
442 ifp->if_hwdl = ifp->if_dl; 442 ifp->if_hwdl = ifp->if_dl;
443 ifaref(ifp->if_hwdl); 443 ifaref(ifp->if_hwdl);
444 } 444 }
445 /* TBD routing socket */ 445 /* TBD routing socket */
446} 446}
447 447
448struct ifaddr * 448struct ifaddr *
449if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp) 449if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp)
450{ 450{
451 unsigned socksize, ifasize; 451 unsigned socksize, ifasize;
452 int addrlen, namelen; 452 int addrlen, namelen;
453 struct sockaddr_dl *mask, *sdl; 453 struct sockaddr_dl *mask, *sdl;
454 struct ifaddr *ifa; 454 struct ifaddr *ifa;
455 455
456 namelen = strlen(ifp->if_xname); 456 namelen = strlen(ifp->if_xname);
457 addrlen = ifp->if_addrlen; 457 addrlen = ifp->if_addrlen;
458 socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long)); 458 socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long));
459 ifasize = sizeof(*ifa) + 2 * socksize; 459 ifasize = sizeof(*ifa) + 2 * socksize;
460 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 460 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
461 461
462 sdl = (struct sockaddr_dl *)(ifa + 1); 462 sdl = (struct sockaddr_dl *)(ifa + 1);
463 mask = (struct sockaddr_dl *)(socksize + (char *)sdl); 463 mask = (struct sockaddr_dl *)(socksize + (char *)sdl);
464 464
465 sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type, 465 sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type,
466 ifp->if_xname, namelen, NULL, addrlen); 466 ifp->if_xname, namelen, NULL, addrlen);
467 mask->sdl_family = AF_LINK; 467 mask->sdl_family = AF_LINK;
468 mask->sdl_len = sockaddr_dl_measure(namelen, 0); 468 mask->sdl_len = sockaddr_dl_measure(namelen, 0);
469 memset(&mask->sdl_data[0], 0xff, namelen); 469 memset(&mask->sdl_data[0], 0xff, namelen);
470 ifa->ifa_rtrequest = link_rtrequest; 470 ifa->ifa_rtrequest = link_rtrequest;
471 ifa->ifa_addr = (struct sockaddr *)sdl; 471 ifa->ifa_addr = (struct sockaddr *)sdl;
472 ifa->ifa_netmask = (struct sockaddr *)mask; 472 ifa->ifa_netmask = (struct sockaddr *)mask;
473 ifa_psref_init(ifa); 473 ifa_psref_init(ifa);
474 474
475 *sdlp = sdl; 475 *sdlp = sdl;
476 476
477 return ifa; 477 return ifa;
478} 478}
479 479
480static void 480static void
481if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa) 481if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa)
482{ 482{
483 const struct sockaddr_dl *sdl; 483 const struct sockaddr_dl *sdl;
484 484
485 ifp->if_dl = ifa; 485 ifp->if_dl = ifa;
486 ifaref(ifa); 486 ifaref(ifa);
487 sdl = satosdl(ifa->ifa_addr); 487 sdl = satosdl(ifa->ifa_addr);
488 ifp->if_sadl = sdl; 488 ifp->if_sadl = sdl;
489} 489}
490 490
491/* 491/*
492 * Allocate the link level name for the specified interface. This 492 * Allocate the link level name for the specified interface. This
493 * is an attachment helper. It must be called after ifp->if_addrlen 493 * is an attachment helper. It must be called after ifp->if_addrlen
494 * is initialized, which may not be the case when if_attach() is 494 * is initialized, which may not be the case when if_attach() is
495 * called. 495 * called.
496 */ 496 */
497void 497void
498if_alloc_sadl(struct ifnet *ifp) 498if_alloc_sadl(struct ifnet *ifp)
499{ 499{
500 struct ifaddr *ifa; 500 struct ifaddr *ifa;
501 const struct sockaddr_dl *sdl; 501 const struct sockaddr_dl *sdl;
502 502
503 /* 503 /*
504 * If the interface already has a link name, release it 504 * If the interface already has a link name, release it
505 * now. This is useful for interfaces that can change 505 * now. This is useful for interfaces that can change
506 * link types, and thus switch link names often. 506 * link types, and thus switch link names often.
507 */ 507 */
508 if (ifp->if_sadl != NULL) 508 if (ifp->if_sadl != NULL)
509 if_free_sadl(ifp, 0); 509 if_free_sadl(ifp, 0);
510 510
511 ifa = if_dl_create(ifp, &sdl); 511 ifa = if_dl_create(ifp, &sdl);
512 512
513 ifa_insert(ifp, ifa); 513 ifa_insert(ifp, ifa);
514 if_sadl_setrefs(ifp, ifa); 514 if_sadl_setrefs(ifp, ifa);
515} 515}
516 516
517static void 517static void
518if_deactivate_sadl(struct ifnet *ifp) 518if_deactivate_sadl(struct ifnet *ifp)
519{ 519{
520 struct ifaddr *ifa; 520 struct ifaddr *ifa;
521 521
522 KASSERT(ifp->if_dl != NULL); 522 KASSERT(ifp->if_dl != NULL);
523 523
524 ifa = ifp->if_dl; 524 ifa = ifp->if_dl;
525 525
526 ifp->if_sadl = NULL; 526 ifp->if_sadl = NULL;
527 527
528 ifp->if_dl = NULL; 528 ifp->if_dl = NULL;
529 ifafree(ifa); 529 ifafree(ifa);
530} 530}
531 531
532static void 532static void
533if_replace_sadl(struct ifnet *ifp, struct ifaddr *ifa) 533if_replace_sadl(struct ifnet *ifp, struct ifaddr *ifa)
534{ 534{
535 struct ifaddr *old; 535 struct ifaddr *old;
536 536
537 KASSERT(ifp->if_dl != NULL); 537 KASSERT(ifp->if_dl != NULL);
538 538
539 old = ifp->if_dl; 539 old = ifp->if_dl;
540 540
541 ifaref(ifa); 541 ifaref(ifa);
542 /* XXX Update if_dl and if_sadl atomically */ 542 /* XXX Update if_dl and if_sadl atomically */
543 ifp->if_dl = ifa; 543 ifp->if_dl = ifa;
544 ifp->if_sadl = satosdl(ifa->ifa_addr); 544 ifp->if_sadl = satosdl(ifa->ifa_addr);
545 545
546 ifafree(old); 546 ifafree(old);
547} 547}
548 548
549void 549void
550if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa0, 550if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa0,
551 const struct sockaddr_dl *sdl) 551 const struct sockaddr_dl *sdl)
552{ 552{
553 int s, ss; 553 int s, ss;
554 struct ifaddr *ifa; 554 struct ifaddr *ifa;
555 int bound = curlwp_bind(); 555 int bound = curlwp_bind();
556 556
557 KASSERT(ifa_held(ifa0)); 557 KASSERT(ifa_held(ifa0));
558 558
559 s = splsoftnet(); 559 s = splsoftnet();
560 560
561 if_replace_sadl(ifp, ifa0); 561 if_replace_sadl(ifp, ifa0);
562 562
563 ss = pserialize_read_enter(); 563 ss = pserialize_read_enter();
564 IFADDR_READER_FOREACH(ifa, ifp) { 564 IFADDR_READER_FOREACH(ifa, ifp) {
565 struct psref psref; 565 struct psref psref;
566 ifa_acquire(ifa, &psref); 566 ifa_acquire(ifa, &psref);
567 pserialize_read_exit(ss); 567 pserialize_read_exit(ss);
568 568
569 rtinit(ifa, RTM_LLINFO_UPD, 0); 569 rtinit(ifa, RTM_LLINFO_UPD, 0);
570 570
571 ss = pserialize_read_enter(); 571 ss = pserialize_read_enter();
572 ifa_release(ifa, &psref); 572 ifa_release(ifa, &psref);
573 } 573 }
574 pserialize_read_exit(ss); 574 pserialize_read_exit(ss);
575 575
576 splx(s); 576 splx(s);
577 curlwp_bindx(bound); 577 curlwp_bindx(bound);
578} 578}
579 579
580/* 580/*
581 * Free the link level name for the specified interface. This is 581 * Free the link level name for the specified interface. This is
582 * a detach helper. This is called from if_detach(). 582 * a detach helper. This is called from if_detach().
583 */ 583 */
584void 584void
585if_free_sadl(struct ifnet *ifp, int factory) 585if_free_sadl(struct ifnet *ifp, int factory)
586{ 586{
587 struct ifaddr *ifa; 587 struct ifaddr *ifa;
588 int s; 588 int s;
589 589
590 if (factory && ifp->if_hwdl != NULL) { 590 if (factory && ifp->if_hwdl != NULL) {
591 ifa = ifp->if_hwdl; 591 ifa = ifp->if_hwdl;
592 ifp->if_hwdl = NULL; 592 ifp->if_hwdl = NULL;
593 ifafree(ifa); 593 ifafree(ifa);
594 } 594 }
595 595
596 ifa = ifp->if_dl; 596 ifa = ifp->if_dl;
597 if (ifa == NULL) { 597 if (ifa == NULL) {
598 KASSERT(ifp->if_sadl == NULL); 598 KASSERT(ifp->if_sadl == NULL);
599 return; 599 return;
600 } 600 }
601 601
602 KASSERT(ifp->if_sadl != NULL); 602 KASSERT(ifp->if_sadl != NULL);
603 603
604 s = splsoftnet(); 604 s = splsoftnet();
605 KASSERT(ifa->ifa_addr->sa_family == AF_LINK); 605 KASSERT(ifa->ifa_addr->sa_family == AF_LINK);
606 ifa_remove(ifp, ifa); 606 ifa_remove(ifp, ifa);
607 if_deactivate_sadl(ifp); 607 if_deactivate_sadl(ifp);
608 splx(s); 608 splx(s);
609} 609}
610 610
611static void 611static void
612if_getindex(ifnet_t *ifp) 612if_getindex(ifnet_t *ifp)
613{ 613{
614 bool hitlimit = false; 614 bool hitlimit = false;
615 615
616 ifp->if_index_gen = index_gen++; 616 ifp->if_index_gen = index_gen++;
617 617
618 ifp->if_index = if_index; 618 ifp->if_index = if_index;
619 if (ifindex2ifnet == NULL) { 619 if (ifindex2ifnet == NULL) {
620 if_index++; 620 if_index++;
621 goto skip; 621 goto skip;
622 } 622 }
623 while (if_byindex(ifp->if_index)) { 623 while (if_byindex(ifp->if_index)) {
624 /* 624 /*
625 * If we hit USHRT_MAX, we skip back to 0 since 625 * If we hit USHRT_MAX, we skip back to 0 since
626 * there are a number of places where the value 626 * there are a number of places where the value
627 * of if_index or if_index itself is compared 627 * of if_index or if_index itself is compared
628 * to or stored in an unsigned short. By 628 * to or stored in an unsigned short. By
629 * jumping back, we won't botch those assignments 629 * jumping back, we won't botch those assignments
630 * or comparisons. 630 * or comparisons.
631 */ 631 */
632 if (++if_index == 0) { 632 if (++if_index == 0) {
633 if_index = 1; 633 if_index = 1;
634 } else if (if_index == USHRT_MAX) { 634 } else if (if_index == USHRT_MAX) {
635 /* 635 /*
636 * However, if we have to jump back to 636 * However, if we have to jump back to
637 * zero *twice* without finding an empty 637 * zero *twice* without finding an empty
638 * slot in ifindex2ifnet[], then there 638 * slot in ifindex2ifnet[], then there
639 * there are too many (>65535) interfaces. 639 * there are too many (>65535) interfaces.
640 */ 640 */
641 if (hitlimit) { 641 if (hitlimit) {
642 panic("too many interfaces"); 642 panic("too many interfaces");
643 } 643 }
644 hitlimit = true; 644 hitlimit = true;
645 if_index = 1; 645 if_index = 1;
646 } 646 }
647 ifp->if_index = if_index; 647 ifp->if_index = if_index;
648 } 648 }
649skip: 649skip:
650 /* 650 /*
651 * ifindex2ifnet is indexed by if_index. Since if_index will 651 * ifindex2ifnet is indexed by if_index. Since if_index will
652 * grow dynamically, it should grow too. 652 * grow dynamically, it should grow too.
653 */ 653 */
654 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) { 654 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
655 size_t m, n, oldlim; 655 size_t m, n, oldlim;
656 void *q; 656 void *q;
657 657
658 oldlim = if_indexlim; 658 oldlim = if_indexlim;
659 while (ifp->if_index >= if_indexlim) 659 while (ifp->if_index >= if_indexlim)
660 if_indexlim <<= 1; 660 if_indexlim <<= 1;
661 661
662 /* grow ifindex2ifnet */ 662 /* grow ifindex2ifnet */
663 m = oldlim * sizeof(struct ifnet *); 663 m = oldlim * sizeof(struct ifnet *);
664 n = if_indexlim * sizeof(struct ifnet *); 664 n = if_indexlim * sizeof(struct ifnet *);
665 q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO); 665 q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
666 if (ifindex2ifnet != NULL) { 666 if (ifindex2ifnet != NULL) {
667 memcpy(q, ifindex2ifnet, m); 667 memcpy(q, ifindex2ifnet, m);
668 free(ifindex2ifnet, M_IFADDR); 668 free(ifindex2ifnet, M_IFADDR);
669 } 669 }
670 ifindex2ifnet = (struct ifnet **)q; 670 ifindex2ifnet = (struct ifnet **)q;
671 } 671 }
672 ifindex2ifnet[ifp->if_index] = ifp; 672 ifindex2ifnet[ifp->if_index] = ifp;
673} 673}
674 674
675/* 675/*
676 * Initialize an interface and assign an index for it. 676 * Initialize an interface and assign an index for it.
677 * 677 *
678 * It must be called prior to a device specific attach routine 678 * It must be called prior to a device specific attach routine
679 * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl, 679 * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl,
680 * and be followed by if_register: 680 * and be followed by if_register:
681 * 681 *
682 * if_initialize(ifp); 682 * if_initialize(ifp);
683 * ether_ifattach(ifp, enaddr); 683 * ether_ifattach(ifp, enaddr);
684 * if_register(ifp); 684 * if_register(ifp);
685 */ 685 */
686int 686int
687if_initialize(ifnet_t *ifp) 687if_initialize(ifnet_t *ifp)
688{ 688{
689 int rv = 0; 689 int rv = 0;
690 690
691 KASSERT(if_indexlim > 0); 691 KASSERT(if_indexlim > 0);
692 TAILQ_INIT(&ifp->if_addrlist); 692 TAILQ_INIT(&ifp->if_addrlist);
693 693
694 /* 694 /*
695 * Link level name is allocated later by a separate call to 695 * Link level name is allocated later by a separate call to
696 * if_alloc_sadl(). 696 * if_alloc_sadl().
697 */ 697 */
698 698
699 if (ifp->if_snd.ifq_maxlen == 0) 699 if (ifp->if_snd.ifq_maxlen == 0)
700 ifp->if_snd.ifq_maxlen = ifqmaxlen; 700 ifp->if_snd.ifq_maxlen = ifqmaxlen;
701 701
702 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */ 702 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
703 703
704 ifp->if_link_state = LINK_STATE_UNKNOWN; 704 ifp->if_link_state = LINK_STATE_UNKNOWN;
705 ifp->if_link_queue = -1; /* all bits set, see link_state_change() */ 705 ifp->if_link_queue = -1; /* all bits set, see link_state_change() */
706 706
707 ifp->if_capenable = 0; 707 ifp->if_capenable = 0;
708 ifp->if_csum_flags_tx = 0; 708 ifp->if_csum_flags_tx = 0;
709 ifp->if_csum_flags_rx = 0; 709 ifp->if_csum_flags_rx = 0;
710 710
711#ifdef ALTQ 711#ifdef ALTQ
712 ifp->if_snd.altq_type = 0; 712 ifp->if_snd.altq_type = 0;
713 ifp->if_snd.altq_disc = NULL; 713 ifp->if_snd.altq_disc = NULL;
714 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 714 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE;
715 ifp->if_snd.altq_tbr = NULL; 715 ifp->if_snd.altq_tbr = NULL;
716 ifp->if_snd.altq_ifp = ifp; 716 ifp->if_snd.altq_ifp = ifp;
717#endif 717#endif
718 718
719 IFQ_LOCK_INIT(&ifp->if_snd); 719 IFQ_LOCK_INIT(&ifp->if_snd);
720 720
721 ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp); 721 ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp);
722 pfil_run_ifhooks(if_pfil, PFIL_IFNET_ATTACH, ifp); 722 pfil_run_ifhooks(if_pfil, PFIL_IFNET_ATTACH, ifp);
723 723
724 IF_AFDATA_LOCK_INIT(ifp); 724 IF_AFDATA_LOCK_INIT(ifp);
725 725
726 PSLIST_ENTRY_INIT(ifp, if_pslist_entry); 726 PSLIST_ENTRY_INIT(ifp, if_pslist_entry);
727 PSLIST_INIT(&ifp->if_addr_pslist); 727 PSLIST_INIT(&ifp->if_addr_pslist);
728 psref_target_init(&ifp->if_psref, ifnet_psref_class); 728 psref_target_init(&ifp->if_psref, ifnet_psref_class);
729 ifp->if_ioctl_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 729 ifp->if_ioctl_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
730 LIST_INIT(&ifp->if_multiaddrs); 730 LIST_INIT(&ifp->if_multiaddrs);
731 if ((rv = if_stats_init(ifp)) != 0) { 731 if ((rv = if_stats_init(ifp)) != 0) {
732 goto fail; 732 goto fail;
733 } 733 }
734 734
735 IFNET_GLOBAL_LOCK(); 735 IFNET_GLOBAL_LOCK();
736 if_getindex(ifp); 736 if_getindex(ifp);
737 IFNET_GLOBAL_UNLOCK(); 737 IFNET_GLOBAL_UNLOCK();
738 738
739 return 0; 739 return 0;
740 740
741fail: 741fail:
742 IF_AFDATA_LOCK_DESTROY(ifp); 742 IF_AFDATA_LOCK_DESTROY(ifp);
743 743
744 pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp); 744 pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp);
745 (void)pfil_head_destroy(ifp->if_pfil); 745 (void)pfil_head_destroy(ifp->if_pfil);
746 746
747 IFQ_LOCK_DESTROY(&ifp->if_snd); 747 IFQ_LOCK_DESTROY(&ifp->if_snd);
748 748
749 return rv; 749 return rv;
750} 750}
751 751
752/* 752/*
753 * Register an interface to the list of "active" interfaces. 753 * Register an interface to the list of "active" interfaces.
754 */ 754 */
755void 755void
756if_register(ifnet_t *ifp) 756if_register(ifnet_t *ifp)
757{ 757{
758 /* 758 /*
759 * If the driver has not supplied its own if_ioctl, then 759 * If the driver has not supplied its own if_ioctl, then
760 * supply the default. 760 * supply the default.
761 */ 761 */
762 if (ifp->if_ioctl == NULL) 762 if (ifp->if_ioctl == NULL)
763 ifp->if_ioctl = ifioctl_common; 763 ifp->if_ioctl = ifioctl_common;
764 764
765 sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd); 765 sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd);
766 766
767 if (!STAILQ_EMPTY(&domains)) 767 if (!STAILQ_EMPTY(&domains))
768 if_attachdomain1(ifp); 768 if_attachdomain1(ifp);
769 769
770 /* Announce the interface. */ 770 /* Announce the interface. */
771 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 771 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
772 772
773 if (ifp->if_slowtimo != NULL) { 773 if (ifp->if_slowtimo != NULL) {
774 ifp->if_slowtimo_ch = 774 ifp->if_slowtimo_ch =
775 kmem_zalloc(sizeof(*ifp->if_slowtimo_ch), KM_SLEEP); 775 kmem_zalloc(sizeof(*ifp->if_slowtimo_ch), KM_SLEEP);
776 callout_init(ifp->if_slowtimo_ch, 0); 776 callout_init(ifp->if_slowtimo_ch, 0);
777 callout_setfunc(ifp->if_slowtimo_ch, if_slowtimo, ifp); 777 callout_setfunc(ifp->if_slowtimo_ch, if_slowtimo, ifp);
778 if_slowtimo(ifp); 778 if_slowtimo(ifp);
779 } 779 }
780 780
781 if (ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit) 781 if (ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit)
782 ifp->if_transmit = if_transmit; 782 ifp->if_transmit = if_transmit;
783 783
784 IFNET_GLOBAL_LOCK(); 784 IFNET_GLOBAL_LOCK();
785 TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list); 785 TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list);
786 IFNET_WRITER_INSERT_TAIL(ifp); 786 IFNET_WRITER_INSERT_TAIL(ifp);
787 IFNET_GLOBAL_UNLOCK(); 787 IFNET_GLOBAL_UNLOCK();
788} 788}
789 789
790/* 790/*
791 * The if_percpuq framework 791 * The if_percpuq framework
792 * 792 *
793 * It allows network device drivers to execute the network stack 793 * It allows network device drivers to execute the network stack
794 * in softint (so called softint-based if_input). It utilizes 794 * in softint (so called softint-based if_input). It utilizes
795 * softint and percpu ifqueue. It doesn't distribute any packets 795 * softint and percpu ifqueue. It doesn't distribute any packets
796 * between CPUs, unlike pktqueue(9). 796 * between CPUs, unlike pktqueue(9).
797 * 797 *
798 * Currently we support two options for device drivers to apply the framework: 798 * Currently we support two options for device drivers to apply the framework:
799 * - Use it implicitly with less changes 799 * - Use it implicitly with less changes
800 * - If you use if_attach in driver's _attach function and if_input in 800 * - If you use if_attach in driver's _attach function and if_input in
801 * driver's Rx interrupt handler, a packet is queued and a softint handles 801 * driver's Rx interrupt handler, a packet is queued and a softint handles
802 * the packet implicitly 802 * the packet implicitly
803 * - Use it explicitly in each driver (recommended) 803 * - Use it explicitly in each driver (recommended)
804 * - You can use if_percpuq_* directly in your driver 804 * - You can use if_percpuq_* directly in your driver
805 * - In this case, you need to allocate struct if_percpuq in driver's softc 805 * - In this case, you need to allocate struct if_percpuq in driver's softc
806 * - See wm(4) as a reference implementation 806 * - See wm(4) as a reference implementation
807 */ 807 */
808 808
809static void 809static void
810if_percpuq_softint(void *arg) 810if_percpuq_softint(void *arg)
811{ 811{
812 struct if_percpuq *ipq = arg; 812 struct if_percpuq *ipq = arg;
813 struct ifnet *ifp = ipq->ipq_ifp; 813 struct ifnet *ifp = ipq->ipq_ifp;
814 struct mbuf *m; 814 struct mbuf *m;
815 815
816 while ((m = if_percpuq_dequeue(ipq)) != NULL) { 816 while ((m = if_percpuq_dequeue(ipq)) != NULL) {
817 if_statinc(ifp, if_ipackets); 817 if_statinc(ifp, if_ipackets);
818 bpf_mtap(ifp, m, BPF_D_IN); 818 bpf_mtap(ifp, m, BPF_D_IN);
819 819
820 ifp->_if_input(ifp, m); 820 ifp->_if_input(ifp, m);
821 } 821 }
822} 822}
823 823
824static void 824static void
825if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused) 825if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
826{ 826{
827 struct ifqueue *const ifq = p; 827 struct ifqueue *const ifq = p;
828 828
829 memset(ifq, 0, sizeof(*ifq)); 829 memset(ifq, 0, sizeof(*ifq));
830 ifq->ifq_maxlen = IFQ_MAXLEN; 830 ifq->ifq_maxlen = IFQ_MAXLEN;
831} 831}
832 832
833struct if_percpuq * 833struct if_percpuq *
834if_percpuq_create(struct ifnet *ifp) 834if_percpuq_create(struct ifnet *ifp)
835{ 835{
836 struct if_percpuq *ipq; 836 struct if_percpuq *ipq;
837 u_int flags = SOFTINT_NET; 837 u_int flags = SOFTINT_NET;
838 838
839 flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0; 839 flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0;
840 840
841 ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP); 841 ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP);
842 ipq->ipq_ifp = ifp; 842 ipq->ipq_ifp = ifp;
843 ipq->ipq_si = softint_establish(flags, if_percpuq_softint, ipq); 843 ipq->ipq_si = softint_establish(flags, if_percpuq_softint, ipq);
844 ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue)); 844 ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue));
845 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL); 845 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL);
846 846
847 sysctl_percpuq_setup(&ifp->if_sysctl_log, ifp->if_xname, ipq); 847 sysctl_percpuq_setup(&ifp->if_sysctl_log, ifp->if_xname, ipq);
848 848
849 return ipq; 849 return ipq;
850} 850}
851 851
852static struct mbuf * 852static struct mbuf *
853if_percpuq_dequeue(struct if_percpuq *ipq) 853if_percpuq_dequeue(struct if_percpuq *ipq)
854{ 854{
855 struct mbuf *m; 855 struct mbuf *m;
856 struct ifqueue *ifq; 856 struct ifqueue *ifq;
857 int s; 857 int s;
858 858
859 s = splnet(); 859 s = splnet();
860 ifq = percpu_getref(ipq->ipq_ifqs); 860 ifq = percpu_getref(ipq->ipq_ifqs);
861 IF_DEQUEUE(ifq, m); 861 IF_DEQUEUE(ifq, m);
862 percpu_putref(ipq->ipq_ifqs); 862 percpu_putref(ipq->ipq_ifqs);
863 splx(s); 863 splx(s);
864 864
865 return m; 865 return m;
866} 866}
867 867
868static void 868static void
869if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused) 869if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused)
870{ 870{
871 struct ifqueue *const ifq = p; 871 struct ifqueue *const ifq = p;
872 872
873 IF_PURGE(ifq); 873 IF_PURGE(ifq);
874} 874}
875 875
876void 876void
877if_percpuq_destroy(struct if_percpuq *ipq) 877if_percpuq_destroy(struct if_percpuq *ipq)
878{ 878{
879 879
880 /* if_detach may already destroy it */ 880 /* if_detach may already destroy it */
881 if (ipq == NULL) 881 if (ipq == NULL)
882 return; 882 return;
883 883
884 softint_disestablish(ipq->ipq_si); 884 softint_disestablish(ipq->ipq_si);
885 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL); 885 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL);
886 percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue)); 886 percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue));
887 kmem_free(ipq, sizeof(*ipq)); 887 kmem_free(ipq, sizeof(*ipq));
888} 888}
889 889
890void 890void
891if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m) 891if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m)
892{ 892{
893 struct ifqueue *ifq; 893 struct ifqueue *ifq;
894 int s; 894 int s;
895 895
896 KASSERT(ipq != NULL); 896 KASSERT(ipq != NULL);
897 897
898 s = splnet(); 898 s = splnet();
899 ifq = percpu_getref(ipq->ipq_ifqs); 899 ifq = percpu_getref(ipq->ipq_ifqs);
900 if (IF_QFULL(ifq)) { 900 if (IF_QFULL(ifq)) {
901 IF_DROP(ifq); 901 IF_DROP(ifq);
902 percpu_putref(ipq->ipq_ifqs); 902 percpu_putref(ipq->ipq_ifqs);
903 m_freem(m); 903 m_freem(m);
904 goto out; 904 goto out;
905 } 905 }
906 IF_ENQUEUE(ifq, m); 906 IF_ENQUEUE(ifq, m);
907 percpu_putref(ipq->ipq_ifqs); 907 percpu_putref(ipq->ipq_ifqs);
908 908
909 softint_schedule(ipq->ipq_si); 909 softint_schedule(ipq->ipq_si);
910out: 910out:
911 splx(s); 911 splx(s);
912} 912}
913 913
914static void 914static void
915if_percpuq_drops(void *p, void *arg, struct cpu_info *ci __unused) 915if_percpuq_drops(void *p, void *arg, struct cpu_info *ci __unused)
916{ 916{
917 struct ifqueue *const ifq = p; 917 struct ifqueue *const ifq = p;
918 int *sum = arg; 918 int *sum = arg;
919 919
920 *sum += ifq->ifq_drops; 920 *sum += ifq->ifq_drops;
921} 921}
922 922
923static int 923static int
924sysctl_percpuq_drops_handler(SYSCTLFN_ARGS) 924sysctl_percpuq_drops_handler(SYSCTLFN_ARGS)
925{ 925{
926 struct sysctlnode node; 926 struct sysctlnode node;
927 struct if_percpuq *ipq; 927 struct if_percpuq *ipq;
928 int sum = 0; 928 int sum = 0;
929 int error; 929 int error;
930 930
931 node = *rnode; 931 node = *rnode;
932 ipq = node.sysctl_data; 932 ipq = node.sysctl_data;
933 933
934 percpu_foreach(ipq->ipq_ifqs, if_percpuq_drops, &sum); 934 percpu_foreach(ipq->ipq_ifqs, if_percpuq_drops, &sum);
935 935
936 node.sysctl_data = &sum; 936 node.sysctl_data = &sum;
937 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 937 error = sysctl_lookup(SYSCTLFN_CALL(&node));
938 if (error != 0 || newp == NULL) 938 if (error != 0 || newp == NULL)
939 return error; 939 return error;
940 940
941 return 0; 941 return 0;
942} 942}
943 943
944static void 944static void
945sysctl_percpuq_setup(struct sysctllog **clog, const char* ifname, 945sysctl_percpuq_setup(struct sysctllog **clog, const char* ifname,
946 struct if_percpuq *ipq) 946 struct if_percpuq *ipq)
947{ 947{
948 const struct sysctlnode *cnode, *rnode; 948 const struct sysctlnode *cnode, *rnode;
949 949
950 if (sysctl_createv(clog, 0, NULL, &rnode, 950 if (sysctl_createv(clog, 0, NULL, &rnode,
951 CTLFLAG_PERMANENT, 951 CTLFLAG_PERMANENT,
952 CTLTYPE_NODE, "interfaces", 952 CTLTYPE_NODE, "interfaces",
953 SYSCTL_DESCR("Per-interface controls"), 953 SYSCTL_DESCR("Per-interface controls"),
954 NULL, 0, NULL, 0, 954 NULL, 0, NULL, 0,
955 CTL_NET, CTL_CREATE, CTL_EOL) != 0) 955 CTL_NET, CTL_CREATE, CTL_EOL) != 0)
956 goto bad; 956 goto bad;
957 957
958 if (sysctl_createv(clog, 0, &rnode, &rnode, 958 if (sysctl_createv(clog, 0, &rnode, &rnode,
959 CTLFLAG_PERMANENT, 959 CTLFLAG_PERMANENT,
960 CTLTYPE_NODE, ifname, 960 CTLTYPE_NODE, ifname,
961 SYSCTL_DESCR("Interface controls"), 961 SYSCTL_DESCR("Interface controls"),
962 NULL, 0, NULL, 0, 962 NULL, 0, NULL, 0,
963 CTL_CREATE, CTL_EOL) != 0) 963 CTL_CREATE, CTL_EOL) != 0)
964 goto bad; 964 goto bad;
965 965
966 if (sysctl_createv(clog, 0, &rnode, &rnode, 966 if (sysctl_createv(clog, 0, &rnode, &rnode,
967 CTLFLAG_PERMANENT, 967 CTLFLAG_PERMANENT,
968 CTLTYPE_NODE, "rcvq", 968 CTLTYPE_NODE, "rcvq",
969 SYSCTL_DESCR("Interface input queue controls"), 969 SYSCTL_DESCR("Interface input queue controls"),
970 NULL, 0, NULL, 0, 970 NULL, 0, NULL, 0,
971 CTL_CREATE, CTL_EOL) != 0) 971 CTL_CREATE, CTL_EOL) != 0)
972 goto bad; 972 goto bad;
973 973
974#ifdef NOTYET 974#ifdef NOTYET
975 /* XXX Should show each per-CPU queue length? */ 975 /* XXX Should show each per-CPU queue length? */
976 if (sysctl_createv(clog, 0, &rnode, &rnode, 976 if (sysctl_createv(clog, 0, &rnode, &rnode,
977 CTLFLAG_PERMANENT, 977 CTLFLAG_PERMANENT,
978 CTLTYPE_INT, "len", 978 CTLTYPE_INT, "len",
979 SYSCTL_DESCR("Current input queue length"), 979 SYSCTL_DESCR("Current input queue length"),
980 sysctl_percpuq_len, 0, NULL, 0, 980 sysctl_percpuq_len, 0, NULL, 0,
981 CTL_CREATE, CTL_EOL) != 0) 981 CTL_CREATE, CTL_EOL) != 0)
982 goto bad; 982 goto bad;
983 983
984 if (sysctl_createv(clog, 0, &rnode, &cnode, 984 if (sysctl_createv(clog, 0, &rnode, &cnode,
985 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 985 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
986 CTLTYPE_INT, "maxlen", 986 CTLTYPE_INT, "maxlen",
987 SYSCTL_DESCR("Maximum allowed input queue length"), 987 SYSCTL_DESCR("Maximum allowed input queue length"),
988 sysctl_percpuq_maxlen_handler, 0, (void *)ipq, 0, 988 sysctl_percpuq_maxlen_handler, 0, (void *)ipq, 0,
989 CTL_CREATE, CTL_EOL) != 0) 989 CTL_CREATE, CTL_EOL) != 0)
990 goto bad; 990 goto bad;
991#endif 991#endif
992 992
993 if (sysctl_createv(clog, 0, &rnode, &cnode, 993 if (sysctl_createv(clog, 0, &rnode, &cnode,
994 CTLFLAG_PERMANENT, 994 CTLFLAG_PERMANENT,
995 CTLTYPE_INT, "drops", 995 CTLTYPE_INT, "drops",
996 SYSCTL_DESCR("Total packets dropped due to full input queue"), 996 SYSCTL_DESCR("Total packets dropped due to full input queue"),
997 sysctl_percpuq_drops_handler, 0, (void *)ipq, 0, 997 sysctl_percpuq_drops_handler, 0, (void *)ipq, 0,
998 CTL_CREATE, CTL_EOL) != 0) 998 CTL_CREATE, CTL_EOL) != 0)
999 goto bad; 999 goto bad;
1000 1000
1001 return; 1001 return;
1002bad: 1002bad:
1003 printf("%s: could not attach sysctl nodes\n", ifname); 1003 printf("%s: could not attach sysctl nodes\n", ifname);
1004 return; 1004 return;
1005} 1005}
1006 1006
1007/* 1007/*
1008 * The deferred if_start framework 1008 * The deferred if_start framework
1009 * 1009 *
1010 * The common APIs to defer if_start to softint when if_start is requested 1010 * The common APIs to defer if_start to softint when if_start is requested
1011 * from a device driver running in hardware interrupt context. 1011 * from a device driver running in hardware interrupt context.
1012 */ 1012 */
1013/* 1013/*
1014 * Call ifp->if_start (or equivalent) in a dedicated softint for 1014 * Call ifp->if_start (or equivalent) in a dedicated softint for
1015 * deferred if_start. 1015 * deferred if_start.
1016 */ 1016 */
1017static void 1017static void
1018if_deferred_start_softint(void *arg) 1018if_deferred_start_softint(void *arg)
1019{ 1019{
1020 struct if_deferred_start *ids = arg; 1020 struct if_deferred_start *ids = arg;
1021 struct ifnet *ifp = ids->ids_ifp; 1021 struct ifnet *ifp = ids->ids_ifp;
1022 1022
1023 ids->ids_if_start(ifp); 1023 ids->ids_if_start(ifp);
1024} 1024}
1025 1025
1026/* 1026/*
1027 * The default callback function for deferred if_start. 1027 * The default callback function for deferred if_start.
1028 */ 1028 */
1029static void 1029static void
1030if_deferred_start_common(struct ifnet *ifp) 1030if_deferred_start_common(struct ifnet *ifp)
1031{ 1031{
1032 int s; 1032 int s;
1033 1033
1034 s = splnet(); 1034 s = splnet();
1035 if_start_lock(ifp); 1035 if_start_lock(ifp);
1036 splx(s); 1036 splx(s);
1037} 1037}
1038 1038
1039static inline bool 1039static inline bool
1040if_snd_is_used(struct ifnet *ifp) 1040if_snd_is_used(struct ifnet *ifp)
1041{ 1041{
1042 1042
1043 return ALTQ_IS_ENABLED(&ifp->if_snd) || 1043 return ALTQ_IS_ENABLED(&ifp->if_snd) ||
1044 ifp->if_transmit == if_transmit || 1044 ifp->if_transmit == if_transmit ||
1045 ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit; 1045 ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit;
1046} 1046}
1047 1047
1048/* 1048/*
1049 * Schedule deferred if_start. 1049 * Schedule deferred if_start.
1050 */ 1050 */
1051void 1051void
1052if_schedule_deferred_start(struct ifnet *ifp) 1052if_schedule_deferred_start(struct ifnet *ifp)
1053{ 1053{
1054 1054
1055 KASSERT(ifp->if_deferred_start != NULL); 1055 KASSERT(ifp->if_deferred_start != NULL);
1056 1056
1057 if (if_snd_is_used(ifp) && IFQ_IS_EMPTY(&ifp->if_snd)) 1057 if (if_snd_is_used(ifp) && IFQ_IS_EMPTY(&ifp->if_snd))
1058 return; 1058 return;
1059 1059
1060 softint_schedule(ifp->if_deferred_start->ids_si); 1060 softint_schedule(ifp->if_deferred_start->ids_si);
1061} 1061}
1062 1062
1063/* 1063/*
1064 * Create an instance of deferred if_start. A driver should call the function 1064 * Create an instance of deferred if_start. A driver should call the function
1065 * only if the driver needs deferred if_start. Drivers can setup their own 1065 * only if the driver needs deferred if_start. Drivers can setup their own
1066 * deferred if_start function via 2nd argument. 1066 * deferred if_start function via 2nd argument.
1067 */ 1067 */
1068void 1068void
1069if_deferred_start_init(struct ifnet *ifp, void (*func)(struct ifnet *)) 1069if_deferred_start_init(struct ifnet *ifp, void (*func)(struct ifnet *))
1070{ 1070{
1071 struct if_deferred_start *ids; 1071 struct if_deferred_start *ids;
1072 u_int flags = SOFTINT_NET; 1072 u_int flags = SOFTINT_NET;
1073 1073
1074 flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0; 1074 flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0;
1075 1075
1076 ids = kmem_zalloc(sizeof(*ids), KM_SLEEP); 1076 ids = kmem_zalloc(sizeof(*ids), KM_SLEEP);
1077 ids->ids_ifp = ifp; 1077 ids->ids_ifp = ifp;
1078 ids->ids_si = softint_establish(flags, if_deferred_start_softint, ids); 1078 ids->ids_si = softint_establish(flags, if_deferred_start_softint, ids);
1079 if (func != NULL) 1079 if (func != NULL)
1080 ids->ids_if_start = func; 1080 ids->ids_if_start = func;
1081 else 1081 else
1082 ids->ids_if_start = if_deferred_start_common; 1082 ids->ids_if_start = if_deferred_start_common;
1083 1083
1084 ifp->if_deferred_start = ids; 1084 ifp->if_deferred_start = ids;
1085} 1085}
1086 1086
1087static void 1087static void
1088if_deferred_start_destroy(struct ifnet *ifp) 1088if_deferred_start_destroy(struct ifnet *ifp)
1089{ 1089{
1090 1090
1091 if (ifp->if_deferred_start == NULL) 1091 if (ifp->if_deferred_start == NULL)
1092 return; 1092 return;
@@ -1523,2003 +1523,2014 @@ restart: @@ -1523,2003 +1523,2014 @@ restart:
1523#endif 1523#endif
1524} 1524}
1525 1525
1526static void 1526static void
1527if_detach_queues(struct ifnet *ifp, struct ifqueue *q) 1527if_detach_queues(struct ifnet *ifp, struct ifqueue *q)
1528{ 1528{
1529 struct mbuf *m, *prev, *next; 1529 struct mbuf *m, *prev, *next;
1530 1530
1531 prev = NULL; 1531 prev = NULL;
1532 for (m = q->ifq_head; m != NULL; m = next) { 1532 for (m = q->ifq_head; m != NULL; m = next) {
1533 KASSERT((m->m_flags & M_PKTHDR) != 0); 1533 KASSERT((m->m_flags & M_PKTHDR) != 0);
1534 1534
1535 next = m->m_nextpkt; 1535 next = m->m_nextpkt;
1536 if (m->m_pkthdr.rcvif_index != ifp->if_index) { 1536 if (m->m_pkthdr.rcvif_index != ifp->if_index) {
1537 prev = m; 1537 prev = m;
1538 continue; 1538 continue;
1539 } 1539 }
1540 1540
1541 if (prev != NULL) 1541 if (prev != NULL)
1542 prev->m_nextpkt = m->m_nextpkt; 1542 prev->m_nextpkt = m->m_nextpkt;
1543 else 1543 else
1544 q->ifq_head = m->m_nextpkt; 1544 q->ifq_head = m->m_nextpkt;
1545 if (q->ifq_tail == m) 1545 if (q->ifq_tail == m)
1546 q->ifq_tail = prev; 1546 q->ifq_tail = prev;
1547 q->ifq_len--; 1547 q->ifq_len--;
1548 1548
1549 m->m_nextpkt = NULL; 1549 m->m_nextpkt = NULL;
1550 m_freem(m); 1550 m_freem(m);
1551 IF_DROP(q); 1551 IF_DROP(q);
1552 } 1552 }
1553} 1553}
1554 1554
1555/* 1555/*
1556 * Callback for a radix tree walk to delete all references to an 1556 * Callback for a radix tree walk to delete all references to an
1557 * ifnet. 1557 * ifnet.
1558 */ 1558 */
1559static int 1559static int
1560if_delroute_matcher(struct rtentry *rt, void *v) 1560if_delroute_matcher(struct rtentry *rt, void *v)
1561{ 1561{
1562 struct ifnet *ifp = (struct ifnet *)v; 1562 struct ifnet *ifp = (struct ifnet *)v;
1563 1563
1564 if (rt->rt_ifp == ifp) 1564 if (rt->rt_ifp == ifp)
1565 return 1; 1565 return 1;
1566 else 1566 else
1567 return 0; 1567 return 0;
1568} 1568}
1569 1569
1570/* 1570/*
1571 * Create a clone network interface. 1571 * Create a clone network interface.
1572 */ 1572 */
1573static int 1573static int
1574if_clone_create(const char *name) 1574if_clone_create(const char *name)
1575{ 1575{
1576 struct if_clone *ifc; 1576 struct if_clone *ifc;
1577 int unit; 1577 int unit;
1578 struct ifnet *ifp; 1578 struct ifnet *ifp;
1579 struct psref psref; 1579 struct psref psref;
1580 1580
1581 KASSERT(mutex_owned(&if_clone_mtx)); 1581 KASSERT(mutex_owned(&if_clone_mtx));
1582 1582
1583 ifc = if_clone_lookup(name, &unit); 1583 ifc = if_clone_lookup(name, &unit);
1584 if (ifc == NULL) 1584 if (ifc == NULL)
1585 return EINVAL; 1585 return EINVAL;
1586 1586
1587 ifp = if_get(name, &psref); 1587 ifp = if_get(name, &psref);
1588 if (ifp != NULL) { 1588 if (ifp != NULL) {
1589 if_put(ifp, &psref); 1589 if_put(ifp, &psref);
1590 return EEXIST; 1590 return EEXIST;
1591 } 1591 }
1592 1592
1593 return (*ifc->ifc_create)(ifc, unit); 1593 return (*ifc->ifc_create)(ifc, unit);
1594} 1594}
1595 1595
1596/* 1596/*
1597 * Destroy a clone network interface. 1597 * Destroy a clone network interface.
1598 */ 1598 */
1599static int 1599static int
1600if_clone_destroy(const char *name) 1600if_clone_destroy(const char *name)
1601{ 1601{
1602 struct if_clone *ifc; 1602 struct if_clone *ifc;
1603 struct ifnet *ifp; 1603 struct ifnet *ifp;
1604 struct psref psref; 1604 struct psref psref;
1605 int error; 1605 int error;
1606 int (*if_ioctl)(struct ifnet *, u_long, void *); 1606 int (*if_ioctl)(struct ifnet *, u_long, void *);
1607 1607
1608 KASSERT(mutex_owned(&if_clone_mtx)); 1608 KASSERT(mutex_owned(&if_clone_mtx));
1609 1609
1610 ifc = if_clone_lookup(name, NULL); 1610 ifc = if_clone_lookup(name, NULL);
1611 if (ifc == NULL) 1611 if (ifc == NULL)
1612 return EINVAL; 1612 return EINVAL;
1613 1613
1614 if (ifc->ifc_destroy == NULL) 1614 if (ifc->ifc_destroy == NULL)
1615 return EOPNOTSUPP; 1615 return EOPNOTSUPP;
1616 1616
1617 ifp = if_get(name, &psref); 1617 ifp = if_get(name, &psref);
1618 if (ifp == NULL) 1618 if (ifp == NULL)
1619 return ENXIO; 1619 return ENXIO;
1620 1620
1621 /* We have to disable ioctls here */ 1621 /* We have to disable ioctls here */
1622 IFNET_LOCK(ifp); 1622 IFNET_LOCK(ifp);
1623 if_ioctl = ifp->if_ioctl; 1623 if_ioctl = ifp->if_ioctl;
1624 ifp->if_ioctl = if_nullioctl; 1624 ifp->if_ioctl = if_nullioctl;
1625 IFNET_UNLOCK(ifp); 1625 IFNET_UNLOCK(ifp);
1626 1626
1627 /* 1627 /*
1628 * We cannot call ifc_destroy with holding ifp. 1628 * We cannot call ifc_destroy with holding ifp.
1629 * Releasing ifp here is safe thanks to if_clone_mtx. 1629 * Releasing ifp here is safe thanks to if_clone_mtx.
1630 */ 1630 */
1631 if_put(ifp, &psref); 1631 if_put(ifp, &psref);
1632 1632
1633 error = (*ifc->ifc_destroy)(ifp); 1633 error = (*ifc->ifc_destroy)(ifp);
1634 1634
1635 if (error != 0) { 1635 if (error != 0) {
1636 /* We have to restore if_ioctl on error */ 1636 /* We have to restore if_ioctl on error */
1637 IFNET_LOCK(ifp); 1637 IFNET_LOCK(ifp);
1638 ifp->if_ioctl = if_ioctl; 1638 ifp->if_ioctl = if_ioctl;
1639 IFNET_UNLOCK(ifp); 1639 IFNET_UNLOCK(ifp);
1640 } 1640 }
1641 1641
1642 return error; 1642 return error;
1643} 1643}
1644 1644
1645static bool 1645static bool
1646if_is_unit(const char *name) 1646if_is_unit(const char *name)
1647{ 1647{
1648 1648
1649 while (*name != '\0') { 1649 while (*name != '\0') {
1650 if (*name < '0' || *name > '9') 1650 if (*name < '0' || *name > '9')
1651 return false; 1651 return false;
1652 name++; 1652 name++;
1653 } 1653 }
1654 1654
1655 return true; 1655 return true;
1656} 1656}
1657 1657
1658/* 1658/*
1659 * Look up a network interface cloner. 1659 * Look up a network interface cloner.
1660 */ 1660 */
1661static struct if_clone * 1661static struct if_clone *
1662if_clone_lookup(const char *name, int *unitp) 1662if_clone_lookup(const char *name, int *unitp)
1663{ 1663{
1664 struct if_clone *ifc; 1664 struct if_clone *ifc;
1665 const char *cp; 1665 const char *cp;
1666 char *dp, ifname[IFNAMSIZ + 3]; 1666 char *dp, ifname[IFNAMSIZ + 3];
1667 int unit; 1667 int unit;
1668 1668
1669 KASSERT(mutex_owned(&if_clone_mtx)); 1669 KASSERT(mutex_owned(&if_clone_mtx));
1670 1670
1671 strcpy(ifname, "if_"); 1671 strcpy(ifname, "if_");
1672 /* separate interface name from unit */ 1672 /* separate interface name from unit */
1673 /* TODO: search unit number from backward */ 1673 /* TODO: search unit number from backward */
1674 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ && 1674 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ &&
1675 *cp && !if_is_unit(cp);) 1675 *cp && !if_is_unit(cp);)
1676 *dp++ = *cp++; 1676 *dp++ = *cp++;
1677 1677
1678 if (cp == name || cp - name == IFNAMSIZ || !*cp) 1678 if (cp == name || cp - name == IFNAMSIZ || !*cp)
1679 return NULL; /* No name or unit number */ 1679 return NULL; /* No name or unit number */
1680 *dp++ = '\0'; 1680 *dp++ = '\0';
1681 1681
1682again: 1682again:
1683 LIST_FOREACH(ifc, &if_cloners, ifc_list) { 1683 LIST_FOREACH(ifc, &if_cloners, ifc_list) {
1684 if (strcmp(ifname + 3, ifc->ifc_name) == 0) 1684 if (strcmp(ifname + 3, ifc->ifc_name) == 0)
1685 break; 1685 break;
1686 } 1686 }
1687 1687
1688 if (ifc == NULL) { 1688 if (ifc == NULL) {
1689 int error; 1689 int error;
1690 if (*ifname == '\0') 1690 if (*ifname == '\0')
1691 return NULL; 1691 return NULL;
1692 mutex_exit(&if_clone_mtx); 1692 mutex_exit(&if_clone_mtx);
1693 error = module_autoload(ifname, MODULE_CLASS_DRIVER); 1693 error = module_autoload(ifname, MODULE_CLASS_DRIVER);
1694 mutex_enter(&if_clone_mtx); 1694 mutex_enter(&if_clone_mtx);
1695 if (error) 1695 if (error)
1696 return NULL; 1696 return NULL;
1697 *ifname = '\0'; 1697 *ifname = '\0';
1698 goto again; 1698 goto again;
1699 } 1699 }
1700 1700
1701 unit = 0; 1701 unit = 0;
1702 while (cp - name < IFNAMSIZ && *cp) { 1702 while (cp - name < IFNAMSIZ && *cp) {
1703 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) { 1703 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) {
1704 /* Bogus unit number. */ 1704 /* Bogus unit number. */
1705 return NULL; 1705 return NULL;
1706 } 1706 }
1707 unit = (unit * 10) + (*cp++ - '0'); 1707 unit = (unit * 10) + (*cp++ - '0');
1708 } 1708 }
1709 1709
1710 if (unitp != NULL) 1710 if (unitp != NULL)
1711 *unitp = unit; 1711 *unitp = unit;
1712 return ifc; 1712 return ifc;
1713} 1713}
1714 1714
1715/* 1715/*
1716 * Register a network interface cloner. 1716 * Register a network interface cloner.
1717 */ 1717 */
1718void 1718void
1719if_clone_attach(struct if_clone *ifc) 1719if_clone_attach(struct if_clone *ifc)
1720{ 1720{
1721 1721
1722 mutex_enter(&if_clone_mtx); 1722 mutex_enter(&if_clone_mtx);
1723 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 1723 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
1724 if_cloners_count++; 1724 if_cloners_count++;
1725 mutex_exit(&if_clone_mtx); 1725 mutex_exit(&if_clone_mtx);
1726} 1726}
1727 1727
1728/* 1728/*
1729 * Unregister a network interface cloner. 1729 * Unregister a network interface cloner.
1730 */ 1730 */
1731void 1731void
1732if_clone_detach(struct if_clone *ifc) 1732if_clone_detach(struct if_clone *ifc)
1733{ 1733{
1734 1734
1735 mutex_enter(&if_clone_mtx); 1735 mutex_enter(&if_clone_mtx);
1736 LIST_REMOVE(ifc, ifc_list); 1736 LIST_REMOVE(ifc, ifc_list);
1737 if_cloners_count--; 1737 if_cloners_count--;
1738 mutex_exit(&if_clone_mtx); 1738 mutex_exit(&if_clone_mtx);
1739} 1739}
1740 1740
1741/* 1741/*
1742 * Provide list of interface cloners to userspace. 1742 * Provide list of interface cloners to userspace.
1743 */ 1743 */
1744int 1744int
1745if_clone_list(int buf_count, char *buffer, int *total) 1745if_clone_list(int buf_count, char *buffer, int *total)
1746{ 1746{
1747 char outbuf[IFNAMSIZ], *dst; 1747 char outbuf[IFNAMSIZ], *dst;
1748 struct if_clone *ifc; 1748 struct if_clone *ifc;
1749 int count, error = 0; 1749 int count, error = 0;
1750 1750
1751 mutex_enter(&if_clone_mtx); 1751 mutex_enter(&if_clone_mtx);
1752 *total = if_cloners_count; 1752 *total = if_cloners_count;
1753 if ((dst = buffer) == NULL) { 1753 if ((dst = buffer) == NULL) {
1754 /* Just asking how many there are. */ 1754 /* Just asking how many there are. */
1755 goto out; 1755 goto out;
1756 } 1756 }
1757 1757
1758 if (buf_count < 0) { 1758 if (buf_count < 0) {
1759 error = EINVAL; 1759 error = EINVAL;
1760 goto out; 1760 goto out;
1761 } 1761 }
1762 1762
1763 count = (if_cloners_count < buf_count) ? 1763 count = (if_cloners_count < buf_count) ?
1764 if_cloners_count : buf_count; 1764 if_cloners_count : buf_count;
1765 1765
1766 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0; 1766 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
1767 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) { 1767 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
1768 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf)); 1768 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf));
1769 if (outbuf[sizeof(outbuf) - 1] != '\0') { 1769 if (outbuf[sizeof(outbuf) - 1] != '\0') {
1770 error = ENAMETOOLONG; 1770 error = ENAMETOOLONG;
1771 goto out; 1771 goto out;
1772 } 1772 }
1773 error = copyout(outbuf, dst, sizeof(outbuf)); 1773 error = copyout(outbuf, dst, sizeof(outbuf));
1774 if (error != 0) 1774 if (error != 0)
1775 break; 1775 break;
1776 } 1776 }
1777 1777
1778out: 1778out:
1779 mutex_exit(&if_clone_mtx); 1779 mutex_exit(&if_clone_mtx);
1780 return error; 1780 return error;
1781} 1781}
1782 1782
1783void 1783void
1784ifa_psref_init(struct ifaddr *ifa) 1784ifa_psref_init(struct ifaddr *ifa)
1785{ 1785{
1786 1786
1787 psref_target_init(&ifa->ifa_psref, ifa_psref_class); 1787 psref_target_init(&ifa->ifa_psref, ifa_psref_class);
1788} 1788}
1789 1789
1790void 1790void
1791ifaref(struct ifaddr *ifa) 1791ifaref(struct ifaddr *ifa)
1792{ 1792{
1793 1793
1794 atomic_inc_uint(&ifa->ifa_refcnt); 1794 atomic_inc_uint(&ifa->ifa_refcnt);
1795} 1795}
1796 1796
1797void 1797void
1798ifafree(struct ifaddr *ifa) 1798ifafree(struct ifaddr *ifa)
1799{ 1799{
1800 KASSERT(ifa != NULL); 1800 KASSERT(ifa != NULL);
1801 KASSERTMSG(ifa->ifa_refcnt > 0, "ifa_refcnt=%d", ifa->ifa_refcnt); 1801 KASSERTMSG(ifa->ifa_refcnt > 0, "ifa_refcnt=%d", ifa->ifa_refcnt);
1802 1802
1803 if (atomic_dec_uint_nv(&ifa->ifa_refcnt) == 0) { 1803 if (atomic_dec_uint_nv(&ifa->ifa_refcnt) == 0) {
1804 free(ifa, M_IFADDR); 1804 free(ifa, M_IFADDR);
1805 } 1805 }
1806} 1806}
1807 1807
1808bool 1808bool
1809ifa_is_destroying(struct ifaddr *ifa) 1809ifa_is_destroying(struct ifaddr *ifa)
1810{ 1810{
1811 1811
1812 return ISSET(ifa->ifa_flags, IFA_DESTROYING); 1812 return ISSET(ifa->ifa_flags, IFA_DESTROYING);
1813} 1813}
1814 1814
1815void 1815void
1816ifa_insert(struct ifnet *ifp, struct ifaddr *ifa) 1816ifa_insert(struct ifnet *ifp, struct ifaddr *ifa)
1817{ 1817{
1818 1818
1819 ifa->ifa_ifp = ifp; 1819 ifa->ifa_ifp = ifp;
1820 1820
1821 /* 1821 /*
1822 * Check MP-safety for IFEF_MPSAFE drivers. 1822 * Check MP-safety for IFEF_MPSAFE drivers.
1823 * Check !IFF_RUNNING for initialization routines that normally don't 1823 * Check !IFF_RUNNING for initialization routines that normally don't
1824 * take IFNET_LOCK but it's safe because there is no competitor. 1824 * take IFNET_LOCK but it's safe because there is no competitor.
1825 * XXX there are false positive cases because IFF_RUNNING can be off on 1825 * XXX there are false positive cases because IFF_RUNNING can be off on
1826 * if_stop. 1826 * if_stop.
1827 */ 1827 */
1828 KASSERT(!if_is_mpsafe(ifp) || !ISSET(ifp->if_flags, IFF_RUNNING) || 1828 KASSERT(!if_is_mpsafe(ifp) || !ISSET(ifp->if_flags, IFF_RUNNING) ||
1829 IFNET_LOCKED(ifp)); 1829 IFNET_LOCKED(ifp));
1830 1830
1831 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list); 1831 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list);
1832 IFADDR_ENTRY_INIT(ifa); 1832 IFADDR_ENTRY_INIT(ifa);
1833 IFADDR_WRITER_INSERT_TAIL(ifp, ifa); 1833 IFADDR_WRITER_INSERT_TAIL(ifp, ifa);
1834 1834
1835 ifaref(ifa); 1835 ifaref(ifa);
1836} 1836}
1837 1837
1838void 1838void
1839ifa_remove(struct ifnet *ifp, struct ifaddr *ifa) 1839ifa_remove(struct ifnet *ifp, struct ifaddr *ifa)
1840{ 1840{
1841 1841
1842 KASSERT(ifa->ifa_ifp == ifp); 1842 KASSERT(ifa->ifa_ifp == ifp);
1843 /* 1843 /*
1844 * Check MP-safety for IFEF_MPSAFE drivers. 1844 * Check MP-safety for IFEF_MPSAFE drivers.
1845 * if_is_deactivated indicates ifa_remove is called form if_detach 1845 * if_is_deactivated indicates ifa_remove is called form if_detach
1846 * where is safe even if IFNET_LOCK isn't held. 1846 * where is safe even if IFNET_LOCK isn't held.
1847 */ 1847 */
1848 KASSERT(!if_is_mpsafe(ifp) || if_is_deactivated(ifp) || IFNET_LOCKED(ifp)); 1848 KASSERT(!if_is_mpsafe(ifp) || if_is_deactivated(ifp) || IFNET_LOCKED(ifp));
1849 1849
1850 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 1850 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list);
1851 IFADDR_WRITER_REMOVE(ifa); 1851 IFADDR_WRITER_REMOVE(ifa);
1852#ifdef NET_MPSAFE 1852#ifdef NET_MPSAFE
1853 IFNET_GLOBAL_LOCK(); 1853 IFNET_GLOBAL_LOCK();
1854 pserialize_perform(ifnet_psz); 1854 pserialize_perform(ifnet_psz);
1855 IFNET_GLOBAL_UNLOCK(); 1855 IFNET_GLOBAL_UNLOCK();
1856#endif 1856#endif
1857 1857
1858#ifdef NET_MPSAFE 1858#ifdef NET_MPSAFE
1859 psref_target_destroy(&ifa->ifa_psref, ifa_psref_class); 1859 psref_target_destroy(&ifa->ifa_psref, ifa_psref_class);
1860#endif 1860#endif
1861 IFADDR_ENTRY_DESTROY(ifa); 1861 IFADDR_ENTRY_DESTROY(ifa);
1862 ifafree(ifa); 1862 ifafree(ifa);
1863} 1863}
1864 1864
1865void 1865void
1866ifa_acquire(struct ifaddr *ifa, struct psref *psref) 1866ifa_acquire(struct ifaddr *ifa, struct psref *psref)
1867{ 1867{
1868 1868
1869 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref); 1869 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
1870 psref_acquire(psref, &ifa->ifa_psref, ifa_psref_class); 1870 psref_acquire(psref, &ifa->ifa_psref, ifa_psref_class);
1871} 1871}
1872 1872
1873void 1873void
1874ifa_release(struct ifaddr *ifa, struct psref *psref) 1874ifa_release(struct ifaddr *ifa, struct psref *psref)
1875{ 1875{
1876 1876
1877 if (ifa == NULL) 1877 if (ifa == NULL)
1878 return; 1878 return;
1879 1879
1880 psref_release(psref, &ifa->ifa_psref, ifa_psref_class); 1880 psref_release(psref, &ifa->ifa_psref, ifa_psref_class);
1881} 1881}
1882 1882
1883bool 1883bool
1884ifa_held(struct ifaddr *ifa) 1884ifa_held(struct ifaddr *ifa)
1885{ 1885{
1886 1886
1887 return psref_held(&ifa->ifa_psref, ifa_psref_class); 1887 return psref_held(&ifa->ifa_psref, ifa_psref_class);
1888} 1888}
1889 1889
1890static inline int 1890static inline int
1891equal(const struct sockaddr *sa1, const struct sockaddr *sa2) 1891equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
1892{ 1892{
1893 return sockaddr_cmp(sa1, sa2) == 0; 1893 return sockaddr_cmp(sa1, sa2) == 0;
1894} 1894}
1895 1895
1896/* 1896/*
1897 * Locate an interface based on a complete address. 1897 * Locate an interface based on a complete address.
1898 */ 1898 */
1899/*ARGSUSED*/ 1899/*ARGSUSED*/
1900struct ifaddr * 1900struct ifaddr *
1901ifa_ifwithaddr(const struct sockaddr *addr) 1901ifa_ifwithaddr(const struct sockaddr *addr)
1902{ 1902{
1903 struct ifnet *ifp; 1903 struct ifnet *ifp;
1904 struct ifaddr *ifa; 1904 struct ifaddr *ifa;
1905 1905
1906 IFNET_READER_FOREACH(ifp) { 1906 IFNET_READER_FOREACH(ifp) {
1907 if (if_is_deactivated(ifp)) 1907 if (if_is_deactivated(ifp))
1908 continue; 1908 continue;
1909 IFADDR_READER_FOREACH(ifa, ifp) { 1909 IFADDR_READER_FOREACH(ifa, ifp) {
1910 if (ifa->ifa_addr->sa_family != addr->sa_family) 1910 if (ifa->ifa_addr->sa_family != addr->sa_family)
1911 continue; 1911 continue;
1912 if (equal(addr, ifa->ifa_addr)) 1912 if (equal(addr, ifa->ifa_addr))
1913 return ifa; 1913 return ifa;
1914 if ((ifp->if_flags & IFF_BROADCAST) && 1914 if ((ifp->if_flags & IFF_BROADCAST) &&
1915 ifa->ifa_broadaddr && 1915 ifa->ifa_broadaddr &&
1916 /* IP6 doesn't have broadcast */ 1916 /* IP6 doesn't have broadcast */
1917 ifa->ifa_broadaddr->sa_len != 0 && 1917 ifa->ifa_broadaddr->sa_len != 0 &&
1918 equal(ifa->ifa_broadaddr, addr)) 1918 equal(ifa->ifa_broadaddr, addr))
1919 return ifa; 1919 return ifa;
1920 } 1920 }
1921 } 1921 }
1922 return NULL; 1922 return NULL;
1923} 1923}
1924 1924
1925struct ifaddr * 1925struct ifaddr *
1926ifa_ifwithaddr_psref(const struct sockaddr *addr, struct psref *psref) 1926ifa_ifwithaddr_psref(const struct sockaddr *addr, struct psref *psref)
1927{ 1927{
1928 struct ifaddr *ifa; 1928 struct ifaddr *ifa;
1929 int s = pserialize_read_enter(); 1929 int s = pserialize_read_enter();
1930 1930
1931 ifa = ifa_ifwithaddr(addr); 1931 ifa = ifa_ifwithaddr(addr);
1932 if (ifa != NULL) 1932 if (ifa != NULL)
1933 ifa_acquire(ifa, psref); 1933 ifa_acquire(ifa, psref);
1934 pserialize_read_exit(s); 1934 pserialize_read_exit(s);
1935 1935
1936 return ifa; 1936 return ifa;
1937} 1937}
1938 1938
1939/* 1939/*
1940 * Locate the point to point interface with a given destination address. 1940 * Locate the point to point interface with a given destination address.
1941 */ 1941 */
1942/*ARGSUSED*/ 1942/*ARGSUSED*/
1943struct ifaddr * 1943struct ifaddr *
1944ifa_ifwithdstaddr(const struct sockaddr *addr) 1944ifa_ifwithdstaddr(const struct sockaddr *addr)
1945{ 1945{
1946 struct ifnet *ifp; 1946 struct ifnet *ifp;
1947 struct ifaddr *ifa; 1947 struct ifaddr *ifa;
1948 1948
1949 IFNET_READER_FOREACH(ifp) { 1949 IFNET_READER_FOREACH(ifp) {
1950 if (if_is_deactivated(ifp)) 1950 if (if_is_deactivated(ifp))
1951 continue; 1951 continue;
1952 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1952 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1953 continue; 1953 continue;
1954 IFADDR_READER_FOREACH(ifa, ifp) { 1954 IFADDR_READER_FOREACH(ifa, ifp) {
1955 if (ifa->ifa_addr->sa_family != addr->sa_family || 1955 if (ifa->ifa_addr->sa_family != addr->sa_family ||
1956 ifa->ifa_dstaddr == NULL) 1956 ifa->ifa_dstaddr == NULL)
1957 continue; 1957 continue;
1958 if (equal(addr, ifa->ifa_dstaddr)) 1958 if (equal(addr, ifa->ifa_dstaddr))
1959 return ifa; 1959 return ifa;
1960 } 1960 }
1961 } 1961 }
1962 1962
1963 return NULL; 1963 return NULL;
1964} 1964}
1965 1965
1966struct ifaddr * 1966struct ifaddr *
1967ifa_ifwithdstaddr_psref(const struct sockaddr *addr, struct psref *psref) 1967ifa_ifwithdstaddr_psref(const struct sockaddr *addr, struct psref *psref)
1968{ 1968{
1969 struct ifaddr *ifa; 1969 struct ifaddr *ifa;
1970 int s; 1970 int s;
1971 1971
1972 s = pserialize_read_enter(); 1972 s = pserialize_read_enter();
1973 ifa = ifa_ifwithdstaddr(addr); 1973 ifa = ifa_ifwithdstaddr(addr);
1974 if (ifa != NULL) 1974 if (ifa != NULL)
1975 ifa_acquire(ifa, psref); 1975 ifa_acquire(ifa, psref);
1976 pserialize_read_exit(s); 1976 pserialize_read_exit(s);
1977 1977
1978 return ifa; 1978 return ifa;
1979} 1979}
1980 1980
1981/* 1981/*
1982 * Find an interface on a specific network. If many, choice 1982 * Find an interface on a specific network. If many, choice
1983 * is most specific found. 1983 * is most specific found.
1984 */ 1984 */
1985struct ifaddr * 1985struct ifaddr *
1986ifa_ifwithnet(const struct sockaddr *addr) 1986ifa_ifwithnet(const struct sockaddr *addr)
1987{ 1987{
1988 struct ifnet *ifp; 1988 struct ifnet *ifp;
1989 struct ifaddr *ifa, *ifa_maybe = NULL; 1989 struct ifaddr *ifa, *ifa_maybe = NULL;
1990 const struct sockaddr_dl *sdl; 1990 const struct sockaddr_dl *sdl;
1991 u_int af = addr->sa_family; 1991 u_int af = addr->sa_family;
1992 const char *addr_data = addr->sa_data, *cplim; 1992 const char *addr_data = addr->sa_data, *cplim;
1993 1993
1994 if (af == AF_LINK) { 1994 if (af == AF_LINK) {
1995 sdl = satocsdl(addr); 1995 sdl = satocsdl(addr);
1996 if (sdl->sdl_index && sdl->sdl_index < if_indexlim && 1996 if (sdl->sdl_index && sdl->sdl_index < if_indexlim &&
1997 ifindex2ifnet[sdl->sdl_index] && 1997 ifindex2ifnet[sdl->sdl_index] &&
1998 !if_is_deactivated(ifindex2ifnet[sdl->sdl_index])) { 1998 !if_is_deactivated(ifindex2ifnet[sdl->sdl_index])) {
1999 return ifindex2ifnet[sdl->sdl_index]->if_dl; 1999 return ifindex2ifnet[sdl->sdl_index]->if_dl;
2000 } 2000 }
2001 } 2001 }
2002#ifdef NETATALK 2002#ifdef NETATALK
2003 if (af == AF_APPLETALK) { 2003 if (af == AF_APPLETALK) {
2004 const struct sockaddr_at *sat, *sat2; 2004 const struct sockaddr_at *sat, *sat2;
2005 sat = (const struct sockaddr_at *)addr; 2005 sat = (const struct sockaddr_at *)addr;
2006 IFNET_READER_FOREACH(ifp) { 2006 IFNET_READER_FOREACH(ifp) {
2007 if (if_is_deactivated(ifp)) 2007 if (if_is_deactivated(ifp))
2008 continue; 2008 continue;
2009 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp); 2009 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp);
2010 if (ifa == NULL) 2010 if (ifa == NULL)
2011 continue; 2011 continue;
2012 sat2 = (struct sockaddr_at *)ifa->ifa_addr; 2012 sat2 = (struct sockaddr_at *)ifa->ifa_addr;
2013 if (sat2->sat_addr.s_net == sat->sat_addr.s_net) 2013 if (sat2->sat_addr.s_net == sat->sat_addr.s_net)
2014 return ifa; /* exact match */ 2014 return ifa; /* exact match */
2015 if (ifa_maybe == NULL) { 2015 if (ifa_maybe == NULL) {
2016 /* else keep the if with the right range */ 2016 /* else keep the if with the right range */
2017 ifa_maybe = ifa; 2017 ifa_maybe = ifa;
2018 } 2018 }
2019 } 2019 }
2020 return ifa_maybe; 2020 return ifa_maybe;
2021 } 2021 }
2022#endif 2022#endif
2023 IFNET_READER_FOREACH(ifp) { 2023 IFNET_READER_FOREACH(ifp) {
2024 if (if_is_deactivated(ifp)) 2024 if (if_is_deactivated(ifp))
2025 continue; 2025 continue;
2026 IFADDR_READER_FOREACH(ifa, ifp) { 2026 IFADDR_READER_FOREACH(ifa, ifp) {
2027 const char *cp, *cp2, *cp3; 2027 const char *cp, *cp2, *cp3;
2028 2028
2029 if (ifa->ifa_addr->sa_family != af || 2029 if (ifa->ifa_addr->sa_family != af ||
2030 ifa->ifa_netmask == NULL) 2030 ifa->ifa_netmask == NULL)
2031 next: continue; 2031 next: continue;
2032 cp = addr_data; 2032 cp = addr_data;
2033 cp2 = ifa->ifa_addr->sa_data; 2033 cp2 = ifa->ifa_addr->sa_data;
2034 cp3 = ifa->ifa_netmask->sa_data; 2034 cp3 = ifa->ifa_netmask->sa_data;
2035 cplim = (const char *)ifa->ifa_netmask + 2035 cplim = (const char *)ifa->ifa_netmask +
2036 ifa->ifa_netmask->sa_len; 2036 ifa->ifa_netmask->sa_len;
2037 while (cp3 < cplim) { 2037 while (cp3 < cplim) {
2038 if ((*cp++ ^ *cp2++) & *cp3++) { 2038 if ((*cp++ ^ *cp2++) & *cp3++) {
2039 /* want to continue for() loop */ 2039 /* want to continue for() loop */
2040 goto next; 2040 goto next;
2041 } 2041 }
2042 } 2042 }
2043 if (ifa_maybe == NULL || 2043 if (ifa_maybe == NULL ||
2044 rt_refines(ifa->ifa_netmask, 2044 rt_refines(ifa->ifa_netmask,
2045 ifa_maybe->ifa_netmask)) 2045 ifa_maybe->ifa_netmask))
2046 ifa_maybe = ifa; 2046 ifa_maybe = ifa;
2047 } 2047 }
2048 } 2048 }
2049 return ifa_maybe; 2049 return ifa_maybe;
2050} 2050}
2051 2051
2052struct ifaddr * 2052struct ifaddr *
2053ifa_ifwithnet_psref(const struct sockaddr *addr, struct psref *psref) 2053ifa_ifwithnet_psref(const struct sockaddr *addr, struct psref *psref)
2054{ 2054{
2055 struct ifaddr *ifa; 2055 struct ifaddr *ifa;
2056 int s; 2056 int s;
2057 2057
2058 s = pserialize_read_enter(); 2058 s = pserialize_read_enter();
2059 ifa = ifa_ifwithnet(addr); 2059 ifa = ifa_ifwithnet(addr);
2060 if (ifa != NULL) 2060 if (ifa != NULL)
2061 ifa_acquire(ifa, psref); 2061 ifa_acquire(ifa, psref);
2062 pserialize_read_exit(s); 2062 pserialize_read_exit(s);
2063 2063
2064 return ifa; 2064 return ifa;
2065} 2065}
2066 2066
2067/* 2067/*
2068 * Find the interface of the addresss. 2068 * Find the interface of the addresss.
2069 */ 2069 */
2070struct ifaddr * 2070struct ifaddr *
2071ifa_ifwithladdr(const struct sockaddr *addr) 2071ifa_ifwithladdr(const struct sockaddr *addr)
2072{ 2072{
2073 struct ifaddr *ia; 2073 struct ifaddr *ia;
2074 2074
2075 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) || 2075 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) ||
2076 (ia = ifa_ifwithnet(addr))) 2076 (ia = ifa_ifwithnet(addr)))
2077 return ia; 2077 return ia;
2078 return NULL; 2078 return NULL;
2079} 2079}
2080 2080
2081struct ifaddr * 2081struct ifaddr *
2082ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref) 2082ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref)
2083{ 2083{
2084 struct ifaddr *ifa; 2084 struct ifaddr *ifa;
2085 int s; 2085 int s;
2086 2086
2087 s = pserialize_read_enter(); 2087 s = pserialize_read_enter();
2088 ifa = ifa_ifwithladdr(addr); 2088 ifa = ifa_ifwithladdr(addr);
2089 if (ifa != NULL) 2089 if (ifa != NULL)
2090 ifa_acquire(ifa, psref); 2090 ifa_acquire(ifa, psref);
2091 pserialize_read_exit(s); 2091 pserialize_read_exit(s);
2092 2092
2093 return ifa; 2093 return ifa;
2094} 2094}
2095 2095
2096/* 2096/*
2097 * Find an interface using a specific address family 2097 * Find an interface using a specific address family
2098 */ 2098 */
2099struct ifaddr * 2099struct ifaddr *
2100ifa_ifwithaf(int af) 2100ifa_ifwithaf(int af)
2101{ 2101{
2102 struct ifnet *ifp; 2102 struct ifnet *ifp;
2103 struct ifaddr *ifa = NULL; 2103 struct ifaddr *ifa = NULL;
2104 int s; 2104 int s;
2105 2105
2106 s = pserialize_read_enter(); 2106 s = pserialize_read_enter();
2107 IFNET_READER_FOREACH(ifp) { 2107 IFNET_READER_FOREACH(ifp) {
2108 if (if_is_deactivated(ifp)) 2108 if (if_is_deactivated(ifp))
2109 continue; 2109 continue;
2110 IFADDR_READER_FOREACH(ifa, ifp) { 2110 IFADDR_READER_FOREACH(ifa, ifp) {
2111 if (ifa->ifa_addr->sa_family == af) 2111 if (ifa->ifa_addr->sa_family == af)
2112 goto out; 2112 goto out;
2113 } 2113 }
2114 } 2114 }
2115out: 2115out:
2116 pserialize_read_exit(s); 2116 pserialize_read_exit(s);
2117 return ifa; 2117 return ifa;
2118} 2118}
2119 2119
2120/* 2120/*
2121 * Find an interface address specific to an interface best matching 2121 * Find an interface address specific to an interface best matching
2122 * a given address. 2122 * a given address.
2123 */ 2123 */
2124struct ifaddr * 2124struct ifaddr *
2125ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp) 2125ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp)
2126{ 2126{
2127 struct ifaddr *ifa; 2127 struct ifaddr *ifa;
2128 const char *cp, *cp2, *cp3; 2128 const char *cp, *cp2, *cp3;
2129 const char *cplim; 2129 const char *cplim;
2130 struct ifaddr *ifa_maybe = 0; 2130 struct ifaddr *ifa_maybe = 0;
2131 u_int af = addr->sa_family; 2131 u_int af = addr->sa_family;
2132 2132
2133 if (if_is_deactivated(ifp)) 2133 if (if_is_deactivated(ifp))
2134 return NULL; 2134 return NULL;
2135 2135
2136 if (af >= AF_MAX) 2136 if (af >= AF_MAX)
2137 return NULL; 2137 return NULL;
2138 2138
2139 IFADDR_READER_FOREACH(ifa, ifp) { 2139 IFADDR_READER_FOREACH(ifa, ifp) {
2140 if (ifa->ifa_addr->sa_family != af) 2140 if (ifa->ifa_addr->sa_family != af)
2141 continue; 2141 continue;
2142 ifa_maybe = ifa; 2142 ifa_maybe = ifa;
2143 if (ifa->ifa_netmask == NULL) { 2143 if (ifa->ifa_netmask == NULL) {
2144 if (equal(addr, ifa->ifa_addr) || 2144 if (equal(addr, ifa->ifa_addr) ||
2145 (ifa->ifa_dstaddr && 2145 (ifa->ifa_dstaddr &&
2146 equal(addr, ifa->ifa_dstaddr))) 2146 equal(addr, ifa->ifa_dstaddr)))
2147 return ifa; 2147 return ifa;
2148 continue; 2148 continue;
2149 } 2149 }
2150 cp = addr->sa_data; 2150 cp = addr->sa_data;
2151 cp2 = ifa->ifa_addr->sa_data; 2151 cp2 = ifa->ifa_addr->sa_data;
2152 cp3 = ifa->ifa_netmask->sa_data; 2152 cp3 = ifa->ifa_netmask->sa_data;
2153 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 2153 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
2154 for (; cp3 < cplim; cp3++) { 2154 for (; cp3 < cplim; cp3++) {
2155 if ((*cp++ ^ *cp2++) & *cp3) 2155 if ((*cp++ ^ *cp2++) & *cp3)
2156 break; 2156 break;
2157 } 2157 }
2158 if (cp3 == cplim) 2158 if (cp3 == cplim)
2159 return ifa; 2159 return ifa;
2160 } 2160 }
2161 return ifa_maybe; 2161 return ifa_maybe;
2162} 2162}
2163 2163
2164struct ifaddr * 2164struct ifaddr *
2165ifaof_ifpforaddr_psref(const struct sockaddr *addr, struct ifnet *ifp, 2165ifaof_ifpforaddr_psref(const struct sockaddr *addr, struct ifnet *ifp,
2166 struct psref *psref) 2166 struct psref *psref)
2167{ 2167{
2168 struct ifaddr *ifa; 2168 struct ifaddr *ifa;
2169 int s; 2169 int s;
2170 2170
2171 s = pserialize_read_enter(); 2171 s = pserialize_read_enter();
2172 ifa = ifaof_ifpforaddr(addr, ifp); 2172 ifa = ifaof_ifpforaddr(addr, ifp);
2173 if (ifa != NULL) 2173 if (ifa != NULL)
2174 ifa_acquire(ifa, psref); 2174 ifa_acquire(ifa, psref);
2175 pserialize_read_exit(s); 2175 pserialize_read_exit(s);
2176 2176
2177 return ifa; 2177 return ifa;
2178} 2178}
2179 2179
2180/* 2180/*
2181 * Default action when installing a route with a Link Level gateway. 2181 * Default action when installing a route with a Link Level gateway.
2182 * Lookup an appropriate real ifa to point to. 2182 * Lookup an appropriate real ifa to point to.
2183 * This should be moved to /sys/net/link.c eventually. 2183 * This should be moved to /sys/net/link.c eventually.
2184 */ 2184 */
2185void 2185void
2186link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info) 2186link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info)
2187{ 2187{
2188 struct ifaddr *ifa; 2188 struct ifaddr *ifa;
2189 const struct sockaddr *dst; 2189 const struct sockaddr *dst;
2190 struct ifnet *ifp; 2190 struct ifnet *ifp;
2191 struct psref psref; 2191 struct psref psref;
2192 2192
2193 if (cmd != RTM_ADD || ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) 2193 if (cmd != RTM_ADD || ISSET(info->rti_flags, RTF_DONTCHANGEIFA))
2194 return; 2194 return;
2195 ifp = rt->rt_ifa->ifa_ifp; 2195 ifp = rt->rt_ifa->ifa_ifp;
2196 dst = rt_getkey(rt); 2196 dst = rt_getkey(rt);
2197 if ((ifa = ifaof_ifpforaddr_psref(dst, ifp, &psref)) != NULL) { 2197 if ((ifa = ifaof_ifpforaddr_psref(dst, ifp, &psref)) != NULL) {
2198 rt_replace_ifa(rt, ifa); 2198 rt_replace_ifa(rt, ifa);
2199 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 2199 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
2200 ifa->ifa_rtrequest(cmd, rt, info); 2200 ifa->ifa_rtrequest(cmd, rt, info);
2201 ifa_release(ifa, &psref); 2201 ifa_release(ifa, &psref);
2202 } 2202 }
2203} 2203}
2204 2204
2205/* 2205/*
2206 * bitmask macros to manage a densely packed link_state change queue. 2206 * bitmask macros to manage a densely packed link_state change queue.
2207 * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and 2207 * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and
2208 * LINK_STATE_UP(2) we need 2 bits for each state change. 2208 * LINK_STATE_UP(2) we need 2 bits for each state change.
2209 * As a state change to store is 0, treat all bits set as an unset item. 2209 * As a state change to store is 0, treat all bits set as an unset item.
2210 */ 2210 */
2211#define LQ_ITEM_BITS 2 2211#define LQ_ITEM_BITS 2
2212#define LQ_ITEM_MASK ((1 << LQ_ITEM_BITS) - 1) 2212#define LQ_ITEM_MASK ((1 << LQ_ITEM_BITS) - 1)
2213#define LQ_MASK(i) (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS) 2213#define LQ_MASK(i) (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS)
2214#define LINK_STATE_UNSET LQ_ITEM_MASK 2214#define LINK_STATE_UNSET LQ_ITEM_MASK
2215#define LQ_ITEM(q, i) (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS) 2215#define LQ_ITEM(q, i) (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS)
2216#define LQ_STORE(q, i, v) \ 2216#define LQ_STORE(q, i, v) \
2217 do { \ 2217 do { \
2218 (q) &= ~LQ_MASK((i)); \ 2218 (q) &= ~LQ_MASK((i)); \
2219 (q) |= (v) << (i) * LQ_ITEM_BITS; \ 2219 (q) |= (v) << (i) * LQ_ITEM_BITS; \
2220 } while (0 /* CONSTCOND */) 2220 } while (0 /* CONSTCOND */)
2221#define LQ_MAX(q) ((sizeof((q)) * NBBY) / LQ_ITEM_BITS) 2221#define LQ_MAX(q) ((sizeof((q)) * NBBY) / LQ_ITEM_BITS)
2222#define LQ_POP(q, v) \ 2222#define LQ_POP(q, v) \
2223 do { \ 2223 do { \
2224 (v) = LQ_ITEM((q), 0); \ 2224 (v) = LQ_ITEM((q), 0); \
2225 (q) >>= LQ_ITEM_BITS; \ 2225 (q) >>= LQ_ITEM_BITS; \
2226 (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \ 2226 (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \
2227 } while (0 /* CONSTCOND */) 2227 } while (0 /* CONSTCOND */)
2228#define LQ_PUSH(q, v) \ 2228#define LQ_PUSH(q, v) \
2229 do { \ 2229 do { \
2230 (q) >>= LQ_ITEM_BITS; \ 2230 (q) >>= LQ_ITEM_BITS; \
2231 (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \ 2231 (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \
2232 } while (0 /* CONSTCOND */) 2232 } while (0 /* CONSTCOND */)
2233#define LQ_FIND_UNSET(q, i) \ 2233#define LQ_FIND_UNSET(q, i) \
2234 for ((i) = 0; i < LQ_MAX((q)); (i)++) { \ 2234 for ((i) = 0; i < LQ_MAX((q)); (i)++) { \
2235 if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET) \ 2235 if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET) \
2236 break; \ 2236 break; \
2237 } 2237 }
2238 2238
2239/* 2239/*
2240 * XXX reusing (ifp)->if_snd->ifq_lock rather than having another spin mutex 2240 * XXX reusing (ifp)->if_snd->ifq_lock rather than having another spin mutex
2241 * for each ifnet. It doesn't matter because: 2241 * for each ifnet. It doesn't matter because:
2242 * - if IFEF_MPSAFE is enabled, if_snd isn't used and lock contentions on 2242 * - if IFEF_MPSAFE is enabled, if_snd isn't used and lock contentions on
2243 * ifq_lock don't happen 2243 * ifq_lock don't happen
2244 * - if IFEF_MPSAFE is disabled, there is no lock contention on ifq_lock 2244 * - if IFEF_MPSAFE is disabled, there is no lock contention on ifq_lock
2245 * because if_snd, if_link_state_change and if_link_state_change_process 2245 * because if_snd, if_link_state_change and if_link_state_change_process
2246 * are all called with KERNEL_LOCK 2246 * are all called with KERNEL_LOCK
2247 */ 2247 */
2248#define IF_LINK_STATE_CHANGE_LOCK(ifp) \ 2248#define IF_LINK_STATE_CHANGE_LOCK(ifp) \
2249 mutex_enter((ifp)->if_snd.ifq_lock) 2249 mutex_enter((ifp)->if_snd.ifq_lock)
2250#define IF_LINK_STATE_CHANGE_UNLOCK(ifp) \ 2250#define IF_LINK_STATE_CHANGE_UNLOCK(ifp) \
2251 mutex_exit((ifp)->if_snd.ifq_lock) 2251 mutex_exit((ifp)->if_snd.ifq_lock)
2252 2252
2253static void 2253static void
2254if_link_state_change_work_schedule(struct ifnet *ifp) 2254if_link_state_change_work_schedule(struct ifnet *ifp)
2255{ 2255{
2256 if (ifp->if_link_cansched && !ifp->if_link_scheduled) { 2256 if (ifp->if_link_cansched && !ifp->if_link_scheduled) {
2257 ifp->if_link_scheduled = true; 2257 ifp->if_link_scheduled = true;
2258 workqueue_enqueue(ifnet_link_state_wq, &ifp->if_link_work, 2258 workqueue_enqueue(ifnet_link_state_wq, &ifp->if_link_work,
2259 NULL); 2259 NULL);
2260 } 2260 }
2261} 2261}
2262 2262
2263/* 2263/*
2264 * Handle a change in the interface link state and 2264 * Handle a change in the interface link state and
2265 * queue notifications. 2265 * queue notifications.
2266 */ 2266 */
2267void 2267void
2268if_link_state_change(struct ifnet *ifp, int link_state) 2268if_link_state_change(struct ifnet *ifp, int link_state)
2269{ 2269{
2270 int idx; 2270 int idx;
2271 2271
2272 KASSERTMSG(if_is_link_state_changeable(ifp), 2272 KASSERTMSG(if_is_link_state_changeable(ifp),
2273 "%s: IFEF_NO_LINK_STATE_CHANGE must not be set, but if_extflags=0x%x", 2273 "%s: IFEF_NO_LINK_STATE_CHANGE must not be set, but if_extflags=0x%x",
2274 ifp->if_xname, ifp->if_extflags); 2274 ifp->if_xname, ifp->if_extflags);
2275 2275
2276 /* Ensure change is to a valid state */ 2276 /* Ensure change is to a valid state */
2277 switch (link_state) { 2277 switch (link_state) {
2278 case LINK_STATE_UNKNOWN: /* FALLTHROUGH */ 2278 case LINK_STATE_UNKNOWN: /* FALLTHROUGH */
2279 case LINK_STATE_DOWN: /* FALLTHROUGH */ 2279 case LINK_STATE_DOWN: /* FALLTHROUGH */
2280 case LINK_STATE_UP: 2280 case LINK_STATE_UP:
2281 break; 2281 break;
2282 default: 2282 default:
2283#ifdef DEBUG 2283#ifdef DEBUG
2284 printf("%s: invalid link state %d\n", 2284 printf("%s: invalid link state %d\n",
2285 ifp->if_xname, link_state); 2285 ifp->if_xname, link_state);
2286#endif 2286#endif
2287 return; 2287 return;
2288 } 2288 }
2289 2289
2290 IF_LINK_STATE_CHANGE_LOCK(ifp); 2290 IF_LINK_STATE_CHANGE_LOCK(ifp);
2291 2291
2292 /* Find the last unset event in the queue. */ 2292 /* Find the last unset event in the queue. */
2293 LQ_FIND_UNSET(ifp->if_link_queue, idx); 2293 LQ_FIND_UNSET(ifp->if_link_queue, idx);
2294 2294
2295 /* 2295 /*
2296 * Ensure link_state doesn't match the last event in the queue. 2296 * Ensure link_state doesn't match the last event in the queue.
2297 * ifp->if_link_state is not checked and set here because 2297 * ifp->if_link_state is not checked and set here because
2298 * that would present an inconsistent picture to the system. 2298 * that would present an inconsistent picture to the system.
2299 */ 2299 */
2300 if (idx != 0 && 2300 if (idx != 0 &&
2301 LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state) 2301 LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state)
2302 goto out; 2302 goto out;
2303 2303
2304 /* Handle queue overflow. */ 2304 /* Handle queue overflow. */
2305 if (idx == LQ_MAX(ifp->if_link_queue)) { 2305 if (idx == LQ_MAX(ifp->if_link_queue)) {
2306 uint8_t lost; 2306 uint8_t lost;
2307 2307
2308 /* 2308 /*
2309 * The DOWN state must be protected from being pushed off 2309 * The DOWN state must be protected from being pushed off
2310 * the queue to ensure that userland will always be 2310 * the queue to ensure that userland will always be
2311 * in a sane state. 2311 * in a sane state.
2312 * Because DOWN is protected, there is no need to protect 2312 * Because DOWN is protected, there is no need to protect
2313 * UNKNOWN. 2313 * UNKNOWN.
2314 * It should be invalid to change from any other state to 2314 * It should be invalid to change from any other state to
2315 * UNKNOWN anyway ... 2315 * UNKNOWN anyway ...
2316 */ 2316 */
2317 lost = LQ_ITEM(ifp->if_link_queue, 0); 2317 lost = LQ_ITEM(ifp->if_link_queue, 0);
2318 LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state); 2318 LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state);
2319 if (lost == LINK_STATE_DOWN) { 2319 if (lost == LINK_STATE_DOWN) {
2320 lost = LQ_ITEM(ifp->if_link_queue, 0); 2320 lost = LQ_ITEM(ifp->if_link_queue, 0);
2321 LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN); 2321 LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN);
2322 } 2322 }
2323 printf("%s: lost link state change %s\n", 2323 printf("%s: lost link state change %s\n",
2324 ifp->if_xname, 2324 ifp->if_xname,
2325 lost == LINK_STATE_UP ? "UP" : 2325 lost == LINK_STATE_UP ? "UP" :
2326 lost == LINK_STATE_DOWN ? "DOWN" : 2326 lost == LINK_STATE_DOWN ? "DOWN" :
2327 "UNKNOWN"); 2327 "UNKNOWN");
2328 } else 2328 } else
2329 LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state); 2329 LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state);
2330 2330
2331 if_link_state_change_work_schedule(ifp); 2331 if_link_state_change_work_schedule(ifp);
2332 2332
2333out: 2333out:
2334 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2334 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2335} 2335}
2336 2336
2337/* 2337/*
2338 * Handle interface link state change notifications. 2338 * Handle interface link state change notifications.
2339 */ 2339 */
2340static void 2340static void
2341if_link_state_change_process(struct ifnet *ifp, int link_state) 2341if_link_state_change_process(struct ifnet *ifp, int link_state)
2342{ 2342{
2343 struct domain *dp; 2343 struct domain *dp;
2344 int s = splnet(); 2344 int s = splnet();
2345 bool notify; 2345 bool notify;
2346 2346
2347 KASSERT(!cpu_intr_p()); 2347 KASSERT(!cpu_intr_p());
2348 2348
2349 IF_LINK_STATE_CHANGE_LOCK(ifp); 2349 IF_LINK_STATE_CHANGE_LOCK(ifp);
2350 2350
2351 /* Ensure the change is still valid. */ 2351 /* Ensure the change is still valid. */
2352 if (ifp->if_link_state == link_state) { 2352 if (ifp->if_link_state == link_state) {
2353 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2353 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2354 splx(s); 2354 splx(s);
2355 return; 2355 return;
2356 } 2356 }
2357 2357
2358#ifdef DEBUG 2358#ifdef DEBUG
2359 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname, 2359 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname,
2360 link_state == LINK_STATE_UP ? "UP" : 2360 link_state == LINK_STATE_UP ? "UP" :
2361 link_state == LINK_STATE_DOWN ? "DOWN" : 2361 link_state == LINK_STATE_DOWN ? "DOWN" :
2362 "UNKNOWN", 2362 "UNKNOWN",
2363 ifp->if_link_state == LINK_STATE_UP ? "UP" : 2363 ifp->if_link_state == LINK_STATE_UP ? "UP" :
2364 ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" : 2364 ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" :
2365 "UNKNOWN"); 2365 "UNKNOWN");
2366#endif 2366#endif
2367 2367
2368 /* 2368 /*
2369 * When going from UNKNOWN to UP, we need to mark existing 2369 * When going from UNKNOWN to UP, we need to mark existing
2370 * addresses as tentative and restart DAD as we may have 2370 * addresses as tentative and restart DAD as we may have
2371 * erroneously not found a duplicate. 2371 * erroneously not found a duplicate.
2372 * 2372 *
2373 * This needs to happen before rt_ifmsg to avoid a race where 2373 * This needs to happen before rt_ifmsg to avoid a race where
2374 * listeners would have an address and expect it to work right 2374 * listeners would have an address and expect it to work right
2375 * away. 2375 * away.
2376 */ 2376 */
2377 notify = (link_state == LINK_STATE_UP && 2377 notify = (link_state == LINK_STATE_UP &&
2378 ifp->if_link_state == LINK_STATE_UNKNOWN); 2378 ifp->if_link_state == LINK_STATE_UNKNOWN);
2379 ifp->if_link_state = link_state; 2379 ifp->if_link_state = link_state;
2380 /* The following routines may sleep so release the spin mutex */ 2380 /* The following routines may sleep so release the spin mutex */
2381 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2381 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2382 2382
2383 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 2383 KERNEL_LOCK_UNLESS_NET_MPSAFE();
2384 if (notify) { 2384 if (notify) {
2385 DOMAIN_FOREACH(dp) { 2385 DOMAIN_FOREACH(dp) {
2386 if (dp->dom_if_link_state_change != NULL) 2386 if (dp->dom_if_link_state_change != NULL)
2387 dp->dom_if_link_state_change(ifp, 2387 dp->dom_if_link_state_change(ifp,
2388 LINK_STATE_DOWN); 2388 LINK_STATE_DOWN);
2389 } 2389 }
2390 } 2390 }
2391 2391
2392 /* Notify that the link state has changed. */ 2392 /* Notify that the link state has changed. */
2393 rt_ifmsg(ifp); 2393 rt_ifmsg(ifp);
2394 2394
2395#if NCARP > 0 2395#if NCARP > 0
2396 if (ifp->if_carp) 2396 if (ifp->if_carp)
2397 carp_carpdev_state(ifp); 2397 carp_carpdev_state(ifp);
2398#endif 2398#endif
2399 2399
2400 DOMAIN_FOREACH(dp) { 2400 DOMAIN_FOREACH(dp) {
2401 if (dp->dom_if_link_state_change != NULL) 2401 if (dp->dom_if_link_state_change != NULL)
2402 dp->dom_if_link_state_change(ifp, link_state); 2402 dp->dom_if_link_state_change(ifp, link_state);
2403 } 2403 }
2404 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 2404 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2405 splx(s); 2405 splx(s);
2406} 2406}
2407 2407
2408/* 2408/*
2409 * Process the interface link state change queue. 2409 * Process the interface link state change queue.
2410 */ 2410 */
2411static void 2411static void
2412if_link_state_change_work(struct work *work, void *arg) 2412if_link_state_change_work(struct work *work, void *arg)
2413{ 2413{
2414 struct ifnet *ifp = container_of(work, struct ifnet, if_link_work); 2414 struct ifnet *ifp = container_of(work, struct ifnet, if_link_work);
2415 int s; 2415 int s;
2416 uint8_t state; 2416 uint8_t state;
2417 bool schedule; 2417 bool schedule;
2418 2418
2419 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 2419 KERNEL_LOCK_UNLESS_NET_MPSAFE();
2420 s = splnet(); 2420 s = splnet();
2421 2421
2422 /* Pop a link state change from the queue and process it. */ 2422 /* Pop a link state change from the queue and process it. */
2423 IF_LINK_STATE_CHANGE_LOCK(ifp); 2423 IF_LINK_STATE_CHANGE_LOCK(ifp);
2424 ifp->if_link_scheduled = false; 2424 ifp->if_link_scheduled = false;
2425 LQ_POP(ifp->if_link_queue, state); 2425 LQ_POP(ifp->if_link_queue, state);
2426 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2426 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2427 2427
2428 if_link_state_change_process(ifp, state); 2428 if_link_state_change_process(ifp, state);
2429 2429
2430 /* If there is a link state change to come, schedule it. */ 2430 /* If there is a link state change to come, schedule it. */
2431 IF_LINK_STATE_CHANGE_LOCK(ifp); 2431 IF_LINK_STATE_CHANGE_LOCK(ifp);
2432 schedule = (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET); 2432 schedule = (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET);
2433 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2433 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2434 2434
2435 if (schedule) 2435 if (schedule)
2436 if_link_state_change_work_schedule(ifp); 2436 if_link_state_change_work_schedule(ifp);
2437 2437
2438 splx(s); 2438 splx(s);
2439 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 2439 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2440} 2440}
2441 2441
2442/* 2442/*
2443 * Default action when installing a local route on a point-to-point 2443 * Default action when installing a local route on a point-to-point
2444 * interface. 2444 * interface.
2445 */ 2445 */
2446void 2446void
2447p2p_rtrequest(int req, struct rtentry *rt, 2447p2p_rtrequest(int req, struct rtentry *rt,
2448 __unused const struct rt_addrinfo *info) 2448 __unused const struct rt_addrinfo *info)
2449{ 2449{
2450 struct ifnet *ifp = rt->rt_ifp; 2450 struct ifnet *ifp = rt->rt_ifp;
2451 struct ifaddr *ifa, *lo0ifa; 2451 struct ifaddr *ifa, *lo0ifa;
2452 int s = pserialize_read_enter(); 2452 int s = pserialize_read_enter();
2453 2453
2454 switch (req) { 2454 switch (req) {
2455 case RTM_ADD: 2455 case RTM_ADD:
2456 if ((rt->rt_flags & RTF_LOCAL) == 0) 2456 if ((rt->rt_flags & RTF_LOCAL) == 0)
2457 break; 2457 break;
2458 2458
2459 rt->rt_ifp = lo0ifp; 2459 rt->rt_ifp = lo0ifp;
2460 2460
2461 if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) 2461 if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA))
2462 break; 2462 break;
2463 2463
2464 IFADDR_READER_FOREACH(ifa, ifp) { 2464 IFADDR_READER_FOREACH(ifa, ifp) {
2465 if (equal(rt_getkey(rt), ifa->ifa_addr)) 2465 if (equal(rt_getkey(rt), ifa->ifa_addr))
2466 break; 2466 break;
2467 } 2467 }
2468 if (ifa == NULL) 2468 if (ifa == NULL)
2469 break; 2469 break;
2470 2470
2471 /* 2471 /*
2472 * Ensure lo0 has an address of the same family. 2472 * Ensure lo0 has an address of the same family.
2473 */ 2473 */
2474 IFADDR_READER_FOREACH(lo0ifa, lo0ifp) { 2474 IFADDR_READER_FOREACH(lo0ifa, lo0ifp) {
2475 if (lo0ifa->ifa_addr->sa_family == 2475 if (lo0ifa->ifa_addr->sa_family ==
2476 ifa->ifa_addr->sa_family) 2476 ifa->ifa_addr->sa_family)
2477 break; 2477 break;
2478 } 2478 }
2479 if (lo0ifa == NULL) 2479 if (lo0ifa == NULL)
2480 break; 2480 break;
2481 2481
2482 /* 2482 /*
2483 * Make sure to set rt->rt_ifa to the interface 2483 * Make sure to set rt->rt_ifa to the interface
2484 * address we are using, otherwise we will have trouble 2484 * address we are using, otherwise we will have trouble
2485 * with source address selection. 2485 * with source address selection.
2486 */ 2486 */
2487 if (ifa != rt->rt_ifa) 2487 if (ifa != rt->rt_ifa)
2488 rt_replace_ifa(rt, ifa); 2488 rt_replace_ifa(rt, ifa);
2489 break; 2489 break;
2490 case RTM_DELETE: 2490 case RTM_DELETE:
2491 default: 2491 default:
2492 break; 2492 break;
2493 } 2493 }
2494 pserialize_read_exit(s); 2494 pserialize_read_exit(s);
2495} 2495}
2496 2496
2497static void 2497static void
2498_if_down(struct ifnet *ifp) 2498_if_down(struct ifnet *ifp)
2499{ 2499{
2500 struct ifaddr *ifa; 2500 struct ifaddr *ifa;
2501 struct domain *dp; 2501 struct domain *dp;
2502 int s, bound; 2502 int s, bound;
2503 struct psref psref; 2503 struct psref psref;
2504 2504
2505 ifp->if_flags &= ~IFF_UP; 2505 ifp->if_flags &= ~IFF_UP;
2506 nanotime(&ifp->if_lastchange); 2506 nanotime(&ifp->if_lastchange);
2507 2507
2508 bound = curlwp_bind(); 2508 bound = curlwp_bind();
2509 s = pserialize_read_enter(); 2509 s = pserialize_read_enter();
2510 IFADDR_READER_FOREACH(ifa, ifp) { 2510 IFADDR_READER_FOREACH(ifa, ifp) {
2511 ifa_acquire(ifa, &psref); 2511 ifa_acquire(ifa, &psref);
2512 pserialize_read_exit(s); 2512 pserialize_read_exit(s);
2513 2513
2514 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 2514 pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
2515 2515
2516 s = pserialize_read_enter(); 2516 s = pserialize_read_enter();
2517 ifa_release(ifa, &psref); 2517 ifa_release(ifa, &psref);
2518 } 2518 }
2519 pserialize_read_exit(s); 2519 pserialize_read_exit(s);
2520 curlwp_bindx(bound); 2520 curlwp_bindx(bound);
2521 2521
 2522 /*
 2523 * Modification of if_link_cansched is serialized with the
 2524 * ifnet ioctl lock.
 2525 *
 2526 * The link state change lock is taken to synchronize with the
 2527 * read in if_link_state_change_work_schedule(). Once we set
 2528 * this to false, our if_link_work won't be scheduled. But
 2529 * we need to wait for our if_link_work to drain in case we
 2530 * lost that race.
 2531 */
2522 IF_LINK_STATE_CHANGE_LOCK(ifp); 2532 IF_LINK_STATE_CHANGE_LOCK(ifp);
2523 ifp->if_link_cansched = false; 2533 ifp->if_link_cansched = false;
2524 workqueue_wait(ifnet_link_state_wq, &ifp->if_link_work); 
2525 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2534 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2526 2535
 2536 workqueue_wait(ifnet_link_state_wq, &ifp->if_link_work);
 2537
2527 IFQ_PURGE(&ifp->if_snd); 2538 IFQ_PURGE(&ifp->if_snd);
2528#if NCARP > 0 2539#if NCARP > 0
2529 if (ifp->if_carp) 2540 if (ifp->if_carp)
2530 carp_carpdev_state(ifp); 2541 carp_carpdev_state(ifp);
2531#endif 2542#endif
2532 rt_ifmsg(ifp); 2543 rt_ifmsg(ifp);
2533 DOMAIN_FOREACH(dp) { 2544 DOMAIN_FOREACH(dp) {
2534 if (dp->dom_if_down) 2545 if (dp->dom_if_down)
2535 dp->dom_if_down(ifp); 2546 dp->dom_if_down(ifp);
2536 } 2547 }
2537} 2548}
2538 2549
2539static void 2550static void
2540if_down_deactivated(struct ifnet *ifp) 2551if_down_deactivated(struct ifnet *ifp)
2541{ 2552{
2542 2553
2543 KASSERT(if_is_deactivated(ifp)); 2554 KASSERT(if_is_deactivated(ifp));
2544 _if_down(ifp); 2555 _if_down(ifp);
2545} 2556}
2546 2557
2547void 2558void
2548if_down_locked(struct ifnet *ifp) 2559if_down_locked(struct ifnet *ifp)
2549{ 2560{
2550 2561
2551 KASSERT(IFNET_LOCKED(ifp)); 2562 KASSERT(IFNET_LOCKED(ifp));
2552 _if_down(ifp); 2563 _if_down(ifp);
2553} 2564}
2554 2565
2555/* 2566/*
2556 * Mark an interface down and notify protocols of 2567 * Mark an interface down and notify protocols of
2557 * the transition. 2568 * the transition.
2558 * NOTE: must be called at splsoftnet or equivalent. 2569 * NOTE: must be called at splsoftnet or equivalent.
2559 */ 2570 */
2560void 2571void
2561if_down(struct ifnet *ifp) 2572if_down(struct ifnet *ifp)
2562{ 2573{
2563 2574
2564 IFNET_LOCK(ifp); 2575 IFNET_LOCK(ifp);
2565 if_down_locked(ifp); 2576 if_down_locked(ifp);
2566 IFNET_UNLOCK(ifp); 2577 IFNET_UNLOCK(ifp);
2567} 2578}
2568 2579
2569/* 2580/*
2570 * Must be called with holding if_ioctl_lock. 2581 * Must be called with holding if_ioctl_lock.
2571 */ 2582 */
2572static void 2583static void
2573if_up_locked(struct ifnet *ifp) 2584if_up_locked(struct ifnet *ifp)
2574{ 2585{
2575#ifdef notyet 2586#ifdef notyet
2576 struct ifaddr *ifa; 2587 struct ifaddr *ifa;
2577#endif 2588#endif
2578 struct domain *dp; 2589 struct domain *dp;
2579 2590
2580 KASSERT(IFNET_LOCKED(ifp)); 2591 KASSERT(IFNET_LOCKED(ifp));
2581 2592
2582 KASSERT(!if_is_deactivated(ifp)); 2593 KASSERT(!if_is_deactivated(ifp));
2583 ifp->if_flags |= IFF_UP; 2594 ifp->if_flags |= IFF_UP;
2584 nanotime(&ifp->if_lastchange); 2595 nanotime(&ifp->if_lastchange);
2585#ifdef notyet 2596#ifdef notyet
2586 /* this has no effect on IP, and will kill all ISO connections XXX */ 2597 /* this has no effect on IP, and will kill all ISO connections XXX */
2587 IFADDR_READER_FOREACH(ifa, ifp) 2598 IFADDR_READER_FOREACH(ifa, ifp)
2588 pfctlinput(PRC_IFUP, ifa->ifa_addr); 2599 pfctlinput(PRC_IFUP, ifa->ifa_addr);
2589#endif 2600#endif
2590#if NCARP > 0 2601#if NCARP > 0
2591 if (ifp->if_carp) 2602 if (ifp->if_carp)
2592 carp_carpdev_state(ifp); 2603 carp_carpdev_state(ifp);
2593#endif 2604#endif
2594 rt_ifmsg(ifp); 2605 rt_ifmsg(ifp);
2595 DOMAIN_FOREACH(dp) { 2606 DOMAIN_FOREACH(dp) {
2596 if (dp->dom_if_up) 2607 if (dp->dom_if_up)
2597 dp->dom_if_up(ifp); 2608 dp->dom_if_up(ifp);
2598 } 2609 }
2599 2610
2600 IF_LINK_STATE_CHANGE_LOCK(ifp); 2611 IF_LINK_STATE_CHANGE_LOCK(ifp);
2601 ifp->if_link_cansched = true; 2612 ifp->if_link_cansched = true;
2602 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2613 IF_LINK_STATE_CHANGE_UNLOCK(ifp);
2603} 2614}
2604 2615
2605/* 2616/*
2606 * Handle interface slowtimo timer routine. Called 2617 * Handle interface slowtimo timer routine. Called
2607 * from softclock, we decrement timer (if set) and 2618 * from softclock, we decrement timer (if set) and
2608 * call the appropriate interface routine on expiration. 2619 * call the appropriate interface routine on expiration.
2609 */ 2620 */
2610static void 2621static void
2611if_slowtimo(void *arg) 2622if_slowtimo(void *arg)
2612{ 2623{
2613 void (*slowtimo)(struct ifnet *); 2624 void (*slowtimo)(struct ifnet *);
2614 struct ifnet *ifp = arg; 2625 struct ifnet *ifp = arg;
2615 int s; 2626 int s;
2616 2627
2617 slowtimo = ifp->if_slowtimo; 2628 slowtimo = ifp->if_slowtimo;
2618 if (__predict_false(slowtimo == NULL)) 2629 if (__predict_false(slowtimo == NULL))
2619 return; 2630 return;
2620 2631
2621 s = splnet(); 2632 s = splnet();
2622 if (ifp->if_timer != 0 && --ifp->if_timer == 0) 2633 if (ifp->if_timer != 0 && --ifp->if_timer == 0)
2623 (*slowtimo)(ifp); 2634 (*slowtimo)(ifp);
2624 2635
2625 splx(s); 2636 splx(s);
2626 2637
2627 if (__predict_true(ifp->if_slowtimo != NULL)) 2638 if (__predict_true(ifp->if_slowtimo != NULL))
2628 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ); 2639 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ);
2629} 2640}
2630 2641
2631/* 2642/*
2632 * Mark an interface up and notify protocols of 2643 * Mark an interface up and notify protocols of
2633 * the transition. 2644 * the transition.
2634 * NOTE: must be called at splsoftnet or equivalent. 2645 * NOTE: must be called at splsoftnet or equivalent.
2635 */ 2646 */
2636void 2647void
2637if_up(struct ifnet *ifp) 2648if_up(struct ifnet *ifp)
2638{ 2649{
2639 2650
2640 IFNET_LOCK(ifp); 2651 IFNET_LOCK(ifp);
2641 if_up_locked(ifp); 2652 if_up_locked(ifp);
2642 IFNET_UNLOCK(ifp); 2653 IFNET_UNLOCK(ifp);
2643} 2654}
2644 2655
2645/* 2656/*
2646 * Set/clear promiscuous mode on interface ifp based on the truth value 2657 * Set/clear promiscuous mode on interface ifp based on the truth value
2647 * of pswitch. The calls are reference counted so that only the first 2658 * of pswitch. The calls are reference counted so that only the first
2648 * "on" request actually has an effect, as does the final "off" request. 2659 * "on" request actually has an effect, as does the final "off" request.
2649 * Results are undefined if the "off" and "on" requests are not matched. 2660 * Results are undefined if the "off" and "on" requests are not matched.
2650 */ 2661 */
2651int 2662int
2652ifpromisc_locked(struct ifnet *ifp, int pswitch) 2663ifpromisc_locked(struct ifnet *ifp, int pswitch)
2653{ 2664{
2654 int pcount, ret = 0; 2665 int pcount, ret = 0;
2655 u_short nflags; 2666 u_short nflags;
2656 2667
2657 KASSERT(IFNET_LOCKED(ifp)); 2668 KASSERT(IFNET_LOCKED(ifp));
2658 2669
2659 pcount = ifp->if_pcount; 2670 pcount = ifp->if_pcount;
2660 if (pswitch) { 2671 if (pswitch) {
2661 /* 2672 /*
2662 * Allow the device to be "placed" into promiscuous 2673 * Allow the device to be "placed" into promiscuous
2663 * mode even if it is not configured up. It will 2674 * mode even if it is not configured up. It will
2664 * consult IFF_PROMISC when it is brought up. 2675 * consult IFF_PROMISC when it is brought up.
2665 */ 2676 */
2666 if (ifp->if_pcount++ != 0) 2677 if (ifp->if_pcount++ != 0)
2667 goto out; 2678 goto out;
2668 nflags = ifp->if_flags | IFF_PROMISC; 2679 nflags = ifp->if_flags | IFF_PROMISC;
2669 } else { 2680 } else {
2670 if (--ifp->if_pcount > 0) 2681 if (--ifp->if_pcount > 0)
2671 goto out; 2682 goto out;
2672 nflags = ifp->if_flags & ~IFF_PROMISC; 2683 nflags = ifp->if_flags & ~IFF_PROMISC;
2673 } 2684 }
2674 ret = if_flags_set(ifp, nflags); 2685 ret = if_flags_set(ifp, nflags);
2675 /* Restore interface state if not successful. */ 2686 /* Restore interface state if not successful. */
2676 if (ret != 0) { 2687 if (ret != 0) {
2677 ifp->if_pcount = pcount; 2688 ifp->if_pcount = pcount;
2678 } 2689 }
2679out: 2690out:
2680 return ret; 2691 return ret;
2681} 2692}
2682 2693
2683int 2694int
2684ifpromisc(struct ifnet *ifp, int pswitch) 2695ifpromisc(struct ifnet *ifp, int pswitch)
2685{ 2696{
2686 int e; 2697 int e;
2687 2698
2688 IFNET_LOCK(ifp); 2699 IFNET_LOCK(ifp);
2689 e = ifpromisc_locked(ifp, pswitch); 2700 e = ifpromisc_locked(ifp, pswitch);
2690 IFNET_UNLOCK(ifp); 2701 IFNET_UNLOCK(ifp);
2691 2702
2692 return e; 2703 return e;
2693} 2704}
2694 2705
2695/* 2706/*
2696 * Map interface name to 2707 * Map interface name to
2697 * interface structure pointer. 2708 * interface structure pointer.
2698 */ 2709 */
2699struct ifnet * 2710struct ifnet *
2700ifunit(const char *name) 2711ifunit(const char *name)
2701{ 2712{
2702 struct ifnet *ifp; 2713 struct ifnet *ifp;
2703 const char *cp = name; 2714 const char *cp = name;
2704 u_int unit = 0; 2715 u_int unit = 0;
2705 u_int i; 2716 u_int i;
2706 int s; 2717 int s;
2707 2718
2708 /* 2719 /*
2709 * If the entire name is a number, treat it as an ifindex. 2720 * If the entire name is a number, treat it as an ifindex.
2710 */ 2721 */
2711 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) { 2722 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2712 unit = unit * 10 + (*cp - '0'); 2723 unit = unit * 10 + (*cp - '0');
2713 } 2724 }
2714 2725
2715 /* 2726 /*
2716 * If the number took all of the name, then it's a valid ifindex. 2727 * If the number took all of the name, then it's a valid ifindex.
2717 */ 2728 */
2718 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) 2729 if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
2719 return if_byindex(unit); 2730 return if_byindex(unit);
2720 2731
2721 ifp = NULL; 2732 ifp = NULL;
2722 s = pserialize_read_enter(); 2733 s = pserialize_read_enter();
2723 IFNET_READER_FOREACH(ifp) { 2734 IFNET_READER_FOREACH(ifp) {
2724 if (if_is_deactivated(ifp)) 2735 if (if_is_deactivated(ifp))
2725 continue; 2736 continue;
2726 if (strcmp(ifp->if_xname, name) == 0) 2737 if (strcmp(ifp->if_xname, name) == 0)
2727 goto out; 2738 goto out;
2728 } 2739 }
2729out: 2740out:
2730 pserialize_read_exit(s); 2741 pserialize_read_exit(s);
2731 return ifp; 2742 return ifp;
2732} 2743}
2733 2744
2734/* 2745/*
2735 * Get a reference of an ifnet object by an interface name. 2746 * Get a reference of an ifnet object by an interface name.
2736 * The returned reference is protected by psref(9). The caller 2747 * The returned reference is protected by psref(9). The caller
2737 * must release a returned reference by if_put after use. 2748 * must release a returned reference by if_put after use.
2738 */ 2749 */
2739struct ifnet * 2750struct ifnet *
2740if_get(const char *name, struct psref *psref) 2751if_get(const char *name, struct psref *psref)
2741{ 2752{
2742 struct ifnet *ifp; 2753 struct ifnet *ifp;
2743 const char *cp = name; 2754 const char *cp = name;
2744 u_int unit = 0; 2755 u_int unit = 0;
2745 u_int i; 2756 u_int i;
2746 int s; 2757 int s;
2747 2758
2748 /* 2759 /*
2749 * If the entire name is a number, treat it as an ifindex. 2760 * If the entire name is a number, treat it as an ifindex.
2750 */ 2761 */
2751 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) { 2762 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) {
2752 unit = unit * 10 + (*cp - '0'); 2763 unit = unit * 10 + (*cp - '0');
2753 } 2764 }
2754 2765
2755 /* 2766 /*
2756 * If the number took all of the name, then it's a valid ifindex. 2767 * If the number took all of the name, then it's a valid ifindex.
2757 */ 2768 */
2758 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) 2769 if (i == IFNAMSIZ || (cp != name && *cp == '\0'))
2759 return if_get_byindex(unit, psref); 2770 return if_get_byindex(unit, psref);
2760 2771
2761 ifp = NULL; 2772 ifp = NULL;
2762 s = pserialize_read_enter(); 2773 s = pserialize_read_enter();
2763 IFNET_READER_FOREACH(ifp) { 2774 IFNET_READER_FOREACH(ifp) {
2764 if (if_is_deactivated(ifp)) 2775 if (if_is_deactivated(ifp))
2765 continue; 2776 continue;
2766 if (strcmp(ifp->if_xname, name) == 0) { 2777 if (strcmp(ifp->if_xname, name) == 0) {
2767 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref); 2778 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
2768 psref_acquire(psref, &ifp->if_psref, 2779 psref_acquire(psref, &ifp->if_psref,
2769 ifnet_psref_class); 2780 ifnet_psref_class);
2770 goto out; 2781 goto out;
2771 } 2782 }
2772 } 2783 }
2773out: 2784out:
2774 pserialize_read_exit(s); 2785 pserialize_read_exit(s);
2775 return ifp; 2786 return ifp;
2776} 2787}
2777 2788
2778/* 2789/*
2779 * Release a reference of an ifnet object given by if_get, if_get_byindex 2790 * Release a reference of an ifnet object given by if_get, if_get_byindex
2780 * or if_get_bylla. 2791 * or if_get_bylla.
2781 */ 2792 */
2782void 2793void
2783if_put(const struct ifnet *ifp, struct psref *psref) 2794if_put(const struct ifnet *ifp, struct psref *psref)
2784{ 2795{
2785 2796
2786 if (ifp == NULL) 2797 if (ifp == NULL)
2787 return; 2798 return;
2788 2799
2789 psref_release(psref, &ifp->if_psref, ifnet_psref_class); 2800 psref_release(psref, &ifp->if_psref, ifnet_psref_class);
2790} 2801}
2791 2802
2792/* 2803/*
2793 * Return ifp having idx. Return NULL if not found. Normally if_byindex 2804 * Return ifp having idx. Return NULL if not found. Normally if_byindex
2794 * should be used. 2805 * should be used.
2795 */ 2806 */
2796ifnet_t * 2807ifnet_t *
2797_if_byindex(u_int idx) 2808_if_byindex(u_int idx)
2798{ 2809{
2799 2810
2800 return (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL; 2811 return (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL;
2801} 2812}
2802 2813
2803/* 2814/*
2804 * Return ifp having idx. Return NULL if not found or the found ifp is 2815 * Return ifp having idx. Return NULL if not found or the found ifp is
2805 * already deactivated. 2816 * already deactivated.
2806 */ 2817 */
2807ifnet_t * 2818ifnet_t *
2808if_byindex(u_int idx) 2819if_byindex(u_int idx)
2809{ 2820{
2810 ifnet_t *ifp; 2821 ifnet_t *ifp;
2811 2822
2812 ifp = _if_byindex(idx); 2823 ifp = _if_byindex(idx);
2813 if (ifp != NULL && if_is_deactivated(ifp)) 2824 if (ifp != NULL && if_is_deactivated(ifp))
2814 ifp = NULL; 2825 ifp = NULL;
2815 return ifp; 2826 return ifp;
2816} 2827}
2817 2828
2818/* 2829/*
2819 * Get a reference of an ifnet object by an interface index. 2830 * Get a reference of an ifnet object by an interface index.
2820 * The returned reference is protected by psref(9). The caller 2831 * The returned reference is protected by psref(9). The caller
2821 * must release a returned reference by if_put after use. 2832 * must release a returned reference by if_put after use.
2822 */ 2833 */
2823ifnet_t * 2834ifnet_t *
2824if_get_byindex(u_int idx, struct psref *psref) 2835if_get_byindex(u_int idx, struct psref *psref)
2825{ 2836{
2826 ifnet_t *ifp; 2837 ifnet_t *ifp;
2827 int s; 2838 int s;
2828 2839
2829 s = pserialize_read_enter(); 2840 s = pserialize_read_enter();
2830 ifp = if_byindex(idx); 2841 ifp = if_byindex(idx);
2831 if (__predict_true(ifp != NULL)) { 2842 if (__predict_true(ifp != NULL)) {
2832 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref); 2843 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref);
2833 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class); 2844 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
2834 } 2845 }
2835 pserialize_read_exit(s); 2846 pserialize_read_exit(s);
2836 2847
2837 return ifp; 2848 return ifp;
2838} 2849}
2839 2850
2840ifnet_t * 2851ifnet_t *
2841if_get_bylla(const void *lla, unsigned char lla_len, struct psref *psref) 2852if_get_bylla(const void *lla, unsigned char lla_len, struct psref *psref)
2842{ 2853{
2843 ifnet_t *ifp; 2854 ifnet_t *ifp;
2844 int s; 2855 int s;
2845 2856
2846 s = pserialize_read_enter(); 2857 s = pserialize_read_enter();
2847 IFNET_READER_FOREACH(ifp) { 2858 IFNET_READER_FOREACH(ifp) {
2848 if (if_is_deactivated(ifp)) 2859 if (if_is_deactivated(ifp))
2849 continue; 2860 continue;
2850 if (ifp->if_addrlen != lla_len) 2861 if (ifp->if_addrlen != lla_len)
2851 continue; 2862 continue;
2852 if (memcmp(lla, CLLADDR(ifp->if_sadl), lla_len) == 0) { 2863 if (memcmp(lla, CLLADDR(ifp->if_sadl), lla_len) == 0) {
2853 psref_acquire(psref, &ifp->if_psref, 2864 psref_acquire(psref, &ifp->if_psref,
2854 ifnet_psref_class); 2865 ifnet_psref_class);
2855 break; 2866 break;
2856 } 2867 }
2857 } 2868 }
2858 pserialize_read_exit(s); 2869 pserialize_read_exit(s);
2859 2870
2860 return ifp; 2871 return ifp;
2861} 2872}
2862 2873
2863/* 2874/*
2864 * Note that it's safe only if the passed ifp is guaranteed to not be freed, 2875 * Note that it's safe only if the passed ifp is guaranteed to not be freed,
2865 * for example using pserialize or the ifp is already held or some other 2876 * for example using pserialize or the ifp is already held or some other
2866 * object is held which guarantes the ifp to not be freed indirectly. 2877 * object is held which guarantes the ifp to not be freed indirectly.
2867 */ 2878 */
2868void 2879void
2869if_acquire(struct ifnet *ifp, struct psref *psref) 2880if_acquire(struct ifnet *ifp, struct psref *psref)
2870{ 2881{
2871 2882
2872 KASSERT(ifp->if_index != 0); 2883 KASSERT(ifp->if_index != 0);
2873 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class); 2884 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class);
2874} 2885}
2875 2886
2876bool 2887bool
2877if_held(struct ifnet *ifp) 2888if_held(struct ifnet *ifp)
2878{ 2889{
2879 2890
2880 return psref_held(&ifp->if_psref, ifnet_psref_class); 2891 return psref_held(&ifp->if_psref, ifnet_psref_class);
2881} 2892}
2882 2893
2883/* 2894/*
2884 * Some tunnel interfaces can nest, e.g. IPv4 over IPv4 gif(4) tunnel over IPv4. 2895 * Some tunnel interfaces can nest, e.g. IPv4 over IPv4 gif(4) tunnel over IPv4.
2885 * Check the tunnel nesting count. 2896 * Check the tunnel nesting count.
2886 * Return > 0, if tunnel nesting count is more than limit. 2897 * Return > 0, if tunnel nesting count is more than limit.
2887 * Return 0, if tunnel nesting count is equal or less than limit. 2898 * Return 0, if tunnel nesting count is equal or less than limit.
2888 */ 2899 */
2889int 2900int
2890if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, int limit) 2901if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, int limit)
2891{ 2902{
2892 struct m_tag *mtag; 2903 struct m_tag *mtag;
2893 int *count; 2904 int *count;
2894 2905
2895 mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO); 2906 mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO);
2896 if (mtag != NULL) { 2907 if (mtag != NULL) {
2897 count = (int *)(mtag + 1); 2908 count = (int *)(mtag + 1);
2898 if (++(*count) > limit) { 2909 if (++(*count) > limit) {
2899 log(LOG_NOTICE, 2910 log(LOG_NOTICE,
2900 "%s: recursively called too many times(%d)\n", 2911 "%s: recursively called too many times(%d)\n",
2901 ifp->if_xname, *count); 2912 ifp->if_xname, *count);
2902 return EIO; 2913 return EIO;
2903 } 2914 }
2904 } else { 2915 } else {
2905 mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count), 2916 mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count),
2906 M_NOWAIT); 2917 M_NOWAIT);
2907 if (mtag != NULL) { 2918 if (mtag != NULL) {
2908 m_tag_prepend(m, mtag); 2919 m_tag_prepend(m, mtag);
2909 count = (int *)(mtag + 1); 2920 count = (int *)(mtag + 1);
2910 *count = 0; 2921 *count = 0;
2911 } else { 2922 } else {
2912 log(LOG_DEBUG, 2923 log(LOG_DEBUG,
2913 "%s: m_tag_get() failed, recursion calls are not prevented.\n", 2924 "%s: m_tag_get() failed, recursion calls are not prevented.\n",
2914 ifp->if_xname); 2925 ifp->if_xname);
2915 } 2926 }
2916 } 2927 }
2917 2928
2918 return 0; 2929 return 0;
2919} 2930}
2920 2931
2921static void 2932static void
2922if_tunnel_ro_init_pc(void *p, void *arg __unused, struct cpu_info *ci __unused) 2933if_tunnel_ro_init_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
2923{ 2934{
2924 struct tunnel_ro *tro = p; 2935 struct tunnel_ro *tro = p;
2925 2936
2926 tro->tr_ro = kmem_zalloc(sizeof(*tro->tr_ro), KM_SLEEP); 2937 tro->tr_ro = kmem_zalloc(sizeof(*tro->tr_ro), KM_SLEEP);
2927 tro->tr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 2938 tro->tr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
2928} 2939}
2929 2940
2930static void 2941static void
2931if_tunnel_ro_fini_pc(void *p, void *arg __unused, struct cpu_info *ci __unused) 2942if_tunnel_ro_fini_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
2932{ 2943{
2933 struct tunnel_ro *tro = p; 2944 struct tunnel_ro *tro = p;
2934 2945
2935 rtcache_free(tro->tr_ro); 2946 rtcache_free(tro->tr_ro);
2936 kmem_free(tro->tr_ro, sizeof(*tro->tr_ro)); 2947 kmem_free(tro->tr_ro, sizeof(*tro->tr_ro));
2937 2948
2938 mutex_obj_free(tro->tr_lock); 2949 mutex_obj_free(tro->tr_lock);
2939} 2950}
2940 2951
2941percpu_t * 2952percpu_t *
2942if_tunnel_alloc_ro_percpu(void) 2953if_tunnel_alloc_ro_percpu(void)
2943{ 2954{
2944 2955
2945 return percpu_create(sizeof(struct tunnel_ro), 2956 return percpu_create(sizeof(struct tunnel_ro),
2946 if_tunnel_ro_init_pc, if_tunnel_ro_fini_pc, NULL); 2957 if_tunnel_ro_init_pc, if_tunnel_ro_fini_pc, NULL);
2947} 2958}
2948 2959
2949void 2960void
2950if_tunnel_free_ro_percpu(percpu_t *ro_percpu) 2961if_tunnel_free_ro_percpu(percpu_t *ro_percpu)
2951{ 2962{
2952 2963
2953 percpu_free(ro_percpu, sizeof(struct tunnel_ro)); 2964 percpu_free(ro_percpu, sizeof(struct tunnel_ro));
2954} 2965}
2955 2966
2956 2967
2957static void 2968static void
2958if_tunnel_rtcache_free_pc(void *p, void *arg __unused, struct cpu_info *ci __unused) 2969if_tunnel_rtcache_free_pc(void *p, void *arg __unused, struct cpu_info *ci __unused)
2959{ 2970{
2960 struct tunnel_ro *tro = p; 2971 struct tunnel_ro *tro = p;
2961 2972
2962 mutex_enter(tro->tr_lock); 2973 mutex_enter(tro->tr_lock);
2963 rtcache_free(tro->tr_ro); 2974 rtcache_free(tro->tr_ro);
2964 mutex_exit(tro->tr_lock); 2975 mutex_exit(tro->tr_lock);
2965} 2976}
2966 2977
2967void if_tunnel_ro_percpu_rtcache_free(percpu_t *ro_percpu) 2978void if_tunnel_ro_percpu_rtcache_free(percpu_t *ro_percpu)
2968{ 2979{
2969 2980
2970 percpu_foreach(ro_percpu, if_tunnel_rtcache_free_pc, NULL); 2981 percpu_foreach(ro_percpu, if_tunnel_rtcache_free_pc, NULL);
2971} 2982}
2972 2983
2973void 2984void
2974if_export_if_data(ifnet_t * const ifp, struct if_data *ifi, bool zero_stats) 2985if_export_if_data(ifnet_t * const ifp, struct if_data *ifi, bool zero_stats)
2975{ 2986{
2976 2987
2977 /* Collet the volatile stats first; this zeros *ifi. */ 2988 /* Collet the volatile stats first; this zeros *ifi. */
2978 if_stats_to_if_data(ifp, ifi, zero_stats); 2989 if_stats_to_if_data(ifp, ifi, zero_stats);
2979 2990
2980 ifi->ifi_type = ifp->if_type; 2991 ifi->ifi_type = ifp->if_type;
2981 ifi->ifi_addrlen = ifp->if_addrlen; 2992 ifi->ifi_addrlen = ifp->if_addrlen;
2982 ifi->ifi_hdrlen = ifp->if_hdrlen; 2993 ifi->ifi_hdrlen = ifp->if_hdrlen;
2983 ifi->ifi_link_state = ifp->if_link_state; 2994 ifi->ifi_link_state = ifp->if_link_state;
2984 ifi->ifi_mtu = ifp->if_mtu; 2995 ifi->ifi_mtu = ifp->if_mtu;
2985 ifi->ifi_metric = ifp->if_metric; 2996 ifi->ifi_metric = ifp->if_metric;
2986 ifi->ifi_baudrate = ifp->if_baudrate; 2997 ifi->ifi_baudrate = ifp->if_baudrate;
2987 ifi->ifi_lastchange = ifp->if_lastchange; 2998 ifi->ifi_lastchange = ifp->if_lastchange;
2988} 2999}
2989 3000
2990/* common */ 3001/* common */
2991int 3002int
2992ifioctl_common(struct ifnet *ifp, u_long cmd, void *data) 3003ifioctl_common(struct ifnet *ifp, u_long cmd, void *data)
2993{ 3004{
2994 int s; 3005 int s;
2995 struct ifreq *ifr; 3006 struct ifreq *ifr;
2996 struct ifcapreq *ifcr; 3007 struct ifcapreq *ifcr;
2997 struct ifdatareq *ifdr; 3008 struct ifdatareq *ifdr;
2998 unsigned short flags; 3009 unsigned short flags;
2999 char *descr; 3010 char *descr;
3000 int error; 3011 int error;
3001 3012
3002 switch (cmd) { 3013 switch (cmd) {
3003 case SIOCSIFCAP: 3014 case SIOCSIFCAP:
3004 ifcr = data; 3015 ifcr = data;
3005 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0) 3016 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0)
3006 return EINVAL; 3017 return EINVAL;
3007 3018
3008 if (ifcr->ifcr_capenable == ifp->if_capenable) 3019 if (ifcr->ifcr_capenable == ifp->if_capenable)
3009 return 0; 3020 return 0;
3010 3021
3011 ifp->if_capenable = ifcr->ifcr_capenable; 3022 ifp->if_capenable = ifcr->ifcr_capenable;
3012 3023
3013 /* Pre-compute the checksum flags mask. */ 3024 /* Pre-compute the checksum flags mask. */
3014 ifp->if_csum_flags_tx = 0; 3025 ifp->if_csum_flags_tx = 0;
3015 ifp->if_csum_flags_rx = 0; 3026 ifp->if_csum_flags_rx = 0;
3016 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) 3027 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx)
3017 ifp->if_csum_flags_tx |= M_CSUM_IPv4; 3028 ifp->if_csum_flags_tx |= M_CSUM_IPv4;
3018 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) 3029 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx)
3019 ifp->if_csum_flags_rx |= M_CSUM_IPv4; 3030 ifp->if_csum_flags_rx |= M_CSUM_IPv4;
3020 3031
3021 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) 3032 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx)
3022 ifp->if_csum_flags_tx |= M_CSUM_TCPv4; 3033 ifp->if_csum_flags_tx |= M_CSUM_TCPv4;
3023 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) 3034 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx)
3024 ifp->if_csum_flags_rx |= M_CSUM_TCPv4; 3035 ifp->if_csum_flags_rx |= M_CSUM_TCPv4;
3025 3036
3026 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) 3037 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx)
3027 ifp->if_csum_flags_tx |= M_CSUM_UDPv4; 3038 ifp->if_csum_flags_tx |= M_CSUM_UDPv4;
3028 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) 3039 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx)
3029 ifp->if_csum_flags_rx |= M_CSUM_UDPv4; 3040 ifp->if_csum_flags_rx |= M_CSUM_UDPv4;
3030 3041
3031 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) 3042 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx)
3032 ifp->if_csum_flags_tx |= M_CSUM_TCPv6; 3043 ifp->if_csum_flags_tx |= M_CSUM_TCPv6;
3033 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) 3044 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx)
3034 ifp->if_csum_flags_rx |= M_CSUM_TCPv6; 3045 ifp->if_csum_flags_rx |= M_CSUM_TCPv6;
3035 3046
3036 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) 3047 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx)
3037 ifp->if_csum_flags_tx |= M_CSUM_UDPv6; 3048 ifp->if_csum_flags_tx |= M_CSUM_UDPv6;
3038 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) 3049 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx)
3039 ifp->if_csum_flags_rx |= M_CSUM_UDPv6; 3050 ifp->if_csum_flags_rx |= M_CSUM_UDPv6;
3040 3051
3041 if (ifp->if_capenable & IFCAP_TSOv4) 3052 if (ifp->if_capenable & IFCAP_TSOv4)
3042 ifp->if_csum_flags_tx |= M_CSUM_TSOv4; 3053 ifp->if_csum_flags_tx |= M_CSUM_TSOv4;
3043 if (ifp->if_capenable & IFCAP_TSOv6) 3054 if (ifp->if_capenable & IFCAP_TSOv6)
3044 ifp->if_csum_flags_tx |= M_CSUM_TSOv6; 3055 ifp->if_csum_flags_tx |= M_CSUM_TSOv6;
3045 3056
3046#if NBRIDGE > 0 3057#if NBRIDGE > 0
3047 if (ifp->if_bridge != NULL) 3058 if (ifp->if_bridge != NULL)
3048 bridge_calc_csum_flags(ifp->if_bridge); 3059 bridge_calc_csum_flags(ifp->if_bridge);
3049#endif 3060#endif
3050 3061
3051 if (ifp->if_flags & IFF_UP) 3062 if (ifp->if_flags & IFF_UP)
3052 return ENETRESET; 3063 return ENETRESET;
3053 return 0; 3064 return 0;
3054 case SIOCSIFFLAGS: 3065 case SIOCSIFFLAGS:
3055 ifr = data; 3066 ifr = data;
3056 /* 3067 /*
3057 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here, but if_up 3068 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here, but if_up
3058 * and if_down aren't MP-safe yet, so we must hold the lock. 3069 * and if_down aren't MP-safe yet, so we must hold the lock.
3059 */ 3070 */
3060 KERNEL_LOCK_IF_IFP_MPSAFE(ifp); 3071 KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
3061 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 3072 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) {
3062 s = splsoftnet(); 3073 s = splsoftnet();
3063 if_down_locked(ifp); 3074 if_down_locked(ifp);
3064 splx(s); 3075 splx(s);
3065 } 3076 }
3066 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 3077 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) {
3067 s = splsoftnet(); 3078 s = splsoftnet();
3068 if_up_locked(ifp); 3079 if_up_locked(ifp);
3069 splx(s); 3080 splx(s);
3070 } 3081 }
3071 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp); 3082 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
3072 flags = (ifp->if_flags & IFF_CANTCHANGE) | 3083 flags = (ifp->if_flags & IFF_CANTCHANGE) |
3073 (ifr->ifr_flags &~ IFF_CANTCHANGE); 3084 (ifr->ifr_flags &~ IFF_CANTCHANGE);
3074 if (ifp->if_flags != flags) { 3085 if (ifp->if_flags != flags) {
3075 ifp->if_flags = flags; 3086 ifp->if_flags = flags;
3076 /* Notify that the flags have changed. */ 3087 /* Notify that the flags have changed. */
3077 rt_ifmsg(ifp); 3088 rt_ifmsg(ifp);
3078 } 3089 }
3079 break; 3090 break;
3080 case SIOCGIFFLAGS: 3091 case SIOCGIFFLAGS:
3081 ifr = data; 3092 ifr = data;
3082 ifr->ifr_flags = ifp->if_flags; 3093 ifr->ifr_flags = ifp->if_flags;
3083 break; 3094 break;
3084 3095
3085 case SIOCGIFMETRIC: 3096 case SIOCGIFMETRIC:
3086 ifr = data; 3097 ifr = data;
3087 ifr->ifr_metric = ifp->if_metric; 3098 ifr->ifr_metric = ifp->if_metric;
3088 break; 3099 break;
3089 3100
3090 case SIOCGIFMTU: 3101 case SIOCGIFMTU:
3091 ifr = data; 3102 ifr = data;
3092 ifr->ifr_mtu = ifp->if_mtu; 3103 ifr->ifr_mtu = ifp->if_mtu;
3093 break; 3104 break;
3094 3105
3095 case SIOCGIFDLT: 3106 case SIOCGIFDLT:
3096 ifr = data; 3107 ifr = data;
3097 ifr->ifr_dlt = ifp->if_dlt; 3108 ifr->ifr_dlt = ifp->if_dlt;
3098 break; 3109 break;
3099 3110
3100 case SIOCGIFCAP: 3111 case SIOCGIFCAP:
3101 ifcr = data; 3112 ifcr = data;
3102 ifcr->ifcr_capabilities = ifp->if_capabilities; 3113 ifcr->ifcr_capabilities = ifp->if_capabilities;
3103 ifcr->ifcr_capenable = ifp->if_capenable; 3114 ifcr->ifcr_capenable = ifp->if_capenable;
3104 break; 3115 break;
3105 3116
3106 case SIOCSIFMETRIC: 3117 case SIOCSIFMETRIC:
3107 ifr = data; 3118 ifr = data;
3108 ifp->if_metric = ifr->ifr_metric; 3119 ifp->if_metric = ifr->ifr_metric;
3109 break; 3120 break;
3110 3121
3111 case SIOCGIFDATA: 3122 case SIOCGIFDATA:
3112 ifdr = data; 3123 ifdr = data;
3113 if_export_if_data(ifp, &ifdr->ifdr_data, false); 3124 if_export_if_data(ifp, &ifdr->ifdr_data, false);
3114 break; 3125 break;
3115 3126
3116 case SIOCGIFINDEX: 3127 case SIOCGIFINDEX:
3117 ifr = data; 3128 ifr = data;
3118 ifr->ifr_index = ifp->if_index; 3129 ifr->ifr_index = ifp->if_index;
3119 break; 3130 break;
3120 3131
3121 case SIOCZIFDATA: 3132 case SIOCZIFDATA:
3122 ifdr = data; 3133 ifdr = data;
3123 if_export_if_data(ifp, &ifdr->ifdr_data, true); 3134 if_export_if_data(ifp, &ifdr->ifdr_data, true);
3124 getnanotime(&ifp->if_lastchange); 3135 getnanotime(&ifp->if_lastchange);
3125 break; 3136 break;
3126 case SIOCSIFMTU: 3137 case SIOCSIFMTU:
3127 ifr = data; 3138 ifr = data;
3128 if (ifp->if_mtu == ifr->ifr_mtu) 3139 if (ifp->if_mtu == ifr->ifr_mtu)
3129 break; 3140 break;
3130 ifp->if_mtu = ifr->ifr_mtu; 3141 ifp->if_mtu = ifr->ifr_mtu;
3131 /* 3142 /*
3132 * If the link MTU changed, do network layer specific procedure. 3143 * If the link MTU changed, do network layer specific procedure.
3133 */ 3144 */
3134#ifdef INET6 3145#ifdef INET6
3135 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 3146 KERNEL_LOCK_UNLESS_NET_MPSAFE();
3136 if (in6_present) 3147 if (in6_present)
3137 nd6_setmtu(ifp); 3148 nd6_setmtu(ifp);
3138 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 3149 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
3139#endif 3150#endif
3140 return ENETRESET; 3151 return ENETRESET;
3141 case SIOCSIFDESCR: 3152 case SIOCSIFDESCR:
3142 error = kauth_authorize_network(curlwp->l_cred, 3153 error = kauth_authorize_network(curlwp->l_cred,
3143 KAUTH_NETWORK_INTERFACE, 3154 KAUTH_NETWORK_INTERFACE,
3144 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd), 3155 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
3145 NULL); 3156 NULL);
3146 if (error) 3157 if (error)
3147 return error; 3158 return error;
3148 3159
3149 ifr = data; 3160 ifr = data;
3150 3161
3151 if (ifr->ifr_buflen > IFDESCRSIZE) 3162 if (ifr->ifr_buflen > IFDESCRSIZE)
3152 return ENAMETOOLONG; 3163 return ENAMETOOLONG;
3153 3164
3154 if (ifr->ifr_buf == NULL || ifr->ifr_buflen == 0) { 3165 if (ifr->ifr_buf == NULL || ifr->ifr_buflen == 0) {
3155 /* unset description */ 3166 /* unset description */
3156 descr = NULL; 3167 descr = NULL;
3157 } else { 3168 } else {
3158 descr = kmem_zalloc(IFDESCRSIZE, KM_SLEEP); 3169 descr = kmem_zalloc(IFDESCRSIZE, KM_SLEEP);
3159 /* 3170 /*
3160 * copy (IFDESCRSIZE - 1) bytes to ensure 3171 * copy (IFDESCRSIZE - 1) bytes to ensure
3161 * terminating nul 3172 * terminating nul
3162 */ 3173 */
3163 error = copyin(ifr->ifr_buf, descr, IFDESCRSIZE - 1); 3174 error = copyin(ifr->ifr_buf, descr, IFDESCRSIZE - 1);
3164 if (error) { 3175 if (error) {
3165 kmem_free(descr, IFDESCRSIZE); 3176 kmem_free(descr, IFDESCRSIZE);
3166 return error; 3177 return error;
3167 } 3178 }
3168 } 3179 }
3169 3180
3170 if (ifp->if_description != NULL) 3181 if (ifp->if_description != NULL)
3171 kmem_free(ifp->if_description, IFDESCRSIZE); 3182 kmem_free(ifp->if_description, IFDESCRSIZE);
3172 3183
3173 ifp->if_description = descr; 3184 ifp->if_description = descr;
3174 break; 3185 break;
3175 3186
3176 case SIOCGIFDESCR: 3187 case SIOCGIFDESCR:
3177 ifr = data; 3188 ifr = data;
3178 descr = ifp->if_description; 3189 descr = ifp->if_description;
3179 3190
3180 if (descr == NULL) 3191 if (descr == NULL)
3181 return ENOMSG; 3192 return ENOMSG;
3182 3193
3183 if (ifr->ifr_buflen < IFDESCRSIZE) 3194 if (ifr->ifr_buflen < IFDESCRSIZE)
3184 return EINVAL; 3195 return EINVAL;
3185 3196
3186 error = copyout(descr, ifr->ifr_buf, IFDESCRSIZE); 3197 error = copyout(descr, ifr->ifr_buf, IFDESCRSIZE);
3187 if (error) 3198 if (error)
3188 return error; 3199 return error;
3189 break; 3200 break;
3190 3201
3191 default: 3202 default:
3192 return ENOTTY; 3203 return ENOTTY;
3193 } 3204 }
3194 return 0; 3205 return 0;
3195} 3206}
3196 3207
3197int 3208int
3198ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) 3209ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
3199{ 3210{
3200 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data; 3211 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data;
3201 struct ifaddr *ifa; 3212 struct ifaddr *ifa;
3202 const struct sockaddr *any, *sa; 3213 const struct sockaddr *any, *sa;
3203 union { 3214 union {
3204 struct sockaddr sa; 3215 struct sockaddr sa;
3205 struct sockaddr_storage ss; 3216 struct sockaddr_storage ss;
3206 } u, v; 3217 } u, v;
3207 int s, error = 0; 3218 int s, error = 0;
3208 3219
3209 switch (cmd) { 3220 switch (cmd) {
3210 case SIOCSIFADDRPREF: 3221 case SIOCSIFADDRPREF:
3211 error = kauth_authorize_network(curlwp->l_cred, 3222 error = kauth_authorize_network(curlwp->l_cred,
3212 KAUTH_NETWORK_INTERFACE, 3223 KAUTH_NETWORK_INTERFACE,
3213 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd), 3224 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd),
3214 NULL); 3225 NULL);
3215 if (error) 3226 if (error)
3216 return error; 3227 return error;
3217 break; 3228 break;
3218 case SIOCGIFADDRPREF: 3229 case SIOCGIFADDRPREF:
3219 break; 3230 break;
3220 default: 3231 default:
3221 return EOPNOTSUPP; 3232 return EOPNOTSUPP;
3222 } 3233 }
3223 3234
3224 /* sanity checks */ 3235 /* sanity checks */
3225 if (data == NULL || ifp == NULL) { 3236 if (data == NULL || ifp == NULL) {
3226 panic("invalid argument to %s", __func__); 3237 panic("invalid argument to %s", __func__);
3227 /*NOTREACHED*/ 3238 /*NOTREACHED*/
3228 } 3239 }
3229 3240
3230 /* address must be specified on ADD and DELETE */ 3241 /* address must be specified on ADD and DELETE */
3231 sa = sstocsa(&ifap->ifap_addr); 3242 sa = sstocsa(&ifap->ifap_addr);
3232 if (sa->sa_family != sofamily(so)) 3243 if (sa->sa_family != sofamily(so))
3233 return EINVAL; 3244 return EINVAL;
3234 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len) 3245 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len)
3235 return EINVAL; 3246 return EINVAL;
3236 3247
3237 sockaddr_externalize(&v.sa, sizeof(v.ss), sa); 3248 sockaddr_externalize(&v.sa, sizeof(v.ss), sa);
3238 3249
3239 s = pserialize_read_enter(); 3250 s = pserialize_read_enter();
3240 IFADDR_READER_FOREACH(ifa, ifp) { 3251 IFADDR_READER_FOREACH(ifa, ifp) {
3241 if (ifa->ifa_addr->sa_family != sa->sa_family) 3252 if (ifa->ifa_addr->sa_family != sa->sa_family)
3242 continue; 3253 continue;
3243 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr); 3254 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr);
3244 if (sockaddr_cmp(&u.sa, &v.sa) == 0) 3255 if (sockaddr_cmp(&u.sa, &v.sa) == 0)
3245 break; 3256 break;
3246 } 3257 }
3247 if (ifa == NULL) { 3258 if (ifa == NULL) {
3248 error = EADDRNOTAVAIL; 3259 error = EADDRNOTAVAIL;
3249 goto out; 3260 goto out;
3250 } 3261 }
3251 3262
3252 switch (cmd) { 3263 switch (cmd) {
3253 case SIOCSIFADDRPREF: 3264 case SIOCSIFADDRPREF:
3254 ifa->ifa_preference = ifap->ifap_preference; 3265 ifa->ifa_preference = ifap->ifap_preference;
3255 goto out; 3266 goto out;
3256 case SIOCGIFADDRPREF: 3267 case SIOCGIFADDRPREF:
3257 /* fill in the if_laddrreq structure */ 3268 /* fill in the if_laddrreq structure */
3258 (void)sockaddr_copy(sstosa(&ifap->ifap_addr), 3269 (void)sockaddr_copy(sstosa(&ifap->ifap_addr),
3259 sizeof(ifap->ifap_addr), ifa->ifa_addr); 3270 sizeof(ifap->ifap_addr), ifa->ifa_addr);
3260 ifap->ifap_preference = ifa->ifa_preference; 3271 ifap->ifap_preference = ifa->ifa_preference;
3261 goto out; 3272 goto out;
3262 default: 3273 default:
3263 error = EOPNOTSUPP; 3274 error = EOPNOTSUPP;
3264 } 3275 }
3265out: 3276out:
3266 pserialize_read_exit(s); 3277 pserialize_read_exit(s);
3267 return error; 3278 return error;
3268} 3279}
3269 3280
3270/* 3281/*
3271 * Interface ioctls. 3282 * Interface ioctls.
3272 */ 3283 */
3273static int 3284static int
3274doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) 3285doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l)
3275{ 3286{
3276 struct ifnet *ifp; 3287 struct ifnet *ifp;
3277 struct ifreq *ifr; 3288 struct ifreq *ifr;
3278 int error = 0; 3289 int error = 0;
3279 u_long ocmd = cmd; 3290 u_long ocmd = cmd;
3280 u_short oif_flags; 3291 u_short oif_flags;
3281 struct ifreq ifrb; 3292 struct ifreq ifrb;
3282 struct oifreq *oifr = NULL; 3293 struct oifreq *oifr = NULL;
3283 int r; 3294 int r;
3284 struct psref psref; 3295 struct psref psref;
3285 int bound; 3296 int bound;
3286 bool do_if43_post = false; 3297 bool do_if43_post = false;
3287 bool do_ifm80_post = false; 3298 bool do_ifm80_post = false;
3288 3299
3289 switch (cmd) { 3300 switch (cmd) {
3290 case SIOCGIFCONF: 3301 case SIOCGIFCONF:
3291 return ifconf(cmd, data); 3302 return ifconf(cmd, data);
3292 case SIOCINITIFADDR: 3303 case SIOCINITIFADDR:
3293 return EPERM; 3304 return EPERM;
3294 default: 3305 default:
3295 MODULE_HOOK_CALL(uipc_syscalls_40_hook, (cmd, data), enosys(), 3306 MODULE_HOOK_CALL(uipc_syscalls_40_hook, (cmd, data), enosys(),
3296 error); 3307 error);
3297 if (error != ENOSYS) 3308 if (error != ENOSYS)
3298 return error; 3309 return error;
3299 MODULE_HOOK_CALL(uipc_syscalls_50_hook, (l, cmd, data), 3310 MODULE_HOOK_CALL(uipc_syscalls_50_hook, (l, cmd, data),
3300 enosys(), error); 3311 enosys(), error);
3301 if (error != ENOSYS) 3312 if (error != ENOSYS)
3302 return error; 3313 return error;
3303 error = 0; 3314 error = 0;
3304 break; 3315 break;
3305 } 3316 }
3306 3317
3307 ifr = data; 3318 ifr = data;
3308 /* Pre-conversion */ 3319 /* Pre-conversion */
3309 MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), error); 3320 MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), error);
3310 if (cmd != ocmd) { 3321 if (cmd != ocmd) {
3311 oifr = data; 3322 oifr = data;
3312 data = ifr = &ifrb; 3323 data = ifr = &ifrb;
3313 IFREQO2N_43(oifr, ifr); 3324 IFREQO2N_43(oifr, ifr);
3314 do_if43_post = true; 3325 do_if43_post = true;
3315 } 3326 }
3316 MODULE_HOOK_CALL(ifmedia_80_pre_hook, (ifr, &cmd, &do_ifm80_post), 3327 MODULE_HOOK_CALL(ifmedia_80_pre_hook, (ifr, &cmd, &do_ifm80_post),
3317 enosys(), error); 3328 enosys(), error);
3318 3329
3319 switch (cmd) { 3330 switch (cmd) {
3320 case SIOCIFCREATE: 3331 case SIOCIFCREATE:
3321 case SIOCIFDESTROY: 3332 case SIOCIFDESTROY:
3322 bound = curlwp_bind(); 3333 bound = curlwp_bind();
3323 if (l != NULL) { 3334 if (l != NULL) {
3324 ifp = if_get(ifr->ifr_name, &psref); 3335 ifp = if_get(ifr->ifr_name, &psref);
3325 error = kauth_authorize_network(l->l_cred, 3336 error = kauth_authorize_network(l->l_cred,
3326 KAUTH_NETWORK_INTERFACE, 3337 KAUTH_NETWORK_INTERFACE,
3327 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, 3338 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
3328 KAUTH_ARG(cmd), NULL); 3339 KAUTH_ARG(cmd), NULL);
3329 if (ifp != NULL) 3340 if (ifp != NULL)
3330 if_put(ifp, &psref); 3341 if_put(ifp, &psref);
3331 if (error != 0) { 3342 if (error != 0) {
3332 curlwp_bindx(bound); 3343 curlwp_bindx(bound);
3333 return error; 3344 return error;
3334 } 3345 }
3335 } 3346 }
3336 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 3347 KERNEL_LOCK_UNLESS_NET_MPSAFE();
3337 mutex_enter(&if_clone_mtx); 3348 mutex_enter(&if_clone_mtx);
3338 r = (cmd == SIOCIFCREATE) ? 3349 r = (cmd == SIOCIFCREATE) ?
3339 if_clone_create(ifr->ifr_name) : 3350 if_clone_create(ifr->ifr_name) :
3340 if_clone_destroy(ifr->ifr_name); 3351 if_clone_destroy(ifr->ifr_name);
3341 mutex_exit(&if_clone_mtx); 3352 mutex_exit(&if_clone_mtx);
3342 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 3353 KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
3343 curlwp_bindx(bound); 3354 curlwp_bindx(bound);
3344 return r; 3355 return r;
3345 3356
3346 case SIOCIFGCLONERS: 3357 case SIOCIFGCLONERS:
3347 { 3358 {
3348 struct if_clonereq *req = (struct if_clonereq *)data; 3359 struct if_clonereq *req = (struct if_clonereq *)data;
3349 return if_clone_list(req->ifcr_count, req->ifcr_buffer, 3360 return if_clone_list(req->ifcr_count, req->ifcr_buffer,
3350 &req->ifcr_total); 3361 &req->ifcr_total);
3351 } 3362 }
3352 } 3363 }
3353 3364
3354 bound = curlwp_bind(); 3365 bound = curlwp_bind();
3355 ifp = if_get(ifr->ifr_name, &psref); 3366 ifp = if_get(ifr->ifr_name, &psref);
3356 if (ifp == NULL) { 3367 if (ifp == NULL) {
3357 curlwp_bindx(bound); 3368 curlwp_bindx(bound);
3358 return ENXIO; 3369 return ENXIO;
3359 } 3370 }
3360 3371
3361 switch (cmd) { 3372 switch (cmd) {
3362 case SIOCALIFADDR: 3373 case SIOCALIFADDR:
3363 case SIOCDLIFADDR: 3374 case SIOCDLIFADDR:
3364 case SIOCSIFADDRPREF: 3375 case SIOCSIFADDRPREF:
3365 case SIOCSIFFLAGS: 3376 case SIOCSIFFLAGS:
3366 case SIOCSIFCAP: 3377 case SIOCSIFCAP:
3367 case SIOCSIFMETRIC: 3378 case SIOCSIFMETRIC:
3368 case SIOCZIFDATA: 3379 case SIOCZIFDATA:
3369 case SIOCSIFMTU: 3380 case SIOCSIFMTU:
3370 case SIOCSIFPHYADDR: 3381 case SIOCSIFPHYADDR:
3371 case SIOCDIFPHYADDR: 3382 case SIOCDIFPHYADDR:
3372#ifdef INET6 3383#ifdef INET6
3373 case SIOCSIFPHYADDR_IN6: 3384 case SIOCSIFPHYADDR_IN6:
3374#endif 3385#endif
3375 case SIOCSLIFPHYADDR: 3386 case SIOCSLIFPHYADDR:
3376 case SIOCADDMULTI: 3387 case SIOCADDMULTI:
3377 case SIOCDELMULTI: 3388 case SIOCDELMULTI:
3378 case SIOCSETHERCAP: 3389 case SIOCSETHERCAP:
3379 case SIOCSIFMEDIA: 3390 case SIOCSIFMEDIA:
3380 case SIOCSDRVSPEC: 3391 case SIOCSDRVSPEC:
3381 case SIOCG80211: 3392 case SIOCG80211:
3382 case SIOCS80211: 3393 case SIOCS80211:
3383 case SIOCS80211NWID: 3394 case SIOCS80211NWID:
3384 case SIOCS80211NWKEY: 3395 case SIOCS80211NWKEY:
3385 case SIOCS80211POWER: 3396 case SIOCS80211POWER:
3386 case SIOCS80211BSSID: 3397 case SIOCS80211BSSID:
3387 case SIOCS80211CHANNEL: 3398 case SIOCS80211CHANNEL:
3388 case SIOCSLINKSTR: 3399 case SIOCSLINKSTR:
3389 if (l != NULL) { 3400 if (l != NULL) {
3390 error = kauth_authorize_network(l->l_cred, 3401 error = kauth_authorize_network(l->l_cred,
3391 KAUTH_NETWORK_INTERFACE, 3402 KAUTH_NETWORK_INTERFACE,
3392 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, 3403 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
3393 KAUTH_ARG(cmd), NULL); 3404 KAUTH_ARG(cmd), NULL);
3394 if (error != 0) 3405 if (error != 0)
3395 goto out; 3406 goto out;
3396 } 3407 }
3397 } 3408 }
3398 3409
3399 oif_flags = ifp->if_flags; 3410 oif_flags = ifp->if_flags;
3400 3411
3401 KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp); 3412 KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp);
3402 IFNET_LOCK(ifp); 3413 IFNET_LOCK(ifp);
3403 3414
3404 error = (*ifp->if_ioctl)(ifp, cmd, data); 3415 error = (*ifp->if_ioctl)(ifp, cmd, data);
3405 if (error != ENOTTY) 3416 if (error != ENOTTY)
3406 ; 3417 ;
3407 else if (so->so_proto == NULL) 3418 else if (so->so_proto == NULL)
3408 error = EOPNOTSUPP; 3419 error = EOPNOTSUPP;
3409 else { 3420 else {
3410 KERNEL_LOCK_IF_IFP_MPSAFE(ifp); 3421 KERNEL_LOCK_IF_IFP_MPSAFE(ifp);
3411 MODULE_HOOK_CALL(if_ifioctl_43_hook, 3422 MODULE_HOOK_CALL(if_ifioctl_43_hook,
3412 (so, ocmd, cmd, data, l), enosys(), error); 3423 (so, ocmd, cmd, data, l), enosys(), error);
3413 if (error == ENOSYS) 3424 if (error == ENOSYS)
3414 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so, 3425 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so,
3415 cmd, data, ifp); 3426 cmd, data, ifp);
3416 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp); 3427 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp);
3417 } 3428 }
3418 3429
3419 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) { 3430 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) {
3420 if ((ifp->if_flags & IFF_UP) != 0) { 3431 if ((ifp->if_flags & IFF_UP) != 0) {
3421 int s = splsoftnet(); 3432 int s = splsoftnet();
3422 if_up_locked(ifp); 3433 if_up_locked(ifp);
3423 splx(s); 3434 splx(s);
3424 } 3435 }
3425 } 3436 }
3426 3437
3427 /* Post-conversion */ 3438 /* Post-conversion */
3428 if (do_ifm80_post && (error == 0)) 3439 if (do_ifm80_post && (error == 0))
3429 MODULE_HOOK_CALL(ifmedia_80_post_hook, (ifr, cmd), 3440 MODULE_HOOK_CALL(ifmedia_80_post_hook, (ifr, cmd),
3430 enosys(), error); 3441 enosys(), error);
3431 if (do_if43_post) 3442 if (do_if43_post)
3432 IFREQN2O_43(oifr, ifr); 3443 IFREQN2O_43(oifr, ifr);
3433 3444
3434 IFNET_UNLOCK(ifp); 3445 IFNET_UNLOCK(ifp);
3435 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp); 3446 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp);
3436out: 3447out:
3437 if_put(ifp, &psref); 3448 if_put(ifp, &psref);
3438 curlwp_bindx(bound); 3449 curlwp_bindx(bound);
3439 return error; 3450 return error;
3440} 3451}
3441 3452
3442/* 3453/*
3443 * Return interface configuration 3454 * Return interface configuration
3444 * of system. List may be used 3455 * of system. List may be used
3445 * in later ioctl's (above) to get 3456 * in later ioctl's (above) to get
3446 * other information. 3457 * other information.
3447 * 3458 *
3448 * Each record is a struct ifreq. Before the addition of 3459 * Each record is a struct ifreq. Before the addition of
3449 * sockaddr_storage, the API rule was that sockaddr flavors that did 3460 * sockaddr_storage, the API rule was that sockaddr flavors that did
3450 * not fit would extend beyond the struct ifreq, with the next struct 3461 * not fit would extend beyond the struct ifreq, with the next struct
3451 * ifreq starting sa_len beyond the struct sockaddr. Because the 3462 * ifreq starting sa_len beyond the struct sockaddr. Because the
3452 * union in struct ifreq includes struct sockaddr_storage, every kind 3463 * union in struct ifreq includes struct sockaddr_storage, every kind
3453 * of sockaddr must fit. Thus, there are no longer any overlength 3464 * of sockaddr must fit. Thus, there are no longer any overlength
3454 * records. 3465 * records.
3455 * 3466 *
3456 * Records are added to the user buffer if they fit, and ifc_len is 3467 * Records are added to the user buffer if they fit, and ifc_len is
3457 * adjusted to the length that was written. Thus, the user is only 3468 * adjusted to the length that was written. Thus, the user is only
3458 * assured of getting the complete list if ifc_len on return is at 3469 * assured of getting the complete list if ifc_len on return is at
3459 * least sizeof(struct ifreq) less than it was on entry. 3470 * least sizeof(struct ifreq) less than it was on entry.
3460 * 3471 *
3461 * If the user buffer pointer is NULL, this routine copies no data and 3472 * If the user buffer pointer is NULL, this routine copies no data and
3462 * returns the amount of space that would be needed. 3473 * returns the amount of space that would be needed.
3463 * 3474 *
3464 * Invariants: 3475 * Invariants:
3465 * ifrp points to the next part of the user's buffer to be used. If 3476 * ifrp points to the next part of the user's buffer to be used. If
3466 * ifrp != NULL, space holds the number of bytes remaining that we may 3477 * ifrp != NULL, space holds the number of bytes remaining that we may
3467 * write at ifrp. Otherwise, space holds the number of bytes that 3478 * write at ifrp. Otherwise, space holds the number of bytes that
3468 * would have been written had there been adequate space. 3479 * would have been written had there been adequate space.
3469 */ 3480 */
3470/*ARGSUSED*/ 3481/*ARGSUSED*/
3471static int 3482static int
3472ifconf(u_long cmd, void *data) 3483ifconf(u_long cmd, void *data)
3473{ 3484{
3474 struct ifconf *ifc = (struct ifconf *)data; 3485 struct ifconf *ifc = (struct ifconf *)data;
3475 struct ifnet *ifp; 3486 struct ifnet *ifp;
3476 struct ifaddr *ifa; 3487 struct ifaddr *ifa;
3477 struct ifreq ifr, *ifrp = NULL; 3488 struct ifreq ifr, *ifrp = NULL;
3478 int space = 0, error = 0; 3489 int space = 0, error = 0;
3479 const int sz = (int)sizeof(struct ifreq); 3490 const int sz = (int)sizeof(struct ifreq);
3480 const bool docopy = ifc->ifc_req != NULL; 3491 const bool docopy = ifc->ifc_req != NULL;
3481 int s; 3492 int s;
3482 int bound; 3493 int bound;
3483 struct psref psref; 3494 struct psref psref;
3484 3495
3485 memset(&ifr, 0, sizeof(ifr)); 3496 memset(&ifr, 0, sizeof(ifr));
3486 if (docopy) { 3497 if (docopy) {
3487 space = ifc->ifc_len; 3498 space = ifc->ifc_len;
3488 ifrp = ifc->ifc_req; 3499 ifrp = ifc->ifc_req;
3489 } 3500 }
3490 3501
3491 bound = curlwp_bind(); 3502 bound = curlwp_bind();
3492 s = pserialize_read_enter(); 3503 s = pserialize_read_enter();
3493 IFNET_READER_FOREACH(ifp) { 3504 IFNET_READER_FOREACH(ifp) {
3494 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class); 3505 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class);
3495 pserialize_read_exit(s); 3506 pserialize_read_exit(s);
3496 3507
3497 (void)strncpy(ifr.ifr_name, ifp->if_xname, 3508 (void)strncpy(ifr.ifr_name, ifp->if_xname,
3498 sizeof(ifr.ifr_name)); 3509 sizeof(ifr.ifr_name));
3499 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') { 3510 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
3500 error = ENAMETOOLONG; 3511 error = ENAMETOOLONG;
3501 goto release_exit; 3512 goto release_exit;
3502 } 3513 }
3503 if (IFADDR_READER_EMPTY(ifp)) { 3514 if (IFADDR_READER_EMPTY(ifp)) {
3504 /* Interface with no addresses - send zero sockaddr. */ 3515 /* Interface with no addresses - send zero sockaddr. */
3505 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 3516 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
3506 if (!docopy) { 3517 if (!docopy) {
3507 space += sz; 3518 space += sz;
3508 goto next; 3519 goto next;
3509 } 3520 }
3510 if (space >= sz) { 3521 if (space >= sz) {
3511 error = copyout(&ifr, ifrp, sz); 3522 error = copyout(&ifr, ifrp, sz);
3512 if (error != 0) 3523 if (error != 0)
3513 goto release_exit; 3524 goto release_exit;
3514 ifrp++; 3525 ifrp++;
3515 space -= sz; 3526 space -= sz;
3516 } 3527 }
3517 } 3528 }
3518 3529
3519 s = pserialize_read_enter(); 3530 s = pserialize_read_enter();
3520 IFADDR_READER_FOREACH(ifa, ifp) { 3531 IFADDR_READER_FOREACH(ifa, ifp) {
3521 struct sockaddr *sa = ifa->ifa_addr; 3532 struct sockaddr *sa = ifa->ifa_addr;
3522 /* all sockaddrs must fit in sockaddr_storage */ 3533 /* all sockaddrs must fit in sockaddr_storage */
3523 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru)); 3534 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru));
3524 3535
3525 if (!docopy) { 3536 if (!docopy) {