xref: /minix3/external/bsd/dhcpcd/dist/bpf-filter.h (revision 9f20bfa6c4c442e2e798d91b11c2a5f8d6833a41)
1 /* $NetBSD: bpf-filter.h,v 1.9 2014/11/07 20:51:02 roy Exp $ */
2 
3 /*
4  * dhcpcd - DHCP client daemon
5  * Copyright (c) 2006-2008 Roy Marples <roy@marples.name>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef BPF_ETHCOOK
30 # define BPF_ETHCOOK 0
31 #endif
32 #ifndef BPF_WHOLEPACKET
33 # define BPF_WHOLEPACKET ~0U
34 #endif
35 static const struct bpf_insn arp_bpf_filter [] = {
36 #ifndef BPF_SKIPTYPE
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),
40 #endif
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),
44 	/* or ARP REPLY... */
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),
51 };
52 #define arp_bpf_filter_len sizeof(arp_bpf_filter) / sizeof(arp_bpf_filter[0])
53 
54 
55 /* dhcp_bpf_filter taken from bpf.c in dhcp-3.1.0
56  *
57  * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
58  * Copyright (c) 1996-2003 by Internet Software Consortium
59  *
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.
63  *
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.
71  *
72  *   Internet Systems Consortium, Inc.
73  *   950 Charter Street
74  *   Redwood City, CA 94063
75  *   <info@isc.org>
76  *   http://www.isc.org/
77  */
78 
79 static const struct bpf_insn dhcp_bpf_filter [] = {
80 #ifndef BPF_SKIPTYPE
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),
84 #endif
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),
100 };
101 #define dhcp_bpf_filter_len sizeof(dhcp_bpf_filter) / sizeof(dhcp_bpf_filter[0])
102