1 /* $OpenBSD: bpf.c,v 1.13 2004/05/05 14:28:58 deraadt Exp $ */ 2 3 /* BPF socket interface code, originally contributed by Archie Cobbs. */ 4 5 /*- 6 * SPDX-License-Identifier: BSD-3-Clause 7 * 8 * Copyright (c) 2021 Franco Fichtner <franco@opnsense.org> 9 * Copyright (c) 1995, 1996, 1998, 1999 10 * The Internet Software Consortium. All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of The Internet Software Consortium nor the names 22 * of its contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 26 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 29 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 33 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 36 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * This software has been written for the Internet Software Consortium 40 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 41 * Enterprises. To learn more about the Internet Software Consortium, 42 * see ``http://www.vix.com/isc''. To learn more about Vixie 43 * Enterprises, see ``http://www.vix.com''. 44 */ 45 46 #include <sys/cdefs.h> 47 #include "dhcpd.h" 48 #include "privsep.h" 49 #include <sys/capsicum.h> 50 #include <sys/ioctl.h> 51 #include <sys/uio.h> 52 53 #include <net/bpf.h> 54 #include <netinet/in_systm.h> 55 #include <netinet/ip.h> 56 #include <netinet/udp.h> 57 #include <netinet/if_ether.h> 58 59 #include <capsicum_helpers.h> 60 61 #define BPF_FORMAT "/dev/bpf%d" 62 63 /* 64 * Called by get_interface_list for each interface that's discovered. 65 * Opens a packet filter for each interface and adds it to the select 66 * mask. 67 */ 68 int 69 if_register_bpf(struct interface_info *info, int flags) 70 { 71 char filename[50]; 72 int sock, b; 73 74 /* Open a BPF device */ 75 for (b = 0;; b++) { 76 snprintf(filename, sizeof(filename), BPF_FORMAT, b); 77 sock = open(filename, flags); 78 if (sock < 0) { 79 if (errno == EBUSY) 80 continue; 81 else 82 error("Can't find free bpf: %m"); 83 } else 84 break; 85 } 86 87 /* Set the BPF device to point at this interface. */ 88 if (ioctl(sock, BIOCSETIF, info->ifp) < 0) 89 error("Can't attach interface %s to bpf device %s: %m", 90 info->name, filename); 91 92 /* Tag the packets with the proper VLAN PCP setting. */ 93 if (info->client->config->vlan_pcp != 0) { 94 if (ioctl(sock, BIOCSETVLANPCP, 95 &info->client->config->vlan_pcp) < 0) 96 error( "Can't set the VLAN PCP tag on interface %s: %m", 97 info->name); 98 } 99 100 return (sock); 101 } 102 103 /* 104 * Packet write filter program: 105 * 'ip and udp and src port bootps and dst port (bootps or bootpc)' 106 */ 107 static const struct bpf_insn dhcp_bpf_wfilter[] = { 108 BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14), 109 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12), 110 111 /* Make sure this is an IP packet... */ 112 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), 113 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10), 114 115 /* Make sure it's a UDP packet... */ 116 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23), 117 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8), 118 119 /* Make sure this isn't a fragment... */ 120 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20), 121 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, IP_MF|IP_OFFMASK, 6, 0), 122 123 /* Get the IP header length... */ 124 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 125 126 /* Make sure it's from the right port... */ 127 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14), 128 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, LOCAL_PORT, 0, 3), 129 130 /* Make sure it is to the right ports ... */ 131 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 132 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, REMOTE_PORT, 0, 1), 133 134 /* If we passed all the tests, ask for the whole packet. */ 135 BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 136 137 /* Otherwise, drop it. */ 138 BPF_STMT(BPF_RET+BPF_K, 0), 139 }; 140 141 void 142 if_register_send(struct interface_info *info) 143 { 144 cap_rights_t rights; 145 struct bpf_version v; 146 struct bpf_program p; 147 int sock, on = 1; 148 149 /* Open a BPF device and hang it on this interface... */ 150 info->wfdesc = if_register_bpf(info, O_WRONLY); 151 152 /* Make sure the BPF version is in range... */ 153 if (ioctl(info->wfdesc, BIOCVERSION, &v) < 0) 154 error("Can't get BPF version: %m"); 155 156 if (v.bv_major != BPF_MAJOR_VERSION || 157 v.bv_minor < BPF_MINOR_VERSION) 158 error("Kernel BPF version out of range - recompile dhcpd!"); 159 160 /* Set up the bpf write filter program structure. */ 161 p.bf_insns = __DECONST(struct bpf_insn *, dhcp_bpf_wfilter); 162 p.bf_len = nitems(dhcp_bpf_wfilter); 163 164 if (ioctl(info->wfdesc, BIOCSETWF, &p) < 0) 165 error("Can't install write filter program: %m"); 166 167 if (ioctl(info->wfdesc, BIOCLOCK, NULL) < 0) 168 error("Cannot lock bpf"); 169 170 cap_rights_init(&rights, CAP_WRITE); 171 if (caph_rights_limit(info->wfdesc, &rights) < 0) 172 error("Can't limit bpf descriptor: %m"); 173 174 /* 175 * Use raw socket for unicast send. 176 */ 177 if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1) 178 error("socket(SOCK_RAW): %m"); 179 if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on, 180 sizeof(on)) == -1) 181 error("setsockopt(IP_HDRINCL): %m"); 182 info->ufdesc = sock; 183 } 184 185 /* 186 * Packet filter program... 187 */ 188 static const struct bpf_insn dhcp_bpf_filter[] = { 189 /* Use relative index (0) for IP packet... */ 190 BPF_STMT(BPF_LDX + BPF_W + BPF_IMM, 0), 191 192 /* 193 * Test whether this is a VLAN packet... 194 * 195 * In case the server packet is using a VLAN ID 196 * of 0, meaning an untagged priority was set, the 197 * response shall be read and replied to. 198 */ 199 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 12), 200 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_VLAN, 0, 4), 201 202 /* Test whether it has a VID of 0 */ 203 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14), 204 BPF_STMT(BPF_ALU + BPF_AND + BPF_K, EVL_VLID_MASK), 205 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 17), 206 207 /* Correct the relative index for VLAN packet (4)... */ 208 BPF_STMT(BPF_LDX + BPF_W + BPF_IMM, 4), 209 210 /* Make sure this is an IP packet... */ 211 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 12), 212 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 14), 213 214 /* Make sure it's a UDP packet... */ 215 BPF_STMT(BPF_LD + BPF_B + BPF_IND, 23), 216 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 12), 217 218 /* Make sure this isn't a fragment... */ 219 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 20), 220 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, IP_MF|IP_OFFMASK, 10, 0), 221 222 /* 223 * Get the IP header length... 224 * 225 * To find the correct position of the IP header 226 * length field store the index (0 or 4) in the 227 * accumulator and compare it with 0. 228 */ 229 BPF_STMT(BPF_MISC + BPF_TXA, 0), 230 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 2), 231 /* Store IP header length of IP packet in index. */ 232 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14), 233 /* Skip over following VLAN handling instruction. */ 234 BPF_JUMP(BPF_JMP + BPF_JA, 1, 0, 0), 235 /* Store IP header length of VLAN packet in index. */ 236 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 18), 237 /* Add IP header length to previous relative index. */ 238 BPF_STMT(BPF_ALU + BPF_ADD + BPF_X, 0), 239 /* Move result back to index to reach UDP header below. */ 240 BPF_STMT(BPF_MISC + BPF_TAX, 0), 241 242 /* Make sure it's to the right port... */ 243 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16), 244 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, LOCAL_PORT, 0, 1), 245 246 /* If we passed all the tests, ask for the whole packet. */ 247 BPF_STMT(BPF_RET+BPF_K, (u_int)-1), 248 249 /* Otherwise, drop it. */ 250 BPF_STMT(BPF_RET+BPF_K, 0), 251 }; 252 253 void 254 if_register_receive(struct interface_info *info) 255 { 256 static const unsigned long cmds[2] = { SIOCGIFFLAGS, SIOCGIFMEDIA }; 257 cap_rights_t rights; 258 struct bpf_version v; 259 struct bpf_program p; 260 int flag = 1, sz; 261 262 /* Open a BPF device and hang it on this interface... */ 263 info->rfdesc = if_register_bpf(info, O_RDONLY); 264 265 /* Make sure the BPF version is in range... */ 266 if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0) 267 error("Can't get BPF version: %m"); 268 269 if (v.bv_major != BPF_MAJOR_VERSION || 270 v.bv_minor < BPF_MINOR_VERSION) 271 error("Kernel BPF version out of range - recompile dhcpd!"); 272 273 /* 274 * Set immediate mode so that reads return as soon as a packet 275 * comes in, rather than waiting for the input buffer to fill 276 * with packets. 277 */ 278 if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0) 279 error("Can't set immediate mode on bpf device: %m"); 280 281 /* Get the required BPF buffer length from the kernel. */ 282 if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0) 283 error("Can't get bpf buffer length: %m"); 284 info->rbuf_max = sz; 285 info->rbuf = malloc(info->rbuf_max); 286 if (!info->rbuf) 287 error("Can't allocate %lu bytes for bpf input buffer.", 288 (unsigned long)info->rbuf_max); 289 info->rbuf_offset = 0; 290 info->rbuf_len = 0; 291 292 /* Set up the bpf filter program structure. */ 293 p.bf_insns = __DECONST(struct bpf_insn *, dhcp_bpf_filter); 294 p.bf_len = nitems(dhcp_bpf_filter); 295 296 if (ioctl(info->rfdesc, BIOCSETF, &p) < 0) 297 error("Can't install packet filter program: %m"); 298 299 if (ioctl(info->rfdesc, BIOCLOCK, NULL) < 0) 300 error("Cannot lock bpf"); 301 302 cap_rights_init(&rights, CAP_IOCTL, CAP_EVENT, CAP_READ); 303 if (caph_rights_limit(info->rfdesc, &rights) < 0) 304 error("Can't limit bpf descriptor: %m"); 305 if (caph_ioctls_limit(info->rfdesc, cmds, 2) < 0) 306 error("Can't limit ioctls for bpf descriptor: %m"); 307 } 308 309 void 310 send_packet_unpriv(int privfd, struct dhcp_packet *raw, size_t len, 311 struct in_addr from, struct in_addr to) 312 { 313 struct imsg_hdr hdr; 314 struct buf *buf; 315 int errs; 316 317 hdr.code = IMSG_SEND_PACKET; 318 hdr.len = sizeof(hdr) + 319 sizeof(size_t) + len + 320 sizeof(from) + sizeof(to); 321 322 if ((buf = buf_open(hdr.len)) == NULL) 323 error("buf_open: %m"); 324 325 errs = 0; 326 errs += buf_add(buf, &hdr, sizeof(hdr)); 327 errs += buf_add(buf, &len, sizeof(len)); 328 errs += buf_add(buf, raw, len); 329 errs += buf_add(buf, &from, sizeof(from)); 330 errs += buf_add(buf, &to, sizeof(to)); 331 if (errs) 332 error("buf_add: %m"); 333 334 if (buf_close(privfd, buf) == -1) 335 error("buf_close: %m"); 336 } 337 338 void 339 send_packet_priv(struct interface_info *interface, struct imsg_hdr *hdr, int fd) 340 { 341 unsigned char buf[256]; 342 struct iovec iov[2]; 343 struct msghdr msg; 344 struct dhcp_packet raw; 345 size_t len; 346 struct in_addr from, to; 347 int result, bufp = 0; 348 349 if (hdr->len < sizeof(*hdr) + sizeof(size_t)) 350 error("corrupted message received"); 351 buf_read(fd, &len, sizeof(len)); 352 if (hdr->len != sizeof(*hdr) + sizeof(size_t) + len + 353 sizeof(from) + sizeof(to)) { 354 error("corrupted message received"); 355 } 356 if (len > sizeof(raw)) 357 error("corrupted message received"); 358 buf_read(fd, &raw, len); 359 buf_read(fd, &from, sizeof(from)); 360 buf_read(fd, &to, sizeof(to)); 361 362 /* Assemble the headers... */ 363 if (to.s_addr == INADDR_BROADCAST) 364 assemble_hw_header(interface, buf, &bufp); 365 assemble_udp_ip_header(buf, &bufp, from.s_addr, to.s_addr, 366 htons(REMOTE_PORT), (unsigned char *)&raw, len); 367 368 iov[0].iov_base = buf; 369 iov[0].iov_len = bufp; 370 iov[1].iov_base = &raw; 371 iov[1].iov_len = len; 372 373 /* Fire it off */ 374 if (to.s_addr == INADDR_BROADCAST) 375 result = writev(interface->wfdesc, iov, 2); 376 else { 377 struct sockaddr_in sato; 378 379 sato.sin_addr = to; 380 sato.sin_port = htons(REMOTE_PORT); 381 sato.sin_family = AF_INET; 382 sato.sin_len = sizeof(sato); 383 384 memset(&msg, 0, sizeof(msg)); 385 msg.msg_name = (struct sockaddr *)&sato; 386 msg.msg_namelen = sizeof(sato); 387 msg.msg_iov = iov; 388 msg.msg_iovlen = 2; 389 result = sendmsg(interface->ufdesc, &msg, 0); 390 } 391 392 if (result < 0) 393 warning("send_packet: %m"); 394 } 395 396 ssize_t 397 receive_packet(struct interface_info *interface, unsigned char *buf, 398 size_t len, struct sockaddr_in *from, struct hardware *hfrom) 399 { 400 int length = 0, offset = 0; 401 struct bpf_hdr hdr; 402 403 /* 404 * All this complexity is because BPF doesn't guarantee that 405 * only one packet will be returned at a time. We're getting 406 * what we deserve, though - this is a terrible abuse of the BPF 407 * interface. Sigh. 408 */ 409 410 /* Process packets until we get one we can return or until we've 411 * done a read and gotten nothing we can return... 412 */ 413 do { 414 /* If the buffer is empty, fill it. */ 415 if (interface->rbuf_offset >= interface->rbuf_len) { 416 length = read(interface->rfdesc, interface->rbuf, 417 interface->rbuf_max); 418 if (length <= 0) 419 return (length); 420 interface->rbuf_offset = 0; 421 interface->rbuf_len = length; 422 } 423 424 /* 425 * If there isn't room for a whole bpf header, something 426 * went wrong, but we'll ignore it and hope it goes 427 * away... XXX 428 */ 429 if (interface->rbuf_len - interface->rbuf_offset < 430 sizeof(hdr)) { 431 interface->rbuf_offset = interface->rbuf_len; 432 continue; 433 } 434 435 /* Copy out a bpf header... */ 436 memcpy(&hdr, &interface->rbuf[interface->rbuf_offset], 437 sizeof(hdr)); 438 439 /* 440 * If the bpf header plus data doesn't fit in what's 441 * left of the buffer, stick head in sand yet again... 442 */ 443 if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen > 444 interface->rbuf_len) { 445 interface->rbuf_offset = interface->rbuf_len; 446 continue; 447 } 448 449 /* Skip over the BPF header... */ 450 interface->rbuf_offset += hdr.bh_hdrlen; 451 452 /* 453 * If the captured data wasn't the whole packet, or if 454 * the packet won't fit in the input buffer, all we can 455 * do is drop it. 456 */ 457 if (hdr.bh_caplen != hdr.bh_datalen) { 458 interface->rbuf_offset = 459 BPF_WORDALIGN(interface->rbuf_offset + 460 hdr.bh_caplen); 461 continue; 462 } 463 464 /* Decode the physical header... */ 465 offset = decode_hw_header(interface->rbuf, 466 interface->rbuf_offset, hfrom); 467 468 /* 469 * If a physical layer checksum failed (dunno of any 470 * physical layer that supports this, but WTH), skip 471 * this packet. 472 */ 473 if (offset < 0) { 474 interface->rbuf_offset = 475 BPF_WORDALIGN(interface->rbuf_offset + 476 hdr.bh_caplen); 477 continue; 478 } 479 interface->rbuf_offset += offset; 480 hdr.bh_caplen -= offset; 481 482 /* Decode the IP and UDP headers... */ 483 offset = decode_udp_ip_header(interface->rbuf, 484 interface->rbuf_offset, from, NULL, hdr.bh_caplen); 485 486 /* If the IP or UDP checksum was bad, skip the packet... */ 487 if (offset < 0) { 488 interface->rbuf_offset = 489 BPF_WORDALIGN(interface->rbuf_offset + 490 hdr.bh_caplen); 491 continue; 492 } 493 interface->rbuf_offset += offset; 494 hdr.bh_caplen -= offset; 495 496 /* 497 * If there's not enough room to stash the packet data, 498 * we have to skip it (this shouldn't happen in real 499 * life, though). 500 */ 501 if (hdr.bh_caplen > len) { 502 interface->rbuf_offset = 503 BPF_WORDALIGN(interface->rbuf_offset + 504 hdr.bh_caplen); 505 continue; 506 } 507 508 /* Copy out the data in the packet... */ 509 memcpy(buf, interface->rbuf + interface->rbuf_offset, 510 hdr.bh_caplen); 511 interface->rbuf_offset = 512 BPF_WORDALIGN(interface->rbuf_offset + 513 hdr.bh_caplen); 514 return (hdr.bh_caplen); 515 } while (!length); 516 return (0); 517 } 518