bpf.c revision 147073
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 "dhcpd.h" 44#include <sys/ioctl.h> 45#include <sys/uio.h> 46 47#include <net/bpf.h> 48#include <netinet/in_systm.h> 49#include <netinet/ip.h> 50#include <netinet/udp.h> 51#include <netinet/if_ether.h> 52 53#define BPF_FORMAT "/dev/bpf%d" 54 55/* 56 * Called by get_interface_list for each interface that's discovered. 57 * Opens a packet filter for each interface and adds it to the select 58 * mask. 59 */ 60int 61if_register_bpf(struct interface_info *info) 62{ 63 char filename[50]; 64 int sock, b; 65 66 /* Open a BPF device */ 67 for (b = 0; 1; b++) { 68 snprintf(filename, sizeof(filename), BPF_FORMAT, b); 69 sock = open(filename, O_RDWR, 0); 70 if (sock < 0) { 71 if (errno == EBUSY) 72 continue; 73 else 74 error("Can't find free bpf: %m"); 75 } else 76 break; 77 } 78 79 /* Set the BPF device to point at this interface. */ 80 if (ioctl(sock, BIOCSETIF, info->ifp) < 0) 81 error("Can't attach interface %s to bpf device %s: %m", 82 info->name, filename); 83 84 return (sock); 85} 86 87void 88if_register_send(struct interface_info *info) 89{ 90 /* 91 * If we're using the bpf API for sending and receiving, we 92 * don't need to register this interface twice. 93 */ 94 info->wfdesc = info->rfdesc; 95} 96 97/* 98 * Packet filter program... 99 * 100 * XXX: Changes to the filter program may require changes to the 101 * constant offsets used in if_register_send to patch the BPF program! 102 */ 103struct bpf_insn dhcp_bpf_filter[] = { 104 /* Make sure this is an IP packet... */ 105 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 106 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8), 107 108 /* Make sure it's a UDP packet... */ 109 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 110 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6), 111 112 /* Make sure this isn't a fragment... */ 113 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 114 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0), 115 116 /* Get the IP header length... */ 117 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 118 119 /* Make sure it's to the right port... */ 120 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 121 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */ 122 123 /* If we passed all the tests, ask for the whole packet. */ 124 BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 125 126 /* Otherwise, drop it. */ 127 BPF_STMT(BPF_RET+BPF_K, 0), 128}; 129 130int dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn); 131 132/* 133 * Packet write filter program: 134 * 'ip and udp and src port bootps and dst port (bootps or bootpc)' 135 */ 136struct bpf_insn dhcp_bpf_wfilter[] = { 137 BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14), 138 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12), 139 140 /* Make sure this is an IP packet... */ 141 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 142 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10), 143 144 /* Make sure it's a UDP packet... */ 145 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 146 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8), 147 148 /* Make sure this isn't a fragment... */ 149 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 150 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 6, 0), /* patched */ 151 152 /* Get the IP header length... */ 153 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 154 155 /* Make sure it's from the right port... */ 156 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14), 157 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 68, 0, 3), 158 159 /* Make sure it is to the right ports ... */ 160 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 161 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), 162 163 /* If we passed all the tests, ask for the whole packet. */ 164 BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 165 166 /* Otherwise, drop it. */ 167 BPF_STMT(BPF_RET+BPF_K, 0), 168}; 169 170int dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn); 171 172void 173if_register_receive(struct interface_info *info) 174{ 175 struct bpf_version v; 176 struct bpf_program p; 177 int flag = 1, sz; 178 179 /* Open a BPF device and hang it on this interface... */ 180 info->rfdesc = if_register_bpf(info); 181 182 /* Make sure the BPF version is in range... */ 183 if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0) 184 error("Can't get BPF version: %m"); 185 186 if (v.bv_major != BPF_MAJOR_VERSION || 187 v.bv_minor < BPF_MINOR_VERSION) 188 error("Kernel BPF version out of range - recompile dhcpd!"); 189 190 /* 191 * Set immediate mode so that reads return as soon as a packet 192 * comes in, rather than waiting for the input buffer to fill 193 * with packets. 194 */ 195 if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0) 196 error("Can't set immediate mode on bpf device: %m"); 197 198 /* Get the required BPF buffer length from the kernel. */ 199 if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0) 200 error("Can't get bpf buffer length: %m"); 201 info->rbuf_max = sz; 202 info->rbuf = malloc(info->rbuf_max); 203 if (!info->rbuf) 204 error("Can't allocate %lu bytes for bpf input buffer.", 205 (unsigned long)info->rbuf_max); 206 info->rbuf_offset = 0; 207 info->rbuf_len = 0; 208 209 /* Set up the bpf filter program structure. */ 210 p.bf_len = dhcp_bpf_filter_len; 211 p.bf_insns = dhcp_bpf_filter; 212 213 /* Patch the server port into the BPF program... 214 * 215 * XXX: changes to filter program may require changes to the 216 * insn number(s) used below! 217 */ 218 dhcp_bpf_filter[8].k = LOCAL_PORT; 219 220 if (ioctl(info->rfdesc, BIOCSETF, &p) < 0) 221 error("Can't install packet filter program: %m"); 222 223 /* Set up the bpf write filter program structure. */ 224 p.bf_len = dhcp_bpf_wfilter_len; 225 p.bf_insns = dhcp_bpf_wfilter; 226 227 if (dhcp_bpf_wfilter[7].k == 0x1fff) 228 dhcp_bpf_wfilter[7].k = htons(IP_MF|IP_OFFMASK); 229 230 if (ioctl(info->rfdesc, BIOCSETWF, &p) < 0) 231 error("Can't install write filter program: %m"); 232 233 if (ioctl(info->rfdesc, BIOCLOCK, NULL) < 0) 234 error("Cannot lock bpf"); 235} 236 237ssize_t 238send_packet(struct interface_info *interface, struct dhcp_packet *raw, 239 size_t len, struct in_addr from, struct sockaddr_in *to, 240 struct hardware *hto) 241{ 242 unsigned char buf[256]; 243 struct iovec iov[2]; 244 int result, bufp = 0; 245 246 /* Assemble the headers... */ 247 assemble_hw_header(interface, buf, &bufp, hto); 248 assemble_udp_ip_header(buf, &bufp, from.s_addr, 249 to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len); 250 251 /* Fire it off */ 252 iov[0].iov_base = (char *)buf; 253 iov[0].iov_len = bufp; 254 iov[1].iov_base = (char *)raw; 255 iov[1].iov_len = len; 256 257 result = writev(interface->wfdesc, iov, 2); 258 if (result < 0) 259 warning("send_packet: %m"); 260 return (result); 261} 262 263ssize_t 264receive_packet(struct interface_info *interface, unsigned char *buf, 265 size_t len, struct sockaddr_in *from, struct hardware *hfrom) 266{ 267 int length = 0, offset = 0; 268 struct bpf_hdr hdr; 269 270 /* 271 * All this complexity is because BPF doesn't guarantee that 272 * only one packet will be returned at a time. We're getting 273 * what we deserve, though - this is a terrible abuse of the BPF 274 * interface. Sigh. 275 */ 276 277 /* Process packets until we get one we can return or until we've 278 * done a read and gotten nothing we can return... 279 */ 280 do { 281 /* If the buffer is empty, fill it. */ 282 if (interface->rbuf_offset == interface->rbuf_len) { 283 length = read(interface->rfdesc, interface->rbuf, 284 interface->rbuf_max); 285 if (length <= 0) 286 return (length); 287 interface->rbuf_offset = 0; 288 interface->rbuf_len = length; 289 } 290 291 /* 292 * If there isn't room for a whole bpf header, something 293 * went wrong, but we'll ignore it and hope it goes 294 * away... XXX 295 */ 296 if (interface->rbuf_len - interface->rbuf_offset < 297 sizeof(hdr)) { 298 interface->rbuf_offset = interface->rbuf_len; 299 continue; 300 } 301 302 /* Copy out a bpf header... */ 303 memcpy(&hdr, &interface->rbuf[interface->rbuf_offset], 304 sizeof(hdr)); 305 306 /* 307 * If the bpf header plus data doesn't fit in what's 308 * left of the buffer, stick head in sand yet again... 309 */ 310 if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen > 311 interface->rbuf_len) { 312 interface->rbuf_offset = interface->rbuf_len; 313 continue; 314 } 315 316 /* 317 * If the captured data wasn't the whole packet, or if 318 * the packet won't fit in the input buffer, all we can 319 * do is drop it. 320 */ 321 if (hdr.bh_caplen != hdr.bh_datalen) { 322 interface->rbuf_offset += hdr.bh_hdrlen = hdr.bh_caplen; 323 continue; 324 } 325 326 /* Skip over the BPF header... */ 327 interface->rbuf_offset += hdr.bh_hdrlen; 328 329 /* Decode the physical header... */ 330 offset = decode_hw_header(interface->rbuf, 331 interface->rbuf_offset, hfrom); 332 333 /* 334 * If a physical layer checksum failed (dunno of any 335 * physical layer that supports this, but WTH), skip 336 * this packet. 337 */ 338 if (offset < 0) { 339 interface->rbuf_offset += hdr.bh_caplen; 340 continue; 341 } 342 interface->rbuf_offset += offset; 343 hdr.bh_caplen -= offset; 344 345 /* Decode the IP and UDP headers... */ 346 offset = decode_udp_ip_header(interface->rbuf, 347 interface->rbuf_offset, from, NULL, hdr.bh_caplen); 348 349 /* If the IP or UDP checksum was bad, skip the packet... */ 350 if (offset < 0) { 351 interface->rbuf_offset += hdr.bh_caplen; 352 continue; 353 } 354 interface->rbuf_offset += offset; 355 hdr.bh_caplen -= offset; 356 357 /* 358 * If there's not enough room to stash the packet data, 359 * we have to skip it (this shouldn't happen in real 360 * life, though). 361 */ 362 if (hdr.bh_caplen > len) { 363 interface->rbuf_offset += hdr.bh_caplen; 364 continue; 365 } 366 367 /* Copy out the data in the packet... */ 368 memcpy(buf, interface->rbuf + interface->rbuf_offset, 369 hdr.bh_caplen); 370 interface->rbuf_offset += hdr.bh_caplen; 371 return (hdr.bh_caplen); 372 } while (!length); 373 return (0); 374} 375