xref: /xnu-11417.140.69/tests/net_test_lib.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2023-2024 Apple Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions 
29*43a90889SApple OSS Distributions 
30*43a90889SApple OSS Distributions #ifndef __net_test_lib_h__
31*43a90889SApple OSS Distributions #define __net_test_lib_h__
32*43a90889SApple OSS Distributions 
33*43a90889SApple OSS Distributions #include <darwintest.h>
34*43a90889SApple OSS Distributions #include <stdio.h>
35*43a90889SApple OSS Distributions #include <unistd.h>
36*43a90889SApple OSS Distributions #include <stddef.h>
37*43a90889SApple OSS Distributions #include <stdbool.h>
38*43a90889SApple OSS Distributions #include <stdlib.h>
39*43a90889SApple OSS Distributions #include <string.h>
40*43a90889SApple OSS Distributions #include <sys/socket.h>
41*43a90889SApple OSS Distributions #include <arpa/inet.h>
42*43a90889SApple OSS Distributions #include <sys/event.h>
43*43a90889SApple OSS Distributions #include <net/if.h>
44*43a90889SApple OSS Distributions #include <netinet/in.h>
45*43a90889SApple OSS Distributions #include <netinet6/in6_var.h>
46*43a90889SApple OSS Distributions #include <netinet6/nd6.h>
47*43a90889SApple OSS Distributions #include <netinet/in.h>
48*43a90889SApple OSS Distributions #include <netinet/ip.h>
49*43a90889SApple OSS Distributions #include <netinet/udp.h>
50*43a90889SApple OSS Distributions #include <netinet/bootp.h>
51*43a90889SApple OSS Distributions #include <netinet/tcp.h>
52*43a90889SApple OSS Distributions #include <netinet/if_ether.h>
53*43a90889SApple OSS Distributions #include <netinet/ip6.h>
54*43a90889SApple OSS Distributions #include <netinet/icmp6.h>
55*43a90889SApple OSS Distributions #include <net/if_arp.h>
56*43a90889SApple OSS Distributions #include <net/bpf.h>
57*43a90889SApple OSS Distributions #include <net/if_bridgevar.h>
58*43a90889SApple OSS Distributions #include <net/if_fake_var.h>
59*43a90889SApple OSS Distributions #include <sys/ioctl.h>
60*43a90889SApple OSS Distributions #include <sys/types.h>
61*43a90889SApple OSS Distributions #include <sys/stat.h>
62*43a90889SApple OSS Distributions #include <errno.h>
63*43a90889SApple OSS Distributions #include <pthread.h>
64*43a90889SApple OSS Distributions #include <stdbool.h>
65*43a90889SApple OSS Distributions #include <TargetConditionals.h>
66*43a90889SApple OSS Distributions #include <darwintest_utils.h>
67*43a90889SApple OSS Distributions #include "inet_transfer.h"
68*43a90889SApple OSS Distributions #include "bpflib.h"
69*43a90889SApple OSS Distributions #include "in_cksum.h"
70*43a90889SApple OSS Distributions 
71*43a90889SApple OSS Distributions extern bool G_debug;
72*43a90889SApple OSS Distributions 
73*43a90889SApple OSS Distributions /*
74*43a90889SApple OSS Distributions  * network_interface routines
75*43a90889SApple OSS Distributions  */
76*43a90889SApple OSS Distributions typedef char if_name_t[IFNAMSIZ];
77*43a90889SApple OSS Distributions 
78*43a90889SApple OSS Distributions typedef struct {
79*43a90889SApple OSS Distributions 	if_name_t               if_name;
80*43a90889SApple OSS Distributions 	u_short                 if_index;
81*43a90889SApple OSS Distributions 	struct in_addr          ip;
82*43a90889SApple OSS Distributions 	struct in6_addr         ip6;
83*43a90889SApple OSS Distributions } network_interface, *network_interface_t;
84*43a90889SApple OSS Distributions 
85*43a90889SApple OSS Distributions typedef struct {
86*43a90889SApple OSS Distributions 	network_interface       one;
87*43a90889SApple OSS Distributions 	network_interface       two;
88*43a90889SApple OSS Distributions } network_interface_pair, *network_interface_pair_t;
89*43a90889SApple OSS Distributions 
90*43a90889SApple OSS Distributions typedef struct {
91*43a90889SApple OSS Distributions 	u_int                   count;
92*43a90889SApple OSS Distributions 	network_interface_pair  list[1];
93*43a90889SApple OSS Distributions } network_interface_pair_list, * network_interface_pair_list_t;
94*43a90889SApple OSS Distributions 
95*43a90889SApple OSS Distributions extern void
96*43a90889SApple OSS Distributions network_interface_create(network_interface_t if_p, const if_name_t name);
97*43a90889SApple OSS Distributions 
98*43a90889SApple OSS Distributions extern void
99*43a90889SApple OSS Distributions network_interface_destroy(network_interface_t if_p);
100*43a90889SApple OSS Distributions 
101*43a90889SApple OSS Distributions extern network_interface_pair_list_t
102*43a90889SApple OSS Distributions network_interface_pair_list_alloc(u_int n);
103*43a90889SApple OSS Distributions 
104*43a90889SApple OSS Distributions extern void
105*43a90889SApple OSS Distributions network_interface_pair_list_destroy(network_interface_pair_list_t list);
106*43a90889SApple OSS Distributions 
107*43a90889SApple OSS Distributions #define DHCP_PAYLOAD_MIN        sizeof(struct bootp)
108*43a90889SApple OSS Distributions #define DHCP_FLAGS_BROADCAST    ((u_short)0x8000)
109*43a90889SApple OSS Distributions 
110*43a90889SApple OSS Distributions #define FETH_NAME       "feth"
111*43a90889SApple OSS Distributions #define VLAN_NAME       "vlan"
112*43a90889SApple OSS Distributions #define BOND_NAME       "bond"
113*43a90889SApple OSS Distributions #define BRIDGE_NAME     "bridge"
114*43a90889SApple OSS Distributions #define BRIDGE200       BRIDGE_NAME "200"
115*43a90889SApple OSS Distributions #define FETH0           FETH_NAME "0"
116*43a90889SApple OSS Distributions 
117*43a90889SApple OSS Distributions extern struct in_addr inet_class_c_subnet_mask;
118*43a90889SApple OSS Distributions 
119*43a90889SApple OSS Distributions typedef union {
120*43a90889SApple OSS Distributions 	char            bytes[DHCP_PAYLOAD_MIN];
121*43a90889SApple OSS Distributions 	/* force 4-byte alignment */
122*43a90889SApple OSS Distributions 	uint32_t        words[DHCP_PAYLOAD_MIN / sizeof(uint32_t)];
123*43a90889SApple OSS Distributions } dhcp_min_payload, *dhcp_min_payload_t;
124*43a90889SApple OSS Distributions 
125*43a90889SApple OSS Distributions #define ETHER_PKT_LEN           (ETHER_HDR_LEN + ETHERMTU)
126*43a90889SApple OSS Distributions typedef union {
127*43a90889SApple OSS Distributions 	char            bytes[ETHER_PKT_LEN];
128*43a90889SApple OSS Distributions 	/* force 4-byte aligment */
129*43a90889SApple OSS Distributions 	uint32_t        words[ETHER_PKT_LEN / sizeof(uint32_t)];
130*43a90889SApple OSS Distributions } ether_packet, *ether_packet_t;
131*43a90889SApple OSS Distributions 
132*43a90889SApple OSS Distributions typedef struct {
133*43a90889SApple OSS Distributions 	struct ip       ip;
134*43a90889SApple OSS Distributions 	struct udphdr   udp;
135*43a90889SApple OSS Distributions } ip_udp_header_t;
136*43a90889SApple OSS Distributions 
137*43a90889SApple OSS Distributions typedef struct {
138*43a90889SApple OSS Distributions 	struct in_addr  src_ip;
139*43a90889SApple OSS Distributions 	struct in_addr  dst_ip;
140*43a90889SApple OSS Distributions 	char            zero;
141*43a90889SApple OSS Distributions 	char            proto;
142*43a90889SApple OSS Distributions 	unsigned short  length;
143*43a90889SApple OSS Distributions } udp_pseudo_hdr_t;
144*43a90889SApple OSS Distributions 
145*43a90889SApple OSS Distributions typedef struct {
146*43a90889SApple OSS Distributions 	struct ip       ip;
147*43a90889SApple OSS Distributions 	struct tcphdr   tcp;
148*43a90889SApple OSS Distributions } ip_tcp_header_t;
149*43a90889SApple OSS Distributions 
150*43a90889SApple OSS Distributions typedef union {
151*43a90889SApple OSS Distributions 	ip_udp_header_t udp;
152*43a90889SApple OSS Distributions 	ip_tcp_header_t tcp;
153*43a90889SApple OSS Distributions } ip_udp_tcp_header_u;
154*43a90889SApple OSS Distributions 
155*43a90889SApple OSS Distributions typedef struct {
156*43a90889SApple OSS Distributions 	struct in_addr  src_ip;
157*43a90889SApple OSS Distributions 	struct in_addr  dst_ip;
158*43a90889SApple OSS Distributions 	char            zero;
159*43a90889SApple OSS Distributions 	char            proto;
160*43a90889SApple OSS Distributions 	unsigned short  length;
161*43a90889SApple OSS Distributions } tcp_pseudo_hdr_t;
162*43a90889SApple OSS Distributions 
163*43a90889SApple OSS Distributions typedef struct {
164*43a90889SApple OSS Distributions 	struct ip6_hdr  ip6;
165*43a90889SApple OSS Distributions 	struct udphdr   udp;
166*43a90889SApple OSS Distributions } ip6_udp_header_t;
167*43a90889SApple OSS Distributions 
168*43a90889SApple OSS Distributions typedef struct {
169*43a90889SApple OSS Distributions 	struct in6_addr src_ip;
170*43a90889SApple OSS Distributions 	struct in6_addr dst_ip;
171*43a90889SApple OSS Distributions 	char            zero;
172*43a90889SApple OSS Distributions 	char            proto;
173*43a90889SApple OSS Distributions 	unsigned short  length;
174*43a90889SApple OSS Distributions } udp6_pseudo_hdr_t;
175*43a90889SApple OSS Distributions 
176*43a90889SApple OSS Distributions extern ether_addr_t ether_broadcast;
177*43a90889SApple OSS Distributions 
178*43a90889SApple OSS Distributions extern int inet_dgram_socket_get(void);
179*43a90889SApple OSS Distributions void inet_dgram_socket_close(void);
180*43a90889SApple OSS Distributions 
181*43a90889SApple OSS Distributions extern int inet6_dgram_socket_get(void);
182*43a90889SApple OSS Distributions void inet6_dgram_socket_close(void);
183*43a90889SApple OSS Distributions 
184*43a90889SApple OSS Distributions extern int ifnet_create(const char * ifname);
185*43a90889SApple OSS Distributions 
186*43a90889SApple OSS Distributions extern int ifnet_create_2(char * ifname, size_t len);
187*43a90889SApple OSS Distributions 
188*43a90889SApple OSS Distributions extern int ifnet_destroy(const char * ifname, bool fail_on_error);
189*43a90889SApple OSS Distributions 
190*43a90889SApple OSS Distributions extern void ifnet_get_lladdr(const char * ifname, ether_addr_t * eaddr);
191*43a90889SApple OSS Distributions 
192*43a90889SApple OSS Distributions extern void ifnet_attach_ip(char * name);
193*43a90889SApple OSS Distributions 
194*43a90889SApple OSS Distributions extern void ifnet_start_ipv6(const char * ifname);
195*43a90889SApple OSS Distributions 
196*43a90889SApple OSS Distributions extern int ifnet_set_lladdr(const char * ifname, ether_addr_t * eaddr);
197*43a90889SApple OSS Distributions 
198*43a90889SApple OSS Distributions extern int ifnet_set_flags(const char * ifname,
199*43a90889SApple OSS Distributions     uint16_t flags_set, uint16_t flags_clear);
200*43a90889SApple OSS Distributions 
201*43a90889SApple OSS Distributions extern void ifnet_add_ip_address(char *ifname, struct in_addr addr,
202*43a90889SApple OSS Distributions     struct in_addr mask);
203*43a90889SApple OSS Distributions 
204*43a90889SApple OSS Distributions extern int ifnet_set_mtu(const char * ifname, int mtu);
205*43a90889SApple OSS Distributions 
206*43a90889SApple OSS Distributions extern int siocdrvspec(const char * ifname,
207*43a90889SApple OSS Distributions     u_long op, void *arg, size_t argsize, bool set);
208*43a90889SApple OSS Distributions 
209*43a90889SApple OSS Distributions extern void fake_set_peer(const char * feth, const char * feth_peer);
210*43a90889SApple OSS Distributions 
211*43a90889SApple OSS Distributions extern void siocsifvlan(const char * vlan, const char * phys, uint16_t tag);
212*43a90889SApple OSS Distributions 
213*43a90889SApple OSS Distributions extern void route_add_inet_scoped_subnet(char * ifname, u_short if_index,
214*43a90889SApple OSS Distributions     struct in_addr ifa, struct in_addr mask);
215*43a90889SApple OSS Distributions 
216*43a90889SApple OSS Distributions extern u_int ethernet_udp4_frame_populate(void * buf, size_t buf_len,
217*43a90889SApple OSS Distributions     const ether_addr_t * src,
218*43a90889SApple OSS Distributions     struct in_addr src_ip,
219*43a90889SApple OSS Distributions     uint16_t src_port,
220*43a90889SApple OSS Distributions     const ether_addr_t * dst,
221*43a90889SApple OSS Distributions     struct in_addr dst_ip,
222*43a90889SApple OSS Distributions     uint16_t dst_port,
223*43a90889SApple OSS Distributions     const void * data, u_int data_len);
224*43a90889SApple OSS Distributions 
225*43a90889SApple OSS Distributions extern u_int
226*43a90889SApple OSS Distributions ethernet_udp6_frame_populate(void * buf, size_t buf_len,
227*43a90889SApple OSS Distributions     const ether_addr_t * src,
228*43a90889SApple OSS Distributions     struct in6_addr *src_ip,
229*43a90889SApple OSS Distributions     uint16_t src_port,
230*43a90889SApple OSS Distributions     const ether_addr_t * dst,
231*43a90889SApple OSS Distributions     struct in6_addr * dst_ip,
232*43a90889SApple OSS Distributions     uint16_t dst_port,
233*43a90889SApple OSS Distributions     const void * data, u_int data_len);
234*43a90889SApple OSS Distributions 
235*43a90889SApple OSS Distributions 
236*43a90889SApple OSS Distributions extern u_int make_dhcp_payload(dhcp_min_payload_t payload, ether_addr_t *eaddr);
237*43a90889SApple OSS Distributions 
238*43a90889SApple OSS Distributions extern bool has_ipv4_default_route(void);
239*43a90889SApple OSS Distributions 
240*43a90889SApple OSS Distributions extern bool has_ipv6_default_route(void);
241*43a90889SApple OSS Distributions 
242*43a90889SApple OSS Distributions extern int bridge_add_member(const char * bridge, const char * member);
243*43a90889SApple OSS Distributions 
244*43a90889SApple OSS Distributions #endif /* __net_test_lib_h__ */
245