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$ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
40 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
41 || defined (USE_LPF_RECEIVE)
42 # 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 /* This one's for BSD based OSes (dunno if it breaks any other OSes) */
49 # define USE_SOCKET_FALLBACK
51 # include <sys/ioctl.h>
53 # include "/usr/include/net/bpf.h"
54 # if defined (NEED_OSF_PFILT_HACKS)
55 # include </usr/include/net/pfilt.h>
59 #include <netinet/in_systm.h>
60 #include "includes/netinet/ip.h"
61 #include "includes/netinet/udp.h"
62 #include "includes/netinet/if_ether.h"
65 /* Reinitializes the specified interface after an address change. This
66 is not required for packet-filter APIs. */
69 void if_reinitialize_send (info
)
70 struct interface_info
*info
;
75 #ifdef USE_BPF_RECEIVE
76 void if_reinitialize_receive (info
)
77 struct interface_info
*info
;
82 /* Called by get_interface_list for each interface that's discovered.
83 Opens a packet filter for each interface and adds it to the select
86 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
87 int if_register_bpf (info
)
88 struct interface_info
*info
;
94 /* Open a BPF device */
96 /* %Audit% 31 bytes max. %2004.06.17,Safe% */
97 sprintf(filename
, BPF_FORMAT
, b
);
98 sock
= open (filename
, O_RDWR
, 0);
100 if (errno
== EBUSY
) {
104 log_fatal ("No bpf devices.%s%s%s",
105 " Please read the README",
106 " section for your operating",
108 log_fatal ("Can't find free bpf: %m");
115 /* Set the BPF device to point at this interface. */
116 if (ioctl (sock
, BIOCSETIF
, info
-> ifp
) < 0)
117 log_fatal ("Can't attach interface %s to bpf device %s: %m",
118 info
-> name
, filename
);
122 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
125 void if_register_send (info
)
126 struct interface_info
*info
;
128 /* If we're using the bpf API for sending and receiving,
129 we don't need to register this interface twice. */
130 #ifndef USE_BPF_RECEIVE
131 info
-> wfdesc
= if_register_bpf (info
, interface
);
133 info
-> wfdesc
= info
-> rfdesc
;
135 if (!quiet_interface_discovery
)
136 log_info ("Sending on BPF/%s/%s%s%s",
138 print_hw_addr (info
-> hw_address
.hbuf
[0],
139 info
-> hw_address
.hlen
- 1,
140 &info
-> hw_address
.hbuf
[1]),
141 (info
-> shared_network
? "/" : ""),
142 (info
-> shared_network
?
143 info
-> shared_network
-> name
: ""));
146 void if_deregister_send (info
)
147 struct interface_info
*info
;
149 /* If we're using the bpf API for sending and receiving,
150 we don't need to register this interface twice. */
151 #ifndef USE_BPF_RECEIVE
152 close (info
-> wfdesc
);
156 if (!quiet_interface_discovery
)
157 log_info ("Disabling output on BPF/%s/%s%s%s",
159 print_hw_addr (info
-> hw_address
.hbuf
[0],
160 info
-> hw_address
.hlen
- 1,
161 &info
-> hw_address
.hbuf
[1]),
162 (info
-> shared_network
? "/" : ""),
163 (info
-> shared_network
?
164 info
-> shared_network
-> name
: ""));
166 #endif /* USE_BPF_SEND */
168 #if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
169 /* Packet filter program...
170 XXX Changes to the filter program may require changes to the constant
171 offsets used in if_register_send to patch the BPF program! XXX */
173 struct bpf_insn dhcp_bpf_filter
[] = {
174 /* Make sure this is an IP packet... */
175 BPF_STMT (BPF_LD
+ BPF_H
+ BPF_ABS
, 12),
176 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, ETHERTYPE_IP
, 0, 8),
178 /* Make sure it's a UDP packet... */
179 BPF_STMT (BPF_LD
+ BPF_B
+ BPF_ABS
, 23),
180 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, IPPROTO_UDP
, 0, 6),
182 /* Make sure this isn't a fragment... */
183 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, 20),
184 BPF_JUMP(BPF_JMP
+ BPF_JSET
+ BPF_K
, 0x1fff, 4, 0),
186 /* Get the IP header length... */
187 BPF_STMT (BPF_LDX
+ BPF_B
+ BPF_MSH
, 14),
189 /* Make sure it's to the right port... */
190 BPF_STMT (BPF_LD
+ BPF_H
+ BPF_IND
, 16),
191 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, 67, 0, 1), /* patch */
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 #if defined (DEC_FDDI)
201 struct bpf_insn
*bpf_fddi_filter
;
204 int dhcp_bpf_filter_len
= sizeof dhcp_bpf_filter
/ sizeof (struct bpf_insn
);
205 #if defined (HAVE_TR_SUPPORT)
206 struct bpf_insn dhcp_bpf_tr_filter
[] = {
207 /* accept all token ring packets due to variable length header */
208 /* if we want to get clever, insert the program here */
210 /* If we passed all the tests, ask for the whole packet. */
211 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1),
213 /* Otherwise, drop it. */
214 BPF_STMT(BPF_RET
+BPF_K
, 0),
217 int dhcp_bpf_tr_filter_len
= (sizeof dhcp_bpf_tr_filter
/
218 sizeof (struct bpf_insn
));
219 #endif /* HAVE_TR_SUPPORT */
220 #endif /* USE_LPF_RECEIVE || USE_BPF_RECEIVE */
222 #if defined (USE_BPF_RECEIVE)
223 void if_register_receive (info
)
224 struct interface_info
*info
;
227 struct bpf_version v
;
229 struct bpf_program p
;
233 #endif /* DEC_FDDI */
235 /* Open a BPF device and hang it on this interface... */
236 info
-> rfdesc
= if_register_bpf (info
);
238 /* Make sure the BPF version is in range... */
239 if (ioctl (info
-> rfdesc
, BIOCVERSION
, &v
) < 0)
240 log_fatal ("Can't get BPF version: %m");
242 if (v
.bv_major
!= BPF_MAJOR_VERSION
||
243 v
.bv_minor
< BPF_MINOR_VERSION
)
244 log_fatal ("BPF version mismatch - recompile DHCP!");
246 /* Set immediate mode so that reads return as soon as a packet
247 comes in, rather than waiting for the input buffer to fill with
249 if (ioctl (info
-> rfdesc
, BIOCIMMEDIATE
, &flag
) < 0)
250 log_fatal ("Can't set immediate mode on bpf device: %m");
252 #ifdef NEED_OSF_PFILT_HACKS
253 /* Allow the copyall flag to be set... */
254 if (ioctl(info
-> rfdesc
, EIOCALLOWCOPYALL
, &flag
) < 0)
255 log_fatal ("Can't set ALLOWCOPYALL: %m");
257 /* Clear all the packet filter mode bits first... */
259 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
260 log_fatal ("Can't clear pfilt bits: %m");
262 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
263 bits
= ENBATCH
| ENCOPYALL
| ENBPFHDR
;
264 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
265 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
267 /* Get the required BPF buffer length from the kernel. */
268 if (ioctl (info
-> rfdesc
, BIOCGBLEN
, &info
-> rbuf_max
) < 0)
269 log_fatal ("Can't get bpf buffer length: %m");
270 info
-> rbuf
= dmalloc (info
-> rbuf_max
, MDL
);
272 log_fatal ("Can't allocate %ld bytes for bpf input buffer.",
273 (long)(info
-> rbuf_max
));
274 info
-> rbuf_offset
= 0;
275 info
-> rbuf_len
= 0;
277 /* Set up the bpf filter program structure. */
278 p
.bf_len
= dhcp_bpf_filter_len
;
281 /* See if this is an FDDI interface, flag it for later. */
282 if (ioctl(info
-> rfdesc
, BIOCGDLT
, &link_layer
) >= 0 &&
283 link_layer
== DLT_FDDI
) {
284 if (!bpf_fddi_filter
) {
285 bpf_fddi_filter
= dmalloc (sizeof bpf_fddi_filter
,
287 if (!bpf_fddi_filter
)
288 log_fatal ("No memory for FDDI filter.");
289 memcpy (bpf_fddi_filter
,
290 dhcp_bpf_filter
, sizeof dhcp_bpf_filter
);
291 /* Patch the BPF program to account for the difference
292 in length between ethernet headers (14), FDDI and
293 802.2 headers (16 +8=24, +10).
294 XXX changes to filter program may require changes to
295 XXX the insn number(s) used below! */
296 bpf_fddi_filter
[0].k
+= 10;
297 bpf_fddi_filter
[2].k
+= 10;
298 bpf_fddi_filter
[4].k
+= 10;
299 bpf_fddi_filter
[6].k
+= 10;
300 bpf_fddi_filter
[7].k
+= 10;
302 p
.bf_insns
= bpf_fddi_filter
;
304 #endif /* DEC_FDDI */
305 p
.bf_insns
= dhcp_bpf_filter
;
307 /* Patch the server port into the BPF program...
308 XXX changes to filter program may require changes
309 to the insn number(s) used below! XXX */
310 dhcp_bpf_filter
[8].k
= ntohs (local_port
);
312 if (ioctl (info
-> rfdesc
, BIOCSETF
, &p
) < 0)
313 log_fatal ("Can't install packet filter program: %m");
314 if (!quiet_interface_discovery
)
315 log_info ("Listening on BPF/%s/%s%s%s",
317 print_hw_addr (info
-> hw_address
.hbuf
[0],
318 info
-> hw_address
.hlen
- 1,
319 &info
-> hw_address
.hbuf
[1]),
320 (info
-> shared_network
? "/" : ""),
321 (info
-> shared_network
?
322 info
-> shared_network
-> name
: ""));
325 void if_deregister_receive (info
)
326 struct interface_info
*info
;
328 close (info
-> rfdesc
);
331 if (!quiet_interface_discovery
)
332 log_info ("Disabling input on BPF/%s/%s%s%s",
334 print_hw_addr (info
-> hw_address
.hbuf
[0],
335 info
-> hw_address
.hlen
- 1,
336 &info
-> hw_address
.hbuf
[1]),
337 (info
-> shared_network
? "/" : ""),
338 (info
-> shared_network
?
339 info
-> shared_network
-> name
: ""));
341 #endif /* USE_BPF_RECEIVE */
344 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
345 struct interface_info
*interface
;
346 struct packet
*packet
;
347 struct dhcp_packet
*raw
;
350 struct sockaddr_in
*to
;
351 struct hardware
*hto
;
353 unsigned hbufp
= 0, ibufp
= 0;
356 struct iovec iov
[3];
360 if (!strcmp (interface
-> name
, "fallback"))
361 return send_fallback (interface
, packet
, raw
,
364 /* Assemble the headers... */
365 assemble_hw_header (interface
, (unsigned char *)hw
, &hbufp
, hto
);
366 assemble_udp_ip_header (interface
,
367 (unsigned char *)ip
, &ibufp
, from
.s_addr
,
368 to
-> sin_addr
.s_addr
, to
-> sin_port
,
369 (unsigned char *)raw
, len
);
372 iov
[0].iov_base
= ((char *)hw
);
373 iov
[0].iov_len
= hbufp
;
374 iov
[1].iov_base
= ((char *)ip
);
375 iov
[1].iov_len
= ibufp
;
376 iov
[2].iov_base
= (char *)raw
;
377 iov
[2].iov_len
= len
;
379 result
= writev(interface
-> wfdesc
, iov
, 3);
381 log_error ("send_packet: %m");
384 #endif /* USE_BPF_SEND */
386 #ifdef USE_BPF_RECEIVE
387 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
388 struct interface_info
*interface
;
391 struct sockaddr_in
*from
;
392 struct hardware
*hfrom
;
398 /* All this complexity is because BPF doesn't guarantee
399 that only one packet will be returned at a time. We're
400 getting what we deserve, though - this is a terrible abuse
401 of the BPF interface. Sigh. */
403 /* Process packets until we get one we can return or until we've
404 done a read and gotten nothing we can return... */
407 /* If the buffer is empty, fill it. */
408 if (interface
-> rbuf_offset
== interface
-> rbuf_len
) {
409 length
= read (interface
-> rfdesc
,
411 (size_t)interface
-> rbuf_max
);
414 if (errno
== ENXIO
) {
418 dhcp_interface_remove
419 ((omapi_object_t
*)interface
,
420 (omapi_object_t
*)0);
424 interface
-> rbuf_offset
= 0;
425 interface
-> rbuf_len
= BPF_WORDALIGN (length
);
428 /* If there isn't room for a whole bpf header, something went
429 wrong, but we'll ignore it and hope it goes away... XXX */
430 if (interface
-> rbuf_len
-
431 interface
-> rbuf_offset
< sizeof hdr
) {
432 interface
-> rbuf_offset
= interface
-> rbuf_len
;
436 /* Copy out a bpf header... */
437 memcpy (&hdr
, &interface
-> rbuf
[interface
-> rbuf_offset
],
440 /* If the bpf header plus data doesn't fit in what's left
441 of the buffer, stick head in sand yet again... */
442 if (interface
-> rbuf_offset
+
443 hdr
.bh_hdrlen
+ hdr
.bh_caplen
> interface
-> rbuf_len
) {
444 interface
-> rbuf_offset
= interface
-> rbuf_len
;
448 /* If the captured data wasn't the whole packet, or if
449 the packet won't fit in the input buffer, all we
450 can do is drop it. */
451 if (hdr
.bh_caplen
!= hdr
.bh_datalen
) {
452 interface
-> rbuf_offset
=
453 BPF_WORDALIGN (interface
-> rbuf_offset
+
454 hdr
.bh_hdrlen
+ hdr
.bh_caplen
);
458 /* Skip over the BPF header... */
459 interface
-> rbuf_offset
+= hdr
.bh_hdrlen
;
461 /* Decode the physical header... */
462 offset
= decode_hw_header (interface
,
464 interface
-> rbuf_offset
,
467 /* If a physical layer checksum failed (dunno of any
468 physical layer that supports this, but WTH), skip this
471 interface
-> rbuf_offset
=
472 BPF_WORDALIGN (interface
-> rbuf_offset
+
476 interface
-> rbuf_offset
+= offset
;
477 hdr
.bh_caplen
-= offset
;
479 /* Decode the IP and UDP headers... */
480 offset
= decode_udp_ip_header (interface
,
482 interface
-> rbuf_offset
,
486 /* If the IP or UDP checksum was bad, skip the packet... */
488 interface
-> rbuf_offset
=
489 BPF_WORDALIGN (interface
-> rbuf_offset
+
493 interface
-> rbuf_offset
= interface
-> rbuf_offset
+ offset
;
494 hdr
.bh_caplen
-= offset
;
496 /* If there's not enough room to stash the packet data,
497 we have to skip it (this shouldn't happen in real
499 if (hdr
.bh_caplen
> len
) {
500 interface
-> rbuf_offset
=
501 BPF_WORDALIGN (interface
-> rbuf_offset
+
506 /* Copy out the data in the packet... */
507 memcpy (buf
, interface
-> rbuf
+ interface
-> rbuf_offset
,
509 interface
-> rbuf_offset
=
510 BPF_WORDALIGN (interface
-> rbuf_offset
+
512 return hdr
.bh_caplen
;
517 int can_unicast_without_arp (ip
)
518 struct interface_info
*ip
;
523 int can_receive_unicast_unconfigured (ip
)
524 struct interface_info
*ip
;
529 int supports_multiple_interfaces (ip
)
530 struct interface_info
*ip
;
535 void maybe_setup_fallback ()
538 struct interface_info
*fbi
= (struct interface_info
*)0;
539 if (setup_fallback (&fbi
, MDL
)) {
540 if_register_fallback (fbi
);
541 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
543 fallback_discard
, 0, 0);
544 if (status
!= ISC_R_SUCCESS
)
545 log_fatal ("Can't register I/O handle for %s: %s",
546 fbi
-> name
, isc_result_totext (status
));
547 interface_dereference (&fbi
, MDL
);