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";
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>
56 #include <netinet/in_systm.h>
57 #include "includes/netinet/ip.h"
58 #include "includes/netinet/udp.h"
59 #include "includes/netinet/if_ether.h"
62 /* Reinitializes the specified interface after an address change. This
63 is not required for packet-filter APIs. */
66 void if_reinitialize_send (info
)
67 struct interface_info
*info
;
72 #ifdef USE_BPF_RECEIVE
73 void if_reinitialize_receive (info
)
74 struct interface_info
*info
;
79 /* Called by get_interface_list for each interface that's discovered.
80 Opens a packet filter for each interface and adds it to the select
83 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
84 int if_register_bpf (info
)
85 struct interface_info
*info
;
91 /* Open a BPF device */
93 /* %Audit% 31 bytes max. %2004.06.17,Safe% */
94 sprintf(filename
, BPF_FORMAT
, b
);
95 sock
= open (filename
, O_RDWR
, 0);
101 log_fatal ("No bpf devices.%s%s%s",
102 " Please read the README",
103 " section for your operating",
105 log_fatal ("Can't find free bpf: %m");
112 /* Set the BPF device to point at this interface. */
113 if (ioctl (sock
, BIOCSETIF
, info
-> ifp
) < 0)
114 log_fatal ("Can't attach interface %s to bpf device %s: %m",
115 info
-> name
, filename
);
119 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
122 void if_register_send (info
)
123 struct interface_info
*info
;
125 /* If we're using the bpf API for sending and receiving,
126 we don't need to register this interface twice. */
127 #ifndef USE_BPF_RECEIVE
128 info
-> wfdesc
= if_register_bpf (info
, interface
);
130 info
-> wfdesc
= info
-> rfdesc
;
132 if (!quiet_interface_discovery
)
133 log_info ("Sending on BPF/%s/%s%s%s",
135 print_hw_addr (info
-> hw_address
.hbuf
[0],
136 info
-> hw_address
.hlen
- 1,
137 &info
-> hw_address
.hbuf
[1]),
138 (info
-> shared_network
? "/" : ""),
139 (info
-> shared_network
?
140 info
-> shared_network
-> name
: ""));
143 void if_deregister_send (info
)
144 struct interface_info
*info
;
146 /* If we're using the bpf API for sending and receiving,
147 we don't need to register this interface twice. */
148 #ifndef USE_BPF_RECEIVE
149 close (info
-> wfdesc
);
153 if (!quiet_interface_discovery
)
154 log_info ("Disabling output on BPF/%s/%s%s%s",
156 print_hw_addr (info
-> hw_address
.hbuf
[0],
157 info
-> hw_address
.hlen
- 1,
158 &info
-> hw_address
.hbuf
[1]),
159 (info
-> shared_network
? "/" : ""),
160 (info
-> shared_network
?
161 info
-> shared_network
-> name
: ""));
163 #endif /* USE_BPF_SEND */
165 #if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
166 /* Packet filter program...
167 XXX Changes to the filter program may require changes to the constant
168 offsets used in if_register_send to patch the BPF program! XXX */
170 struct bpf_insn dhcp_bpf_filter
[] = {
171 /* Make sure this is an IP packet... */
172 BPF_STMT (BPF_LD
+ BPF_H
+ BPF_ABS
, 12),
173 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, ETHERTYPE_IP
, 0, 8),
175 /* Make sure it's a UDP packet... */
176 BPF_STMT (BPF_LD
+ BPF_B
+ BPF_ABS
, 23),
177 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, IPPROTO_UDP
, 0, 6),
179 /* Make sure this isn't a fragment... */
180 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, 20),
181 BPF_JUMP(BPF_JMP
+ BPF_JSET
+ BPF_K
, 0x1fff, 4, 0),
183 /* Get the IP header length... */
184 BPF_STMT (BPF_LDX
+ BPF_B
+ BPF_MSH
, 14),
186 /* Make sure it's to the right port... */
187 BPF_STMT (BPF_LD
+ BPF_H
+ BPF_IND
, 16),
188 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, 67, 0, 1), /* patch */
190 /* If we passed all the tests, ask for the whole packet. */
191 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1),
193 /* Otherwise, drop it. */
194 BPF_STMT(BPF_RET
+BPF_K
, 0),
197 #if defined (DEC_FDDI)
198 struct bpf_insn
*bpf_fddi_filter
;
201 int dhcp_bpf_filter_len
= sizeof dhcp_bpf_filter
/ sizeof (struct bpf_insn
);
202 #if defined (HAVE_TR_SUPPORT)
203 struct bpf_insn dhcp_bpf_tr_filter
[] = {
204 /* accept all token ring packets due to variable length header */
205 /* if we want to get clever, insert the program here */
207 /* If we passed all the tests, ask for the whole packet. */
208 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1),
210 /* Otherwise, drop it. */
211 BPF_STMT(BPF_RET
+BPF_K
, 0),
214 int dhcp_bpf_tr_filter_len
= (sizeof dhcp_bpf_tr_filter
/
215 sizeof (struct bpf_insn
));
216 #endif /* HAVE_TR_SUPPORT */
217 #endif /* USE_LPF_RECEIVE || USE_BPF_RECEIVE */
219 #if defined (USE_BPF_RECEIVE)
220 void if_register_receive (info
)
221 struct interface_info
*info
;
224 struct bpf_version v
;
226 struct bpf_program p
;
230 #endif /* DEC_FDDI */
232 /* Open a BPF device and hang it on this interface... */
233 info
-> rfdesc
= if_register_bpf (info
);
235 /* Make sure the BPF version is in range... */
236 if (ioctl (info
-> rfdesc
, BIOCVERSION
, &v
) < 0)
237 log_fatal ("Can't get BPF version: %m");
239 if (v
.bv_major
!= BPF_MAJOR_VERSION
||
240 v
.bv_minor
< BPF_MINOR_VERSION
)
241 log_fatal ("BPF version mismatch - recompile DHCP!");
243 /* Set immediate mode so that reads return as soon as a packet
244 comes in, rather than waiting for the input buffer to fill with
246 if (ioctl (info
-> rfdesc
, BIOCIMMEDIATE
, &flag
) < 0)
247 log_fatal ("Can't set immediate mode on bpf device: %m");
249 #ifdef NEED_OSF_PFILT_HACKS
250 /* Allow the copyall flag to be set... */
251 if (ioctl(info
-> rfdesc
, EIOCALLOWCOPYALL
, &flag
) < 0)
252 log_fatal ("Can't set ALLOWCOPYALL: %m");
254 /* Clear all the packet filter mode bits first... */
256 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
257 log_fatal ("Can't clear pfilt bits: %m");
259 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
260 bits
= ENBATCH
| ENCOPYALL
| ENBPFHDR
;
261 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
262 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
264 /* Get the required BPF buffer length from the kernel. */
265 if (ioctl (info
-> rfdesc
, BIOCGBLEN
, &info
-> rbuf_max
) < 0)
266 log_fatal ("Can't get bpf buffer length: %m");
267 info
-> rbuf
= dmalloc (info
-> rbuf_max
, MDL
);
269 log_fatal ("Can't allocate %ld bytes for bpf input buffer.",
270 (long)(info
-> rbuf_max
));
271 info
-> rbuf_offset
= 0;
272 info
-> rbuf_len
= 0;
274 /* Set up the bpf filter program structure. */
275 p
.bf_len
= dhcp_bpf_filter_len
;
278 /* See if this is an FDDI interface, flag it for later. */
279 if (ioctl(info
-> rfdesc
, BIOCGDLT
, &link_layer
) >= 0 &&
280 link_layer
== DLT_FDDI
) {
281 if (!bpf_fddi_filter
) {
282 bpf_fddi_filter
= dmalloc (sizeof bpf_fddi_filter
,
284 if (!bpf_fddi_filter
)
285 log_fatal ("No memory for FDDI filter.");
286 memcpy (bpf_fddi_filter
,
287 dhcp_bpf_filter
, sizeof dhcp_bpf_filter
);
288 /* Patch the BPF program to account for the difference
289 in length between ethernet headers (14), FDDI and
290 802.2 headers (16 +8=24, +10).
291 XXX changes to filter program may require changes to
292 XXX the insn number(s) used below! */
293 bpf_fddi_filter
[0].k
+= 10;
294 bpf_fddi_filter
[2].k
+= 10;
295 bpf_fddi_filter
[4].k
+= 10;
296 bpf_fddi_filter
[6].k
+= 10;
297 bpf_fddi_filter
[7].k
+= 10;
299 p
.bf_insns
= bpf_fddi_filter
;
301 #endif /* DEC_FDDI */
302 p
.bf_insns
= dhcp_bpf_filter
;
304 /* Patch the server port into the BPF program...
305 XXX changes to filter program may require changes
306 to the insn number(s) used below! XXX */
307 dhcp_bpf_filter
[8].k
= ntohs (local_port
);
309 if (ioctl (info
-> rfdesc
, BIOCSETF
, &p
) < 0)
310 log_fatal ("Can't install packet filter program: %m");
311 if (!quiet_interface_discovery
)
312 log_info ("Listening on BPF/%s/%s%s%s",
314 print_hw_addr (info
-> hw_address
.hbuf
[0],
315 info
-> hw_address
.hlen
- 1,
316 &info
-> hw_address
.hbuf
[1]),
317 (info
-> shared_network
? "/" : ""),
318 (info
-> shared_network
?
319 info
-> shared_network
-> name
: ""));
322 void if_deregister_receive (info
)
323 struct interface_info
*info
;
325 close (info
-> rfdesc
);
328 if (!quiet_interface_discovery
)
329 log_info ("Disabling input on BPF/%s/%s%s%s",
331 print_hw_addr (info
-> hw_address
.hbuf
[0],
332 info
-> hw_address
.hlen
- 1,
333 &info
-> hw_address
.hbuf
[1]),
334 (info
-> shared_network
? "/" : ""),
335 (info
-> shared_network
?
336 info
-> shared_network
-> name
: ""));
338 #endif /* USE_BPF_RECEIVE */
341 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
342 struct interface_info
*interface
;
343 struct packet
*packet
;
344 struct dhcp_packet
*raw
;
347 struct sockaddr_in
*to
;
348 struct hardware
*hto
;
350 unsigned hbufp
= 0, ibufp
= 0;
353 struct iovec iov
[3];
357 if (!strcmp (interface
-> name
, "fallback"))
358 return send_fallback (interface
, packet
, raw
,
361 /* Assemble the headers... */
362 assemble_hw_header (interface
, (unsigned char *)hw
, &hbufp
, hto
);
363 assemble_udp_ip_header (interface
,
364 (unsigned char *)ip
, &ibufp
, from
.s_addr
,
365 to
-> sin_addr
.s_addr
, to
-> sin_port
,
366 (unsigned char *)raw
, len
);
369 iov
[0].iov_base
= ((char *)hw
);
370 iov
[0].iov_len
= hbufp
;
371 iov
[1].iov_base
= ((char *)ip
);
372 iov
[1].iov_len
= ibufp
;
373 iov
[2].iov_base
= (char *)raw
;
374 iov
[2].iov_len
= len
;
376 result
= writev(interface
-> wfdesc
, iov
, 3);
378 log_error ("send_packet: %m");
381 #endif /* USE_BPF_SEND */
383 #ifdef USE_BPF_RECEIVE
384 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
385 struct interface_info
*interface
;
388 struct sockaddr_in
*from
;
389 struct hardware
*hfrom
;
395 /* All this complexity is because BPF doesn't guarantee
396 that only one packet will be returned at a time. We're
397 getting what we deserve, though - this is a terrible abuse
398 of the BPF interface. Sigh. */
400 /* Process packets until we get one we can return or until we've
401 done a read and gotten nothing we can return... */
404 /* If the buffer is empty, fill it. */
405 if (interface
-> rbuf_offset
== interface
-> rbuf_len
) {
406 length
= read (interface
-> rfdesc
,
408 (size_t)interface
-> rbuf_max
);
411 if (errno
== ENXIO
) {
415 dhcp_interface_remove
416 ((omapi_object_t
*)interface
,
417 (omapi_object_t
*)0);
421 interface
-> rbuf_offset
= 0;
422 interface
-> rbuf_len
= BPF_WORDALIGN (length
);
425 /* If there isn't room for a whole bpf header, something went
426 wrong, but we'll ignore it and hope it goes away... XXX */
427 if (interface
-> rbuf_len
-
428 interface
-> rbuf_offset
< sizeof hdr
) {
429 interface
-> rbuf_offset
= interface
-> rbuf_len
;
433 /* Copy out a bpf header... */
434 memcpy (&hdr
, &interface
-> rbuf
[interface
-> rbuf_offset
],
437 /* If the bpf header plus data doesn't fit in what's left
438 of the buffer, stick head in sand yet again... */
439 if (interface
-> rbuf_offset
+
440 hdr
.bh_hdrlen
+ hdr
.bh_caplen
> interface
-> rbuf_len
) {
441 interface
-> rbuf_offset
= interface
-> rbuf_len
;
445 /* If the captured data wasn't the whole packet, or if
446 the packet won't fit in the input buffer, all we
447 can do is drop it. */
448 if (hdr
.bh_caplen
!= hdr
.bh_datalen
) {
449 interface
-> rbuf_offset
=
450 BPF_WORDALIGN (interface
-> rbuf_offset
+
451 hdr
.bh_hdrlen
+ hdr
.bh_caplen
);
455 /* Skip over the BPF header... */
456 interface
-> rbuf_offset
+= hdr
.bh_hdrlen
;
458 /* Decode the physical header... */
459 offset
= decode_hw_header (interface
,
461 interface
-> rbuf_offset
,
464 /* If a physical layer checksum failed (dunno of any
465 physical layer that supports this, but WTH), skip this
468 interface
-> rbuf_offset
=
469 BPF_WORDALIGN (interface
-> rbuf_offset
+
473 interface
-> rbuf_offset
+= offset
;
474 hdr
.bh_caplen
-= offset
;
476 /* Decode the IP and UDP headers... */
477 offset
= decode_udp_ip_header (interface
,
479 interface
-> rbuf_offset
,
483 /* If the IP or UDP checksum was bad, skip the packet... */
485 interface
-> rbuf_offset
=
486 BPF_WORDALIGN (interface
-> rbuf_offset
+
490 interface
-> rbuf_offset
= interface
-> rbuf_offset
+ offset
;
491 hdr
.bh_caplen
-= offset
;
493 /* If there's not enough room to stash the packet data,
494 we have to skip it (this shouldn't happen in real
496 if (hdr
.bh_caplen
> len
) {
497 interface
-> rbuf_offset
=
498 BPF_WORDALIGN (interface
-> rbuf_offset
+
503 /* Copy out the data in the packet... */
504 memcpy (buf
, interface
-> rbuf
+ interface
-> rbuf_offset
,
506 interface
-> rbuf_offset
=
507 BPF_WORDALIGN (interface
-> rbuf_offset
+
509 return hdr
.bh_caplen
;
514 int can_unicast_without_arp (ip
)
515 struct interface_info
*ip
;
520 int can_receive_unicast_unconfigured (ip
)
521 struct interface_info
*ip
;
526 int supports_multiple_interfaces (ip
)
527 struct interface_info
*ip
;
532 void maybe_setup_fallback ()
535 struct interface_info
*fbi
= (struct interface_info
*)0;
536 if (setup_fallback (&fbi
, MDL
)) {
537 if_register_fallback (fbi
);
538 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
540 fallback_discard
, 0, 0);
541 if (status
!= ISC_R_SUCCESS
)
542 log_fatal ("Can't register I/O handle for %s: %s",
543 fbi
-> name
, isc_result_totext (status
));
544 interface_dereference (&fbi
, MDL
);