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