xref: /xnu-11417.140.69/bsd/net/pktap.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
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_ipproto;
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_comp_gencnt;
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 #define PKTAP_HAS_COMP_GENCNT 1
118 
119 /*
120  * The original version 1 of the pktap_header structure always had the field
121  * pth_type_next set to PTH_TYPE_PACKET
122  */
123 #define PTH_TYPE_NONE   0               /* No more data following */
124 #define PTH_TYPE_PACKET 1               /* Actual captured packet data */
125 #define PTH_TYPE_DROP   2               /* Dropped packet capture */
126 
127 /*
128  * Size of buffer that can contain any pktap header
129  * followed by the optional 4 bytes protocol field
130  * or 16 bytes link layer header
131  */
132 union pktap_header_extra {
133 	uint8_t         llhdr[16];
134 	uint32_t        proto;
135 };
136 
137 /*
138  * Version 2 version of the header
139  *
140  * The field pth_flags is at the same offset as the orignal pktap_header and
141  * the flag PTH_FLAG_V2_HDR allows to differentiate the header version.
142  */
143 
144 #define PKTAP_MAX_COMM_SIZE (MAXCOMLEN + 1)
145 
146 struct pktap_v2_hdr {
147 	uint8_t                 pth_length;                     /* length of this header */
148 	uint8_t                 pth_uuid_offset;                /* max size: sizeof(uuid_t) */
149 	uint8_t                 pth_e_uuid_offset;              /* max size: sizeof(uuid_t) */
150 	uint8_t                 pth_ifname_offset;              /* max size: PKTAP_IFXNAMESIZE*/
151 	uint8_t                 pth_comm_offset;                /* max size: PKTAP_MAX_COMM_SIZE */
152 	uint8_t                 pth_e_comm_offset;              /* max size: PKTAP_MAX_COMM_SIZE */
153 	uint16_t                pth_dlt;                        /* DLT of packet */
154 	uint16_t                pth_frame_pre_length;
155 	uint16_t                pth_frame_post_length;
156 	uint16_t                pth_iftype;
157 	uint16_t                pth_ipproto;
158 	uint32_t                pth_protocol_family;
159 	uint32_t                pth_svc;                        /* service class */
160 	uint32_t                pth_flowid;
161 	pid_t                   pth_pid;                        /* process ID */
162 	pid_t                   pth_e_pid;                      /* effective process ID */
163 	uint32_t                pth_flags;                      /* flags */
164 };
165 
166 struct pktap_v2_hdr_space {
167 	struct pktap_v2_hdr pth_hdr;
168 	uint8_t pth_uuid[sizeof(uuid_t)];
169 	uint8_t pth_e_uuid[sizeof(uuid_t)];
170 	uint8_t pth_ifname[PKTAP_IFXNAMESIZE];
171 	uint8_t pth_comm[PKTAP_MAX_COMM_SIZE];
172 	uint8_t pth_e_comm[PKTAP_MAX_COMM_SIZE];
173 };
174 
175 struct pktap_buffer_v2_hdr_extra {
176 	struct pktap_v2_hdr_space hdr_space;
177 	union pktap_header_extra extra;
178 };
179 
180 #define COPY_PKTAP_COMMON_FIELDS_TO_V2(pktap_v2_hdr_dst, pktap_header_src) { \
181 	(pktap_v2_hdr_dst)->pth_length = sizeof(struct pktap_v2_hdr); \
182 	(pktap_v2_hdr_dst)->pth_uuid_offset = 0; \
183 	(pktap_v2_hdr_dst)->pth_e_uuid_offset = 0; \
184 	(pktap_v2_hdr_dst)->pth_ifname_offset = 0; \
185 	(pktap_v2_hdr_dst)->pth_comm_offset = 0; \
186 	(pktap_v2_hdr_dst)->pth_e_comm_offset = 0; \
187 	(pktap_v2_hdr_dst)->pth_dlt = (uint16_t)(pktap_header_src)->pth_dlt; \
188 	(pktap_v2_hdr_dst)->pth_frame_pre_length = (uint16_t)(pktap_header_src)->pth_frame_pre_length; \
189 	(pktap_v2_hdr_dst)->pth_frame_post_length = (uint16_t)(pktap_header_src)->pth_frame_post_length; \
190 	(pktap_v2_hdr_dst)->pth_iftype = (pktap_header_src)->pth_iftype; \
191 	(pktap_v2_hdr_dst)->pth_ipproto = (uint16_t)(pktap_header_src)->pth_ipproto; \
192 	(pktap_v2_hdr_dst)->pth_protocol_family = (pktap_header_src)->pth_protocol_family; \
193 	(pktap_v2_hdr_dst)->pth_svc = (pktap_header_src)->pth_svc; \
194 	(pktap_v2_hdr_dst)->pth_flowid = (pktap_header_src)->pth_flowid; \
195 	(pktap_v2_hdr_dst)->pth_pid = (pktap_header_src)->pth_pid; \
196 	(pktap_v2_hdr_dst)->pth_e_pid = (pktap_header_src)->pth_epid; \
197 	(pktap_v2_hdr_dst)->pth_flags = (pktap_header_src)->pth_flags; \
198 	(pktap_v2_hdr_dst)->pth_flags |= PTH_FLAG_V2_HDR; \
199 }
200 
201 /*
202  * Values for field pth_flags
203  */
204 #define PTH_FLAG_DIR_IN         0x00000001 /* Outgoing packet */
205 #define PTH_FLAG_DIR_OUT        0x00000002 /* Incoming packet */
206 #define PTH_FLAG_PROC_DELEGATED 0x00000004 /* Process delegated */
207 #define PTH_FLAG_IF_DELEGATED   0x00000008 /* Interface delegated */
208 #ifdef BSD_KERNEL_PRIVATE
209 #define PTH_FLAG_DELAY_PKTAP    0x00001000 /* Finalize pktap header on read */
210 #endif /* BSD_KERNEL_PRIVATE */
211 #define PTH_FLAG_TSTAMP         0x00002000 /* Has time stamp */
212 #define PTH_FLAG_NEW_FLOW       0x00004000 /* Packet from a new flow */
213 #define PTH_FLAG_REXMIT         0x00008000 /* Packet is a retransmission */
214 #define PTH_FLAG_KEEP_ALIVE     0x00010000 /* Is keep alive packet */
215 #define PTH_FLAG_SOCKET         0x00020000 /* Packet on a Socket */
216 #define PTH_FLAG_NEXUS_CHAN     0x00040000 /* Packet on a nexus channel */
217 #define PTH_FLAG_V2_HDR         0x00080000 /* Version 2 of pktap */
218 #define PTH_FLAG_WAKE_PKT       0x00100000 /* Packet caused system to ake from sleep */
219 
220 #include <net/droptap.h>
221 
222 #ifdef BSD_KERNEL_PRIVATE
223 
224 #include <net/bpf.h>
225 
226 struct pktap_header_buffer {
227 	struct pktap_header             pkth;
228 	union pktap_header_extra        extra;
229 };
230 
231 extern uint32_t pktap_total_tap_count;
232 
233 extern void pktap_init(void);
234 extern void pktap_input(struct ifnet *, protocol_family_t, struct mbuf *, char *);
235 extern void pktap_output(struct ifnet *, protocol_family_t, struct mbuf *,
236     u_int32_t, u_int32_t);
237 extern void pktap_fill_proc_info(struct pktap_header *, protocol_family_t,
238     struct mbuf *, u_int32_t, int, struct ifnet *);
239 extern void pktap_finalize_proc_info(struct pktap_header *);
240 extern void pktap_v2_finalize_proc_info(struct pktap_v2_hdr *);
241 #if SKYWALK
242 #include <skywalk/os_skywalk.h>
243 extern void pktap_input_packet(struct ifnet *, protocol_family_t, uint32_t,
244     pid_t, const char *, pid_t, const char *, kern_packet_t,
245     const void *__sized_by(header_len) header, size_t header_len,
246     uint8_t, uint32_t, uint32_t);
247 extern void pktap_output_packet(struct ifnet *, protocol_family_t, uint32_t,
248     pid_t, const char *, pid_t, const char *, kern_packet_t,
249     const void *__sized_by(header_len) header, size_t header_len,
250     uint8_t, uint32_t, uint32_t);
251 #endif /* SKYWALK */
252 extern void convert_to_pktap_header_to_v2(struct bpf_packet *bpf_pkt, bool truncate);
253 #endif /* BSD_KERNEL_PRIVATE */
254 #endif /* PRIVATE */
255 
256 #endif /* _NET_PKTAP_H_ */
257