3 BPF socket interface code, originally contributed by Archie Cobbs. */
6 * Copyright (c) 1996-2002 Internet Software Consortium.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software was contributed to the Internet Software Consortium
37 * by Archie Cobbs, and is now maintained by Ted Lemon in cooperation
38 * with Nominum, Inc. To learn more about the Internet Software
39 * Consortium, see ``http://www.isc.org/''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''. To learn more about
41 * Nominum, Inc., see ``http://www.nominum.com''.
43 * Patches for FDDI support on Digital Unix were written by Bill
44 * Stapleton, and maintained for a while by Mike Meredith before he
45 * managed to get me to integrate them.
49 static char copyright[] =
50 "$Id: bpf.c,v 1.48.2.3 2002/11/17 02:26:56 dhankins Exp $ Copyright (c) 1995-2002 The Internet Software Consortium. All rights reserved.\n";
54 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE) \
55 || defined (USE_LPF_RECEIVE)
56 # if defined (USE_LPF_RECEIVE)
57 # include <asm/types.h>
58 # include <linux/filter.h>
59 # define bpf_insn sock_filter /* Linux: dare to be gratuitously different. */
61 # include <sys/ioctl.h>
64 # if defined (NEED_OSF_PFILT_HACKS)
65 # include <net/pfilt.h>
69 #include <netinet/in_systm.h>
70 #include "includes/netinet/ip.h"
71 #include "includes/netinet/udp.h"
72 #include "includes/netinet/if_ether.h"
75 /* Reinitializes the specified interface after an address change. This
76 is not required for packet-filter APIs. */
79 void if_reinitialize_send (info)
80 struct interface_info *info;
85 #ifdef USE_BPF_RECEIVE
86 void if_reinitialize_receive (info)
87 struct interface_info *info;
92 /* Called by get_interface_list for each interface that's discovered.
93 Opens a packet filter for each interface and adds it to the select
96 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
97 int if_register_bpf (info)
98 struct interface_info *info;
104 /* Open a BPF device */
105 for (b = 0; 1; b++) {
107 snprintf(filename, sizeof(filename), BPF_FORMAT, b);
109 sprintf(filename, BPF_FORMAT, b);
111 sock = open (filename, O_RDWR, 0);
113 if (errno == EBUSY) {
117 log_fatal ("No bpf devices.%s%s%s",
118 " Please read the README",
119 " section for your operating",
121 log_fatal ("Can't find free bpf: %m");
128 /* Set the BPF device to point at this interface. */
129 if (ioctl (sock, BIOCSETIF, info -> ifp) < 0)
130 log_fatal ("Can't attach interface %s to bpf device %s: %m",
131 info -> name, filename);
135 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
138 void if_register_send (info)
139 struct interface_info *info;
141 /* If we're using the bpf API for sending and receiving,
142 we don't need to register this interface twice. */
143 #ifndef USE_BPF_RECEIVE
144 info -> wfdesc = if_register_bpf (info, interface);
146 info -> wfdesc = info -> rfdesc;
148 if (!quiet_interface_discovery)
149 log_info ("Sending on BPF/%s/%s%s%s",
151 print_hw_addr (info -> hw_address.hbuf [0],
152 info -> hw_address.hlen - 1,
153 &info -> hw_address.hbuf [1]),
154 (info -> shared_network ? "/" : ""),
155 (info -> shared_network ?
156 info -> shared_network -> name : ""));
159 void if_deregister_send (info)
160 struct interface_info *info;
162 /* If we're using the bpf API for sending and receiving,
163 we don't need to register this interface twice. */
164 #ifndef USE_BPF_RECEIVE
165 close (info -> wfdesc);
169 if (!quiet_interface_discovery)
170 log_info ("Disabling output on BPF/%s/%s%s%s",
172 print_hw_addr (info -> hw_address.hbuf [0],
173 info -> hw_address.hlen - 1,
174 &info -> hw_address.hbuf [1]),
175 (info -> shared_network ? "/" : ""),
176 (info -> shared_network ?
177 info -> shared_network -> name : ""));
179 #endif /* USE_BPF_SEND */
181 #if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
182 /* Packet filter program...
183 XXX Changes to the filter program may require changes to the constant
184 offsets used in if_register_send to patch the BPF program! XXX */
186 struct bpf_insn dhcp_bpf_filter [] = {
187 /* Make sure this is an IP packet... */
188 BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12),
189 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
191 /* Make sure it's a UDP packet... */
192 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23),
193 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
195 /* Make sure this isn't a fragment... */
196 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
197 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
199 /* Get the IP header length... */
200 BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14),
202 /* Make sure it's to the right port... */
203 BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16),
204 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */
206 /* If we passed all the tests, ask for the whole packet. */
207 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
209 /* Otherwise, drop it. */
210 BPF_STMT(BPF_RET+BPF_K, 0),
213 #if defined (DEC_FDDI)
214 struct bpf_insn *bpf_fddi_filter;
217 int dhcp_bpf_filter_len = sizeof dhcp_bpf_filter / sizeof (struct bpf_insn);
218 #if defined (HAVE_TR_SUPPORT)
219 struct bpf_insn dhcp_bpf_tr_filter [] = {
220 /* accept all token ring packets due to variable length header */
221 /* if we want to get clever, insert the program here */
223 /* If we passed all the tests, ask for the whole packet. */
224 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
226 /* Otherwise, drop it. */
227 BPF_STMT(BPF_RET+BPF_K, 0),
230 int dhcp_bpf_tr_filter_len = (sizeof dhcp_bpf_tr_filter /
231 sizeof (struct bpf_insn));
232 #endif /* HAVE_TR_SUPPORT */
233 #endif /* USE_LPF_RECEIVE || USE_BPF_RECEIVE */
235 #if defined (USE_BPF_RECEIVE)
236 void if_register_receive (info)
237 struct interface_info *info;
240 struct bpf_version v;
242 struct bpf_program p;
246 #endif /* DEC_FDDI */
248 /* Open a BPF device and hang it on this interface... */
249 info -> rfdesc = if_register_bpf (info);
251 /* Make sure the BPF version is in range... */
252 if (ioctl (info -> rfdesc, BIOCVERSION, &v) < 0)
253 log_fatal ("Can't get BPF version: %m");
255 if (v.bv_major != BPF_MAJOR_VERSION ||
256 v.bv_minor < BPF_MINOR_VERSION)
257 log_fatal ("BPF version mismatch - recompile DHCP!");
259 /* Set immediate mode so that reads return as soon as a packet
260 comes in, rather than waiting for the input buffer to fill with
262 if (ioctl (info -> rfdesc, BIOCIMMEDIATE, &flag) < 0)
263 log_fatal ("Can't set immediate mode on bpf device: %m");
265 #ifdef NEED_OSF_PFILT_HACKS
266 /* Allow the copyall flag to be set... */
267 if (ioctl(info -> rfdesc, EIOCALLOWCOPYALL, &flag) < 0)
268 log_fatal ("Can't set ALLOWCOPYALL: %m");
270 /* Clear all the packet filter mode bits first... */
272 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
273 log_fatal ("Can't clear pfilt bits: %m");
275 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
276 bits = ENBATCH | ENCOPYALL | ENBPFHDR;
277 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
278 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
280 /* Get the required BPF buffer length from the kernel. */
281 if (ioctl (info -> rfdesc, BIOCGBLEN, &info -> rbuf_max) < 0)
282 log_fatal ("Can't get bpf buffer length: %m");
283 info -> rbuf = dmalloc (info -> rbuf_max, MDL);
285 log_fatal ("Can't allocate %ld bytes for bpf input buffer.",
286 (long)(info -> rbuf_max));
287 info -> rbuf_offset = 0;
288 info -> rbuf_len = 0;
290 /* Set up the bpf filter program structure. */
291 p.bf_len = dhcp_bpf_filter_len;
294 /* See if this is an FDDI interface, flag it for later. */
295 if (ioctl(info -> rfdesc, BIOCGDLT, &link_layer) >= 0 &&
296 link_layer == DLT_FDDI) {
297 if (!bpf_fddi_filter) {
298 bpf_fddi_filter = dmalloc (sizeof bpf_fddi_filter,
300 if (!bpf_fddi_filter)
301 log_fatal ("No memory for FDDI filter.");
302 memcpy (bpf_fddi_filter,
303 dhcp_bpf_filter, sizeof dhcp_bpf_filter);
304 /* Patch the BPF program to account for the difference
305 in length between ethernet headers (14), FDDI and
306 802.2 headers (16 +8=24, +10).
307 XXX changes to filter program may require changes to
308 XXX the insn number(s) used below! */
309 bpf_fddi_filter[0].k += 10;
310 bpf_fddi_filter[2].k += 10;
311 bpf_fddi_filter[4].k += 10;
312 bpf_fddi_filter[6].k += 10;
313 bpf_fddi_filter[7].k += 10;
315 p.bf_insns = bpf_fddi_filter;
317 #endif /* DEC_FDDI */
318 p.bf_insns = dhcp_bpf_filter;
320 /* Patch the server port into the BPF program...
321 XXX changes to filter program may require changes
322 to the insn number(s) used below! XXX */
323 dhcp_bpf_filter [8].k = ntohs (local_port);
325 if (ioctl (info -> rfdesc, BIOCSETF, &p) < 0)
326 log_fatal ("Can't install packet filter program: %m");
327 if (!quiet_interface_discovery)
328 log_info ("Listening on BPF/%s/%s%s%s",
330 print_hw_addr (info -> hw_address.hbuf [0],
331 info -> hw_address.hlen - 1,
332 &info -> hw_address.hbuf [1]),
333 (info -> shared_network ? "/" : ""),
334 (info -> shared_network ?
335 info -> shared_network -> name : ""));
338 void if_deregister_receive (info)
339 struct interface_info *info;
341 close (info -> rfdesc);
344 if (!quiet_interface_discovery)
345 log_info ("Disabling input on BPF/%s/%s%s%s",
347 print_hw_addr (info -> hw_address.hbuf [0],
348 info -> hw_address.hlen - 1,
349 &info -> hw_address.hbuf [1]),
350 (info -> shared_network ? "/" : ""),
351 (info -> shared_network ?
352 info -> shared_network -> name : ""));
354 #endif /* USE_BPF_RECEIVE */
357 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
358 struct interface_info *interface;
359 struct packet *packet;
360 struct dhcp_packet *raw;
363 struct sockaddr_in *to;
364 struct hardware *hto;
366 unsigned hbufp = 0, ibufp = 0;
369 struct iovec iov [3];
373 if (!strcmp (interface -> name, "fallback"))
374 return send_fallback (interface, packet, raw,
377 /* Assemble the headers... */
378 assemble_hw_header (interface, (unsigned char *)hw, &hbufp, hto);
379 assemble_udp_ip_header (interface,
380 (unsigned char *)ip, &ibufp, from.s_addr,
381 to -> sin_addr.s_addr, to -> sin_port,
382 (unsigned char *)raw, len);
385 iov [0].iov_base = ((char *)hw);
386 iov [0].iov_len = hbufp;
387 iov [1].iov_base = ((char *)ip);
388 iov [1].iov_len = ibufp;
389 iov [2].iov_base = (char *)raw;
390 iov [2].iov_len = len;
392 result = writev(interface -> wfdesc, iov, 3);
394 log_error ("send_packet: %m");
397 #endif /* USE_BPF_SEND */
399 #ifdef USE_BPF_RECEIVE
400 ssize_t receive_packet (interface, buf, len, from, hfrom)
401 struct interface_info *interface;
404 struct sockaddr_in *from;
405 struct hardware *hfrom;
411 /* All this complexity is because BPF doesn't guarantee
412 that only one packet will be returned at a time. We're
413 getting what we deserve, though - this is a terrible abuse
414 of the BPF interface. Sigh. */
416 /* Process packets until we get one we can return or until we've
417 done a read and gotten nothing we can return... */
420 /* If the buffer is empty, fill it. */
421 if (interface -> rbuf_offset == interface -> rbuf_len) {
422 length = read (interface -> rfdesc,
424 (size_t)interface -> rbuf_max);
427 if (errno == ENXIO) {
431 dhcp_interface_remove
432 ((omapi_object_t *)interface,
433 (omapi_object_t *)0);
437 interface -> rbuf_offset = 0;
438 interface -> rbuf_len = BPF_WORDALIGN (length);
441 /* If there isn't room for a whole bpf header, something went
442 wrong, but we'll ignore it and hope it goes away... XXX */
443 if (interface -> rbuf_len -
444 interface -> rbuf_offset < sizeof hdr) {
445 interface -> rbuf_offset = interface -> rbuf_len;
449 /* Copy out a bpf header... */
450 memcpy (&hdr, &interface -> rbuf [interface -> rbuf_offset],
453 /* If the bpf header plus data doesn't fit in what's left
454 of the buffer, stick head in sand yet again... */
455 if (interface -> rbuf_offset +
456 hdr.bh_hdrlen + hdr.bh_caplen > interface -> rbuf_len) {
457 interface -> rbuf_offset = interface -> rbuf_len;
461 /* If the captured data wasn't the whole packet, or if
462 the packet won't fit in the input buffer, all we
463 can do is drop it. */
464 if (hdr.bh_caplen != hdr.bh_datalen) {
465 interface -> rbuf_offset =
466 BPF_WORDALIGN (interface -> rbuf_offset +
467 hdr.bh_hdrlen + hdr.bh_caplen);
471 /* Skip over the BPF header... */
472 interface -> rbuf_offset += hdr.bh_hdrlen;
474 /* Decode the physical header... */
475 offset = decode_hw_header (interface,
477 interface -> rbuf_offset,
480 /* If a physical layer checksum failed (dunno of any
481 physical layer that supports this, but WTH), skip this
484 interface -> rbuf_offset =
485 BPF_WORDALIGN (interface -> rbuf_offset +
489 interface -> rbuf_offset += offset;
490 hdr.bh_caplen -= offset;
492 /* Decode the IP and UDP headers... */
493 offset = decode_udp_ip_header (interface,
495 interface -> rbuf_offset,
500 /* If the IP or UDP checksum was bad, skip the packet... */
502 interface -> rbuf_offset =
503 BPF_WORDALIGN (interface -> rbuf_offset +
507 interface -> rbuf_offset = interface -> rbuf_offset + offset;
508 hdr.bh_caplen -= offset;
510 /* If there's not enough room to stash the packet data,
511 we have to skip it (this shouldn't happen in real
513 if (hdr.bh_caplen > len) {
514 interface -> rbuf_offset =
515 BPF_WORDALIGN (interface -> rbuf_offset +
520 /* Copy out the data in the packet... */
521 memcpy (buf, interface -> rbuf + interface -> rbuf_offset,
523 interface -> rbuf_offset =
524 BPF_WORDALIGN (interface -> rbuf_offset +
526 return hdr.bh_caplen;
531 int can_unicast_without_arp (ip)
532 struct interface_info *ip;
537 int can_receive_unicast_unconfigured (ip)
538 struct interface_info *ip;
543 int supports_multiple_interfaces (ip)
544 struct interface_info *ip;
549 void maybe_setup_fallback ()
552 struct interface_info *fbi = (struct interface_info *)0;
553 if (setup_fallback (&fbi, MDL)) {
554 if_register_fallback (fbi);
555 status = omapi_register_io_object ((omapi_object_t *)fbi,
557 fallback_discard, 0, 0);
558 if (status != ISC_R_SUCCESS)
559 log_fatal ("Can't register I/O handle for %s: %s",
560 fbi -> name, isc_result_totext (status));
561 interface_dereference (&fbi, MDL);