Samuel Tan | d7ed851 | 2015-08-13 16:11:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dhcpcd - DHCP client daemon |
| 3 | * Copyright (c) 2006-2008 Roy Marples <roy@marples.name> |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 24 | * SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #ifndef BPF_ETHCOOK |
| 28 | # define BPF_ETHCOOK 0 |
| 29 | #endif |
| 30 | #ifndef BPF_WHOLEPACKET |
| 31 | # define BPF_WHOLEPACKET ~0U |
| 32 | #endif |
| 33 | static const struct bpf_insn arp_bpf_filter [] = { |
| 34 | #ifndef BPF_SKIPTYPE |
| 35 | /* Make sure this is an ARP packet... */ |
| 36 | BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), |
| 37 | BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_ARP, 0, 3), |
| 38 | #endif |
| 39 | /* Make sure this is an ARP REQUEST... */ |
| 40 | BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK), |
| 41 | BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REQUEST, 2, 0), |
| 42 | /* or ARP REPLY... */ |
| 43 | BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK), |
| 44 | BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ARPOP_REPLY, 0, 1), |
| 45 | /* If we passed all the tests, ask for the whole packet. */ |
| 46 | BPF_STMT(BPF_RET + BPF_K, BPF_WHOLEPACKET), |
| 47 | /* Otherwise, drop it. */ |
| 48 | BPF_STMT(BPF_RET + BPF_K, 0), |
| 49 | }; |
| 50 | #define arp_bpf_filter_len sizeof(arp_bpf_filter) / sizeof(arp_bpf_filter[0]) |
| 51 | |
| 52 | |
| 53 | /* dhcp_bpf_filter taken from bpf.c in dhcp-3.1.0 |
| 54 | * |
| 55 | * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC") |
| 56 | * Copyright (c) 1996-2003 by Internet Software Consortium |
| 57 | * |
| 58 | * Permission to use, copy, modify, and distribute this software for any |
| 59 | * purpose with or without fee is hereby granted, provided that the above |
| 60 | * copyright notice and this permission notice appear in all copies. |
| 61 | * |
| 62 | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
| 63 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 64 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
| 65 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 66 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 67 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 68 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 69 | * |
| 70 | * Internet Systems Consortium, Inc. |
| 71 | * 950 Charter Street |
| 72 | * Redwood City, CA 94063 |
| 73 | * <info@isc.org> |
| 74 | * http://www.isc.org/ |
| 75 | */ |
| 76 | |
| 77 | static const struct bpf_insn dhcp_bpf_filter [] = { |
| 78 | #ifndef BPF_SKIPTYPE |
| 79 | /* Make sure this is an IP packet... */ |
| 80 | BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), |
| 81 | BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8), |
| 82 | #endif |
| 83 | /* Make sure it's a UDP packet... */ |
| 84 | BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23 + BPF_ETHCOOK), |
| 85 | BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6), |
| 86 | /* Make sure this isn't a fragment... */ |
| 87 | BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK), |
| 88 | BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0), |
| 89 | /* Get the IP header length... */ |
| 90 | BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14 + BPF_ETHCOOK), |
| 91 | /* Make sure it's to the right port... */ |
| 92 | BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16 + BPF_ETHCOOK), |
| 93 | BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1), |
| 94 | /* If we passed all the tests, ask for the whole packet. */ |
| 95 | BPF_STMT(BPF_RET + BPF_K, BPF_WHOLEPACKET), |
| 96 | /* Otherwise, drop it. */ |
| 97 | BPF_STMT(BPF_RET + BPF_K, 0), |
| 98 | }; |
| 99 | #define dhcp_bpf_filter_len sizeof(dhcp_bpf_filter) / sizeof(dhcp_bpf_filter[0]) |