]> The Tcpdump Group git mirrors - libpcap/blob - pcap-linux.c
Add support for sending packets; includes contributions from Mark
[libpcap] / pcap-linux.c
1 /*
2 * pcap-linux.c: Packet capture interface to the Linux kernel
3 *
4 * Copyright (c) 2000 Torsten Landschoff <torsten@debian.org>
5 * Sebastian Krahmer <krahmer@cs.uni-potsdam.de>
6 *
7 * License: BSD
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
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
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 */
27
28 #ifndef lint
29 static const char rcsid[] _U_ =
30 "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.106 2004-03-23 19:18:05 guy Exp $ (LBL)";
31 #endif
32
33 /*
34 * Known problems with 2.0[.x] kernels:
35 *
36 * - The loopback device gives every packet twice; on 2.2[.x] kernels,
37 * if we use PF_PACKET, we can filter out the transmitted version
38 * of the packet by using data in the "sockaddr_ll" returned by
39 * "recvfrom()", but, on 2.0[.x] kernels, we have to use
40 * PF_INET/SOCK_PACKET, which means "recvfrom()" supplies a
41 * "sockaddr_pkt" which doesn't give us enough information to let
42 * us do that.
43 *
44 * - We have to set the interface's IFF_PROMISC flag ourselves, if
45 * we're to run in promiscuous mode, which means we have to turn
46 * it off ourselves when we're done; the kernel doesn't keep track
47 * of how many sockets are listening promiscuously, which means
48 * it won't get turned off automatically when no sockets are
49 * listening promiscuously. We catch "pcap_close()" and, for
50 * interfaces we put into promiscuous mode, take them out of
51 * promiscuous mode - which isn't necessarily the right thing to
52 * do, if another socket also requested promiscuous mode between
53 * the time when we opened the socket and the time when we close
54 * the socket.
55 *
56 * - MSG_TRUNC isn't supported, so you can't specify that "recvfrom()"
57 * return the amount of data that you could have read, rather than
58 * the amount that was returned, so we can't just allocate a buffer
59 * whose size is the snapshot length and pass the snapshot length
60 * as the byte count, and also pass MSG_TRUNC, so that the return
61 * value tells us how long the packet was on the wire.
62 *
63 * This means that, if we want to get the actual size of the packet,
64 * so we can return it in the "len" field of the packet header,
65 * we have to read the entire packet, not just the part that fits
66 * within the snapshot length, and thus waste CPU time copying data
67 * from the kernel that our caller won't see.
68 *
69 * We have to get the actual size, and supply it in "len", because
70 * otherwise, the IP dissector in tcpdump, for example, will complain
71 * about "truncated-ip", as the packet will appear to have been
72 * shorter, on the wire, than the IP header said it should have been.
73 */
74
75
76 #ifdef HAVE_CONFIG_H
77 #include "config.h"
78 #endif
79
80 #include "pcap-int.h"
81 #include "sll.h"
82
83 #ifdef HAVE_DAG_API
84 #include "pcap-dag.h"
85 #endif /* HAVE_DAG_API */
86
87 #include <errno.h>
88 #include <stdlib.h>
89 #include <unistd.h>
90 #include <fcntl.h>
91 #include <string.h>
92 #include <sys/socket.h>
93 #include <sys/ioctl.h>
94 #include <sys/utsname.h>
95 #include <net/if.h>
96 #include <netinet/in.h>
97 #include <linux/if_ether.h>
98 #include <net/if_arp.h>
99
100 /*
101 * If PF_PACKET is defined, we can use {SOCK_RAW,SOCK_DGRAM}/PF_PACKET
102 * sockets rather than SOCK_PACKET sockets.
103 *
104 * To use them, we include <linux/if_packet.h> rather than
105 * <netpacket/packet.h>; we do so because
106 *
107 * some Linux distributions (e.g., Slackware 4.0) have 2.2 or
108 * later kernels and libc5, and don't provide a <netpacket/packet.h>
109 * file;
110 *
111 * not all versions of glibc2 have a <netpacket/packet.h> file
112 * that defines stuff needed for some of the 2.4-or-later-kernel
113 * features, so if the system has a 2.4 or later kernel, we
114 * still can't use those features.
115 *
116 * We're already including a number of other <linux/XXX.h> headers, and
117 * this code is Linux-specific (no other OS has PF_PACKET sockets as
118 * a raw packet capture mechanism), so it's not as if you gain any
119 * useful portability by using <netpacket/packet.h>
120 *
121 * XXX - should we just include <linux/if_packet.h> even if PF_PACKET
122 * isn't defined? It only defines one data structure in 2.0.x, so
123 * it shouldn't cause any problems.
124 */
125 #ifdef PF_PACKET
126 # include <linux/if_packet.h>
127
128 /*
129 * On at least some Linux distributions (for example, Red Hat 5.2),
130 * there's no <netpacket/packet.h> file, but PF_PACKET is defined if
131 * you include <sys/socket.h>, but <linux/if_packet.h> doesn't define
132 * any of the PF_PACKET stuff such as "struct sockaddr_ll" or any of
133 * the PACKET_xxx stuff.
134 *
135 * So we check whether PACKET_HOST is defined, and assume that we have
136 * PF_PACKET sockets only if it is defined.
137 */
138 # ifdef PACKET_HOST
139 # define HAVE_PF_PACKET_SOCKETS
140 # endif /* PACKET_HOST */
141 #endif /* PF_PACKET */
142
143 #ifdef SO_ATTACH_FILTER
144 #include <linux/types.h>
145 #include <linux/filter.h>
146 #endif
147
148 #ifndef __GLIBC__
149 typedef int socklen_t;
150 #endif
151
152 #ifndef MSG_TRUNC
153 /*
154 * This is being compiled on a system that lacks MSG_TRUNC; define it
155 * with the value it has in the 2.2 and later kernels, so that, on
156 * those kernels, when we pass it in the flags argument to "recvfrom()"
157 * we're passing the right value and thus get the MSG_TRUNC behavior
158 * we want. (We don't get that behavior on 2.0[.x] kernels, because
159 * they didn't support MSG_TRUNC.)
160 */
161 #define MSG_TRUNC 0x20
162 #endif
163
164 #ifndef SOL_PACKET
165 /*
166 * This is being compiled on a system that lacks SOL_PACKET; define it
167 * with the value it has in the 2.2 and later kernels, so that we can
168 * set promiscuous mode in the good modern way rather than the old
169 * 2.0-kernel crappy way.
170 */
171 #define SOL_PACKET 263
172 #endif
173
174 #define MAX_LINKHEADER_SIZE 256
175
176 /*
177 * When capturing on all interfaces we use this as the buffer size.
178 * Should be bigger then all MTUs that occur in real life.
179 * 64kB should be enough for now.
180 */
181 #define BIGGER_THAN_ALL_MTUS (64*1024)
182
183 /*
184 * Prototypes for internal functions
185 */
186 static void map_arphrd_to_dlt(pcap_t *, int, int);
187 static int live_open_old(pcap_t *, const char *, int, int, char *);
188 static int live_open_new(pcap_t *, const char *, int, int, char *);
189 static int pcap_read_linux(pcap_t *, int, pcap_handler, u_char *);
190 static int pcap_read_packet(pcap_t *, pcap_handler, u_char *);
191 static int pcap_inject_linux(pcap_t *, const void *, size_t);
192 static int pcap_stats_linux(pcap_t *, struct pcap_stat *);
193 static int pcap_setfilter_linux(pcap_t *, struct bpf_program *);
194 static void pcap_close_linux(pcap_t *);
195
196 /*
197 * Wrap some ioctl calls
198 */
199 #ifdef HAVE_PF_PACKET_SOCKETS
200 static int iface_get_id(int fd, const char *device, char *ebuf);
201 #endif
202 static int iface_get_mtu(int fd, const char *device, char *ebuf);
203 static int iface_get_arptype(int fd, const char *device, char *ebuf);
204 #ifdef HAVE_PF_PACKET_SOCKETS
205 static int iface_bind(int fd, int ifindex, char *ebuf);
206 #endif
207 static int iface_bind_old(int fd, const char *device, char *ebuf);
208
209 #ifdef SO_ATTACH_FILTER
210 static int fix_program(pcap_t *handle, struct sock_fprog *fcode);
211 static int fix_offset(struct bpf_insn *p);
212 static int set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode);
213 static int reset_kernel_filter(pcap_t *handle);
214
215 static struct sock_filter total_insn
216 = BPF_STMT(BPF_RET | BPF_K, 0);
217 static struct sock_fprog total_fcode
218 = { 1, &total_insn };
219 #endif
220
221 /*
222 * Get a handle for a live capture from the given device. You can
223 * pass NULL as device to get all packages (without link level
224 * information of course). If you pass 1 as promisc the interface
225 * will be set to promiscous mode (XXX: I think this usage should
226 * be deprecated and functions be added to select that later allow
227 * modification of that values -- Torsten).
228 *
229 * See also pcap(3).
230 */
231 pcap_t *
232 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
233 char *ebuf)
234 {
235 pcap_t *handle;
236 int mtu;
237 int err;
238 int live_open_ok = 0;
239 struct utsname utsname;
240
241 #ifdef HAVE_DAG_API
242 if (strstr(device, "dag")) {
243 return dag_open_live(device, snaplen, promisc, to_ms, ebuf);
244 }
245 #endif /* HAVE_DAG_API */
246
247 /* Allocate a handle for this session. */
248
249 handle = malloc(sizeof(*handle));
250 if (handle == NULL) {
251 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
252 pcap_strerror(errno));
253 return NULL;
254 }
255
256 /* Initialize some components of the pcap structure. */
257
258 memset(handle, 0, sizeof(*handle));
259 handle->snapshot = snaplen;
260 handle->md.timeout = to_ms;
261
262 /*
263 * NULL and "any" are special devices which give us the hint to
264 * monitor all devices.
265 */
266 if (!device || strcmp(device, "any") == 0) {
267 device = NULL;
268 handle->md.device = strdup("any");
269 if (promisc) {
270 promisc = 0;
271 /* Just a warning. */
272 snprintf(ebuf, PCAP_ERRBUF_SIZE,
273 "Promiscuous mode not supported on the \"any\" device");
274 }
275
276 } else
277 handle->md.device = strdup(device);
278
279 if (handle->md.device == NULL) {
280 snprintf(ebuf, PCAP_ERRBUF_SIZE, "strdup: %s",
281 pcap_strerror(errno) );
282 free(handle);
283 return NULL;
284 }
285
286 /*
287 * Current Linux kernels use the protocol family PF_PACKET to
288 * allow direct access to all packets on the network while
289 * older kernels had a special socket type SOCK_PACKET to
290 * implement this feature.
291 * While this old implementation is kind of obsolete we need
292 * to be compatible with older kernels for a while so we are
293 * trying both methods with the newer method preferred.
294 */
295
296 if ((err = live_open_new(handle, device, promisc, to_ms, ebuf)) == 1)
297 live_open_ok = 1;
298 else if (err == 0) {
299 /* Non-fatal error; try old way */
300 if (live_open_old(handle, device, promisc, to_ms, ebuf))
301 live_open_ok = 1;
302 }
303 if (!live_open_ok) {
304 /*
305 * Both methods to open the packet socket failed. Tidy
306 * up and report our failure (ebuf is expected to be
307 * set by the functions above).
308 */
309
310 if (handle->md.device != NULL)
311 free(handle->md.device);
312 free(handle);
313 return NULL;
314 }
315
316 /*
317 * Compute the buffer size.
318 *
319 * If we're using SOCK_PACKET, this might be a 2.0[.x] kernel,
320 * and might require special handling - check.
321 */
322 if (handle->md.sock_packet && (uname(&utsname) < 0 ||
323 strncmp(utsname.release, "2.0", 3) == 0)) {
324 /*
325 * We're using a SOCK_PACKET structure, and either
326 * we couldn't find out what kernel release this is,
327 * or it's a 2.0[.x] kernel.
328 *
329 * In the 2.0[.x] kernel, a "recvfrom()" on
330 * a SOCK_PACKET socket, with MSG_TRUNC set, will
331 * return the number of bytes read, so if we pass
332 * a length based on the snapshot length, it'll
333 * return the number of bytes from the packet
334 * copied to userland, not the actual length
335 * of the packet.
336 *
337 * This means that, for example, the IP dissector
338 * in tcpdump will get handed a packet length less
339 * than the length in the IP header, and will
340 * complain about "truncated-ip".
341 *
342 * So we don't bother trying to copy from the
343 * kernel only the bytes in which we're interested,
344 * but instead copy them all, just as the older
345 * versions of libpcap for Linux did.
346 *
347 * The buffer therefore needs to be big enough to
348 * hold the largest packet we can get from this
349 * device. Unfortunately, we can't get the MRU
350 * of the network; we can only get the MTU. The
351 * MTU may be too small, in which case a packet larger
352 * than the buffer size will be truncated *and* we
353 * won't get the actual packet size.
354 *
355 * However, if the snapshot length is larger than
356 * the buffer size based on the MTU, we use the
357 * snapshot length as the buffer size, instead;
358 * this means that with a sufficiently large snapshot
359 * length we won't artificially truncate packets
360 * to the MTU-based size.
361 *
362 * This mess just one of many problems with packet
363 * capture on 2.0[.x] kernels; you really want a
364 * 2.2[.x] or later kernel if you want packet capture
365 * to work well.
366 */
367 mtu = iface_get_mtu(handle->fd, device, ebuf);
368 if (mtu == -1) {
369 pcap_close_linux(handle);
370 free(handle);
371 return NULL;
372 }
373 handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
374 if (handle->bufsize < handle->snapshot)
375 handle->bufsize = handle->snapshot;
376 } else {
377 /*
378 * This is a 2.2[.x] or later kernel (we know that
379 * either because we're not using a SOCK_PACKET
380 * socket - PF_PACKET is supported only in 2.2
381 * and later kernels - or because we checked the
382 * kernel version).
383 *
384 * We can safely pass "recvfrom()" a byte count
385 * based on the snapshot length.
386 */
387 handle->bufsize = handle->snapshot;
388 }
389
390 /* Allocate the buffer */
391
392 handle->buffer = malloc(handle->bufsize + handle->offset);
393 if (!handle->buffer) {
394 snprintf(ebuf, PCAP_ERRBUF_SIZE,
395 "malloc: %s", pcap_strerror(errno));
396 pcap_close_linux(handle);
397 free(handle);
398 return NULL;
399 }
400
401 /*
402 * "handle->fd" is a socket, so "select()" and "poll()"
403 * should work on it.
404 */
405 handle->selectable_fd = handle->fd;
406
407 handle->read_op = pcap_read_linux;
408 handle->inject_op = pcap_inject_linux;
409 handle->setfilter_op = pcap_setfilter_linux;
410 handle->set_datalink_op = NULL; /* can't change data link type */
411 handle->getnonblock_op = pcap_getnonblock_fd;
412 handle->setnonblock_op = pcap_setnonblock_fd;
413 handle->stats_op = pcap_stats_linux;
414 handle->close_op = pcap_close_linux;
415
416 return handle;
417 }
418
419 /*
420 * Read at most max_packets from the capture stream and call the callback
421 * for each of them. Returns the number of packets handled or -1 if an
422 * error occured.
423 */
424 static int
425 pcap_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
426 {
427 /*
428 * Currently, on Linux only one packet is delivered per read,
429 * so we don't loop.
430 */
431 return pcap_read_packet(handle, callback, user);
432 }
433
434 /*
435 * Read a packet from the socket calling the handler provided by
436 * the user. Returns the number of packets received or -1 if an
437 * error occured.
438 */
439 static int
440 pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata)
441 {
442 u_char *bp;
443 int offset;
444 #ifdef HAVE_PF_PACKET_SOCKETS
445 struct sockaddr_ll from;
446 struct sll_header *hdrp;
447 #else
448 struct sockaddr from;
449 #endif
450 socklen_t fromlen;
451 int packet_len, caplen;
452 struct pcap_pkthdr pcap_header;
453
454 #ifdef HAVE_PF_PACKET_SOCKETS
455 /*
456 * If this is a cooked device, leave extra room for a
457 * fake packet header.
458 */
459 if (handle->md.cooked)
460 offset = SLL_HDR_LEN;
461 else
462 offset = 0;
463 #else
464 /*
465 * This system doesn't have PF_PACKET sockets, so it doesn't
466 * support cooked devices.
467 */
468 offset = 0;
469 #endif
470
471 /* Receive a single packet from the kernel */
472
473 bp = handle->buffer + handle->offset;
474 do {
475 /*
476 * Has "pcap_breakloop()" been called?
477 */
478 if (handle->break_loop) {
479 /*
480 * Yes - clear the flag that indicates that it
481 * has, and return -2 as an indication that we
482 * were told to break out of the loop.
483 */
484 handle->break_loop = 0;
485 return -2;
486 }
487 fromlen = sizeof(from);
488 packet_len = recvfrom(
489 handle->fd, bp + offset,
490 handle->bufsize - offset, MSG_TRUNC,
491 (struct sockaddr *) &from, &fromlen);
492 } while (packet_len == -1 && errno == EINTR);
493
494 /* Check if an error occured */
495
496 if (packet_len == -1) {
497 if (errno == EAGAIN)
498 return 0; /* no packet there */
499 else {
500 snprintf(handle->errbuf, sizeof(handle->errbuf),
501 "recvfrom: %s", pcap_strerror(errno));
502 return -1;
503 }
504 }
505
506 #ifdef HAVE_PF_PACKET_SOCKETS
507 /*
508 * If this is from the loopback device, reject outgoing packets;
509 * we'll see the packet as an incoming packet as well, and
510 * we don't want to see it twice.
511 *
512 * We can only do this if we're using PF_PACKET; the address
513 * returned for SOCK_PACKET is a "sockaddr_pkt" which lacks
514 * the relevant packet type information.
515 */
516 if (!handle->md.sock_packet &&
517 from.sll_ifindex == handle->md.lo_ifindex &&
518 from.sll_pkttype == PACKET_OUTGOING)
519 return 0;
520 #endif
521
522 #ifdef HAVE_PF_PACKET_SOCKETS
523 /*
524 * If this is a cooked device, fill in the fake packet header.
525 */
526 if (handle->md.cooked) {
527 /*
528 * Add the length of the fake header to the length
529 * of packet data we read.
530 */
531 packet_len += SLL_HDR_LEN;
532
533 hdrp = (struct sll_header *)bp;
534
535 /*
536 * Map the PACKET_ value to a LINUX_SLL_ value; we
537 * want the same numerical value to be used in
538 * the link-layer header even if the numerical values
539 * for the PACKET_ #defines change, so that programs
540 * that look at the packet type field will always be
541 * able to handle DLT_LINUX_SLL captures.
542 */
543 switch (from.sll_pkttype) {
544
545 case PACKET_HOST:
546 hdrp->sll_pkttype = htons(LINUX_SLL_HOST);
547 break;
548
549 case PACKET_BROADCAST:
550 hdrp->sll_pkttype = htons(LINUX_SLL_BROADCAST);
551 break;
552
553 case PACKET_MULTICAST:
554 hdrp->sll_pkttype = htons(LINUX_SLL_MULTICAST);
555 break;
556
557 case PACKET_OTHERHOST:
558 hdrp->sll_pkttype = htons(LINUX_SLL_OTHERHOST);
559 break;
560
561 case PACKET_OUTGOING:
562 hdrp->sll_pkttype = htons(LINUX_SLL_OUTGOING);
563 break;
564
565 default:
566 hdrp->sll_pkttype = -1;
567 break;
568 }
569
570 hdrp->sll_hatype = htons(from.sll_hatype);
571 hdrp->sll_halen = htons(from.sll_halen);
572 memcpy(hdrp->sll_addr, from.sll_addr,
573 (from.sll_halen > SLL_ADDRLEN) ?
574 SLL_ADDRLEN :
575 from.sll_halen);
576 hdrp->sll_protocol = from.sll_protocol;
577 }
578 #endif
579
580 /*
581 * XXX: According to the kernel source we should get the real
582 * packet len if calling recvfrom with MSG_TRUNC set. It does
583 * not seem to work here :(, but it is supported by this code
584 * anyway.
585 * To be honest the code RELIES on that feature so this is really
586 * broken with 2.2.x kernels.
587 * I spend a day to figure out what's going on and I found out
588 * that the following is happening:
589 *
590 * The packet comes from a random interface and the packet_rcv
591 * hook is called with a clone of the packet. That code inserts
592 * the packet into the receive queue of the packet socket.
593 * If a filter is attached to that socket that filter is run
594 * first - and there lies the problem. The default filter always
595 * cuts the packet at the snaplen:
596 *
597 * # tcpdump -d
598 * (000) ret #68
599 *
600 * So the packet filter cuts down the packet. The recvfrom call
601 * says "hey, it's only 68 bytes, it fits into the buffer" with
602 * the result that we don't get the real packet length. This
603 * is valid at least until kernel 2.2.17pre6.
604 *
605 * We currently handle this by making a copy of the filter
606 * program, fixing all "ret" instructions with non-zero
607 * operands to have an operand of 65535 so that the filter
608 * doesn't truncate the packet, and supplying that modified
609 * filter to the kernel.
610 */
611
612 caplen = packet_len;
613 if (caplen > handle->snapshot)
614 caplen = handle->snapshot;
615
616 /* Run the packet filter if not using kernel filter */
617 if (!handle->md.use_bpf && handle->fcode.bf_insns) {
618 if (bpf_filter(handle->fcode.bf_insns, bp,
619 packet_len, caplen) == 0)
620 {
621 /* rejected by filter */
622 return 0;
623 }
624 }
625
626 /* Fill in our own header data */
627
628 if (ioctl(handle->fd, SIOCGSTAMP, &pcap_header.ts) == -1) {
629 snprintf(handle->errbuf, sizeof(handle->errbuf),
630 "ioctl: %s", pcap_strerror(errno));
631 return -1;
632 }
633 pcap_header.caplen = caplen;
634 pcap_header.len = packet_len;
635
636 /*
637 * Count the packet.
638 *
639 * Arguably, we should count them before we check the filter,
640 * as on many other platforms "ps_recv" counts packets
641 * handed to the filter rather than packets that passed
642 * the filter, but if filtering is done in the kernel, we
643 * can't get a count of packets that passed the filter,
644 * and that would mean the meaning of "ps_recv" wouldn't
645 * be the same on all Linux systems.
646 *
647 * XXX - it's not the same on all systems in any case;
648 * ideally, we should have a "get the statistics" call
649 * that supplies more counts and indicates which of them
650 * it supplies, so that we supply a count of packets
651 * handed to the filter only on platforms where that
652 * information is available.
653 *
654 * We count them here even if we can get the packet count
655 * from the kernel, as we can only determine at run time
656 * whether we'll be able to get it from the kernel (if
657 * HAVE_TPACKET_STATS isn't defined, we can't get it from
658 * the kernel, but if it is defined, the library might
659 * have been built with a 2.4 or later kernel, but we
660 * might be running on a 2.2[.x] kernel without Alexey
661 * Kuznetzov's turbopacket patches, and thus the kernel
662 * might not be able to supply those statistics). We
663 * could, I guess, try, when opening the socket, to get
664 * the statistics, and if we can not increment the count
665 * here, but it's not clear that always incrementing
666 * the count is more expensive than always testing a flag
667 * in memory.
668 */
669 handle->md.stat.ps_recv++;
670
671 /* Call the user supplied callback function */
672 callback(userdata, &pcap_header, bp);
673
674 return 1;
675 }
676
677 static int
678 pcap_inject_linux(pcap_t *handle, const void *buf, size_t size)
679 {
680 #ifdef HAVE_PF_PACKET_SOCKETS
681 struct sockaddr_ll sa_ll;
682 #endif
683 struct sockaddr_pkt sa_pkt;
684 int ret;
685
686 #ifdef HAVE_PF_PACKET_SOCKETS
687 if (!handle->md.sock_packet)) {
688 /* PF_PACKET socket */
689 if (handle->md.ifindex == -1) {
690 /*
691 * Cooked mode - can't send.
692 * XXX - how do you send on a bound cooked-mode
693 * socket?
694 */
695 strlcpy(handle->errbuf,
696 "Sending packets isn't supported in cooked mode",
697 PCAP_ERRBUF_SIZE);
698 return (-1);
699 }
700
701 memset(&sa_ll, 0, sizeof(sa_ll));
702 sa_ll.sll_family = AF_PACKET;
703 sa_ll.sll_ifindex = handle->md.ifindex;
704 /*
705 * Do we have to set the hardware address?
706 */
707 sa_ll.sll_protocol = htons(ETH_P_ALL);
708
709 ret = sendto(handle->fd, buf, size, 0, &sa_ll, sizeof(sa_ll));
710 if (ret == -1) {
711 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
712 pcap_strerror(errno));
713 return (-1);
714 }
715 return (ret);
716 }
717 #endif
718 memset(&sa_pkt, 0, sizeof(sa_pkt));
719 sa_pkt.spkt_family = PF_INET;
720 strcpy(sa_pkt.spkt_device, handle->md.device);
721 /*
722 * Do we have to set "spkt_protocol" to the Ethernet protocol?
723 */
724
725 ret = sendto(handle->fd, buf, size, 0, &sa, sizeof(sa)));
726 if (ret == -1) {
727 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
728 pcap_strerror(errno));
729 return (-1);
730 }
731 return (ret);
732 }
733
734 /*
735 * Get the statistics for the given packet capture handle.
736 * Reports the number of dropped packets iff the kernel supports
737 * the PACKET_STATISTICS "getsockopt()" argument (2.4 and later
738 * kernels, and 2.2[.x] kernels with Alexey Kuznetzov's turbopacket
739 * patches); otherwise, that information isn't available, and we lie
740 * and report 0 as the count of dropped packets.
741 */
742 static int
743 pcap_stats_linux(pcap_t *handle, struct pcap_stat *stats)
744 {
745 #ifdef HAVE_TPACKET_STATS
746 struct tpacket_stats kstats;
747 socklen_t len = sizeof (struct tpacket_stats);
748 #endif
749
750 #ifdef HAVE_TPACKET_STATS
751 /*
752 * Try to get the packet counts from the kernel.
753 */
754 if (getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS,
755 &kstats, &len) > -1) {
756 /*
757 * In "linux/net/packet/af_packet.c", at least in the
758 * 2.4.9 kernel, "tp_packets" is incremented for every
759 * packet that passes the packet filter *and* is
760 * successfully queued on the socket; "tp_drops" is
761 * incremented for every packet dropped because there's
762 * not enough free space in the socket buffer.
763 *
764 * When the statistics are returned for a PACKET_STATISTICS
765 * "getsockopt()" call, "tp_drops" is added to "tp_packets",
766 * so that "tp_packets" counts all packets handed to
767 * the PF_PACKET socket, including packets dropped because
768 * there wasn't room on the socket buffer - but not
769 * including packets that didn't pass the filter.
770 *
771 * In the BSD BPF, the count of received packets is
772 * incremented for every packet handed to BPF, regardless
773 * of whether it passed the filter.
774 *
775 * We can't make "pcap_stats()" work the same on both
776 * platforms, but the best approximation is to return
777 * "tp_packets" as the count of packets and "tp_drops"
778 * as the count of drops.
779 *
780 * Keep a running total because each call to
781 * getsockopt(handle->fd, SOL_PACKET, PACKET_STATISTICS, ....
782 * resets the counters to zero.
783 */
784 handle->md.stat.ps_recv += kstats.tp_packets;
785 handle->md.stat.ps_drop += kstats.tp_drops;
786 }
787 else
788 {
789 /*
790 * If the error was EOPNOTSUPP, fall through, so that
791 * if you build the library on a system with
792 * "struct tpacket_stats" and run it on a system
793 * that doesn't, it works as it does if the library
794 * is built on a system without "struct tpacket_stats".
795 */
796 if (errno != EOPNOTSUPP) {
797 snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
798 "pcap_stats: %s", pcap_strerror(errno));
799 return -1;
800 }
801 }
802 #endif
803 /*
804 * On systems where the PACKET_STATISTICS "getsockopt()" argument
805 * is supported on PF_PACKET sockets:
806 *
807 * "ps_recv" counts only packets that *passed* the filter,
808 * not packets that didn't pass the filter. This includes
809 * packets later dropped because we ran out of buffer space.
810 *
811 * "ps_drop" counts packets dropped because we ran out of
812 * buffer space. It doesn't count packets dropped by the
813 * interface driver. It counts only packets that passed
814 * the filter.
815 *
816 * Both statistics include packets not yet read from the
817 * kernel by libpcap, and thus not yet seen by the application.
818 *
819 * On systems where the PACKET_STATISTICS "getsockopt()" argument
820 * is not supported on PF_PACKET sockets:
821 *
822 * "ps_recv" counts only packets that *passed* the filter,
823 * not packets that didn't pass the filter. It does not
824 * count packets dropped because we ran out of buffer
825 * space.
826 *
827 * "ps_drop" is not supported.
828 *
829 * "ps_recv" doesn't include packets not yet read from
830 * the kernel by libpcap.
831 */
832 *stats = handle->md.stat;
833 return 0;
834 }
835
836 /*
837 * Description string for the "any" device.
838 */
839 static const char any_descr[] = "Pseudo-device that captures on all interfaces";
840
841 int
842 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
843 {
844 if (pcap_add_if(alldevsp, "any", 0, any_descr, errbuf) < 0)
845 return (-1);
846
847 #ifdef HAVE_DAG_API
848 if (dag_platform_finddevs(alldevsp, errbuf) < 0)
849 return (-1);
850 #endif /* HAVE_DAG_API */
851
852 return (0);
853 }
854
855 /*
856 * Attach the given BPF code to the packet capture device.
857 */
858 static int
859 pcap_setfilter_linux(pcap_t *handle, struct bpf_program *filter)
860 {
861 #ifdef SO_ATTACH_FILTER
862 struct sock_fprog fcode;
863 int can_filter_in_kernel;
864 int err = 0;
865 #endif
866
867 if (!handle)
868 return -1;
869 if (!filter) {
870 strncpy(handle->errbuf, "setfilter: No filter specified",
871 sizeof(handle->errbuf));
872 return -1;
873 }
874
875 /* Make our private copy of the filter */
876
877 if (install_bpf_program(handle, filter) < 0)
878 /* install_bpf_program() filled in errbuf */
879 return -1;
880
881 /*
882 * Run user level packet filter by default. Will be overriden if
883 * installing a kernel filter succeeds.
884 */
885 handle->md.use_bpf = 0;
886
887 /* Install kernel level filter if possible */
888
889 #ifdef SO_ATTACH_FILTER
890 #ifdef USHRT_MAX
891 if (handle->fcode.bf_len > USHRT_MAX) {
892 /*
893 * fcode.len is an unsigned short for current kernel.
894 * I have yet to see BPF-Code with that much
895 * instructions but still it is possible. So for the
896 * sake of correctness I added this check.
897 */
898 fprintf(stderr, "Warning: Filter too complex for kernel\n");
899 fcode.filter = NULL;
900 can_filter_in_kernel = 0;
901 } else
902 #endif /* USHRT_MAX */
903 {
904 /*
905 * Oh joy, the Linux kernel uses struct sock_fprog instead
906 * of struct bpf_program and of course the length field is
907 * of different size. Pointed out by Sebastian
908 *
909 * Oh, and we also need to fix it up so that all "ret"
910 * instructions with non-zero operands have 65535 as the
911 * operand, and so that, if we're in cooked mode, all
912 * memory-reference instructions use special magic offsets
913 * in references to the link-layer header and assume that
914 * the link-layer payload begins at 0; "fix_program()"
915 * will do that.
916 */
917 switch (fix_program(handle, &fcode)) {
918
919 case -1:
920 default:
921 /*
922 * Fatal error; just quit.
923 * (The "default" case shouldn't happen; we
924 * return -1 for that reason.)
925 */
926 return -1;
927
928 case 0:
929 /*
930 * The program performed checks that we can't make
931 * work in the kernel.
932 */
933 can_filter_in_kernel = 0;
934 break;
935
936 case 1:
937 /*
938 * We have a filter that'll work in the kernel.
939 */
940 can_filter_in_kernel = 1;
941 break;
942 }
943 }
944
945 if (can_filter_in_kernel) {
946 if ((err = set_kernel_filter(handle, &fcode)) == 0)
947 {
948 /* Installation succeded - using kernel filter. */
949 handle->md.use_bpf = 1;
950 }
951 else if (err == -1) /* Non-fatal error */
952 {
953 /*
954 * Print a warning if we weren't able to install
955 * the filter for a reason other than "this kernel
956 * isn't configured to support socket filters.
957 */
958 if (errno != ENOPROTOOPT && errno != EOPNOTSUPP) {
959 fprintf(stderr,
960 "Warning: Kernel filter failed: %s\n",
961 pcap_strerror(errno));
962 }
963 }
964 }
965
966 /*
967 * If we're not using the kernel filter, get rid of any kernel
968 * filter that might've been there before, e.g. because the
969 * previous filter could work in the kernel, or because some other
970 * code attached a filter to the socket by some means other than
971 * calling "pcap_setfilter()". Otherwise, the kernel filter may
972 * filter out packets that would pass the new userland filter.
973 */
974 if (!handle->md.use_bpf)
975 reset_kernel_filter(handle);
976
977 /*
978 * Free up the copy of the filter that was made by "fix_program()".
979 */
980 if (fcode.filter != NULL)
981 free(fcode.filter);
982
983 if (err == -2)
984 /* Fatal error */
985 return -1;
986 #endif /* SO_ATTACH_FILTER */
987
988 return 0;
989 }
990
991 /*
992 * Linux uses the ARP hardware type to identify the type of an
993 * interface. pcap uses the DLT_xxx constants for this. This
994 * function takes a pointer to a "pcap_t", and an ARPHRD_xxx
995 * constant, as arguments, and sets "handle->linktype" to the
996 * appropriate DLT_XXX constant and sets "handle->offset" to
997 * the appropriate value (to make "handle->offset" plus link-layer
998 * header length be a multiple of 4, so that the link-layer payload
999 * will be aligned on a 4-byte boundary when capturing packets).
1000 * (If the offset isn't set here, it'll be 0; add code as appropriate
1001 * for cases where it shouldn't be 0.)
1002 *
1003 * If "cooked_ok" is non-zero, we can use DLT_LINUX_SLL and capture
1004 * in cooked mode; otherwise, we can't use cooked mode, so we have
1005 * to pick some type that works in raw mode, or fail.
1006 *
1007 * Sets the link type to -1 if unable to map the type.
1008 */
1009 static void map_arphrd_to_dlt(pcap_t *handle, int arptype, int cooked_ok)
1010 {
1011 switch (arptype) {
1012
1013 case ARPHRD_ETHER:
1014 /*
1015 * This is (presumably) a real Ethernet capture; give it a
1016 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1017 * that an application can let you choose it, in case you're
1018 * capturing DOCSIS traffic that a Cisco Cable Modem
1019 * Termination System is putting out onto an Ethernet (it
1020 * doesn't put an Ethernet header onto the wire, it puts raw
1021 * DOCSIS frames out on the wire inside the low-level
1022 * Ethernet framing).
1023 *
1024 * XXX - are there any sorts of "fake Ethernet" that have
1025 * ARPHRD_ETHER but that *shouldn't offer DLT_DOCSIS as
1026 * a Cisco CMTS won't put traffic onto it or get traffic
1027 * bridged onto it? ISDN is handled in "live_open_new()",
1028 * as we fall back on cooked mode there; are there any
1029 * others?
1030 */
1031 handle->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
1032 /*
1033 * If that fails, just leave the list empty.
1034 */
1035 if (handle->dlt_list != NULL) {
1036 handle->dlt_list[0] = DLT_EN10MB;
1037 handle->dlt_list[1] = DLT_DOCSIS;
1038 handle->dlt_count = 2;
1039 }
1040 /* FALLTHROUGH */
1041
1042 case ARPHRD_METRICOM:
1043 case ARPHRD_LOOPBACK:
1044 handle->linktype = DLT_EN10MB;
1045 handle->offset = 2;
1046 break;
1047
1048 case ARPHRD_EETHER:
1049 handle->linktype = DLT_EN3MB;
1050 break;
1051
1052 case ARPHRD_AX25:
1053 handle->linktype = DLT_AX25;
1054 break;
1055
1056 case ARPHRD_PRONET:
1057 handle->linktype = DLT_PRONET;
1058 break;
1059
1060 case ARPHRD_CHAOS:
1061 handle->linktype = DLT_CHAOS;
1062 break;
1063
1064 #ifndef ARPHRD_IEEE802_TR
1065 #define ARPHRD_IEEE802_TR 800 /* From Linux 2.4 */
1066 #endif
1067 case ARPHRD_IEEE802_TR:
1068 case ARPHRD_IEEE802:
1069 handle->linktype = DLT_IEEE802;
1070 handle->offset = 2;
1071 break;
1072
1073 case ARPHRD_ARCNET:
1074 handle->linktype = DLT_ARCNET_LINUX;
1075 break;
1076
1077 #ifndef ARPHRD_FDDI /* From Linux 2.2.13 */
1078 #define ARPHRD_FDDI 774
1079 #endif
1080 case ARPHRD_FDDI:
1081 handle->linktype = DLT_FDDI;
1082 handle->offset = 3;
1083 break;
1084
1085 #ifndef ARPHRD_ATM /* FIXME: How to #include this? */
1086 #define ARPHRD_ATM 19
1087 #endif
1088 case ARPHRD_ATM:
1089 /*
1090 * The Classical IP implementation in ATM for Linux
1091 * supports both what RFC 1483 calls "LLC Encapsulation",
1092 * in which each packet has an LLC header, possibly
1093 * with a SNAP header as well, prepended to it, and
1094 * what RFC 1483 calls "VC Based Multiplexing", in which
1095 * different virtual circuits carry different network
1096 * layer protocols, and no header is prepended to packets.
1097 *
1098 * They both have an ARPHRD_ type of ARPHRD_ATM, so
1099 * you can't use the ARPHRD_ type to find out whether
1100 * captured packets will have an LLC header, and,
1101 * while there's a socket ioctl to *set* the encapsulation
1102 * type, there's no ioctl to *get* the encapsulation type.
1103 *
1104 * This means that
1105 *
1106 * programs that dissect Linux Classical IP frames
1107 * would have to check for an LLC header and,
1108 * depending on whether they see one or not, dissect
1109 * the frame as LLC-encapsulated or as raw IP (I
1110 * don't know whether there's any traffic other than
1111 * IP that would show up on the socket, or whether
1112 * there's any support for IPv6 in the Linux
1113 * Classical IP code);
1114 *
1115 * filter expressions would have to compile into
1116 * code that checks for an LLC header and does
1117 * the right thing.
1118 *
1119 * Both of those are a nuisance - and, at least on systems
1120 * that support PF_PACKET sockets, we don't have to put
1121 * up with those nuisances; instead, we can just capture
1122 * in cooked mode. That's what we'll do, if we can.
1123 * Otherwise, we'll just fail.
1124 */
1125 if (cooked_ok)
1126 handle->linktype = DLT_LINUX_SLL;
1127 else
1128 handle->linktype = -1;
1129 break;
1130
1131 #ifndef ARPHRD_IEEE80211 /* From Linux 2.4.6 */
1132 #define ARPHRD_IEEE80211 801
1133 #endif
1134 case ARPHRD_IEEE80211:
1135 handle->linktype = DLT_IEEE802_11;
1136 break;
1137
1138 #ifndef ARPHRD_IEEE80211_PRISM /* From Linux 2.4.18 */
1139 #define ARPHRD_IEEE80211_PRISM 802
1140 #endif
1141 case ARPHRD_IEEE80211_PRISM:
1142 handle->linktype = DLT_PRISM_HEADER;
1143 break;
1144
1145 case ARPHRD_PPP:
1146 /*
1147 * Some PPP code in the kernel supplies no link-layer
1148 * header whatsoever to PF_PACKET sockets; other PPP
1149 * code supplies PPP link-layer headers ("syncppp.c");
1150 * some PPP code might supply random link-layer
1151 * headers (PPP over ISDN - there's code in Ethereal,
1152 * for example, to cope with PPP-over-ISDN captures
1153 * with which the Ethereal developers have had to cope,
1154 * heuristically trying to determine which of the
1155 * oddball link-layer headers particular packets have).
1156 *
1157 * As such, we just punt, and run all PPP interfaces
1158 * in cooked mode, if we can; otherwise, we just treat
1159 * it as DLT_RAW, for now - if somebody needs to capture,
1160 * on a 2.0[.x] kernel, on PPP devices that supply a
1161 * link-layer header, they'll have to add code here to
1162 * map to the appropriate DLT_ type (possibly adding a
1163 * new DLT_ type, if necessary).
1164 */
1165 if (cooked_ok)
1166 handle->linktype = DLT_LINUX_SLL;
1167 else {
1168 /*
1169 * XXX - handle ISDN types here? We can't fall
1170 * back on cooked sockets, so we'd have to
1171 * figure out from the device name what type of
1172 * link-layer encapsulation it's using, and map
1173 * that to an appropriate DLT_ value, meaning
1174 * we'd map "isdnN" devices to DLT_RAW (they
1175 * supply raw IP packets with no link-layer
1176 * header) and "isdY" devices to a new DLT_I4L_IP
1177 * type that has only an Ethernet packet type as
1178 * a link-layer header.
1179 *
1180 * But sometimes we seem to get random crap
1181 * in the link-layer header when capturing on
1182 * ISDN devices....
1183 */
1184 handle->linktype = DLT_RAW;
1185 }
1186 break;
1187
1188 #ifndef ARPHRD_CISCO
1189 #define ARPHRD_CISCO 513 /* previously ARPHRD_HDLC */
1190 #endif
1191 case ARPHRD_CISCO:
1192 handle->linktype = DLT_C_HDLC;
1193 break;
1194
1195 /* Not sure if this is correct for all tunnels, but it
1196 * works for CIPE */
1197 case ARPHRD_TUNNEL:
1198 #ifndef ARPHRD_SIT
1199 #define ARPHRD_SIT 776 /* From Linux 2.2.13 */
1200 #endif
1201 case ARPHRD_SIT:
1202 case ARPHRD_CSLIP:
1203 case ARPHRD_SLIP6:
1204 case ARPHRD_CSLIP6:
1205 case ARPHRD_ADAPT:
1206 case ARPHRD_SLIP:
1207 #ifndef ARPHRD_RAWHDLC
1208 #define ARPHRD_RAWHDLC 518
1209 #endif
1210 case ARPHRD_RAWHDLC:
1211 #ifndef ARPHRD_DLCI
1212 #define ARPHRD_DLCI 15
1213 #endif
1214 case ARPHRD_DLCI:
1215 /*
1216 * XXX - should some of those be mapped to DLT_LINUX_SLL
1217 * instead? Should we just map all of them to DLT_LINUX_SLL?
1218 */
1219 handle->linktype = DLT_RAW;
1220 break;
1221
1222 #ifndef ARPHRD_FRAD
1223 #define ARPHRD_FRAD 770
1224 #endif
1225 case ARPHRD_FRAD:
1226 handle->linktype = DLT_FRELAY;
1227 break;
1228
1229 case ARPHRD_LOCALTLK:
1230 handle->linktype = DLT_LTALK;
1231 break;
1232
1233 #ifndef ARPHRD_FCPP
1234 #define ARPHRD_FCPP 784
1235 #endif
1236 case ARPHRD_FCPP:
1237 #ifndef ARPHRD_FCAL
1238 #define ARPHRD_FCAL 785
1239 #endif
1240 case ARPHRD_FCAL:
1241 #ifndef ARPHRD_FCPL
1242 #define ARPHRD_FCPL 786
1243 #endif
1244 case ARPHRD_FCPL:
1245 #ifndef ARPHRD_FCFABRIC
1246 #define ARPHRD_FCFABRIC 787
1247 #endif
1248 case ARPHRD_FCFABRIC:
1249 /*
1250 * We assume that those all mean RFC 2625 IP-over-
1251 * Fibre Channel, with the RFC 2625 header at
1252 * the beginning of the packet.
1253 */
1254 handle->linktype = DLT_IP_OVER_FC;
1255 break;
1256
1257 case ARPHRD_IRDA:
1258 /* Don't expect IP packet out of this interfaces... */
1259 handle->linktype = DLT_LINUX_IRDA;
1260 /* We need to save packet direction for IrDA decoding,
1261 * so let's use "Linux-cooked" mode. Jean II */
1262 //handle->md.cooked = 1;
1263 break;
1264
1265 default:
1266 handle->linktype = -1;
1267 break;
1268 }
1269 }
1270
1271 /* ===== Functions to interface to the newer kernels ================== */
1272
1273 /*
1274 * Try to open a packet socket using the new kernel interface.
1275 * Returns 0 on failure.
1276 * FIXME: 0 uses to mean success (Sebastian)
1277 */
1278 static int
1279 live_open_new(pcap_t *handle, const char *device, int promisc,
1280 int to_ms, char *ebuf)
1281 {
1282 #ifdef HAVE_PF_PACKET_SOCKETS
1283 int sock_fd = -1, arptype;
1284 int err;
1285 int fatal_err = 0;
1286 struct packet_mreq mr;
1287
1288 /* One shot loop used for error handling - bail out with break */
1289
1290 do {
1291 /*
1292 * Open a socket with protocol family packet. If a device is
1293 * given we try to open it in raw mode otherwise we use
1294 * the cooked interface.
1295 */
1296 sock_fd = device ?
1297 socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
1298 : socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
1299
1300 if (sock_fd == -1) {
1301 snprintf(ebuf, PCAP_ERRBUF_SIZE, "socket: %s",
1302 pcap_strerror(errno) );
1303 break;
1304 }
1305
1306 /* It seems the kernel supports the new interface. */
1307 handle->md.sock_packet = 0;
1308
1309 /*
1310 * Get the interface index of the loopback device.
1311 * If the attempt fails, don't fail, just set the
1312 * "md.lo_ifindex" to -1.
1313 *
1314 * XXX - can there be more than one device that loops
1315 * packets back, i.e. devices other than "lo"? If so,
1316 * we'd need to find them all, and have an array of
1317 * indices for them, and check all of them in
1318 * "pcap_read_packet()".
1319 */
1320 handle->md.lo_ifindex = iface_get_id(sock_fd, "lo", ebuf);
1321
1322 /*
1323 * Default value for offset to align link-layer payload
1324 * on a 4-byte boundary.
1325 */
1326 handle->offset = 0;
1327
1328 /*
1329 * What kind of frames do we have to deal with? Fall back
1330 * to cooked mode if we have an unknown interface type.
1331 */
1332
1333 if (device) {
1334 /* Assume for now we don't need cooked mode. */
1335 handle->md.cooked = 0;
1336
1337 arptype = iface_get_arptype(sock_fd, device, ebuf);
1338 if (arptype == -1) {
1339 fatal_err = 1;
1340 break;
1341 }
1342 map_arphrd_to_dlt(handle, arptype, 1);
1343 if (handle->linktype == -1 ||
1344 handle->linktype == DLT_LINUX_SLL ||
1345 handle->linktype == DLT_LINUX_IRDA ||
1346 (handle->linktype == DLT_EN10MB &&
1347 (strncmp("isdn", device, 4) == 0 ||
1348 strncmp("isdY", device, 4) == 0))) {
1349 /*
1350 * Unknown interface type (-1), or a
1351 * device we explicitly chose to run
1352 * in cooked mode (e.g., PPP devices),
1353 * or an ISDN device (whose link-layer
1354 * type we can only determine by using
1355 * APIs that may be different on different
1356 * kernels) - reopen in cooked mode.
1357 */
1358 if (close(sock_fd) == -1) {
1359 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1360 "close: %s", pcap_strerror(errno));
1361 break;
1362 }
1363 sock_fd = socket(PF_PACKET, SOCK_DGRAM,
1364 htons(ETH_P_ALL));
1365 if (sock_fd == -1) {
1366 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1367 "socket: %s", pcap_strerror(errno));
1368 break;
1369 }
1370 handle->md.cooked = 1;
1371
1372 /*
1373 * Get rid of any link-layer type list
1374 * we allocated - this only supports cooked
1375 * capture.
1376 */
1377 if (handle->dlt_list != NULL) {
1378 free(handle->dlt_list);
1379 handle->dlt_list = NULL;
1380 handle->dlt_count = 0;
1381 }
1382
1383 if (handle->linktype == -1) {
1384 /*
1385 * Warn that we're falling back on
1386 * cooked mode; we may want to
1387 * update "map_arphrd_to_dlt()"
1388 * to handle the new type.
1389 */
1390 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1391 "arptype %d not "
1392 "supported by libpcap - "
1393 "falling back to cooked "
1394 "socket",
1395 arptype);
1396 }
1397 /* IrDA capture is not a real "cooked" capture,
1398 * it's IrLAP frames, not IP packets. */
1399 if (handle->linktype != DLT_LINUX_IRDA)
1400 handle->linktype = DLT_LINUX_SLL;
1401 }
1402
1403 handle->md.ifindex = iface_get_id(sock_fd, device, ebuf);
1404 if (handle->md.ifindex == -1)
1405 break;
1406
1407 if ((err = iface_bind(sock_fd, handle->md.ifindex,
1408 ebuf)) < 0) {
1409 if (err == -2)
1410 fatal_err = 1;
1411 break;
1412 }
1413 } else {
1414 /*
1415 * This is cooked mode.
1416 */
1417 handle->md.cooked = 1;
1418 handle->linktype = DLT_LINUX_SLL;
1419
1420 /*
1421 * We're not bound to a device.
1422 * XXX - true? Or true only if we're using
1423 * the "any" device?
1424 * For now, we're using this as an indication
1425 * that we can't transmit; stop doing that only
1426 * if we figure out how to transmit in cooked
1427 * mode.
1428 */
1429 handle->md.ifindex = -1;
1430 }
1431
1432 /*
1433 * Select promiscuous mode on if "promisc" is set.
1434 *
1435 * Do not turn allmulti mode on if we don't select
1436 * promiscuous mode - on some devices (e.g., Orinoco
1437 * wireless interfaces), allmulti mode isn't supported
1438 * and the driver implements it by turning promiscuous
1439 * mode on, and that screws up the operation of the
1440 * card as a normal networking interface, and on no
1441 * other platform I know of does starting a non-
1442 * promiscuous capture affect which multicast packets
1443 * are received by the interface.
1444 */
1445
1446 /*
1447 * Hmm, how can we set promiscuous mode on all interfaces?
1448 * I am not sure if that is possible at all.
1449 */
1450
1451 if (device && promisc) {
1452 memset(&mr, 0, sizeof(mr));
1453 mr.mr_ifindex = handle->md.ifindex;
1454 mr.mr_type = PACKET_MR_PROMISC;
1455 if (setsockopt(sock_fd, SOL_PACKET,
1456 PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == -1)
1457 {
1458 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1459 "setsockopt: %s", pcap_strerror(errno));
1460 break;
1461 }
1462 }
1463
1464 /* Save the socket FD in the pcap structure */
1465
1466 handle->fd = sock_fd;
1467
1468 return 1;
1469
1470 } while(0);
1471
1472 if (sock_fd != -1)
1473 close(sock_fd);
1474
1475 if (fatal_err) {
1476 /*
1477 * Get rid of any link-layer type list we allocated.
1478 */
1479 if (handle->dlt_list != NULL)
1480 free(handle->dlt_list);
1481 return -2;
1482 } else
1483 return 0;
1484 #else
1485 strncpy(ebuf,
1486 "New packet capturing interface not supported by build "
1487 "environment", PCAP_ERRBUF_SIZE);
1488 return 0;
1489 #endif
1490 }
1491
1492 #ifdef HAVE_PF_PACKET_SOCKETS
1493 /*
1494 * Return the index of the given device name. Fill ebuf and return
1495 * -1 on failure.
1496 */
1497 static int
1498 iface_get_id(int fd, const char *device, char *ebuf)
1499 {
1500 struct ifreq ifr;
1501
1502 memset(&ifr, 0, sizeof(ifr));
1503 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1504
1505 if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
1506 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1507 "ioctl: %s", pcap_strerror(errno));
1508 return -1;
1509 }
1510
1511 return ifr.ifr_ifindex;
1512 }
1513
1514 /*
1515 * Bind the socket associated with FD to the given device.
1516 */
1517 static int
1518 iface_bind(int fd, int ifindex, char *ebuf)
1519 {
1520 struct sockaddr_ll sll;
1521 int err;
1522 socklen_t errlen = sizeof(err);
1523
1524 memset(&sll, 0, sizeof(sll));
1525 sll.sll_family = AF_PACKET;
1526 sll.sll_ifindex = ifindex;
1527 sll.sll_protocol = htons(ETH_P_ALL);
1528
1529 if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
1530 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1531 "bind: %s", pcap_strerror(errno));
1532 return -1;
1533 }
1534
1535 /* Any pending errors, e.g., network is down? */
1536
1537 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
1538 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1539 "getsockopt: %s", pcap_strerror(errno));
1540 return -2;
1541 }
1542
1543 if (err > 0) {
1544 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1545 "bind: %s", pcap_strerror(err));
1546 return -2;
1547 }
1548
1549 return 0;
1550 }
1551
1552 #endif
1553
1554
1555 /* ===== Functions to interface to the older kernels ================== */
1556
1557 /*
1558 * With older kernels promiscuous mode is kind of interesting because we
1559 * have to reset the interface before exiting. The problem can't really
1560 * be solved without some daemon taking care of managing usage counts.
1561 * If we put the interface into promiscuous mode, we set a flag indicating
1562 * that we must take it out of that mode when the interface is closed,
1563 * and, when closing the interface, if that flag is set we take it out
1564 * of promiscuous mode.
1565 */
1566
1567 /*
1568 * List of pcaps for which we turned promiscuous mode on by hand.
1569 * If there are any such pcaps, we arrange to call "pcap_close_all()"
1570 * when we exit, and have it close all of them to turn promiscuous mode
1571 * off.
1572 */
1573 static struct pcap *pcaps_to_close;
1574
1575 /*
1576 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
1577 * be called on exit.
1578 */
1579 static int did_atexit;
1580
1581 static void pcap_close_all(void)
1582 {
1583 struct pcap *handle;
1584
1585 while ((handle = pcaps_to_close) != NULL)
1586 pcap_close(handle);
1587 }
1588
1589 static void pcap_close_linux( pcap_t *handle )
1590 {
1591 struct pcap *p, *prevp;
1592 struct ifreq ifr;
1593
1594 if (handle->md.clear_promisc) {
1595 /*
1596 * We put the interface into promiscuous mode; take
1597 * it out of promiscuous mode.
1598 *
1599 * XXX - if somebody else wants it in promiscuous mode,
1600 * this code cannot know that, so it'll take it out
1601 * of promiscuous mode. That's not fixable in 2.0[.x]
1602 * kernels.
1603 */
1604 memset(&ifr, 0, sizeof(ifr));
1605 strncpy(ifr.ifr_name, handle->md.device, sizeof(ifr.ifr_name));
1606 if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
1607 fprintf(stderr,
1608 "Can't restore interface flags (SIOCGIFFLAGS failed: %s).\n"
1609 "Please adjust manually.\n"
1610 "Hint: This can't happen with Linux >= 2.2.0.\n",
1611 strerror(errno));
1612 } else {
1613 if (ifr.ifr_flags & IFF_PROMISC) {
1614 /*
1615 * Promiscuous mode is currently on; turn it
1616 * off.
1617 */
1618 ifr.ifr_flags &= ~IFF_PROMISC;
1619 if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
1620 fprintf(stderr,
1621 "Can't restore interface flags (SIOCSIFFLAGS failed: %s).\n"
1622 "Please adjust manually.\n"
1623 "Hint: This can't happen with Linux >= 2.2.0.\n",
1624 strerror(errno));
1625 }
1626 }
1627 }
1628
1629 /*
1630 * Take this pcap out of the list of pcaps for which we
1631 * have to take the interface out of promiscuous mode.
1632 */
1633 for (p = pcaps_to_close, prevp = NULL; p != NULL;
1634 prevp = p, p = p->md.next) {
1635 if (p == handle) {
1636 /*
1637 * Found it. Remove it from the list.
1638 */
1639 if (prevp == NULL) {
1640 /*
1641 * It was at the head of the list.
1642 */
1643 pcaps_to_close = p->md.next;
1644 } else {
1645 /*
1646 * It was in the middle of the list.
1647 */
1648 prevp->md.next = p->md.next;
1649 }
1650 break;
1651 }
1652 }
1653 }
1654
1655 if (handle->md.device != NULL)
1656 free(handle->md.device);
1657 handle->md.device = NULL;
1658 if (handle->buffer != NULL)
1659 free(handle->buffer);
1660 if (handle->fd >= 0)
1661 close(handle->fd);
1662 }
1663
1664 /*
1665 * Try to open a packet socket using the old kernel interface.
1666 * Returns 0 on failure.
1667 * FIXME: 0 uses to mean success (Sebastian)
1668 */
1669 static int
1670 live_open_old(pcap_t *handle, const char *device, int promisc,
1671 int to_ms, char *ebuf)
1672 {
1673 int arptype;
1674 struct ifreq ifr;
1675
1676 do {
1677 /* Open the socket */
1678
1679 handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
1680 if (handle->fd == -1) {
1681 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1682 "socket: %s", pcap_strerror(errno));
1683 break;
1684 }
1685
1686 /* It worked - we are using the old interface */
1687 handle->md.sock_packet = 1;
1688
1689 /* ...which means we get the link-layer header. */
1690 handle->md.cooked = 0;
1691
1692 /* Bind to the given device */
1693
1694 if (!device) {
1695 strncpy(ebuf, "pcap_open_live: The \"any\" device isn't supported on 2.0[.x]-kernel systems",
1696 PCAP_ERRBUF_SIZE);
1697 break;
1698 }
1699 if (iface_bind_old(handle->fd, device, ebuf) == -1)
1700 break;
1701
1702 /*
1703 * Try to get the link-layer type.
1704 */
1705 arptype = iface_get_arptype(handle->fd, device, ebuf);
1706 if (arptype == -1)
1707 break;
1708
1709 /*
1710 * Try to find the DLT_ type corresponding to that
1711 * link-layer type.
1712 */
1713 map_arphrd_to_dlt(handle, arptype, 0);
1714 if (handle->linktype == -1) {
1715 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1716 "unknown arptype %d", arptype);
1717 break;
1718 }
1719
1720 /* Go to promisc mode if requested */
1721
1722 if (promisc) {
1723 memset(&ifr, 0, sizeof(ifr));
1724 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1725 if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
1726 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1727 "ioctl: %s", pcap_strerror(errno));
1728 break;
1729 }
1730 if ((ifr.ifr_flags & IFF_PROMISC) == 0) {
1731 /*
1732 * Promiscuous mode isn't currently on,
1733 * so turn it on, and remember that
1734 * we should turn it off when the
1735 * pcap_t is closed.
1736 */
1737
1738 /*
1739 * If we haven't already done so, arrange
1740 * to have "pcap_close_all()" called when
1741 * we exit.
1742 */
1743 if (!did_atexit) {
1744 if (atexit(pcap_close_all) == -1) {
1745 /*
1746 * "atexit()" failed; don't
1747 * put the interface in
1748 * promiscuous mode, just
1749 * give up.
1750 */
1751 strncpy(ebuf, "atexit failed",
1752 PCAP_ERRBUF_SIZE);
1753 break;
1754 }
1755 did_atexit = 1;
1756 }
1757
1758 ifr.ifr_flags |= IFF_PROMISC;
1759 if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
1760 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1761 "ioctl: %s",
1762 pcap_strerror(errno));
1763 break;
1764 }
1765 handle->md.clear_promisc = 1;
1766
1767 /*
1768 * Add this to the list of pcaps
1769 * to close when we exit.
1770 */
1771 handle->md.next = pcaps_to_close;
1772 pcaps_to_close = handle;
1773 }
1774 }
1775
1776 /*
1777 * Default value for offset to align link-layer payload
1778 * on a 4-byte boundary.
1779 */
1780 handle->offset = 0;
1781
1782 return 1;
1783
1784 } while (0);
1785
1786 pcap_close_linux(handle);
1787 return 0;
1788 }
1789
1790 /*
1791 * Bind the socket associated with FD to the given device using the
1792 * interface of the old kernels.
1793 */
1794 static int
1795 iface_bind_old(int fd, const char *device, char *ebuf)
1796 {
1797 struct sockaddr saddr;
1798 int err;
1799 socklen_t errlen = sizeof(err);
1800
1801 memset(&saddr, 0, sizeof(saddr));
1802 strncpy(saddr.sa_data, device, sizeof(saddr.sa_data));
1803 if (bind(fd, &saddr, sizeof(saddr)) == -1) {
1804 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1805 "bind: %s", pcap_strerror(errno));
1806 return -1;
1807 }
1808
1809 /* Any pending errors, e.g., network is down? */
1810
1811 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
1812 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1813 "getsockopt: %s", pcap_strerror(errno));
1814 return -1;
1815 }
1816
1817 if (err > 0) {
1818 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1819 "bind: %s", pcap_strerror(err));
1820 return -1;
1821 }
1822
1823 return 0;
1824 }
1825
1826
1827 /* ===== System calls available on all supported kernels ============== */
1828
1829 /*
1830 * Query the kernel for the MTU of the given interface.
1831 */
1832 static int
1833 iface_get_mtu(int fd, const char *device, char *ebuf)
1834 {
1835 struct ifreq ifr;
1836
1837 if (!device)
1838 return BIGGER_THAN_ALL_MTUS;
1839
1840 memset(&ifr, 0, sizeof(ifr));
1841 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1842
1843 if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) {
1844 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1845 "ioctl: %s", pcap_strerror(errno));
1846 return -1;
1847 }
1848
1849 return ifr.ifr_mtu;
1850 }
1851
1852 /*
1853 * Get the hardware type of the given interface as ARPHRD_xxx constant.
1854 */
1855 static int
1856 iface_get_arptype(int fd, const char *device, char *ebuf)
1857 {
1858 struct ifreq ifr;
1859
1860 memset(&ifr, 0, sizeof(ifr));
1861 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
1862
1863 if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
1864 snprintf(ebuf, PCAP_ERRBUF_SIZE,
1865 "ioctl: %s", pcap_strerror(errno));
1866 return -1;
1867 }
1868
1869 return ifr.ifr_hwaddr.sa_family;
1870 }
1871
1872 #ifdef SO_ATTACH_FILTER
1873 static int
1874 fix_program(pcap_t *handle, struct sock_fprog *fcode)
1875 {
1876 size_t prog_size;
1877 register int i;
1878 register struct bpf_insn *p;
1879 struct bpf_insn *f;
1880 int len;
1881
1882 /*
1883 * Make a copy of the filter, and modify that copy if
1884 * necessary.
1885 */
1886 prog_size = sizeof(*handle->fcode.bf_insns) * handle->fcode.bf_len;
1887 len = handle->fcode.bf_len;
1888 f = (struct bpf_insn *)malloc(prog_size);
1889 if (f == NULL) {
1890 snprintf(handle->errbuf, sizeof(handle->errbuf),
1891 "malloc: %s", pcap_strerror(errno));
1892 return -1;
1893 }
1894 memcpy(f, handle->fcode.bf_insns, prog_size);
1895 fcode->len = len;
1896 fcode->filter = (struct sock_filter *) f;
1897
1898 for (i = 0; i < len; ++i) {
1899 p = &f[i];
1900 /*
1901 * What type of instruction is this?
1902 */
1903 switch (BPF_CLASS(p->code)) {
1904
1905 case BPF_RET:
1906 /*
1907 * It's a return instruction; is the snapshot
1908 * length a constant, rather than the contents
1909 * of the accumulator?
1910 */
1911 if (BPF_MODE(p->code) == BPF_K) {
1912 /*
1913 * Yes - if the value to be returned,
1914 * i.e. the snapshot length, is anything
1915 * other than 0, make it 65535, so that
1916 * the packet is truncated by "recvfrom()",
1917 * not by the filter.
1918 *
1919 * XXX - there's nothing we can easily do
1920 * if it's getting the value from the
1921 * accumulator; we'd have to insert
1922 * code to force non-zero values to be
1923 * 65535.
1924 */
1925 if (p->k != 0)
1926 p->k = 65535;
1927 }
1928 break;
1929
1930 case BPF_LD:
1931 case BPF_LDX:
1932 /*
1933 * It's a load instruction; is it loading
1934 * from the packet?
1935 */
1936 switch (BPF_MODE(p->code)) {
1937
1938 case BPF_ABS:
1939 case BPF_IND:
1940 case BPF_MSH:
1941 /*
1942 * Yes; are we in cooked mode?
1943 */
1944 if (handle->md.cooked) {
1945 /*
1946 * Yes, so we need to fix this
1947 * instruction.
1948 */
1949 if (fix_offset(p) < 0) {
1950 /*
1951 * We failed to do so.
1952 * Return 0, so our caller
1953 * knows to punt to userland.
1954 */
1955 return 0;
1956 }
1957 }
1958 break;
1959 }
1960 break;
1961 }
1962 }
1963 return 1; /* we succeeded */
1964 }
1965
1966 static int
1967 fix_offset(struct bpf_insn *p)
1968 {
1969 /*
1970 * What's the offset?
1971 */
1972 if (p->k >= SLL_HDR_LEN) {
1973 /*
1974 * It's within the link-layer payload; that starts at an
1975 * offset of 0, as far as the kernel packet filter is
1976 * concerned, so subtract the length of the link-layer
1977 * header.
1978 */
1979 p->k -= SLL_HDR_LEN;
1980 } else if (p->k == 14) {
1981 /*
1982 * It's the protocol field; map it to the special magic
1983 * kernel offset for that field.
1984 */
1985 p->k = SKF_AD_OFF + SKF_AD_PROTOCOL;
1986 } else {
1987 /*
1988 * It's within the header, but it's not one of those
1989 * fields; we can't do that in the kernel, so punt
1990 * to userland.
1991 */
1992 return -1;
1993 }
1994 return 0;
1995 }
1996
1997 static int
1998 set_kernel_filter(pcap_t *handle, struct sock_fprog *fcode)
1999 {
2000 int total_filter_on = 0;
2001 int save_mode;
2002 int ret;
2003 int save_errno;
2004
2005 /*
2006 * The socket filter code doesn't discard all packets queued
2007 * up on the socket when the filter is changed; this means
2008 * that packets that don't match the new filter may show up
2009 * after the new filter is put onto the socket, if those
2010 * packets haven't yet been read.
2011 *
2012 * This means, for example, that if you do a tcpdump capture
2013 * with a filter, the first few packets in the capture might
2014 * be packets that wouldn't have passed the filter.
2015 *
2016 * We therefore discard all packets queued up on the socket
2017 * when setting a kernel filter. (This isn't an issue for
2018 * userland filters, as the userland filtering is done after
2019 * packets are queued up.)
2020 *
2021 * To flush those packets, we put the socket in read-only mode,
2022 * and read packets from the socket until there are no more to
2023 * read.
2024 *
2025 * In order to keep that from being an infinite loop - i.e.,
2026 * to keep more packets from arriving while we're draining
2027 * the queue - we put the "total filter", which is a filter
2028 * that rejects all packets, onto the socket before draining
2029 * the queue.
2030 *
2031 * This code deliberately ignores any errors, so that you may
2032 * get bogus packets if an error occurs, rather than having
2033 * the filtering done in userland even if it could have been
2034 * done in the kernel.
2035 */
2036 if (setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER,
2037 &total_fcode, sizeof(total_fcode)) == 0) {
2038 char drain[1];
2039
2040 /*
2041 * Note that we've put the total filter onto the socket.
2042 */
2043 total_filter_on = 1;
2044
2045 /*
2046 * Save the socket's current mode, and put it in
2047 * non-blocking mode; we drain it by reading packets
2048 * until we get an error (which is normally a
2049 * "nothing more to be read" error).
2050 */
2051 save_mode = fcntl(handle->fd, F_GETFL, 0);
2052 if (save_mode != -1 &&
2053 fcntl(handle->fd, F_SETFL, save_mode | O_NONBLOCK) >= 0) {
2054 while (recv(handle->fd, &drain, sizeof drain,
2055 MSG_TRUNC) >= 0)
2056 ;
2057 save_errno = errno;
2058 fcntl(handle->fd, F_SETFL, save_mode);
2059 if (save_errno != EAGAIN) {
2060 /* Fatal error */
2061 reset_kernel_filter(handle);
2062 snprintf(handle->errbuf, sizeof(handle->errbuf),
2063 "recv: %s", pcap_strerror(save_errno));
2064 return -2;
2065 }
2066 }
2067 }
2068
2069 /*
2070 * Now attach the new filter.
2071 */
2072 ret = setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER,
2073 fcode, sizeof(*fcode));
2074 if (ret == -1 && total_filter_on) {
2075 /*
2076 * Well, we couldn't set that filter on the socket,
2077 * but we could set the total filter on the socket.
2078 *
2079 * This could, for example, mean that the filter was
2080 * too big to put into the kernel, so we'll have to
2081 * filter in userland; in any case, we'll be doing
2082 * filtering in userland, so we need to remove the
2083 * total filter so we see packets.
2084 */
2085 save_errno = errno;
2086
2087 /*
2088 * XXX - if this fails, we're really screwed;
2089 * we have the total filter on the socket,
2090 * and it won't come off. What do we do then?
2091 */
2092 reset_kernel_filter(handle);
2093
2094 errno = save_errno;
2095 }
2096 return ret;
2097 }
2098
2099 static int
2100 reset_kernel_filter(pcap_t *handle)
2101 {
2102 /* setsockopt() barfs unless it get a dummy parameter */
2103 int dummy;
2104
2105 return setsockopt(handle->fd, SOL_SOCKET, SO_DETACH_FILTER,
2106 &dummy, sizeof(dummy));
2107 }
2108 #endif