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) 1995, 1996, 1998, 1999
9 * The Internet Software Consortium. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
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''.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
50 #include <sys/capsicum.h>
51 #include <sys/ioctl.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/udp.h>
58 #include <netinet/if_ether.h>
60 #include <capsicum_helpers.h>
62 #define BPF_FORMAT "/dev/bpf%d"
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
70 if_register_bpf(struct interface_info *info, int flags)
75 /* Open a BPF device */
77 snprintf(filename, sizeof(filename), BPF_FORMAT, b);
78 sock = open(filename, flags);
83 error("Can't find free bpf: %m");
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);
93 /* Tag the packets with the proper VLAN PCP setting. */
94 if (info->client->config->vlan_pcp != 0) {
95 if (ioctl(sock, BIOCSETVLANPCP,
96 &info->client->config->vlan_pcp) < 0)
97 error( "Can't set the VLAN PCP tag on interface %s: %m",
105 * Packet write filter program:
106 * 'ip and udp and src port bootps and dst port (bootps or bootpc)'
108 static struct bpf_insn dhcp_bpf_wfilter[] = {
109 BPF_STMT(BPF_LD + BPF_B + BPF_IND, 14),
110 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (IPVERSION << 4) + 5, 0, 12),
112 /* Make sure this is an IP packet... */
113 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
114 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 10),
116 /* Make sure it's a UDP packet... */
117 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
118 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 8),
120 /* Make sure this isn't a fragment... */
121 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
122 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 6, 0), /* patched */
124 /* Get the IP header length... */
125 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
127 /* Make sure it's from the right port... */
128 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
129 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 68, 0, 3),
131 /* Make sure it is to the right ports ... */
132 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
133 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1),
135 /* If we passed all the tests, ask for the whole packet. */
136 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
138 /* Otherwise, drop it. */
139 BPF_STMT(BPF_RET+BPF_K, 0),
142 static int dhcp_bpf_wfilter_len = nitems(dhcp_bpf_wfilter);
145 if_register_send(struct interface_info *info)
148 struct bpf_version v;
149 struct bpf_program p;
152 /* Open a BPF device and hang it on this interface... */
153 info->wfdesc = if_register_bpf(info, O_WRONLY);
155 /* Make sure the BPF version is in range... */
156 if (ioctl(info->wfdesc, BIOCVERSION, &v) < 0)
157 error("Can't get BPF version: %m");
159 if (v.bv_major != BPF_MAJOR_VERSION ||
160 v.bv_minor < BPF_MINOR_VERSION)
161 error("Kernel BPF version out of range - recompile dhcpd!");
163 /* Set up the bpf write filter program structure. */
164 p.bf_len = dhcp_bpf_wfilter_len;
165 p.bf_insns = dhcp_bpf_wfilter;
167 if (dhcp_bpf_wfilter[7].k == 0x1fff)
168 dhcp_bpf_wfilter[7].k = htons(IP_MF|IP_OFFMASK);
170 if (ioctl(info->wfdesc, BIOCSETWF, &p) < 0)
171 error("Can't install write filter program: %m");
173 if (ioctl(info->wfdesc, BIOCLOCK, NULL) < 0)
174 error("Cannot lock bpf");
176 cap_rights_init(&rights, CAP_WRITE);
177 if (caph_rights_limit(info->wfdesc, &rights) < 0)
178 error("Can't limit bpf descriptor: %m");
181 * Use raw socket for unicast send.
183 if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_UDP)) == -1)
184 error("socket(SOCK_RAW): %m");
185 if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &on,
187 error("setsockopt(IP_HDRINCL): %m");
192 * Packet filter program...
194 * XXX: Changes to the filter program may require changes to the
195 * constant offsets used in if_register_send to patch the BPF program!
197 static struct bpf_insn dhcp_bpf_filter[] = {
198 /* Make sure this is an IP packet... */
199 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
200 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
202 /* Make sure it's a UDP packet... */
203 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
204 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
206 /* Make sure this isn't a fragment... */
207 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
208 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
210 /* Get the IP header length... */
211 BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
213 /* Make sure it's to the right port... */
214 BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
215 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */
217 /* If we passed all the tests, ask for the whole packet. */
218 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
220 /* Otherwise, drop it. */
221 BPF_STMT(BPF_RET+BPF_K, 0),
224 static int dhcp_bpf_filter_len = nitems(dhcp_bpf_filter);
227 if_register_receive(struct interface_info *info)
229 static const unsigned long cmds[2] = { SIOCGIFFLAGS, SIOCGIFMEDIA };
231 struct bpf_version v;
232 struct bpf_program p;
235 /* Open a BPF device and hang it on this interface... */
236 info->rfdesc = if_register_bpf(info, O_RDONLY);
238 /* Make sure the BPF version is in range... */
239 if (ioctl(info->rfdesc, BIOCVERSION, &v) < 0)
240 error("Can't get BPF version: %m");
242 if (v.bv_major != BPF_MAJOR_VERSION ||
243 v.bv_minor < BPF_MINOR_VERSION)
244 error("Kernel BPF version out of range - recompile dhcpd!");
247 * Set immediate mode so that reads return as soon as a packet
248 * comes in, rather than waiting for the input buffer to fill
251 if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) < 0)
252 error("Can't set immediate mode on bpf device: %m");
254 /* Get the required BPF buffer length from the kernel. */
255 if (ioctl(info->rfdesc, BIOCGBLEN, &sz) < 0)
256 error("Can't get bpf buffer length: %m");
258 info->rbuf = malloc(info->rbuf_max);
260 error("Can't allocate %lu bytes for bpf input buffer.",
261 (unsigned long)info->rbuf_max);
262 info->rbuf_offset = 0;
265 /* Set up the bpf filter program structure. */
266 p.bf_len = dhcp_bpf_filter_len;
267 p.bf_insns = dhcp_bpf_filter;
269 /* Patch the server port into the BPF program...
271 * XXX: changes to filter program may require changes to the
272 * insn number(s) used below!
274 dhcp_bpf_filter[8].k = LOCAL_PORT;
276 if (ioctl(info->rfdesc, BIOCSETF, &p) < 0)
277 error("Can't install packet filter program: %m");
279 if (ioctl(info->rfdesc, BIOCLOCK, NULL) < 0)
280 error("Cannot lock bpf");
282 cap_rights_init(&rights, CAP_IOCTL, CAP_EVENT, CAP_READ);
283 if (caph_rights_limit(info->rfdesc, &rights) < 0)
284 error("Can't limit bpf descriptor: %m");
285 if (caph_ioctls_limit(info->rfdesc, cmds, 2) < 0)
286 error("Can't limit ioctls for bpf descriptor: %m");
290 send_packet_unpriv(int privfd, struct dhcp_packet *raw, size_t len,
291 struct in_addr from, struct in_addr to)
297 hdr.code = IMSG_SEND_PACKET;
298 hdr.len = sizeof(hdr) +
299 sizeof(size_t) + len +
300 sizeof(from) + sizeof(to);
302 if ((buf = buf_open(hdr.len)) == NULL)
303 error("buf_open: %m");
306 errs += buf_add(buf, &hdr, sizeof(hdr));
307 errs += buf_add(buf, &len, sizeof(len));
308 errs += buf_add(buf, raw, len);
309 errs += buf_add(buf, &from, sizeof(from));
310 errs += buf_add(buf, &to, sizeof(to));
312 error("buf_add: %m");
314 if (buf_close(privfd, buf) == -1)
315 error("buf_close: %m");
319 send_packet_priv(struct interface_info *interface, struct imsg_hdr *hdr, int fd)
321 unsigned char buf[256];
324 struct dhcp_packet raw;
326 struct in_addr from, to;
327 int result, bufp = 0;
329 if (hdr->len < sizeof(*hdr) + sizeof(size_t))
330 error("corrupted message received");
331 buf_read(fd, &len, sizeof(len));
332 if (hdr->len != sizeof(*hdr) + sizeof(size_t) + len +
333 sizeof(from) + sizeof(to)) {
334 error("corrupted message received");
336 if (len > sizeof(raw))
337 error("corrupted message received");
338 buf_read(fd, &raw, len);
339 buf_read(fd, &from, sizeof(from));
340 buf_read(fd, &to, sizeof(to));
342 /* Assemble the headers... */
343 if (to.s_addr == INADDR_BROADCAST)
344 assemble_hw_header(interface, buf, &bufp);
345 assemble_udp_ip_header(buf, &bufp, from.s_addr, to.s_addr,
346 htons(REMOTE_PORT), (unsigned char *)&raw, len);
348 iov[0].iov_base = buf;
349 iov[0].iov_len = bufp;
350 iov[1].iov_base = &raw;
351 iov[1].iov_len = len;
354 if (to.s_addr == INADDR_BROADCAST)
355 result = writev(interface->wfdesc, iov, 2);
357 struct sockaddr_in sato;
360 sato.sin_port = htons(REMOTE_PORT);
361 sato.sin_family = AF_INET;
362 sato.sin_len = sizeof(sato);
364 memset(&msg, 0, sizeof(msg));
365 msg.msg_name = (struct sockaddr *)&sato;
366 msg.msg_namelen = sizeof(sato);
369 result = sendmsg(interface->ufdesc, &msg, 0);
373 warning("send_packet: %m");
377 receive_packet(struct interface_info *interface, unsigned char *buf,
378 size_t len, struct sockaddr_in *from, struct hardware *hfrom)
380 int length = 0, offset = 0;
384 * All this complexity is because BPF doesn't guarantee that
385 * only one packet will be returned at a time. We're getting
386 * what we deserve, though - this is a terrible abuse of the BPF
390 /* Process packets until we get one we can return or until we've
391 * done a read and gotten nothing we can return...
394 /* If the buffer is empty, fill it. */
395 if (interface->rbuf_offset >= interface->rbuf_len) {
396 length = read(interface->rfdesc, interface->rbuf,
397 interface->rbuf_max);
400 interface->rbuf_offset = 0;
401 interface->rbuf_len = length;
405 * If there isn't room for a whole bpf header, something
406 * went wrong, but we'll ignore it and hope it goes
409 if (interface->rbuf_len - interface->rbuf_offset <
411 interface->rbuf_offset = interface->rbuf_len;
415 /* Copy out a bpf header... */
416 memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
420 * If the bpf header plus data doesn't fit in what's
421 * left of the buffer, stick head in sand yet again...
423 if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
424 interface->rbuf_len) {
425 interface->rbuf_offset = interface->rbuf_len;
429 /* Skip over the BPF header... */
430 interface->rbuf_offset += hdr.bh_hdrlen;
433 * If the captured data wasn't the whole packet, or if
434 * the packet won't fit in the input buffer, all we can
437 if (hdr.bh_caplen != hdr.bh_datalen) {
438 interface->rbuf_offset =
439 BPF_WORDALIGN(interface->rbuf_offset +
444 /* Decode the physical header... */
445 offset = decode_hw_header(interface->rbuf,
446 interface->rbuf_offset, hfrom);
449 * If a physical layer checksum failed (dunno of any
450 * physical layer that supports this, but WTH), skip
454 interface->rbuf_offset =
455 BPF_WORDALIGN(interface->rbuf_offset +
459 interface->rbuf_offset += offset;
460 hdr.bh_caplen -= offset;
462 /* Decode the IP and UDP headers... */
463 offset = decode_udp_ip_header(interface->rbuf,
464 interface->rbuf_offset, from, NULL, hdr.bh_caplen);
466 /* If the IP or UDP checksum was bad, skip the packet... */
468 interface->rbuf_offset =
469 BPF_WORDALIGN(interface->rbuf_offset +
473 interface->rbuf_offset += offset;
474 hdr.bh_caplen -= offset;
477 * If there's not enough room to stash the packet data,
478 * we have to skip it (this shouldn't happen in real
481 if (hdr.bh_caplen > len) {
482 interface->rbuf_offset =
483 BPF_WORDALIGN(interface->rbuf_offset +
488 /* Copy out the data in the packet... */
489 memcpy(buf, interface->rbuf + interface->rbuf_offset,
491 interface->rbuf_offset =
492 BPF_WORDALIGN(interface->rbuf_offset +
494 return (hdr.bh_caplen);