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