]>
Commit | Line | Data |
---|---|---|
91447636 | 1 | /* |
39236c6e | 2 | * Copyright (c) 2004-2013 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
39236c6e | 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. | |
39236c6e | 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. | |
39236c6e | 17 | * |
2d21ac55 A |
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. | |
39236c6e | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
91447636 A |
27 | */ |
28 | ||
29 | #include "kpi_protocol.h" | |
30 | ||
31 | #include <sys/param.h> | |
32 | #include <sys/malloc.h> | |
33 | #include <sys/socket.h> | |
34 | #include <sys/systm.h> | |
35 | #include <sys/kpi_mbuf.h> | |
36 | #include <sys/domain.h> | |
37 | #include <net/if.h> | |
38 | #include <net/dlil.h> | |
39 | #include <libkern/OSAtomic.h> | |
40 | ||
91447636 A |
41 | void proto_input_run(void); |
42 | ||
b0d623f7 A |
43 | typedef int (*attach_t)(struct ifnet *ifp, uint32_t protocol_family); |
44 | typedef int (*detach_t)(struct ifnet *ifp, uint32_t protocol_family); | |
91447636 | 45 | |
91447636 | 46 | struct proto_input_entry { |
316670eb A |
47 | struct proto_input_entry *next; |
48 | int detach; | |
49 | struct domain *domain; | |
50 | int hash; | |
51 | int chain; | |
52 | ||
53 | protocol_family_t protocol; | |
54 | proto_input_handler input; | |
91447636 | 55 | proto_input_detached_handler detached; |
316670eb A |
56 | |
57 | mbuf_t inject_first; | |
58 | mbuf_t inject_last; | |
59 | ||
60 | struct proto_input_entry *input_next; | |
61 | mbuf_t input_first; | |
62 | mbuf_t input_last; | |
2d21ac55 A |
63 | }; |
64 | ||
65 | ||
66 | struct proto_family_str { | |
67 | TAILQ_ENTRY(proto_family_str) proto_fam_next; | |
316670eb A |
68 | protocol_family_t proto_family; |
69 | ifnet_family_t if_family; | |
70 | proto_plumb_handler attach_proto; | |
71 | proto_unplumb_handler detach_proto; | |
91447636 A |
72 | }; |
73 | ||
316670eb A |
74 | static struct proto_input_entry *proto_hash[PROTO_HASH_SLOTS]; |
75 | static int proto_total_waiting = 0; | |
76 | static struct proto_input_entry *proto_input_add_list = NULL; | |
77 | decl_lck_mtx_data(static, proto_family_mutex_data); | |
78 | static lck_mtx_t *proto_family_mutex = &proto_family_mutex_data; | |
79 | static TAILQ_HEAD(, proto_family_str) proto_family_head = | |
80 | TAILQ_HEAD_INITIALIZER(proto_family_head); | |
91447636 | 81 | |
91447636 A |
82 | __private_extern__ void |
83 | proto_kpi_init(void) | |
84 | { | |
316670eb A |
85 | lck_grp_attr_t *grp_attrib = NULL; |
86 | lck_attr_t *lck_attrib = NULL; | |
87 | lck_grp_t *lck_group = NULL; | |
88 | ||
91447636 A |
89 | /* Allocate a mtx lock */ |
90 | grp_attrib = lck_grp_attr_alloc_init(); | |
91447636 A |
91 | lck_group = lck_grp_alloc_init("protocol kpi", grp_attrib); |
92 | lck_grp_attr_free(grp_attrib); | |
93 | lck_attrib = lck_attr_alloc_init(); | |
316670eb | 94 | lck_mtx_init(proto_family_mutex, lck_group, lck_attrib); |
91447636 A |
95 | lck_grp_free(lck_group); |
96 | lck_attr_free(lck_attrib); | |
316670eb A |
97 | |
98 | bzero(proto_hash, sizeof (proto_hash)); | |
91447636 A |
99 | } |
100 | ||
101 | __private_extern__ errno_t | |
316670eb A |
102 | proto_register_input(protocol_family_t protocol, proto_input_handler input, |
103 | proto_input_detached_handler detached, int chains) | |
91447636 | 104 | { |
91447636 | 105 | struct proto_input_entry *entry; |
316670eb | 106 | struct dlil_threading_info *inp = dlil_main_input_thread; |
39236c6e A |
107 | struct domain *dp; |
108 | domain_guard_t guard; | |
316670eb A |
109 | |
110 | entry = _MALLOC(sizeof (*entry), M_IFADDR, M_WAITOK); | |
91447636 | 111 | if (entry == NULL) |
316670eb A |
112 | return (ENOMEM); |
113 | ||
114 | bzero(entry, sizeof (*entry)); | |
91447636 A |
115 | entry->protocol = protocol; |
116 | entry->input = input; | |
117 | entry->detached = detached; | |
2d21ac55 A |
118 | entry->hash = proto_hash_value(protocol); |
119 | entry->chain = chains; | |
91447636 | 120 | |
39236c6e A |
121 | guard = domain_guard_deploy(); |
122 | TAILQ_FOREACH(dp, &domains, dom_entry) { | |
123 | if (dp->dom_family == (int)protocol) | |
124 | break; | |
125 | } | |
126 | domain_guard_release(guard); | |
127 | if (dp == NULL) | |
128 | return (EINVAL); | |
129 | ||
316670eb | 130 | entry->domain = dp; |
316670eb A |
131 | |
132 | lck_mtx_lock(&inp->input_lck); | |
2d21ac55 A |
133 | entry->next = proto_input_add_list; |
134 | proto_input_add_list = entry; | |
91447636 | 135 | |
316670eb A |
136 | inp->input_waiting |= DLIL_PROTO_REGISTER; |
137 | if ((inp->input_waiting & DLIL_INPUT_RUNNING) == 0) | |
138 | wakeup((caddr_t)&inp->input_waiting); | |
139 | lck_mtx_unlock(&inp->input_lck); | |
140 | ||
141 | return (0); | |
142 | } | |
91447636 A |
143 | |
144 | __private_extern__ void | |
316670eb | 145 | proto_unregister_input(protocol_family_t protocol) |
91447636 A |
146 | { |
147 | struct proto_input_entry *entry = NULL; | |
316670eb A |
148 | |
149 | for (entry = proto_hash[proto_hash_value(protocol)]; entry != NULL; | |
150 | entry = entry->next) { | |
91447636 A |
151 | if (entry->protocol == protocol) |
152 | break; | |
316670eb A |
153 | } |
154 | ||
155 | if (entry != NULL) | |
91447636 A |
156 | entry->detach = 1; |
157 | } | |
158 | ||
91447636 | 159 | static void |
316670eb | 160 | proto_delayed_attach(struct proto_input_entry *entry) |
91447636 A |
161 | { |
162 | struct proto_input_entry *next_entry; | |
316670eb A |
163 | |
164 | for (next_entry = entry->next; entry != NULL; entry = next_entry) { | |
91447636 A |
165 | struct proto_input_entry *exist; |
166 | int hash_slot; | |
316670eb | 167 | |
91447636 A |
168 | hash_slot = proto_hash_value(entry->protocol); |
169 | next_entry = entry->next; | |
316670eb A |
170 | |
171 | for (exist = proto_hash[hash_slot]; exist != NULL; | |
172 | exist = exist->next) { | |
91447636 A |
173 | if (exist->protocol == entry->protocol) |
174 | break; | |
316670eb A |
175 | } |
176 | ||
91447636 | 177 | /* If the entry already exists, call detached and dispose */ |
316670eb | 178 | if (exist != NULL) { |
91447636 A |
179 | if (entry->detached) |
180 | entry->detached(entry->protocol); | |
181 | FREE(entry, M_IFADDR); | |
316670eb | 182 | } else { |
91447636 A |
183 | entry->next = proto_hash[hash_slot]; |
184 | proto_hash[hash_slot] = entry; | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
91447636 A |
189 | __private_extern__ void |
190 | proto_input_run(void) | |
191 | { | |
316670eb A |
192 | struct proto_input_entry *entry; |
193 | struct dlil_threading_info *inp = dlil_main_input_thread; | |
2d21ac55 A |
194 | mbuf_t packet_list; |
195 | int i, locked = 0; | |
196 | ||
316670eb | 197 | lck_mtx_assert(&inp->input_lck, LCK_MTX_ASSERT_NOTOWNED); |
91447636 | 198 | |
316670eb A |
199 | if (inp->input_waiting & DLIL_PROTO_REGISTER) { |
200 | lck_mtx_lock_spin(&inp->input_lck); | |
91447636 | 201 | entry = proto_input_add_list; |
2d21ac55 | 202 | proto_input_add_list = NULL; |
316670eb A |
203 | inp->input_waiting &= ~DLIL_PROTO_REGISTER; |
204 | lck_mtx_unlock(&inp->input_lck); | |
91447636 | 205 | proto_delayed_attach(entry); |
2d21ac55 | 206 | } |
316670eb | 207 | |
2d21ac55 | 208 | /* |
316670eb A |
209 | * Move everything from the lock protected list to the thread |
210 | * specific list. | |
2d21ac55 A |
211 | */ |
212 | for (i = 0; proto_total_waiting != 0 && i < PROTO_HASH_SLOTS; i++) { | |
316670eb A |
213 | for (entry = proto_hash[i]; |
214 | entry != NULL && proto_total_waiting; entry = entry->next) { | |
215 | if (entry->inject_first != NULL) { | |
216 | lck_mtx_lock_spin(&inp->input_lck); | |
217 | inp->input_waiting &= ~DLIL_PROTO_WAITING; | |
2d21ac55 A |
218 | |
219 | packet_list = entry->inject_first; | |
220 | ||
221 | entry->inject_first = NULL; | |
222 | entry->inject_last = NULL; | |
223 | proto_total_waiting--; | |
224 | ||
316670eb | 225 | lck_mtx_unlock(&inp->input_lck); |
2d21ac55 | 226 | |
316670eb A |
227 | if (entry->domain != NULL && !(entry->domain-> |
228 | dom_flags & DOM_REENTRANT)) { | |
2d21ac55 A |
229 | lck_mtx_lock(entry->domain->dom_mtx); |
230 | locked = 1; | |
231 | } | |
316670eb | 232 | |
2d21ac55 | 233 | if (entry->chain) { |
316670eb A |
234 | entry->input(entry->protocol, |
235 | packet_list); | |
236 | } else { | |
2d21ac55 | 237 | mbuf_t packet; |
316670eb A |
238 | |
239 | for (packet = packet_list; | |
240 | packet != NULL; | |
241 | packet = packet_list) { | |
242 | packet_list = | |
243 | mbuf_nextpkt(packet); | |
2d21ac55 | 244 | mbuf_setnextpkt(packet, NULL); |
316670eb A |
245 | entry->input(entry->protocol, |
246 | packet); | |
91447636 A |
247 | } |
248 | } | |
2d21ac55 | 249 | if (locked) { |
4a3eedf9 | 250 | locked = 0; |
2d21ac55 | 251 | lck_mtx_unlock(entry->domain->dom_mtx); |
316670eb A |
252 | } |
253 | } | |
91447636 A |
254 | } |
255 | } | |
256 | } | |
257 | ||
258 | errno_t | |
316670eb | 259 | proto_input(protocol_family_t protocol, mbuf_t packet_list) |
91447636 | 260 | { |
316670eb A |
261 | struct proto_input_entry *entry; |
262 | errno_t locked = 0, result = 0; | |
2d21ac55 | 263 | |
316670eb A |
264 | for (entry = proto_hash[proto_hash_value(protocol)]; entry != NULL; |
265 | entry = entry->next) { | |
91447636 A |
266 | if (entry->protocol == protocol) |
267 | break; | |
268 | } | |
269 | ||
316670eb | 270 | if (entry->domain && !(entry->domain->dom_flags & DOM_REENTRANT)) { |
2d21ac55 A |
271 | lck_mtx_lock(entry->domain->dom_mtx); |
272 | locked = 1; | |
273 | } | |
316670eb | 274 | |
2d21ac55 A |
275 | if (entry->chain) { |
276 | entry->input(entry->protocol, packet_list); | |
316670eb | 277 | } else { |
91447636 | 278 | mbuf_t packet; |
316670eb A |
279 | |
280 | for (packet = packet_list; packet != NULL; | |
281 | packet = packet_list) { | |
91447636 A |
282 | packet_list = mbuf_nextpkt(packet); |
283 | mbuf_setnextpkt(packet, NULL); | |
284 | entry->input(entry->protocol, packet); | |
285 | } | |
91447636 | 286 | } |
316670eb | 287 | |
2d21ac55 A |
288 | if (locked) { |
289 | lck_mtx_unlock(entry->domain->dom_mtx); | |
316670eb A |
290 | } |
291 | return (result); | |
91447636 A |
292 | } |
293 | ||
294 | errno_t | |
316670eb | 295 | proto_inject(protocol_family_t protocol, mbuf_t packet_list) |
91447636 | 296 | { |
316670eb A |
297 | struct proto_input_entry *entry; |
298 | mbuf_t last_packet; | |
299 | int hash_slot = proto_hash_value(protocol); | |
300 | struct dlil_threading_info *inp = dlil_main_input_thread; | |
301 | ||
302 | for (last_packet = packet_list; mbuf_nextpkt(last_packet) != NULL; | |
303 | last_packet = mbuf_nextpkt(last_packet)) | |
91447636 | 304 | /* find the last packet */; |
316670eb A |
305 | |
306 | for (entry = proto_hash[hash_slot]; entry != NULL; | |
307 | entry = entry->next) { | |
91447636 A |
308 | if (entry->protocol == protocol) |
309 | break; | |
310 | } | |
316670eb A |
311 | |
312 | if (entry != NULL) { | |
313 | lck_mtx_lock(&inp->input_lck); | |
2d21ac55 A |
314 | if (entry->inject_first == NULL) { |
315 | proto_total_waiting++; | |
316670eb | 316 | inp->input_waiting |= DLIL_PROTO_WAITING; |
2d21ac55 | 317 | entry->inject_first = packet_list; |
316670eb | 318 | } else { |
2d21ac55 | 319 | mbuf_setnextpkt(entry->inject_last, packet_list); |
91447636 | 320 | } |
2d21ac55 | 321 | entry->inject_last = last_packet; |
316670eb A |
322 | if ((inp->input_waiting & DLIL_INPUT_RUNNING) == 0) { |
323 | wakeup((caddr_t)&inp->input_waiting); | |
2d21ac55 | 324 | } |
316670eb A |
325 | lck_mtx_unlock(&inp->input_lck); |
326 | } else { | |
327 | return (ENOENT); | |
91447636 A |
328 | } |
329 | ||
316670eb | 330 | return (0); |
91447636 A |
331 | } |
332 | ||
316670eb A |
333 | static struct proto_family_str * |
334 | proto_plumber_find(protocol_family_t proto_family, ifnet_family_t if_family) | |
2d21ac55 A |
335 | { |
336 | struct proto_family_str *mod = NULL; | |
337 | ||
338 | TAILQ_FOREACH(mod, &proto_family_head, proto_fam_next) { | |
316670eb A |
339 | if ((mod->proto_family == (proto_family & 0xffff)) && |
340 | (mod->if_family == (if_family & 0xffff))) | |
2d21ac55 | 341 | break; |
316670eb | 342 | } |
2d21ac55 | 343 | |
316670eb | 344 | return (mod); |
2d21ac55 A |
345 | } |
346 | ||
91447636 | 347 | errno_t |
316670eb A |
348 | proto_register_plumber(protocol_family_t protocol_family, |
349 | ifnet_family_t interface_family, proto_plumb_handler attach, | |
350 | proto_unplumb_handler detach) | |
91447636 | 351 | { |
2d21ac55 A |
352 | struct proto_family_str *proto_family; |
353 | ||
316670eb A |
354 | if (attach == NULL) |
355 | return (EINVAL); | |
2d21ac55 A |
356 | |
357 | lck_mtx_lock(proto_family_mutex); | |
316670eb | 358 | |
2d21ac55 A |
359 | TAILQ_FOREACH(proto_family, &proto_family_head, proto_fam_next) { |
360 | if (proto_family->proto_family == protocol_family && | |
316670eb | 361 | proto_family->if_family == interface_family) { |
2d21ac55 | 362 | lck_mtx_unlock(proto_family_mutex); |
316670eb | 363 | return (EEXIST); |
2d21ac55 A |
364 | } |
365 | } | |
366 | ||
316670eb A |
367 | proto_family = (struct proto_family_str *) |
368 | _MALLOC(sizeof (struct proto_family_str), M_IFADDR, M_WAITOK); | |
2d21ac55 A |
369 | if (!proto_family) { |
370 | lck_mtx_unlock(proto_family_mutex); | |
316670eb | 371 | return (ENOMEM); |
2d21ac55 A |
372 | } |
373 | ||
316670eb | 374 | bzero(proto_family, sizeof (struct proto_family_str)); |
2d21ac55 A |
375 | proto_family->proto_family = protocol_family; |
376 | proto_family->if_family = interface_family & 0xffff; | |
377 | proto_family->attach_proto = attach; | |
378 | proto_family->detach_proto = detach; | |
379 | ||
380 | TAILQ_INSERT_TAIL(&proto_family_head, proto_family, proto_fam_next); | |
381 | lck_mtx_unlock(proto_family_mutex); | |
316670eb | 382 | return (0); |
91447636 A |
383 | } |
384 | ||
385 | void | |
316670eb A |
386 | proto_unregister_plumber(protocol_family_t protocol_family, |
387 | ifnet_family_t interface_family) | |
91447636 | 388 | { |
2d21ac55 A |
389 | struct proto_family_str *proto_family; |
390 | ||
391 | lck_mtx_lock(proto_family_mutex); | |
392 | ||
393 | proto_family = proto_plumber_find(protocol_family, interface_family); | |
316670eb | 394 | if (proto_family == NULL) { |
2d21ac55 A |
395 | lck_mtx_unlock(proto_family_mutex); |
396 | return; | |
397 | } | |
398 | ||
399 | TAILQ_REMOVE(&proto_family_head, proto_family, proto_fam_next); | |
400 | FREE(proto_family, M_IFADDR); | |
316670eb | 401 | |
2d21ac55 | 402 | lck_mtx_unlock(proto_family_mutex); |
2d21ac55 A |
403 | } |
404 | ||
405 | __private_extern__ errno_t | |
316670eb | 406 | proto_plumb(protocol_family_t protocol_family, ifnet_t ifp) |
2d21ac55 A |
407 | { |
408 | struct proto_family_str *proto_family; | |
409 | int ret = 0; | |
410 | ||
411 | lck_mtx_lock(proto_family_mutex); | |
412 | proto_family = proto_plumber_find(protocol_family, ifp->if_family); | |
316670eb | 413 | if (proto_family == NULL) { |
2d21ac55 | 414 | lck_mtx_unlock(proto_family_mutex); |
316670eb | 415 | return (ENXIO); |
2d21ac55 A |
416 | } |
417 | ||
418 | ret = proto_family->attach_proto(ifp, protocol_family); | |
419 | ||
420 | lck_mtx_unlock(proto_family_mutex); | |
316670eb | 421 | return (ret); |
2d21ac55 A |
422 | } |
423 | ||
424 | ||
425 | __private_extern__ errno_t | |
316670eb | 426 | proto_unplumb(protocol_family_t protocol_family, ifnet_t ifp) |
2d21ac55 A |
427 | { |
428 | struct proto_family_str *proto_family; | |
429 | int ret = 0; | |
430 | ||
431 | lck_mtx_lock(proto_family_mutex); | |
432 | ||
433 | proto_family = proto_plumber_find(protocol_family, ifp->if_family); | |
316670eb | 434 | if (proto_family != NULL && proto_family->detach_proto) |
2d21ac55 A |
435 | proto_family->detach_proto(ifp, protocol_family); |
436 | else | |
437 | ret = ifnet_detach_protocol(ifp, protocol_family); | |
316670eb | 438 | |
2d21ac55 | 439 | lck_mtx_unlock(proto_family_mutex); |
316670eb | 440 | return (ret); |
91447636 | 441 | } |