]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/pktap.c
xnu-4570.1.46.tar.gz
[apple/xnu.git] / bsd / net / pktap.c
CommitLineData
39236c6e 1/*
5ba3f43e 2 * Copyright (c) 2012-2017 Apple Inc. All rights reserved.
39236c6e
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <kern/locks.h>
30
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>
fe8ab488 38#include <sys/proc.h>
39236c6e
A
39
40#include <net/if.h>
41#include <net/if_var.h>
42#include <net/if_types.h>
43#include <net/bpf.h>
44#include <net/net_osdep.h>
45#include <net/pktap.h>
46
47#include <netinet/in_pcb.h>
48#include <netinet/tcp.h>
49#include <netinet/tcp_var.h>
fe8ab488 50#define _IP_VHL
39236c6e
A
51#include <netinet/ip.h>
52#include <netinet/ip_var.h>
53#include <netinet/udp.h>
54#include <netinet/udp_var.h>
55
56#include <netinet/ip6.h>
57#include <netinet6/in6_pcb.h>
58
59#include <libkern/OSAtomic.h>
60
61#include <kern/debug.h>
62
63#include <sys/mcache.h>
64
65#include <string.h>
66
67extern struct inpcbinfo ripcbinfo;
68
69struct pktap_softc {
70 LIST_ENTRY(pktap_softc) pktp_link;
71 uint32_t pktp_unit;
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];
76};
77
78#ifndef PKTAP_DEBUG
5ba3f43e 79#define PKTAP_DEBUG 0
39236c6e
A
80#endif /* PKTAP_DEBUG */
81
fe8ab488
A
82#define PKTAP_FILTER_OK 0 /* Packet passes filter checks */
83#define PKTAP_FILTER_SKIP 1 /* Do not tap this packet */
39236c6e
A
84
85static int pktap_inited = 0;
86
87SYSCTL_DECL(_net_link);
fe8ab488
A
88SYSCTL_NODE(_net_link, IFT_PKTAP, pktap,
89 CTLFLAG_RW |CTLFLAG_LOCKED, 0, "pktap virtual interface");
39236c6e 90
5ba3f43e
A
91uint32_t pktap_total_tap_count = 0;
92SYSCTL_UINT(_net_link_pktap, OID_AUTO, total_tap_count,
fe8ab488 93 CTLFLAG_RD | CTLFLAG_LOCKED, &pktap_total_tap_count, 0, "");
39236c6e
A
94
95static u_int64_t pktap_count_unknown_if_type = 0;
fe8ab488
A
96SYSCTL_QUAD(_net_link_pktap, OID_AUTO, count_unknown_if_type,
97 CTLFLAG_RD | CTLFLAG_LOCKED, &pktap_count_unknown_if_type, "");
39236c6e
A
98
99static int pktap_log = 0;
fe8ab488
A
100SYSCTL_INT(_net_link_pktap, OID_AUTO, log,
101 CTLFLAG_RW | CTLFLAG_LOCKED, &pktap_log, 0, "");
39236c6e 102
fe8ab488 103#define PKTAP_LOG(mask, fmt, ...) \
39236c6e 104do { \
fe8ab488
A
105 if ((pktap_log & mask)) \
106 printf("%s:%d " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
107} while (false)
39236c6e 108
fe8ab488
A
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
39236c6e
A
115
116/*
117 * pktap_lck_rw protects the global list of pktap interfaces
118 */
119decl_lck_rw_data(static, pktap_lck_rw_data);
120static lck_rw_t *pktap_lck_rw = &pktap_lck_rw_data;
121static lck_grp_t *pktap_lck_grp = NULL;
122static lck_attr_t *pktap_lck_attr = NULL;
123
fe8ab488
A
124static LIST_HEAD(pktap_list, pktap_softc) pktap_list =
125 LIST_HEAD_INITIALIZER(pktap_list);
39236c6e
A
126
127int pktap_clone_create(struct if_clone *, u_int32_t, void *);
128int pktap_clone_destroy(struct ifnet *);
129
fe8ab488
A
130static struct if_clone pktap_cloner =
131 IF_CLONE_INITIALIZER(PKTAP_IFNAME,
132 pktap_clone_create,
39236c6e 133 pktap_clone_destroy,
fe8ab488 134 0,
39236c6e
A
135 IF_MAXUNIT);
136
137errno_t pktap_if_output(ifnet_t, mbuf_t);
fe8ab488
A
138errno_t pktap_demux(ifnet_t, mbuf_t, char *, protocol_family_t *);
139errno_t pktap_add_proto(ifnet_t, protocol_family_t,
140 const struct ifnet_demux_desc *, u_int32_t);
39236c6e
A
141errno_t pktap_del_proto(ifnet_t, protocol_family_t);
142errno_t pktap_getdrvspec(ifnet_t, struct ifdrv64 *);
143errno_t pktap_setdrvspec(ifnet_t, struct ifdrv64 *);
144errno_t pktap_ioctl(ifnet_t, unsigned long, void *);
145void pktap_detach(ifnet_t);
146int pktap_filter_evaluate(struct pktap_softc *, struct ifnet *);
fe8ab488
A
147void pktap_bpf_tap(struct ifnet *, protocol_family_t, struct mbuf *,
148 u_int32_t, u_int32_t, int);
149errno_t pktap_tap_callback(ifnet_t, u_int32_t, bpf_tap_mode);
39236c6e
A
150
151static void
152pktap_hexdump(int mask, void *addr, size_t len)
153{
154 unsigned char *buf = addr;
155 size_t i;
fe8ab488 156
39236c6e
A
157 if (!(pktap_log & mask))
158 return;
fe8ab488 159
39236c6e
A
160 for (i = 0; i < len; i++) {
161 unsigned char h = (buf[i] & 0xf0) >> 4;
162 unsigned char l = buf[i] & 0x0f;
fe8ab488 163
39236c6e 164 if (i != 0) {
fe8ab488 165 if (i % 32 == 0)
39236c6e 166 printf("\n");
fe8ab488 167 else if (i % 4 == 0)
39236c6e
A
168 printf(" ");
169 }
fe8ab488 170 printf("%c%c",
39236c6e
A
171 h < 10 ? h + '0' : h - 10 + 'a',
172 l < 10 ? l + '0' : l - 10 + 'a');
173 }
174 if (i % 32 != 0)
175 printf("\n");
39236c6e
A
176}
177
178__private_extern__ void
179pktap_init(void)
180{
181 int error = 0;
182 lck_grp_attr_t *lck_grp_attr = NULL;
fe8ab488 183
39236c6e
A
184 /* Make sure we're called only once */
185 VERIFY(pktap_inited == 0);
186
187 pktap_inited = 1;
fe8ab488 188
39236c6e
A
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();
192#if PKTAP_DEBUG
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);
197
198 LIST_INIT(&pktap_list);
fe8ab488 199
39236c6e
A
200 error = if_clone_attach(&pktap_cloner);
201 if (error != 0)
fe8ab488
A
202 panic("%s: if_clone_attach() failed, error %d\n",
203 __func__, error);
39236c6e
A
204}
205
206__private_extern__ int
207pktap_clone_create(struct if_clone *ifc, u_int32_t unit, __unused void *params)
208{
209 int error = 0;
210 struct pktap_softc *pktap = NULL;
5ba3f43e 211 struct ifnet_init_eparams if_init;
39236c6e
A
212
213 PKTAP_LOG(PKTP_LOG_FUNC, "unit %u\n", unit);
fe8ab488
A
214
215 pktap = _MALLOC(sizeof(struct pktap_softc), M_DEVBUF,
216 M_WAITOK | M_ZERO);
39236c6e
A
217 if (pktap == NULL) {
218 printf("%s: _MALLOC failed\n", __func__);
219 error = ENOMEM;
220 goto done;
221 }
222 pktap->pktp_unit = unit;
223
224 /*
225 * By default accept packet from physical interfaces
226 */
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;
230
5ba3f43e
A
231#if CONFIG_EMBEDDED
232 pktap->pktp_filters[1].filter_op = PKTAP_FILTER_OP_PASS;
233 pktap->pktp_filters[1].filter_param = PKTAP_FILTER_PARAM_IF_TYPE;
234 pktap->pktp_filters[1].filter_param_if_type = IFT_CELLULAR;
235#else /* CONFIG_EMBEDDED */
39236c6e
A
236 pktap->pktp_filters[1].filter_op = PKTAP_FILTER_OP_PASS;
237 pktap->pktp_filters[1].filter_param = PKTAP_FILTER_PARAM_IF_TYPE;
238 pktap->pktp_filters[1].filter_param_if_type = IFT_IEEE1394;
5ba3f43e 239#endif /* CONFIG_EMBEDDED */
39037602
A
240
241#if (DEVELOPMENT || DEBUG)
242 pktap->pktp_filters[2].filter_op = PKTAP_FILTER_OP_PASS;
243 pktap->pktp_filters[2].filter_param = PKTAP_FILTER_PARAM_IF_TYPE;
244 pktap->pktp_filters[2].filter_param_if_type = IFT_OTHER;
245#endif /* DEVELOPMENT || DEBUG */
246
39236c6e 247 /*
fe8ab488 248 * We do not use a set_bpf_tap() function as we rather rely on the more
39236c6e
A
249 * accurate callback passed to bpf_attach()
250 */
5ba3f43e
A
251 bzero(&if_init, sizeof(if_init));
252 if_init.ver = IFNET_INIT_CURRENT_VERSION;
253 if_init.len = sizeof (if_init);
254 if_init.flags = IFNET_INIT_LEGACY;
39236c6e
A
255 if_init.name = ifc->ifc_name;
256 if_init.unit = unit;
257 if_init.type = IFT_PKTAP;
258 if_init.family = IFNET_FAMILY_LOOPBACK;
259 if_init.output = pktap_if_output;
260 if_init.demux = pktap_demux;
261 if_init.add_proto = pktap_add_proto;
262 if_init.del_proto = pktap_del_proto;
263 if_init.softc = pktap;
264 if_init.ioctl = pktap_ioctl;
265 if_init.detach = pktap_detach;
266
5ba3f43e 267 error = ifnet_allocate_extended(&if_init, &pktap->pktp_ifp);
39236c6e 268 if (error != 0) {
fe8ab488
A
269 printf("%s: ifnet_allocate failed, error %d\n",
270 __func__, error);
39236c6e
A
271 goto done;
272 }
fe8ab488 273
39236c6e 274 ifnet_set_flags(pktap->pktp_ifp, IFF_UP, IFF_UP);
fe8ab488 275
39236c6e
A
276 error = ifnet_attach(pktap->pktp_ifp, NULL);
277 if (error != 0) {
278 printf("%s: ifnet_attach failed - error %d\n", __func__, error);
279 ifnet_release(pktap->pktp_ifp);
280 goto done;
281 }
fe8ab488 282
39236c6e 283 /* Attach DLT_PKTAP as the default DLT */
fe8ab488
A
284 bpf_attach(pktap->pktp_ifp, DLT_PKTAP, sizeof(struct pktap_header),
285 NULL, pktap_tap_callback);
39236c6e 286 bpf_attach(pktap->pktp_ifp, DLT_RAW, 0, NULL, pktap_tap_callback);
fe8ab488 287
39236c6e
A
288 /* Take a reference and add to the global list */
289 ifnet_reference(pktap->pktp_ifp);
290 lck_rw_lock_exclusive(pktap_lck_rw);
291 LIST_INSERT_HEAD(&pktap_list, pktap, pktp_link);
292 lck_rw_done(pktap_lck_rw);
293done:
294 if (error != 0) {
295 if (pktap != NULL)
296 _FREE(pktap, M_DEVBUF);
297 }
298 return (error);
299}
300
301__private_extern__ int
302pktap_clone_destroy(struct ifnet *ifp)
303{
304 int error = 0;
305
306 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
307
308 (void) ifnet_detach(ifp);
fe8ab488 309
39236c6e
A
310 return (error);
311}
312
313/*
314 * This function is called whenever a DLT is set on the interface:
fe8ab488
A
315 * - When interface is attached to a BPF device via BIOCSETIF for the
316 * default DLT
39236c6e
A
317 * - Whenever a new DLT is selected via BIOCSDLT
318 * - When the interface is detached from a BPF device (direction is zero)
319 */
320__private_extern__ errno_t
321pktap_tap_callback(ifnet_t ifp, u_int32_t dlt, bpf_tap_mode direction)
322{
323 struct pktap_softc *pktap;
324
325 pktap = ifp->if_softc;
326 if (pktap == NULL) {
327 printf("%s: if_softc is NULL for ifp %s\n", __func__,
328 ifp->if_xname);
329 goto done;
330 }
331 switch (dlt) {
332 case DLT_RAW:
333 if (direction == 0) {
334 if (pktap->pktp_dlt_raw_count > 0) {
335 pktap->pktp_dlt_raw_count--;
336 OSAddAtomic(-1, &pktap_total_tap_count);
337
338 }
339 } else {
340 pktap->pktp_dlt_raw_count++;
341 OSAddAtomic(1, &pktap_total_tap_count);
342 }
343 break;
344 case DLT_PKTAP:
345 if (direction == 0) {
346 if (pktap->pktp_dlt_pkttap_count > 0) {
347 pktap->pktp_dlt_pkttap_count--;
348 OSAddAtomic(-1, &pktap_total_tap_count);
349 }
350 } else {
351 pktap->pktp_dlt_pkttap_count++;
352 OSAddAtomic(1, &pktap_total_tap_count);
353 }
354 break;
355 }
356done:
fe8ab488
A
357 /*
358 * Attachements count must be positive and we're in trouble
39236c6e
A
359 * if we have more that 2**31 attachements
360 */
361 VERIFY(pktap_total_tap_count >= 0);
362
363 return (0);
364}
365
366__private_extern__ errno_t
367pktap_if_output(ifnet_t ifp, mbuf_t m)
368{
369 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
370 mbuf_freem(m);
371 return (ENOTSUP);
372}
373
374__private_extern__ errno_t
fe8ab488 375pktap_demux(ifnet_t ifp, __unused mbuf_t m, __unused char *header,
39236c6e
A
376 __unused protocol_family_t *ppf)
377{
378 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
379 return (ENOTSUP);
380}
381
382__private_extern__ errno_t
383pktap_add_proto(__unused ifnet_t ifp, protocol_family_t pf,
384 __unused const struct ifnet_demux_desc *dmx, __unused u_int32_t cnt)
385{
386 PKTAP_LOG(PKTP_LOG_FUNC, "%s pf %u\n", ifp->if_xname, pf);
387 return (0);
388}
389
390__private_extern__ errno_t
391pktap_del_proto(__unused ifnet_t ifp, __unused protocol_family_t pf)
392{
393 PKTAP_LOG(PKTP_LOG_FUNC, "%s pf %u\n", ifp->if_xname, pf);
394 return (0);
395}
396
397__private_extern__ errno_t
398pktap_getdrvspec(ifnet_t ifp, struct ifdrv64 *ifd)
399{
400 errno_t error = 0;
401 struct pktap_softc *pktap;
402 int i;
403
404 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
fe8ab488 405
39236c6e
A
406 pktap = ifp->if_softc;
407 if (pktap == NULL) {
408 error = ENOENT;
409 printf("%s: pktap NULL - error %d\n", __func__, error);
410 goto done;
411 }
412
413 switch (ifd->ifd_cmd) {
414 case PKTP_CMD_FILTER_GET: {
415 struct x_pktap_filter x_filters[PKTAP_MAX_FILTERS];
fe8ab488 416
39236c6e
A
417 bzero(&x_filters, sizeof(x_filters));
418
419 if (ifd->ifd_len < PKTAP_MAX_FILTERS * sizeof(struct x_pktap_filter)) {
fe8ab488 420 printf("%s: PKTP_CMD_FILTER_GET ifd_len %llu too small - error %d\n",
39236c6e
A
421 __func__, ifd->ifd_len, error);
422 error = EINVAL;
423 break;
424 }
425 for (i = 0; i < PKTAP_MAX_FILTERS; i++) {
426 struct pktap_filter *pktap_filter = pktap->pktp_filters + i;
427 struct x_pktap_filter *x_filter = x_filters + i;
fe8ab488 428
39236c6e
A
429 x_filter->filter_op = pktap_filter->filter_op;
430 x_filter->filter_param = pktap_filter->filter_param;
fe8ab488 431
39236c6e
A
432 if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_TYPE)
433 x_filter->filter_param_if_type = pktap_filter->filter_param_if_type;
434 else if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_NAME)
435 strlcpy(x_filter->filter_param_if_name,
436 pktap_filter->filter_param_if_name,
437 sizeof(x_filter->filter_param_if_name));
438 }
fe8ab488 439 error = copyout(x_filters, ifd->ifd_data,
39236c6e
A
440 PKTAP_MAX_FILTERS * sizeof(struct x_pktap_filter));
441 if (error) {
442 printf("%s: PKTP_CMD_FILTER_GET copyout - error %d\n", __func__, error);
443 goto done;
444 }
445 break;
446 }
447 case PKTP_CMD_TAP_COUNT: {
448 uint32_t tap_count = pktap->pktp_dlt_raw_count + pktap->pktp_dlt_pkttap_count;
fe8ab488 449
39236c6e 450 if (ifd->ifd_len < sizeof(tap_count)) {
fe8ab488 451 printf("%s: PKTP_CMD_TAP_COUNT ifd_len %llu too small - error %d\n",
39236c6e
A
452 __func__, ifd->ifd_len, error);
453 error = EINVAL;
454 break;
455 }
456 error = copyout(&tap_count, ifd->ifd_data, sizeof(tap_count));
457 if (error) {
458 printf("%s: PKTP_CMD_TAP_COUNT copyout - error %d\n", __func__, error);
459 goto done;
460 }
461 break;
462 }
463 default:
464 error = EINVAL;
465 break;
466 }
467
468done:
469 return (error);
470}
471
472__private_extern__ errno_t
473pktap_setdrvspec(ifnet_t ifp, struct ifdrv64 *ifd)
474{
475 errno_t error = 0;
476 struct pktap_softc *pktap;
477
478 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
fe8ab488 479
39236c6e
A
480 pktap = ifp->if_softc;
481 if (pktap == NULL) {
482 error = ENOENT;
483 printf("%s: pktap NULL - error %d\n", __func__, error);
484 goto done;
485 }
486
487 switch (ifd->ifd_cmd) {
488 case PKTP_CMD_FILTER_SET: {
489 struct x_pktap_filter user_filters[PKTAP_MAX_FILTERS];
490 int i;
491 int got_op_none = 0;
fe8ab488 492
39236c6e 493 if (ifd->ifd_len != PKTAP_MAX_FILTERS * sizeof(struct x_pktap_filter)) {
fe8ab488 494 printf("%s: PKTP_CMD_FILTER_SET bad ifd_len %llu - error %d\n",
39236c6e
A
495 __func__, ifd->ifd_len, error);
496 error = EINVAL;
497 break;
498 }
499 error = copyin(ifd->ifd_data, &user_filters, ifd->ifd_len);
500 if (error) {
501 printf("%s: copyin - error %d\n", __func__, error);
502 goto done;
503 }
504 /*
505 * Validate user provided parameters
506 */
507 for (i = 0; i < PKTAP_MAX_FILTERS; i++) {
508 struct x_pktap_filter *x_filter = user_filters + i;
fe8ab488 509
39236c6e
A
510 switch (x_filter->filter_op) {
511 case PKTAP_FILTER_OP_NONE:
512 /* Following entries must be PKTAP_FILTER_OP_NONE */
513 got_op_none = 1;
514 break;
515 case PKTAP_FILTER_OP_PASS:
516 case PKTAP_FILTER_OP_SKIP:
517 /* Invalid after PKTAP_FILTER_OP_NONE */
518 if (got_op_none) {
519 error = EINVAL;
520 break;
521 }
522 break;
523 default:
524 error = EINVAL;
525 break;
526 }
527 if (error != 0)
528 break;
fe8ab488 529
39236c6e
A
530 switch (x_filter->filter_param) {
531 case PKTAP_FILTER_OP_NONE:
532 if (x_filter->filter_op != PKTAP_FILTER_OP_NONE) {
533 error = EINVAL;
534 break;
535 }
536 break;
fe8ab488 537
39236c6e
A
538 /*
539 * Do not allow to tap a pktap from a pktap
540 */
541 case PKTAP_FILTER_PARAM_IF_TYPE:
542 if (x_filter->filter_param_if_type == IFT_PKTAP ||
543 x_filter->filter_param_if_type > 0xff) {
544 error = EINVAL;
fe8ab488 545 break;
39236c6e
A
546 }
547 break;
548
549 case PKTAP_FILTER_PARAM_IF_NAME:
3e170ce0 550 if (strncmp(x_filter->filter_param_if_name, PKTAP_IFNAME,
39236c6e
A
551 strlen(PKTAP_IFNAME)) == 0) {
552 error = EINVAL;
fe8ab488 553 break;
39236c6e
A
554 }
555 break;
556
557 default:
558 error = EINVAL;
559 break;
fe8ab488 560 }
39236c6e
A
561 if (error != 0)
562 break;
563 }
564 if (error != 0)
565 break;
566 for (i = 0; i < PKTAP_MAX_FILTERS; i++) {
567 struct pktap_filter *pktap_filter = pktap->pktp_filters + i;
568 struct x_pktap_filter *x_filter = user_filters + i;
fe8ab488 569
39236c6e
A
570 pktap_filter->filter_op = x_filter->filter_op;
571 pktap_filter->filter_param = x_filter->filter_param;
fe8ab488 572
39236c6e
A
573 if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_TYPE)
574 pktap_filter->filter_param_if_type = x_filter->filter_param_if_type;
575 else if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_NAME) {
576 size_t len;
fe8ab488 577
39236c6e
A
578 strlcpy(pktap_filter->filter_param_if_name,
579 x_filter->filter_param_if_name,
580 sizeof(pktap_filter->filter_param_if_name));
581 /*
fe8ab488 582 * If name does not end with a number then it's a "wildcard" match
39236c6e
A
583 * where we compare the prefix of the interface name
584 */
585 len = strlen(pktap_filter->filter_param_if_name);
586 if (pktap_filter->filter_param_if_name[len] < '0' ||
587 pktap_filter->filter_param_if_name[len] > '9')
588 pktap_filter->filter_ifname_prefix_len = len;
589 }
590 }
591 break;
592 }
593 default:
594 error = EINVAL;
595 break;
596 }
597
598done:
599 return (error);
600}
601
602__private_extern__ errno_t
603pktap_ioctl(ifnet_t ifp, unsigned long cmd, void *data)
604{
605 errno_t error = 0;
606
607 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
608
609 if ((cmd & IOC_IN)) {
610 error = kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER);
611 if (error) {
fe8ab488
A
612 PKTAP_LOG(PKTP_LOG_ERROR,
613 "%s: kauth_authorize_generic(KAUTH_GENERIC_ISSUSER) - error %d\n",
39236c6e
A
614 __func__, error);
615 goto done;
616 }
617 }
fe8ab488 618
39236c6e
A
619 switch (cmd) {
620 case SIOCGDRVSPEC32: {
621 struct ifdrv64 ifd;
622 struct ifdrv32 *ifd32 = (struct ifdrv32 *)data;
fe8ab488 623
39236c6e
A
624 memcpy(ifd.ifd_name, ifd32->ifd_name, sizeof(ifd.ifd_name));
625 ifd.ifd_cmd = ifd32->ifd_cmd;
626 ifd.ifd_len = ifd32->ifd_len;
627 ifd.ifd_data = ifd32->ifd_data;
fe8ab488 628
39236c6e 629 error = pktap_getdrvspec(ifp, &ifd);
fe8ab488 630
39236c6e
A
631 break;
632 }
633 case SIOCGDRVSPEC64: {
634 struct ifdrv64 *ifd64 = (struct ifdrv64 *)data;
fe8ab488 635
39236c6e
A
636 error = pktap_getdrvspec(ifp, ifd64);
637
638 break;
639 }
640 case SIOCSDRVSPEC32: {
641 struct ifdrv64 ifd;
642 struct ifdrv32 *ifd32 = (struct ifdrv32 *)data;
643
644 memcpy(ifd.ifd_name, ifd32->ifd_name, sizeof(ifd.ifd_name));
645 ifd.ifd_cmd = ifd32->ifd_cmd;
646 ifd.ifd_len = ifd32->ifd_len;
647 ifd.ifd_data = ifd32->ifd_data;
648
649 error = pktap_setdrvspec(ifp, &ifd);
650 break;
651 }
652 case SIOCSDRVSPEC64: {
653 struct ifdrv64 *ifd64 = (struct ifdrv64 *)data;
654
655 error = pktap_setdrvspec(ifp, ifd64);
656
657 break;
658 }
659 default:
660 error = ENOTSUP;
661 break;
662 }
663done:
664 return (error);
665}
666
667__private_extern__ void
668pktap_detach(ifnet_t ifp)
669{
670 struct pktap_softc *pktap;
671
672 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
fe8ab488 673
39236c6e
A
674 lck_rw_lock_exclusive(pktap_lck_rw);
675
676 pktap = ifp->if_softc;
677 ifp->if_softc = NULL;
678 LIST_REMOVE(pktap, pktp_link);
679
680 lck_rw_done(pktap_lck_rw);
681
682 /* Drop reference as it's no more on the global list */
683 ifnet_release(ifp);
fe8ab488 684
39236c6e
A
685 _FREE(pktap, M_DEVBUF);
686
687 /* This is for the reference taken by ifnet_attach() */
688 (void) ifnet_release(ifp);
689}
690
691__private_extern__ int
692pktap_filter_evaluate(struct pktap_softc *pktap, struct ifnet *ifp)
693{
694 int i;
695 int result = PKTAP_FILTER_SKIP; /* Need positive matching rule to pass */
696 int match = 0;
fe8ab488 697
39236c6e
A
698 for (i = 0; i < PKTAP_MAX_FILTERS; i++) {
699 struct pktap_filter *pktap_filter = pktap->pktp_filters + i;
700 size_t len = pktap_filter->filter_ifname_prefix_len != 0 ?
701 pktap_filter->filter_ifname_prefix_len : PKTAP_IFXNAMESIZE;
fe8ab488 702
39236c6e
A
703 switch (pktap_filter->filter_op) {
704 case PKTAP_FILTER_OP_NONE:
705 match = 1;
706 break;
707
708 case PKTAP_FILTER_OP_PASS:
709 if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_TYPE) {
710 if (pktap_filter->filter_param_if_type == 0 ||
711 ifp->if_type == pktap_filter->filter_param_if_type) {
712 result = PKTAP_FILTER_OK;
713 match = 1;
fe8ab488 714 PKTAP_LOG(PKTP_LOG_FILTER, "pass %s match type %u\n",
39236c6e
A
715 ifp->if_xname, pktap_filter->filter_param_if_type);
716 break;
717 }
718 }
719 if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_NAME) {
720 if (strncmp(ifp->if_xname, pktap_filter->filter_param_if_name,
721 len) == 0) {
722 result = PKTAP_FILTER_OK;
723 match = 1;
fe8ab488 724 PKTAP_LOG(PKTP_LOG_FILTER, "pass %s match name %s\n",
39236c6e
A
725 ifp->if_xname, pktap_filter->filter_param_if_name);
726 break;
727 }
728 }
729 break;
730
731 case PKTAP_FILTER_OP_SKIP:
732 if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_TYPE) {
733 if (pktap_filter->filter_param_if_type == 0 ||
734 ifp->if_type == pktap_filter->filter_param_if_type) {
735 result = PKTAP_FILTER_SKIP;
736 match = 1;
fe8ab488 737 PKTAP_LOG(PKTP_LOG_FILTER, "skip %s match type %u\n",
39236c6e
A
738 ifp->if_xname, pktap_filter->filter_param_if_type);
739 break;
740 }
741 }
742 if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_NAME) {
fe8ab488 743 if (strncmp(ifp->if_xname, pktap_filter->filter_param_if_name,
39236c6e
A
744 len) == 0) {
745 result = PKTAP_FILTER_SKIP;
746 match = 1;
fe8ab488 747 PKTAP_LOG(PKTP_LOG_FILTER, "skip %s match name %s\n",
39236c6e
A
748 ifp->if_xname, pktap_filter->filter_param_if_name);
749 break;
750 }
751 }
752 break;
753 }
754 if (match)
755 break;
756 }
757
758 if (match == 0) {
fe8ab488 759 PKTAP_LOG(PKTP_LOG_FILTER, "%s no match\n",
39236c6e
A
760 ifp->if_xname);
761 }
762 return (result);
763}
764
fe8ab488
A
765static void
766pktap_set_procinfo(struct pktap_header *hdr, struct so_procinfo *soprocinfo)
767{
768 hdr->pth_pid = soprocinfo->spi_pid;
769 proc_name(soprocinfo->spi_pid, hdr->pth_comm, MAXCOMLEN);
770 if (soprocinfo->spi_pid != 0)
771 uuid_copy(hdr->pth_uuid, soprocinfo->spi_uuid);
772
773 /*
774 * When not delegated, the effective pid is the same as the real pid
775 */
3e170ce0 776 if (soprocinfo->spi_delegated != 0) {
fe8ab488
A
777 hdr->pth_flags |= PTH_FLAG_PROC_DELEGATED;
778 hdr->pth_epid = soprocinfo->spi_epid;
779 proc_name(soprocinfo->spi_epid, hdr->pth_ecomm, MAXCOMLEN);
3e170ce0 780 uuid_copy(hdr->pth_euuid, soprocinfo->spi_euuid);
fe8ab488
A
781 }
782}
783
39236c6e 784__private_extern__ void
fe8ab488
A
785pktap_finalize_proc_info(struct pktap_header *hdr)
786{
787 int found;
788 struct so_procinfo soprocinfo;
789
790 if (!(hdr->pth_flags & PTH_FLAG_DELAY_PKTAP))
791 return;
792
793 /*
794 * Clear the flag as it's internal
795 */
796 hdr->pth_flags &= ~PTH_FLAG_DELAY_PKTAP;
797
798 if (hdr->pth_ipproto == IPPROTO_TCP)
799 found = inp_findinpcb_procinfo(&tcbinfo, hdr->pth_flowid,
800 &soprocinfo);
801 else if (hdr->pth_ipproto == IPPROTO_UDP)
802 found = inp_findinpcb_procinfo(&udbinfo, hdr->pth_flowid,
803 &soprocinfo);
804 else
805 found = inp_findinpcb_procinfo(&ripcbinfo, hdr->pth_flowid,
806 &soprocinfo);
807
808 if (found == 1)
809 pktap_set_procinfo(hdr, &soprocinfo);
810}
811
812__private_extern__ void
813pktap_fill_proc_info(struct pktap_header *hdr, protocol_family_t proto,
39236c6e
A
814 struct mbuf *m, u_int32_t pre, int outgoing, struct ifnet *ifp)
815{
816 int found = 0;
817 struct so_procinfo soprocinfo;
fe8ab488 818
39236c6e
A
819 /*
820 * Getting the pid and procname is expensive
fe8ab488 821 * For outgoing, do the lookup only if there's an
39236c6e
A
822 * associated socket as indicated by the flowhash
823 */
3e170ce0 824 if (outgoing != 0 && m->m_pkthdr.pkt_flowsrc == FLOWSRC_INPCB) {
fe8ab488
A
825 /*
826 * To avoid lock ordering issues we delay the process lookup
827 * to the BPF read as we cannot
828 * assume the socket lock is unlocked on output
829 */
3e170ce0
A
830 found = 0;
831 hdr->pth_flags |= PTH_FLAG_DELAY_PKTAP;
832 hdr->pth_flowid = m->m_pkthdr.pkt_flowid;
833 if (m->m_pkthdr.pkt_flags & PKTF_FLOW_RAWSOCK)
834 hdr->pth_ipproto = IPPROTO_RAW;
835 else
836 hdr->pth_ipproto = m->m_pkthdr.pkt_proto;
39037602
A
837 if (m->m_pkthdr.pkt_flags & PKTF_NEW_FLOW)
838 hdr->pth_flags |= PTH_FLAG_NEW_FLOW;
39236c6e
A
839 } else if (outgoing == 0) {
840 struct inpcb *inp = NULL;
fe8ab488 841
39236c6e
A
842 if (proto == PF_INET) {
843 struct ip ip;
844 errno_t error;
845 size_t hlen;
846 struct in_addr faddr, laddr;
5ba3f43e 847 u_short fport = 0, lport = 0;
39236c6e
A
848 struct inpcbinfo *pcbinfo = NULL;
849 int wildcard = 0;
fe8ab488 850
39236c6e
A
851 error = mbuf_copydata(m, pre, sizeof(struct ip), &ip);
852 if (error != 0) {
fe8ab488
A
853 PKTAP_LOG(PKTP_LOG_ERROR,
854 "mbuf_copydata tcp v4 failed for %s\n",
855 hdr->pth_ifname);
39236c6e
A
856 goto done;
857 }
858 hlen = IP_VHL_HL(ip.ip_vhl) << 2;
fe8ab488 859
39236c6e
A
860 faddr = ip.ip_src;
861 laddr = ip.ip_dst;
862
863 if (ip.ip_p == IPPROTO_TCP) {
864 struct tcphdr th;
fe8ab488
A
865
866 error = mbuf_copydata(m, pre + hlen,
39236c6e
A
867 sizeof(struct tcphdr), &th);
868 if (error != 0)
869 goto done;
fe8ab488 870
39236c6e
A
871 fport = th.th_sport;
872 lport = th.th_dport;
873
874 pcbinfo = &tcbinfo;
875 } else if (ip.ip_p == IPPROTO_UDP) {
876 struct udphdr uh;
fe8ab488
A
877
878 error = mbuf_copydata(m, pre + hlen,
39236c6e
A
879 sizeof(struct udphdr), &uh);
880 if (error != 0) {
fe8ab488
A
881 PKTAP_LOG(PKTP_LOG_ERROR,
882 "mbuf_copydata udp v4 failed for %s\n",
883 hdr->pth_ifname);
39236c6e
A
884 goto done;
885 }
886 fport = uh.uh_sport;
887 lport = uh.uh_dport;
fe8ab488 888
39236c6e
A
889 pcbinfo = &udbinfo;
890 wildcard = 1;
891 }
892 if (pcbinfo != NULL) {
893 inp = in_pcblookup_hash(pcbinfo, faddr, fport,
894 laddr, lport, wildcard, outgoing ? NULL : ifp);
fe8ab488 895
39236c6e 896 if (inp == NULL && hdr->pth_iftype != IFT_LOOP)
fe8ab488
A
897 PKTAP_LOG(PKTP_LOG_NOPCB,
898 "in_pcblookup_hash no pcb %s\n",
899 hdr->pth_ifname);
39236c6e 900 } else {
fe8ab488
A
901 PKTAP_LOG(PKTP_LOG_NOPCB,
902 "unknown ip_p %u on %s\n",
903 ip.ip_p, hdr->pth_ifname);
39236c6e
A
904 pktap_hexdump(PKTP_LOG_NOPCB, &ip, sizeof(struct ip));
905 }
906 } else if (proto == PF_INET6) {
907 struct ip6_hdr ip6;
908 errno_t error;
909 struct in6_addr *faddr;
910 struct in6_addr *laddr;
5ba3f43e 911 u_short fport = 0, lport = 0;
39236c6e
A
912 struct inpcbinfo *pcbinfo = NULL;
913 int wildcard = 0;
fe8ab488 914
39236c6e
A
915 error = mbuf_copydata(m, pre, sizeof(struct ip6_hdr), &ip6);
916 if (error != 0)
917 goto done;
fe8ab488 918
39236c6e
A
919 faddr = &ip6.ip6_src;
920 laddr = &ip6.ip6_dst;
fe8ab488 921
39236c6e
A
922 if (ip6.ip6_nxt == IPPROTO_TCP) {
923 struct tcphdr th;
fe8ab488
A
924
925 error = mbuf_copydata(m, pre + sizeof(struct ip6_hdr),
39236c6e
A
926 sizeof(struct tcphdr), &th);
927 if (error != 0) {
fe8ab488
A
928 PKTAP_LOG(PKTP_LOG_ERROR,
929 "mbuf_copydata tcp v6 failed for %s\n",
930 hdr->pth_ifname);
39236c6e
A
931 goto done;
932 }
fe8ab488 933
39236c6e
A
934 fport = th.th_sport;
935 lport = th.th_dport;
fe8ab488 936
39236c6e
A
937 pcbinfo = &tcbinfo;
938 } else if (ip6.ip6_nxt == IPPROTO_UDP) {
939 struct udphdr uh;
fe8ab488
A
940
941 error = mbuf_copydata(m, pre + sizeof(struct ip6_hdr),
39236c6e
A
942 sizeof(struct udphdr), &uh);
943 if (error != 0) {
fe8ab488
A
944 PKTAP_LOG(PKTP_LOG_ERROR,
945 "mbuf_copydata udp v6 failed for %s\n",
946 hdr->pth_ifname);
39236c6e
A
947 goto done;
948 }
fe8ab488 949
39236c6e
A
950 fport = uh.uh_sport;
951 lport = uh.uh_dport;
fe8ab488 952
39236c6e
A
953 pcbinfo = &udbinfo;
954 wildcard = 1;
955 }
956 if (pcbinfo != NULL) {
957 inp = in6_pcblookup_hash(pcbinfo, faddr, fport,
958 laddr, lport, wildcard, outgoing ? NULL : ifp);
fe8ab488 959
39236c6e 960 if (inp == NULL && hdr->pth_iftype != IFT_LOOP)
fe8ab488
A
961 PKTAP_LOG(PKTP_LOG_NOPCB,
962 "in6_pcblookup_hash no pcb %s\n",
963 hdr->pth_ifname);
39236c6e 964 } else {
fe8ab488
A
965 PKTAP_LOG(PKTP_LOG_NOPCB,
966 "unknown ip6.ip6_nxt %u on %s\n",
967 ip6.ip6_nxt, hdr->pth_ifname);
39236c6e
A
968 pktap_hexdump(PKTP_LOG_NOPCB, &ip6, sizeof(struct ip6_hdr));
969 }
970 }
971 if (inp != NULL) {
972 if (inp->inp_state != INPCB_STATE_DEAD && inp->inp_socket != NULL) {
973 found = 1;
974 inp_get_soprocinfo(inp, &soprocinfo);
975 }
976 in_pcb_checkstate(inp, WNT_RELEASE, 0);
977 }
978 }
fe8ab488 979done:
39236c6e
A
980 /*
981 * -1 means PID not found
982 */
983 hdr->pth_pid = -1;
984 hdr->pth_epid = -1;
fe8ab488
A
985 if (found != 0)
986 pktap_set_procinfo(hdr, &soprocinfo);
39236c6e
A
987}
988
989__private_extern__ void
990pktap_bpf_tap(struct ifnet *ifp, protocol_family_t proto, struct mbuf *m,
991 u_int32_t pre, u_int32_t post, int outgoing)
992{
993 struct pktap_softc *pktap;
fe8ab488 994 void (*bpf_tap_func)(ifnet_t, u_int32_t, mbuf_t, void *, size_t) =
39236c6e
A
995 outgoing ? bpf_tap_out : bpf_tap_in;
996
997 lck_rw_lock_shared(pktap_lck_rw);
998
999 /*
fe8ab488 1000 * No need to take the ifnet_lock as the struct ifnet field if_bpf is
39236c6e
A
1001 * protected by the BPF subsystem
1002 */
1003 LIST_FOREACH(pktap, &pktap_list, pktp_link) {
1004 int filter_result;
fe8ab488 1005
39236c6e
A
1006 filter_result = pktap_filter_evaluate(pktap, ifp);
1007 if (filter_result == PKTAP_FILTER_SKIP)
1008 continue;
1009
1010 if (pktap->pktp_dlt_raw_count > 0) {
1011 /* We accept only IPv4 and IPv6 packets for the raw DLT */
1012 if ((proto == AF_INET ||proto == AF_INET6) &&
1013 !(m->m_pkthdr.pkt_flags & PKTF_INET_RESOLVE)) {
1014 /*
fe8ab488 1015 * We can play just with the length of the first mbuf in the
39236c6e
A
1016 * chain because bpf_tap_imp() disregard the packet length
1017 * of the mbuf packet header.
1018 */
1019 if (mbuf_setdata(m, m->m_data + pre, m->m_len - pre) == 0) {
1020 bpf_tap_func(pktap->pktp_ifp, DLT_RAW, m, NULL, 0);
1021 mbuf_setdata(m, m->m_data - pre, m->m_len + pre);
1022 }
1023 }
1024 }
fe8ab488 1025
39236c6e
A
1026 if (pktap->pktp_dlt_pkttap_count > 0) {
1027 struct {
1028 struct pktap_header hdr;
1029 u_int32_t proto;
1030 } hdr_buffer;
1031 struct pktap_header *hdr = &hdr_buffer.hdr;
1032 size_t hdr_size = sizeof(struct pktap_header);
1033 int unknown_if_type = 0;
1034 size_t data_adjust = 0;
1035 u_int32_t pre_adjust = 0;
fe8ab488
A
1036
1037 /* Verify the structure is packed */
39236c6e 1038 _CASSERT(sizeof(hdr_buffer) == sizeof(struct pktap_header) + sizeof(u_int32_t));
fe8ab488 1039
39236c6e
A
1040 bzero(&hdr_buffer, sizeof(hdr_buffer));
1041 hdr->pth_length = sizeof(struct pktap_header);
1042 hdr->pth_type_next = PTH_TYPE_PACKET;
1043
1044 /*
1045 * Set DLT of packet based on interface type
1046 */
1047 switch (ifp->if_type) {
1048 case IFT_LOOP:
1049 case IFT_GIF:
1050 case IFT_STF:
1051 case IFT_CELLULAR:
1052 /*
fe8ab488 1053 * Packets from pdp interfaces have no loopback
39236c6e 1054 * header that contain the protocol number.
fe8ab488 1055 * As BPF just concatenate the header and the
39236c6e
A
1056 * packet content in a single buffer,
1057 * stash the protocol after the pktap header
1058 * and adjust the size of the header accordingly
1059 */
1060 hdr->pth_dlt = DLT_NULL;
1061 if (pre == 0) {
1062 hdr_buffer.proto = proto;
1063 hdr_size = sizeof(hdr_buffer);
1064 pre_adjust = sizeof(hdr_buffer.proto);
1065 }
1066 break;
1067 case IFT_ETHER:
1068 case IFT_BRIDGE:
1069 case IFT_L2VLAN:
1070 case IFT_IEEE8023ADLAG:
1071 hdr->pth_dlt = DLT_EN10MB;
1072 break;
1073 case IFT_PPP:
1074 hdr->pth_dlt = DLT_PPP;
1075 break;
1076 case IFT_IEEE1394:
1077 hdr->pth_dlt = DLT_APPLE_IP_OVER_IEEE1394;
1078 break;
1079 case IFT_OTHER:
1080 if (strncmp(ifp->if_name, "utun", strlen("utun")) == 0) {
1081 /*
1082 * For utun:
1083 * - incoming packets do not have the prefix set to four
1084 * - some packets are as small as two bytes!
1085 */
1086 if (m_pktlen(m) < 4)
1087 goto done;
1088 if (proto != AF_INET && proto != AF_INET6)
1089 goto done;
1090 if (proto == AF_INET && (size_t) m_pktlen(m) - 4 < sizeof(struct ip))
1091 goto done;
1092 if (proto == AF_INET6 && (size_t) m_pktlen(m) - 4 < sizeof(struct ip6_hdr))
1093 goto done;
5ba3f43e 1094
39236c6e 1095 /*
5ba3f43e
A
1096 * Handle two cases:
1097 * - The old utun encapsulation with the protocol family in network order
1098 * - A raw IPv4 or IPv6 packet
39236c6e 1099 */
5ba3f43e
A
1100 uint8_t data = *(uint8_t *)mbuf_data(m);
1101 if ((data >> 4) == 4 || (data >> 4) == 6) {
1102 pre = 4;
1103 } else {
1104 /*
1105 * Skip the protocol in the mbuf as it's in network order
1106 */
1107 pre = 4;
1108 data_adjust = 4;
1109 }
39236c6e 1110 }
39037602
A
1111 hdr->pth_dlt = DLT_NULL;
1112 hdr_buffer.proto = proto;
1113 hdr_size = sizeof(hdr_buffer);
1114 break;
39236c6e
A
1115 default:
1116 if (pre == 0)
1117 hdr->pth_dlt = DLT_RAW;
1118 else
1119 unknown_if_type = 1;
1120 break;
1121 }
1122 if (unknown_if_type) {
fe8ab488
A
1123 PKTAP_LOG(PKTP_LOG_FUNC,
1124 "unknown if_type %u for %s\n",
1125 ifp->if_type, ifp->if_xname);
39236c6e
A
1126 pktap_count_unknown_if_type += 1;
1127 } else {
5ba3f43e
A
1128 strlcpy(hdr->pth_ifname, ifp->if_xname,
1129 sizeof(hdr->pth_ifname));
39236c6e
A
1130 hdr->pth_flags |= outgoing ? PTH_FLAG_DIR_OUT : PTH_FLAG_DIR_IN;
1131 hdr->pth_protocol_family = proto;
1132 hdr->pth_frame_pre_length = pre + pre_adjust;
1133 hdr->pth_frame_post_length = post;
1134 hdr->pth_iftype = ifp->if_type;
1135 hdr->pth_ifunit = ifp->if_unit;
fe8ab488 1136
39236c6e 1137 pktap_fill_proc_info(hdr, proto, m, pre, outgoing, ifp);
fe8ab488 1138
39236c6e 1139 hdr->pth_svc = so_svc2tc(m->m_pkthdr.pkt_svc);
fe8ab488 1140
39236c6e
A
1141 if (data_adjust == 0) {
1142 bpf_tap_func(pktap->pktp_ifp, DLT_PKTAP, m, hdr, hdr_size);
1143 } else {
1144 /*
fe8ab488 1145 * We can play just with the length of the first mbuf in the
39236c6e
A
1146 * chain because bpf_tap_imp() disregard the packet length
1147 * of the mbuf packet header.
1148 */
1149 if (mbuf_setdata(m, m->m_data + data_adjust, m->m_len - data_adjust) == 0) {
1150 bpf_tap_func(pktap->pktp_ifp, DLT_PKTAP, m, hdr, hdr_size);
1151 mbuf_setdata(m, m->m_data - data_adjust, m->m_len + data_adjust);
1152 }
1153 }
1154 }
1155 }
1156 }
1157done:
1158 lck_rw_done(pktap_lck_rw);
1159}
1160
1161__private_extern__ void
1162pktap_input(struct ifnet *ifp, protocol_family_t proto, struct mbuf *m,
1163 char *frame_header)
1164{
5ba3f43e
A
1165 char *hdr;
1166 char *start;
39236c6e
A
1167
1168 /* Fast path */
1169 if (pktap_total_tap_count == 0)
1170 return;
1171
5ba3f43e
A
1172 hdr = (char *)mbuf_data(m);
1173 start = (char *)mbuf_datastart(m);
39236c6e
A
1174 /* Make sure the frame header is fully contained in the mbuf */
1175 if (frame_header != NULL && frame_header >= start && frame_header <= hdr) {
1176 size_t o_len = m->m_len;
1177 u_int32_t pre = hdr - frame_header;
fe8ab488 1178
39236c6e 1179 if (mbuf_setdata(m, frame_header, o_len + pre) == 0) {
fe8ab488 1180 PKTAP_LOG(PKTP_LOG_INPUT, "ifp %s proto %u pre %u post %u\n",
39236c6e
A
1181 ifp->if_xname, proto, pre, 0);
1182
1183 pktap_bpf_tap(ifp, proto, m, pre, 0, 0);
1184 mbuf_setdata(m, hdr, o_len);
1185 }
1186 } else {
fe8ab488 1187 PKTAP_LOG(PKTP_LOG_INPUT, "ifp %s proto %u pre %u post %u\n",
39236c6e
A
1188 ifp->if_xname, proto, 0, 0);
1189
1190 pktap_bpf_tap(ifp, proto, m, 0, 0, 0);
1191 }
1192}
1193
1194__private_extern__ void
1195pktap_output(struct ifnet *ifp, protocol_family_t proto, struct mbuf *m,
fe8ab488 1196 u_int32_t pre, u_int32_t post)
39236c6e
A
1197{
1198 /* Fast path */
1199 if (pktap_total_tap_count == 0)
1200 return;
1201
fe8ab488 1202 PKTAP_LOG(PKTP_LOG_OUTPUT, "ifp %s proto %u pre %u post %u\n",
39236c6e
A
1203 ifp->if_xname, proto, pre, post);
1204
1205 pktap_bpf_tap(ifp, proto, m, pre, post, 1);
1206}
5ba3f43e 1207