bpf.c revision 147072
1147072Sbrooks/* $OpenBSD: bpf.c,v 1.13 2004/05/05 14:28:58 deraadt Exp $ */ 2147072Sbrooks 3147072Sbrooks/* BPF socket interface code, originally contributed by Archie Cobbs. */ 4147072Sbrooks 5147072Sbrooks/* 6147072Sbrooks * Copyright (c) 1995, 1996, 1998, 1999 7147072Sbrooks * The Internet Software Consortium. All rights reserved. 8147072Sbrooks * 9147072Sbrooks * Redistribution and use in source and binary forms, with or without 10147072Sbrooks * modification, are permitted provided that the following conditions 11147072Sbrooks * are met: 12147072Sbrooks * 13147072Sbrooks * 1. Redistributions of source code must retain the above copyright 14147072Sbrooks * notice, this list of conditions and the following disclaimer. 15147072Sbrooks * 2. Redistributions in binary form must reproduce the above copyright 16147072Sbrooks * notice, this list of conditions and the following disclaimer in the 17147072Sbrooks * documentation and/or other materials provided with the distribution. 18147072Sbrooks * 3. Neither the name of The Internet Software Consortium nor the names 19147072Sbrooks * of its contributors may be used to endorse or promote products derived 20147072Sbrooks * from this software without specific prior written permission. 21147072Sbrooks * 22147072Sbrooks * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 23147072Sbrooks * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24147072Sbrooks * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25147072Sbrooks * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26147072Sbrooks * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 27147072Sbrooks * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28147072Sbrooks * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29147072Sbrooks * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 30147072Sbrooks * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31147072Sbrooks * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32147072Sbrooks * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33147072Sbrooks * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34147072Sbrooks * SUCH DAMAGE. 35147072Sbrooks * 36147072Sbrooks * This software has been written for the Internet Software Consortium 37147072Sbrooks * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 38147072Sbrooks * Enterprises. To learn more about the Internet Software Consortium, 39147072Sbrooks * see ``http://www.vix.com/isc''. To learn more about Vixie 40147072Sbrooks * Enterprises, see ``http://www.vix.com''. 41147072Sbrooks */ 42147072Sbrooks 43147072Sbrooks#include "dhcpd.h" 44147072Sbrooks#include <sys/ioctl.h> 45147072Sbrooks#include <sys/uio.h> 46147072Sbrooks 47147072Sbrooks#include <net/bpf.h> 48147072Sbrooks#include <netinet/in_systm.h> 49147072Sbrooks#include <netinet/ip.h> 50147072Sbrooks#include <netinet/udp.h> 51147072Sbrooks#include <netinet/if_ether.h> 52147072Sbrooks 53147072Sbrooks#define BPF_FORMAT "/dev/bpf%d" 54147072Sbrooks 55147072Sbrooks/* 56147072Sbrooks * Called by get_interface_list for each interface that's discovered. 57147072Sbrooks * Opens a packet filter for each interface and adds it to the select 58147072Sbrooks * mask. 59147072Sbrooks */ 60147072Sbrooksint 61147072Sbrooksif_register_bpf(struct interface_info *info) 62147072Sbrooks{ 63147072Sbrooks char filename[50]; 64147072Sbrooks int sock, b; 65147072Sbrooks 66147072Sbrooks /* Open a BPF device */ 67147072Sbrooks for (b = 0; 1; b++) { 68147072Sbrooks snprintf(filename, sizeof(filename), BPF_FORMAT, b); 69147072Sbrooks sock = open(filename, O_RDWR, 0); 70147072Sbrooks if (sock < 0) { 71147072Sbrooks if (errno == EBUSY) 72147072Sbrooks continue; 73147072Sbrooks else 74147072Sbrooks error("Can't find free bpf: %m"); 75147072Sbrooks } else 76147072Sbrooks break; 77147072Sbrooks } 78147072Sbrooks 79147072Sbrooks /* Set the BPF device to point at this interface. */ 80147072Sbrooks if (ioctl(sock, BIOCSETIF, info->ifp) < 0) 81147072Sbrooks error("Can't attach interface %s to bpf device %s: %m", 82147072Sbrooks info->name, filename); 83147072Sbrooks 84147072Sbrooks return (sock); 85147072Sbrooks} 86147072Sbrooks 87147072Sbrooksvoid 88147072Sbrooksif_register_send(struct interface_info *info) 89147072Sbrooks{ 90147072Sbrooks /* 91147072Sbrooks * If we're using the bpf API for sending and receiving, we 92147072Sbrooks * don't need to register this interface twice. 93147072Sbrooks */ 94147072Sbrooks info->wfdesc = info->rfdesc; 95147072Sbrooks} 96147072Sbrooks 97147072Sbrooks/* 98147072Sbrooks * Packet filter program... 99147072Sbrooks * 100147072Sbrooks * XXX: Changes to the filter program may require changes to the 101147072Sbrooks * constant offsets used in if_register_send to patch the BPF program! 102147072Sbrooks */ 103147072Sbrooksstruct bpf_insn dhcp_bpf_filter[] = { 104147072Sbrooks /* Make sure this is an IP packet... */ 105147072Sbrooks BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 106147072Sbrooks BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8), 107147072Sbrooks 108147072Sbrooks /* Make sure it's a UDP packet... */ 109147072Sbrooks BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 110147072Sbrooks BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6), 111147072Sbrooks 112147072Sbrooks /* Make sure this isn't a fragment... */ 113147072Sbrooks BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 114147072Sbrooks BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0), 115147072Sbrooks 116147072Sbrooks /* Get the IP header length... */ 117147072Sbrooks BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 118147072Sbrooks 119147072Sbrooks /* Make sure it's to the right port... */ 120147072Sbrooks BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 121147072Sbrooks BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */ 122147072Sbrooks 123147072Sbrooks /* If we passed all the tests, ask for the whole packet. */ 124147072Sbrooks BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 125147072Sbrooks 126147072Sbrooks /* Otherwise, drop it. */ 127147072Sbrooks BPF_STMT(BPF_RET+BPF_K, 0), 128147072Sbrooks}; 129147072Sbrooks 130147072Sbrooksint dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn); 131147072Sbrooks 132147072Sbrooks/* 133147072Sbrooks * Packet write filter program: 134147072Sbrooks * 'ip and udp and src port bootps and dst port (bootps or bootpc)' 135147072Sbrooks */ 136147072Sbrooksstruct bpf_insn dhcp_bpf_wfilter[] = { 137147072Sbrooks BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14), 138147072Sbrooks BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12), 139147072Sbrooks 140147072Sbrooks /* Make sure this is an IP packet... */ 141147072Sbrooks BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 142147072Sbrooks BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10), 143147072Sbrooks 144147072Sbrooks /* Make sure it's a UDP packet... */ 145147072Sbrooks BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 146147072Sbrooks BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8), 147147072Sbrooks 148147072Sbrooks /* Make sure this isn't a fragment... */ 149147072Sbrooks BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 150147072Sbrooks BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 6, 0), /* patched */ 151147072Sbrooks 152147072Sbrooks /* Get the IP header length... */ 153147072Sbrooks BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 154147072Sbrooks 155147072Sbrooks /* Make sure it's from the right port... */ 156147072Sbrooks BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14), 157147072Sbrooks BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 68, 0, 3), 158147072Sbrooks 159147072Sbrooks /* Make sure it is to the right ports ... */ 160147072Sbrooks BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 161147072Sbrooks BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), 162147072Sbrooks 163147072Sbrooks /* If we passed all the tests, ask for the whole packet. */ 164147072Sbrooks BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 165147072Sbrooks 166147072Sbrooks /* Otherwise, drop it. */ 167147072Sbrooks BPF_STMT(BPF_RET+BPF_K, 0), 168147072Sbrooks}; 169147072Sbrooks 170147072Sbrooksint dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn); 171147072Sbrooks 172147072Sbrooksvoid 173147072Sbrooksif_register_receive(struct interface_info *info) 174147072Sbrooks{ 175147072Sbrooks struct bpf_version v; 176147072Sbrooks struct bpf_program p; 177147072Sbrooks int flag = 1, sz; 178147072Sbrooks 179147072Sbrooks /* Open a BPF device and hang it on this interface... */ 180147072Sbrooks info->rfdesc = if_register_bpf(info); 181147072Sbrooks 182147072Sbrooks /* Make sure the BPF version is in range... */ 183147072Sbrooks if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0) 184147072Sbrooks error("Can't get BPF version: %m"); 185147072Sbrooks 186147072Sbrooks if (v.bv_major != BPF_MAJOR_VERSION || 187147072Sbrooks v.bv_minor < BPF_MINOR_VERSION) 188147072Sbrooks error("Kernel BPF version out of range - recompile dhcpd!"); 189147072Sbrooks 190147072Sbrooks /* 191147072Sbrooks * Set immediate mode so that reads return as soon as a packet 192147072Sbrooks * comes in, rather than waiting for the input buffer to fill 193147072Sbrooks * with packets. 194147072Sbrooks */ 195147072Sbrooks if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0) 196147072Sbrooks error("Can't set immediate mode on bpf device: %m"); 197147072Sbrooks 198147072Sbrooks /* Get the required BPF buffer length from the kernel. */ 199147072Sbrooks if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0) 200147072Sbrooks error("Can't get bpf buffer length: %m"); 201147072Sbrooks info->rbuf_max = sz; 202147072Sbrooks info->rbuf = malloc(info->rbuf_max); 203147072Sbrooks if (!info->rbuf) 204147072Sbrooks error("Can't allocate %lu bytes for bpf input buffer.", 205147072Sbrooks (unsigned long)info->rbuf_max); 206147072Sbrooks info->rbuf_offset = 0; 207147072Sbrooks info->rbuf_len = 0; 208147072Sbrooks 209147072Sbrooks /* Set up the bpf filter program structure. */ 210147072Sbrooks p.bf_len = dhcp_bpf_filter_len; 211147072Sbrooks p.bf_insns = dhcp_bpf_filter; 212147072Sbrooks 213147072Sbrooks /* Patch the server port into the BPF program... 214147072Sbrooks * 215147072Sbrooks * XXX: changes to filter program may require changes to the 216147072Sbrooks * insn number(s) used below! 217147072Sbrooks */ 218147072Sbrooks dhcp_bpf_filter[8].k = LOCAL_PORT; 219147072Sbrooks 220147072Sbrooks if (ioctl(info->rfdesc, BIOCSETF, &p) < 0) 221147072Sbrooks error("Can't install packet filter program: %m"); 222147072Sbrooks 223147072Sbrooks /* Set up the bpf write filter program structure. */ 224147072Sbrooks p.bf_len = dhcp_bpf_wfilter_len; 225147072Sbrooks p.bf_insns = dhcp_bpf_wfilter; 226147072Sbrooks 227147072Sbrooks if (dhcp_bpf_wfilter[7].k == 0x1fff) 228147072Sbrooks dhcp_bpf_wfilter[7].k = htons(IP_MF|IP_OFFMASK); 229147072Sbrooks 230147072Sbrooks if (ioctl(info->rfdesc, BIOCSETWF, &p) < 0) 231147072Sbrooks error("Can't install write filter program: %m"); 232147072Sbrooks 233147072Sbrooks if (ioctl(info->rfdesc, BIOCLOCK, NULL) < 0) 234147072Sbrooks error("Cannot lock bpf"); 235147072Sbrooks} 236147072Sbrooks 237147072Sbrooksssize_t 238147072Sbrookssend_packet(struct interface_info *interface, struct dhcp_packet *raw, 239147072Sbrooks size_t len, struct in_addr from, struct sockaddr_in *to, 240147072Sbrooks struct hardware *hto) 241147072Sbrooks{ 242147072Sbrooks unsigned char buf[256]; 243147072Sbrooks struct iovec iov[2]; 244147072Sbrooks int result, bufp = 0; 245147072Sbrooks 246147072Sbrooks /* Assemble the headers... */ 247147072Sbrooks assemble_hw_header(interface, buf, &bufp, hto); 248147072Sbrooks assemble_udp_ip_header(buf, &bufp, from.s_addr, 249147072Sbrooks to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len); 250147072Sbrooks 251147072Sbrooks /* Fire it off */ 252147072Sbrooks iov[0].iov_base = (char *)buf; 253147072Sbrooks iov[0].iov_len = bufp; 254147072Sbrooks iov[1].iov_base = (char *)raw; 255147072Sbrooks iov[1].iov_len = len; 256147072Sbrooks 257147072Sbrooks result = writev(interface->wfdesc, iov, 2); 258147072Sbrooks if (result < 0) 259147072Sbrooks warning("send_packet: %m"); 260147072Sbrooks return (result); 261147072Sbrooks} 262147072Sbrooks 263147072Sbrooksssize_t 264147072Sbrooksreceive_packet(struct interface_info *interface, unsigned char *buf, 265147072Sbrooks size_t len, struct sockaddr_in *from, struct hardware *hfrom) 266147072Sbrooks{ 267147072Sbrooks int length = 0, offset = 0; 268147072Sbrooks struct bpf_hdr hdr; 269147072Sbrooks 270147072Sbrooks /* 271147072Sbrooks * All this complexity is because BPF doesn't guarantee that 272147072Sbrooks * only one packet will be returned at a time. We're getting 273147072Sbrooks * what we deserve, though - this is a terrible abuse of the BPF 274147072Sbrooks * interface. Sigh. 275147072Sbrooks */ 276147072Sbrooks 277147072Sbrooks /* Process packets until we get one we can return or until we've 278147072Sbrooks * done a read and gotten nothing we can return... 279147072Sbrooks */ 280147072Sbrooks do { 281147072Sbrooks /* If the buffer is empty, fill it. */ 282147072Sbrooks if (interface->rbuf_offset == interface->rbuf_len) { 283147072Sbrooks length = read(interface->rfdesc, interface->rbuf, 284147072Sbrooks interface->rbuf_max); 285147072Sbrooks if (length <= 0) 286147072Sbrooks return (length); 287147072Sbrooks interface->rbuf_offset = 0; 288147072Sbrooks interface->rbuf_len = length; 289147072Sbrooks } 290147072Sbrooks 291147072Sbrooks /* 292147072Sbrooks * If there isn't room for a whole bpf header, something 293147072Sbrooks * went wrong, but we'll ignore it and hope it goes 294147072Sbrooks * away... XXX 295147072Sbrooks */ 296147072Sbrooks if (interface->rbuf_len - interface->rbuf_offset < 297147072Sbrooks sizeof(hdr)) { 298147072Sbrooks interface->rbuf_offset = interface->rbuf_len; 299147072Sbrooks continue; 300147072Sbrooks } 301147072Sbrooks 302147072Sbrooks /* Copy out a bpf header... */ 303147072Sbrooks memcpy(&hdr, &interface->rbuf[interface->rbuf_offset], 304147072Sbrooks sizeof(hdr)); 305147072Sbrooks 306147072Sbrooks /* 307147072Sbrooks * If the bpf header plus data doesn't fit in what's 308147072Sbrooks * left of the buffer, stick head in sand yet again... 309147072Sbrooks */ 310147072Sbrooks if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen > 311147072Sbrooks interface->rbuf_len) { 312147072Sbrooks interface->rbuf_offset = interface->rbuf_len; 313147072Sbrooks continue; 314147072Sbrooks } 315147072Sbrooks 316147072Sbrooks /* 317147072Sbrooks * If the captured data wasn't the whole packet, or if 318147072Sbrooks * the packet won't fit in the input buffer, all we can 319147072Sbrooks * do is drop it. 320147072Sbrooks */ 321147072Sbrooks if (hdr.bh_caplen != hdr.bh_datalen) { 322147072Sbrooks interface->rbuf_offset += hdr.bh_hdrlen = hdr.bh_caplen; 323147072Sbrooks continue; 324147072Sbrooks } 325147072Sbrooks 326147072Sbrooks /* Skip over the BPF header... */ 327147072Sbrooks interface->rbuf_offset += hdr.bh_hdrlen; 328147072Sbrooks 329147072Sbrooks /* Decode the physical header... */ 330147072Sbrooks offset = decode_hw_header(interface->rbuf, 331147072Sbrooks interface->rbuf_offset, hfrom); 332147072Sbrooks 333147072Sbrooks /* 334147072Sbrooks * If a physical layer checksum failed (dunno of any 335147072Sbrooks * physical layer that supports this, but WTH), skip 336147072Sbrooks * this packet. 337147072Sbrooks */ 338147072Sbrooks if (offset < 0) { 339147072Sbrooks interface->rbuf_offset += hdr.bh_caplen; 340147072Sbrooks continue; 341147072Sbrooks } 342147072Sbrooks interface->rbuf_offset += offset; 343147072Sbrooks hdr.bh_caplen -= offset; 344147072Sbrooks 345147072Sbrooks /* Decode the IP and UDP headers... */ 346147072Sbrooks offset = decode_udp_ip_header(interface->rbuf, 347147072Sbrooks interface->rbuf_offset, from, NULL, hdr.bh_caplen); 348147072Sbrooks 349147072Sbrooks /* If the IP or UDP checksum was bad, skip the packet... */ 350147072Sbrooks if (offset < 0) { 351147072Sbrooks interface->rbuf_offset += hdr.bh_caplen; 352147072Sbrooks continue; 353147072Sbrooks } 354147072Sbrooks interface->rbuf_offset += offset; 355147072Sbrooks hdr.bh_caplen -= offset; 356147072Sbrooks 357147072Sbrooks /* 358147072Sbrooks * If there's not enough room to stash the packet data, 359147072Sbrooks * we have to skip it (this shouldn't happen in real 360147072Sbrooks * life, though). 361147072Sbrooks */ 362147072Sbrooks if (hdr.bh_caplen > len) { 363147072Sbrooks interface->rbuf_offset += hdr.bh_caplen; 364147072Sbrooks continue; 365147072Sbrooks } 366147072Sbrooks 367147072Sbrooks /* Copy out the data in the packet... */ 368147072Sbrooks memcpy(buf, interface->rbuf + interface->rbuf_offset, 369147072Sbrooks hdr.bh_caplen); 370147072Sbrooks interface->rbuf_offset += hdr.bh_caplen; 371147072Sbrooks return (hdr.bh_caplen); 372147072Sbrooks } while (!length); 373147072Sbrooks return (0); 374147072Sbrooks} 375