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