1 /* 2 * Copyright (c) 2023-2025 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 <stdbool.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 #include <netinet/in.h> 45 #include <netinet6/in6_var.h> 46 #include <netinet6/nd6.h> 47 #include <netinet/in.h> 48 #include <netinet/ip.h> 49 #include <netinet/udp.h> 50 #include <netinet/bootp.h> 51 #include <netinet/tcp.h> 52 #include <netinet/if_ether.h> 53 #include <netinet/ip6.h> 54 #include <netinet/icmp6.h> 55 #include <net/if_arp.h> 56 #include <net/bpf.h> 57 #include <net/if_bridgevar.h> 58 #include <net/if_fake_var.h> 59 #include <sys/ioctl.h> 60 #include <sys/types.h> 61 #include <sys/stat.h> 62 #include <errno.h> 63 #include <pthread.h> 64 #include <stdbool.h> 65 #include <TargetConditionals.h> 66 #include <darwintest_utils.h> 67 #include "inet_transfer.h" 68 #include "bpflib.h" 69 #include "in_cksum.h" 70 71 #include <mach/mach_host.h> 72 #include <mach/mach_error.h> 73 74 extern kern_return_t mach_zone_force_gc(host_t host); 75 76 extern bool G_debug; 77 78 /* 79 * network_interface routines 80 */ 81 typedef char if_name_t[IFNAMSIZ]; 82 83 typedef struct { 84 if_name_t if_name; 85 u_short if_index; 86 struct in_addr ip; 87 struct in6_addr ip6; 88 } network_interface, *network_interface_t; 89 90 typedef struct { 91 network_interface one; 92 network_interface two; 93 } network_interface_pair, *network_interface_pair_t; 94 95 typedef struct { 96 u_int count; 97 network_interface_pair list[1]; 98 } network_interface_pair_list, * network_interface_pair_list_t; 99 100 extern void 101 network_interface_create(network_interface_t if_p, const if_name_t name); 102 103 extern void 104 network_interface_destroy(network_interface_t if_p); 105 106 extern network_interface_pair_list_t 107 network_interface_pair_list_alloc(u_int n); 108 109 extern void 110 network_interface_pair_list_destroy(network_interface_pair_list_t list); 111 112 #define DHCP_PAYLOAD_MIN sizeof(struct bootp) 113 #define DHCP_FLAGS_BROADCAST ((u_short)0x8000) 114 115 #define FETH_NAME "feth" 116 #define VLAN_NAME "vlan" 117 #define BOND_NAME "bond" 118 #define BRIDGE_NAME "bridge" 119 #define BRIDGE200 BRIDGE_NAME "200" 120 #define FETH0 FETH_NAME "0" 121 122 extern struct in_addr inet_class_c_subnet_mask; 123 124 typedef union { 125 char bytes[DHCP_PAYLOAD_MIN]; 126 /* force 4-byte alignment */ 127 uint32_t words[DHCP_PAYLOAD_MIN / sizeof(uint32_t)]; 128 } dhcp_min_payload, *dhcp_min_payload_t; 129 130 #define ETHER_PKT_LEN (ETHER_HDR_LEN + ETHERMTU) 131 typedef union { 132 char bytes[ETHER_PKT_LEN]; 133 /* force 4-byte aligment */ 134 uint32_t words[ETHER_PKT_LEN / sizeof(uint32_t)]; 135 } ether_packet, *ether_packet_t; 136 137 typedef struct { 138 struct ip ip; 139 struct udphdr udp; 140 } ip_udp_header_t; 141 142 typedef struct { 143 struct in_addr src_ip; 144 struct in_addr dst_ip; 145 char zero; 146 char proto; 147 unsigned short length; 148 } udp_pseudo_hdr_t; 149 150 typedef struct { 151 struct ip ip; 152 struct tcphdr tcp; 153 } ip_tcp_header_t; 154 155 typedef union { 156 ip_udp_header_t udp; 157 ip_tcp_header_t tcp; 158 } ip_udp_tcp_header_u; 159 160 typedef struct { 161 struct in_addr src_ip; 162 struct in_addr dst_ip; 163 char zero; 164 char proto; 165 unsigned short length; 166 } tcp_pseudo_hdr_t; 167 168 typedef struct { 169 struct ip6_hdr ip6; 170 struct udphdr udp; 171 } ip6_udp_header_t; 172 173 typedef struct { 174 struct in6_addr src_ip; 175 struct in6_addr dst_ip; 176 char zero; 177 char proto; 178 unsigned short length; 179 } udp6_pseudo_hdr_t; 180 181 extern ether_addr_t ether_broadcast; 182 183 extern int inet_dgram_socket_get(void); 184 void inet_dgram_socket_close(void); 185 186 extern int inet6_dgram_socket_get(void); 187 void inet6_dgram_socket_close(void); 188 189 extern bool inet6_get_linklocal_address(unsigned int if_index, struct in6_addr *ret_addr); 190 191 extern int ifnet_create(const char * ifname); 192 193 extern int ifnet_create_2(char * ifname, size_t len); 194 195 extern int ifnet_destroy(const char * ifname, bool fail_on_error); 196 197 extern void ifnet_get_lladdr(const char * ifname, ether_addr_t * eaddr); 198 199 extern void ifnet_attach_ip(char * name); 200 201 extern void ifnet_start_ipv6(const char * ifname); 202 203 extern int ifnet_set_lladdr(const char * ifname, ether_addr_t * eaddr); 204 205 extern int ifnet_set_flags(const char * ifname, 206 uint16_t flags_set, uint16_t flags_clear); 207 208 extern void ifnet_add_ip_address(char *ifname, struct in_addr addr, 209 struct in_addr mask); 210 211 extern void ifnet_remove_ip_address(char *ifname, struct in_addr addr, 212 struct in_addr mask); 213 214 extern int ifnet_set_mtu(const char * ifname, int mtu); 215 216 extern int siocdrvspec(const char * ifname, 217 u_long op, void *arg, size_t argsize, bool set); 218 219 extern void fake_set_peer(const char * feth, const char * feth_peer); 220 221 extern void siocsifvlan(const char * vlan, const char * phys, uint16_t tag); 222 223 extern void route_add_inet_scoped_subnet(char * ifname, u_short if_index, 224 struct in_addr ifa, struct in_addr mask); 225 226 extern u_int ethernet_udp4_frame_populate(void * buf, size_t buf_len, 227 const ether_addr_t * src, 228 struct in_addr src_ip, 229 uint16_t src_port, 230 const ether_addr_t * dst, 231 struct in_addr dst_ip, 232 uint16_t dst_port, 233 const void * data, u_int data_len); 234 235 extern u_int 236 ethernet_udp6_frame_populate(void * buf, size_t buf_len, 237 const ether_addr_t * src, 238 struct in6_addr *src_ip, 239 uint16_t src_port, 240 const ether_addr_t * dst, 241 struct in6_addr * dst_ip, 242 uint16_t dst_port, 243 const void * data, u_int data_len); 244 245 246 extern u_int make_dhcp_payload(dhcp_min_payload_t payload, ether_addr_t *eaddr); 247 248 extern bool has_ipv4_default_route(void); 249 250 extern bool has_ipv6_default_route(void); 251 252 extern int bridge_add_member(const char * bridge, const char * member); 253 254 extern void force_zone_gc(void); 255 256 #endif /* __net_test_lib_h__ */ 257