3 BPF socket interface code, originally contributed by Archie Cobbs. */
6 * Copyright (c) 1996-1999 Internet Software Consortium.
7 * Use is subject to license terms which appear in the file named
8 * ISC-LICENSE that should have accompanied this file when you
9 * received it. If a file named ISC-LICENSE did not accompany this
10 * file, or you are not sure the one you have is correct, you may
11 * obtain an applicable copy of the license at:
13 * http://www.isc.org/isc-license-1.0.html.
15 * This file is part of the ISC DHCP distribution. The documentation
16 * associated with this file is listed in the file DOCUMENTATION,
17 * included in the top-level directory of this release.
19 * Support and other services are available for ISC products - see
20 * http://www.isc.org for more information.
24 static char copyright
[] =
25 "$Id: bpf.c,v 1.34 2000/03/06 19:39:53 mellon Exp $ Copyright (c) 1995, 1996 The Internet Software Consortium. All rights reserved.\n";
29 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
30 || defined (USE_LPF_RECEIVE)
31 # if defined (USE_LPF_RECEIVE)
32 # include <asm/types.h>
33 # include <linux/filter.h>
34 # define bpf_insn sock_filter /* Linux: dare to be gratuitously different. */
36 # include <sys/ioctl.h>
39 # if defined (NEED_OSF_PFILT_HACKS)
40 # include <net/pfilt.h>
44 #include <netinet/in_systm.h>
45 #include "includes/netinet/ip.h"
46 #include "includes/netinet/udp.h"
47 #include "includes/netinet/if_ether.h"
50 /* Reinitializes the specified interface after an address change. This
51 is not required for packet-filter APIs. */
54 void if_reinitialize_send (info
)
55 struct interface_info
*info
;
60 #ifdef USE_BPF_RECEIVE
61 void if_reinitialize_receive (info
)
62 struct interface_info
*info
;
67 /* Called by get_interface_list for each interface that's discovered.
68 Opens a packet filter for each interface and adds it to the select
71 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
72 int if_register_bpf (info
)
73 struct interface_info
*info
;
79 /* Open a BPF device */
82 snprintf(filename
, sizeof(filename
), BPF_FORMAT
, b
);
84 sprintf(filename
, BPF_FORMAT
, b
);
86 sock
= open (filename
, O_RDWR
, 0);
92 log_fatal ("No bpf devices.%s%s%s",
93 " Please read the README",
94 " section for your operating",
96 log_fatal ("Can't find free bpf: %m");
103 /* Set the BPF device to point at this interface. */
104 if (ioctl (sock
, BIOCSETIF
, info
-> ifp
) < 0)
105 log_fatal ("Can't attach interface %s to bpf device %s: %m",
106 info
-> name
, filename
);
110 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
113 void if_register_send (info
)
114 struct interface_info
*info
;
116 /* If we're using the bpf API for sending and receiving,
117 we don't need to register this interface twice. */
118 #ifndef USE_BPF_RECEIVE
119 info
-> wfdesc
= if_register_bpf (info
, interface
);
121 info
-> wfdesc
= info
-> rfdesc
;
123 if (!quiet_interface_discovery
)
124 log_info ("Sending on BPF/%s/%s%s%s",
126 print_hw_addr (info
-> hw_address
.hbuf
[0],
127 info
-> hw_address
.hlen
- 1,
128 &info
-> hw_address
.hbuf
[1]),
129 (info
-> shared_network
? "/" : ""),
130 (info
-> shared_network
?
131 info
-> shared_network
-> name
: ""));
134 void if_deregister_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 close (info
-> wfdesc
);
144 if (!quiet_interface_discovery
)
145 log_info ("Disabling output on BPF/%s/%s%s%s",
147 print_hw_addr (info
-> hw_address
.hbuf
[0],
148 info
-> hw_address
.hlen
- 1,
149 &info
-> hw_address
.hbuf
[1]),
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
);
189 struct bpf_insn dhcp_bpf_tr_filter
[] = {
190 /* accept all token ring packets due to variable length header */
191 /* if we want to get clever, insert the program here */
193 /* If we passed all the tests, ask for the whole packet. */
194 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1),
196 /* Otherwise, drop it. */
197 BPF_STMT(BPF_RET
+BPF_K
, 0),
200 int dhcp_bpf_tr_filter_len
= (sizeof dhcp_bpf_tr_filter
/
201 sizeof (struct bpf_insn
));
204 #if defined (USE_BPF_RECEIVE)
205 void if_register_receive (info
)
206 struct interface_info
*info
;
209 struct bpf_version v
;
211 struct bpf_program p
;
214 /* Open a BPF device and hang it on this interface... */
215 info
-> rfdesc
= if_register_bpf (info
);
217 /* Make sure the BPF version is in range... */
218 if (ioctl (info
-> rfdesc
, BIOCVERSION
, &v
) < 0)
219 log_fatal ("Can't get BPF version: %m");
221 if (v
.bv_major
!= BPF_MAJOR_VERSION
||
222 v
.bv_minor
< BPF_MINOR_VERSION
)
223 log_fatal ("Kernel BPF version out of range - recompile dhcpd!");
225 /* Set immediate mode so that reads return as soon as a packet
226 comes in, rather than waiting for the input buffer to fill with
228 if (ioctl (info
-> rfdesc
, BIOCIMMEDIATE
, &flag
) < 0)
229 log_fatal ("Can't set immediate mode on bpf device: %m");
231 #ifdef NEED_OSF_PFILT_HACKS
232 /* Allow the copyall flag to be set... */
233 if (ioctl(info
-> rfdesc
, EIOCALLOWCOPYALL
, &flag
) < 0)
234 log_fatal ("Can't set ALLOWCOPYALL: %m");
236 /* Clear all the packet filter mode bits first... */
238 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
239 log_fatal ("Can't clear pfilt bits: %m");
241 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
242 bits
= ENBATCH
| ENCOPYALL
| ENBPFHDR
;
243 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
244 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
246 /* Get the required BPF buffer length from the kernel. */
247 if (ioctl (info
-> rfdesc
, BIOCGBLEN
, &info
-> rbuf_max
) < 0)
248 log_fatal ("Can't get bpf buffer length: %m");
249 info
-> rbuf
= dmalloc (info
-> rbuf_max
, MDL
);
251 log_fatal ("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 log_fatal ("Can't install packet filter program: %m");
267 if (!quiet_interface_discovery
)
268 log_info ("Listening on BPF/%s/%s%s%s",
270 print_hw_addr (info
-> hw_address
.hbuf
[0],
271 info
-> hw_address
.hlen
- 1,
272 &info
-> hw_address
.hbuf
[1]),
273 (info
-> shared_network
? "/" : ""),
274 (info
-> shared_network
?
275 info
-> shared_network
-> name
: ""));
278 void if_deregister_receive (info
)
279 struct interface_info
*info
;
281 close (info
-> rfdesc
);
284 if (!quiet_interface_discovery
)
285 log_info ("Disabling input on BPF/%s/%s%s%s",
287 print_hw_addr (info
-> hw_address
.hbuf
[0],
288 info
-> hw_address
.hlen
- 1,
289 &info
-> hw_address
.hbuf
[1]),
290 (info
-> shared_network
? "/" : ""),
291 (info
-> shared_network
?
292 info
-> shared_network
-> name
: ""));
294 #endif /* USE_BPF_RECEIVE */
297 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
298 struct interface_info
*interface
;
299 struct packet
*packet
;
300 struct dhcp_packet
*raw
;
303 struct sockaddr_in
*to
;
304 struct hardware
*hto
;
307 unsigned char buf
[256];
308 struct iovec iov
[2];
311 if (!strcmp (interface
-> name
, "fallback"))
312 return send_fallback (interface
, packet
, raw
,
315 /* Assemble the headers... */
316 assemble_hw_header (interface
, buf
, &bufp
, hto
);
317 assemble_udp_ip_header (interface
, buf
, &bufp
, from
.s_addr
,
318 to
-> sin_addr
.s_addr
, to
-> sin_port
,
319 (unsigned char *)raw
, len
);
322 iov
[0].iov_base
= (char *)buf
;
323 iov
[0].iov_len
= bufp
;
324 iov
[1].iov_base
= (char *)raw
;
325 iov
[1].iov_len
= len
;
327 result
= writev(interface
-> wfdesc
, iov
, 2);
329 log_error ("send_packet: %m");
332 #endif /* USE_BPF_SEND */
334 #ifdef USE_BPF_RECEIVE
335 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
336 struct interface_info
*interface
;
339 struct sockaddr_in
*from
;
340 struct hardware
*hfrom
;
346 /* All this complexity is because BPF doesn't guarantee
347 that only one packet will be returned at a time. We're
348 getting what we deserve, though - this is a terrible abuse
349 of the BPF interface. Sigh. */
351 /* Process packets until we get one we can return or until we've
352 done a read and gotten nothing we can return... */
355 /* If the buffer is empty, fill it. */
356 if (interface
-> rbuf_offset
== interface
-> rbuf_len
) {
357 length
= read (interface
-> rfdesc
,
359 interface
-> rbuf_max
);
362 interface
-> rbuf_offset
= 0;
363 interface
-> rbuf_len
= length
;
366 /* If there isn't room for a whole bpf header, something went
367 wrong, but we'll ignore it and hope it goes away... XXX */
368 if (interface
-> rbuf_len
-
369 interface
-> rbuf_offset
< sizeof hdr
) {
370 interface
-> rbuf_offset
= interface
-> rbuf_len
;
374 /* Copy out a bpf header... */
375 memcpy (&hdr
, &interface
-> rbuf
[interface
-> rbuf_offset
],
378 /* If the bpf header plus data doesn't fit in what's left
379 of the buffer, stick head in sand yet again... */
380 if (interface
-> rbuf_offset
+
381 hdr
.bh_hdrlen
+ hdr
.bh_caplen
> interface
-> rbuf_len
) {
382 interface
-> rbuf_offset
= interface
-> rbuf_len
;
386 /* If the captured data wasn't the whole packet, or if
387 the packet won't fit in the input buffer, all we
388 can do is drop it. */
389 if (hdr
.bh_caplen
!= hdr
.bh_datalen
) {
390 interface
-> rbuf_offset
+=
391 hdr
.bh_hdrlen
= hdr
.bh_caplen
;
395 /* Skip over the BPF header... */
396 interface
-> rbuf_offset
+= hdr
.bh_hdrlen
;
398 /* Decode the physical header... */
399 offset
= decode_hw_header (interface
,
401 interface
-> rbuf_offset
,
404 /* If a physical layer checksum failed (dunno of any
405 physical layer that supports this, but WTH), skip this
408 interface
-> rbuf_offset
+= hdr
.bh_caplen
;
411 interface
-> rbuf_offset
+= offset
;
412 hdr
.bh_caplen
-= offset
;
414 /* Decode the IP and UDP headers... */
415 offset
= decode_udp_ip_header (interface
,
417 interface
-> rbuf_offset
,
422 /* If the IP or UDP checksum was bad, skip the packet... */
424 interface
-> rbuf_offset
+= hdr
.bh_caplen
;
427 interface
-> rbuf_offset
+= offset
;
428 hdr
.bh_caplen
-= offset
;
430 /* If there's not enough room to stash the packet data,
431 we have to skip it (this shouldn't happen in real
433 if (hdr
.bh_caplen
> len
) {
434 interface
-> rbuf_offset
+= hdr
.bh_caplen
;
438 /* Copy out the data in the packet... */
439 memcpy (buf
, interface
-> rbuf
+ interface
-> rbuf_offset
,
441 interface
-> rbuf_offset
+= hdr
.bh_caplen
;
442 return hdr
.bh_caplen
;
447 int can_unicast_without_arp (ip
)
448 struct interface_info
*ip
;
453 int can_receive_unicast_unconfigured (ip
)
454 struct interface_info
*ip
;
459 void maybe_setup_fallback ()
462 struct interface_info
*fbi
;
463 fbi
= setup_fallback ();
465 if_register_fallback (fbi
);
467 fbi
-> type
= dhcp_type_interface
;
468 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
470 fallback_discard
, 0, 0);
471 if (status
!= ISC_R_SUCCESS
)
472 log_fatal ("Can't register I/O handle for %s: %s",
473 fbi
-> name
, isc_result_totext (status
));