1 /*
2 * Copyright (c) 2021 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 /*
30 * inet_transfer.c
31 * - perform IPv4/IPv6 UDP/TCP transfer tests
32 */
33
34 #include <darwintest.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <stddef.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/socket.h>
41 #include <arpa/inet.h>
42 #include <sys/event.h>
43 #include <net/if.h>
44 #define __APPLE_USE_RFC_3542 1
45 #include <netinet/in.h>
46 #include <netinet6/in6_var.h>
47 #include <netinet6/nd6.h>
48 #include <netinet/in.h>
49 #include <netinet/ip.h>
50 #include <netinet/udp.h>
51 #include <netinet/bootp.h>
52 #include <netinet/tcp.h>
53 #include <netinet/if_ether.h>
54 #include <netinet/ip6.h>
55 #include <netinet/icmp6.h>
56 #include <net/if_arp.h>
57 #include <net/bpf.h>
58 #include <net/if_bridgevar.h>
59 #include <net/if_fake_var.h>
60 #include <sys/ioctl.h>
61 #include <sys/types.h>
62 #include <errno.h>
63 #include <pthread.h>
64 #include <stdbool.h>
65 #include <sysexits.h>
66 #include <darwintest_utils.h>
67
68 #include "inet_transfer.h"
69
70 #define s6_addr16 __u6_addr.__u6_addr16
71
72 typedef union {
73 struct sockaddr sa;
74 struct sockaddr_in sin;
75 struct sockaddr_in6 sin6;
76 } inet_sockaddr, *inet_sockaddr_t;
77
78 typedef struct {
79 inet_sockaddr addr;
80 uint8_t proto;
81 int sock_fd;
82 int server_fd;
83 } inet_socket, *inet_socket_t;
84
85 static char error_string[2048];
86
87 #define set_error_string(__format, ...) \
88 do { \
89 snprintf(error_string, sizeof(error_string), \
90 __format, ## __VA_ARGS__); \
91 } while (0)
92
93 static void
inet_sockaddr_init(inet_sockaddr_t addr,uint8_t af)94 inet_sockaddr_init(inet_sockaddr_t addr, uint8_t af)
95 {
96 bzero(addr, sizeof(*addr));
97 addr->sa.sa_family = af;
98 if (af == AF_INET) {
99 addr->sa.sa_len = sizeof(struct sockaddr_in);
100 } else {
101 addr->sa.sa_len = sizeof(struct sockaddr_in6);
102 }
103 return;
104 }
105
106 static void
inet_sockaddr_init_with_endpoint(inet_sockaddr_t addr,inet_endpoint_t endpoint)107 inet_sockaddr_init_with_endpoint(inet_sockaddr_t addr, inet_endpoint_t endpoint)
108 {
109 bzero(addr, sizeof(*addr));
110 addr->sa.sa_family = endpoint->af;
111 if (endpoint->af == AF_INET) {
112 struct sockaddr_in *sin_p = &addr->sin;
113
114 sin_p->sin_len = sizeof(*sin_p);
115 sin_p->sin_addr = endpoint->addr.v4;
116 if (endpoint->port != 0) {
117 sin_p->sin_port = htons(endpoint->port);
118 }
119 } else {
120 struct sockaddr_in6 *sin6_p = &addr->sin6;
121
122 sin6_p->sin6_len = sizeof(*sin6_p);
123 sin6_p->sin6_addr = endpoint->addr.v6;
124 if (endpoint->port != 0) {
125 sin6_p->sin6_port = htons(endpoint->port);
126 }
127 }
128 return;
129 }
130
131 static uint8_t
inet_sockaddr_get_family(inet_sockaddr_t addr)132 inet_sockaddr_get_family(inet_sockaddr_t addr)
133 {
134 return addr->sa.sa_family;
135 }
136
137 static void
inet_sockaddr_embed_scope(inet_sockaddr_t addr,int if_index)138 inet_sockaddr_embed_scope(inet_sockaddr_t addr, int if_index)
139 {
140 struct sockaddr_in6 *sin6_p;
141
142 if (inet_sockaddr_get_family(addr) != AF_INET6) {
143 return;
144 }
145 sin6_p = &addr->sin6;
146 if (!IN6_IS_ADDR_LINKLOCAL(&sin6_p->sin6_addr)) {
147 return;
148 }
149 sin6_p->sin6_addr.s6_addr16[1] = htons(if_index);
150 return;
151 }
152
153 static bool
inet_endpoint_is_valid(inet_endpoint_t node)154 inet_endpoint_is_valid(inet_endpoint_t node)
155 {
156 switch (node->af) {
157 case AF_INET:
158 case AF_INET6:
159 break;
160 default:
161 set_error_string("invalid address family %d", node->af);
162 return false;
163 }
164 switch (node->proto) {
165 case IPPROTO_TCP:
166 case IPPROTO_UDP:
167 break;
168 default:
169 set_error_string("invalid protocol %d", node->proto);
170 return false;
171 }
172 return true;
173 }
174
175 static uint8_t
proto_get_socket_type(uint8_t proto)176 proto_get_socket_type(uint8_t proto)
177 {
178 return (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
179 }
180
181 static const char *
af_get_string(uint8_t af)182 af_get_string(uint8_t af)
183 {
184 return (af == AF_INET) ? "AF_INET" : "AF_INET6";
185 }
186
187 static const char *
socket_type_get_string(uint8_t type)188 socket_type_get_string(uint8_t type)
189 {
190 return (type == SOCK_DGRAM) ? "SOCK_DGRAM" : "SOCK_STREAM";
191 }
192
193 static bool
socket_bind_to_interface(const char * msg,int s,uint8_t af,int if_index)194 socket_bind_to_interface(const char * msg, int s, uint8_t af, int if_index)
195 {
196 int level;
197 int opt;
198
199 /* bind to interface */
200 if (af == AF_INET) {
201 level = IPPROTO_IP;
202 opt = IP_BOUND_IF;
203 } else {
204 level = IPPROTO_IPV6;
205 opt = IPV6_BOUND_IF;
206 }
207 if (setsockopt(s, level, opt, &if_index, sizeof(if_index)) < 0) {
208 set_error_string("%s: setsockopt(IP%s_BOUND_IF, %d) %s (%d)",
209 msg, (af == AF_INET) ? "" : "V6", if_index,
210 strerror(errno), errno);
211 return false;
212 }
213 return true;
214 }
215
216 static bool
socket_setsockopt_int(const char * msg,int s,int level,int opt,int val)217 socket_setsockopt_int(const char * msg, int s, int level, int opt, int val)
218 {
219 if (setsockopt(s, level, opt, &val, sizeof(val)) < 0) {
220 set_error_string("%s: setsockopt(%d, %d, %d) %s (%d)",
221 msg, level, opt, val, strerror(errno), errno);
222 return false;
223 }
224 return true;
225 }
226
227 static void
inet_socket_init(inet_socket_t sock)228 inet_socket_init(inet_socket_t sock)
229 {
230 bzero(sock, sizeof(*sock));
231 sock->server_fd = -1;
232 sock->sock_fd = -1;
233 return;
234 }
235
236 static void
inet_socket_close(inet_socket_t sock)237 inet_socket_close(inet_socket_t sock)
238 {
239 if (sock->server_fd >= 0) {
240 close(sock->server_fd);
241 }
242 if (sock->sock_fd >= 0) {
243 close(sock->sock_fd);
244 }
245 return;
246 }
247
248 static bool
inet_socket_init_server(inet_socket_t server,inet_endpoint_t endpoint,int server_if_index)249 inet_socket_init_server(inet_socket_t server, inet_endpoint_t endpoint,
250 int server_if_index)
251 {
252 int s;
253 uint8_t socket_type = proto_get_socket_type(endpoint->proto);
254
255 inet_sockaddr_init_with_endpoint(&server->addr, endpoint);
256 inet_sockaddr_embed_scope(&server->addr, server_if_index);
257 server->proto = endpoint->proto;
258 s = socket(server->addr.sa.sa_family, socket_type, 0);
259 if (s < 0) {
260 set_error_string("%s: socket(%s, %s) failed %s (%d)",
261 __func__,
262 af_get_string(endpoint->af),
263 socket_type_get_string(socket_type),
264 strerror(errno), errno);
265 return false;
266 }
267 if (!socket_setsockopt_int(__func__, s, SOL_SOCKET, SO_REUSEADDR, 1)) {
268 return false;
269 }
270 if (!socket_setsockopt_int(__func__, s, SOL_SOCKET, SO_REUSEPORT, 1)) {
271 return false;
272 }
273
274 /* bind to interface */
275 if (!socket_bind_to_interface(__func__, s,
276 endpoint->af, server_if_index)) {
277 return false;
278 }
279
280 /* bind to address */
281 if (bind(s, &server->addr.sa, server->addr.sa.sa_len) < 0) {
282 set_error_string("%s: bind(%s, %s, port=%d) failed %s (%d)",
283 __func__,
284 af_get_string(endpoint->af),
285 socket_type_get_string(socket_type),
286 endpoint->port, strerror(errno), errno);
287 goto failed;
288 }
289
290 /* get the bound port */
291 if (endpoint->port == 0) {
292 inet_sockaddr bound_sa;
293 socklen_t bound_sa_len = sizeof(bound_sa);
294 uint16_t port;
295
296 if (getsockname(s, &bound_sa.sa, &bound_sa_len) < 0) {
297 set_error_string("%s: getsockname(%s, %s) %s (%d)",
298 __func__,
299 af_get_string(endpoint->af),
300 socket_type_get_string(socket_type),
301 strerror(errno), errno);
302 goto failed;
303 }
304 if (endpoint->af == AF_INET) {
305 port = server->addr.sin.sin_port
306 = bound_sa.sin.sin_port;
307 } else {
308 port = server->addr.sin6.sin6_port
309 = bound_sa.sin6.sin6_port;
310 }
311 endpoint->port = ntohs(port);
312 }
313
314 /* listen (TCP) */
315 if (endpoint->proto == IPPROTO_TCP && listen(s, 1) < 0) {
316 set_error_string("%s: listen(%s, 1) failed %s (%d)",
317 __func__,
318 af_get_string(endpoint->af),
319 strerror(errno), errno);
320 goto failed;
321 }
322 server->server_fd = s;
323 return true;
324
325 failed:
326 close(s);
327 return false;
328 }
329
330 static bool
inet_socket_init_client(inet_socket_t client,int client_if_index,inet_endpoint_t endpoint)331 inet_socket_init_client(inet_socket_t client, int client_if_index,
332 inet_endpoint_t endpoint)
333 {
334 int s;
335 uint8_t socket_type = proto_get_socket_type(endpoint->proto);
336
337 inet_sockaddr_init(&client->addr, endpoint->af);
338 s = socket(endpoint->af, socket_type, 0);
339 if (s < 0) {
340 set_error_string("%s: socket(%s, %s) failed %s (%d)",
341 __func__,
342 af_get_string(endpoint->af),
343 socket_type_get_string(socket_type),
344 strerror(errno), errno);
345 return false;
346 }
347 /* bind to interface */
348 if (!socket_bind_to_interface(__func__,
349 s, endpoint->af, client_if_index)) {
350 goto failed;
351 }
352 client->sock_fd = s;
353 return true;
354
355 failed:
356 close(s);
357 return false;
358 }
359
360 static bool
inet_socket_client_connect(inet_socket_t client,int client_if_index,inet_endpoint_t endpoint)361 inet_socket_client_connect(inet_socket_t client, int client_if_index,
362 inet_endpoint_t endpoint)
363 {
364 inet_sockaddr_init_with_endpoint(&client->addr, endpoint);
365 inet_sockaddr_embed_scope(&client->addr, client_if_index);
366 if (connect(client->sock_fd, &client->addr.sa, client->addr.sa.sa_len)
367 < 0) {
368 set_error_string("%s: connect failed %s (%d)",
369 __func__, strerror(errno), errno);
370 return false;
371 }
372 return true;
373 }
374
375 #if 0
376 static const char *
377 inet_sockaddr_ntop(inet_sockaddr_t addr, char * ntopbuf, u_int ntopbuf_size)
378 {
379 const char * ptr;
380
381 if (addr->sa.sa_family == AF_INET) {
382 ptr = (const char *)&addr->sin.sin_addr;
383 } else {
384 ptr = (const char *)&addr->sin6.sin6_addr;
385 }
386 return inet_ntop(addr->sa.sa_family, ptr, ntopbuf, ntopbuf_size);
387 }
388 #endif
389
390 static bool
inet_socket_server_accept(inet_socket_t server)391 inet_socket_server_accept(inet_socket_t server)
392 {
393 inet_sockaddr new_client;
394 int new_fd;
395 socklen_t socklen = sizeof(new_client);
396
397 new_fd = accept(server->server_fd, &new_client.sa, &socklen);
398 if (new_fd < 0) {
399 set_error_string("%s: accept failed %s (%d)",
400 __func__, strerror(errno), errno);
401 return false;
402 }
403 server->sock_fd = new_fd;
404 return true;
405 }
406
407 static void
fill_with_random(uint8_t * buf,u_int len)408 fill_with_random(uint8_t * buf, u_int len)
409 {
410 u_int i;
411 u_int n;
412 uint8_t * p;
413 uint32_t random;
414
415 n = len / sizeof(random);
416 for (i = 0, p = buf; i < n; i++, p += sizeof(random)) {
417 random = arc4random();
418 bcopy(&random, p, sizeof(random));
419 }
420 return;
421 }
422
423 static bool
wait_for_receive(int fd,bool * error)424 wait_for_receive(int fd, bool * error)
425 {
426 fd_set readfds;
427 int n;
428 struct timeval tv;
429
430 *error = false;
431 FD_ZERO(&readfds);
432 FD_SET(fd, &readfds);
433 tv.tv_sec = 0;
434 tv.tv_usec = 200 * 1000;
435 n = select(FD_SETSIZE, &readfds, NULL, NULL, &tv);
436 if (n < 0) {
437 *error = true;
438 set_error_string("%s: select failed %s (%d)",
439 __func__, strerror(errno), errno);
440 }
441 return n > 0;
442 }
443
444 static bool
send_receive(const char * msg,int send_fd,int recv_fd,const uint8_t * data,uint16_t data_size,bool retry,bool need_connect)445 send_receive(const char * msg,
446 int send_fd, int recv_fd, const uint8_t * data, uint16_t data_size,
447 bool retry, bool need_connect)
448 {
449 ssize_t n;
450 uint8_t rbuf[2048];
451 inet_sockaddr sa;
452 socklen_t sa_len = sizeof(sa);
453 int try = 0;
454 ssize_t total = 0;
455
456 /* send payload to receiver */
457 bzero(&sa, sizeof(sa));
458 do {
459 bool failed = false;
460
461 n = send(send_fd, data, data_size, 0);
462 if (n != data_size) {
463 set_error_string("%s: %s %d bytes (actual %ld)"
464 " failed %s (%d)",
465 __func__, msg,
466 data_size, n, strerror(errno), errno);
467 return false;
468 }
469 #define MAX_TRY 2
470 if (retry && !wait_for_receive(recv_fd, &failed)) {
471 if (failed) {
472 return false;
473 }
474 try++;
475 if (try == MAX_TRY) {
476 set_error_string("%s: %s max retry",
477 __func__, msg);
478 return false;
479 }
480 continue;
481 }
482 } while (false);
483
484 /* receive payload from sender */
485 total = 0;
486 while (total < data_size) {
487 if (need_connect) {
488 /* need originator's address to connect UDP socket */
489 n = recvfrom(recv_fd, rbuf, sizeof(rbuf), 0,
490 &sa.sa, &sa_len);
491 } else {
492 n = recv(recv_fd, rbuf, sizeof(rbuf), 0);
493 }
494 if (n <= 0) {
495 perror("recv");
496 break;
497 }
498 total += n;
499 }
500 if (total != data_size) {
501 set_error_string("%s: %s %d bytes (actual %ld)"
502 " failed %s (%d)",
503 __func__, msg,
504 data_size, total, strerror(errno),
505 errno);
506 return false;
507 }
508 if (need_connect && connect(recv_fd, &sa.sa, sa_len) < 0) {
509 set_error_string("%s: %s connect failed %s (%d)",
510 __func__, msg, strerror(errno), errno);
511 return false;
512 }
513 return true;
514 }
515
516 typedef struct {
517 uint16_t start;
518 uint16_t end;
519 } uint16_range, *uint16_range_t;
520
521 static uint16_range
522 data_sizes[] = { { 1, 15 },
523 { 39, 60 },
524 { 79, 100 },
525 { 215, 236 },
526 { 485, 506 },
527 { 985, 1006 },
528 { 1250, 1279 },
529 { 1461, 1500 }, /* this will result in IP fragmentation */
530 { 0, 0 }};
531
532 static bool
inet_transfer_loop(inet_socket_t server,inet_socket_t client)533 inet_transfer_loop(inet_socket_t server, inet_socket_t client)
534 {
535 bool need_connect;
536 uint8_t buf[2048];
537 bool retry;
538 uint16_range_t scan;
539 int server_fd;
540
541 if (server->proto == IPPROTO_TCP) {
542 server_fd = server->sock_fd;
543 need_connect = false;
544 retry = false;
545 need_connect = false;
546 } else {
547 server_fd = server->server_fd;
548 need_connect = true;
549 retry = true; /* UDP is unreliable, retry if necessary */
550 need_connect = true;
551 }
552 fill_with_random(buf, sizeof(buf));
553
554 /*
555 * ping pong packets back and forth between the client and server.
556 */
557 for (scan = data_sizes; scan->start != 0; scan++) {
558 for (uint16_t data_size = scan->start;
559 data_size < scan->end; data_size++) {
560 bool success;
561
562 /* Client to Server */
563 success = send_receive("client send",
564 client->sock_fd, server_fd,
565 buf, data_size, retry,
566 need_connect);
567 if (!success) {
568 return false;
569 }
570 /* UDP socket only needs to be connect()'d first time */
571 need_connect = false;
572
573 /* Server to Client */
574 success = send_receive("server send",
575 server_fd, client->sock_fd,
576 buf, data_size, retry,
577 false);
578 if (!success) {
579 return false;
580 }
581 }
582 }
583 return true;
584 }
585
586 bool
inet_transfer_local(inet_endpoint_t server_endpoint,int server_if_index,int client_if_index)587 inet_transfer_local(inet_endpoint_t server_endpoint,
588 int server_if_index, int client_if_index)
589 {
590 inet_socket client;
591 inet_socket server;
592 bool success = false;
593
594 inet_socket_init(&server);
595 inet_socket_init(&client);
596
597 if (!inet_endpoint_is_valid(server_endpoint)) {
598 return false;
599 }
600 if (!inet_socket_init_server(&server, server_endpoint,
601 server_if_index)) {
602 goto failed;
603 }
604 if (!inet_socket_init_client(&client, client_if_index,
605 server_endpoint)) {
606 goto failed;
607 }
608 if (!inet_socket_client_connect(&client, client_if_index,
609 server_endpoint)) {
610 goto failed;
611 }
612 if (server.proto == IPPROTO_TCP
613 && !inet_socket_server_accept(&server)) {
614 goto failed;
615 }
616 if (!inet_transfer_loop(&server, &client)) {
617 goto failed;
618 }
619 success = true;
620
621 failed:
622 inet_socket_close(&client);
623 inet_socket_close(&server);
624 return success;
625 }
626
627 const char *
inet_transfer_error_string(void)628 inet_transfer_error_string(void)
629 {
630 return error_string;
631 }
632
633
634 #ifdef TEST_INET_TRANSFER
635
636 static void
usage(const char * progname)637 usage(const char * progname)
638 {
639 fprintf(stderr,
640 "usage: %s -i <client-interface> -I <server-interface> "
641 "-s <server_ip> [ -p <port> ] [ -t | -u ]\n", progname);
642 exit(EX_USAGE);
643 }
644
645 int
main(int argc,char * argv[])646 main(int argc, char *argv[])
647 {
648 int client_if_index = 0;
649 int server_if_index = 0;
650 int ch;
651 const char * progname = argv[0];
652 inet_endpoint endpoint;
653
654 bzero(&endpoint, sizeof(endpoint));
655 while ((ch = getopt(argc, argv, "i:I:p:s:tu")) != EOF) {
656 switch ((char)ch) {
657 case 'i':
658 if (client_if_index != 0) {
659 usage(progname);
660 }
661 client_if_index = if_nametoindex(optarg);
662 if (client_if_index == 0) {
663 fprintf(stderr, "No such interface '%s'\n",
664 optarg);
665 exit(EX_USAGE);
666 }
667 break;
668 case 'I':
669 if (server_if_index != 0) {
670 usage(progname);
671 }
672 server_if_index = if_nametoindex(optarg);
673 if (server_if_index == 0) {
674 fprintf(stderr, "No such interface '%s'\n",
675 optarg);
676 exit(EX_USAGE);
677 }
678 break;
679 case 'p':
680 if (endpoint.port != 0) {
681 fprintf(stderr,
682 "port specified multiple times\n");
683 usage(progname);
684 }
685 endpoint.port = strtoul(optarg, NULL, 0);
686 if (endpoint.port == 0) {
687 fprintf(stderr,
688 "Invalid port '%s'\n", optarg);
689 usage(progname);
690 }
691 break;
692 case 's':
693 if (endpoint.af != 0) {
694 fprintf(stderr,
695 "-s may only be specified once\n");
696 usage(progname);
697 }
698 if (inet_pton(AF_INET, optarg,
699 &endpoint.addr) == 1) {
700 endpoint.af = AF_INET;
701 } else if (inet_pton(AF_INET6, optarg,
702 &endpoint.addr) == 1) {
703 endpoint.af = AF_INET6;
704 } else {
705 fprintf(stderr, "invalid IP address '%s'\n",
706 optarg);
707 exit(EX_USAGE);
708 }
709 break;
710 case 't':
711 if (endpoint.proto != 0) {
712 fprintf(stderr,
713 "protocol specified multiple times\n");
714 usage(progname);
715 }
716 endpoint.proto = IPPROTO_TCP;
717 break;
718 case 'u':
719 if (endpoint.proto != 0) {
720 fprintf(stderr,
721 "protocol specified multiple times\n");
722 usage(progname);
723 }
724 endpoint.proto = IPPROTO_UDP;
725 break;
726 default:
727 break;
728 }
729 }
730 if (server_if_index == 0 || client_if_index == 0 || endpoint.af == 0) {
731 usage(progname);
732 }
733 if (endpoint.proto == 0) {
734 endpoint.proto = IPPROTO_TCP;
735 }
736 if (!inet_transfer_local(&endpoint, server_if_index, client_if_index)) {
737 fprintf(stderr, "inet_transfer_local failed\n%s\n",
738 inet_transfer_error_string());
739 exit(EX_OSERR);
740 }
741 exit(0);
742 return 0;
743 }
744 #endif /* TEST_INET_TRANSFER */
745