2 * Copyright (c) 2012-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <kern/locks.h>
30 #include <kern/zalloc.h>
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>
42 #include <net/if_var.h>
43 #include <net/if_types.h>
45 #include <net/net_osdep.h>
46 #include <net/pktap.h>
48 #include <netinet/in_pcb.h>
49 #include <netinet/tcp.h>
50 #include <netinet/tcp_var.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/udp.h>
55 #include <netinet/udp_var.h>
57 #include <netinet/ip6.h>
58 #include <netinet6/in6_pcb.h>
60 #include <libkern/OSAtomic.h>
62 #include <kern/debug.h>
64 #include <sys/mcache.h>
68 extern struct inpcbinfo ripcbinfo
;
71 LIST_ENTRY(pktap_softc
) pktp_link
;
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
];
81 #endif /* PKTAP_DEBUG */
83 #define PKTAP_FILTER_OK 0 /* Packet passes filter checks */
84 #define PKTAP_FILTER_SKIP 1 /* Do not tap this packet */
86 static int pktap_inited
= 0;
88 SYSCTL_DECL(_net_link
);
89 SYSCTL_NODE(_net_link
, IFT_PKTAP
, pktap
,
90 CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "pktap virtual interface");
92 uint32_t pktap_total_tap_count
= 0;
93 SYSCTL_UINT(_net_link_pktap
, OID_AUTO
, total_tap_count
,
94 CTLFLAG_RD
| CTLFLAG_LOCKED
, &pktap_total_tap_count
, 0, "");
96 static u_int64_t pktap_count_unknown_if_type
= 0;
97 SYSCTL_QUAD(_net_link_pktap
, OID_AUTO
, count_unknown_if_type
,
98 CTLFLAG_RD
| CTLFLAG_LOCKED
, &pktap_count_unknown_if_type
, "");
100 static int pktap_log
= 0;
101 SYSCTL_INT(_net_link_pktap
, OID_AUTO
, log
,
102 CTLFLAG_RW
| CTLFLAG_LOCKED
, &pktap_log
, 0, "");
104 #define PKTAP_LOG(mask, fmt, ...) \
106 if ((pktap_log & mask)) \
107 printf("%s:%d " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
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
118 * pktap_lck_rw protects the global list of pktap interfaces
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
;
125 static LIST_HEAD(pktap_list
, pktap_softc
) pktap_list
=
126 LIST_HEAD_INITIALIZER(pktap_list
);
128 int pktap_clone_create(struct if_clone
*, u_int32_t
, void *);
129 int pktap_clone_destroy(struct ifnet
*);
131 #define PKTAP_MAXUNIT IF_MAXUNIT
132 #define PKTAP_ZONE_MAX_ELEM MIN(IFNETS_MAX, PKTAP_MAXUNIT)
134 static struct if_clone pktap_cloner
=
135 IF_CLONE_INITIALIZER(PKTAP_IFNAME
,
141 sizeof(struct pktap_softc
));
143 errno_t
pktap_if_output(ifnet_t
, mbuf_t
);
144 errno_t
pktap_demux(ifnet_t
, mbuf_t
, char *, protocol_family_t
*);
145 errno_t
pktap_add_proto(ifnet_t
, protocol_family_t
,
146 const struct ifnet_demux_desc
*, u_int32_t
);
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
*);
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
);
158 pktap_hexdump(int mask
, void *addr
, size_t len
)
160 unsigned char *buf
= addr
;
163 if (!(pktap_log
& mask
))
166 for (i
= 0; i
< len
; i
++) {
167 unsigned char h
= (buf
[i
] & 0xf0) >> 4;
168 unsigned char l
= buf
[i
] & 0x0f;
177 h
< 10 ? h
+ '0' : h
- 10 + 'a',
178 l
< 10 ? l
+ '0' : l
- 10 + 'a');
184 #define _CASSERT_OFFFSETOF_FIELD(s1, s2, f) \
185 _CASSERT(offsetof(struct s1, f) == offsetof(struct s2, f))
187 __private_extern__
void
191 lck_grp_attr_t
*lck_grp_attr
= NULL
;
193 _CASSERT_OFFFSETOF_FIELD(pktap_header
, pktap_v2_hdr
, pth_flags
);
195 /* Make sure we're called only once */
196 VERIFY(pktap_inited
== 0);
200 lck_grp_attr
= lck_grp_attr_alloc_init();
201 pktap_lck_grp
= lck_grp_alloc_init("pktap", lck_grp_attr
);
202 pktap_lck_attr
= lck_attr_alloc_init();
204 lck_attr_setdebug(pktap_lck_attr
);
205 #endif /* PKTAP_DEBUG */
206 lck_rw_init(pktap_lck_rw
, pktap_lck_grp
, pktap_lck_attr
);
207 lck_grp_attr_free(lck_grp_attr
);
209 LIST_INIT(&pktap_list
);
211 error
= if_clone_attach(&pktap_cloner
);
213 panic("%s: if_clone_attach() failed, error %d\n",
217 __private_extern__
int
218 pktap_clone_create(struct if_clone
*ifc
, u_int32_t unit
, __unused
void *params
)
221 struct pktap_softc
*pktap
= NULL
;
222 struct ifnet_init_eparams if_init
;
224 PKTAP_LOG(PKTP_LOG_FUNC
, "unit %u\n", unit
);
226 pktap
= if_clone_softc_allocate(&pktap_cloner
);
228 printf("%s: _MALLOC failed\n", __func__
);
232 pktap
->pktp_unit
= unit
;
235 * By default accept packet from physical interfaces
237 pktap
->pktp_filters
[0].filter_op
= PKTAP_FILTER_OP_PASS
;
238 pktap
->pktp_filters
[0].filter_param
= PKTAP_FILTER_PARAM_IF_TYPE
;
239 pktap
->pktp_filters
[0].filter_param_if_type
= IFT_ETHER
;
242 pktap
->pktp_filters
[1].filter_op
= PKTAP_FILTER_OP_PASS
;
243 pktap
->pktp_filters
[1].filter_param
= PKTAP_FILTER_PARAM_IF_TYPE
;
244 pktap
->pktp_filters
[1].filter_param_if_type
= IFT_CELLULAR
;
245 #else /* CONFIG_EMBEDDED */
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_IEEE1394
;
249 #endif /* CONFIG_EMBEDDED */
251 #if (DEVELOPMENT || DEBUG)
252 pktap
->pktp_filters
[2].filter_op
= PKTAP_FILTER_OP_PASS
;
253 pktap
->pktp_filters
[2].filter_param
= PKTAP_FILTER_PARAM_IF_TYPE
;
254 pktap
->pktp_filters
[2].filter_param_if_type
= IFT_OTHER
;
255 #endif /* DEVELOPMENT || DEBUG */
258 * We do not use a set_bpf_tap() function as we rather rely on the more
259 * accurate callback passed to bpf_attach()
261 bzero(&if_init
, sizeof(if_init
));
262 if_init
.ver
= IFNET_INIT_CURRENT_VERSION
;
263 if_init
.len
= sizeof (if_init
);
264 if_init
.flags
= IFNET_INIT_LEGACY
;
265 if_init
.name
= ifc
->ifc_name
;
267 if_init
.type
= IFT_PKTAP
;
268 if_init
.family
= IFNET_FAMILY_LOOPBACK
;
269 if_init
.output
= pktap_if_output
;
270 if_init
.demux
= pktap_demux
;
271 if_init
.add_proto
= pktap_add_proto
;
272 if_init
.del_proto
= pktap_del_proto
;
273 if_init
.softc
= pktap
;
274 if_init
.ioctl
= pktap_ioctl
;
275 if_init
.detach
= pktap_detach
;
277 error
= ifnet_allocate_extended(&if_init
, &pktap
->pktp_ifp
);
279 printf("%s: ifnet_allocate failed, error %d\n",
284 ifnet_set_flags(pktap
->pktp_ifp
, IFF_UP
, IFF_UP
);
286 error
= ifnet_attach(pktap
->pktp_ifp
, NULL
);
288 printf("%s: ifnet_attach failed - error %d\n", __func__
, error
);
289 ifnet_release(pktap
->pktp_ifp
);
293 /* Attach DLT_PKTAP as the default DLT */
294 bpf_attach(pktap
->pktp_ifp
, DLT_PKTAP
, sizeof(struct pktap_header
),
295 NULL
, pktap_tap_callback
);
296 bpf_attach(pktap
->pktp_ifp
, DLT_RAW
, 0, NULL
, pktap_tap_callback
);
298 /* Take a reference and add to the global list */
299 ifnet_reference(pktap
->pktp_ifp
);
300 lck_rw_lock_exclusive(pktap_lck_rw
);
301 LIST_INSERT_HEAD(&pktap_list
, pktap
, pktp_link
);
302 lck_rw_done(pktap_lck_rw
);
304 if (error
!= 0 && pktap
!= NULL
)
305 if_clone_softc_deallocate(&pktap_cloner
, pktap
);
309 __private_extern__
int
310 pktap_clone_destroy(struct ifnet
*ifp
)
314 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
316 (void) ifnet_detach(ifp
);
322 * This function is called whenever a DLT is set on the interface:
323 * - When interface is attached to a BPF device via BIOCSETIF for the
325 * - Whenever a new DLT is selected via BIOCSDLT
326 * - When the interface is detached from a BPF device (direction is zero)
328 __private_extern__ errno_t
329 pktap_tap_callback(ifnet_t ifp
, u_int32_t dlt
, bpf_tap_mode direction
)
331 struct pktap_softc
*pktap
;
333 pktap
= ifp
->if_softc
;
335 printf("%s: if_softc is NULL for ifp %s\n", __func__
,
341 if (direction
== 0) {
342 if (pktap
->pktp_dlt_raw_count
> 0) {
343 pktap
->pktp_dlt_raw_count
--;
344 OSAddAtomic(-1, &pktap_total_tap_count
);
348 pktap
->pktp_dlt_raw_count
++;
349 OSAddAtomic(1, &pktap_total_tap_count
);
353 if (direction
== 0) {
354 if (pktap
->pktp_dlt_pkttap_count
> 0) {
355 pktap
->pktp_dlt_pkttap_count
--;
356 OSAddAtomic(-1, &pktap_total_tap_count
);
359 pktap
->pktp_dlt_pkttap_count
++;
360 OSAddAtomic(1, &pktap_total_tap_count
);
366 * Attachements count must be positive and we're in trouble
367 * if we have more that 2**31 attachements
369 VERIFY(pktap_total_tap_count
>= 0);
374 __private_extern__ errno_t
375 pktap_if_output(ifnet_t ifp
, mbuf_t m
)
377 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
382 __private_extern__ errno_t
383 pktap_demux(ifnet_t ifp
, __unused mbuf_t m
, __unused
char *header
,
384 __unused protocol_family_t
*ppf
)
386 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
390 __private_extern__ errno_t
391 pktap_add_proto(__unused ifnet_t ifp
, protocol_family_t pf
,
392 __unused
const struct ifnet_demux_desc
*dmx
, __unused u_int32_t cnt
)
394 PKTAP_LOG(PKTP_LOG_FUNC
, "%s pf %u\n", ifp
->if_xname
, pf
);
398 __private_extern__ errno_t
399 pktap_del_proto(__unused ifnet_t ifp
, __unused protocol_family_t pf
)
401 PKTAP_LOG(PKTP_LOG_FUNC
, "%s pf %u\n", ifp
->if_xname
, pf
);
405 __private_extern__ errno_t
406 pktap_getdrvspec(ifnet_t ifp
, struct ifdrv64
*ifd
)
409 struct pktap_softc
*pktap
;
412 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
414 pktap
= ifp
->if_softc
;
417 printf("%s: pktap NULL - error %d\n", __func__
, error
);
421 switch (ifd
->ifd_cmd
) {
422 case PKTP_CMD_FILTER_GET
: {
423 struct x_pktap_filter x_filters
[PKTAP_MAX_FILTERS
];
425 bzero(&x_filters
, sizeof(x_filters
));
427 if (ifd
->ifd_len
< PKTAP_MAX_FILTERS
* sizeof(struct x_pktap_filter
)) {
428 printf("%s: PKTP_CMD_FILTER_GET ifd_len %llu too small - error %d\n",
429 __func__
, ifd
->ifd_len
, error
);
433 for (i
= 0; i
< PKTAP_MAX_FILTERS
; i
++) {
434 struct pktap_filter
*pktap_filter
= pktap
->pktp_filters
+ i
;
435 struct x_pktap_filter
*x_filter
= x_filters
+ i
;
437 x_filter
->filter_op
= pktap_filter
->filter_op
;
438 x_filter
->filter_param
= pktap_filter
->filter_param
;
440 if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_TYPE
)
441 x_filter
->filter_param_if_type
= pktap_filter
->filter_param_if_type
;
442 else if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_NAME
)
443 strlcpy(x_filter
->filter_param_if_name
,
444 pktap_filter
->filter_param_if_name
,
445 sizeof(x_filter
->filter_param_if_name
));
447 error
= copyout(x_filters
, ifd
->ifd_data
,
448 PKTAP_MAX_FILTERS
* sizeof(struct x_pktap_filter
));
450 printf("%s: PKTP_CMD_FILTER_GET copyout - error %d\n", __func__
, error
);
455 case PKTP_CMD_TAP_COUNT
: {
456 uint32_t tap_count
= pktap
->pktp_dlt_raw_count
+ pktap
->pktp_dlt_pkttap_count
;
458 if (ifd
->ifd_len
< sizeof(tap_count
)) {
459 printf("%s: PKTP_CMD_TAP_COUNT ifd_len %llu too small - error %d\n",
460 __func__
, ifd
->ifd_len
, error
);
464 error
= copyout(&tap_count
, ifd
->ifd_data
, sizeof(tap_count
));
466 printf("%s: PKTP_CMD_TAP_COUNT copyout - error %d\n", __func__
, error
);
480 __private_extern__ errno_t
481 pktap_setdrvspec(ifnet_t ifp
, struct ifdrv64
*ifd
)
484 struct pktap_softc
*pktap
;
486 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
488 pktap
= ifp
->if_softc
;
491 printf("%s: pktap NULL - error %d\n", __func__
, error
);
495 switch (ifd
->ifd_cmd
) {
496 case PKTP_CMD_FILTER_SET
: {
497 struct x_pktap_filter user_filters
[PKTAP_MAX_FILTERS
];
501 if (ifd
->ifd_len
!= PKTAP_MAX_FILTERS
* sizeof(struct x_pktap_filter
)) {
502 printf("%s: PKTP_CMD_FILTER_SET bad ifd_len %llu - error %d\n",
503 __func__
, ifd
->ifd_len
, error
);
507 error
= copyin(ifd
->ifd_data
, &user_filters
, ifd
->ifd_len
);
509 printf("%s: copyin - error %d\n", __func__
, error
);
513 * Validate user provided parameters
515 for (i
= 0; i
< PKTAP_MAX_FILTERS
; i
++) {
516 struct x_pktap_filter
*x_filter
= user_filters
+ i
;
518 switch (x_filter
->filter_op
) {
519 case PKTAP_FILTER_OP_NONE
:
520 /* Following entries must be PKTAP_FILTER_OP_NONE */
523 case PKTAP_FILTER_OP_PASS
:
524 case PKTAP_FILTER_OP_SKIP
:
525 /* Invalid after PKTAP_FILTER_OP_NONE */
538 switch (x_filter
->filter_param
) {
539 case PKTAP_FILTER_OP_NONE
:
540 if (x_filter
->filter_op
!= PKTAP_FILTER_OP_NONE
) {
547 * Do not allow to tap a pktap from a pktap
549 case PKTAP_FILTER_PARAM_IF_TYPE
:
550 if (x_filter
->filter_param_if_type
== IFT_PKTAP
||
551 x_filter
->filter_param_if_type
> 0xff) {
557 case PKTAP_FILTER_PARAM_IF_NAME
:
558 if (strncmp(x_filter
->filter_param_if_name
, PKTAP_IFNAME
,
559 strlen(PKTAP_IFNAME
)) == 0) {
574 for (i
= 0; i
< PKTAP_MAX_FILTERS
; i
++) {
575 struct pktap_filter
*pktap_filter
= pktap
->pktp_filters
+ i
;
576 struct x_pktap_filter
*x_filter
= user_filters
+ i
;
578 pktap_filter
->filter_op
= x_filter
->filter_op
;
579 pktap_filter
->filter_param
= x_filter
->filter_param
;
581 if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_TYPE
)
582 pktap_filter
->filter_param_if_type
= x_filter
->filter_param_if_type
;
583 else if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_NAME
) {
586 strlcpy(pktap_filter
->filter_param_if_name
,
587 x_filter
->filter_param_if_name
,
588 sizeof(pktap_filter
->filter_param_if_name
));
590 * If name does not end with a number then it's a "wildcard" match
591 * where we compare the prefix of the interface name
593 len
= strlen(pktap_filter
->filter_param_if_name
);
594 if (pktap_filter
->filter_param_if_name
[len
] < '0' ||
595 pktap_filter
->filter_param_if_name
[len
] > '9')
596 pktap_filter
->filter_ifname_prefix_len
= len
;
610 __private_extern__ errno_t
611 pktap_ioctl(ifnet_t ifp
, unsigned long cmd
, void *data
)
615 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
617 if ((cmd
& IOC_IN
)) {
618 error
= kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER
);
620 PKTAP_LOG(PKTP_LOG_ERROR
,
621 "%s: kauth_authorize_generic(KAUTH_GENERIC_ISSUSER) - error %d\n",
628 case SIOCGDRVSPEC32
: {
630 struct ifdrv32
*ifd32
= (struct ifdrv32
*)data
;
632 memcpy(ifd
.ifd_name
, ifd32
->ifd_name
, sizeof(ifd
.ifd_name
));
633 ifd
.ifd_cmd
= ifd32
->ifd_cmd
;
634 ifd
.ifd_len
= ifd32
->ifd_len
;
635 ifd
.ifd_data
= ifd32
->ifd_data
;
637 error
= pktap_getdrvspec(ifp
, &ifd
);
641 case SIOCGDRVSPEC64
: {
642 struct ifdrv64
*ifd64
= (struct ifdrv64
*)data
;
644 error
= pktap_getdrvspec(ifp
, ifd64
);
648 case SIOCSDRVSPEC32
: {
650 struct ifdrv32
*ifd32
= (struct ifdrv32
*)data
;
652 memcpy(ifd
.ifd_name
, ifd32
->ifd_name
, sizeof(ifd
.ifd_name
));
653 ifd
.ifd_cmd
= ifd32
->ifd_cmd
;
654 ifd
.ifd_len
= ifd32
->ifd_len
;
655 ifd
.ifd_data
= ifd32
->ifd_data
;
657 error
= pktap_setdrvspec(ifp
, &ifd
);
660 case SIOCSDRVSPEC64
: {
661 struct ifdrv64
*ifd64
= (struct ifdrv64
*)data
;
663 error
= pktap_setdrvspec(ifp
, ifd64
);
675 __private_extern__
void
676 pktap_detach(ifnet_t ifp
)
678 struct pktap_softc
*pktap
;
680 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
682 lck_rw_lock_exclusive(pktap_lck_rw
);
684 pktap
= ifp
->if_softc
;
685 ifp
->if_softc
= NULL
;
686 LIST_REMOVE(pktap
, pktp_link
);
688 lck_rw_done(pktap_lck_rw
);
690 /* Drop reference as it's no more on the global list */
693 if_clone_softc_deallocate(&pktap_cloner
, pktap
);
694 /* This is for the reference taken by ifnet_attach() */
695 (void) ifnet_release(ifp
);
698 __private_extern__
int
699 pktap_filter_evaluate(struct pktap_softc
*pktap
, struct ifnet
*ifp
)
702 int result
= PKTAP_FILTER_SKIP
; /* Need positive matching rule to pass */
705 for (i
= 0; i
< PKTAP_MAX_FILTERS
; i
++) {
706 struct pktap_filter
*pktap_filter
= pktap
->pktp_filters
+ i
;
707 size_t len
= pktap_filter
->filter_ifname_prefix_len
!= 0 ?
708 pktap_filter
->filter_ifname_prefix_len
: PKTAP_IFXNAMESIZE
;
710 switch (pktap_filter
->filter_op
) {
711 case PKTAP_FILTER_OP_NONE
:
715 case PKTAP_FILTER_OP_PASS
:
716 if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_TYPE
) {
717 if (pktap_filter
->filter_param_if_type
== 0 ||
718 ifp
->if_type
== pktap_filter
->filter_param_if_type
) {
719 result
= PKTAP_FILTER_OK
;
721 PKTAP_LOG(PKTP_LOG_FILTER
, "pass %s match type %u\n",
722 ifp
->if_xname
, pktap_filter
->filter_param_if_type
);
726 if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_NAME
) {
727 if (strncmp(ifp
->if_xname
, pktap_filter
->filter_param_if_name
,
729 result
= PKTAP_FILTER_OK
;
731 PKTAP_LOG(PKTP_LOG_FILTER
, "pass %s match name %s\n",
732 ifp
->if_xname
, pktap_filter
->filter_param_if_name
);
738 case PKTAP_FILTER_OP_SKIP
:
739 if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_TYPE
) {
740 if (pktap_filter
->filter_param_if_type
== 0 ||
741 ifp
->if_type
== pktap_filter
->filter_param_if_type
) {
742 result
= PKTAP_FILTER_SKIP
;
744 PKTAP_LOG(PKTP_LOG_FILTER
, "skip %s match type %u\n",
745 ifp
->if_xname
, pktap_filter
->filter_param_if_type
);
749 if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_NAME
) {
750 if (strncmp(ifp
->if_xname
, pktap_filter
->filter_param_if_name
,
752 result
= PKTAP_FILTER_SKIP
;
754 PKTAP_LOG(PKTP_LOG_FILTER
, "skip %s match name %s\n",
755 ifp
->if_xname
, pktap_filter
->filter_param_if_name
);
766 PKTAP_LOG(PKTP_LOG_FILTER
, "%s no match\n",
773 pktap_set_procinfo(struct pktap_header
*hdr
, struct so_procinfo
*soprocinfo
)
775 hdr
->pth_pid
= soprocinfo
->spi_pid
;
776 if (hdr
->pth_comm
[0] == 0)
777 proc_name(soprocinfo
->spi_pid
, hdr
->pth_comm
, MAXCOMLEN
);
778 if (soprocinfo
->spi_pid
!= 0)
779 uuid_copy(hdr
->pth_uuid
, soprocinfo
->spi_uuid
);
781 if (soprocinfo
->spi_delegated
!= 0) {
782 hdr
->pth_flags
|= PTH_FLAG_PROC_DELEGATED
;
783 hdr
->pth_epid
= soprocinfo
->spi_epid
;
784 if (hdr
->pth_ecomm
[0] == 0)
785 proc_name(soprocinfo
->spi_epid
, hdr
->pth_ecomm
, MAXCOMLEN
);
786 uuid_copy(hdr
->pth_euuid
, soprocinfo
->spi_euuid
);
790 __private_extern__
void
791 pktap_finalize_proc_info(struct pktap_header
*hdr
)
794 struct so_procinfo soprocinfo
;
796 if (!(hdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
))
799 if (hdr
->pth_ipproto
== IPPROTO_TCP
)
800 found
= inp_findinpcb_procinfo(&tcbinfo
, hdr
->pth_flowid
,
802 else if (hdr
->pth_ipproto
== IPPROTO_UDP
)
803 found
= inp_findinpcb_procinfo(&udbinfo
, hdr
->pth_flowid
,
806 found
= inp_findinpcb_procinfo(&ripcbinfo
, hdr
->pth_flowid
,
810 pktap_set_procinfo(hdr
, &soprocinfo
);
814 pktap_v2_set_procinfo(struct pktap_v2_hdr
*pktap_v2_hdr
,
815 struct so_procinfo
*soprocinfo
)
817 pktap_v2_hdr
->pth_pid
= soprocinfo
->spi_pid
;
819 if (soprocinfo
->spi_pid
!= 0 && soprocinfo
->spi_pid
!= -1) {
820 if (pktap_v2_hdr
->pth_comm_offset
!= 0) {
821 char *ptr
= ((char *)pktap_v2_hdr
) +
822 pktap_v2_hdr
->pth_comm_offset
;
824 proc_name(soprocinfo
->spi_pid
,
825 ptr
, PKTAP_MAX_COMM_SIZE
);
827 if (pktap_v2_hdr
->pth_uuid_offset
!= 0) {
828 uuid_t
*ptr
= (uuid_t
*) (((char *)pktap_v2_hdr
) +
829 pktap_v2_hdr
->pth_uuid_offset
);
831 uuid_copy(*ptr
, soprocinfo
->spi_uuid
);
835 if (!(pktap_v2_hdr
->pth_flags
& PTH_FLAG_PROC_DELEGATED
))
839 * The effective UUID may be set independently from the effective pid
841 if (soprocinfo
->spi_delegated
!= 0) {
842 pktap_v2_hdr
->pth_flags
|= PTH_FLAG_PROC_DELEGATED
;
843 pktap_v2_hdr
->pth_e_pid
= soprocinfo
->spi_epid
;
845 if (soprocinfo
->spi_pid
!= 0 && soprocinfo
->spi_pid
!= -1 &&
846 pktap_v2_hdr
->pth_e_comm_offset
!= 0) {
847 char *ptr
= ((char *)pktap_v2_hdr
) +
848 pktap_v2_hdr
->pth_e_comm_offset
;
850 proc_name(soprocinfo
->spi_epid
,
851 ptr
, PKTAP_MAX_COMM_SIZE
);
853 if (pktap_v2_hdr
->pth_e_uuid_offset
!= 0) {
854 uuid_t
*ptr
= (uuid_t
*) (((char *)pktap_v2_hdr
) +
855 pktap_v2_hdr
->pth_e_uuid_offset
);
857 uuid_copy(*ptr
, soprocinfo
->spi_euuid
);
862 __private_extern__
void
863 pktap_v2_finalize_proc_info(struct pktap_v2_hdr
*pktap_v2_hdr
)
866 struct so_procinfo soprocinfo
;
868 if (!(pktap_v2_hdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
))
871 if (pktap_v2_hdr
->pth_ipproto
== IPPROTO_TCP
) {
872 found
= inp_findinpcb_procinfo(&tcbinfo
,
873 pktap_v2_hdr
->pth_flowid
, &soprocinfo
);
874 } else if (pktap_v2_hdr
->pth_ipproto
== IPPROTO_UDP
) {
875 found
= inp_findinpcb_procinfo(&udbinfo
,
876 pktap_v2_hdr
->pth_flowid
, &soprocinfo
);
878 found
= inp_findinpcb_procinfo(&ripcbinfo
,
879 pktap_v2_hdr
->pth_flowid
, &soprocinfo
);
882 pktap_v2_set_procinfo(pktap_v2_hdr
, &soprocinfo
);
886 __private_extern__
void
887 pktap_fill_proc_info(struct pktap_header
*hdr
, protocol_family_t proto
,
888 struct mbuf
*m
, u_int32_t pre
, int outgoing
, struct ifnet
*ifp
)
891 * Getting the pid and procname is expensive
892 * For outgoing, do the lookup only if there's an
893 * associated socket as indicated by the flowhash
895 if (outgoing
!= 0 && m
->m_pkthdr
.pkt_flowsrc
== FLOWSRC_INPCB
) {
897 * To avoid lock ordering issues we delay the proc UUID lookup
898 * to the BPF read as we cannot
899 * assume the socket lock is unlocked on output
901 hdr
->pth_flags
|= PTH_FLAG_DELAY_PKTAP
;
902 hdr
->pth_flags
|= PTH_FLAG_SOCKET
;
903 hdr
->pth_flowid
= m
->m_pkthdr
.pkt_flowid
;
905 if (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_RAWSOCK
) {
906 hdr
->pth_ipproto
= IPPROTO_RAW
;
908 hdr
->pth_ipproto
= m
->m_pkthdr
.pkt_proto
;
911 if (hdr
->pth_ipproto
== IPPROTO_TCP
) {
912 hdr
->pth_pid
= m
->m_pkthdr
.tx_tcp_pid
;
913 hdr
->pth_epid
= m
->m_pkthdr
.tx_tcp_e_pid
;
914 } else if (hdr
->pth_ipproto
== IPPROTO_UDP
) {
915 hdr
->pth_pid
= m
->m_pkthdr
.tx_udp_pid
;
916 hdr
->pth_epid
= m
->m_pkthdr
.tx_udp_e_pid
;
917 } else if (hdr
->pth_ipproto
== IPPROTO_RAW
) {
918 hdr
->pth_pid
= m
->m_pkthdr
.tx_rawip_pid
;
919 hdr
->pth_epid
= m
->m_pkthdr
.tx_rawip_e_pid
;
922 if (hdr
->pth_pid
!= 0 && hdr
->pth_pid
!= -1) {
923 proc_name(hdr
->pth_pid
, hdr
->pth_comm
, MAXCOMLEN
);
928 if (hdr
->pth_epid
!= 0 && hdr
->pth_epid
!= -1) {
929 hdr
->pth_flags
|= PTH_FLAG_PROC_DELEGATED
;
930 proc_name(hdr
->pth_epid
, hdr
->pth_ecomm
, MAXCOMLEN
);
935 if (m
->m_pkthdr
.pkt_flags
& PKTF_NEW_FLOW
) {
936 hdr
->pth_flags
|= PTH_FLAG_NEW_FLOW
;
938 } else if (outgoing
== 0) {
940 struct so_procinfo soprocinfo
;
941 struct inpcb
*inp
= NULL
;
943 memset(&soprocinfo
, 0, sizeof(struct so_procinfo
));
945 if (proto
== PF_INET
) {
949 struct in_addr faddr
, laddr
;
950 u_short fport
= 0, lport
= 0;
951 struct inpcbinfo
*pcbinfo
= NULL
;
954 error
= mbuf_copydata(m
, pre
, sizeof(struct ip
), &ip
);
956 PKTAP_LOG(PKTP_LOG_ERROR
,
957 "mbuf_copydata tcp v4 failed for %s\n",
961 hlen
= IP_VHL_HL(ip
.ip_vhl
) << 2;
966 if (ip
.ip_p
== IPPROTO_TCP
) {
969 error
= mbuf_copydata(m
, pre
+ hlen
,
970 sizeof(struct tcphdr
), &th
);
978 } else if (ip
.ip_p
== IPPROTO_UDP
) {
981 error
= mbuf_copydata(m
, pre
+ hlen
,
982 sizeof(struct udphdr
), &uh
);
984 PKTAP_LOG(PKTP_LOG_ERROR
,
985 "mbuf_copydata udp v4 failed for %s\n",
995 if (pcbinfo
!= NULL
) {
996 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
,
997 laddr
, lport
, wildcard
, outgoing
? NULL
: ifp
);
999 if (inp
== NULL
&& hdr
->pth_iftype
!= IFT_LOOP
)
1000 PKTAP_LOG(PKTP_LOG_NOPCB
,
1001 "in_pcblookup_hash no pcb %s\n",
1004 PKTAP_LOG(PKTP_LOG_NOPCB
,
1005 "unknown ip_p %u on %s\n",
1006 ip
.ip_p
, hdr
->pth_ifname
);
1007 pktap_hexdump(PKTP_LOG_NOPCB
, &ip
, sizeof(struct ip
));
1009 } else if (proto
== PF_INET6
) {
1012 struct in6_addr
*faddr
;
1013 struct in6_addr
*laddr
;
1014 u_short fport
= 0, lport
= 0;
1015 struct inpcbinfo
*pcbinfo
= NULL
;
1018 error
= mbuf_copydata(m
, pre
, sizeof(struct ip6_hdr
), &ip6
);
1022 faddr
= &ip6
.ip6_src
;
1023 laddr
= &ip6
.ip6_dst
;
1025 if (ip6
.ip6_nxt
== IPPROTO_TCP
) {
1028 error
= mbuf_copydata(m
, pre
+ sizeof(struct ip6_hdr
),
1029 sizeof(struct tcphdr
), &th
);
1031 PKTAP_LOG(PKTP_LOG_ERROR
,
1032 "mbuf_copydata tcp v6 failed for %s\n",
1037 fport
= th
.th_sport
;
1038 lport
= th
.th_dport
;
1041 } else if (ip6
.ip6_nxt
== IPPROTO_UDP
) {
1044 error
= mbuf_copydata(m
, pre
+ sizeof(struct ip6_hdr
),
1045 sizeof(struct udphdr
), &uh
);
1047 PKTAP_LOG(PKTP_LOG_ERROR
,
1048 "mbuf_copydata udp v6 failed for %s\n",
1053 fport
= uh
.uh_sport
;
1054 lport
= uh
.uh_dport
;
1059 if (pcbinfo
!= NULL
) {
1060 inp
= in6_pcblookup_hash(pcbinfo
, faddr
, fport
,
1061 laddr
, lport
, wildcard
, outgoing
? NULL
: ifp
);
1063 if (inp
== NULL
&& hdr
->pth_iftype
!= IFT_LOOP
)
1064 PKTAP_LOG(PKTP_LOG_NOPCB
,
1065 "in6_pcblookup_hash no pcb %s\n",
1068 PKTAP_LOG(PKTP_LOG_NOPCB
,
1069 "unknown ip6.ip6_nxt %u on %s\n",
1070 ip6
.ip6_nxt
, hdr
->pth_ifname
);
1071 pktap_hexdump(PKTP_LOG_NOPCB
, &ip6
, sizeof(struct ip6_hdr
));
1075 hdr
->pth_flags
|= PTH_FLAG_SOCKET
;
1076 if (inp
->inp_state
!= INPCB_STATE_DEAD
&& inp
->inp_socket
!= NULL
) {
1078 inp_get_soprocinfo(inp
, &soprocinfo
);
1080 in_pcb_checkstate(inp
, WNT_RELEASE
, 0);
1084 * -1 means PID not found
1090 pktap_set_procinfo(hdr
, &soprocinfo
);
1094 __private_extern__
void
1095 pktap_bpf_tap(struct ifnet
*ifp
, protocol_family_t proto
, struct mbuf
*m
,
1096 u_int32_t pre
, u_int32_t post
, int outgoing
)
1098 struct pktap_softc
*pktap
;
1099 void (*bpf_tap_func
)(ifnet_t
, u_int32_t
, mbuf_t
, void *, size_t) =
1100 outgoing
? bpf_tap_out
: bpf_tap_in
;
1103 * Skip the coprocessor interface
1105 if (!intcoproc_unrestricted
&& IFNET_IS_INTCOPROC(ifp
))
1108 lck_rw_lock_shared(pktap_lck_rw
);
1111 * No need to take the ifnet_lock as the struct ifnet field if_bpf is
1112 * protected by the BPF subsystem
1114 LIST_FOREACH(pktap
, &pktap_list
, pktp_link
) {
1117 filter_result
= pktap_filter_evaluate(pktap
, ifp
);
1118 if (filter_result
== PKTAP_FILTER_SKIP
)
1121 if (pktap
->pktp_dlt_raw_count
> 0) {
1122 /* We accept only IPv4 and IPv6 packets for the raw DLT */
1123 if ((proto
== AF_INET
||proto
== AF_INET6
) &&
1124 !(m
->m_pkthdr
.pkt_flags
& PKTF_INET_RESOLVE
)) {
1126 * We can play just with the length of the first mbuf in the
1127 * chain because bpf_tap_imp() disregard the packet length
1128 * of the mbuf packet header.
1130 if (mbuf_setdata(m
, m
->m_data
+ pre
, m
->m_len
- pre
) == 0) {
1131 bpf_tap_func(pktap
->pktp_ifp
, DLT_RAW
, m
, NULL
, 0);
1132 mbuf_setdata(m
, m
->m_data
- pre
, m
->m_len
+ pre
);
1137 if (pktap
->pktp_dlt_pkttap_count
> 0) {
1139 struct pktap_header hdr
;
1142 struct pktap_header
*hdr
= &hdr_buffer
.hdr
;
1143 size_t hdr_size
= sizeof(struct pktap_header
);
1144 int unknown_if_type
= 0;
1145 size_t data_adjust
= 0;
1146 u_int32_t pre_adjust
= 0;
1148 /* Verify the structure is packed */
1149 _CASSERT(sizeof(hdr_buffer
) == sizeof(struct pktap_header
) + sizeof(u_int32_t
));
1151 bzero(&hdr_buffer
, sizeof(hdr_buffer
));
1152 hdr
->pth_length
= sizeof(struct pktap_header
);
1153 hdr
->pth_type_next
= PTH_TYPE_PACKET
;
1156 * Set DLT of packet based on interface type
1158 switch (ifp
->if_type
) {
1164 * Packets from pdp interfaces have no loopback
1165 * header that contain the protocol number.
1166 * As BPF just concatenate the header and the
1167 * packet content in a single buffer,
1168 * stash the protocol after the pktap header
1169 * and adjust the size of the header accordingly
1171 hdr
->pth_dlt
= DLT_NULL
;
1173 hdr_buffer
.proto
= proto
;
1174 hdr_size
= sizeof(hdr_buffer
);
1175 pre_adjust
= sizeof(hdr_buffer
.proto
);
1181 case IFT_IEEE8023ADLAG
:
1182 hdr
->pth_dlt
= DLT_EN10MB
;
1185 hdr
->pth_dlt
= DLT_PPP
;
1188 hdr
->pth_dlt
= DLT_APPLE_IP_OVER_IEEE1394
;
1191 if (ifp
->if_subfamily
== IFNET_SUBFAMILY_IPSEC
||
1192 ifp
->if_subfamily
== IFNET_SUBFAMILY_UTUN
) {
1195 * - incoming packets do not have the prefix set to four
1196 * - some packets are as small as two bytes!
1198 if (m_pktlen(m
) < 4)
1200 if (proto
!= AF_INET
&& proto
!= AF_INET6
)
1202 if (proto
== AF_INET
&& (size_t) m_pktlen(m
) - 4 < sizeof(struct ip
))
1204 if (proto
== AF_INET6
&& (size_t) m_pktlen(m
) - 4 < sizeof(struct ip6_hdr
))
1209 * - The old utun encapsulation with the protocol family in network order
1210 * - A raw IPv4 or IPv6 packet
1212 uint8_t data
= *(uint8_t *)mbuf_data(m
);
1213 if ((data
>> 4) == 4 || (data
>> 4) == 6) {
1217 * Skip the protocol in the mbuf as it's in network order
1223 hdr
->pth_dlt
= DLT_NULL
;
1224 hdr_buffer
.proto
= proto
;
1225 hdr_size
= sizeof(hdr_buffer
);
1229 hdr
->pth_dlt
= DLT_RAW
;
1231 unknown_if_type
= 1;
1234 if (unknown_if_type
) {
1235 PKTAP_LOG(PKTP_LOG_FUNC
,
1236 "unknown if_type %u for %s\n",
1237 ifp
->if_type
, ifp
->if_xname
);
1238 pktap_count_unknown_if_type
+= 1;
1240 strlcpy(hdr
->pth_ifname
, ifp
->if_xname
,
1241 sizeof(hdr
->pth_ifname
));
1242 hdr
->pth_flags
|= outgoing
? PTH_FLAG_DIR_OUT
: PTH_FLAG_DIR_IN
;
1243 hdr
->pth_protocol_family
= proto
;
1244 hdr
->pth_frame_pre_length
= pre
+ pre_adjust
;
1245 hdr
->pth_frame_post_length
= post
;
1246 hdr
->pth_iftype
= ifp
->if_type
;
1247 hdr
->pth_ifunit
= ifp
->if_unit
;
1249 if (m
->m_pkthdr
.pkt_flags
& PKTF_KEEPALIVE
)
1250 hdr
->pth_flags
|= PTH_FLAG_KEEP_ALIVE
;
1251 if (m
->m_pkthdr
.pkt_flags
& PKTF_TCP_REXMT
)
1252 hdr
->pth_flags
|= PTH_FLAG_REXMIT
;
1254 pktap_fill_proc_info(hdr
, proto
, m
, pre
, outgoing
, ifp
);
1256 hdr
->pth_svc
= so_svc2tc(m
->m_pkthdr
.pkt_svc
);
1258 if (data_adjust
== 0) {
1259 bpf_tap_func(pktap
->pktp_ifp
, DLT_PKTAP
, m
, hdr
, hdr_size
);
1262 * We can play just with the length of the first mbuf in the
1263 * chain because bpf_tap_imp() disregard the packet length
1264 * of the mbuf packet header.
1266 if (mbuf_setdata(m
, m
->m_data
+ data_adjust
, m
->m_len
- data_adjust
) == 0) {
1267 bpf_tap_func(pktap
->pktp_ifp
, DLT_PKTAP
, m
, hdr
, hdr_size
);
1268 mbuf_setdata(m
, m
->m_data
- data_adjust
, m
->m_len
+ data_adjust
);
1275 lck_rw_done(pktap_lck_rw
);
1278 __private_extern__
void
1279 pktap_input(struct ifnet
*ifp
, protocol_family_t proto
, struct mbuf
*m
,
1286 if (pktap_total_tap_count
== 0)
1289 hdr
= (char *)mbuf_data(m
);
1290 start
= (char *)mbuf_datastart(m
);
1291 /* Make sure the frame header is fully contained in the mbuf */
1292 if (frame_header
!= NULL
&& frame_header
>= start
&& frame_header
<= hdr
) {
1293 size_t o_len
= m
->m_len
;
1294 u_int32_t pre
= hdr
- frame_header
;
1296 if (mbuf_setdata(m
, frame_header
, o_len
+ pre
) == 0) {
1297 PKTAP_LOG(PKTP_LOG_INPUT
, "ifp %s proto %u pre %u post %u\n",
1298 ifp
->if_xname
, proto
, pre
, 0);
1300 pktap_bpf_tap(ifp
, proto
, m
, pre
, 0, 0);
1301 mbuf_setdata(m
, hdr
, o_len
);
1304 PKTAP_LOG(PKTP_LOG_INPUT
, "ifp %s proto %u pre %u post %u\n",
1305 ifp
->if_xname
, proto
, 0, 0);
1307 pktap_bpf_tap(ifp
, proto
, m
, 0, 0, 0);
1311 __private_extern__
void
1312 pktap_output(struct ifnet
*ifp
, protocol_family_t proto
, struct mbuf
*m
,
1313 u_int32_t pre
, u_int32_t post
)
1316 if (pktap_total_tap_count
== 0)
1319 PKTAP_LOG(PKTP_LOG_OUTPUT
, "ifp %s proto %u pre %u post %u\n",
1320 ifp
->if_xname
, proto
, pre
, post
);
1322 pktap_bpf_tap(ifp
, proto
, m
, pre
, post
, 1);
1327 convert_to_pktap_header_to_v2(struct bpf_packet
*bpf_pkt
, bool truncate
)
1329 struct pktap_header
*pktap_header
;
1330 size_t extra_src_size
;
1331 struct pktap_buffer_v2_hdr_extra pktap_buffer_v2_hdr_extra
;
1332 struct pktap_v2_hdr_space
*pktap_v2_hdr_space
;
1333 struct pktap_v2_hdr
*pktap_v2_hdr
;
1336 pktap_header
= (struct pktap_header
*)bpf_pkt
->bpfp_header
;
1338 if (pktap_header
->pth_type_next
!= PTH_TYPE_PACKET
) {
1342 VERIFY(bpf_pkt
->bpfp_header_length
>= sizeof(struct pktap_header
));
1345 * extra_src_size is the length of the optional link layer header
1347 extra_src_size
= bpf_pkt
->bpfp_header_length
-
1348 sizeof(struct pktap_header
);
1350 VERIFY(extra_src_size
<= sizeof(union pktap_header_extra
));
1352 pktap_v2_hdr_space
= &pktap_buffer_v2_hdr_extra
.hdr_space
;
1353 pktap_v2_hdr
= &pktap_v2_hdr_space
->pth_hdr
;
1354 ptr
= (uint8_t *) (pktap_v2_hdr
+ 1);
1356 COPY_PKTAP_COMMON_FIELDS_TO_V2(pktap_v2_hdr
, pktap_header
);
1359 * When truncating don't bother with the process UUIDs
1362 if ((pktap_header
->pth_flags
& PTH_FLAG_DELAY_PKTAP
)) {
1363 pktap_v2_hdr
->pth_uuid_offset
= pktap_v2_hdr
->pth_length
;
1364 pktap_v2_hdr
->pth_length
+= sizeof(uuid_t
);
1365 uuid_clear(*(uuid_t
*)ptr
);
1366 ptr
+= sizeof(uuid_t
);
1367 VERIFY((void *)ptr
< (void *)(pktap_v2_hdr_space
+ 1));
1368 } else if (!uuid_is_null(pktap_header
->pth_uuid
)) {
1369 pktap_v2_hdr
->pth_uuid_offset
= pktap_v2_hdr
->pth_length
;
1370 uuid_copy(*(uuid_t
*)ptr
, pktap_header
->pth_uuid
);
1371 pktap_v2_hdr
->pth_length
+= sizeof(uuid_t
);
1372 ptr
+= sizeof(uuid_t
);
1373 VERIFY((void *)ptr
< (void *)(pktap_v2_hdr_space
+ 1));
1376 if ((pktap_header
->pth_flags
& PTH_FLAG_DELAY_PKTAP
)) {
1377 if (pktap_header
->pth_flags
& PTH_FLAG_PROC_DELEGATED
) {
1378 pktap_v2_hdr
->pth_e_uuid_offset
= pktap_v2_hdr
->pth_length
;
1379 uuid_clear(*(uuid_t
*)ptr
);
1380 pktap_v2_hdr
->pth_length
+= sizeof(uuid_t
);
1381 ptr
+= sizeof(uuid_t
);
1382 VERIFY((void *)ptr
< (void *)(pktap_v2_hdr_space
+ 1));
1384 } else if(!uuid_is_null(pktap_header
->pth_euuid
)) {
1385 pktap_v2_hdr
->pth_e_uuid_offset
= pktap_v2_hdr
->pth_length
;
1386 uuid_copy(*(uuid_t
*)ptr
, pktap_header
->pth_euuid
);
1387 pktap_v2_hdr
->pth_length
+= sizeof(uuid_t
);
1388 ptr
+= sizeof(uuid_t
);
1389 VERIFY((void *)ptr
< (void *)(pktap_v2_hdr_space
+ 1));
1393 if (pktap_header
->pth_ifname
[0] != 0) {
1396 pktap_v2_hdr
->pth_ifname_offset
= pktap_v2_hdr
->pth_length
;
1399 * Note: strlcpy() returns the length of the string so we need
1400 * to add one for the end-of-string
1402 strsize
= 1 + strlcpy((char *)ptr
, pktap_header
->pth_ifname
,
1403 sizeof(pktap_v2_hdr_space
->pth_ifname
));
1404 pktap_v2_hdr
->pth_length
+= strsize
;
1406 VERIFY((void *)ptr
< (void *)(pktap_v2_hdr_space
+ 1));
1410 * Do not waste space with the process name if we do not have a pid
1412 if (pktap_header
->pth_pid
!= 0 && pktap_header
->pth_pid
!= -1) {
1413 if (pktap_header
->pth_comm
[0] != 0) {
1416 pktap_v2_hdr
->pth_comm_offset
= pktap_v2_hdr
->pth_length
;
1418 strsize
= 1 + strlcpy((char *)ptr
, pktap_header
->pth_comm
,
1419 sizeof(pktap_v2_hdr_space
->pth_comm
));
1420 pktap_v2_hdr
->pth_length
+= strsize
;
1422 VERIFY((void *)ptr
< (void *)(pktap_v2_hdr_space
+ 1));
1423 } else if ((pktap_header
->pth_flags
& PTH_FLAG_DELAY_PKTAP
)) {
1424 size_t strsize
= sizeof(pktap_v2_hdr_space
->pth_comm
);
1426 pktap_v2_hdr
->pth_comm_offset
= pktap_v2_hdr
->pth_length
;
1428 *ptr
= 0; /* empty string by default */
1429 pktap_v2_hdr
->pth_length
+= strsize
;
1431 VERIFY((void *)ptr
< (void *)(pktap_v2_hdr_space
+ 1));
1436 * Do not waste space with the effective process name if we do not have
1437 * an effective pid or it's the same as the pid
1439 if (pktap_header
->pth_epid
!= 0 && pktap_header
->pth_epid
!= -1 &&
1440 pktap_header
->pth_epid
!= pktap_header
->pth_pid
) {
1441 if (pktap_header
->pth_ecomm
[0] != 0) {
1444 pktap_v2_hdr
->pth_e_comm_offset
= pktap_v2_hdr
->pth_length
;
1446 strsize
= 1 + strlcpy((char *)ptr
, pktap_header
->pth_ecomm
,
1447 sizeof(pktap_v2_hdr_space
->pth_e_comm
));
1448 pktap_v2_hdr
->pth_length
+= strsize
;
1450 VERIFY((void *)ptr
< (void *)(pktap_v2_hdr_space
+ 1));
1451 } else if ((pktap_header
->pth_flags
& PTH_FLAG_DELAY_PKTAP
)) {
1452 size_t strsize
= sizeof(pktap_v2_hdr_space
->pth_e_comm
);
1454 pktap_v2_hdr
->pth_e_comm_offset
= pktap_v2_hdr
->pth_length
;
1455 *ptr
= 0; /* empty string by default */
1456 pktap_v2_hdr
->pth_length
+= strsize
;
1458 VERIFY((void *)ptr
< (void *)(pktap_v2_hdr_space
+ 1));
1462 if (extra_src_size
> 0) {
1463 char *extra_src_ptr
= (char *)(pktap_header
+ 1);
1464 char *extra_dst_ptr
= ((char *)pktap_v2_hdr
) +
1465 pktap_v2_hdr
->pth_length
;
1467 VERIFY(pktap_v2_hdr
->pth_length
+ extra_src_size
<=
1468 sizeof(struct pktap_buffer_v2_hdr_extra
));
1470 memcpy(extra_dst_ptr
, extra_src_ptr
, extra_src_size
);
1473 VERIFY(pktap_v2_hdr
->pth_length
+ extra_src_size
<=
1474 bpf_pkt
->bpfp_header_length
);
1476 memcpy(bpf_pkt
->bpfp_header
, pktap_v2_hdr
,
1477 pktap_v2_hdr
->pth_length
+ extra_src_size
);
1479 bpf_pkt
->bpfp_total_length
+= pktap_v2_hdr
->pth_length
-
1480 sizeof(struct pktap_header
);
1481 bpf_pkt
->bpfp_header_length
+= pktap_v2_hdr
->pth_length
-
1482 sizeof(struct pktap_header
);