bpf.c revision 252619
1105059Scognet/* $OpenBSD: bpf.c,v 1.13 2004/05/05 14:28:58 deraadt Exp $ */ 2111158Scognet 3105059Scognet/* BPF socket interface code, originally contributed by Archie Cobbs. */ 4105059Scognet 5111158Scognet/* 6111158Scognet * Copyright (c) 1995, 1996, 1998, 1999 7111158Scognet * The Internet Software Consortium. All rights reserved. 8111158Scognet * 9111158Scognet * Redistribution and use in source and binary forms, with or without 10111158Scognet * modification, are permitted provided that the following conditions 11105059Scognet * are met: 12105059Scognet * 13119418Sobrien * 1. Redistributions of source code must retain the above copyright 14119418Sobrien * notice, this list of conditions and the following disclaimer. 15119418Sobrien * 2. Redistributions in binary form must reproduce the above copyright 16105059Scognet * notice, this list of conditions and the following disclaimer in the 17105059Scognet * documentation and/or other materials provided with the distribution. 18105059Scognet * 3. Neither the name of The Internet Software Consortium nor the names 19105059Scognet * of its contributors may be used to endorse or promote products derived 20105059Scognet * from this software without specific prior written permission. 21105059Scognet * 22105059Scognet * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 23105059Scognet * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24111158Scognet * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25111158Scognet * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26111158Scognet * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 27105059Scognet * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28105059Scognet * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29139749Simp * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 30139749Simp * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31105059Scognet * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32105059Scognet * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33105059Scognet * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34105059Scognet * SUCH DAMAGE. 35105059Scognet * 36105059Scognet * This software has been written for the Internet Software Consortium 37105059Scognet * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 38105059Scognet * Enterprises. To learn more about the Internet Software Consortium, 39105059Scognet * see ``http://www.vix.com/isc''. To learn more about Vixie 40105059Scognet * Enterprises, see ``http://www.vix.com''. 41105059Scognet */ 42105059Scognet 43105059Scognet#include <sys/cdefs.h> 44105059Scognet__FBSDID("$FreeBSD: head/sbin/dhclient/bpf.c 252619 2013-07-03 21:58:26Z pjd $"); 45105059Scognet 46105059Scognet#include "dhcpd.h" 47105059Scognet#include <sys/ioctl.h> 48105059Scognet#include <sys/uio.h> 49105059Scognet 50105059Scognet#include <net/bpf.h> 51105059Scognet#include <netinet/in_systm.h> 52105059Scognet#include <netinet/ip.h> 53105059Scognet#include <netinet/udp.h> 54105059Scognet#include <netinet/if_ether.h> 55105059Scognet 56105059Scognet#define BPF_FORMAT "/dev/bpf%d" 57105059Scognet 58105059Scognet/* 59105059Scognet * Called by get_interface_list for each interface that's discovered. 60105059Scognet * Opens a packet filter for each interface and adds it to the select 61105059Scognet * mask. 62105059Scognet */ 63105059Scognetint 64105059Scognetif_register_bpf(struct interface_info *info) 65105059Scognet{ 66105059Scognet char filename[50]; 67105059Scognet int sock, b; 68105059Scognet 69105059Scognet /* Open a BPF device */ 70105059Scognet for (b = 0; 1; b++) { 71105059Scognet snprintf(filename, sizeof(filename), BPF_FORMAT, b); 72105059Scognet sock = open(filename, O_RDWR, 0); 73105059Scognet if (sock < 0) { 74105059Scognet if (errno == EBUSY) 75105059Scognet continue; 76105059Scognet else 77129879Sphk error("Can't find free bpf: %m"); 78105059Scognet } else 79105059Scognet break; 80105059Scognet } 81105059Scognet 82119287Simp /* Set the BPF device to point at this interface. */ 83119287Simp if (ioctl(sock, BIOCSETIF, info->ifp) < 0) 84105059Scognet error("Can't attach interface %s to bpf device %s: %m", 85105059Scognet info->name, filename); 86105059Scognet 87105059Scognet return (sock); 88105059Scognet} 89105059Scognet 90105059Scognetvoid 91105059Scognetif_register_send(struct interface_info *info) 92105059Scognet{ 93105059Scognet int sock, on = 1; 94105059Scognet 95111158Scognet /* 96105059Scognet * If we're using the bpf API for sending and receiving, we 97105059Scognet * don't need to register this interface twice. 98105059Scognet */ 99105059Scognet info->wfdesc = info->rfdesc; 100105059Scognet 101105059Scognet /* 102105059Scognet * Use raw socket for unicast send. 103105059Scognet */ 104105059Scognet if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1) 105105059Scognet error("socket(SOCK_RAW): %m"); 106105059Scognet if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, 107105059Scognet sizeof(on)) == -1) 108105059Scognet error("setsockopt(IP_HDRINCL): %m"); 109105059Scognet info->ufdesc = sock; 110105059Scognet} 111105059Scognet 112111158Scognet/* 113105059Scognet * Packet filter program... 114105059Scognet * 115105059Scognet * XXX: Changes to the filter program may require changes to the 116105059Scognet * constant offsets used in if_register_send to patch the BPF program! 117105059Scognet */ 118105059Scognetstruct bpf_insn dhcp_bpf_filter[] = { 119105059Scognet /* Make sure this is an IP packet... */ 120105059Scognet BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 121111158Scognet BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8), 122111158Scognet 123111158Scognet /* Make sure it's a UDP packet... */ 124111158Scognet BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 125111158Scognet BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6), 126111158Scognet 127105059Scognet /* Make sure this isn't a fragment... */ 128105059Scognet BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 129105059Scognet BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0), 130111158Scognet 131105059Scognet /* Get the IP header length... */ 132111158Scognet BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 133105059Scognet 134111158Scognet /* Make sure it's to the right port... */ 135105059Scognet BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 136111158Scognet BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */ 137105059Scognet 138111158Scognet /* If we passed all the tests, ask for the whole packet. */ 139105059Scognet BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 140111158Scognet 141105059Scognet /* Otherwise, drop it. */ 142111158Scognet BPF_STMT(BPF_RET+BPF_K, 0), 143105059Scognet}; 144111158Scognet 145105059Scognetint dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn); 146111158Scognet 147105059Scognet/* 148111158Scognet * Packet write filter program: 149105059Scognet * 'ip and udp and src port bootps and dst port (bootps or bootpc)' 150111158Scognet */ 151105059Scognetstruct bpf_insn dhcp_bpf_wfilter[] = { 152111158Scognet BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14), 153111158Scognet BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12), 154111158Scognet 155105059Scognet /* Make sure this is an IP packet... */ 156105059Scognet BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 157105059Scognet BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10), 158105059Scognet 159105059Scognet /* Make sure it's a UDP packet... */ 160105059Scognet BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 161105059Scognet BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8), 162105059Scognet 163105059Scognet /* Make sure this isn't a fragment... */ 164105059Scognet BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 165105059Scognet BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 6, 0), /* patched */ 166105059Scognet 167105059Scognet /* Get the IP header length... */ 168105059Scognet BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 169105059Scognet 170105059Scognet /* Make sure it's from the right port... */ 171105059Scognet BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14), 172105059Scognet BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 68, 0, 3), 173111158Scognet 174105059Scognet /* Make sure it is to the right ports ... */ 175105059Scognet BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 176111158Scognet BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), 177111158Scognet 178105059Scognet /* If we passed all the tests, ask for the whole packet. */ 179105059Scognet BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 180105059Scognet 181105059Scognet /* Otherwise, drop it. */ 182105059Scognet BPF_STMT(BPF_RET+BPF_K, 0), 183105059Scognet}; 184105059Scognet 185105059Scognetint dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn); 186105059Scognet 187105059Scognetvoid 188105059Scognetif_register_receive(struct interface_info *info) 189105059Scognet{ 190105059Scognet struct bpf_version v; 191105059Scognet struct bpf_program p; 192105059Scognet int flag = 1, sz; 193105059Scognet 194105059Scognet /* Open a BPF device and hang it on this interface... */ 195105059Scognet info->rfdesc = if_register_bpf(info); 196105059Scognet 197105059Scognet /* Make sure the BPF version is in range... */ 198105059Scognet if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0) 199105059Scognet error("Can't get BPF version: %m"); 200105059Scognet 201105059Scognet if (v.bv_major != BPF_MAJOR_VERSION || 202105059Scognet v.bv_minor < BPF_MINOR_VERSION) 203105059Scognet error("Kernel BPF version out of range - recompile dhcpd!"); 204105059Scognet 205105059Scognet /* 206105059Scognet * Set immediate mode so that reads return as soon as a packet 207105059Scognet * comes in, rather than waiting for the input buffer to fill 208105059Scognet * with packets. 209105059Scognet */ 210105059Scognet if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0) 211105059Scognet error("Can't set immediate mode on bpf device: %m"); 212105059Scognet 213111158Scognet /* Get the required BPF buffer length from the kernel. */ 214105059Scognet if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0) 215105059Scognet error("Can't get bpf buffer length: %m"); 216105059Scognet info->rbuf_max = sz; 217105059Scognet info->rbuf = malloc(info->rbuf_max); 218105059Scognet if (!info->rbuf) 219105059Scognet error("Can't allocate %lu bytes for bpf input buffer.", 220105059Scognet (unsigned long)info->rbuf_max); 221105059Scognet info->rbuf_offset = 0; 222105059Scognet info->rbuf_len = 0; 223105059Scognet 224105059Scognet /* Set up the bpf filter program structure. */ 225105059Scognet p.bf_len = dhcp_bpf_filter_len; 226105059Scognet p.bf_insns = dhcp_bpf_filter; 227105059Scognet 228105059Scognet /* Patch the server port into the BPF program... 229105059Scognet * 230105059Scognet * XXX: changes to filter program may require changes to the 231105059Scognet * insn number(s) used below! 232105059Scognet */ 233105059Scognet dhcp_bpf_filter[8].k = LOCAL_PORT; 234111158Scognet 235105059Scognet if (ioctl(info->rfdesc, BIOCSETF, &p) < 0) 236105059Scognet error("Can't install packet filter program: %m"); 237105059Scognet 238105059Scognet /* Set up the bpf write filter program structure. */ 239105059Scognet p.bf_len = dhcp_bpf_wfilter_len; 240105059Scognet p.bf_insns = dhcp_bpf_wfilter; 241105059Scognet 242105059Scognet if (dhcp_bpf_wfilter[7].k == 0x1fff) 243105059Scognet dhcp_bpf_wfilter[7].k = htons(IP_MF|IP_OFFMASK); 244105059Scognet 245111158Scognet if (ioctl(info->rfdesc, BIOCSETWF, &p) < 0) 246111158Scognet error("Can't install write filter program: %m"); 247111158Scognet 248111158Scognet if (ioctl(info->rfdesc, BIOCLOCK, NULL) < 0) 249111158Scognet error("Cannot lock bpf"); 250111158Scognet} 251111158Scognet 252111158Scognetvoid 253111158Scognetsend_packet(struct interface_info *interface, struct dhcp_packet *raw, 254105059Scognet size_t len, struct in_addr from, struct in_addr to) 255105059Scognet{ 256111158Scognet unsigned char buf[256]; 257111158Scognet struct iovec iov[2]; 258111158Scognet struct msghdr msg; 259105059Scognet int result, bufp = 0; 260105059Scognet 261105059Scognet /* Assemble the headers... */ 262105059Scognet if (to.s_addr == INADDR_BROADCAST) 263105059Scognet assemble_hw_header(interface, buf, &bufp); 264105059Scognet assemble_udp_ip_header(buf, &bufp, from.s_addr, to.s_addr, 265105059Scognet htons(REMOTE_PORT), (unsigned char *)raw, len); 266105059Scognet 267105059Scognet iov[0].iov_base = buf; 268105059Scognet iov[0].iov_len = bufp; 269105059Scognet iov[1].iov_base = raw; 270105059Scognet iov[1].iov_len = len; 271105059Scognet 272105059Scognet /* Fire it off */ 273105059Scognet if (to.s_addr == INADDR_BROADCAST) 274105059Scognet result = writev(interface->wfdesc, iov, 2); 275105059Scognet else { 276105059Scognet struct sockaddr_in sato; 277105059Scognet 278105059Scognet sato.sin_addr = to; 279105059Scognet sato.sin_port = htons(REMOTE_PORT); 280105059Scognet sato.sin_family = AF_INET; 281105059Scognet sato.sin_len = sizeof(sato); 282105059Scognet 283105059Scognet memset(&msg, 0, sizeof(msg)); 284105059Scognet msg.msg_name = (struct sockaddr *)&sato; 285105059Scognet msg.msg_namelen = sizeof(sato); 286105059Scognet msg.msg_iov = iov; 287105059Scognet msg.msg_iovlen = 2; 288111158Scognet result = sendmsg(interface->ufdesc, &msg, 0); 289105059Scognet } 290105059Scognet 291105059Scognet if (result < 0) 292105059Scognet warning("send_packet: %m"); 293105059Scognet} 294105059Scognet 295105059Scognetssize_t 296105059Scognetreceive_packet(struct interface_info *interface, unsigned char *buf, 297105059Scognet size_t len, struct sockaddr_in *from, struct hardware *hfrom) 298105059Scognet{ 299105059Scognet int length = 0, offset = 0; 300105059Scognet struct bpf_hdr hdr; 301105059Scognet 302105059Scognet /* 303111158Scognet * All this complexity is because BPF doesn't guarantee that 304111158Scognet * only one packet will be returned at a time. We're getting 305111158Scognet * what we deserve, though - this is a terrible abuse of the BPF 306111158Scognet * interface. Sigh. 307105059Scognet */ 308105059Scognet 309111158Scognet /* Process packets until we get one we can return or until we've 310111158Scognet * done a read and gotten nothing we can return... 311111158Scognet */ 312111158Scognet do { 313105059Scognet /* If the buffer is empty, fill it. */ 314105059Scognet if (interface->rbuf_offset >= interface->rbuf_len) { 315105059Scognet length = read(interface->rfdesc, interface->rbuf, 316105059Scognet interface->rbuf_max); 317105059Scognet if (length <= 0) 318105059Scognet return (length); 319105059Scognet interface->rbuf_offset = 0; 320111158Scognet interface->rbuf_len = length; 321111158Scognet } 322111158Scognet 323111158Scognet /* 324111158Scognet * If there isn't room for a whole bpf header, something 325105059Scognet * went wrong, but we'll ignore it and hope it goes 326105059Scognet * away... XXX 327105059Scognet */ 328105059Scognet if (interface->rbuf_len - interface->rbuf_offset < 329105059Scognet sizeof(hdr)) { 330111158Scognet interface->rbuf_offset = interface->rbuf_len; 331105059Scognet continue; 332105059Scognet } 333105059Scognet 334105059Scognet /* Copy out a bpf header... */ 335105059Scognet memcpy(&hdr, &interface->rbuf[interface->rbuf_offset], 336105059Scognet sizeof(hdr)); 337105059Scognet 338105059Scognet /* 339105059Scognet * If the bpf header plus data doesn't fit in what's 340105059Scognet * left of the buffer, stick head in sand yet again... 341105059Scognet */ 342105059Scognet if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen > 343105059Scognet interface->rbuf_len) { 344105059Scognet interface->rbuf_offset = interface->rbuf_len; 345105059Scognet continue; 346105059Scognet } 347105059Scognet 348105059Scognet /* Skip over the BPF header... */ 349105059Scognet interface->rbuf_offset += hdr.bh_hdrlen; 350105059Scognet 351105059Scognet /* 352105059Scognet * If the captured data wasn't the whole packet, or if 353111158Scognet * the packet won't fit in the input buffer, all we can 354105059Scognet * do is drop it. 355105059Scognet */ 356105059Scognet if (hdr.bh_caplen != hdr.bh_datalen) { 357105059Scognet interface->rbuf_offset = 358105059Scognet BPF_WORDALIGN(interface->rbuf_offset + 359105059Scognet hdr.bh_caplen); 360105059Scognet continue; 361105059Scognet } 362105059Scognet 363105059Scognet /* Decode the physical header... */ 364105059Scognet offset = decode_hw_header(interface->rbuf, 365105059Scognet interface->rbuf_offset, hfrom); 366111158Scognet 367105059Scognet /* 368111158Scognet * If a physical layer checksum failed (dunno of any 369105059Scognet * physical layer that supports this, but WTH), skip 370105059Scognet * this packet. 371105059Scognet */ 372105059Scognet if (offset < 0) { 373105059Scognet interface->rbuf_offset = 374105059Scognet BPF_WORDALIGN(interface->rbuf_offset + 375105059Scognet hdr.bh_caplen); 376105059Scognet continue; 377105059Scognet } 378105059Scognet interface->rbuf_offset += offset; 379105059Scognet hdr.bh_caplen -= offset; 380105059Scognet 381105059Scognet /* Decode the IP and UDP headers... */ 382105059Scognet offset = decode_udp_ip_header(interface->rbuf, 383105059Scognet interface->rbuf_offset, from, NULL, hdr.bh_caplen); 384105059Scognet 385105059Scognet /* If the IP or UDP checksum was bad, skip the packet... */ 386105059Scognet if (offset < 0) { 387105059Scognet interface->rbuf_offset = 388105059Scognet BPF_WORDALIGN(interface->rbuf_offset + 389105059Scognet hdr.bh_caplen); 390105059Scognet continue; 391111158Scognet } 392111158Scognet interface->rbuf_offset += offset; 393105059Scognet hdr.bh_caplen -= offset; 394105059Scognet 395105059Scognet /* 396111158Scognet * If there's not enough room to stash the packet data, 397105059Scognet * we have to skip it (this shouldn't happen in real 398105059Scognet * life, though). 399105059Scognet */ 400111158Scognet if (hdr.bh_caplen > len) { 401111158Scognet interface->rbuf_offset = 402111158Scognet BPF_WORDALIGN(interface->rbuf_offset + 403111158Scognet hdr.bh_caplen); 404111158Scognet continue; 405111158Scognet } 406111158Scognet 407111158Scognet /* Copy out the data in the packet... */ 408111158Scognet memcpy(buf, interface->rbuf + interface->rbuf_offset, 409111158Scognet hdr.bh_caplen); 410111158Scognet interface->rbuf_offset = 411111158Scognet BPF_WORDALIGN(interface->rbuf_offset + 412111158Scognet hdr.bh_caplen); 413111158Scognet return (hdr.bh_caplen); 414111158Scognet } while (!length); 415111158Scognet return (0); 416111158Scognet} 417111158Scognet