1 /* $OpenBSD: bpf.c,v 1.13 2004/05/05 14:28:58 deraadt Exp $ */
3 /* BPF socket interface code, originally contributed by Archie Cobbs. */
6 * SPDX-License-Identifier: BSD-3-Clause
8 * Copyright (c) 2021 Franco Fichtner <franco@opnsense.org>
9 * Copyright (c) 1995, 1996, 1998, 1999
10 * The Internet Software Consortium. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of The Internet Software Consortium nor the names
22 * of its contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
26 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
33 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * This software has been written for the Internet Software Consortium
40 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
41 * Enterprises. To learn more about the Internet Software Consortium,
42 * see ``http://www.vix.com/isc''. To learn more about Vixie
43 * Enterprises, see ``http://www.vix.com''.
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
51 #include <sys/capsicum.h>
52 #include <sys/ioctl.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/udp.h>
59 #include <netinet/if_ether.h>
61 #include <capsicum_helpers.h>
63 #define BPF_FORMAT "/dev/bpf%d"
66 * Called by get_interface_list for each interface that's discovered.
67 * Opens a packet filter for each interface and adds it to the select
71 if_register_bpf(struct interface_info *info, int flags)
76 /* Open a BPF device */
78 snprintf(filename, sizeof(filename), BPF_FORMAT, b);
79 sock = open(filename, flags);
84 error("Can't find free bpf: %m");
89 /* Set the BPF device to point at this interface. */
90 if (ioctl(sock, BIOCSETIF, info->ifp) < 0)
91 error("Can't attach interface %s to bpf device %s: %m",
92 info->name, filename);
94 /* Tag the packets with the proper VLAN PCP setting. */
95 if (info->client->config->vlan_pcp != 0) {
96 if (ioctl(sock, BIOCSETVLANPCP,
97 &info->client->config->vlan_pcp) < 0)
98 error( "Can't set the VLAN PCP tag on interface %s: %m",
106 * Packet write filter program:
107 * 'ip and udp and src port bootps and dst port (bootps or bootpc)'
109 static const struct bpf_insn dhcp_bpf_wfilter[] = {
110 BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14),
111 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12),
113 /* Make sure this is an IP packet... */
114 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
115 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10),
117 /* Make sure it's a UDP packet... */
118 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
119 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8),
121 /* Make sure this isn't a fragment... */
122 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
123 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, IP_MF|IP_OFFMASK, 6, 0),
125 /* Get the IP header length... */
126 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
128 /* Make sure it's from the right port... */
129 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
130 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, LOCAL_PORT, 0, 3),
132 /* Make sure it is to the right ports ... */
133 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
134 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, REMOTE_PORT, 0, 1),
136 /* If we passed all the tests, ask for the whole packet. */
137 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
139 /* Otherwise, drop it. */
140 BPF_STMT(BPF_RET+BPF_K, 0),
144 if_register_send(struct interface_info *info)
147 struct bpf_version v;
148 struct bpf_program p;
151 /* Open a BPF device and hang it on this interface... */
152 info->wfdesc = if_register_bpf(info, O_WRONLY);
154 /* Make sure the BPF version is in range... */
155 if (ioctl(info->wfdesc, BIOCVERSION, &v) < 0)
156 error("Can't get BPF version: %m");
158 if (v.bv_major != BPF_MAJOR_VERSION ||
159 v.bv_minor < BPF_MINOR_VERSION)
160 error("Kernel BPF version out of range - recompile dhcpd!");
162 /* Set up the bpf write filter program structure. */
163 p.bf_insns = __DECONST(struct bpf_insn *, dhcp_bpf_wfilter);
164 p.bf_len = nitems(dhcp_bpf_wfilter);
166 if (ioctl(info->wfdesc, BIOCSETWF, &p) < 0)
167 error("Can't install write filter program: %m");
169 if (ioctl(info->wfdesc, BIOCLOCK, NULL) < 0)
170 error("Cannot lock bpf");
172 cap_rights_init(&rights, CAP_WRITE);
173 if (caph_rights_limit(info->wfdesc, &rights) < 0)
174 error("Can't limit bpf descriptor: %m");
177 * Use raw socket for unicast send.
179 if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1)
180 error("socket(SOCK_RAW): %m");
181 if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on,
183 error("setsockopt(IP_HDRINCL): %m");
188 * Packet filter program...
190 static const struct bpf_insn dhcp_bpf_filter[] = {
191 /* Use relative index (0) for IP packet... */
192 BPF_STMT(BPF_LDX + BPF_W + BPF_IMM, 0),
195 * Test whether this is a VLAN packet...
197 * In case the server packet is using a VLAN ID
198 * of 0, meaning an untagged priority was set, the
199 * response shall be read and replied to.
201 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 12),
202 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_VLAN, 0, 4),
204 /* Test whether it has a VID of 0 */
205 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
206 BPF_STMT(BPF_ALU + BPF_AND + BPF_K, EVL_VLID_MASK),
207 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 17),
209 /* Correct the relative index for VLAN packet (4)... */
210 BPF_STMT(BPF_LDX + BPF_W + BPF_IMM, 4),
212 /* Make sure this is an IP packet... */
213 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 12),
214 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 14),
216 /* Make sure it's a UDP packet... */
217 BPF_STMT(BPF_LD + BPF_B + BPF_IND, 23),
218 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 12),
220 /* Make sure this isn't a fragment... */
221 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 20),
222 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, IP_MF|IP_OFFMASK, 10, 0),
225 * Get the IP header length...
227 * To find the correct position of the IP header
228 * length field store the index (0 or 4) in the
229 * accumulator and compare it with 0.
231 BPF_STMT(BPF_MISC + BPF_TXA, 0),
232 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0, 0, 2),
233 /* Store IP header length of IP packet in index. */
234 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
235 /* Skip over following VLAN handling instruction. */
236 BPF_JUMP(BPF_JMP + BPF_JA, 1, 0, 0),
237 /* Store IP header length of VLAN packet in index. */
238 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 18),
239 /* Add IP header length to previous relative index. */
240 BPF_STMT(BPF_ALU + BPF_ADD + BPF_X, 0),
241 /* Move result back to index to reach UDP header below. */
242 BPF_STMT(BPF_MISC + BPF_TAX, 0),
244 /* Make sure it's to the right port... */
245 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
246 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, LOCAL_PORT, 0, 1),
248 /* If we passed all the tests, ask for the whole packet. */
249 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
251 /* Otherwise, drop it. */
252 BPF_STMT(BPF_RET+BPF_K, 0),
256 if_register_receive(struct interface_info *info)
258 static const unsigned long cmds[2] = { SIOCGIFFLAGS, SIOCGIFMEDIA };
260 struct bpf_version v;
261 struct bpf_program p;
264 /* Open a BPF device and hang it on this interface... */
265 info->rfdesc = if_register_bpf(info, O_RDONLY);
267 /* Make sure the BPF version is in range... */
268 if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0)
269 error("Can't get BPF version: %m");
271 if (v.bv_major != BPF_MAJOR_VERSION ||
272 v.bv_minor < BPF_MINOR_VERSION)
273 error("Kernel BPF version out of range - recompile dhcpd!");
276 * Set immediate mode so that reads return as soon as a packet
277 * comes in, rather than waiting for the input buffer to fill
280 if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0)
281 error("Can't set immediate mode on bpf device: %m");
283 /* Get the required BPF buffer length from the kernel. */
284 if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0)
285 error("Can't get bpf buffer length: %m");
287 info->rbuf = malloc(info->rbuf_max);
289 error("Can't allocate %lu bytes for bpf input buffer.",
290 (unsigned long)info->rbuf_max);
291 info->rbuf_offset = 0;
294 /* Set up the bpf filter program structure. */
295 p.bf_insns = __DECONST(struct bpf_insn *, dhcp_bpf_filter);
296 p.bf_len = nitems(dhcp_bpf_filter);
298 if (ioctl(info->rfdesc, BIOCSETF, &p) < 0)
299 error("Can't install packet filter program: %m");
301 if (ioctl(info->rfdesc, BIOCLOCK, NULL) < 0)
302 error("Cannot lock bpf");
304 cap_rights_init(&rights, CAP_IOCTL, CAP_EVENT, CAP_READ);
305 if (caph_rights_limit(info->rfdesc, &rights) < 0)
306 error("Can't limit bpf descriptor: %m");
307 if (caph_ioctls_limit(info->rfdesc, cmds, 2) < 0)
308 error("Can't limit ioctls for bpf descriptor: %m");
312 send_packet_unpriv(int privfd, struct dhcp_packet *raw, size_t len,
313 struct in_addr from, struct in_addr to)
319 hdr.code = IMSG_SEND_PACKET;
320 hdr.len = sizeof(hdr) +
321 sizeof(size_t) + len +
322 sizeof(from) + sizeof(to);
324 if ((buf = buf_open(hdr.len)) == NULL)
325 error("buf_open: %m");
328 errs += buf_add(buf, &hdr, sizeof(hdr));
329 errs += buf_add(buf, &len, sizeof(len));
330 errs += buf_add(buf, raw, len);
331 errs += buf_add(buf, &from, sizeof(from));
332 errs += buf_add(buf, &to, sizeof(to));
334 error("buf_add: %m");
336 if (buf_close(privfd, buf) == -1)
337 error("buf_close: %m");
341 send_packet_priv(struct interface_info *interface, struct imsg_hdr *hdr, int fd)
343 unsigned char buf[256];
346 struct dhcp_packet raw;
348 struct in_addr from, to;
349 int result, bufp = 0;
351 if (hdr->len < sizeof(*hdr) + sizeof(size_t))
352 error("corrupted message received");
353 buf_read(fd, &len, sizeof(len));
354 if (hdr->len != sizeof(*hdr) + sizeof(size_t) + len +
355 sizeof(from) + sizeof(to)) {
356 error("corrupted message received");
358 if (len > sizeof(raw))
359 error("corrupted message received");
360 buf_read(fd, &raw, len);
361 buf_read(fd, &from, sizeof(from));
362 buf_read(fd, &to, sizeof(to));
364 /* Assemble the headers... */
365 if (to.s_addr == INADDR_BROADCAST)
366 assemble_hw_header(interface, buf, &bufp);
367 assemble_udp_ip_header(buf, &bufp, from.s_addr, to.s_addr,
368 htons(REMOTE_PORT), (unsigned char *)&raw, len);
370 iov[0].iov_base = buf;
371 iov[0].iov_len = bufp;
372 iov[1].iov_base = &raw;
373 iov[1].iov_len = len;
376 if (to.s_addr == INADDR_BROADCAST)
377 result = writev(interface->wfdesc, iov, 2);
379 struct sockaddr_in sato;
382 sato.sin_port = htons(REMOTE_PORT);
383 sato.sin_family = AF_INET;
384 sato.sin_len = sizeof(sato);
386 memset(&msg, 0, sizeof(msg));
387 msg.msg_name = (struct sockaddr *)&sato;
388 msg.msg_namelen = sizeof(sato);
391 result = sendmsg(interface->ufdesc, &msg, 0);
395 warning("send_packet: %m");
399 receive_packet(struct interface_info *interface, unsigned char *buf,
400 size_t len, struct sockaddr_in *from, struct hardware *hfrom)
402 int length = 0, offset = 0;
406 * All this complexity is because BPF doesn't guarantee that
407 * only one packet will be returned at a time. We're getting
408 * what we deserve, though - this is a terrible abuse of the BPF
412 /* Process packets until we get one we can return or until we've
413 * done a read and gotten nothing we can return...
416 /* If the buffer is empty, fill it. */
417 if (interface->rbuf_offset >= interface->rbuf_len) {
418 length = read(interface->rfdesc, interface->rbuf,
419 interface->rbuf_max);
422 interface->rbuf_offset = 0;
423 interface->rbuf_len = length;
427 * If there isn't room for a whole bpf header, something
428 * went wrong, but we'll ignore it and hope it goes
431 if (interface->rbuf_len - interface->rbuf_offset <
433 interface->rbuf_offset = interface->rbuf_len;
437 /* Copy out a bpf header... */
438 memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
442 * If the bpf header plus data doesn't fit in what's
443 * left of the buffer, stick head in sand yet again...
445 if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
446 interface->rbuf_len) {
447 interface->rbuf_offset = interface->rbuf_len;
451 /* Skip over the BPF header... */
452 interface->rbuf_offset += hdr.bh_hdrlen;
455 * If the captured data wasn't the whole packet, or if
456 * the packet won't fit in the input buffer, all we can
459 if (hdr.bh_caplen != hdr.bh_datalen) {
460 interface->rbuf_offset =
461 BPF_WORDALIGN(interface->rbuf_offset +
466 /* Decode the physical header... */
467 offset = decode_hw_header(interface->rbuf,
468 interface->rbuf_offset, hfrom);
471 * If a physical layer checksum failed (dunno of any
472 * physical layer that supports this, but WTH), skip
476 interface->rbuf_offset =
477 BPF_WORDALIGN(interface->rbuf_offset +
481 interface->rbuf_offset += offset;
482 hdr.bh_caplen -= offset;
484 /* Decode the IP and UDP headers... */
485 offset = decode_udp_ip_header(interface->rbuf,
486 interface->rbuf_offset, from, NULL, hdr.bh_caplen);
488 /* If the IP or UDP checksum was bad, skip the packet... */
490 interface->rbuf_offset =
491 BPF_WORDALIGN(interface->rbuf_offset +
495 interface->rbuf_offset += offset;
496 hdr.bh_caplen -= offset;
499 * If there's not enough room to stash the packet data,
500 * we have to skip it (this shouldn't happen in real
503 if (hdr.bh_caplen > len) {
504 interface->rbuf_offset =
505 BPF_WORDALIGN(interface->rbuf_offset +
510 /* Copy out the data in the packet... */
511 memcpy(buf, interface->rbuf + interface->rbuf_offset,
513 interface->rbuf_offset =
514 BPF_WORDALIGN(interface->rbuf_offset +
516 return (hdr.bh_caplen);