xref: /freebsd-src/sbin/dhclient/bpf.c (revision d1f4d8549443b49fae4b15d1d0781502ffe8b1e5)
1 /*	$OpenBSD: bpf.c,v 1.13 2004/05/05 14:28:58 deraadt Exp $	*/
2 
3 /* BPF socket interface code, originally contributed by Archie Cobbs. */
4 
5 /*
6  * Copyright (c) 1995, 1996, 1998, 1999
7  * The Internet Software Consortium.    All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The Internet Software Consortium nor the names
19  *    of its contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * This software has been written for the Internet Software Consortium
37  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38  * Enterprises.  To learn more about the Internet Software Consortium,
39  * see ``http://www.vix.com/isc''.  To learn more about Vixie
40  * Enterprises, see ``http://www.vix.com''.
41  */
42 
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include "dhcpd.h"
47 #include <sys/ioctl.h>
48 #include <sys/uio.h>
49 
50 #include <net/bpf.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/udp.h>
54 #include <netinet/if_ether.h>
55 
56 #define BPF_FORMAT "/dev/bpf%d"
57 
58 /*
59  * Called by get_interface_list for each interface that's discovered.
60  * Opens a packet filter for each interface and adds it to the select
61  * mask.
62  */
63 int
64 if_register_bpf(struct interface_info *info)
65 {
66 	char filename[50];
67 	int sock, b;
68 
69 	/* Open a BPF device */
70 	for (b = 0; 1; b++) {
71 		snprintf(filename, sizeof(filename), BPF_FORMAT, b);
72 		sock = open(filename, O_RDWR, 0);
73 		if (sock < 0) {
74 			if (errno == EBUSY)
75 				continue;
76 			else
77 				error("Can't find free bpf: %m");
78 		} else
79 			break;
80 	}
81 
82 	/* Set the BPF device to point at this interface. */
83 	if (ioctl(sock, BIOCSETIF, info->ifp) < 0)
84 		error("Can't attach interface %s to bpf device %s: %m",
85 		    info->name, filename);
86 
87 	return (sock);
88 }
89 
90 void
91 if_register_send(struct interface_info *info)
92 {
93 	int sock, on = 1;
94 
95 	/*
96 	 * If we're using the bpf API for sending and receiving, we
97 	 * don't need to register this interface twice.
98 	 */
99 	info->wfdesc = info->rfdesc;
100 
101 	/*
102 	 * Use raw socket for unicast send.
103 	 */
104 	if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1)
105 		error("socket(SOCK_RAW): %m");
106 	if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on,
107 	    sizeof(on)) == -1)
108 		error("setsockopt(IP_HDRINCL): %m");
109 	info->ufdesc = sock;
110 }
111 
112 /*
113  * Packet filter program...
114  *
115  * XXX: Changes to the filter program may require changes to the
116  * constant offsets used in if_register_send to patch the BPF program!
117  */
118 struct bpf_insn dhcp_bpf_filter[] = {
119 	/* Make sure this is an IP packet... */
120 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
121 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
122 
123 	/* Make sure it's a UDP packet... */
124 	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
125 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
126 
127 	/* Make sure this isn't a fragment... */
128 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
129 	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
130 
131 	/* Get the IP header length... */
132 	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
133 
134 	/* Make sure it's to the right port... */
135 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
136 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1),		/* patch */
137 
138 	/* If we passed all the tests, ask for the whole packet. */
139 	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
140 
141 	/* Otherwise, drop it. */
142 	BPF_STMT(BPF_RET+BPF_K, 0),
143 };
144 
145 int dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn);
146 
147 /*
148  * Packet write filter program:
149  * 'ip and udp and src port bootps and dst port (bootps or bootpc)'
150  */
151 struct bpf_insn dhcp_bpf_wfilter[] = {
152 	BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14),
153 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12),
154 
155 	/* Make sure this is an IP packet... */
156 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
157 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10),
158 
159 	/* Make sure it's a UDP packet... */
160 	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
161 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8),
162 
163 	/* Make sure this isn't a fragment... */
164 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
165 	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 6, 0),	/* patched */
166 
167 	/* Get the IP header length... */
168 	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
169 
170 	/* Make sure it's from the right port... */
171 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
172 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 68, 0, 3),
173 
174 	/* Make sure it is to the right ports ... */
175 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
176 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1),
177 
178 	/* If we passed all the tests, ask for the whole packet. */
179 	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
180 
181 	/* Otherwise, drop it. */
182 	BPF_STMT(BPF_RET+BPF_K, 0),
183 };
184 
185 int dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn);
186 
187 void
188 if_register_receive(struct interface_info *info)
189 {
190 	struct bpf_version v;
191 	struct bpf_program p;
192 	int flag = 1, sz;
193 
194 	/* Open a BPF device and hang it on this interface... */
195 	info->rfdesc = if_register_bpf(info);
196 
197 	/* Make sure the BPF version is in range... */
198 	if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0)
199 		error("Can't get BPF version: %m");
200 
201 	if (v.bv_major != BPF_MAJOR_VERSION ||
202 	    v.bv_minor < BPF_MINOR_VERSION)
203 		error("Kernel BPF version out of range - recompile dhcpd!");
204 
205 	/*
206 	 * Set immediate mode so that reads return as soon as a packet
207 	 * comes in, rather than waiting for the input buffer to fill
208 	 * with packets.
209 	 */
210 	if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0)
211 		error("Can't set immediate mode on bpf device: %m");
212 
213 	/* Get the required BPF buffer length from the kernel. */
214 	if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0)
215 		error("Can't get bpf buffer length: %m");
216 	info->rbuf_max = sz;
217 	info->rbuf = malloc(info->rbuf_max);
218 	if (!info->rbuf)
219 		error("Can't allocate %lu bytes for bpf input buffer.",
220 		    (unsigned long)info->rbuf_max);
221 	info->rbuf_offset = 0;
222 	info->rbuf_len = 0;
223 
224 	/* Set up the bpf filter program structure. */
225 	p.bf_len = dhcp_bpf_filter_len;
226 	p.bf_insns = dhcp_bpf_filter;
227 
228 	/* Patch the server port into the BPF program...
229 	 *
230 	 * XXX: changes to filter program may require changes to the
231 	 * insn number(s) used below!
232 	 */
233 	dhcp_bpf_filter[8].k = LOCAL_PORT;
234 
235 	if (ioctl(info->rfdesc, BIOCSETF, &p) < 0)
236 		error("Can't install packet filter program: %m");
237 
238 	/* Set up the bpf write filter program structure. */
239 	p.bf_len = dhcp_bpf_wfilter_len;
240 	p.bf_insns = dhcp_bpf_wfilter;
241 
242 	if (dhcp_bpf_wfilter[7].k == 0x1fff)
243 		dhcp_bpf_wfilter[7].k = htons(IP_MF|IP_OFFMASK);
244 
245 	if (ioctl(info->rfdesc, BIOCSETWF, &p) < 0)
246 		error("Can't install write filter program: %m");
247 
248 	if (ioctl(info->rfdesc, BIOCLOCK, NULL) < 0)
249 		error("Cannot lock bpf");
250 }
251 
252 ssize_t
253 send_packet(struct interface_info *interface, struct dhcp_packet *raw,
254     size_t len, struct in_addr from, struct sockaddr_in *to)
255 {
256 	unsigned char buf[256];
257 	struct iovec iov[2];
258 	struct msghdr msg;
259 	int result, bufp = 0;
260 
261 	/* Assemble the headers... */
262 	if (to->sin_addr.s_addr == INADDR_BROADCAST)
263 		assemble_hw_header(interface, buf, &bufp);
264 	assemble_udp_ip_header(buf, &bufp, from.s_addr,
265 	    to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len);
266 
267 	iov[0].iov_base = (char *)buf;
268 	iov[0].iov_len = bufp;
269 	iov[1].iov_base = (char *)raw;
270 	iov[1].iov_len = len;
271 
272 	/* Fire it off */
273 	if (to->sin_addr.s_addr == INADDR_BROADCAST)
274 		result = writev(interface->wfdesc, iov, 2);
275 	else {
276 		memset(&msg, 0, sizeof(msg));
277 		msg.msg_name = (struct sockaddr *)to;
278 		msg.msg_namelen = sizeof(*to);
279 		msg.msg_iov = iov;
280 		msg.msg_iovlen = 2;
281 		result = sendmsg(interface->ufdesc, &msg, 0);
282 	}
283 
284 	if (result < 0)
285 		warning("send_packet: %m");
286 	return (result);
287 }
288 
289 ssize_t
290 receive_packet(struct interface_info *interface, unsigned char *buf,
291     size_t len, struct sockaddr_in *from, struct hardware *hfrom)
292 {
293 	int length = 0, offset = 0;
294 	struct bpf_hdr hdr;
295 
296 	/*
297 	 * All this complexity is because BPF doesn't guarantee that
298 	 * only one packet will be returned at a time.  We're getting
299 	 * what we deserve, though - this is a terrible abuse of the BPF
300 	 * interface.  Sigh.
301 	 */
302 
303 	/* Process packets until we get one we can return or until we've
304 	 * done a read and gotten nothing we can return...
305 	 */
306 	do {
307 		/* If the buffer is empty, fill it. */
308 		if (interface->rbuf_offset >= interface->rbuf_len) {
309 			length = read(interface->rfdesc, interface->rbuf,
310 			    interface->rbuf_max);
311 			if (length <= 0)
312 				return (length);
313 			interface->rbuf_offset = 0;
314 			interface->rbuf_len = length;
315 		}
316 
317 		/*
318 		 * If there isn't room for a whole bpf header, something
319 		 * went wrong, but we'll ignore it and hope it goes
320 		 * away... XXX
321 		 */
322 		if (interface->rbuf_len - interface->rbuf_offset <
323 		    sizeof(hdr)) {
324 			interface->rbuf_offset = interface->rbuf_len;
325 			continue;
326 		}
327 
328 		/* Copy out a bpf header... */
329 		memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
330 		    sizeof(hdr));
331 
332 		/*
333 		 * If the bpf header plus data doesn't fit in what's
334 		 * left of the buffer, stick head in sand yet again...
335 		 */
336 		if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
337 		    interface->rbuf_len) {
338 			interface->rbuf_offset = interface->rbuf_len;
339 			continue;
340 		}
341 
342 		/* Skip over the BPF header... */
343 		interface->rbuf_offset += hdr.bh_hdrlen;
344 
345 		/*
346 		 * If the captured data wasn't the whole packet, or if
347 		 * the packet won't fit in the input buffer, all we can
348 		 * do is drop it.
349 		 */
350 		if (hdr.bh_caplen != hdr.bh_datalen) {
351 			interface->rbuf_offset =
352 			    BPF_WORDALIGN(interface->rbuf_offset +
353 			    hdr.bh_caplen);
354 			continue;
355 		}
356 
357 		/* Decode the physical header... */
358 		offset = decode_hw_header(interface->rbuf,
359 		    interface->rbuf_offset, hfrom);
360 
361 		/*
362 		 * If a physical layer checksum failed (dunno of any
363 		 * physical layer that supports this, but WTH), skip
364 		 * this packet.
365 		 */
366 		if (offset < 0) {
367 			interface->rbuf_offset =
368 			    BPF_WORDALIGN(interface->rbuf_offset +
369 			    hdr.bh_caplen);
370 			continue;
371 		}
372 		interface->rbuf_offset += offset;
373 		hdr.bh_caplen -= offset;
374 
375 		/* Decode the IP and UDP headers... */
376 		offset = decode_udp_ip_header(interface->rbuf,
377 		    interface->rbuf_offset, from, NULL, hdr.bh_caplen);
378 
379 		/* If the IP or UDP checksum was bad, skip the packet... */
380 		if (offset < 0) {
381 			interface->rbuf_offset =
382 			    BPF_WORDALIGN(interface->rbuf_offset +
383 			    hdr.bh_caplen);
384 			continue;
385 		}
386 		interface->rbuf_offset += offset;
387 		hdr.bh_caplen -= offset;
388 
389 		/*
390 		 * If there's not enough room to stash the packet data,
391 		 * we have to skip it (this shouldn't happen in real
392 		 * life, though).
393 		 */
394 		if (hdr.bh_caplen > len) {
395 			interface->rbuf_offset =
396 			    BPF_WORDALIGN(interface->rbuf_offset +
397 			    hdr.bh_caplen);
398 			continue;
399 		}
400 
401 		/* Copy out the data in the packet... */
402 		memcpy(buf, interface->rbuf + interface->rbuf_offset,
403 		    hdr.bh_caplen);
404 		interface->rbuf_offset =
405 		    BPF_WORDALIGN(interface->rbuf_offset +
406 		    hdr.bh_caplen);
407 		return (hdr.bh_caplen);
408 	} while (!length);
409 	return (0);
410 }
411