xref: /openbsd-src/usr.sbin/dhcpd/bpf.c (revision b57001d2402253b2754c7550188932001fb07564)
1 /*	$OpenBSD: bpf.c,v 1.20 2019/03/18 00:00:59 dlg 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/ioctl.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 
47 #include <arpa/inet.h>
48 
49 #include <net/bpf.h>
50 #include <net/if.h>
51 
52 #include <netinet/if_ether.h>
53 #include <netinet/in.h>
54 
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 #include "dhcp.h"
63 #include "tree.h"
64 #include "dhcpd.h"
65 #include "log.h"
66 
67 ssize_t send_packet	 (struct interface_info *, struct dhcp_packet *,
68     size_t, struct in_addr, struct sockaddr_in *, struct hardware *);
69 
70 /*
71  * Called by get_interface_list for each interface that's discovered.
72  * Opens a packet filter for each interface and adds it to the select
73  * mask.
74  */
75 int
if_register_bpf(struct interface_info * info)76 if_register_bpf(struct interface_info *info)
77 {
78 	int sock;
79 
80 	if ((sock = open("/dev/bpf", O_RDWR)) == -1)
81 		fatal("Can't open bpf device");
82 
83 	/* Set the BPF device to point at this interface. */
84 	if (ioctl(sock, BIOCSETIF, info->ifp) == -1)
85 		fatal("Can't attach interface %s to bpf device", info->name);
86 
87 	info->send_packet = send_packet;
88 	return (sock);
89 }
90 
91 void
if_register_send(struct interface_info * info)92 if_register_send(struct interface_info *info)
93 {
94 	/*
95 	 * If we're using the bpf API for sending and receiving, we
96 	 * don't need to register this interface twice.
97 	 */
98 	info->wfdesc = info->rfdesc;
99 }
100 
101 /*
102  * Packet read filter program: 'ip and udp and dst port bootps'
103  */
104 struct bpf_insn dhcp_bpf_filter[] = {
105 	/* Make sure this is an IP packet... */
106 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
107 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
108 
109 	/* Make sure it's a UDP packet... */
110 	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
111 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
112 
113 	/* Make sure this isn't a fragment... */
114 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
115 	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
116 
117 	/* Get the IP header length... */
118 	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
119 
120 	/* Make sure it's to the right port... */
121 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
122 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1),
123 
124 	/* If we passed all the tests, ask for the whole packet. */
125 	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
126 
127 	/* Otherwise, drop it. */
128 	BPF_STMT(BPF_RET+BPF_K, 0),
129 };
130 
131 int dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn);
132 
133 
134 /*
135  * Packet write filter program:
136  * 'ip and udp and src port bootps and dst port (bootps or bootpc)'
137  */
138 struct bpf_insn dhcp_bpf_wfilter[] = {
139 	/* Make sure this is an IP packet... */
140 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
141 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 11),
142 
143 	/* Make sure it's a UDP packet... */
144 	BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
145 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 9),
146 
147 	/* Make sure this isn't a fragment... */
148 	BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
149 	BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 7, 0),
150 
151 	/* Get the IP header length... */
152 	BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
153 
154 	/* Make sure it's from the right port... */
155 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
156 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 4),
157 
158 	/* Make sure it is to the right ports ... */
159 	BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
160 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, CLIENT_PORT, 1, 0),
161 	BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1),
162 
163 	/* If we passed all the tests, ask for the whole packet. */
164 	BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
165 
166 	/* Otherwise, drop it. */
167 	BPF_STMT(BPF_RET+BPF_K, 0),
168 };
169 
170 int dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn);
171 
172 void
if_register_receive(struct interface_info * info)173 if_register_receive(struct interface_info *info)
174 {
175 	struct bpf_version v;
176 	struct bpf_program p;
177 	int flag = 1, sz, cmplt = 0;
178 	int fildrop = BPF_FILDROP_CAPTURE;
179 
180 	/* Open a BPF device and hang it on this interface... */
181 	info->rfdesc = if_register_bpf(info);
182 
183 	/* Make sure the BPF version is in range... */
184 	if (ioctl(info->rfdesc, BIOCVERSION, &v) == -1)
185 		fatal("Can't get BPF version");
186 
187 	if (v.bv_major != BPF_MAJOR_VERSION ||
188 	    v.bv_minor < BPF_MINOR_VERSION)
189 		fatalx("Kernel BPF version out of range - recompile dhcpd!");
190 
191 	/*
192 	 * Set immediate mode so that reads return as soon as a packet
193 	 * comes in, rather than waiting for the input buffer to fill
194 	 * with packets.
195 	 */
196 	if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) == -1)
197 		fatal("Can't set immediate mode on bpf device");
198 
199 	if (ioctl(info->rfdesc, BIOCSFILDROP, &fildrop) == -1)
200 		fatal("Can't set filter-drop mode on bpf device");
201 
202 	/* make sure kernel fills in the source ethernet address */
203 	if (ioctl(info->rfdesc, BIOCSHDRCMPLT, &cmplt) == -1)
204 		fatal("Can't set header complete flag on bpf device");
205 
206 	/* Get the required BPF buffer length from the kernel. */
207 	if (ioctl(info->rfdesc, BIOCGBLEN, &sz) == -1)
208 		fatal("Can't get bpf buffer length");
209 	info->rbuf_max = sz;
210 	info->rbuf = malloc(info->rbuf_max);
211 	if (!info->rbuf)
212 		fatalx("Can't allocate %lu bytes for bpf input buffer.",
213 		    (unsigned long)info->rbuf_max);
214 	info->rbuf_offset = 0;
215 	info->rbuf_len = 0;
216 
217 	/* Set up the bpf filter program structure. */
218 	p.bf_len = dhcp_bpf_filter_len;
219 	p.bf_insns = dhcp_bpf_filter;
220 
221 	if (ioctl(info->rfdesc, BIOCSETF, &p) == -1)
222 		fatal("Can't install packet filter program");
223 
224 	/* Set up the bpf write filter program structure. */
225 	p.bf_len = dhcp_bpf_wfilter_len;
226 	p.bf_insns = dhcp_bpf_wfilter;
227 
228 	if (ioctl(info->rfdesc, BIOCSETWF, &p) == -1)
229 		fatal("Can't install write filter program");
230 
231 	/* make sure these settings cannot be changed after dropping privs */
232 	if (ioctl(info->rfdesc, BIOCLOCK) == -1)
233 		fatal("Failed to lock bpf descriptor");
234 }
235 
236 ssize_t
send_packet(struct interface_info * interface,struct dhcp_packet * raw,size_t len,struct in_addr from,struct sockaddr_in * to,struct hardware * hto)237 send_packet(struct interface_info *interface, struct dhcp_packet *raw,
238     size_t len, struct in_addr from, struct sockaddr_in *to,
239     struct hardware *hto)
240 {
241 	unsigned char buf[256];
242 	struct iovec iov[2];
243 	ssize_t result;
244 	int bufp = 0;
245 
246 	/* Assemble the headers... */
247 	assemble_hw_header(interface, buf, &bufp, hto);
248 	assemble_udp_ip_header(interface, buf, &bufp, from.s_addr,
249 	    to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len);
250 
251 	/* Fire it off */
252 	iov[0].iov_base = (char *)buf;
253 	iov[0].iov_len = bufp;
254 	iov[1].iov_base = (char *)raw;
255 	iov[1].iov_len = len;
256 
257 	result = writev(interface->wfdesc, iov, 2);
258 	if (result == -1)
259 		log_warn("send_packet");
260 	return (result);
261 }
262 
263 ssize_t
receive_packet(struct interface_info * interface,unsigned char * buf,size_t len,struct sockaddr_in * from,struct hardware * hfrom)264 receive_packet(struct interface_info *interface, unsigned char *buf,
265     size_t len, struct sockaddr_in *from, struct hardware *hfrom)
266 {
267 	int length = 0, offset = 0;
268 	struct bpf_hdr hdr;
269 
270 	/*
271 	 * All this complexity is because BPF doesn't guarantee that
272 	 * only one packet will be returned at a time.  We're getting
273 	 * what we deserve, though - this is a terrible abuse of the BPF
274 	 * interface.  Sigh.
275 	 */
276 
277 	/* Process packets until we get one we can return or until we've
278 	 * done a read and gotten nothing we can return...
279 	 */
280 	do {
281 		/* If the buffer is empty, fill it. */
282 		if (interface->rbuf_offset >= interface->rbuf_len) {
283 			length = read(interface->rfdesc, interface->rbuf,
284 			    interface->rbuf_max);
285 			if (length <= 0)
286 				return (length);
287 			interface->rbuf_offset = 0;
288 			interface->rbuf_len = length;
289 		}
290 
291 		/*
292 		 * If there isn't room for a whole bpf header, something
293 		 * went wrong, but we'll ignore it and hope it goes
294 		 * away... XXX
295 		 */
296 		if (interface->rbuf_len - interface->rbuf_offset <
297 		    sizeof(hdr)) {
298 			interface->rbuf_offset = interface->rbuf_len;
299 			continue;
300 		}
301 
302 		/* Copy out a bpf header... */
303 		memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
304 		    sizeof(hdr));
305 
306 		/*
307 		 * If the bpf header plus data doesn't fit in what's
308 		 * left of the buffer, stick head in sand yet again...
309 		 */
310 		if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
311 		    interface->rbuf_len) {
312 			interface->rbuf_offset = interface->rbuf_len;
313 			continue;
314 		}
315 
316 		/*
317 		 * If the captured data wasn't the whole packet, or if
318 		 * the packet won't fit in the input buffer, all we can
319 		 * do is drop it.
320 		 */
321 		if (hdr.bh_caplen != hdr.bh_datalen) {
322 			interface->rbuf_offset = BPF_WORDALIGN(
323 			    interface->rbuf_offset + hdr.bh_hdrlen +
324 			    hdr.bh_caplen);
325 			continue;
326 		}
327 
328 		/* Skip over the BPF header... */
329 		interface->rbuf_offset += hdr.bh_hdrlen;
330 
331 		/* Decode the physical header... */
332 		offset = decode_hw_header(interface->rbuf +
333 		    interface->rbuf_offset, hdr.bh_caplen, hfrom);
334 
335 		/*
336 		 * If a physical layer checksum failed (dunno of any
337 		 * physical layer that supports this, but WTH), skip
338 		 * this packet.
339 		 */
340 		if (offset < 0) {
341 			interface->rbuf_offset = BPF_WORDALIGN(
342 			    interface->rbuf_offset + hdr.bh_caplen);
343 			continue;
344 		}
345 		interface->rbuf_offset += offset;
346 		hdr.bh_caplen -= offset;
347 
348 		/* Decode the IP and UDP headers... */
349 		offset = decode_udp_ip_header(interface->rbuf +
350 		    interface->rbuf_offset, hdr.bh_caplen, from);
351 
352 		/* If the IP or UDP checksum was bad, skip the packet... */
353 		if (offset < 0) {
354 			interface->rbuf_offset = BPF_WORDALIGN(
355 			    interface->rbuf_offset + hdr.bh_caplen);
356 			continue;
357 		}
358 		interface->rbuf_offset += offset;
359 		hdr.bh_caplen -= offset;
360 
361 		/*
362 		 * If there's not enough room to stash the packet data,
363 		 * we have to skip it (this shouldn't happen in real
364 		 * life, though).
365 		 */
366 		if (hdr.bh_caplen > len) {
367 			interface->rbuf_offset = BPF_WORDALIGN(
368 			    interface->rbuf_offset + hdr.bh_caplen);
369 			continue;
370 		}
371 
372 		/* Copy out the data in the packet... */
373 		memcpy(buf, interface->rbuf + interface->rbuf_offset,
374 		    hdr.bh_caplen);
375 		interface->rbuf_offset = BPF_WORDALIGN(interface->rbuf_offset +
376 		    hdr.bh_caplen);
377 		return (hdr.bh_caplen);
378 	} while (!length);
379 	return (0);
380 }
381