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 const 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 static const size_t arp_bpf_filter_len =
51     sizeof(arp_bpf_filter) / sizeof(arp_bpf_filter[0]);
52 
53 
54 /* dhcp_bpf_filter taken from bpf.c in dhcp-3.1.0
55  *
56  * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
57  * Copyright (c) 1996-2003 by Internet Software Consortium
58  *
59  * Permission to use, copy, modify, and distribute this software for any
60  * purpose with or without fee is hereby granted, provided that the above
61  * copyright notice and this permission notice appear in all copies.
62  *
63  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
64  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
65  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
66  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
67  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
68  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
69  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
70  *
71  *   Internet Systems Consortium, Inc.
72  *   950 Charter Street
73  *   Redwood City, CA 94063
74  *   <info@isc.org>
75  *   http://www.isc.org/
76  */
77 
78 static const struct bpf_insn const dhcp_bpf_filter [] = {
79 #ifndef BPF_SKIPTYPE
80 	/* Make sure this is an IP packet... */
81 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
82 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
83 #endif
84 	/* Make sure it's a UDP packet... */
85 	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23 + BPF_ETHCOOK),
86 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
87 	/* Make sure this isn't a fragment... */
88 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20 + BPF_ETHCOOK),
89 	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
90 	/* Get the IP header length... */
91 	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14 + BPF_ETHCOOK),
92 	/* Make sure it's to the right port... */
93 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16 + BPF_ETHCOOK),
94 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, DHCP_CLIENT_PORT, 0, 1),
95 	/* If we passed all the tests, ask for the whole packet. */
96 	BPF_STMT(BPF_RET + BPF_K, BPF_WHOLEPACKET),
97 	/* Otherwise, drop it. */
98 	BPF_STMT(BPF_RET + BPF_K, 0),
99 };
100 static const size_t dhcp_bpf_filter_len =
101     sizeof(dhcp_bpf_filter) / sizeof(dhcp_bpf_filter[0]);
102