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