Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 * Copyright (c) 2004-2021 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

#include "kpi_protocol.h"

#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/systm.h>
#include <sys/kpi_mbuf.h>
#include <sys/domain.h>
#include <net/if.h>
#include <net/dlil.h>
#include <libkern/OSAtomic.h>

void proto_input_run(void);

typedef int (*attach_t)(struct ifnet *ifp, uint32_t protocol_family);
typedef int (*detach_t)(struct ifnet *ifp, uint32_t protocol_family);

struct proto_input_entry {
	struct proto_input_entry        *next;
	int                             detach;
	struct domain                   *domain;
	int                             hash;
	int                             chain;

	protocol_family_t               protocol;
	proto_input_handler             input;
	proto_input_detached_handler    detached;

	mbuf_t                          inject_first;
	mbuf_t                          inject_last;

	struct proto_input_entry        *input_next;
	mbuf_t                          input_first;
	mbuf_t                          input_last;
};


struct proto_family_str {
	TAILQ_ENTRY(proto_family_str)   proto_fam_next;
	protocol_family_t               proto_family;
	ifnet_family_t                  if_family;
	proto_plumb_handler             attach_proto;
	proto_unplumb_handler           detach_proto;
};

static LCK_GRP_DECLARE(proto_family_grp, "protocol kpi");
static LCK_MTX_DECLARE(proto_family_mutex, &proto_family_grp);

static struct proto_input_entry *proto_hash[PROTO_HASH_SLOTS];
static int proto_total_waiting = 0;
static struct proto_input_entry *proto_input_add_list = NULL;
static TAILQ_HEAD(, proto_family_str) proto_family_head =
    TAILQ_HEAD_INITIALIZER(proto_family_head);

__private_extern__ errno_t
proto_register_input(protocol_family_t protocol, proto_input_handler input,
    proto_input_detached_handler detached, int  chains)
{
	struct proto_input_entry *entry;
	struct dlil_threading_info *inp = dlil_main_input_thread;
	struct domain *dp;
	domain_guard_t __single guard;

	entry = kalloc_type(struct proto_input_entry, Z_WAITOK | Z_ZERO);
	if (entry == NULL) {
		return ENOMEM;
	}

	entry->protocol = protocol;
	entry->input = input;
	entry->detached = detached;
	entry->hash = proto_hash_value(protocol);
	entry->chain = chains;

	guard = domain_guard_deploy();
	TAILQ_FOREACH(dp, &domains, dom_entry) {
		if (dp->dom_family == (int)protocol) {
			break;
		}
	}
	domain_guard_release(guard);
	if (dp == NULL) {
		return EINVAL;
	}

	entry->domain = dp;

	lck_mtx_lock(&inp->dlth_lock);
	entry->next = proto_input_add_list;
	proto_input_add_list = entry;

	inp->dlth_flags |= DLIL_PROTO_REGISTER;
	if ((inp->dlth_flags & DLIL_INPUT_RUNNING) == 0) {
		wakeup((caddr_t)&inp->dlth_flags);
	}
	lck_mtx_unlock(&inp->dlth_lock);

	return 0;
}

__private_extern__ void
proto_unregister_input(protocol_family_t protocol)
{
	struct proto_input_entry *entry = NULL;

	for (entry = proto_hash[proto_hash_value(protocol)]; entry != NULL;
	    entry = entry->next) {
		if (entry->protocol == protocol) {
			break;
		}
	}

	if (entry != NULL) {
		entry->detach = 1;
	}
}

static void
proto_delayed_attach(struct proto_input_entry *entry)
{
	struct proto_input_entry *next_entry;

	for (next_entry = entry->next; entry != NULL; entry = next_entry) {
		struct proto_input_entry *exist;
		int hash_slot;

		hash_slot = proto_hash_value(entry->protocol);
		next_entry = entry->next;

		for (exist = proto_hash[hash_slot]; exist != NULL;
		    exist = exist->next) {
			if (exist->protocol == entry->protocol) {
				break;
			}
		}

		/* If the entry already exists, call detached and dispose */
		if (exist != NULL) {
			if (entry->detached) {
				entry->detached(entry->protocol);
			}
			kfree_type(struct proto_input_entry, entry);
		} else {
			entry->next = proto_hash[hash_slot];
			proto_hash[hash_slot] = entry;
		}
	}
}

