xref: /openbsd/usr.sbin/dhcpd/bpf.c (revision 1a4d4b6b)
1*1a4d4b6bSkrw /*	$OpenBSD: bpf.c,v 1.10 2013/04/05 19:31:36 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);
709bb003e4Sclaudio 		if (sock == -1) {
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. */
809bb003e4Sclaudio 	if (ioctl(sock, BIOCSETIF, info->ifp) == -1)
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... */
1799bb003e4Sclaudio 	if (ioctl(info->rfdesc, BIOCVERSION, &v) == -1)
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 	 */
1919bb003e4Sclaudio 	if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) == -1)
192e853bc5dShenning 		error("Can't set immediate mode on bpf device: %m");
193e853bc5dShenning 
194e7791a15Smpf 	if (ioctl(info->rfdesc, BIOCSFILDROP, &flag) == -1)
195e7791a15Smpf 		error("Can't set filter-drop mode on bpf device: %m");
196e7791a15Smpf 
197390956b7Scanacar 	/* make sure kernel fills in the source ethernet address */
1989bb003e4Sclaudio 	if (ioctl(info->rfdesc, BIOCSHDRCMPLT, &cmplt) == -1)
199390956b7Scanacar 		error("Can't set header complete flag on bpf device: %m");
200390956b7Scanacar 
201e853bc5dShenning 	/* Get the required BPF buffer length from the kernel. */
2029bb003e4Sclaudio 	if (ioctl(info->rfdesc, BIOCGBLEN, &sz) == -1)
203e853bc5dShenning 		error("Can't get bpf buffer length: %m");
204e853bc5dShenning 	info->rbuf_max = sz;
205e853bc5dShenning 	info->rbuf = malloc(info->rbuf_max);
206e853bc5dShenning 	if (!info->rbuf)
207e853bc5dShenning 		error("Can't allocate %lu bytes for bpf input buffer.",
208e853bc5dShenning 		    (unsigned long)info->rbuf_max);
209e853bc5dShenning 	info->rbuf_offset = 0;
210e853bc5dShenning 	info->rbuf_len = 0;
211e853bc5dShenning 
212e853bc5dShenning 	/* Set up the bpf filter program structure. */
213e853bc5dShenning 	p.bf_len = dhcp_bpf_filter_len;
214e853bc5dShenning 	p.bf_insns = dhcp_bpf_filter;
215e853bc5dShenning 
2169bb003e4Sclaudio 	if (ioctl(info->rfdesc, BIOCSETF, &p) == -1)
217e853bc5dShenning 		error("Can't install packet filter program: %m");
218390956b7Scanacar 
219390956b7Scanacar 	/* Set up the bpf write filter program structure. */
220390956b7Scanacar 	p.bf_len = dhcp_bpf_wfilter_len;
221390956b7Scanacar 	p.bf_insns = dhcp_bpf_wfilter;
222390956b7Scanacar 
2239bb003e4Sclaudio 	if (ioctl(info->rfdesc, BIOCSETWF, &p) == -1)
224390956b7Scanacar 		error("Can't install write filter program: %m");
225390956b7Scanacar 
226390956b7Scanacar 	/* make sure these settings cannot be changed after dropping privs */
2279bb003e4Sclaudio 	if (ioctl(info->rfdesc, BIOCLOCK) == -1)
228390956b7Scanacar 		error("Failed to lock bpf descriptor: %m");
229e853bc5dShenning }
230e853bc5dShenning 
231e853bc5dShenning ssize_t
232285f06efSderaadt send_packet(struct interface_info *interface, struct dhcp_packet *raw,
233285f06efSderaadt     size_t len, struct in_addr from, struct sockaddr_in *to,
234285f06efSderaadt     struct hardware *hto)
235e853bc5dShenning {
236e853bc5dShenning 	unsigned char buf[256];
237e853bc5dShenning 	struct iovec iov[2];
238*1a4d4b6bSkrw 	ssize_t result;
239*1a4d4b6bSkrw 	int bufp = 0;
240e853bc5dShenning 
241e853bc5dShenning 	/* Assemble the headers... */
242e853bc5dShenning 	assemble_hw_header(interface, buf, &bufp, hto);
243e853bc5dShenning 	assemble_udp_ip_header(interface, buf, &bufp, from.s_addr,
244e853bc5dShenning 	    to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len);
245e853bc5dShenning 
246e853bc5dShenning 	/* Fire it off */
247e853bc5dShenning 	iov[0].iov_base = (char *)buf;
248e853bc5dShenning 	iov[0].iov_len = bufp;
249e853bc5dShenning 	iov[1].iov_base = (char *)raw;
250e853bc5dShenning 	iov[1].iov_len = len;
251e853bc5dShenning 
252e853bc5dShenning 	result = writev(interface->wfdesc, iov, 2);
2539bb003e4Sclaudio 	if (result == -1)
2540795b389Sderaadt 		warning("send_packet: %m");
255e853bc5dShenning 	return (result);
256e853bc5dShenning }
257e853bc5dShenning 
258e853bc5dShenning ssize_t
259e853bc5dShenning receive_packet(struct interface_info *interface, unsigned char *buf,
260e853bc5dShenning     size_t len, struct sockaddr_in *from, struct hardware *hfrom)
261e853bc5dShenning {
262e853bc5dShenning 	int length = 0, offset = 0;
263e853bc5dShenning 	struct bpf_hdr hdr;
264e853bc5dShenning 
265e853bc5dShenning 	/*
266e853bc5dShenning 	 * All this complexity is because BPF doesn't guarantee that
267e853bc5dShenning 	 * only one packet will be returned at a time.  We're getting
268e853bc5dShenning 	 * what we deserve, though - this is a terrible abuse of the BPF
269e853bc5dShenning 	 * interface.  Sigh.
270e853bc5dShenning 	 */
271e853bc5dShenning 
272e853bc5dShenning 	/* Process packets until we get one we can return or until we've
273e853bc5dShenning 	 * done a read and gotten nothing we can return...
274e853bc5dShenning 	 */
275e853bc5dShenning 	do {
276e853bc5dShenning 		/* If the buffer is empty, fill it. */
277e853bc5dShenning 		if (interface->rbuf_offset == interface->rbuf_len) {
278e853bc5dShenning 			length = read(interface->rfdesc, interface->rbuf,
279e853bc5dShenning 			    interface->rbuf_max);
280e853bc5dShenning 			if (length <= 0)
281e853bc5dShenning 				return (length);
282e853bc5dShenning 			interface->rbuf_offset = 0;
28315035272Skrw 			interface->rbuf_len = BPF_WORDALIGN(length);
284e853bc5dShenning 		}
285e853bc5dShenning 
286e853bc5dShenning 		/*
287e853bc5dShenning 		 * If there isn't room for a whole bpf header, something
288e853bc5dShenning 		 * went wrong, but we'll ignore it and hope it goes
289e853bc5dShenning 		 * away... XXX
290e853bc5dShenning 		 */
291e853bc5dShenning 		if (interface->rbuf_len - interface->rbuf_offset <
292e853bc5dShenning 		    sizeof(hdr)) {
293e853bc5dShenning 			interface->rbuf_offset = interface->rbuf_len;
294e853bc5dShenning 			continue;
295e853bc5dShenning 		}
296e853bc5dShenning 
297e853bc5dShenning 		/* Copy out a bpf header... */
298e853bc5dShenning 		memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
299e853bc5dShenning 		    sizeof(hdr));
300e853bc5dShenning 
301e853bc5dShenning 		/*
302e853bc5dShenning 		 * If the bpf header plus data doesn't fit in what's
303e853bc5dShenning 		 * left of the buffer, stick head in sand yet again...
304e853bc5dShenning 		 */
305e853bc5dShenning 		if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
306e853bc5dShenning 		    interface->rbuf_len) {
307e853bc5dShenning 			interface->rbuf_offset = interface->rbuf_len;
308e853bc5dShenning 			continue;
309e853bc5dShenning 		}
310e853bc5dShenning 
311e853bc5dShenning 		/*
312e853bc5dShenning 		 * If the captured data wasn't the whole packet, or if
313e853bc5dShenning 		 * the packet won't fit in the input buffer, all we can
314e853bc5dShenning 		 * do is drop it.
315e853bc5dShenning 		 */
316e853bc5dShenning 		if (hdr.bh_caplen != hdr.bh_datalen) {
31715035272Skrw 			interface->rbuf_offset = BPF_WORDALIGN(
31815035272Skrw 			    interface->rbuf_offset + hdr.bh_hdrlen +
31915035272Skrw 			    hdr.bh_caplen);
320e853bc5dShenning 			continue;
321e853bc5dShenning 		}
322e853bc5dShenning 
323e853bc5dShenning 		/* Skip over the BPF header... */
324e853bc5dShenning 		interface->rbuf_offset += hdr.bh_hdrlen;
325e853bc5dShenning 
326e853bc5dShenning 		/* Decode the physical header... */
327e853bc5dShenning 		offset = decode_hw_header(interface,
328e853bc5dShenning 		    interface->rbuf, interface->rbuf_offset, hfrom);
329e853bc5dShenning 
330e853bc5dShenning 		/*
331e853bc5dShenning 		 * If a physical layer checksum failed (dunno of any
332e853bc5dShenning 		 * physical layer that supports this, but WTH), skip
333e853bc5dShenning 		 * this packet.
334e853bc5dShenning 		 */
335e853bc5dShenning 		if (offset < 0) {
33615035272Skrw 			interface->rbuf_offset = BPF_WORDALIGN(
33715035272Skrw 			    interface->rbuf_offset + hdr.bh_caplen);
338e853bc5dShenning 			continue;
339e853bc5dShenning 		}
340e853bc5dShenning 		interface->rbuf_offset += offset;
341e853bc5dShenning 		hdr.bh_caplen -= offset;
342e853bc5dShenning 
343e853bc5dShenning 		/* Decode the IP and UDP headers... */
344e853bc5dShenning 		offset = decode_udp_ip_header(interface, interface->rbuf,
3459c0483d2Skrw 		    interface->rbuf_offset, from, hdr.bh_caplen);
346e853bc5dShenning 
347e853bc5dShenning 		/* If the IP or UDP checksum was bad, skip the packet... */
348e853bc5dShenning 		if (offset < 0) {
34915035272Skrw 			interface->rbuf_offset = BPF_WORDALIGN(
35015035272Skrw 			    interface->rbuf_offset + hdr.bh_caplen);
351e853bc5dShenning 			continue;
352e853bc5dShenning 		}
353e853bc5dShenning 		interface->rbuf_offset += offset;
354e853bc5dShenning 		hdr.bh_caplen -= offset;
355e853bc5dShenning 
356e853bc5dShenning 		/*
357e853bc5dShenning 		 * If there's not enough room to stash the packet data,
358e853bc5dShenning 		 * we have to skip it (this shouldn't happen in real
359e853bc5dShenning 		 * life, though).
360e853bc5dShenning 		 */
361e853bc5dShenning 		if (hdr.bh_caplen > len) {
36215035272Skrw 			interface->rbuf_offset = BPF_WORDALIGN(
36315035272Skrw 			    interface->rbuf_offset + hdr.bh_caplen);
364e853bc5dShenning 			continue;
365e853bc5dShenning 		}
366e853bc5dShenning 
367e853bc5dShenning 		/* Copy out the data in the packet... */
368e853bc5dShenning 		memcpy(buf, interface->rbuf + interface->rbuf_offset,
369e853bc5dShenning 		    hdr.bh_caplen);
37015035272Skrw 		interface->rbuf_offset = BPF_WORDALIGN(interface->rbuf_offset +
37115035272Skrw 		    hdr.bh_caplen);
372e853bc5dShenning 		return (hdr.bh_caplen);
373e853bc5dShenning 	} while (!length);
374e853bc5dShenning 	return (0);
375e853bc5dShenning }
376