]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/pktap.c
xnu-3248.40.184.tar.gz
[apple/xnu.git] / bsd / net / pktap.c
1 /*
2 * Copyright (c) 2012-2014 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
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>
38 #include <sys/proc.h>
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>
50 #define _IP_VHL
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
67 extern struct inpcbinfo ripcbinfo;
68
69 struct 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
79 #define PKTAP_DEBUG 1
80 #endif /* PKTAP_DEBUG */
81
82 #define PKTAP_FILTER_OK 0 /* Packet passes filter checks */
83 #define PKTAP_FILTER_SKIP 1 /* Do not tap this packet */
84
85 static int pktap_inited = 0;
86
87 SYSCTL_DECL(_net_link);
88 SYSCTL_NODE(_net_link, IFT_PKTAP, pktap,
89 CTLFLAG_RW |CTLFLAG_LOCKED, 0, "pktap virtual interface");
90
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, "");
94
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, "");
98
99 static int pktap_log = 0;
100 SYSCTL_INT(_net_link_pktap, OID_AUTO, log,
101 CTLFLAG_RW | CTLFLAG_LOCKED, &pktap_log, 0, "");
102
103 #define PKTAP_LOG(mask, fmt, ...) \
104 do { \
105 if ((pktap_log & mask)) \
106 printf("%s:%d " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
107 } while (false)
108
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
115
116 /*
117 * pktap_lck_rw protects the global list of pktap interfaces
118 */
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;
123
124 static LIST_HEAD(pktap_list, pktap_softc) pktap_list =
125 LIST_HEAD_INITIALIZER(pktap_list);
126
127 int pktap_clone_create(struct if_clone *, u_int32_t, void *);
128 int pktap_clone_destroy(struct ifnet *);
129
130 static struct if_clone pktap_cloner =
131 IF_CLONE_INITIALIZER(PKTAP_IFNAME,
132 pktap_clone_create,
133 pktap_clone_destroy,
134 0,
135 IF_MAXUNIT);
136
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);
150
151 static void
152 pktap_hexdump(int mask, void *addr, size_t len)
153 {
154 unsigned char *buf = addr;
155 size_t i;
156
157 if (!(pktap_log & mask))
158 return;
159
160 for (i = 0; i < len; i++) {
161 unsigned char h = (buf[i] & 0xf0) >> 4;
162 unsigned char l = buf[i] & 0x0f;
163
164 if (i != 0) {
165 if (i % 32 == 0)
166 printf("\n");
167 else if (i % 4 == 0)
168 printf(" ");
169 }
170 printf("%c%c",
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");
176 }
177
178 __private_extern__ void
179 pktap_init(void)
180 {
181 int error = 0;
182 lck_grp_attr_t *lck_grp_attr = NULL;
183
184 /* Make sure we're called only once */
185 VERIFY(pktap_inited == 0);
186
187 pktap_inited = 1;
188
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);
199
200 error = if_clone_attach(&pktap_cloner);
201 if (error != 0)
202 panic("%s: if_clone_attach() failed, error %d\n",
203 __func__, error);
204 }
205
206 __private_extern__ int
207 pktap_clone_create(struct if_clone *ifc, u_int32_t unit, __unused void *params)
208 {
209 int error = 0;
210 struct pktap_softc *pktap = NULL;
211 struct ifnet_init_params if_init;
212
213 PKTAP_LOG(PKTP_LOG_FUNC, "unit %u\n", unit);
214
215 pktap = _MALLOC(sizeof(struct pktap_softc), M_DEVBUF,
216 M_WAITOK | M_ZERO);
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
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;
234 /*
235 * We do not use a set_bpf_tap() function as we rather rely on the more
236 * accurate callback passed to bpf_attach()
237 */
238 bzero(&if_init, sizeof(struct ifnet_init_params));
239 if_init.name = ifc->ifc_name;
240 if_init.unit = unit;
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;
250
251 error = ifnet_allocate(&if_init, &pktap->pktp_ifp);
252 if (error != 0) {
253 printf("%s: ifnet_allocate failed, error %d\n",
254 __func__, error);
255 goto done;
256 }
257
258 ifnet_set_flags(pktap->pktp_ifp, IFF_UP, IFF_UP);
259
260 error = ifnet_attach(pktap->pktp_ifp, NULL);
261 if (error != 0) {
262 printf("%s: ifnet_attach failed - error %d\n", __func__, error);
263 ifnet_release(pktap->pktp_ifp);
264 goto done;
265 }
266
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);
271
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);
277 done:
278 if (error != 0) {
279 if (pktap != NULL)
280 _FREE(pktap, M_DEVBUF);
281 }
282 return (error);
283 }
284
285 __private_extern__ int
286 pktap_clone_destroy(struct ifnet *ifp)
287 {
288 int error = 0;
289
290 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
291
292 (void) ifnet_detach(ifp);
293
294 return (error);
295 }
296
297 /*
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
300 * default DLT
301 * - Whenever a new DLT is selected via BIOCSDLT
302 * - When the interface is detached from a BPF device (direction is zero)
303 */
304 __private_extern__ errno_t
305 pktap_tap_callback(ifnet_t ifp, u_int32_t dlt, bpf_tap_mode direction)
306 {
307 struct pktap_softc *pktap;
308
309 pktap = ifp->if_softc;
310 if (pktap == NULL) {
311 printf("%s: if_softc is NULL for ifp %s\n", __func__,
312 ifp->if_xname);
313 goto done;
314 }
315 switch (dlt) {
316 case DLT_RAW:
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);
321
322 }
323 } else {
324 pktap->pktp_dlt_raw_count++;
325 OSAddAtomic(1, &pktap_total_tap_count);
326 }
327 break;
328 case DLT_PKTAP:
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);
333 }
334 } else {
335 pktap->pktp_dlt_pkttap_count++;
336 OSAddAtomic(1, &pktap_total_tap_count);
337 }
338 break;
339 }
340 done:
341 /*
342 * Attachements count must be positive and we're in trouble
343 * if we have more that 2**31 attachements
344 */
345 VERIFY(pktap_total_tap_count >= 0);
346
347 return (0);
348 }
349
350 __private_extern__ errno_t
351 pktap_if_output(ifnet_t ifp, mbuf_t m)
352 {
353 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
354 mbuf_freem(m);
355 return (ENOTSUP);
356 }
357
358 __private_extern__ errno_t
359 pktap_demux(ifnet_t ifp, __unused mbuf_t m, __unused char *header,
360 __unused protocol_family_t *ppf)
361 {
362 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
363 return (ENOTSUP);
364 }
365
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)
369 {
370 PKTAP_LOG(PKTP_LOG_FUNC, "%s pf %u\n", ifp->if_xname, pf);
371 return (0);
372 }
373
374 __private_extern__ errno_t
375 pktap_del_proto(__unused ifnet_t ifp, __unused protocol_family_t pf)
376 {
377 PKTAP_LOG(PKTP_LOG_FUNC, "%s pf %u\n", ifp->if_xname, pf);
378 return (0);
379 }
380
381 __private_extern__ errno_t
382 pktap_getdrvspec(ifnet_t ifp, struct ifdrv64 *ifd)
383 {
384 errno_t error = 0;
385 struct pktap_softc *pktap;
386 int i;
387
388 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
389
390 pktap = ifp->if_softc;
391 if (pktap == NULL) {
392 error = ENOENT;
393 printf("%s: pktap NULL - error %d\n", __func__, error);
394 goto done;
395 }
396
397 switch (ifd->ifd_cmd) {
398 case PKTP_CMD_FILTER_GET: {
399 struct x_pktap_filter x_filters[PKTAP_MAX_FILTERS];
400
401 bzero(&x_filters, sizeof(x_filters));
402
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);
406 error = EINVAL;
407 break;
408 }
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;
412
413 x_filter->filter_op = pktap_filter->filter_op;
414 x_filter->filter_param = pktap_filter->filter_param;
415
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));
422 }
423 error = copyout(x_filters, ifd->ifd_data,
424 PKTAP_MAX_FILTERS * sizeof(struct x_pktap_filter));
425 if (error) {
426 printf("%s: PKTP_CMD_FILTER_GET copyout - error %d\n", __func__, error);
427 goto done;
428 }
429 break;
430 }
431 case PKTP_CMD_TAP_COUNT: {
432 uint32_t tap_count = pktap->pktp_dlt_raw_count + pktap->pktp_dlt_pkttap_count;
433
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);
437 error = EINVAL;
438 break;
439 }
440 error = copyout(&tap_count, ifd->ifd_data, sizeof(tap_count));
441 if (error) {
442 printf("%s: PKTP_CMD_TAP_COUNT copyout - error %d\n", __func__, error);
443 goto done;
444 }
445 break;
446 }
447 default:
448 error = EINVAL;
449 break;
450 }
451
452 done:
453 return (error);
454 }
455
456 __private_extern__ errno_t
457 pktap_setdrvspec(ifnet_t ifp, struct ifdrv64 *ifd)
458 {
459 errno_t error = 0;
460 struct pktap_softc *pktap;
461
462 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
463
464 pktap = ifp->if_softc;
465 if (pktap == NULL) {
466 error = ENOENT;
467 printf("%s: pktap NULL - error %d\n", __func__, error);
468 goto done;
469 }
470
471 switch (ifd->ifd_cmd) {
472 case PKTP_CMD_FILTER_SET: {
473 struct x_pktap_filter user_filters[PKTAP_MAX_FILTERS];
474 int i;
475 int got_op_none = 0;
476
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);
480 error = EINVAL;
481 break;
482 }
483 error = copyin(ifd->ifd_data, &user_filters, ifd->ifd_len);
484 if (error) {
485 printf("%s: copyin - error %d\n", __func__, error);
486 goto done;
487 }
488 /*
489 * Validate user provided parameters
490 */
491 for (i = 0; i < PKTAP_MAX_FILTERS; i++) {
492 struct x_pktap_filter *x_filter = user_filters + i;
493
494 switch (x_filter->filter_op) {
495 case PKTAP_FILTER_OP_NONE:
496 /* Following entries must be PKTAP_FILTER_OP_NONE */
497 got_op_none = 1;
498 break;
499 case PKTAP_FILTER_OP_PASS:
500 case PKTAP_FILTER_OP_SKIP:
501 /* Invalid after PKTAP_FILTER_OP_NONE */
502 if (got_op_none) {
503 error = EINVAL;
504 break;
505 }
506 break;
507 default:
508 error = EINVAL;
509 break;
510 }
511 if (error != 0)
512 break;
513
514 switch (x_filter->filter_param) {
515 case PKTAP_FILTER_OP_NONE:
516 if (x_filter->filter_op != PKTAP_FILTER_OP_NONE) {
517 error = EINVAL;
518 break;
519 }
520 break;
521
522 /*
523 * Do not allow to tap a pktap from a pktap
524 */
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) {
528 error = EINVAL;
529 break;
530 }
531 break;
532
533 case PKTAP_FILTER_PARAM_IF_NAME:
534 if (strncmp(x_filter->filter_param_if_name, PKTAP_IFNAME,
535 strlen(PKTAP_IFNAME)) == 0) {
536 error = EINVAL;
537 break;
538 }
539 break;
540
541 default:
542 error = EINVAL;
543 break;
544 }
545 if (error != 0)
546 break;
547 }
548 if (error != 0)
549 break;
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;
553
554 pktap_filter->filter_op = x_filter->filter_op;
555 pktap_filter->filter_param = x_filter->filter_param;
556
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) {
560 size_t len;
561
562 strlcpy(pktap_filter->filter_param_if_name,
563 x_filter->filter_param_if_name,
564 sizeof(pktap_filter->filter_param_if_name));
565 /*
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
568 */
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;
573 }
574 }
575 break;
576 }
577 default:
578 error = EINVAL;
579 break;
580 }
581
582 done:
583 return (error);
584 }
585
586 __private_extern__ errno_t
587 pktap_ioctl(ifnet_t ifp, unsigned long cmd, void *data)
588 {
589 errno_t error = 0;
590
591 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
592
593 if ((cmd & IOC_IN)) {
594 error = kauth_authorize_generic(kauth_cred_get(), KAUTH_GENERIC_ISSUSER);
595 if (error) {
596 PKTAP_LOG(PKTP_LOG_ERROR,
597 "%s: kauth_authorize_generic(KAUTH_GENERIC_ISSUSER) - error %d\n",
598 __func__, error);
599 goto done;
600 }
601 }
602
603 switch (cmd) {
604 case SIOCGDRVSPEC32: {
605 struct ifdrv64 ifd;
606 struct ifdrv32 *ifd32 = (struct ifdrv32 *)data;
607
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;
612
613 error = pktap_getdrvspec(ifp, &ifd);
614
615 break;
616 }
617 case SIOCGDRVSPEC64: {
618 struct ifdrv64 *ifd64 = (struct ifdrv64 *)data;
619
620 error = pktap_getdrvspec(ifp, ifd64);
621
622 break;
623 }
624 case SIOCSDRVSPEC32: {
625 struct ifdrv64 ifd;
626 struct ifdrv32 *ifd32 = (struct ifdrv32 *)data;
627
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;
632
633 error = pktap_setdrvspec(ifp, &ifd);
634 break;
635 }
636 case SIOCSDRVSPEC64: {
637 struct ifdrv64 *ifd64 = (struct ifdrv64 *)data;
638
639 error = pktap_setdrvspec(ifp, ifd64);
640
641 break;
642 }
643 default:
644 error = ENOTSUP;
645 break;
646 }
647 done:
648 return (error);
649 }
650
651 __private_extern__ void
652 pktap_detach(ifnet_t ifp)
653 {
654 struct pktap_softc *pktap;
655
656 PKTAP_LOG(PKTP_LOG_FUNC, "%s\n", ifp->if_xname);
657
658 lck_rw_lock_exclusive(pktap_lck_rw);
659
660 pktap = ifp->if_softc;
661 ifp->if_softc = NULL;
662 LIST_REMOVE(pktap, pktp_link);
663
664 lck_rw_done(pktap_lck_rw);
665
666 /* Drop reference as it's no more on the global list */
667 ifnet_release(ifp);
668
669 _FREE(pktap, M_DEVBUF);
670
671 /* This is for the reference taken by ifnet_attach() */
672 (void) ifnet_release(ifp);
673 }
674
675 __private_extern__ int
676 pktap_filter_evaluate(struct pktap_softc *pktap, struct ifnet *ifp)
677 {
678 int i;
679 int result = PKTAP_FILTER_SKIP; /* Need positive matching rule to pass */
680 int match = 0;
681
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;
686
687 switch (pktap_filter->filter_op) {
688 case PKTAP_FILTER_OP_NONE:
689 match = 1;
690 break;
691
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;
697 match = 1;
698 PKTAP_LOG(PKTP_LOG_FILTER, "pass %s match type %u\n",
699 ifp->if_xname, pktap_filter->filter_param_if_type);
700 break;
701 }
702 }
703 if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_NAME) {
704 if (strncmp(ifp->if_xname, pktap_filter->filter_param_if_name,
705 len) == 0) {
706 result = PKTAP_FILTER_OK;
707 match = 1;
708 PKTAP_LOG(PKTP_LOG_FILTER, "pass %s match name %s\n",
709 ifp->if_xname, pktap_filter->filter_param_if_name);
710 break;
711 }
712 }
713 break;
714
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;
720 match = 1;
721 PKTAP_LOG(PKTP_LOG_FILTER, "skip %s match type %u\n",
722 ifp->if_xname, pktap_filter->filter_param_if_type);
723 break;
724 }
725 }
726 if (pktap_filter->filter_param == PKTAP_FILTER_PARAM_IF_NAME) {
727 if (strncmp(ifp->if_xname, pktap_filter->filter_param_if_name,
728 len) == 0) {
729 result = PKTAP_FILTER_SKIP;
730 match = 1;
731 PKTAP_LOG(PKTP_LOG_FILTER, "skip %s match name %s\n",
732 ifp->if_xname, pktap_filter->filter_param_if_name);
733 break;
734 }
735 }
736 break;
737 }
738 if (match)
739 break;
740 }
741
742 if (match == 0) {
743 PKTAP_LOG(PKTP_LOG_FILTER, "%s no match\n",
744 ifp->if_xname);
745 }
746 return (result);
747 }
748
749 static void
750 pktap_set_procinfo(struct pktap_header *hdr, struct so_procinfo *soprocinfo)
751 {
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);
756
757 /*
758 * When not delegated, the effective pid is the same as the real pid
759 */
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);
765 }
766 }
767
768 __private_extern__ void
769 pktap_finalize_proc_info(struct pktap_header *hdr)
770 {
771 int found;
772 struct so_procinfo soprocinfo;
773
774 if (!(hdr->pth_flags & PTH_FLAG_DELAY_PKTAP))
775 return;
776
777 /*
778 * Clear the flag as it's internal
779 */
780 hdr->pth_flags &= ~PTH_FLAG_DELAY_PKTAP;
781
782 if (hdr->pth_ipproto == IPPROTO_TCP)
783 found = inp_findinpcb_procinfo(&tcbinfo, hdr->pth_flowid,
784 &soprocinfo);
785 else if (hdr->pth_ipproto == IPPROTO_UDP)
786 found = inp_findinpcb_procinfo(&udbinfo, hdr->pth_flowid,
787 &soprocinfo);
788 else
789 found = inp_findinpcb_procinfo(&ripcbinfo, hdr->pth_flowid,
790 &soprocinfo);
791
792 if (found == 1)
793 pktap_set_procinfo(hdr, &soprocinfo);
794 }
795
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)
799 {
800 int found = 0;
801 struct so_procinfo soprocinfo;
802
803 /*
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
807 */
808 if (outgoing != 0 && m->m_pkthdr.pkt_flowsrc == FLOWSRC_INPCB) {
809 /*
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
813 */
814 found = 0;
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;
819 else
820 hdr->pth_ipproto = m->m_pkthdr.pkt_proto;
821 } else if (outgoing == 0) {
822 struct inpcb *inp = NULL;
823
824 if (proto == PF_INET) {
825 struct ip ip;
826 errno_t error;
827 size_t hlen;
828 struct in_addr faddr, laddr;
829 u_short fport, lport;
830 struct inpcbinfo *pcbinfo = NULL;
831 int wildcard = 0;
832
833 error = mbuf_copydata(m, pre, sizeof(struct ip), &ip);
834 if (error != 0) {
835 PKTAP_LOG(PKTP_LOG_ERROR,
836 "mbuf_copydata tcp v4 failed for %s\n",
837 hdr->pth_ifname);
838 goto done;
839 }
840 hlen = IP_VHL_HL(ip.ip_vhl) << 2;
841
842 faddr = ip.ip_src;
843 laddr = ip.ip_dst;
844
845 if (ip.ip_p == IPPROTO_TCP) {
846 struct tcphdr th;
847
848 error = mbuf_copydata(m, pre + hlen,
849 sizeof(struct tcphdr), &th);
850 if (error != 0)
851 goto done;
852
853 fport = th.th_sport;
854 lport = th.th_dport;
855
856 pcbinfo = &tcbinfo;
857 } else if (ip.ip_p == IPPROTO_UDP) {
858 struct udphdr uh;
859
860 error = mbuf_copydata(m, pre + hlen,
861 sizeof(struct udphdr), &uh);
862 if (error != 0) {
863 PKTAP_LOG(PKTP_LOG_ERROR,
864 "mbuf_copydata udp v4 failed for %s\n",
865 hdr->pth_ifname);
866 goto done;
867 }
868 fport = uh.uh_sport;
869 lport = uh.uh_dport;
870
871 pcbinfo = &udbinfo;
872 wildcard = 1;
873 }
874 if (pcbinfo != NULL) {
875 inp = in_pcblookup_hash(pcbinfo, faddr, fport,
876 laddr, lport, wildcard, outgoing ? NULL : ifp);
877
878 if (inp == NULL && hdr->pth_iftype != IFT_LOOP)
879 PKTAP_LOG(PKTP_LOG_NOPCB,
880 "in_pcblookup_hash no pcb %s\n",
881 hdr->pth_ifname);
882 } else {
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));
887 }
888 } else if (proto == PF_INET6) {
889 struct ip6_hdr ip6;
890 errno_t error;
891 struct in6_addr *faddr;
892 struct in6_addr *laddr;
893 u_short fport, lport;
894 struct inpcbinfo *pcbinfo = NULL;
895 int wildcard = 0;
896
897 error = mbuf_copydata(m, pre, sizeof(struct ip6_hdr), &ip6);
898 if (error != 0)
899 goto done;
900
901 faddr = &ip6.ip6_src;
902 laddr = &ip6.ip6_dst;
903
904 if (ip6.ip6_nxt == IPPROTO_TCP) {
905 struct tcphdr th;
906
907 error = mbuf_copydata(m, pre + sizeof(struct ip6_hdr),
908 sizeof(struct tcphdr), &th);
909 if (error != 0) {
910 PKTAP_LOG(PKTP_LOG_ERROR,
911 "mbuf_copydata tcp v6 failed for %s\n",
912 hdr->pth_ifname);
913 goto done;
914 }
915
916 fport = th.th_sport;
917 lport = th.th_dport;
918
919 pcbinfo = &tcbinfo;
920 } else if (ip6.ip6_nxt == IPPROTO_UDP) {
921 struct udphdr uh;
922
923 error = mbuf_copydata(m, pre + sizeof(struct ip6_hdr),
924 sizeof(struct udphdr), &uh);
925 if (error != 0) {
926 PKTAP_LOG(PKTP_LOG_ERROR,
927 "mbuf_copydata udp v6 failed for %s\n",
928 hdr->pth_ifname);
929 goto done;
930 }
931
932 fport = uh.uh_sport;
933 lport = uh.uh_dport;
934
935 pcbinfo = &udbinfo;
936 wildcard = 1;
937 }
938 if (pcbinfo != NULL) {
939 inp = in6_pcblookup_hash(pcbinfo, faddr, fport,
940 laddr, lport, wildcard, outgoing ? NULL : ifp);
941
942 if (inp == NULL && hdr->pth_iftype != IFT_LOOP)
943 PKTAP_LOG(PKTP_LOG_NOPCB,
944 "in6_pcblookup_hash no pcb %s\n",
945 hdr->pth_ifname);
946 } else {
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));
951 }
952 }
953 if (inp != NULL) {
954 if (inp->inp_state != INPCB_STATE_DEAD && inp->inp_socket != NULL) {
955 found = 1;
956 inp_get_soprocinfo(inp, &soprocinfo);
957 }
958 in_pcb_checkstate(inp, WNT_RELEASE, 0);
959 }
960 }
961 done:
962 /*
963 * -1 means PID not found
964 */
965 hdr->pth_pid = -1;
966 hdr->pth_epid = -1;
967 if (found != 0)
968 pktap_set_procinfo(hdr, &soprocinfo);
969 }
970
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)
974 {
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;
978
979 lck_rw_lock_shared(pktap_lck_rw);
980
981 /*
982 * No need to take the ifnet_lock as the struct ifnet field if_bpf is
983 * protected by the BPF subsystem
984 */
985 LIST_FOREACH(pktap, &pktap_list, pktp_link) {
986 int filter_result;
987
988 filter_result = pktap_filter_evaluate(pktap, ifp);
989 if (filter_result == PKTAP_FILTER_SKIP)
990 continue;
991
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)) {
996 /*
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.
1000 */
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);
1004 }
1005 }
1006 }
1007
1008 if (pktap->pktp_dlt_pkttap_count > 0) {
1009 struct {
1010 struct pktap_header hdr;
1011 u_int32_t proto;
1012 } hdr_buffer;
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;
1018
1019 /* Verify the structure is packed */
1020 _CASSERT(sizeof(hdr_buffer) == sizeof(struct pktap_header) + sizeof(u_int32_t));
1021
1022 bzero(&hdr_buffer, sizeof(hdr_buffer));
1023 hdr->pth_length = sizeof(struct pktap_header);
1024 hdr->pth_type_next = PTH_TYPE_PACKET;
1025
1026 /*
1027 * Set DLT of packet based on interface type
1028 */
1029 switch (ifp->if_type) {
1030 case IFT_LOOP:
1031 case IFT_GIF:
1032 case IFT_STF:
1033 case IFT_CELLULAR:
1034 /*
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
1041 */
1042 hdr->pth_dlt = DLT_NULL;
1043 if (pre == 0) {
1044 hdr_buffer.proto = proto;
1045 hdr_size = sizeof(hdr_buffer);
1046 pre_adjust = sizeof(hdr_buffer.proto);
1047 }
1048 break;
1049 case IFT_ETHER:
1050 case IFT_BRIDGE:
1051 case IFT_L2VLAN:
1052 case IFT_IEEE8023ADLAG:
1053 hdr->pth_dlt = DLT_EN10MB;
1054 break;
1055 case IFT_PPP:
1056 hdr->pth_dlt = DLT_PPP;
1057 break;
1058 case IFT_IEEE1394:
1059 hdr->pth_dlt = DLT_APPLE_IP_OVER_IEEE1394;
1060 break;
1061 case IFT_OTHER:
1062 if (strncmp(ifp->if_name, "utun", strlen("utun")) == 0) {
1063 /*
1064 * For utun:
1065 * - incoming packets do not have the prefix set to four
1066 * - some packets are as small as two bytes!
1067 */
1068 if (m_pktlen(m) < 4)
1069 goto done;
1070 if (proto != AF_INET && proto != AF_INET6)
1071 goto done;
1072 if (proto == AF_INET && (size_t) m_pktlen(m) - 4 < sizeof(struct ip))
1073 goto done;
1074 if (proto == AF_INET6 && (size_t) m_pktlen(m) - 4 < sizeof(struct ip6_hdr))
1075 goto done;
1076 /*
1077 * Skip the protocol in the mbuf as it's in network order
1078 */
1079 pre = 4;
1080 data_adjust = 4;
1081 hdr->pth_dlt = DLT_NULL;
1082 hdr_buffer.proto = proto;
1083 hdr_size = sizeof(hdr_buffer);
1084 break;
1085 }
1086 default:
1087 if (pre == 0)
1088 hdr->pth_dlt = DLT_RAW;
1089 else
1090 unknown_if_type = 1;
1091 break;
1092 }
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;
1098 } else {
1099 snprintf(hdr->pth_ifname, sizeof(hdr->pth_ifname), "%s",
1100 ifp->if_xname);
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;
1107
1108 pktap_fill_proc_info(hdr, proto, m, pre, outgoing, ifp);
1109
1110 hdr->pth_svc = so_svc2tc(m->m_pkthdr.pkt_svc);
1111
1112 if (data_adjust == 0) {
1113 bpf_tap_func(pktap->pktp_ifp, DLT_PKTAP, m, hdr, hdr_size);
1114 } else {
1115 /*
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.
1119 */
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);
1123 }
1124 }
1125 }
1126 }
1127 }
1128 done:
1129 lck_rw_done(pktap_lck_rw);
1130 }
1131
1132 __private_extern__ void
1133 pktap_input(struct ifnet *ifp, protocol_family_t proto, struct mbuf *m,
1134 char *frame_header)
1135 {
1136 char *hdr = (char *)mbuf_data(m);
1137 char *start = (char *)mbuf_datastart(m);
1138
1139 /* Fast path */
1140 if (pktap_total_tap_count == 0)
1141 return;
1142
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;
1147
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);
1151
1152 pktap_bpf_tap(ifp, proto, m, pre, 0, 0);
1153 mbuf_setdata(m, hdr, o_len);
1154 }
1155 } else {
1156 PKTAP_LOG(PKTP_LOG_INPUT, "ifp %s proto %u pre %u post %u\n",
1157 ifp->if_xname, proto, 0, 0);
1158
1159 pktap_bpf_tap(ifp, proto, m, 0, 0, 0);
1160 }
1161 }
1162
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)
1166 {
1167 /* Fast path */
1168 if (pktap_total_tap_count == 0)
1169 return;
1170
1171 PKTAP_LOG(PKTP_LOG_OUTPUT, "ifp %s proto %u pre %u post %u\n",
1172 ifp->if_xname, proto, pre, post);
1173
1174 pktap_bpf_tap(ifp, proto, m, pre, post, 1);
1175 }