1*0438cf0aSkrw /* $OpenBSD: bpf.c,v 1.16 2017/02/13 23:04:05 krw Exp $ */ 2e853bc5dShenning 3e853bc5dShenning /* BPF socket interface code, originally contributed by Archie Cobbs. */ 4e853bc5dShenning 5e853bc5dShenning /* 6e853bc5dShenning * Copyright (c) 1995, 1996, 1998, 1999 7e853bc5dShenning * The Internet Software Consortium. All rights reserved. 8e853bc5dShenning * 9e853bc5dShenning * Redistribution and use in source and binary forms, with or without 10e853bc5dShenning * modification, are permitted provided that the following conditions 11e853bc5dShenning * are met: 12e853bc5dShenning * 13e853bc5dShenning * 1. Redistributions of source code must retain the above copyright 14e853bc5dShenning * notice, this list of conditions and the following disclaimer. 15e853bc5dShenning * 2. Redistributions in binary form must reproduce the above copyright 16e853bc5dShenning * notice, this list of conditions and the following disclaimer in the 17e853bc5dShenning * documentation and/or other materials provided with the distribution. 18e853bc5dShenning * 3. Neither the name of The Internet Software Consortium nor the names 19e853bc5dShenning * of its contributors may be used to endorse or promote products derived 20e853bc5dShenning * from this software without specific prior written permission. 21e853bc5dShenning * 22e853bc5dShenning * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 23e853bc5dShenning * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 24e853bc5dShenning * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 25e853bc5dShenning * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 26e853bc5dShenning * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 27e853bc5dShenning * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28e853bc5dShenning * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29e853bc5dShenning * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 30e853bc5dShenning * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 31e853bc5dShenning * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 32e853bc5dShenning * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 33e853bc5dShenning * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34e853bc5dShenning * SUCH DAMAGE. 35e853bc5dShenning * 36e853bc5dShenning * This software has been written for the Internet Software Consortium 37e853bc5dShenning * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 38e853bc5dShenning * Enterprises. To learn more about the Internet Software Consortium, 39e853bc5dShenning * see ``http://www.vix.com/isc''. To learn more about Vixie 40e853bc5dShenning * Enterprises, see ``http://www.vix.com''. 41e853bc5dShenning */ 42e853bc5dShenning 43e853bc5dShenning #include <sys/ioctl.h> 44837cddffSkrw #include <sys/types.h> 45837cddffSkrw #include <sys/socket.h> 46837cddffSkrw 47837cddffSkrw #include <arpa/inet.h> 48e853bc5dShenning 49e853bc5dShenning #include <net/bpf.h> 50837cddffSkrw #include <net/if.h> 51837cddffSkrw 52e853bc5dShenning #include <netinet/if_ether.h> 53837cddffSkrw #include <netinet/in.h> 54837cddffSkrw 55837cddffSkrw #include <errno.h> 56837cddffSkrw #include <fcntl.h> 57837cddffSkrw #include <stdio.h> 58837cddffSkrw #include <stdlib.h> 59837cddffSkrw #include <string.h> 60837cddffSkrw #include <unistd.h> 61837cddffSkrw 62837cddffSkrw #include "dhcp.h" 63837cddffSkrw #include "tree.h" 64837cddffSkrw #include "dhcpd.h" 65c525a185Skrw #include "log.h" 66e853bc5dShenning 6784d8c049Syasuoka ssize_t send_packet (struct interface_info *, struct dhcp_packet *, 6884d8c049Syasuoka size_t, struct in_addr, struct sockaddr_in *, struct hardware *); 6984d8c049Syasuoka 70e853bc5dShenning /* 71e853bc5dShenning * Called by get_interface_list for each interface that's discovered. 72e853bc5dShenning * Opens a packet filter for each interface and adds it to the select 73e853bc5dShenning * mask. 74e853bc5dShenning */ 75e853bc5dShenning int 76e853bc5dShenning if_register_bpf(struct interface_info *info) 77e853bc5dShenning { 782abf9a0dSnatano int sock; 79e853bc5dShenning 802abf9a0dSnatano if ((sock = open("/dev/bpf0", O_RDWR)) == -1) 81*0438cf0aSkrw fatal("Can't open bpf device"); 82e853bc5dShenning 83e853bc5dShenning /* Set the BPF device to point at this interface. */ 849bb003e4Sclaudio if (ioctl(sock, BIOCSETIF, info->ifp) == -1) 85*0438cf0aSkrw fatal("Can't attach interface %s to bpf device", info->name); 86e853bc5dShenning 8784d8c049Syasuoka info->send_packet = send_packet; 88e853bc5dShenning return (sock); 89e853bc5dShenning } 90e853bc5dShenning 91e853bc5dShenning void 92e853bc5dShenning if_register_send(struct interface_info *info) 93e853bc5dShenning { 94e853bc5dShenning /* 95e853bc5dShenning * If we're using the bpf API for sending and receiving, we 96e853bc5dShenning * don't need to register this interface twice. 97e853bc5dShenning */ 98e853bc5dShenning info->wfdesc = info->rfdesc; 99e853bc5dShenning } 100e853bc5dShenning 101e853bc5dShenning /* 102390956b7Scanacar * Packet read filter program: 'ip and udp and dst port bootps' 103e853bc5dShenning */ 104e853bc5dShenning struct bpf_insn dhcp_bpf_filter[] = { 105e853bc5dShenning /* Make sure this is an IP packet... */ 106e853bc5dShenning BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 107e853bc5dShenning BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8), 108e853bc5dShenning 109e853bc5dShenning /* Make sure it's a UDP packet... */ 110e853bc5dShenning BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 111e853bc5dShenning BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6), 112e853bc5dShenning 113e853bc5dShenning /* Make sure this isn't a fragment... */ 114e853bc5dShenning BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 115e853bc5dShenning BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0), 116e853bc5dShenning 117e853bc5dShenning /* Get the IP header length... */ 118e853bc5dShenning BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 119e853bc5dShenning 120e853bc5dShenning /* Make sure it's to the right port... */ 121e853bc5dShenning BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 122390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1), 123e853bc5dShenning 124e853bc5dShenning /* If we passed all the tests, ask for the whole packet. */ 125e853bc5dShenning BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 126e853bc5dShenning 127e853bc5dShenning /* Otherwise, drop it. */ 128e853bc5dShenning BPF_STMT(BPF_RET+BPF_K, 0), 129e853bc5dShenning }; 130e853bc5dShenning 131e853bc5dShenning int dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn); 132e853bc5dShenning 133390956b7Scanacar 134390956b7Scanacar /* 135390956b7Scanacar * Packet write filter program: 136390956b7Scanacar * 'ip and udp and src port bootps and dst port (bootps or bootpc)' 137390956b7Scanacar */ 138390956b7Scanacar struct bpf_insn dhcp_bpf_wfilter[] = { 139390956b7Scanacar /* Make sure this is an IP packet... */ 140390956b7Scanacar BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 141390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 11), 142390956b7Scanacar 143390956b7Scanacar /* Make sure it's a UDP packet... */ 144390956b7Scanacar BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 145390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 9), 146390956b7Scanacar 147390956b7Scanacar /* Make sure this isn't a fragment... */ 148390956b7Scanacar BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 149390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 7, 0), 150390956b7Scanacar 151390956b7Scanacar /* Get the IP header length... */ 152390956b7Scanacar BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 153390956b7Scanacar 154390956b7Scanacar /* Make sure it's from the right port... */ 155390956b7Scanacar BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14), 156390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 4), 157390956b7Scanacar 158390956b7Scanacar /* Make sure it is to the right ports ... */ 159390956b7Scanacar BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 160390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, CLIENT_PORT, 1, 0), 161390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1), 162390956b7Scanacar 163390956b7Scanacar /* If we passed all the tests, ask for the whole packet. */ 164390956b7Scanacar BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 165390956b7Scanacar 166390956b7Scanacar /* Otherwise, drop it. */ 167390956b7Scanacar BPF_STMT(BPF_RET+BPF_K, 0), 168390956b7Scanacar }; 169390956b7Scanacar 170390956b7Scanacar int dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn); 171390956b7Scanacar 172e853bc5dShenning void 173e853bc5dShenning if_register_receive(struct interface_info *info) 174e853bc5dShenning { 175e853bc5dShenning struct bpf_version v; 176e853bc5dShenning struct bpf_program p; 177390956b7Scanacar int flag = 1, sz, cmplt = 0; 178e853bc5dShenning 179e853bc5dShenning /* Open a BPF device and hang it on this interface... */ 180e853bc5dShenning info->rfdesc = if_register_bpf(info); 181e853bc5dShenning 182e853bc5dShenning /* Make sure the BPF version is in range... */ 1839bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCVERSION, &v) == -1) 184*0438cf0aSkrw fatal("Can't get BPF version"); 185e853bc5dShenning 186e853bc5dShenning if (v.bv_major != BPF_MAJOR_VERSION || 187e853bc5dShenning v.bv_minor < BPF_MINOR_VERSION) 188c525a185Skrw fatalx("Kernel BPF version out of range - recompile dhcpd!"); 189e853bc5dShenning 190e853bc5dShenning /* 191e853bc5dShenning * Set immediate mode so that reads return as soon as a packet 192e853bc5dShenning * comes in, rather than waiting for the input buffer to fill 193e853bc5dShenning * with packets. 194e853bc5dShenning */ 1959bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) == -1) 196*0438cf0aSkrw fatal("Can't set immediate mode on bpf device"); 197e853bc5dShenning 198e7791a15Smpf if (ioctl(info->rfdesc, BIOCSFILDROP, &flag) == -1) 199*0438cf0aSkrw fatal("Can't set filter-drop mode on bpf device"); 200e7791a15Smpf 201390956b7Scanacar /* make sure kernel fills in the source ethernet address */ 2029bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCSHDRCMPLT, &cmplt) == -1) 203*0438cf0aSkrw fatal("Can't set header complete flag on bpf device"); 204390956b7Scanacar 205e853bc5dShenning /* Get the required BPF buffer length from the kernel. */ 2069bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCGBLEN, &sz) == -1) 207*0438cf0aSkrw fatal("Can't get bpf buffer length"); 208e853bc5dShenning info->rbuf_max = sz; 209e853bc5dShenning info->rbuf = malloc(info->rbuf_max); 210e853bc5dShenning if (!info->rbuf) 211c525a185Skrw fatalx("Can't allocate %lu bytes for bpf input buffer.", 212e853bc5dShenning (unsigned long)info->rbuf_max); 213e853bc5dShenning info->rbuf_offset = 0; 214e853bc5dShenning info->rbuf_len = 0; 215e853bc5dShenning 216e853bc5dShenning /* Set up the bpf filter program structure. */ 217e853bc5dShenning p.bf_len = dhcp_bpf_filter_len; 218e853bc5dShenning p.bf_insns = dhcp_bpf_filter; 219e853bc5dShenning 2209bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCSETF, &p) == -1) 221*0438cf0aSkrw fatal("Can't install packet filter program"); 222390956b7Scanacar 223390956b7Scanacar /* Set up the bpf write filter program structure. */ 224390956b7Scanacar p.bf_len = dhcp_bpf_wfilter_len; 225390956b7Scanacar p.bf_insns = dhcp_bpf_wfilter; 226390956b7Scanacar 2279bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCSETWF, &p) == -1) 228*0438cf0aSkrw fatal("Can't install write filter program"); 229390956b7Scanacar 230390956b7Scanacar /* make sure these settings cannot be changed after dropping privs */ 2319bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCLOCK) == -1) 232*0438cf0aSkrw fatal("Failed to lock bpf descriptor"); 233e853bc5dShenning } 234e853bc5dShenning 235e853bc5dShenning ssize_t 236285f06efSderaadt send_packet(struct interface_info *interface, struct dhcp_packet *raw, 237285f06efSderaadt size_t len, struct in_addr from, struct sockaddr_in *to, 238285f06efSderaadt struct hardware *hto) 239e853bc5dShenning { 240e853bc5dShenning unsigned char buf[256]; 241e853bc5dShenning struct iovec iov[2]; 2421a4d4b6bSkrw ssize_t result; 2431a4d4b6bSkrw int bufp = 0; 244e853bc5dShenning 245e853bc5dShenning /* Assemble the headers... */ 246e853bc5dShenning assemble_hw_header(interface, buf, &bufp, hto); 247e853bc5dShenning assemble_udp_ip_header(interface, buf, &bufp, from.s_addr, 248e853bc5dShenning to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len); 249e853bc5dShenning 250e853bc5dShenning /* Fire it off */ 251e853bc5dShenning iov[0].iov_base = (char *)buf; 252e853bc5dShenning iov[0].iov_len = bufp; 253e853bc5dShenning iov[1].iov_base = (char *)raw; 254e853bc5dShenning iov[1].iov_len = len; 255e853bc5dShenning 256e853bc5dShenning result = writev(interface->wfdesc, iov, 2); 2579bb003e4Sclaudio if (result == -1) 258*0438cf0aSkrw log_warn("send_packet"); 259e853bc5dShenning return (result); 260e853bc5dShenning } 261e853bc5dShenning 262e853bc5dShenning ssize_t 263e853bc5dShenning receive_packet(struct interface_info *interface, unsigned char *buf, 264e853bc5dShenning size_t len, struct sockaddr_in *from, struct hardware *hfrom) 265e853bc5dShenning { 266e853bc5dShenning int length = 0, offset = 0; 267e853bc5dShenning struct bpf_hdr hdr; 268e853bc5dShenning 269e853bc5dShenning /* 270e853bc5dShenning * All this complexity is because BPF doesn't guarantee that 271e853bc5dShenning * only one packet will be returned at a time. We're getting 272e853bc5dShenning * what we deserve, though - this is a terrible abuse of the BPF 273e853bc5dShenning * interface. Sigh. 274e853bc5dShenning */ 275e853bc5dShenning 276e853bc5dShenning /* Process packets until we get one we can return or until we've 277e853bc5dShenning * done a read and gotten nothing we can return... 278e853bc5dShenning */ 279e853bc5dShenning do { 280e853bc5dShenning /* If the buffer is empty, fill it. */ 281e853bc5dShenning if (interface->rbuf_offset == interface->rbuf_len) { 282e853bc5dShenning length = read(interface->rfdesc, interface->rbuf, 283e853bc5dShenning interface->rbuf_max); 284e853bc5dShenning if (length <= 0) 285e853bc5dShenning return (length); 286e853bc5dShenning interface->rbuf_offset = 0; 28715035272Skrw interface->rbuf_len = BPF_WORDALIGN(length); 288e853bc5dShenning } 289e853bc5dShenning 290e853bc5dShenning /* 291e853bc5dShenning * If there isn't room for a whole bpf header, something 292e853bc5dShenning * went wrong, but we'll ignore it and hope it goes 293e853bc5dShenning * away... XXX 294e853bc5dShenning */ 295e853bc5dShenning if (interface->rbuf_len - interface->rbuf_offset < 296e853bc5dShenning sizeof(hdr)) { 297e853bc5dShenning interface->rbuf_offset = interface->rbuf_len; 298e853bc5dShenning continue; 299e853bc5dShenning } 300e853bc5dShenning 301e853bc5dShenning /* Copy out a bpf header... */ 302e853bc5dShenning memcpy(&hdr, &interface->rbuf[interface->rbuf_offset], 303e853bc5dShenning sizeof(hdr)); 304e853bc5dShenning 305e853bc5dShenning /* 306e853bc5dShenning * If the bpf header plus data doesn't fit in what's 307e853bc5dShenning * left of the buffer, stick head in sand yet again... 308e853bc5dShenning */ 309e853bc5dShenning if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen > 310e853bc5dShenning interface->rbuf_len) { 311e853bc5dShenning interface->rbuf_offset = interface->rbuf_len; 312e853bc5dShenning continue; 313e853bc5dShenning } 314e853bc5dShenning 315e853bc5dShenning /* 316e853bc5dShenning * If the captured data wasn't the whole packet, or if 317e853bc5dShenning * the packet won't fit in the input buffer, all we can 318e853bc5dShenning * do is drop it. 319e853bc5dShenning */ 320e853bc5dShenning if (hdr.bh_caplen != hdr.bh_datalen) { 32115035272Skrw interface->rbuf_offset = BPF_WORDALIGN( 32215035272Skrw interface->rbuf_offset + hdr.bh_hdrlen + 32315035272Skrw hdr.bh_caplen); 324e853bc5dShenning continue; 325e853bc5dShenning } 326e853bc5dShenning 327e853bc5dShenning /* Skip over the BPF header... */ 328e853bc5dShenning interface->rbuf_offset += hdr.bh_hdrlen; 329e853bc5dShenning 330e853bc5dShenning /* Decode the physical header... */ 331e853bc5dShenning offset = decode_hw_header(interface, 332e853bc5dShenning interface->rbuf, interface->rbuf_offset, hfrom); 333e853bc5dShenning 334e853bc5dShenning /* 335e853bc5dShenning * If a physical layer checksum failed (dunno of any 336e853bc5dShenning * physical layer that supports this, but WTH), skip 337e853bc5dShenning * this packet. 338e853bc5dShenning */ 339e853bc5dShenning if (offset < 0) { 34015035272Skrw interface->rbuf_offset = BPF_WORDALIGN( 34115035272Skrw interface->rbuf_offset + hdr.bh_caplen); 342e853bc5dShenning continue; 343e853bc5dShenning } 344e853bc5dShenning interface->rbuf_offset += offset; 345e853bc5dShenning hdr.bh_caplen -= offset; 346e853bc5dShenning 347e853bc5dShenning /* Decode the IP and UDP headers... */ 348e853bc5dShenning offset = decode_udp_ip_header(interface, interface->rbuf, 3499c0483d2Skrw interface->rbuf_offset, from, hdr.bh_caplen); 350e853bc5dShenning 351e853bc5dShenning /* If the IP or UDP checksum was bad, skip the packet... */ 352e853bc5dShenning if (offset < 0) { 35315035272Skrw interface->rbuf_offset = BPF_WORDALIGN( 35415035272Skrw interface->rbuf_offset + hdr.bh_caplen); 355e853bc5dShenning continue; 356e853bc5dShenning } 357e853bc5dShenning interface->rbuf_offset += offset; 358e853bc5dShenning hdr.bh_caplen -= offset; 359e853bc5dShenning 360e853bc5dShenning /* 361e853bc5dShenning * If there's not enough room to stash the packet data, 362e853bc5dShenning * we have to skip it (this shouldn't happen in real 363e853bc5dShenning * life, though). 364e853bc5dShenning */ 365e853bc5dShenning if (hdr.bh_caplen > len) { 36615035272Skrw interface->rbuf_offset = BPF_WORDALIGN( 36715035272Skrw interface->rbuf_offset + hdr.bh_caplen); 368e853bc5dShenning continue; 369e853bc5dShenning } 370e853bc5dShenning 371e853bc5dShenning /* Copy out the data in the packet... */ 372e853bc5dShenning memcpy(buf, interface->rbuf + interface->rbuf_offset, 373e853bc5dShenning hdr.bh_caplen); 37415035272Skrw interface->rbuf_offset = BPF_WORDALIGN(interface->rbuf_offset + 37515035272Skrw hdr.bh_caplen); 376e853bc5dShenning return (hdr.bh_caplen); 377e853bc5dShenning } while (!length); 378e853bc5dShenning return (0); 379e853bc5dShenning } 380