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