]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/kpi_protocol.c
xnu-3789.41.3.tar.gz
[apple/xnu.git] / bsd / net / kpi_protocol.c
CommitLineData
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
41void proto_input_run(void);
42
b0d623f7
A
43typedef int (*attach_t)(struct ifnet *ifp, uint32_t protocol_family);
44typedef int (*detach_t)(struct ifnet *ifp, uint32_t protocol_family);
91447636 45
91447636 46struct 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
66struct 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
74static struct proto_input_entry *proto_hash[PROTO_HASH_SLOTS];
75static int proto_total_waiting = 0;
76static struct proto_input_entry *proto_input_add_list = NULL;
77decl_lck_mtx_data(static, proto_family_mutex_data);
78static lck_mtx_t *proto_family_mutex = &proto_family_mutex_data;
79static TAILQ_HEAD(, proto_family_str) proto_family_head =
80 TAILQ_HEAD_INITIALIZER(proto_family_head);
91447636 81
91447636
A
82__private_extern__ void
83proto_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
102proto_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 109
3e170ce0 110 entry = _MALLOC(sizeof (*entry), M_IFADDR, M_WAITOK | M_ZERO);
91447636 111 if (entry == NULL)
316670eb
A
112 return (ENOMEM);
113
91447636
A
114 entry->protocol = protocol;
115 entry->input = input;
116 entry->detached = detached;
2d21ac55
A
117 entry->hash = proto_hash_value(protocol);
118 entry->chain = chains;
91447636 119
39236c6e
A
120 guard = domain_guard_deploy();
121 TAILQ_FOREACH(dp, &domains, dom_entry) {
122 if (dp->dom_family == (int)protocol)
123 break;
124 }
125 domain_guard_release(guard);
126 if (dp == NULL)
127 return (EINVAL);
128
316670eb 129 entry->domain = dp;
316670eb
A
130
131 lck_mtx_lock(&inp->input_lck);
2d21ac55
A
132 entry->next = proto_input_add_list;
133 proto_input_add_list = entry;
91447636 134
316670eb
A
135 inp->input_waiting |= DLIL_PROTO_REGISTER;
136 if ((inp->input_waiting & DLIL_INPUT_RUNNING) == 0)
137 wakeup((caddr_t)&inp->input_waiting);
138 lck_mtx_unlock(&inp->input_lck);
139
140 return (0);
141}
91447636
A
142
143__private_extern__ void
316670eb 144proto_unregister_input(protocol_family_t protocol)
91447636
A
145{
146 struct proto_input_entry *entry = NULL;
316670eb
A
147
148 for (entry = proto_hash[proto_hash_value(protocol)]; entry != NULL;
149 entry = entry->next) {
91447636
A
150 if (entry->protocol == protocol)
151 break;
316670eb
A
152 }
153
154 if (entry != NULL)
91447636
A
155 entry->detach = 1;
156}
157
91447636 158static void
316670eb 159proto_delayed_attach(struct proto_input_entry *entry)
91447636
A
160{
161 struct proto_input_entry *next_entry;
316670eb
A
162
163 for (next_entry = entry->next; entry != NULL; entry = next_entry) {
91447636
A
164 struct proto_input_entry *exist;
165 int hash_slot;
316670eb 166
91447636
A
167 hash_slot = proto_hash_value(entry->protocol);
168 next_entry = entry->next;
316670eb
A
169
170 for (exist = proto_hash[hash_slot]; exist != NULL;
171 exist = exist->next) {
91447636
A
172 if (exist->protocol == entry->protocol)
173 break;
316670eb
A
174 }
175
91447636 176 /* If the entry already exists, call detached and dispose */
316670eb 177 if (exist != NULL) {
91447636
A
178 if (entry->detached)
179 entry->detached(entry->protocol);
180 FREE(entry, M_IFADDR);
316670eb 181 } else {
91447636
A
182 entry->next = proto_hash[hash_slot];
183 proto_hash[hash_slot] = entry;
184 }
185 }
186}
187
91447636
A
188__private_extern__ void
189proto_input_run(void)
190{
316670eb
A
191 struct proto_input_entry *entry;
192 struct dlil_threading_info *inp = dlil_main_input_thread;
2d21ac55
A
193 mbuf_t packet_list;
194 int i, locked = 0;
195
316670eb 196 lck_mtx_assert(&inp->input_lck, LCK_MTX_ASSERT_NOTOWNED);
91447636 197
316670eb
A
198 if (inp->input_waiting & DLIL_PROTO_REGISTER) {
199 lck_mtx_lock_spin(&inp->input_lck);
91447636 200 entry = proto_input_add_list;
2d21ac55 201 proto_input_add_list = NULL;
316670eb
A
202 inp->input_waiting &= ~DLIL_PROTO_REGISTER;
203 lck_mtx_unlock(&inp->input_lck);
91447636 204 proto_delayed_attach(entry);
2d21ac55 205 }
316670eb 206
2d21ac55 207 /*
316670eb
A
208 * Move everything from the lock protected list to the thread
209 * specific list.
2d21ac55
A
210 */
211 for (i = 0; proto_total_waiting != 0 && i < PROTO_HASH_SLOTS; i++) {
316670eb
A
212 for (entry = proto_hash[i];
213 entry != NULL && proto_total_waiting; entry = entry->next) {
214 if (entry->inject_first != NULL) {
215 lck_mtx_lock_spin(&inp->input_lck);
216 inp->input_waiting &= ~DLIL_PROTO_WAITING;
2d21ac55
A
217
218 packet_list = entry->inject_first;
219
220 entry->inject_first = NULL;
221 entry->inject_last = NULL;
222 proto_total_waiting--;
223
316670eb 224 lck_mtx_unlock(&inp->input_lck);
2d21ac55 225
316670eb
A
226 if (entry->domain != NULL && !(entry->domain->
227 dom_flags & DOM_REENTRANT)) {
2d21ac55
A
228 lck_mtx_lock(entry->domain->dom_mtx);
229 locked = 1;
230 }
316670eb 231
2d21ac55 232 if (entry->chain) {
316670eb
A
233 entry->input(entry->protocol,
234 packet_list);
235 } else {
2d21ac55 236 mbuf_t packet;
316670eb
A
237
238 for (packet = packet_list;
239 packet != NULL;
240 packet = packet_list) {
241 packet_list =
242 mbuf_nextpkt(packet);
2d21ac55 243 mbuf_setnextpkt(packet, NULL);
316670eb
A
244 entry->input(entry->protocol,
245 packet);
91447636
A
246 }
247 }
2d21ac55 248 if (locked) {
4a3eedf9 249 locked = 0;
2d21ac55 250 lck_mtx_unlock(entry->domain->dom_mtx);
316670eb
A
251 }
252 }
91447636
A
253 }
254 }
255}
256
257errno_t
316670eb 258proto_input(protocol_family_t protocol, mbuf_t packet_list)
91447636 259{
316670eb
A
260 struct proto_input_entry *entry;
261 errno_t locked = 0, result = 0;
2d21ac55 262
316670eb
A
263 for (entry = proto_hash[proto_hash_value(protocol)]; entry != NULL;
264 entry = entry->next) {
91447636
A
265 if (entry->protocol == protocol)
266 break;
267 }
268
316670eb 269 if (entry->domain && !(entry->domain->dom_flags & DOM_REENTRANT)) {
2d21ac55
A
270 lck_mtx_lock(entry->domain->dom_mtx);
271 locked = 1;
272 }
316670eb 273
2d21ac55
A
274 if (entry->chain) {
275 entry->input(entry->protocol, packet_list);
316670eb 276 } else {
91447636 277 mbuf_t packet;
316670eb
A
278
279 for (packet = packet_list; packet != NULL;
280 packet = packet_list) {
91447636
A
281 packet_list = mbuf_nextpkt(packet);
282 mbuf_setnextpkt(packet, NULL);
283 entry->input(entry->protocol, packet);
284 }
91447636 285 }
316670eb 286
2d21ac55
A
287 if (locked) {
288 lck_mtx_unlock(entry->domain->dom_mtx);
316670eb
A
289 }
290 return (result);
91447636
A
291}
292
293errno_t
316670eb 294proto_inject(protocol_family_t protocol, mbuf_t packet_list)
91447636 295{
316670eb
A
296 struct proto_input_entry *entry;
297 mbuf_t last_packet;
298 int hash_slot = proto_hash_value(protocol);
299 struct dlil_threading_info *inp = dlil_main_input_thread;
300
301 for (last_packet = packet_list; mbuf_nextpkt(last_packet) != NULL;
302 last_packet = mbuf_nextpkt(last_packet))
91447636 303 /* find the last packet */;
316670eb
A
304
305 for (entry = proto_hash[hash_slot]; entry != NULL;
306 entry = entry->next) {
91447636
A
307 if (entry->protocol == protocol)
308 break;
309 }
316670eb
A
310
311 if (entry != NULL) {
312 lck_mtx_lock(&inp->input_lck);
2d21ac55
A
313 if (entry->inject_first == NULL) {
314 proto_total_waiting++;
316670eb 315 inp->input_waiting |= DLIL_PROTO_WAITING;
2d21ac55 316 entry->inject_first = packet_list;
316670eb 317 } else {
2d21ac55 318 mbuf_setnextpkt(entry->inject_last, packet_list);
91447636 319 }
2d21ac55 320 entry->inject_last = last_packet;
316670eb
A
321 if ((inp->input_waiting & DLIL_INPUT_RUNNING) == 0) {
322 wakeup((caddr_t)&inp->input_waiting);
2d21ac55 323 }
316670eb
A
324 lck_mtx_unlock(&inp->input_lck);
325 } else {
326 return (ENOENT);
91447636
A
327 }
328
316670eb 329 return (0);
91447636
A
330}
331
316670eb
A
332static struct proto_family_str *
333proto_plumber_find(protocol_family_t proto_family, ifnet_family_t if_family)
2d21ac55
A
334{
335 struct proto_family_str *mod = NULL;
336
337 TAILQ_FOREACH(mod, &proto_family_head, proto_fam_next) {
316670eb
A
338 if ((mod->proto_family == (proto_family & 0xffff)) &&
339 (mod->if_family == (if_family & 0xffff)))
2d21ac55 340 break;
316670eb 341 }
2d21ac55 342
316670eb 343 return (mod);
2d21ac55
A
344}
345
91447636 346errno_t
316670eb
A
347proto_register_plumber(protocol_family_t protocol_family,
348 ifnet_family_t interface_family, proto_plumb_handler attach,
349 proto_unplumb_handler detach)
91447636 350{
2d21ac55
A
351 struct proto_family_str *proto_family;
352
316670eb
A
353 if (attach == NULL)
354 return (EINVAL);
2d21ac55
A
355
356 lck_mtx_lock(proto_family_mutex);
316670eb 357
2d21ac55
A
358 TAILQ_FOREACH(proto_family, &proto_family_head, proto_fam_next) {
359 if (proto_family->proto_family == protocol_family &&
316670eb 360 proto_family->if_family == interface_family) {
2d21ac55 361 lck_mtx_unlock(proto_family_mutex);
316670eb 362 return (EEXIST);
2d21ac55
A
363 }
364 }
365
316670eb 366 proto_family = (struct proto_family_str *)
3e170ce0
A
367 _MALLOC(sizeof (struct proto_family_str), M_IFADDR,
368 M_WAITOK | M_ZERO);
2d21ac55
A
369 if (!proto_family) {
370 lck_mtx_unlock(proto_family_mutex);
316670eb 371 return (ENOMEM);
2d21ac55
A
372 }
373
2d21ac55
A
374 proto_family->proto_family = protocol_family;
375 proto_family->if_family = interface_family & 0xffff;
376 proto_family->attach_proto = attach;
377 proto_family->detach_proto = detach;
378
379 TAILQ_INSERT_TAIL(&proto_family_head, proto_family, proto_fam_next);
380 lck_mtx_unlock(proto_family_mutex);
316670eb 381 return (0);
91447636
A
382}
383
384void
316670eb
A
385proto_unregister_plumber(protocol_family_t protocol_family,
386 ifnet_family_t interface_family)
91447636 387{
2d21ac55
A
388 struct proto_family_str *proto_family;
389
390 lck_mtx_lock(proto_family_mutex);
391
392 proto_family = proto_plumber_find(protocol_family, interface_family);
316670eb 393 if (proto_family == NULL) {
2d21ac55
A
394 lck_mtx_unlock(proto_family_mutex);
395 return;
396 }
397
398 TAILQ_REMOVE(&proto_family_head, proto_family, proto_fam_next);
399 FREE(proto_family, M_IFADDR);
316670eb 400
2d21ac55 401 lck_mtx_unlock(proto_family_mutex);
2d21ac55
A
402}
403
404__private_extern__ errno_t
316670eb 405proto_plumb(protocol_family_t protocol_family, ifnet_t ifp)
2d21ac55
A
406{
407 struct proto_family_str *proto_family;
408 int ret = 0;
409
410 lck_mtx_lock(proto_family_mutex);
411 proto_family = proto_plumber_find(protocol_family, ifp->if_family);
316670eb 412 if (proto_family == NULL) {
2d21ac55 413 lck_mtx_unlock(proto_family_mutex);
316670eb 414 return (ENXIO);
2d21ac55
A
415 }
416
417 ret = proto_family->attach_proto(ifp, protocol_family);
418
419 lck_mtx_unlock(proto_family_mutex);
316670eb 420 return (ret);
2d21ac55
A
421}
422
423
424__private_extern__ errno_t
316670eb 425proto_unplumb(protocol_family_t protocol_family, ifnet_t ifp)
2d21ac55
A
426{
427 struct proto_family_str *proto_family;
428 int ret = 0;
429
430 lck_mtx_lock(proto_family_mutex);
431
432 proto_family = proto_plumber_find(protocol_family, ifp->if_family);
316670eb 433 if (proto_family != NULL && proto_family->detach_proto)
2d21ac55
A
434 proto_family->detach_proto(ifp, protocol_family);
435 else
436 ret = ifnet_detach_protocol(ifp, protocol_family);
316670eb 437
2d21ac55 438 lck_mtx_unlock(proto_family_mutex);
316670eb 439 return (ret);
91447636 440}