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