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