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