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