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