1*b57001d2Sdlg /* $OpenBSD: bpf.c,v 1.20 2019/03/18 00:00:59 dlg Exp $ */
2e853bc5dShenning
3e853bc5dShenning /* BPF socket interface code, originally contributed by Archie Cobbs. */
4e853bc5dShenning
5e853bc5dShenning /*
6e853bc5dShenning * Copyright (c) 1995, 1996, 1998, 1999
7e853bc5dShenning * The Internet Software Consortium. All rights reserved.
8e853bc5dShenning *
9e853bc5dShenning * Redistribution and use in source and binary forms, with or without
10e853bc5dShenning * modification, are permitted provided that the following conditions
11e853bc5dShenning * are met:
12e853bc5dShenning *
13e853bc5dShenning * 1. Redistributions of source code must retain the above copyright
14e853bc5dShenning * notice, this list of conditions and the following disclaimer.
15e853bc5dShenning * 2. Redistributions in binary form must reproduce the above copyright
16e853bc5dShenning * notice, this list of conditions and the following disclaimer in the
17e853bc5dShenning * documentation and/or other materials provided with the distribution.
18e853bc5dShenning * 3. Neither the name of The Internet Software Consortium nor the names
19e853bc5dShenning * of its contributors may be used to endorse or promote products derived
20e853bc5dShenning * from this software without specific prior written permission.
21e853bc5dShenning *
22e853bc5dShenning * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23e853bc5dShenning * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24e853bc5dShenning * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25e853bc5dShenning * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26e853bc5dShenning * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27e853bc5dShenning * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28e853bc5dShenning * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29e853bc5dShenning * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30e853bc5dShenning * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31e853bc5dShenning * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32e853bc5dShenning * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33e853bc5dShenning * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34e853bc5dShenning * SUCH DAMAGE.
35e853bc5dShenning *
36e853bc5dShenning * This software has been written for the Internet Software Consortium
37e853bc5dShenning * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38e853bc5dShenning * Enterprises. To learn more about the Internet Software Consortium,
39e853bc5dShenning * see ``http://www.vix.com/isc''. To learn more about Vixie
40e853bc5dShenning * Enterprises, see ``http://www.vix.com''.
41e853bc5dShenning */
42e853bc5dShenning
43e853bc5dShenning #include <sys/ioctl.h>
44837cddffSkrw #include <sys/types.h>
45837cddffSkrw #include <sys/socket.h>
46837cddffSkrw
47837cddffSkrw #include <arpa/inet.h>
48e853bc5dShenning
49e853bc5dShenning #include <net/bpf.h>
50837cddffSkrw #include <net/if.h>
51837cddffSkrw
52e853bc5dShenning #include <netinet/if_ether.h>
53837cddffSkrw #include <netinet/in.h>
54837cddffSkrw
55837cddffSkrw #include <errno.h>
56837cddffSkrw #include <fcntl.h>
57837cddffSkrw #include <stdio.h>
58837cddffSkrw #include <stdlib.h>
59837cddffSkrw #include <string.h>
60837cddffSkrw #include <unistd.h>
61837cddffSkrw
62837cddffSkrw #include "dhcp.h"
63837cddffSkrw #include "tree.h"
64837cddffSkrw #include "dhcpd.h"
65c525a185Skrw #include "log.h"
66e853bc5dShenning
6784d8c049Syasuoka ssize_t send_packet (struct interface_info *, struct dhcp_packet *,
6884d8c049Syasuoka size_t, struct in_addr, struct sockaddr_in *, struct hardware *);
6984d8c049Syasuoka
70e853bc5dShenning /*
71e853bc5dShenning * Called by get_interface_list for each interface that's discovered.
72e853bc5dShenning * Opens a packet filter for each interface and adds it to the select
73e853bc5dShenning * mask.
74e853bc5dShenning */
75e853bc5dShenning int
if_register_bpf(struct interface_info * info)76e853bc5dShenning if_register_bpf(struct interface_info *info)
77e853bc5dShenning {
782abf9a0dSnatano int sock;
79e853bc5dShenning
807b08a90aSnatano if ((sock = open("/dev/bpf", O_RDWR)) == -1)
810438cf0aSkrw fatal("Can't open bpf device");
82e853bc5dShenning
83e853bc5dShenning /* Set the BPF device to point at this interface. */
849bb003e4Sclaudio if (ioctl(sock, BIOCSETIF, info->ifp) == -1)
850438cf0aSkrw fatal("Can't attach interface %s to bpf device", info->name);
86e853bc5dShenning
8784d8c049Syasuoka info->send_packet = send_packet;
88e853bc5dShenning return (sock);
89e853bc5dShenning }
90e853bc5dShenning
91e853bc5dShenning void
if_register_send(struct interface_info * info)92e853bc5dShenning if_register_send(struct interface_info *info)
93e853bc5dShenning {
94e853bc5dShenning /*
95e853bc5dShenning * If we're using the bpf API for sending and receiving, we
96e853bc5dShenning * don't need to register this interface twice.
97e853bc5dShenning */
98e853bc5dShenning info->wfdesc = info->rfdesc;
99e853bc5dShenning }
100e853bc5dShenning
101e853bc5dShenning /*
102390956b7Scanacar * Packet read filter program: 'ip and udp and dst port bootps'
103e853bc5dShenning */
104e853bc5dShenning struct bpf_insn dhcp_bpf_filter[] = {
105e853bc5dShenning /* Make sure this is an IP packet... */
106e853bc5dShenning BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
107e853bc5dShenning BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
108e853bc5dShenning
109e853bc5dShenning /* Make sure it's a UDP packet... */
110e853bc5dShenning BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
111e853bc5dShenning BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
112e853bc5dShenning
113e853bc5dShenning /* Make sure this isn't a fragment... */
114e853bc5dShenning BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
115e853bc5dShenning BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
116e853bc5dShenning
117e853bc5dShenning /* Get the IP header length... */
118e853bc5dShenning BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
119e853bc5dShenning
120e853bc5dShenning /* Make sure it's to the right port... */
121e853bc5dShenning BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
122390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1),
123e853bc5dShenning
124e853bc5dShenning /* If we passed all the tests, ask for the whole packet. */
125e853bc5dShenning BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
126e853bc5dShenning
127e853bc5dShenning /* Otherwise, drop it. */
128e853bc5dShenning BPF_STMT(BPF_RET+BPF_K, 0),
129e853bc5dShenning };
130e853bc5dShenning
131e853bc5dShenning int dhcp_bpf_filter_len = sizeof(dhcp_bpf_filter) / sizeof(struct bpf_insn);
132e853bc5dShenning
133390956b7Scanacar
134390956b7Scanacar /*
135390956b7Scanacar * Packet write filter program:
136390956b7Scanacar * 'ip and udp and src port bootps and dst port (bootps or bootpc)'
137390956b7Scanacar */
138390956b7Scanacar struct bpf_insn dhcp_bpf_wfilter[] = {
139390956b7Scanacar /* Make sure this is an IP packet... */
140390956b7Scanacar BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
141390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 11),
142390956b7Scanacar
143390956b7Scanacar /* Make sure it's a UDP packet... */
144390956b7Scanacar BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 23),
145390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 9),
146390956b7Scanacar
147390956b7Scanacar /* Make sure this isn't a fragment... */
148390956b7Scanacar BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
149390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 7, 0),
150390956b7Scanacar
151390956b7Scanacar /* Get the IP header length... */
152390956b7Scanacar BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, 14),
153390956b7Scanacar
154390956b7Scanacar /* Make sure it's from the right port... */
155390956b7Scanacar BPF_STMT(BPF_LD + BPF_H + BPF_IND, 14),
156390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 4),
157390956b7Scanacar
158390956b7Scanacar /* Make sure it is to the right ports ... */
159390956b7Scanacar BPF_STMT(BPF_LD + BPF_H + BPF_IND, 16),
160390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, CLIENT_PORT, 1, 0),
161390956b7Scanacar BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SERVER_PORT, 0, 1),
162390956b7Scanacar
163390956b7Scanacar /* If we passed all the tests, ask for the whole packet. */
164390956b7Scanacar BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
165390956b7Scanacar
166390956b7Scanacar /* Otherwise, drop it. */
167390956b7Scanacar BPF_STMT(BPF_RET+BPF_K, 0),
168390956b7Scanacar };
169390956b7Scanacar
170390956b7Scanacar int dhcp_bpf_wfilter_len = sizeof(dhcp_bpf_wfilter) / sizeof(struct bpf_insn);
171390956b7Scanacar
172e853bc5dShenning void
if_register_receive(struct interface_info * info)173e853bc5dShenning if_register_receive(struct interface_info *info)
174e853bc5dShenning {
175e853bc5dShenning struct bpf_version v;
176e853bc5dShenning struct bpf_program p;
177390956b7Scanacar int flag = 1, sz, cmplt = 0;
178*b57001d2Sdlg int fildrop = BPF_FILDROP_CAPTURE;
179e853bc5dShenning
180e853bc5dShenning /* Open a BPF device and hang it on this interface... */
181e853bc5dShenning info->rfdesc = if_register_bpf(info);
182e853bc5dShenning
183e853bc5dShenning /* Make sure the BPF version is in range... */
1849bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCVERSION, &v) == -1)
1850438cf0aSkrw fatal("Can't get BPF version");
186e853bc5dShenning
187e853bc5dShenning if (v.bv_major != BPF_MAJOR_VERSION ||
188e853bc5dShenning v.bv_minor < BPF_MINOR_VERSION)
189c525a185Skrw fatalx("Kernel BPF version out of range - recompile dhcpd!");
190e853bc5dShenning
191e853bc5dShenning /*
192e853bc5dShenning * Set immediate mode so that reads return as soon as a packet
193e853bc5dShenning * comes in, rather than waiting for the input buffer to fill
194e853bc5dShenning * with packets.
195e853bc5dShenning */
1969bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCIMMEDIATE, &flag) == -1)
1970438cf0aSkrw fatal("Can't set immediate mode on bpf device");
198e853bc5dShenning
199*b57001d2Sdlg if (ioctl(info->rfdesc, BIOCSFILDROP, &fildrop) == -1)
2000438cf0aSkrw fatal("Can't set filter-drop mode on bpf device");
201e7791a15Smpf
202390956b7Scanacar /* make sure kernel fills in the source ethernet address */
2039bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCSHDRCMPLT, &cmplt) == -1)
2040438cf0aSkrw fatal("Can't set header complete flag on bpf device");
205390956b7Scanacar
206e853bc5dShenning /* Get the required BPF buffer length from the kernel. */
2079bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCGBLEN, &sz) == -1)
2080438cf0aSkrw fatal("Can't get bpf buffer length");
209e853bc5dShenning info->rbuf_max = sz;
210e853bc5dShenning info->rbuf = malloc(info->rbuf_max);
211e853bc5dShenning if (!info->rbuf)
212c525a185Skrw fatalx("Can't allocate %lu bytes for bpf input buffer.",
213e853bc5dShenning (unsigned long)info->rbuf_max);
214e853bc5dShenning info->rbuf_offset = 0;
215e853bc5dShenning info->rbuf_len = 0;
216e853bc5dShenning
217e853bc5dShenning /* Set up the bpf filter program structure. */
218e853bc5dShenning p.bf_len = dhcp_bpf_filter_len;
219e853bc5dShenning p.bf_insns = dhcp_bpf_filter;
220e853bc5dShenning
2219bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCSETF, &p) == -1)
2220438cf0aSkrw fatal("Can't install packet filter program");
223390956b7Scanacar
224390956b7Scanacar /* Set up the bpf write filter program structure. */
225390956b7Scanacar p.bf_len = dhcp_bpf_wfilter_len;
226390956b7Scanacar p.bf_insns = dhcp_bpf_wfilter;
227390956b7Scanacar
2289bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCSETWF, &p) == -1)
2290438cf0aSkrw fatal("Can't install write filter program");
230390956b7Scanacar
231390956b7Scanacar /* make sure these settings cannot be changed after dropping privs */
2329bb003e4Sclaudio if (ioctl(info->rfdesc, BIOCLOCK) == -1)
2330438cf0aSkrw fatal("Failed to lock bpf descriptor");
234e853bc5dShenning }
235e853bc5dShenning
236e853bc5dShenning 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)237285f06efSderaadt send_packet(struct interface_info *interface, struct dhcp_packet *raw,
238285f06efSderaadt size_t len, struct in_addr from, struct sockaddr_in *to,
239285f06efSderaadt struct hardware *hto)
240e853bc5dShenning {
241e853bc5dShenning unsigned char buf[256];
242e853bc5dShenning struct iovec iov[2];
2431a4d4b6bSkrw ssize_t result;
2441a4d4b6bSkrw int bufp = 0;
245e853bc5dShenning
246e853bc5dShenning /* Assemble the headers... */
247e853bc5dShenning assemble_hw_header(interface, buf, &bufp, hto);
248e853bc5dShenning assemble_udp_ip_header(interface, buf, &bufp, from.s_addr,
249e853bc5dShenning to->sin_addr.s_addr, to->sin_port, (unsigned char *)raw, len);
250e853bc5dShenning
251e853bc5dShenning /* Fire it off */
252e853bc5dShenning iov[0].iov_base = (char *)buf;
253e853bc5dShenning iov[0].iov_len = bufp;
254e853bc5dShenning iov[1].iov_base = (char *)raw;
255e853bc5dShenning iov[1].iov_len = len;
256e853bc5dShenning
257e853bc5dShenning result = writev(interface->wfdesc, iov, 2);
2589bb003e4Sclaudio if (result == -1)
2590438cf0aSkrw log_warn("send_packet");
260e853bc5dShenning return (result);
261e853bc5dShenning }
262e853bc5dShenning
263e853bc5dShenning ssize_t
receive_packet(struct interface_info * interface,unsigned char * buf,size_t len,struct sockaddr_in * from,struct hardware * hfrom)264e853bc5dShenning receive_packet(struct interface_info *interface, unsigned char *buf,
265e853bc5dShenning size_t len, struct sockaddr_in *from, struct hardware *hfrom)
266e853bc5dShenning {
267e853bc5dShenning int length = 0, offset = 0;
268e853bc5dShenning struct bpf_hdr hdr;
269e853bc5dShenning
270e853bc5dShenning /*
271e853bc5dShenning * All this complexity is because BPF doesn't guarantee that
272e853bc5dShenning * only one packet will be returned at a time. We're getting
273e853bc5dShenning * what we deserve, though - this is a terrible abuse of the BPF
274e853bc5dShenning * interface. Sigh.
275e853bc5dShenning */
276e853bc5dShenning
277e853bc5dShenning /* Process packets until we get one we can return or until we've
278e853bc5dShenning * done a read and gotten nothing we can return...
279e853bc5dShenning */
280e853bc5dShenning do {
281e853bc5dShenning /* If the buffer is empty, fill it. */
282bb4d9bd7Skrw if (interface->rbuf_offset >= interface->rbuf_len) {
283e853bc5dShenning length = read(interface->rfdesc, interface->rbuf,
284e853bc5dShenning interface->rbuf_max);
285e853bc5dShenning if (length <= 0)
286e853bc5dShenning return (length);
287e853bc5dShenning interface->rbuf_offset = 0;
288bb4d9bd7Skrw interface->rbuf_len = length;
289e853bc5dShenning }
290e853bc5dShenning
291e853bc5dShenning /*
292e853bc5dShenning * If there isn't room for a whole bpf header, something
293e853bc5dShenning * went wrong, but we'll ignore it and hope it goes
294e853bc5dShenning * away... XXX
295e853bc5dShenning */
296e853bc5dShenning if (interface->rbuf_len - interface->rbuf_offset <
297e853bc5dShenning sizeof(hdr)) {
298e853bc5dShenning interface->rbuf_offset = interface->rbuf_len;
299e853bc5dShenning continue;
300e853bc5dShenning }
301e853bc5dShenning
302e853bc5dShenning /* Copy out a bpf header... */
303e853bc5dShenning memcpy(&hdr, &interface->rbuf[interface->rbuf_offset],
304e853bc5dShenning sizeof(hdr));
305e853bc5dShenning
306e853bc5dShenning /*
307e853bc5dShenning * If the bpf header plus data doesn't fit in what's
308e853bc5dShenning * left of the buffer, stick head in sand yet again...
309e853bc5dShenning */
310e853bc5dShenning if (interface->rbuf_offset + hdr.bh_hdrlen + hdr.bh_caplen >
311e853bc5dShenning interface->rbuf_len) {
312e853bc5dShenning interface->rbuf_offset = interface->rbuf_len;
313e853bc5dShenning continue;
314e853bc5dShenning }
315e853bc5dShenning
316e853bc5dShenning /*
317e853bc5dShenning * If the captured data wasn't the whole packet, or if
318e853bc5dShenning * the packet won't fit in the input buffer, all we can
319e853bc5dShenning * do is drop it.
320e853bc5dShenning */
321e853bc5dShenning if (hdr.bh_caplen != hdr.bh_datalen) {
32215035272Skrw interface->rbuf_offset = BPF_WORDALIGN(
32315035272Skrw interface->rbuf_offset + hdr.bh_hdrlen +
32415035272Skrw hdr.bh_caplen);
325e853bc5dShenning continue;
326e853bc5dShenning }
327e853bc5dShenning
328e853bc5dShenning /* Skip over the BPF header... */
329e853bc5dShenning interface->rbuf_offset += hdr.bh_hdrlen;
330e853bc5dShenning
331e853bc5dShenning /* Decode the physical header... */
3326c731deeSkrw offset = decode_hw_header(interface->rbuf +
3336c731deeSkrw interface->rbuf_offset, hdr.bh_caplen, hfrom);
334e853bc5dShenning
335e853bc5dShenning /*
336e853bc5dShenning * If a physical layer checksum failed (dunno of any
337e853bc5dShenning * physical layer that supports this, but WTH), skip
338e853bc5dShenning * this packet.
339e853bc5dShenning */
340e853bc5dShenning if (offset < 0) {
34115035272Skrw interface->rbuf_offset = BPF_WORDALIGN(
34215035272Skrw interface->rbuf_offset + hdr.bh_caplen);
343e853bc5dShenning continue;
344e853bc5dShenning }
345e853bc5dShenning interface->rbuf_offset += offset;
346e853bc5dShenning hdr.bh_caplen -= offset;
347e853bc5dShenning
348e853bc5dShenning /* Decode the IP and UDP headers... */
3496c731deeSkrw offset = decode_udp_ip_header(interface->rbuf +
3506c731deeSkrw interface->rbuf_offset, hdr.bh_caplen, from);
351e853bc5dShenning
352e853bc5dShenning /* If the IP or UDP checksum was bad, skip the packet... */
353e853bc5dShenning if (offset < 0) {
35415035272Skrw interface->rbuf_offset = BPF_WORDALIGN(
35515035272Skrw interface->rbuf_offset + hdr.bh_caplen);
356e853bc5dShenning continue;
357e853bc5dShenning }
358e853bc5dShenning interface->rbuf_offset += offset;
359e853bc5dShenning hdr.bh_caplen -= offset;
360e853bc5dShenning
361e853bc5dShenning /*
362e853bc5dShenning * If there's not enough room to stash the packet data,
363e853bc5dShenning * we have to skip it (this shouldn't happen in real
364e853bc5dShenning * life, though).
365e853bc5dShenning */
366e853bc5dShenning if (hdr.bh_caplen > len) {
36715035272Skrw interface->rbuf_offset = BPF_WORDALIGN(
36815035272Skrw interface->rbuf_offset + hdr.bh_caplen);
369e853bc5dShenning continue;
370e853bc5dShenning }
371e853bc5dShenning
372e853bc5dShenning /* Copy out the data in the packet... */
373e853bc5dShenning memcpy(buf, interface->rbuf + interface->rbuf_offset,
374e853bc5dShenning hdr.bh_caplen);
37515035272Skrw interface->rbuf_offset = BPF_WORDALIGN(interface->rbuf_offset +
37615035272Skrw hdr.bh_caplen);
377e853bc5dShenning return (hdr.bh_caplen);
378e853bc5dShenning } while (!length);
379e853bc5dShenning return (0);
380e853bc5dShenning }
381