3 BPF socket interface code, originally contributed by Archie Cobbs. */
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1996-2003 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
23 * Redwood City, CA 94063
27 * This software was contributed to Internet Systems Consortium
30 * Patches for FDDI support on Digital Unix were written by Bill
31 * Stapleton, and maintained for a while by Mike Meredith before he
32 * managed to get me to integrate them.
36 static char copyright
[] =
37 "$Id: bpf.c,v 1.10 2005/08/11 17:13:21 drochner Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
41 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
42 || defined (USE_LPF_RECEIVE)
43 # if defined (USE_LPF_RECEIVE)
44 # include <asm/types.h>
45 # include <linux/filter.h>
46 # define bpf_insn sock_filter /* Linux: dare to be gratuitously different. */
48 # include <sys/ioctl.h>
51 # if defined (NEED_OSF_PFILT_HACKS)
52 # include <net/pfilt.h>
57 #include <netinet/in_systm.h>
58 #include "includes/netinet/ip.h"
59 #include "includes/netinet/udp.h"
60 #include "includes/netinet/if_ether.h"
63 /* Reinitializes the specified interface after an address change. This
64 is not required for packet-filter APIs. */
67 void if_reinitialize_send (info
)
68 struct interface_info
*info
;
73 #ifdef USE_BPF_RECEIVE
74 void if_reinitialize_receive (info
)
75 struct interface_info
*info
;
80 /* Called by get_interface_list for each interface that's discovered.
81 Opens a packet filter for each interface and adds it to the select
84 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
85 int if_register_bpf (info
)
86 struct interface_info
*info
;
91 /* Open a BPF device */
93 const char *filename
= _PATH_BPF
;
94 sock
= open (filename
, O_RDWR
, 0);
96 log_fatal ("No bpf devices.%s%s%s",
97 " Please read the README",
98 " section for your operating",
104 for (b
= 0; 1; b
++) {
105 /* %Audit% 31 bytes max. %2004.06.17,Safe% */
106 sprintf(filename
, BPF_FORMAT
, b
);
107 sock
= open (filename
, O_RDWR
, 0);
109 if (errno
== EBUSY
) {
113 log_fatal ("No bpf devices.%s%s%s",
114 " Please read the README",
115 " section for your operating",
117 log_fatal ("Can't find free bpf: %m");
125 /* Set the BPF buffer size to 32k */
127 if (ioctl (sock
, BIOCSBLEN
, &bufsize
) < 0)
128 log_error ("Can't set bpf buffer to %d: %m", bufsize
);
130 /* Set the BPF device to point at this interface. */
131 if (ioctl (sock
, BIOCSETIF
, info
-> ifp
) < 0)
132 log_fatal ("Can't attach interface %s to bpf device %s: %m",
133 info
-> name
, filename
);
137 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
140 void if_register_send (info
)
141 struct interface_info
*info
;
143 /* If we're using the bpf API for sending and receiving,
144 we don't need to register this interface twice. */
145 #ifndef USE_BPF_RECEIVE
146 info
-> wfdesc
= if_register_bpf (info
, interface
);
148 info
-> wfdesc
= info
-> rfdesc
;
150 if (!quiet_interface_discovery
)
151 log_info ("Sending on BPF/%s/%s%s%s",
153 print_hw_addr (info
-> hw_address
.hbuf
[0],
154 info
-> hw_address
.hlen
- 1,
155 &info
-> hw_address
.hbuf
[1]),
156 (info
-> shared_network
? "/" : ""),
157 (info
-> shared_network
?
158 info
-> shared_network
-> name
: ""));
161 void if_deregister_send (info
)
162 struct interface_info
*info
;
164 /* If we're using the bpf API for sending and receiving,
165 we don't need to register this interface twice. */
166 #ifndef USE_BPF_RECEIVE
167 close (info
-> wfdesc
);
171 if (!quiet_interface_discovery
)
172 log_info ("Disabling output on BPF/%s/%s%s%s",
174 print_hw_addr (info
-> hw_address
.hbuf
[0],
175 info
-> hw_address
.hlen
- 1,
176 &info
-> hw_address
.hbuf
[1]),
177 (info
-> shared_network
? "/" : ""),
178 (info
-> shared_network
?
179 info
-> shared_network
-> name
: ""));
181 #endif /* USE_BPF_SEND */
183 #if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
184 /* Packet filter program...
185 XXX Changes to the filter program may require changes to the constant
186 offsets used in if_register_send to patch the BPF program! XXX */
188 struct bpf_insn dhcp_bpf_filter
[] = {
189 /* Make sure this is an IP packet... */
190 BPF_STMT (BPF_LD
+ BPF_H
+ BPF_ABS
, 12),
191 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, ETHERTYPE_IP
, 0, 8),
193 /* Make sure it's a UDP packet... */
194 BPF_STMT (BPF_LD
+ BPF_B
+ BPF_ABS
, 23),
195 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, IPPROTO_UDP
, 0, 6),
197 /* Make sure this isn't a fragment... */
198 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, 20),
199 BPF_JUMP(BPF_JMP
+ BPF_JSET
+ BPF_K
, 0x1fff, 4, 0),
201 /* Get the IP header length... */
202 BPF_STMT (BPF_LDX
+ BPF_B
+ BPF_MSH
, 14),
204 /* Make sure it's to the right port... */
205 BPF_STMT (BPF_LD
+ BPF_H
+ BPF_IND
, 16),
206 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, 67, 0, 1), /* patch */
208 /* If we passed all the tests, ask for the whole packet. */
209 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1),
211 /* Otherwise, drop it. */
212 BPF_STMT(BPF_RET
+BPF_K
, 0),
215 #if defined(DEC_FDDI) || defined(NETBSD_FDDI)
216 struct bpf_insn
*bpf_fddi_filter
;
219 int dhcp_bpf_filter_len
= sizeof dhcp_bpf_filter
/ sizeof (struct bpf_insn
);
220 #if defined (HAVE_TR_SUPPORT)
221 struct bpf_insn dhcp_bpf_tr_filter
[] = {
222 /* accept all token ring packets due to variable length header */
223 /* if we want to get clever, insert the program here */
225 /* If we passed all the tests, ask for the whole packet. */
226 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1),
228 /* Otherwise, drop it. */
229 BPF_STMT(BPF_RET
+BPF_K
, 0),
232 int dhcp_bpf_tr_filter_len
= (sizeof dhcp_bpf_tr_filter
/
233 sizeof (struct bpf_insn
));
234 #endif /* HAVE_TR_SUPPORT */
235 #endif /* USE_LPF_RECEIVE || USE_BPF_RECEIVE */
237 #if defined (USE_BPF_RECEIVE)
238 void if_register_receive (info
)
239 struct interface_info
*info
;
242 struct bpf_version v
;
243 struct bpf_program p
;
244 #ifdef NEED_OSF_PFILT_HACKS
248 #if defined(DEC_FDDI) || defined(NETBSD_FDDI)
250 #endif /* DEC_FDDI || NETBSD_FDDI */
252 /* Open a BPF device and hang it on this interface... */
253 info
-> rfdesc
= if_register_bpf (info
);
255 /* Make sure the BPF version is in range... */
256 if (ioctl (info
-> rfdesc
, BIOCVERSION
, &v
) < 0)
257 log_fatal ("Can't get BPF version: %m");
259 if (v
.bv_major
!= BPF_MAJOR_VERSION
||
260 v
.bv_minor
< BPF_MINOR_VERSION
)
261 log_fatal ("BPF version mismatch - recompile DHCP!");
263 /* Set immediate mode so that reads return as soon as a packet
264 comes in, rather than waiting for the input buffer to fill with
266 if (ioctl (info
-> rfdesc
, BIOCIMMEDIATE
, &flag
) < 0)
267 log_fatal ("Can't set immediate mode on bpf device: %m");
269 #ifdef NEED_OSF_PFILT_HACKS
270 /* Allow the copyall flag to be set... */
271 if (ioctl(info
-> rfdesc
, EIOCALLOWCOPYALL
, &flag
) < 0)
272 log_fatal ("Can't set ALLOWCOPYALL: %m");
274 /* Clear all the packet filter mode bits first... */
276 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
277 log_fatal ("Can't clear pfilt bits: %m");
279 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
280 bits
= ENBATCH
| ENCOPYALL
| ENBPFHDR
;
281 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
282 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
284 /* Get the required BPF buffer length from the kernel. */
285 if (ioctl (info
-> rfdesc
, BIOCGBLEN
, &len
) < 0)
286 log_fatal ("Can't get bpf buffer length: %m");
287 info
-> rbuf_max
= len
;
288 info
-> rbuf
= dmalloc (info
-> rbuf_max
, MDL
);
290 log_fatal ("Can't allocate %ld bytes for bpf input buffer.",
291 (long)(info
-> rbuf_max
));
292 info
-> rbuf_offset
= 0;
293 info
-> rbuf_len
= 0;
295 /* Set up the bpf filter program structure. */
296 p
.bf_len
= dhcp_bpf_filter_len
;
298 #if defined(DEC_FDDI) || defined(NETBSD_FDDI)
299 /* See if this is an FDDI interface, flag it for later. */
300 if (ioctl(info
-> rfdesc
, BIOCGDLT
, &link_layer
) >= 0 &&
301 link_layer
== DLT_FDDI
) {
302 if (!bpf_fddi_filter
) {
303 bpf_fddi_filter
= dmalloc (sizeof bpf_fddi_filter
,
305 if (!bpf_fddi_filter
)
306 log_fatal ("No memory for FDDI filter.");
307 memcpy (bpf_fddi_filter
,
308 dhcp_bpf_filter
, sizeof dhcp_bpf_filter
);
309 /* Patch the BPF program to account for the difference
310 in length between ethernet headers (14), FDDI and
311 802.2 headers (16 +8=24, +10).
312 XXX changes to filter program may require changes to
313 XXX the insn number(s) used below! */
314 bpf_fddi_filter
[0].k
+= 10;
315 bpf_fddi_filter
[2].k
+= 10;
316 bpf_fddi_filter
[4].k
+= 10;
317 bpf_fddi_filter
[6].k
+= 10;
318 bpf_fddi_filter
[7].k
+= 10;
320 p
.bf_insns
= bpf_fddi_filter
;
322 #endif /* DEC_FDDI || NETBSD_FDDI */
323 p
.bf_insns
= dhcp_bpf_filter
;
325 /* Patch the server port into the BPF program...
326 XXX changes to filter program may require changes
327 to the insn number(s) used below! XXX */
328 dhcp_bpf_filter
[8].k
= ntohs (local_port
);
330 if (ioctl (info
-> rfdesc
, BIOCSETF
, &p
) < 0)
331 log_fatal ("Can't install packet filter program: %m");
332 if (!quiet_interface_discovery
)
333 log_info ("Listening on BPF/%s/%s%s%s",
335 print_hw_addr (info
-> hw_address
.hbuf
[0],
336 info
-> hw_address
.hlen
- 1,
337 &info
-> hw_address
.hbuf
[1]),
338 (info
-> shared_network
? "/" : ""),
339 (info
-> shared_network
?
340 info
-> shared_network
-> name
: ""));
343 void if_deregister_receive (info
)
344 struct interface_info
*info
;
346 close (info
-> rfdesc
);
349 if (!quiet_interface_discovery
)
350 log_info ("Disabling input on BPF/%s/%s%s%s",
352 print_hw_addr (info
-> hw_address
.hbuf
[0],
353 info
-> hw_address
.hlen
- 1,
354 &info
-> hw_address
.hbuf
[1]),
355 (info
-> shared_network
? "/" : ""),
356 (info
-> shared_network
?
357 info
-> shared_network
-> name
: ""));
359 #endif /* USE_BPF_RECEIVE */
362 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
363 struct interface_info
*interface
;
364 struct packet
*packet
;
365 struct dhcp_packet
*raw
;
368 struct sockaddr_in
*to
;
369 struct hardware
*hto
;
371 unsigned hbufp
= 0, ibufp
= 0;
374 struct iovec iov
[3];
377 if (!strcmp (interface
-> name
, "fallback"))
378 return send_fallback (interface
, packet
, raw
,
381 /* Assemble the headers... */
382 assemble_hw_header (interface
, (unsigned char *)hw
, &hbufp
, hto
);
383 assemble_udp_ip_header (interface
,
384 (unsigned char *)ip
, &ibufp
, from
.s_addr
,
385 to
-> sin_addr
.s_addr
, to
-> sin_port
,
386 (unsigned char *)raw
, len
);
389 iov
[0].iov_base
= ((char *)hw
);
390 iov
[0].iov_len
= hbufp
;
391 iov
[1].iov_base
= ((char *)ip
);
392 iov
[1].iov_len
= ibufp
;
393 iov
[2].iov_base
= (char *)raw
;
394 iov
[2].iov_len
= len
;
396 result
= writev(interface
-> wfdesc
, iov
, 3);
398 log_error ("send_packet: %m");
401 #endif /* USE_BPF_SEND */
403 #ifdef USE_BPF_RECEIVE
404 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
405 struct interface_info
*interface
;
408 struct sockaddr_in
*from
;
409 struct hardware
*hfrom
;
416 /* All this complexity is because BPF doesn't guarantee
417 that only one packet will be returned at a time. We're
418 getting what we deserve, though - this is a terrible abuse
419 of the BPF interface. Sigh. */
421 /* Process packets until we get one we can return or until we've
422 done a read and gotten nothing we can return... */
425 /* If the buffer is empty, fill it. */
426 if (interface
-> rbuf_offset
== interface
-> rbuf_len
) {
427 length
= read (interface
-> rfdesc
,
429 (size_t)interface
-> rbuf_max
);
432 if (errno
== ENXIO
) {
436 dhcp_interface_remove
437 ((omapi_object_t
*)interface
,
438 (omapi_object_t
*)0);
442 interface
-> rbuf_offset
= 0;
443 interface
-> rbuf_len
= BPF_WORDALIGN (length
);
446 /* If there isn't room for a whole bpf header, something went
447 wrong, but we'll ignore it and hope it goes away... XXX */
448 if (interface
-> rbuf_len
-
449 interface
-> rbuf_offset
< sizeof hdr
) {
450 interface
-> rbuf_offset
= interface
-> rbuf_len
;
454 /* Adjust for any padding BPF inserted between the packets. */
455 interface
-> rbuf_offset
=
456 BPF_WORDALIGN (interface
-> rbuf_offset
);
458 /* Copy out a bpf header... */
459 memcpy (&hdr
, &interface
-> rbuf
[interface
-> rbuf_offset
],
462 /* If the bpf header plus data doesn't fit in what's left
463 of the buffer, stick head in sand yet again... */
464 if (interface
-> rbuf_offset
+
465 hdr
.bh_hdrlen
+ hdr
.bh_caplen
> interface
-> rbuf_len
) {
466 interface
-> rbuf_offset
= interface
-> rbuf_len
;
470 /* If the captured data wasn't the whole packet, or if
471 the packet won't fit in the input buffer, all we
472 can do is drop it. */
473 if (hdr
.bh_caplen
!= hdr
.bh_datalen
) {
474 interface
-> rbuf_offset
=
475 BPF_WORDALIGN (interface
-> rbuf_offset
+
476 hdr
.bh_hdrlen
+ hdr
.bh_caplen
);
480 /* Skip over the BPF header... */
481 interface
-> rbuf_offset
+= hdr
.bh_hdrlen
;
483 /* Decode the physical header... */
484 offset
= decode_hw_header (interface
,
486 interface
-> rbuf_offset
,
489 /* If a physical layer checksum failed (dunno of any
490 physical layer that supports this, but WTH), skip this
493 interface
-> rbuf_offset
=
494 BPF_WORDALIGN (interface
-> rbuf_offset
+
498 interface
-> rbuf_offset
+= offset
;
499 hdr
.bh_caplen
-= offset
;
501 /* Decode the IP and UDP headers... */
502 offset
= decode_udp_ip_header (interface
,
504 interface
-> rbuf_offset
,
506 hdr
.bh_caplen
, &paylen
);
508 /* If the IP or UDP checksum was bad, skip the packet... */
510 interface
-> rbuf_offset
=
511 BPF_WORDALIGN (interface
-> rbuf_offset
+
515 interface
-> rbuf_offset
= interface
-> rbuf_offset
+ offset
;
516 hdr
.bh_caplen
-= offset
;
518 /* If there's not enough room to stash the packet data,
519 we have to skip it (this shouldn't happen in real
521 if (hdr
.bh_caplen
> len
) {
522 interface
-> rbuf_offset
=
523 BPF_WORDALIGN (interface
-> rbuf_offset
+
528 /* Copy out the data in the packet... */
529 memcpy (buf
, interface
-> rbuf
+ interface
-> rbuf_offset
,
531 interface
-> rbuf_offset
=
532 BPF_WORDALIGN (interface
-> rbuf_offset
+
539 int can_unicast_without_arp (ip
)
540 struct interface_info
*ip
;
545 int can_receive_unicast_unconfigured (ip
)
546 struct interface_info
*ip
;
551 int supports_multiple_interfaces (ip
)
552 struct interface_info
*ip
;
557 void maybe_setup_fallback ()
560 struct interface_info
*fbi
= (struct interface_info
*)0;
561 if (setup_fallback (&fbi
, MDL
)) {
562 if_register_fallback (fbi
);
563 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
565 fallback_discard
, 0, 0);
566 if (status
!= ISC_R_SUCCESS
)
567 log_fatal ("Can't register I/O handle for %s: %s",
568 fbi
-> name
, isc_result_totext (status
));
569 interface_dereference (&fbi
, MDL
);