]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
7ddcb079 | 2 | * Copyright (c) 1999-2012 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b | 27 | */ |
2d21ac55 A |
28 | /* |
29 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
30 | * support for mandatory and extensible security protections. This notice | |
31 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
32 | * Version 2.0. | |
33 | */ | |
1c79356b | 34 | |
1c79356b A |
35 | #include <sys/param.h> |
36 | #include <sys/systm.h> | |
37 | #include <sys/kernel.h> | |
38 | #include <sys/malloc.h> | |
39 | #include <sys/mbuf.h> | |
40 | #include <sys/socket.h> | |
91447636 A |
41 | #include <sys/domain.h> |
42 | #include <sys/user.h> | |
2d21ac55 | 43 | #include <sys/random.h> |
1c79356b A |
44 | #include <net/if_dl.h> |
45 | #include <net/if.h> | |
91447636 | 46 | #include <net/route.h> |
1c79356b A |
47 | #include <net/if_var.h> |
48 | #include <net/dlil.h> | |
91447636 | 49 | #include <net/if_arp.h> |
1c79356b A |
50 | #include <sys/kern_event.h> |
51 | #include <sys/kdebug.h> | |
6d2010ae | 52 | #include <sys/mcache.h> |
1c79356b | 53 | |
91447636 | 54 | #include <kern/assert.h> |
1c79356b | 55 | #include <kern/task.h> |
9bccf70c A |
56 | #include <kern/thread.h> |
57 | #include <kern/sched_prim.h> | |
91447636 | 58 | #include <kern/locks.h> |
6d2010ae | 59 | #include <kern/zalloc.h> |
2d21ac55 | 60 | #include <net/kpi_protocol.h> |
9bccf70c | 61 | |
1c79356b | 62 | #include <net/if_types.h> |
6d2010ae | 63 | #include <net/if_llreach.h> |
91447636 A |
64 | #include <net/kpi_interfacefilter.h> |
65 | ||
6d2010ae A |
66 | #if INET |
67 | #include <netinet/in_var.h> | |
68 | #include <netinet/igmp_var.h> | |
69 | #endif /* INET */ | |
70 | ||
71 | #if INET6 | |
72 | #include <netinet6/in6_var.h> | |
73 | #include <netinet6/nd6.h> | |
74 | #include <netinet6/mld6_var.h> | |
75 | #endif /* INET6 */ | |
76 | ||
77 | #if NETAT | |
78 | #include <netat/at_var.h> | |
79 | #endif /* NETAT */ | |
80 | ||
91447636 | 81 | #include <libkern/OSAtomic.h> |
1c79356b | 82 | |
d52fe63f | 83 | #include <machine/machine_routines.h> |
1c79356b | 84 | |
2d21ac55 | 85 | #include <mach/thread_act.h> |
6d2010ae | 86 | #include <mach/sdt.h> |
2d21ac55 A |
87 | |
88 | #if CONFIG_MACF_NET | |
89 | #include <security/mac_framework.h> | |
90 | #endif /* MAC_NET */ | |
91 | ||
b0d623f7 A |
92 | #if PF |
93 | #include <net/pfvar.h> | |
94 | #endif /* PF */ | |
95 | ||
6d2010ae A |
96 | #define DBG_LAYER_BEG DLILDBG_CODE(DBG_DLIL_STATIC, 0) |
97 | #define DBG_LAYER_END DLILDBG_CODE(DBG_DLIL_STATIC, 2) | |
1c79356b A |
98 | #define DBG_FNC_DLIL_INPUT DLILDBG_CODE(DBG_DLIL_STATIC, (1 << 8)) |
99 | #define DBG_FNC_DLIL_OUTPUT DLILDBG_CODE(DBG_DLIL_STATIC, (2 << 8)) | |
100 | #define DBG_FNC_DLIL_IFOUT DLILDBG_CODE(DBG_DLIL_STATIC, (3 << 8)) | |
101 | ||
102 | ||
1c79356b A |
103 | #define MAX_FRAME_TYPE_SIZE 4 /* LONGWORDS */ |
104 | #define MAX_LINKADDR 4 /* LONGWORDS */ | |
105 | #define M_NKE M_IFADDR | |
106 | ||
2d21ac55 | 107 | #if 1 |
91447636 A |
108 | #define DLIL_PRINTF printf |
109 | #else | |
110 | #define DLIL_PRINTF kprintf | |
111 | #endif | |
112 | ||
d1ecb069 A |
113 | #define _CASSERT(x) \ |
114 | switch (0) { case 0: case (x): ; } | |
115 | ||
6d2010ae A |
116 | #define IF_DATA_REQUIRE_ALIGNED_64(f) \ |
117 | _CASSERT(!(offsetof(struct if_data_internal, f) % sizeof (u_int64_t))) | |
d1ecb069 | 118 | |
6d2010ae A |
119 | #define IFNET_IF_DATA_REQUIRE_ALIGNED_64(f) \ |
120 | _CASSERT(!(offsetof(struct ifnet, if_data.f) % sizeof (u_int64_t))) | |
121 | ||
122 | #define IFNET_IF_TC_REQUIRE_ALIGNED_64(f) \ | |
123 | _CASSERT(!(offsetof(struct ifnet, if_tc.f) % sizeof (u_int64_t))) | |
2d21ac55 | 124 | |
91447636 | 125 | enum { |
2d21ac55 A |
126 | kProtoKPI_v1 = 1, |
127 | kProtoKPI_v2 = 2 | |
91447636 A |
128 | }; |
129 | ||
6d2010ae A |
130 | /* |
131 | * List of if_proto structures in if_proto_hash[] is protected by | |
132 | * the ifnet lock. The rest of the fields are initialized at protocol | |
133 | * attach time and never change, thus no lock required as long as | |
134 | * a reference to it is valid, via if_proto_ref(). | |
135 | */ | |
91447636 | 136 | struct if_proto { |
6d2010ae A |
137 | SLIST_ENTRY(if_proto) next_hash; |
138 | u_int32_t refcount; | |
139 | u_int32_t detached; | |
140 | struct ifnet *ifp; | |
91447636 | 141 | protocol_family_t protocol_family; |
6d2010ae | 142 | int proto_kpi; |
91447636 | 143 | union { |
91447636 | 144 | struct { |
6d2010ae A |
145 | proto_media_input input; |
146 | proto_media_preout pre_output; | |
147 | proto_media_event event; | |
148 | proto_media_ioctl ioctl; | |
91447636 A |
149 | proto_media_detached detached; |
150 | proto_media_resolve_multi resolve_multi; | |
151 | proto_media_send_arp send_arp; | |
152 | } v1; | |
2d21ac55 A |
153 | struct { |
154 | proto_media_input_v2 input; | |
6d2010ae A |
155 | proto_media_preout pre_output; |
156 | proto_media_event event; | |
157 | proto_media_ioctl ioctl; | |
2d21ac55 A |
158 | proto_media_detached detached; |
159 | proto_media_resolve_multi resolve_multi; | |
160 | proto_media_send_arp send_arp; | |
161 | } v2; | |
91447636 | 162 | } kpi; |
1c79356b A |
163 | }; |
164 | ||
91447636 A |
165 | SLIST_HEAD(proto_hash_entry, if_proto); |
166 | ||
6d2010ae A |
167 | #define DLIL_SDLMAXLEN 64 |
168 | #define DLIL_SDLDATALEN \ | |
169 | (DLIL_SDLMAXLEN - offsetof(struct sockaddr_dl, sdl_data[0])) | |
1c79356b | 170 | |
9bccf70c | 171 | struct dlil_ifnet { |
6d2010ae A |
172 | struct ifnet dl_if; /* public ifnet */ |
173 | /* | |
174 | * dlil private fields, protected by dl_if_lock | |
175 | */ | |
176 | decl_lck_mtx_data(, dl_if_lock); | |
177 | TAILQ_ENTRY(dlil_ifnet) dl_if_link; /* dlil_ifnet link */ | |
178 | u_int32_t dl_if_flags; /* flags (below) */ | |
179 | u_int32_t dl_if_refcnt; /* refcnt */ | |
180 | void (*dl_if_trace)(struct dlil_ifnet *, int); /* ref trace callback */ | |
181 | void *dl_if_uniqueid; /* unique interface id */ | |
182 | size_t dl_if_uniqueid_len; /* length of the unique id */ | |
183 | char dl_if_namestorage[IFNAMSIZ]; /* interface name storage */ | |
184 | struct { | |
185 | struct ifaddr ifa; /* lladdr ifa */ | |
186 | u_int8_t asdl[DLIL_SDLMAXLEN]; /* addr storage */ | |
187 | u_int8_t msdl[DLIL_SDLMAXLEN]; /* mask storage */ | |
188 | } dl_if_lladdr; | |
189 | ctrace_t dl_if_attach; /* attach PC stacktrace */ | |
190 | ctrace_t dl_if_detach; /* detach PC stacktrace */ | |
191 | }; | |
192 | ||
193 | /* Values for dl_if_flags (private to DLIL) */ | |
194 | #define DLIF_INUSE 0x1 /* DLIL ifnet recycler, ifnet in use */ | |
195 | #define DLIF_REUSE 0x2 /* DLIL ifnet recycles, ifnet is not new */ | |
196 | #define DLIF_DEBUG 0x4 /* has debugging info */ | |
197 | ||
198 | #define IF_REF_TRACE_HIST_SIZE 8 /* size of ref trace history */ | |
199 | ||
200 | /* For gdb */ | |
201 | __private_extern__ unsigned int if_ref_trace_hist_size = IF_REF_TRACE_HIST_SIZE; | |
202 | ||
203 | struct dlil_ifnet_dbg { | |
204 | struct dlil_ifnet dldbg_dlif; /* dlil_ifnet */ | |
205 | u_int16_t dldbg_if_refhold_cnt; /* # ifnet references */ | |
206 | u_int16_t dldbg_if_refrele_cnt; /* # ifnet releases */ | |
207 | /* | |
208 | * Circular lists of ifnet_{reference,release} callers. | |
209 | */ | |
210 | ctrace_t dldbg_if_refhold[IF_REF_TRACE_HIST_SIZE]; | |
211 | ctrace_t dldbg_if_refrele[IF_REF_TRACE_HIST_SIZE]; | |
1c79356b A |
212 | }; |
213 | ||
6d2010ae A |
214 | #define DLIL_TO_IFP(s) (&s->dl_if) |
215 | #define IFP_TO_DLIL(s) ((struct dlil_ifnet *)s) | |
216 | ||
91447636 A |
217 | struct ifnet_filter { |
218 | TAILQ_ENTRY(ifnet_filter) filt_next; | |
6d2010ae A |
219 | u_int32_t filt_skip; |
220 | ifnet_t filt_ifp; | |
221 | const char *filt_name; | |
222 | void *filt_cookie; | |
223 | protocol_family_t filt_protocol; | |
224 | iff_input_func filt_input; | |
225 | iff_output_func filt_output; | |
226 | iff_event_func filt_event; | |
227 | iff_ioctl_func filt_ioctl; | |
228 | iff_detached_func filt_detached; | |
1c79356b A |
229 | }; |
230 | ||
2d21ac55 | 231 | struct proto_input_entry; |
55e303ae | 232 | |
91447636 | 233 | static TAILQ_HEAD(, dlil_ifnet) dlil_ifnet_head; |
91447636 | 234 | static lck_grp_t *dlil_lock_group; |
6d2010ae | 235 | lck_grp_t *ifnet_lock_group; |
91447636 | 236 | static lck_grp_t *ifnet_head_lock_group; |
6d2010ae | 237 | lck_attr_t *ifnet_lock_attr; |
7ddcb079 A |
238 | decl_lck_rw_data(static, ifnet_head_lock); |
239 | decl_lck_mtx_data(static, dlil_ifnet_lock); | |
b0d623f7 | 240 | u_int32_t dlil_filter_count = 0; |
91447636 | 241 | extern u_int32_t ipv4_ll_arp_aware; |
1c79356b | 242 | |
6d2010ae A |
243 | #if DEBUG |
244 | static unsigned int ifnet_debug = 1; /* debugging (enabled) */ | |
245 | #else | |
246 | static unsigned int ifnet_debug; /* debugging (disabled) */ | |
247 | #endif /* !DEBUG */ | |
248 | static unsigned int dlif_size; /* size of dlil_ifnet to allocate */ | |
249 | static unsigned int dlif_bufsize; /* size of dlif_size + headroom */ | |
250 | static struct zone *dlif_zone; /* zone for dlil_ifnet */ | |
251 | ||
252 | #define DLIF_ZONE_MAX 64 /* maximum elements in zone */ | |
253 | #define DLIF_ZONE_NAME "ifnet" /* zone name */ | |
254 | ||
255 | static unsigned int dlif_filt_size; /* size of ifnet_filter */ | |
256 | static struct zone *dlif_filt_zone; /* zone for ifnet_filter */ | |
257 | ||
258 | #define DLIF_FILT_ZONE_MAX 8 /* maximum elements in zone */ | |
259 | #define DLIF_FILT_ZONE_NAME "ifnet_filter" /* zone name */ | |
260 | ||
261 | static unsigned int dlif_inp_size; /* size of dlil_threading_info */ | |
262 | static struct zone *dlif_inp_zone; /* zone for dlil_threading_info */ | |
263 | ||
264 | #define DLIF_INP_ZONE_MAX DLIF_ZONE_MAX /* maximum elements in zone */ | |
265 | #define DLIF_INP_ZONE_NAME "ifnet_thread" /* zone name */ | |
266 | ||
267 | static unsigned int dlif_phash_size; /* size of ifnet proto hash table */ | |
268 | static struct zone *dlif_phash_zone; /* zone for ifnet proto hash table */ | |
269 | ||
270 | #define DLIF_PHASH_ZONE_MAX DLIF_ZONE_MAX /* maximum elements in zone */ | |
271 | #define DLIF_PHASH_ZONE_NAME "ifnet_proto_hash" /* zone name */ | |
272 | ||
273 | static unsigned int dlif_proto_size; /* size of if_proto */ | |
274 | static struct zone *dlif_proto_zone; /* zone for if_proto */ | |
275 | ||
276 | #define DLIF_PROTO_ZONE_MAX (DLIF_ZONE_MAX*2) /* maximum elements in zone */ | |
277 | #define DLIF_PROTO_ZONE_NAME "ifnet_proto" /* zone name */ | |
278 | ||
d1ecb069 A |
279 | /* |
280 | * Updating this variable should be done by first acquiring the global | |
281 | * radix node head (rnh_lock), in tandem with settting/clearing the | |
282 | * PR_AGGDRAIN for routedomain. | |
283 | */ | |
284 | u_int32_t ifnet_aggressive_drainers; | |
285 | static u_int32_t net_rtref; | |
d1ecb069 | 286 | |
2d21ac55 A |
287 | static struct dlil_threading_info dlil_lo_thread; |
288 | __private_extern__ struct dlil_threading_info *dlil_lo_thread_ptr = &dlil_lo_thread; | |
289 | ||
290 | static struct mbuf *dlil_lo_input_mbuf_head = NULL; | |
291 | static struct mbuf *dlil_lo_input_mbuf_tail = NULL; | |
292 | ||
91447636 | 293 | static int dlil_event_internal(struct ifnet *ifp, struct kev_msg *msg); |
91447636 | 294 | static int dlil_detach_filter_internal(interface_filter_t filter, int detached); |
6d2010ae A |
295 | static void dlil_if_trace(struct dlil_ifnet *, int); |
296 | static void if_proto_ref(struct if_proto *); | |
297 | static void if_proto_free(struct if_proto *); | |
298 | static struct if_proto *find_attached_proto(struct ifnet *, u_int32_t); | |
299 | static int dlil_ifp_proto_count(struct ifnet *); | |
300 | static void if_flt_monitor_busy(struct ifnet *); | |
301 | static void if_flt_monitor_unbusy(struct ifnet *); | |
302 | static void if_flt_monitor_enter(struct ifnet *); | |
303 | static void if_flt_monitor_leave(struct ifnet *); | |
304 | static int dlil_interface_filters_input(struct ifnet *, struct mbuf **, | |
305 | char **, protocol_family_t); | |
306 | static int dlil_interface_filters_output(struct ifnet *, struct mbuf **, | |
307 | protocol_family_t); | |
308 | static struct ifaddr *dlil_alloc_lladdr(struct ifnet *, | |
309 | const struct sockaddr_dl *); | |
310 | static int ifnet_lookup(struct ifnet *); | |
311 | static void if_purgeaddrs(struct ifnet *); | |
312 | ||
313 | static errno_t ifproto_media_input_v1(struct ifnet *, protocol_family_t, | |
314 | struct mbuf *, char *); | |
315 | static errno_t ifproto_media_input_v2(struct ifnet *, protocol_family_t, | |
316 | struct mbuf *); | |
317 | static errno_t ifproto_media_preout(struct ifnet *, protocol_family_t, | |
318 | mbuf_t *, const struct sockaddr *, void *, char *, char *); | |
319 | static void ifproto_media_event(struct ifnet *, protocol_family_t, | |
320 | const struct kev_msg *); | |
321 | static errno_t ifproto_media_ioctl(struct ifnet *, protocol_family_t, | |
322 | unsigned long, void *); | |
323 | static errno_t ifproto_media_resolve_multi(ifnet_t, const struct sockaddr *, | |
324 | struct sockaddr_dl *, size_t); | |
325 | static errno_t ifproto_media_send_arp(struct ifnet *, u_short, | |
326 | const struct sockaddr_dl *, const struct sockaddr *, | |
327 | const struct sockaddr_dl *, const struct sockaddr *); | |
328 | ||
329 | static errno_t ifp_if_output(struct ifnet *, struct mbuf *); | |
330 | static errno_t ifp_if_demux(struct ifnet *, struct mbuf *, char *, | |
331 | protocol_family_t *); | |
332 | static errno_t ifp_if_add_proto(struct ifnet *, protocol_family_t, | |
333 | const struct ifnet_demux_desc *, u_int32_t); | |
334 | static errno_t ifp_if_del_proto(struct ifnet *, protocol_family_t); | |
335 | static errno_t ifp_if_check_multi(struct ifnet *, const struct sockaddr *); | |
336 | static errno_t ifp_if_framer(struct ifnet *, struct mbuf **, | |
337 | const struct sockaddr *, const char *, const char *); | |
338 | static errno_t ifp_if_ioctl(struct ifnet *, unsigned long, void *); | |
339 | static errno_t ifp_if_set_bpf_tap(struct ifnet *, bpf_tap_mode, bpf_packet_func); | |
340 | static void ifp_if_free(struct ifnet *); | |
341 | static void ifp_if_event(struct ifnet *, const struct kev_msg *); | |
342 | ||
343 | static void dlil_input_thread_func(struct dlil_threading_info *inpthread); | |
344 | static int dlil_create_input_thread(ifnet_t, struct dlil_threading_info *); | |
345 | ||
346 | static void ifnet_delayed_thread_func(void); | |
347 | static void ifnet_detach_final(struct ifnet *); | |
348 | static void ifnet_detaching_enqueue(struct ifnet *); | |
349 | static struct ifnet *ifnet_detaching_dequeue(void); | |
350 | ||
351 | static void ifp_src_route_copyout(struct ifnet *, struct route *); | |
352 | static void ifp_src_route_copyin(struct ifnet *, struct route *); | |
353 | #if INET6 | |
354 | static void ifp_src_route6_copyout(struct ifnet *, struct route_in6 *); | |
355 | static void ifp_src_route6_copyin(struct ifnet *, struct route_in6 *); | |
356 | #endif /* INET6 */ | |
357 | ||
358 | /* The following are protected by dlil_ifnet_lock */ | |
359 | static TAILQ_HEAD(, ifnet) ifnet_detaching_head; | |
360 | static u_int32_t ifnet_detaching_cnt; | |
361 | static void *ifnet_delayed_run; /* wait channel for detaching thread */ | |
362 | ||
363 | extern void bpfdetach(struct ifnet*); | |
364 | extern void proto_input_run(void); | |
91447636 | 365 | |
6d2010ae | 366 | __private_extern__ void link_rtrequest(int, struct rtentry *, struct sockaddr *); |
91447636 | 367 | |
b0d623f7 | 368 | #if DEBUG |
6d2010ae | 369 | static int dlil_verbose = 1; |
b0d623f7 | 370 | #else |
6d2010ae | 371 | static int dlil_verbose = 0; |
b0d623f7 | 372 | #endif /* DEBUG */ |
6d2010ae A |
373 | static int dlil_multithreaded_input = 1; |
374 | static int cur_dlil_input_threads = 0; | |
375 | #if IFNET_INPUT_SANITY_CHK | |
376 | static int dlil_lo_input_mbuf_count = 0; | |
377 | /* sanity checking of input packet lists received */ | |
378 | static int dlil_input_sanity_check = 0; | |
379 | #endif | |
b0d623f7 | 380 | |
6d2010ae | 381 | SYSCTL_DECL(_net_link_generic_system); |
91447636 | 382 | |
6d2010ae A |
383 | SYSCTL_INT(_net_link_generic_system, OID_AUTO, dlil_verbose, CTLFLAG_RW, |
384 | &dlil_verbose, 0, "Log DLIL error messages"); | |
91447636 | 385 | |
6d2010ae A |
386 | SYSCTL_INT(_net_link_generic_system, OID_AUTO, multi_threaded_input, CTLFLAG_RW, |
387 | &dlil_multithreaded_input , 0, "Uses multiple input thread for DLIL input"); | |
91447636 | 388 | |
6d2010ae A |
389 | #if IFNET_INPUT_SANITY_CHK |
390 | SYSCTL_INT(_net_link_generic_system, OID_AUTO, dlil_input_sanity_check, | |
391 | CTLFLAG_RW, &dlil_input_sanity_check , 0, | |
392 | "Turn on sanity checking in DLIL input"); | |
393 | #endif | |
1c79356b | 394 | |
6d2010ae A |
395 | unsigned int net_affinity = 1; |
396 | static kern_return_t dlil_affinity_set(struct thread *, u_int32_t); | |
1c79356b | 397 | |
b36670ce A |
398 | extern u_int32_t inject_buckets; |
399 | ||
2d21ac55 A |
400 | static lck_grp_attr_t *dlil_grp_attributes = NULL; |
401 | static lck_attr_t *dlil_lck_attributes = NULL; | |
402 | static lck_grp_t *dlil_input_lock_grp = NULL; | |
91447636 | 403 | |
91447636 A |
404 | #define PROTO_HASH_SLOTS 0x5 |
405 | ||
1c79356b A |
406 | /* |
407 | * Internal functions. | |
408 | */ | |
409 | ||
91447636 | 410 | static int |
b0d623f7 | 411 | proto_hash_value(u_int32_t protocol_family) |
91447636 | 412 | { |
4a3eedf9 A |
413 | /* |
414 | * dlil_proto_unplumb_all() depends on the mapping between | |
415 | * the hash bucket index and the protocol family defined | |
416 | * here; future changes must be applied there as well. | |
417 | */ | |
91447636 A |
418 | switch(protocol_family) { |
419 | case PF_INET: | |
6d2010ae | 420 | return (0); |
91447636 | 421 | case PF_INET6: |
6d2010ae | 422 | return (1); |
91447636 | 423 | case PF_APPLETALK: |
6d2010ae | 424 | return (2); |
91447636 | 425 | case PF_VLAN: |
6d2010ae A |
426 | return (3); |
427 | case PF_UNSPEC: | |
91447636 | 428 | default: |
6d2010ae | 429 | return (4); |
91447636 A |
430 | } |
431 | } | |
432 | ||
6d2010ae A |
433 | /* |
434 | * Caller must already be holding ifnet lock. | |
435 | */ | |
436 | static struct if_proto * | |
b0d623f7 | 437 | find_attached_proto(struct ifnet *ifp, u_int32_t protocol_family) |
1c79356b | 438 | { |
91447636 | 439 | struct if_proto *proto = NULL; |
b0d623f7 | 440 | u_int32_t i = proto_hash_value(protocol_family); |
6d2010ae A |
441 | |
442 | ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_OWNED); | |
443 | ||
444 | if (ifp->if_proto_hash != NULL) | |
91447636 | 445 | proto = SLIST_FIRST(&ifp->if_proto_hash[i]); |
6d2010ae A |
446 | |
447 | while (proto != NULL && proto->protocol_family != protocol_family) | |
91447636 | 448 | proto = SLIST_NEXT(proto, next_hash); |
6d2010ae A |
449 | |
450 | if (proto != NULL) | |
451 | if_proto_ref(proto); | |
452 | ||
453 | return (proto); | |
1c79356b A |
454 | } |
455 | ||
91447636 A |
456 | static void |
457 | if_proto_ref(struct if_proto *proto) | |
1c79356b | 458 | { |
6d2010ae | 459 | atomic_add_32(&proto->refcount, 1); |
1c79356b A |
460 | } |
461 | ||
6d2010ae A |
462 | extern void if_rtproto_del(struct ifnet *ifp, int protocol); |
463 | ||
91447636 A |
464 | static void |
465 | if_proto_free(struct if_proto *proto) | |
0b4e3aa0 | 466 | { |
6d2010ae A |
467 | u_int32_t oldval; |
468 | struct ifnet *ifp = proto->ifp; | |
469 | u_int32_t proto_family = proto->protocol_family; | |
470 | struct kev_dl_proto_data ev_pr_data; | |
471 | ||
472 | oldval = atomic_add_32_ov(&proto->refcount, -1); | |
473 | if (oldval > 1) | |
474 | return; | |
475 | ||
476 | /* No more reference on this, protocol must have been detached */ | |
477 | VERIFY(proto->detached); | |
478 | ||
479 | if (proto->proto_kpi == kProtoKPI_v1) { | |
480 | if (proto->kpi.v1.detached) | |
481 | proto->kpi.v1.detached(ifp, proto->protocol_family); | |
482 | } | |
483 | if (proto->proto_kpi == kProtoKPI_v2) { | |
484 | if (proto->kpi.v2.detached) | |
485 | proto->kpi.v2.detached(ifp, proto->protocol_family); | |
91447636 | 486 | } |
6d2010ae A |
487 | |
488 | /* | |
489 | * Cleanup routes that may still be in the routing table for that | |
490 | * interface/protocol pair. | |
491 | */ | |
492 | if_rtproto_del(ifp, proto_family); | |
493 | ||
494 | /* | |
495 | * The reserved field carries the number of protocol still attached | |
496 | * (subject to change) | |
497 | */ | |
498 | ifnet_lock_shared(ifp); | |
499 | ev_pr_data.proto_family = proto_family; | |
500 | ev_pr_data.proto_remaining_count = dlil_ifp_proto_count(ifp); | |
501 | ifnet_lock_done(ifp); | |
502 | ||
503 | dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_PROTO_DETACHED, | |
504 | (struct net_event_data *)&ev_pr_data, | |
505 | sizeof(struct kev_dl_proto_data)); | |
506 | ||
507 | zfree(dlif_proto_zone, proto); | |
0b4e3aa0 A |
508 | } |
509 | ||
91447636 | 510 | __private_extern__ void |
6d2010ae | 511 | ifnet_lock_assert(struct ifnet *ifp, ifnet_lock_assert_t what) |
1c79356b | 512 | { |
6d2010ae A |
513 | unsigned int type = 0; |
514 | int ass = 1; | |
515 | ||
516 | switch (what) { | |
517 | case IFNET_LCK_ASSERT_EXCLUSIVE: | |
518 | type = LCK_RW_ASSERT_EXCLUSIVE; | |
519 | break; | |
520 | ||
521 | case IFNET_LCK_ASSERT_SHARED: | |
522 | type = LCK_RW_ASSERT_SHARED; | |
523 | break; | |
524 | ||
525 | case IFNET_LCK_ASSERT_OWNED: | |
526 | type = LCK_RW_ASSERT_HELD; | |
527 | break; | |
528 | ||
529 | case IFNET_LCK_ASSERT_NOTOWNED: | |
530 | /* nothing to do here for RW lock; bypass assert */ | |
531 | ass = 0; | |
532 | break; | |
533 | ||
534 | default: | |
535 | panic("bad ifnet assert type: %d", what); | |
536 | /* NOTREACHED */ | |
537 | } | |
538 | if (ass) | |
539 | lck_rw_assert(&ifp->if_lock, type); | |
1c79356b A |
540 | } |
541 | ||
91447636 | 542 | __private_extern__ void |
6d2010ae | 543 | ifnet_lock_shared(struct ifnet *ifp) |
1c79356b | 544 | { |
6d2010ae | 545 | lck_rw_lock_shared(&ifp->if_lock); |
1c79356b A |
546 | } |
547 | ||
91447636 | 548 | __private_extern__ void |
6d2010ae | 549 | ifnet_lock_exclusive(struct ifnet *ifp) |
0b4e3aa0 | 550 | { |
6d2010ae | 551 | lck_rw_lock_exclusive(&ifp->if_lock); |
0b4e3aa0 A |
552 | } |
553 | ||
91447636 | 554 | __private_extern__ void |
6d2010ae | 555 | ifnet_lock_done(struct ifnet *ifp) |
1c79356b | 556 | { |
6d2010ae | 557 | lck_rw_done(&ifp->if_lock); |
1c79356b A |
558 | } |
559 | ||
91447636 | 560 | __private_extern__ void |
2d21ac55 | 561 | ifnet_head_lock_shared(void) |
1c79356b | 562 | { |
6d2010ae | 563 | lck_rw_lock_shared(&ifnet_head_lock); |
1c79356b A |
564 | } |
565 | ||
91447636 | 566 | __private_extern__ void |
2d21ac55 | 567 | ifnet_head_lock_exclusive(void) |
91447636 | 568 | { |
6d2010ae | 569 | lck_rw_lock_exclusive(&ifnet_head_lock); |
91447636 | 570 | } |
1c79356b | 571 | |
91447636 | 572 | __private_extern__ void |
2d21ac55 | 573 | ifnet_head_done(void) |
1c79356b | 574 | { |
6d2010ae | 575 | lck_rw_done(&ifnet_head_lock); |
91447636 | 576 | } |
1c79356b | 577 | |
6d2010ae A |
578 | /* |
579 | * Caller must already be holding ifnet lock. | |
580 | */ | |
581 | static int | |
582 | dlil_ifp_proto_count(struct ifnet * ifp) | |
91447636 | 583 | { |
6d2010ae A |
584 | int i, count = 0; |
585 | ||
586 | ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_OWNED); | |
587 | ||
588 | if (ifp->if_proto_hash == NULL) | |
589 | goto done; | |
590 | ||
591 | for (i = 0; i < PROTO_HASH_SLOTS; i++) { | |
592 | struct if_proto *proto; | |
593 | SLIST_FOREACH(proto, &ifp->if_proto_hash[i], next_hash) { | |
594 | count++; | |
91447636 A |
595 | } |
596 | } | |
6d2010ae A |
597 | done: |
598 | return (count); | |
91447636 | 599 | } |
1c79356b | 600 | |
91447636 | 601 | __private_extern__ void |
6d2010ae A |
602 | dlil_post_msg(struct ifnet *ifp, u_int32_t event_subclass, |
603 | u_int32_t event_code, struct net_event_data *event_data, | |
604 | u_int32_t event_data_len) | |
91447636 | 605 | { |
6d2010ae A |
606 | struct net_event_data ev_data; |
607 | struct kev_msg ev_msg; | |
608 | ||
609 | bzero(&ev_msg, sizeof (ev_msg)); | |
610 | bzero(&ev_data, sizeof (ev_data)); | |
611 | /* | |
2d21ac55 | 612 | * a net event always starts with a net_event_data structure |
91447636 A |
613 | * but the caller can generate a simple net event or |
614 | * provide a longer event structure to post | |
615 | */ | |
6d2010ae A |
616 | ev_msg.vendor_code = KEV_VENDOR_APPLE; |
617 | ev_msg.kev_class = KEV_NETWORK_CLASS; | |
618 | ev_msg.kev_subclass = event_subclass; | |
619 | ev_msg.event_code = event_code; | |
620 | ||
621 | if (event_data == NULL) { | |
91447636 A |
622 | event_data = &ev_data; |
623 | event_data_len = sizeof(struct net_event_data); | |
624 | } | |
6d2010ae | 625 | |
91447636 A |
626 | strncpy(&event_data->if_name[0], ifp->if_name, IFNAMSIZ); |
627 | event_data->if_family = ifp->if_family; | |
b0d623f7 | 628 | event_data->if_unit = (u_int32_t) ifp->if_unit; |
6d2010ae | 629 | |
91447636 | 630 | ev_msg.dv[0].data_length = event_data_len; |
6d2010ae | 631 | ev_msg.dv[0].data_ptr = event_data; |
91447636 | 632 | ev_msg.dv[1].data_length = 0; |
6d2010ae | 633 | |
91447636 | 634 | dlil_event_internal(ifp, &ev_msg); |
1c79356b A |
635 | } |
636 | ||
6d2010ae A |
637 | static int |
638 | dlil_create_input_thread(ifnet_t ifp, struct dlil_threading_info *inputthread) | |
2d21ac55 A |
639 | { |
640 | int error; | |
641 | ||
642 | bzero(inputthread, sizeof(*inputthread)); | |
6d2010ae A |
643 | /* loopback ifp may not be configured at dlil_init time. */ |
644 | if (ifp == lo_ifp) { | |
645 | (void) strlcat(inputthread->input_name, | |
646 | "dlil_input_main_thread_mtx", DLIL_THREADNAME_LEN); | |
647 | } else { | |
648 | (void) snprintf(inputthread->input_name, DLIL_THREADNAME_LEN, | |
649 | "dlil_input_%s%d_mtx", ifp->if_name, ifp->if_unit); | |
650 | } | |
2d21ac55 | 651 | |
6d2010ae A |
652 | inputthread->lck_grp = lck_grp_alloc_init(inputthread->input_name, |
653 | dlil_grp_attributes); | |
654 | lck_mtx_init(&inputthread->input_lck, inputthread->lck_grp, | |
655 | dlil_lck_attributes); | |
2d21ac55 | 656 | |
6d2010ae A |
657 | error= kernel_thread_start((thread_continue_t)dlil_input_thread_func, |
658 | inputthread, &inputthread->input_thread); | |
2d21ac55 | 659 | if (error == 0) { |
6d2010ae A |
660 | ml_thread_policy(inputthread->input_thread, MACHINE_GROUP, |
661 | (MACHINE_NETWORK_GROUP|MACHINE_NETWORK_NETISR)); | |
2d21ac55 A |
662 | /* |
663 | * Except for the loopback dlil input thread, we create | |
664 | * an affinity set so that the matching workloop thread | |
665 | * can be scheduled on the same processor set. | |
666 | */ | |
667 | if (net_affinity && inputthread != dlil_lo_thread_ptr) { | |
668 | struct thread *tp = inputthread->input_thread; | |
669 | u_int32_t tag; | |
670 | /* | |
671 | * Randomize to reduce the probability | |
672 | * of affinity tag namespace collision. | |
673 | */ | |
674 | read_random(&tag, sizeof (tag)); | |
675 | if (dlil_affinity_set(tp, tag) == KERN_SUCCESS) { | |
676 | thread_reference(tp); | |
677 | inputthread->tag = tag; | |
678 | inputthread->net_affinity = TRUE; | |
679 | } | |
680 | } | |
681 | } else { | |
6d2010ae A |
682 | panic("%s: couldn't create thread", __func__); |
683 | /* NOTREACHED */ | |
2d21ac55 | 684 | } |
b0d623f7 | 685 | OSAddAtomic(1, &cur_dlil_input_threads); |
2d21ac55 | 686 | #if DLIL_DEBUG |
6d2010ae A |
687 | printf("%s: threadinfo: %p input_thread=%p threads: cur=%d max=%d\n", |
688 | __func__, inputthread, inputthread->input_thread, | |
689 | dlil_multithreaded_input, cur_dlil_input_threads); | |
2d21ac55 | 690 | #endif |
6d2010ae | 691 | return (error); |
2d21ac55 A |
692 | } |
693 | ||
694 | static kern_return_t | |
695 | dlil_affinity_set(struct thread *tp, u_int32_t tag) | |
696 | { | |
697 | thread_affinity_policy_data_t policy; | |
698 | ||
699 | bzero(&policy, sizeof (policy)); | |
700 | policy.affinity_tag = tag; | |
701 | return (thread_policy_set(tp, THREAD_AFFINITY_POLICY, | |
702 | (thread_policy_t)&policy, THREAD_AFFINITY_POLICY_COUNT)); | |
703 | } | |
704 | ||
91447636 A |
705 | void |
706 | dlil_init(void) | |
707 | { | |
6d2010ae A |
708 | thread_t thread = THREAD_NULL; |
709 | ||
710 | /* | |
711 | * The following fields must be 64-bit aligned for atomic operations. | |
712 | */ | |
713 | IF_DATA_REQUIRE_ALIGNED_64(ifi_ipackets); | |
714 | IF_DATA_REQUIRE_ALIGNED_64(ifi_ierrors) | |
715 | IF_DATA_REQUIRE_ALIGNED_64(ifi_opackets); | |
716 | IF_DATA_REQUIRE_ALIGNED_64(ifi_oerrors); | |
717 | IF_DATA_REQUIRE_ALIGNED_64(ifi_collisions); | |
718 | IF_DATA_REQUIRE_ALIGNED_64(ifi_ibytes); | |
719 | IF_DATA_REQUIRE_ALIGNED_64(ifi_obytes); | |
720 | IF_DATA_REQUIRE_ALIGNED_64(ifi_imcasts); | |
721 | IF_DATA_REQUIRE_ALIGNED_64(ifi_omcasts); | |
722 | IF_DATA_REQUIRE_ALIGNED_64(ifi_iqdrops); | |
723 | IF_DATA_REQUIRE_ALIGNED_64(ifi_noproto); | |
724 | ||
725 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_ipackets); | |
726 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_ierrors) | |
727 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_opackets); | |
728 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_oerrors); | |
729 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_collisions); | |
730 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_ibytes); | |
731 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_obytes); | |
732 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_imcasts); | |
733 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_omcasts); | |
734 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_iqdrops); | |
735 | IFNET_IF_DATA_REQUIRE_ALIGNED_64(ifi_noproto); | |
736 | ||
737 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ibkpackets); | |
738 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ibkbytes); | |
739 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_obkpackets); | |
740 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_obkbytes); | |
741 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ivipackets); | |
742 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ivibytes); | |
743 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ovipackets); | |
744 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ovibytes); | |
745 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ivopackets); | |
746 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ivobytes); | |
747 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ovopackets); | |
748 | IFNET_IF_TC_REQUIRE_ALIGNED_64(ifi_ovobytes); | |
749 | ||
750 | /* | |
751 | * These IF_HWASSIST_ flags must be equal to their IFNET_* counterparts. | |
752 | */ | |
753 | _CASSERT(IF_HWASSIST_CSUM_IP == IFNET_CSUM_IP); | |
754 | _CASSERT(IF_HWASSIST_CSUM_TCP == IFNET_CSUM_TCP); | |
755 | _CASSERT(IF_HWASSIST_CSUM_UDP == IFNET_CSUM_UDP); | |
756 | _CASSERT(IF_HWASSIST_CSUM_IP_FRAGS == IFNET_CSUM_FRAGMENT); | |
757 | _CASSERT(IF_HWASSIST_CSUM_FRAGMENT == IFNET_IP_FRAGMENT); | |
758 | _CASSERT(IF_HWASSIST_CSUM_TCP_SUM16 == IFNET_CSUM_SUM16); | |
759 | _CASSERT(IF_HWASSIST_VLAN_TAGGING == IFNET_VLAN_TAGGING); | |
760 | _CASSERT(IF_HWASSIST_VLAN_MTU == IFNET_VLAN_MTU); | |
761 | _CASSERT(IF_HWASSIST_TSO_V4 == IFNET_TSO_IPV4); | |
762 | _CASSERT(IF_HWASSIST_TSO_V6 == IFNET_TSO_IPV6); | |
763 | ||
764 | /* | |
765 | * Make sure we have at least IF_LLREACH_MAXLEN in the llreach info. | |
766 | */ | |
767 | _CASSERT(IF_LLREACH_MAXLEN <= IF_LLREACHINFO_ADDRLEN); | |
768 | ||
769 | PE_parse_boot_argn("net_affinity", &net_affinity, | |
770 | sizeof (net_affinity)); | |
b0d623f7 | 771 | |
d1ecb069 | 772 | PE_parse_boot_argn("net_rtref", &net_rtref, sizeof (net_rtref)); |
6d2010ae A |
773 | |
774 | PE_parse_boot_argn("ifnet_debug", &ifnet_debug, sizeof (ifnet_debug)); | |
775 | ||
776 | dlif_size = (ifnet_debug == 0) ? sizeof (struct dlil_ifnet) : | |
777 | sizeof (struct dlil_ifnet_dbg); | |
778 | /* Enforce 64-bit alignment for dlil_ifnet structure */ | |
779 | dlif_bufsize = dlif_size + sizeof (void *) + sizeof (u_int64_t); | |
780 | dlif_bufsize = P2ROUNDUP(dlif_bufsize, sizeof (u_int64_t)); | |
781 | dlif_zone = zinit(dlif_bufsize, DLIF_ZONE_MAX * dlif_bufsize, | |
782 | 0, DLIF_ZONE_NAME); | |
783 | if (dlif_zone == NULL) { | |
784 | panic("%s: failed allocating %s", __func__, DLIF_ZONE_NAME); | |
785 | /* NOTREACHED */ | |
786 | } | |
787 | zone_change(dlif_zone, Z_EXPAND, TRUE); | |
788 | zone_change(dlif_zone, Z_CALLERACCT, FALSE); | |
789 | ||
790 | dlif_filt_size = sizeof (struct ifnet_filter); | |
791 | dlif_filt_zone = zinit(dlif_filt_size, | |
792 | DLIF_FILT_ZONE_MAX * dlif_filt_size, 0, DLIF_FILT_ZONE_NAME); | |
793 | if (dlif_filt_zone == NULL) { | |
794 | panic("%s: failed allocating %s", __func__, | |
795 | DLIF_FILT_ZONE_NAME); | |
796 | /* NOTREACHED */ | |
797 | } | |
798 | zone_change(dlif_filt_zone, Z_EXPAND, TRUE); | |
799 | zone_change(dlif_filt_zone, Z_CALLERACCT, FALSE); | |
800 | ||
801 | dlif_inp_size = sizeof (struct dlil_threading_info); | |
802 | dlif_inp_zone = zinit(dlif_inp_size, | |
803 | DLIF_INP_ZONE_MAX * dlif_inp_size, 0, DLIF_INP_ZONE_NAME); | |
804 | if (dlif_inp_zone == NULL) { | |
805 | panic("%s: failed allocating %s", __func__, DLIF_INP_ZONE_NAME); | |
806 | /* NOTREACHED */ | |
807 | } | |
808 | zone_change(dlif_inp_zone, Z_EXPAND, TRUE); | |
809 | zone_change(dlif_inp_zone, Z_CALLERACCT, FALSE); | |
810 | ||
811 | dlif_phash_size = sizeof (struct proto_hash_entry) * PROTO_HASH_SLOTS; | |
812 | dlif_phash_zone = zinit(dlif_phash_size, | |
813 | DLIF_PHASH_ZONE_MAX * dlif_phash_size, 0, DLIF_PHASH_ZONE_NAME); | |
814 | if (dlif_phash_zone == NULL) { | |
815 | panic("%s: failed allocating %s", __func__, | |
816 | DLIF_PHASH_ZONE_NAME); | |
817 | /* NOTREACHED */ | |
818 | } | |
819 | zone_change(dlif_phash_zone, Z_EXPAND, TRUE); | |
820 | zone_change(dlif_phash_zone, Z_CALLERACCT, FALSE); | |
821 | ||
822 | dlif_proto_size = sizeof (struct if_proto); | |
823 | dlif_proto_zone = zinit(dlif_proto_size, | |
824 | DLIF_PROTO_ZONE_MAX * dlif_proto_size, 0, DLIF_PROTO_ZONE_NAME); | |
825 | if (dlif_proto_zone == NULL) { | |
826 | panic("%s: failed allocating %s", __func__, | |
827 | DLIF_PROTO_ZONE_NAME); | |
828 | /* NOTREACHED */ | |
829 | } | |
830 | zone_change(dlif_proto_zone, Z_EXPAND, TRUE); | |
831 | zone_change(dlif_proto_zone, Z_CALLERACCT, FALSE); | |
832 | ||
833 | ifnet_llreach_init(); | |
d1ecb069 | 834 | |
91447636 | 835 | TAILQ_INIT(&dlil_ifnet_head); |
91447636 | 836 | TAILQ_INIT(&ifnet_head); |
6d2010ae A |
837 | TAILQ_INIT(&ifnet_detaching_head); |
838 | ||
91447636 | 839 | /* Setup the lock groups we will use */ |
2d21ac55 | 840 | dlil_grp_attributes = lck_grp_attr_alloc_init(); |
91447636 | 841 | |
6d2010ae A |
842 | dlil_lock_group = lck_grp_alloc_init("dlil internal locks", |
843 | dlil_grp_attributes); | |
844 | ifnet_lock_group = lck_grp_alloc_init("ifnet locks", | |
845 | dlil_grp_attributes); | |
846 | ifnet_head_lock_group = lck_grp_alloc_init("ifnet head lock", | |
847 | dlil_grp_attributes); | |
848 | dlil_input_lock_grp = lck_grp_alloc_init("dlil input lock", | |
849 | dlil_grp_attributes); | |
850 | ||
91447636 | 851 | /* Setup the lock attributes we will use */ |
2d21ac55 | 852 | dlil_lck_attributes = lck_attr_alloc_init(); |
6d2010ae | 853 | |
91447636 | 854 | ifnet_lock_attr = lck_attr_alloc_init(); |
6d2010ae A |
855 | |
856 | lck_rw_init(&ifnet_head_lock, ifnet_head_lock_group, | |
857 | dlil_lck_attributes); | |
858 | lck_mtx_init(&dlil_ifnet_lock, dlil_lock_group, dlil_lck_attributes); | |
859 | ||
2d21ac55 A |
860 | lck_attr_free(dlil_lck_attributes); |
861 | dlil_lck_attributes = NULL; | |
6d2010ae A |
862 | |
863 | ifa_init(); | |
864 | ||
91447636 | 865 | /* |
6d2010ae A |
866 | * Create and start up the first dlil input thread once everything |
867 | * is initialized. | |
91447636 | 868 | */ |
6d2010ae | 869 | dlil_create_input_thread(lo_ifp, dlil_lo_thread_ptr); |
2d21ac55 | 870 | |
6d2010ae A |
871 | if (kernel_thread_start((thread_continue_t)ifnet_delayed_thread_func, |
872 | NULL, &thread) != 0) { | |
873 | panic("%s: couldn't create detach thread", __func__); | |
874 | /* NOTREACHED */ | |
875 | } | |
b0d623f7 | 876 | thread_deallocate(thread); |
6d2010ae | 877 | |
b0d623f7 A |
878 | #if PF |
879 | /* Initialize the packet filter */ | |
880 | pfinit(); | |
881 | #endif /* PF */ | |
91447636 | 882 | } |
1c79356b | 883 | |
6d2010ae A |
884 | static void |
885 | if_flt_monitor_busy(struct ifnet *ifp) | |
886 | { | |
887 | lck_mtx_assert(&ifp->if_flt_lock, LCK_MTX_ASSERT_OWNED); | |
888 | ||
889 | ++ifp->if_flt_busy; | |
890 | VERIFY(ifp->if_flt_busy != 0); | |
891 | } | |
892 | ||
893 | static void | |
894 | if_flt_monitor_unbusy(struct ifnet *ifp) | |
895 | { | |
896 | if_flt_monitor_leave(ifp); | |
897 | } | |
898 | ||
899 | static void | |
900 | if_flt_monitor_enter(struct ifnet *ifp) | |
901 | { | |
902 | lck_mtx_assert(&ifp->if_flt_lock, LCK_MTX_ASSERT_OWNED); | |
903 | ||
904 | while (ifp->if_flt_busy) { | |
905 | ++ifp->if_flt_waiters; | |
906 | (void) msleep(&ifp->if_flt_head, &ifp->if_flt_lock, | |
907 | (PZERO - 1), "if_flt_monitor", NULL); | |
908 | } | |
909 | if_flt_monitor_busy(ifp); | |
910 | } | |
911 | ||
912 | static void | |
913 | if_flt_monitor_leave(struct ifnet *ifp) | |
914 | { | |
915 | lck_mtx_assert(&ifp->if_flt_lock, LCK_MTX_ASSERT_OWNED); | |
916 | ||
917 | VERIFY(ifp->if_flt_busy != 0); | |
918 | --ifp->if_flt_busy; | |
919 | ||
920 | if (ifp->if_flt_busy == 0 && ifp->if_flt_waiters > 0) { | |
921 | ifp->if_flt_waiters = 0; | |
922 | wakeup(&ifp->if_flt_head); | |
923 | } | |
924 | } | |
925 | ||
2d21ac55 | 926 | __private_extern__ int |
6d2010ae A |
927 | dlil_attach_filter(struct ifnet *ifp, const struct iff_filter *if_filter, |
928 | interface_filter_t *filter_ref) | |
929 | { | |
930 | int retval = 0; | |
931 | struct ifnet_filter *filter = NULL; | |
9bccf70c | 932 | |
6d2010ae A |
933 | ifnet_head_lock_shared(); |
934 | /* Check that the interface is in the global list */ | |
935 | if (!ifnet_lookup(ifp)) { | |
936 | retval = ENXIO; | |
937 | goto done; | |
938 | } | |
939 | ||
940 | filter = zalloc(dlif_filt_zone); | |
941 | if (filter == NULL) { | |
942 | retval = ENOMEM; | |
943 | goto done; | |
944 | } | |
945 | bzero(filter, dlif_filt_size); | |
946 | ||
947 | /* refcnt held above during lookup */ | |
91447636 A |
948 | filter->filt_ifp = ifp; |
949 | filter->filt_cookie = if_filter->iff_cookie; | |
950 | filter->filt_name = if_filter->iff_name; | |
951 | filter->filt_protocol = if_filter->iff_protocol; | |
952 | filter->filt_input = if_filter->iff_input; | |
953 | filter->filt_output = if_filter->iff_output; | |
954 | filter->filt_event = if_filter->iff_event; | |
955 | filter->filt_ioctl = if_filter->iff_ioctl; | |
956 | filter->filt_detached = if_filter->iff_detached; | |
6d2010ae A |
957 | |
958 | lck_mtx_lock(&ifp->if_flt_lock); | |
959 | if_flt_monitor_enter(ifp); | |
960 | ||
961 | lck_mtx_assert(&ifp->if_flt_lock, LCK_MTX_ASSERT_OWNED); | |
91447636 | 962 | TAILQ_INSERT_TAIL(&ifp->if_flt_head, filter, filt_next); |
6d2010ae A |
963 | |
964 | if_flt_monitor_leave(ifp); | |
965 | lck_mtx_unlock(&ifp->if_flt_lock); | |
966 | ||
91447636 | 967 | *filter_ref = filter; |
b0d623f7 A |
968 | |
969 | /* | |
970 | * Bump filter count and route_generation ID to let TCP | |
971 | * know it shouldn't do TSO on this connection | |
972 | */ | |
973 | OSAddAtomic(1, &dlil_filter_count); | |
974 | if (use_routegenid) | |
975 | routegenid_update(); | |
976 | ||
6d2010ae A |
977 | if (dlil_verbose) { |
978 | printf("%s%d: %s filter attached\n", ifp->if_name, | |
979 | ifp->if_unit, if_filter->iff_name); | |
980 | } | |
981 | done: | |
982 | ifnet_head_done(); | |
983 | if (retval != 0 && ifp != NULL) { | |
984 | DLIL_PRINTF("%s%d: failed to attach %s (err=%d)\n", | |
985 | ifp->if_name, ifp->if_unit, if_filter->iff_name, retval); | |
986 | } | |
987 | if (retval != 0 && filter != NULL) | |
988 | zfree(dlif_filt_zone, filter); | |
989 | ||
990 | return (retval); | |
1c79356b A |
991 | } |
992 | ||
91447636 | 993 | static int |
6d2010ae | 994 | dlil_detach_filter_internal(interface_filter_t filter, int detached) |
1c79356b | 995 | { |
91447636 | 996 | int retval = 0; |
6d2010ae | 997 | |
3a60a9f5 | 998 | if (detached == 0) { |
6d2010ae A |
999 | ifnet_t ifp = NULL; |
1000 | ||
3a60a9f5 A |
1001 | ifnet_head_lock_shared(); |
1002 | TAILQ_FOREACH(ifp, &ifnet_head, if_link) { | |
6d2010ae A |
1003 | interface_filter_t entry = NULL; |
1004 | ||
1005 | lck_mtx_lock(&ifp->if_flt_lock); | |
3a60a9f5 | 1006 | TAILQ_FOREACH(entry, &ifp->if_flt_head, filt_next) { |
6d2010ae A |
1007 | if (entry != filter || entry->filt_skip) |
1008 | continue; | |
1009 | /* | |
1010 | * We've found a match; since it's possible | |
1011 | * that the thread gets blocked in the monitor, | |
1012 | * we do the lock dance. Interface should | |
1013 | * not be detached since we still have a use | |
1014 | * count held during filter attach. | |
1015 | */ | |
1016 | entry->filt_skip = 1; /* skip input/output */ | |
1017 | lck_mtx_unlock(&ifp->if_flt_lock); | |
1018 | ifnet_head_done(); | |
1019 | ||
1020 | lck_mtx_lock(&ifp->if_flt_lock); | |
1021 | if_flt_monitor_enter(ifp); | |
1022 | lck_mtx_assert(&ifp->if_flt_lock, | |
1023 | LCK_MTX_ASSERT_OWNED); | |
1024 | ||
1025 | /* Remove the filter from the list */ | |
1026 | TAILQ_REMOVE(&ifp->if_flt_head, filter, | |
1027 | filt_next); | |
1028 | ||
1029 | if_flt_monitor_leave(ifp); | |
1030 | lck_mtx_unlock(&ifp->if_flt_lock); | |
1031 | if (dlil_verbose) { | |
1032 | printf("%s%d: %s filter detached\n", | |
1033 | ifp->if_name, ifp->if_unit, | |
1034 | filter->filt_name); | |
1035 | } | |
1036 | goto destroy; | |
3a60a9f5 | 1037 | } |
6d2010ae | 1038 | lck_mtx_unlock(&ifp->if_flt_lock); |
3a60a9f5 A |
1039 | } |
1040 | ifnet_head_done(); | |
6d2010ae A |
1041 | |
1042 | /* filter parameter is not a valid filter ref */ | |
1043 | retval = EINVAL; | |
1044 | goto done; | |
3a60a9f5 | 1045 | } |
6d2010ae A |
1046 | |
1047 | if (dlil_verbose) | |
1048 | printf("%s filter detached\n", filter->filt_name); | |
1049 | ||
1050 | destroy: | |
1051 | ||
1052 | /* Call the detached function if there is one */ | |
91447636 A |
1053 | if (filter->filt_detached) |
1054 | filter->filt_detached(filter->filt_cookie, filter->filt_ifp); | |
9bccf70c | 1055 | |
3a60a9f5 | 1056 | /* Free the filter */ |
6d2010ae A |
1057 | zfree(dlif_filt_zone, filter); |
1058 | ||
b0d623f7 A |
1059 | /* |
1060 | * Decrease filter count and route_generation ID to let TCP | |
1061 | * know it should reevalute doing TSO or not | |
1062 | */ | |
1063 | OSAddAtomic(-1, &dlil_filter_count); | |
1064 | if (use_routegenid) | |
1065 | routegenid_update(); | |
1066 | ||
6d2010ae A |
1067 | done: |
1068 | if (retval != 0) { | |
1069 | DLIL_PRINTF("failed to detach %s filter (err=%d)\n", | |
1070 | filter->filt_name, retval); | |
1071 | } | |
1072 | return (retval); | |
1c79356b A |
1073 | } |
1074 | ||
2d21ac55 | 1075 | __private_extern__ void |
91447636 A |
1076 | dlil_detach_filter(interface_filter_t filter) |
1077 | { | |
3a60a9f5 A |
1078 | if (filter == NULL) |
1079 | return; | |
91447636 A |
1080 | dlil_detach_filter_internal(filter, 0); |
1081 | } | |
1c79356b | 1082 | |
91447636 | 1083 | static void |
6d2010ae | 1084 | dlil_input_thread_func(struct dlil_threading_info *inputthread) |
91447636 A |
1085 | { |
1086 | while (1) { | |
2d21ac55 A |
1087 | struct mbuf *m = NULL, *m_loop = NULL; |
1088 | #if IFNET_INPUT_SANITY_CHK | |
1089 | int loop_cnt = 0, mbuf_cnt; | |
1090 | int count; | |
1091 | struct mbuf *m1; | |
1092 | #endif /* IFNET_INPUT_SANITY_CHK */ | |
6d2010ae A |
1093 | |
1094 | lck_mtx_lock_spin(&inputthread->input_lck); | |
1095 | ||
2d21ac55 | 1096 | /* Wait until there is work to be done */ |
6d2010ae | 1097 | while (!(inputthread->input_waiting & ~DLIL_INPUT_RUNNING)) { |
2d21ac55 | 1098 | inputthread->input_waiting &= ~DLIL_INPUT_RUNNING; |
6d2010ae A |
1099 | msleep(&inputthread->input_waiting, |
1100 | &inputthread->input_lck, 0, | |
1101 | inputthread->input_name, 0); | |
2d21ac55 A |
1102 | } |
1103 | ||
6d2010ae | 1104 | lck_mtx_assert(&inputthread->input_lck, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
1105 | |
1106 | m = inputthread->mbuf_head; | |
1107 | inputthread->mbuf_head = NULL; | |
1108 | inputthread->mbuf_tail = NULL; | |
1109 | ||
1110 | if (inputthread->input_waiting & DLIL_INPUT_TERMINATE) { | |
6d2010ae A |
1111 | lck_mtx_unlock(&inputthread->input_lck); |
1112 | ||
1113 | if (m != NULL) | |
1114 | mbuf_freem_list(m); | |
1115 | ||
1116 | OSAddAtomic(-1, &cur_dlil_input_threads); | |
1117 | ||
1118 | lck_mtx_destroy(&inputthread->input_lck, | |
1119 | inputthread->lck_grp); | |
1120 | lck_grp_free(inputthread->lck_grp); | |
1121 | ||
1122 | zfree(dlif_inp_zone, inputthread); | |
1123 | ||
1124 | /* for the extra refcnt from kernel_thread_start() */ | |
1125 | thread_deallocate(current_thread()); | |
1126 | ||
1127 | /* this is the end */ | |
1128 | thread_terminate(current_thread()); | |
1129 | /* NOTREACHED */ | |
1130 | return; | |
2d21ac55 A |
1131 | } |
1132 | ||
1133 | inputthread->input_waiting |= DLIL_INPUT_RUNNING; | |
1134 | inputthread->input_waiting &= ~DLIL_INPUT_WAITING; | |
1135 | ||
1136 | if (inputthread == dlil_lo_thread_ptr) { | |
1137 | m_loop = dlil_lo_input_mbuf_head; | |
1138 | dlil_lo_input_mbuf_head = NULL; | |
1139 | dlil_lo_input_mbuf_tail = NULL; | |
1140 | } | |
1141 | ||
1142 | #if IFNET_INPUT_SANITY_CHK | |
1143 | if (dlil_input_sanity_check != 0) { | |
1144 | mbuf_cnt = inputthread->mbuf_count; | |
1145 | inputthread->mbuf_count = 0; | |
1146 | if (inputthread == dlil_lo_thread_ptr) { | |
1147 | loop_cnt = dlil_lo_input_mbuf_count; | |
1148 | dlil_lo_input_mbuf_count = 0; | |
1149 | } | |
6d2010ae A |
1150 | |
1151 | lck_mtx_unlock(&inputthread->input_lck); | |
1152 | ||
2d21ac55 A |
1153 | for (m1 = m, count = 0; m1; m1 = mbuf_nextpkt(m1)) { |
1154 | count++; | |
1155 | } | |
1156 | if (count != mbuf_cnt) { | |
6d2010ae A |
1157 | panic("%s - thread=%p reg. loop queue " |
1158 | "has %d packets, should have %d\n", | |
1159 | __func__, inputthread, count, mbuf_cnt); | |
1160 | /* NOTREACHED */ | |
2d21ac55 | 1161 | } |
6d2010ae | 1162 | |
2d21ac55 | 1163 | if (inputthread == dlil_lo_thread_ptr) { |
6d2010ae A |
1164 | for (m1 = m_loop, count = 0; m1; |
1165 | m1 = mbuf_nextpkt(m1)) { | |
2d21ac55 A |
1166 | count++; |
1167 | } | |
1168 | if (count != loop_cnt) { | |
6d2010ae A |
1169 | panic("%s - thread=%p loop queue " |
1170 | "has %d packets, should have %d\n", | |
1171 | __func__, inputthread, count, | |
1172 | loop_cnt); | |
1173 | /* NOTREACHED */ | |
2d21ac55 A |
1174 | } |
1175 | } | |
6d2010ae | 1176 | } else |
2d21ac55 A |
1177 | #endif /* IFNET_INPUT_SANITY_CHK */ |
1178 | { | |
6d2010ae | 1179 | lck_mtx_unlock(&inputthread->input_lck); |
2d21ac55 A |
1180 | } |
1181 | ||
1182 | ||
91447636 A |
1183 | /* |
1184 | * NOTE warning %%% attention !!!! | |
6d2010ae A |
1185 | * We should think about putting some thread starvation |
1186 | * safeguards if we deal with long chains of packets. | |
91447636 | 1187 | */ |
2d21ac55 | 1188 | if (m_loop) { |
6d2010ae | 1189 | if (inputthread == dlil_lo_thread_ptr) { |
2d21ac55 | 1190 | dlil_input_packet_list(lo_ifp, m_loop); |
6d2010ae | 1191 | } |
2d21ac55 | 1192 | #if IFNET_INPUT_SANITY_CHK |
6d2010ae A |
1193 | else { |
1194 | panic("%s - thread=%p loop queue has %d " | |
1195 | "packets, should have none!\n", __func__, | |
1196 | inputthread, loop_cnt); | |
1197 | /* NOTREACHED */ | |
1198 | } | |
2d21ac55 | 1199 | #endif /* IFNET_INPUT_SANITY_CHK */ |
91447636 | 1200 | } |
2d21ac55 | 1201 | |
6d2010ae | 1202 | if (m != NULL) |
2d21ac55 A |
1203 | dlil_input_packet_list(0, m); |
1204 | ||
6d2010ae | 1205 | lck_mtx_lock_spin(&inputthread->input_lck); |
2d21ac55 | 1206 | |
6d2010ae A |
1207 | if (inputthread->input_waiting & |
1208 | (DLIL_PROTO_WAITING | DLIL_PROTO_REGISTER)) { | |
1209 | lck_mtx_unlock(&inputthread->input_lck); | |
2d21ac55 | 1210 | proto_input_run(); |
6d2010ae A |
1211 | } else { |
1212 | lck_mtx_unlock(&inputthread->input_lck); | |
1213 | } | |
2d21ac55 A |
1214 | } |
1215 | } | |
1216 | ||
1217 | errno_t | |
6d2010ae A |
1218 | ifnet_input(ifnet_t ifp, mbuf_t m_head, |
1219 | const struct ifnet_stat_increment_param *stats) | |
2d21ac55 A |
1220 | { |
1221 | struct thread *tp = current_thread(); | |
1222 | mbuf_t m_tail; | |
1223 | struct dlil_threading_info *inp; | |
1224 | #if IFNET_INPUT_SANITY_CHK | |
1225 | u_int32_t pkt_count = 0; | |
1226 | #endif /* IFNET_INPUT_SANITY_CHK */ | |
1227 | ||
1228 | if (ifp == NULL || m_head == NULL) { | |
6d2010ae | 1229 | if (m_head != NULL) |
2d21ac55 | 1230 | mbuf_freem_list(m_head); |
6d2010ae | 1231 | return (EINVAL); |
2d21ac55 A |
1232 | } |
1233 | ||
1234 | m_tail = m_head; | |
1235 | while (1) { | |
1236 | #if IFNET_INPUT_SANITY_CHK | |
1237 | if (dlil_input_sanity_check != 0) { | |
1238 | ifnet_t rcvif; | |
6d2010ae | 1239 | |
2d21ac55 A |
1240 | rcvif = mbuf_pkthdr_rcvif(m_tail); |
1241 | pkt_count++; | |
6d2010ae | 1242 | |
2d21ac55 | 1243 | if (rcvif == NULL || |
6d2010ae A |
1244 | (ifp->if_type != IFT_LOOP && rcvif != ifp) || |
1245 | !(mbuf_flags(m_head) & MBUF_PKTHDR)) { | |
1246 | panic("%s - invalid mbuf %p\n", | |
1247 | __func__, m_tail); | |
1248 | /* NOTREACHED */ | |
2d21ac55 | 1249 | } |
91447636 | 1250 | } |
2d21ac55 A |
1251 | #endif /* IFNET_INPUT_SANITY_CHK */ |
1252 | if (mbuf_nextpkt(m_tail) == NULL) | |
1253 | break; | |
1254 | m_tail = mbuf_nextpkt(m_tail); | |
91447636 | 1255 | } |
1c79356b | 1256 | |
2d21ac55 | 1257 | inp = ifp->if_input_thread; |
1c79356b | 1258 | |
6d2010ae | 1259 | if (dlil_multithreaded_input == 0 || inp == NULL) |
2d21ac55 A |
1260 | inp = dlil_lo_thread_ptr; |
1261 | ||
1262 | /* | |
1263 | * If there is a matching dlil input thread associated with an | |
1264 | * affinity set, associate this workloop thread with the same set. | |
1265 | * We will only do this once. | |
1266 | */ | |
6d2010ae | 1267 | lck_mtx_lock_spin(&inp->input_lck); |
2d21ac55 A |
1268 | if (inp->net_affinity && inp->workloop_thread == NULL) { |
1269 | u_int32_t tag = inp->tag; | |
1270 | inp->workloop_thread = tp; | |
6d2010ae | 1271 | lck_mtx_unlock(&inp->input_lck); |
2d21ac55 A |
1272 | |
1273 | /* Associated the current thread with the new affinity tag */ | |
1274 | (void) dlil_affinity_set(tp, tag); | |
1275 | ||
1276 | /* | |
1277 | * Take a reference on the workloop (current) thread; during | |
1278 | * detach, we will need to refer to it in order ot tear down | |
1279 | * its affinity. | |
1280 | */ | |
1281 | thread_reference(tp); | |
6d2010ae | 1282 | lck_mtx_lock_spin(&inp->input_lck); |
2d21ac55 A |
1283 | } |
1284 | ||
1285 | /* WARNING | |
91447636 A |
1286 | * Because of loopbacked multicast we cannot stuff the ifp in |
1287 | * the rcvif of the packet header: loopback has its own dlil | |
1288 | * input queue | |
1289 | */ | |
2d21ac55 A |
1290 | |
1291 | if (inp == dlil_lo_thread_ptr && ifp->if_type == IFT_LOOP) { | |
1292 | if (dlil_lo_input_mbuf_head == NULL) | |
1293 | dlil_lo_input_mbuf_head = m_head; | |
1294 | else if (dlil_lo_input_mbuf_tail != NULL) | |
1295 | dlil_lo_input_mbuf_tail->m_nextpkt = m_head; | |
1296 | dlil_lo_input_mbuf_tail = m_tail; | |
1297 | #if IFNET_INPUT_SANITY_CHK | |
1298 | if (dlil_input_sanity_check != 0) { | |
1299 | dlil_lo_input_mbuf_count += pkt_count; | |
1300 | inp->input_mbuf_cnt += pkt_count; | |
1301 | inp->input_wake_cnt++; | |
1302 | ||
6d2010ae | 1303 | lck_mtx_assert(&inp->input_lck, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
1304 | } |
1305 | #endif | |
6d2010ae | 1306 | } else { |
2d21ac55 A |
1307 | if (inp->mbuf_head == NULL) |
1308 | inp->mbuf_head = m_head; | |
1309 | else if (inp->mbuf_tail != NULL) | |
1310 | inp->mbuf_tail->m_nextpkt = m_head; | |
1311 | inp->mbuf_tail = m_tail; | |
1312 | #if IFNET_INPUT_SANITY_CHK | |
1313 | if (dlil_input_sanity_check != 0) { | |
1314 | inp->mbuf_count += pkt_count; | |
1315 | inp->input_mbuf_cnt += pkt_count; | |
1316 | inp->input_wake_cnt++; | |
1317 | ||
6d2010ae | 1318 | lck_mtx_assert(&inp->input_lck, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
1319 | } |
1320 | #endif | |
1321 | } | |
1322 | ||
2d21ac55 A |
1323 | inp->input_waiting |= DLIL_INPUT_WAITING; |
1324 | if ((inp->input_waiting & DLIL_INPUT_RUNNING) == 0) { | |
1325 | wakeup((caddr_t)&inp->input_waiting); | |
91447636 | 1326 | } |
6d2010ae A |
1327 | lck_mtx_unlock(&inp->input_lck); |
1328 | ||
91447636 | 1329 | if (stats) { |
6d2010ae A |
1330 | atomic_add_64(&ifp->if_data.ifi_ipackets, stats->packets_in); |
1331 | atomic_add_64(&ifp->if_data.ifi_ibytes, stats->bytes_in); | |
1332 | atomic_add_64(&ifp->if_data.ifi_ierrors, stats->errors_in); | |
1333 | ||
1334 | atomic_add_64(&ifp->if_data.ifi_opackets, stats->packets_out); | |
1335 | atomic_add_64(&ifp->if_data.ifi_obytes, stats->bytes_out); | |
1336 | atomic_add_64(&ifp->if_data.ifi_oerrors, stats->errors_out); | |
1337 | ||
1338 | atomic_add_64(&ifp->if_data.ifi_collisions, stats->collisions); | |
1339 | atomic_add_64(&ifp->if_data.ifi_iqdrops, stats->dropped); | |
91447636 | 1340 | } |
2d21ac55 | 1341 | |
6d2010ae | 1342 | return (0); |
1c79356b A |
1343 | } |
1344 | ||
2d21ac55 | 1345 | static int |
6d2010ae A |
1346 | dlil_interface_filters_input(struct ifnet *ifp, struct mbuf **m_p, |
1347 | char **frame_header_p, protocol_family_t protocol_family) | |
91447636 | 1348 | { |
6d2010ae | 1349 | struct ifnet_filter *filter; |
2d21ac55 | 1350 | |
6d2010ae A |
1351 | /* |
1352 | * Pass the inbound packet to the interface filters | |
1353 | */ | |
1354 | lck_mtx_lock_spin(&ifp->if_flt_lock); | |
1355 | /* prevent filter list from changing in case we drop the lock */ | |
1356 | if_flt_monitor_busy(ifp); | |
2d21ac55 A |
1357 | TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) { |
1358 | int result; | |
1359 | ||
6d2010ae A |
1360 | if (!filter->filt_skip && filter->filt_input != NULL && |
1361 | (filter->filt_protocol == 0 || | |
1362 | filter->filt_protocol == protocol_family)) { | |
1363 | lck_mtx_unlock(&ifp->if_flt_lock); | |
1364 | ||
2d21ac55 | 1365 | result = (*filter->filt_input)(filter->filt_cookie, |
6d2010ae A |
1366 | ifp, protocol_family, m_p, frame_header_p); |
1367 | ||
1368 | lck_mtx_lock_spin(&ifp->if_flt_lock); | |
2d21ac55 | 1369 | if (result != 0) { |
6d2010ae A |
1370 | /* we're done with the filter list */ |
1371 | if_flt_monitor_unbusy(ifp); | |
1372 | lck_mtx_unlock(&ifp->if_flt_lock); | |
2d21ac55 A |
1373 | return (result); |
1374 | } | |
1375 | } | |
1376 | } | |
6d2010ae A |
1377 | /* we're done with the filter list */ |
1378 | if_flt_monitor_unbusy(ifp); | |
1379 | lck_mtx_unlock(&ifp->if_flt_lock); | |
b7266188 A |
1380 | |
1381 | /* | |
6d2010ae | 1382 | * Strip away M_PROTO1 bit prior to sending packet up the stack as |
b7266188 A |
1383 | * it is meant to be local to a subsystem -- if_bridge for M_PROTO1 |
1384 | */ | |
1385 | if (*m_p != NULL) | |
1386 | (*m_p)->m_flags &= ~M_PROTO1; | |
1387 | ||
2d21ac55 | 1388 | return (0); |
1c79356b A |
1389 | } |
1390 | ||
6d2010ae A |
1391 | static int |
1392 | dlil_interface_filters_output(struct ifnet *ifp, struct mbuf **m_p, | |
1393 | protocol_family_t protocol_family) | |
1394 | { | |
1395 | struct ifnet_filter *filter; | |
1396 | ||
1397 | /* | |
1398 | * Pass the outbound packet to the interface filters | |
1399 | */ | |
1400 | lck_mtx_lock_spin(&ifp->if_flt_lock); | |
1401 | /* prevent filter list from changing in case we drop the lock */ | |
1402 | if_flt_monitor_busy(ifp); | |
1403 | TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) { | |
1404 | int result; | |
1405 | ||
1406 | if (!filter->filt_skip && filter->filt_output != NULL && | |
1407 | (filter->filt_protocol == 0 || | |
1408 | filter->filt_protocol == protocol_family)) { | |
1409 | lck_mtx_unlock(&ifp->if_flt_lock); | |
1410 | ||
1411 | result = filter->filt_output(filter->filt_cookie, ifp, | |
1412 | protocol_family, m_p); | |
1413 | ||
1414 | lck_mtx_lock_spin(&ifp->if_flt_lock); | |
1415 | if (result != 0) { | |
1416 | /* we're done with the filter list */ | |
1417 | if_flt_monitor_unbusy(ifp); | |
1418 | lck_mtx_unlock(&ifp->if_flt_lock); | |
1419 | return (result); | |
1420 | } | |
1421 | } | |
1422 | } | |
1423 | /* we're done with the filter list */ | |
1424 | if_flt_monitor_unbusy(ifp); | |
1425 | lck_mtx_unlock(&ifp->if_flt_lock); | |
1426 | ||
1427 | return (0); | |
1428 | } | |
1429 | ||
2d21ac55 A |
1430 | static void |
1431 | dlil_ifproto_input(struct if_proto * ifproto, mbuf_t m) | |
1c79356b | 1432 | { |
2d21ac55 | 1433 | int error; |
1c79356b | 1434 | |
2d21ac55 A |
1435 | if (ifproto->proto_kpi == kProtoKPI_v1) { |
1436 | /* Version 1 protocols get one packet at a time */ | |
1437 | while (m != NULL) { | |
1438 | char * frame_header; | |
1439 | mbuf_t next_packet; | |
6d2010ae | 1440 | |
2d21ac55 A |
1441 | next_packet = m->m_nextpkt; |
1442 | m->m_nextpkt = NULL; | |
1443 | frame_header = m->m_pkthdr.header; | |
1444 | m->m_pkthdr.header = NULL; | |
6d2010ae A |
1445 | error = (*ifproto->kpi.v1.input)(ifproto->ifp, |
1446 | ifproto->protocol_family, m, frame_header); | |
2d21ac55 A |
1447 | if (error != 0 && error != EJUSTRETURN) |
1448 | m_freem(m); | |
1449 | m = next_packet; | |
1450 | } | |
6d2010ae | 1451 | } else if (ifproto->proto_kpi == kProtoKPI_v2) { |
2d21ac55 A |
1452 | /* Version 2 protocols support packet lists */ |
1453 | error = (*ifproto->kpi.v2.input)(ifproto->ifp, | |
6d2010ae | 1454 | ifproto->protocol_family, m); |
2d21ac55 A |
1455 | if (error != 0 && error != EJUSTRETURN) |
1456 | m_freem_list(m); | |
91447636 | 1457 | } |
2d21ac55 A |
1458 | return; |
1459 | } | |
1c79356b | 1460 | |
2d21ac55 A |
1461 | __private_extern__ void |
1462 | dlil_input_packet_list(struct ifnet * ifp_param, struct mbuf *m) | |
1463 | { | |
1464 | int error = 0; | |
2d21ac55 A |
1465 | protocol_family_t protocol_family; |
1466 | mbuf_t next_packet; | |
1467 | ifnet_t ifp = ifp_param; | |
1468 | char * frame_header; | |
1469 | struct if_proto * last_ifproto = NULL; | |
1470 | mbuf_t pkt_first = NULL; | |
1471 | mbuf_t * pkt_next = NULL; | |
1472 | ||
1473 | KERNEL_DEBUG(DBG_FNC_DLIL_INPUT | DBG_FUNC_START,0,0,0,0,0); | |
1474 | ||
6d2010ae | 1475 | |
2d21ac55 | 1476 | while (m != NULL) { |
6d2010ae A |
1477 | struct if_proto *ifproto = NULL; |
1478 | int iorefcnt = 0; | |
2d21ac55 | 1479 | |
2d21ac55 A |
1480 | if (ifp_param == NULL) |
1481 | ifp = m->m_pkthdr.rcvif; | |
6d2010ae A |
1482 | |
1483 | /* Check if this mbuf looks valid */ | |
1484 | MBUF_INPUT_CHECK(m, ifp); | |
1485 | ||
1486 | next_packet = m->m_nextpkt; | |
1487 | m->m_nextpkt = NULL; | |
2d21ac55 A |
1488 | frame_header = m->m_pkthdr.header; |
1489 | m->m_pkthdr.header = NULL; | |
1490 | ||
6d2010ae A |
1491 | /* Get an IO reference count if the interface is not |
1492 | * loopback and it is attached. | |
1493 | */ | |
1494 | if (ifp != lo_ifp) { | |
1495 | if (!ifnet_is_attached(ifp, 1)) { | |
1496 | m_freem(m); | |
1497 | goto next; | |
1498 | } | |
1499 | iorefcnt = 1; | |
2d21ac55 | 1500 | } |
d41d1dae | 1501 | |
d41d1dae A |
1502 | switch (m->m_pkthdr.prio) { |
1503 | case MBUF_TC_BK: | |
6d2010ae A |
1504 | atomic_add_64(&ifp->if_tc.ifi_ibkpackets, 1); |
1505 | atomic_add_64(&ifp->if_tc.ifi_ibkbytes, m->m_pkthdr.len); | |
d41d1dae A |
1506 | break; |
1507 | case MBUF_TC_VI: | |
6d2010ae A |
1508 | atomic_add_64(&ifp->if_tc.ifi_ivipackets, 1); |
1509 | atomic_add_64(&ifp->if_tc.ifi_ivibytes, m->m_pkthdr.len); | |
d41d1dae A |
1510 | break; |
1511 | case MBUF_TC_VO: | |
6d2010ae A |
1512 | atomic_add_64(&ifp->if_tc.ifi_ivopackets, 1); |
1513 | atomic_add_64(&ifp->if_tc.ifi_ivobytes, m->m_pkthdr.len); | |
d41d1dae A |
1514 | break; |
1515 | default: | |
1516 | break; | |
1517 | } | |
d41d1dae | 1518 | |
2d21ac55 | 1519 | /* find which protocol family this packet is for */ |
6d2010ae | 1520 | ifnet_lock_shared(ifp); |
2d21ac55 | 1521 | error = (*ifp->if_demux)(ifp, m, frame_header, |
6d2010ae A |
1522 | &protocol_family); |
1523 | ifnet_lock_done(ifp); | |
2d21ac55 | 1524 | if (error != 0) { |
6d2010ae | 1525 | if (error == EJUSTRETURN) |
2d21ac55 | 1526 | goto next; |
2d21ac55 A |
1527 | protocol_family = 0; |
1528 | } | |
6d2010ae | 1529 | |
2d21ac55 | 1530 | if (m->m_flags & (M_BCAST|M_MCAST)) |
6d2010ae | 1531 | atomic_add_64(&ifp->if_imcasts, 1); |
1c79356b | 1532 | |
2d21ac55 A |
1533 | /* run interface filters, exclude VLAN packets PR-3586856 */ |
1534 | if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) == 0) { | |
6d2010ae A |
1535 | error = dlil_interface_filters_input(ifp, &m, |
1536 | &frame_header, protocol_family); | |
1537 | if (error != 0) { | |
1538 | if (error != EJUSTRETURN) | |
2d21ac55 | 1539 | m_freem(m); |
2d21ac55 | 1540 | goto next; |
91447636 A |
1541 | } |
1542 | } | |
2d21ac55 | 1543 | if (error != 0 || ((m->m_flags & M_PROMISC) != 0) ) { |
91447636 | 1544 | m_freem(m); |
2d21ac55 | 1545 | goto next; |
91447636 | 1546 | } |
6d2010ae | 1547 | |
2d21ac55 A |
1548 | /* Lookup the protocol attachment to this interface */ |
1549 | if (protocol_family == 0) { | |
1550 | ifproto = NULL; | |
6d2010ae A |
1551 | } else if (last_ifproto != NULL && last_ifproto->ifp == ifp && |
1552 | (last_ifproto->protocol_family == protocol_family)) { | |
1553 | VERIFY(ifproto == NULL); | |
2d21ac55 | 1554 | ifproto = last_ifproto; |
6d2010ae A |
1555 | if_proto_ref(last_ifproto); |
1556 | } else { | |
1557 | VERIFY(ifproto == NULL); | |
1558 | ifnet_lock_shared(ifp); | |
1559 | /* callee holds a proto refcnt upon success */ | |
2d21ac55 | 1560 | ifproto = find_attached_proto(ifp, protocol_family); |
6d2010ae | 1561 | ifnet_lock_done(ifp); |
2d21ac55 A |
1562 | } |
1563 | if (ifproto == NULL) { | |
1564 | /* no protocol for this packet, discard */ | |
1565 | m_freem(m); | |
1566 | goto next; | |
1567 | } | |
1568 | if (ifproto != last_ifproto) { | |
2d21ac55 A |
1569 | if (last_ifproto != NULL) { |
1570 | /* pass up the list for the previous protocol */ | |
2d21ac55 A |
1571 | dlil_ifproto_input(last_ifproto, pkt_first); |
1572 | pkt_first = NULL; | |
1573 | if_proto_free(last_ifproto); | |
2d21ac55 A |
1574 | } |
1575 | last_ifproto = ifproto; | |
6d2010ae | 1576 | if_proto_ref(ifproto); |
2d21ac55 A |
1577 | } |
1578 | /* extend the list */ | |
1579 | m->m_pkthdr.header = frame_header; | |
1580 | if (pkt_first == NULL) { | |
1581 | pkt_first = m; | |
1582 | } else { | |
1583 | *pkt_next = m; | |
1584 | } | |
1585 | pkt_next = &m->m_nextpkt; | |
1c79356b | 1586 | |
6d2010ae | 1587 | next: |
2d21ac55 A |
1588 | if (next_packet == NULL && last_ifproto != NULL) { |
1589 | /* pass up the last list of packets */ | |
2d21ac55 A |
1590 | dlil_ifproto_input(last_ifproto, pkt_first); |
1591 | if_proto_free(last_ifproto); | |
6d2010ae A |
1592 | last_ifproto = NULL; |
1593 | } | |
1594 | if (ifproto != NULL) { | |
1595 | if_proto_free(ifproto); | |
1596 | ifproto = NULL; | |
2d21ac55 | 1597 | } |
6d2010ae | 1598 | |
2d21ac55 | 1599 | m = next_packet; |
1c79356b | 1600 | |
6d2010ae A |
1601 | /* update the driver's multicast filter, if needed */ |
1602 | if (ifp->if_updatemcasts > 0 && if_mcasts_update(ifp) == 0) | |
1603 | ifp->if_updatemcasts = 0; | |
1604 | if (iorefcnt == 1) | |
1605 | ifnet_decr_iorefcnt(ifp); | |
91447636 | 1606 | } |
6d2010ae | 1607 | |
91447636 | 1608 | KERNEL_DEBUG(DBG_FNC_DLIL_INPUT | DBG_FUNC_END,0,0,0,0,0); |
2d21ac55 | 1609 | return; |
1c79356b A |
1610 | } |
1611 | ||
6d2010ae A |
1612 | errno_t |
1613 | if_mcasts_update(struct ifnet *ifp) | |
1614 | { | |
1615 | errno_t err; | |
1616 | ||
1617 | err = ifnet_ioctl(ifp, 0, SIOCADDMULTI, NULL); | |
1618 | if (err == EAFNOSUPPORT) | |
1619 | err = 0; | |
1620 | printf("%s%d: %s %d suspended link-layer multicast membership(s) " | |
1621 | "(err=%d)\n", ifp->if_name, ifp->if_unit, | |
1622 | (err == 0 ? "successfully restored" : "failed to restore"), | |
1623 | ifp->if_updatemcasts, err); | |
1624 | ||
1625 | /* just return success */ | |
1626 | return (0); | |
1627 | } | |
1628 | ||
91447636 A |
1629 | static int |
1630 | dlil_event_internal(struct ifnet *ifp, struct kev_msg *event) | |
1c79356b | 1631 | { |
91447636 | 1632 | struct ifnet_filter *filter; |
6d2010ae A |
1633 | |
1634 | /* Get an io ref count if the interface is attached */ | |
1635 | if (!ifnet_is_attached(ifp, 1)) | |
1636 | goto done; | |
1637 | ||
1638 | /* | |
1639 | * Pass the event to the interface filters | |
1640 | */ | |
1641 | lck_mtx_lock_spin(&ifp->if_flt_lock); | |
1642 | /* prevent filter list from changing in case we drop the lock */ | |
1643 | if_flt_monitor_busy(ifp); | |
1644 | TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) { | |
1645 | if (filter->filt_event != NULL) { | |
1646 | lck_mtx_unlock(&ifp->if_flt_lock); | |
1647 | ||
1648 | filter->filt_event(filter->filt_cookie, ifp, | |
1649 | filter->filt_protocol, event); | |
1650 | ||
1651 | lck_mtx_lock_spin(&ifp->if_flt_lock); | |
91447636 | 1652 | } |
6d2010ae A |
1653 | } |
1654 | /* we're done with the filter list */ | |
1655 | if_flt_monitor_unbusy(ifp); | |
1656 | lck_mtx_unlock(&ifp->if_flt_lock); | |
1657 | ||
1658 | ifnet_lock_shared(ifp); | |
1659 | if (ifp->if_proto_hash != NULL) { | |
1660 | int i; | |
1661 | ||
1662 | for (i = 0; i < PROTO_HASH_SLOTS; i++) { | |
1663 | struct if_proto *proto; | |
1664 | ||
1665 | SLIST_FOREACH(proto, &ifp->if_proto_hash[i], | |
1666 | next_hash) { | |
1667 | proto_media_event eventp = | |
1668 | (proto->proto_kpi == kProtoKPI_v1 ? | |
1669 | proto->kpi.v1.event : | |
1670 | proto->kpi.v2.event); | |
1671 | ||
1672 | if (eventp != NULL) { | |
1673 | if_proto_ref(proto); | |
1674 | ifnet_lock_done(ifp); | |
1675 | ||
1676 | eventp(ifp, proto->protocol_family, | |
1677 | event); | |
1678 | ||
1679 | ifnet_lock_shared(ifp); | |
1680 | if_proto_free(proto); | |
91447636 A |
1681 | } |
1682 | } | |
1683 | } | |
91447636 | 1684 | } |
6d2010ae A |
1685 | ifnet_lock_done(ifp); |
1686 | ||
1687 | /* Pass the event to the interface */ | |
1688 | if (ifp->if_event != NULL) | |
1689 | ifp->if_event(ifp, event); | |
1690 | ||
1691 | /* Release the io ref count */ | |
1692 | ifnet_decr_iorefcnt(ifp); | |
1693 | ||
1694 | done: | |
1695 | return (kev_post_msg(event)); | |
1c79356b A |
1696 | } |
1697 | ||
2d21ac55 | 1698 | errno_t |
6d2010ae | 1699 | ifnet_event(ifnet_t ifp, struct kern_event_msg *event) |
1c79356b | 1700 | { |
91447636 | 1701 | struct kev_msg kev_msg; |
2d21ac55 A |
1702 | int result = 0; |
1703 | ||
6d2010ae A |
1704 | if (ifp == NULL || event == NULL) |
1705 | return (EINVAL); | |
1c79356b | 1706 | |
6d2010ae | 1707 | bzero(&kev_msg, sizeof (kev_msg)); |
91447636 A |
1708 | kev_msg.vendor_code = event->vendor_code; |
1709 | kev_msg.kev_class = event->kev_class; | |
1710 | kev_msg.kev_subclass = event->kev_subclass; | |
1711 | kev_msg.event_code = event->event_code; | |
1712 | kev_msg.dv[0].data_ptr = &event->event_data[0]; | |
1713 | kev_msg.dv[0].data_length = event->total_size - KEV_MSG_HEADER_SIZE; | |
1714 | kev_msg.dv[1].data_length = 0; | |
6d2010ae | 1715 | |
91447636 | 1716 | result = dlil_event_internal(ifp, &kev_msg); |
1c79356b | 1717 | |
6d2010ae | 1718 | return (result); |
91447636 | 1719 | } |
1c79356b | 1720 | |
2d21ac55 A |
1721 | #if CONFIG_MACF_NET |
1722 | #include <netinet/ip6.h> | |
1723 | #include <netinet/ip.h> | |
6d2010ae A |
1724 | static int |
1725 | dlil_get_socket_type(struct mbuf **mp, int family, int raw) | |
2d21ac55 A |
1726 | { |
1727 | struct mbuf *m; | |
1728 | struct ip *ip; | |
1729 | struct ip6_hdr *ip6; | |
1730 | int type = SOCK_RAW; | |
1731 | ||
1732 | if (!raw) { | |
1733 | switch (family) { | |
1734 | case PF_INET: | |
1735 | m = m_pullup(*mp, sizeof(struct ip)); | |
1736 | if (m == NULL) | |
1737 | break; | |
1738 | *mp = m; | |
1739 | ip = mtod(m, struct ip *); | |
1740 | if (ip->ip_p == IPPROTO_TCP) | |
1741 | type = SOCK_STREAM; | |
1742 | else if (ip->ip_p == IPPROTO_UDP) | |
1743 | type = SOCK_DGRAM; | |
1744 | break; | |
1745 | case PF_INET6: | |
1746 | m = m_pullup(*mp, sizeof(struct ip6_hdr)); | |
1747 | if (m == NULL) | |
1748 | break; | |
1749 | *mp = m; | |
1750 | ip6 = mtod(m, struct ip6_hdr *); | |
1751 | if (ip6->ip6_nxt == IPPROTO_TCP) | |
1752 | type = SOCK_STREAM; | |
1753 | else if (ip6->ip6_nxt == IPPROTO_UDP) | |
1754 | type = SOCK_DGRAM; | |
1755 | break; | |
1756 | } | |
1757 | } | |
1758 | ||
1759 | return (type); | |
1760 | } | |
1761 | #endif | |
1762 | ||
d41d1dae A |
1763 | static void |
1764 | if_inc_traffic_class_out(ifnet_t ifp, mbuf_t m) | |
1765 | { | |
d41d1dae A |
1766 | if (!(m->m_flags & M_PKTHDR)) |
1767 | return; | |
1768 | ||
1769 | switch (m->m_pkthdr.prio) { | |
1770 | case MBUF_TC_BK: | |
6d2010ae A |
1771 | atomic_add_64(&ifp->if_tc.ifi_obkpackets, 1); |
1772 | atomic_add_64(&ifp->if_tc.ifi_obkbytes, m->m_pkthdr.len); | |
d41d1dae A |
1773 | break; |
1774 | case MBUF_TC_VI: | |
6d2010ae A |
1775 | atomic_add_64(&ifp->if_tc.ifi_ovipackets, 1); |
1776 | atomic_add_64(&ifp->if_tc.ifi_ovibytes, m->m_pkthdr.len); | |
d41d1dae A |
1777 | break; |
1778 | case MBUF_TC_VO: | |
6d2010ae A |
1779 | atomic_add_64(&ifp->if_tc.ifi_ovopackets, 1); |
1780 | atomic_add_64(&ifp->if_tc.ifi_ovobytes, m->m_pkthdr.len); | |
d41d1dae A |
1781 | break; |
1782 | default: | |
1783 | break; | |
1784 | } | |
1c79356b A |
1785 | } |
1786 | ||
1c79356b | 1787 | /* |
91447636 A |
1788 | * dlil_output |
1789 | * | |
1790 | * Caller should have a lock on the protocol domain if the protocol | |
1791 | * doesn't support finer grained locking. In most cases, the lock | |
1792 | * will be held from the socket layer and won't be released until | |
1793 | * we return back to the socket layer. | |
1794 | * | |
1795 | * This does mean that we must take a protocol lock before we take | |
1796 | * an interface lock if we're going to take both. This makes sense | |
1797 | * because a protocol is likely to interact with an ifp while it | |
1798 | * is under the protocol lock. | |
1c79356b | 1799 | */ |
6d2010ae A |
1800 | errno_t |
1801 | dlil_output(ifnet_t ifp, protocol_family_t proto_family, mbuf_t packetlist, | |
1802 | void *route, const struct sockaddr *dest, int raw) | |
1803 | { | |
1804 | char *frame_type = NULL; | |
1805 | char *dst_linkaddr = NULL; | |
1806 | int retval = 0; | |
1807 | char frame_type_buffer[MAX_FRAME_TYPE_SIZE * 4]; | |
1808 | char dst_linkaddr_buffer[MAX_LINKADDR * 4]; | |
1809 | struct if_proto *proto = NULL; | |
2d21ac55 A |
1810 | mbuf_t m; |
1811 | mbuf_t send_head = NULL; | |
1812 | mbuf_t *send_tail = &send_head; | |
6d2010ae A |
1813 | int iorefcnt = 0; |
1814 | ||
91447636 | 1815 | KERNEL_DEBUG(DBG_FNC_DLIL_OUTPUT | DBG_FUNC_START,0,0,0,0,0); |
6d2010ae A |
1816 | |
1817 | /* Get an io refcnt if the interface is attached to prevent ifnet_detach | |
1818 | * from happening while this operation is in progress */ | |
1819 | if (!ifnet_is_attached(ifp, 1)) { | |
1820 | retval = ENXIO; | |
1821 | goto cleanup; | |
1822 | } | |
1823 | iorefcnt = 1; | |
1824 | ||
1825 | /* update the driver's multicast filter, if needed */ | |
1826 | if (ifp->if_updatemcasts > 0 && if_mcasts_update(ifp) == 0) | |
1827 | ifp->if_updatemcasts = 0; | |
1828 | ||
1829 | frame_type = frame_type_buffer; | |
1830 | dst_linkaddr = dst_linkaddr_buffer; | |
1831 | ||
91447636 | 1832 | if (raw == 0) { |
6d2010ae A |
1833 | ifnet_lock_shared(ifp); |
1834 | /* callee holds a proto refcnt upon success */ | |
91447636 A |
1835 | proto = find_attached_proto(ifp, proto_family); |
1836 | if (proto == NULL) { | |
6d2010ae | 1837 | ifnet_lock_done(ifp); |
91447636 A |
1838 | retval = ENXIO; |
1839 | goto cleanup; | |
1840 | } | |
6d2010ae | 1841 | ifnet_lock_done(ifp); |
2d21ac55 | 1842 | } |
6d2010ae | 1843 | |
2d21ac55 A |
1844 | preout_again: |
1845 | if (packetlist == NULL) | |
1846 | goto cleanup; | |
6d2010ae | 1847 | |
2d21ac55 A |
1848 | m = packetlist; |
1849 | packetlist = packetlist->m_nextpkt; | |
1850 | m->m_nextpkt = NULL; | |
6d2010ae | 1851 | |
2d21ac55 | 1852 | if (raw == 0) { |
6d2010ae A |
1853 | proto_media_preout preoutp = (proto->proto_kpi == kProtoKPI_v1 ? |
1854 | proto->kpi.v1.pre_output : proto->kpi.v2.pre_output); | |
91447636 | 1855 | retval = 0; |
6d2010ae A |
1856 | if (preoutp != NULL) { |
1857 | retval = preoutp(ifp, proto_family, &m, dest, route, | |
1858 | frame_type, dst_linkaddr); | |
1859 | ||
1860 | if (retval != 0) { | |
1861 | if (retval == EJUSTRETURN) | |
1862 | goto preout_again; | |
1863 | m_freem(m); | |
1864 | goto cleanup; | |
91447636 | 1865 | } |
1c79356b | 1866 | } |
1c79356b | 1867 | } |
2d21ac55 A |
1868 | |
1869 | #if CONFIG_MACF_NET | |
1870 | retval = mac_ifnet_check_transmit(ifp, m, proto_family, | |
1871 | dlil_get_socket_type(&m, proto_family, raw)); | |
1872 | if (retval) { | |
1873 | m_freem(m); | |
1874 | goto cleanup; | |
1875 | } | |
1876 | #endif | |
1877 | ||
1878 | do { | |
6d2010ae A |
1879 | #if CONFIG_DTRACE |
1880 | if (proto_family == PF_INET) { | |
1881 | struct ip *ip = mtod(m, struct ip*); | |
1882 | DTRACE_IP6(send, struct mbuf *, m, struct inpcb *, NULL, | |
1883 | struct ip *, ip, struct ifnet *, ifp, | |
1884 | struct ip *, ip, struct ip6_hdr *, NULL); | |
1885 | ||
1886 | } else if (proto_family == PF_INET6) { | |
1887 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr*); | |
1888 | DTRACE_IP6(send, struct mbuf*, m, struct inpcb *, NULL, | |
1889 | struct ip6_hdr *, ip6, struct ifnet*, ifp, | |
1890 | struct ip*, NULL, struct ip6_hdr *, ip6); | |
1891 | } | |
1892 | #endif /* CONFIG_DTRACE */ | |
1893 | ||
2d21ac55 | 1894 | if (raw == 0 && ifp->if_framer) { |
7e4a7d39 A |
1895 | int rcvif_set = 0; |
1896 | ||
1897 | /* | |
1898 | * If this is a broadcast packet that needs to be | |
1899 | * looped back into the system, set the inbound ifp | |
1900 | * to that of the outbound ifp. This will allow | |
1901 | * us to determine that it is a legitimate packet | |
1902 | * for the system. Only set the ifp if it's not | |
1903 | * already set, just to be safe. | |
1904 | */ | |
1905 | if ((m->m_flags & (M_BCAST | M_LOOP)) && | |
1906 | m->m_pkthdr.rcvif == NULL) { | |
1907 | m->m_pkthdr.rcvif = ifp; | |
1908 | rcvif_set = 1; | |
1909 | } | |
1910 | ||
6d2010ae A |
1911 | retval = ifp->if_framer(ifp, &m, dest, dst_linkaddr, |
1912 | frame_type); | |
2d21ac55 | 1913 | if (retval) { |
6d2010ae | 1914 | if (retval != EJUSTRETURN) |
2d21ac55 | 1915 | m_freem(m); |
2d21ac55 | 1916 | goto next; |
91447636 | 1917 | } |
7e4a7d39 A |
1918 | |
1919 | /* | |
1920 | * Clear the ifp if it was set above, and to be | |
1921 | * safe, only if it is still the same as the | |
1922 | * outbound ifp we have in context. If it was | |
1923 | * looped back, then a copy of it was sent to the | |
1924 | * loopback interface with the rcvif set, and we | |
1925 | * are clearing the one that will go down to the | |
1926 | * layer below. | |
1927 | */ | |
1928 | if (rcvif_set && m->m_pkthdr.rcvif == ifp) | |
1929 | m->m_pkthdr.rcvif = NULL; | |
91447636 | 1930 | } |
6d2010ae A |
1931 | |
1932 | /* | |
2d21ac55 A |
1933 | * Let interface filters (if any) do their thing ... |
1934 | */ | |
1935 | /* Do not pass VLAN tagged packets to filters PR-3586856 */ | |
1936 | if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) == 0) { | |
6d2010ae A |
1937 | retval = dlil_interface_filters_output(ifp, |
1938 | &m, proto_family); | |
1939 | if (retval != 0) { | |
1940 | if (retval != EJUSTRETURN) | |
1941 | m_freem(m); | |
1942 | goto next; | |
1c79356b | 1943 | } |
1c79356b | 1944 | } |
b7266188 A |
1945 | /* |
1946 | * Strip away M_PROTO1 bit prior to sending packet to the driver | |
1947 | * as this field may be used by the driver | |
1948 | */ | |
1949 | m->m_flags &= ~M_PROTO1; | |
1950 | ||
2d21ac55 A |
1951 | /* |
1952 | * If the underlying interface is not capable of handling a | |
1953 | * packet whose data portion spans across physically disjoint | |
1954 | * pages, we need to "normalize" the packet so that we pass | |
1955 | * down a chain of mbufs where each mbuf points to a span that | |
1956 | * resides in the system page boundary. If the packet does | |
1957 | * not cross page(s), the following is a no-op. | |
1958 | */ | |
1959 | if (!(ifp->if_hwassist & IFNET_MULTIPAGES)) { | |
1960 | if ((m = m_normalize(m)) == NULL) | |
1961 | goto next; | |
1962 | } | |
1963 | ||
6d2010ae A |
1964 | /* |
1965 | * If this is a TSO packet, make sure the interface still | |
1966 | * advertise TSO capability. | |
b0d623f7 A |
1967 | */ |
1968 | ||
6d2010ae A |
1969 | if ((m->m_pkthdr.csum_flags & CSUM_TSO_IPV4) && |
1970 | !(ifp->if_hwassist & IFNET_TSO_IPV4)) { | |
1971 | retval = EMSGSIZE; | |
1972 | m_freem(m); | |
1973 | goto cleanup; | |
b0d623f7 A |
1974 | } |
1975 | ||
6d2010ae A |
1976 | if ((m->m_pkthdr.csum_flags & CSUM_TSO_IPV6) && |
1977 | !(ifp->if_hwassist & IFNET_TSO_IPV6)) { | |
1978 | retval = EMSGSIZE; | |
1979 | m_freem(m); | |
1980 | goto cleanup; | |
b0d623f7 | 1981 | } |
6d2010ae | 1982 | |
2d21ac55 A |
1983 | /* |
1984 | * Finally, call the driver. | |
1985 | */ | |
2d21ac55 A |
1986 | if ((ifp->if_eflags & IFEF_SENDLIST) != 0) { |
1987 | *send_tail = m; | |
1988 | send_tail = &m->m_nextpkt; | |
6d2010ae | 1989 | } else { |
d41d1dae | 1990 | if_inc_traffic_class_out(ifp, m); |
6d2010ae A |
1991 | KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_START, |
1992 | 0,0,0,0,0); | |
2d21ac55 | 1993 | retval = ifp->if_output(ifp, m); |
b0d623f7 | 1994 | if (retval && dlil_verbose) { |
6d2010ae A |
1995 | printf("%s: output error on %s%d retval = %d\n", |
1996 | __func__, ifp->if_name, ifp->if_unit, | |
1997 | retval); | |
2d21ac55 | 1998 | } |
6d2010ae A |
1999 | KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, |
2000 | 0,0,0,0,0); | |
2d21ac55 A |
2001 | } |
2002 | KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0); | |
2003 | ||
2004 | next: | |
2005 | m = packetlist; | |
2006 | if (m) { | |
2007 | packetlist = packetlist->m_nextpkt; | |
2008 | m->m_nextpkt = NULL; | |
2009 | } | |
2010 | } while (m); | |
2011 | ||
2012 | if (send_head) { | |
d41d1dae A |
2013 | if_inc_traffic_class_out(ifp, send_head); |
2014 | ||
6d2010ae | 2015 | KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_START, 0,0,0,0,0); |
2d21ac55 | 2016 | retval = ifp->if_output(ifp, send_head); |
b0d623f7 | 2017 | if (retval && dlil_verbose) { |
6d2010ae A |
2018 | printf("%s: output error on %s%d retval = %d\n", |
2019 | __func__, ifp->if_name, ifp->if_unit, retval); | |
2d21ac55 A |
2020 | } |
2021 | KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0); | |
1c79356b | 2022 | } |
6d2010ae | 2023 | |
91447636 | 2024 | KERNEL_DEBUG(DBG_FNC_DLIL_OUTPUT | DBG_FUNC_END,0,0,0,0,0); |
1c79356b | 2025 | |
91447636 | 2026 | cleanup: |
6d2010ae A |
2027 | if (proto != NULL) |
2028 | if_proto_free(proto); | |
2029 | if (packetlist) /* if any packets are left, clean up */ | |
2d21ac55 | 2030 | mbuf_freem_list(packetlist); |
91447636 A |
2031 | if (retval == EJUSTRETURN) |
2032 | retval = 0; | |
6d2010ae A |
2033 | if (iorefcnt == 1) |
2034 | ifnet_decr_iorefcnt(ifp); | |
2035 | ||
2036 | return (retval); | |
1c79356b A |
2037 | } |
2038 | ||
2d21ac55 | 2039 | errno_t |
6d2010ae A |
2040 | ifnet_ioctl(ifnet_t ifp, protocol_family_t proto_fam, u_long ioctl_code, |
2041 | void *ioctl_arg) | |
2042 | { | |
2043 | struct ifnet_filter *filter; | |
2044 | int retval = EOPNOTSUPP; | |
2045 | int result = 0; | |
2046 | ||
2d21ac55 | 2047 | if (ifp == NULL || ioctl_code == 0) |
6d2010ae A |
2048 | return (EINVAL); |
2049 | ||
2050 | /* Get an io ref count if the interface is attached */ | |
2051 | if (!ifnet_is_attached(ifp, 1)) | |
2052 | return (EOPNOTSUPP); | |
2053 | ||
91447636 A |
2054 | /* Run the interface filters first. |
2055 | * We want to run all filters before calling the protocol, | |
2056 | * interface family, or interface. | |
2057 | */ | |
6d2010ae A |
2058 | lck_mtx_lock_spin(&ifp->if_flt_lock); |
2059 | /* prevent filter list from changing in case we drop the lock */ | |
2060 | if_flt_monitor_busy(ifp); | |
91447636 | 2061 | TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) { |
6d2010ae A |
2062 | if (filter->filt_ioctl != NULL && (filter->filt_protocol == 0 || |
2063 | filter->filt_protocol == proto_fam)) { | |
2064 | lck_mtx_unlock(&ifp->if_flt_lock); | |
2065 | ||
2066 | result = filter->filt_ioctl(filter->filt_cookie, ifp, | |
2067 | proto_fam, ioctl_code, ioctl_arg); | |
2068 | ||
2069 | lck_mtx_lock_spin(&ifp->if_flt_lock); | |
2070 | ||
91447636 A |
2071 | /* Only update retval if no one has handled the ioctl */ |
2072 | if (retval == EOPNOTSUPP || result == EJUSTRETURN) { | |
2073 | if (result == ENOTSUP) | |
2074 | result = EOPNOTSUPP; | |
2075 | retval = result; | |
6d2010ae A |
2076 | if (retval != 0 && retval != EOPNOTSUPP) { |
2077 | /* we're done with the filter list */ | |
2078 | if_flt_monitor_unbusy(ifp); | |
2079 | lck_mtx_unlock(&ifp->if_flt_lock); | |
91447636 A |
2080 | goto cleanup; |
2081 | } | |
2082 | } | |
2083 | } | |
2084 | } | |
6d2010ae A |
2085 | /* we're done with the filter list */ |
2086 | if_flt_monitor_unbusy(ifp); | |
2087 | lck_mtx_unlock(&ifp->if_flt_lock); | |
2088 | ||
91447636 | 2089 | /* Allow the protocol to handle the ioctl */ |
6d2010ae A |
2090 | if (proto_fam != 0) { |
2091 | struct if_proto *proto; | |
2092 | ||
2093 | /* callee holds a proto refcnt upon success */ | |
2094 | ifnet_lock_shared(ifp); | |
2095 | proto = find_attached_proto(ifp, proto_fam); | |
2096 | ifnet_lock_done(ifp); | |
2097 | if (proto != NULL) { | |
2098 | proto_media_ioctl ioctlp = | |
2099 | (proto->proto_kpi == kProtoKPI_v1 ? | |
2100 | proto->kpi.v1.ioctl : proto->kpi.v2.ioctl); | |
91447636 | 2101 | result = EOPNOTSUPP; |
6d2010ae A |
2102 | if (ioctlp != NULL) |
2103 | result = ioctlp(ifp, proto_fam, ioctl_code, | |
2104 | ioctl_arg); | |
2105 | if_proto_free(proto); | |
2106 | ||
91447636 A |
2107 | /* Only update retval if no one has handled the ioctl */ |
2108 | if (retval == EOPNOTSUPP || result == EJUSTRETURN) { | |
2109 | if (result == ENOTSUP) | |
2110 | result = EOPNOTSUPP; | |
2111 | retval = result; | |
6d2010ae | 2112 | if (retval && retval != EOPNOTSUPP) |
91447636 | 2113 | goto cleanup; |
91447636 A |
2114 | } |
2115 | } | |
2116 | } | |
6d2010ae | 2117 | |
91447636 | 2118 | /* retval is either 0 or EOPNOTSUPP */ |
6d2010ae | 2119 | |
91447636 A |
2120 | /* |
2121 | * Let the interface handle this ioctl. | |
2122 | * If it returns EOPNOTSUPP, ignore that, we may have | |
2123 | * already handled this in the protocol or family. | |
2124 | */ | |
6d2010ae | 2125 | if (ifp->if_ioctl) |
91447636 | 2126 | result = (*ifp->if_ioctl)(ifp, ioctl_code, ioctl_arg); |
6d2010ae | 2127 | |
91447636 A |
2128 | /* Only update retval if no one has handled the ioctl */ |
2129 | if (retval == EOPNOTSUPP || result == EJUSTRETURN) { | |
2130 | if (result == ENOTSUP) | |
2131 | result = EOPNOTSUPP; | |
2132 | retval = result; | |
2133 | if (retval && retval != EOPNOTSUPP) { | |
2134 | goto cleanup; | |
2135 | } | |
2136 | } | |
1c79356b | 2137 | |
6d2010ae | 2138 | cleanup: |
91447636 A |
2139 | if (retval == EJUSTRETURN) |
2140 | retval = 0; | |
6d2010ae A |
2141 | |
2142 | ifnet_decr_iorefcnt(ifp); | |
2143 | ||
2144 | return (retval); | |
91447636 | 2145 | } |
1c79356b | 2146 | |
91447636 | 2147 | __private_extern__ errno_t |
6d2010ae | 2148 | dlil_set_bpf_tap(ifnet_t ifp, bpf_tap_mode mode, bpf_packet_func callback) |
91447636 A |
2149 | { |
2150 | errno_t error = 0; | |
6d2010ae A |
2151 | |
2152 | ||
2153 | if (ifp->if_set_bpf_tap) { | |
2154 | /* Get an io reference on the interface if it is attached */ | |
2155 | if (!ifnet_is_attached(ifp, 1)) | |
2156 | return ENXIO; | |
91447636 | 2157 | error = ifp->if_set_bpf_tap(ifp, mode, callback); |
6d2010ae A |
2158 | ifnet_decr_iorefcnt(ifp); |
2159 | } | |
2160 | return (error); | |
1c79356b A |
2161 | } |
2162 | ||
2d21ac55 | 2163 | errno_t |
6d2010ae A |
2164 | dlil_resolve_multi(struct ifnet *ifp, const struct sockaddr *proto_addr, |
2165 | struct sockaddr *ll_addr, size_t ll_len) | |
1c79356b | 2166 | { |
91447636 A |
2167 | errno_t result = EOPNOTSUPP; |
2168 | struct if_proto *proto; | |
2169 | const struct sockaddr *verify; | |
2d21ac55 | 2170 | proto_media_resolve_multi resolvep; |
6d2010ae A |
2171 | |
2172 | if (!ifnet_is_attached(ifp, 1)) | |
2173 | return result; | |
2174 | ||
91447636 | 2175 | bzero(ll_addr, ll_len); |
6d2010ae A |
2176 | |
2177 | /* Call the protocol first; callee holds a proto refcnt upon success */ | |
2178 | ifnet_lock_shared(ifp); | |
91447636 | 2179 | proto = find_attached_proto(ifp, proto_addr->sa_family); |
6d2010ae | 2180 | ifnet_lock_done(ifp); |
2d21ac55 | 2181 | if (proto != NULL) { |
6d2010ae A |
2182 | resolvep = (proto->proto_kpi == kProtoKPI_v1 ? |
2183 | proto->kpi.v1.resolve_multi : proto->kpi.v2.resolve_multi); | |
2d21ac55 | 2184 | if (resolvep != NULL) |
6d2010ae A |
2185 | result = resolvep(ifp, proto_addr, |
2186 | (struct sockaddr_dl*)ll_addr, ll_len); | |
2187 | if_proto_free(proto); | |
91447636 | 2188 | } |
6d2010ae | 2189 | |
91447636 A |
2190 | /* Let the interface verify the multicast address */ |
2191 | if ((result == EOPNOTSUPP || result == 0) && ifp->if_check_multi) { | |
2192 | if (result == 0) | |
2193 | verify = ll_addr; | |
2194 | else | |
2195 | verify = proto_addr; | |
2196 | result = ifp->if_check_multi(ifp, verify); | |
2197 | } | |
6d2010ae A |
2198 | |
2199 | ifnet_decr_iorefcnt(ifp); | |
2200 | return (result); | |
91447636 | 2201 | } |
1c79356b | 2202 | |
91447636 | 2203 | __private_extern__ errno_t |
6d2010ae A |
2204 | dlil_send_arp_internal(ifnet_t ifp, u_short arpop, |
2205 | const struct sockaddr_dl* sender_hw, const struct sockaddr* sender_proto, | |
2206 | const struct sockaddr_dl* target_hw, const struct sockaddr* target_proto) | |
91447636 A |
2207 | { |
2208 | struct if_proto *proto; | |
2209 | errno_t result = 0; | |
6d2010ae A |
2210 | |
2211 | /* callee holds a proto refcnt upon success */ | |
2212 | ifnet_lock_shared(ifp); | |
91447636 | 2213 | proto = find_attached_proto(ifp, target_proto->sa_family); |
6d2010ae | 2214 | ifnet_lock_done(ifp); |
2d21ac55 | 2215 | if (proto == NULL) { |
91447636 | 2216 | result = ENOTSUP; |
6d2010ae | 2217 | } else { |
2d21ac55 | 2218 | proto_media_send_arp arpp; |
6d2010ae A |
2219 | arpp = (proto->proto_kpi == kProtoKPI_v1 ? |
2220 | proto->kpi.v1.send_arp : proto->kpi.v2.send_arp); | |
2d21ac55 A |
2221 | if (arpp == NULL) |
2222 | result = ENOTSUP; | |
2223 | else | |
6d2010ae A |
2224 | result = arpp(ifp, arpop, sender_hw, sender_proto, |
2225 | target_hw, target_proto); | |
2226 | if_proto_free(proto); | |
91447636 | 2227 | } |
6d2010ae A |
2228 | |
2229 | return (result); | |
91447636 | 2230 | } |
1c79356b | 2231 | |
2d21ac55 A |
2232 | static __inline__ int |
2233 | _is_announcement(const struct sockaddr_in * sender_sin, | |
6d2010ae | 2234 | const struct sockaddr_in * target_sin) |
2d21ac55 A |
2235 | { |
2236 | if (sender_sin == NULL) { | |
6d2010ae | 2237 | return (FALSE); |
2d21ac55 A |
2238 | } |
2239 | return (sender_sin->sin_addr.s_addr == target_sin->sin_addr.s_addr); | |
2240 | } | |
2241 | ||
91447636 | 2242 | __private_extern__ errno_t |
6d2010ae A |
2243 | dlil_send_arp(ifnet_t ifp, u_short arpop, const struct sockaddr_dl* sender_hw, |
2244 | const struct sockaddr* sender_proto, const struct sockaddr_dl* target_hw, | |
2245 | const struct sockaddr* target_proto) | |
91447636 A |
2246 | { |
2247 | errno_t result = 0; | |
2d21ac55 A |
2248 | const struct sockaddr_in * sender_sin; |
2249 | const struct sockaddr_in * target_sin; | |
6d2010ae A |
2250 | |
2251 | if (target_proto == NULL || (sender_proto != NULL && | |
2252 | sender_proto->sa_family != target_proto->sa_family)) | |
2253 | return (EINVAL); | |
2254 | ||
91447636 A |
2255 | /* |
2256 | * If this is an ARP request and the target IP is IPv4LL, | |
2d21ac55 A |
2257 | * send the request on all interfaces. The exception is |
2258 | * an announcement, which must only appear on the specific | |
2259 | * interface. | |
91447636 | 2260 | */ |
2d21ac55 A |
2261 | sender_sin = (const struct sockaddr_in *)sender_proto; |
2262 | target_sin = (const struct sockaddr_in *)target_proto; | |
6d2010ae A |
2263 | if (target_proto->sa_family == AF_INET && |
2264 | IN_LINKLOCAL(ntohl(target_sin->sin_addr.s_addr)) && | |
2265 | ipv4_ll_arp_aware != 0 && arpop == ARPOP_REQUEST && | |
2266 | !_is_announcement(target_sin, sender_sin)) { | |
91447636 A |
2267 | ifnet_t *ifp_list; |
2268 | u_int32_t count; | |
2269 | u_int32_t ifp_on; | |
6d2010ae | 2270 | |
91447636 A |
2271 | result = ENOTSUP; |
2272 | ||
2273 | if (ifnet_list_get(IFNET_FAMILY_ANY, &ifp_list, &count) == 0) { | |
2274 | for (ifp_on = 0; ifp_on < count; ifp_on++) { | |
6d2010ae A |
2275 | errno_t new_result; |
2276 | ifaddr_t source_hw = NULL; | |
2277 | ifaddr_t source_ip = NULL; | |
2278 | struct sockaddr_in source_ip_copy; | |
2279 | struct ifnet *cur_ifp = ifp_list[ifp_on]; | |
2280 | ||
91447636 | 2281 | /* |
6d2010ae A |
2282 | * Only arp on interfaces marked for IPv4LL |
2283 | * ARPing. This may mean that we don't ARP on | |
2284 | * the interface the subnet route points to. | |
91447636 | 2285 | */ |
6d2010ae | 2286 | if (!(cur_ifp->if_eflags & IFEF_ARPLL)) |
91447636 | 2287 | continue; |
b0d623f7 | 2288 | |
91447636 | 2289 | /* Find the source IP address */ |
6d2010ae A |
2290 | ifnet_lock_shared(cur_ifp); |
2291 | source_hw = cur_ifp->if_lladdr; | |
2292 | TAILQ_FOREACH(source_ip, &cur_ifp->if_addrhead, | |
2293 | ifa_link) { | |
2294 | IFA_LOCK(source_ip); | |
2295 | if (source_ip->ifa_addr != NULL && | |
2296 | source_ip->ifa_addr->sa_family == | |
2297 | AF_INET) { | |
2298 | /* Copy the source IP address */ | |
2299 | source_ip_copy = | |
2300 | *(struct sockaddr_in *) | |
2301 | source_ip->ifa_addr; | |
2302 | IFA_UNLOCK(source_ip); | |
91447636 A |
2303 | break; |
2304 | } | |
6d2010ae | 2305 | IFA_UNLOCK(source_ip); |
91447636 | 2306 | } |
6d2010ae | 2307 | |
91447636 A |
2308 | /* No IP Source, don't arp */ |
2309 | if (source_ip == NULL) { | |
6d2010ae | 2310 | ifnet_lock_done(cur_ifp); |
91447636 A |
2311 | continue; |
2312 | } | |
6d2010ae A |
2313 | |
2314 | IFA_ADDREF(source_hw); | |
2315 | ifnet_lock_done(cur_ifp); | |
2316 | ||
91447636 | 2317 | /* Send the ARP */ |
6d2010ae A |
2318 | new_result = dlil_send_arp_internal(cur_ifp, |
2319 | arpop, | |
2320 | (struct sockaddr_dl *)source_hw->ifa_addr, | |
2321 | (struct sockaddr *)&source_ip_copy, NULL, | |
2322 | target_proto); | |
b0d623f7 | 2323 | |
6d2010ae | 2324 | IFA_REMREF(source_hw); |
91447636 A |
2325 | if (result == ENOTSUP) { |
2326 | result = new_result; | |
2327 | } | |
2328 | } | |
6d2010ae | 2329 | ifnet_list_free(ifp_list); |
91447636 | 2330 | } |
6d2010ae A |
2331 | } else { |
2332 | result = dlil_send_arp_internal(ifp, arpop, sender_hw, | |
2333 | sender_proto, target_hw, target_proto); | |
91447636 | 2334 | } |
6d2010ae A |
2335 | |
2336 | return (result); | |
91447636 | 2337 | } |
1c79356b | 2338 | |
6d2010ae A |
2339 | /* |
2340 | * Caller must hold ifnet head lock. | |
2341 | */ | |
2342 | static int | |
2343 | ifnet_lookup(struct ifnet *ifp) | |
91447636 | 2344 | { |
6d2010ae A |
2345 | struct ifnet *_ifp; |
2346 | ||
2347 | lck_rw_assert(&ifnet_head_lock, LCK_RW_ASSERT_HELD); | |
2348 | TAILQ_FOREACH(_ifp, &ifnet_head, if_link) { | |
2349 | if (_ifp == ifp) | |
91447636 | 2350 | break; |
6d2010ae A |
2351 | } |
2352 | return (_ifp != NULL); | |
91447636 | 2353 | } |
6d2010ae A |
2354 | /* |
2355 | * Caller has to pass a non-zero refio argument to get a | |
2356 | * IO reference count. This will prevent ifnet_detach from | |
2357 | * being called when there are outstanding io reference counts. | |
91447636 | 2358 | */ |
6d2010ae A |
2359 | int |
2360 | ifnet_is_attached(struct ifnet *ifp, int refio) | |
2361 | { | |
2362 | int ret; | |
2363 | ||
2364 | lck_mtx_lock_spin(&ifp->if_ref_lock); | |
2365 | if ((ret = ((ifp->if_refflags & (IFRF_ATTACHED | IFRF_DETACHING)) == | |
2366 | IFRF_ATTACHED))) { | |
2367 | if (refio > 0) | |
2368 | ifp->if_refio++; | |
2369 | } | |
2370 | lck_mtx_unlock(&ifp->if_ref_lock); | |
2371 | ||
2372 | return (ret); | |
2373 | } | |
2374 | ||
2375 | void | |
2376 | ifnet_decr_iorefcnt(struct ifnet *ifp) | |
2377 | { | |
2378 | lck_mtx_lock_spin(&ifp->if_ref_lock); | |
2379 | VERIFY(ifp->if_refio > 0); | |
2380 | VERIFY((ifp->if_refflags & (IFRF_ATTACHED | IFRF_DETACHING)) != 0); | |
2381 | ifp->if_refio--; | |
2382 | ||
2383 | /* if there are no more outstanding io references, wakeup the | |
2384 | * ifnet_detach thread if detaching flag is set. | |
2385 | */ | |
2386 | if (ifp->if_refio == 0 && | |
2387 | (ifp->if_refflags & IFRF_DETACHING) != 0) { | |
2388 | /* Convert the spinlock to a regular mutex if we have | |
2389 | * to wait for any reason while doing a wakeup. | |
91447636 | 2390 | */ |
6d2010ae A |
2391 | lck_mtx_convert_spin(&ifp->if_ref_lock); |
2392 | wakeup(&(ifp->if_refio)); | |
91447636 | 2393 | } |
6d2010ae A |
2394 | lck_mtx_unlock(&ifp->if_ref_lock); |
2395 | } | |
b0d623f7 | 2396 | |
6d2010ae A |
2397 | static void |
2398 | dlil_if_trace(struct dlil_ifnet *dl_if, int refhold) | |
2399 | { | |
2400 | struct dlil_ifnet_dbg *dl_if_dbg = (struct dlil_ifnet_dbg *)dl_if; | |
2401 | ctrace_t *tr; | |
2402 | u_int32_t idx; | |
2403 | u_int16_t *cnt; | |
1c79356b | 2404 | |
6d2010ae A |
2405 | if (!(dl_if->dl_if_flags & DLIF_DEBUG)) { |
2406 | panic("%s: dl_if %p has no debug structure", __func__, dl_if); | |
2407 | /* NOTREACHED */ | |
2408 | } | |
2409 | ||
2410 | if (refhold) { | |
2411 | cnt = &dl_if_dbg->dldbg_if_refhold_cnt; | |
2412 | tr = dl_if_dbg->dldbg_if_refhold; | |
2413 | } else { | |
2414 | cnt = &dl_if_dbg->dldbg_if_refrele_cnt; | |
2415 | tr = dl_if_dbg->dldbg_if_refrele; | |
2416 | } | |
2417 | ||
2418 | idx = atomic_add_16_ov(cnt, 1) % IF_REF_TRACE_HIST_SIZE; | |
2419 | ctrace_record(&tr[idx]); | |
91447636 | 2420 | } |
1c79356b | 2421 | |
6d2010ae A |
2422 | errno_t |
2423 | dlil_if_ref(struct ifnet *ifp) | |
2424 | { | |
2425 | struct dlil_ifnet *dl_if = (struct dlil_ifnet *)ifp; | |
2426 | ||
2427 | if (dl_if == NULL) | |
2428 | return (EINVAL); | |
2429 | ||
2430 | lck_mtx_lock_spin(&dl_if->dl_if_lock); | |
2431 | ++dl_if->dl_if_refcnt; | |
2432 | if (dl_if->dl_if_refcnt == 0) { | |
2433 | panic("%s: wraparound refcnt for ifp=%p", __func__, ifp); | |
2434 | /* NOTREACHED */ | |
2435 | } | |
2436 | if (dl_if->dl_if_trace != NULL) | |
2437 | (*dl_if->dl_if_trace)(dl_if, TRUE); | |
2438 | lck_mtx_unlock(&dl_if->dl_if_lock); | |
2439 | ||
2440 | return (0); | |
91447636 | 2441 | } |
1c79356b | 2442 | |
6d2010ae A |
2443 | errno_t |
2444 | dlil_if_free(struct ifnet *ifp) | |
2445 | { | |
2446 | struct dlil_ifnet *dl_if = (struct dlil_ifnet *)ifp; | |
2447 | ||
2448 | if (dl_if == NULL) | |
2449 | return (EINVAL); | |
2450 | ||
2451 | lck_mtx_lock_spin(&dl_if->dl_if_lock); | |
2452 | if (dl_if->dl_if_refcnt == 0) { | |
2453 | panic("%s: negative refcnt for ifp=%p", __func__, ifp); | |
2454 | /* NOTREACHED */ | |
2455 | } | |
2456 | --dl_if->dl_if_refcnt; | |
2457 | if (dl_if->dl_if_trace != NULL) | |
2458 | (*dl_if->dl_if_trace)(dl_if, FALSE); | |
2459 | lck_mtx_unlock(&dl_if->dl_if_lock); | |
2460 | ||
2461 | return (0); | |
2462 | } | |
1c79356b | 2463 | |
2d21ac55 | 2464 | static errno_t |
6d2010ae A |
2465 | dlil_attach_protocol_internal(struct if_proto *proto, |
2466 | const struct ifnet_demux_desc *demux_list, u_int32_t demux_count) | |
91447636 | 2467 | { |
6d2010ae | 2468 | struct kev_dl_proto_data ev_pr_data; |
91447636 A |
2469 | struct ifnet *ifp = proto->ifp; |
2470 | int retval = 0; | |
b0d623f7 | 2471 | u_int32_t hash_value = proto_hash_value(proto->protocol_family); |
6d2010ae A |
2472 | struct if_proto *prev_proto; |
2473 | struct if_proto *_proto; | |
2474 | ||
2475 | /* callee holds a proto refcnt upon success */ | |
2476 | ifnet_lock_exclusive(ifp); | |
2477 | _proto = find_attached_proto(ifp, proto->protocol_family); | |
2478 | if (_proto != NULL) { | |
91447636 | 2479 | ifnet_lock_done(ifp); |
6d2010ae A |
2480 | if_proto_free(_proto); |
2481 | return (EEXIST); | |
91447636 | 2482 | } |
6d2010ae | 2483 | |
91447636 A |
2484 | /* |
2485 | * Call family module add_proto routine so it can refine the | |
2486 | * demux descriptors as it wishes. | |
2487 | */ | |
6d2010ae A |
2488 | retval = ifp->if_add_proto(ifp, proto->protocol_family, demux_list, |
2489 | demux_count); | |
91447636 | 2490 | if (retval) { |
6d2010ae A |
2491 | ifnet_lock_done(ifp); |
2492 | return (retval); | |
91447636 | 2493 | } |
6d2010ae | 2494 | |
91447636 A |
2495 | /* |
2496 | * Insert the protocol in the hash | |
2497 | */ | |
6d2010ae A |
2498 | prev_proto = SLIST_FIRST(&ifp->if_proto_hash[hash_value]); |
2499 | while (prev_proto != NULL && SLIST_NEXT(prev_proto, next_hash) != NULL) | |
2500 | prev_proto = SLIST_NEXT(prev_proto, next_hash); | |
2501 | if (prev_proto) | |
2502 | SLIST_INSERT_AFTER(prev_proto, proto, next_hash); | |
2503 | else | |
2504 | SLIST_INSERT_HEAD(&ifp->if_proto_hash[hash_value], | |
2505 | proto, next_hash); | |
2506 | ||
2507 | /* hold a proto refcnt for attach */ | |
2508 | if_proto_ref(proto); | |
1c79356b | 2509 | |
91447636 | 2510 | /* |
6d2010ae A |
2511 | * The reserved field carries the number of protocol still attached |
2512 | * (subject to change) | |
91447636 | 2513 | */ |
91447636 A |
2514 | ev_pr_data.proto_family = proto->protocol_family; |
2515 | ev_pr_data.proto_remaining_count = dlil_ifp_proto_count(ifp); | |
6d2010ae A |
2516 | ifnet_lock_done(ifp); |
2517 | ||
2518 | dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_PROTO_ATTACHED, | |
2519 | (struct net_event_data *)&ev_pr_data, | |
2520 | sizeof (struct kev_dl_proto_data)); | |
2521 | return (retval); | |
91447636 | 2522 | } |
0b4e3aa0 | 2523 | |
2d21ac55 A |
2524 | errno_t |
2525 | ifnet_attach_protocol(ifnet_t ifp, protocol_family_t protocol, | |
6d2010ae | 2526 | const struct ifnet_attach_proto_param *proto_details) |
91447636 A |
2527 | { |
2528 | int retval = 0; | |
2529 | struct if_proto *ifproto = NULL; | |
6d2010ae A |
2530 | |
2531 | ifnet_head_lock_shared(); | |
2532 | if (ifp == NULL || protocol == 0 || proto_details == NULL) { | |
2533 | retval = EINVAL; | |
2534 | goto end; | |
2535 | } | |
2536 | /* Check that the interface is in the global list */ | |
2537 | if (!ifnet_lookup(ifp)) { | |
2538 | retval = ENXIO; | |
2539 | goto end; | |
2540 | } | |
2541 | ||
2542 | ifproto = zalloc(dlif_proto_zone); | |
2543 | if (ifproto == NULL) { | |
91447636 A |
2544 | retval = ENOMEM; |
2545 | goto end; | |
2546 | } | |
6d2010ae A |
2547 | bzero(ifproto, dlif_proto_size); |
2548 | ||
2549 | /* refcnt held above during lookup */ | |
91447636 A |
2550 | ifproto->ifp = ifp; |
2551 | ifproto->protocol_family = protocol; | |
2552 | ifproto->proto_kpi = kProtoKPI_v1; | |
2553 | ifproto->kpi.v1.input = proto_details->input; | |
2554 | ifproto->kpi.v1.pre_output = proto_details->pre_output; | |
2555 | ifproto->kpi.v1.event = proto_details->event; | |
2556 | ifproto->kpi.v1.ioctl = proto_details->ioctl; | |
2557 | ifproto->kpi.v1.detached = proto_details->detached; | |
2558 | ifproto->kpi.v1.resolve_multi = proto_details->resolve; | |
2559 | ifproto->kpi.v1.send_arp = proto_details->send_arp; | |
6d2010ae | 2560 | |
2d21ac55 | 2561 | retval = dlil_attach_protocol_internal(ifproto, |
6d2010ae A |
2562 | proto_details->demux_list, proto_details->demux_count); |
2563 | ||
2564 | if (dlil_verbose) { | |
2565 | printf("%s%d: attached v1 protocol %d\n", ifp->if_name, | |
2566 | ifp->if_unit, protocol); | |
2567 | } | |
2568 | ||
9bccf70c | 2569 | end: |
6d2010ae A |
2570 | if (retval != 0 && retval != EEXIST && ifp != NULL) { |
2571 | DLIL_PRINTF("%s%d: failed to attach v1 protocol %d (err=%d)\n", | |
2572 | ifp->if_name, ifp->if_unit, protocol, retval); | |
2573 | } | |
2574 | ifnet_head_done(); | |
2575 | if (retval != 0 && ifproto != NULL) | |
2576 | zfree(dlif_proto_zone, ifproto); | |
2577 | return (retval); | |
1c79356b A |
2578 | } |
2579 | ||
2d21ac55 A |
2580 | errno_t |
2581 | ifnet_attach_protocol_v2(ifnet_t ifp, protocol_family_t protocol, | |
6d2010ae | 2582 | const struct ifnet_attach_proto_param_v2 *proto_details) |
91447636 | 2583 | { |
2d21ac55 | 2584 | int retval = 0; |
91447636 | 2585 | struct if_proto *ifproto = NULL; |
6d2010ae A |
2586 | |
2587 | ifnet_head_lock_shared(); | |
2588 | if (ifp == NULL || protocol == 0 || proto_details == NULL) { | |
2589 | retval = EINVAL; | |
2590 | goto end; | |
2591 | } | |
2592 | /* Check that the interface is in the global list */ | |
2593 | if (!ifnet_lookup(ifp)) { | |
2594 | retval = ENXIO; | |
2595 | goto end; | |
2596 | } | |
2597 | ||
2598 | ifproto = zalloc(dlif_proto_zone); | |
2599 | if (ifproto == NULL) { | |
91447636 A |
2600 | retval = ENOMEM; |
2601 | goto end; | |
2602 | } | |
2d21ac55 | 2603 | bzero(ifproto, sizeof(*ifproto)); |
6d2010ae A |
2604 | |
2605 | /* refcnt held above during lookup */ | |
2d21ac55 A |
2606 | ifproto->ifp = ifp; |
2607 | ifproto->protocol_family = protocol; | |
2608 | ifproto->proto_kpi = kProtoKPI_v2; | |
2609 | ifproto->kpi.v2.input = proto_details->input; | |
2610 | ifproto->kpi.v2.pre_output = proto_details->pre_output; | |
2611 | ifproto->kpi.v2.event = proto_details->event; | |
2612 | ifproto->kpi.v2.ioctl = proto_details->ioctl; | |
2613 | ifproto->kpi.v2.detached = proto_details->detached; | |
2614 | ifproto->kpi.v2.resolve_multi = proto_details->resolve; | |
2615 | ifproto->kpi.v2.send_arp = proto_details->send_arp; | |
1c79356b | 2616 | |
6d2010ae A |
2617 | retval = dlil_attach_protocol_internal(ifproto, |
2618 | proto_details->demux_list, proto_details->demux_count); | |
1c79356b | 2619 | |
6d2010ae A |
2620 | if (dlil_verbose) { |
2621 | printf("%s%d: attached v2 protocol %d\n", ifp->if_name, | |
2622 | ifp->if_unit, protocol); | |
91447636 | 2623 | } |
6d2010ae A |
2624 | |
2625 | end: | |
2626 | if (retval != 0 && retval != EEXIST && ifp != NULL) { | |
2627 | DLIL_PRINTF("%s%d: failed to attach v2 protocol %d (err=%d)\n", | |
2628 | ifp->if_name, ifp->if_unit, protocol, retval); | |
2d21ac55 | 2629 | } |
6d2010ae A |
2630 | ifnet_head_done(); |
2631 | if (retval != 0 && ifproto != NULL) | |
2632 | zfree(dlif_proto_zone, ifproto); | |
2633 | return (retval); | |
91447636 | 2634 | } |
1c79356b | 2635 | |
2d21ac55 A |
2636 | errno_t |
2637 | ifnet_detach_protocol(ifnet_t ifp, protocol_family_t proto_family) | |
91447636 A |
2638 | { |
2639 | struct if_proto *proto = NULL; | |
2640 | int retval = 0; | |
6d2010ae A |
2641 | |
2642 | if (ifp == NULL || proto_family == 0) { | |
2643 | retval = EINVAL; | |
91447636 A |
2644 | goto end; |
2645 | } | |
6d2010ae A |
2646 | |
2647 | ifnet_lock_exclusive(ifp); | |
2648 | /* callee holds a proto refcnt upon success */ | |
91447636 | 2649 | proto = find_attached_proto(ifp, proto_family); |
91447636 A |
2650 | if (proto == NULL) { |
2651 | retval = ENXIO; | |
6d2010ae | 2652 | ifnet_lock_done(ifp); |
91447636 A |
2653 | goto end; |
2654 | } | |
6d2010ae A |
2655 | |
2656 | /* call family module del_proto */ | |
91447636 A |
2657 | if (ifp->if_del_proto) |
2658 | ifp->if_del_proto(ifp, proto->protocol_family); | |
1c79356b | 2659 | |
6d2010ae A |
2660 | SLIST_REMOVE(&ifp->if_proto_hash[proto_hash_value(proto_family)], |
2661 | proto, if_proto, next_hash); | |
2662 | ||
2663 | if (proto->proto_kpi == kProtoKPI_v1) { | |
2664 | proto->kpi.v1.input = ifproto_media_input_v1; | |
2665 | proto->kpi.v1.pre_output= ifproto_media_preout; | |
2666 | proto->kpi.v1.event = ifproto_media_event; | |
2667 | proto->kpi.v1.ioctl = ifproto_media_ioctl; | |
2668 | proto->kpi.v1.resolve_multi = ifproto_media_resolve_multi; | |
2669 | proto->kpi.v1.send_arp = ifproto_media_send_arp; | |
2670 | } else { | |
2671 | proto->kpi.v2.input = ifproto_media_input_v2; | |
2672 | proto->kpi.v2.pre_output = ifproto_media_preout; | |
2673 | proto->kpi.v2.event = ifproto_media_event; | |
2674 | proto->kpi.v2.ioctl = ifproto_media_ioctl; | |
2675 | proto->kpi.v2.resolve_multi = ifproto_media_resolve_multi; | |
2676 | proto->kpi.v2.send_arp = ifproto_media_send_arp; | |
2677 | } | |
2678 | proto->detached = 1; | |
2679 | ifnet_lock_done(ifp); | |
2680 | ||
2681 | if (dlil_verbose) { | |
2682 | printf("%s%d: detached %s protocol %d\n", ifp->if_name, | |
2683 | ifp->if_unit, (proto->proto_kpi == kProtoKPI_v1) ? | |
2684 | "v1" : "v2", proto_family); | |
2685 | } | |
2686 | ||
2687 | /* release proto refcnt held during protocol attach */ | |
2688 | if_proto_free(proto); | |
91447636 A |
2689 | |
2690 | /* | |
6d2010ae A |
2691 | * Release proto refcnt held during lookup; the rest of |
2692 | * protocol detach steps will happen when the last proto | |
2693 | * reference is released. | |
91447636 | 2694 | */ |
6d2010ae A |
2695 | if_proto_free(proto); |
2696 | ||
91447636 | 2697 | end: |
6d2010ae | 2698 | return (retval); |
91447636 | 2699 | } |
1c79356b | 2700 | |
6d2010ae A |
2701 | |
2702 | static errno_t | |
2703 | ifproto_media_input_v1(struct ifnet *ifp, protocol_family_t protocol, | |
2704 | struct mbuf *packet, char *header) | |
91447636 | 2705 | { |
6d2010ae A |
2706 | #pragma unused(ifp, protocol, packet, header) |
2707 | return (ENXIO); | |
2708 | } | |
2709 | ||
2710 | static errno_t | |
2711 | ifproto_media_input_v2(struct ifnet *ifp, protocol_family_t protocol, | |
2712 | struct mbuf *packet) | |
2713 | { | |
2714 | #pragma unused(ifp, protocol, packet) | |
2715 | return (ENXIO); | |
2716 | ||
2717 | } | |
2718 | ||
2719 | static errno_t | |
2720 | ifproto_media_preout(struct ifnet *ifp, protocol_family_t protocol, | |
2721 | mbuf_t *packet, const struct sockaddr *dest, void *route, char *frame_type, | |
2722 | char *link_layer_dest) | |
2723 | { | |
2724 | #pragma unused(ifp, protocol, packet, dest, route, frame_type, link_layer_dest) | |
2725 | return (ENXIO); | |
9bccf70c | 2726 | |
91447636 | 2727 | } |
9bccf70c | 2728 | |
91447636 | 2729 | static void |
6d2010ae A |
2730 | ifproto_media_event(struct ifnet *ifp, protocol_family_t protocol, |
2731 | const struct kev_msg *event) | |
2732 | { | |
2733 | #pragma unused(ifp, protocol, event) | |
2734 | } | |
2735 | ||
2736 | static errno_t | |
2737 | ifproto_media_ioctl(struct ifnet *ifp, protocol_family_t protocol, | |
2738 | unsigned long command, void *argument) | |
2739 | { | |
2740 | #pragma unused(ifp, protocol, command, argument) | |
2741 | return (ENXIO); | |
2742 | } | |
2743 | ||
2744 | static errno_t | |
2745 | ifproto_media_resolve_multi(ifnet_t ifp, const struct sockaddr *proto_addr, | |
2746 | struct sockaddr_dl *out_ll, size_t ll_len) | |
2747 | { | |
2748 | #pragma unused(ifp, proto_addr, out_ll, ll_len) | |
2749 | return (ENXIO); | |
2750 | } | |
2751 | ||
2752 | static errno_t | |
2753 | ifproto_media_send_arp(struct ifnet *ifp, u_short arpop, | |
2754 | const struct sockaddr_dl *sender_hw, const struct sockaddr *sender_proto, | |
2755 | const struct sockaddr_dl *target_hw, const struct sockaddr *target_proto) | |
2756 | { | |
2757 | #pragma unused(ifp, arpop, sender_hw, sender_proto, target_hw, target_proto) | |
2758 | return (ENXIO); | |
91447636 | 2759 | } |
9bccf70c | 2760 | |
91447636 A |
2761 | extern int if_next_index(void); |
2762 | ||
2d21ac55 | 2763 | errno_t |
6d2010ae | 2764 | ifnet_attach(ifnet_t ifp, const struct sockaddr_dl *ll_addr) |
91447636 | 2765 | { |
91447636 | 2766 | struct ifnet *tmp_if; |
6d2010ae A |
2767 | struct ifaddr *ifa; |
2768 | struct if_data_internal if_data_saved; | |
2769 | struct dlil_ifnet *dl_if = (struct dlil_ifnet *)ifp; | |
1c79356b | 2770 | |
6d2010ae A |
2771 | if (ifp == NULL) |
2772 | return (EINVAL); | |
2773 | ||
7ddcb079 A |
2774 | /* |
2775 | * Serialize ifnet attach using dlil_ifnet_lock, in order to | |
2776 | * prevent the interface from being configured while it is | |
2777 | * embryonic, as ifnet_head_lock is dropped and reacquired | |
2778 | * below prior to marking the ifnet with IFRF_ATTACHED. | |
2779 | */ | |
2780 | dlil_if_lock(); | |
6d2010ae | 2781 | ifnet_head_lock_exclusive(); |
91447636 A |
2782 | /* Verify we aren't already on the list */ |
2783 | TAILQ_FOREACH(tmp_if, &ifnet_head, if_link) { | |
2784 | if (tmp_if == ifp) { | |
2785 | ifnet_head_done(); | |
7ddcb079 | 2786 | dlil_if_unlock(); |
6d2010ae | 2787 | return (EEXIST); |
91447636 A |
2788 | } |
2789 | } | |
0b4e3aa0 | 2790 | |
6d2010ae A |
2791 | lck_mtx_lock_spin(&ifp->if_ref_lock); |
2792 | if (ifp->if_refflags & IFRF_ATTACHED) { | |
2793 | panic("%s: flags mismatch (attached set) ifp=%p", | |
2794 | __func__, ifp); | |
2795 | /* NOTREACHED */ | |
91447636 | 2796 | } |
6d2010ae | 2797 | lck_mtx_unlock(&ifp->if_ref_lock); |
1c79356b | 2798 | |
6d2010ae | 2799 | ifnet_lock_exclusive(ifp); |
b0d623f7 | 2800 | |
6d2010ae A |
2801 | /* Sanity check */ |
2802 | VERIFY(ifp->if_detaching_link.tqe_next == NULL); | |
2803 | VERIFY(ifp->if_detaching_link.tqe_prev == NULL); | |
2804 | ||
2805 | if (ll_addr != NULL) { | |
2806 | if (ifp->if_addrlen == 0) { | |
2807 | ifp->if_addrlen = ll_addr->sdl_alen; | |
2808 | } else if (ll_addr->sdl_alen != ifp->if_addrlen) { | |
2809 | ifnet_lock_done(ifp); | |
2810 | ifnet_head_done(); | |
7ddcb079 | 2811 | dlil_if_unlock(); |
6d2010ae | 2812 | return (EINVAL); |
b0d623f7 A |
2813 | } |
2814 | } | |
2815 | ||
91447636 | 2816 | /* |
b0d623f7 | 2817 | * Allow interfaces without protocol families to attach |
91447636 A |
2818 | * only if they have the necessary fields filled out. |
2819 | */ | |
6d2010ae A |
2820 | if (ifp->if_add_proto == NULL || ifp->if_del_proto == NULL) { |
2821 | DLIL_PRINTF("%s: Attempt to attach interface without " | |
2822 | "family module - %d\n", __func__, ifp->if_family); | |
2823 | ifnet_lock_done(ifp); | |
2824 | ifnet_head_done(); | |
7ddcb079 | 2825 | dlil_if_unlock(); |
6d2010ae | 2826 | return (ENODEV); |
1c79356b A |
2827 | } |
2828 | ||
6d2010ae A |
2829 | /* Allocate protocol hash table */ |
2830 | VERIFY(ifp->if_proto_hash == NULL); | |
2831 | ifp->if_proto_hash = zalloc(dlif_phash_zone); | |
2832 | if (ifp->if_proto_hash == NULL) { | |
2833 | ifnet_lock_done(ifp); | |
2834 | ifnet_head_done(); | |
7ddcb079 | 2835 | dlil_if_unlock(); |
6d2010ae A |
2836 | return (ENOBUFS); |
2837 | } | |
2838 | bzero(ifp->if_proto_hash, dlif_phash_size); | |
91447636 | 2839 | |
6d2010ae A |
2840 | lck_mtx_lock_spin(&ifp->if_flt_lock); |
2841 | VERIFY(TAILQ_EMPTY(&ifp->if_flt_head)); | |
91447636 | 2842 | TAILQ_INIT(&ifp->if_flt_head); |
6d2010ae A |
2843 | VERIFY(ifp->if_flt_busy == 0); |
2844 | VERIFY(ifp->if_flt_waiters == 0); | |
2845 | lck_mtx_unlock(&ifp->if_flt_lock); | |
2846 | ||
2847 | VERIFY(TAILQ_EMPTY(&ifp->if_prefixhead)); | |
2848 | TAILQ_INIT(&ifp->if_prefixhead); | |
2849 | ||
2850 | if (!(dl_if->dl_if_flags & DLIF_REUSE)) { | |
2851 | VERIFY(LIST_EMPTY(&ifp->if_multiaddrs)); | |
91447636 | 2852 | LIST_INIT(&ifp->if_multiaddrs); |
6d2010ae | 2853 | } |
1c79356b | 2854 | |
6d2010ae A |
2855 | VERIFY(ifp->if_allhostsinm == NULL); |
2856 | VERIFY(TAILQ_EMPTY(&ifp->if_addrhead)); | |
2857 | TAILQ_INIT(&ifp->if_addrhead); | |
2858 | ||
2859 | if (ifp->if_snd.ifq_maxlen == 0) | |
2860 | ifp->if_snd.ifq_maxlen = ifqmaxlen; | |
2861 | ||
2862 | if (ifp->if_index == 0) { | |
2863 | int idx = if_next_index(); | |
2864 | ||
2865 | if (idx == -1) { | |
2866 | ifp->if_index = 0; | |
2867 | ifnet_lock_done(ifp); | |
2868 | ifnet_head_done(); | |
7ddcb079 | 2869 | dlil_if_unlock(); |
6d2010ae | 2870 | return (ENOBUFS); |
1c79356b | 2871 | } |
6d2010ae A |
2872 | ifp->if_index = idx; |
2873 | } | |
2874 | /* There should not be anything occupying this slot */ | |
2875 | VERIFY(ifindex2ifnet[ifp->if_index] == NULL); | |
2876 | ||
2877 | /* allocate (if needed) and initialize a link address */ | |
2878 | VERIFY(!(dl_if->dl_if_flags & DLIF_REUSE) || ifp->if_lladdr != NULL); | |
2879 | ifa = dlil_alloc_lladdr(ifp, ll_addr); | |
2880 | if (ifa == NULL) { | |
2881 | ifnet_lock_done(ifp); | |
2882 | ifnet_head_done(); | |
7ddcb079 | 2883 | dlil_if_unlock(); |
6d2010ae A |
2884 | return (ENOBUFS); |
2885 | } | |
2886 | ||
2887 | VERIFY(ifnet_addrs[ifp->if_index - 1] == NULL); | |
2888 | ifnet_addrs[ifp->if_index - 1] = ifa; | |
2889 | ||
2890 | /* make this address the first on the list */ | |
2891 | IFA_LOCK(ifa); | |
2892 | /* hold a reference for ifnet_addrs[] */ | |
2893 | IFA_ADDREF_LOCKED(ifa); | |
2894 | /* if_attach_link_ifa() holds a reference for ifa_link */ | |
2895 | if_attach_link_ifa(ifp, ifa); | |
2896 | IFA_UNLOCK(ifa); | |
2897 | ||
2d21ac55 | 2898 | #if CONFIG_MACF_NET |
6d2010ae | 2899 | mac_ifnet_label_associate(ifp); |
2d21ac55 | 2900 | #endif |
2d21ac55 | 2901 | |
6d2010ae A |
2902 | TAILQ_INSERT_TAIL(&ifnet_head, ifp, if_link); |
2903 | ifindex2ifnet[ifp->if_index] = ifp; | |
2d21ac55 | 2904 | |
6d2010ae A |
2905 | /* Hold a reference to the underlying dlil_ifnet */ |
2906 | ifnet_reference(ifp); | |
2907 | ||
2908 | /* | |
2909 | * A specific dlil input thread is created per Ethernet/cellular | |
2910 | * interface. pseudo interfaces or other types of interfaces use | |
2911 | * the main ("loopback") thread. | |
2912 | * | |
2913 | * If the sysctl "net.link.generic.system.multi_threaded_input" is set | |
2914 | * to zero, all packets will be handled by the main loopback thread, | |
2915 | * reverting to 10.4.x behaviour. | |
2916 | */ | |
2917 | if (dlil_multithreaded_input && | |
2918 | (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_CELLULAR)) { | |
2d21ac55 A |
2919 | int err; |
2920 | ||
6d2010ae A |
2921 | ifp->if_input_thread = zalloc(dlif_inp_zone); |
2922 | if (ifp->if_input_thread == NULL) { | |
2923 | panic("%s: ifp=%p couldn't alloc threading", | |
2924 | __func__, ifp); | |
2925 | /* NOTREACHED */ | |
2926 | } | |
2927 | bzero(ifp->if_input_thread, dlif_inp_size); | |
2928 | err = dlil_create_input_thread(ifp, ifp->if_input_thread); | |
2929 | if (err != 0) { | |
2930 | panic("%s: ifp=%p couldn't get a thread. " | |
2931 | "err=%d", __func__, ifp, err); | |
2932 | /* NOTREACHED */ | |
2933 | } | |
2d21ac55 | 2934 | #ifdef DLIL_DEBUG |
6d2010ae A |
2935 | printf("%s: dlil thread for ifp=%p if_index=%d\n", |
2936 | __func__, ifp, ifp->if_index); | |
2d21ac55 | 2937 | #endif |
91447636 | 2938 | } |
6d2010ae A |
2939 | |
2940 | /* Clear stats (save and restore other fields that we care) */ | |
2941 | if_data_saved = ifp->if_data; | |
2942 | bzero(&ifp->if_data, sizeof (ifp->if_data)); | |
2943 | ifp->if_data.ifi_type = if_data_saved.ifi_type; | |
2944 | ifp->if_data.ifi_typelen = if_data_saved.ifi_typelen; | |
2945 | ifp->if_data.ifi_physical = if_data_saved.ifi_physical; | |
2946 | ifp->if_data.ifi_addrlen = if_data_saved.ifi_addrlen; | |
2947 | ifp->if_data.ifi_hdrlen = if_data_saved.ifi_hdrlen; | |
2948 | ifp->if_data.ifi_mtu = if_data_saved.ifi_mtu; | |
2949 | ifp->if_data.ifi_baudrate = if_data_saved.ifi_baudrate; | |
2950 | ifp->if_data.ifi_hwassist = if_data_saved.ifi_hwassist; | |
2951 | ifp->if_data.ifi_tso_v4_mtu = if_data_saved.ifi_tso_v4_mtu; | |
2952 | ifp->if_data.ifi_tso_v6_mtu = if_data_saved.ifi_tso_v6_mtu; | |
2953 | ifnet_touch_lastchange(ifp); | |
2954 | ||
2955 | /* Record attach PC stacktrace */ | |
2956 | ctrace_record(&((struct dlil_ifnet *)ifp)->dl_if_attach); | |
2957 | ||
2958 | ifp->if_updatemcasts = 0; | |
2959 | if (!LIST_EMPTY(&ifp->if_multiaddrs)) { | |
2960 | struct ifmultiaddr *ifma; | |
2961 | LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { | |
2962 | IFMA_LOCK(ifma); | |
2963 | if (ifma->ifma_addr->sa_family == AF_LINK || | |
2964 | ifma->ifma_addr->sa_family == AF_UNSPEC) | |
2965 | ifp->if_updatemcasts++; | |
2966 | IFMA_UNLOCK(ifma); | |
2967 | } | |
2968 | ||
2969 | printf("%s%d: attached with %d suspended link-layer multicast " | |
2970 | "membership(s)\n", ifp->if_name, ifp->if_unit, | |
2971 | ifp->if_updatemcasts); | |
2972 | } | |
2973 | ||
0c530ab8 | 2974 | ifnet_lock_done(ifp); |
b0d623f7 | 2975 | ifnet_head_done(); |
6d2010ae A |
2976 | |
2977 | lck_mtx_lock(&ifp->if_cached_route_lock); | |
2978 | /* Enable forwarding cached route */ | |
2979 | ifp->if_fwd_cacheok = 1; | |
2980 | /* Clean up any existing cached routes */ | |
2981 | if (ifp->if_fwd_route.ro_rt != NULL) | |
2982 | rtfree(ifp->if_fwd_route.ro_rt); | |
2983 | bzero(&ifp->if_fwd_route, sizeof (ifp->if_fwd_route)); | |
2984 | if (ifp->if_src_route.ro_rt != NULL) | |
2985 | rtfree(ifp->if_src_route.ro_rt); | |
2986 | bzero(&ifp->if_src_route, sizeof (ifp->if_src_route)); | |
2987 | if (ifp->if_src_route6.ro_rt != NULL) | |
2988 | rtfree(ifp->if_src_route6.ro_rt); | |
2989 | bzero(&ifp->if_src_route6, sizeof (ifp->if_src_route6)); | |
2990 | lck_mtx_unlock(&ifp->if_cached_route_lock); | |
2991 | ||
2992 | ifnet_llreach_ifattach(ifp, (dl_if->dl_if_flags & DLIF_REUSE)); | |
2993 | ||
b0d623f7 | 2994 | /* |
6d2010ae A |
2995 | * Allocate and attach IGMPv3/MLDv2 interface specific variables |
2996 | * and trees; do this before the ifnet is marked as attached. | |
2997 | * The ifnet keeps the reference to the info structures even after | |
2998 | * the ifnet is detached, since the network-layer records still | |
2999 | * refer to the info structures even after that. This also | |
3000 | * makes it possible for them to still function after the ifnet | |
3001 | * is recycled or reattached. | |
b0d623f7 | 3002 | */ |
6d2010ae A |
3003 | #if INET |
3004 | if (IGMP_IFINFO(ifp) == NULL) { | |
3005 | IGMP_IFINFO(ifp) = igmp_domifattach(ifp, M_WAITOK); | |
3006 | VERIFY(IGMP_IFINFO(ifp) != NULL); | |
3007 | } else { | |
3008 | VERIFY(IGMP_IFINFO(ifp)->igi_ifp == ifp); | |
3009 | igmp_domifreattach(IGMP_IFINFO(ifp)); | |
3010 | } | |
3011 | #endif /* INET */ | |
3012 | #if INET6 | |
3013 | if (MLD_IFINFO(ifp) == NULL) { | |
3014 | MLD_IFINFO(ifp) = mld_domifattach(ifp, M_WAITOK); | |
3015 | VERIFY(MLD_IFINFO(ifp) != NULL); | |
3016 | } else { | |
3017 | VERIFY(MLD_IFINFO(ifp)->mli_ifp == ifp); | |
3018 | mld_domifreattach(MLD_IFINFO(ifp)); | |
3019 | } | |
3020 | #endif /* INET6 */ | |
b0d623f7 | 3021 | |
6d2010ae A |
3022 | /* |
3023 | * Finally, mark this ifnet as attached. | |
3024 | */ | |
3025 | lck_mtx_lock(rnh_lock); | |
3026 | ifnet_lock_exclusive(ifp); | |
3027 | lck_mtx_lock_spin(&ifp->if_ref_lock); | |
3028 | ifp->if_refflags = IFRF_ATTACHED; | |
3029 | lck_mtx_unlock(&ifp->if_ref_lock); | |
d1ecb069 | 3030 | if (net_rtref) { |
6d2010ae A |
3031 | /* boot-args override; enable idle notification */ |
3032 | (void) ifnet_set_idle_flags_locked(ifp, IFRF_IDLE_NOTIFY, | |
d1ecb069 | 3033 | IFRF_IDLE_NOTIFY); |
6d2010ae A |
3034 | } else { |
3035 | /* apply previous request(s) to set the idle flags, if any */ | |
3036 | (void) ifnet_set_idle_flags_locked(ifp, ifp->if_idle_new_flags, | |
3037 | ifp->if_idle_new_flags_mask); | |
3038 | ||
d1ecb069 | 3039 | } |
6d2010ae A |
3040 | ifnet_lock_done(ifp); |
3041 | lck_mtx_unlock(rnh_lock); | |
7ddcb079 | 3042 | dlil_if_unlock(); |
6d2010ae A |
3043 | |
3044 | #if PF | |
3045 | /* | |
3046 | * Attach packet filter to this interface, if enabled. | |
3047 | */ | |
3048 | pf_ifnet_hook(ifp, 1); | |
3049 | #endif /* PF */ | |
d1ecb069 | 3050 | |
2d21ac55 | 3051 | dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_ATTACHED, NULL, 0); |
1c79356b | 3052 | |
6d2010ae A |
3053 | if (dlil_verbose) { |
3054 | printf("%s%d: attached%s\n", ifp->if_name, ifp->if_unit, | |
3055 | (dl_if->dl_if_flags & DLIF_REUSE) ? " (recycled)" : ""); | |
3056 | } | |
3057 | ||
3058 | return (0); | |
3059 | } | |
3060 | ||
3061 | /* | |
3062 | * Prepare the storage for the first/permanent link address, which must | |
3063 | * must have the same lifetime as the ifnet itself. Although the link | |
3064 | * address gets removed from if_addrhead and ifnet_addrs[] at detach time, | |
3065 | * its location in memory must never change as it may still be referred | |
3066 | * to by some parts of the system afterwards (unfortunate implementation | |
3067 | * artifacts inherited from BSD.) | |
3068 | * | |
3069 | * Caller must hold ifnet lock as writer. | |
3070 | */ | |
3071 | static struct ifaddr * | |
3072 | dlil_alloc_lladdr(struct ifnet *ifp, const struct sockaddr_dl *ll_addr) | |
3073 | { | |
3074 | struct ifaddr *ifa, *oifa; | |
3075 | struct sockaddr_dl *asdl, *msdl; | |
3076 | char workbuf[IFNAMSIZ*2]; | |
3077 | int namelen, masklen, socksize; | |
3078 | struct dlil_ifnet *dl_if = (struct dlil_ifnet *)ifp; | |
3079 | ||
3080 | ifnet_lock_assert(ifp, IFNET_LCK_ASSERT_EXCLUSIVE); | |
3081 | VERIFY(ll_addr == NULL || ll_addr->sdl_alen == ifp->if_addrlen); | |
3082 | ||
3083 | namelen = snprintf(workbuf, sizeof (workbuf), "%s%d", | |
3084 | ifp->if_name, ifp->if_unit); | |
3085 | masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; | |
3086 | socksize = masklen + ifp->if_addrlen; | |
3087 | #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof (u_int32_t) - 1))) | |
3088 | if ((u_int32_t)socksize < sizeof (struct sockaddr_dl)) | |
3089 | socksize = sizeof(struct sockaddr_dl); | |
3090 | socksize = ROUNDUP(socksize); | |
3091 | #undef ROUNDUP | |
3092 | ||
3093 | ifa = ifp->if_lladdr; | |
3094 | if (socksize > DLIL_SDLMAXLEN || | |
3095 | (ifa != NULL && ifa != &dl_if->dl_if_lladdr.ifa)) { | |
3096 | /* | |
3097 | * Rare, but in the event that the link address requires | |
3098 | * more storage space than DLIL_SDLMAXLEN, allocate the | |
3099 | * largest possible storages for address and mask, such | |
3100 | * that we can reuse the same space when if_addrlen grows. | |
3101 | * This same space will be used when if_addrlen shrinks. | |
3102 | */ | |
3103 | if (ifa == NULL || ifa == &dl_if->dl_if_lladdr.ifa) { | |
3104 | int ifasize = sizeof (*ifa) + 2 * SOCK_MAXADDRLEN; | |
3105 | ifa = _MALLOC(ifasize, M_IFADDR, M_WAITOK | M_ZERO); | |
3106 | if (ifa == NULL) | |
3107 | return (NULL); | |
3108 | ifa_lock_init(ifa); | |
3109 | /* Don't set IFD_ALLOC, as this is permanent */ | |
3110 | ifa->ifa_debug = IFD_LINK; | |
3111 | } | |
3112 | IFA_LOCK(ifa); | |
3113 | /* address and mask sockaddr_dl locations */ | |
3114 | asdl = (struct sockaddr_dl *)(ifa + 1); | |
3115 | bzero(asdl, SOCK_MAXADDRLEN); | |
3116 | msdl = (struct sockaddr_dl *)((char *)asdl + SOCK_MAXADDRLEN); | |
3117 | bzero(msdl, SOCK_MAXADDRLEN); | |
3118 | } else { | |
3119 | VERIFY(ifa == NULL || ifa == &dl_if->dl_if_lladdr.ifa); | |
3120 | /* | |
3121 | * Use the storage areas for address and mask within the | |
3122 | * dlil_ifnet structure. This is the most common case. | |
3123 | */ | |
3124 | if (ifa == NULL) { | |
3125 | ifa = &dl_if->dl_if_lladdr.ifa; | |
3126 | ifa_lock_init(ifa); | |
3127 | /* Don't set IFD_ALLOC, as this is permanent */ | |
3128 | ifa->ifa_debug = IFD_LINK; | |
3129 | } | |
3130 | IFA_LOCK(ifa); | |
3131 | /* address and mask sockaddr_dl locations */ | |
3132 | asdl = (struct sockaddr_dl *)&dl_if->dl_if_lladdr.asdl; | |
3133 | bzero(asdl, sizeof (dl_if->dl_if_lladdr.asdl)); | |
3134 | msdl = (struct sockaddr_dl *)&dl_if->dl_if_lladdr.msdl; | |
3135 | bzero(msdl, sizeof (dl_if->dl_if_lladdr.msdl)); | |
3136 | } | |
3137 | ||
3138 | /* hold a permanent reference for the ifnet itself */ | |
3139 | IFA_ADDREF_LOCKED(ifa); | |
3140 | oifa = ifp->if_lladdr; | |
3141 | ifp->if_lladdr = ifa; | |
3142 | ||
3143 | VERIFY(ifa->ifa_debug == IFD_LINK); | |
3144 | ifa->ifa_ifp = ifp; | |
3145 | ifa->ifa_rtrequest = link_rtrequest; | |
3146 | ifa->ifa_addr = (struct sockaddr *)asdl; | |
3147 | asdl->sdl_len = socksize; | |
3148 | asdl->sdl_family = AF_LINK; | |
3149 | bcopy(workbuf, asdl->sdl_data, namelen); | |
3150 | asdl->sdl_nlen = namelen; | |
3151 | asdl->sdl_index = ifp->if_index; | |
3152 | asdl->sdl_type = ifp->if_type; | |
3153 | if (ll_addr != NULL) { | |
3154 | asdl->sdl_alen = ll_addr->sdl_alen; | |
3155 | bcopy(CONST_LLADDR(ll_addr), LLADDR(asdl), asdl->sdl_alen); | |
3156 | } else { | |
3157 | asdl->sdl_alen = 0; | |
3158 | } | |
3159 | ifa->ifa_netmask = (struct sockaddr*)msdl; | |
3160 | msdl->sdl_len = masklen; | |
3161 | while (namelen != 0) | |
3162 | msdl->sdl_data[--namelen] = 0xff; | |
3163 | IFA_UNLOCK(ifa); | |
3164 | ||
3165 | if (oifa != NULL) | |
3166 | IFA_REMREF(oifa); | |
3167 | ||
3168 | return (ifa); | |
3169 | } | |
3170 | ||
3171 | static void | |
3172 | if_purgeaddrs(struct ifnet *ifp) | |
3173 | { | |
3174 | #if INET | |
3175 | in_purgeaddrs(ifp); | |
3176 | #endif /* INET */ | |
3177 | #if INET6 | |
3178 | in6_purgeaddrs(ifp); | |
3179 | #endif /* INET6 */ | |
3180 | #if NETAT | |
3181 | at_purgeaddrs(ifp); | |
3182 | #endif | |
1c79356b A |
3183 | } |
3184 | ||
2d21ac55 | 3185 | errno_t |
6d2010ae | 3186 | ifnet_detach(ifnet_t ifp) |
1c79356b | 3187 | { |
6d2010ae A |
3188 | if (ifp == NULL) |
3189 | return (EINVAL); | |
3190 | ||
3191 | ifnet_head_lock_exclusive(); | |
3192 | lck_mtx_lock(rnh_lock); | |
91447636 | 3193 | ifnet_lock_exclusive(ifp); |
6d2010ae A |
3194 | |
3195 | /* | |
3196 | * Check to see if this interface has previously triggered | |
3197 | * aggressive protocol draining; if so, decrement the global | |
3198 | * refcnt and clear PR_AGGDRAIN on the route domain if | |
3199 | * there are no more of such an interface around. | |
3200 | */ | |
3201 | (void) ifnet_set_idle_flags_locked(ifp, 0, ~0); | |
3202 | ||
3203 | lck_mtx_lock_spin(&ifp->if_ref_lock); | |
3204 | if (!(ifp->if_refflags & IFRF_ATTACHED)) { | |
3205 | lck_mtx_unlock(&ifp->if_ref_lock); | |
3206 | ifnet_lock_done(ifp); | |
3207 | lck_mtx_unlock(rnh_lock); | |
3208 | ifnet_head_done(); | |
3209 | return (EINVAL); | |
3210 | } else if (ifp->if_refflags & IFRF_DETACHING) { | |
91447636 | 3211 | /* Interface has already been detached */ |
6d2010ae | 3212 | lck_mtx_unlock(&ifp->if_ref_lock); |
91447636 | 3213 | ifnet_lock_done(ifp); |
6d2010ae A |
3214 | lck_mtx_unlock(rnh_lock); |
3215 | ifnet_head_done(); | |
3216 | return (ENXIO); | |
55e303ae | 3217 | } |
6d2010ae A |
3218 | /* Indicate this interface is being detached */ |
3219 | ifp->if_refflags &= ~IFRF_ATTACHED; | |
3220 | ifp->if_refflags |= IFRF_DETACHING; | |
3221 | lck_mtx_unlock(&ifp->if_ref_lock); | |
3222 | ||
3223 | if (dlil_verbose) | |
3224 | printf("%s%d: detaching\n", ifp->if_name, ifp->if_unit); | |
3225 | ||
91447636 | 3226 | /* |
6d2010ae A |
3227 | * Remove ifnet from the ifnet_head, ifindex2ifnet[]; it will |
3228 | * no longer be visible during lookups from this point. | |
91447636 | 3229 | */ |
6d2010ae A |
3230 | VERIFY(ifindex2ifnet[ifp->if_index] == ifp); |
3231 | TAILQ_REMOVE(&ifnet_head, ifp, if_link); | |
3232 | ifp->if_link.tqe_next = NULL; | |
3233 | ifp->if_link.tqe_prev = NULL; | |
3234 | ifindex2ifnet[ifp->if_index] = NULL; | |
3235 | ||
3236 | /* Record detach PC stacktrace */ | |
3237 | ctrace_record(&((struct dlil_ifnet *)ifp)->dl_if_detach); | |
3238 | ||
91447636 | 3239 | ifnet_lock_done(ifp); |
6d2010ae A |
3240 | lck_mtx_unlock(rnh_lock); |
3241 | ifnet_head_done(); | |
3242 | ||
2d21ac55 A |
3243 | /* Let BPF know we're detaching */ |
3244 | bpfdetach(ifp); | |
6d2010ae A |
3245 | |
3246 | /* Mark the interface as DOWN */ | |
3247 | if_down(ifp); | |
3248 | ||
3249 | /* Disable forwarding cached route */ | |
3250 | lck_mtx_lock(&ifp->if_cached_route_lock); | |
3251 | ifp->if_fwd_cacheok = 0; | |
3252 | lck_mtx_unlock(&ifp->if_cached_route_lock); | |
3253 | ||
d1ecb069 | 3254 | /* |
6d2010ae A |
3255 | * Drain any deferred IGMPv3/MLDv2 query responses, but keep the |
3256 | * references to the info structures and leave them attached to | |
3257 | * this ifnet. | |
d1ecb069 | 3258 | */ |
6d2010ae A |
3259 | #if INET |
3260 | igmp_domifdetach(ifp); | |
3261 | #endif /* INET */ | |
3262 | #if INET6 | |
3263 | mld_domifdetach(ifp); | |
3264 | #endif /* INET6 */ | |
3265 | ||
3266 | dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_DETACHING, NULL, 0); | |
3267 | ||
3268 | /* Let worker thread take care of the rest, to avoid reentrancy */ | |
7ddcb079 | 3269 | dlil_if_lock(); |
6d2010ae | 3270 | ifnet_detaching_enqueue(ifp); |
7ddcb079 | 3271 | dlil_if_unlock(); |
6d2010ae A |
3272 | |
3273 | return (0); | |
3274 | } | |
3275 | ||
3276 | static void | |
3277 | ifnet_detaching_enqueue(struct ifnet *ifp) | |
3278 | { | |
7ddcb079 | 3279 | dlil_if_lock_assert(); |
6d2010ae A |
3280 | |
3281 | ++ifnet_detaching_cnt; | |
3282 | VERIFY(ifnet_detaching_cnt != 0); | |
3283 | TAILQ_INSERT_TAIL(&ifnet_detaching_head, ifp, if_detaching_link); | |
3284 | wakeup((caddr_t)&ifnet_delayed_run); | |
3285 | } | |
3286 | ||
3287 | static struct ifnet * | |
3288 | ifnet_detaching_dequeue(void) | |
3289 | { | |
3290 | struct ifnet *ifp; | |
3291 | ||
7ddcb079 | 3292 | dlil_if_lock_assert(); |
6d2010ae A |
3293 | |
3294 | ifp = TAILQ_FIRST(&ifnet_detaching_head); | |
3295 | VERIFY(ifnet_detaching_cnt != 0 || ifp == NULL); | |
3296 | if (ifp != NULL) { | |
3297 | VERIFY(ifnet_detaching_cnt != 0); | |
3298 | --ifnet_detaching_cnt; | |
3299 | TAILQ_REMOVE(&ifnet_detaching_head, ifp, if_detaching_link); | |
3300 | ifp->if_detaching_link.tqe_next = NULL; | |
3301 | ifp->if_detaching_link.tqe_prev = NULL; | |
3302 | } | |
3303 | return (ifp); | |
3304 | } | |
3305 | ||
3306 | static void | |
3307 | ifnet_delayed_thread_func(void) | |
3308 | { | |
3309 | struct ifnet *ifp; | |
3310 | ||
3311 | for (;;) { | |
7ddcb079 | 3312 | dlil_if_lock(); |
6d2010ae A |
3313 | while (ifnet_detaching_cnt == 0) { |
3314 | (void) msleep(&ifnet_delayed_run, &dlil_ifnet_lock, | |
3315 | (PZERO - 1), "ifnet_delayed_thread", NULL); | |
3316 | } | |
3317 | ||
3318 | VERIFY(TAILQ_FIRST(&ifnet_detaching_head) != NULL); | |
3319 | ||
3320 | /* Take care of detaching ifnet */ | |
3321 | ifp = ifnet_detaching_dequeue(); | |
7ddcb079 A |
3322 | dlil_if_unlock(); |
3323 | if (ifp != NULL) | |
6d2010ae | 3324 | ifnet_detach_final(ifp); |
55e303ae | 3325 | } |
6d2010ae | 3326 | } |
b0d623f7 | 3327 | |
6d2010ae A |
3328 | static void |
3329 | ifnet_detach_final(struct ifnet *ifp) | |
3330 | { | |
3331 | struct ifnet_filter *filter, *filter_next; | |
3332 | struct ifnet_filter_head fhead; | |
3333 | struct dlil_threading_info *inputthread; | |
3334 | struct ifaddr *ifa; | |
3335 | ifnet_detached_func if_free; | |
3336 | int i; | |
3337 | ||
3338 | lck_mtx_lock(&ifp->if_ref_lock); | |
3339 | if (!(ifp->if_refflags & IFRF_DETACHING)) { | |
3340 | panic("%s: flags mismatch (detaching not set) ifp=%p", | |
3341 | __func__, ifp); | |
3342 | /* NOTREACHED */ | |
3343 | } | |
3344 | ||
3345 | /* Wait until the existing IO references get released | |
3346 | * before we proceed with ifnet_detach | |
b0d623f7 | 3347 | */ |
6d2010ae A |
3348 | while (ifp->if_refio > 0) { |
3349 | printf("%s: Waiting for IO references on %s%d interface " | |
3350 | "to be released\n", __func__, ifp->if_name, ifp->if_unit); | |
3351 | (void) msleep(&(ifp->if_refio), &ifp->if_ref_lock, | |
3352 | (PZERO - 1), "ifnet_ioref_wait", NULL); | |
3353 | } | |
3354 | lck_mtx_unlock(&ifp->if_ref_lock); | |
3355 | ||
3356 | /* Detach interface filters */ | |
3357 | lck_mtx_lock(&ifp->if_flt_lock); | |
3358 | if_flt_monitor_enter(ifp); | |
b0d623f7 | 3359 | |
6d2010ae | 3360 | lck_mtx_assert(&ifp->if_flt_lock, LCK_MTX_ASSERT_OWNED); |
91447636 A |
3361 | fhead = ifp->if_flt_head; |
3362 | TAILQ_INIT(&ifp->if_flt_head); | |
2d21ac55 | 3363 | |
6d2010ae A |
3364 | for (filter = TAILQ_FIRST(&fhead); filter; filter = filter_next) { |
3365 | filter_next = TAILQ_NEXT(filter, filt_next); | |
3366 | lck_mtx_unlock(&ifp->if_flt_lock); | |
3367 | ||
3368 | dlil_detach_filter_internal(filter, 1); | |
3369 | lck_mtx_lock(&ifp->if_flt_lock); | |
3370 | } | |
3371 | if_flt_monitor_leave(ifp); | |
3372 | lck_mtx_unlock(&ifp->if_flt_lock); | |
3373 | ||
3374 | /* Tell upper layers to drop their network addresses */ | |
3375 | if_purgeaddrs(ifp); | |
3376 | ||
3377 | ifnet_lock_exclusive(ifp); | |
3378 | ||
3379 | /* Uplumb all protocols */ | |
3380 | for (i = 0; i < PROTO_HASH_SLOTS; i++) { | |
3381 | struct if_proto *proto; | |
3382 | ||
3383 | proto = SLIST_FIRST(&ifp->if_proto_hash[i]); | |
3384 | while (proto != NULL) { | |
3385 | protocol_family_t family = proto->protocol_family; | |
3386 | ifnet_lock_done(ifp); | |
3387 | proto_unplumb(family, ifp); | |
3388 | ifnet_lock_exclusive(ifp); | |
3389 | proto = SLIST_FIRST(&ifp->if_proto_hash[i]); | |
3390 | } | |
3391 | /* There should not be any protocols left */ | |
3392 | VERIFY(SLIST_EMPTY(&ifp->if_proto_hash[i])); | |
3393 | } | |
3394 | zfree(dlif_phash_zone, ifp->if_proto_hash); | |
3395 | ifp->if_proto_hash = NULL; | |
3396 | ||
3397 | /* Detach (permanent) link address from if_addrhead */ | |
3398 | ifa = TAILQ_FIRST(&ifp->if_addrhead); | |
3399 | VERIFY(ifnet_addrs[ifp->if_index - 1] == ifa); | |
3400 | IFA_LOCK(ifa); | |
3401 | if_detach_link_ifa(ifp, ifa); | |
3402 | IFA_UNLOCK(ifa); | |
3403 | ||
3404 | /* Remove (permanent) link address from ifnet_addrs[] */ | |
3405 | IFA_REMREF(ifa); | |
3406 | ifnet_addrs[ifp->if_index - 1] = NULL; | |
3407 | ||
3408 | /* This interface should not be on {ifnet_head,detaching} */ | |
3409 | VERIFY(ifp->if_link.tqe_next == NULL); | |
3410 | VERIFY(ifp->if_link.tqe_prev == NULL); | |
3411 | VERIFY(ifp->if_detaching_link.tqe_next == NULL); | |
3412 | VERIFY(ifp->if_detaching_link.tqe_prev == NULL); | |
3413 | ||
3414 | /* Prefix list should be empty by now */ | |
3415 | VERIFY(TAILQ_EMPTY(&ifp->if_prefixhead)); | |
3416 | ||
3417 | /* The slot should have been emptied */ | |
3418 | VERIFY(ifindex2ifnet[ifp->if_index] == NULL); | |
3419 | ||
3420 | /* There should not be any addresses left */ | |
3421 | VERIFY(TAILQ_EMPTY(&ifp->if_addrhead)); | |
1c79356b | 3422 | |
2d21ac55 A |
3423 | /* |
3424 | * If thread affinity was set for the workloop thread, we will need | |
3425 | * to tear down the affinity and release the extra reference count | |
3426 | * taken at attach time; | |
3427 | */ | |
3428 | if ((inputthread = ifp->if_input_thread) != NULL) { | |
3429 | if (inputthread->net_affinity) { | |
3430 | struct thread *tp; | |
3431 | ||
6d2010ae A |
3432 | if (inputthread == dlil_lo_thread_ptr) { |
3433 | panic("%s: Thread affinity should not be " | |
3434 | "enabled on the loopback dlil input " | |
3435 | "thread", __func__); | |
3436 | /* NOTREACHED */ | |
3437 | } | |
2d21ac55 | 3438 | |
6d2010ae | 3439 | lck_mtx_lock_spin(&inputthread->input_lck); |
2d21ac55 A |
3440 | tp = inputthread->workloop_thread; |
3441 | inputthread->workloop_thread = NULL; | |
3442 | inputthread->tag = 0; | |
3443 | inputthread->net_affinity = FALSE; | |
6d2010ae | 3444 | lck_mtx_unlock(&inputthread->input_lck); |
2d21ac55 A |
3445 | |
3446 | /* Tear down workloop thread affinity */ | |
3447 | if (tp != NULL) { | |
3448 | (void) dlil_affinity_set(tp, | |
3449 | THREAD_AFFINITY_TAG_NULL); | |
3450 | thread_deallocate(tp); | |
3451 | } | |
1c79356b | 3452 | |
2d21ac55 A |
3453 | /* Tear down dlil input thread affinity */ |
3454 | tp = inputthread->input_thread; | |
3455 | (void) dlil_affinity_set(tp, THREAD_AFFINITY_TAG_NULL); | |
3456 | thread_deallocate(tp); | |
9bccf70c | 3457 | } |
1c79356b | 3458 | |
2d21ac55 A |
3459 | /* cleanup ifp dlil input thread, if any */ |
3460 | ifp->if_input_thread = NULL; | |
55e303ae | 3461 | |
2d21ac55 A |
3462 | if (inputthread != dlil_lo_thread_ptr) { |
3463 | #ifdef DLIL_DEBUG | |
6d2010ae | 3464 | printf("%s: wakeup thread threadinfo: %p " |
2d21ac55 | 3465 | "input_thread=%p threads: cur=%d max=%d\n", |
6d2010ae | 3466 | __func__, inputthread, inputthread->input_thread, |
2d21ac55 A |
3467 | dlil_multithreaded_input, cur_dlil_input_threads); |
3468 | #endif | |
6d2010ae | 3469 | lck_mtx_lock_spin(&inputthread->input_lck); |
55e303ae | 3470 | |
2d21ac55 | 3471 | inputthread->input_waiting |= DLIL_INPUT_TERMINATE; |
6d2010ae | 3472 | if (!(inputthread->input_waiting & DLIL_INPUT_RUNNING)) |
2d21ac55 | 3473 | wakeup((caddr_t)&inputthread->input_waiting); |
6d2010ae A |
3474 | |
3475 | lck_mtx_unlock(&inputthread->input_lck); | |
91447636 | 3476 | } |
55e303ae | 3477 | } |
6d2010ae A |
3478 | |
3479 | /* The driver might unload, so point these to ourselves */ | |
3480 | if_free = ifp->if_free; | |
3481 | ifp->if_output = ifp_if_output; | |
3482 | ifp->if_ioctl = ifp_if_ioctl; | |
3483 | ifp->if_set_bpf_tap = ifp_if_set_bpf_tap; | |
3484 | ifp->if_free = ifp_if_free; | |
3485 | ifp->if_demux = ifp_if_demux; | |
3486 | ifp->if_event = ifp_if_event; | |
3487 | ifp->if_framer = ifp_if_framer; | |
3488 | ifp->if_add_proto = ifp_if_add_proto; | |
3489 | ifp->if_del_proto = ifp_if_del_proto; | |
3490 | ifp->if_check_multi = ifp_if_check_multi; | |
3491 | ||
3492 | ifnet_lock_done(ifp); | |
3493 | ||
3494 | #if PF | |
3495 | /* | |
3496 | * Detach this interface from packet filter, if enabled. | |
3497 | */ | |
3498 | pf_ifnet_hook(ifp, 0); | |
3499 | #endif /* PF */ | |
3500 | ||
3501 | /* Filter list should be empty */ | |
3502 | lck_mtx_lock_spin(&ifp->if_flt_lock); | |
3503 | VERIFY(TAILQ_EMPTY(&ifp->if_flt_head)); | |
3504 | VERIFY(ifp->if_flt_busy == 0); | |
3505 | VERIFY(ifp->if_flt_waiters == 0); | |
3506 | lck_mtx_unlock(&ifp->if_flt_lock); | |
3507 | ||
3508 | /* Last chance to cleanup any cached route */ | |
3509 | lck_mtx_lock(&ifp->if_cached_route_lock); | |
3510 | VERIFY(!ifp->if_fwd_cacheok); | |
3511 | if (ifp->if_fwd_route.ro_rt != NULL) | |
b0d623f7 | 3512 | rtfree(ifp->if_fwd_route.ro_rt); |
6d2010ae A |
3513 | bzero(&ifp->if_fwd_route, sizeof (ifp->if_fwd_route)); |
3514 | if (ifp->if_src_route.ro_rt != NULL) | |
3515 | rtfree(ifp->if_src_route.ro_rt); | |
3516 | bzero(&ifp->if_src_route, sizeof (ifp->if_src_route)); | |
3517 | if (ifp->if_src_route6.ro_rt != NULL) | |
3518 | rtfree(ifp->if_src_route6.ro_rt); | |
3519 | bzero(&ifp->if_src_route6, sizeof (ifp->if_src_route6)); | |
3520 | lck_mtx_unlock(&ifp->if_cached_route_lock); | |
3521 | ||
3522 | ifnet_llreach_ifdetach(ifp); | |
3523 | ||
3524 | dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_DETACHED, NULL, 0); | |
3525 | ||
3526 | if (if_free != NULL) | |
3527 | if_free(ifp); | |
3528 | ||
3529 | /* | |
3530 | * Finally, mark this ifnet as detached. | |
3531 | */ | |
3532 | lck_mtx_lock_spin(&ifp->if_ref_lock); | |
3533 | if (!(ifp->if_refflags & IFRF_DETACHING)) { | |
3534 | panic("%s: flags mismatch (detaching not set) ifp=%p", | |
3535 | __func__, ifp); | |
3536 | /* NOTREACHED */ | |
55e303ae | 3537 | } |
6d2010ae A |
3538 | ifp->if_refflags &= ~IFRF_DETACHING; |
3539 | lck_mtx_unlock(&ifp->if_ref_lock); | |
3540 | ||
3541 | if (dlil_verbose) | |
3542 | printf("%s%d: detached\n", ifp->if_name, ifp->if_unit); | |
3543 | ||
3544 | /* Release reference held during ifnet attach */ | |
3545 | ifnet_release(ifp); | |
1c79356b | 3546 | } |
9bccf70c | 3547 | |
91447636 | 3548 | static errno_t |
6d2010ae | 3549 | ifp_if_output(struct ifnet *ifp, struct mbuf *m) |
9bccf70c | 3550 | { |
6d2010ae A |
3551 | #pragma unused(ifp) |
3552 | m_freem(m); | |
3553 | return (0); | |
9bccf70c A |
3554 | } |
3555 | ||
6d2010ae A |
3556 | static errno_t |
3557 | ifp_if_demux(struct ifnet *ifp, struct mbuf *m, char *fh, protocol_family_t *pf) | |
9bccf70c | 3558 | { |
6d2010ae A |
3559 | #pragma unused(ifp, fh, pf) |
3560 | m_freem(m); | |
3561 | return (EJUSTRETURN); | |
9bccf70c A |
3562 | } |
3563 | ||
6d2010ae A |
3564 | static errno_t |
3565 | ifp_if_add_proto(struct ifnet *ifp, protocol_family_t pf, | |
3566 | const struct ifnet_demux_desc *da, u_int32_t dc) | |
9bccf70c | 3567 | { |
6d2010ae A |
3568 | #pragma unused(ifp, pf, da, dc) |
3569 | return (EINVAL); | |
9bccf70c A |
3570 | } |
3571 | ||
91447636 | 3572 | static errno_t |
6d2010ae | 3573 | ifp_if_del_proto(struct ifnet *ifp, protocol_family_t pf) |
9bccf70c | 3574 | { |
6d2010ae A |
3575 | #pragma unused(ifp, pf) |
3576 | return (EINVAL); | |
3577 | } | |
3578 | ||
3579 | static errno_t | |
3580 | ifp_if_check_multi(struct ifnet *ifp, const struct sockaddr *sa) | |
3581 | { | |
3582 | #pragma unused(ifp, sa) | |
3583 | return (EOPNOTSUPP); | |
3584 | } | |
3585 | ||
3586 | static errno_t | |
3587 | ifp_if_framer(struct ifnet *ifp, struct mbuf **m, | |
3588 | const struct sockaddr *sa, const char *ll, const char *t) | |
3589 | { | |
3590 | #pragma unused(ifp, m, sa, ll, t) | |
3591 | m_freem(*m); | |
3592 | *m = NULL; | |
3593 | return (EJUSTRETURN); | |
3594 | } | |
3595 | ||
3596 | static errno_t | |
3597 | ifp_if_ioctl(struct ifnet *ifp, unsigned long cmd, void *arg) | |
3598 | { | |
3599 | #pragma unused(ifp, cmd, arg) | |
3600 | return (EOPNOTSUPP); | |
3601 | } | |
3602 | ||
3603 | static errno_t | |
3604 | ifp_if_set_bpf_tap(struct ifnet *ifp, bpf_tap_mode tm, bpf_packet_func f) | |
3605 | { | |
3606 | #pragma unused(ifp, tm, f) | |
3607 | /* XXX not sure what to do here */ | |
3608 | return (0); | |
3609 | } | |
3610 | ||
3611 | static void | |
3612 | ifp_if_free(struct ifnet *ifp) | |
3613 | { | |
3614 | #pragma unused(ifp) | |
3615 | } | |
3616 | ||
3617 | static void | |
3618 | ifp_if_event(struct ifnet *ifp, const struct kev_msg *e) | |
3619 | { | |
3620 | #pragma unused(ifp, e) | |
9bccf70c A |
3621 | } |
3622 | ||
2d21ac55 | 3623 | __private_extern__ |
6d2010ae A |
3624 | int dlil_if_acquire(u_int32_t family, const void *uniqueid, |
3625 | size_t uniqueid_len, struct ifnet **ifp) | |
3626 | { | |
3627 | struct ifnet *ifp1 = NULL; | |
3628 | struct dlil_ifnet *dlifp1 = NULL; | |
3629 | void *buf, *base, **pbuf; | |
3630 | int ret = 0; | |
3631 | ||
7ddcb079 | 3632 | dlil_if_lock(); |
6d2010ae A |
3633 | TAILQ_FOREACH(dlifp1, &dlil_ifnet_head, dl_if_link) { |
3634 | ifp1 = (struct ifnet *)dlifp1; | |
3635 | ||
3636 | if (ifp1->if_family != family) | |
3637 | continue; | |
3638 | ||
3639 | lck_mtx_lock(&dlifp1->dl_if_lock); | |
3640 | /* same uniqueid and same len or no unique id specified */ | |
3641 | if ((uniqueid_len == dlifp1->dl_if_uniqueid_len) && | |
3642 | !bcmp(uniqueid, dlifp1->dl_if_uniqueid, uniqueid_len)) { | |
3643 | /* check for matching interface in use */ | |
3644 | if (dlifp1->dl_if_flags & DLIF_INUSE) { | |
3645 | if (uniqueid_len) { | |
3646 | ret = EBUSY; | |
3647 | lck_mtx_unlock(&dlifp1->dl_if_lock); | |
9bccf70c | 3648 | goto end; |
6d2010ae A |
3649 | } |
3650 | } else { | |
3651 | dlifp1->dl_if_flags |= (DLIF_INUSE|DLIF_REUSE); | |
3652 | lck_mtx_unlock(&dlifp1->dl_if_lock); | |
3653 | *ifp = ifp1; | |
3654 | goto end; | |
3655 | } | |
3656 | } | |
3657 | lck_mtx_unlock(&dlifp1->dl_if_lock); | |
3658 | } | |
3659 | ||
3660 | /* no interface found, allocate a new one */ | |
3661 | buf = zalloc(dlif_zone); | |
3662 | if (buf == NULL) { | |
3663 | ret = ENOMEM; | |
3664 | goto end; | |
3665 | } | |
3666 | bzero(buf, dlif_bufsize); | |
3667 | ||
3668 | /* Get the 64-bit aligned base address for this object */ | |
3669 | base = (void *)P2ROUNDUP((intptr_t)buf + sizeof (u_int64_t), | |
3670 | sizeof (u_int64_t)); | |
3671 | VERIFY(((intptr_t)base + dlif_size) <= ((intptr_t)buf + dlif_bufsize)); | |
3672 | ||
3673 | /* | |
3674 | * Wind back a pointer size from the aligned base and | |
3675 | * save the original address so we can free it later. | |
3676 | */ | |
3677 | pbuf = (void **)((intptr_t)base - sizeof (void *)); | |
3678 | *pbuf = buf; | |
3679 | dlifp1 = base; | |
3680 | ||
3681 | if (uniqueid_len) { | |
3682 | MALLOC(dlifp1->dl_if_uniqueid, void *, uniqueid_len, | |
3683 | M_NKE, M_WAITOK); | |
3684 | if (dlifp1->dl_if_uniqueid == NULL) { | |
3685 | zfree(dlif_zone, dlifp1); | |
3686 | ret = ENOMEM; | |
3687 | goto end; | |
3688 | } | |
3689 | bcopy(uniqueid, dlifp1->dl_if_uniqueid, uniqueid_len); | |
3690 | dlifp1->dl_if_uniqueid_len = uniqueid_len; | |
3691 | } | |
3692 | ||
3693 | ifp1 = (struct ifnet *)dlifp1; | |
3694 | dlifp1->dl_if_flags = DLIF_INUSE; | |
3695 | if (ifnet_debug) { | |
3696 | dlifp1->dl_if_flags |= DLIF_DEBUG; | |
3697 | dlifp1->dl_if_trace = dlil_if_trace; | |
3698 | } | |
3699 | ifp1->if_name = dlifp1->dl_if_namestorage; | |
2d21ac55 | 3700 | #if CONFIG_MACF_NET |
6d2010ae | 3701 | mac_ifnet_label_init(ifp1); |
2d21ac55 | 3702 | #endif |
9bccf70c | 3703 | |
6d2010ae A |
3704 | lck_mtx_init(&dlifp1->dl_if_lock, ifnet_lock_group, ifnet_lock_attr); |
3705 | lck_rw_init(&ifp1->if_lock, ifnet_lock_group, ifnet_lock_attr); | |
3706 | lck_mtx_init(&ifp1->if_ref_lock, ifnet_lock_group, ifnet_lock_attr); | |
3707 | lck_mtx_init(&ifp1->if_flt_lock, ifnet_lock_group, ifnet_lock_attr); | |
3708 | lck_mtx_init(&ifp1->if_cached_route_lock, ifnet_lock_group, | |
3709 | ifnet_lock_attr); | |
3710 | lck_mtx_init(&ifp1->if_addrconfig_lock, ifnet_lock_group, | |
3711 | ifnet_lock_attr); | |
3712 | lck_rw_init(&ifp1->if_llreach_lock, ifnet_lock_group, ifnet_lock_attr); | |
3713 | ||
3714 | TAILQ_INSERT_TAIL(&dlil_ifnet_head, dlifp1, dl_if_link); | |
3715 | ||
3716 | *ifp = ifp1; | |
9bccf70c A |
3717 | |
3718 | end: | |
7ddcb079 | 3719 | dlil_if_unlock(); |
9bccf70c | 3720 | |
6d2010ae A |
3721 | VERIFY(dlifp1 == NULL || (IS_P2ALIGNED(dlifp1, sizeof (u_int64_t)) && |
3722 | IS_P2ALIGNED(&ifp1->if_data, sizeof (u_int64_t)))); | |
3723 | ||
3724 | return (ret); | |
9bccf70c A |
3725 | } |
3726 | ||
2d21ac55 | 3727 | __private_extern__ void |
6d2010ae A |
3728 | dlil_if_release(ifnet_t ifp) |
3729 | { | |
3730 | struct dlil_ifnet *dlifp = (struct dlil_ifnet *)ifp; | |
3731 | ||
3732 | ifnet_lock_exclusive(ifp); | |
3733 | lck_mtx_lock(&dlifp->dl_if_lock); | |
3734 | dlifp->dl_if_flags &= ~DLIF_INUSE; | |
3735 | strncpy(dlifp->dl_if_namestorage, ifp->if_name, IFNAMSIZ); | |
3736 | ifp->if_name = dlifp->dl_if_namestorage; | |
3737 | lck_mtx_unlock(&dlifp->dl_if_lock); | |
2d21ac55 | 3738 | #if CONFIG_MACF_NET |
6d2010ae A |
3739 | /* |
3740 | * We can either recycle the MAC label here or in dlil_if_acquire(). | |
3741 | * It seems logical to do it here but this means that anything that | |
3742 | * still has a handle on ifp will now see it as unlabeled. | |
3743 | * Since the interface is "dead" that may be OK. Revisit later. | |
3744 | */ | |
3745 | mac_ifnet_label_recycle(ifp); | |
2d21ac55 | 3746 | #endif |
6d2010ae | 3747 | ifnet_lock_done(ifp); |
9bccf70c | 3748 | } |
4a3eedf9 | 3749 | |
7ddcb079 A |
3750 | __private_extern__ void |
3751 | dlil_if_lock(void) | |
3752 | { | |
3753 | lck_mtx_lock(&dlil_ifnet_lock); | |
3754 | } | |
3755 | ||
3756 | __private_extern__ void | |
3757 | dlil_if_unlock(void) | |
3758 | { | |
3759 | lck_mtx_unlock(&dlil_ifnet_lock); | |
3760 | } | |
3761 | ||
3762 | __private_extern__ void | |
3763 | dlil_if_lock_assert(void) | |
3764 | { | |
3765 | lck_mtx_assert(&dlil_ifnet_lock, LCK_MTX_ASSERT_OWNED); | |
3766 | } | |
3767 | ||
4a3eedf9 A |
3768 | __private_extern__ void |
3769 | dlil_proto_unplumb_all(struct ifnet *ifp) | |
3770 | { | |
3771 | /* | |
3772 | * if_proto_hash[0-3] are for PF_INET, PF_INET6, PF_APPLETALK | |
3773 | * and PF_VLAN, where each bucket contains exactly one entry; | |
3774 | * PF_VLAN does not need an explicit unplumb. | |
3775 | * | |
3776 | * if_proto_hash[4] is for other protocols; we expect anything | |
3777 | * in this bucket to respond to the DETACHING event (which would | |
3778 | * have happened by now) and do the unplumb then. | |
3779 | */ | |
3780 | (void) proto_unplumb(PF_INET, ifp); | |
3781 | #if INET6 | |
3782 | (void) proto_unplumb(PF_INET6, ifp); | |
3783 | #endif /* INET6 */ | |
3784 | #if NETAT | |
3785 | (void) proto_unplumb(PF_APPLETALK, ifp); | |
3786 | #endif /* NETAT */ | |
3787 | } | |
6d2010ae A |
3788 | |
3789 | static void | |
3790 | ifp_src_route_copyout(struct ifnet *ifp, struct route *dst) | |
3791 | { | |
3792 | lck_mtx_lock_spin(&ifp->if_cached_route_lock); | |
3793 | lck_mtx_convert_spin(&ifp->if_cached_route_lock); | |
3794 | ||
3795 | route_copyout(dst, &ifp->if_src_route, sizeof (*dst)); | |
3796 | ||
3797 | lck_mtx_unlock(&ifp->if_cached_route_lock); | |
3798 | } | |
3799 | ||
3800 | static void | |
3801 | ifp_src_route_copyin(struct ifnet *ifp, struct route *src) | |
3802 | { | |
3803 | lck_mtx_lock_spin(&ifp->if_cached_route_lock); | |
3804 | lck_mtx_convert_spin(&ifp->if_cached_route_lock); | |
3805 | ||
3806 | if (ifp->if_fwd_cacheok) { | |
3807 | route_copyin(src, &ifp->if_src_route, sizeof (*src)); | |
3808 | } else { | |
3809 | rtfree(src->ro_rt); | |
3810 | src->ro_rt = NULL; | |
3811 | } | |
3812 | lck_mtx_unlock(&ifp->if_cached_route_lock); | |
3813 | } | |
3814 | ||
3815 | #if INET6 | |
3816 | static void | |
3817 | ifp_src_route6_copyout(struct ifnet *ifp, struct route_in6 *dst) | |
3818 | { | |
3819 | lck_mtx_lock_spin(&ifp->if_cached_route_lock); | |
3820 | lck_mtx_convert_spin(&ifp->if_cached_route_lock); | |
3821 | ||
3822 | route_copyout((struct route *)dst, (struct route *)&ifp->if_src_route6, | |
3823 | sizeof (*dst)); | |
3824 | ||
3825 | lck_mtx_unlock(&ifp->if_cached_route_lock); | |
3826 | } | |
3827 | ||
3828 | static void | |
3829 | ifp_src_route6_copyin(struct ifnet *ifp, struct route_in6 *src) | |
3830 | { | |
3831 | lck_mtx_lock_spin(&ifp->if_cached_route_lock); | |
3832 | lck_mtx_convert_spin(&ifp->if_cached_route_lock); | |
3833 | ||
3834 | if (ifp->if_fwd_cacheok) { | |
3835 | route_copyin((struct route *)src, | |
3836 | (struct route *)&ifp->if_src_route6, sizeof (*src)); | |
3837 | } else { | |
3838 | rtfree(src->ro_rt); | |
3839 | src->ro_rt = NULL; | |
3840 | } | |
3841 | lck_mtx_unlock(&ifp->if_cached_route_lock); | |
3842 | } | |
3843 | #endif /* INET6 */ | |
3844 | ||
3845 | struct rtentry * | |
3846 | ifnet_cached_rtlookup_inet(struct ifnet *ifp, struct in_addr src_ip) | |
3847 | { | |
3848 | struct route src_rt; | |
3849 | struct sockaddr_in *dst = (struct sockaddr_in *)(&src_rt.ro_dst); | |
3850 | ||
3851 | ifp_src_route_copyout(ifp, &src_rt); | |
3852 | ||
3853 | if (src_rt.ro_rt == NULL || !(src_rt.ro_rt->rt_flags & RTF_UP) || | |
3854 | src_ip.s_addr != dst->sin_addr.s_addr || | |
3855 | src_rt.ro_rt->generation_id != route_generation) { | |
3856 | if (src_rt.ro_rt != NULL) { | |
3857 | rtfree(src_rt.ro_rt); | |
3858 | src_rt.ro_rt = NULL; | |
3859 | } else if (dst->sin_family != AF_INET) { | |
3860 | bzero(&src_rt.ro_dst, sizeof (src_rt.ro_dst)); | |
3861 | dst->sin_len = sizeof (src_rt.ro_dst); | |
3862 | dst->sin_family = AF_INET; | |
3863 | } | |
3864 | dst->sin_addr = src_ip; | |
3865 | ||
3866 | if (src_rt.ro_rt == NULL) { | |
3867 | src_rt.ro_rt = rtalloc1_scoped((struct sockaddr *)dst, | |
3868 | 0, 0, ifp->if_index); | |
3869 | ||
3870 | if (src_rt.ro_rt != NULL) { | |
3871 | /* retain a ref, copyin consumes one */ | |
3872 | struct rtentry *rte = src_rt.ro_rt; | |
3873 | RT_ADDREF(rte); | |
3874 | ifp_src_route_copyin(ifp, &src_rt); | |
3875 | src_rt.ro_rt = rte; | |
3876 | } | |
3877 | } | |
3878 | } | |
3879 | ||
3880 | return (src_rt.ro_rt); | |
3881 | } | |
3882 | ||
3883 | #if INET6 | |
3884 | struct rtentry* | |
3885 | ifnet_cached_rtlookup_inet6(struct ifnet *ifp, struct in6_addr *src_ip6) | |
3886 | { | |
3887 | struct route_in6 src_rt; | |
3888 | ||
3889 | ifp_src_route6_copyout(ifp, &src_rt); | |
3890 | ||
3891 | if (src_rt.ro_rt == NULL || !(src_rt.ro_rt->rt_flags & RTF_UP) || | |
3892 | !IN6_ARE_ADDR_EQUAL(src_ip6, &src_rt.ro_dst.sin6_addr) || | |
3893 | src_rt.ro_rt->generation_id != route_generation) { | |
3894 | if (src_rt.ro_rt != NULL) { | |
3895 | rtfree(src_rt.ro_rt); | |
3896 | src_rt.ro_rt = NULL; | |
3897 | } else if (src_rt.ro_dst.sin6_family != AF_INET6) { | |
3898 | bzero(&src_rt.ro_dst, sizeof (src_rt.ro_dst)); | |
3899 | src_rt.ro_dst.sin6_len = sizeof (src_rt.ro_dst); | |
3900 | src_rt.ro_dst.sin6_family = AF_INET6; | |
3901 | } | |
3902 | src_rt.ro_dst.sin6_scope_id = in6_addr2scopeid(ifp, src_ip6); | |
3903 | src_rt.ro_dst.sin6_addr = *src_ip6; | |
3904 | ||
3905 | if (src_rt.ro_rt == NULL) { | |
3906 | src_rt.ro_rt = rtalloc1_scoped( | |
3907 | (struct sockaddr *)&src_rt.ro_dst, 0, 0, | |
3908 | ifp->if_index); | |
3909 | ||
3910 | if (src_rt.ro_rt != NULL) { | |
3911 | /* retain a ref, copyin consumes one */ | |
3912 | struct rtentry *rte = src_rt.ro_rt; | |
3913 | RT_ADDREF(rte); | |
3914 | ifp_src_route6_copyin(ifp, &src_rt); | |
3915 | src_rt.ro_rt = rte; | |
3916 | } | |
3917 | } | |
3918 | } | |
3919 | ||
3920 | return (src_rt.ro_rt); | |
3921 | } | |
3922 | #endif /* INET6 */ |