Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / dhcp / common / bpf.c
blob6ad9a6a78d7fa024e6a6f6b62fd6889893598eaa
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.10 2005/08/11 17:13:21 drochner 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 <paths.h>
57 #include <netinet/in_systm.h>
58 #include "includes/netinet/ip.h"
59 #include "includes/netinet/udp.h"
60 #include "includes/netinet/if_ether.h"
61 #endif
63 /* Reinitializes the specified interface after an address change. This
64 is not required for packet-filter APIs. */
66 #ifdef USE_BPF_SEND
67 void if_reinitialize_send (info)
68 struct interface_info *info;
71 #endif
73 #ifdef USE_BPF_RECEIVE
74 void if_reinitialize_receive (info)
75 struct interface_info *info;
78 #endif
80 /* Called by get_interface_list for each interface that's discovered.
81 Opens a packet filter for each interface and adds it to the select
82 mask. */
84 #if defined (USE_BPF_SEND) || defined (USE_BPF_RECEIVE)
85 int if_register_bpf (info)
86 struct interface_info *info;
88 int sock;
89 u_int bufsize;
91 /* Open a BPF device */
92 #ifdef _PATH_BPF
93 const char *filename = _PATH_BPF;
94 sock = open (filename, O_RDWR, 0);
95 if (sock < 0)
96 log_fatal ("No bpf devices.%s%s%s",
97 " Please read the README",
98 " section for your operating",
99 " system.");
101 #else
102 char filename[50];
103 int b;
104 for (b = 0; 1; b++) {
105 /* %Audit% 31 bytes max. %2004.06.17,Safe% */
106 sprintf(filename, BPF_FORMAT, b);
107 sock = open (filename, O_RDWR, 0);
108 if (sock < 0) {
109 if (errno == EBUSY) {
110 continue;
111 } else {
112 if (!b)
113 log_fatal ("No bpf devices.%s%s%s",
114 " Please read the README",
115 " section for your operating",
116 " system.");
117 log_fatal ("Can't find free bpf: %m");
119 } else {
120 break;
123 #endif
125 /* Set the BPF buffer size to 32k */
126 bufsize = 32768;
127 if (ioctl (sock, BIOCSBLEN, &bufsize) < 0)
128 log_error ("Can't set bpf buffer to %d: %m", bufsize);
130 /* Set the BPF device to point at this interface. */
131 if (ioctl (sock, BIOCSETIF, info -> ifp) < 0)
132 log_fatal ("Can't attach interface %s to bpf device %s: %m",
133 info -> name, filename);
135 return sock;
137 #endif /* USE_BPF_SEND || USE_BPF_RECEIVE */
139 #ifdef USE_BPF_SEND
140 void if_register_send (info)
141 struct interface_info *info;
143 /* If we're using the bpf API for sending and receiving,
144 we don't need to register this interface twice. */
145 #ifndef USE_BPF_RECEIVE
146 info -> wfdesc = if_register_bpf (info, interface);
147 #else
148 info -> wfdesc = info -> rfdesc;
149 #endif
150 if (!quiet_interface_discovery)
151 log_info ("Sending on BPF/%s/%s%s%s",
152 info -> name,
153 print_hw_addr (info -> hw_address.hbuf [0],
154 info -> hw_address.hlen - 1,
155 &info -> hw_address.hbuf [1]),
156 (info -> shared_network ? "/" : ""),
157 (info -> shared_network ?
158 info -> shared_network -> name : ""));
161 void if_deregister_send (info)
162 struct interface_info *info;
164 /* If we're using the bpf API for sending and receiving,
165 we don't need to register this interface twice. */
166 #ifndef USE_BPF_RECEIVE
167 close (info -> wfdesc);
168 #endif
169 info -> wfdesc = -1;
171 if (!quiet_interface_discovery)
172 log_info ("Disabling output on BPF/%s/%s%s%s",
173 info -> name,
174 print_hw_addr (info -> hw_address.hbuf [0],
175 info -> hw_address.hlen - 1,
176 &info -> hw_address.hbuf [1]),
177 (info -> shared_network ? "/" : ""),
178 (info -> shared_network ?
179 info -> shared_network -> name : ""));
181 #endif /* USE_BPF_SEND */
183 #if defined (USE_BPF_RECEIVE) || defined (USE_LPF_RECEIVE)
184 /* Packet filter program...
185 XXX Changes to the filter program may require changes to the constant
186 offsets used in if_register_send to patch the BPF program! XXX */
188 struct bpf_insn dhcp_bpf_filter [] = {
189 /* Make sure this is an IP packet... */
190 BPF_STMT (BPF_LD + BPF_H + BPF_ABS, 12),
191 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 8),
193 /* Make sure it's a UDP packet... */
194 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 23),
195 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_UDP, 0, 6),
197 /* Make sure this isn't a fragment... */
198 BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 20),
199 BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x1fff, 4, 0),
201 /* Get the IP header length... */
202 BPF_STMT (BPF_LDX + BPF_B + BPF_MSH, 14),
204 /* Make sure it's to the right port... */
205 BPF_STMT (BPF_LD + BPF_H + BPF_IND, 16),
206 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 67, 0, 1), /* patch */
208 /* If we passed all the tests, ask for the whole packet. */
209 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
211 /* Otherwise, drop it. */
212 BPF_STMT(BPF_RET+BPF_K, 0),
215 #if defined(DEC_FDDI) || defined(NETBSD_FDDI)
216 struct bpf_insn *bpf_fddi_filter;
217 #endif
219 int dhcp_bpf_filter_len = sizeof dhcp_bpf_filter / sizeof (struct bpf_insn);
220 #if defined (HAVE_TR_SUPPORT)
221 struct bpf_insn dhcp_bpf_tr_filter [] = {
222 /* accept all token ring packets due to variable length header */
223 /* if we want to get clever, insert the program here */
225 /* If we passed all the tests, ask for the whole packet. */
226 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
228 /* Otherwise, drop it. */
229 BPF_STMT(BPF_RET+BPF_K, 0),
232 int dhcp_bpf_tr_filter_len = (sizeof dhcp_bpf_tr_filter /
233 sizeof (struct bpf_insn));
234 #endif /* HAVE_TR_SUPPORT */
235 #endif /* USE_LPF_RECEIVE || USE_BPF_RECEIVE */
237 #if defined (USE_BPF_RECEIVE)
238 void if_register_receive (info)
239 struct interface_info *info;
241 int flag = 1;
242 struct bpf_version v;
243 struct bpf_program p;
244 #ifdef NEED_OSF_PFILT_HACKS
245 u_int32_t bits;
246 #endif
247 u_int32_t len;
248 #if defined(DEC_FDDI) || defined(NETBSD_FDDI)
249 int link_layer;
250 #endif /* DEC_FDDI || NETBSD_FDDI */
252 /* Open a BPF device and hang it on this interface... */
253 info -> rfdesc = if_register_bpf (info);
255 /* Make sure the BPF version is in range... */
256 if (ioctl (info -> rfdesc, BIOCVERSION, &v) < 0)
257 log_fatal ("Can't get BPF version: %m");
259 if (v.bv_major != BPF_MAJOR_VERSION ||
260 v.bv_minor < BPF_MINOR_VERSION)
261 log_fatal ("BPF version mismatch - recompile DHCP!");
263 /* Set immediate mode so that reads return as soon as a packet
264 comes in, rather than waiting for the input buffer to fill with
265 packets. */
266 if (ioctl (info -> rfdesc, BIOCIMMEDIATE, &flag) < 0)
267 log_fatal ("Can't set immediate mode on bpf device: %m");
269 #ifdef NEED_OSF_PFILT_HACKS
270 /* Allow the copyall flag to be set... */
271 if (ioctl(info -> rfdesc, EIOCALLOWCOPYALL, &flag) < 0)
272 log_fatal ("Can't set ALLOWCOPYALL: %m");
274 /* Clear all the packet filter mode bits first... */
275 bits = 0;
276 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
277 log_fatal ("Can't clear pfilt bits: %m");
279 /* Set the ENBATCH, ENCOPYALL, ENBPFHDR bits... */
280 bits = ENBATCH | ENCOPYALL | ENBPFHDR;
281 if (ioctl (info -> rfdesc, EIOCMBIS, &bits) < 0)
282 log_fatal ("Can't set ENBATCH|ENCOPYALL|ENBPFHDR: %m");
283 #endif
284 /* Get the required BPF buffer length from the kernel. */
285 if (ioctl (info -> rfdesc, BIOCGBLEN, &len) < 0)
286 log_fatal ("Can't get bpf buffer length: %m");
287 info -> rbuf_max = len;
288 info -> rbuf = dmalloc (info -> rbuf_max, MDL);
289 if (!info -> rbuf)
290 log_fatal ("Can't allocate %ld bytes for bpf input buffer.",
291 (long)(info -> rbuf_max));
292 info -> rbuf_offset = 0;
293 info -> rbuf_len = 0;
295 /* Set up the bpf filter program structure. */
296 p.bf_len = dhcp_bpf_filter_len;
298 #if defined(DEC_FDDI) || defined(NETBSD_FDDI)
299 /* See if this is an FDDI interface, flag it for later. */
300 if (ioctl(info -> rfdesc, BIOCGDLT, &link_layer) >= 0 &&
301 link_layer == DLT_FDDI) {
302 if (!bpf_fddi_filter) {
303 bpf_fddi_filter = dmalloc (sizeof bpf_fddi_filter,
304 MDL);
305 if (!bpf_fddi_filter)
306 log_fatal ("No memory for FDDI filter.");
307 memcpy (bpf_fddi_filter,
308 dhcp_bpf_filter, sizeof dhcp_bpf_filter);
309 /* Patch the BPF program to account for the difference
310 in length between ethernet headers (14), FDDI and
311 802.2 headers (16 +8=24, +10).
312 XXX changes to filter program may require changes to
313 XXX the insn number(s) used below! */
314 bpf_fddi_filter[0].k += 10;
315 bpf_fddi_filter[2].k += 10;
316 bpf_fddi_filter[4].k += 10;
317 bpf_fddi_filter[6].k += 10;
318 bpf_fddi_filter[7].k += 10;
320 p.bf_insns = bpf_fddi_filter;
321 } else
322 #endif /* DEC_FDDI || NETBSD_FDDI */
323 p.bf_insns = dhcp_bpf_filter;
325 /* Patch the server port into the BPF program...
326 XXX changes to filter program may require changes
327 to the insn number(s) used below! XXX */
328 dhcp_bpf_filter [8].k = ntohs (local_port);
330 if (ioctl (info -> rfdesc, BIOCSETF, &p) < 0)
331 log_fatal ("Can't install packet filter program: %m");
332 if (!quiet_interface_discovery)
333 log_info ("Listening on BPF/%s/%s%s%s",
334 info -> name,
335 print_hw_addr (info -> hw_address.hbuf [0],
336 info -> hw_address.hlen - 1,
337 &info -> hw_address.hbuf [1]),
338 (info -> shared_network ? "/" : ""),
339 (info -> shared_network ?
340 info -> shared_network -> name : ""));
343 void if_deregister_receive (info)
344 struct interface_info *info;
346 close (info -> rfdesc);
347 info -> rfdesc = -1;
349 if (!quiet_interface_discovery)
350 log_info ("Disabling input on BPF/%s/%s%s%s",
351 info -> name,
352 print_hw_addr (info -> hw_address.hbuf [0],
353 info -> hw_address.hlen - 1,
354 &info -> hw_address.hbuf [1]),
355 (info -> shared_network ? "/" : ""),
356 (info -> shared_network ?
357 info -> shared_network -> name : ""));
359 #endif /* USE_BPF_RECEIVE */
361 #ifdef USE_BPF_SEND
362 ssize_t send_packet (interface, packet, raw, len, from, to, hto)
363 struct interface_info *interface;
364 struct packet *packet;
365 struct dhcp_packet *raw;
366 size_t len;
367 struct in_addr from;
368 struct sockaddr_in *to;
369 struct hardware *hto;
371 unsigned hbufp = 0, ibufp = 0;
372 double hw [4];
373 double ip [32];
374 struct iovec iov [3];
375 int result;
377 if (!strcmp (interface -> name, "fallback"))
378 return send_fallback (interface, packet, raw,
379 len, from, to, hto);
381 /* Assemble the headers... */
382 assemble_hw_header (interface, (unsigned char *)hw, &hbufp, hto);
383 assemble_udp_ip_header (interface,
384 (unsigned char *)ip, &ibufp, from.s_addr,
385 to -> sin_addr.s_addr, to -> sin_port,
386 (unsigned char *)raw, len);
388 /* Fire it off */
389 iov [0].iov_base = ((char *)hw);
390 iov [0].iov_len = hbufp;
391 iov [1].iov_base = ((char *)ip);
392 iov [1].iov_len = ibufp;
393 iov [2].iov_base = (char *)raw;
394 iov [2].iov_len = len;
396 result = writev(interface -> wfdesc, iov, 3);
397 if (result < 0)
398 log_error ("send_packet: %m");
399 return result;
401 #endif /* USE_BPF_SEND */
403 #ifdef USE_BPF_RECEIVE
404 ssize_t receive_packet (interface, buf, len, from, hfrom)
405 struct interface_info *interface;
406 unsigned char *buf;
407 size_t len;
408 struct sockaddr_in *from;
409 struct hardware *hfrom;
411 int length = 0;
412 int offset = 0;
413 struct bpf_hdr hdr;
414 unsigned paylen;
416 /* All this complexity is because BPF doesn't guarantee
417 that only one packet will be returned at a time. We're
418 getting what we deserve, though - this is a terrible abuse
419 of the BPF interface. Sigh. */
421 /* Process packets until we get one we can return or until we've
422 done a read and gotten nothing we can return... */
424 do {
425 /* If the buffer is empty, fill it. */
426 if (interface -> rbuf_offset == interface -> rbuf_len) {
427 length = read (interface -> rfdesc,
428 interface -> rbuf,
429 (size_t)interface -> rbuf_max);
430 if (length <= 0) {
431 #ifdef __FreeBSD__
432 if (errno == ENXIO) {
433 #else
434 if (errno == EIO) {
435 #endif
436 dhcp_interface_remove
437 ((omapi_object_t *)interface,
438 (omapi_object_t *)0);
440 return length;
442 interface -> rbuf_offset = 0;
443 interface -> rbuf_len = BPF_WORDALIGN (length);
446 /* If there isn't room for a whole bpf header, something went
447 wrong, but we'll ignore it and hope it goes away... XXX */
448 if (interface -> rbuf_len -
449 interface -> rbuf_offset < sizeof hdr) {
450 interface -> rbuf_offset = interface -> rbuf_len;
451 continue;
454 /* Adjust for any padding BPF inserted between the packets. */
455 interface -> rbuf_offset =
456 BPF_WORDALIGN (interface -> rbuf_offset);
458 /* Copy out a bpf header... */
459 memcpy (&hdr, &interface -> rbuf [interface -> rbuf_offset],
460 sizeof hdr);
462 /* If the bpf header plus data doesn't fit in what's left
463 of the buffer, stick head in sand yet again... */
464 if (interface -> rbuf_offset +
465 hdr.bh_hdrlen + hdr.bh_caplen > interface -> rbuf_len) {
466 interface -> rbuf_offset = interface -> rbuf_len;
467 continue;
470 /* If the captured data wasn't the whole packet, or if
471 the packet won't fit in the input buffer, all we
472 can do is drop it. */
473 if (hdr.bh_caplen != hdr.bh_datalen) {
474 interface -> rbuf_offset =
475 BPF_WORDALIGN (interface -> rbuf_offset +
476 hdr.bh_hdrlen + hdr.bh_caplen);
477 continue;
480 /* Skip over the BPF header... */
481 interface -> rbuf_offset += hdr.bh_hdrlen;
483 /* Decode the physical header... */
484 offset = decode_hw_header (interface,
485 interface -> rbuf,
486 interface -> rbuf_offset,
487 hfrom);
489 /* If a physical layer checksum failed (dunno of any
490 physical layer that supports this, but WTH), skip this
491 packet. */
492 if (offset < 0) {
493 interface -> rbuf_offset =
494 BPF_WORDALIGN (interface -> rbuf_offset +
495 hdr.bh_caplen);
496 continue;
498 interface -> rbuf_offset += offset;
499 hdr.bh_caplen -= offset;
501 /* Decode the IP and UDP headers... */
502 offset = decode_udp_ip_header (interface,
503 interface -> rbuf,
504 interface -> rbuf_offset,
505 from,
506 hdr.bh_caplen, &paylen);
508 /* If the IP or UDP checksum was bad, skip the packet... */
509 if (offset < 0) {
510 interface -> rbuf_offset =
511 BPF_WORDALIGN (interface -> rbuf_offset +
512 hdr.bh_caplen);
513 continue;
515 interface -> rbuf_offset = interface -> rbuf_offset + offset;
516 hdr.bh_caplen -= offset;
518 /* If there's not enough room to stash the packet data,
519 we have to skip it (this shouldn't happen in real
520 life, though). */
521 if (hdr.bh_caplen > len) {
522 interface -> rbuf_offset =
523 BPF_WORDALIGN (interface -> rbuf_offset +
524 hdr.bh_caplen);
525 continue;
528 /* Copy out the data in the packet... */
529 memcpy (buf, interface -> rbuf + interface -> rbuf_offset,
530 paylen);
531 interface -> rbuf_offset =
532 BPF_WORDALIGN (interface -> rbuf_offset +
533 hdr.bh_caplen);
534 return paylen;
535 } while (!length);
536 return 0;
539 int can_unicast_without_arp (ip)
540 struct interface_info *ip;
542 return 1;
545 int can_receive_unicast_unconfigured (ip)
546 struct interface_info *ip;
548 return 1;
551 int supports_multiple_interfaces (ip)
552 struct interface_info *ip;
554 return 1;
557 void maybe_setup_fallback ()
559 isc_result_t status;
560 struct interface_info *fbi = (struct interface_info *)0;
561 if (setup_fallback (&fbi, MDL)) {
562 if_register_fallback (fbi);
563 status = omapi_register_io_object ((omapi_object_t *)fbi,
564 if_readsocket, 0,
565 fallback_discard, 0, 0);
566 if (status != ISC_R_SUCCESS)
567 log_fatal ("Can't register I/O handle for %s: %s",
568 fbi -> name, isc_result_totext (status));
569 interface_dereference (&fbi, MDL);
572 #endif