xref: /xnu-11417.140.69/tests/bpflib.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1 /*
2  * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
3  *
4  * @APPLE_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. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
13  * The Original Code and all software distributed under the License are
14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18  * Please see the License for the specific language governing rights and
19  * limitations under the License.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23 
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/time.h>
29 #include <sys/ioctl.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <net/bpf.h>
33 #include <string.h>
34 #include <sys/socket.h>
35 #include <errno.h>
36 #include <net/if.h>
37 #include <stdbool.h>
38 #define PRIVATE_EXTERN __private_extern__
39 
40 #include "bpflib.h"
41 
42 #ifdef TESTING
43 #include "util.h"
44 #endif /* TESTING */
45 
46 PRIVATE_EXTERN int
bpf_set_timeout(int fd,struct timeval * tv_p)47 bpf_set_timeout(int fd, struct timeval * tv_p)
48 {
49 	return ioctl(fd, BIOCSRTIMEOUT, tv_p);
50 }
51 
52 PRIVATE_EXTERN int
bpf_get_blen(int fd,int * blen)53 bpf_get_blen(int fd, int * blen)
54 {
55 	return ioctl(fd, BIOCGBLEN, blen);
56 }
57 
58 PRIVATE_EXTERN int
bpf_set_blen(int fd,int blen)59 bpf_set_blen(int fd, int blen)
60 {
61 	return ioctl(fd, BIOCSBLEN, &blen);
62 }
63 
64 PRIVATE_EXTERN int
bpf_set_header_complete(int fd,u_int header_complete)65 bpf_set_header_complete(int fd, u_int header_complete)
66 {
67 	return ioctl(fd, BIOCSHDRCMPLT, &header_complete);
68 }
69 
70 PRIVATE_EXTERN int
bpf_set_see_sent(int fd,u_int see_sent)71 bpf_set_see_sent(int fd, u_int see_sent)
72 {
73 	return ioctl(fd, BIOCSSEESENT, &see_sent);
74 }
75 
76 PRIVATE_EXTERN int
bpf_dispose(int bpf_fd)77 bpf_dispose(int bpf_fd)
78 {
79 	if (bpf_fd >= 0) {
80 		return close(bpf_fd);
81 	}
82 	return 0;
83 }
84 
85 PRIVATE_EXTERN int
bpf_set_traffic_class(int fd,int tc)86 bpf_set_traffic_class(int fd, int tc)
87 {
88 	return ioctl(fd, BIOCSETTC, &tc);
89 }
90 
91 #ifdef BIOCSDIRECTION
92 PRIVATE_EXTERN int
bpf_set_direction(int fd,u_int direction)93 bpf_set_direction(int fd, u_int direction)
94 {
95 	return ioctl(fd, BIOCSDIRECTION, &direction);
96 }
97 
98 PRIVATE_EXTERN int
bpf_get_direction(int fd,u_int * direction)99 bpf_get_direction(int fd, u_int *direction)
100 {
101 	return ioctl(fd, BIOCGDIRECTION, direction);
102 }
103 
104 #endif /* BIOCSDIRECTION */
105 
106 PRIVATE_EXTERN int
bpf_new(void)107 bpf_new(void)
108 {
109 	char bpfdev[256];
110 	int i;
111 	int fd = -1;
112 
113 	for (i = 0; true; i++) {
114 		snprintf(bpfdev, sizeof(bpfdev), "/dev/bpf%d", i);
115 		fd = open(bpfdev, O_RDWR, 0);
116 		if (fd >= 0) {
117 			break;
118 		}
119 		if (errno != EBUSY) {
120 			break;
121 		}
122 	}
123 	return fd;
124 }
125 
126 PRIVATE_EXTERN int
bpf_setif(int fd,const char * en_name)127 bpf_setif(int fd, const char * en_name)
128 {
129 	struct ifreq ifr;
130 
131 	strlcpy(ifr.ifr_name, en_name, sizeof(ifr.ifr_name));
132 	return ioctl(fd, BIOCSETIF, &ifr);
133 }
134 
135 PRIVATE_EXTERN int
bpf_set_immediate(int fd,u_int value)136 bpf_set_immediate(int fd, u_int value)
137 {
138 	return ioctl(fd, BIOCIMMEDIATE, &value);
139 }
140 
141 PRIVATE_EXTERN int
bpf_filter_receive_none(int fd)142 bpf_filter_receive_none(int fd)
143 {
144 	struct bpf_insn insns[] = {
145 		BPF_STMT(BPF_RET + BPF_K, 0),
146 	};
147 	struct bpf_program prog;
148 
149 	prog.bf_len = sizeof(insns) / sizeof(struct bpf_insn);
150 	prog.bf_insns = insns;
151 	return ioctl(fd, BIOCSETF, &prog);
152 }
153 
154 PRIVATE_EXTERN int
bpf_arp_filter(int fd,int type_offset,int type,u_int pkt_size)155 bpf_arp_filter(int fd, int type_offset, int type, u_int pkt_size)
156 {
157 	struct bpf_insn insns[] = {
158 		BPF_STMT(BPF_LD + BPF_H + BPF_ABS, type_offset),
159 		BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, type, 0, 1),
160 		BPF_STMT(BPF_RET + BPF_K, pkt_size),
161 		BPF_STMT(BPF_RET + BPF_K, 0),
162 	};
163 	struct bpf_program prog;
164 
165 	prog.bf_len = sizeof(insns) / sizeof(struct bpf_insn);
166 	prog.bf_insns = insns;
167 	return ioctl(fd, BIOCSETF, &prog);
168 }
169 
170 PRIVATE_EXTERN int
bpf_set_write_size_max(int fd,u_int value)171 bpf_set_write_size_max(int fd, u_int value)
172 {
173 #ifdef BIOCSWRITEMAX
174 	return ioctl(fd, BIOCSWRITEMAX, &value);
175 #else
176 #pragma unused(fd, value)
177 	return ENOTSUP;
178 #endif
179 }
180 
181 PRIVATE_EXTERN int
bpf_get_write_size_max(int fd,u_int * value)182 bpf_get_write_size_max(int fd, u_int *value)
183 {
184 #ifdef BIOCGWRITEMAX
185 	return ioctl(fd, BIOCGWRITEMAX, value);
186 #else
187 #pragma unused(fd, value)
188 	return ENOTSUP;
189 #endif
190 }
191 
192 PRIVATE_EXTERN int
bpf_set_batch_write(int fd,u_int value)193 bpf_set_batch_write(int fd, u_int value)
194 {
195 #ifdef BIOCSBATCHWRITE
196 	return ioctl(fd, BIOCSBATCHWRITE, &value);
197 #else
198 #pragma unused(fd, value)
199 	return ENOTSUP;
200 #endif
201 }
202 
203 PRIVATE_EXTERN int
bpf_get_batch_write(int fd,u_int * value)204 bpf_get_batch_write(int fd, u_int *value)
205 {
206 #ifdef BIOCGBATCHWRITE
207 	return ioctl(fd, BIOCGBATCHWRITE, value);
208 #else
209 #pragma unused(fd, value)
210 	return ENOTSUP;
211 #endif
212 }
213 
214 #ifdef TESTING
215 #include <net/if_arp.h>
216 #include <net/ethernet.h>
217 #include <netinet/if_ether.h>
218 
219 void
bpf_read_continuously(int fd,u_int blen)220 bpf_read_continuously(int fd, u_int blen)
221 {
222 	int n;
223 	char * rxbuf = malloc(blen);
224 
225 	printf("rx buf len is %d\n", blen);
226 	while (1) {
227 		n = read(fd, rxbuf, blen);
228 		if (n < 0) {
229 			perror("bpf_read_continuously");
230 			return;
231 		}
232 		if (n == 0) {
233 			continue;
234 		}
235 		print_data(rxbuf, n);
236 	}
237 }
238 
239 int
main(int argc,char * argv[])240 main(int argc, char * argv[])
241 {
242 	int fd = bpf_new();
243 	char * en_name = "en0";
244 	u_int bpf_blen = 0;
245 
246 	if (fd < 0) {
247 		perror("no bpf devices");
248 		exit(1);
249 	}
250 	bpf_set_traffic_class(SO_TC_CTL);
251 
252 	if (argc > 1) {
253 		en_name = argv[1];
254 	}
255 	(void)bpf_set_immediate(fd, 1);
256 	if (bpf_arp_filter(fd, 12, ETHERTYPE_ARP,
257 	    sizeof(struct ether_arp) + sizeof(struct ether_header))
258 	    < 0) {
259 		perror("bpf_arp_filter");
260 	}
261 	if (bpf_setif(fd, en_name) < 0) {
262 		perror("bpf_attach");
263 		exit(1);
264 	}
265 
266 	if (bpf_get_blen(fd, &bpf_blen) < 0) {
267 		perror("bpf_get_blen");
268 		exit(1);
269 	}
270 	bpf_read_continuously(fd, bpf_blen);
271 	exit(0);
272 	return 0;
273 }
274 #endif /* TESTING */
275