__private_extern__ void
proto_input_run(void)
{
	struct proto_input_entry *entry;
	struct dlil_threading_info *inp = dlil_main_input_thread;
	mbuf_t packet_list;
	int i, locked = 0;

	LCK_MTX_ASSERT(&inp->dlth_lock, LCK_MTX_ASSERT_NOTOWNED);

	if (inp->dlth_flags & DLIL_PROTO_REGISTER) {
		lck_mtx_lock_spin(&inp->dlth_lock);
		entry = proto_input_add_list;
		proto_input_add_list = NULL;
		inp->dlth_flags &= ~DLIL_PROTO_REGISTER;
		lck_mtx_unlock(&inp->dlth_lock);
		proto_delayed_attach(entry);
	}

	/*
	 * Move everything from the lock protected list to the thread
	 * specific list.
	 */
	for (i = 0; proto_total_waiting != 0 && i < PROTO_HASH_SLOTS; i++) {
		for (entry = proto_hash[i];
		    entry != NULL && proto_total_waiting; entry = entry->next) {
			if (entry->inject_first != NULL) {
				lck_mtx_lock_spin(&inp->dlth_lock);
				inp->dlth_flags &= ~DLIL_PROTO_WAITING;

				packet_list = entry->inject_first;

				entry->inject_first = NULL;
				entry->inject_last = NULL;
				proto_total_waiting--;

				lck_mtx_unlock(&inp->dlth_lock);

				if (entry->domain != NULL && !(entry->domain->
				    dom_flags & DOM_REENTRANT)) {
					lck_mtx_lock(entry->domain->dom_mtx);
					locked = 1;
				}

				if (entry->chain) {
					entry->input(entry->protocol,
					    packet_list);
				} else {
					mbuf_t  packet;

					for (packet = packet_list;
					    packet != NULL;
					    packet = packet_list) {
						packet_list =
						    mbuf_nextpkt(packet);
						mbuf_setnextpkt(packet, NULL);
						entry->input(entry->protocol,
						    packet);
					}
				}
				if (locked) {
					locked = 0;
					lck_mtx_unlock(entry->domain->dom_mtx);
				}
			}
		}
	}
}

errno_t
proto_input(protocol_family_t protocol, mbuf_t packet_list)
{
	struct proto_input_entry *entry;
	errno_t locked = 0, result = 0;

	for (entry = proto_hash[proto_hash_value(protocol)]; entry != NULL;
	    entry = entry->next) {
		if (entry->protocol == protocol) {
			break;
		}
	}

	if (entry == NULL) {
		return -1;
	}

	if (entry->domain && !(entry->domain->dom_flags & DOM_REENTRANT)) {
		lck_mtx_lock(entry->domain->dom_mtx);
		locked = 1;
	}

	if (entry->chain) {
		entry->input(entry->protocol, packet_list);
	} else {
		mbuf_t  packet;

		for (packet = packet_list; packet != NULL;
		    packet = packet_list) {
			packet_list = mbuf_nextpkt(packet);
			mbuf_setnextpkt(packet, NULL);
			entry->input(entry->protocol, packet);
		}
	}

	if (locked) {
		lck_mtx_unlock(entry->domain->dom_mtx);
	}
	return result;
}

