xref: /xnu-8796.141.3/tests/bpflib.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
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_header_complete(int fd,u_int header_complete)59 bpf_set_header_complete(int fd, u_int header_complete)
60 {
61 	return ioctl(fd, BIOCSHDRCMPLT, &header_complete);
62 }
63 
64 PRIVATE_EXTERN int
bpf_set_see_sent(int fd,u_int see_sent)65 bpf_set_see_sent(int fd, u_int see_sent)
66 {
67 	return ioctl(fd, BIOCSSEESENT, &see_sent);
68 }
69 
70 PRIVATE_EXTERN int
bpf_dispose(int bpf_fd)71 bpf_dispose(int bpf_fd)
72 {
73 	if (bpf_fd >= 0) {
74 		return close(bpf_fd);
75 	}
76 	return 0;
77 }
78 
79 PRIVATE_EXTERN int
bpf_new(void)80 bpf_new(void)
81 {
82 	char bpfdev[256];
83 	int i;
84 	int fd = -1;
85 
86 	for (i = 0; true; i++) {
87 		snprintf(bpfdev, sizeof(bpfdev), "/dev/bpf%d", i);
88 		fd = open(bpfdev, O_RDWR, 0);
89 		if (fd >= 0) {
90 #ifdef SO_TC_CTL
91 			int tc = SO_TC_CTL;
92 			(void) ioctl(fd, BIOCSETTC, &tc);
93 #endif /* SO_TC_CTL */
94 			break;
95 		}
96 		if (errno != EBUSY) {
97 			break;
98 		}
99 	}
100 	return fd;
101 }
102 
103 PRIVATE_EXTERN int
bpf_setif(int fd,const char * en_name)104 bpf_setif(int fd, const char * en_name)
105 {
106 	struct ifreq ifr;
107 
108 	strlcpy(ifr.ifr_name, en_name, sizeof(ifr.ifr_name));
109 	return ioctl(fd, BIOCSETIF, &ifr);
110 }
111 
112 PRIVATE_EXTERN int
bpf_set_immediate(int fd,u_int value)113 bpf_set_immediate(int fd, u_int value)
114 {
115 	return ioctl(fd, BIOCIMMEDIATE, &value);
116 }
117 
118 PRIVATE_EXTERN int
bpf_filter_receive_none(int fd)119 bpf_filter_receive_none(int fd)
120 {
121 	struct bpf_insn insns[] = {
122 		BPF_STMT(BPF_RET + BPF_K, 0),
123 	};
124 	struct bpf_program prog;
125 
126 	prog.bf_len = sizeof(insns) / sizeof(struct bpf_insn);
127 	prog.bf_insns = insns;
128 	return ioctl(fd, BIOCSETF, &prog);
129 }
130 
131 PRIVATE_EXTERN int
bpf_arp_filter(int fd,int type_offset,int type,u_int pkt_size)132 bpf_arp_filter(int fd, int type_offset, int type, u_int pkt_size)
133 {
134 	struct bpf_insn insns[] = {
135 		BPF_STMT(BPF_LD + BPF_H + BPF_ABS, type_offset),
136 		BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, type, 0, 1),
137 		BPF_STMT(BPF_RET + BPF_K, pkt_size),
138 		BPF_STMT(BPF_RET + BPF_K, 0),
139 	};
140 	struct bpf_program prog;
141 
142 	prog.bf_len = sizeof(insns) / sizeof(struct bpf_insn);
143 	prog.bf_insns = insns;
144 	return ioctl(fd, BIOCSETF, &prog);
145 }
146 
147 #ifdef TESTING
148 #include <net/if_arp.h>
149 #include <net/ethernet.h>
150 #include <netinet/if_ether.h>
151 
152 
153 void
bpf_read_continuously(int fd,u_int blen)154 bpf_read_continuously(int fd, u_int blen)
155 {
156 	int n;
157 	char * rxbuf = malloc(blen);
158 
159 	printf("rx buf len is %d\n", blen);
160 	while (1) {
161 		n = read(fd, rxbuf, blen);
162 		if (n < 0) {
163 			perror("bpf_read_continuously");
164 			return;
165 		}
166 		if (n == 0) {
167 			continue;
168 		}
169 		print_data(rxbuf, n);
170 	}
171 }
172 
173 int
main(int argc,char * argv[])174 main(int argc, char * argv[])
175 {
176 	int fd = bpf_new();
177 	char * en_name = "en0";
178 	u_int bpf_blen = 0;
179 
180 	if (fd < 0) {
181 		perror("no bpf devices");
182 		exit(1);
183 	}
184 
185 	if (argc > 1) {
186 		en_name = argv[1];
187 	}
188 	(void)bpf_set_immediate(fd, 1);
189 	if (bpf_arp_filter(fd, 12, ETHERTYPE_ARP,
190 	    sizeof(struct ether_arp) + sizeof(struct ether_header))
191 	    < 0) {
192 		perror("bpf_arp_filter");
193 	}
194 	if (bpf_setif(fd, en_name) < 0) {
195 		perror("bpf_attach");
196 		exit(1);
197 	}
198 
199 	if (bpf_get_blen(fd, &bpf_blen) < 0) {
200 		perror("bpf_get_blen");
201 		exit(1);
202 	}
203 	bpf_read_continuously(fd, bpf_blen);
204 	exit(0);
205 	return 0;
206 }
207 #endif /* TESTING */
208