xref: /xnu-8796.141.3/tests/bpflib.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*1b191cb5SApple OSS Distributions  * file.
12*1b191cb5SApple OSS Distributions  *
13*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*1b191cb5SApple OSS Distributions  * limitations under the License.
20*1b191cb5SApple OSS Distributions  *
21*1b191cb5SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*1b191cb5SApple OSS Distributions  */
23*1b191cb5SApple OSS Distributions 
24*1b191cb5SApple OSS Distributions #include <stdlib.h>
25*1b191cb5SApple OSS Distributions #include <unistd.h>
26*1b191cb5SApple OSS Distributions #include <stdio.h>
27*1b191cb5SApple OSS Distributions #include <sys/types.h>
28*1b191cb5SApple OSS Distributions #include <sys/time.h>
29*1b191cb5SApple OSS Distributions #include <sys/ioctl.h>
30*1b191cb5SApple OSS Distributions #include <sys/stat.h>
31*1b191cb5SApple OSS Distributions #include <fcntl.h>
32*1b191cb5SApple OSS Distributions #include <net/bpf.h>
33*1b191cb5SApple OSS Distributions #include <string.h>
34*1b191cb5SApple OSS Distributions #include <sys/socket.h>
35*1b191cb5SApple OSS Distributions #include <errno.h>
36*1b191cb5SApple OSS Distributions #include <net/if.h>
37*1b191cb5SApple OSS Distributions #include <stdbool.h>
38*1b191cb5SApple OSS Distributions #define PRIVATE_EXTERN __private_extern__
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions #include "bpflib.h"
41*1b191cb5SApple OSS Distributions 
42*1b191cb5SApple OSS Distributions #ifdef TESTING
43*1b191cb5SApple OSS Distributions #include "util.h"
44*1b191cb5SApple OSS Distributions #endif /* TESTING */
45*1b191cb5SApple OSS Distributions 
46*1b191cb5SApple OSS Distributions PRIVATE_EXTERN int
bpf_set_timeout(int fd,struct timeval * tv_p)47*1b191cb5SApple OSS Distributions bpf_set_timeout(int fd, struct timeval * tv_p)
48*1b191cb5SApple OSS Distributions {
49*1b191cb5SApple OSS Distributions 	return ioctl(fd, BIOCSRTIMEOUT, tv_p);
50*1b191cb5SApple OSS Distributions }
51*1b191cb5SApple OSS Distributions 
52*1b191cb5SApple OSS Distributions PRIVATE_EXTERN int
bpf_get_blen(int fd,int * blen)53*1b191cb5SApple OSS Distributions bpf_get_blen(int fd, int * blen)
54*1b191cb5SApple OSS Distributions {
55*1b191cb5SApple OSS Distributions 	return ioctl(fd, BIOCGBLEN, blen);
56*1b191cb5SApple OSS Distributions }
57*1b191cb5SApple OSS Distributions 
58*1b191cb5SApple OSS Distributions PRIVATE_EXTERN int
bpf_set_header_complete(int fd,u_int header_complete)59*1b191cb5SApple OSS Distributions bpf_set_header_complete(int fd, u_int header_complete)
60*1b191cb5SApple OSS Distributions {
61*1b191cb5SApple OSS Distributions 	return ioctl(fd, BIOCSHDRCMPLT, &header_complete);
62*1b191cb5SApple OSS Distributions }
63*1b191cb5SApple OSS Distributions 
64*1b191cb5SApple OSS Distributions PRIVATE_EXTERN int
bpf_set_see_sent(int fd,u_int see_sent)65*1b191cb5SApple OSS Distributions bpf_set_see_sent(int fd, u_int see_sent)
66*1b191cb5SApple OSS Distributions {
67*1b191cb5SApple OSS Distributions 	return ioctl(fd, BIOCSSEESENT, &see_sent);
68*1b191cb5SApple OSS Distributions }
69*1b191cb5SApple OSS Distributions 
70*1b191cb5SApple OSS Distributions PRIVATE_EXTERN int
bpf_dispose(int bpf_fd)71*1b191cb5SApple OSS Distributions bpf_dispose(int bpf_fd)
72*1b191cb5SApple OSS Distributions {
73*1b191cb5SApple OSS Distributions 	if (bpf_fd >= 0) {
74*1b191cb5SApple OSS Distributions 		return close(bpf_fd);
75*1b191cb5SApple OSS Distributions 	}
76*1b191cb5SApple OSS Distributions 	return 0;
77*1b191cb5SApple OSS Distributions }
78*1b191cb5SApple OSS Distributions 
79*1b191cb5SApple OSS Distributions PRIVATE_EXTERN int
bpf_new(void)80*1b191cb5SApple OSS Distributions bpf_new(void)
81*1b191cb5SApple OSS Distributions {
82*1b191cb5SApple OSS Distributions 	char bpfdev[256];
83*1b191cb5SApple OSS Distributions 	int i;
84*1b191cb5SApple OSS Distributions 	int fd = -1;
85*1b191cb5SApple OSS Distributions 
86*1b191cb5SApple OSS Distributions 	for (i = 0; true; i++) {
87*1b191cb5SApple OSS Distributions 		snprintf(bpfdev, sizeof(bpfdev), "/dev/bpf%d", i);
88*1b191cb5SApple OSS Distributions 		fd = open(bpfdev, O_RDWR, 0);
89*1b191cb5SApple OSS Distributions 		if (fd >= 0) {
90*1b191cb5SApple OSS Distributions #ifdef SO_TC_CTL
91*1b191cb5SApple OSS Distributions 			int tc = SO_TC_CTL;
92*1b191cb5SApple OSS Distributions 			(void) ioctl(fd, BIOCSETTC, &tc);
93*1b191cb5SApple OSS Distributions #endif /* SO_TC_CTL */
94*1b191cb5SApple OSS Distributions 			break;
95*1b191cb5SApple OSS Distributions 		}
96*1b191cb5SApple OSS Distributions 		if (errno != EBUSY) {
97*1b191cb5SApple OSS Distributions 			break;
98*1b191cb5SApple OSS Distributions 		}
99*1b191cb5SApple OSS Distributions 	}
100*1b191cb5SApple OSS Distributions 	return fd;
101*1b191cb5SApple OSS Distributions }
102*1b191cb5SApple OSS Distributions 
103*1b191cb5SApple OSS Distributions PRIVATE_EXTERN int
bpf_setif(int fd,const char * en_name)104*1b191cb5SApple OSS Distributions bpf_setif(int fd, const char * en_name)
105*1b191cb5SApple OSS Distributions {
106*1b191cb5SApple OSS Distributions 	struct ifreq ifr;
107*1b191cb5SApple OSS Distributions 
108*1b191cb5SApple OSS Distributions 	strlcpy(ifr.ifr_name, en_name, sizeof(ifr.ifr_name));
109*1b191cb5SApple OSS Distributions 	return ioctl(fd, BIOCSETIF, &ifr);
110*1b191cb5SApple OSS Distributions }
111*1b191cb5SApple OSS Distributions 
112*1b191cb5SApple OSS Distributions PRIVATE_EXTERN int
bpf_set_immediate(int fd,u_int value)113*1b191cb5SApple OSS Distributions bpf_set_immediate(int fd, u_int value)
114*1b191cb5SApple OSS Distributions {
115*1b191cb5SApple OSS Distributions 	return ioctl(fd, BIOCIMMEDIATE, &value);
116*1b191cb5SApple OSS Distributions }
117*1b191cb5SApple OSS Distributions 
118*1b191cb5SApple OSS Distributions PRIVATE_EXTERN int
bpf_filter_receive_none(int fd)119*1b191cb5SApple OSS Distributions bpf_filter_receive_none(int fd)
120*1b191cb5SApple OSS Distributions {
121*1b191cb5SApple OSS Distributions 	struct bpf_insn insns[] = {
122*1b191cb5SApple OSS Distributions 		BPF_STMT(BPF_RET + BPF_K, 0),
123*1b191cb5SApple OSS Distributions 	};
124*1b191cb5SApple OSS Distributions 	struct bpf_program prog;
125*1b191cb5SApple OSS Distributions 
126*1b191cb5SApple OSS Distributions 	prog.bf_len = sizeof(insns) / sizeof(struct bpf_insn);
127*1b191cb5SApple OSS Distributions 	prog.bf_insns = insns;
128*1b191cb5SApple OSS Distributions 	return ioctl(fd, BIOCSETF, &prog);
129*1b191cb5SApple OSS Distributions }
130*1b191cb5SApple OSS Distributions 
131*1b191cb5SApple OSS Distributions PRIVATE_EXTERN int
bpf_arp_filter(int fd,int type_offset,int type,u_int pkt_size)132*1b191cb5SApple OSS Distributions bpf_arp_filter(int fd, int type_offset, int type, u_int pkt_size)
133*1b191cb5SApple OSS Distributions {
134*1b191cb5SApple OSS Distributions 	struct bpf_insn insns[] = {
135*1b191cb5SApple OSS Distributions 		BPF_STMT(BPF_LD + BPF_H + BPF_ABS, type_offset),
136*1b191cb5SApple OSS Distributions 		BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, type, 0, 1),
137*1b191cb5SApple OSS Distributions 		BPF_STMT(BPF_RET + BPF_K, pkt_size),
138*1b191cb5SApple OSS Distributions 		BPF_STMT(BPF_RET + BPF_K, 0),
139*1b191cb5SApple OSS Distributions 	};
140*1b191cb5SApple OSS Distributions 	struct bpf_program prog;
141*1b191cb5SApple OSS Distributions 
142*1b191cb5SApple OSS Distributions 	prog.bf_len = sizeof(insns) / sizeof(struct bpf_insn);
143*1b191cb5SApple OSS Distributions 	prog.bf_insns = insns;
144*1b191cb5SApple OSS Distributions 	return ioctl(fd, BIOCSETF, &prog);
145*1b191cb5SApple OSS Distributions }
146*1b191cb5SApple OSS Distributions 
147*1b191cb5SApple OSS Distributions #ifdef TESTING
148*1b191cb5SApple OSS Distributions #include <net/if_arp.h>
149*1b191cb5SApple OSS Distributions #include <net/ethernet.h>
150*1b191cb5SApple OSS Distributions #include <netinet/if_ether.h>
151*1b191cb5SApple OSS Distributions 
152*1b191cb5SApple OSS Distributions 
153*1b191cb5SApple OSS Distributions void
bpf_read_continuously(int fd,u_int blen)154*1b191cb5SApple OSS Distributions bpf_read_continuously(int fd, u_int blen)
155*1b191cb5SApple OSS Distributions {
156*1b191cb5SApple OSS Distributions 	int n;
157*1b191cb5SApple OSS Distributions 	char * rxbuf = malloc(blen);
158*1b191cb5SApple OSS Distributions 
159*1b191cb5SApple OSS Distributions 	printf("rx buf len is %d\n", blen);
160*1b191cb5SApple OSS Distributions 	while (1) {
161*1b191cb5SApple OSS Distributions 		n = read(fd, rxbuf, blen);
162*1b191cb5SApple OSS Distributions 		if (n < 0) {
163*1b191cb5SApple OSS Distributions 			perror("bpf_read_continuously");
164*1b191cb5SApple OSS Distributions 			return;
165*1b191cb5SApple OSS Distributions 		}
166*1b191cb5SApple OSS Distributions 		if (n == 0) {
167*1b191cb5SApple OSS Distributions 			continue;
168*1b191cb5SApple OSS Distributions 		}
169*1b191cb5SApple OSS Distributions 		print_data(rxbuf, n);
170*1b191cb5SApple OSS Distributions 	}
171*1b191cb5SApple OSS Distributions }
172*1b191cb5SApple OSS Distributions 
173*1b191cb5SApple OSS Distributions int
main(int argc,char * argv[])174*1b191cb5SApple OSS Distributions main(int argc, char * argv[])
175*1b191cb5SApple OSS Distributions {
176*1b191cb5SApple OSS Distributions 	int fd = bpf_new();
177*1b191cb5SApple OSS Distributions 	char * en_name = "en0";
178*1b191cb5SApple OSS Distributions 	u_int bpf_blen = 0;
179*1b191cb5SApple OSS Distributions 
180*1b191cb5SApple OSS Distributions 	if (fd < 0) {
181*1b191cb5SApple OSS Distributions 		perror("no bpf devices");
182*1b191cb5SApple OSS Distributions 		exit(1);
183*1b191cb5SApple OSS Distributions 	}
184*1b191cb5SApple OSS Distributions 
185*1b191cb5SApple OSS Distributions 	if (argc > 1) {
186*1b191cb5SApple OSS Distributions 		en_name = argv[1];
187*1b191cb5SApple OSS Distributions 	}
188*1b191cb5SApple OSS Distributions 	(void)bpf_set_immediate(fd, 1);
189*1b191cb5SApple OSS Distributions 	if (bpf_arp_filter(fd, 12, ETHERTYPE_ARP,
190*1b191cb5SApple OSS Distributions 	    sizeof(struct ether_arp) + sizeof(struct ether_header))
191*1b191cb5SApple OSS Distributions 	    < 0) {
192*1b191cb5SApple OSS Distributions 		perror("bpf_arp_filter");
193*1b191cb5SApple OSS Distributions 	}
194*1b191cb5SApple OSS Distributions 	if (bpf_setif(fd, en_name) < 0) {
195*1b191cb5SApple OSS Distributions 		perror("bpf_attach");
196*1b191cb5SApple OSS Distributions 		exit(1);
197*1b191cb5SApple OSS Distributions 	}
198*1b191cb5SApple OSS Distributions 
199*1b191cb5SApple OSS Distributions 	if (bpf_get_blen(fd, &bpf_blen) < 0) {
200*1b191cb5SApple OSS Distributions 		perror("bpf_get_blen");
201*1b191cb5SApple OSS Distributions 		exit(1);
202*1b191cb5SApple OSS Distributions 	}
203*1b191cb5SApple OSS Distributions 	bpf_read_continuously(fd, bpf_blen);
204*1b191cb5SApple OSS Distributions 	exit(0);
205*1b191cb5SApple OSS Distributions 	return 0;
206*1b191cb5SApple OSS Distributions }
207*1b191cb5SApple OSS Distributions #endif /* TESTING */
208