xref: /xnu-10063.141.1/tests/net_test_lib.h (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1 /*
2  * Copyright (c) 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 #ifndef __net_test_lib_h__
31 #define __net_test_lib_h__
32 
33 #include <darwintest.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <stddef.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/socket.h>
40 #include <arpa/inet.h>
41 #include <sys/event.h>
42 #include <net/if.h>
43 #include <netinet/in.h>
44 #include <netinet6/in6_var.h>
45 #include <netinet6/nd6.h>
46 #include <netinet/in.h>
47 #include <netinet/ip.h>
48 #include <netinet/udp.h>
49 #include <netinet/bootp.h>
50 #include <netinet/tcp.h>
51 #include <netinet/if_ether.h>
52 #include <netinet/ip6.h>
53 #include <netinet/icmp6.h>
54 #include <net/if_arp.h>
55 #include <net/bpf.h>
56 #include <net/if_bridgevar.h>
57 #include <net/if_fake_var.h>
58 #include <sys/ioctl.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <errno.h>
62 #include <pthread.h>
63 #include <stdbool.h>
64 #include <TargetConditionals.h>
65 #include <darwintest_utils.h>
66 #include "inet_transfer.h"
67 #include "bpflib.h"
68 #include "in_cksum.h"
69 
70 extern bool S_debug;
71 
72 #define DHCP_PAYLOAD_MIN        sizeof(struct bootp)
73 #define DHCP_FLAGS_BROADCAST    ((u_short)0x8000)
74 
75 #define FETH_NAME       "feth"
76 
77 typedef union {
78 	char            bytes[DHCP_PAYLOAD_MIN];
79 	/* force 4-byte alignment */
80 	uint32_t        words[DHCP_PAYLOAD_MIN / sizeof(uint32_t)];
81 } dhcp_min_payload, *dhcp_min_payload_t;
82 
83 #define ETHER_PKT_LEN           (ETHER_HDR_LEN + ETHERMTU)
84 typedef union {
85 	char            bytes[ETHER_PKT_LEN];
86 	/* force 4-byte aligment */
87 	uint32_t        words[ETHER_PKT_LEN / sizeof(uint32_t)];
88 } ether_packet, *ether_packet_t;
89 
90 typedef struct {
91 	struct ip       ip;
92 	struct udphdr   udp;
93 } ip_udp_header_t;
94 
95 typedef struct {
96 	struct in_addr  src_ip;
97 	struct in_addr  dst_ip;
98 	char            zero;
99 	char            proto;
100 	unsigned short  length;
101 } udp_pseudo_hdr_t;
102 
103 typedef struct {
104 	struct ip       ip;
105 	struct tcphdr   tcp;
106 } ip_tcp_header_t;
107 
108 typedef union {
109 	ip_udp_header_t udp;
110 	ip_tcp_header_t tcp;
111 } ip_udp_tcp_header_u;
112 
113 typedef struct {
114 	struct in_addr  src_ip;
115 	struct in_addr  dst_ip;
116 	char            zero;
117 	char            proto;
118 	unsigned short  length;
119 } tcp_pseudo_hdr_t;
120 
121 typedef struct {
122 	struct ip6_hdr  ip6;
123 	struct udphdr   udp;
124 } ip6_udp_header_t;
125 
126 typedef struct {
127 	struct in6_addr src_ip;
128 	struct in6_addr dst_ip;
129 	char            zero;
130 	char            proto;
131 	unsigned short  length;
132 } udp6_pseudo_hdr_t;
133 
134 extern int inet_dgram_socket(void);
135 
136 extern int inet6_dgram_socket(void);
137 
138 extern int routing_socket(void);
139 
140 extern int ifnet_create(int s, const char * ifname);
141 
142 extern int ifnet_create_2(int s, char * ifname, size_t len);
143 
144 extern int ifnet_destroy(int s, const char * ifname, bool fail_on_error);
145 
146 extern void ifnet_get_lladdr(int s, const char * ifname, ether_addr_t * eaddr);
147 
148 extern int ifnet_attach_ip(int s, char * name);
149 
150 extern int ifnet_set_flags(int s, const char * ifname,
151     uint16_t flags_set, uint16_t flags_clear);
152 
153 extern int siocdrvspec(int s, const char * ifname,
154     u_long op, void *arg, size_t argsize, bool set);
155 
156 extern int fake_set_peer(int s, const char * feth, const char * feth_peer);
157 
158 extern u_int ethernet_udp4_frame_populate(void * buf, size_t buf_len,
159     const ether_addr_t * src,
160     struct in_addr src_ip,
161     uint16_t src_port,
162     const ether_addr_t * dst,
163     struct in_addr dst_ip,
164     uint16_t dst_port,
165     const void * data, u_int data_len);
166 
167 extern u_int
168 ethernet_udp6_frame_populate(void * buf, size_t buf_len,
169     const ether_addr_t * src,
170     struct in6_addr *src_ip,
171     uint16_t src_port,
172     const ether_addr_t * dst,
173     struct in6_addr * dst_ip,
174     uint16_t dst_port,
175     const void * data, u_int data_len);
176 
177 
178 extern u_int make_dhcp_payload(dhcp_min_payload_t payload, ether_addr_t *eaddr);
179 
180 
181 
182 #endif /* __net_test_lib_h__ */
183