1 /* 2 * Copyright (c) 2012-2021 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 #ifndef _NET_PKTAP_H_ 30 #define _NET_PKTAP_H_ 31 32 #include <sys/_types/_timeval32.h> 33 #include <stdint.h> 34 #include <net/if.h> 35 #include <uuid/uuid.h> 36 #include <string.h> 37 38 #ifdef PRIVATE 39 40 #define PKTAP_IFNAME "pktap" 41 42 /* To store interface name + unit */ 43 #define PKTAP_IFXNAMESIZE (IF_NAMESIZE + 8) 44 45 /* 46 * Commands via SIOCGDRVSPEC/SIOCSDRVSPEC 47 */ 48 #define PKTP_CMD_FILTER_GET 1 /* array of PKTAP_MAX_FILTERS * struct pktap_filter */ 49 #define PKTP_CMD_FILTER_SET 3 /* array of PKTAP_MAX_FILTERS * struct pktap_filter */ 50 #define PKTP_CMD_TAP_COUNT 4 /* uint32_t number of active bpf tap on the interface */ 51 52 /* 53 * Filtering is currently based on network interface properties -- 54 * the interface type and the interface name -- and has two types of 55 * operations -- pass and skip. 56 * By default only interfaces of type IFT_ETHER and IFT_CELLULAR pass 57 * the filter. 58 * It's possible to include other interfaces by type or by name 59 * The interface type is evaluated before the interface name 60 * The first matching rule stops the evaluation. 61 * A rule with interface type 0 (zero) matches any interfaces 62 */ 63 #define PKTAP_FILTER_OP_NONE 0 /* For inactive entries at the end of the list */ 64 #define PKTAP_FILTER_OP_PASS 1 65 #define PKTAP_FILTER_OP_SKIP 2 66 67 #define PKTAP_FILTER_PARAM_NONE 0 68 #define PKTAP_FILTER_PARAM_IF_TYPE 1 69 #define PKTAP_FILTER_PARAM_IF_NAME 2 70 71 struct pktap_filter { 72 uint32_t filter_op; 73 uint32_t filter_param; 74 union { 75 uint32_t _filter_if_type; 76 char _filter_if_name[PKTAP_IFXNAMESIZE]; 77 } param_; 78 }; 79 #define filter_param_if_type param_._filter_if_type 80 #define filter_param_if_name param_._filter_if_name 81 82 #define PKTAP_MAX_FILTERS 8 83 84 /* 85 * Header for DLT_PKTAP 86 * 87 * In theory, there could be several types of blocks in a chain before the actual packet 88 */ 89 struct pktap_header { 90 uint32_t pth_length; /* length of this header */ 91 uint32_t pth_type_next; /* type of data following */ 92 uint32_t pth_dlt; /* DLT of packet */ 93 char pth_ifname[PKTAP_IFXNAMESIZE]; /* interface name */ 94 uint32_t pth_flags; /* flags */ 95 uint32_t pth_protocol_family; 96 uint32_t pth_frame_pre_length; 97 uint32_t pth_frame_post_length; 98 pid_t pth_pid; /* process ID */ 99 char pth_comm[MAXCOMLEN + 1]; /* process name */ 100 uint8_t pth_pad1; 101 uint16_t pth_trace_tag; 102 uint32_t pth_svc; /* service class */ 103 uint16_t pth_iftype; 104 uint16_t pth_ifunit; 105 pid_t pth_epid; /* effective process ID */ 106 char pth_ecomm[MAXCOMLEN + 1]; /* effective command name */ 107 uint8_t pth_pad2; 108 uint16_t pth_pad3; 109 uint32_t pth_flowid; 110 uint32_t pth_ipproto; 111 struct timeval32 pth_tstamp; 112 uuid_t pth_uuid; 113 uuid_t pth_euuid; 114 }; 115 116 #define PKTAP_HAS_TRACE_TAG 1 117 118 /* 119 * The original version 1 of the pktap_header structure always had the field 120 * pth_type_next set to PTH_TYPE_PACKET 121 */ 122 #define PTH_TYPE_NONE 0 /* No more data following */ 123 #define PTH_TYPE_PACKET 1 /* Actual captured packet data */ 124 #define PTH_TYPE_DROP 2 /* Dropped packet capture */ 125 126 /* 127 * Size of buffer that can contain any pktap header 128 * followed by the optional 4 bytes protocol field 129 * or 16 bytes link layer header 130 */ 131 union pktap_header_extra { 132 uint8_t llhdr[16]; 133 uint32_t proto; 134 }; 135 136 /* 137 * Version 2 version of the header 138 * 139 * The field pth_flags is at the same offset as the orignal pktap_header and 140 * the flag PTH_FLAG_V2_HDR allows to differentiate the header version. 141 */ 142 143 #define PKTAP_MAX_COMM_SIZE (MAXCOMLEN + 1) 144 145 struct pktap_v2_hdr { 146 uint8_t pth_length; /* length of this header */ 147 uint8_t pth_uuid_offset; /* max size: sizeof(uuid_t) */ 148 uint8_t pth_e_uuid_offset; /* max size: sizeof(uuid_t) */ 149 uint8_t pth_ifname_offset; /* max size: PKTAP_IFXNAMESIZE*/ 150 uint8_t pth_comm_offset; /* max size: PKTAP_MAX_COMM_SIZE */ 151 uint8_t pth_e_comm_offset; /* max size: PKTAP_MAX_COMM_SIZE */ 152 uint16_t pth_dlt; /* DLT of packet */ 153 uint16_t pth_frame_pre_length; 154 uint16_t pth_frame_post_length; 155 uint16_t pth_iftype; 156 uint16_t pth_ipproto; 157 uint32_t pth_protocol_family; 158 uint32_t pth_svc; /* service class */ 159 uint32_t pth_flowid; 160 pid_t pth_pid; /* process ID */ 161 pid_t pth_e_pid; /* effective process ID */ 162 uint32_t pth_flags; /* flags */ 163 }; 164 165 struct pktap_v2_hdr_space { 166 struct pktap_v2_hdr pth_hdr; 167 uint8_t pth_uuid[sizeof(uuid_t)]; 168 uint8_t pth_e_uuid[sizeof(uuid_t)]; 169 uint8_t pth_ifname[PKTAP_IFXNAMESIZE]; 170 uint8_t pth_comm[PKTAP_MAX_COMM_SIZE]; 171 uint8_t pth_e_comm[PKTAP_MAX_COMM_SIZE]; 172 }; 173 174 struct pktap_buffer_v2_hdr_extra { 175 struct pktap_v2_hdr_space hdr_space; 176 union pktap_header_extra extra; 177 }; 178 179 #define COPY_PKTAP_COMMON_FIELDS_TO_V2(pktap_v2_hdr_dst, pktap_header_src) { \ 180 (pktap_v2_hdr_dst)->pth_length = sizeof(struct pktap_v2_hdr); \ 181 (pktap_v2_hdr_dst)->pth_uuid_offset = 0; \ 182 (pktap_v2_hdr_dst)->pth_e_uuid_offset = 0; \ 183 (pktap_v2_hdr_dst)->pth_ifname_offset = 0; \ 184 (pktap_v2_hdr_dst)->pth_comm_offset = 0; \ 185 (pktap_v2_hdr_dst)->pth_e_comm_offset = 0; \ 186 (pktap_v2_hdr_dst)->pth_dlt = (uint16_t)(pktap_header_src)->pth_dlt; \ 187 (pktap_v2_hdr_dst)->pth_frame_pre_length = (uint16_t)(pktap_header_src)->pth_frame_pre_length; \ 188 (pktap_v2_hdr_dst)->pth_frame_post_length = (uint16_t)(pktap_header_src)->pth_frame_post_length; \ 189 (pktap_v2_hdr_dst)->pth_iftype = (pktap_header_src)->pth_iftype; \ 190 (pktap_v2_hdr_dst)->pth_ipproto = (uint16_t)(pktap_header_src)->pth_ipproto; \ 191 (pktap_v2_hdr_dst)->pth_protocol_family = (pktap_header_src)->pth_protocol_family; \ 192 (pktap_v2_hdr_dst)->pth_svc = (pktap_header_src)->pth_svc; \ 193 (pktap_v2_hdr_dst)->pth_flowid = (pktap_header_src)->pth_flowid; \ 194 (pktap_v2_hdr_dst)->pth_pid = (pktap_header_src)->pth_pid; \ 195 (pktap_v2_hdr_dst)->pth_e_pid = (pktap_header_src)->pth_epid; \ 196 (pktap_v2_hdr_dst)->pth_flags = (pktap_header_src)->pth_flags; \ 197 (pktap_v2_hdr_dst)->pth_flags |= PTH_FLAG_V2_HDR; \ 198 } 199 200 /* 201 * Values for field pth_flags 202 */ 203 #define PTH_FLAG_DIR_IN 0x00000001 /* Outgoing packet */ 204 #define PTH_FLAG_DIR_OUT 0x00000002 /* Incoming packet */ 205 #define PTH_FLAG_PROC_DELEGATED 0x00000004 /* Process delegated */ 206 #define PTH_FLAG_IF_DELEGATED 0x00000008 /* Interface delegated */ 207 #ifdef BSD_KERNEL_PRIVATE 208 #define PTH_FLAG_DELAY_PKTAP 0x00001000 /* Finalize pktap header on read */ 209 #endif /* BSD_KERNEL_PRIVATE */ 210 #define PTH_FLAG_TSTAMP 0x00002000 /* Has time stamp */ 211 #define PTH_FLAG_NEW_FLOW 0x00004000 /* Packet from a new flow */ 212 #define PTH_FLAG_REXMIT 0x00008000 /* Packet is a retransmission */ 213 #define PTH_FLAG_KEEP_ALIVE 0x00010000 /* Is keep alive packet */ 214 #define PTH_FLAG_SOCKET 0x00020000 /* Packet on a Socket */ 215 #define PTH_FLAG_NEXUS_CHAN 0x00040000 /* Packet on a nexus channel */ 216 #define PTH_FLAG_V2_HDR 0x00080000 /* Version 2 of pktap */ 217 #define PTH_FLAG_WAKE_PKT 0x00100000 /* Packet caused system to ake from sleep */ 218 219 #include <net/droptap.h> 220 221 #ifdef BSD_KERNEL_PRIVATE 222 223 #include <net/bpf.h> 224 225 struct pktap_header_buffer { 226 struct pktap_header pkth; 227 union pktap_header_extra extra; 228 }; 229 230 extern uint32_t pktap_total_tap_count; 231 232 extern void pktap_init(void); 233 extern void pktap_input(struct ifnet *, protocol_family_t, struct mbuf *, char *); 234 extern void pktap_output(struct ifnet *, protocol_family_t, struct mbuf *, 235 u_int32_t, u_int32_t); 236 extern void pktap_fill_proc_info(struct pktap_header *, protocol_family_t, 237 struct mbuf *, u_int32_t, int, struct ifnet *); 238 extern void pktap_finalize_proc_info(struct pktap_header *); 239 extern void pktap_v2_finalize_proc_info(struct pktap_v2_hdr *); 240 #if SKYWALK 241 #include <skywalk/os_skywalk.h> 242 extern void pktap_input_packet(struct ifnet *, protocol_family_t, uint32_t, 243 pid_t, const char *, pid_t, const char *, kern_packet_t, 244 const void *__sized_by(header_len) header, size_t header_len, 245 uint8_t, uint32_t, uint32_t); 246 extern void pktap_output_packet(struct ifnet *, protocol_family_t, uint32_t, 247 pid_t, const char *, pid_t, const char *, kern_packet_t, 248 const void *__sized_by(header_len) header, size_t header_len, 249 uint8_t, uint32_t, uint32_t); 250 #endif /* SKYWALK */ 251 extern void convert_to_pktap_header_to_v2(struct bpf_packet *bpf_pkt, bool truncate); 252 #endif /* BSD_KERNEL_PRIVATE */ 253 #endif /* PRIVATE */ 254 255 #endif /* _NET_PKTAP_H_ */ 256