xref: /openbsd/usr.sbin/dhcpd/bpf.c (revision 15035272)
1*15035272Skrw /*	$OpenBSD: bpf.c,v 1.6 2005/07/29 17:26:28 krw Exp $	*/
2e853bc5dShenning 
3e853bc5dShenning /* BPF socket interface code, originally contributed by Archie Cobbs. */
4e853bc5dShenning 
5e853bc5dShenning /*
6e853bc5dShenning  * Copyright (c) 1995, 1996, 1998, 1999
7e853bc5dShenning  * The Internet Software Consortium.    All rights reserved.
8e853bc5dShenning  *
9e853bc5dShenning  * Redistribution and use in source and binary forms, with or without
10e853bc5dShenning  * modification, are permitted provided that the following conditions
11e853bc5dShenning  * are met:
12e853bc5dShenning  *
13e853bc5dShenning  * 1. Redistributions of source code must retain the above copyright
14e853bc5dShenning  *    notice, this list of conditions and the following disclaimer.
15e853bc5dShenning  * 2. Redistributions in binary form must reproduce the above copyright
16e853bc5dShenning  *    notice, this list of conditions and the following disclaimer in the
17e853bc5dShenning  *    documentation and/or other materials provided with the distribution.
18e853bc5dShenning  * 3. Neither the name of The Internet Software Consortium nor the names
19e853bc5dShenning  *    of its contributors may be used to endorse or promote products derived
20e853bc5dShenning  *    from this software without specific prior written permission.
21e853bc5dShenning  *
22e853bc5dShenning  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23e853bc5dShenning  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24e853bc5dShenning  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25e853bc5dShenning  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26e853bc5dShenning  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27e853bc5dShenning  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28e853bc5dShenning  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29e853bc5dShenning  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30e853bc5dShenning  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31e853bc5dShenning  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32e853bc5dShenning  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33e853bc5dShenning  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34e853bc5dShenning  * SUCH DAMAGE.
35e853bc5dShenning  *
36e853bc5dShenning  * This software has been written for the Internet Software Consortium
37e853bc5dShenning  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38e853bc5dShenning  * Enterprises.  To learn more about the Internet Software Consortium,
39e853bc5dShenning  * see ``http://www.vix.com/isc''.  To learn more about Vixie
40e853bc5dShenning  * Enterprises, see ``http://www.vix.com''.
41e853bc5dShenning  */
42e853bc5dShenning 
43e853bc5dShenning #include "dhcpd.h"
44e853bc5dShenning #include <sys/ioctl.h>
45e853bc5dShenning #include <sys/uio.h>
46e853bc5dShenning 
47e853bc5dShenning #include <net/bpf.h>
48e853bc5dShenning #include <netinet/in_systm.h>
49e853bc5dShenning #include <netinet/ip.h>
50e853bc5dShenning #include <netinet/udp.h>
51e853bc5dShenning #include <netinet/if_ether.h>
52e853bc5dShenning 
53e853bc5dShenning #define BPF_FORMAT "/dev/bpf%d"
54e853bc5dShenning 
55e853bc5dShenning /*
56e853bc5dShenning  * Called by get_interface_list for each interface that's discovered.
57e853bc5dShenning  * Opens a packet filter for each interface and adds it to the select
58e853bc5dShenning  * mask.
59e853bc5dShenning  */
60e853bc5dShenning int
61e853bc5dShenning if_register_bpf(struct interface_info *info)
62e853bc5dShenning {
63e853bc5dShenning 	char filename[50];
64e853bc5dShenning 	int sock, b;
65e853bc5dShenning 
66e853bc5dShenning 	/* Open a BPF device */
67e853bc5dShenning 	for (b = 0; 1; b++) {
68e853bc5dShenning 		snprintf(filename, sizeof(filename), BPF_FORMAT, b);
69e853bc5dShenning 		sock = open(filename, O_RDWR, 0);
70e853bc5dShenning 		if (sock < 0) {
71e853bc5dShenning 			if (errno == EBUSY)
72e853bc5dShenning 				continue;
73e853bc5dShenning 			else
74e853bc5dShenning 				error("Can't find free bpf: %m");
75e853bc5dShenning 		} else
76e853bc5dShenning 			break;
77e853bc5dShenning 	}
78e853bc5dShenning 
79e853bc5dShenning 	/* Set the BPF device to point at this interface. */
80e853bc5dShenning 	if (ioctl(sock, BIOCSETIF, info->ifp) < 0)
81e853bc5dShenning 		error("Can't attach interface %s to bpf device %s: %m",
82e853bc5dShenning 		    info->name, filename);
83e853bc5dShenning 
84e853bc5dShenning 	return (sock);
85e853bc5dShenning }
86e853bc5dShenning 
87e853bc5dShenning void
88e853bc5dShenning if_register_send(struct interface_info *info)
89e853bc5dShenning {
90e853bc5dShenning 	/*
91e853bc5dShenning 	 * If we're using the bpf API for sending and receiving, we
92e853bc5dShenning 	 * don't need to register this interface twice.
93e853bc5dShenning 	 */
94e853bc5dShenning 	info->wfdesc = info->rfdesc;
95e853bc5dShenning }
96e853bc5dShenning 
97e853bc5dShenning /*
98390956b7Scanacar  * Packet read filter program: 'ip and udp and dst port bootps'
99e853bc5dShenning  */
100e853bc5dShenning struct bpf_insn dhcp_bpf_filter[] = {
101e853bc5dShenning 	/* Make sure this is an IP packet... */
102e853bc5dShenning 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
103e853bc5dShenning 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
104e853bc5dShenning 
105e853bc5dShenning 	/* Make sure it's a UDP packet... */
106e853bc5dShenning 	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
107e853bc5dShenning 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
108e853bc5dShenning 
109e853bc5dShenning 	/* Make sure this isn't a fragment... */
110e853bc5dShenning 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
111e853bc5dShenning 	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
112e853bc5dShenning 
113e853bc5dShenning 	/* Get the IP header length... */
114e853bc5dShenning 	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
115e853bc5dShenning 
116e853bc5dShenning 	/* Make sure it's to the right port... */
117e853bc5dShenning 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
118390956b7Scanacar 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1),
119e853bc5dShenning 
120e853bc5dShenning 	/* If we passed all the tests, ask for the whole packet. */
121e853bc5dShenning 	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
122e853bc5dShenning 
123e853bc5dShenning 	/* Otherwise, drop it. */
124e853bc5dShenning 	BPF_STMT(BPF_RET+BPF_K, 0),
125e853bc5dShenning };
126e853bc5dShenning 
127e853bc5dShenning int dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn);
128e853bc5dShenning 
129390956b7Scanacar 
130390956b7Scanacar /*
131390956b7Scanacar  * Packet write filter program:
132390956b7Scanacar  * 'ip and udp and src port bootps and dst port (bootps or bootpc)'
133390956b7Scanacar  */
134390956b7Scanacar struct bpf_insn dhcp_bpf_wfilter[] = {
135390956b7Scanacar 	/* Make sure this is an IP packet... */
136390956b7Scanacar 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
137390956b7Scanacar 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 11),
138390956b7Scanacar 
139390956b7Scanacar 	/* Make sure it's a UDP packet... */
140390956b7Scanacar 	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
141390956b7Scanacar 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 9),
142390956b7Scanacar 
143390956b7Scanacar 	/* Make sure this isn't a fragment... */
144390956b7Scanacar 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
145390956b7Scanacar 	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 7, 0),
146390956b7Scanacar 
147390956b7Scanacar 	/* Get the IP header length... */
148390956b7Scanacar 	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
149390956b7Scanacar 
150390956b7Scanacar 	/* Make sure it's from the right port... */
151390956b7Scanacar 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
152390956b7Scanacar 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 4),
153390956b7Scanacar 
154390956b7Scanacar 	/* Make sure it is to the right ports ... */
155390956b7Scanacar 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
156390956b7Scanacar 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, CLIENT_PORT, 1, 0),
157390956b7Scanacar 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1),
158390956b7Scanacar 
159390956b7Scanacar 	/* If we passed all the tests, ask for the whole packet. */
160390956b7Scanacar 	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
161390956b7Scanacar 
162390956b7Scanacar 	/* Otherwise, drop it. */
163390956b7Scanacar 	BPF_STMT(BPF_RET+BPF_K, 0),
164390956b7Scanacar };
165390956b7Scanacar 
166390956b7Scanacar int dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn);
167390956b7Scanacar 
168e853bc5dShenning void
169e853bc5dShenning if_register_receive(struct interface_info *info)
170e853bc5dShenning {
171e853bc5dShenning 	struct bpf_version v;
172e853bc5dShenning 	struct bpf_program p;
173390956b7Scanacar 	int flag = 1, sz, cmplt = 0;
174e853bc5dShenning 
175e853bc5dShenning 	/* Open a BPF device and hang it on this interface... */
176e853bc5dShenning 	info->rfdesc = if_register_bpf(info);
177e853bc5dShenning 
178e853bc5dShenning 	/* Make sure the BPF version is in range... */
179e853bc5dShenning 	if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0)
180e853bc5dShenning 		error("Can't get BPF version: %m");
181e853bc5dShenning 
182e853bc5dShenning 	if (v.bv_major != BPF_MAJOR_VERSION ||
183e853bc5dShenning 	    v.bv_minor < BPF_MINOR_VERSION)
184e853bc5dShenning 		error("Kernel BPF version out of range - recompile dhcpd!");
185e853bc5dShenning 
186e853bc5dShenning 	/*
187e853bc5dShenning 	 * Set immediate mode so that reads return as soon as a packet
188e853bc5dShenning 	 * comes in, rather than waiting for the input buffer to fill
189e853bc5dShenning 	 * with packets.
190e853bc5dShenning 	 */
191e853bc5dShenning 	if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0)
192e853bc5dShenning 		error("Can't set immediate mode on bpf device: %m");
193e853bc5dShenning 
194390956b7Scanacar 	/* make sure kernel fills in the source ethernet address */
195390956b7Scanacar 	if (ioctl(info->rfdesc, BIOCSHDRCMPLT, &cmplt) < 0)
196390956b7Scanacar 		error("Can't set header complete flag on bpf device: %m");
197390956b7Scanacar 
198e853bc5dShenning 	/* Get the required BPF buffer length from the kernel. */
199e853bc5dShenning 	if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0)
200e853bc5dShenning 		error("Can't get bpf buffer length: %m");
201e853bc5dShenning 	info->rbuf_max = sz;
202e853bc5dShenning 	info->rbuf = malloc(info->rbuf_max);
203e853bc5dShenning 	if (!info->rbuf)
204e853bc5dShenning 		error("Can't allocate %lu bytes for bpf input buffer.",
205e853bc5dShenning 		    (unsigned long)info->rbuf_max);
206e853bc5dShenning 	info->rbuf_offset = 0;
207e853bc5dShenning 	info->rbuf_len = 0;
208e853bc5dShenning 
209e853bc5dShenning 	/* Set up the bpf filter program structure. */
210e853bc5dShenning 	p.bf_len = dhcp_bpf_filter_len;
211e853bc5dShenning 	p.bf_insns = dhcp_bpf_filter;
212e853bc5dShenning 
213e853bc5dShenning 	if (ioctl(info->rfdesc, BIOCSETF, &p) < 0)
214e853bc5dShenning 		error("Can't install packet filter program: %m");
215390956b7Scanacar 
216390956b7Scanacar 	/* Set up the bpf write filter program structure. */
217390956b7Scanacar 	p.bf_len = dhcp_bpf_wfilter_len;
218390956b7Scanacar 	p.bf_insns = dhcp_bpf_wfilter;
219390956b7Scanacar 
220390956b7Scanacar 	if (ioctl(info->rfdesc, BIOCSETWF, &p) < 0)
221390956b7Scanacar 		error("Can't install write filter program: %m");
222390956b7Scanacar 
223390956b7Scanacar 	/* make sure these settings cannot be changed after dropping privs */
224390956b7Scanacar 	if (ioctl(info->rfdesc, BIOCLOCK) < 0)
225390956b7Scanacar 		error("Failed to lock bpf descriptor: %m");
226e853bc5dShenning }
227e853bc5dShenning 
228e853bc5dShenning ssize_t
229285f06efSderaadt send_packet(struct interface_info *interface, struct dhcp_packet *raw,
230285f06efSderaadt     size_t len, struct in_addr from, struct sockaddr_in *to,
231285f06efSderaadt     struct hardware *hto)
232e853bc5dShenning {
233e853bc5dShenning 	unsigned char buf[256];
234e853bc5dShenning 	struct iovec iov[2];
235e853bc5dShenning 	int result, bufp = 0;
236e853bc5dShenning 
237e853bc5dShenning 	/* Assemble the headers... */
238e853bc5dShenning 	assemble_hw_header(interface, buf, &bufp, hto);
239e853bc5dShenning 	assemble_udp_ip_header(interface, buf, &bufp, from.s_addr,
240e853bc5dShenning 	    to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len);
241e853bc5dShenning 
242e853bc5dShenning 	/* Fire it off */
243e853bc5dShenning 	iov[0].iov_base = (char *)buf;
244e853bc5dShenning 	iov[0].iov_len = bufp;
245e853bc5dShenning 	iov[1].iov_base = (char *)raw;
246e853bc5dShenning 	iov[1].iov_len = len;
247e853bc5dShenning 
248e853bc5dShenning 	result = writev(interface->wfdesc, iov, 2);
249e853bc5dShenning 	if (result < 0)
2500795b389Sderaadt 		warning("send_packet: %m");
251e853bc5dShenning 	return (result);
252e853bc5dShenning }
253e853bc5dShenning 
254e853bc5dShenning ssize_t
255e853bc5dShenning receive_packet(struct interface_info *interface, unsigned char *buf,
256e853bc5dShenning     size_t len, struct sockaddr_in *from, struct hardware *hfrom)
257e853bc5dShenning {
258e853bc5dShenning 	int length = 0, offset = 0;
259e853bc5dShenning 	struct bpf_hdr hdr;
260e853bc5dShenning 
261e853bc5dShenning 	/*
262e853bc5dShenning 	 * All this complexity is because BPF doesn't guarantee that
263e853bc5dShenning 	 * only one packet will be returned at a time.  We're getting
264e853bc5dShenning 	 * what we deserve, though - this is a terrible abuse of the BPF
265e853bc5dShenning 	 * interface.  Sigh.
266e853bc5dShenning 	 */
267e853bc5dShenning 
268e853bc5dShenning 	/* Process packets until we get one we can return or until we've
269e853bc5dShenning 	 * done a read and gotten nothing we can return...
270e853bc5dShenning 	 */
271e853bc5dShenning 	do {
272e853bc5dShenning 		/* If the buffer is empty, fill it. */
273e853bc5dShenning 		if (interface->rbuf_offset == interface->rbuf_len) {
274e853bc5dShenning 			length = read(interface->rfdesc, interface->rbuf,
275e853bc5dShenning 			    interface->rbuf_max);
276e853bc5dShenning 			if (length <= 0)
277e853bc5dShenning 				return (length);
278e853bc5dShenning 			interface->rbuf_offset = 0;
279*15035272Skrw 			interface->rbuf_len = BPF_WORDALIGN(length);
280e853bc5dShenning 		}
281e853bc5dShenning 
282e853bc5dShenning 		/*
283e853bc5dShenning 		 * If there isn't room for a whole bpf header, something
284e853bc5dShenning 		 * went wrong, but we'll ignore it and hope it goes
285e853bc5dShenning 		 * away... XXX
286e853bc5dShenning 		 */
287e853bc5dShenning 		if (interface->rbuf_len - interface->rbuf_offset <
288e853bc5dShenning 		    sizeof(hdr)) {
289e853bc5dShenning 			interface->rbuf_offset = interface->rbuf_len;
290e853bc5dShenning 			continue;
291e853bc5dShenning 		}
292e853bc5dShenning 
293e853bc5dShenning 		/* Copy out a bpf header... */
294e853bc5dShenning 		memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
295e853bc5dShenning 		    sizeof(hdr));
296e853bc5dShenning 
297e853bc5dShenning 		/*
298e853bc5dShenning 		 * If the bpf header plus data doesn't fit in what's
299e853bc5dShenning 		 * left of the buffer, stick head in sand yet again...
300e853bc5dShenning 		 */
301e853bc5dShenning 		if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
302e853bc5dShenning 		    interface->rbuf_len) {
303e853bc5dShenning 			interface->rbuf_offset = interface->rbuf_len;
304e853bc5dShenning 			continue;
305e853bc5dShenning 		}
306e853bc5dShenning 
307e853bc5dShenning 		/*
308e853bc5dShenning 		 * If the captured data wasn't the whole packet, or if
309e853bc5dShenning 		 * the packet won't fit in the input buffer, all we can
310e853bc5dShenning 		 * do is drop it.
311e853bc5dShenning 		 */
312e853bc5dShenning 		if (hdr.bh_caplen != hdr.bh_datalen) {
313*15035272Skrw 			interface->rbuf_offset = BPF_WORDALIGN(
314*15035272Skrw 			    interface->rbuf_offset + hdr.bh_hdrlen +
315*15035272Skrw 			    hdr.bh_caplen);
316e853bc5dShenning 			continue;
317e853bc5dShenning 		}
318e853bc5dShenning 
319e853bc5dShenning 		/* Skip over the BPF header... */
320e853bc5dShenning 		interface->rbuf_offset += hdr.bh_hdrlen;
321e853bc5dShenning 
322e853bc5dShenning 		/* Decode the physical header... */
323e853bc5dShenning 		offset = decode_hw_header(interface,
324e853bc5dShenning 		    interface->rbuf, interface->rbuf_offset, hfrom);
325e853bc5dShenning 
326e853bc5dShenning 		/*
327e853bc5dShenning 		 * If a physical layer checksum failed (dunno of any
328e853bc5dShenning 		 * physical layer that supports this, but WTH), skip
329e853bc5dShenning 		 * this packet.
330e853bc5dShenning 		 */
331e853bc5dShenning 		if (offset < 0) {
332*15035272Skrw 			interface->rbuf_offset = BPF_WORDALIGN(
333*15035272Skrw 			    interface->rbuf_offset + hdr.bh_caplen);
334e853bc5dShenning 			continue;
335e853bc5dShenning 		}
336e853bc5dShenning 		interface->rbuf_offset += offset;
337e853bc5dShenning 		hdr.bh_caplen -= offset;
338e853bc5dShenning 
339e853bc5dShenning 		/* Decode the IP and UDP headers... */
340e853bc5dShenning 		offset = decode_udp_ip_header(interface, interface->rbuf,
341e853bc5dShenning 		    interface->rbuf_offset, from, NULL, hdr.bh_caplen);
342e853bc5dShenning 
343e853bc5dShenning 		/* If the IP or UDP checksum was bad, skip the packet... */
344e853bc5dShenning 		if (offset < 0) {
345*15035272Skrw 			interface->rbuf_offset = BPF_WORDALIGN(
346*15035272Skrw 			    interface->rbuf_offset + hdr.bh_caplen);
347e853bc5dShenning 			continue;
348e853bc5dShenning 		}
349e853bc5dShenning 		interface->rbuf_offset += offset;
350e853bc5dShenning 		hdr.bh_caplen -= offset;
351e853bc5dShenning 
352e853bc5dShenning 		/*
353e853bc5dShenning 		 * If there's not enough room to stash the packet data,
354e853bc5dShenning 		 * we have to skip it (this shouldn't happen in real
355e853bc5dShenning 		 * life, though).
356e853bc5dShenning 		 */
357e853bc5dShenning 		if (hdr.bh_caplen > len) {
358*15035272Skrw 			interface->rbuf_offset = BPF_WORDALIGN(
359*15035272Skrw 			    interface->rbuf_offset + hdr.bh_caplen);
360e853bc5dShenning 			continue;
361e853bc5dShenning 		}
362e853bc5dShenning 
363e853bc5dShenning 		/* Copy out the data in the packet... */
364e853bc5dShenning 		memcpy(buf, interface->rbuf + interface->rbuf_offset,
365e853bc5dShenning 		    hdr.bh_caplen);
366*15035272Skrw 		interface->rbuf_offset = BPF_WORDALIGN(interface->rbuf_offset +
367*15035272Skrw 		    hdr.bh_caplen);
368e853bc5dShenning 		return (hdr.bh_caplen);
369e853bc5dShenning 	} while (!length);
370e853bc5dShenning 	return (0);
371e853bc5dShenning }
372