forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
if_utun.c
2794 lines (2382 loc) · 74.4 KB
/
if_utun.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008-2017 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/* ----------------------------------------------------------------------------------
Application of kernel control for interface creation
Theory of operation:
utun (user tunnel) acts as glue between kernel control sockets and network interfaces.
This kernel control will register an interface for every client that connects.
---------------------------------------------------------------------------------- */
#include <sys/systm.h>
#include <sys/kern_control.h>
#include <net/kpi_protocol.h>
#include <net/kpi_interface.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_types.h>
#include <net/bpf.h>
#include <net/if_utun.h>
#include <sys/mbuf.h>
#include <sys/sockio.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet6/in6_var.h>
#include <netinet6/in6_var.h>
#include <sys/kauth.h>
#include <net/necp.h>
#include <kern/zalloc.h>
#define UTUN_NEXUS 0
extern unsigned int if_enable_netagent;
#if UTUN_NEXUS
static nexus_controller_t utun_ncd;
static int utun_ncd_refcount;
static uuid_t utun_kpipe_uuid;
static uuid_t utun_nx_dom_prov;
typedef struct utun_nx {
uuid_t if_provider;
uuid_t if_instance;
uuid_t ms_provider;
uuid_t ms_instance;
uuid_t ms_device;
uuid_t ms_host;
uuid_t ms_agent;
} *utun_nx_t;
#endif // UTUN_NEXUS
/* Control block allocated for each kernel control connection */
struct utun_pcb {
TAILQ_ENTRY(utun_pcb) utun_chain;
kern_ctl_ref utun_ctlref;
ifnet_t utun_ifp;
u_int32_t utun_unit;
u_int32_t utun_unique_id;
u_int32_t utun_flags;
int utun_ext_ifdata_stats;
u_int32_t utun_max_pending_packets;
char utun_if_xname[IFXNAMSIZ];
char utun_unique_name[IFXNAMSIZ];
// PCB lock protects state fields and rings
decl_lck_rw_data(, utun_pcb_lock);
struct mbuf * utun_input_chain;
struct mbuf * utun_input_chain_last;
// Input chain lock protects the list of input mbufs
// The input chain lock must be taken AFTER the PCB lock if both are held
lck_mtx_t utun_input_chain_lock;
bool utun_output_disabled;
#if UTUN_NEXUS
struct utun_nx utun_nx;
int utun_kpipe_enabled;
uuid_t utun_kpipe_uuid;
void * utun_kpipe_rxring;
void * utun_kpipe_txring;
kern_nexus_t utun_netif_nexus;
void * utun_netif_rxring;
void * utun_netif_txring;
uint64_t utun_netif_txring_size;
#endif // UTUN_NEXUS
};
/* Kernel Control functions */
static errno_t utun_ctl_connect(kern_ctl_ref kctlref, struct sockaddr_ctl *sac,
void **unitinfo);
static errno_t utun_ctl_disconnect(kern_ctl_ref kctlref, u_int32_t unit,
void *unitinfo);
static errno_t utun_ctl_send(kern_ctl_ref kctlref, u_int32_t unit,
void *unitinfo, mbuf_t m, int flags);
static errno_t utun_ctl_getopt(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
int opt, void *data, size_t *len);
static errno_t utun_ctl_setopt(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
int opt, void *data, size_t len);
static void utun_ctl_rcvd(kern_ctl_ref kctlref, u_int32_t unit, void *unitinfo,
int flags);
/* Network Interface functions */
#if !UTUN_NEXUS
static void utun_start(ifnet_t interface);
static errno_t utun_framer(ifnet_t interface, mbuf_t *packet,
const struct sockaddr *dest, const char *desk_linkaddr,
const char *frame_type, u_int32_t *prepend_len, u_int32_t *postpend_len);
#endif // !UTUN_NEXUS
static errno_t utun_output(ifnet_t interface, mbuf_t data);
static errno_t utun_demux(ifnet_t interface, mbuf_t data, char *frame_header,
protocol_family_t *protocol);
static errno_t utun_add_proto(ifnet_t interface, protocol_family_t protocol,
const struct ifnet_demux_desc *demux_array,
u_int32_t demux_count);
static errno_t utun_del_proto(ifnet_t interface, protocol_family_t protocol);
static errno_t utun_ioctl(ifnet_t interface, u_long cmd, void *data);
static void utun_detached(ifnet_t interface);
/* Protocol handlers */
static errno_t utun_attach_proto(ifnet_t interface, protocol_family_t proto);
static errno_t utun_proto_input(ifnet_t interface, protocol_family_t protocol,
mbuf_t m, char *frame_header);
static errno_t utun_proto_pre_output(ifnet_t interface, protocol_family_t protocol,
mbuf_t *packet, const struct sockaddr *dest, void *route,
char *frame_type, char *link_layer_dest);
static errno_t utun_pkt_input (struct utun_pcb *pcb, mbuf_t m);
#if UTUN_NEXUS
#define UTUN_IF_DEFAULT_SLOT_SIZE 4096
#define UTUN_IF_DEFAULT_RING_SIZE 64
#define UTUN_IF_DEFAULT_TX_FSW_RING_SIZE 64
#define UTUN_IF_DEFAULT_RX_FSW_RING_SIZE 128
#define UTUN_IF_HEADROOM_SIZE 32
#define UTUN_IF_MIN_RING_SIZE 16
#define UTUN_IF_MAX_RING_SIZE 1024
static int sysctl_if_utun_ring_size SYSCTL_HANDLER_ARGS;
static int sysctl_if_utun_tx_fsw_ring_size SYSCTL_HANDLER_ARGS;
static int sysctl_if_utun_rx_fsw_ring_size SYSCTL_HANDLER_ARGS;
static int if_utun_ring_size = UTUN_IF_DEFAULT_RING_SIZE;
static int if_utun_tx_fsw_ring_size = UTUN_IF_DEFAULT_TX_FSW_RING_SIZE;
static int if_utun_rx_fsw_ring_size = UTUN_IF_DEFAULT_RX_FSW_RING_SIZE;
SYSCTL_DECL(_net_utun);
SYSCTL_NODE(_net, OID_AUTO, utun, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "UTun");
SYSCTL_PROC(_net_utun, OID_AUTO, ring_size, CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
&if_utun_ring_size, UTUN_IF_DEFAULT_RING_SIZE, &sysctl_if_utun_ring_size, "I", "");
SYSCTL_PROC(_net_utun, OID_AUTO, tx_fsw_ring_size, CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
&if_utun_tx_fsw_ring_size, UTUN_IF_DEFAULT_TX_FSW_RING_SIZE, &sysctl_if_utun_tx_fsw_ring_size, "I", "");
SYSCTL_PROC(_net_utun, OID_AUTO, rx_fsw_ring_size, CTLTYPE_INT | CTLFLAG_LOCKED | CTLFLAG_RW,
&if_utun_rx_fsw_ring_size, UTUN_IF_DEFAULT_RX_FSW_RING_SIZE, &sysctl_if_utun_rx_fsw_ring_size, "I", "");
static errno_t
utun_register_nexus(void);
static errno_t
utun_netif_prepare(__unused kern_nexus_t nexus, ifnet_t ifp);
static errno_t
utun_nexus_pre_connect(kern_nexus_provider_t nxprov,
proc_t p, kern_nexus_t nexus,
nexus_port_t nexus_port, kern_channel_t channel, void **ch_ctx);
static errno_t
utun_nexus_connected(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_t channel);
static void
utun_netif_pre_disconnect(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_t channel);
static void
utun_nexus_pre_disconnect(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_t channel);
static void
utun_nexus_disconnected(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_t channel);
static errno_t
utun_kpipe_ring_init(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_t channel, kern_channel_ring_t ring, boolean_t is_tx_ring,
void **ring_ctx);
static void
utun_kpipe_ring_fini(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_ring_t ring);
static errno_t
utun_kpipe_sync_tx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_ring_t ring, uint32_t flags);
static errno_t
utun_kpipe_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_ring_t ring, uint32_t flags);
#endif // UTUN_NEXUS
#define UTUN_DEFAULT_MTU 1500
#define UTUN_HEADER_SIZE(_pcb) (sizeof(u_int32_t) + (((_pcb)->utun_flags & UTUN_FLAGS_ENABLE_PROC_UUID) ? sizeof(uuid_t) : 0))
static kern_ctl_ref utun_kctlref;
static u_int32_t utun_family;
static lck_attr_t *utun_lck_attr;
static lck_grp_attr_t *utun_lck_grp_attr;
static lck_grp_t *utun_lck_grp;
static lck_mtx_t utun_lock;
TAILQ_HEAD(utun_list, utun_pcb) utun_head;
#define UTUN_PCB_ZONE_MAX 32
#define UTUN_PCB_ZONE_NAME "net.if_utun"
static unsigned int utun_pcb_size; /* size of zone element */
static struct zone *utun_pcb_zone; /* zone for utun_pcb */
#if UTUN_NEXUS
static int
sysctl_if_utun_ring_size SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
int value = if_utun_ring_size;
int error = sysctl_handle_int(oidp, &value, 0, req);
if (error || !req->newptr) {
return (error);
}
if (value < UTUN_IF_MIN_RING_SIZE ||
value > UTUN_IF_MAX_RING_SIZE) {
return (EINVAL);
}
if_utun_ring_size = value;
return (0);
}
static int
sysctl_if_utun_tx_fsw_ring_size SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
int value = if_utun_tx_fsw_ring_size;
int error = sysctl_handle_int(oidp, &value, 0, req);
if (error || !req->newptr) {
return (error);
}
if (value < UTUN_IF_MIN_RING_SIZE ||
value > UTUN_IF_MAX_RING_SIZE) {
return (EINVAL);
}
if_utun_tx_fsw_ring_size = value;
return (0);
}
static int
sysctl_if_utun_rx_fsw_ring_size SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
int value = if_utun_rx_fsw_ring_size;
int error = sysctl_handle_int(oidp, &value, 0, req);
if (error || !req->newptr) {
return (error);
}
if (value < UTUN_IF_MIN_RING_SIZE ||
value > UTUN_IF_MAX_RING_SIZE) {
return (EINVAL);
}
if_utun_rx_fsw_ring_size = value;
return (0);
}
static errno_t
utun_netif_ring_init(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_t channel, kern_channel_ring_t ring, boolean_t is_tx_ring,
void **ring_ctx)
{
#pragma unused(nxprov)
#pragma unused(channel)
#pragma unused(ring_ctx)
struct utun_pcb *pcb = kern_nexus_get_context(nexus);
if (!is_tx_ring) {
VERIFY(pcb->utun_netif_rxring == NULL);
pcb->utun_netif_rxring = ring;
} else {
VERIFY(pcb->utun_netif_txring == NULL);
pcb->utun_netif_txring = ring;
}
return 0;
}
static void
utun_netif_ring_fini(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_ring_t ring)
{
#pragma unused(nxprov)
struct utun_pcb *pcb = kern_nexus_get_context(nexus);
if (pcb->utun_netif_rxring == ring) {
pcb->utun_netif_rxring = NULL;
} else if (pcb->utun_netif_txring == ring) {
pcb->utun_netif_txring = NULL;
}
}
static errno_t
utun_netif_sync_tx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_ring_t tx_ring, uint32_t flags)
{
#pragma unused(nxprov)
#pragma unused(flags)
struct utun_pcb *pcb = kern_nexus_get_context(nexus);
struct netif_stats *nifs = &NX_NETIF_PRIVATE(nexus)->nif_stats;
lck_rw_lock_shared(&pcb->utun_pcb_lock);
struct kern_channel_ring_stat_increment tx_ring_stats;
bzero(&tx_ring_stats, sizeof(tx_ring_stats));
kern_channel_slot_t tx_pslot = NULL;
kern_channel_slot_t tx_slot = kern_channel_get_next_slot(tx_ring, NULL, NULL);
STATS_INC(nifs, NETIF_STATS_TXSYNC);
if (tx_slot == NULL) {
// Nothing to write, don't bother signalling
lck_rw_unlock_shared(&pcb->utun_pcb_lock);
return 0;
}
if (pcb->utun_kpipe_enabled) {
kern_channel_ring_t rx_ring = pcb->utun_kpipe_rxring;
lck_rw_unlock_shared(&pcb->utun_pcb_lock);
// Signal the kernel pipe ring to read
if (rx_ring != NULL) {
kern_channel_notify(rx_ring, 0);
}
return 0;
}
// If we're here, we're injecting into the utun kernel control socket
while (tx_slot != NULL) {
size_t length = 0;
mbuf_t data = NULL;
kern_packet_t tx_ph = kern_channel_slot_get_packet(tx_ring, tx_slot);
if (tx_ph == 0) {
// Advance TX ring
tx_pslot = tx_slot;
tx_slot = kern_channel_get_next_slot(tx_ring, tx_slot, NULL);
continue;
}
(void) kern_channel_slot_detach_packet(tx_ring, tx_slot, tx_ph);
// Advance TX ring
tx_pslot = tx_slot;
tx_slot = kern_channel_get_next_slot(tx_ring, tx_slot, NULL);
kern_buflet_t tx_buf = kern_packet_get_next_buflet(tx_ph, NULL);
VERIFY(tx_buf != NULL);
/* tx_baddr is the absolute buffer address */
uint8_t *tx_baddr = kern_buflet_get_object_address(tx_buf);
VERIFY(tx_baddr != 0);
bpf_tap_packet_out(pcb->utun_ifp, DLT_RAW, tx_ph, NULL, 0);
uint16_t tx_offset = kern_buflet_get_data_offset(tx_buf);
uint32_t tx_length = kern_buflet_get_data_length(tx_buf);
// The offset must be large enough for the headers
VERIFY(tx_offset >= UTUN_HEADER_SIZE(pcb));
// Find family
uint32_t af = 0;
uint8_t vhl = *(uint8_t *)(tx_baddr + tx_offset);
u_int ip_version = (vhl >> 4);
switch (ip_version) {
case 4: {
af = AF_INET;
break;
}
case 6: {
af = AF_INET6;
break;
}
default: {
printf("utun_netif_sync_tx %s: unknown ip version %u vhl %u tx_offset %u len %u header_size %zu\n",
pcb->utun_ifp->if_xname, ip_version, vhl, tx_offset, tx_length,
UTUN_HEADER_SIZE(pcb));
break;
}
}
tx_offset -= UTUN_HEADER_SIZE(pcb);
tx_length += UTUN_HEADER_SIZE(pcb);
tx_baddr += tx_offset;
length = MIN(tx_length, UTUN_IF_DEFAULT_SLOT_SIZE);
// Copy in family
memcpy(tx_baddr, &af, sizeof(af));
if (pcb->utun_flags & UTUN_FLAGS_ENABLE_PROC_UUID) {
kern_packet_get_euuid(tx_ph, (void *)(tx_baddr + sizeof(af)));
}
if (length > 0) {
errno_t error = mbuf_gethdr(MBUF_DONTWAIT, MBUF_TYPE_HEADER, &data);
if (error == 0) {
error = mbuf_copyback(data, 0, length, tx_baddr, MBUF_DONTWAIT);
if (error == 0) {
error = utun_output(pcb->utun_ifp, data);
if (error != 0) {
printf("utun_netif_sync_tx %s - utun_output error %d\n", pcb->utun_ifp->if_xname, error);
}
} else {
printf("utun_netif_sync_tx %s - mbuf_copyback(%zu) error %d\n", pcb->utun_ifp->if_xname, length, error);
STATS_INC(nifs, NETIF_STATS_NOMEM_MBUF);
STATS_INC(nifs, NETIF_STATS_DROPPED);
mbuf_freem(data);
data = NULL;
}
} else {
printf("utun_netif_sync_tx %s - mbuf_gethdr error %d\n", pcb->utun_ifp->if_xname, error);
STATS_INC(nifs, NETIF_STATS_NOMEM_MBUF);
STATS_INC(nifs, NETIF_STATS_DROPPED);
}
} else {
printf("utun_netif_sync_tx %s - 0 length packet\n", pcb->utun_ifp->if_xname);
STATS_INC(nifs, NETIF_STATS_NOMEM_MBUF);
STATS_INC(nifs, NETIF_STATS_DROPPED);
}
kern_pbufpool_free(tx_ring->ckr_pp, tx_ph);
if (data == NULL) {
continue;
}
STATS_INC(nifs, NETIF_STATS_TXPKTS);
STATS_INC(nifs, NETIF_STATS_TXCOPY_MBUF);
tx_ring_stats.kcrsi_slots_transferred++;
tx_ring_stats.kcrsi_bytes_transferred += length;
}
if (tx_pslot) {
kern_channel_advance_slot(tx_ring, tx_pslot);
kern_channel_increment_ring_net_stats(tx_ring, pcb->utun_ifp, &tx_ring_stats);
(void)kern_channel_reclaim(tx_ring);
}
lck_rw_unlock_shared(&pcb->utun_pcb_lock);
return 0;
}
static errno_t
utun_netif_tx_doorbell(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_ring_t ring, __unused uint32_t flags)
{
#pragma unused(nxprov)
struct utun_pcb *pcb = kern_nexus_get_context(nexus);
lck_rw_lock_shared(&pcb->utun_pcb_lock);
boolean_t more = false;
errno_t rc = 0;
do {
/* Refill and sync the ring */
rc = kern_channel_tx_refill(ring, UINT32_MAX, UINT32_MAX, true, &more);
if (rc != 0 && rc != EAGAIN && rc != EBUSY) {
printf("%s, tx refill failed %d\n", __func__, rc);
}
} while ((rc == 0) && more);
if (pcb->utun_kpipe_enabled && !pcb->utun_output_disabled) {
uint32_t tx_available = kern_channel_available_slot_count(ring);
if (pcb->utun_netif_txring_size > 0 &&
tx_available >= pcb->utun_netif_txring_size - 1) {
// No room left in tx ring, disable output for now
errno_t error = ifnet_disable_output(pcb->utun_ifp);
if (error != 0) {
printf("utun_netif_tx_doorbell: ifnet_disable_output returned error %d\n", error);
} else {
pcb->utun_output_disabled = true;
}
}
}
if (pcb->utun_kpipe_enabled &&
(((rc != 0) && (rc != EAGAIN)) || pcb->utun_output_disabled)) {
kern_channel_ring_t rx_ring = pcb->utun_kpipe_rxring;
// Unlock while calling notify
lck_rw_unlock_shared(&pcb->utun_pcb_lock);
// Signal the kernel pipe ring to read
if (rx_ring != NULL) {
kern_channel_notify(rx_ring, 0);
}
} else {
lck_rw_unlock_shared(&pcb->utun_pcb_lock);
}
return (0);
}
static errno_t
utun_netif_sync_rx(kern_nexus_provider_t nxprov, kern_nexus_t nexus,
kern_channel_ring_t rx_ring, uint32_t flags)
{
#pragma unused(nxprov)
#pragma unused(flags)
struct utun_pcb *pcb = kern_nexus_get_context(nexus);
struct kern_channel_ring_stat_increment rx_ring_stats;
struct netif_stats *nifs = &NX_NETIF_PRIVATE(nexus)->nif_stats;
lck_rw_lock_shared(&pcb->utun_pcb_lock);
// Reclaim user-released slots
(void) kern_channel_reclaim(rx_ring);
STATS_INC(nifs, NETIF_STATS_RXSYNC);
uint32_t avail = kern_channel_available_slot_count(rx_ring);
if (avail == 0) {
lck_rw_unlock_shared(&pcb->utun_pcb_lock);
return 0;
}
struct kern_pbufpool *rx_pp = rx_ring->ckr_pp;
VERIFY(rx_pp != NULL);
bzero(&rx_ring_stats, sizeof(rx_ring_stats));
kern_channel_slot_t rx_pslot = NULL;
kern_channel_slot_t rx_slot = kern_channel_get_next_slot(rx_ring, NULL, NULL);
while (rx_slot != NULL) {
// Check for a waiting packet
lck_mtx_lock(&pcb->utun_input_chain_lock);
mbuf_t data = pcb->utun_input_chain;
if (data == NULL) {
lck_mtx_unlock(&pcb->utun_input_chain_lock);
break;
}
// Allocate rx packet
kern_packet_t rx_ph = 0;
errno_t error = kern_pbufpool_alloc_nosleep(rx_pp, 1, &rx_ph);
if (unlikely(error != 0)) {
STATS_INC(nifs, NETIF_STATS_NOMEM_PKT);
STATS_INC(nifs, NETIF_STATS_DROPPED);
printf("utun_netif_sync_rx %s: failed to allocate packet\n",
pcb->utun_ifp->if_xname);
lck_mtx_unlock(&pcb->utun_input_chain_lock);
break;
}
// Advance waiting packets
pcb->utun_input_chain = data->m_nextpkt;
data->m_nextpkt = NULL;
if (pcb->utun_input_chain == NULL) {
pcb->utun_input_chain_last = NULL;
}
lck_mtx_unlock(&pcb->utun_input_chain_lock);
size_t header_offset = UTUN_HEADER_SIZE(pcb);
size_t length = mbuf_pkthdr_len(data);
if (length < header_offset) {
// mbuf is too small
mbuf_freem(data);
kern_pbufpool_free(rx_pp, rx_ph);
STATS_INC(nifs, NETIF_STATS_BADLEN);
STATS_INC(nifs, NETIF_STATS_DROPPED);
printf("utun_netif_sync_rx %s: legacy packet length too short for header %zu < %zu\n",
pcb->utun_ifp->if_xname, length, header_offset);
continue;
}
length -= header_offset;
if (length > rx_pp->pp_buflet_size) {
// Flush data
mbuf_freem(data);
kern_pbufpool_free(rx_pp, rx_ph);
STATS_INC(nifs, NETIF_STATS_BADLEN);
STATS_INC(nifs, NETIF_STATS_DROPPED);
printf("utun_netif_sync_rx %s: legacy packet length %zu > %u\n",
pcb->utun_ifp->if_xname, length, rx_pp->pp_buflet_size);
continue;
}
mbuf_pkthdr_setrcvif(data, pcb->utun_ifp);
// Fillout rx packet
kern_buflet_t rx_buf = kern_packet_get_next_buflet(rx_ph, NULL);
VERIFY(rx_buf != NULL);
void *rx_baddr = kern_buflet_get_object_address(rx_buf);
VERIFY(rx_baddr != NULL);
// Copy-in data from mbuf to buflet
mbuf_copydata(data, header_offset, length, (void *)rx_baddr);
kern_packet_clear_flow_uuid(rx_ph); // Zero flow id
// Finalize and attach the packet
error = kern_buflet_set_data_offset(rx_buf, 0);
VERIFY(error == 0);
error = kern_buflet_set_data_length(rx_buf, length);
VERIFY(error == 0);
error = kern_packet_set_link_header_offset(rx_ph, 0);
VERIFY(error == 0);
error = kern_packet_set_network_header_offset(rx_ph, 0);
VERIFY(error == 0);
error = kern_packet_finalize(rx_ph);
VERIFY(error == 0);
error = kern_channel_slot_attach_packet(rx_ring, rx_slot, rx_ph);
VERIFY(error == 0);
STATS_INC(nifs, NETIF_STATS_RXPKTS);
STATS_INC(nifs, NETIF_STATS_RXCOPY_MBUF);
bpf_tap_packet_in(pcb->utun_ifp, DLT_RAW, rx_ph, NULL, 0);
rx_ring_stats.kcrsi_slots_transferred++;
rx_ring_stats.kcrsi_bytes_transferred += length;
mbuf_freem(data);
// Advance ring
rx_pslot = rx_slot;
rx_slot = kern_channel_get_next_slot(rx_ring, rx_slot, NULL);
}
struct kern_channel_ring_stat_increment tx_ring_stats;
bzero(&tx_ring_stats, sizeof(tx_ring_stats));
kern_channel_ring_t tx_ring = pcb->utun_kpipe_txring;
kern_channel_slot_t tx_pslot = NULL;
kern_channel_slot_t tx_slot = NULL;
if (tx_ring == NULL) {
// Net-If TX ring not set up yet, nothing to read
goto done;
}
// Unlock utun before entering ring
lck_rw_unlock_shared(&pcb->utun_pcb_lock);
(void)kr_enter(tx_ring, TRUE);
// Lock again after entering and validate
lck_rw_lock_shared(&pcb->utun_pcb_lock);
if (tx_ring != pcb->utun_kpipe_txring) {
goto done;
}
tx_slot = kern_channel_get_next_slot(tx_ring, NULL, NULL);
if (tx_slot == NULL) {
// Nothing to read, don't bother signalling
goto done;
}
while (rx_slot != NULL && tx_slot != NULL) {
// Allocate rx packet
kern_packet_t rx_ph = 0;
kern_packet_t tx_ph = kern_channel_slot_get_packet(tx_ring, tx_slot);
// Advance TX ring
tx_pslot = tx_slot;
tx_slot = kern_channel_get_next_slot(tx_ring, tx_slot, NULL);
/* Skip slot if packet is zero-length or marked as dropped (QUMF_DROPPED) */
if (tx_ph == 0) {
continue;
}
errno_t error = kern_pbufpool_alloc_nosleep(rx_pp, 1, &rx_ph);
if (unlikely(error != 0)) {
STATS_INC(nifs, NETIF_STATS_NOMEM_PKT);
STATS_INC(nifs, NETIF_STATS_DROPPED);
printf("utun_netif_sync_rx %s: failed to allocate packet\n",
pcb->utun_ifp->if_xname);
break;
}
kern_buflet_t tx_buf = kern_packet_get_next_buflet(tx_ph, NULL);
VERIFY(tx_buf != NULL);
uint8_t *tx_baddr = kern_buflet_get_object_address(tx_buf);
VERIFY(tx_baddr != 0);
tx_baddr += kern_buflet_get_data_offset(tx_buf);
// Check packet length
size_t header_offset = UTUN_HEADER_SIZE(pcb);
uint32_t tx_length = kern_packet_get_data_length(tx_ph);
if (tx_length < header_offset) {
// Packet is too small
kern_pbufpool_free(rx_pp, rx_ph);
STATS_INC(nifs, NETIF_STATS_BADLEN);
STATS_INC(nifs, NETIF_STATS_DROPPED);
printf("utun_netif_sync_rx %s: packet length too short for header %u < %zu\n",
pcb->utun_ifp->if_xname, tx_length, header_offset);
continue;
}
size_t length = MIN(tx_length - header_offset,
UTUN_IF_DEFAULT_SLOT_SIZE);
tx_ring_stats.kcrsi_slots_transferred++;
tx_ring_stats.kcrsi_bytes_transferred += length;
// Fillout rx packet
kern_buflet_t rx_buf = kern_packet_get_next_buflet(rx_ph, NULL);
VERIFY(rx_buf != NULL);
void *rx_baddr = kern_buflet_get_object_address(rx_buf);
VERIFY(rx_baddr != NULL);
// Copy-in data from tx to rx
memcpy((void *)rx_baddr, (void *)(tx_baddr + header_offset), length);
kern_packet_clear_flow_uuid(rx_ph); // Zero flow id
// Finalize and attach the packet
error = kern_buflet_set_data_offset(rx_buf, 0);
VERIFY(error == 0);
error = kern_buflet_set_data_length(rx_buf, length);
VERIFY(error == 0);
error = kern_packet_set_link_header_offset(rx_ph, 0);
VERIFY(error == 0);
error = kern_packet_set_network_header_offset(rx_ph, 0);
VERIFY(error == 0);
error = kern_packet_finalize(rx_ph);
VERIFY(error == 0);
error = kern_channel_slot_attach_packet(rx_ring, rx_slot, rx_ph);
VERIFY(error == 0);
STATS_INC(nifs, NETIF_STATS_RXPKTS);
STATS_INC(nifs, NETIF_STATS_RXCOPY_DIRECT);
bpf_tap_packet_in(pcb->utun_ifp, DLT_RAW, rx_ph, NULL, 0);
rx_ring_stats.kcrsi_slots_transferred++;
rx_ring_stats.kcrsi_bytes_transferred += length;
rx_pslot = rx_slot;
rx_slot = kern_channel_get_next_slot(rx_ring, rx_slot, NULL);
}
done:
if (rx_pslot) {
kern_channel_advance_slot(rx_ring, rx_pslot);
kern_channel_increment_ring_net_stats(rx_ring, pcb->utun_ifp, &rx_ring_stats);
}
if (tx_pslot) {
kern_channel_advance_slot(tx_ring, tx_pslot);
kern_channel_increment_ring_net_stats(tx_ring, pcb->utun_ifp, &tx_ring_stats);
(void)kern_channel_reclaim(tx_ring);
}
// Unlock first, then exit ring
lck_rw_unlock_shared(&pcb->utun_pcb_lock);
if (tx_ring != NULL) {
if (tx_pslot != NULL) {
kern_channel_notify(tx_ring, 0);
}
kr_exit(tx_ring);
}
return 0;
}
static errno_t
utun_nexus_ifattach(struct utun_pcb *pcb,
struct ifnet_init_eparams *init_params,
struct ifnet **ifp)
{
errno_t err;
nexus_controller_t controller = kern_nexus_shared_controller();
struct kern_nexus_net_init net_init;
nexus_name_t provider_name;
snprintf((char *)provider_name, sizeof(provider_name),
"com.apple.netif.utun%d", pcb->utun_unit);
struct kern_nexus_provider_init prov_init = {
.nxpi_version = KERN_NEXUS_DOMAIN_PROVIDER_CURRENT_VERSION,
.nxpi_flags = NXPIF_VIRTUAL_DEVICE,
.nxpi_pre_connect = utun_nexus_pre_connect,
.nxpi_connected = utun_nexus_connected,
.nxpi_pre_disconnect = utun_netif_pre_disconnect,
.nxpi_disconnected = utun_nexus_disconnected,
.nxpi_ring_init = utun_netif_ring_init,
.nxpi_ring_fini = utun_netif_ring_fini,
.nxpi_slot_init = NULL,
.nxpi_slot_fini = NULL,
.nxpi_sync_tx = utun_netif_sync_tx,
.nxpi_sync_rx = utun_netif_sync_rx,
.nxpi_tx_doorbell = utun_netif_tx_doorbell,
};
nexus_attr_t nxa = NULL;
err = kern_nexus_attr_create(&nxa);
if (err != 0) {
printf("%s: kern_nexus_attr_create failed: %d\n",
__func__, err);
goto failed;
}
uint64_t slot_buffer_size = UTUN_IF_DEFAULT_SLOT_SIZE;
err = kern_nexus_attr_set(nxa, NEXUS_ATTR_SLOT_BUF_SIZE, slot_buffer_size);
VERIFY(err == 0);
// Reset ring size for netif nexus to limit memory usage
uint64_t ring_size = if_utun_ring_size;
err = kern_nexus_attr_set(nxa, NEXUS_ATTR_TX_SLOTS, ring_size);
VERIFY(err == 0);
err = kern_nexus_attr_set(nxa, NEXUS_ATTR_RX_SLOTS, ring_size);
VERIFY(err == 0);
pcb->utun_netif_txring_size = ring_size;
err = kern_nexus_controller_register_provider(controller,
utun_nx_dom_prov,
provider_name,
&prov_init,
sizeof(prov_init),
nxa,
&pcb->utun_nx.if_provider);
if (err != 0) {
printf("%s register provider failed, error %d\n",
__func__, err);
goto failed;
}
bzero(&net_init, sizeof(net_init));
net_init.nxneti_version = KERN_NEXUS_NET_CURRENT_VERSION;
net_init.nxneti_flags = 0;
net_init.nxneti_eparams = init_params;
net_init.nxneti_lladdr = NULL;
net_init.nxneti_prepare = utun_netif_prepare;
err = kern_nexus_controller_alloc_net_provider_instance(controller,
pcb->utun_nx.if_provider,
pcb,
&pcb->utun_nx.if_instance,
&net_init,
ifp);
if (err != 0) {
printf("%s alloc_net_provider_instance failed, %d\n",
__func__, err);
kern_nexus_controller_deregister_provider(controller,
pcb->utun_nx.if_provider);
uuid_clear(pcb->utun_nx.if_provider);
goto failed;
}
failed:
if (nxa) {
kern_nexus_attr_destroy(nxa);
}
return (err);
}
static void
utun_detach_provider_and_instance(uuid_t provider, uuid_t instance)
{
nexus_controller_t controller = kern_nexus_shared_controller();
errno_t err;
if (!uuid_is_null(instance)) {
err = kern_nexus_controller_free_provider_instance(controller,
instance);
if (err != 0) {
printf("%s free_provider_instance failed %d\n",
__func__, err);
}
uuid_clear(instance);
}
if (!uuid_is_null(provider)) {
err = kern_nexus_controller_deregister_provider(controller,
provider);
if (err != 0) {
printf("%s deregister_provider %d\n", __func__, err);
}
uuid_clear(provider);
}
return;
}
static void
utun_nexus_detach(utun_nx_t nx)
{
nexus_controller_t controller = kern_nexus_shared_controller();
errno_t err;
if (!uuid_is_null(nx->ms_host)) {
err = kern_nexus_ifdetach(controller,
nx->ms_instance,
nx->ms_host);
if (err != 0) {
printf("%s: kern_nexus_ifdetach ms host failed %d\n",
__func__, err);
}
}
if (!uuid_is_null(nx->ms_device)) {
err = kern_nexus_ifdetach(controller,
nx->ms_instance,
nx->ms_device);
if (err != 0) {
printf("%s: kern_nexus_ifdetach ms device failed %d\n",
__func__, err);
}
}
utun_detach_provider_and_instance(nx->if_provider,
nx->if_instance);
utun_detach_provider_and_instance(nx->ms_provider,
nx->ms_instance);
memset(nx, 0, sizeof(*nx));
}
static errno_t
utun_create_fs_provider_and_instance(uint32_t subtype, const char *type_name,
const char *ifname,
uuid_t *provider, uuid_t *instance)
{
nexus_attr_t attr = NULL;
nexus_controller_t controller = kern_nexus_shared_controller();
uuid_t dom_prov;
errno_t err;
struct kern_nexus_init init;
nexus_name_t provider_name;
err = kern_nexus_get_builtin_domain_provider(NEXUS_TYPE_FLOW_SWITCH,
&dom_prov);
if (err != 0) {
printf("%s can't get %s provider, error %d\n",
__func__, type_name, err);
goto failed;
}
err = kern_nexus_attr_create(&attr);
if (err != 0) {
printf("%s: kern_nexus_attr_create failed: %d\n",
__func__, err);
goto failed;
}
err = kern_nexus_attr_set(attr, NEXUS_ATTR_EXTENSIONS, subtype);
VERIFY(err == 0);
uint64_t slot_buffer_size = UTUN_IF_DEFAULT_SLOT_SIZE;
err = kern_nexus_attr_set(attr, NEXUS_ATTR_SLOT_BUF_SIZE, slot_buffer_size);
VERIFY(err == 0);
// Reset ring size for flowswitch nexus to limit memory usage. Larger RX than netif.
uint64_t tx_ring_size = if_utun_tx_fsw_ring_size;
err = kern_nexus_attr_set(attr, NEXUS_ATTR_TX_SLOTS, tx_ring_size);
VERIFY(err == 0);
uint64_t rx_ring_size = if_utun_rx_fsw_ring_size;
err = kern_nexus_attr_set(attr, NEXUS_ATTR_RX_SLOTS, rx_ring_size);
VERIFY(err == 0);
snprintf((char *)provider_name, sizeof(provider_name),
"com.apple.%s.%s", type_name, ifname);
err = kern_nexus_controller_register_provider(controller,
dom_prov,
provider_name,
NULL,
0,
attr,
provider);
kern_nexus_attr_destroy(attr);