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