3
BPF socket interface code, originally contributed by Archie Cobbs. */
6
* Copyright (c) 1995, 1996, 1998, 1999
7
* The Internet Software Consortium. All rights reserved.
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
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.
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
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''.
44
static char copyright[] =
45
"$Id: bpf.c,v 1.19.2.10 1999/05/27 17:44:51 mellon Exp $ Copyright (c) 1995, 1996, 1998, 1999 The Internet Software Consortium. All rights reserved.\n";
49
#if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
50
|| defined (USE_LPF_RECEIVE)
51
# if defined (USE_LPF_RECEIVE)
52
# include <asm/types.h>
53
# include <linux/filter.h>
54
# define bpf_insn sock_filter /* Linux: dare to be gratuitously different. */
56
# include <sys/ioctl.h>
60
# if defined (NEED_OSF_PFILT_HACKS)
61
# include <net/pfilt.h>
65
#include <netinet/in_systm.h>
66
#include "includes/netinet/ip.h"
67
#include "includes/netinet/udp.h"
68
#include "includes/netinet/if_ether.h"
71
/* Reinitializes the specified interface after an address change. This
72
is not required for packet-filter APIs. */
75
void if_reinitialize_send (info)
76
struct interface_info *info;
81
#ifdef USE_BPF_RECEIVE
82
void if_reinitialize_receive (info)
83
struct interface_info *info;
88
/* Called by get_interface_list for each interface that's discovered.
89
Opens a packet filter for each interface and adds it to the select
92
#if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
93
int if_register_bpf (info)
94
struct interface_info *info;
100
/* Open a BPF device */
101
for (b = 0; 1; b++) {
103
snprintf(filename, sizeof(filename), BPF_FORMAT, b);
105
sprintf(filename, BPF_FORMAT, b);
107
sock = open (filename, O_RDWR, 0);
109
if (errno == EBUSY) {
113
error ("No bpf devices.%s%s%s",
114
" Please read the README",
115
" section for your operating",
117
error ("Can't find free bpf: %m");
124
/* Set the BPF device to point at this interface. */
125
if (ioctl (sock, BIOCSETIF, info -> ifp) < 0)
126
error ("Can't attach interface %s to bpf device %s: %m",
127
info -> name, filename);
131
#endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
134
void if_register_send (info)
135
struct interface_info *info;
137
/* If we're using the bpf API for sending and receiving,
138
we don't need to register this interface twice. */
139
#ifndef USE_BPF_RECEIVE
140
info -> wfdesc = if_register_bpf (info, interface);
142
info -> wfdesc = info -> rfdesc;
144
if (!quiet_interface_discovery)
145
note ("Sending on BPF/%s/%s%s%s",
147
print_hw_addr (info -> hw_address.htype,
148
info -> hw_address.hlen,
149
info -> hw_address.haddr),
150
(info -> shared_network ? "/" : ""),
151
(info -> shared_network ?
152
info -> shared_network -> name : ""));
154
#endif /* USE_BPF_SEND */
156
#if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
157
/* Packet filter program...
158
XXX Changes to the filter program may require changes to the constant
159
offsets used in if_register_send to patch the BPF program! XXX */
161
struct bpf_insn dhcp_bpf_filter [] = {
162
/* Make sure this is an IP packet... */
163
BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12),
164
BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
166
/* Make sure it's a UDP packet... */
167
BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23),
168
BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
170
/* Make sure this isn't a fragment... */
171
BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
172
BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
174
/* Get the IP header length... */
175
BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14),
177
/* Make sure it's to the right port... */
178
BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16),
179
BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */
181
/* If we passed all the tests, ask for the whole packet. */
182
BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
184
/* Otherwise, drop it. */
185
BPF_STMT(BPF_RET+BPF_K, 0),
188
int dhcp_bpf_filter_len = sizeof dhcp_bpf_filter / sizeof (struct bpf_insn);
190
struct bpf_insn dhcp_bpf_tr_filter [] = {
191
/* accept all token ring packets due to variable length header */
192
/* if we want to get clever, insert the program here */
194
/* If we passed all the tests, ask for the whole packet. */
195
BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
197
/* Otherwise, drop it. */
198
BPF_STMT(BPF_RET+BPF_K, 0),
201
int dhcp_bpf_tr_filter_len = (sizeof dhcp_bpf_tr_filter /
202
sizeof (struct bpf_insn));
205
#if defined (USE_BPF_RECEIVE)
206
void if_register_receive (info)
207
struct interface_info *info;
210
struct bpf_version v;
212
struct bpf_program p;
215
/* Open a BPF device and hang it on this interface... */
216
info -> rfdesc = if_register_bpf (info);
218
/* Make sure the BPF version is in range... */
219
if (ioctl (info -> rfdesc, BIOCVERSION, &v) < 0)
220
error ("Can't get BPF version: %m");
222
if (v.bv_major != BPF_MAJOR_VERSION ||
223
v.bv_minor < BPF_MINOR_VERSION)
224
error ("Kernel BPF version out of range - recompile dhcpd!");
226
/* Set immediate mode so that reads return as soon as a packet
227
comes in, rather than waiting for the input buffer to fill with
229
if (ioctl (info -> rfdesc, BIOCIMMEDIATE, &flag) < 0)
230
error ("Can't set immediate mode on bpf device: %m");
232
#ifdef NEED_OSF_PFILT_HACKS
233
/* Allow the copyall flag to be set... */
234
if (ioctl(info -> rfdesc, EIOCALLOWCOPYALL, &flag) < 0)
235
error ("Can't set ALLOWCOPYALL: %m");
237
/* Clear all the packet filter mode bits first... */
239
if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
240
error ("Can't clear pfilt bits: %m");
242
/* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
243
bits = ENBATCH | ENCOPYALL | ENBPFHDR;
244
if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
245
error ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
247
/* Get the required BPF buffer length from the kernel. */
248
if (ioctl (info -> rfdesc, BIOCGBLEN, &info -> rbuf_max) < 0)
249
error ("Can't get bpf buffer length: %m");
250
info -> rbuf = malloc (info -> rbuf_max);
252
error ("Can't allocate %d bytes for bpf input buffer.");
253
info -> rbuf_offset = 0;
254
info -> rbuf_len = 0;
256
/* Set up the bpf filter program structure. */
257
p.bf_len = dhcp_bpf_filter_len;
258
p.bf_insns = dhcp_bpf_filter;
260
/* Patch the server port into the BPF program...
261
XXX changes to filter program may require changes
262
to the insn number(s) used below! XXX */
263
dhcp_bpf_filter [8].k = ntohs (local_port);
265
if (ioctl (info -> rfdesc, BIOCSETF, &p) < 0)
266
error ("Can't install packet filter program: %m");
267
if (!quiet_interface_discovery)
268
note ("Listening on BPF/%s/%s%s%s",
270
print_hw_addr (info -> hw_address.htype,
271
info -> hw_address.hlen,
272
info -> hw_address.haddr),
273
(info -> shared_network ? "/" : ""),
274
(info -> shared_network ?
275
info -> shared_network -> name : ""));
277
#endif /* USE_BPF_RECEIVE */
280
ssize_t send_packet (interface, packet, raw, len, from, to, hto)
281
struct interface_info *interface;
282
struct packet *packet;
283
struct dhcp_packet *raw;
286
struct sockaddr_in *to;
287
struct hardware *hto;
290
unsigned char buf [256];
291
struct iovec iov [2];
294
if (!strcmp (interface -> name, "fallback"))
295
return send_fallback (interface, packet, raw,
298
/* Assemble the headers... */
299
assemble_hw_header (interface, buf, &bufp, hto);
300
assemble_udp_ip_header (interface, buf, &bufp, from.s_addr,
301
to -> sin_addr.s_addr, to -> sin_port,
302
(unsigned char *)raw, len);
305
iov [0].iov_base = (char *)buf;
306
iov [0].iov_len = bufp;
307
iov [1].iov_base = (char *)raw;
308
iov [1].iov_len = len;
310
result = writev(interface -> wfdesc, iov, 2);
312
warn ("send_packet: %m");
315
#endif /* USE_BPF_SEND */
317
#ifdef USE_BPF_RECEIVE
318
ssize_t receive_packet (interface, buf, len, from, hfrom)
319
struct interface_info *interface;
322
struct sockaddr_in *from;
323
struct hardware *hfrom;
329
/* All this complexity is because BPF doesn't guarantee
330
that only one packet will be returned at a time. We're
331
getting what we deserve, though - this is a terrible abuse
332
of the BPF interface. Sigh. */
334
/* Process packets until we get one we can return or until we've
335
done a read and gotten nothing we can return... */
338
/* If the buffer is empty, fill it. */
339
if (interface -> rbuf_offset == interface -> rbuf_len) {
340
length = read (interface -> rfdesc,
342
interface -> rbuf_max);
345
interface -> rbuf_offset = 0;
346
interface -> rbuf_len = length;
349
/* If there isn't room for a whole bpf header, something went
350
wrong, but we'll ignore it and hope it goes away... XXX */
351
if (interface -> rbuf_len -
352
interface -> rbuf_offset < sizeof hdr) {
353
interface -> rbuf_offset = interface -> rbuf_len;
357
/* Copy out a bpf header... */
358
memcpy (&hdr, &interface -> rbuf [interface -> rbuf_offset],
361
/* If the bpf header plus data doesn't fit in what's left
362
of the buffer, stick head in sand yet again... */
363
if (interface -> rbuf_offset +
364
hdr.bh_hdrlen + hdr.bh_caplen > interface -> rbuf_len) {
365
interface -> rbuf_offset = interface -> rbuf_len;
369
/* If the captured data wasn't the whole packet, or if
370
the packet won't fit in the input buffer, all we
371
can do is drop it. */
372
if (hdr.bh_caplen != hdr.bh_datalen) {
373
interface -> rbuf_offset +=
374
hdr.bh_hdrlen = hdr.bh_caplen;
378
/* Skip over the BPF header... */
379
interface -> rbuf_offset += hdr.bh_hdrlen;
381
/* Decode the physical header... */
382
offset = decode_hw_header (interface,
384
interface -> rbuf_offset,
387
/* If a physical layer checksum failed (dunno of any
388
physical layer that supports this, but WTH), skip this
391
interface -> rbuf_offset += hdr.bh_caplen;
394
interface -> rbuf_offset += offset;
395
hdr.bh_caplen -= offset;
397
/* Decode the IP and UDP headers... */
398
offset = decode_udp_ip_header (interface,
400
interface -> rbuf_offset,
405
/* If the IP or UDP checksum was bad, skip the packet... */
407
interface -> rbuf_offset += hdr.bh_caplen;
410
interface -> rbuf_offset += offset;
411
hdr.bh_caplen -= offset;
413
/* If there's not enough room to stash the packet data,
414
we have to skip it (this shouldn't happen in real
416
if (hdr.bh_caplen > len) {
417
interface -> rbuf_offset += hdr.bh_caplen;
421
/* Copy out the data in the packet... */
422
memcpy (buf, interface -> rbuf + interface -> rbuf_offset,
424
interface -> rbuf_offset += hdr.bh_caplen;
425
return hdr.bh_caplen;
430
int can_unicast_without_arp ()
435
int can_receive_unicast_unconfigured (ip)
436
struct interface_info *ip;
441
void maybe_setup_fallback ()
443
struct interface_info *fbi;
444
fbi = setup_fallback ();
446
if_register_fallback (fbi);
447
add_protocol ("fallback", fallback_interface -> wfdesc,
448
fallback_discard, fallback_interface);