2 * Copyright (c) 2004-2020 Apple 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
, uint32_t protocol_family
);
44 typedef int (*detach_t
)(struct ifnet
*ifp
, uint32_t 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 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
);
82 __private_extern__
void
85 lck_grp_attr_t
*grp_attrib
= NULL
;
86 lck_attr_t
*lck_attrib
= NULL
;
87 lck_grp_t
*lck_group
= NULL
;
89 /* Allocate a mtx lock */
90 grp_attrib
= lck_grp_attr_alloc_init();
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();
94 lck_mtx_init(proto_family_mutex
, lck_group
, lck_attrib
);
95 lck_grp_free(lck_group
);
96 lck_attr_free(lck_attrib
);
98 bzero(proto_hash
, sizeof(proto_hash
));
101 __private_extern__ errno_t
102 proto_register_input(protocol_family_t protocol
, proto_input_handler input
,
103 proto_input_detached_handler detached
, int chains
)
105 struct proto_input_entry
*entry
;
106 struct dlil_threading_info
*inp
= dlil_main_input_thread
;
108 domain_guard_t guard
;
110 entry
= _MALLOC(sizeof(*entry
), M_IFADDR
, M_WAITOK
| M_ZERO
);
115 entry
->protocol
= protocol
;
116 entry
->input
= input
;
117 entry
->detached
= detached
;
118 entry
->hash
= proto_hash_value(protocol
);
119 entry
->chain
= chains
;
121 guard
= domain_guard_deploy();
122 TAILQ_FOREACH(dp
, &domains
, dom_entry
) {
123 if (dp
->dom_family
== (int)protocol
) {
127 domain_guard_release(guard
);
134 lck_mtx_lock(&inp
->dlth_lock
);
135 entry
->next
= proto_input_add_list
;
136 proto_input_add_list
= entry
;
138 inp
->dlth_flags
|= DLIL_PROTO_REGISTER
;
139 if ((inp
->dlth_flags
& DLIL_INPUT_RUNNING
) == 0) {
140 wakeup((caddr_t
)&inp
->dlth_flags
);
142 lck_mtx_unlock(&inp
->dlth_lock
);
147 __private_extern__
void
148 proto_unregister_input(protocol_family_t protocol
)
150 struct proto_input_entry
*entry
= NULL
;
152 for (entry
= proto_hash
[proto_hash_value(protocol
)]; entry
!= NULL
;
153 entry
= entry
->next
) {
154 if (entry
->protocol
== protocol
) {
165 proto_delayed_attach(struct proto_input_entry
*entry
)
167 struct proto_input_entry
*next_entry
;
169 for (next_entry
= entry
->next
; entry
!= NULL
; entry
= next_entry
) {
170 struct proto_input_entry
*exist
;
173 hash_slot
= proto_hash_value(entry
->protocol
);
174 next_entry
= entry
->next
;
176 for (exist
= proto_hash
[hash_slot
]; exist
!= NULL
;
177 exist
= exist
->next
) {
178 if (exist
->protocol
== entry
->protocol
) {
183 /* If the entry already exists, call detached and dispose */
185 if (entry
->detached
) {
186 entry
->detached(entry
->protocol
);
188 FREE(entry
, M_IFADDR
);
190 entry
->next
= proto_hash
[hash_slot
];
191 proto_hash
[hash_slot
] = entry
;
196 __private_extern__
void
197 proto_input_run(void)
199 struct proto_input_entry
*entry
;
200 struct dlil_threading_info
*inp
= dlil_main_input_thread
;
204 LCK_MTX_ASSERT(&inp
->dlth_lock
, LCK_MTX_ASSERT_NOTOWNED
);
206 if (inp
->dlth_flags
& DLIL_PROTO_REGISTER
) {
207 lck_mtx_lock_spin(&inp
->dlth_lock
);
208 entry
= proto_input_add_list
;
209 proto_input_add_list
= NULL
;
210 inp
->dlth_flags
&= ~DLIL_PROTO_REGISTER
;
211 lck_mtx_unlock(&inp
->dlth_lock
);
212 proto_delayed_attach(entry
);
216 * Move everything from the lock protected list to the thread
219 for (i
= 0; proto_total_waiting
!= 0 && i
< PROTO_HASH_SLOTS
; i
++) {
220 for (entry
= proto_hash
[i
];
221 entry
!= NULL
&& proto_total_waiting
; entry
= entry
->next
) {
222 if (entry
->inject_first
!= NULL
) {
223 lck_mtx_lock_spin(&inp
->dlth_lock
);
224 inp
->dlth_flags
&= ~DLIL_PROTO_WAITING
;
226 packet_list
= entry
->inject_first
;
228 entry
->inject_first
= NULL
;
229 entry
->inject_last
= NULL
;
230 proto_total_waiting
--;
232 lck_mtx_unlock(&inp
->dlth_lock
);
234 if (entry
->domain
!= NULL
&& !(entry
->domain
->
235 dom_flags
& DOM_REENTRANT
)) {
236 lck_mtx_lock(entry
->domain
->dom_mtx
);
241 entry
->input(entry
->protocol
,
246 for (packet
= packet_list
;
248 packet
= packet_list
) {
250 mbuf_nextpkt(packet
);
251 mbuf_setnextpkt(packet
, NULL
);
252 entry
->input(entry
->protocol
,
258 lck_mtx_unlock(entry
->domain
->dom_mtx
);
266 proto_input(protocol_family_t protocol
, mbuf_t packet_list
)
268 struct proto_input_entry
*entry
;
269 errno_t locked
= 0, result
= 0;
271 for (entry
= proto_hash
[proto_hash_value(protocol
)]; entry
!= NULL
;
272 entry
= entry
->next
) {
273 if (entry
->protocol
== protocol
) {
282 if (entry
->domain
&& !(entry
->domain
->dom_flags
& DOM_REENTRANT
)) {
283 lck_mtx_lock(entry
->domain
->dom_mtx
);
288 entry
->input(entry
->protocol
, packet_list
);
292 for (packet
= packet_list
; packet
!= NULL
;
293 packet
= packet_list
) {
294 packet_list
= mbuf_nextpkt(packet
);
295 mbuf_setnextpkt(packet
, NULL
);
296 entry
->input(entry
->protocol
, packet
);
301 lck_mtx_unlock(entry
->domain
->dom_mtx
);
307 proto_inject(protocol_family_t protocol
, mbuf_t packet_list
)
309 struct proto_input_entry
*entry
;
311 int hash_slot
= proto_hash_value(protocol
);
312 struct dlil_threading_info
*inp
= dlil_main_input_thread
;
314 for (last_packet
= packet_list
; mbuf_nextpkt(last_packet
) != NULL
;
315 last_packet
= mbuf_nextpkt(last_packet
)) {
316 /* find the last packet */;
319 for (entry
= proto_hash
[hash_slot
]; entry
!= NULL
;
320 entry
= entry
->next
) {
321 if (entry
->protocol
== protocol
) {
327 lck_mtx_lock(&inp
->dlth_lock
);
328 if (entry
->inject_first
== NULL
) {
329 proto_total_waiting
++;
330 inp
->dlth_flags
|= DLIL_PROTO_WAITING
;
331 entry
->inject_first
= packet_list
;
333 mbuf_setnextpkt(entry
->inject_last
, packet_list
);
335 entry
->inject_last
= last_packet
;
336 if ((inp
->dlth_flags
& DLIL_INPUT_RUNNING
) == 0) {
337 wakeup((caddr_t
)&inp
->dlth_flags
);
339 lck_mtx_unlock(&inp
->dlth_lock
);
347 static struct proto_family_str
*
348 proto_plumber_find(protocol_family_t proto_family
, ifnet_family_t if_family
)
350 struct proto_family_str
*mod
= NULL
;
352 TAILQ_FOREACH(mod
, &proto_family_head
, proto_fam_next
) {
353 if ((mod
->proto_family
== (proto_family
& 0xffff)) &&
354 (mod
->if_family
== (if_family
& 0xffff))) {
363 proto_register_plumber(protocol_family_t protocol_family
,
364 ifnet_family_t interface_family
, proto_plumb_handler attach
,
365 proto_unplumb_handler detach
)
367 struct proto_family_str
*proto_family
;
369 if (attach
== NULL
) {
373 lck_mtx_lock(proto_family_mutex
);
375 TAILQ_FOREACH(proto_family
, &proto_family_head
, proto_fam_next
) {
376 if (proto_family
->proto_family
== protocol_family
&&
377 proto_family
->if_family
== interface_family
) {
378 lck_mtx_unlock(proto_family_mutex
);
383 proto_family
= (struct proto_family_str
*)
384 _MALLOC(sizeof(struct proto_family_str
), M_IFADDR
,
387 lck_mtx_unlock(proto_family_mutex
);
391 proto_family
->proto_family
= protocol_family
;
392 proto_family
->if_family
= interface_family
& 0xffff;
393 proto_family
->attach_proto
= attach
;
394 proto_family
->detach_proto
= detach
;
396 TAILQ_INSERT_TAIL(&proto_family_head
, proto_family
, proto_fam_next
);
397 lck_mtx_unlock(proto_family_mutex
);
402 proto_unregister_plumber(protocol_family_t protocol_family
,
403 ifnet_family_t interface_family
)
405 struct proto_family_str
*proto_family
;
407 lck_mtx_lock(proto_family_mutex
);
409 proto_family
= proto_plumber_find(protocol_family
, interface_family
);
410 if (proto_family
== NULL
) {
411 lck_mtx_unlock(proto_family_mutex
);
415 TAILQ_REMOVE(&proto_family_head
, proto_family
, proto_fam_next
);
416 FREE(proto_family
, M_IFADDR
);
418 lck_mtx_unlock(proto_family_mutex
);
421 __private_extern__ errno_t
422 proto_plumb(protocol_family_t protocol_family
, ifnet_t ifp
)
424 struct proto_family_str
*proto_family
;
427 lck_mtx_lock(proto_family_mutex
);
428 proto_family
= proto_plumber_find(protocol_family
, ifp
->if_family
);
429 if (proto_family
== NULL
) {
430 lck_mtx_unlock(proto_family_mutex
);
434 ret
= proto_family
->attach_proto(ifp
, protocol_family
);
436 lck_mtx_unlock(proto_family_mutex
);
441 __private_extern__ errno_t
442 proto_unplumb(protocol_family_t protocol_family
, ifnet_t ifp
)
444 struct proto_family_str
*proto_family
;
447 lck_mtx_lock(proto_family_mutex
);
449 proto_family
= proto_plumber_find(protocol_family
, ifp
->if_family
);
450 if (proto_family
!= NULL
&& proto_family
->detach_proto
) {
451 proto_family
->detach_proto(ifp
, protocol_family
);
453 ret
= ifnet_detach_protocol(ifp
, protocol_family
);
456 lck_mtx_unlock(proto_family_mutex
);