1 /*
2 * Copyright (c) 2021-2023 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 break;
483 } while (true);
484
485 /* receive payload from sender */
486 total = 0;
487 while (total < data_size) {
488 if (need_connect) {
489 /* need originator's address to connect UDP socket */
490 n = recvfrom(recv_fd, rbuf, sizeof(rbuf), 0,
491 &sa.sa, &sa_len);
492 } else {
493 n = recv(recv_fd, rbuf, sizeof(rbuf), 0);
494 }
495 if (n <= 0) {
496 perror("recv");
497 break;
498 }
499 total += n;
500 }
501 if (total != data_size) {
502 set_error_string("%s: %s %d bytes (actual %ld)"
503 " failed %s (%d)",
504 __func__, msg,
505 data_size, total, strerror(errno),
506 errno);
507 return false;
508 }
509 if (need_connect && connect(recv_fd, &sa.sa, sa_len) < 0) {
510 set_error_string("%s: %s connect failed %s (%d)",
511 __func__, msg, strerror(errno), errno);
512 return false;
513 }
514 return true;
515 }
516
517 typedef struct {
518 uint16_t start;
519 uint16_t end;
520 } uint16_range, *uint16_range_t;
521
522 static uint16_range
523 data_sizes[] = { { 1, 15 },
524 { 39, 60 },
525 { 79, 100 },
526 { 215, 236 },
527 { 485, 506 },
528 { 985, 1006 },
529 { 1250, 1279 },
530 { 1461, 1500 }, /* this will result in IP fragmentation */
531 { 0, 0 }};
532
533 static bool
inet_transfer_loop(inet_socket_t server,inet_socket_t client)534 inet_transfer_loop(inet_socket_t server, inet_socket_t client)
535 {
536 bool need_connect;
537 uint8_t buf[2048];
538 bool retry;
539 uint16_range_t scan;
540 int server_fd;
541
542 if (server->proto == IPPROTO_TCP) {
543 server_fd = server->sock_fd;
544 need_connect = false;
545 retry = false;
546 need_connect = false;
547 } else {
548 server_fd = server->server_fd;
549 need_connect = true;
550 retry = true; /* UDP is unreliable, retry if necessary */
551 need_connect = true;
552 }
553 fill_with_random(buf, sizeof(buf));
554
555 /*
556 * ping pong packets back and forth between the client and server.
557 */
558 for (scan = data_sizes; scan->start != 0; scan++) {
559 for (uint16_t data_size = scan->start;
560 data_size < scan->end; data_size++) {
561 bool success;
562
563 /* Client to Server */
564 success = send_receive("client send",
565 client->sock_fd, server_fd,
566 buf, data_size, retry,
567 need_connect);
568 if (!success) {
569 return false;
570 }
571 /* UDP socket only needs to be connect()'d first time */
572 need_connect = false;
573
574 /* Server to Client */
575 success = send_receive("server send",
576 server_fd, client->sock_fd,
577 buf, data_size, retry,
578 false);
579 if (!success) {
580 return false;
581 }
582 }
583 }
584 return true;
585 }
586
587 bool
inet_transfer_local(inet_endpoint_t server_endpoint,int server_if_index,int client_if_index)588 inet_transfer_local(inet_endpoint_t server_endpoint,
589 int server_if_index, int client_if_index)
590 {
591 inet_socket client;
592 inet_socket server;
593 bool success = false;
594
595 inet_socket_init(&server);
596 inet_socket_init(&client);
597
598 if (!inet_endpoint_is_valid(server_endpoint)) {
599 return false;
600 }
601 if (!inet_socket_init_server(&server, server_endpoint,
602 server_if_index)) {
603 goto failed;
604 }
605 if (!inet_socket_init_client(&client, client_if_index,
606 server_endpoint)) {
607 goto failed;
608 }
609 if (!inet_socket_client_connect(&client, client_if_index,
610 server_endpoint)) {
611 goto failed;
612 }
613 if (server.proto == IPPROTO_TCP
614 && !inet_socket_server_accept(&server)) {
615 goto failed;
616 }
617 if (!inet_transfer_loop(&server, &client)) {
618 goto failed;
619 }
620 success = true;
621
622 failed:
623 inet_socket_close(&client);
624 inet_socket_close(&server);
625 return success;
626 }
627
628 const char *
inet_transfer_error_string(void)629 inet_transfer_error_string(void)
630 {
631 return error_string;
632 }
633
634
635 #ifdef TEST_INET_TRANSFER
636
637 static void
usage(const char * progname)638 usage(const char * progname)
639 {
640 fprintf(stderr,
641 "usage: %s -i <client-interface> -I <server-interface> "
642 "-s <server_ip> [ -p <port> ] [ -t | -u ]\n", progname);
643 exit(EX_USAGE);
644 }
645
646 int
main(int argc,char * argv[])647 main(int argc, char *argv[])
648 {
649 int client_if_index = 0;
650 int server_if_index = 0;
651 int ch;
652 const char * progname = argv[0];
653 inet_endpoint endpoint;
654
655 bzero(&endpoint, sizeof(endpoint));
656 while ((ch = getopt(argc, argv, "i:I:p:s:tu")) != EOF) {
657 switch ((char)ch) {
658 case 'i':
659 if (client_if_index != 0) {
660 usage(progname);
661 }
662 client_if_index = if_nametoindex(optarg);
663 if (client_if_index == 0) {
664 fprintf(stderr, "No such interface '%s'\n",
665 optarg);
666 exit(EX_USAGE);
667 }
668 break;
669 case 'I':
670 if (server_if_index != 0) {
671 usage(progname);
672 }
673 server_if_index = if_nametoindex(optarg);
674 if (server_if_index == 0) {
675 fprintf(stderr, "No such interface '%s'\n",
676 optarg);
677 exit(EX_USAGE);
678 }
679 break;
680 case 'p':
681 if (endpoint.port != 0) {
682 fprintf(stderr,
683 "port specified multiple times\n");
684 usage(progname);
685 }
686 endpoint.port = strtoul(optarg, NULL, 0);
687 if (endpoint.port == 0) {
688 fprintf(stderr,
689 "Invalid port '%s'\n", optarg);
690 usage(progname);
691 }
692 break;
693 case 's':
694 if (endpoint.af != 0) {
695 fprintf(stderr,
696 "-s may only be specified once\n");
697 usage(progname);
698 }
699 if (inet_pton(AF_INET, optarg,
700 &endpoint.addr) == 1) {
701 endpoint.af = AF_INET;
702 } else if (inet_pton(AF_INET6, optarg,
703 &endpoint.addr) == 1) {
704 endpoint.af = AF_INET6;
705 } else {
706 fprintf(stderr, "invalid IP address '%s'\n",
707 optarg);
708 exit(EX_USAGE);
709 }
710 break;
711 case 't':
712 if (endpoint.proto != 0) {
713 fprintf(stderr,
714 "protocol specified multiple times\n");
715 usage(progname);
716 }
717 endpoint.proto = IPPROTO_TCP;
718 break;
719 case 'u':
720 if (endpoint.proto != 0) {
721 fprintf(stderr,
722 "protocol specified multiple times\n");
723 usage(progname);
724 }
725 endpoint.proto = IPPROTO_UDP;
726 break;
727 default:
728 break;
729 }
730 }
731 if (server_if_index == 0 || client_if_index == 0 || endpoint.af == 0) {
732 usage(progname);
733 }
734 if (endpoint.proto == 0) {
735 endpoint.proto = IPPROTO_TCP;
736 }
737 if (!inet_transfer_local(&endpoint, server_if_index, client_if_index)) {
738 fprintf(stderr, "inet_transfer_local failed\n%s\n",
739 inet_transfer_error_string());
740 exit(EX_OSERR);
741 }
742 exit(0);
743 return 0;
744 }
745 #endif /* TEST_INET_TRANSFER */
746