-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple.c
365 lines (308 loc) · 9.56 KB
/
simple.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
/*
*
*/
#include <assert.h>
#include <unistd.h>
#include <sys/uio.h>
#include <vmnet/vmnet.h>
#include <SystemConfiguration/SCNetworkConfiguration.h>
#include <stdio.h>
#include <time.h>
#include <CoreFoundation/CoreFoundation.h>
void fhexdump(unsigned int display_addr, void *in, int size, FILE *stream) {
uint8_t *p = in;
while(size>0) {
int i;
fprintf(stream, "%03x: ", display_addr);
for (i = 0; i < 16; i++) {
if (i < size) {
fprintf(stream, "%02x", p[i]);
} else {
fprintf(stream, " ");
}
if (i==7) {
fprintf(stream, " ");
} else {
fprintf(stream, " ");
}
}
fprintf(stream, " |");
for (i = 0; i < 16; i++) {
if (i < size) {
char ch = p[i];
if (ch>=0x20 && ch<=0x7e) {
fprintf(stream, "%c", ch);
} else {
fprintf(stream, " ");
}
}
}
fprintf(stream, "|\n");
size -= 16;
display_addr += 16;
p += 16;
}
}
/************************************************************************/
int tap_write(interface_ref vmnet_iface_ref, char *buf, int len) {
struct iovec iov;
iov.iov_base = buf;
iov.iov_len = len;
struct vmpktdesc v;
v.vm_pkt_size = len;
v.vm_pkt_iov = &iov;
v.vm_pkt_iovcnt = 1;
v.vm_flags = 0;
int pktcnt = 1;
vmnet_return_t result = vmnet_write(vmnet_iface_ref, &v, &pktcnt);
if (result != VMNET_SUCCESS || pktcnt != 1) {
printf("Failed to read packet from host: %i\n", result);
return -1;
}
return v.vm_pkt_size;
}
void send_dummy(interface_ref vmnet_iface_ref) {
/*
* Generate a dummy packet and transmit it
*
* Use purely for testing that sending packets works
*
*/
char dummy[] =
"\xff\xff\xff\xff\xff\xff" // eth dest
"\x02\x10\x20\x30\x40\x50" // eth src
"\x55\xaa" // eth proto
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int size = sizeof(dummy);
size = tap_write(vmnet_iface_ref, dummy, size);
if (size != sizeof(dummy)) {
printf("write error: size=%i\n", size);
}
}
void handle_rx_packet(interface_ref vmnet_iface_ref, char *buf, int len) {
if (len==0) {
return;
}
/* For debugging, dump the packet we got */
fhexdump(0, buf, len, stdout);
printf("\n");
/* For generating some reply traffic, send a dummy packet */
send_dummy(vmnet_iface_ref);
}
interface_ref tap_open(const char *ip4addr) {
operating_modes_t mode = VMNET_HOST_MODE;
xpc_object_t interface_desc = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_uint64(
interface_desc,
vmnet_operation_mode_key,
mode
);
#define INSANE 0
#if INSANE
uuid_t set_uuid;
uuid_parse("000000-0000-0000-0000-000000000001", set_uuid);
xpc_dictionary_set_uuid(interface_desc,
vmnet_interface_id_key,
set_uuid
);
#endif /* INSANE */
xpc_dictionary_set_string(interface_desc,
vmnet_start_address_key,
ip4addr
);
xpc_dictionary_set_string(interface_desc,
vmnet_end_address_key,
ip4addr
);
xpc_dictionary_set_string(interface_desc,
vmnet_subnet_mask_key,
"255.255.255.0"
);
#if 0
// Available from 11.0
xpc_dictionary_set_uuid(interface_desc,
vmnet_network_identifier_key,
set_uuid
);
xpc_dictionary_set_string(interface_desc,
vmnet_host_ip_address_key,
"10.20.30.40"
);
xpc_dictionary_set_string(interface_desc,
vmnet_host_subnet_mask_key,
"255.255.255.0"
);
// for completeness sake
// vmnet_host_ipv6_address_key
xpc_dictionary_set_bool(interface_desc,
vmnet_enable_isolation_key,
true
);
#endif
// Appears to simply generate a mac address unlikely to be used elsewhere.
// No edge mac filtering was seen with this simple test tool.
// If false then the interface_param generated will not have either a
// vmnet_mac_address or a vmnet_interface_id key.
//
// The documentation implies that if you want the same mac address, you
// set the interface_desc vmnet_interface_id_key to the uuid you get from
// the interface_param. However, this all refers to the "guest" MAC
// and we are trying to emulate a VPN...
xpc_dictionary_set_bool(interface_desc,
vmnet_allocate_mac_address_key,
false
);
dispatch_queue_t vmnet_dispatch_queue = dispatch_queue_create(
"org.qemu.vmnet.iface_queue",
DISPATCH_QUEUE_SERIAL
);
__block vmnet_return_t vmnet_start_status = 0;
__block uint64_t vmnet_iface_mtu = 0;
__block uint64_t vmnet_max_packet_size = 0;
dispatch_semaphore_t vmnet_iface_sem = dispatch_semaphore_create(0);
interface_ref vmnet_iface_ref = vmnet_start_interface(
interface_desc,
vmnet_dispatch_queue,
^(vmnet_return_t status, xpc_object_t _Nullable interface_param) {
vmnet_start_status = status;
if (vmnet_start_status != VMNET_SUCCESS || !interface_param) {
/* Early return if the interface couldn't be started */
dispatch_semaphore_signal(vmnet_iface_sem);
return;
}
vmnet_iface_mtu = xpc_dictionary_get_uint64(
interface_param,
vmnet_mtu_key
);
vmnet_max_packet_size = xpc_dictionary_get_uint64(
interface_param,
vmnet_max_packet_size_key
);
printf("got interface_param with:\n");
xpc_dictionary_apply(interface_param,
^bool (const char * _Nonnull key, xpc_object_t _Nonnull value) {
char *desc = xpc_copy_description(value);
printf(" %s %s\n",
key,
desc
);
free(desc);
return true;
});
printf("\n\n");
dispatch_semaphore_signal(vmnet_iface_sem);
});
/* And block until we receive a response from vmnet */
dispatch_semaphore_wait(vmnet_iface_sem, DISPATCH_TIME_FOREVER);
/* Did we manage to start the interface? */
if (vmnet_start_status != VMNET_SUCCESS || !vmnet_iface_ref) {
printf("Failed to start interface: %i\n", vmnet_start_status);
if (vmnet_start_status == VMNET_FAILURE) {
printf("Hint: vmnet requires running with root access\n");
}
return NULL;
}
printf("Started vmnet interface with configuration:\n");
printf("MTU: %llu\n", vmnet_iface_mtu);
printf("Max packet size: %llu\n", vmnet_max_packet_size);
printf("\n\n");
vmnet_return_t event_cb_stat = vmnet_interface_set_event_callback(
vmnet_iface_ref,
VMNET_INTERFACE_PACKETS_AVAILABLE,
vmnet_dispatch_queue,
^(interface_event_t event_mask, xpc_object_t _Nonnull event) {
if (event_mask != VMNET_INTERFACE_PACKETS_AVAILABLE) {
printf("Unknown vmnet interface event 0x%08x\n", event_mask);
return;
}
char buf[1600]; /* TODO: this should be vmnet_max_packet_size */
struct iovec iov;
iov.iov_base = buf;
iov.iov_len = sizeof(buf);
struct vmpktdesc v;
v.vm_pkt_size = sizeof(buf);
v.vm_pkt_iov = &iov;
v.vm_pkt_iovcnt = 1;
v.vm_flags = 0;
int pktcnt = 1;
vmnet_return_t result = vmnet_read(vmnet_iface_ref, &v, &pktcnt);
if (result != VMNET_SUCCESS) {
printf("Failed to read packet from host: %i\n", result);
return;
}
/* Ensure we read exactly one packet */
assert(pktcnt == 1);
/* Pass the received packet to the handler */
handle_rx_packet(vmnet_iface_ref, buf, v.vm_pkt_size);
});
/* Did we manage to set an event callback? */
if (event_cb_stat != VMNET_SUCCESS) {
printf("Failed to set up a callback to receive packets: %i\n", event_cb_stat);
return NULL;
}
return vmnet_iface_ref;
}
/*
* Experiments in querying the network details
*/
void interface_list1() {
xpc_object_t list = vmnet_copy_shared_interface_list();
printf("interface list from vmnet_copy_shared_interface_list():\n");
xpc_array_apply(list, ^bool(size_t index, xpc_object_t value) {
char *desc = xpc_copy_description(value);
printf(" %lu %s\n",
index,
desc
);
free(desc);
return true;
});
printf("\n");
free(list);
}
void interface_list2() {
CFArrayRef list2 = SCNetworkInterfaceCopyAll();
printf("interface list from SCNetworkInterfaceCopyAll():\n");
CFShow(list2);
printf("\n");
CFRelease(list2);
list2 = SCVLANInterfaceCopyAvailablePhysicalInterfaces();
printf("interface list from SCVLANInterfaceCopyAvailablePhysicalInterfaces():\n");
CFShow(list2);
printf("\n");
CFRelease(list2);
}
void interface_list() {
interface_list1();
interface_list2();
}
int main(int argc, char **argv) {
char *ip4addr = "10.20.30.40";
if (argc>1) {
ip4addr = argv[1];
}
if (0==strcmp(ip4addr,"list")) {
interface_list();
return(0);
}
int timeout = 1000000;
if (argc>2) {
timeout = atoi(argv[2]);
}
interface_ref vmnet_iface_ref = tap_open(ip4addr);
if (vmnet_iface_ref == NULL) {
exit(1);
}
printf("Waiting for packets\n");
int now = time(NULL);
int stopat = now + timeout;
for(;;) {
// Everything is done in the dispatch thread
sleep(1);
now = time(NULL);
if (now > stopat) {
break;
}
}
}