2 * Copyright (c) 2012-2014 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>
31 #include <sys/types.h>
32 #include <sys/kernel_types.h>
33 #include <sys/kauth.h>
34 #include <sys/socket.h>
35 #include <sys/socketvar.h>
36 #include <sys/sockio.h>
37 #include <sys/sysctl.h>
41 #include <net/if_var.h>
42 #include <net/if_types.h>
44 #include <net/net_osdep.h>
45 #include <net/pktap.h>
47 #include <netinet/in_pcb.h>
48 #include <netinet/tcp.h>
49 #include <netinet/tcp_var.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/udp.h>
54 #include <netinet/udp_var.h>
56 #include <netinet/ip6.h>
57 #include <netinet6/in6_pcb.h>
59 #include <libkern/OSAtomic.h>
61 #include <kern/debug.h>
63 #include <sys/mcache.h>
67 extern struct inpcbinfo ripcbinfo
;
70 LIST_ENTRY(pktap_softc
) pktp_link
;
72 uint32_t pktp_dlt_raw_count
;
73 uint32_t pktp_dlt_pkttap_count
;
74 struct ifnet
*pktp_ifp
;
75 struct pktap_filter pktp_filters
[PKTAP_MAX_FILTERS
];
80 #endif /* PKTAP_DEBUG */
82 #define PKTAP_FILTER_OK 0 /* Packet passes filter checks */
83 #define PKTAP_FILTER_SKIP 1 /* Do not tap this packet */
85 static int pktap_inited
= 0;
87 SYSCTL_DECL(_net_link
);
88 SYSCTL_NODE(_net_link
, IFT_PKTAP
, pktap
,
89 CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "pktap virtual interface");
91 static int pktap_total_tap_count
= 0;
92 SYSCTL_INT(_net_link_pktap
, OID_AUTO
, total_tap_count
,
93 CTLFLAG_RD
| CTLFLAG_LOCKED
, &pktap_total_tap_count
, 0, "");
95 static u_int64_t pktap_count_unknown_if_type
= 0;
96 SYSCTL_QUAD(_net_link_pktap
, OID_AUTO
, count_unknown_if_type
,
97 CTLFLAG_RD
| CTLFLAG_LOCKED
, &pktap_count_unknown_if_type
, "");
99 static int pktap_log
= 0;
100 SYSCTL_INT(_net_link_pktap
, OID_AUTO
, log
,
101 CTLFLAG_RW
| CTLFLAG_LOCKED
, &pktap_log
, 0, "");
103 #define PKTAP_LOG(mask, fmt, ...) \
105 if ((pktap_log & mask)) \
106 printf("%s:%d " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
109 #define PKTP_LOG_FUNC 0x01
110 #define PKTP_LOG_FILTER 0x02
111 #define PKTP_LOG_INPUT 0x04
112 #define PKTP_LOG_OUTPUT 0x08
113 #define PKTP_LOG_ERROR 0x10
114 #define PKTP_LOG_NOPCB 0x20
117 * pktap_lck_rw protects the global list of pktap interfaces
119 decl_lck_rw_data(static, pktap_lck_rw_data
);
120 static lck_rw_t
*pktap_lck_rw
= &pktap_lck_rw_data
;
121 static lck_grp_t
*pktap_lck_grp
= NULL
;
122 static lck_attr_t
*pktap_lck_attr
= NULL
;
124 static LIST_HEAD(pktap_list
, pktap_softc
) pktap_list
=
125 LIST_HEAD_INITIALIZER(pktap_list
);
127 int pktap_clone_create(struct if_clone
*, u_int32_t
, void *);
128 int pktap_clone_destroy(struct ifnet
*);
130 static struct if_clone pktap_cloner
=
131 IF_CLONE_INITIALIZER(PKTAP_IFNAME
,
137 errno_t
pktap_if_output(ifnet_t
, mbuf_t
);
138 errno_t
pktap_demux(ifnet_t
, mbuf_t
, char *, protocol_family_t
*);
139 errno_t
pktap_add_proto(ifnet_t
, protocol_family_t
,
140 const struct ifnet_demux_desc
*, u_int32_t
);
141 errno_t
pktap_del_proto(ifnet_t
, protocol_family_t
);
142 errno_t
pktap_getdrvspec(ifnet_t
, struct ifdrv64
*);
143 errno_t
pktap_setdrvspec(ifnet_t
, struct ifdrv64
*);
144 errno_t
pktap_ioctl(ifnet_t
, unsigned long, void *);
145 void pktap_detach(ifnet_t
);
146 int pktap_filter_evaluate(struct pktap_softc
*, struct ifnet
*);
147 void pktap_bpf_tap(struct ifnet
*, protocol_family_t
, struct mbuf
*,
148 u_int32_t
, u_int32_t
, int);
149 errno_t
pktap_tap_callback(ifnet_t
, u_int32_t
, bpf_tap_mode
);
152 pktap_hexdump(int mask
, void *addr
, size_t len
)
154 unsigned char *buf
= addr
;
157 if (!(pktap_log
& mask
))
160 for (i
= 0; i
< len
; i
++) {
161 unsigned char h
= (buf
[i
] & 0xf0) >> 4;
162 unsigned char l
= buf
[i
] & 0x0f;
171 h
< 10 ? h
+ '0' : h
- 10 + 'a',
172 l
< 10 ? l
+ '0' : l
- 10 + 'a');
178 __private_extern__
void
182 lck_grp_attr_t
*lck_grp_attr
= NULL
;
184 /* Make sure we're called only once */
185 VERIFY(pktap_inited
== 0);
189 lck_grp_attr
= lck_grp_attr_alloc_init();
190 pktap_lck_grp
= lck_grp_alloc_init("pktap", lck_grp_attr
);
191 pktap_lck_attr
= lck_attr_alloc_init();
193 lck_attr_setdebug(pktap_lck_attr
);
194 #endif /* PKTAP_DEBUG */
195 lck_rw_init(pktap_lck_rw
, pktap_lck_grp
, pktap_lck_attr
);
196 lck_grp_attr_free(lck_grp_attr
);
198 LIST_INIT(&pktap_list
);
200 error
= if_clone_attach(&pktap_cloner
);
202 panic("%s: if_clone_attach() failed, error %d\n",
206 __private_extern__
int
207 pktap_clone_create(struct if_clone
*ifc
, u_int32_t unit
, __unused
void *params
)
210 struct pktap_softc
*pktap
= NULL
;
211 struct ifnet_init_params if_init
;
213 PKTAP_LOG(PKTP_LOG_FUNC
, "unit %u\n", unit
);
215 pktap
= _MALLOC(sizeof(struct pktap_softc
), M_DEVBUF
,
218 printf("%s: _MALLOC failed\n", __func__
);
222 pktap
->pktp_unit
= unit
;
225 * By default accept packet from physical interfaces
227 pktap
->pktp_filters
[0].filter_op
= PKTAP_FILTER_OP_PASS
;
228 pktap
->pktp_filters
[0].filter_param
= PKTAP_FILTER_PARAM_IF_TYPE
;
229 pktap
->pktp_filters
[0].filter_param_if_type
= IFT_ETHER
;
231 pktap
->pktp_filters
[1].filter_op
= PKTAP_FILTER_OP_PASS
;
232 pktap
->pktp_filters
[1].filter_param
= PKTAP_FILTER_PARAM_IF_TYPE
;
233 pktap
->pktp_filters
[1].filter_param_if_type
= IFT_IEEE1394
;
235 * We do not use a set_bpf_tap() function as we rather rely on the more
236 * accurate callback passed to bpf_attach()
238 bzero(&if_init
, sizeof(struct ifnet_init_params
));
239 if_init
.name
= ifc
->ifc_name
;
241 if_init
.type
= IFT_PKTAP
;
242 if_init
.family
= IFNET_FAMILY_LOOPBACK
;
243 if_init
.output
= pktap_if_output
;
244 if_init
.demux
= pktap_demux
;
245 if_init
.add_proto
= pktap_add_proto
;
246 if_init
.del_proto
= pktap_del_proto
;
247 if_init
.softc
= pktap
;
248 if_init
.ioctl
= pktap_ioctl
;
249 if_init
.detach
= pktap_detach
;
251 error
= ifnet_allocate(&if_init
, &pktap
->pktp_ifp
);
253 printf("%s: ifnet_allocate failed, error %d\n",
258 ifnet_set_flags(pktap
->pktp_ifp
, IFF_UP
, IFF_UP
);
260 error
= ifnet_attach(pktap
->pktp_ifp
, NULL
);
262 printf("%s: ifnet_attach failed - error %d\n", __func__
, error
);
263 ifnet_release(pktap
->pktp_ifp
);
267 /* Attach DLT_PKTAP as the default DLT */
268 bpf_attach(pktap
->pktp_ifp
, DLT_PKTAP
, sizeof(struct pktap_header
),
269 NULL
, pktap_tap_callback
);
270 bpf_attach(pktap
->pktp_ifp
, DLT_RAW
, 0, NULL
, pktap_tap_callback
);
272 /* Take a reference and add to the global list */
273 ifnet_reference(pktap
->pktp_ifp
);
274 lck_rw_lock_exclusive(pktap_lck_rw
);
275 LIST_INSERT_HEAD(&pktap_list
, pktap
, pktp_link
);
276 lck_rw_done(pktap_lck_rw
);
280 _FREE(pktap
, M_DEVBUF
);
285 __private_extern__
int
286 pktap_clone_destroy(struct ifnet
*ifp
)
290 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
292 (void) ifnet_detach(ifp
);
298 * This function is called whenever a DLT is set on the interface:
299 * - When interface is attached to a BPF device via BIOCSETIF for the
301 * - Whenever a new DLT is selected via BIOCSDLT
302 * - When the interface is detached from a BPF device (direction is zero)
304 __private_extern__ errno_t
305 pktap_tap_callback(ifnet_t ifp
, u_int32_t dlt
, bpf_tap_mode direction
)
307 struct pktap_softc
*pktap
;
309 pktap
= ifp
->if_softc
;
311 printf("%s: if_softc is NULL for ifp %s\n", __func__
,
317 if (direction
== 0) {
318 if (pktap
->pktp_dlt_raw_count
> 0) {
319 pktap
->pktp_dlt_raw_count
--;
320 OSAddAtomic(-1, &pktap_total_tap_count
);
324 pktap
->pktp_dlt_raw_count
++;
325 OSAddAtomic(1, &pktap_total_tap_count
);
329 if (direction
== 0) {
330 if (pktap
->pktp_dlt_pkttap_count
> 0) {
331 pktap
->pktp_dlt_pkttap_count
--;
332 OSAddAtomic(-1, &pktap_total_tap_count
);
335 pktap
->pktp_dlt_pkttap_count
++;
336 OSAddAtomic(1, &pktap_total_tap_count
);
342 * Attachements count must be positive and we're in trouble
343 * if we have more that 2**31 attachements
345 VERIFY(pktap_total_tap_count
>= 0);
350 __private_extern__ errno_t
351 pktap_if_output(ifnet_t ifp
, mbuf_t m
)
353 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
358 __private_extern__ errno_t
359 pktap_demux(ifnet_t ifp
, __unused mbuf_t m
, __unused
char *header
,
360 __unused protocol_family_t
*ppf
)
362 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
366 __private_extern__ errno_t
367 pktap_add_proto(__unused ifnet_t ifp
, protocol_family_t pf
,
368 __unused
const struct ifnet_demux_desc
*dmx
, __unused u_int32_t cnt
)
370 PKTAP_LOG(PKTP_LOG_FUNC
, "%s pf %u\n", ifp
->if_xname
, pf
);
374 __private_extern__ errno_t
375 pktap_del_proto(__unused ifnet_t ifp
, __unused protocol_family_t pf
)
377 PKTAP_LOG(PKTP_LOG_FUNC
, "%s pf %u\n", ifp
->if_xname
, pf
);
381 __private_extern__ errno_t
382 pktap_getdrvspec(ifnet_t ifp
, struct ifdrv64
*ifd
)
385 struct pktap_softc
*pktap
;
388 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
390 pktap
= ifp
->if_softc
;
393 printf("%s: pktap NULL - error %d\n", __func__
, error
);
397 switch (ifd
->ifd_cmd
) {
398 case PKTP_CMD_FILTER_GET
: {
399 struct x_pktap_filter x_filters
[PKTAP_MAX_FILTERS
];
401 bzero(&x_filters
, sizeof(x_filters
));
403 if (ifd
->ifd_len
< PKTAP_MAX_FILTERS
* sizeof(struct x_pktap_filter
)) {
404 printf("%s: PKTP_CMD_FILTER_GET ifd_len %llu too small - error %d\n",
405 __func__
, ifd
->ifd_len
, error
);
409 for (i
= 0; i
< PKTAP_MAX_FILTERS
; i
++) {
410 struct pktap_filter
*pktap_filter
= pktap
->pktp_filters
+ i
;
411 struct x_pktap_filter
*x_filter
= x_filters
+ i
;
413 x_filter
->filter_op
= pktap_filter
->filter_op
;
414 x_filter
->filter_param
= pktap_filter
->filter_param
;
416 if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_TYPE
)
417 x_filter
->filter_param_if_type
= pktap_filter
->filter_param_if_type
;
418 else if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_NAME
)
419 strlcpy(x_filter
->filter_param_if_name
,
420 pktap_filter
->filter_param_if_name
,
421 sizeof(x_filter
->filter_param_if_name
));
423 error
= copyout(x_filters
, ifd
->ifd_data
,
424 PKTAP_MAX_FILTERS
* sizeof(struct x_pktap_filter
));
426 printf("%s: PKTP_CMD_FILTER_GET copyout - error %d\n", __func__
, error
);
431 case PKTP_CMD_TAP_COUNT
: {
432 uint32_t tap_count
= pktap
->pktp_dlt_raw_count
+ pktap
->pktp_dlt_pkttap_count
;
434 if (ifd
->ifd_len
< sizeof(tap_count
)) {
435 printf("%s: PKTP_CMD_TAP_COUNT ifd_len %llu too small - error %d\n",
436 __func__
, ifd
->ifd_len
, error
);
440 error
= copyout(&tap_count
, ifd
->ifd_data
, sizeof(tap_count
));
442 printf("%s: PKTP_CMD_TAP_COUNT copyout - error %d\n", __func__
, error
);
456 __private_extern__ errno_t
457 pktap_setdrvspec(ifnet_t ifp
, struct ifdrv64
*ifd
)
460 struct pktap_softc
*pktap
;
462 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
464 pktap
= ifp
->if_softc
;
467 printf("%s: pktap NULL - error %d\n", __func__
, error
);
471 switch (ifd
->ifd_cmd
) {
472 case PKTP_CMD_FILTER_SET
: {
473 struct x_pktap_filter user_filters
[PKTAP_MAX_FILTERS
];
477 if (ifd
->ifd_len
!= PKTAP_MAX_FILTERS
* sizeof(struct x_pktap_filter
)) {
478 printf("%s: PKTP_CMD_FILTER_SET bad ifd_len %llu - error %d\n",
479 __func__
, ifd
->ifd_len
, error
);
483 error
= copyin(ifd
->ifd_data
, &user_filters
, ifd
->ifd_len
);
485 printf("%s: copyin - error %d\n", __func__
, error
);
489 * Validate user provided parameters
491 for (i
= 0; i
< PKTAP_MAX_FILTERS
; i
++) {
492 struct x_pktap_filter
*x_filter
= user_filters
+ i
;
494 switch (x_filter
->filter_op
) {
495 case PKTAP_FILTER_OP_NONE
:
496 /* Following entries must be PKTAP_FILTER_OP_NONE */
499 case PKTAP_FILTER_OP_PASS
:
500 case PKTAP_FILTER_OP_SKIP
:
501 /* Invalid after PKTAP_FILTER_OP_NONE */
514 switch (x_filter
->filter_param
) {
515 case PKTAP_FILTER_OP_NONE
:
516 if (x_filter
->filter_op
!= PKTAP_FILTER_OP_NONE
) {
523 * Do not allow to tap a pktap from a pktap
525 case PKTAP_FILTER_PARAM_IF_TYPE
:
526 if (x_filter
->filter_param_if_type
== IFT_PKTAP
||
527 x_filter
->filter_param_if_type
> 0xff) {
533 case PKTAP_FILTER_PARAM_IF_NAME
:
534 if (strncmp(x_filter
->filter_param_if_name
, PKTAP_IFNAME
,
535 strlen(PKTAP_IFNAME
)) == 0) {
550 for (i
= 0; i
< PKTAP_MAX_FILTERS
; i
++) {
551 struct pktap_filter
*pktap_filter
= pktap
->pktp_filters
+ i
;
552 struct x_pktap_filter
*x_filter
= user_filters
+ i
;
554 pktap_filter
->filter_op
= x_filter
->filter_op
;
555 pktap_filter
->filter_param
= x_filter
->filter_param
;
557 if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_TYPE
)
558 pktap_filter
->filter_param_if_type
= x_filter
->filter_param_if_type
;
559 else if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_NAME
) {
562 strlcpy(pktap_filter
->filter_param_if_name
,
563 x_filter
->filter_param_if_name
,
564 sizeof(pktap_filter
->filter_param_if_name
));
566 * If name does not end with a number then it's a "wildcard" match
567 * where we compare the prefix of the interface name
569 len
= strlen(pktap_filter
->filter_param_if_name
);
570 if (pktap_filter
->filter_param_if_name
[len
] < '0' ||
571 pktap_filter
->filter_param_if_name
[len
] > '9')
572 pktap_filter
->filter_ifname_prefix_len
= len
;
586 __private_extern__ errno_t
587 pktap_ioctl(ifnet_t ifp
, unsigned long cmd
, void *data
)
591 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
593 if ((cmd
& IOC_IN
)) {
594 error
= kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER
);
596 PKTAP_LOG(PKTP_LOG_ERROR
,
597 "%s: kauth_authorize_generic(KAUTH_GENERIC_ISSUSER) - error %d\n",
604 case SIOCGDRVSPEC32
: {
606 struct ifdrv32
*ifd32
= (struct ifdrv32
*)data
;
608 memcpy(ifd
.ifd_name
, ifd32
->ifd_name
, sizeof(ifd
.ifd_name
));
609 ifd
.ifd_cmd
= ifd32
->ifd_cmd
;
610 ifd
.ifd_len
= ifd32
->ifd_len
;
611 ifd
.ifd_data
= ifd32
->ifd_data
;
613 error
= pktap_getdrvspec(ifp
, &ifd
);
617 case SIOCGDRVSPEC64
: {
618 struct ifdrv64
*ifd64
= (struct ifdrv64
*)data
;
620 error
= pktap_getdrvspec(ifp
, ifd64
);
624 case SIOCSDRVSPEC32
: {
626 struct ifdrv32
*ifd32
= (struct ifdrv32
*)data
;
628 memcpy(ifd
.ifd_name
, ifd32
->ifd_name
, sizeof(ifd
.ifd_name
));
629 ifd
.ifd_cmd
= ifd32
->ifd_cmd
;
630 ifd
.ifd_len
= ifd32
->ifd_len
;
631 ifd
.ifd_data
= ifd32
->ifd_data
;
633 error
= pktap_setdrvspec(ifp
, &ifd
);
636 case SIOCSDRVSPEC64
: {
637 struct ifdrv64
*ifd64
= (struct ifdrv64
*)data
;
639 error
= pktap_setdrvspec(ifp
, ifd64
);
651 __private_extern__
void
652 pktap_detach(ifnet_t ifp
)
654 struct pktap_softc
*pktap
;
656 PKTAP_LOG(PKTP_LOG_FUNC
, "%s\n", ifp
->if_xname
);
658 lck_rw_lock_exclusive(pktap_lck_rw
);
660 pktap
= ifp
->if_softc
;
661 ifp
->if_softc
= NULL
;
662 LIST_REMOVE(pktap
, pktp_link
);
664 lck_rw_done(pktap_lck_rw
);
666 /* Drop reference as it's no more on the global list */
669 _FREE(pktap
, M_DEVBUF
);
671 /* This is for the reference taken by ifnet_attach() */
672 (void) ifnet_release(ifp
);
675 __private_extern__
int
676 pktap_filter_evaluate(struct pktap_softc
*pktap
, struct ifnet
*ifp
)
679 int result
= PKTAP_FILTER_SKIP
; /* Need positive matching rule to pass */
682 for (i
= 0; i
< PKTAP_MAX_FILTERS
; i
++) {
683 struct pktap_filter
*pktap_filter
= pktap
->pktp_filters
+ i
;
684 size_t len
= pktap_filter
->filter_ifname_prefix_len
!= 0 ?
685 pktap_filter
->filter_ifname_prefix_len
: PKTAP_IFXNAMESIZE
;
687 switch (pktap_filter
->filter_op
) {
688 case PKTAP_FILTER_OP_NONE
:
692 case PKTAP_FILTER_OP_PASS
:
693 if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_TYPE
) {
694 if (pktap_filter
->filter_param_if_type
== 0 ||
695 ifp
->if_type
== pktap_filter
->filter_param_if_type
) {
696 result
= PKTAP_FILTER_OK
;
698 PKTAP_LOG(PKTP_LOG_FILTER
, "pass %s match type %u\n",
699 ifp
->if_xname
, pktap_filter
->filter_param_if_type
);
703 if (pktap_filter
->filter_param
== PKTAP_FILTER_PARAM_IF_NAME
) {
704 if (strncmp(ifp
->if_xname
, pktap_filter
->filter_param_if_name
,
706 result
= PKTAP_FILTER_OK
;
708 PKTAP_LOG(PKTP_LOG_FILTER
, "pass %s match name %s\n",
709 ifp
->if_xname
, pktap_filter
->filter_param_if_name
);
715 case PKTAP_FILTER_OP_SKIP
:
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_SKIP
;
721 PKTAP_LOG(PKTP_LOG_FILTER
, "skip %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_SKIP
;
731 PKTAP_LOG(PKTP_LOG_FILTER
, "skip %s match name %s\n",
732 ifp
->if_xname
, pktap_filter
->filter_param_if_name
);
743 PKTAP_LOG(PKTP_LOG_FILTER
, "%s no match\n",
750 pktap_set_procinfo(struct pktap_header
*hdr
, struct so_procinfo
*soprocinfo
)
752 hdr
->pth_pid
= soprocinfo
->spi_pid
;
753 proc_name(soprocinfo
->spi_pid
, hdr
->pth_comm
, MAXCOMLEN
);
754 if (soprocinfo
->spi_pid
!= 0)
755 uuid_copy(hdr
->pth_uuid
, soprocinfo
->spi_uuid
);
758 * When not delegated, the effective pid is the same as the real pid
760 if (soprocinfo
->spi_delegated
!= 0) {
761 hdr
->pth_flags
|= PTH_FLAG_PROC_DELEGATED
;
762 hdr
->pth_epid
= soprocinfo
->spi_epid
;
763 proc_name(soprocinfo
->spi_epid
, hdr
->pth_ecomm
, MAXCOMLEN
);
764 uuid_copy(hdr
->pth_euuid
, soprocinfo
->spi_euuid
);
768 __private_extern__
void
769 pktap_finalize_proc_info(struct pktap_header
*hdr
)
772 struct so_procinfo soprocinfo
;
774 if (!(hdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
))
778 * Clear the flag as it's internal
780 hdr
->pth_flags
&= ~PTH_FLAG_DELAY_PKTAP
;
782 if (hdr
->pth_ipproto
== IPPROTO_TCP
)
783 found
= inp_findinpcb_procinfo(&tcbinfo
, hdr
->pth_flowid
,
785 else if (hdr
->pth_ipproto
== IPPROTO_UDP
)
786 found
= inp_findinpcb_procinfo(&udbinfo
, hdr
->pth_flowid
,
789 found
= inp_findinpcb_procinfo(&ripcbinfo
, hdr
->pth_flowid
,
793 pktap_set_procinfo(hdr
, &soprocinfo
);
796 __private_extern__
void
797 pktap_fill_proc_info(struct pktap_header
*hdr
, protocol_family_t proto
,
798 struct mbuf
*m
, u_int32_t pre
, int outgoing
, struct ifnet
*ifp
)
801 struct so_procinfo soprocinfo
;
804 * Getting the pid and procname is expensive
805 * For outgoing, do the lookup only if there's an
806 * associated socket as indicated by the flowhash
808 if (outgoing
!= 0 && m
->m_pkthdr
.pkt_flowsrc
== FLOWSRC_INPCB
) {
810 * To avoid lock ordering issues we delay the process lookup
811 * to the BPF read as we cannot
812 * assume the socket lock is unlocked on output
815 hdr
->pth_flags
|= PTH_FLAG_DELAY_PKTAP
;
816 hdr
->pth_flowid
= m
->m_pkthdr
.pkt_flowid
;
817 if (m
->m_pkthdr
.pkt_flags
& PKTF_FLOW_RAWSOCK
)
818 hdr
->pth_ipproto
= IPPROTO_RAW
;
820 hdr
->pth_ipproto
= m
->m_pkthdr
.pkt_proto
;
821 } else if (outgoing
== 0) {
822 struct inpcb
*inp
= NULL
;
824 if (proto
== PF_INET
) {
828 struct in_addr faddr
, laddr
;
829 u_short fport
, lport
;
830 struct inpcbinfo
*pcbinfo
= NULL
;
833 error
= mbuf_copydata(m
, pre
, sizeof(struct ip
), &ip
);
835 PKTAP_LOG(PKTP_LOG_ERROR
,
836 "mbuf_copydata tcp v4 failed for %s\n",
840 hlen
= IP_VHL_HL(ip
.ip_vhl
) << 2;
845 if (ip
.ip_p
== IPPROTO_TCP
) {
848 error
= mbuf_copydata(m
, pre
+ hlen
,
849 sizeof(struct tcphdr
), &th
);
857 } else if (ip
.ip_p
== IPPROTO_UDP
) {
860 error
= mbuf_copydata(m
, pre
+ hlen
,
861 sizeof(struct udphdr
), &uh
);
863 PKTAP_LOG(PKTP_LOG_ERROR
,
864 "mbuf_copydata udp v4 failed for %s\n",
874 if (pcbinfo
!= NULL
) {
875 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
,
876 laddr
, lport
, wildcard
, outgoing
? NULL
: ifp
);
878 if (inp
== NULL
&& hdr
->pth_iftype
!= IFT_LOOP
)
879 PKTAP_LOG(PKTP_LOG_NOPCB
,
880 "in_pcblookup_hash no pcb %s\n",
883 PKTAP_LOG(PKTP_LOG_NOPCB
,
884 "unknown ip_p %u on %s\n",
885 ip
.ip_p
, hdr
->pth_ifname
);
886 pktap_hexdump(PKTP_LOG_NOPCB
, &ip
, sizeof(struct ip
));
888 } else if (proto
== PF_INET6
) {
891 struct in6_addr
*faddr
;
892 struct in6_addr
*laddr
;
893 u_short fport
, lport
;
894 struct inpcbinfo
*pcbinfo
= NULL
;
897 error
= mbuf_copydata(m
, pre
, sizeof(struct ip6_hdr
), &ip6
);
901 faddr
= &ip6
.ip6_src
;
902 laddr
= &ip6
.ip6_dst
;
904 if (ip6
.ip6_nxt
== IPPROTO_TCP
) {
907 error
= mbuf_copydata(m
, pre
+ sizeof(struct ip6_hdr
),
908 sizeof(struct tcphdr
), &th
);
910 PKTAP_LOG(PKTP_LOG_ERROR
,
911 "mbuf_copydata tcp v6 failed for %s\n",
920 } else if (ip6
.ip6_nxt
== IPPROTO_UDP
) {
923 error
= mbuf_copydata(m
, pre
+ sizeof(struct ip6_hdr
),
924 sizeof(struct udphdr
), &uh
);
926 PKTAP_LOG(PKTP_LOG_ERROR
,
927 "mbuf_copydata udp v6 failed for %s\n",
938 if (pcbinfo
!= NULL
) {
939 inp
= in6_pcblookup_hash(pcbinfo
, faddr
, fport
,
940 laddr
, lport
, wildcard
, outgoing
? NULL
: ifp
);
942 if (inp
== NULL
&& hdr
->pth_iftype
!= IFT_LOOP
)
943 PKTAP_LOG(PKTP_LOG_NOPCB
,
944 "in6_pcblookup_hash no pcb %s\n",
947 PKTAP_LOG(PKTP_LOG_NOPCB
,
948 "unknown ip6.ip6_nxt %u on %s\n",
949 ip6
.ip6_nxt
, hdr
->pth_ifname
);
950 pktap_hexdump(PKTP_LOG_NOPCB
, &ip6
, sizeof(struct ip6_hdr
));
954 if (inp
->inp_state
!= INPCB_STATE_DEAD
&& inp
->inp_socket
!= NULL
) {
956 inp_get_soprocinfo(inp
, &soprocinfo
);
958 in_pcb_checkstate(inp
, WNT_RELEASE
, 0);
963 * -1 means PID not found
968 pktap_set_procinfo(hdr
, &soprocinfo
);
971 __private_extern__
void
972 pktap_bpf_tap(struct ifnet
*ifp
, protocol_family_t proto
, struct mbuf
*m
,
973 u_int32_t pre
, u_int32_t post
, int outgoing
)
975 struct pktap_softc
*pktap
;
976 void (*bpf_tap_func
)(ifnet_t
, u_int32_t
, mbuf_t
, void *, size_t) =
977 outgoing
? bpf_tap_out
: bpf_tap_in
;
979 lck_rw_lock_shared(pktap_lck_rw
);
982 * No need to take the ifnet_lock as the struct ifnet field if_bpf is
983 * protected by the BPF subsystem
985 LIST_FOREACH(pktap
, &pktap_list
, pktp_link
) {
988 filter_result
= pktap_filter_evaluate(pktap
, ifp
);
989 if (filter_result
== PKTAP_FILTER_SKIP
)
992 if (pktap
->pktp_dlt_raw_count
> 0) {
993 /* We accept only IPv4 and IPv6 packets for the raw DLT */
994 if ((proto
== AF_INET
||proto
== AF_INET6
) &&
995 !(m
->m_pkthdr
.pkt_flags
& PKTF_INET_RESOLVE
)) {
997 * We can play just with the length of the first mbuf in the
998 * chain because bpf_tap_imp() disregard the packet length
999 * of the mbuf packet header.
1001 if (mbuf_setdata(m
, m
->m_data
+ pre
, m
->m_len
- pre
) == 0) {
1002 bpf_tap_func(pktap
->pktp_ifp
, DLT_RAW
, m
, NULL
, 0);
1003 mbuf_setdata(m
, m
->m_data
- pre
, m
->m_len
+ pre
);
1008 if (pktap
->pktp_dlt_pkttap_count
> 0) {
1010 struct pktap_header hdr
;
1013 struct pktap_header
*hdr
= &hdr_buffer
.hdr
;
1014 size_t hdr_size
= sizeof(struct pktap_header
);
1015 int unknown_if_type
= 0;
1016 size_t data_adjust
= 0;
1017 u_int32_t pre_adjust
= 0;
1019 /* Verify the structure is packed */
1020 _CASSERT(sizeof(hdr_buffer
) == sizeof(struct pktap_header
) + sizeof(u_int32_t
));
1022 bzero(&hdr_buffer
, sizeof(hdr_buffer
));
1023 hdr
->pth_length
= sizeof(struct pktap_header
);
1024 hdr
->pth_type_next
= PTH_TYPE_PACKET
;
1027 * Set DLT of packet based on interface type
1029 switch (ifp
->if_type
) {
1035 * Packets from pdp interfaces have no loopback
1036 * header that contain the protocol number.
1037 * As BPF just concatenate the header and the
1038 * packet content in a single buffer,
1039 * stash the protocol after the pktap header
1040 * and adjust the size of the header accordingly
1042 hdr
->pth_dlt
= DLT_NULL
;
1044 hdr_buffer
.proto
= proto
;
1045 hdr_size
= sizeof(hdr_buffer
);
1046 pre_adjust
= sizeof(hdr_buffer
.proto
);
1052 case IFT_IEEE8023ADLAG
:
1053 hdr
->pth_dlt
= DLT_EN10MB
;
1056 hdr
->pth_dlt
= DLT_PPP
;
1059 hdr
->pth_dlt
= DLT_APPLE_IP_OVER_IEEE1394
;
1062 if (strncmp(ifp
->if_name
, "utun", strlen("utun")) == 0) {
1065 * - incoming packets do not have the prefix set to four
1066 * - some packets are as small as two bytes!
1068 if (m_pktlen(m
) < 4)
1070 if (proto
!= AF_INET
&& proto
!= AF_INET6
)
1072 if (proto
== AF_INET
&& (size_t) m_pktlen(m
) - 4 < sizeof(struct ip
))
1074 if (proto
== AF_INET6
&& (size_t) m_pktlen(m
) - 4 < sizeof(struct ip6_hdr
))
1077 * Skip the protocol in the mbuf as it's in network order
1081 hdr
->pth_dlt
= DLT_NULL
;
1082 hdr_buffer
.proto
= proto
;
1083 hdr_size
= sizeof(hdr_buffer
);
1088 hdr
->pth_dlt
= DLT_RAW
;
1090 unknown_if_type
= 1;
1093 if (unknown_if_type
) {
1094 PKTAP_LOG(PKTP_LOG_FUNC
,
1095 "unknown if_type %u for %s\n",
1096 ifp
->if_type
, ifp
->if_xname
);
1097 pktap_count_unknown_if_type
+= 1;
1099 snprintf(hdr
->pth_ifname
, sizeof(hdr
->pth_ifname
), "%s",
1101 hdr
->pth_flags
|= outgoing
? PTH_FLAG_DIR_OUT
: PTH_FLAG_DIR_IN
;
1102 hdr
->pth_protocol_family
= proto
;
1103 hdr
->pth_frame_pre_length
= pre
+ pre_adjust
;
1104 hdr
->pth_frame_post_length
= post
;
1105 hdr
->pth_iftype
= ifp
->if_type
;
1106 hdr
->pth_ifunit
= ifp
->if_unit
;
1108 pktap_fill_proc_info(hdr
, proto
, m
, pre
, outgoing
, ifp
);
1110 hdr
->pth_svc
= so_svc2tc(m
->m_pkthdr
.pkt_svc
);
1112 if (data_adjust
== 0) {
1113 bpf_tap_func(pktap
->pktp_ifp
, DLT_PKTAP
, m
, hdr
, hdr_size
);
1116 * We can play just with the length of the first mbuf in the
1117 * chain because bpf_tap_imp() disregard the packet length
1118 * of the mbuf packet header.
1120 if (mbuf_setdata(m
, m
->m_data
+ data_adjust
, m
->m_len
- data_adjust
) == 0) {
1121 bpf_tap_func(pktap
->pktp_ifp
, DLT_PKTAP
, m
, hdr
, hdr_size
);
1122 mbuf_setdata(m
, m
->m_data
- data_adjust
, m
->m_len
+ data_adjust
);
1129 lck_rw_done(pktap_lck_rw
);
1132 __private_extern__
void
1133 pktap_input(struct ifnet
*ifp
, protocol_family_t proto
, struct mbuf
*m
,
1136 char *hdr
= (char *)mbuf_data(m
);
1137 char *start
= (char *)mbuf_datastart(m
);
1140 if (pktap_total_tap_count
== 0)
1143 /* Make sure the frame header is fully contained in the mbuf */
1144 if (frame_header
!= NULL
&& frame_header
>= start
&& frame_header
<= hdr
) {
1145 size_t o_len
= m
->m_len
;
1146 u_int32_t pre
= hdr
- frame_header
;
1148 if (mbuf_setdata(m
, frame_header
, o_len
+ pre
) == 0) {
1149 PKTAP_LOG(PKTP_LOG_INPUT
, "ifp %s proto %u pre %u post %u\n",
1150 ifp
->if_xname
, proto
, pre
, 0);
1152 pktap_bpf_tap(ifp
, proto
, m
, pre
, 0, 0);
1153 mbuf_setdata(m
, hdr
, o_len
);
1156 PKTAP_LOG(PKTP_LOG_INPUT
, "ifp %s proto %u pre %u post %u\n",
1157 ifp
->if_xname
, proto
, 0, 0);
1159 pktap_bpf_tap(ifp
, proto
, m
, 0, 0, 0);
1163 __private_extern__
void
1164 pktap_output(struct ifnet
*ifp
, protocol_family_t proto
, struct mbuf
*m
,
1165 u_int32_t pre
, u_int32_t post
)
1168 if (pktap_total_tap_count
== 0)
1171 PKTAP_LOG(PKTP_LOG_OUTPUT
, "ifp %s proto %u pre %u post %u\n",
1172 ifp
->if_xname
, proto
, pre
, post
);
1174 pktap_bpf_tap(ifp
, proto
, m
, pre
, post
, 1);