18d36e1dfSRoy Marples /* SPDX-License-Identifier: BSD-2-Clause */ 27827cba2SAaron LI /* 37827cba2SAaron LI * dhcpcd: BPF arp and bootp filtering 4*80aa9461SRoy Marples * Copyright (c) 2006-2023 Roy Marples <roy@marples.name> 57827cba2SAaron LI * All rights reserved 67827cba2SAaron LI 77827cba2SAaron LI * Redistribution and use in source and binary forms, with or without 87827cba2SAaron LI * modification, are permitted provided that the following conditions 97827cba2SAaron LI * are met: 107827cba2SAaron LI * 1. Redistributions of source code must retain the above copyright 117827cba2SAaron LI * notice, this list of conditions and the following disclaimer. 127827cba2SAaron LI * 2. Redistributions in binary form must reproduce the above copyright 137827cba2SAaron LI * notice, this list of conditions and the following disclaimer in the 147827cba2SAaron LI * documentation and/or other materials provided with the distribution. 157827cba2SAaron LI * 167827cba2SAaron LI * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 177827cba2SAaron LI * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 187827cba2SAaron LI * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 197827cba2SAaron LI * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 207827cba2SAaron LI * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 217827cba2SAaron LI * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 227827cba2SAaron LI * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 237827cba2SAaron LI * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 247827cba2SAaron LI * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 257827cba2SAaron LI * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 267827cba2SAaron LI * SUCH DAMAGE. 277827cba2SAaron LI */ 287827cba2SAaron LI 297827cba2SAaron LI #ifndef BPF_HEADER 307827cba2SAaron LI #define BPF_HEADER 317827cba2SAaron LI 32d4fb1e02SRoy Marples #define BPF_EOF 0x01U 33d4fb1e02SRoy Marples #define BPF_PARTIALCSUM 0x02U 34d4fb1e02SRoy Marples #define BPF_BCAST 0x04U 357827cba2SAaron LI 361b3b16a2SRoy Marples /* 371b3b16a2SRoy Marples * Even though we program the BPF filter should we trust it? 381b3b16a2SRoy Marples * On Linux at least there is a window between opening the socket, 391b3b16a2SRoy Marples * binding the interface and setting the filter where we receive data. 401b3b16a2SRoy Marples * This data is NOT checked OR flushed and IS returned when reading. 411b3b16a2SRoy Marples * We have no way of flushing it other than reading these packets! 421b3b16a2SRoy Marples * But we don't know if they passed the filter or not ..... so we need 431b3b16a2SRoy Marples * to validate each and every packet that comes through ourselves as well. 441b3b16a2SRoy Marples * Even if Linux does fix this sorry state, who is to say other kernels 451b3b16a2SRoy Marples * don't have bugs causing a similar effect? 461b3b16a2SRoy Marples * 471b3b16a2SRoy Marples * As such, let's strive to keep the filters just for pattern matching 481b3b16a2SRoy Marples * to avoid waking dhcpcd up. 491b3b16a2SRoy Marples * 501b3b16a2SRoy Marples * If you want to be notified of any packet failing the BPF filter, 511b3b16a2SRoy Marples * define BPF_DEBUG below. 521b3b16a2SRoy Marples */ 531b3b16a2SRoy Marples //#define BPF_DEBUG 541b3b16a2SRoy Marples 557827cba2SAaron LI #include "dhcpcd.h" 567827cba2SAaron LI 57d4fb1e02SRoy Marples struct bpf { 58d4fb1e02SRoy Marples const struct interface *bpf_ifp; 59d4fb1e02SRoy Marples int bpf_fd; 60d4fb1e02SRoy Marples unsigned int bpf_flags; 61d4fb1e02SRoy Marples void *bpf_buffer; 62d4fb1e02SRoy Marples size_t bpf_size; 63d4fb1e02SRoy Marples size_t bpf_len; 64d4fb1e02SRoy Marples size_t bpf_pos; 65d4fb1e02SRoy Marples }; 66d4fb1e02SRoy Marples 677827cba2SAaron LI extern const char *bpf_name; 687827cba2SAaron LI size_t bpf_frame_header_len(const struct interface *); 696e63cc1fSRoy Marples void *bpf_frame_header_src(const struct interface *, void *, size_t *); 706e63cc1fSRoy Marples void *bpf_frame_header_dst(const struct interface *, void *, size_t *); 71d4fb1e02SRoy Marples int bpf_frame_bcast(const struct interface *, const void *); 72d4fb1e02SRoy Marples struct bpf * bpf_open(const struct interface *, 73d4fb1e02SRoy Marples int (*)(const struct bpf *, const struct in_addr *), 74d4fb1e02SRoy Marples const struct in_addr *); 75d4fb1e02SRoy Marples void bpf_close(struct bpf *); 767827cba2SAaron LI int bpf_attach(int, void *, unsigned int); 77d4fb1e02SRoy Marples ssize_t bpf_send(const struct bpf *, uint16_t, const void *, size_t); 78d4fb1e02SRoy Marples ssize_t bpf_read(struct bpf *, void *, size_t); 79d4fb1e02SRoy Marples int bpf_arp(const struct bpf *, const struct in_addr *); 80d4fb1e02SRoy Marples int bpf_bootp(const struct bpf *, const struct in_addr *); 817827cba2SAaron LI #endif 82