MFC: following 2 commits:
[dragonfly.git] / contrib / dhcp-3.0 / common / bpf.c
blob2066268ba9b0bf27b7a4f630481f99e5daac4b0c
1 /* bpf.c
3 BPF socket interface code, originally contributed by Archie Cobbs. */
5 /*
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.
22 * 950 Charter Street
23 * Redwood City, CA 94063
24 * <info@isc.org>
25 * http://www.isc.org/
27 * This software was contributed to Internet Systems Consortium
28 * by Archie Cobbs.
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.
35 #ifndef lint
36 static char copyright[] =
37 "$Id: bpf.c,v 1.48.2.7 2004/11/24 17:39:15 dhankins Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
38 #endif /* not lint */
40 #include "dhcpd.h"
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. */
47 # else
48 # include <sys/ioctl.h>
49 # include <sys/uio.h>
50 # include <net/bpf.h>
51 # if defined (NEED_OSF_PFILT_HACKS)
52 # include <net/pfilt.h>
53 # endif
54 # endif
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"
60 #endif
62 /* Reinitializes the specified interface after an address change. This
63 is not required for packet-filter APIs. */
65 #ifdef USE_BPF_SEND
66 void if_reinitialize_send (info)
67 struct interface_info *info;
70 #endif
72 #ifdef USE_BPF_RECEIVE
73 void if_reinitialize_receive (info)
74 struct interface_info *info;
77 #endif
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
81 mask. */
83 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
84 int if_register_bpf (info)
85 struct interface_info *info;
87 int sock;
88 char filename[50];
89 int b;
91 /* Open a BPF device */
92 for (b = 0; 1; b++) {
93 /* %Audit% 31 bytes max. %2004.06.17,Safe% */
94 sprintf(filename, BPF_FORMAT, b);
95 sock = open (filename, O_RDWR, 0);
96 if (sock < 0) {
97 if (errno == EBUSY) {
98 continue;
99 } else {
100 if (!b)
101 log_fatal ("No bpf devices.%s%s%s",
102 " Please read the README",
103 " section for your operating",
104 " system.");
105 log_fatal ("Can't find free bpf: %m");
107 } else {
108 break;
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);
117 return sock;
119 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
121 #ifdef USE_BPF_SEND
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);
129 #else
130 info -> wfdesc = info -> rfdesc;
131 #endif
132 if (!quiet_interface_discovery)
133 log_info ("Sending on BPF/%s/%s%s%s",
134 info -> name,
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);
150 #endif
151 info -> wfdesc = -1;
153 if (!quiet_interface_discovery)
154 log_info ("Disabling output on BPF/%s/%s%s%s",
155 info -> name,
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;
199 #endif
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;
223 int flag = 1;
224 struct bpf_version v;
225 u_int32_t addr;
226 struct bpf_program p;
227 u_int32_t bits;
228 #ifdef DEC_FDDI
229 int link_layer;
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
245 packets. */
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... */
255 bits = 0;
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");
263 #endif
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);
268 if (!info -> rbuf)
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;
277 #ifdef DEC_FDDI
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,
283 MDL);
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;
300 } else
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",
313 info -> name,
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);
326 info -> rfdesc = -1;
328 if (!quiet_interface_discovery)
329 log_info ("Disabling input on BPF/%s/%s%s%s",
330 info -> name,
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 */
340 #ifdef USE_BPF_SEND
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;
345 size_t len;
346 struct in_addr from;
347 struct sockaddr_in *to;
348 struct hardware *hto;
350 unsigned hbufp = 0, ibufp = 0;
351 double hw [4];
352 double ip [32];
353 struct iovec iov [3];
354 int result;
355 int fudge;
357 if (!strcmp (interface -> name, "fallback"))
358 return send_fallback (interface, packet, raw,
359 len, from, to, hto);
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);
368 /* Fire it off */
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);
377 if (result < 0)
378 log_error ("send_packet: %m");
379 return result;
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;
386 unsigned char *buf;
387 size_t len;
388 struct sockaddr_in *from;
389 struct hardware *hfrom;
391 int length = 0;
392 int offset = 0;
393 struct bpf_hdr hdr;
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... */
403 do {
404 /* If the buffer is empty, fill it. */
405 if (interface -> rbuf_offset == interface -> rbuf_len) {
406 length = read (interface -> rfdesc,
407 interface -> rbuf,
408 (size_t)interface -> rbuf_max);
409 if (length <= 0) {
410 #ifdef __FreeBSD__
411 if (errno == ENXIO) {
412 #else
413 if (errno == EIO) {
414 #endif
415 dhcp_interface_remove
416 ((omapi_object_t *)interface,
417 (omapi_object_t *)0);
419 return length;
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;
430 continue;
433 /* Copy out a bpf header... */
434 memcpy (&hdr, &interface -> rbuf [interface -> rbuf_offset],
435 sizeof hdr);
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;
442 continue;
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);
452 continue;
455 /* Skip over the BPF header... */
456 interface -> rbuf_offset += hdr.bh_hdrlen;
458 /* Decode the physical header... */
459 offset = decode_hw_header (interface,
460 interface -> rbuf,
461 interface -> rbuf_offset,
462 hfrom);
464 /* If a physical layer checksum failed (dunno of any
465 physical layer that supports this, but WTH), skip this
466 packet. */
467 if (offset < 0) {
468 interface -> rbuf_offset =
469 BPF_WORDALIGN (interface -> rbuf_offset +
470 hdr.bh_caplen);
471 continue;
473 interface -> rbuf_offset += offset;
474 hdr.bh_caplen -= offset;
476 /* Decode the IP and UDP headers... */
477 offset = decode_udp_ip_header (interface,
478 interface -> rbuf,
479 interface -> rbuf_offset,
480 from,
481 hdr.bh_caplen);
483 /* If the IP or UDP checksum was bad, skip the packet... */
484 if (offset < 0) {
485 interface -> rbuf_offset =
486 BPF_WORDALIGN (interface -> rbuf_offset +
487 hdr.bh_caplen);
488 continue;
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
495 life, though). */
496 if (hdr.bh_caplen > len) {
497 interface -> rbuf_offset =
498 BPF_WORDALIGN (interface -> rbuf_offset +
499 hdr.bh_caplen);
500 continue;
503 /* Copy out the data in the packet... */
504 memcpy (buf, interface -> rbuf + interface -> rbuf_offset,
505 hdr.bh_caplen);
506 interface -> rbuf_offset =
507 BPF_WORDALIGN (interface -> rbuf_offset +
508 hdr.bh_caplen);
509 return hdr.bh_caplen;
510 } while (!length);
511 return 0;
514 int can_unicast_without_arp (ip)
515 struct interface_info *ip;
517 return 1;
520 int can_receive_unicast_unconfigured (ip)
521 struct interface_info *ip;
523 return 1;
526 int supports_multiple_interfaces (ip)
527 struct interface_info *ip;
529 return 1;
532 void maybe_setup_fallback ()
534 isc_result_t status;
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,
539 if_readsocket, 0,
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);
547 #endif