xref: /xnu-10002.1.13/tests/bpflib.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
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 #ifdef TESTING
171 #include <net/if_arp.h>
172 #include <net/ethernet.h>
173 #include <netinet/if_ether.h>
174 
175 void
bpf_read_continuously(int fd,u_int blen)176 bpf_read_continuously(int fd, u_int blen)
177 {
178 	int n;
179 	char * rxbuf = malloc(blen);
180 
181 	printf("rx buf len is %d\n", blen);
182 	while (1) {
183 		n = read(fd, rxbuf, blen);
184 		if (n < 0) {
185 			perror("bpf_read_continuously");
186 			return;
187 		}
188 		if (n == 0) {
189 			continue;
190 		}
191 		print_data(rxbuf, n);
192 	}
193 }
194 
195 int
main(int argc,char * argv[])196 main(int argc, char * argv[])
197 {
198 	int fd = bpf_new();
199 	char * en_name = "en0";
200 	u_int bpf_blen = 0;
201 
202 	if (fd < 0) {
203 		perror("no bpf devices");
204 		exit(1);
205 	}
206 	bpf_set_traffic_class(SO_TC_CTL);
207 
208 	if (argc > 1) {
209 		en_name = argv[1];
210 	}
211 	(void)bpf_set_immediate(fd, 1);
212 	if (bpf_arp_filter(fd, 12, ETHERTYPE_ARP,
213 	    sizeof(struct ether_arp) + sizeof(struct ether_header))
214 	    < 0) {
215 		perror("bpf_arp_filter");
216 	}
217 	if (bpf_setif(fd, en_name) < 0) {
218 		perror("bpf_attach");
219 		exit(1);
220 	}
221 
222 	if (bpf_get_blen(fd, &bpf_blen) < 0) {
223 		perror("bpf_get_blen");
224 		exit(1);
225 	}
226 	bpf_read_continuously(fd, bpf_blen);
227 	exit(0);
228 	return 0;
229 }
230 #endif /* TESTING */
231