bpf.c revision 252620
1/* $OpenBSD: bpf.c,v 1.13 2004/05/05 14:28:58 deraadt Exp $ */ 2 3/* BPF socket interface code, originally contributed by Archie Cobbs. */ 4 5/* 6 * Copyright (c) 1995, 1996, 1998, 1999 7 * The Internet Software Consortium. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of The Internet Software Consortium nor the names 19 * of its contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * This software has been written for the Internet Software Consortium 37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 38 * Enterprises. To learn more about the Internet Software Consortium, 39 * see ``http://www.vix.com/isc''. To learn more about Vixie 40 * Enterprises, see ``http://www.vix.com''. 41 */ 42 43#include <sys/cdefs.h> 44__FBSDID("$FreeBSD: head/sbin/dhclient/bpf.c 252620 2013-07-03 22:01:52Z pjd $"); 45 46#include "dhcpd.h" 47#include <sys/ioctl.h> 48#include <sys/uio.h> 49 50#include <net/bpf.h> 51#include <netinet/in_systm.h> 52#include <netinet/ip.h> 53#include <netinet/udp.h> 54#include <netinet/if_ether.h> 55 56#define BPF_FORMAT "/dev/bpf%d" 57 58/* 59 * Called by get_interface_list for each interface that's discovered. 60 * Opens a packet filter for each interface and adds it to the select 61 * mask. 62 */ 63int 64if_register_bpf(struct interface_info *info, int flags) 65{ 66 char filename[50]; 67 int sock, b; 68 69 /* Open a BPF device */ 70 for (b = 0;; b++) { 71 snprintf(filename, sizeof(filename), BPF_FORMAT, b); 72 sock = open(filename, flags); 73 if (sock < 0) { 74 if (errno == EBUSY) 75 continue; 76 else 77 error("Can't find free bpf: %m"); 78 } else 79 break; 80 } 81 82 /* Set the BPF device to point at this interface. */ 83 if (ioctl(sock, BIOCSETIF, info->ifp) < 0) 84 error("Can't attach interface %s to bpf device %s: %m", 85 info->name, filename); 86 87 return (sock); 88} 89 90/* 91 * Packet write filter program: 92 * 'ip and udp and src port bootps and dst port (bootps or bootpc)' 93 */ 94struct bpf_insn dhcp_bpf_wfilter[] = { 95 BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14), 96 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12), 97 98 /* Make sure this is an IP packet... */ 99 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 100 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10), 101 102 /* Make sure it's a UDP packet... */ 103 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 104 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8), 105 106 /* Make sure this isn't a fragment... */ 107 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 108 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 6, 0), /* patched */ 109 110 /* Get the IP header length... */ 111 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 112 113 /* Make sure it's from the right port... */ 114 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14), 115 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 68, 0, 3), 116 117 /* Make sure it is to the right ports ... */ 118 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 119 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), 120 121 /* If we passed all the tests, ask for the whole packet. */ 122 BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 123 124 /* Otherwise, drop it. */ 125 BPF_STMT(BPF_RET+BPF_K, 0), 126}; 127 128int dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn); 129 130void 131if_register_send(struct interface_info *info) 132{ 133 struct bpf_version v; 134 struct bpf_program p; 135 int sock, on = 1; 136 137 /* Open a BPF device and hang it on this interface... */ 138 info->wfdesc = if_register_bpf(info, O_WRONLY); 139 140 /* Make sure the BPF version is in range... */ 141 if (ioctl(info->wfdesc, BIOCVERSION, &v) < 0) 142 error("Can't get BPF version: %m"); 143 144 if (v.bv_major != BPF_MAJOR_VERSION || 145 v.bv_minor < BPF_MINOR_VERSION) 146 error("Kernel BPF version out of range - recompile dhcpd!"); 147 148 /* Set up the bpf write filter program structure. */ 149 p.bf_len = dhcp_bpf_wfilter_len; 150 p.bf_insns = dhcp_bpf_wfilter; 151 152 if (dhcp_bpf_wfilter[7].k == 0x1fff) 153 dhcp_bpf_wfilter[7].k = htons(IP_MF|IP_OFFMASK); 154 155 if (ioctl(info->wfdesc, BIOCSETWF, &p) < 0) 156 error("Can't install write filter program: %m"); 157 158 if (ioctl(info->wfdesc, BIOCLOCK, NULL) < 0) 159 error("Cannot lock bpf"); 160 161 /* 162 * Use raw socket for unicast send. 163 */ 164 if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1) 165 error("socket(SOCK_RAW): %m"); 166 if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, 167 sizeof(on)) == -1) 168 error("setsockopt(IP_HDRINCL): %m"); 169 info->ufdesc = sock; 170} 171 172/* 173 * Packet filter program... 174 * 175 * XXX: Changes to the filter program may require changes to the 176 * constant offsets used in if_register_send to patch the BPF program! 177 */ 178struct bpf_insn dhcp_bpf_filter[] = { 179 /* Make sure this is an IP packet... */ 180 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 181 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8), 182 183 /* Make sure it's a UDP packet... */ 184 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 185 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6), 186 187 /* Make sure this isn't a fragment... */ 188 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 189 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0), 190 191 /* Get the IP header length... */ 192 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 193 194 /* Make sure it's to the right port... */ 195 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 196 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */ 197 198 /* If we passed all the tests, ask for the whole packet. */ 199 BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 200 201 /* Otherwise, drop it. */ 202 BPF_STMT(BPF_RET+BPF_K, 0), 203}; 204 205int dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn); 206 207void 208if_register_receive(struct interface_info *info) 209{ 210 struct bpf_version v; 211 struct bpf_program p; 212 int flag = 1, sz; 213 214 /* Open a BPF device and hang it on this interface... */ 215 info->rfdesc = if_register_bpf(info, O_RDONLY); 216 217 /* Make sure the BPF version is in range... */ 218 if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0) 219 error("Can't get BPF version: %m"); 220 221 if (v.bv_major != BPF_MAJOR_VERSION || 222 v.bv_minor < BPF_MINOR_VERSION) 223 error("Kernel BPF version out of range - recompile dhcpd!"); 224 225 /* 226 * Set immediate mode so that reads return as soon as a packet 227 * comes in, rather than waiting for the input buffer to fill 228 * with packets. 229 */ 230 if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0) 231 error("Can't set immediate mode on bpf device: %m"); 232 233 /* Get the required BPF buffer length from the kernel. */ 234 if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0) 235 error("Can't get bpf buffer length: %m"); 236 info->rbuf_max = sz; 237 info->rbuf = malloc(info->rbuf_max); 238 if (!info->rbuf) 239 error("Can't allocate %lu bytes for bpf input buffer.", 240 (unsigned long)info->rbuf_max); 241 info->rbuf_offset = 0; 242 info->rbuf_len = 0; 243 244 /* Set up the bpf filter program structure. */ 245 p.bf_len = dhcp_bpf_filter_len; 246 p.bf_insns = dhcp_bpf_filter; 247 248 /* Patch the server port into the BPF program... 249 * 250 * XXX: changes to filter program may require changes to the 251 * insn number(s) used below! 252 */ 253 dhcp_bpf_filter[8].k = LOCAL_PORT; 254 255 if (ioctl(info->rfdesc, BIOCSETF, &p) < 0) 256 error("Can't install packet filter program: %m"); 257 258 if (ioctl(info->rfdesc, BIOCLOCK, NULL) < 0) 259 error("Cannot lock bpf"); 260} 261 262void 263send_packet(struct interface_info *interface, struct dhcp_packet *raw, 264 size_t len, struct in_addr from, struct in_addr to) 265{ 266 unsigned char buf[256]; 267 struct iovec iov[2]; 268 struct msghdr msg; 269 int result, bufp = 0; 270 271 /* Assemble the headers... */ 272 if (to.s_addr == INADDR_BROADCAST) 273 assemble_hw_header(interface, buf, &bufp); 274 assemble_udp_ip_header(buf, &bufp, from.s_addr, to.s_addr, 275 htons(REMOTE_PORT), (unsigned char *)raw, len); 276 277 iov[0].iov_base = buf; 278 iov[0].iov_len = bufp; 279 iov[1].iov_base = raw; 280 iov[1].iov_len = len; 281 282 /* Fire it off */ 283 if (to.s_addr == INADDR_BROADCAST) 284 result = writev(interface->wfdesc, iov, 2); 285 else { 286 struct sockaddr_in sato; 287 288 sato.sin_addr = to; 289 sato.sin_port = htons(REMOTE_PORT); 290 sato.sin_family = AF_INET; 291 sato.sin_len = sizeof(sato); 292 293 memset(&msg, 0, sizeof(msg)); 294 msg.msg_name = (struct sockaddr *)&sato; 295 msg.msg_namelen = sizeof(sato); 296 msg.msg_iov = iov; 297 msg.msg_iovlen = 2; 298 result = sendmsg(interface->ufdesc, &msg, 0); 299 } 300 301 if (result < 0) 302 warning("send_packet: %m"); 303} 304 305ssize_t 306receive_packet(struct interface_info *interface, unsigned char *buf, 307 size_t len, struct sockaddr_in *from, struct hardware *hfrom) 308{ 309 int length = 0, offset = 0; 310 struct bpf_hdr hdr; 311 312 /* 313 * All this complexity is because BPF doesn't guarantee that 314 * only one packet will be returned at a time. We're getting 315 * what we deserve, though - this is a terrible abuse of the BPF 316 * interface. Sigh. 317 */ 318 319 /* Process packets until we get one we can return or until we've 320 * done a read and gotten nothing we can return... 321 */ 322 do { 323 /* If the buffer is empty, fill it. */ 324 if (interface->rbuf_offset >= interface->rbuf_len) { 325 length = read(interface->rfdesc, interface->rbuf, 326 interface->rbuf_max); 327 if (length <= 0) 328 return (length); 329 interface->rbuf_offset = 0; 330 interface->rbuf_len = length; 331 } 332 333 /* 334 * If there isn't room for a whole bpf header, something 335 * went wrong, but we'll ignore it and hope it goes 336 * away... XXX 337 */ 338 if (interface->rbuf_len - interface->rbuf_offset < 339 sizeof(hdr)) { 340 interface->rbuf_offset = interface->rbuf_len; 341 continue; 342 } 343 344 /* Copy out a bpf header... */ 345 memcpy(&hdr, &interface->rbuf[interface->rbuf_offset], 346 sizeof(hdr)); 347 348 /* 349 * If the bpf header plus data doesn't fit in what's 350 * left of the buffer, stick head in sand yet again... 351 */ 352 if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen > 353 interface->rbuf_len) { 354 interface->rbuf_offset = interface->rbuf_len; 355 continue; 356 } 357 358 /* Skip over the BPF header... */ 359 interface->rbuf_offset += hdr.bh_hdrlen; 360 361 /* 362 * If the captured data wasn't the whole packet, or if 363 * the packet won't fit in the input buffer, all we can 364 * do is drop it. 365 */ 366 if (hdr.bh_caplen != hdr.bh_datalen) { 367 interface->rbuf_offset = 368 BPF_WORDALIGN(interface->rbuf_offset + 369 hdr.bh_caplen); 370 continue; 371 } 372 373 /* Decode the physical header... */ 374 offset = decode_hw_header(interface->rbuf, 375 interface->rbuf_offset, hfrom); 376 377 /* 378 * If a physical layer checksum failed (dunno of any 379 * physical layer that supports this, but WTH), skip 380 * this packet. 381 */ 382 if (offset < 0) { 383 interface->rbuf_offset = 384 BPF_WORDALIGN(interface->rbuf_offset + 385 hdr.bh_caplen); 386 continue; 387 } 388 interface->rbuf_offset += offset; 389 hdr.bh_caplen -= offset; 390 391 /* Decode the IP and UDP headers... */ 392 offset = decode_udp_ip_header(interface->rbuf, 393 interface->rbuf_offset, from, NULL, hdr.bh_caplen); 394 395 /* If the IP or UDP checksum was bad, skip the packet... */ 396 if (offset < 0) { 397 interface->rbuf_offset = 398 BPF_WORDALIGN(interface->rbuf_offset + 399 hdr.bh_caplen); 400 continue; 401 } 402 interface->rbuf_offset += offset; 403 hdr.bh_caplen -= offset; 404 405 /* 406 * If there's not enough room to stash the packet data, 407 * we have to skip it (this shouldn't happen in real 408 * life, though). 409 */ 410 if (hdr.bh_caplen > len) { 411 interface->rbuf_offset = 412 BPF_WORDALIGN(interface->rbuf_offset + 413 hdr.bh_caplen); 414 continue; 415 } 416 417 /* Copy out the data in the packet... */ 418 memcpy(buf, interface->rbuf + interface->rbuf_offset, 419 hdr.bh_caplen); 420 interface->rbuf_offset = 421 BPF_WORDALIGN(interface->rbuf_offset + 422 hdr.bh_caplen); 423 return (hdr.bh_caplen); 424 } while (!length); 425 return (0); 426} 427