2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include "kpi_protocol.h"
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>
39 #include <libkern/OSAtomic.h>
41 void proto_input_run(void);
43 typedef int (*attach_t
)(struct ifnet
*ifp
, u_long protocol_family
);
44 typedef int (*detach_t
)(struct ifnet
*ifp
, u_long protocol_family
);
46 struct proto_input_entry
{
47 struct proto_input_entry
*next
;
49 struct domain
*domain
;
53 protocol_family_t protocol
;
54 proto_input_handler input
;
55 proto_input_detached_handler detached
;
60 struct proto_input_entry
*input_next
;
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
;
74 #define PROTO_HASH_SLOTS 5
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
);
83 extern lck_mtx_t
*domain_proto_mtx
;
84 extern struct dlil_threading_info
*dlil_lo_thread_ptr
;
88 protocol_family_t protocol
)
103 __private_extern__
void
106 lck_grp_attr_t
*grp_attrib
= 0;
107 lck_attr_t
*lck_attrib
= 0;
108 lck_grp_t
*lck_group
= 0;
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
);
119 bzero(proto_hash
, sizeof(proto_hash
));
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
,
130 struct proto_input_entry
*entry
;
131 struct dlil_threading_info
*thread
= dlil_lo_thread_ptr
;
133 entry
= _MALLOC(sizeof(*entry
), M_IFADDR
, M_WAITOK
);
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
;
146 struct domain
*dp
= domains
;
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
)
153 lck_mtx_unlock(domain_proto_mtx
);
157 lck_mtx_lock(thread
->input_lck
);
158 entry
->next
= proto_input_add_list
;
159 proto_input_add_list
= entry
;
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
);
170 __private_extern__
void
171 proto_unregister_input(
172 protocol_family_t protocol
)
174 struct proto_input_entry
*entry
= NULL
;
176 for (entry
= proto_hash
[proto_hash_value(protocol
)]; entry
; entry
= entry
->next
)
177 if (entry
->protocol
== protocol
)
186 proto_delayed_attach(
187 struct proto_input_entry
*entry
)
189 struct proto_input_entry
*next_entry
;
190 for (next_entry
= entry
->next
; entry
; entry
= next_entry
) {
191 struct proto_input_entry
*exist
;
194 hash_slot
= proto_hash_value(entry
->protocol
);
195 next_entry
= entry
->next
;
197 for (exist
= proto_hash
[hash_slot
]; exist
; exist
= exist
->next
)
198 if (exist
->protocol
== entry
->protocol
)
201 /* If the entry already exists, call detached and dispose */
204 entry
->detached(entry
->protocol
);
205 FREE(entry
, M_IFADDR
);
208 entry
->next
= proto_hash
[hash_slot
];
209 proto_hash
[hash_slot
] = entry
;
214 __private_extern__
void
215 proto_input_run(void)
217 struct proto_input_entry
*entry
;
218 struct dlil_threading_info
*thread
= dlil_lo_thread_ptr
;
222 lck_mtx_assert(thread
->input_lck
, LCK_MTX_ASSERT_NOTOWNED
);
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
);
233 Move everything from the lock protected list to the thread
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
;
243 packet_list
= entry
->inject_first
;
245 entry
->inject_first
= NULL
;
246 entry
->inject_last
= NULL
;
247 proto_total_waiting
--;
249 lck_mtx_unlock(thread
->input_lck
);
251 if (entry
->domain
&& (entry
->domain
->dom_flags
& DOM_REENTRANT
) == 0) {
252 lck_mtx_lock(entry
->domain
->dom_mtx
);
257 entry
->input(entry
->protocol
, packet_list
);
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
);
270 lck_mtx_unlock(entry
->domain
->dom_mtx
);
280 protocol_family_t protocol
,
283 struct proto_input_entry
*entry
;
284 errno_t locked
=0, result
= 0;
286 for (entry
= proto_hash
[proto_hash_value(protocol
)]; entry
;
287 entry
= entry
->next
) {
288 if (entry
->protocol
== protocol
)
292 if (entry
->domain
&& (entry
->domain
->dom_flags
& DOM_REENTRANT
) == 0) {
293 lck_mtx_lock(entry
->domain
->dom_mtx
);
298 entry
->input(entry
->protocol
, packet_list
);
303 for (packet
= packet_list
; packet
; packet
= packet_list
) {
304 packet_list
= mbuf_nextpkt(packet
);
305 mbuf_setnextpkt(packet
, NULL
);
306 entry
->input(entry
->protocol
, packet
);
311 lck_mtx_unlock(entry
->domain
->dom_mtx
);
318 protocol_family_t protocol
,
321 struct proto_input_entry
*entry
;
323 int hash_slot
= proto_hash_value(protocol
);
324 struct dlil_threading_info
*thread
= dlil_lo_thread_ptr
;
326 for (last_packet
= packet_list
; mbuf_nextpkt(last_packet
);
327 last_packet
= mbuf_nextpkt(last_packet
))
328 /* find the last packet */;
330 for (entry
= proto_hash
[hash_slot
]; entry
; entry
= entry
->next
) {
331 if (entry
->protocol
== protocol
)
336 lck_mtx_lock(thread
->input_lck
);
337 if (entry
->inject_first
== NULL
) {
338 proto_total_waiting
++;
339 thread
->input_waiting
|= DLIL_PROTO_WAITING
;
340 entry
->inject_first
= packet_list
;
343 mbuf_setnextpkt(entry
->inject_last
, packet_list
);
345 entry
->inject_last
= last_packet
;
346 if ((thread
->input_waiting
& DLIL_INPUT_RUNNING
) == 0) {
347 wakeup((caddr_t
)&thread
->input_waiting
);
349 lck_mtx_unlock(thread
->input_lck
);
359 static struct proto_family_str
*
361 protocol_family_t proto_family
,
362 ifnet_family_t if_family
)
364 struct proto_family_str
*mod
= NULL
;
366 TAILQ_FOREACH(mod
, &proto_family_head
, proto_fam_next
) {
367 if ((mod
->proto_family
== (proto_family
& 0xffff))
368 && (mod
->if_family
== (if_family
& 0xffff)))
376 proto_register_plumber(
377 protocol_family_t protocol_family
,
378 ifnet_family_t interface_family
,
379 proto_plumb_handler attach
,
380 proto_unplumb_handler detach
)
382 struct proto_family_str
*proto_family
;
384 if (attach
== NULL
) return EINVAL
;
386 lck_mtx_lock(proto_family_mutex
);
388 TAILQ_FOREACH(proto_family
, &proto_family_head
, proto_fam_next
) {
389 if (proto_family
->proto_family
== protocol_family
&&
390 proto_family
->if_family
== interface_family
) {
391 lck_mtx_unlock(proto_family_mutex
);
396 proto_family
= (struct proto_family_str
*) _MALLOC(sizeof(struct proto_family_str
), M_IFADDR
, M_WAITOK
);
398 lck_mtx_unlock(proto_family_mutex
);
402 bzero(proto_family
, sizeof(struct proto_family_str
));
403 proto_family
->proto_family
= protocol_family
;
404 proto_family
->if_family
= interface_family
& 0xffff;
405 proto_family
->attach_proto
= attach
;
406 proto_family
->detach_proto
= detach
;
408 TAILQ_INSERT_TAIL(&proto_family_head
, proto_family
, proto_fam_next
);
409 lck_mtx_unlock(proto_family_mutex
);
414 proto_unregister_plumber(
415 protocol_family_t protocol_family
,
416 ifnet_family_t interface_family
)
418 struct proto_family_str
*proto_family
;
420 lck_mtx_lock(proto_family_mutex
);
422 proto_family
= proto_plumber_find(protocol_family
, interface_family
);
423 if (proto_family
== 0) {
424 lck_mtx_unlock(proto_family_mutex
);
428 TAILQ_REMOVE(&proto_family_head
, proto_family
, proto_fam_next
);
429 FREE(proto_family
, M_IFADDR
);
431 lck_mtx_unlock(proto_family_mutex
);
435 __private_extern__ errno_t
437 protocol_family_t protocol_family
,
440 struct proto_family_str
*proto_family
;
443 lck_mtx_lock(proto_family_mutex
);
444 proto_family
= proto_plumber_find(protocol_family
, ifp
->if_family
);
445 if (proto_family
== 0) {
446 lck_mtx_unlock(proto_family_mutex
);
450 ret
= proto_family
->attach_proto(ifp
, protocol_family
);
452 lck_mtx_unlock(proto_family_mutex
);
457 __private_extern__ errno_t
459 protocol_family_t protocol_family
,
462 struct proto_family_str
*proto_family
;
465 lck_mtx_lock(proto_family_mutex
);
467 proto_family
= proto_plumber_find(protocol_family
, ifp
->if_family
);
468 if (proto_family
&& proto_family
->detach_proto
)
469 proto_family
->detach_proto(ifp
, protocol_family
);
471 ret
= ifnet_detach_protocol(ifp
, protocol_family
);
473 lck_mtx_unlock(proto_family_mutex
);