]>
Commit | Line | Data |
---|---|---|
39236c6e | 1 | /* |
f427ee49 | 2 | * Copyright (c) 2012-2020 Apple Inc. All rights reserved. |
39236c6e A |
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 | #include <kern/locks.h> | |
d9a64523 | 30 | #include <kern/zalloc.h> |
39236c6e A |
31 | |
32 | #include <sys/types.h> | |
33 | #include <sys/kernel_types.h> | |
34 | #include <sys/kauth.h> | |
35 | #include <sys/socket.h> | |
36 | #include <sys/socketvar.h> | |
37 | #include <sys/sockio.h> | |
38 | #include <sys/sysctl.h> | |
fe8ab488 | 39 | #include <sys/proc.h> |
39236c6e A |
40 | |
41 | #include <net/if.h> | |
42 | #include <net/if_var.h> | |
43 | #include <net/if_types.h> | |
44 | #include <net/bpf.h> | |
45 | #include <net/net_osdep.h> | |
46 | #include <net/pktap.h> | |
47 | ||
48 | #include <netinet/in_pcb.h> | |
49 | #include <netinet/tcp.h> | |
50 | #include <netinet/tcp_var.h> | |
0a7de745 | 51 | #define _IP_VHL |
39236c6e A |
52 | #include <netinet/ip.h> |
53 | #include <netinet/ip_var.h> | |
54 | #include <netinet/udp.h> | |
55 | #include <netinet/udp_var.h> | |
56 | ||
57 | #include <netinet/ip6.h> | |
58 | #include <netinet6/in6_pcb.h> | |
59 | ||
60 | #include <libkern/OSAtomic.h> | |
61 | ||
62 | #include <kern/debug.h> | |
63 | ||
64 | #include <sys/mcache.h> | |
65 | ||
66 | #include <string.h> | |
67 | ||
68 | extern struct inpcbinfo ripcbinfo; | |
69 | ||
70 | struct pktap_softc { | |
0a7de745 A |
71 | LIST_ENTRY(pktap_softc) pktp_link; |
72 | uint32_t pktp_unit; | |
73 | uint32_t pktp_dlt_raw_count; | |
74 | uint32_t pktp_dlt_pkttap_count; | |
75 | struct ifnet *pktp_ifp; | |
76 | struct pktap_filter pktp_filters[PKTAP_MAX_FILTERS]; | |
39236c6e A |
77 | }; |
78 | ||
79 | #ifndef PKTAP_DEBUG | |
0a7de745 | 80 | #define PKTAP_DEBUG 0 |
39236c6e A |
81 | #endif /* PKTAP_DEBUG */ |
82 | ||
0a7de745 A |
83 | #define PKTAP_FILTER_OK 0 /* Packet passes filter checks */ |
84 | #define PKTAP_FILTER_SKIP 1 /* Do not tap this packet */ | |
39236c6e A |
85 | |
86 | static int pktap_inited = 0; | |
87 | ||
88 | SYSCTL_DECL(_net_link); | |
fe8ab488 | 89 | SYSCTL_NODE(_net_link, IFT_PKTAP, pktap, |
0a7de745 | 90 | CTLFLAG_RW | CTLFLAG_LOCKED, 0, "pktap virtual interface"); |
39236c6e | 91 | |
5ba3f43e A |
92 | uint32_t pktap_total_tap_count = 0; |
93 | SYSCTL_UINT(_net_link_pktap, OID_AUTO, total_tap_count, | |
fe8ab488 | 94 | CTLFLAG_RD | CTLFLAG_LOCKED, &pktap_total_tap_count, 0, ""); |
39236c6e A |
95 | |
96 | static u_int64_t pktap_count_unknown_if_type = 0; | |
fe8ab488 A |
97 | SYSCTL_QUAD(_net_link_pktap, OID_AUTO, count_unknown_if_type, |
98 | CTLFLAG_RD | CTLFLAG_LOCKED, &pktap_count_unknown_if_type, ""); | |
39236c6e A |
99 | |
100 | static int pktap_log = 0; | |
fe8ab488 A |
101 | SYSCTL_INT(_net_link_pktap, OID_AUTO, log, |
102 | CTLFLAG_RW | CTLFLAG_LOCKED, &pktap_log, 0, ""); | |
39236c6e | 103 | |
0a7de745 | 104 | #define PKTAP_LOG(mask, fmt, ...) \ |
39236c6e | 105 | do { \ |
fe8ab488 | 106 | if ((pktap_log & mask)) \ |
0a7de745 | 107 | printf("%s:%d " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
fe8ab488 | 108 | } while (false) |
39236c6e | 109 | |
0a7de745 A |
110 | #define PKTP_LOG_FUNC 0x01 |
111 | #define PKTP_LOG_FILTER 0x02 | |
112 | #define PKTP_LOG_INPUT 0x04 | |
113 | #define PKTP_LOG_OUTPUT 0x08 | |
114 | #define PKTP_LOG_ERROR 0x10 | |
115 | #define PKTP_LOG_NOPCB 0x20 | |
39236c6e A |
116 | |
117 | /* | |
118 | * pktap_lck_rw protects the global list of pktap interfaces | |
119 | */ | |
120 | decl_lck_rw_data(static, pktap_lck_rw_data); | |
121 | static lck_rw_t *pktap_lck_rw = &pktap_lck_rw_data; | |
122 | static lck_grp_t *pktap_lck_grp = NULL; | |
123 | static lck_attr_t *pktap_lck_attr = NULL; | |
124 | ||
fe8ab488 A |
125 | static LIST_HEAD(pktap_list, pktap_softc) pktap_list = |
126 | LIST_HEAD_INITIALIZER(pktap_list); | |
39236c6e A |
127 | |
128 | int pktap_clone_create(struct if_clone *, u_int32_t, void *); | |
129 | int pktap_clone_destroy(struct ifnet *); | |
130 | ||
0a7de745 A |
131 | #define PKTAP_MAXUNIT IF_MAXUNIT |
132 | #define PKTAP_ZONE_MAX_ELEM MIN(IFNETS_MAX, PKTAP_MAXUNIT) | |
d9a64523 | 133 | |
fe8ab488 | 134 | static struct if_clone pktap_cloner = |
0a7de745 A |
135 | IF_CLONE_INITIALIZER(PKTAP_IFNAME, |
136 | pktap_clone_create, | |
137 | pktap_clone_destroy, | |
138 | 0, | |
139 | PKTAP_MAXUNIT, | |
140 | PKTAP_ZONE_MAX_ELEM, | |
141 | sizeof(struct pktap_softc)); | |
39236c6e A |
142 | |
143 | errno_t pktap_if_output(ifnet_t, mbuf_t); | |
fe8ab488 A |
144 | errno_t pktap_demux(ifnet_t, mbuf_t, char *, protocol_family_t *); |
145 | errno_t pktap_add_proto(ifnet_t, protocol_family_t, | |
0a7de745 | 146 | const struct ifnet_demux_desc *, u_int32_t); |
39236c6e A |
147 | errno_t pktap_del_proto(ifnet_t, protocol_family_t); |
148 | errno_t pktap_getdrvspec(ifnet_t, struct ifdrv64 *); | |
149 | errno_t pktap_setdrvspec(ifnet_t, struct ifdrv64 *); | |
150 | errno_t pktap_ioctl(ifnet_t, unsigned long, void *); | |
151 | void pktap_detach(ifnet_t); | |
152 | int pktap_filter_evaluate(struct pktap_softc *, struct ifnet *); | |
fe8ab488 A |
153 | void pktap_bpf_tap(struct ifnet *, protocol_family_t, struct mbuf *, |
154 | u_int32_t, u_int32_t, int); | |
155 | errno_t pktap_tap_callback(ifnet_t, u_int32_t, bpf_tap_mode); | |
39236c6e A |
156 | |
157 | static void | |
158 | pktap_hexdump(int mask, void *addr, size_t len) | |
159 | { | |
160 | unsigned char *buf = addr; | |
161 | size_t i; | |
fe8ab488 | 162 | |
0a7de745 | 163 | if (!(pktap_log & mask)) { |
39236c6e | 164 | return; |
0a7de745 | 165 | } |
fe8ab488 | 166 | |
39236c6e A |
167 | for (i = 0; i < len; i++) { |
168 | unsigned char h = (buf[i] & 0xf0) >> 4; | |
169 | unsigned char l = buf[i] & 0x0f; | |
fe8ab488 | 170 | |
39236c6e | 171 | if (i != 0) { |
0a7de745 | 172 | if (i % 32 == 0) { |
39236c6e | 173 | printf("\n"); |
0a7de745 | 174 | } else if (i % 4 == 0) { |
39236c6e | 175 | printf(" "); |
0a7de745 | 176 | } |
39236c6e | 177 | } |
fe8ab488 | 178 | printf("%c%c", |
0a7de745 A |
179 | h < 10 ? h + '0' : h - 10 + 'a', |
180 | l < 10 ? l + '0' : l - 10 + 'a'); | |
39236c6e | 181 | } |
0a7de745 | 182 | if (i % 32 != 0) { |
39236c6e | 183 | printf("\n"); |
0a7de745 | 184 | } |
39236c6e A |
185 | } |
186 | ||
d9a64523 A |
187 | #define _CASSERT_OFFFSETOF_FIELD(s1, s2, f) \ |
188 | _CASSERT(offsetof(struct s1, f) == offsetof(struct s2, f)) | |
189 | ||
39236c6e A |
190 | __private_extern__ void |
191 | pktap_init(void) | |
192 | { | |
193 | int error = 0; | |
194 | lck_grp_attr_t *lck_grp_attr = NULL; | |
fe8ab488 | 195 | |
d9a64523 A |
196 | _CASSERT_OFFFSETOF_FIELD(pktap_header, pktap_v2_hdr, pth_flags); |
197 | ||
39236c6e A |
198 | /* Make sure we're called only once */ |
199 | VERIFY(pktap_inited == 0); | |
200 | ||
201 | pktap_inited = 1; | |
fe8ab488 | 202 | |
39236c6e A |
203 | lck_grp_attr = lck_grp_attr_alloc_init(); |
204 | pktap_lck_grp = lck_grp_alloc_init("pktap", lck_grp_attr); | |
205 | pktap_lck_attr = lck_attr_alloc_init(); | |
206 | #if PKTAP_DEBUG | |
207 | lck_attr_setdebug(pktap_lck_attr); | |
208 | #endif /* PKTAP_DEBUG */ | |
209 | lck_rw_init(pktap_lck_rw, pktap_lck_grp, pktap_lck_attr); | |
210 | lck_grp_attr_free(lck_grp_attr); | |
211 | ||
212 | LIST_INIT(&pktap_list); | |
fe8ab488 | 213 | |
39236c6e | 214 | error = if_clone_attach(&pktap_cloner); |
0a7de745 | 215 | if (error != 0) { |
fe8ab488 A |
216 | panic("%s: if_clone_attach() failed, error %d\n", |
217 | __func__, error); | |
0a7de745 | 218 | } |
39236c6e A |
219 | } |
220 | ||
221 | __private_extern__ int | |
222 | pktap_clone_create(struct if_clone *ifc, u_int32_t unit, __unused void *params) | |
223 | { | |
224 | int error = 0; | |
225 | struct pktap_softc *pktap = NULL; | |
5ba3f43e | 226 | struct ifnet_init_eparams if_init; |
39236c6e A |
227 | |
228 | PKTAP_LOG(PKTP_LOG_FUNC, "unit %u\n", unit); | |
fe8ab488 | 229 | |
d9a64523 | 230 | pktap = if_clone_softc_allocate(&pktap_cloner); |
39236c6e A |
231 | if (pktap == NULL) { |
232 | printf("%s: _MALLOC failed\n", __func__); | |
233 | error = ENOMEM; | |
234 | goto done; | |
235 | } | |
236 | pktap->pktp_unit = unit; | |
237 | ||
238 | /* | |
239 | * By default accept packet from physical interfaces | |
240 | */ | |
241 | pktap->pktp_filters[0].filter_op = PKTAP_FILTER_OP_PASS; | |
242 | pktap->pktp_filters[0].filter_param = PKTAP_FILTER_PARAM_IF_TYPE; | |
243 | pktap->pktp_filters[0].filter_param_if_type = IFT_ETHER; | |
244 | ||
f427ee49 | 245 | #if !XNU_TARGET_OS_OSX |
5ba3f43e A |
246 | pktap->pktp_filters[1].filter_op = PKTAP_FILTER_OP_PASS; |
247 | pktap->pktp_filters[1].filter_param = PKTAP_FILTER_PARAM_IF_TYPE; | |
248 | pktap->pktp_filters[1].filter_param_if_type = IFT_CELLULAR; | |
f427ee49 | 249 | #else /* XNU_TARGET_OS_OSX */ |
39236c6e A |
250 | pktap->pktp_filters[1].filter_op = PKTAP_FILTER_OP_PASS; |
251 | pktap->pktp_filters[1].filter_param = PKTAP_FILTER_PARAM_IF_TYPE; | |
252 | pktap->pktp_filters[1].filter_param_if_type = IFT_IEEE1394; | |
f427ee49 | 253 | #endif /* XNU_TARGET_OS_OSX */ |
39037602 | 254 | |
39037602 A |
255 | pktap->pktp_filters[2].filter_op = PKTAP_FILTER_OP_PASS; |
256 | pktap->pktp_filters[2].filter_param = PKTAP_FILTER_PARAM_IF_TYPE; | |
257 | pktap->pktp_filters[2].filter_param_if_type = IFT_OTHER; | |
39037602 | 258 | |
39236c6e | 259 | /* |
fe8ab488 | 260 | * We do not use a set_bpf_tap() function as we rather rely on the more |
39236c6e A |
261 | * accurate callback passed to bpf_attach() |
262 | */ | |
5ba3f43e A |
263 | bzero(&if_init, sizeof(if_init)); |
264 | if_init.ver = IFNET_INIT_CURRENT_VERSION; | |
0a7de745 | 265 | if_init.len = sizeof(if_init); |
5ba3f43e | 266 | if_init.flags = IFNET_INIT_LEGACY; |
39236c6e A |
267 | if_init.name = ifc->ifc_name; |
268 | if_init.unit = unit; | |
269 | if_init.type = IFT_PKTAP; | |
270 | if_init.family = IFNET_FAMILY_LOOPBACK; | |
271 | if_init.output = pktap_if_output; | |
272 | if_init.demux = pktap_demux; | |
273 | if_init.add_proto = pktap_add_proto; | |
274 | if_init.del_proto = pktap_del_proto; | |
275 | if_init.softc = pktap; | |
276 | if_init.ioctl = pktap_ioctl; | |
277 | if_init.detach = pktap_detach; | |
278 | ||
5ba3f43e | 279 | error = ifnet_allocate_extended(&if_init, &pktap->pktp_ifp); |
39236c6e | 280 | if (error != 0) { |
fe8ab488 A |
281 | printf("%s: ifnet_allocate failed, error %d\n", |
282 | __func__, error); | |
39236c6e A |
283 | goto done; |
284 | } | |
fe8ab488 | 285 | |
39236c6e | 286 | ifnet_set_flags(pktap->pktp_ifp, IFF_UP, IFF_UP); |
fe8ab488 | 287 | |
39236c6e A |
288 | error = ifnet_attach(pktap->pktp_ifp, NULL); |
289 | if (error != 0) { | |
290 | printf("%s: ifnet_attach failed - error %d\n", __func__, error); | |
291 | ifnet_release(pktap->pktp_ifp); | |
292 | goto done; | |
293 | } | |
fe8ab488 | 294 | |
39236c6e | 295 | /* Attach DLT_PKTAP as the default DLT */ |
fe8ab488 A |
296 | bpf_attach(pktap->pktp_ifp, DLT_PKTAP, sizeof(struct pktap_header), |
297 | NULL, pktap_tap_callback); | |
39236c6e | 298 | bpf_attach(pktap->pktp_ifp, DLT_RAW, 0, NULL, pktap_tap_callback); |
fe8ab488 | 299 | |
39236c6e A |
300 | /* Take a reference and add to the global list */ |
301 | ifnet_reference(pktap->pktp_ifp); | |
302 | lck_rw_lock_exclusive(pktap_lck_rw); | |
303 | LIST_INSERT_HEAD(&pktap_list, pktap, pktp_link); | |
304 | lck_rw_done(pktap_lck_rw); | |
305 | done: | |
0a7de745 | 306 | if (error != 0 && pktap != NULL) { |
d9a64523 | 307 | if_clone_softc_deallocate(&pktap_cloner, pktap); |
0a7de745 A |
308 | } |
309 | return error; | |
39236c6e A |
310 | } |
311 | ||
312 | __private_extern__ int | |
313 | pktap_clone_destroy(struct ifnet *ifp) | |
314 | { | |
315 | int error = 0; | |
316 | ||
317 | PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname); | |
318 | ||
319 | (void) ifnet_detach(ifp); | |
fe8ab488 | 320 | |
0a7de745 | 321 | return error; |
39236c6e A |
322 | } |
323 | ||
324 | /* | |
325 | * This function is called whenever a DLT is set on the interface: | |
fe8ab488 A |
326 | * - When interface is attached to a BPF device via BIOCSETIF for the |
327 | * default DLT | |
39236c6e A |
328 | * - Whenever a new DLT is selected via BIOCSDLT |
329 | * - When the interface is detached from a BPF device (direction is zero) | |
330 | */ | |
331 | __private_extern__ errno_t | |
332 | pktap_tap_callback(ifnet_t ifp, u_int32_t dlt, bpf_tap_mode direction) | |
333 | { | |
334 | struct pktap_softc *pktap; | |
335 | ||
336 | pktap = ifp->if_softc; | |
337 | if (pktap == NULL) { | |
338 | printf("%s: if_softc is NULL for ifp %s\n", __func__, | |
339 | ifp->if_xname); | |
340 | goto done; | |
341 | } | |
342 | switch (dlt) { | |
0a7de745 A |
343 | case DLT_RAW: |
344 | if (direction == 0) { | |
345 | if (pktap->pktp_dlt_raw_count > 0) { | |
346 | pktap->pktp_dlt_raw_count--; | |
347 | OSAddAtomic(-1, &pktap_total_tap_count); | |
39236c6e | 348 | } |
0a7de745 A |
349 | } else { |
350 | pktap->pktp_dlt_raw_count++; | |
351 | OSAddAtomic(1, &pktap_total_tap_count); | |
352 | } | |
353 | break; | |
354 | case DLT_PKTAP: | |
355 | if (direction == 0) { | |
356 | if (pktap->pktp_dlt_pkttap_count > 0) { | |
357 | pktap->pktp_dlt_pkttap_count--; | |
358 | OSAddAtomic(-1, &pktap_total_tap_count); | |
39236c6e | 359 | } |
0a7de745 A |
360 | } else { |
361 | pktap->pktp_dlt_pkttap_count++; | |
362 | OSAddAtomic(1, &pktap_total_tap_count); | |
363 | } | |
364 | break; | |
39236c6e A |
365 | } |
366 | done: | |
fe8ab488 A |
367 | /* |
368 | * Attachements count must be positive and we're in trouble | |
39236c6e A |
369 | * if we have more that 2**31 attachements |
370 | */ | |
371 | VERIFY(pktap_total_tap_count >= 0); | |
372 | ||
0a7de745 | 373 | return 0; |
39236c6e A |
374 | } |
375 | ||
376 | __private_extern__ errno_t | |
377 | pktap_if_output(ifnet_t ifp, mbuf_t m) | |
378 | { | |
379 | PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname); | |
380 | mbuf_freem(m); | |
0a7de745 | 381 | return ENOTSUP; |
39236c6e A |
382 | } |
383 | ||
384 | __private_extern__ errno_t | |
fe8ab488 | 385 | pktap_demux(ifnet_t ifp, __unused mbuf_t m, __unused char *header, |
0a7de745 | 386 | __unused protocol_family_t *ppf) |
39236c6e A |
387 | { |
388 | PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname); | |
0a7de745 | 389 | return ENOTSUP; |
39236c6e A |
390 | } |
391 | ||
392 | __private_extern__ errno_t | |
393 | pktap_add_proto(__unused ifnet_t ifp, protocol_family_t pf, | |
394 | __unused const struct ifnet_demux_desc *dmx, __unused u_int32_t cnt) | |
395 | { | |
396 | PKTAP_LOG(PKTP_LOG_FUNC, "%s pf %u\n", ifp->if_xname, pf); | |
0a7de745 | 397 | return 0; |
39236c6e A |
398 | } |
399 | ||
400 | __private_extern__ errno_t | |
401 | pktap_del_proto(__unused ifnet_t ifp, __unused protocol_family_t pf) | |
402 | { | |
403 | PKTAP_LOG(PKTP_LOG_FUNC, "%s pf %u\n", ifp->if_xname, pf); | |
0a7de745 | 404 | return 0; |
39236c6e A |
405 | } |
406 | ||
407 | __private_extern__ errno_t | |
408 | pktap_getdrvspec(ifnet_t ifp, struct ifdrv64 *ifd) | |
409 | { | |
410 | errno_t error = 0; | |
411 | struct pktap_softc *pktap; | |
412 | int i; | |
413 | ||
414 | PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname); | |
fe8ab488 | 415 | |
39236c6e A |
416 | pktap = ifp->if_softc; |
417 | if (pktap == NULL) { | |
418 | error = ENOENT; | |
419 | printf("%s: pktap NULL - error %d\n", __func__, error); | |
420 | goto done; | |
421 | } | |
422 | ||
423 | switch (ifd->ifd_cmd) { | |
424 | case PKTP_CMD_FILTER_GET: { | |
425 | struct x_pktap_filter x_filters[PKTAP_MAX_FILTERS]; | |
fe8ab488 | 426 | |
39236c6e A |
427 | bzero(&x_filters, sizeof(x_filters)); |
428 | ||
429 | if (ifd->ifd_len < PKTAP_MAX_FILTERS * sizeof(struct x_pktap_filter)) { | |
fe8ab488 | 430 | printf("%s: PKTP_CMD_FILTER_GET ifd_len %llu too small - error %d\n", |
0a7de745 | 431 | __func__, ifd->ifd_len, error); |
39236c6e A |
432 | error = EINVAL; |
433 | break; | |
434 | } | |
435 | for (i = 0; i < PKTAP_MAX_FILTERS; i++) { | |
436 | struct pktap_filter *pktap_filter = pktap->pktp_filters + i; | |
437 | struct x_pktap_filter *x_filter = x_filters + i; | |
fe8ab488 | 438 | |
39236c6e A |
439 | x_filter->filter_op = pktap_filter->filter_op; |
440 | x_filter->filter_param = pktap_filter->filter_param; | |
fe8ab488 | 441 | |
0a7de745 | 442 | if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_TYPE) { |
39236c6e | 443 | x_filter->filter_param_if_type = pktap_filter->filter_param_if_type; |
0a7de745 | 444 | } else if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_NAME) { |
39236c6e | 445 | strlcpy(x_filter->filter_param_if_name, |
0a7de745 A |
446 | pktap_filter->filter_param_if_name, |
447 | sizeof(x_filter->filter_param_if_name)); | |
448 | } | |
39236c6e | 449 | } |
f427ee49 | 450 | error = copyout(x_filters, CAST_USER_ADDR_T(ifd->ifd_data), |
0a7de745 | 451 | PKTAP_MAX_FILTERS * sizeof(struct x_pktap_filter)); |
39236c6e A |
452 | if (error) { |
453 | printf("%s: PKTP_CMD_FILTER_GET copyout - error %d\n", __func__, error); | |
454 | goto done; | |
455 | } | |
456 | break; | |
457 | } | |
458 | case PKTP_CMD_TAP_COUNT: { | |
459 | uint32_t tap_count = pktap->pktp_dlt_raw_count + pktap->pktp_dlt_pkttap_count; | |
fe8ab488 | 460 | |
39236c6e | 461 | if (ifd->ifd_len < sizeof(tap_count)) { |
fe8ab488 | 462 | printf("%s: PKTP_CMD_TAP_COUNT ifd_len %llu too small - error %d\n", |
0a7de745 | 463 | __func__, ifd->ifd_len, error); |
39236c6e A |
464 | error = EINVAL; |
465 | break; | |
466 | } | |
f427ee49 | 467 | error = copyout(&tap_count, CAST_USER_ADDR_T(ifd->ifd_data), sizeof(tap_count)); |
39236c6e A |
468 | if (error) { |
469 | printf("%s: PKTP_CMD_TAP_COUNT copyout - error %d\n", __func__, error); | |
470 | goto done; | |
471 | } | |
472 | break; | |
473 | } | |
474 | default: | |
475 | error = EINVAL; | |
476 | break; | |
477 | } | |
478 | ||
479 | done: | |
0a7de745 | 480 | return error; |
39236c6e A |
481 | } |
482 | ||
483 | __private_extern__ errno_t | |
484 | pktap_setdrvspec(ifnet_t ifp, struct ifdrv64 *ifd) | |
485 | { | |
486 | errno_t error = 0; | |
487 | struct pktap_softc *pktap; | |
488 | ||
489 | PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname); | |
fe8ab488 | 490 | |
39236c6e A |
491 | pktap = ifp->if_softc; |
492 | if (pktap == NULL) { | |
493 | error = ENOENT; | |
494 | printf("%s: pktap NULL - error %d\n", __func__, error); | |
495 | goto done; | |
496 | } | |
497 | ||
498 | switch (ifd->ifd_cmd) { | |
499 | case PKTP_CMD_FILTER_SET: { | |
500 | struct x_pktap_filter user_filters[PKTAP_MAX_FILTERS]; | |
501 | int i; | |
502 | int got_op_none = 0; | |
fe8ab488 | 503 | |
39236c6e | 504 | if (ifd->ifd_len != PKTAP_MAX_FILTERS * sizeof(struct x_pktap_filter)) { |
fe8ab488 | 505 | printf("%s: PKTP_CMD_FILTER_SET bad ifd_len %llu - error %d\n", |
0a7de745 | 506 | __func__, ifd->ifd_len, error); |
39236c6e A |
507 | error = EINVAL; |
508 | break; | |
509 | } | |
f427ee49 | 510 | error = copyin(CAST_USER_ADDR_T(ifd->ifd_data), &user_filters, (size_t)ifd->ifd_len); |
39236c6e A |
511 | if (error) { |
512 | printf("%s: copyin - error %d\n", __func__, error); | |
513 | goto done; | |
514 | } | |
515 | /* | |
516 | * Validate user provided parameters | |
517 | */ | |
518 | for (i = 0; i < PKTAP_MAX_FILTERS; i++) { | |
519 | struct x_pktap_filter *x_filter = user_filters + i; | |
fe8ab488 | 520 | |
39236c6e | 521 | switch (x_filter->filter_op) { |
0a7de745 A |
522 | case PKTAP_FILTER_OP_NONE: |
523 | /* Following entries must be PKTAP_FILTER_OP_NONE */ | |
524 | got_op_none = 1; | |
525 | break; | |
526 | case PKTAP_FILTER_OP_PASS: | |
527 | case PKTAP_FILTER_OP_SKIP: | |
528 | /* Invalid after PKTAP_FILTER_OP_NONE */ | |
529 | if (got_op_none) { | |
39236c6e A |
530 | error = EINVAL; |
531 | break; | |
0a7de745 A |
532 | } |
533 | break; | |
534 | default: | |
535 | error = EINVAL; | |
536 | break; | |
39236c6e | 537 | } |
0a7de745 | 538 | if (error != 0) { |
39236c6e | 539 | break; |
0a7de745 | 540 | } |
fe8ab488 | 541 | |
39236c6e | 542 | switch (x_filter->filter_param) { |
0a7de745 A |
543 | case PKTAP_FILTER_OP_NONE: |
544 | if (x_filter->filter_op != PKTAP_FILTER_OP_NONE) { | |
545 | error = EINVAL; | |
39236c6e | 546 | break; |
0a7de745 A |
547 | } |
548 | break; | |
39236c6e | 549 | |
0a7de745 A |
550 | /* |
551 | * Do not allow to tap a pktap from a pktap | |
552 | */ | |
553 | case PKTAP_FILTER_PARAM_IF_TYPE: | |
554 | if (x_filter->filter_param_if_type == IFT_PKTAP || | |
555 | x_filter->filter_param_if_type > 0xff) { | |
556 | error = EINVAL; | |
39236c6e | 557 | break; |
0a7de745 A |
558 | } |
559 | break; | |
39236c6e | 560 | |
0a7de745 A |
561 | case PKTAP_FILTER_PARAM_IF_NAME: |
562 | if (strncmp(x_filter->filter_param_if_name, PKTAP_IFNAME, | |
563 | strlen(PKTAP_IFNAME)) == 0) { | |
39236c6e A |
564 | error = EINVAL; |
565 | break; | |
0a7de745 A |
566 | } |
567 | break; | |
568 | ||
569 | default: | |
570 | error = EINVAL; | |
571 | break; | |
fe8ab488 | 572 | } |
0a7de745 | 573 | if (error != 0) { |
39236c6e | 574 | break; |
0a7de745 | 575 | } |
39236c6e | 576 | } |
0a7de745 | 577 | if (error != 0) { |
39236c6e | 578 | break; |
0a7de745 | 579 | } |
39236c6e A |
580 | for (i = 0; i < PKTAP_MAX_FILTERS; i++) { |
581 | struct pktap_filter *pktap_filter = pktap->pktp_filters + i; | |
582 | struct x_pktap_filter *x_filter = user_filters + i; | |
fe8ab488 | 583 | |
39236c6e A |
584 | pktap_filter->filter_op = x_filter->filter_op; |
585 | pktap_filter->filter_param = x_filter->filter_param; | |
fe8ab488 | 586 | |
0a7de745 | 587 | if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_TYPE) { |
39236c6e | 588 | pktap_filter->filter_param_if_type = x_filter->filter_param_if_type; |
0a7de745 | 589 | } else if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_NAME) { |
39236c6e | 590 | size_t len; |
fe8ab488 | 591 | |
39236c6e | 592 | strlcpy(pktap_filter->filter_param_if_name, |
0a7de745 A |
593 | x_filter->filter_param_if_name, |
594 | sizeof(pktap_filter->filter_param_if_name)); | |
39236c6e | 595 | /* |
fe8ab488 | 596 | * If name does not end with a number then it's a "wildcard" match |
39236c6e A |
597 | * where we compare the prefix of the interface name |
598 | */ | |
599 | len = strlen(pktap_filter->filter_param_if_name); | |
600 | if (pktap_filter->filter_param_if_name[len] < '0' || | |
0a7de745 | 601 | pktap_filter->filter_param_if_name[len] > '9') { |
39236c6e | 602 | pktap_filter->filter_ifname_prefix_len = len; |
0a7de745 | 603 | } |
39236c6e A |
604 | } |
605 | } | |
606 | break; | |
607 | } | |
608 | default: | |
609 | error = EINVAL; | |
610 | break; | |
611 | } | |
612 | ||
613 | done: | |
0a7de745 | 614 | return error; |
39236c6e A |
615 | } |
616 | ||
617 | __private_extern__ errno_t | |
618 | pktap_ioctl(ifnet_t ifp, unsigned long cmd, void *data) | |
619 | { | |
620 | errno_t error = 0; | |
621 | ||
622 | PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname); | |
623 | ||
624 | if ((cmd & IOC_IN)) { | |
625 | error = kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER); | |
626 | if (error) { | |
fe8ab488 | 627 | PKTAP_LOG(PKTP_LOG_ERROR, |
0a7de745 A |
628 | "%s: kauth_authorize_generic(KAUTH_GENERIC_ISSUSER) - error %d\n", |
629 | __func__, error); | |
39236c6e A |
630 | goto done; |
631 | } | |
632 | } | |
fe8ab488 | 633 | |
39236c6e A |
634 | switch (cmd) { |
635 | case SIOCGDRVSPEC32: { | |
636 | struct ifdrv64 ifd; | |
637 | struct ifdrv32 *ifd32 = (struct ifdrv32 *)data; | |
fe8ab488 | 638 | |
39236c6e A |
639 | memcpy(ifd.ifd_name, ifd32->ifd_name, sizeof(ifd.ifd_name)); |
640 | ifd.ifd_cmd = ifd32->ifd_cmd; | |
641 | ifd.ifd_len = ifd32->ifd_len; | |
642 | ifd.ifd_data = ifd32->ifd_data; | |
fe8ab488 | 643 | |
39236c6e | 644 | error = pktap_getdrvspec(ifp, &ifd); |
fe8ab488 | 645 | |
39236c6e A |
646 | break; |
647 | } | |
648 | case SIOCGDRVSPEC64: { | |
649 | struct ifdrv64 *ifd64 = (struct ifdrv64 *)data; | |
fe8ab488 | 650 | |
39236c6e A |
651 | error = pktap_getdrvspec(ifp, ifd64); |
652 | ||
653 | break; | |
654 | } | |
655 | case SIOCSDRVSPEC32: { | |
656 | struct ifdrv64 ifd; | |
657 | struct ifdrv32 *ifd32 = (struct ifdrv32 *)data; | |
658 | ||
659 | memcpy(ifd.ifd_name, ifd32->ifd_name, sizeof(ifd.ifd_name)); | |
660 | ifd.ifd_cmd = ifd32->ifd_cmd; | |
661 | ifd.ifd_len = ifd32->ifd_len; | |
662 | ifd.ifd_data = ifd32->ifd_data; | |
663 | ||
664 | error = pktap_setdrvspec(ifp, &ifd); | |
665 | break; | |
666 | } | |
667 | case SIOCSDRVSPEC64: { | |
668 | struct ifdrv64 *ifd64 = (struct ifdrv64 *)data; | |
669 | ||
670 | error = pktap_setdrvspec(ifp, ifd64); | |
671 | ||
672 | break; | |
673 | } | |
674 | default: | |
675 | error = ENOTSUP; | |
676 | break; | |
677 | } | |
678 | done: | |
0a7de745 | 679 | return error; |
39236c6e A |
680 | } |
681 | ||
682 | __private_extern__ void | |
683 | pktap_detach(ifnet_t ifp) | |
684 | { | |
685 | struct pktap_softc *pktap; | |
686 | ||
687 | PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname); | |
fe8ab488 | 688 | |
39236c6e A |
689 | lck_rw_lock_exclusive(pktap_lck_rw); |
690 | ||
691 | pktap = ifp->if_softc; | |
692 | ifp->if_softc = NULL; | |
693 | LIST_REMOVE(pktap, pktp_link); | |
694 | ||
695 | lck_rw_done(pktap_lck_rw); | |
696 | ||
697 | /* Drop reference as it's no more on the global list */ | |
698 | ifnet_release(ifp); | |
fe8ab488 | 699 | |
d9a64523 | 700 | if_clone_softc_deallocate(&pktap_cloner, pktap); |
39236c6e A |
701 | /* This is for the reference taken by ifnet_attach() */ |
702 | (void) ifnet_release(ifp); | |
703 | } | |
704 | ||
705 | __private_extern__ int | |
706 | pktap_filter_evaluate(struct pktap_softc *pktap, struct ifnet *ifp) | |
707 | { | |
708 | int i; | |
709 | int result = PKTAP_FILTER_SKIP; /* Need positive matching rule to pass */ | |
710 | int match = 0; | |
fe8ab488 | 711 | |
39236c6e A |
712 | for (i = 0; i < PKTAP_MAX_FILTERS; i++) { |
713 | struct pktap_filter *pktap_filter = pktap->pktp_filters + i; | |
714 | size_t len = pktap_filter->filter_ifname_prefix_len != 0 ? | |
0a7de745 | 715 | pktap_filter->filter_ifname_prefix_len : PKTAP_IFXNAMESIZE; |
fe8ab488 | 716 | |
39236c6e | 717 | switch (pktap_filter->filter_op) { |
0a7de745 A |
718 | case PKTAP_FILTER_OP_NONE: |
719 | match = 1; | |
720 | break; | |
39236c6e | 721 | |
0a7de745 A |
722 | case PKTAP_FILTER_OP_PASS: |
723 | if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_TYPE) { | |
724 | if (pktap_filter->filter_param_if_type == 0 || | |
725 | ifp->if_type == pktap_filter->filter_param_if_type) { | |
726 | result = PKTAP_FILTER_OK; | |
727 | match = 1; | |
728 | PKTAP_LOG(PKTP_LOG_FILTER, "pass %s match type %u\n", | |
729 | ifp->if_xname, pktap_filter->filter_param_if_type); | |
730 | break; | |
39236c6e | 731 | } |
0a7de745 A |
732 | } |
733 | if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_NAME) { | |
734 | if (strncmp(ifp->if_xname, pktap_filter->filter_param_if_name, | |
735 | len) == 0) { | |
736 | result = PKTAP_FILTER_OK; | |
737 | match = 1; | |
738 | PKTAP_LOG(PKTP_LOG_FILTER, "pass %s match name %s\n", | |
739 | ifp->if_xname, pktap_filter->filter_param_if_name); | |
740 | break; | |
39236c6e | 741 | } |
0a7de745 A |
742 | } |
743 | break; | |
39236c6e | 744 | |
0a7de745 A |
745 | case PKTAP_FILTER_OP_SKIP: |
746 | if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_TYPE) { | |
747 | if (pktap_filter->filter_param_if_type == 0 || | |
748 | ifp->if_type == pktap_filter->filter_param_if_type) { | |
749 | result = PKTAP_FILTER_SKIP; | |
750 | match = 1; | |
751 | PKTAP_LOG(PKTP_LOG_FILTER, "skip %s match type %u\n", | |
752 | ifp->if_xname, pktap_filter->filter_param_if_type); | |
753 | break; | |
39236c6e | 754 | } |
0a7de745 A |
755 | } |
756 | if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_NAME) { | |
757 | if (strncmp(ifp->if_xname, pktap_filter->filter_param_if_name, | |
758 | len) == 0) { | |
759 | result = PKTAP_FILTER_SKIP; | |
760 | match = 1; | |
761 | PKTAP_LOG(PKTP_LOG_FILTER, "skip %s match name %s\n", | |
762 | ifp->if_xname, pktap_filter->filter_param_if_name); | |
763 | break; | |
39236c6e | 764 | } |
0a7de745 A |
765 | } |
766 | break; | |
39236c6e | 767 | } |
0a7de745 | 768 | if (match) { |
39236c6e | 769 | break; |
0a7de745 | 770 | } |
39236c6e A |
771 | } |
772 | ||
773 | if (match == 0) { | |
fe8ab488 | 774 | PKTAP_LOG(PKTP_LOG_FILTER, "%s no match\n", |
0a7de745 | 775 | ifp->if_xname); |
39236c6e | 776 | } |
0a7de745 | 777 | return result; |
39236c6e A |
778 | } |
779 | ||
fe8ab488 A |
780 | static void |
781 | pktap_set_procinfo(struct pktap_header *hdr, struct so_procinfo *soprocinfo) | |
782 | { | |
783 | hdr->pth_pid = soprocinfo->spi_pid; | |
0a7de745 | 784 | if (hdr->pth_comm[0] == 0) { |
d9a64523 | 785 | proc_name(soprocinfo->spi_pid, hdr->pth_comm, MAXCOMLEN); |
0a7de745 | 786 | } |
cb323159 A |
787 | strlcpy(&hdr->pth_comm[0], &soprocinfo->spi_proc_name[0], sizeof(hdr->pth_comm)); |
788 | ||
0a7de745 | 789 | if (soprocinfo->spi_pid != 0) { |
fe8ab488 | 790 | uuid_copy(hdr->pth_uuid, soprocinfo->spi_uuid); |
0a7de745 | 791 | } |
fe8ab488 | 792 | |
3e170ce0 | 793 | if (soprocinfo->spi_delegated != 0) { |
fe8ab488 A |
794 | hdr->pth_flags |= PTH_FLAG_PROC_DELEGATED; |
795 | hdr->pth_epid = soprocinfo->spi_epid; | |
cb323159 | 796 | strlcpy(&hdr->pth_ecomm[0], &soprocinfo->spi_e_proc_name[0], sizeof(hdr->pth_ecomm)); |
3e170ce0 | 797 | uuid_copy(hdr->pth_euuid, soprocinfo->spi_euuid); |
fe8ab488 A |
798 | } |
799 | } | |
800 | ||
39236c6e | 801 | __private_extern__ void |
fe8ab488 A |
802 | pktap_finalize_proc_info(struct pktap_header *hdr) |
803 | { | |
804 | int found; | |
805 | struct so_procinfo soprocinfo; | |
806 | ||
0a7de745 | 807 | if (!(hdr->pth_flags & PTH_FLAG_DELAY_PKTAP)) { |
fe8ab488 | 808 | return; |
0a7de745 | 809 | } |
fe8ab488 | 810 | |
0a7de745 | 811 | if (hdr->pth_ipproto == IPPROTO_TCP) { |
fe8ab488 A |
812 | found = inp_findinpcb_procinfo(&tcbinfo, hdr->pth_flowid, |
813 | &soprocinfo); | |
0a7de745 | 814 | } else if (hdr->pth_ipproto == IPPROTO_UDP) { |
fe8ab488 A |
815 | found = inp_findinpcb_procinfo(&udbinfo, hdr->pth_flowid, |
816 | &soprocinfo); | |
0a7de745 | 817 | } else { |
fe8ab488 A |
818 | found = inp_findinpcb_procinfo(&ripcbinfo, hdr->pth_flowid, |
819 | &soprocinfo); | |
0a7de745 | 820 | } |
fe8ab488 | 821 | |
0a7de745 | 822 | if (found == 1) { |
fe8ab488 | 823 | pktap_set_procinfo(hdr, &soprocinfo); |
0a7de745 | 824 | } |
fe8ab488 A |
825 | } |
826 | ||
d9a64523 A |
827 | static void |
828 | pktap_v2_set_procinfo(struct pktap_v2_hdr *pktap_v2_hdr, | |
829 | struct so_procinfo *soprocinfo) | |
830 | { | |
831 | pktap_v2_hdr->pth_pid = soprocinfo->spi_pid; | |
832 | ||
833 | if (soprocinfo->spi_pid != 0 && soprocinfo->spi_pid != -1) { | |
834 | if (pktap_v2_hdr->pth_comm_offset != 0) { | |
835 | char *ptr = ((char *)pktap_v2_hdr) + | |
836 | pktap_v2_hdr->pth_comm_offset; | |
837 | ||
cb323159 | 838 | strlcpy(ptr, &soprocinfo->spi_proc_name[0], PKTAP_MAX_COMM_SIZE); |
d9a64523 A |
839 | } |
840 | if (pktap_v2_hdr->pth_uuid_offset != 0) { | |
841 | uuid_t *ptr = (uuid_t *) (((char *)pktap_v2_hdr) + | |
842 | pktap_v2_hdr->pth_uuid_offset); | |
843 | ||
844 | uuid_copy(*ptr, soprocinfo->spi_uuid); | |
845 | } | |
846 | } | |
847 | ||
0a7de745 | 848 | if (!(pktap_v2_hdr->pth_flags & PTH_FLAG_PROC_DELEGATED)) { |
d9a64523 | 849 | return; |
0a7de745 | 850 | } |
d9a64523 A |
851 | |
852 | /* | |
853 | * The effective UUID may be set independently from the effective pid | |
854 | */ | |
855 | if (soprocinfo->spi_delegated != 0) { | |
856 | pktap_v2_hdr->pth_flags |= PTH_FLAG_PROC_DELEGATED; | |
857 | pktap_v2_hdr->pth_e_pid = soprocinfo->spi_epid; | |
858 | ||
859 | if (soprocinfo->spi_pid != 0 && soprocinfo->spi_pid != -1 && | |
860 | pktap_v2_hdr->pth_e_comm_offset != 0) { | |
861 | char *ptr = ((char *)pktap_v2_hdr) + | |
862 | pktap_v2_hdr->pth_e_comm_offset; | |
863 | ||
cb323159 | 864 | strlcpy(ptr, &soprocinfo->spi_e_proc_name[0], PKTAP_MAX_COMM_SIZE); |
d9a64523 A |
865 | } |
866 | if (pktap_v2_hdr->pth_e_uuid_offset != 0) { | |
867 | uuid_t *ptr = (uuid_t *) (((char *)pktap_v2_hdr) + | |
868 | pktap_v2_hdr->pth_e_uuid_offset); | |
869 | ||
870 | uuid_copy(*ptr, soprocinfo->spi_euuid); | |
871 | } | |
872 | } | |
873 | } | |
874 | ||
fe8ab488 | 875 | __private_extern__ void |
d9a64523 | 876 | pktap_v2_finalize_proc_info(struct pktap_v2_hdr *pktap_v2_hdr) |
39236c6e | 877 | { |
d9a64523 | 878 | int found; |
39236c6e | 879 | struct so_procinfo soprocinfo; |
fe8ab488 | 880 | |
0a7de745 | 881 | if (!(pktap_v2_hdr->pth_flags & PTH_FLAG_DELAY_PKTAP)) { |
d9a64523 | 882 | return; |
0a7de745 | 883 | } |
d9a64523 A |
884 | |
885 | if (pktap_v2_hdr->pth_ipproto == IPPROTO_TCP) { | |
886 | found = inp_findinpcb_procinfo(&tcbinfo, | |
887 | pktap_v2_hdr->pth_flowid, &soprocinfo); | |
888 | } else if (pktap_v2_hdr->pth_ipproto == IPPROTO_UDP) { | |
889 | found = inp_findinpcb_procinfo(&udbinfo, | |
890 | pktap_v2_hdr->pth_flowid, &soprocinfo); | |
891 | } else { | |
892 | found = inp_findinpcb_procinfo(&ripcbinfo, | |
893 | pktap_v2_hdr->pth_flowid, &soprocinfo); | |
894 | } | |
895 | if (found == 1) { | |
896 | pktap_v2_set_procinfo(pktap_v2_hdr, &soprocinfo); | |
897 | } | |
898 | } | |
899 | ||
900 | __private_extern__ void | |
901 | pktap_fill_proc_info(struct pktap_header *hdr, protocol_family_t proto, | |
0a7de745 | 902 | struct mbuf *m, u_int32_t pre, int outgoing, struct ifnet *ifp) |
d9a64523 | 903 | { |
39236c6e A |
904 | /* |
905 | * Getting the pid and procname is expensive | |
fe8ab488 | 906 | * For outgoing, do the lookup only if there's an |
39236c6e A |
907 | * associated socket as indicated by the flowhash |
908 | */ | |
3e170ce0 | 909 | if (outgoing != 0 && m->m_pkthdr.pkt_flowsrc == FLOWSRC_INPCB) { |
fe8ab488 | 910 | /* |
d9a64523 | 911 | * To avoid lock ordering issues we delay the proc UUID lookup |
fe8ab488 A |
912 | * to the BPF read as we cannot |
913 | * assume the socket lock is unlocked on output | |
914 | */ | |
3e170ce0 | 915 | hdr->pth_flags |= PTH_FLAG_DELAY_PKTAP; |
d9a64523 | 916 | hdr->pth_flags |= PTH_FLAG_SOCKET; |
3e170ce0 | 917 | hdr->pth_flowid = m->m_pkthdr.pkt_flowid; |
d9a64523 A |
918 | |
919 | if (m->m_pkthdr.pkt_flags & PKTF_FLOW_RAWSOCK) { | |
3e170ce0 | 920 | hdr->pth_ipproto = IPPROTO_RAW; |
d9a64523 | 921 | } else { |
3e170ce0 | 922 | hdr->pth_ipproto = m->m_pkthdr.pkt_proto; |
d9a64523 A |
923 | } |
924 | ||
925 | if (hdr->pth_ipproto == IPPROTO_TCP) { | |
926 | hdr->pth_pid = m->m_pkthdr.tx_tcp_pid; | |
927 | hdr->pth_epid = m->m_pkthdr.tx_tcp_e_pid; | |
928 | } else if (hdr->pth_ipproto == IPPROTO_UDP) { | |
929 | hdr->pth_pid = m->m_pkthdr.tx_udp_pid; | |
930 | hdr->pth_epid = m->m_pkthdr.tx_udp_e_pid; | |
931 | } else if (hdr->pth_ipproto == IPPROTO_RAW) { | |
932 | hdr->pth_pid = m->m_pkthdr.tx_rawip_pid; | |
933 | hdr->pth_epid = m->m_pkthdr.tx_rawip_e_pid; | |
934 | } | |
935 | ||
936 | if (hdr->pth_pid != 0 && hdr->pth_pid != -1) { | |
937 | proc_name(hdr->pth_pid, hdr->pth_comm, MAXCOMLEN); | |
938 | } else { | |
939 | hdr->pth_pid = -1; | |
940 | } | |
941 | ||
942 | if (hdr->pth_epid != 0 && hdr->pth_epid != -1) { | |
0a7de745 | 943 | hdr->pth_flags |= PTH_FLAG_PROC_DELEGATED; |
d9a64523 A |
944 | proc_name(hdr->pth_epid, hdr->pth_ecomm, MAXCOMLEN); |
945 | } else { | |
946 | hdr->pth_epid = -1; | |
947 | } | |
948 | ||
949 | if (m->m_pkthdr.pkt_flags & PKTF_NEW_FLOW) { | |
39037602 | 950 | hdr->pth_flags |= PTH_FLAG_NEW_FLOW; |
d9a64523 | 951 | } |
39236c6e | 952 | } else if (outgoing == 0) { |
d9a64523 A |
953 | int found = 0; |
954 | struct so_procinfo soprocinfo; | |
39236c6e | 955 | struct inpcb *inp = NULL; |
fe8ab488 | 956 | |
d9a64523 A |
957 | memset(&soprocinfo, 0, sizeof(struct so_procinfo)); |
958 | ||
39236c6e A |
959 | if (proto == PF_INET) { |
960 | struct ip ip; | |
961 | errno_t error; | |
962 | size_t hlen; | |
963 | struct in_addr faddr, laddr; | |
5ba3f43e | 964 | u_short fport = 0, lport = 0; |
39236c6e A |
965 | struct inpcbinfo *pcbinfo = NULL; |
966 | int wildcard = 0; | |
fe8ab488 | 967 | |
39236c6e A |
968 | error = mbuf_copydata(m, pre, sizeof(struct ip), &ip); |
969 | if (error != 0) { | |
fe8ab488 A |
970 | PKTAP_LOG(PKTP_LOG_ERROR, |
971 | "mbuf_copydata tcp v4 failed for %s\n", | |
972 | hdr->pth_ifname); | |
39236c6e A |
973 | goto done; |
974 | } | |
975 | hlen = IP_VHL_HL(ip.ip_vhl) << 2; | |
fe8ab488 | 976 | |
39236c6e A |
977 | faddr = ip.ip_src; |
978 | laddr = ip.ip_dst; | |
979 | ||
980 | if (ip.ip_p == IPPROTO_TCP) { | |
981 | struct tcphdr th; | |
fe8ab488 A |
982 | |
983 | error = mbuf_copydata(m, pre + hlen, | |
0a7de745 A |
984 | sizeof(struct tcphdr), &th); |
985 | if (error != 0) { | |
39236c6e | 986 | goto done; |
0a7de745 | 987 | } |
fe8ab488 | 988 | |
39236c6e A |
989 | fport = th.th_sport; |
990 | lport = th.th_dport; | |
991 | ||
992 | pcbinfo = &tcbinfo; | |
993 | } else if (ip.ip_p == IPPROTO_UDP) { | |
994 | struct udphdr uh; | |
fe8ab488 A |
995 | |
996 | error = mbuf_copydata(m, pre + hlen, | |
0a7de745 | 997 | sizeof(struct udphdr), &uh); |
39236c6e | 998 | if (error != 0) { |
fe8ab488 A |
999 | PKTAP_LOG(PKTP_LOG_ERROR, |
1000 | "mbuf_copydata udp v4 failed for %s\n", | |
1001 | hdr->pth_ifname); | |
39236c6e A |
1002 | goto done; |
1003 | } | |
1004 | fport = uh.uh_sport; | |
1005 | lport = uh.uh_dport; | |
fe8ab488 | 1006 | |
39236c6e A |
1007 | pcbinfo = &udbinfo; |
1008 | wildcard = 1; | |
1009 | } | |
1010 | if (pcbinfo != NULL) { | |
1011 | inp = in_pcblookup_hash(pcbinfo, faddr, fport, | |
0a7de745 | 1012 | laddr, lport, wildcard, outgoing ? NULL : ifp); |
fe8ab488 | 1013 | |
0a7de745 | 1014 | if (inp == NULL && hdr->pth_iftype != IFT_LOOP) { |
fe8ab488 A |
1015 | PKTAP_LOG(PKTP_LOG_NOPCB, |
1016 | "in_pcblookup_hash no pcb %s\n", | |
1017 | hdr->pth_ifname); | |
0a7de745 | 1018 | } |
39236c6e | 1019 | } else { |
fe8ab488 A |
1020 | PKTAP_LOG(PKTP_LOG_NOPCB, |
1021 | "unknown ip_p %u on %s\n", | |
1022 | ip.ip_p, hdr->pth_ifname); | |
39236c6e A |
1023 | pktap_hexdump(PKTP_LOG_NOPCB, &ip, sizeof(struct ip)); |
1024 | } | |
1025 | } else if (proto == PF_INET6) { | |
1026 | struct ip6_hdr ip6; | |
1027 | errno_t error; | |
1028 | struct in6_addr *faddr; | |
1029 | struct in6_addr *laddr; | |
5ba3f43e | 1030 | u_short fport = 0, lport = 0; |
39236c6e A |
1031 | struct inpcbinfo *pcbinfo = NULL; |
1032 | int wildcard = 0; | |
fe8ab488 | 1033 | |
39236c6e | 1034 | error = mbuf_copydata(m, pre, sizeof(struct ip6_hdr), &ip6); |
0a7de745 | 1035 | if (error != 0) { |
39236c6e | 1036 | goto done; |
0a7de745 | 1037 | } |
fe8ab488 | 1038 | |
39236c6e A |
1039 | faddr = &ip6.ip6_src; |
1040 | laddr = &ip6.ip6_dst; | |
fe8ab488 | 1041 | |
39236c6e A |
1042 | if (ip6.ip6_nxt == IPPROTO_TCP) { |
1043 | struct tcphdr th; | |
fe8ab488 A |
1044 | |
1045 | error = mbuf_copydata(m, pre + sizeof(struct ip6_hdr), | |
0a7de745 | 1046 | sizeof(struct tcphdr), &th); |
39236c6e | 1047 | if (error != 0) { |
fe8ab488 A |
1048 | PKTAP_LOG(PKTP_LOG_ERROR, |
1049 | "mbuf_copydata tcp v6 failed for %s\n", | |
1050 | hdr->pth_ifname); | |
39236c6e A |
1051 | goto done; |
1052 | } | |
fe8ab488 | 1053 | |
39236c6e A |
1054 | fport = th.th_sport; |
1055 | lport = th.th_dport; | |
fe8ab488 | 1056 | |
39236c6e A |
1057 | pcbinfo = &tcbinfo; |
1058 | } else if (ip6.ip6_nxt == IPPROTO_UDP) { | |
1059 | struct udphdr uh; | |
fe8ab488 A |
1060 | |
1061 | error = mbuf_copydata(m, pre + sizeof(struct ip6_hdr), | |
0a7de745 | 1062 | sizeof(struct udphdr), &uh); |
39236c6e | 1063 | if (error != 0) { |
fe8ab488 A |
1064 | PKTAP_LOG(PKTP_LOG_ERROR, |
1065 | "mbuf_copydata udp v6 failed for %s\n", | |
1066 | hdr->pth_ifname); | |
39236c6e A |
1067 | goto done; |
1068 | } | |
fe8ab488 | 1069 | |
39236c6e A |
1070 | fport = uh.uh_sport; |
1071 | lport = uh.uh_dport; | |
fe8ab488 | 1072 | |
39236c6e A |
1073 | pcbinfo = &udbinfo; |
1074 | wildcard = 1; | |
1075 | } | |
1076 | if (pcbinfo != NULL) { | |
1077 | inp = in6_pcblookup_hash(pcbinfo, faddr, fport, | |
0a7de745 | 1078 | laddr, lport, wildcard, outgoing ? NULL : ifp); |
fe8ab488 | 1079 | |
0a7de745 | 1080 | if (inp == NULL && hdr->pth_iftype != IFT_LOOP) { |
fe8ab488 A |
1081 | PKTAP_LOG(PKTP_LOG_NOPCB, |
1082 | "in6_pcblookup_hash no pcb %s\n", | |
1083 | hdr->pth_ifname); | |
0a7de745 | 1084 | } |
39236c6e | 1085 | } else { |
fe8ab488 A |
1086 | PKTAP_LOG(PKTP_LOG_NOPCB, |
1087 | "unknown ip6.ip6_nxt %u on %s\n", | |
1088 | ip6.ip6_nxt, hdr->pth_ifname); | |
39236c6e A |
1089 | pktap_hexdump(PKTP_LOG_NOPCB, &ip6, sizeof(struct ip6_hdr)); |
1090 | } | |
1091 | } | |
1092 | if (inp != NULL) { | |
d9a64523 | 1093 | hdr->pth_flags |= PTH_FLAG_SOCKET; |
39236c6e A |
1094 | if (inp->inp_state != INPCB_STATE_DEAD && inp->inp_socket != NULL) { |
1095 | found = 1; | |
1096 | inp_get_soprocinfo(inp, &soprocinfo); | |
1097 | } | |
1098 | in_pcb_checkstate(inp, WNT_RELEASE, 0); | |
1099 | } | |
fe8ab488 | 1100 | done: |
d9a64523 A |
1101 | /* |
1102 | * -1 means PID not found | |
1103 | */ | |
1104 | hdr->pth_pid = -1; | |
1105 | hdr->pth_epid = -1; | |
1106 | ||
0a7de745 A |
1107 | if (found != 0) { |
1108 | pktap_set_procinfo(hdr, &soprocinfo); | |
1109 | } | |
1110 | } | |
d9a64523 | 1111 | } |
39236c6e A |
1112 | |
1113 | __private_extern__ void | |
1114 | pktap_bpf_tap(struct ifnet *ifp, protocol_family_t proto, struct mbuf *m, | |
1115 | u_int32_t pre, u_int32_t post, int outgoing) | |
1116 | { | |
1117 | struct pktap_softc *pktap; | |
fe8ab488 | 1118 | void (*bpf_tap_func)(ifnet_t, u_int32_t, mbuf_t, void *, size_t) = |
0a7de745 | 1119 | outgoing ? bpf_tap_out : bpf_tap_in; |
39236c6e | 1120 | |
5c9f4661 A |
1121 | /* |
1122 | * Skip the coprocessor interface | |
1123 | */ | |
0a7de745 | 1124 | if (!intcoproc_unrestricted && IFNET_IS_INTCOPROC(ifp)) { |
5c9f4661 | 1125 | return; |
0a7de745 | 1126 | } |
5c9f4661 | 1127 | |
39236c6e A |
1128 | lck_rw_lock_shared(pktap_lck_rw); |
1129 | ||
1130 | /* | |
fe8ab488 | 1131 | * No need to take the ifnet_lock as the struct ifnet field if_bpf is |
39236c6e A |
1132 | * protected by the BPF subsystem |
1133 | */ | |
1134 | LIST_FOREACH(pktap, &pktap_list, pktp_link) { | |
1135 | int filter_result; | |
fe8ab488 | 1136 | |
39236c6e | 1137 | filter_result = pktap_filter_evaluate(pktap, ifp); |
0a7de745 | 1138 | if (filter_result == PKTAP_FILTER_SKIP) { |
39236c6e | 1139 | continue; |
0a7de745 | 1140 | } |
39236c6e A |
1141 | |
1142 | if (pktap->pktp_dlt_raw_count > 0) { | |
1143 | /* We accept only IPv4 and IPv6 packets for the raw DLT */ | |
0a7de745 A |
1144 | if ((proto == AF_INET || proto == AF_INET6) && |
1145 | !(m->m_pkthdr.pkt_flags & PKTF_INET_RESOLVE)) { | |
39236c6e | 1146 | /* |
fe8ab488 | 1147 | * We can play just with the length of the first mbuf in the |
39236c6e A |
1148 | * chain because bpf_tap_imp() disregard the packet length |
1149 | * of the mbuf packet header. | |
1150 | */ | |
0a7de745 | 1151 | if (mbuf_setdata(m, m->m_data + pre, m->m_len - pre) == 0) { |
39236c6e A |
1152 | bpf_tap_func(pktap->pktp_ifp, DLT_RAW, m, NULL, 0); |
1153 | mbuf_setdata(m, m->m_data - pre, m->m_len + pre); | |
1154 | } | |
1155 | } | |
1156 | } | |
fe8ab488 | 1157 | |
39236c6e A |
1158 | if (pktap->pktp_dlt_pkttap_count > 0) { |
1159 | struct { | |
1160 | struct pktap_header hdr; | |
1161 | u_int32_t proto; | |
1162 | } hdr_buffer; | |
1163 | struct pktap_header *hdr = &hdr_buffer.hdr; | |
1164 | size_t hdr_size = sizeof(struct pktap_header); | |
1165 | int unknown_if_type = 0; | |
1166 | size_t data_adjust = 0; | |
1167 | u_int32_t pre_adjust = 0; | |
fe8ab488 A |
1168 | |
1169 | /* Verify the structure is packed */ | |
39236c6e | 1170 | _CASSERT(sizeof(hdr_buffer) == sizeof(struct pktap_header) + sizeof(u_int32_t)); |
fe8ab488 | 1171 | |
39236c6e A |
1172 | bzero(&hdr_buffer, sizeof(hdr_buffer)); |
1173 | hdr->pth_length = sizeof(struct pktap_header); | |
1174 | hdr->pth_type_next = PTH_TYPE_PACKET; | |
1175 | ||
1176 | /* | |
1177 | * Set DLT of packet based on interface type | |
1178 | */ | |
1179 | switch (ifp->if_type) { | |
0a7de745 A |
1180 | case IFT_LOOP: |
1181 | case IFT_GIF: | |
1182 | case IFT_STF: | |
1183 | case IFT_CELLULAR: | |
1184 | /* | |
1185 | * Packets from pdp interfaces have no loopback | |
1186 | * header that contain the protocol number. | |
1187 | * As BPF just concatenate the header and the | |
1188 | * packet content in a single buffer, | |
1189 | * stash the protocol after the pktap header | |
1190 | * and adjust the size of the header accordingly | |
1191 | */ | |
1192 | hdr->pth_dlt = DLT_NULL; | |
1193 | if (pre == 0) { | |
1194 | hdr_buffer.proto = proto; | |
1195 | hdr_size = sizeof(hdr_buffer); | |
1196 | pre_adjust = sizeof(hdr_buffer.proto); | |
1197 | } | |
1198 | break; | |
1199 | case IFT_ETHER: | |
1200 | case IFT_BRIDGE: | |
1201 | case IFT_L2VLAN: | |
1202 | case IFT_IEEE8023ADLAG: | |
1203 | hdr->pth_dlt = DLT_EN10MB; | |
1204 | break; | |
1205 | case IFT_PPP: | |
1206 | hdr->pth_dlt = DLT_PPP; | |
1207 | break; | |
1208 | case IFT_IEEE1394: | |
1209 | hdr->pth_dlt = DLT_APPLE_IP_OVER_IEEE1394; | |
1210 | break; | |
1211 | case IFT_OTHER: | |
cb323159 A |
1212 | if (ifp->if_family == IFNET_FAMILY_IPSEC || |
1213 | ifp->if_family == IFNET_FAMILY_UTUN) { | |
39236c6e | 1214 | /* |
0a7de745 A |
1215 | * For utun: |
1216 | * - incoming packets do not have the prefix set to four | |
1217 | * - some packets are as small as two bytes! | |
39236c6e | 1218 | */ |
0a7de745 A |
1219 | if (m_pktlen(m) < 4) { |
1220 | goto done; | |
1221 | } | |
1222 | if (proto != AF_INET && proto != AF_INET6) { | |
1223 | goto done; | |
1224 | } | |
1225 | if (proto == AF_INET && (size_t) m_pktlen(m) - 4 < sizeof(struct ip)) { | |
1226 | goto done; | |
1227 | } | |
1228 | if (proto == AF_INET6 && (size_t) m_pktlen(m) - 4 < sizeof(struct ip6_hdr)) { | |
1229 | goto done; | |
39236c6e | 1230 | } |
5ba3f43e | 1231 | |
0a7de745 A |
1232 | /* |
1233 | * Handle two cases: | |
1234 | * - The old utun encapsulation with the protocol family in network order | |
1235 | * - A raw IPv4 or IPv6 packet | |
1236 | */ | |
1237 | uint8_t data = *(uint8_t *)mbuf_data(m); | |
1238 | if ((data >> 4) == 4 || (data >> 4) == 6) { | |
1239 | pre = 4; | |
1240 | } else { | |
39236c6e | 1241 | /* |
0a7de745 | 1242 | * Skip the protocol in the mbuf as it's in network order |
39236c6e | 1243 | */ |
0a7de745 A |
1244 | pre = 4; |
1245 | data_adjust = 4; | |
39236c6e | 1246 | } |
0a7de745 A |
1247 | } |
1248 | hdr->pth_dlt = DLT_NULL; | |
1249 | hdr_buffer.proto = proto; | |
1250 | hdr_size = sizeof(hdr_buffer); | |
1251 | break; | |
1252 | default: | |
1253 | if (pre == 0) { | |
1254 | hdr->pth_dlt = DLT_RAW; | |
1255 | } else { | |
1256 | unknown_if_type = 1; | |
1257 | } | |
1258 | break; | |
39236c6e A |
1259 | } |
1260 | if (unknown_if_type) { | |
fe8ab488 A |
1261 | PKTAP_LOG(PKTP_LOG_FUNC, |
1262 | "unknown if_type %u for %s\n", | |
1263 | ifp->if_type, ifp->if_xname); | |
39236c6e A |
1264 | pktap_count_unknown_if_type += 1; |
1265 | } else { | |
5ba3f43e A |
1266 | strlcpy(hdr->pth_ifname, ifp->if_xname, |
1267 | sizeof(hdr->pth_ifname)); | |
39236c6e A |
1268 | hdr->pth_flags |= outgoing ? PTH_FLAG_DIR_OUT : PTH_FLAG_DIR_IN; |
1269 | hdr->pth_protocol_family = proto; | |
1270 | hdr->pth_frame_pre_length = pre + pre_adjust; | |
1271 | hdr->pth_frame_post_length = post; | |
1272 | hdr->pth_iftype = ifp->if_type; | |
1273 | hdr->pth_ifunit = ifp->if_unit; | |
fe8ab488 | 1274 | |
0a7de745 | 1275 | if (m->m_pkthdr.pkt_flags & PKTF_KEEPALIVE) { |
d9a64523 | 1276 | hdr->pth_flags |= PTH_FLAG_KEEP_ALIVE; |
0a7de745 A |
1277 | } |
1278 | if (m->m_pkthdr.pkt_flags & PKTF_TCP_REXMT) { | |
d9a64523 | 1279 | hdr->pth_flags |= PTH_FLAG_REXMIT; |
0a7de745 | 1280 | } |
d9a64523 | 1281 | |
39236c6e | 1282 | pktap_fill_proc_info(hdr, proto, m, pre, outgoing, ifp); |
fe8ab488 | 1283 | |
39236c6e | 1284 | hdr->pth_svc = so_svc2tc(m->m_pkthdr.pkt_svc); |
fe8ab488 | 1285 | |
39236c6e A |
1286 | if (data_adjust == 0) { |
1287 | bpf_tap_func(pktap->pktp_ifp, DLT_PKTAP, m, hdr, hdr_size); | |
1288 | } else { | |
1289 | /* | |
fe8ab488 | 1290 | * We can play just with the length of the first mbuf in the |
39236c6e A |
1291 | * chain because bpf_tap_imp() disregard the packet length |
1292 | * of the mbuf packet header. | |
1293 | */ | |
0a7de745 | 1294 | if (mbuf_setdata(m, m->m_data + data_adjust, m->m_len - data_adjust) == 0) { |
39236c6e A |
1295 | bpf_tap_func(pktap->pktp_ifp, DLT_PKTAP, m, hdr, hdr_size); |
1296 | mbuf_setdata(m, m->m_data - data_adjust, m->m_len + data_adjust); | |
1297 | } | |
1298 | } | |
1299 | } | |
1300 | } | |
1301 | } | |
1302 | done: | |
1303 | lck_rw_done(pktap_lck_rw); | |
1304 | } | |
1305 | ||
1306 | __private_extern__ void | |
1307 | pktap_input(struct ifnet *ifp, protocol_family_t proto, struct mbuf *m, | |
1308 | char *frame_header) | |
1309 | { | |
5ba3f43e A |
1310 | char *hdr; |
1311 | char *start; | |
39236c6e A |
1312 | |
1313 | /* Fast path */ | |
f427ee49 A |
1314 | if (pktap_total_tap_count == 0 || |
1315 | (m->m_pkthdr.pkt_flags & PKTF_SKIP_PKTAP) != 0) { | |
39236c6e | 1316 | return; |
0a7de745 | 1317 | } |
39236c6e | 1318 | |
5ba3f43e A |
1319 | hdr = (char *)mbuf_data(m); |
1320 | start = (char *)mbuf_datastart(m); | |
39236c6e A |
1321 | /* Make sure the frame header is fully contained in the mbuf */ |
1322 | if (frame_header != NULL && frame_header >= start && frame_header <= hdr) { | |
1323 | size_t o_len = m->m_len; | |
f427ee49 | 1324 | u_int32_t pre = (u_int32_t)(hdr - frame_header); |
fe8ab488 | 1325 | |
39236c6e | 1326 | if (mbuf_setdata(m, frame_header, o_len + pre) == 0) { |
fe8ab488 | 1327 | PKTAP_LOG(PKTP_LOG_INPUT, "ifp %s proto %u pre %u post %u\n", |
0a7de745 | 1328 | ifp->if_xname, proto, pre, 0); |
39236c6e | 1329 | |
0a7de745 | 1330 | pktap_bpf_tap(ifp, proto, m, pre, 0, 0); |
39236c6e A |
1331 | mbuf_setdata(m, hdr, o_len); |
1332 | } | |
1333 | } else { | |
fe8ab488 | 1334 | PKTAP_LOG(PKTP_LOG_INPUT, "ifp %s proto %u pre %u post %u\n", |
0a7de745 | 1335 | ifp->if_xname, proto, 0, 0); |
39236c6e A |
1336 | |
1337 | pktap_bpf_tap(ifp, proto, m, 0, 0, 0); | |
1338 | } | |
1339 | } | |
1340 | ||
1341 | __private_extern__ void | |
1342 | pktap_output(struct ifnet *ifp, protocol_family_t proto, struct mbuf *m, | |
fe8ab488 | 1343 | u_int32_t pre, u_int32_t post) |
39236c6e A |
1344 | { |
1345 | /* Fast path */ | |
f427ee49 A |
1346 | if (pktap_total_tap_count == 0 || |
1347 | (m->m_pkthdr.pkt_flags & PKTF_SKIP_PKTAP) != 0) { | |
39236c6e | 1348 | return; |
0a7de745 | 1349 | } |
39236c6e | 1350 | |
fe8ab488 | 1351 | PKTAP_LOG(PKTP_LOG_OUTPUT, "ifp %s proto %u pre %u post %u\n", |
0a7de745 | 1352 | ifp->if_xname, proto, pre, post); |
39236c6e A |
1353 | |
1354 | pktap_bpf_tap(ifp, proto, m, pre, post, 1); | |
1355 | } | |
5ba3f43e | 1356 | |
d9a64523 A |
1357 | |
1358 | void | |
1359 | convert_to_pktap_header_to_v2(struct bpf_packet *bpf_pkt, bool truncate) | |
1360 | { | |
1361 | struct pktap_header *pktap_header; | |
1362 | size_t extra_src_size; | |
1363 | struct pktap_buffer_v2_hdr_extra pktap_buffer_v2_hdr_extra; | |
1364 | struct pktap_v2_hdr_space *pktap_v2_hdr_space; | |
1365 | struct pktap_v2_hdr *pktap_v2_hdr; | |
1366 | uint8_t *ptr; | |
1367 | ||
1368 | pktap_header = (struct pktap_header *)bpf_pkt->bpfp_header; | |
1369 | ||
1370 | if (pktap_header->pth_type_next != PTH_TYPE_PACKET) { | |
1371 | return; | |
1372 | } | |
1373 | ||
1374 | VERIFY(bpf_pkt->bpfp_header_length >= sizeof(struct pktap_header)); | |
1375 | ||
1376 | /* | |
1377 | * extra_src_size is the length of the optional link layer header | |
1378 | */ | |
1379 | extra_src_size = bpf_pkt->bpfp_header_length - | |
1380 | sizeof(struct pktap_header); | |
1381 | ||
1382 | VERIFY(extra_src_size <= sizeof(union pktap_header_extra)); | |
1383 | ||
1384 | pktap_v2_hdr_space = &pktap_buffer_v2_hdr_extra.hdr_space; | |
1385 | pktap_v2_hdr = &pktap_v2_hdr_space->pth_hdr; | |
1386 | ptr = (uint8_t *) (pktap_v2_hdr + 1); | |
1387 | ||
1388 | COPY_PKTAP_COMMON_FIELDS_TO_V2(pktap_v2_hdr, pktap_header); | |
1389 | ||
1390 | /* | |
1391 | * When truncating don't bother with the process UUIDs | |
1392 | */ | |
1393 | if (!truncate) { | |
1394 | if ((pktap_header->pth_flags & PTH_FLAG_DELAY_PKTAP)) { | |
1395 | pktap_v2_hdr->pth_uuid_offset = pktap_v2_hdr->pth_length; | |
1396 | pktap_v2_hdr->pth_length += sizeof(uuid_t); | |
1397 | uuid_clear(*(uuid_t *)ptr); | |
1398 | ptr += sizeof(uuid_t); | |
1399 | VERIFY((void *)ptr < (void *)(pktap_v2_hdr_space + 1)); | |
1400 | } else if (!uuid_is_null(pktap_header->pth_uuid)) { | |
1401 | pktap_v2_hdr->pth_uuid_offset = pktap_v2_hdr->pth_length; | |
1402 | uuid_copy(*(uuid_t *)ptr, pktap_header->pth_uuid); | |
1403 | pktap_v2_hdr->pth_length += sizeof(uuid_t); | |
1404 | ptr += sizeof(uuid_t); | |
1405 | VERIFY((void *)ptr < (void *)(pktap_v2_hdr_space + 1)); | |
1406 | } | |
1407 | ||
1408 | if ((pktap_header->pth_flags & PTH_FLAG_DELAY_PKTAP)) { | |
1409 | if (pktap_header->pth_flags & PTH_FLAG_PROC_DELEGATED) { | |
1410 | pktap_v2_hdr->pth_e_uuid_offset = pktap_v2_hdr->pth_length; | |
1411 | uuid_clear(*(uuid_t *)ptr); | |
1412 | pktap_v2_hdr->pth_length += sizeof(uuid_t); | |
1413 | ptr += sizeof(uuid_t); | |
1414 | VERIFY((void *)ptr < (void *)(pktap_v2_hdr_space + 1)); | |
1415 | } | |
0a7de745 | 1416 | } else if (!uuid_is_null(pktap_header->pth_euuid)) { |
d9a64523 A |
1417 | pktap_v2_hdr->pth_e_uuid_offset = pktap_v2_hdr->pth_length; |
1418 | uuid_copy(*(uuid_t *)ptr, pktap_header->pth_euuid); | |
1419 | pktap_v2_hdr->pth_length += sizeof(uuid_t); | |
1420 | ptr += sizeof(uuid_t); | |
1421 | VERIFY((void *)ptr < (void *)(pktap_v2_hdr_space + 1)); | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | if (pktap_header->pth_ifname[0] != 0) { | |
1426 | size_t strsize; | |
1427 | ||
1428 | pktap_v2_hdr->pth_ifname_offset = pktap_v2_hdr->pth_length; | |
1429 | ||
1430 | /* | |
1431 | * Note: strlcpy() returns the length of the string so we need | |
1432 | * to add one for the end-of-string | |
1433 | */ | |
1434 | strsize = 1 + strlcpy((char *)ptr, pktap_header->pth_ifname, | |
1435 | sizeof(pktap_v2_hdr_space->pth_ifname)); | |
1436 | pktap_v2_hdr->pth_length += strsize; | |
1437 | ptr += strsize; | |
1438 | VERIFY((void *)ptr < (void *)(pktap_v2_hdr_space + 1)); | |
1439 | } | |
1440 | ||
1441 | /* | |
1442 | * Do not waste space with the process name if we do not have a pid | |
1443 | */ | |
1444 | if (pktap_header->pth_pid != 0 && pktap_header->pth_pid != -1) { | |
1445 | if (pktap_header->pth_comm[0] != 0) { | |
1446 | size_t strsize; | |
1447 | ||
1448 | pktap_v2_hdr->pth_comm_offset = pktap_v2_hdr->pth_length; | |
1449 | ||
1450 | strsize = 1 + strlcpy((char *)ptr, pktap_header->pth_comm, | |
1451 | sizeof(pktap_v2_hdr_space->pth_comm)); | |
1452 | pktap_v2_hdr->pth_length += strsize; | |
1453 | ptr += strsize; | |
1454 | VERIFY((void *)ptr < (void *)(pktap_v2_hdr_space + 1)); | |
1455 | } else if ((pktap_header->pth_flags & PTH_FLAG_DELAY_PKTAP)) { | |
1456 | size_t strsize = sizeof(pktap_v2_hdr_space->pth_comm); | |
1457 | ||
1458 | pktap_v2_hdr->pth_comm_offset = pktap_v2_hdr->pth_length; | |
1459 | ||
0a7de745 | 1460 | *ptr = 0; /* empty string by default */ |
d9a64523 A |
1461 | pktap_v2_hdr->pth_length += strsize; |
1462 | ptr += strsize; | |
1463 | VERIFY((void *)ptr < (void *)(pktap_v2_hdr_space + 1)); | |
1464 | } | |
1465 | } | |
1466 | ||
1467 | /* | |
1468 | * Do not waste space with the effective process name if we do not have | |
1469 | * an effective pid or it's the same as the pid | |
1470 | */ | |
1471 | if (pktap_header->pth_epid != 0 && pktap_header->pth_epid != -1 && | |
1472 | pktap_header->pth_epid != pktap_header->pth_pid) { | |
1473 | if (pktap_header->pth_ecomm[0] != 0) { | |
1474 | size_t strsize; | |
1475 | ||
1476 | pktap_v2_hdr->pth_e_comm_offset = pktap_v2_hdr->pth_length; | |
1477 | ||
1478 | strsize = 1 + strlcpy((char *)ptr, pktap_header->pth_ecomm, | |
1479 | sizeof(pktap_v2_hdr_space->pth_e_comm)); | |
1480 | pktap_v2_hdr->pth_length += strsize; | |
1481 | ptr += strsize; | |
1482 | VERIFY((void *)ptr < (void *)(pktap_v2_hdr_space + 1)); | |
1483 | } else if ((pktap_header->pth_flags & PTH_FLAG_DELAY_PKTAP)) { | |
1484 | size_t strsize = sizeof(pktap_v2_hdr_space->pth_e_comm); | |
1485 | ||
1486 | pktap_v2_hdr->pth_e_comm_offset = pktap_v2_hdr->pth_length; | |
0a7de745 | 1487 | *ptr = 0; /* empty string by default */ |
d9a64523 A |
1488 | pktap_v2_hdr->pth_length += strsize; |
1489 | ptr += strsize; | |
1490 | VERIFY((void *)ptr < (void *)(pktap_v2_hdr_space + 1)); | |
1491 | } | |
1492 | } | |
1493 | ||
1494 | if (extra_src_size > 0) { | |
1495 | char *extra_src_ptr = (char *)(pktap_header + 1); | |
1496 | char *extra_dst_ptr = ((char *)pktap_v2_hdr) + | |
1497 | pktap_v2_hdr->pth_length; | |
1498 | ||
1499 | VERIFY(pktap_v2_hdr->pth_length + extra_src_size <= | |
1500 | sizeof(struct pktap_buffer_v2_hdr_extra)); | |
1501 | ||
1502 | memcpy(extra_dst_ptr, extra_src_ptr, extra_src_size); | |
1503 | } | |
1504 | ||
1505 | VERIFY(pktap_v2_hdr->pth_length + extra_src_size <= | |
1506 | bpf_pkt->bpfp_header_length); | |
1507 | ||
1508 | memcpy(bpf_pkt->bpfp_header, pktap_v2_hdr, | |
1509 | pktap_v2_hdr->pth_length + extra_src_size); | |
1510 | ||
1511 | bpf_pkt->bpfp_total_length += pktap_v2_hdr->pth_length - | |
1512 | sizeof(struct pktap_header); | |
1513 | bpf_pkt->bpfp_header_length += pktap_v2_hdr->pth_length - | |
1514 | sizeof(struct pktap_header); | |
1515 | } |