xref: /freebsd/sbin/dhclient/bpf.c (revision d6b92ffa)
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 "privsep.h"
48 #include <sys/capsicum.h>
49 #include <sys/ioctl.h>
50 #include <sys/uio.h>
51 
52 #include <net/bpf.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/udp.h>
56 #include <netinet/if_ether.h>
57 
58 #define BPF_FORMAT "/dev/bpf%d"
59 
60 /*
61  * Called by get_interface_list for each interface that's discovered.
62  * Opens a packet filter for each interface and adds it to the select
63  * mask.
64  */
65 int
66 if_register_bpf(struct interface_info *info, int flags)
67 {
68 	char filename[50];
69 	int sock, b;
70 
71 	/* Open a BPF device */
72 	for (b = 0;; b++) {
73 		snprintf(filename, sizeof(filename), BPF_FORMAT, b);
74 		sock = open(filename, flags);
75 		if (sock < 0) {
76 			if (errno == EBUSY)
77 				continue;
78 			else
79 				error("Can't find free bpf: %m");
80 		} else
81 			break;
82 	}
83 
84 	/* Set the BPF device to point at this interface. */
85 	if (ioctl(sock, BIOCSETIF, info->ifp) < 0)
86 		error("Can't attach interface %s to bpf device %s: %m",
87 		    info->name, filename);
88 
89 	return (sock);
90 }
91 
92 /*
93  * Packet write filter program:
94  * 'ip and udp and src port bootps and dst port (bootps or bootpc)'
95  */
96 struct bpf_insn dhcp_bpf_wfilter[] = {
97 	BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14),
98 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12),
99 
100 	/* Make sure this is an IP packet... */
101 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
102 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10),
103 
104 	/* Make sure it's a UDP packet... */
105 	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
106 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8),
107 
108 	/* Make sure this isn't a fragment... */
109 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
110 	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 6, 0),	/* patched */
111 
112 	/* Get the IP header length... */
113 	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
114 
115 	/* Make sure it's from the right port... */
116 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
117 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 68, 0, 3),
118 
119 	/* Make sure it is to the right ports ... */
120 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
121 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1),
122 
123 	/* If we passed all the tests, ask for the whole packet. */
124 	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
125 
126 	/* Otherwise, drop it. */
127 	BPF_STMT(BPF_RET+BPF_K, 0),
128 };
129 
130 int dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn);
131 
132 void
133 if_register_send(struct interface_info *info)
134 {
135 	cap_rights_t rights;
136 	struct bpf_version v;
137 	struct bpf_program p;
138 	int sock, on = 1;
139 
140 	/* Open a BPF device and hang it on this interface... */
141 	info->wfdesc = if_register_bpf(info, O_WRONLY);
142 
143 	/* Make sure the BPF version is in range... */
144 	if (ioctl(info->wfdesc, BIOCVERSION, &v) < 0)
145 		error("Can't get BPF version: %m");
146 
147 	if (v.bv_major != BPF_MAJOR_VERSION ||
148 	    v.bv_minor < BPF_MINOR_VERSION)
149 		error("Kernel BPF version out of range - recompile dhcpd!");
150 
151 	/* Set up the bpf write filter program structure. */
152 	p.bf_len = dhcp_bpf_wfilter_len;
153 	p.bf_insns = dhcp_bpf_wfilter;
154 
155 	if (dhcp_bpf_wfilter[7].k == 0x1fff)
156 		dhcp_bpf_wfilter[7].k = htons(IP_MF|IP_OFFMASK);
157 
158 	if (ioctl(info->wfdesc, BIOCSETWF, &p) < 0)
159 		error("Can't install write filter program: %m");
160 
161 	if (ioctl(info->wfdesc, BIOCLOCK, NULL) < 0)
162 		error("Cannot lock bpf");
163 
164 	cap_rights_init(&rights, CAP_WRITE);
165 	if (cap_rights_limit(info->wfdesc, &rights) < 0 && errno != ENOSYS)
166 		error("Can't limit bpf descriptor: %m");
167 
168 	/*
169 	 * Use raw socket for unicast send.
170 	 */
171 	if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1)
172 		error("socket(SOCK_RAW): %m");
173 	if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on,
174 	    sizeof(on)) == -1)
175 		error("setsockopt(IP_HDRINCL): %m");
176 	info->ufdesc = sock;
177 }
178 
179 /*
180  * Packet filter program...
181  *
182  * XXX: Changes to the filter program may require changes to the
183  * constant offsets used in if_register_send to patch the BPF program!
184  */
185 struct bpf_insn dhcp_bpf_filter[] = {
186 	/* Make sure this is an IP packet... */
187 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
188 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
189 
190 	/* Make sure it's a UDP packet... */
191 	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
192 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
193 
194 	/* Make sure this isn't a fragment... */
195 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
196 	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
197 
198 	/* Get the IP header length... */
199 	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
200 
201 	/* Make sure it's to the right port... */
202 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
203 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1),		/* patch */
204 
205 	/* If we passed all the tests, ask for the whole packet. */
206 	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
207 
208 	/* Otherwise, drop it. */
209 	BPF_STMT(BPF_RET+BPF_K, 0),
210 };
211 
212 int dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn);
213 
214 void
215 if_register_receive(struct interface_info *info)
216 {
217 	static const unsigned long cmds[2] = { SIOCGIFFLAGS, SIOCGIFMEDIA };
218 	cap_rights_t rights;
219 	struct bpf_version v;
220 	struct bpf_program p;
221 	int flag = 1, sz;
222 
223 	/* Open a BPF device and hang it on this interface... */
224 	info->rfdesc = if_register_bpf(info, O_RDONLY);
225 
226 	/* Make sure the BPF version is in range... */
227 	if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0)
228 		error("Can't get BPF version: %m");
229 
230 	if (v.bv_major != BPF_MAJOR_VERSION ||
231 	    v.bv_minor < BPF_MINOR_VERSION)
232 		error("Kernel BPF version out of range - recompile dhcpd!");
233 
234 	/*
235 	 * Set immediate mode so that reads return as soon as a packet
236 	 * comes in, rather than waiting for the input buffer to fill
237 	 * with packets.
238 	 */
239 	if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0)
240 		error("Can't set immediate mode on bpf device: %m");
241 
242 	/* Get the required BPF buffer length from the kernel. */
243 	if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0)
244 		error("Can't get bpf buffer length: %m");
245 	info->rbuf_max = sz;
246 	info->rbuf = malloc(info->rbuf_max);
247 	if (!info->rbuf)
248 		error("Can't allocate %lu bytes for bpf input buffer.",
249 		    (unsigned long)info->rbuf_max);
250 	info->rbuf_offset = 0;
251 	info->rbuf_len = 0;
252 
253 	/* Set up the bpf filter program structure. */
254 	p.bf_len = dhcp_bpf_filter_len;
255 	p.bf_insns = dhcp_bpf_filter;
256 
257 	/* Patch the server port into the BPF program...
258 	 *
259 	 * XXX: changes to filter program may require changes to the
260 	 * insn number(s) used below!
261 	 */
262 	dhcp_bpf_filter[8].k = LOCAL_PORT;
263 
264 	if (ioctl(info->rfdesc, BIOCSETF, &p) < 0)
265 		error("Can't install packet filter program: %m");
266 
267 	if (ioctl(info->rfdesc, BIOCLOCK, NULL) < 0)
268 		error("Cannot lock bpf");
269 
270 	cap_rights_init(&rights, CAP_IOCTL, CAP_EVENT, CAP_READ);
271 	if (cap_rights_limit(info->rfdesc, &rights) < 0 && errno != ENOSYS)
272 		error("Can't limit bpf descriptor: %m");
273 	if (cap_ioctls_limit(info->rfdesc, cmds, 2) < 0 && errno != ENOSYS)
274 		error("Can't limit ioctls for bpf descriptor: %m");
275 }
276 
277 void
278 send_packet_unpriv(int privfd, struct dhcp_packet *raw, size_t len,
279     struct in_addr from, struct in_addr to)
280 {
281 	struct imsg_hdr hdr;
282 	struct buf *buf;
283 	int errs;
284 
285 	hdr.code = IMSG_SEND_PACKET;
286 	hdr.len = sizeof(hdr) +
287 	    sizeof(size_t) + len +
288 	    sizeof(from) + sizeof(to);
289 
290 	if ((buf = buf_open(hdr.len)) == NULL)
291 		error("buf_open: %m");
292 
293 	errs = 0;
294 	errs += buf_add(buf, &hdr, sizeof(hdr));
295 	errs += buf_add(buf, &len, sizeof(len));
296 	errs += buf_add(buf, raw, len);
297 	errs += buf_add(buf, &from, sizeof(from));
298 	errs += buf_add(buf, &to, sizeof(to));
299 	if (errs)
300 		error("buf_add: %m");
301 
302 	if (buf_close(privfd, buf) == -1)
303 		error("buf_close: %m");
304 }
305 
306 void
307 send_packet_priv(struct interface_info *interface, struct imsg_hdr *hdr, int fd)
308 {
309 	unsigned char buf[256];
310 	struct iovec iov[2];
311 	struct msghdr msg;
312 	struct dhcp_packet raw;
313 	size_t len;
314 	struct in_addr from, to;
315 	int result, bufp = 0;
316 
317 	if (hdr->len < sizeof(*hdr) + sizeof(size_t))
318 		error("corrupted message received");
319 	buf_read(fd, &len, sizeof(len));
320 	if (hdr->len != sizeof(*hdr) + sizeof(size_t) + len +
321 	    sizeof(from) + sizeof(to)) {
322 		error("corrupted message received");
323 	}
324 	if (len > sizeof(raw))
325 		error("corrupted message received");
326 	buf_read(fd, &raw, len);
327 	buf_read(fd, &from, sizeof(from));
328 	buf_read(fd, &to, sizeof(to));
329 
330 	/* Assemble the headers... */
331 	if (to.s_addr == INADDR_BROADCAST)
332 		assemble_hw_header(interface, buf, &bufp);
333 	assemble_udp_ip_header(buf, &bufp, from.s_addr, to.s_addr,
334 	    htons(REMOTE_PORT), (unsigned char *)&raw, len);
335 
336 	iov[0].iov_base = buf;
337 	iov[0].iov_len = bufp;
338 	iov[1].iov_base = &raw;
339 	iov[1].iov_len = len;
340 
341 	/* Fire it off */
342 	if (to.s_addr == INADDR_BROADCAST)
343 		result = writev(interface->wfdesc, iov, 2);
344 	else {
345 		struct sockaddr_in sato;
346 
347 		sato.sin_addr = to;
348 		sato.sin_port = htons(REMOTE_PORT);
349 		sato.sin_family = AF_INET;
350 		sato.sin_len = sizeof(sato);
351 
352 		memset(&msg, 0, sizeof(msg));
353 		msg.msg_name = (struct sockaddr *)&sato;
354 		msg.msg_namelen = sizeof(sato);
355 		msg.msg_iov = iov;
356 		msg.msg_iovlen = 2;
357 		result = sendmsg(interface->ufdesc, &msg, 0);
358 	}
359 
360 	if (result < 0)
361 		warning("send_packet: %m");
362 }
363 
364 ssize_t
365 receive_packet(struct interface_info *interface, unsigned char *buf,
366     size_t len, struct sockaddr_in *from, struct hardware *hfrom)
367 {
368 	int length = 0, offset = 0;
369 	struct bpf_hdr hdr;
370 
371 	/*
372 	 * All this complexity is because BPF doesn't guarantee that
373 	 * only one packet will be returned at a time.  We're getting
374 	 * what we deserve, though - this is a terrible abuse of the BPF
375 	 * interface.  Sigh.
376 	 */
377 
378 	/* Process packets until we get one we can return or until we've
379 	 * done a read and gotten nothing we can return...
380 	 */
381 	do {
382 		/* If the buffer is empty, fill it. */
383 		if (interface->rbuf_offset >= interface->rbuf_len) {
384 			length = read(interface->rfdesc, interface->rbuf,
385 			    interface->rbuf_max);
386 			if (length <= 0)
387 				return (length);
388 			interface->rbuf_offset = 0;
389 			interface->rbuf_len = length;
390 		}
391 
392 		/*
393 		 * If there isn't room for a whole bpf header, something
394 		 * went wrong, but we'll ignore it and hope it goes
395 		 * away... XXX
396 		 */
397 		if (interface->rbuf_len - interface->rbuf_offset <
398 		    sizeof(hdr)) {
399 			interface->rbuf_offset = interface->rbuf_len;
400 			continue;
401 		}
402 
403 		/* Copy out a bpf header... */
404 		memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
405 		    sizeof(hdr));
406 
407 		/*
408 		 * If the bpf header plus data doesn't fit in what's
409 		 * left of the buffer, stick head in sand yet again...
410 		 */
411 		if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
412 		    interface->rbuf_len) {
413 			interface->rbuf_offset = interface->rbuf_len;
414 			continue;
415 		}
416 
417 		/* Skip over the BPF header... */
418 		interface->rbuf_offset += hdr.bh_hdrlen;
419 
420 		/*
421 		 * If the captured data wasn't the whole packet, or if
422 		 * the packet won't fit in the input buffer, all we can
423 		 * do is drop it.
424 		 */
425 		if (hdr.bh_caplen != hdr.bh_datalen) {
426 			interface->rbuf_offset =
427 			    BPF_WORDALIGN(interface->rbuf_offset +
428 			    hdr.bh_caplen);
429 			continue;
430 		}
431 
432 		/* Decode the physical header... */
433 		offset = decode_hw_header(interface->rbuf,
434 		    interface->rbuf_offset, hfrom);
435 
436 		/*
437 		 * If a physical layer checksum failed (dunno of any
438 		 * physical layer that supports this, but WTH), skip
439 		 * this packet.
440 		 */
441 		if (offset < 0) {
442 			interface->rbuf_offset =
443 			    BPF_WORDALIGN(interface->rbuf_offset +
444 			    hdr.bh_caplen);
445 			continue;
446 		}
447 		interface->rbuf_offset += offset;
448 		hdr.bh_caplen -= offset;
449 
450 		/* Decode the IP and UDP headers... */
451 		offset = decode_udp_ip_header(interface->rbuf,
452 		    interface->rbuf_offset, from, NULL, hdr.bh_caplen);
453 
454 		/* If the IP or UDP checksum was bad, skip the packet... */
455 		if (offset < 0) {
456 			interface->rbuf_offset =
457 			    BPF_WORDALIGN(interface->rbuf_offset +
458 			    hdr.bh_caplen);
459 			continue;
460 		}
461 		interface->rbuf_offset += offset;
462 		hdr.bh_caplen -= offset;
463 
464 		/*
465 		 * If there's not enough room to stash the packet data,
466 		 * we have to skip it (this shouldn't happen in real
467 		 * life, though).
468 		 */
469 		if (hdr.bh_caplen > len) {
470 			interface->rbuf_offset =
471 			    BPF_WORDALIGN(interface->rbuf_offset +
472 			    hdr.bh_caplen);
473 			continue;
474 		}
475 
476 		/* Copy out the data in the packet... */
477 		memcpy(buf, interface->rbuf + interface->rbuf_offset,
478 		    hdr.bh_caplen);
479 		interface->rbuf_offset =
480 		    BPF_WORDALIGN(interface->rbuf_offset +
481 		    hdr.bh_caplen);
482 		return (hdr.bh_caplen);
483 	} while (!length);
484 	return (0);
485 }
486