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