bpf.c revision 162641
1147072Sbrooks/*	$OpenBSD: bpf.c,v 1.13 2004/05/05 14:28:58 deraadt Exp $	*/
2147072Sbrooks
3147072Sbrooks/* BPF socket interface code, originally contributed by Archie Cobbs. */
4147072Sbrooks
5147072Sbrooks/*
6147072Sbrooks * Copyright (c) 1995, 1996, 1998, 1999
7147072Sbrooks * The Internet Software Consortium.    All rights reserved.
8147072Sbrooks *
9147072Sbrooks * Redistribution and use in source and binary forms, with or without
10147072Sbrooks * modification, are permitted provided that the following conditions
11147072Sbrooks * are met:
12147072Sbrooks *
13147072Sbrooks * 1. Redistributions of source code must retain the above copyright
14147072Sbrooks *    notice, this list of conditions and the following disclaimer.
15147072Sbrooks * 2. Redistributions in binary form must reproduce the above copyright
16147072Sbrooks *    notice, this list of conditions and the following disclaimer in the
17147072Sbrooks *    documentation and/or other materials provided with the distribution.
18147072Sbrooks * 3. Neither the name of The Internet Software Consortium nor the names
19147072Sbrooks *    of its contributors may be used to endorse or promote products derived
20147072Sbrooks *    from this software without specific prior written permission.
21147072Sbrooks *
22147072Sbrooks * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23147072Sbrooks * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24147072Sbrooks * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25147072Sbrooks * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26147072Sbrooks * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27147072Sbrooks * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28147072Sbrooks * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29147072Sbrooks * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30147072Sbrooks * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31147072Sbrooks * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32147072Sbrooks * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33147072Sbrooks * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34147072Sbrooks * SUCH DAMAGE.
35147072Sbrooks *
36147072Sbrooks * This software has been written for the Internet Software Consortium
37147072Sbrooks * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38147072Sbrooks * Enterprises.  To learn more about the Internet Software Consortium,
39147072Sbrooks * see ``http://www.vix.com/isc''.  To learn more about Vixie
40147072Sbrooks * Enterprises, see ``http://www.vix.com''.
41147072Sbrooks */
42147072Sbrooks
43149399Sbrooks#include <sys/cdefs.h>
44149399Sbrooks__FBSDID("$FreeBSD: head/sbin/dhclient/bpf.c 162641 2006-09-26 01:02:02Z brooks $");
45149399Sbrooks
46147072Sbrooks#include "dhcpd.h"
47147072Sbrooks#include <sys/ioctl.h>
48147072Sbrooks#include <sys/uio.h>
49147072Sbrooks
50147072Sbrooks#include <net/bpf.h>
51147072Sbrooks#include <netinet/in_systm.h>
52147072Sbrooks#include <netinet/ip.h>
53147072Sbrooks#include <netinet/udp.h>
54147072Sbrooks#include <netinet/if_ether.h>
55147072Sbrooks
56147072Sbrooks#define BPF_FORMAT "/dev/bpf%d"
57147072Sbrooks
58147072Sbrooks/*
59147072Sbrooks * Called by get_interface_list for each interface that's discovered.
60147072Sbrooks * Opens a packet filter for each interface and adds it to the select
61147072Sbrooks * mask.
62147072Sbrooks */
63147072Sbrooksint
64147072Sbrooksif_register_bpf(struct interface_info *info)
65147072Sbrooks{
66147072Sbrooks	char filename[50];
67147072Sbrooks	int sock, b;
68147072Sbrooks
69147072Sbrooks	/* Open a BPF device */
70147072Sbrooks	for (b = 0; 1; b++) {
71147072Sbrooks		snprintf(filename, sizeof(filename), BPF_FORMAT, b);
72147072Sbrooks		sock = open(filename, O_RDWR, 0);
73147072Sbrooks		if (sock < 0) {
74147072Sbrooks			if (errno == EBUSY)
75147072Sbrooks				continue;
76147072Sbrooks			else
77147072Sbrooks				error("Can't find free bpf: %m");
78147072Sbrooks		} else
79147072Sbrooks			break;
80147072Sbrooks	}
81147072Sbrooks
82147072Sbrooks	/* Set the BPF device to point at this interface. */
83147072Sbrooks	if (ioctl(sock, BIOCSETIF, info->ifp) < 0)
84147072Sbrooks		error("Can't attach interface %s to bpf device %s: %m",
85147072Sbrooks		    info->name, filename);
86147072Sbrooks
87147072Sbrooks	return (sock);
88147072Sbrooks}
89147072Sbrooks
90147072Sbrooksvoid
91147072Sbrooksif_register_send(struct interface_info *info)
92147072Sbrooks{
93147072Sbrooks	/*
94147072Sbrooks	 * If we're using the bpf API for sending and receiving, we
95147072Sbrooks	 * don't need to register this interface twice.
96147072Sbrooks	 */
97147072Sbrooks	info->wfdesc = info->rfdesc;
98147072Sbrooks}
99147072Sbrooks
100147072Sbrooks/*
101147072Sbrooks * Packet filter program...
102147072Sbrooks *
103147072Sbrooks * XXX: Changes to the filter program may require changes to the
104147072Sbrooks * constant offsets used in if_register_send to patch the BPF program!
105147072Sbrooks */
106147072Sbrooksstruct bpf_insn dhcp_bpf_filter[] = {
107147072Sbrooks	/* Make sure this is an IP packet... */
108147072Sbrooks	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
109147072Sbrooks	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
110147072Sbrooks
111147072Sbrooks	/* Make sure it's a UDP packet... */
112147072Sbrooks	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
113147072Sbrooks	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
114147072Sbrooks
115147072Sbrooks	/* Make sure this isn't a fragment... */
116147072Sbrooks	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
117147072Sbrooks	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
118147072Sbrooks
119147072Sbrooks	/* Get the IP header length... */
120147072Sbrooks	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
121147072Sbrooks
122147072Sbrooks	/* Make sure it's to the right port... */
123147072Sbrooks	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
124147072Sbrooks	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1),		/* patch */
125147072Sbrooks
126147072Sbrooks	/* If we passed all the tests, ask for the whole packet. */
127147072Sbrooks	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
128147072Sbrooks
129147072Sbrooks	/* Otherwise, drop it. */
130147072Sbrooks	BPF_STMT(BPF_RET+BPF_K, 0),
131147072Sbrooks};
132147072Sbrooks
133147072Sbrooksint dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn);
134147072Sbrooks
135147072Sbrooks/*
136147072Sbrooks * Packet write filter program:
137147072Sbrooks * 'ip and udp and src port bootps and dst port (bootps or bootpc)'
138147072Sbrooks */
139147072Sbrooksstruct bpf_insn dhcp_bpf_wfilter[] = {
140147072Sbrooks	BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14),
141147072Sbrooks	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12),
142147072Sbrooks
143147072Sbrooks	/* Make sure this is an IP packet... */
144147072Sbrooks	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
145147072Sbrooks	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10),
146147072Sbrooks
147147072Sbrooks	/* Make sure it's a UDP packet... */
148147072Sbrooks	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
149147072Sbrooks	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8),
150147072Sbrooks
151147072Sbrooks	/* Make sure this isn't a fragment... */
152147072Sbrooks	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
153147072Sbrooks	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 6, 0),	/* patched */
154147072Sbrooks
155147072Sbrooks	/* Get the IP header length... */
156147072Sbrooks	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
157147072Sbrooks
158147072Sbrooks	/* Make sure it's from the right port... */
159147072Sbrooks	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
160147072Sbrooks	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 68, 0, 3),
161147072Sbrooks
162147072Sbrooks	/* Make sure it is to the right ports ... */
163147072Sbrooks	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
164147072Sbrooks	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1),
165147072Sbrooks
166147072Sbrooks	/* If we passed all the tests, ask for the whole packet. */
167147072Sbrooks	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
168147072Sbrooks
169147072Sbrooks	/* Otherwise, drop it. */
170147072Sbrooks	BPF_STMT(BPF_RET+BPF_K, 0),
171147072Sbrooks};
172147072Sbrooks
173147072Sbrooksint dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn);
174147072Sbrooks
175147072Sbrooksvoid
176147072Sbrooksif_register_receive(struct interface_info *info)
177147072Sbrooks{
178147072Sbrooks	struct bpf_version v;
179147072Sbrooks	struct bpf_program p;
180147072Sbrooks	int flag = 1, sz;
181147072Sbrooks
182147072Sbrooks	/* Open a BPF device and hang it on this interface... */
183147072Sbrooks	info->rfdesc = if_register_bpf(info);
184147072Sbrooks
185147072Sbrooks	/* Make sure the BPF version is in range... */
186147072Sbrooks	if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0)
187147072Sbrooks		error("Can't get BPF version: %m");
188147072Sbrooks
189147072Sbrooks	if (v.bv_major != BPF_MAJOR_VERSION ||
190147072Sbrooks	    v.bv_minor < BPF_MINOR_VERSION)
191147072Sbrooks		error("Kernel BPF version out of range - recompile dhcpd!");
192147072Sbrooks
193147072Sbrooks	/*
194147072Sbrooks	 * Set immediate mode so that reads return as soon as a packet
195147072Sbrooks	 * comes in, rather than waiting for the input buffer to fill
196147072Sbrooks	 * with packets.
197147072Sbrooks	 */
198147072Sbrooks	if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0)
199147072Sbrooks		error("Can't set immediate mode on bpf device: %m");
200147072Sbrooks
201147072Sbrooks	/* Get the required BPF buffer length from the kernel. */
202147072Sbrooks	if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0)
203147072Sbrooks		error("Can't get bpf buffer length: %m");
204147072Sbrooks	info->rbuf_max = sz;
205147072Sbrooks	info->rbuf = malloc(info->rbuf_max);
206147072Sbrooks	if (!info->rbuf)
207147072Sbrooks		error("Can't allocate %lu bytes for bpf input buffer.",
208147072Sbrooks		    (unsigned long)info->rbuf_max);
209147072Sbrooks	info->rbuf_offset = 0;
210147072Sbrooks	info->rbuf_len = 0;
211147072Sbrooks
212147072Sbrooks	/* Set up the bpf filter program structure. */
213147072Sbrooks	p.bf_len = dhcp_bpf_filter_len;
214147072Sbrooks	p.bf_insns = dhcp_bpf_filter;
215147072Sbrooks
216147072Sbrooks	/* Patch the server port into the BPF program...
217147072Sbrooks	 *
218147072Sbrooks	 * XXX: changes to filter program may require changes to the
219147072Sbrooks	 * insn number(s) used below!
220147072Sbrooks	 */
221147072Sbrooks	dhcp_bpf_filter[8].k = LOCAL_PORT;
222147072Sbrooks
223147072Sbrooks	if (ioctl(info->rfdesc, BIOCSETF, &p) < 0)
224147072Sbrooks		error("Can't install packet filter program: %m");
225147072Sbrooks
226147072Sbrooks	/* Set up the bpf write filter program structure. */
227147072Sbrooks	p.bf_len = dhcp_bpf_wfilter_len;
228147072Sbrooks	p.bf_insns = dhcp_bpf_wfilter;
229147072Sbrooks
230147072Sbrooks	if (dhcp_bpf_wfilter[7].k == 0x1fff)
231147072Sbrooks		dhcp_bpf_wfilter[7].k = htons(IP_MF|IP_OFFMASK);
232147072Sbrooks
233147072Sbrooks	if (ioctl(info->rfdesc, BIOCSETWF, &p) < 0)
234147072Sbrooks		error("Can't install write filter program: %m");
235147072Sbrooks
236147072Sbrooks	if (ioctl(info->rfdesc, BIOCLOCK, NULL) < 0)
237147072Sbrooks		error("Cannot lock bpf");
238147072Sbrooks}
239147072Sbrooks
240147072Sbrooksssize_t
241147072Sbrookssend_packet(struct interface_info *interface, struct dhcp_packet *raw,
242147072Sbrooks    size_t len, struct in_addr from, struct sockaddr_in *to,
243147072Sbrooks    struct hardware *hto)
244147072Sbrooks{
245147072Sbrooks	unsigned char buf[256];
246147072Sbrooks	struct iovec iov[2];
247147072Sbrooks	int result, bufp = 0;
248147072Sbrooks
249147072Sbrooks	/* Assemble the headers... */
250147072Sbrooks	assemble_hw_header(interface, buf, &bufp, hto);
251147072Sbrooks	assemble_udp_ip_header(buf, &bufp, from.s_addr,
252147072Sbrooks	    to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len);
253147072Sbrooks
254147072Sbrooks	/* Fire it off */
255147072Sbrooks	iov[0].iov_base = (char *)buf;
256147072Sbrooks	iov[0].iov_len = bufp;
257147072Sbrooks	iov[1].iov_base = (char *)raw;
258147072Sbrooks	iov[1].iov_len = len;
259147072Sbrooks
260147072Sbrooks	result = writev(interface->wfdesc, iov, 2);
261147072Sbrooks	if (result < 0)
262147072Sbrooks		warning("send_packet: %m");
263147072Sbrooks	return (result);
264147072Sbrooks}
265147072Sbrooks
266147072Sbrooksssize_t
267147072Sbrooksreceive_packet(struct interface_info *interface, unsigned char *buf,
268147072Sbrooks    size_t len, struct sockaddr_in *from, struct hardware *hfrom)
269147072Sbrooks{
270147072Sbrooks	int length = 0, offset = 0;
271147072Sbrooks	struct bpf_hdr hdr;
272147072Sbrooks
273147072Sbrooks	/*
274147072Sbrooks	 * All this complexity is because BPF doesn't guarantee that
275147072Sbrooks	 * only one packet will be returned at a time.  We're getting
276147072Sbrooks	 * what we deserve, though - this is a terrible abuse of the BPF
277147072Sbrooks	 * interface.  Sigh.
278147072Sbrooks	 */
279147072Sbrooks
280147072Sbrooks	/* Process packets until we get one we can return or until we've
281147072Sbrooks	 * done a read and gotten nothing we can return...
282147072Sbrooks	 */
283147072Sbrooks	do {
284147072Sbrooks		/* If the buffer is empty, fill it. */
285162641Sbrooks		if (interface->rbuf_offset >= interface->rbuf_len) {
286147072Sbrooks			length = read(interface->rfdesc, interface->rbuf,
287147072Sbrooks			    interface->rbuf_max);
288147072Sbrooks			if (length <= 0)
289147072Sbrooks				return (length);
290147072Sbrooks			interface->rbuf_offset = 0;
291147072Sbrooks			interface->rbuf_len = length;
292147072Sbrooks		}
293147072Sbrooks
294147072Sbrooks		/*
295147072Sbrooks		 * If there isn't room for a whole bpf header, something
296147072Sbrooks		 * went wrong, but we'll ignore it and hope it goes
297147072Sbrooks		 * away... XXX
298147072Sbrooks		 */
299147072Sbrooks		if (interface->rbuf_len - interface->rbuf_offset <
300147072Sbrooks		    sizeof(hdr)) {
301147072Sbrooks			interface->rbuf_offset = interface->rbuf_len;
302147072Sbrooks			continue;
303147072Sbrooks		}
304147072Sbrooks
305147072Sbrooks		/* Copy out a bpf header... */
306147072Sbrooks		memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
307147072Sbrooks		    sizeof(hdr));
308147072Sbrooks
309147072Sbrooks		/*
310147072Sbrooks		 * If the bpf header plus data doesn't fit in what's
311147072Sbrooks		 * left of the buffer, stick head in sand yet again...
312147072Sbrooks		 */
313147072Sbrooks		if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
314147072Sbrooks		    interface->rbuf_len) {
315147072Sbrooks			interface->rbuf_offset = interface->rbuf_len;
316147072Sbrooks			continue;
317147072Sbrooks		}
318147072Sbrooks
319148451Sbrooks		/* Skip over the BPF header... */
320148451Sbrooks		interface->rbuf_offset += hdr.bh_hdrlen;
321148451Sbrooks
322147072Sbrooks		/*
323147072Sbrooks		 * If the captured data wasn't the whole packet, or if
324147072Sbrooks		 * the packet won't fit in the input buffer, all we can
325147072Sbrooks		 * do is drop it.
326147072Sbrooks		 */
327147072Sbrooks		if (hdr.bh_caplen != hdr.bh_datalen) {
328148484Sbrooks			interface->rbuf_offset =
329148484Sbrooks			    BPF_WORDALIGN(interface->rbuf_offset +
330148484Sbrooks			    hdr.bh_caplen);
331147072Sbrooks			continue;
332147072Sbrooks		}
333147072Sbrooks
334147072Sbrooks		/* Decode the physical header... */
335147072Sbrooks		offset = decode_hw_header(interface->rbuf,
336147072Sbrooks		    interface->rbuf_offset, hfrom);
337147072Sbrooks
338147072Sbrooks		/*
339147072Sbrooks		 * If a physical layer checksum failed (dunno of any
340147072Sbrooks		 * physical layer that supports this, but WTH), skip
341147072Sbrooks		 * this packet.
342147072Sbrooks		 */
343147072Sbrooks		if (offset < 0) {
344148484Sbrooks			interface->rbuf_offset =
345148484Sbrooks			    BPF_WORDALIGN(interface->rbuf_offset +
346148484Sbrooks			    hdr.bh_caplen);
347147072Sbrooks			continue;
348147072Sbrooks		}
349147072Sbrooks		interface->rbuf_offset += offset;
350147072Sbrooks		hdr.bh_caplen -= offset;
351147072Sbrooks
352147072Sbrooks		/* Decode the IP and UDP headers... */
353147072Sbrooks		offset = decode_udp_ip_header(interface->rbuf,
354147072Sbrooks		    interface->rbuf_offset, from, NULL, hdr.bh_caplen);
355147072Sbrooks
356147072Sbrooks		/* If the IP or UDP checksum was bad, skip the packet... */
357147072Sbrooks		if (offset < 0) {
358148484Sbrooks			interface->rbuf_offset =
359148484Sbrooks			    BPF_WORDALIGN(interface->rbuf_offset +
360148484Sbrooks			    hdr.bh_caplen);
361147072Sbrooks			continue;
362147072Sbrooks		}
363147072Sbrooks		interface->rbuf_offset += offset;
364147072Sbrooks		hdr.bh_caplen -= offset;
365147072Sbrooks
366147072Sbrooks		/*
367147072Sbrooks		 * If there's not enough room to stash the packet data,
368147072Sbrooks		 * we have to skip it (this shouldn't happen in real
369147072Sbrooks		 * life, though).
370147072Sbrooks		 */
371147072Sbrooks		if (hdr.bh_caplen > len) {
372148484Sbrooks			interface->rbuf_offset =
373148484Sbrooks			    BPF_WORDALIGN(interface->rbuf_offset +
374148484Sbrooks			    hdr.bh_caplen);
375147072Sbrooks			continue;
376147072Sbrooks		}
377147072Sbrooks
378147072Sbrooks		/* Copy out the data in the packet... */
379147072Sbrooks		memcpy(buf, interface->rbuf + interface->rbuf_offset,
380147072Sbrooks		    hdr.bh_caplen);
381148484Sbrooks		interface->rbuf_offset =
382148484Sbrooks		    BPF_WORDALIGN(interface->rbuf_offset +
383148484Sbrooks		    hdr.bh_caplen);
384147072Sbrooks		return (hdr.bh_caplen);
385147072Sbrooks	} while (!length);
386147072Sbrooks	return (0);
387147072Sbrooks}
388