errno_t
proto_inject(protocol_family_t protocol, mbuf_t packet_list)
{
	struct proto_input_entry *entry;
	mbuf_t last_packet;
	int hash_slot = proto_hash_value(protocol);
	struct dlil_threading_info *inp = dlil_main_input_thread;

	for (last_packet = packet_list; mbuf_nextpkt(last_packet) != NULL;
	    last_packet = mbuf_nextpkt(last_packet)) {
		/* find the last packet */;
	}

	for (entry = proto_hash[hash_slot]; entry != NULL;
	    entry = entry->next) {
		if (entry->protocol == protocol) {
			break;
		}
	}

	if (entry != NULL) {
		lck_mtx_lock(&inp->dlth_lock);
		if (entry->inject_first == NULL) {
			proto_total_waiting++;
			inp->dlth_flags |= DLIL_PROTO_WAITING;
			entry->inject_first = packet_list;
		} else {
			mbuf_setnextpkt(entry->inject_last, packet_list);
		}
		entry->inject_last = last_packet;
		if ((inp->dlth_flags & DLIL_INPUT_RUNNING) == 0) {
			wakeup((caddr_t)&inp->dlth_flags);
		}
		lck_mtx_unlock(&inp->dlth_lock);
	} else {
		return ENOENT;
	}

	return 0;
}

static struct proto_family_str *
proto_plumber_find(protocol_family_t proto_family, ifnet_family_t if_family)
{
	struct proto_family_str  *mod = NULL;

	TAILQ_FOREACH(mod, &proto_family_head, proto_fam_next) {
		if ((mod->proto_family == (proto_family & 0xffff)) &&
		    (mod->if_family == (if_family & 0xffff))) {
			break;
		}
	}

	return mod;
}

errno_t
proto_register_plumber(protocol_family_t protocol_family,
    ifnet_family_t interface_family, proto_plumb_handler attach,
    proto_unplumb_handler detach)
{
	struct proto_family_str *proto_family;

	if (attach == NULL) {
		return EINVAL;
	}

	lck_mtx_lock(&proto_family_mutex);

	TAILQ_FOREACH(proto_family, &proto_family_head, proto_fam_next) {
		if (proto_family->proto_family == protocol_family &&
		    proto_family->if_family == interface_family) {
			lck_mtx_unlock(&proto_family_mutex);
			return EEXIST;
		}
	}

	proto_family = kalloc_type(struct proto_family_str,
	    Z_WAITOK | Z_ZERO | Z_NOFAIL);

	proto_family->proto_family      = protocol_family;
	proto_family->if_family         = interface_family & 0xffff;
	proto_family->attach_proto      = attach;
	proto_family->detach_proto      = detach;

	TAILQ_INSERT_TAIL(&proto_family_head, proto_family, proto_fam_next);
	lck_mtx_unlock(&proto_family_mutex);
	return 0;
}

void
proto_unregister_plumber(protocol_family_t protocol_family,
    ifnet_family_t interface_family)
{
	struct proto_family_str  *proto_family;

	lck_mtx_lock(&proto_family_mutex);

	proto_family = proto_plumber_find(protocol_family, interface_family);
	if (proto_family == NULL) {
		lck_mtx_unlock(&proto_family_mutex);
		return;
	}

	TAILQ_REMOVE(&proto_family_head, proto_family, proto_fam_next);
	kfree_type(struct proto_family_str, proto_family);

	lck_mtx_unlock(&proto_family_mutex);
}

__private_extern__ errno_t
proto_plumb(protocol_family_t protocol_family, ifnet_t ifp)
{
	struct proto_family_str  *proto_family;
	int ret = 0;

	lck_mtx_lock(&proto_family_mutex);
	proto_family = proto_plumber_find(protocol_family, ifp->if_family);
	if (proto_family == NULL) {
		lck_mtx_unlock(&proto_family_mutex);
		return ENXIO;
	}

	ret = proto_family->attach_proto(ifp, protocol_family);

	lck_mtx_unlock(&proto_family_mutex);
	return ret;
}


__private_extern__ errno_t
proto_unplumb(protocol_family_t protocol_family, ifnet_t ifp)
{
	struct proto_family_str  *proto_family;
	int ret = 0;

	lck_mtx_lock(&proto_family_mutex);

	proto_family = proto_plumber_find(protocol_family, ifp->if_family);
	if (proto_family != NULL && proto_family->detach_proto) {
		proto_family->detach_proto(ifp, protocol_family);
	} else {
		ret = ifnet_detach_protocol(ifp, protocol_family);
	}

	lck_mtx_unlock(&proto_family_mutex);
	return ret;
}