3 BPF socket interface code, originally contributed by Archie Cobbs. */
6 * Copyright (c) 2009,2012-2014,2016 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
8 * Copyright (c) 1996-2003 by Internet Software Consortium
10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Internet Systems Consortium, Inc.
24 * Redwood City, CA 94063
26 * https://www.isc.org/
28 * This software was contributed to Internet Systems Consortium
31 * Patches for FDDI support on Digital Unix were written by Bill
32 * Stapleton, and maintained for a while by Mike Meredith before he
33 * managed to get me to integrate them.
37 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
38 || defined (USE_LPF_RECEIVE)
39 # if defined (USE_LPF_RECEIVE)
40 # include <asm/types.h>
41 # include <linux/filter.h>
42 # define bpf_insn sock_filter /* Linux: dare to be gratuitously different. */
44 # include <sys/ioctl.h>
47 # if defined (NEED_OSF_PFILT_HACKS)
48 # include <net/pfilt.h>
52 #include <netinet/in_systm.h>
53 #include "includes/netinet/ip.h"
54 #include "includes/netinet/udp.h"
55 #include "includes/netinet/if_ether.h"
58 #if defined(USE_BPF_SEND) || defined(USE_BPF_RECEIVE) || defined(USE_BPF_HWADDR)
59 #include <net/if_types.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
);
120 get_hw_addr(info
->name
, &info
->hw_address
);
124 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
127 void if_register_send (info
)
128 struct interface_info
*info
;
130 /* If we're using the bpf API for sending and receiving,
131 we don't need to register this interface twice. */
132 #ifndef USE_BPF_RECEIVE
133 info
-> wfdesc
= if_register_bpf (info
, interface
);
135 info
-> wfdesc
= info
-> rfdesc
;
137 if (!quiet_interface_discovery
)
138 log_info ("Sending on BPF/%s/%s%s%s",
140 print_hw_addr (info
-> hw_address
.hbuf
[0],
141 info
-> hw_address
.hlen
- 1,
142 &info
-> hw_address
.hbuf
[1]),
143 (info
-> shared_network
? "/" : ""),
144 (info
-> shared_network
?
145 info
-> shared_network
-> name
: ""));
148 void if_deregister_send (info
)
149 struct interface_info
*info
;
151 /* If we're using the bpf API for sending and receiving,
152 we don't need to register this interface twice. */
153 #ifndef USE_BPF_RECEIVE
154 close (info
-> wfdesc
);
158 if (!quiet_interface_discovery
)
159 log_info ("Disabling output on BPF/%s/%s%s%s",
161 print_hw_addr (info
-> hw_address
.hbuf
[0],
162 info
-> hw_address
.hlen
- 1,
163 &info
-> hw_address
.hbuf
[1]),
164 (info
-> shared_network
? "/" : ""),
165 (info
-> shared_network
?
166 info
-> shared_network
-> name
: ""));
168 #endif /* USE_BPF_SEND */
170 #if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
171 /* Packet filter program...
172 XXX Changes to the filter program may require changes to the constant
173 offsets used in if_register_send to patch the BPF program! XXX */
175 struct bpf_insn dhcp_bpf_filter
[] = {
176 /* Make sure this is an IP packet... */
177 BPF_STMT (BPF_LD
+ BPF_H
+ BPF_ABS
, 12),
178 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, ETHERTYPE_IP
, 0, 8),
180 /* Make sure it's a UDP packet... */
181 BPF_STMT (BPF_LD
+ BPF_B
+ BPF_ABS
, 23),
182 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, IPPROTO_UDP
, 0, 6),
184 /* Make sure this isn't a fragment... */
185 BPF_STMT(BPF_LD
+ BPF_H
+ BPF_ABS
, 20),
186 BPF_JUMP(BPF_JMP
+ BPF_JSET
+ BPF_K
, 0x1fff, 4, 0),
188 /* Get the IP header length... */
189 BPF_STMT (BPF_LDX
+ BPF_B
+ BPF_MSH
, 14),
191 /* Make sure it's to the right port... */
192 BPF_STMT (BPF_LD
+ BPF_H
+ BPF_IND
, 16),
193 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, 67, 0, 1), /* patch */
195 /* If we passed all the tests, ask for the whole packet. */
196 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1),
198 /* Otherwise, drop it. */
199 BPF_STMT(BPF_RET
+BPF_K
, 0),
202 #if defined (DEC_FDDI)
203 struct bpf_insn
*bpf_fddi_filter
= NULL
;
206 int dhcp_bpf_filter_len
= sizeof dhcp_bpf_filter
/ sizeof (struct bpf_insn
);
207 #if defined (HAVE_TR_SUPPORT)
208 struct bpf_insn dhcp_bpf_tr_filter
[] = {
209 /* accept all token ring packets due to variable length header */
210 /* if we want to get clever, insert the program here */
212 /* If we passed all the tests, ask for the whole packet. */
213 BPF_STMT(BPF_RET
+BPF_K
, (u_int
)-1),
215 /* Otherwise, drop it. */
216 BPF_STMT(BPF_RET
+BPF_K
, 0),
219 int dhcp_bpf_tr_filter_len
= (sizeof dhcp_bpf_tr_filter
/
220 sizeof (struct bpf_insn
));
221 #endif /* HAVE_TR_SUPPORT */
222 #endif /* USE_LPF_RECEIVE || USE_BPF_RECEIVE */
224 #if defined (USE_BPF_RECEIVE)
225 void if_register_receive (info
)
226 struct interface_info
*info
;
229 struct bpf_version v
;
230 struct bpf_program p
;
231 #ifdef NEED_OSF_PFILT_HACKS
236 #endif /* DEC_FDDI */
238 /* Open a BPF device and hang it on this interface... */
239 info
-> rfdesc
= if_register_bpf (info
);
241 /* Make sure the BPF version is in range... */
242 if (ioctl (info
-> rfdesc
, BIOCVERSION
, &v
) < 0)
243 log_fatal ("Can't get BPF version: %m");
245 if (v
.bv_major
!= BPF_MAJOR_VERSION
||
246 v
.bv_minor
< BPF_MINOR_VERSION
)
247 log_fatal ("BPF version mismatch - recompile DHCP!");
249 /* Set immediate mode so that reads return as soon as a packet
250 comes in, rather than waiting for the input buffer to fill with
252 if (ioctl (info
-> rfdesc
, BIOCIMMEDIATE
, &flag
) < 0)
253 log_fatal ("Can't set immediate mode on bpf device: %m");
255 #ifdef NEED_OSF_PFILT_HACKS
256 /* Allow the copyall flag to be set... */
257 if (ioctl(info
-> rfdesc
, EIOCALLOWCOPYALL
, &flag
) < 0)
258 log_fatal ("Can't set ALLOWCOPYALL: %m");
260 /* Clear all the packet filter mode bits first... */
262 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
263 log_fatal ("Can't clear pfilt bits: %m");
265 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
266 bits
= ENBATCH
| ENCOPYALL
| ENBPFHDR
;
267 if (ioctl (info
-> rfdesc
, EIOCMBIS
, &bits
) < 0)
268 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
270 /* Get the required BPF buffer length from the kernel. */
271 if (ioctl (info
-> rfdesc
, BIOCGBLEN
, &info
-> rbuf_max
) < 0)
272 log_fatal ("Can't get bpf buffer length: %m");
273 info
-> rbuf
= dmalloc (info
-> rbuf_max
, MDL
);
275 log_fatal ("Can't allocate %ld bytes for bpf input buffer.",
276 (long)(info
-> rbuf_max
));
277 info
-> rbuf_offset
= 0;
278 info
-> rbuf_len
= 0;
280 /* Set up the bpf filter program structure. */
281 p
.bf_len
= dhcp_bpf_filter_len
;
284 /* See if this is an FDDI interface, flag it for later. */
285 if (ioctl(info
-> rfdesc
, BIOCGDLT
, &link_layer
) >= 0 &&
286 link_layer
== DLT_FDDI
) {
287 if (!bpf_fddi_filter
) {
288 bpf_fddi_filter
= dmalloc (sizeof dhcp_bpf_filter
,
290 if (!bpf_fddi_filter
)
291 log_fatal ("No memory for FDDI filter.");
292 memcpy (bpf_fddi_filter
,
293 dhcp_bpf_filter
, sizeof dhcp_bpf_filter
);
294 /* Patch the BPF program to account for the difference
295 in length between ethernet headers (14), FDDI and
296 802.2 headers (16 +8=24, +10).
297 XXX changes to filter program may require changes to
298 XXX the insn number(s) used below! */
299 bpf_fddi_filter
[0].k
+= 10;
300 bpf_fddi_filter
[2].k
+= 10;
301 bpf_fddi_filter
[4].k
+= 10;
302 bpf_fddi_filter
[6].k
+= 10;
303 bpf_fddi_filter
[7].k
+= 10;
305 p
.bf_insns
= bpf_fddi_filter
;
307 #endif /* DEC_FDDI */
308 p
.bf_insns
= dhcp_bpf_filter
;
310 /* Patch the server port into the BPF program...
311 XXX changes to filter program may require changes
312 to the insn number(s) used below! XXX */
313 dhcp_bpf_filter
[8].k
= ntohs (local_port
);
315 if (ioctl (info
-> rfdesc
, BIOCSETF
, &p
) < 0)
316 log_fatal ("Can't install packet filter program: %m");
317 if (!quiet_interface_discovery
)
318 log_info ("Listening on BPF/%s/%s%s%s",
320 print_hw_addr (info
-> hw_address
.hbuf
[0],
321 info
-> hw_address
.hlen
- 1,
322 &info
-> hw_address
.hbuf
[1]),
323 (info
-> shared_network
? "/" : ""),
324 (info
-> shared_network
?
325 info
-> shared_network
-> name
: ""));
328 void if_deregister_receive (info
)
329 struct interface_info
*info
;
331 close (info
-> rfdesc
);
334 if (!quiet_interface_discovery
)
335 log_info ("Disabling input on BPF/%s/%s%s%s",
337 print_hw_addr (info
-> hw_address
.hbuf
[0],
338 info
-> hw_address
.hlen
- 1,
339 &info
-> hw_address
.hbuf
[1]),
340 (info
-> shared_network
? "/" : ""),
341 (info
-> shared_network
?
342 info
-> shared_network
-> name
: ""));
344 #endif /* USE_BPF_RECEIVE */
347 ssize_t
send_packet (interface
, packet
, raw
, len
, from
, to
, hto
)
348 struct interface_info
*interface
;
349 struct packet
*packet
;
350 struct dhcp_packet
*raw
;
353 struct sockaddr_in
*to
;
354 struct hardware
*hto
;
356 unsigned hbufp
= 0, ibufp
= 0;
359 struct iovec iov
[3];
362 if (!strcmp (interface
-> name
, "fallback"))
363 return send_fallback (interface
, packet
, raw
,
366 if (hto
== NULL
&& interface
->anycast_mac_addr
.hlen
)
367 hto
= &interface
->anycast_mac_addr
;
369 /* Assemble the headers... */
370 assemble_hw_header (interface
, (unsigned char *)hw
, &hbufp
, hto
);
371 assemble_udp_ip_header (interface
,
372 (unsigned char *)ip
, &ibufp
, from
.s_addr
,
373 to
-> sin_addr
.s_addr
, to
-> sin_port
,
374 (unsigned char *)raw
, len
);
377 iov
[0].iov_base
= ((char *)hw
);
378 iov
[0].iov_len
= hbufp
;
379 iov
[1].iov_base
= ((char *)ip
);
380 iov
[1].iov_len
= ibufp
;
381 iov
[2].iov_base
= (char *)raw
;
382 iov
[2].iov_len
= len
;
384 result
= writev(interface
-> wfdesc
, iov
, 3);
386 log_error ("send_packet: %m");
389 #endif /* USE_BPF_SEND */
391 #ifdef USE_BPF_RECEIVE
392 ssize_t
receive_packet (interface
, buf
, len
, from
, hfrom
)
393 struct interface_info
*interface
;
396 struct sockaddr_in
*from
;
397 struct hardware
*hfrom
;
404 /* All this complexity is because BPF doesn't guarantee
405 that only one packet will be returned at a time. We're
406 getting what we deserve, though - this is a terrible abuse
407 of the BPF interface. Sigh. */
409 /* Process packets until we get one we can return or until we've
410 done a read and gotten nothing we can return... */
412 /* If the buffer is empty, fill it. */
413 if (interface
->rbuf_offset
>= interface
->rbuf_len
) {
414 length
= read(interface
->rfdesc
, interface
->rbuf
,
415 (size_t)interface
->rbuf_max
);
418 if (errno
== ENXIO
) {
422 dhcp_interface_remove
423 ((omapi_object_t
*)interface
, NULL
);
427 interface
->rbuf_offset
= 0;
428 interface
->rbuf_len
= BPF_WORDALIGN(length
);
432 /* If there isn't room for a whole bpf header, something went
433 wrong, but we'll ignore it and hope it goes away... XXX */
434 if (interface
->rbuf_len
-
435 interface
->rbuf_offset
< sizeof hdr
) {
436 interface
->rbuf_offset
= interface
->rbuf_len
;
440 /* Copy out a bpf header... */
441 memcpy(&hdr
, &interface
->rbuf
[interface
->rbuf_offset
],
444 /* If the bpf header plus data doesn't fit in what's left
445 of the buffer, stick head in sand yet again... */
446 if (interface
->rbuf_offset
+
447 hdr
.bh_hdrlen
+ hdr
.bh_caplen
> interface
->rbuf_len
) {
448 interface
->rbuf_offset
= interface
->rbuf_len
;
452 /* If the captured data wasn't the whole packet, or if
453 the packet won't fit in the input buffer, all we
454 can do is drop it. */
455 if (hdr
.bh_caplen
!= hdr
.bh_datalen
) {
456 interface
->rbuf_offset
=
457 BPF_WORDALIGN(interface
->rbuf_offset
+
458 hdr
.bh_hdrlen
+ hdr
.bh_caplen
);
462 /* Skip over the BPF header... */
463 interface
->rbuf_offset
+= hdr
.bh_hdrlen
;
465 /* Decode the physical header... */
466 offset
= decode_hw_header(interface
, interface
->rbuf
,
467 interface
->rbuf_offset
, hfrom
);
469 /* If a physical layer checksum failed (dunno of any
470 physical layer that supports this, but WTH), skip this
473 interface
->rbuf_offset
=
474 BPF_WORDALIGN(interface
->rbuf_offset
+
478 interface
->rbuf_offset
+= offset
;
479 hdr
.bh_caplen
-= offset
;
481 /* Decode the IP and UDP headers... */
482 offset
= decode_udp_ip_header(interface
, interface
->rbuf
,
483 interface
->rbuf_offset
,
484 from
, hdr
.bh_caplen
, &paylen
, 1);
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
, paylen
);
508 interface
->rbuf_offset
=
509 BPF_WORDALIGN(interface
->rbuf_offset
+ hdr
.bh_caplen
);
511 } while (interface
->rbuf_offset
< interface
->rbuf_len
);
516 int can_unicast_without_arp (ip
)
517 struct interface_info
*ip
;
522 int can_receive_unicast_unconfigured (ip
)
523 struct interface_info
*ip
;
528 int supports_multiple_interfaces (ip
)
529 struct interface_info
*ip
;
534 void maybe_setup_fallback ()
537 struct interface_info
*fbi
= (struct interface_info
*)0;
538 if (setup_fallback (&fbi
, MDL
)) {
539 if_register_fallback (fbi
);
540 status
= omapi_register_io_object ((omapi_object_t
*)fbi
,
542 fallback_discard
, 0, 0);
543 if (status
!= ISC_R_SUCCESS
)
544 log_fatal ("Can't register I/O handle for %s: %s",
545 fbi
-> name
, isc_result_totext (status
));
546 interface_dereference (&fbi
, MDL
);
551 #if defined(USE_BPF_RECEIVE) || defined(USE_BPF_HWADDR)
553 get_hw_addr(const char *name
, struct hardware
*hw
) {
556 struct sockaddr_dl
*sa
;
558 if (getifaddrs(&ifa
) != 0) {
559 log_fatal("Error getting interface information; %m");
563 * Loop through our interfaces finding a match.
566 for (p
=ifa
; (p
!= NULL
) && (sa
== NULL
); p
= p
->ifa_next
) {
567 if ((p
->ifa_addr
->sa_family
== AF_LINK
) &&
568 !strcmp(p
->ifa_name
, name
)) {
569 sa
= (struct sockaddr_dl
*)p
->ifa_addr
;
573 log_fatal("No interface called '%s'", name
);
577 * Pull out the appropriate information.
579 switch (sa
->sdl_type
) {
581 #if defined (IFT_L2VLAN)
584 hw
->hlen
= sa
->sdl_alen
+ 1;
585 hw
->hbuf
[0] = HTYPE_ETHER
;
586 memcpy(&hw
->hbuf
[1], LLADDR(sa
), sa
->sdl_alen
);
589 case IFT_ISO88024
: /* "token ring" */
592 hw
->hlen
= sa
->sdl_alen
+ 1;
593 hw
->hbuf
[0] = HTYPE_IEEE802
;
594 memcpy(&hw
->hbuf
[1], LLADDR(sa
), sa
->sdl_alen
);
598 hw
->hlen
= sa
->sdl_alen
+ 1;
599 hw
->hbuf
[0] = HTYPE_FDDI
;
600 memcpy(&hw
->hbuf
[1], LLADDR(sa
), sa
->sdl_alen
);
602 #endif /* IFT_FDDI */
604 log_fatal("Unsupported device type %d for \"%s\"",