]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/dlil.c
xnu-1228.0.2.tar.gz
[apple/xnu.git] / bsd / net / dlil.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 1999-2007 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 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.
8f6c56a5 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.
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
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
1c79356b
A
29 * Data Link Inteface Layer
30 * Author: Ted Walker
31 */
2d21ac55
A
32/*
33 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
34 * support for mandatory and extensible security protections. This notice
35 * is included in support of clause 2.2 (b) of the Apple Public License,
36 * Version 2.0.
37 */
1c79356b 38
1c79356b
A
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
91447636
A
45#include <sys/domain.h>
46#include <sys/user.h>
2d21ac55 47#include <sys/random.h>
1c79356b
A
48#include <net/if_dl.h>
49#include <net/if.h>
91447636 50#include <net/route.h>
1c79356b
A
51#include <net/if_var.h>
52#include <net/dlil.h>
91447636 53#include <net/if_arp.h>
1c79356b
A
54#include <sys/kern_event.h>
55#include <sys/kdebug.h>
1c79356b 56
91447636 57#include <kern/assert.h>
1c79356b 58#include <kern/task.h>
9bccf70c
A
59#include <kern/thread.h>
60#include <kern/sched_prim.h>
91447636 61#include <kern/locks.h>
2d21ac55 62#include <net/kpi_protocol.h>
9bccf70c 63
1c79356b 64#include <net/if_types.h>
91447636
A
65#include <net/kpi_interfacefilter.h>
66
67#include <libkern/OSAtomic.h>
1c79356b 68
d52fe63f 69#include <machine/machine_routines.h>
1c79356b 70
2d21ac55
A
71#include <mach/thread_act.h>
72
73#if CONFIG_MACF_NET
74#include <security/mac_framework.h>
75#endif /* MAC_NET */
76
77#define DBG_LAYER_BEG DLILDBG_CODE(DBG_DLIL_STATIC, 0)
78#define DBG_LAYER_END DLILDBG_CODE(DBG_DLIL_STATIC, 2)
1c79356b
A
79#define DBG_FNC_DLIL_INPUT DLILDBG_CODE(DBG_DLIL_STATIC, (1 << 8))
80#define DBG_FNC_DLIL_OUTPUT DLILDBG_CODE(DBG_DLIL_STATIC, (2 << 8))
81#define DBG_FNC_DLIL_IFOUT DLILDBG_CODE(DBG_DLIL_STATIC, (3 << 8))
82
83
1c79356b
A
84#define MAX_FRAME_TYPE_SIZE 4 /* LONGWORDS */
85#define MAX_LINKADDR 4 /* LONGWORDS */
86#define M_NKE M_IFADDR
87
2d21ac55 88#if 1
91447636
A
89#define DLIL_PRINTF printf
90#else
91#define DLIL_PRINTF kprintf
92#endif
93
2d21ac55 94
91447636 95enum {
2d21ac55
A
96 kProtoKPI_v1 = 1,
97 kProtoKPI_v2 = 2
91447636
A
98};
99
100struct if_proto {
101 SLIST_ENTRY(if_proto) next_hash;
102 int refcount;
103 int detaching;
104 struct ifnet *ifp;
105 struct domain *dl_domain;
106 protocol_family_t protocol_family;
107 int proto_kpi;
108 union {
91447636
A
109 struct {
110 proto_media_input input;
111 proto_media_preout pre_output;
112 proto_media_event event;
113 proto_media_ioctl ioctl;
114 proto_media_detached detached;
115 proto_media_resolve_multi resolve_multi;
116 proto_media_send_arp send_arp;
117 } v1;
2d21ac55
A
118 struct {
119 proto_media_input_v2 input;
120 proto_media_preout pre_output;
121 proto_media_event event;
122 proto_media_ioctl ioctl;
123 proto_media_detached detached;
124 proto_media_resolve_multi resolve_multi;
125 proto_media_send_arp send_arp;
126 } v2;
91447636 127 } kpi;
1c79356b
A
128};
129
91447636
A
130SLIST_HEAD(proto_hash_entry, if_proto);
131
1c79356b 132
9bccf70c
A
133struct dlil_ifnet {
134 /* ifnet and drvr_ext are used by the stack and drivers
135 drvr_ext extends the public ifnet and must follow dl_if */
136 struct ifnet dl_if; /* public ifnet */
9bccf70c
A
137
138 /* dlil private fields */
139 TAILQ_ENTRY(dlil_ifnet) dl_if_link; /* dlil_ifnet are link together */
140 /* it is not the ifnet list */
141 void *if_uniqueid; /* unique id identifying the interface */
142 size_t if_uniqueid_len;/* length of the unique id */
91447636 143 char if_namestorage[IFNAMSIZ]; /* interface name storage */
1c79356b
A
144};
145
91447636
A
146struct ifnet_filter {
147 TAILQ_ENTRY(ifnet_filter) filt_next;
148 ifnet_t filt_ifp;
149 int filt_detaching;
150
151 const char *filt_name;
152 void *filt_cookie;
153 protocol_family_t filt_protocol;
154 iff_input_func filt_input;
155 iff_output_func filt_output;
156 iff_event_func filt_event;
157 iff_ioctl_func filt_ioctl;
158 iff_detached_func filt_detached;
1c79356b
A
159};
160
2d21ac55 161struct proto_input_entry;
55e303ae 162
91447636 163static TAILQ_HEAD(, dlil_ifnet) dlil_ifnet_head;
91447636
A
164static lck_grp_t *dlil_lock_group;
165static lck_grp_t *ifnet_lock_group;
166static lck_grp_t *ifnet_head_lock_group;
167static lck_attr_t *ifnet_lock_attr;
91447636
A
168static lck_rw_t *ifnet_head_mutex;
169static lck_mtx_t *dlil_ifnet_mutex;
170static lck_mtx_t *dlil_mutex;
171static unsigned long dlil_read_count = 0;
172static unsigned long dlil_detach_waiting = 0;
173extern u_int32_t ipv4_ll_arp_aware;
1c79356b 174
2d21ac55
A
175static struct dlil_threading_info dlil_lo_thread;
176__private_extern__ struct dlil_threading_info *dlil_lo_thread_ptr = &dlil_lo_thread;
177
178static struct mbuf *dlil_lo_input_mbuf_head = NULL;
179static struct mbuf *dlil_lo_input_mbuf_tail = NULL;
180
181#if IFNET_INPUT_SANITY_CHK
182static int dlil_lo_input_mbuf_count = 0;
183int dlil_input_sanity_check = 0; /* sanity checking of input packet lists received */
9bccf70c 184#endif
2d21ac55
A
185int dlil_multithreaded_input = 1;
186static int cur_dlil_input_threads = 0;
1c79356b 187
91447636 188static int dlil_event_internal(struct ifnet *ifp, struct kev_msg *msg);
91447636
A
189static int dlil_detach_filter_internal(interface_filter_t filter, int detached);
190static void dlil_call_delayed_detach_thread(void);
191
192static void dlil_read_begin(void);
2d21ac55 193static __inline__ void dlil_read_end(void);
91447636
A
194static int dlil_write_begin(void);
195static void dlil_write_end(void);
196
2d21ac55
A
197unsigned int net_affinity = 1;
198static kern_return_t dlil_affinity_set(struct thread *, u_int32_t);
91447636 199
9bccf70c 200extern void bpfdetach(struct ifnet*);
91447636
A
201extern void proto_input_run(void); // new run_netisr
202
2d21ac55
A
203void dlil_input_packet_list(struct ifnet *ifp, struct mbuf *m);
204static void dlil_input_thread_func(struct dlil_threading_info *inpthread);
205__private_extern__ int dlil_create_input_thread(
206 ifnet_t, struct dlil_threading_info *);
207__private_extern__ void dlil_terminate_input_thread(
208 struct dlil_threading_info *);
91447636
A
209
210__private_extern__ void link_rtrequest(int, struct rtentry *, struct sockaddr *);
1c79356b 211
55e303ae 212int dlil_expand_mcl;
1c79356b 213
b36670ce
A
214extern u_int32_t inject_buckets;
215
91447636 216static const u_int32_t dlil_writer_waiting = 0x80000000;
2d21ac55
A
217static lck_grp_attr_t *dlil_grp_attributes = NULL;
218static lck_attr_t *dlil_lck_attributes = NULL;
219static lck_grp_t *dlil_input_lock_grp = NULL;
91447636 220
2d21ac55 221static inline void*
91447636
A
222_cast_non_const(const void * ptr) {
223 union {
224 const void* cval;
225 void* val;
226 } ret;
227
228 ret.cval = ptr;
229 return (ret.val);
230}
231
232/* Should these be inline? */
233static void
234dlil_read_begin(void)
235{
236 unsigned long new_value;
237 unsigned long old_value;
238 struct uthread *uth = get_bsdthread_info(current_thread());
239
240 if (uth->dlil_incremented_read == dlil_writer_waiting)
241 panic("dlil_read_begin - thread is already a writer");
242
243 do {
244again:
245 old_value = dlil_read_count;
246
247 if ((old_value & dlil_writer_waiting) != 0 && uth->dlil_incremented_read == 0)
248 {
249 tsleep(&dlil_read_count, PRIBIO, "dlil_read_count", 1);
250 goto again;
251 }
252
253 new_value = old_value + 1;
254 } while (!OSCompareAndSwap((UInt32)old_value, (UInt32)new_value, (UInt32*)&dlil_read_count));
255
256 uth->dlil_incremented_read++;
257}
258
259static void
260dlil_read_end(void)
261{
262 struct uthread *uth = get_bsdthread_info(current_thread());
263
2d21ac55 264 OSDecrementAtomic((SInt32*)&dlil_read_count);
91447636
A
265 uth->dlil_incremented_read--;
266 if (dlil_read_count == dlil_writer_waiting)
267 wakeup(_cast_non_const(&dlil_writer_waiting));
268}
269
270static int
271dlil_write_begin(void)
272{
273 struct uthread *uth = get_bsdthread_info(current_thread());
274
275 if (uth->dlil_incremented_read != 0) {
276 return EDEADLK;
277 }
278 lck_mtx_lock(dlil_mutex);
279 OSBitOrAtomic((UInt32)dlil_writer_waiting, (UInt32*)&dlil_read_count);
280again:
281 if (dlil_read_count == dlil_writer_waiting) {
282 uth->dlil_incremented_read = dlil_writer_waiting;
283 return 0;
284 }
285 else {
286 tsleep(_cast_non_const(&dlil_writer_waiting), PRIBIO, "dlil_writer_waiting", 1);
287 goto again;
288 }
289}
290
291static void
292dlil_write_end(void)
293{
294 struct uthread *uth = get_bsdthread_info(current_thread());
295
296 if (uth->dlil_incremented_read != dlil_writer_waiting)
297 panic("dlil_write_end - thread is not a writer");
298 OSBitAndAtomic((UInt32)~dlil_writer_waiting, (UInt32*)&dlil_read_count);
299 lck_mtx_unlock(dlil_mutex);
300 uth->dlil_incremented_read = 0;
301 wakeup(&dlil_read_count);
302}
303
304#define PROTO_HASH_SLOTS 0x5
305
1c79356b
A
306/*
307 * Internal functions.
308 */
309
91447636
A
310static int
311proto_hash_value(u_long protocol_family)
312{
313 switch(protocol_family) {
314 case PF_INET:
315 return 0;
316 case PF_INET6:
317 return 1;
318 case PF_APPLETALK:
319 return 2;
320 case PF_VLAN:
321 return 3;
322 default:
323 return 4;
324 }
325}
326
91447636
A
327static struct if_proto*
328find_attached_proto(struct ifnet *ifp, u_long protocol_family)
1c79356b 329{
91447636
A
330 struct if_proto *proto = NULL;
331 u_long i = proto_hash_value(protocol_family);
332 if (ifp->if_proto_hash) {
333 proto = SLIST_FIRST(&ifp->if_proto_hash[i]);
334 }
335
336 while(proto && proto->protocol_family != protocol_family) {
337 proto = SLIST_NEXT(proto, next_hash);
338 }
339
340 return proto;
1c79356b
A
341}
342
91447636
A
343static void
344if_proto_ref(struct if_proto *proto)
1c79356b 345{
2d21ac55 346 OSAddAtomic(1, (SInt32*)&proto->refcount);
1c79356b
A
347}
348
91447636
A
349static void
350if_proto_free(struct if_proto *proto)
0b4e3aa0 351{
2d21ac55 352 int oldval = OSAddAtomic(-1, (SInt32*)&proto->refcount);
91447636
A
353
354 if (oldval == 1) { /* This was the last reference */
355 FREE(proto, M_IFADDR);
356 }
0b4e3aa0
A
357}
358
91447636
A
359__private_extern__ void
360ifnet_lock_assert(
361 __unused struct ifnet *ifp,
362 __unused int what)
1c79356b 363{
91447636
A
364#if IFNET_RW_LOCK
365 /*
366 * Not implemented for rw locks.
367 *
368 * Function exists so when/if we use mutex we can
369 * enable this check.
370 */
371#else
372 lck_mtx_assert(ifp->if_lock, what);
373#endif
1c79356b
A
374}
375
91447636
A
376__private_extern__ void
377ifnet_lock_shared(
378 struct ifnet *ifp)
1c79356b 379{
91447636
A
380#if IFNET_RW_LOCK
381 lck_rw_lock_shared(ifp->if_lock);
382#else
383 lck_mtx_assert(ifp->if_lock, LCK_MTX_ASSERT_NOTOWNED);
384 lck_mtx_lock(ifp->if_lock);
385#endif
1c79356b
A
386}
387
91447636
A
388__private_extern__ void
389ifnet_lock_exclusive(
390 struct ifnet *ifp)
0b4e3aa0 391{
91447636
A
392#if IFNET_RW_LOCK
393 lck_rw_lock_exclusive(ifp->if_lock);
394#else
395 lck_mtx_assert(ifp->if_lock, LCK_MTX_ASSERT_NOTOWNED);
396 lck_mtx_lock(ifp->if_lock);
397#endif
0b4e3aa0
A
398}
399
91447636
A
400__private_extern__ void
401ifnet_lock_done(
402 struct ifnet *ifp)
1c79356b 403{
91447636
A
404#if IFNET_RW_LOCK
405 lck_rw_done(ifp->if_lock);
406#else
407 lck_mtx_assert(ifp->if_lock, LCK_MTX_ASSERT_OWNED);
408 lck_mtx_unlock(ifp->if_lock);
409#endif
1c79356b
A
410}
411
91447636 412__private_extern__ void
2d21ac55 413ifnet_head_lock_shared(void)
1c79356b 414{
91447636 415 lck_rw_lock_shared(ifnet_head_mutex);
1c79356b
A
416}
417
91447636 418__private_extern__ void
2d21ac55 419ifnet_head_lock_exclusive(void)
91447636
A
420{
421 lck_rw_lock_exclusive(ifnet_head_mutex);
422}
1c79356b 423
91447636 424__private_extern__ void
2d21ac55 425ifnet_head_done(void)
1c79356b 426{
91447636
A
427 lck_rw_done(ifnet_head_mutex);
428}
1c79356b 429
91447636
A
430static int dlil_ifp_proto_count(struct ifnet * ifp)
431{
432 int count = 0;
433 int i;
434
435 if (ifp->if_proto_hash != NULL) {
436 for (i = 0; i < PROTO_HASH_SLOTS; i++) {
437 struct if_proto *proto;
438 SLIST_FOREACH(proto, &ifp->if_proto_hash[i], next_hash) {
439 count++;
440 }
441 }
442 }
443
444 return count;
445}
1c79356b 446
91447636
A
447__private_extern__ void
448dlil_post_msg(struct ifnet *ifp, u_long event_subclass, u_long event_code,
449 struct net_event_data *event_data, u_long event_data_len)
450{
451 struct net_event_data ev_data;
452 struct kev_msg ev_msg;
453
454 /*
2d21ac55 455 * a net event always starts with a net_event_data structure
91447636
A
456 * but the caller can generate a simple net event or
457 * provide a longer event structure to post
458 */
459
460 ev_msg.vendor_code = KEV_VENDOR_APPLE;
461 ev_msg.kev_class = KEV_NETWORK_CLASS;
462 ev_msg.kev_subclass = event_subclass;
463 ev_msg.event_code = event_code;
464
465 if (event_data == 0) {
466 event_data = &ev_data;
467 event_data_len = sizeof(struct net_event_data);
468 }
469
470 strncpy(&event_data->if_name[0], ifp->if_name, IFNAMSIZ);
471 event_data->if_family = ifp->if_family;
472 event_data->if_unit = (unsigned long) ifp->if_unit;
473
474 ev_msg.dv[0].data_length = event_data_len;
475 ev_msg.dv[0].data_ptr = event_data;
476 ev_msg.dv[1].data_length = 0;
477
478 dlil_event_internal(ifp, &ev_msg);
1c79356b
A
479}
480
2d21ac55
A
481__private_extern__ int
482dlil_create_input_thread(
483 ifnet_t ifp, struct dlil_threading_info *inputthread)
484{
485 int error;
486
487 bzero(inputthread, sizeof(*inputthread));
488 // loopback ifp may not be configured at dlil_init time.
489 if (ifp == lo_ifp)
490 strlcat(inputthread->input_name, "dlil_input_main_thread_mtx", 32);
491 else
492 snprintf(inputthread->input_name, 32, "dlil_input_%s%d_mtx", ifp->if_name, ifp->if_unit);
493
494 inputthread->lck_grp = lck_grp_alloc_init(inputthread->input_name, dlil_grp_attributes);
495 inputthread->input_lck = lck_mtx_alloc_init(inputthread->lck_grp, dlil_lck_attributes);
496
497 error= kernel_thread_start((thread_continue_t)dlil_input_thread_func, inputthread, &inputthread->input_thread);
498 if (error == 0) {
499 ml_thread_policy(inputthread->input_thread, MACHINE_GROUP,
500 (MACHINE_NETWORK_GROUP|MACHINE_NETWORK_NETISR));
501 /*
502 * Except for the loopback dlil input thread, we create
503 * an affinity set so that the matching workloop thread
504 * can be scheduled on the same processor set.
505 */
506 if (net_affinity && inputthread != dlil_lo_thread_ptr) {
507 struct thread *tp = inputthread->input_thread;
508 u_int32_t tag;
509 /*
510 * Randomize to reduce the probability
511 * of affinity tag namespace collision.
512 */
513 read_random(&tag, sizeof (tag));
514 if (dlil_affinity_set(tp, tag) == KERN_SUCCESS) {
515 thread_reference(tp);
516 inputthread->tag = tag;
517 inputthread->net_affinity = TRUE;
518 }
519 }
520 } else {
521 panic("dlil_create_input_thread: couldn't create thread\n");
522 }
523 OSAddAtomic(1, (SInt32*)&cur_dlil_input_threads);
524#if DLIL_DEBUG
525 printf("dlil_create_input_thread: threadinfo: %p input_thread=%p threads: cur=%d max=%d\n",
526 inputthread, inputthread->input_thread, dlil_multithreaded_input, cur_dlil_input_threads);
527#endif
528 return error;
529}
530__private_extern__ void
531dlil_terminate_input_thread(
532 struct dlil_threading_info *inputthread)
533{
534 OSAddAtomic(-1, (SInt32*)&cur_dlil_input_threads);
535
536 lck_mtx_unlock(inputthread->input_lck);
537 lck_mtx_free(inputthread->input_lck, inputthread->lck_grp);
538 lck_grp_free(inputthread->lck_grp);
539
540 FREE(inputthread, M_NKE);
541
542 /* For the extra reference count from kernel_thread_start() */
543 thread_deallocate(current_thread());
544
545 thread_terminate(current_thread());
546}
547
548static kern_return_t
549dlil_affinity_set(struct thread *tp, u_int32_t tag)
550{
551 thread_affinity_policy_data_t policy;
552
553 bzero(&policy, sizeof (policy));
554 policy.affinity_tag = tag;
555 return (thread_policy_set(tp, THREAD_AFFINITY_POLICY,
556 (thread_policy_t)&policy, THREAD_AFFINITY_POLICY_COUNT));
557}
558
91447636
A
559void
560dlil_init(void)
561{
2d21ac55 562 PE_parse_boot_arg("net_affinity", &net_affinity);
91447636
A
563
564 TAILQ_INIT(&dlil_ifnet_head);
91447636
A
565 TAILQ_INIT(&ifnet_head);
566
567 /* Setup the lock groups we will use */
2d21ac55 568 dlil_grp_attributes = lck_grp_attr_alloc_init();
91447636 569
2d21ac55
A
570 dlil_lock_group = lck_grp_alloc_init("dlil internal locks", dlil_grp_attributes);
571 ifnet_lock_group = lck_grp_alloc_init("ifnet locks", dlil_grp_attributes);
572 ifnet_head_lock_group = lck_grp_alloc_init("ifnet head lock", dlil_grp_attributes);
573 dlil_input_lock_grp = lck_grp_alloc_init("dlil input lock", dlil_grp_attributes);
91447636
A
574
575 /* Setup the lock attributes we will use */
2d21ac55 576 dlil_lck_attributes = lck_attr_alloc_init();
91447636
A
577
578 ifnet_lock_attr = lck_attr_alloc_init();
91447636 579
91447636 580
2d21ac55
A
581 ifnet_head_mutex = lck_rw_alloc_init(ifnet_head_lock_group, dlil_lck_attributes);
582 dlil_ifnet_mutex = lck_mtx_alloc_init(dlil_lock_group, dlil_lck_attributes);
583 dlil_mutex = lck_mtx_alloc_init(dlil_lock_group, dlil_lck_attributes);
91447636 584
2d21ac55
A
585 lck_attr_free(dlil_lck_attributes);
586 dlil_lck_attributes = NULL;
91447636
A
587
588 /*
2d21ac55 589 * Create and start up the first dlil input thread once everything is initialized
91447636 590 */
2d21ac55
A
591 dlil_create_input_thread(0, dlil_lo_thread_ptr);
592
91447636
A
593 (void) kernel_thread(kernel_task, dlil_call_delayed_detach_thread);
594}
1c79356b 595
2d21ac55 596__private_extern__ int
91447636
A
597dlil_attach_filter(
598 struct ifnet *ifp,
599 const struct iff_filter *if_filter,
600 interface_filter_t *filter_ref)
1c79356b 601{
9bccf70c 602 int retval = 0;
91447636 603 struct ifnet_filter *filter;
9bccf70c 604
91447636
A
605 MALLOC(filter, struct ifnet_filter *, sizeof(*filter), M_NKE, M_WAITOK);
606 if (filter == NULL)
607 return ENOMEM;
608 bzero(filter, sizeof(*filter));
9bccf70c 609
9bccf70c 610
91447636
A
611 filter->filt_ifp = ifp;
612 filter->filt_cookie = if_filter->iff_cookie;
613 filter->filt_name = if_filter->iff_name;
614 filter->filt_protocol = if_filter->iff_protocol;
615 filter->filt_input = if_filter->iff_input;
616 filter->filt_output = if_filter->iff_output;
617 filter->filt_event = if_filter->iff_event;
618 filter->filt_ioctl = if_filter->iff_ioctl;
619 filter->filt_detached = if_filter->iff_detached;
620
621 if ((retval = dlil_write_begin()) != 0) {
622 /* Failed to acquire the write lock */
623 FREE(filter, M_NKE);
624 return retval;
625 }
626 TAILQ_INSERT_TAIL(&ifp->if_flt_head, filter, filt_next);
627 dlil_write_end();
628 *filter_ref = filter;
629 return retval;
1c79356b
A
630}
631
91447636 632static int
2d21ac55
A
633dlil_detach_filter_internal(
634 interface_filter_t filter,
635 int detached)
1c79356b 636{
91447636
A
637 int retval = 0;
638
3a60a9f5
A
639 if (detached == 0) {
640 ifnet_t ifp = NULL;
641 interface_filter_t entry = NULL;
642
643 /* Take the write lock */
644 retval = dlil_write_begin();
645 if (retval != 0 && retval != EDEADLK)
646 return retval;
647
648 /*
649 * At this point either we have the write lock (retval == 0)
650 * or we couldn't get it (retval == EDEADLK) because someone
651 * else up the stack is holding the read lock. It is safe to
652 * read, either the read or write is held. Verify the filter
653 * parameter before proceeding.
654 */
655 ifnet_head_lock_shared();
656 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
657 TAILQ_FOREACH(entry, &ifp->if_flt_head, filt_next) {
658 if (entry == filter)
659 break;
660 }
661 if (entry == filter)
662 break;
663 }
664 ifnet_head_done();
665
666 if (entry != filter) {
667 /* filter parameter is not a valid filter ref */
668 if (retval == 0) {
669 dlil_write_end();
670 }
671 return EINVAL;
672 }
673
91447636
A
674 if (retval == EDEADLK) {
675 /* Perform a delayed detach */
676 filter->filt_detaching = 1;
677 dlil_detach_waiting = 1;
678 wakeup(&dlil_detach_waiting);
3a60a9f5 679 return 0;
91447636 680 }
3a60a9f5
A
681
682 /* Remove the filter from the list */
683 TAILQ_REMOVE(&ifp->if_flt_head, filter, filt_next);
91447636 684 dlil_write_end();
3a60a9f5 685 }
91447636 686
3a60a9f5 687 /* Call the detached funciton if there is one */
91447636
A
688 if (filter->filt_detached)
689 filter->filt_detached(filter->filt_cookie, filter->filt_ifp);
9bccf70c 690
3a60a9f5 691 /* Free the filter */
91447636
A
692 FREE(filter, M_NKE);
693
694 return retval;
1c79356b
A
695}
696
2d21ac55 697__private_extern__ void
91447636
A
698dlil_detach_filter(interface_filter_t filter)
699{
3a60a9f5
A
700 if (filter == NULL)
701 return;
91447636
A
702 dlil_detach_filter_internal(filter, 0);
703}
1c79356b 704
91447636 705static void
2d21ac55
A
706dlil_input_thread_func(
707 struct dlil_threading_info *inputthread)
91447636
A
708{
709 while (1) {
2d21ac55
A
710 struct mbuf *m = NULL, *m_loop = NULL;
711#if IFNET_INPUT_SANITY_CHK
712 int loop_cnt = 0, mbuf_cnt;
713 int count;
714 struct mbuf *m1;
715#endif /* IFNET_INPUT_SANITY_CHK */
716
717 lck_mtx_lock(inputthread->input_lck);
718
719 /* Wait until there is work to be done */
720 while ((inputthread->input_waiting & ~DLIL_INPUT_RUNNING) == 0) {
721 inputthread->input_waiting &= ~DLIL_INPUT_RUNNING;
722 msleep(&inputthread->input_waiting, inputthread->input_lck, 0, inputthread->input_name, 0);
723 }
724
725
726 lck_mtx_assert(inputthread->input_lck, LCK_MTX_ASSERT_OWNED);
727
728 m = inputthread->mbuf_head;
729 inputthread->mbuf_head = NULL;
730 inputthread->mbuf_tail = NULL;
731
732 if (inputthread->input_waiting & DLIL_INPUT_TERMINATE) {
733 if (m)
734 mbuf_freem_list(m);
735 /* this is the end */
736 dlil_terminate_input_thread(inputthread);
737 return;
738 }
739
740 inputthread->input_waiting |= DLIL_INPUT_RUNNING;
741 inputthread->input_waiting &= ~DLIL_INPUT_WAITING;
742
743 if (inputthread == dlil_lo_thread_ptr) {
744 m_loop = dlil_lo_input_mbuf_head;
745 dlil_lo_input_mbuf_head = NULL;
746 dlil_lo_input_mbuf_tail = NULL;
747 }
748
749#if IFNET_INPUT_SANITY_CHK
750 if (dlil_input_sanity_check != 0) {
751 mbuf_cnt = inputthread->mbuf_count;
752 inputthread->mbuf_count = 0;
753 if (inputthread == dlil_lo_thread_ptr) {
754 loop_cnt = dlil_lo_input_mbuf_count;
755 dlil_lo_input_mbuf_count = 0;
756 }
91447636 757
2d21ac55 758 lck_mtx_unlock(inputthread->input_lck);
91447636 759
2d21ac55
A
760 for (m1 = m, count = 0; m1; m1 = mbuf_nextpkt(m1)) {
761 count++;
762 }
763 if (count != mbuf_cnt) {
764 panic("dlil_input_func - thread=%p reg. loop queue has %d packets, should have %d\n",
765 inputthread, count, mbuf_cnt);
766 }
767
768 if (inputthread == dlil_lo_thread_ptr) {
769 for (m1 = m_loop, count = 0; m1; m1 = mbuf_nextpkt(m1)) {
770 count++;
771 }
772 if (count != loop_cnt) {
773 panic("dlil_input_func - thread=%p loop queue has %d packets, should have %d\n",
774 inputthread, count, loop_cnt);
775 }
776 }
777 } else
778#endif /* IFNET_INPUT_SANITY_CHK */
779 {
780 lck_mtx_unlock(inputthread->input_lck);
781 }
782
783
91447636
A
784 /*
785 * NOTE warning %%% attention !!!!
786 * We should think about putting some thread starvation safeguards if
787 * we deal with long chains of packets.
788 */
2d21ac55
A
789 if (m_loop) {
790 if (inputthread == dlil_lo_thread_ptr)
791 dlil_input_packet_list(lo_ifp, m_loop);
792#if IFNET_INPUT_SANITY_CHK
793 else
794 panic("dlil_input_func - thread=%p loop queue has %d packets, should have none!\n",
795 inputthread, loop_cnt);
796#endif /* IFNET_INPUT_SANITY_CHK */
91447636 797 }
2d21ac55
A
798
799
800 if (m)
801 dlil_input_packet_list(0, m);
802
803
804 lck_mtx_lock(inputthread->input_lck);
805
806 if ((inputthread->input_waiting & (DLIL_PROTO_WAITING | DLIL_PROTO_REGISTER)) != 0) {
807 lck_mtx_unlock(inputthread->input_lck);
808 proto_input_run();
809 }
810 else
811 lck_mtx_unlock(inputthread->input_lck);
812 }
813}
814
815errno_t
816ifnet_input(
817 ifnet_t ifp,
818 mbuf_t m_head,
819 const struct ifnet_stat_increment_param *stats)
820{
821 struct thread *tp = current_thread();
822 mbuf_t m_tail;
823 struct dlil_threading_info *inp;
824#if IFNET_INPUT_SANITY_CHK
825 u_int32_t pkt_count = 0;
826#endif /* IFNET_INPUT_SANITY_CHK */
827
828 if (ifp == NULL || m_head == NULL) {
829 if (m_head)
830 mbuf_freem_list(m_head);
831 return EINVAL;
832 }
833
834 m_tail = m_head;
835 while (1) {
836#if IFNET_INPUT_SANITY_CHK
837 if (dlil_input_sanity_check != 0) {
838 ifnet_t rcvif;
91447636 839
2d21ac55
A
840 rcvif = mbuf_pkthdr_rcvif(m_tail);
841 pkt_count++;
b36670ce 842
2d21ac55
A
843 if (rcvif == NULL ||
844 (ifp->if_type != IFT_LOOP && rcvif != ifp) ||
845 (mbuf_flags(m_head) & MBUF_PKTHDR) == 0) {
846 panic("ifnet_input - invalid mbuf %p\n", m_tail);
847 }
91447636 848 }
2d21ac55
A
849#endif /* IFNET_INPUT_SANITY_CHK */
850 if (mbuf_nextpkt(m_tail) == NULL)
851 break;
852 m_tail = mbuf_nextpkt(m_tail);
91447636 853 }
1c79356b 854
2d21ac55 855 inp = ifp->if_input_thread;
1c79356b 856
2d21ac55
A
857 if (dlil_multithreaded_input == 0 || inp == NULL)
858 inp = dlil_lo_thread_ptr;
859
860 /*
861 * If there is a matching dlil input thread associated with an
862 * affinity set, associate this workloop thread with the same set.
863 * We will only do this once.
864 */
865 lck_mtx_lock(inp->input_lck);
866 if (inp->net_affinity && inp->workloop_thread == NULL) {
867 u_int32_t tag = inp->tag;
868 inp->workloop_thread = tp;
869 lck_mtx_unlock(inp->input_lck);
870
871 /* Associated the current thread with the new affinity tag */
872 (void) dlil_affinity_set(tp, tag);
873
874 /*
875 * Take a reference on the workloop (current) thread; during
876 * detach, we will need to refer to it in order ot tear down
877 * its affinity.
878 */
879 thread_reference(tp);
880 lck_mtx_lock(inp->input_lck);
881 }
882
883 /* WARNING
91447636
A
884 * Because of loopbacked multicast we cannot stuff the ifp in
885 * the rcvif of the packet header: loopback has its own dlil
886 * input queue
887 */
2d21ac55
A
888
889 if (inp == dlil_lo_thread_ptr && ifp->if_type == IFT_LOOP) {
890 if (dlil_lo_input_mbuf_head == NULL)
891 dlil_lo_input_mbuf_head = m_head;
892 else if (dlil_lo_input_mbuf_tail != NULL)
893 dlil_lo_input_mbuf_tail->m_nextpkt = m_head;
894 dlil_lo_input_mbuf_tail = m_tail;
895#if IFNET_INPUT_SANITY_CHK
896 if (dlil_input_sanity_check != 0) {
897 dlil_lo_input_mbuf_count += pkt_count;
898 inp->input_mbuf_cnt += pkt_count;
899 inp->input_wake_cnt++;
900
901 lck_mtx_assert(inp->input_lck, LCK_MTX_ASSERT_OWNED);
902 }
903#endif
904 }
905 else {
906 if (inp->mbuf_head == NULL)
907 inp->mbuf_head = m_head;
908 else if (inp->mbuf_tail != NULL)
909 inp->mbuf_tail->m_nextpkt = m_head;
910 inp->mbuf_tail = m_tail;
911#if IFNET_INPUT_SANITY_CHK
912 if (dlil_input_sanity_check != 0) {
913 inp->mbuf_count += pkt_count;
914 inp->input_mbuf_cnt += pkt_count;
915 inp->input_wake_cnt++;
916
917 lck_mtx_assert(inp->input_lck, LCK_MTX_ASSERT_OWNED);
918 }
919#endif
920 }
921
922
923 inp->input_waiting |= DLIL_INPUT_WAITING;
924 if ((inp->input_waiting & DLIL_INPUT_RUNNING) == 0) {
925 wakeup((caddr_t)&inp->input_waiting);
91447636
A
926 }
927 if (stats) {
928 ifp->if_data.ifi_ipackets += stats->packets_in;
929 ifp->if_data.ifi_ibytes += stats->bytes_in;
930 ifp->if_data.ifi_ierrors += stats->errors_in;
931
932 ifp->if_data.ifi_opackets += stats->packets_out;
933 ifp->if_data.ifi_obytes += stats->bytes_out;
934 ifp->if_data.ifi_oerrors += stats->errors_out;
935
936 ifp->if_data.ifi_collisions += stats->collisions;
937 ifp->if_data.ifi_iqdrops += stats->dropped;
938 }
2d21ac55
A
939
940 lck_mtx_unlock(inp->input_lck);
91447636
A
941
942 return 0;
1c79356b
A
943}
944
2d21ac55
A
945static int
946dlil_interface_filters_input(struct ifnet * ifp, struct mbuf * * m_p,
947 char * * frame_header_p,
948 protocol_family_t protocol_family)
91447636 949{
2d21ac55
A
950 struct ifnet_filter * filter;
951
952 TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) {
953 int result;
954
955 if (filter->filt_input
956 && (filter->filt_protocol == 0
957 || filter->filt_protocol == protocol_family)) {
958 result = (*filter->filt_input)(filter->filt_cookie,
959 ifp, protocol_family,
960 m_p, frame_header_p);
961 if (result != 0) {
962 return (result);
963 }
964 }
965 }
966 return (0);
1c79356b
A
967}
968
2d21ac55
A
969static void
970dlil_ifproto_input(struct if_proto * ifproto, mbuf_t m)
1c79356b 971{
2d21ac55 972 int error;
1c79356b 973
2d21ac55
A
974 if (ifproto->proto_kpi == kProtoKPI_v1) {
975 /* Version 1 protocols get one packet at a time */
976 while (m != NULL) {
977 char * frame_header;
978 mbuf_t next_packet;
979
980 next_packet = m->m_nextpkt;
981 m->m_nextpkt = NULL;
982 frame_header = m->m_pkthdr.header;
983 m->m_pkthdr.header = NULL;
984 error = (*ifproto->kpi.v1.input)(ifproto->ifp,
985 ifproto->protocol_family,
986 m, frame_header);
987 if (error != 0 && error != EJUSTRETURN)
988 m_freem(m);
989 m = next_packet;
990 }
991 }
992 else if (ifproto->proto_kpi == kProtoKPI_v2) {
993 /* Version 2 protocols support packet lists */
994 error = (*ifproto->kpi.v2.input)(ifproto->ifp,
995 ifproto->protocol_family,
996 m);
997 if (error != 0 && error != EJUSTRETURN)
998 m_freem_list(m);
91447636 999 }
2d21ac55
A
1000 return;
1001}
1c79356b 1002
2d21ac55
A
1003__private_extern__ void
1004dlil_input_packet_list(struct ifnet * ifp_param, struct mbuf *m)
1005{
1006 int error = 0;
1007 int locked = 0;
1008 protocol_family_t protocol_family;
1009 mbuf_t next_packet;
1010 ifnet_t ifp = ifp_param;
1011 char * frame_header;
1012 struct if_proto * last_ifproto = NULL;
1013 mbuf_t pkt_first = NULL;
1014 mbuf_t * pkt_next = NULL;
1015
1016 KERNEL_DEBUG(DBG_FNC_DLIL_INPUT | DBG_FUNC_START,0,0,0,0,0);
1017
1018 while (m != NULL) {
1019 struct if_proto * ifproto = NULL;
1020
1021 next_packet = m->m_nextpkt;
1022 m->m_nextpkt = NULL;
1023 if (ifp_param == NULL)
1024 ifp = m->m_pkthdr.rcvif;
1025 frame_header = m->m_pkthdr.header;
1026 m->m_pkthdr.header = NULL;
1027
1028 if (locked == 0) {
1029 /* dlil lock protects the demux and interface filters */
1030 locked = 1;
1031 dlil_read_begin();
1032 }
1033 /* find which protocol family this packet is for */
1034 error = (*ifp->if_demux)(ifp, m, frame_header,
1035 &protocol_family);
1036 if (error != 0) {
1037 if (error == EJUSTRETURN) {
1038 goto next;
1039 }
1040 protocol_family = 0;
1041 }
1042
1043 /* DANGER!!! */
1044 if (m->m_flags & (M_BCAST|M_MCAST))
1045 ifp->if_imcasts++;
1c79356b 1046
2d21ac55
A
1047 /* run interface filters, exclude VLAN packets PR-3586856 */
1048 if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) == 0) {
91447636 1049 int filter_result;
2d21ac55
A
1050
1051 filter_result = dlil_interface_filters_input(ifp, &m,
1052 &frame_header,
1053 protocol_family);
1054 if (filter_result != 0) {
1055 if (filter_result != EJUSTRETURN) {
1056 m_freem(m);
91447636 1057 }
2d21ac55 1058 goto next;
91447636
A
1059 }
1060 }
2d21ac55 1061 if (error != 0 || ((m->m_flags & M_PROMISC) != 0) ) {
91447636 1062 m_freem(m);
2d21ac55 1063 goto next;
91447636 1064 }
2d21ac55
A
1065
1066 /* Lookup the protocol attachment to this interface */
1067 if (protocol_family == 0) {
1068 ifproto = NULL;
1069 }
1070 else if (last_ifproto != NULL
1071 && last_ifproto->ifp == ifp
1072 && (last_ifproto->protocol_family
1073 == protocol_family)) {
1074 ifproto = last_ifproto;
1075 }
1076 else {
1077 ifproto = find_attached_proto(ifp, protocol_family);
1078 }
1079 if (ifproto == NULL) {
1080 /* no protocol for this packet, discard */
1081 m_freem(m);
1082 goto next;
1083 }
1084 if (ifproto != last_ifproto) {
1085 /* make sure ifproto can't go away during input */
1086 if_proto_ref(ifproto);
1087 if (last_ifproto != NULL) {
1088 /* pass up the list for the previous protocol */
1089 dlil_read_end();
1090
1091 dlil_ifproto_input(last_ifproto, pkt_first);
1092 pkt_first = NULL;
1093 if_proto_free(last_ifproto);
1094 dlil_read_begin();
1095 }
1096 last_ifproto = ifproto;
1097 }
1098 /* extend the list */
1099 m->m_pkthdr.header = frame_header;
1100 if (pkt_first == NULL) {
1101 pkt_first = m;
1102 } else {
1103 *pkt_next = m;
1104 }
1105 pkt_next = &m->m_nextpkt;
1c79356b 1106
2d21ac55
A
1107 next:
1108 if (next_packet == NULL && last_ifproto != NULL) {
1109 /* pass up the last list of packets */
1110 dlil_read_end();
1c79356b 1111
2d21ac55
A
1112 dlil_ifproto_input(last_ifproto, pkt_first);
1113 if_proto_free(last_ifproto);
1114 locked = 0;
1115 }
1116 m = next_packet;
1c79356b 1117
91447636 1118 }
2d21ac55
A
1119 if (locked != 0) {
1120 dlil_read_end();
1121 }
91447636 1122 KERNEL_DEBUG(DBG_FNC_DLIL_INPUT | DBG_FUNC_END,0,0,0,0,0);
2d21ac55 1123 return;
1c79356b
A
1124}
1125
91447636
A
1126static int
1127dlil_event_internal(struct ifnet *ifp, struct kev_msg *event)
1c79356b 1128{
91447636
A
1129 struct ifnet_filter *filter;
1130
1131 if (ifp_use(ifp, kIfNetUseCount_MustNotBeZero) == 0) {
1132 dlil_read_begin();
1133
1134 /* Pass the event to the interface filters */
1135 TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) {
1136 if (filter->filt_event)
1137 filter->filt_event(filter->filt_cookie, ifp, filter->filt_protocol, event);
1138 }
1139
1140 if (ifp->if_proto_hash) {
1141 int i;
1142
1143 for (i = 0; i < PROTO_HASH_SLOTS; i++) {
1144 struct if_proto *proto;
1145
1146 SLIST_FOREACH(proto, &ifp->if_proto_hash[i], next_hash) {
2d21ac55
A
1147 proto_media_event eventp = proto->proto_kpi == kProtoKPI_v1
1148 ? proto->kpi.v1.event : proto->kpi.v2.event;
1149
1150 if (eventp)
1151 eventp(ifp, proto->protocol_family, event);
91447636
A
1152 }
1153 }
1154 }
1155
1156 dlil_read_end();
1157
1158 /* Pass the event to the interface */
1159 if (ifp->if_event)
1160 ifp->if_event(ifp, event);
1161
1162 if (ifp_unuse(ifp))
1163 ifp_use_reached_zero(ifp);
1164 }
1165
1166 return kev_post_msg(event);
1c79356b
A
1167}
1168
2d21ac55
A
1169errno_t
1170ifnet_event(
1171 ifnet_t ifp,
1172 struct kern_event_msg *event)
1c79356b 1173{
91447636 1174 struct kev_msg kev_msg;
2d21ac55
A
1175 int result = 0;
1176
1177 if (ifp == NULL || event == NULL) return EINVAL;
1c79356b 1178
91447636
A
1179 kev_msg.vendor_code = event->vendor_code;
1180 kev_msg.kev_class = event->kev_class;
1181 kev_msg.kev_subclass = event->kev_subclass;
1182 kev_msg.event_code = event->event_code;
1183 kev_msg.dv[0].data_ptr = &event->event_data[0];
1184 kev_msg.dv[0].data_length = event->total_size - KEV_MSG_HEADER_SIZE;
1185 kev_msg.dv[1].data_length = 0;
1186
91447636 1187 result = dlil_event_internal(ifp, &kev_msg);
1c79356b 1188
91447636
A
1189 return result;
1190}
1c79356b 1191
2d21ac55
A
1192#if CONFIG_MACF_NET
1193#include <netinet/ip6.h>
1194#include <netinet/ip.h>
1195static int dlil_get_socket_type(struct mbuf **mp, int family, int raw)
1196{
1197 struct mbuf *m;
1198 struct ip *ip;
1199 struct ip6_hdr *ip6;
1200 int type = SOCK_RAW;
1201
1202 if (!raw) {
1203 switch (family) {
1204 case PF_INET:
1205 m = m_pullup(*mp, sizeof(struct ip));
1206 if (m == NULL)
1207 break;
1208 *mp = m;
1209 ip = mtod(m, struct ip *);
1210 if (ip->ip_p == IPPROTO_TCP)
1211 type = SOCK_STREAM;
1212 else if (ip->ip_p == IPPROTO_UDP)
1213 type = SOCK_DGRAM;
1214 break;
1215 case PF_INET6:
1216 m = m_pullup(*mp, sizeof(struct ip6_hdr));
1217 if (m == NULL)
1218 break;
1219 *mp = m;
1220 ip6 = mtod(m, struct ip6_hdr *);
1221 if (ip6->ip6_nxt == IPPROTO_TCP)
1222 type = SOCK_STREAM;
1223 else if (ip6->ip6_nxt == IPPROTO_UDP)
1224 type = SOCK_DGRAM;
1225 break;
1226 }
1227 }
1228
1229 return (type);
1230}
1231#endif
1232
1233#if 0
3a60a9f5 1234int
91447636
A
1235dlil_output_list(
1236 struct ifnet* ifp,
1237 u_long proto_family,
1238 struct mbuf *packetlist,
1239 caddr_t route,
1240 const struct sockaddr *dest,
2d21ac55 1241 int raw)
91447636 1242{
2d21ac55
A
1243 char *frame_type = NULL;
1244 char *dst_linkaddr = NULL;
1245 int retval = 0;
91447636
A
1246 char frame_type_buffer[MAX_FRAME_TYPE_SIZE * 4];
1247 char dst_linkaddr_buffer[MAX_LINKADDR * 4];
1248 struct ifnet_filter *filter;
1249 struct if_proto *proto = 0;
2d21ac55
A
1250 mbuf_t m;
1251 mbuf_t send_head = NULL;
1252 mbuf_t *send_tail = &send_head;
91447636
A
1253
1254 KERNEL_DEBUG(DBG_FNC_DLIL_OUTPUT | DBG_FUNC_START,0,0,0,0,0);
91447636
A
1255
1256 dlil_read_begin();
1257
1258 frame_type = frame_type_buffer;
1259 dst_linkaddr = dst_linkaddr_buffer;
2d21ac55
A
1260
1261 if (raw == 0) {
1262 proto = find_attached_proto(ifp, proto_family);
1263 if (proto == NULL) {
1264 retval = ENXIO;
1265 goto cleanup;
1266 }
1267 }
1268
1269preout_again:
1270 if (packetlist == NULL)
1271 goto cleanup;
91447636
A
1272 m = packetlist;
1273 packetlist = packetlist->m_nextpkt;
1274 m->m_nextpkt = NULL;
1275
2d21ac55
A
1276 if (raw == 0) {
1277 proto_media_preout preoutp = proto->proto_kpi == kProtoKPI_v1
1278 ? proto->kpi.v1.pre_output : proto->kpi.v2.pre_output;
1279 retval = 0;
1280 if (preoutp)
1281 retval = preoutp(ifp, proto_family, &m, dest, route, frame_type, dst_linkaddr);
1282
1283 if (retval) {
1284 if (retval == EJUSTRETURN) {
1285 goto preout_again;
1286 }
1287
91447636 1288 m_freem(m);
2d21ac55 1289 goto cleanup;
91447636 1290 }
91447636 1291 }
1c79356b 1292
91447636 1293 do {
2d21ac55
A
1294#if CONFIG_MACF_NET
1295 retval = mac_ifnet_check_transmit(ifp, m, proto_family,
1296 dlil_get_socket_type(&m, proto_family, raw));
1297 if (retval) {
1298 m_freem(m);
1299 goto cleanup;
1300 }
1301#endif
91447636 1302
2d21ac55 1303 if (raw == 0 && ifp->if_framer) {
91447636
A
1304 retval = ifp->if_framer(ifp, &m, dest, dst_linkaddr, frame_type);
1305 if (retval) {
1306 if (retval != EJUSTRETURN) {
1307 m_freem(m);
1308 }
2d21ac55 1309 goto next;
91447636
A
1310 }
1311 }
1312
2d21ac55
A
1313#if BRIDGE
1314 /* !!!LOCKING!!!
1315 *
1316 * Need to consider how to handle this.
1317 * Also note that return should be a goto cleanup
1318 */
1319 broken-locking
1320 if (do_bridge) {
1321 struct mbuf *m0 = m;
1322 struct ether_header *eh = mtod(m, struct ether_header *);
1323
1324 if (m->m_pkthdr.rcvif)
1325 m->m_pkthdr.rcvif = NULL;
1326 ifp = bridge_dst_lookup(eh);
1327 bdg_forward(&m0, ifp);
1328 if (m0)
1329 m_freem(m0);
1330
1331 return 0 - should be goto cleanup?
1332 }
1333#endif
1334
91447636
A
1335 /*
1336 * Let interface filters (if any) do their thing ...
1337 */
1338 /* Do not pass VLAN tagged packets to filters PR-3586856 */
1339 if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) == 0) {
1340 TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) {
1341 if ((filter->filt_protocol == 0 || (filter->filt_protocol == proto_family)) &&
1342 filter->filt_output) {
1343 retval = filter->filt_output(filter->filt_cookie, ifp, proto_family, &m);
1344 if (retval) {
6601e61a 1345 if (retval != EJUSTRETURN)
91447636 1346 m_freem(m);
6601e61a 1347 goto next;
91447636
A
1348 }
1349 }
1350 }
1351 }
2d21ac55 1352
91447636 1353 /*
2d21ac55
A
1354 * Finally, call the driver.
1355 */
91447636 1356
2d21ac55
A
1357 if ((ifp->if_eflags & IFEF_SENDLIST) != 0) {
1358 *send_tail = m;
1359 send_tail = &m->m_nextpkt;
1360 }
1361 else {
1362 KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_START, 0,0,0,0,0);
1363 retval = ifp->if_output(ifp, m);
1364 if (retval) {
1365 printf("dlil_output: output error retval = %x\n", retval);
1366 }
1367 KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0);
91447636
A
1368 }
1369 KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0);
2d21ac55 1370
6601e61a 1371next:
91447636
A
1372 m = packetlist;
1373 if (m) {
1374 packetlist = packetlist->m_nextpkt;
1375 m->m_nextpkt = NULL;
1376 }
1377 } while (m);
1c79356b 1378
2d21ac55
A
1379 if (send_head) {
1380 KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_START, 0,0,0,0,0);
1381 retval = ifp->if_output(ifp, send_head);
1382 if (retval) {
1383 printf("dlil_output: output error retval = %x\n", retval);
1384 }
1385 KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0);
1386 }
91447636
A
1387
1388 KERNEL_DEBUG(DBG_FNC_DLIL_OUTPUT | DBG_FUNC_END,0,0,0,0,0);
1c79356b 1389
91447636
A
1390cleanup:
1391 dlil_read_end();
1392 if (packetlist) /* if any packet left, clean up */
2d21ac55 1393 mbuf_freem_list(packetlist);
91447636
A
1394 if (retval == EJUSTRETURN)
1395 retval = 0;
1396 return retval;
1c79356b 1397}
2d21ac55 1398#endif
1c79356b 1399
1c79356b 1400/*
91447636
A
1401 * dlil_output
1402 *
1403 * Caller should have a lock on the protocol domain if the protocol
1404 * doesn't support finer grained locking. In most cases, the lock
1405 * will be held from the socket layer and won't be released until
1406 * we return back to the socket layer.
1407 *
1408 * This does mean that we must take a protocol lock before we take
1409 * an interface lock if we're going to take both. This makes sense
1410 * because a protocol is likely to interact with an ifp while it
1411 * is under the protocol lock.
1c79356b 1412 */
2d21ac55 1413__private_extern__ errno_t
91447636 1414dlil_output(
2d21ac55
A
1415 ifnet_t ifp,
1416 protocol_family_t proto_family,
1417 mbuf_t packetlist,
1418 void *route,
91447636 1419 const struct sockaddr *dest,
2d21ac55 1420 int raw)
91447636 1421{
2d21ac55
A
1422 char *frame_type = NULL;
1423 char *dst_linkaddr = NULL;
91447636
A
1424 int retval = 0;
1425 char frame_type_buffer[MAX_FRAME_TYPE_SIZE * 4];
1426 char dst_linkaddr_buffer[MAX_LINKADDR * 4];
1427 struct ifnet_filter *filter;
2d21ac55
A
1428 struct if_proto *proto = 0;
1429 mbuf_t m;
1430 mbuf_t send_head = NULL;
1431 mbuf_t *send_tail = &send_head;
91447636
A
1432
1433 KERNEL_DEBUG(DBG_FNC_DLIL_OUTPUT | DBG_FUNC_START,0,0,0,0,0);
1434
1435 dlil_read_begin();
1436
1437 frame_type = frame_type_buffer;
1438 dst_linkaddr = dst_linkaddr_buffer;
1439
1440 if (raw == 0) {
91447636
A
1441 proto = find_attached_proto(ifp, proto_family);
1442 if (proto == NULL) {
91447636
A
1443 retval = ENXIO;
1444 goto cleanup;
1445 }
2d21ac55
A
1446 }
1447
1448preout_again:
1449 if (packetlist == NULL)
1450 goto cleanup;
1451 m = packetlist;
1452 packetlist = packetlist->m_nextpkt;
1453 m->m_nextpkt = NULL;
1454
1455 if (raw == 0) {
1456 proto_media_preout preoutp = proto->proto_kpi == kProtoKPI_v1
1457 ? proto->kpi.v1.pre_output : proto->kpi.v2.pre_output;
91447636 1458 retval = 0;
2d21ac55
A
1459 if (preoutp)
1460 retval = preoutp(ifp, proto_family, &m, dest, route, frame_type, dst_linkaddr);
1461
91447636 1462 if (retval) {
2d21ac55
A
1463 if (retval == EJUSTRETURN) {
1464 goto preout_again;
91447636 1465 }
2d21ac55
A
1466
1467 m_freem(m);
91447636 1468 goto cleanup;
1c79356b 1469 }
1c79356b 1470 }
2d21ac55
A
1471
1472#if CONFIG_MACF_NET
1473 retval = mac_ifnet_check_transmit(ifp, m, proto_family,
1474 dlil_get_socket_type(&m, proto_family, raw));
1475 if (retval) {
1476 m_freem(m);
1477 goto cleanup;
1478 }
1479#endif
1480
1481 do {
1482 if (raw == 0 && ifp->if_framer) {
1483 retval = ifp->if_framer(ifp, &m, dest, dst_linkaddr, frame_type);
1484 if (retval) {
1485 if (retval != EJUSTRETURN) {
1486 m_freem(m);
1487 }
1488 goto next;
91447636 1489 }
91447636 1490 }
1c79356b 1491
91447636 1492#if BRIDGE
2d21ac55
A
1493 /* !!!LOCKING!!!
1494 *
1495 * Need to consider how to handle this.
1496 * Also note that return should be a goto cleanup
1497 */
1498 broken-locking
1499 if (do_bridge) {
1500 struct mbuf *m0 = m;
1501 struct ether_header *eh = mtod(m, struct ether_header *);
1502
1503 if (m->m_pkthdr.rcvif)
1504 m->m_pkthdr.rcvif = NULL;
1505 ifp = bridge_dst_lookup(eh);
1506 bdg_forward(&m0, ifp);
1507 if (m0)
1508 m_freem(m0);
1509
1510 return 0 - should be goto cleanup?
1511 }
1c79356b 1512#endif
2d21ac55
A
1513
1514 /*
1515 * Let interface filters (if any) do their thing ...
1516 */
1517 /* Do not pass VLAN tagged packets to filters PR-3586856 */
1518 if ((m->m_pkthdr.csum_flags & CSUM_VLAN_TAG_VALID) == 0) {
1519 TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) {
1520 if ((filter->filt_protocol == 0 || (filter->filt_protocol == proto_family)) &&
1521 filter->filt_output) {
1522 retval = filter->filt_output(filter->filt_cookie, ifp, proto_family, &m);
1523 if (retval) {
1524 if (retval != EJUSTRETURN)
1525 m_freem(m);
1526 goto next;
1527 }
91447636 1528 }
1c79356b 1529 }
1c79356b 1530 }
2d21ac55
A
1531
1532 /*
1533 * If the underlying interface is not capable of handling a
1534 * packet whose data portion spans across physically disjoint
1535 * pages, we need to "normalize" the packet so that we pass
1536 * down a chain of mbufs where each mbuf points to a span that
1537 * resides in the system page boundary. If the packet does
1538 * not cross page(s), the following is a no-op.
1539 */
1540 if (!(ifp->if_hwassist & IFNET_MULTIPAGES)) {
1541 if ((m = m_normalize(m)) == NULL)
1542 goto next;
1543 }
1544
1545 /*
1546 * Finally, call the driver.
1547 */
1548
1549 if ((ifp->if_eflags & IFEF_SENDLIST) != 0) {
1550 *send_tail = m;
1551 send_tail = &m->m_nextpkt;
1552 }
1553 else {
1554 KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_START, 0,0,0,0,0);
1555 retval = ifp->if_output(ifp, m);
1556 if (retval) {
1557 printf("dlil_output: output error retval = %x\n", retval);
1558 }
1559 KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0);
1560 }
1561 KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0);
1562
1563next:
1564 m = packetlist;
1565 if (m) {
1566 packetlist = packetlist->m_nextpkt;
1567 m->m_nextpkt = NULL;
1568 }
1569 } while (m);
1570
1571 if (send_head) {
1572 KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_START, 0,0,0,0,0);
1573 retval = ifp->if_output(ifp, send_head);
1574 if (retval) {
1575 printf("dlil_output: output error retval = %x\n", retval);
1576 }
1577 KERNEL_DEBUG(DBG_FNC_DLIL_IFOUT | DBG_FUNC_END, 0,0,0,0,0);
1c79356b 1578 }
91447636 1579
91447636 1580 KERNEL_DEBUG(DBG_FNC_DLIL_OUTPUT | DBG_FUNC_END,0,0,0,0,0);
1c79356b 1581
91447636
A
1582cleanup:
1583 dlil_read_end();
2d21ac55
A
1584 if (packetlist) /* if any packet left, clean up */
1585 mbuf_freem_list(packetlist);
91447636
A
1586 if (retval == EJUSTRETURN)
1587 retval = 0;
1c79356b
A
1588 return retval;
1589}
1590
2d21ac55
A
1591errno_t
1592ifnet_ioctl(
1593 ifnet_t ifp,
1594 protocol_family_t proto_fam,
1595 u_int32_t ioctl_code,
1596 void *ioctl_arg)
1c79356b 1597{
91447636
A
1598 struct ifnet_filter *filter;
1599 int retval = EOPNOTSUPP;
1600 int result = 0;
91447636
A
1601 int holding_read = 0;
1602
2d21ac55
A
1603 if (ifp == NULL || ioctl_code == 0)
1604 return EINVAL;
1605
91447636
A
1606 /* Attempt to increment the use count. If it's zero, bail out, the ifp is invalid */
1607 result = ifp_use(ifp, kIfNetUseCount_MustNotBeZero);
1608 if (result != 0)
1609 return EOPNOTSUPP;
1610
1611 dlil_read_begin();
1612 holding_read = 1;
1613
1614 /* Run the interface filters first.
1615 * We want to run all filters before calling the protocol,
1616 * interface family, or interface.
1617 */
1618 TAILQ_FOREACH(filter, &ifp->if_flt_head, filt_next) {
1619 if ((filter->filt_protocol == 0 || (filter->filt_protocol == proto_fam)) &&
1620 filter->filt_ioctl != NULL) {
1621 result = filter->filt_ioctl(filter->filt_cookie, ifp, proto_fam, ioctl_code, ioctl_arg);
1622 /* Only update retval if no one has handled the ioctl */
1623 if (retval == EOPNOTSUPP || result == EJUSTRETURN) {
1624 if (result == ENOTSUP)
1625 result = EOPNOTSUPP;
1626 retval = result;
1627 if (retval && retval != EOPNOTSUPP) {
1628 goto cleanup;
1629 }
1630 }
1631 }
1632 }
1633
1634 /* Allow the protocol to handle the ioctl */
1635 if (proto_fam) {
1636 struct if_proto *proto = find_attached_proto(ifp, proto_fam);
1637
1638 if (proto != 0) {
2d21ac55
A
1639 proto_media_ioctl ioctlp = proto->proto_kpi == kProtoKPI_v1
1640 ? proto->kpi.v1.ioctl : proto->kpi.v2.ioctl;
91447636 1641 result = EOPNOTSUPP;
2d21ac55
A
1642 if (ioctlp)
1643 result = ioctlp(ifp, proto_fam, ioctl_code, ioctl_arg);
91447636
A
1644
1645 /* Only update retval if no one has handled the ioctl */
1646 if (retval == EOPNOTSUPP || result == EJUSTRETURN) {
1647 if (result == ENOTSUP)
1648 result = EOPNOTSUPP;
1649 retval = result;
1650 if (retval && retval != EOPNOTSUPP) {
1651 goto cleanup;
1652 }
1653 }
1654 }
1655 }
1656
1657 /*
1658 * Since we have incremented the use count on the ifp, we are guaranteed
1659 * that the ifp will not go away (the function pointers may not be changed).
1660 * We release the dlil read lock so the interface ioctl may trigger a
1661 * protocol attach. This happens with vlan and may occur with other virtual
1662 * interfaces.
1663 */
1664 dlil_read_end();
1665 holding_read = 0;
1666
1667 /* retval is either 0 or EOPNOTSUPP */
1668
91447636
A
1669 /*
1670 * Let the interface handle this ioctl.
1671 * If it returns EOPNOTSUPP, ignore that, we may have
1672 * already handled this in the protocol or family.
1673 */
1674 if (ifp->if_ioctl)
1675 result = (*ifp->if_ioctl)(ifp, ioctl_code, ioctl_arg);
1676
1677 /* Only update retval if no one has handled the ioctl */
1678 if (retval == EOPNOTSUPP || result == EJUSTRETURN) {
1679 if (result == ENOTSUP)
1680 result = EOPNOTSUPP;
1681 retval = result;
1682 if (retval && retval != EOPNOTSUPP) {
1683 goto cleanup;
1684 }
1685 }
1686
1687cleanup:
1688 if (holding_read)
1689 dlil_read_end();
1690 if (ifp_unuse(ifp))
1691 ifp_use_reached_zero(ifp);
1c79356b 1692
91447636
A
1693 if (retval == EJUSTRETURN)
1694 retval = 0;
1695 return retval;
1696}
1c79356b 1697
91447636
A
1698__private_extern__ errno_t
1699dlil_set_bpf_tap(
1700 ifnet_t ifp,
1701 bpf_tap_mode mode,
1702 bpf_packet_func callback)
1703{
1704 errno_t error = 0;
1c79356b 1705
91447636
A
1706 dlil_read_begin();
1707 if (ifp->if_set_bpf_tap)
1708 error = ifp->if_set_bpf_tap(ifp, mode, callback);
1709 dlil_read_end();
1710
1711 return error;
1c79356b
A
1712}
1713
2d21ac55 1714errno_t
91447636
A
1715dlil_resolve_multi(
1716 struct ifnet *ifp,
1717 const struct sockaddr *proto_addr,
1718 struct sockaddr *ll_addr,
1719 size_t ll_len)
1c79356b 1720{
91447636
A
1721 errno_t result = EOPNOTSUPP;
1722 struct if_proto *proto;
1723 const struct sockaddr *verify;
2d21ac55 1724 proto_media_resolve_multi resolvep;
91447636
A
1725
1726 dlil_read_begin();
1727
1728 bzero(ll_addr, ll_len);
1729
1730 /* Call the protocol first */
1731 proto = find_attached_proto(ifp, proto_addr->sa_family);
2d21ac55
A
1732 if (proto != NULL) {
1733 resolvep = proto->proto_kpi == kProtoKPI_v1
1734 ? proto->kpi.v1.resolve_multi : proto->kpi.v2.resolve_multi;
1735 if (resolvep != NULL)
1736 result = resolvep(ifp, proto_addr,(struct sockaddr_dl*)ll_addr,
1737 ll_len);
91447636
A
1738 }
1739
1740 /* Let the interface verify the multicast address */
1741 if ((result == EOPNOTSUPP || result == 0) && ifp->if_check_multi) {
1742 if (result == 0)
1743 verify = ll_addr;
1744 else
1745 verify = proto_addr;
1746 result = ifp->if_check_multi(ifp, verify);
1747 }
1748
1749 dlil_read_end();
1750
1751 return result;
1752}
1c79356b 1753
91447636
A
1754__private_extern__ errno_t
1755dlil_send_arp_internal(
1756 ifnet_t ifp,
1757 u_short arpop,
1758 const struct sockaddr_dl* sender_hw,
1759 const struct sockaddr* sender_proto,
1760 const struct sockaddr_dl* target_hw,
1761 const struct sockaddr* target_proto)
1762{
1763 struct if_proto *proto;
1764 errno_t result = 0;
1765
1766 dlil_read_begin();
1767
1768 proto = find_attached_proto(ifp, target_proto->sa_family);
2d21ac55 1769 if (proto == NULL) {
91447636
A
1770 result = ENOTSUP;
1771 }
1772 else {
2d21ac55
A
1773 proto_media_send_arp arpp;
1774 arpp = proto->proto_kpi == kProtoKPI_v1
1775 ? proto->kpi.v1.send_arp : proto->kpi.v2.send_arp;
1776 if (arpp == NULL)
1777 result = ENOTSUP;
1778 else
1779 result = arpp(ifp, arpop, sender_hw, sender_proto, target_hw,
1780 target_proto);
91447636
A
1781 }
1782
1783 dlil_read_end();
1784
1785 return result;
1786}
1c79356b 1787
2d21ac55
A
1788static __inline__ int
1789_is_announcement(const struct sockaddr_in * sender_sin,
1790 const struct sockaddr_in * target_sin)
1791{
1792 if (sender_sin == NULL) {
1793 return FALSE;
1794 }
1795 return (sender_sin->sin_addr.s_addr == target_sin->sin_addr.s_addr);
1796}
1797
91447636
A
1798__private_extern__ errno_t
1799dlil_send_arp(
1800 ifnet_t ifp,
1801 u_short arpop,
1802 const struct sockaddr_dl* sender_hw,
1803 const struct sockaddr* sender_proto,
1804 const struct sockaddr_dl* target_hw,
1805 const struct sockaddr* target_proto)
1806{
1807 errno_t result = 0;
2d21ac55
A
1808 const struct sockaddr_in * sender_sin;
1809 const struct sockaddr_in * target_sin;
91447636
A
1810
1811 if (target_proto == NULL || (sender_proto &&
1812 sender_proto->sa_family != target_proto->sa_family))
1813 return EINVAL;
1814
1815 /*
1816 * If this is an ARP request and the target IP is IPv4LL,
2d21ac55
A
1817 * send the request on all interfaces. The exception is
1818 * an announcement, which must only appear on the specific
1819 * interface.
91447636 1820 */
2d21ac55
A
1821 sender_sin = (const struct sockaddr_in *)sender_proto;
1822 target_sin = (const struct sockaddr_in *)target_proto;
1823 if (target_proto->sa_family == AF_INET
1824 && IN_LINKLOCAL(ntohl(target_sin->sin_addr.s_addr))
1825 && ipv4_ll_arp_aware != 0
1826 && arpop == ARPOP_REQUEST
1827 && !_is_announcement(target_sin, sender_sin)) {
91447636
A
1828 ifnet_t *ifp_list;
1829 u_int32_t count;
1830 u_int32_t ifp_on;
1831
1832 result = ENOTSUP;
1833
1834 if (ifnet_list_get(IFNET_FAMILY_ANY, &ifp_list, &count) == 0) {
1835 for (ifp_on = 0; ifp_on < count; ifp_on++) {
1836 errno_t new_result;
1837 ifaddr_t source_hw = NULL;
1838 ifaddr_t source_ip = NULL;
1839 struct sockaddr_in source_ip_copy;
1840
1841 /*
1842 * Only arp on interfaces marked for IPv4LL ARPing. This may
1843 * mean that we don't ARP on the interface the subnet route
1844 * points to.
1845 */
1846 if ((ifp_list[ifp_on]->if_eflags & IFEF_ARPLL) == 0) {
1847 continue;
1848 }
1849
1850 source_hw = TAILQ_FIRST(&ifp_list[ifp_on]->if_addrhead);
1851
1852 /* Find the source IP address */
1853 ifnet_lock_shared(ifp_list[ifp_on]);
1854 TAILQ_FOREACH(source_ip, &ifp_list[ifp_on]->if_addrhead,
1855 ifa_link) {
1856 if (source_ip->ifa_addr &&
1857 source_ip->ifa_addr->sa_family == AF_INET) {
1858 break;
1859 }
1860 }
1861
1862 /* No IP Source, don't arp */
1863 if (source_ip == NULL) {
1864 ifnet_lock_done(ifp_list[ifp_on]);
1865 continue;
1866 }
1867
1868 /* Copy the source IP address */
1869 source_ip_copy = *(struct sockaddr_in*)source_ip->ifa_addr;
1870
1871 ifnet_lock_done(ifp_list[ifp_on]);
1872
1873 /* Send the ARP */
1874 new_result = dlil_send_arp_internal(ifp_list[ifp_on], arpop,
1875 (struct sockaddr_dl*)source_hw->ifa_addr,
1876 (struct sockaddr*)&source_ip_copy, NULL,
1877 target_proto);
1878
1879 if (result == ENOTSUP) {
1880 result = new_result;
1881 }
1882 }
1883 }
1884
1885 ifnet_list_free(ifp_list);
1886 }
1887 else {
1888 result = dlil_send_arp_internal(ifp, arpop, sender_hw, sender_proto,
1889 target_hw, target_proto);
1890 }
1891
1892 return result;
1893}
1c79356b 1894
2d21ac55 1895__private_extern__ int
91447636
A
1896ifp_use(
1897 struct ifnet *ifp,
1898 int handle_zero)
1899{
1900 int old_value;
1901 int retval = 0;
1902
1903 do {
1904 old_value = ifp->if_usecnt;
1905 if (old_value == 0 && handle_zero == kIfNetUseCount_MustNotBeZero) {
1906 retval = ENXIO; // ifp is invalid
1907 break;
1908 }
1909 } while (!OSCompareAndSwap((UInt32)old_value, (UInt32)old_value + 1, (UInt32*)&ifp->if_usecnt));
1910
1911 return retval;
1912}
1c79356b 1913
91447636
A
1914/* ifp_unuse is broken into two pieces.
1915 *
1916 * ifp_use and ifp_unuse must be called between when the caller calls
1917 * dlil_write_begin and dlil_write_end. ifp_unuse needs to perform some
1918 * operations after dlil_write_end has been called. For this reason,
1919 * anyone calling ifp_unuse must call ifp_use_reached_zero if ifp_unuse
1920 * returns a non-zero value. The caller must call ifp_use_reached_zero
1921 * after the caller has called dlil_write_end.
1922 */
2d21ac55 1923__private_extern__ void
91447636
A
1924ifp_use_reached_zero(
1925 struct ifnet *ifp)
1926{
91447636
A
1927 ifnet_detached_func free_func;
1928
1929 dlil_read_begin();
1930
1931 if (ifp->if_usecnt != 0)
1932 panic("ifp_use_reached_zero: ifp->if_usecnt != 0");
1933
91447636
A
1934 ifnet_head_lock_exclusive();
1935 ifnet_lock_exclusive(ifp);
1936
1937 /* Remove ourselves from the list */
1938 TAILQ_REMOVE(&ifnet_head, ifp, if_link);
2d21ac55 1939 ifnet_addrs[ifp->if_index - 1] = NULL;
91447636
A
1940
1941 /* ifp should be removed from the interface list */
1942 while (ifp->if_multiaddrs.lh_first) {
1943 struct ifmultiaddr *ifma = ifp->if_multiaddrs.lh_first;
1944
1945 /*
1946 * When the interface is gone, we will no longer
1947 * be listening on these multicasts. Various bits
1948 * of the stack may be referencing these multicasts,
1949 * release only our reference.
1950 */
1951 LIST_REMOVE(ifma, ifma_link);
1952 ifma->ifma_ifp = NULL;
1953 ifma_release(ifma);
1954 }
1955 ifnet_head_done();
1956
1957 ifp->if_eflags &= ~IFEF_DETACHING; // clear the detaching flag
1958 ifnet_lock_done(ifp);
1c79356b 1959
91447636
A
1960 free_func = ifp->if_free;
1961 dlil_read_end();
2d21ac55 1962 dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_DETACHED, NULL, 0);
91447636
A
1963
1964 if (free_func)
1965 free_func(ifp);
1966}
1c79356b 1967
2d21ac55 1968__private_extern__ int
91447636
A
1969ifp_unuse(
1970 struct ifnet *ifp)
1971{
1972 int oldval;
2d21ac55 1973 oldval = OSDecrementAtomic((SInt32*)&ifp->if_usecnt);
91447636 1974 if (oldval == 0)
2d21ac55 1975 panic("ifp_unuse: ifp(%s%d)->if_usecnt was zero\n", ifp->if_name, ifp->if_unit);
91447636
A
1976
1977 if (oldval > 1)
1978 return 0;
1979
1980 if ((ifp->if_eflags & IFEF_DETACHING) == 0)
1981 panic("ifp_unuse: use count reached zero but detching flag is not set!");
1982
1983 return 1; /* caller must call ifp_use_reached_zero */
1984}
1c79356b 1985
91447636 1986extern lck_mtx_t *domain_proto_mtx;
1c79356b 1987
2d21ac55 1988static errno_t
91447636
A
1989dlil_attach_protocol_internal(
1990 struct if_proto *proto,
91447636
A
1991 const struct ifnet_demux_desc *demux_list,
1992 u_int32_t demux_count)
1993{
91447636
A
1994 struct kev_dl_proto_data ev_pr_data;
1995 struct ifnet *ifp = proto->ifp;
1996 int retval = 0;
1997 u_long hash_value = proto_hash_value(proto->protocol_family);
91447636
A
1998
1999 /* setup some of the common values */
91447636 2000 {
2d21ac55 2001 struct domain *dp;
91447636 2002 lck_mtx_lock(domain_proto_mtx);
2d21ac55 2003 dp = domains;
91447636
A
2004 while (dp && (protocol_family_t)dp->dom_family != proto->protocol_family)
2005 dp = dp->dom_next;
2006 proto->dl_domain = dp;
2007 lck_mtx_unlock(domain_proto_mtx);
2008 }
2009
91447636
A
2010 /*
2011 * Take the write lock to protect readers and exclude other writers.
2012 */
2d21ac55
A
2013 if ((retval = dlil_write_begin()) != 0) {
2014 printf("dlil_attach_protocol_internal - dlil_write_begin returned %d\n", retval);
2015 return retval;
2016 }
91447636
A
2017
2018 /* Check that the interface isn't currently detaching */
2019 ifnet_lock_shared(ifp);
2020 if ((ifp->if_eflags & IFEF_DETACHING) != 0) {
2021 ifnet_lock_done(ifp);
2022 dlil_write_end();
91447636
A
2023 return ENXIO;
2024 }
2025 ifnet_lock_done(ifp);
2026
2027 if (find_attached_proto(ifp, proto->protocol_family) != NULL) {
2028 dlil_write_end();
91447636
A
2029 return EEXIST;
2030 }
2031
2032 /*
2033 * Call family module add_proto routine so it can refine the
2034 * demux descriptors as it wishes.
2035 */
2d21ac55 2036 retval = ifp->if_add_proto(ifp, proto->protocol_family, demux_list, demux_count);
91447636
A
2037 if (retval) {
2038 dlil_write_end();
91447636
A
2039 return retval;
2040 }
2041
2042 /*
2043 * We can't fail from this point on.
2044 * Increment the number of uses (protocol attachments + interface attached).
2045 */
2046 ifp_use(ifp, kIfNetUseCount_MustNotBeZero);
2047
2048 /*
2049 * Insert the protocol in the hash
2050 */
2051 {
2052 struct if_proto* prev_proto = SLIST_FIRST(&ifp->if_proto_hash[hash_value]);
2053 while (prev_proto && SLIST_NEXT(prev_proto, next_hash) != NULL)
2054 prev_proto = SLIST_NEXT(prev_proto, next_hash);
2055 if (prev_proto)
2056 SLIST_INSERT_AFTER(prev_proto, proto, next_hash);
2057 else
2058 SLIST_INSERT_HEAD(&ifp->if_proto_hash[hash_value], proto, next_hash);
2059 }
1c79356b 2060
91447636
A
2061 /*
2062 * Add to if_proto list for this interface
2063 */
2064 if_proto_ref(proto);
91447636
A
2065 dlil_write_end();
2066
2067 /* the reserved field carries the number of protocol still attached (subject to change) */
2068 ev_pr_data.proto_family = proto->protocol_family;
2069 ev_pr_data.proto_remaining_count = dlil_ifp_proto_count(ifp);
2070 dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_PROTO_ATTACHED,
2071 (struct net_event_data *)&ev_pr_data,
2072 sizeof(struct kev_dl_proto_data));
2d21ac55
A
2073#if 0
2074 DLIL_PRINTF("dlil. Attached protocol %d to %s%d - %d\n", proto->protocol_family,
91447636 2075 ifp->if_name, ifp->if_unit, retval);
2d21ac55 2076#endif
91447636
A
2077 return retval;
2078}
0b4e3aa0 2079
2d21ac55
A
2080errno_t
2081ifnet_attach_protocol(ifnet_t ifp, protocol_family_t protocol,
91447636
A
2082 const struct ifnet_attach_proto_param *proto_details)
2083{
2084 int retval = 0;
2085 struct if_proto *ifproto = NULL;
2086
2d21ac55
A
2087 if (ifp == NULL || protocol == 0 || proto_details == NULL)
2088 return EINVAL;
2089
91447636
A
2090 ifproto = _MALLOC(sizeof(struct if_proto), M_IFADDR, M_WAITOK);
2091 if (ifproto == 0) {
2d21ac55 2092 DLIL_PRINTF("ERROR - dlil failed if_proto allocation\n");
91447636
A
2093 retval = ENOMEM;
2094 goto end;
2095 }
2096 bzero(ifproto, sizeof(*ifproto));
2097
2098 ifproto->ifp = ifp;
2099 ifproto->protocol_family = protocol;
2100 ifproto->proto_kpi = kProtoKPI_v1;
2101 ifproto->kpi.v1.input = proto_details->input;
2102 ifproto->kpi.v1.pre_output = proto_details->pre_output;
2103 ifproto->kpi.v1.event = proto_details->event;
2104 ifproto->kpi.v1.ioctl = proto_details->ioctl;
2105 ifproto->kpi.v1.detached = proto_details->detached;
2106 ifproto->kpi.v1.resolve_multi = proto_details->resolve;
2107 ifproto->kpi.v1.send_arp = proto_details->send_arp;
2108
2d21ac55 2109 retval = dlil_attach_protocol_internal(ifproto,
91447636
A
2110 proto_details->demux_list, proto_details->demux_count);
2111
9bccf70c 2112end:
91447636
A
2113 if (retval && ifproto)
2114 FREE(ifproto, M_IFADDR);
2115 return retval;
1c79356b
A
2116}
2117
2d21ac55
A
2118errno_t
2119ifnet_attach_protocol_v2(ifnet_t ifp, protocol_family_t protocol,
2120 const struct ifnet_attach_proto_param_v2 *proto_details)
91447636 2121{
2d21ac55 2122 int retval = 0;
91447636 2123 struct if_proto *ifproto = NULL;
91447636 2124
2d21ac55 2125 if (ifp == NULL || protocol == 0 || proto_details == NULL)
91447636 2126 return EINVAL;
2d21ac55 2127
91447636 2128 ifproto = _MALLOC(sizeof(struct if_proto), M_IFADDR, M_WAITOK);
2d21ac55
A
2129 if (ifproto == 0) {
2130 DLIL_PRINTF("ERROR - dlil failed if_proto allocation\n");
91447636
A
2131 retval = ENOMEM;
2132 goto end;
2133 }
2d21ac55 2134 bzero(ifproto, sizeof(*ifproto));
91447636 2135
2d21ac55
A
2136 ifproto->ifp = ifp;
2137 ifproto->protocol_family = protocol;
2138 ifproto->proto_kpi = kProtoKPI_v2;
2139 ifproto->kpi.v2.input = proto_details->input;
2140 ifproto->kpi.v2.pre_output = proto_details->pre_output;
2141 ifproto->kpi.v2.event = proto_details->event;
2142 ifproto->kpi.v2.ioctl = proto_details->ioctl;
2143 ifproto->kpi.v2.detached = proto_details->detached;
2144 ifproto->kpi.v2.resolve_multi = proto_details->resolve;
2145 ifproto->kpi.v2.send_arp = proto_details->send_arp;
2146
2147 retval = dlil_attach_protocol_internal(ifproto,
2148 proto_details->demux_list, proto_details->demux_count);
91447636
A
2149
2150end:
2151 if (retval && ifproto)
2152 FREE(ifproto, M_IFADDR);
2153 return retval;
2154}
1c79356b 2155
91447636 2156extern void if_rtproto_del(struct ifnet *ifp, int protocol);
1c79356b 2157
91447636
A
2158static int
2159dlil_detach_protocol_internal(
2160 struct if_proto *proto)
2161{
2162 struct ifnet *ifp = proto->ifp;
2163 u_long proto_family = proto->protocol_family;
2164 struct kev_dl_proto_data ev_pr_data;
2165
2d21ac55 2166 if (proto->proto_kpi == kProtoKPI_v1) {
91447636
A
2167 if (proto->kpi.v1.detached)
2168 proto->kpi.v1.detached(ifp, proto->protocol_family);
2169 }
2d21ac55
A
2170 if (proto->proto_kpi == kProtoKPI_v2) {
2171 if (proto->kpi.v2.detached)
2172 proto->kpi.v2.detached(ifp, proto->protocol_family);
2173 }
91447636
A
2174 if_proto_free(proto);
2175
2176 /*
2177 * Cleanup routes that may still be in the routing table for that interface/protocol pair.
2178 */
2179
2180 if_rtproto_del(ifp, proto_family);
2181
2182 /* the reserved field carries the number of protocol still attached (subject to change) */
2183 ev_pr_data.proto_family = proto_family;
2184 ev_pr_data.proto_remaining_count = dlil_ifp_proto_count(ifp);
2185 dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_PROTO_DETACHED,
2186 (struct net_event_data *)&ev_pr_data,
2187 sizeof(struct kev_dl_proto_data));
2188 return 0;
2189}
1c79356b 2190
2d21ac55
A
2191errno_t
2192ifnet_detach_protocol(ifnet_t ifp, protocol_family_t proto_family)
91447636
A
2193{
2194 struct if_proto *proto = NULL;
2195 int retval = 0;
2196 int use_reached_zero = 0;
2197
2d21ac55 2198 if (ifp == NULL || proto_family == 0) return EINVAL;
1c79356b 2199
91447636 2200 if ((retval = dlil_write_begin()) != 0) {
91447636
A
2201 if (retval == EDEADLK) {
2202 retval = 0;
2203 dlil_read_begin();
2204 proto = find_attached_proto(ifp, proto_family);
2205 if (proto == 0) {
2206 retval = ENXIO;
2207 }
2208 else {
2209 proto->detaching = 1;
2210 dlil_detach_waiting = 1;
2211 wakeup(&dlil_detach_waiting);
2212 }
2213 dlil_read_end();
2214 }
2215 goto end;
2216 }
2217
2218 proto = find_attached_proto(ifp, proto_family);
2219
2220 if (proto == NULL) {
2221 retval = ENXIO;
2222 dlil_write_end();
2223 goto end;
2224 }
2225
2226 /*
2227 * Call family module del_proto
2228 */
2229
2230 if (ifp->if_del_proto)
2231 ifp->if_del_proto(ifp, proto->protocol_family);
1c79356b 2232
91447636
A
2233 SLIST_REMOVE(&ifp->if_proto_hash[proto_hash_value(proto_family)], proto, if_proto, next_hash);
2234
2235 /*
2236 * We can do the rest of the work outside of the write lock.
2237 */
2238 use_reached_zero = ifp_unuse(ifp);
2239 dlil_write_end();
2240
2241 dlil_detach_protocol_internal(proto);
2242
2243 /*
2244 * Only handle the case where the interface will go away after
2245 * we've sent the message. This way post message can send the
2246 * message to the interface safely.
2247 */
2248
2249 if (use_reached_zero)
2250 ifp_use_reached_zero(ifp);
2251
2252end:
2253 return retval;
2254}
1c79356b 2255
91447636
A
2256/*
2257 * dlil_delayed_detach_thread is responsible for detaching
2258 * protocols, protocol filters, and interface filters after
2259 * an attempt was made to detach one of those items while
2260 * it was not safe to do so (i.e. called dlil_read_begin).
2261 *
2262 * This function will take the dlil write lock and walk
2263 * through each of the interfaces looking for items with
2264 * the detaching flag set. When an item is found, it is
2265 * detached from the interface and placed on a local list.
2266 * After all of the items have been collected, we drop the
2267 * write lock and performed the post detach. This is done
2268 * so we only have to take the write lock once.
2269 *
2270 * When detaching a protocol filter, if we find that we
2271 * have detached the very last protocol and we need to call
2272 * ifp_use_reached_zero, we have to break out of our work
2273 * to drop the write lock so we can call ifp_use_reached_zero.
2274 */
2275
2276static void
2277dlil_delayed_detach_thread(__unused void* foo, __unused wait_result_t wait)
2278{
2279 thread_t self = current_thread();
2280 int asserted = 0;
0b4e3aa0 2281
91447636
A
2282 ml_thread_policy(self, MACHINE_GROUP,
2283 (MACHINE_NETWORK_GROUP|MACHINE_NETWORK_NETISR));
9bccf70c 2284
91447636
A
2285
2286 while (1) {
2287 if (dlil_detach_waiting != 0 && dlil_write_begin() == 0) {
2288 struct ifnet *ifp;
2289 struct proto_hash_entry detached_protos;
2290 struct ifnet_filter_head detached_filters;
2291 struct if_proto *proto;
2292 struct if_proto *next_proto;
2293 struct ifnet_filter *filt;
2294 struct ifnet_filter *next_filt;
2295 int reached_zero;
2296
2297 reached_zero = 0;
2298
2299 /* Clear the detach waiting flag */
2300 dlil_detach_waiting = 0;
2301 TAILQ_INIT(&detached_filters);
2302 SLIST_INIT(&detached_protos);
2303
2304 ifnet_head_lock_shared();
2305 TAILQ_FOREACH(ifp, &ifnet_head, if_link) {
2306 int i;
2307
2308 // Look for protocols and protocol filters
2309 for (i = 0; i < PROTO_HASH_SLOTS && !reached_zero; i++) {
2310 struct if_proto **prev_nextptr = &SLIST_FIRST(&ifp->if_proto_hash[i]);
2311 for (proto = *prev_nextptr; proto; proto = *prev_nextptr) {
2312
2313 // Detach this protocol
2314 if (proto->detaching) {
2315 if (ifp->if_del_proto)
2316 ifp->if_del_proto(ifp, proto->protocol_family);
91447636
A
2317 *prev_nextptr = SLIST_NEXT(proto, next_hash);
2318 SLIST_INSERT_HEAD(&detached_protos, proto, next_hash);
2319 reached_zero = ifp_unuse(ifp);
2320 if (reached_zero) {
2321 break;
2322 }
2323 }
2324 else {
2325 // Update prev_nextptr to point to our next ptr
2326 prev_nextptr = &SLIST_NEXT(proto, next_hash);
2327 }
2328 }
2329 }
2330
2331 // look for interface filters that need to be detached
2332 for (filt = TAILQ_FIRST(&ifp->if_flt_head); filt; filt = next_filt) {
2333 next_filt = TAILQ_NEXT(filt, filt_next);
2334 if (filt->filt_detaching != 0) {
2335 // take this interface filter off the interface filter list
2336 TAILQ_REMOVE(&ifp->if_flt_head, filt, filt_next);
2337
2338 // put this interface filter on the detached filters list
2339 TAILQ_INSERT_TAIL(&detached_filters, filt, filt_next);
2340 }
2341 }
2342
2343 if (ifp->if_delayed_detach) {
2344 ifp->if_delayed_detach = 0;
2345 reached_zero = ifp_unuse(ifp);
2346 }
2347
2348 if (reached_zero)
2349 break;
2350 }
2351 ifnet_head_done();
2352 dlil_write_end();
2353
2354 for (filt = TAILQ_FIRST(&detached_filters); filt; filt = next_filt) {
2355 next_filt = TAILQ_NEXT(filt, filt_next);
2356 /*
2357 * dlil_detach_filter_internal won't remove an item from
2358 * the list if it is already detached (second parameter).
2359 * The item will be freed though.
2360 */
2361 dlil_detach_filter_internal(filt, 1);
2362 }
2363
2364 for (proto = SLIST_FIRST(&detached_protos); proto; proto = next_proto) {
2365 next_proto = SLIST_NEXT(proto, next_hash);
2366 dlil_detach_protocol_internal(proto);
2367 }
2368
2369 if (reached_zero) {
2370 ifp_use_reached_zero(ifp);
2371 dlil_detach_waiting = 1; // we may have missed something
2372 }
2373 }
2374
2375 if (!asserted && dlil_detach_waiting == 0) {
2376 asserted = 1;
2377 assert_wait(&dlil_detach_waiting, THREAD_UNINT);
2378 }
2379
2380 if (dlil_detach_waiting == 0) {
2381 asserted = 0;
2382 thread_block(dlil_delayed_detach_thread);
2383 }
2384 }
2385}
9bccf70c 2386
91447636
A
2387static void
2388dlil_call_delayed_detach_thread(void) {
2389 dlil_delayed_detach_thread(NULL, THREAD_RESTART);
2390}
9bccf70c 2391
91447636
A
2392extern int if_next_index(void);
2393
2d21ac55
A
2394errno_t
2395ifnet_attach(
2396 ifnet_t ifp,
91447636
A
2397 const struct sockaddr_dl *ll_addr)
2398{
2d21ac55 2399 u_long interface_family;
91447636
A
2400 struct ifnet *tmp_if;
2401 struct proto_hash_entry *new_proto_list = NULL;
2402 int locked = 0;
2403
2d21ac55
A
2404 if (ifp == NULL) return EINVAL;
2405 if (ll_addr && ifp->if_addrlen == 0) {
2406 ifp->if_addrlen = ll_addr->sdl_alen;
2407 }
2408 else if (ll_addr && ll_addr->sdl_alen != ifp->if_addrlen) {
2409 return EINVAL;
2410 }
2411
2412 interface_family = ifp->if_family;
91447636
A
2413
2414 ifnet_head_lock_shared();
1c79356b 2415
91447636
A
2416 /* Verify we aren't already on the list */
2417 TAILQ_FOREACH(tmp_if, &ifnet_head, if_link) {
2418 if (tmp_if == ifp) {
2419 ifnet_head_done();
2420 return EEXIST;
2421 }
2422 }
2423
2424 ifnet_head_done();
2425
2426 if ((ifp->if_eflags & IFEF_REUSE) == 0 || ifp->if_lock == 0)
2427#if IFNET_RW_LOCK
2428 ifp->if_lock = lck_rw_alloc_init(ifnet_lock_group, ifnet_lock_attr);
2429#else
2430 ifp->if_lock = lck_mtx_alloc_init(ifnet_lock_group, ifnet_lock_attr);
2431#endif
0b4e3aa0 2432
91447636
A
2433 if (ifp->if_lock == 0) {
2434 return ENOMEM;
2435 }
1c79356b 2436
91447636
A
2437 /*
2438 * Allow interfaces withouth protocol families to attach
2439 * only if they have the necessary fields filled out.
2440 */
2441
2d21ac55
A
2442 if (ifp->if_add_proto == 0 || ifp->if_del_proto == 0) {
2443 DLIL_PRINTF("dlil Attempt to attach interface without family module - %ld\n",
91447636
A
2444 interface_family);
2445 return ENODEV;
2446 }
2447
2448 if ((ifp->if_eflags & IFEF_REUSE) == 0 || ifp->if_proto_hash == NULL) {
2449 MALLOC(new_proto_list, struct proto_hash_entry*, sizeof(struct proto_hash_entry) * PROTO_HASH_SLOTS,
2450 M_NKE, M_WAITOK);
1c79356b 2451
91447636
A
2452 if (new_proto_list == 0) {
2453 return ENOBUFS;
2454 }
1c79356b
A
2455 }
2456
91447636
A
2457 dlil_write_begin();
2458 locked = 1;
2459
91447636
A
2460 TAILQ_INIT(&ifp->if_flt_head);
2461
2462
2463 if (new_proto_list) {
2464 bzero(new_proto_list, (PROTO_HASH_SLOTS * sizeof(struct proto_hash_entry)));
2465 ifp->if_proto_hash = new_proto_list;
2d21ac55 2466 new_proto_list = NULL;
91447636
A
2467 }
2468
2469 /* old_if_attach */
2470 {
2d21ac55
A
2471 char workbuf[64];
2472 int namelen, masklen, socksize, ifasize;
2473 struct ifaddr *ifa = NULL;
91447636
A
2474
2475 if (ifp->if_snd.ifq_maxlen == 0)
2476 ifp->if_snd.ifq_maxlen = ifqmaxlen;
2477 TAILQ_INIT(&ifp->if_prefixhead);
2478 LIST_INIT(&ifp->if_multiaddrs);
2479 ifnet_touch_lastchange(ifp);
2480
2481 /* usecount to track attachment to the ifnet list */
2482 ifp_use(ifp, kIfNetUseCount_MayBeZero);
2483
2484 /* Lock the list of interfaces */
2485 ifnet_head_lock_exclusive();
2486 ifnet_lock_exclusive(ifp);
2487
2d21ac55 2488 if ((ifp->if_eflags & IFEF_REUSE) == 0 || ifp->if_index == 0)
91447636 2489 ifp->if_index = if_next_index();
2d21ac55
A
2490 else
2491 ifa = TAILQ_FIRST(&ifp->if_addrhead);
2492
2493 namelen = snprintf(workbuf, sizeof(workbuf), "%s%d", ifp->if_name, ifp->if_unit);
91447636 2494#define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
2d21ac55
A
2495 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
2496 socksize = masklen + ifp->if_addrlen;
91447636 2497#define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
2d21ac55
A
2498 if ((u_long)socksize < sizeof(struct sockaddr_dl))
2499 socksize = sizeof(struct sockaddr_dl);
2500 socksize = ROUNDUP(socksize);
2501 ifasize = sizeof(struct ifaddr) + 2 * socksize;
2502
2503 /*
2504 * Allocate a new ifa if we don't have one
2505 * or the old one is too small.
2506 */
2507 if (ifa == NULL || socksize > ifa->ifa_addr->sa_len) {
2508 if (ifa)
2509 if_detach_ifa(ifp, ifa);
91447636 2510 ifa = (struct ifaddr*)_MALLOC(ifasize, M_IFADDR, M_WAITOK);
91447636
A
2511 }
2512
2d21ac55
A
2513 if (ifa) {
2514 struct sockaddr_dl *sdl = (struct sockaddr_dl *)(ifa + 1);
2515 ifnet_addrs[ifp->if_index - 1] = ifa;
2516 bzero(ifa, ifasize);
2517 sdl->sdl_len = socksize;
2518 sdl->sdl_family = AF_LINK;
2519 bcopy(workbuf, sdl->sdl_data, namelen);
2520 sdl->sdl_nlen = namelen;
2521 sdl->sdl_index = ifp->if_index;
2522 sdl->sdl_type = ifp->if_type;
2523 if (ll_addr) {
2524 sdl->sdl_alen = ll_addr->sdl_alen;
2525 if (ll_addr->sdl_alen != ifp->if_addrlen)
2526 panic("ifnet_attach - ll_addr->sdl_alen != ifp->if_addrlen");
2527 bcopy(CONST_LLADDR(ll_addr), LLADDR(sdl), sdl->sdl_alen);
2528 }
2529 ifa->ifa_ifp = ifp;
2530 ifa->ifa_rtrequest = link_rtrequest;
2531 ifa->ifa_addr = (struct sockaddr*)sdl;
2532 sdl = (struct sockaddr_dl*)(socksize + (caddr_t)sdl);
2533 ifa->ifa_netmask = (struct sockaddr*)sdl;
2534 sdl->sdl_len = masklen;
2535 while (namelen != 0)
2536 sdl->sdl_data[--namelen] = 0xff;
2537 }
1c79356b 2538
91447636
A
2539 TAILQ_INIT(&ifp->if_addrhead);
2540 ifa = ifnet_addrs[ifp->if_index - 1];
2541
2542 if (ifa) {
2543 /*
2544 * We don't use if_attach_ifa because we want
2545 * this address to be first on the list.
2546 */
2547 ifaref(ifa);
2548 ifa->ifa_debug |= IFA_ATTACHED;
2549 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
1c79356b 2550 }
2d21ac55
A
2551#if CONFIG_MACF_NET
2552 mac_ifnet_label_associate(ifp);
2553#endif
91447636
A
2554
2555 TAILQ_INSERT_TAIL(&ifnet_head, ifp, if_link);
2556 ifindex2ifnet[ifp->if_index] = ifp;
2557
2558 ifnet_head_done();
1c79356b 2559 }
2d21ac55
A
2560
2561 /*
2562 * A specific dlil input thread is created per Ethernet interface.
2563 * pseudo interfaces or other types of interfaces use the main ("loopback") thread.
2564 * If the sysctl "net.link.generic.system.multi_threaded_input" is set to zero, all packets will
2565 * be handled by the main loopback thread, reverting to 10.4.x behaviour.
2566 *
2567 */
2568
2569 if (ifp->if_type == IFT_ETHER) {
2570 int err;
2571
2572 if (dlil_multithreaded_input > 0) {
2573 ifp->if_input_thread = _MALLOC(sizeof(struct dlil_threading_info), M_NKE, M_WAITOK);
2574 if (ifp->if_input_thread == NULL)
2575 panic("ifnet_attach ifp=%p couldn't alloc threading\n", ifp);
2576 if ((err = dlil_create_input_thread(ifp, ifp->if_input_thread)) != 0)
2577 panic("ifnet_attach ifp=%p couldn't get a thread. err=%x\n", ifp, err);
2578#ifdef DLIL_DEBUG
2579 printf("ifnet_attach: dlil thread for ifp=%p if_index=%x\n", ifp, ifp->if_index);
2580#endif
91447636
A
2581 }
2582 }
2d21ac55 2583 dlil_write_end();
0c530ab8 2584 ifnet_lock_done(ifp);
2d21ac55
A
2585
2586 dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_ATTACHED, NULL, 0);
1c79356b 2587
91447636 2588 return 0;
1c79356b
A
2589}
2590
2d21ac55
A
2591errno_t
2592ifnet_detach(
2593 ifnet_t ifp)
1c79356b 2594{
91447636
A
2595 struct ifnet_filter *filter;
2596 struct ifnet_filter *filter_next;
2597 int zeroed = 0;
2598 int retval = 0;
2599 struct ifnet_filter_head fhead;
2d21ac55 2600 struct dlil_threading_info *inputthread;
55e303ae 2601
2d21ac55 2602 if (ifp == NULL) return EINVAL;
55e303ae 2603
91447636 2604 ifnet_lock_exclusive(ifp);
55e303ae 2605
91447636
A
2606 if ((ifp->if_eflags & IFEF_DETACHING) != 0) {
2607 /* Interface has already been detached */
2608 ifnet_lock_done(ifp);
2609 return ENXIO;
55e303ae
A
2610 }
2611
91447636
A
2612 /*
2613 * Indicate this interface is being detached.
2614 *
2615 * This should prevent protocols from attaching
2616 * from this point on. Interface will remain on
2617 * the list until all of the protocols are detached.
2618 */
2619 ifp->if_eflags |= IFEF_DETACHING;
2620 ifnet_lock_done(ifp);
55e303ae 2621
2d21ac55
A
2622 dlil_post_msg(ifp, KEV_DL_SUBCLASS, KEV_DL_IF_DETACHING, NULL, 0);
2623
2624 /* Let BPF know we're detaching */
2625 bpfdetach(ifp);
55e303ae 2626
91447636
A
2627 if ((retval = dlil_write_begin()) != 0) {
2628 if (retval == EDEADLK) {
2d21ac55 2629 retval = 0;
91447636
A
2630
2631 /* We need to perform a delayed detach */
2632 ifp->if_delayed_detach = 1;
2633 dlil_detach_waiting = 1;
2634 wakeup(&dlil_detach_waiting);
2635 }
2636 return retval;
55e303ae
A
2637 }
2638
91447636
A
2639 /* Steal the list of interface filters */
2640 fhead = ifp->if_flt_head;
2641 TAILQ_INIT(&ifp->if_flt_head);
2d21ac55 2642
91447636
A
2643 /* unuse the interface */
2644 zeroed = ifp_unuse(ifp);
1c79356b 2645
2d21ac55
A
2646 /*
2647 * If thread affinity was set for the workloop thread, we will need
2648 * to tear down the affinity and release the extra reference count
2649 * taken at attach time;
2650 */
2651 if ((inputthread = ifp->if_input_thread) != NULL) {
2652 if (inputthread->net_affinity) {
2653 struct thread *tp;
2654
2655 if (inputthread == dlil_lo_thread_ptr)
2656 panic("Thread affinity should not be enabled "
2657 "on the loopback dlil input thread\n");
2658
2659 lck_mtx_lock(inputthread->input_lck);
2660 tp = inputthread->workloop_thread;
2661 inputthread->workloop_thread = NULL;
2662 inputthread->tag = 0;
2663 inputthread->net_affinity = FALSE;
2664 lck_mtx_unlock(inputthread->input_lck);
2665
2666 /* Tear down workloop thread affinity */
2667 if (tp != NULL) {
2668 (void) dlil_affinity_set(tp,
2669 THREAD_AFFINITY_TAG_NULL);
2670 thread_deallocate(tp);
2671 }
1c79356b 2672
2d21ac55
A
2673 /* Tear down dlil input thread affinity */
2674 tp = inputthread->input_thread;
2675 (void) dlil_affinity_set(tp, THREAD_AFFINITY_TAG_NULL);
2676 thread_deallocate(tp);
9bccf70c 2677 }
1c79356b 2678
2d21ac55
A
2679 /* cleanup ifp dlil input thread, if any */
2680 ifp->if_input_thread = NULL;
55e303ae 2681
2d21ac55
A
2682 if (inputthread != dlil_lo_thread_ptr) {
2683#ifdef DLIL_DEBUG
2684 printf("ifnet_detach: wakeup thread threadinfo: %p "
2685 "input_thread=%p threads: cur=%d max=%d\n",
2686 inputthread, inputthread->input_thread,
2687 dlil_multithreaded_input, cur_dlil_input_threads);
2688#endif
2689 lck_mtx_lock(inputthread->input_lck);
55e303ae 2690
2d21ac55
A
2691 inputthread->input_waiting |= DLIL_INPUT_TERMINATE;
2692 if ((inputthread->input_waiting & DLIL_INPUT_RUNNING) == 0) {
2693 wakeup((caddr_t)&inputthread->input_waiting);
2694 }
2695 lck_mtx_unlock(inputthread->input_lck);
91447636 2696 }
55e303ae 2697 }
2d21ac55
A
2698 dlil_write_end();
2699
2700 for (filter = TAILQ_FIRST(&fhead); filter; filter = filter_next) {
2701 filter_next = TAILQ_NEXT(filter, filt_next);
2702 dlil_detach_filter_internal(filter, 1);
55e303ae 2703 }
91447636 2704
2d21ac55
A
2705 if (zeroed != 0) {
2706 ifp_use_reached_zero(ifp);
55e303ae 2707 }
2d21ac55
A
2708
2709 return retval;
1c79356b 2710}
9bccf70c 2711
91447636
A
2712static errno_t
2713dlil_recycle_ioctl(
2714 __unused ifnet_t ifnet_ptr,
2715 __unused u_int32_t ioctl_code,
2716 __unused void *ioctl_arg)
9bccf70c 2717{
9bccf70c
A
2718 return EOPNOTSUPP;
2719}
2720
91447636
A
2721static int
2722dlil_recycle_output(
2723 __unused struct ifnet *ifnet_ptr,
2724 struct mbuf *m)
9bccf70c 2725{
9bccf70c
A
2726 m_freem(m);
2727 return 0;
2728}
2729
91447636
A
2730static void
2731dlil_recycle_free(
2732 __unused ifnet_t ifnet_ptr)
9bccf70c 2733{
9bccf70c
A
2734}
2735
91447636
A
2736static errno_t
2737dlil_recycle_set_bpf_tap(
2738 __unused ifnet_t ifp,
2739 __unused bpf_tap_mode mode,
2740 __unused bpf_packet_func callback)
9bccf70c
A
2741{
2742 /* XXX not sure what to do here */
2743 return 0;
2744}
2745
2d21ac55 2746__private_extern__
91447636
A
2747int dlil_if_acquire(
2748 u_long family,
2749 const void *uniqueid,
2750 size_t uniqueid_len,
2751 struct ifnet **ifp)
9bccf70c
A
2752{
2753 struct ifnet *ifp1 = NULL;
2754 struct dlil_ifnet *dlifp1 = NULL;
91447636 2755 int ret = 0;
9bccf70c 2756
91447636 2757 lck_mtx_lock(dlil_ifnet_mutex);
9bccf70c
A
2758 TAILQ_FOREACH(dlifp1, &dlil_ifnet_head, dl_if_link) {
2759
2760 ifp1 = (struct ifnet *)dlifp1;
2761
2762 if (ifp1->if_family == family) {
2763
2764 /* same uniqueid and same len or no unique id specified */
2765 if ((uniqueid_len == dlifp1->if_uniqueid_len)
2766 && !bcmp(uniqueid, dlifp1->if_uniqueid, uniqueid_len)) {
2767
2768 /* check for matching interface in use */
2769 if (ifp1->if_eflags & IFEF_INUSE) {
2770 if (uniqueid_len) {
2771 ret = EBUSY;
2772 goto end;
2773 }
2774 }
2775 else {
91447636
A
2776 if (!ifp1->if_lock)
2777 panic("ifp's lock is gone\n");
2778 ifnet_lock_exclusive(ifp1);
2779 ifp1->if_eflags |= (IFEF_INUSE | IFEF_REUSE);
2780 ifnet_lock_done(ifp1);
9bccf70c
A
2781 *ifp = ifp1;
2782 goto end;
2783 }
2784 }
2785 }
2786 }
2787
2788 /* no interface found, allocate a new one */
2789 MALLOC(dlifp1, struct dlil_ifnet *, sizeof(*dlifp1), M_NKE, M_WAITOK);
2790 if (dlifp1 == 0) {
2791 ret = ENOMEM;
2792 goto end;
2793 }
2794
2795 bzero(dlifp1, sizeof(*dlifp1));
2796
2797 if (uniqueid_len) {
2798 MALLOC(dlifp1->if_uniqueid, void *, uniqueid_len, M_NKE, M_WAITOK);
2799 if (dlifp1->if_uniqueid == 0) {
2800 FREE(dlifp1, M_NKE);
2801 ret = ENOMEM;
2802 goto end;
2803 }
2804 bcopy(uniqueid, dlifp1->if_uniqueid, uniqueid_len);
2805 dlifp1->if_uniqueid_len = uniqueid_len;
2806 }
2807
2808 ifp1 = (struct ifnet *)dlifp1;
2809 ifp1->if_eflags |= IFEF_INUSE;
91447636 2810 ifp1->if_name = dlifp1->if_namestorage;
2d21ac55
A
2811#if CONFIG_MACF_NET
2812 mac_ifnet_label_init(ifp1);
2813#endif
9bccf70c
A
2814
2815 TAILQ_INSERT_TAIL(&dlil_ifnet_head, dlifp1, dl_if_link);
2816
2817 *ifp = ifp1;
2818
2819end:
91447636 2820 lck_mtx_unlock(dlil_ifnet_mutex);
9bccf70c 2821
9bccf70c
A
2822 return ret;
2823}
2824
2d21ac55
A
2825__private_extern__ void
2826dlil_if_release(
2827 ifnet_t ifp)
9bccf70c
A
2828{
2829 struct dlil_ifnet *dlifp = (struct dlil_ifnet *)ifp;
9bccf70c 2830
91447636
A
2831 /* Interface does not have a lock until it is attached - radar 3713951 */
2832 if (ifp->if_lock)
2833 ifnet_lock_exclusive(ifp);
9bccf70c
A
2834 ifp->if_eflags &= ~IFEF_INUSE;
2835 ifp->if_ioctl = dlil_recycle_ioctl;
2836 ifp->if_output = dlil_recycle_output;
2837 ifp->if_free = dlil_recycle_free;
2838 ifp->if_set_bpf_tap = dlil_recycle_set_bpf_tap;
2839
2840 strncpy(dlifp->if_namestorage, ifp->if_name, IFNAMSIZ);
2841 ifp->if_name = dlifp->if_namestorage;
2d21ac55
A
2842#if CONFIG_MACF_NET
2843 /*
2844 * We can either recycle the MAC label here or in dlil_if_acquire().
2845 * It seems logical to do it here but this means that anything that
2846 * still has a handle on ifp will now see it as unlabeled.
2847 * Since the interface is "dead" that may be OK. Revisit later.
2848 */
2849 mac_ifnet_label_recycle(ifp);
2850#endif
91447636
A
2851 if (ifp->if_lock)
2852 ifnet_lock_done(ifp);
9bccf70c 2853
9bccf70c 2854}