]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/kpi_protocol.c
xnu-1228.0.2.tar.gz
[apple/xnu.git] / bsd / net / kpi_protocol.c
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
41 void proto_input_run(void);
42
43 typedef int (*attach_t)(struct ifnet *ifp, u_long protocol_family);
44 typedef int (*detach_t)(struct ifnet *ifp, u_long protocol_family);
45
46 struct proto_input_entry {
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;
55 proto_input_detached_handler detached;
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;
63 };
64
65
66 struct proto_family_str {
67 TAILQ_ENTRY(proto_family_str) proto_fam_next;
68 protocol_family_t proto_family;
69 ifnet_family_t if_family;
70 proto_plumb_handler attach_proto;
71 proto_unplumb_handler detach_proto;
72 };
73
74 #define PROTO_HASH_SLOTS 5
75
76 static struct proto_input_entry *proto_hash[PROTO_HASH_SLOTS];
77 static int proto_total_waiting = 0;
78 static struct proto_input_entry *proto_input_add_list = NULL;
79 static lck_mtx_t *proto_family_mutex = 0;
80 static TAILQ_HEAD(, proto_family_str) proto_family_head =
81 TAILQ_HEAD_INITIALIZER(proto_family_head);
82
83 extern lck_mtx_t *domain_proto_mtx;
84 extern struct dlil_threading_info *dlil_lo_thread_ptr;
85
86 static int
87 proto_hash_value(
88 protocol_family_t protocol)
89 {
90 switch(protocol) {
91 case PF_INET:
92 return 0;
93 case PF_INET6:
94 return 1;
95 case PF_APPLETALK:
96 return 2;
97 case PF_VLAN:
98 return 3;
99 }
100 return 4;
101 }
102
103 __private_extern__ void
104 proto_kpi_init(void)
105 {
106 lck_grp_attr_t *grp_attrib = 0;
107 lck_attr_t *lck_attrib = 0;
108 lck_grp_t *lck_group = 0;
109
110 /* Allocate a mtx lock */
111 grp_attrib = lck_grp_attr_alloc_init();
112 lck_group = lck_grp_alloc_init("protocol kpi", grp_attrib);
113 lck_grp_attr_free(grp_attrib);
114 lck_attrib = lck_attr_alloc_init();
115 proto_family_mutex = lck_mtx_alloc_init(lck_group, lck_attrib);
116 lck_grp_free(lck_group);
117 lck_attr_free(lck_attrib);
118
119 bzero(proto_hash, sizeof(proto_hash));
120 }
121
122 __private_extern__ errno_t
123 proto_register_input(
124 protocol_family_t protocol,
125 proto_input_handler input,
126 proto_input_detached_handler detached,
127 int chains)
128 {
129
130 struct proto_input_entry *entry;
131 struct dlil_threading_info *thread = dlil_lo_thread_ptr;
132
133 entry = _MALLOC(sizeof(*entry), M_IFADDR, M_WAITOK);
134
135 if (entry == NULL)
136 return ENOMEM;
137
138 bzero(entry, sizeof(*entry));
139 entry->protocol = protocol;
140 entry->input = input;
141 entry->detached = detached;
142 entry->hash = proto_hash_value(protocol);
143 entry->chain = chains;
144
145 {
146 struct domain *dp = domains;
147
148 lck_mtx_assert(domain_proto_mtx, LCK_MTX_ASSERT_NOTOWNED);
149 lck_mtx_lock(domain_proto_mtx);
150 while (dp && (protocol_family_t)dp->dom_family != protocol)
151 dp = dp->dom_next;
152 entry->domain = dp;
153 lck_mtx_unlock(domain_proto_mtx);
154 }
155
156
157 lck_mtx_lock(thread->input_lck);
158 entry->next = proto_input_add_list;
159 proto_input_add_list = entry;
160
161 thread->input_waiting |= DLIL_PROTO_REGISTER;
162 if ((thread->input_waiting & DLIL_INPUT_RUNNING) == 0)
163 wakeup((caddr_t)&thread->input_waiting);
164 lck_mtx_unlock(thread->input_lck);
165
166 return 0;
167 }
168
169
170 __private_extern__ void
171 proto_unregister_input(
172 protocol_family_t protocol)
173 {
174 struct proto_input_entry *entry = NULL;
175
176 for (entry = proto_hash[proto_hash_value(protocol)]; entry; entry = entry->next)
177 if (entry->protocol == protocol)
178 break;
179
180 if (entry)
181 entry->detach = 1;
182 }
183
184
185 static void
186 proto_delayed_attach(
187 struct proto_input_entry *entry)
188 {
189 struct proto_input_entry *next_entry;
190 for (next_entry = entry->next; entry; entry = next_entry) {
191 struct proto_input_entry *exist;
192 int hash_slot;
193
194 hash_slot = proto_hash_value(entry->protocol);
195 next_entry = entry->next;
196
197 for (exist = proto_hash[hash_slot]; exist; exist = exist->next)
198 if (exist->protocol == entry->protocol)
199 break;
200
201 /* If the entry already exists, call detached and dispose */
202 if (exist) {
203 if (entry->detached)
204 entry->detached(entry->protocol);
205 FREE(entry, M_IFADDR);
206 }
207 else {
208 entry->next = proto_hash[hash_slot];
209 proto_hash[hash_slot] = entry;
210 }
211 }
212 }
213
214 __private_extern__ void
215 proto_input_run(void)
216 {
217 struct proto_input_entry *entry;
218 struct dlil_threading_info *thread = dlil_lo_thread_ptr;
219 mbuf_t packet_list;
220 int i, locked = 0;
221
222 lck_mtx_assert(thread->input_lck, LCK_MTX_ASSERT_NOTOWNED);
223
224 if ((thread->input_waiting & DLIL_PROTO_REGISTER) != 0) {
225 lck_mtx_lock(thread->input_lck);
226 entry = proto_input_add_list;
227 proto_input_add_list = NULL;
228 thread->input_waiting &= ~DLIL_PROTO_REGISTER;
229 lck_mtx_unlock(thread->input_lck);
230 proto_delayed_attach(entry);
231 }
232 /*
233 Move everything from the lock protected list to the thread
234 specific list.
235 */
236 for (i = 0; proto_total_waiting != 0 && i < PROTO_HASH_SLOTS; i++) {
237 for (entry = proto_hash[i]; entry && proto_total_waiting;
238 entry = entry->next) {
239 if (entry->inject_first) {
240 lck_mtx_lock(thread->input_lck);
241 thread->input_waiting &= ~DLIL_PROTO_WAITING;
242
243 packet_list = entry->inject_first;
244
245 entry->inject_first = NULL;
246 entry->inject_last = NULL;
247 proto_total_waiting--;
248
249 lck_mtx_unlock(thread->input_lck);
250
251 if (entry->domain && (entry->domain->dom_flags & DOM_REENTRANT) == 0) {
252 lck_mtx_lock(entry->domain->dom_mtx);
253 locked = 1;
254 }
255
256 if (entry->chain) {
257 entry->input(entry->protocol, packet_list);
258 }
259 else {
260 mbuf_t packet;
261
262 for (packet = packet_list; packet; packet = packet_list) {
263 packet_list = mbuf_nextpkt(packet);
264 mbuf_setnextpkt(packet, NULL);
265 entry->input(entry->protocol, packet);
266 }
267 }
268 if (locked) {
269 lck_mtx_unlock(entry->domain->dom_mtx);
270 }
271 }
272 }
273 }
274
275 }
276
277 errno_t
278 proto_input(
279 protocol_family_t protocol,
280 mbuf_t packet_list)
281 {
282 struct proto_input_entry *entry;
283 errno_t locked =0, result = 0;
284
285 for (entry = proto_hash[proto_hash_value(protocol)]; entry;
286 entry = entry->next) {
287 if (entry->protocol == protocol)
288 break;
289 }
290
291 if (entry->domain && (entry->domain->dom_flags & DOM_REENTRANT) == 0) {
292 lck_mtx_lock(entry->domain->dom_mtx);
293 locked = 1;
294 }
295
296 if (entry->chain) {
297 entry->input(entry->protocol, packet_list);
298 }
299 else {
300 mbuf_t packet;
301
302 for (packet = packet_list; packet; packet = packet_list) {
303 packet_list = mbuf_nextpkt(packet);
304 mbuf_setnextpkt(packet, NULL);
305 entry->input(entry->protocol, packet);
306 }
307 }
308
309 if (locked) {
310 lck_mtx_unlock(entry->domain->dom_mtx);
311 }
312 return result;
313 }
314
315 errno_t
316 proto_inject(
317 protocol_family_t protocol,
318 mbuf_t packet_list)
319 {
320 struct proto_input_entry *entry;
321 mbuf_t last_packet;
322 int hash_slot = proto_hash_value(protocol);
323 struct dlil_threading_info *thread = dlil_lo_thread_ptr;
324
325 for (last_packet = packet_list; mbuf_nextpkt(last_packet);
326 last_packet = mbuf_nextpkt(last_packet))
327 /* find the last packet */;
328
329 for (entry = proto_hash[hash_slot]; entry; entry = entry->next) {
330 if (entry->protocol == protocol)
331 break;
332 }
333
334 if (entry) {
335 lck_mtx_lock(thread->input_lck);
336 if (entry->inject_first == NULL) {
337 proto_total_waiting++;
338 thread->input_waiting |= DLIL_PROTO_WAITING;
339 entry->inject_first = packet_list;
340 }
341 else {
342 mbuf_setnextpkt(entry->inject_last, packet_list);
343 }
344 entry->inject_last = last_packet;
345 if ((thread->input_waiting & DLIL_INPUT_RUNNING) == 0) {
346 wakeup((caddr_t)&thread->input_waiting);
347 }
348 lck_mtx_unlock(thread->input_lck);
349 }
350 else
351 {
352 return ENOENT;
353 }
354
355 return 0;
356 }
357
358 static struct proto_family_str*
359 proto_plumber_find(
360 protocol_family_t proto_family,
361 ifnet_family_t if_family)
362 {
363 struct proto_family_str *mod = NULL;
364
365 TAILQ_FOREACH(mod, &proto_family_head, proto_fam_next) {
366 if ((mod->proto_family == (proto_family & 0xffff))
367 && (mod->if_family == (if_family & 0xffff)))
368 break;
369 }
370
371 return mod;
372 }
373
374 errno_t
375 proto_register_plumber(
376 protocol_family_t protocol_family,
377 ifnet_family_t interface_family,
378 proto_plumb_handler attach,
379 proto_unplumb_handler detach)
380 {
381 struct proto_family_str *proto_family;
382
383 if (attach == NULL) return EINVAL;
384
385 lck_mtx_lock(proto_family_mutex);
386
387 TAILQ_FOREACH(proto_family, &proto_family_head, proto_fam_next) {
388 if (proto_family->proto_family == protocol_family &&
389 proto_family->if_family == interface_family) {
390 lck_mtx_unlock(proto_family_mutex);
391 return EEXIST;
392 }
393 }
394
395 proto_family = (struct proto_family_str *) _MALLOC(sizeof(struct proto_family_str), M_IFADDR, M_WAITOK);
396 if (!proto_family) {
397 lck_mtx_unlock(proto_family_mutex);
398 return ENOMEM;
399 }
400
401 bzero(proto_family, sizeof(struct proto_family_str));
402 proto_family->proto_family = protocol_family;
403 proto_family->if_family = interface_family & 0xffff;
404 proto_family->attach_proto = attach;
405 proto_family->detach_proto = detach;
406
407 TAILQ_INSERT_TAIL(&proto_family_head, proto_family, proto_fam_next);
408 lck_mtx_unlock(proto_family_mutex);
409 return 0;
410 }
411
412 void
413 proto_unregister_plumber(
414 protocol_family_t protocol_family,
415 ifnet_family_t interface_family)
416 {
417 struct proto_family_str *proto_family;
418
419 lck_mtx_lock(proto_family_mutex);
420
421 proto_family = proto_plumber_find(protocol_family, interface_family);
422 if (proto_family == 0) {
423 lck_mtx_unlock(proto_family_mutex);
424 return;
425 }
426
427 TAILQ_REMOVE(&proto_family_head, proto_family, proto_fam_next);
428 FREE(proto_family, M_IFADDR);
429
430 lck_mtx_unlock(proto_family_mutex);
431 return;
432 }
433
434 __private_extern__ errno_t
435 proto_plumb(
436 protocol_family_t protocol_family,
437 ifnet_t ifp)
438 {
439 struct proto_family_str *proto_family;
440 int ret = 0;
441
442 lck_mtx_lock(proto_family_mutex);
443 proto_family = proto_plumber_find(protocol_family, ifp->if_family);
444 if (proto_family == 0) {
445 lck_mtx_unlock(proto_family_mutex);
446 return ENXIO;
447 }
448
449 ret = proto_family->attach_proto(ifp, protocol_family);
450
451 lck_mtx_unlock(proto_family_mutex);
452 return ret;
453 }
454
455
456 __private_extern__ errno_t
457 proto_unplumb(
458 protocol_family_t protocol_family,
459 ifnet_t ifp)
460 {
461 struct proto_family_str *proto_family;
462 int ret = 0;
463
464 lck_mtx_lock(proto_family_mutex);
465
466 proto_family = proto_plumber_find(protocol_family, ifp->if_family);
467 if (proto_family && proto_family->detach_proto)
468 proto_family->detach_proto(ifp, protocol_family);
469 else
470 ret = ifnet_detach_protocol(ifp, protocol_family);
471
472 lck_mtx_unlock(proto_family_mutex);
473 return ret;
474 }