]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-linux.c
2 * pcap-linux.c: Packet capture interface to the Linux kernel
4 * Copyright (c) 2000 Torsten Landschoff <torsten@debian.org>
5 * Sebastian Krahmer <krahmer@cs.uni-potsdam.de>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior
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.
28 static const char rcsid
[] =
29 "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.32 2000-10-18 08:32:55 guy Exp $ (LBL)";
34 * - setting promiscuous on loopback gives every packet twice
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
52 #include <netinet/in.h>
53 #include <linux/if_ether.h>
54 #include <netinet/if_ether.h>
56 #ifdef HAVE_NETPACKET_PACKET_H
57 #include <netpacket/packet.h>
59 #ifdef SO_ATTACH_FILTER
60 #include <linux/types.h>
61 #include <linux/filter.h>
65 typedef int socklen_t
;
72 #define MAX_LINKHEADER_SIZE 256
75 * When capturing on all interfaces we use this as the buffer size.
76 * Should be bigger then all MTUs that occur in real life.
77 * 64kB should be enough for now.
79 #define BIGGER_THAN_ALL_MTUS (64*1024)
82 * Prototypes for internal functions
84 static int map_arphrd_to_dlt(int arptype
);
85 static int live_open_old(pcap_t
*, char *, int, int, char *);
86 static int live_open_new(pcap_t
*, char *, int, int, char *);
87 static int pcap_read_packet(pcap_t
*, pcap_handler
, u_char
*);
90 * Wrap some ioctl calls
92 static int iface_get_id(int fd
, const char *device
, char *ebuf
);
93 static int iface_get_mtu(int fd
, const char *device
, char *ebuf
);
94 static int iface_get_arptype(int fd
, const char *device
, char *ebuf
);
95 static int iface_bind(int fd
, int ifindex
, char *ebuf
);
96 static int iface_bind_old(int fd
, const char *device
, char *ebuf
);
99 * Get a handle for a live capture from the given device. You can
100 * pass NULL as device to get all packages (without link level
101 * information of course). If you pass 1 as promisc the interface
102 * will be set to promiscous mode (XXX: I think this usage should
103 * be deprecated and functions be added to select that later allow
104 * modification of that values -- Torsten).
109 pcap_open_live(char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
111 /* Allocate a handle for this session. */
113 pcap_t
*handle
= malloc(sizeof(*handle
));
114 if (handle
== NULL
) {
115 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
116 pcap_strerror(errno
));
120 /* Initialize some components of the pcap structure. */
122 memset(handle
, 0, sizeof(*handle
));
123 handle
->snapshot
= snaplen
;
124 handle
->md
.timeout
= to_ms
;
125 handle
->md
.promisc
= promisc
;
128 * NULL and "any" are special devices which give us the hint to
129 * monitor all devices.
131 if (!device
|| strcmp(device
, "any") == 0) {
133 handle
->md
.device
= strdup("any");
135 handle
->md
.device
= strdup(device
);
137 if (handle
->md
.device
== NULL
) {
138 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
139 pcap_strerror(errno
) );
145 * Current Linux kernels use the protocol family PF_PACKET to
146 * allow direct access to all packets on the network while
147 * older kernels had a special socket type SOCK_PACKET to
148 * implement this feature.
149 * While this old implementation is kind of obsolete we need
150 * to be compatible with older kernels for a while so we are
151 * trying both methods with the newer method preferred.
154 if (! (live_open_new(handle
, device
, promisc
, to_ms
, ebuf
) ||
155 live_open_old(handle
, device
, promisc
, to_ms
, ebuf
)) )
158 * Both methods to open the packet socket failed. Tidy
159 * up and report our failure (ebuf is expected to be
160 * set by the functions above).
163 free(handle
->md
.device
);
169 * Okay, now we have a packet stream open. Maybe we need to handle
170 * a timeout? In that case we set the filehandle to nonblocking
171 * so pcap_read can try reading the fd and call select if no data
172 * is available at first.
176 int flags
= fcntl(handle
->fd
, F_GETFL
);
179 flags
= fcntl(handle
->fd
, F_SETFL
, flags
);
182 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "fcntl: %s",
183 pcap_strerror(errno
));
193 * Read at most max_packets from the capture stream and call the callback
194 * for each of them. Returns the number of packets handled or -1 if an
197 * XXX: Can I rely on the Linux-specified behaviour of select (returning
198 * the time left in the timeval structure)? I really don't want to query
199 * the system time before each select call...
201 * pcap_read currently gets not only a packet from the kernel but also
202 * the sockaddr_ll returned as source of the packet. This way we can at
203 * some time extend tcpdump and libpcap to sniff on all devices at a time
204 * and find the right printing routine by using the information in the
205 * sockaddr_ll structure.
208 pcap_read(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
215 * Fill in a timeval structure for select if we need to obeye a
218 if (handle
->md
.timeout
> 0) {
219 tv
.tv_usec
= (handle
->md
.timeout
% 1000) * 1000;
220 tv
.tv_sec
= (handle
->md
.timeout
/ 1000);
224 * Read packets until the packet limit has been reached or
225 * an error occured while reading. Call the user function
226 * for each received packet.
228 for (packets
= 0; max_packets
== -1 || packets
< max_packets
;)
230 status
= pcap_read_packet(handle
, callback
, user
);
235 } else if (status
== -1)
239 * If no packet is available we go to sleep. FIXME: This
240 * might be better implemented using poll(?)
243 FD_SET(handle
->fd
, &read_fds
);
244 status
= select(handle
->fd
+ 1,
245 &read_fds
, NULL
, NULL
, &tv
);
247 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
248 "select: %s", pcap_strerror(errno
));
250 } else if (status
== 0 ||
251 (tv
.tv_usec
== 0 && tv
.tv_sec
== 0))
259 * Read a packet from the socket calling the handler provided by
260 * the user. Returns the number of packets received or -1 if an
264 pcap_read_packet(pcap_t
*handle
, pcap_handler callback
, u_char
*userdata
)
266 struct sockaddr from
;
268 int packet_len
, caplen
;
269 struct pcap_pkthdr pcap_header
;
272 * We don't currently use the from return value of recvfrom but
273 * this will probably be implemented in the future.
276 /* Receive a single packet from the kernel */
279 fromlen
= sizeof(from
);
280 packet_len
= recvfrom(
281 handle
->fd
, handle
->buffer
+ handle
->offset
,
282 handle
->snapshot
, MSG_TRUNC
,
283 (struct sockaddr
*) &from
, &fromlen
);
284 } while (packet_len
== -1 && errno
== EINTR
);
286 /* Check if an error occured */
288 if (packet_len
== -1) {
290 return 0; /* no packet there */
292 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
293 "recvfrom: %s", pcap_strerror(errno
));
299 * XXX: According to the kernel source we should get the real
300 * packet len if calling recvfrom with MSG_TRUNC set. It does
301 * not seem to work here :(, but it is supported by this code
303 * To be honest the code RELIES on that feature so this is really
304 * broken with 2.2.x kernels.
305 * I spend a day to figure out what's going on and I found out
306 * that the following is happening:
308 * The packet comes from a random interface and the packet_rcv
309 * hook is called with a clone of the packet. That code inserts
310 * the packet into the receive queue of the packet socket.
311 * If a filter is attached to that socket that filter is run
312 * first - and there lies the problem. The default filter always
313 * cuts the packet at the snaplen:
318 * So the packet filter cuts down the packet. The recvfrom call
319 * says "hey, it's only 68 bytes, it fits into the buffer" with
320 * the result that we don't get the real packet length. This
321 * is valid at least until kernel 2.2.17pre6.
323 * tcpdump is currently fixed by changing the BPF code generator
324 * to not truncate the received packet.
328 if (caplen
> handle
->snapshot
)
329 caplen
= handle
->snapshot
;
331 /* Run the packet filter if not using kernel filter */
332 if (!handle
->md
.use_bpf
&& handle
->fcode
.bf_insns
) {
333 if (bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
334 packet_len
, caplen
) == 0)
336 /* rejected by filter */
341 /* Fill in our own header data */
343 if (ioctl(handle
->fd
, SIOCGSTAMP
, &pcap_header
.ts
) == -1) {
344 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
345 "ioctl: %s", pcap_strerror(errno
));
348 pcap_header
.caplen
= caplen
;
349 pcap_header
.len
= packet_len
;
351 /* Call the user supplied callback function */
352 handle
->md
.stat
.ps_recv
++;
353 callback(userdata
, &pcap_header
, handle
->buffer
+ handle
->offset
);
359 * Get the statistics for the given packet capture handle.
360 * FIXME: Currently does not report the number of dropped packets.
363 pcap_stats(pcap_t
*handle
, struct pcap_stat
*stats
)
365 *stats
= handle
->md
.stat
;
370 * Attach the given BPF code to the packet capture device.
373 pcap_setfilter(pcap_t
*handle
, struct bpf_program
*filter
)
375 #ifdef SO_ATTACH_FILTER
376 struct sock_fprog fcode
;
382 strncpy(handle
->errbuf
, "setfilter: No filter specified",
383 sizeof(handle
->errbuf
));
387 /* Free old filter code if existing */
389 handle
->fcode
.bf_len
= 0;
390 if (handle
->fcode
.bf_insns
) {
391 free(handle
->fcode
.bf_insns
);
392 handle
->fcode
.bf_insns
= NULL
;
396 /* Make our private copy of the filter */
398 handle
->fcode
.bf_len
= filter
->bf_len
;
399 handle
->fcode
.bf_insns
=
400 malloc(filter
->bf_len
* sizeof(*filter
->bf_insns
));
401 if (handle
->fcode
.bf_insns
== NULL
) {
402 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
403 "malloc: %s", pcap_strerror(errno
));
406 memcpy(handle
->fcode
.bf_insns
, filter
->bf_insns
,
407 filter
->bf_len
* sizeof(*filter
->bf_insns
));
410 * Run user level packet filter by default. Will be overriden if
411 * installing a kernel filter succeeds.
413 handle
->md
.use_bpf
= 0;
415 /* Install kernel level filter if possible */
417 #ifdef SO_ATTACH_FILTER
419 * Oh joy, the Linux kernel uses struct sock_fprog instead of
420 * struct bpf_program and of course the length field is of
421 * different size. Pointed out by Sebastian
424 fcode
.filter
= (struct sock_filter
*) handle
->fcode
.bf_insns
;
425 fcode
.len
= filter
->bf_len
;
427 if (filter
->bf_len
> USHRT_MAX
) {
429 * fcode.len is an unsigned short for current kernel.
430 * I have yet to see BPF-Code with that much instructions
431 * but still it is possible. So for the sake of
432 * correctness I added this check.
434 fprintf(stderr
, "Warning: Filter to complex for kernel\n");
438 if (setsockopt(handle
->fd
, SOL_SOCKET
, SO_ATTACH_FILTER
,
439 &fcode
, sizeof(fcode
)) == 0)
441 /* Installation succeded - using kernel filter. */
442 handle
->md
.use_bpf
= 1;
447 * Print a warning if kernel filter available but a problem
450 if (errno
!= ENOPROTOOPT
&& errno
!= EOPNOTSUPP
) {
451 fprintf(stderr
, "Warning: Kernel filter failed: %s\n",
452 pcap_strerror(errno
));
461 * Linux uses the ARP hardware type to identify the type of an
462 * interface. pcap uses the DLT_xxx constants for this. This
463 * function maps the ARPHRD_xxx constant to an appropriate
466 * Returns -1 if unable to map the type.
468 static int map_arphrd_to_dlt(int arptype
)
472 case ARPHRD_METRICOM
:
473 case ARPHRD_LOOPBACK
: return DLT_EN10MB
;
474 case ARPHRD_EETHER
: return DLT_EN3MB
;
475 case ARPHRD_AX25
: return DLT_AX25
;
476 case ARPHRD_PRONET
: return DLT_PRONET
;
477 case ARPHRD_CHAOS
: return DLT_CHAOS
;
478 case ARPHRD_IEEE802
: return DLT_IEEE802
;
479 case ARPHRD_ARCNET
: return DLT_ARCNET
;
480 case ARPHRD_FDDI
: return DLT_FDDI
;
482 #ifndef ARPHRD_ATM /* FIXME: How to #include this? */
483 #define ARPHRD_ATM 19
485 case ARPHRD_ATM
: return DLT_ATM_CLIP
;
491 case ARPHRD_SLIP
: return DLT_RAW
;
497 /* ===== Functions to interface to the newer kernels ================== */
500 * Try to open a packet socket using the new kernel interface.
501 * Returns 0 on failure.
502 * FIXME: 0 uses to mean success (Sebastian)
505 live_open_new(pcap_t
*handle
, char *device
, int promisc
,
506 int to_ms
, char *ebuf
)
508 #ifdef HAVE_NETPACKET_PACKET_H
509 int sock_fd
= -1, device_id
, mtu
, arptype
;
510 struct packet_mreq mr
;
512 /* One shot loop used for error handling - bail out with break */
516 * Open a socket with protocol family packet. If a device is
517 * given we try to open it in raw mode otherwise we use
518 * the cooked interface.
521 socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
))
522 : socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_ALL
));
525 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
526 pcap_strerror(errno
) );
530 /* It seems the kernel supports the new interface. */
531 handle
->md
.sock_packet
= 0;
534 * What kind of frames do we have to deal with? Fall back
535 * to cooked mode if we have an unknown interface type.
539 arptype
= iface_get_arptype(sock_fd
, device
, ebuf
);
542 handle
->linktype
= map_arphrd_to_dlt(arptype
);
544 handle
->linktype
= DLT_RAW
;
546 if (handle
->linktype
== -1) {
547 /* Unknown interface type - reopen in cooked mode */
549 if (close(sock_fd
) == -1) {
550 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
551 "close: %s", pcap_strerror(errno
));
554 sock_fd
= socket(PF_PACKET
, SOCK_DGRAM
,
557 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
558 "socket: %s", pcap_strerror(errno
));
563 "Warning: Falling back to cooked socket\n");
564 handle
->linktype
= DLT_RAW
;
569 device_id
= iface_get_id(sock_fd
, device
, ebuf
);
573 if (iface_bind(sock_fd
, device_id
, ebuf
) == -1)
577 /* Select promiscous mode on/off */
581 * Hmm, how can we set promiscuous mode on all interfaces?
582 * I am not sure if that is possible at all.
586 memset(&mr
, 0, sizeof(mr
));
587 mr
.mr_ifindex
= device_id
;
588 mr
.mr_type
= promisc
?
589 PACKET_MR_PROMISC
: PACKET_MR_ALLMULTI
;
590 if (setsockopt(sock_fd
, SOL_PACKET
,
591 PACKET_ADD_MEMBERSHIP
, &mr
, sizeof(mr
)) == -1)
593 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
594 "setsockopt: %s", pcap_strerror(errno
));
600 /* Compute the buffersize */
602 mtu
= iface_get_mtu(sock_fd
, device
, ebuf
);
605 handle
->bufsize
= MAX_LINKHEADER_SIZE
+ mtu
;
607 /* Fill in the pcap structure */
609 handle
->fd
= sock_fd
;
612 handle
->buffer
= malloc(handle
->bufsize
);
613 if (!handle
->buffer
) {
614 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
615 "malloc: %s", pcap_strerror(errno
));
628 "New packet capturing interface not supported by build "
629 "environment", PCAP_ERRBUF_SIZE
);
634 #ifdef HAVE_NETPACKET_PACKET_H
636 * Return the index of the given device name. Fill ebuf and return
640 iface_get_id(int fd
, const char *device
, char *ebuf
)
644 memset(&ifr
, 0, sizeof(ifr
));
645 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
647 if (ioctl(fd
, SIOCGIFINDEX
, &ifr
) == -1) {
648 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
649 "ioctl: %s", pcap_strerror(errno
));
653 return ifr
.ifr_ifindex
;
657 * Bind the socket associated with FD to the given device.
660 iface_bind(int fd
, int ifindex
, char *ebuf
)
662 struct sockaddr_ll sll
;
664 memset(&sll
, 0, sizeof(sll
));
665 sll
.sll_family
= AF_PACKET
;
666 sll
.sll_ifindex
= ifindex
;
667 sll
.sll_protocol
= htons(ETH_P_ALL
);
669 if (bind(fd
, (struct sockaddr
*) &sll
, sizeof(sll
)) == -1) {
670 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
671 "bind: %s", pcap_strerror(errno
));
681 /* ===== Functions to interface to the older kernels ================== */
684 * With older kernels promiscuous mode is kind of interesting because we
685 * have to reset the interface before exiting. The problem can't really
686 * be solved without some daemon taking care of managing usage counts.
687 * We save the promiscuous state of the device when opening the capture
688 * stream and arrange for it to be reset on process exit.
690 * XXX: This solution is still not correct even for this case. The
691 * devices stay in promiscuous mode until the process exits. I need to
692 * modify pcap_close to solve this.
696 * The device name and the interface flags to be restored at exit
698 struct ifreq restore_ifr
;
700 static void restore_interface( void )
702 int status
= socket(PF_INET
, SOCK_PACKET
, 0);
705 status
= ioctl(status
, SIOCSIFFLAGS
, &restore_ifr
);
709 "Can't restore interface flags. Please adjust manually. \n"
710 "Hint: This can't happen with Linux >= 2.2.0.\n");
715 * Try to open a packet socket using the old kernel interface.
716 * Returns 0 on failure.
717 * FIXME: 0 uses to mean success (Sebastian)
720 live_open_old(pcap_t
*handle
, char *device
, int promisc
,
721 int to_ms
, char *ebuf
)
723 int sock_fd
= -1, mtu
, arptype
;
727 /* Open the socket */
729 sock_fd
= socket(PF_INET
, SOCK_PACKET
, htons(ETH_P_ALL
));
731 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
732 "socket: %s", pcap_strerror(errno
));
736 /* It worked - we are using the old interface */
737 handle
->md
.sock_packet
= 1;
739 /* Bind to the given device */
742 strncpy(ebuf
, "pcap_open_live: No interface given",
746 if (iface_bind_old(sock_fd
, device
, ebuf
) == -1)
749 /* Go to promisc mode */
751 memset(&ifr
, 0, sizeof(ifr
));
752 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
753 if (ioctl(sock_fd
, SIOCGIFFLAGS
, &ifr
) == -1) {
754 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
755 "ioctl: %s", pcap_strerror(errno
));
758 if ((ifr
.ifr_flags
& IFF_PROMISC
) == 0) {
760 ifr
.ifr_flags
|= IFF_PROMISC
;
761 if (ioctl(sock_fd
, SIOCSIFFLAGS
, &ifr
) == -1) {
762 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
764 pcap_strerror(errno
));
767 if (atexit(restore_interface
) == -1) {
769 strncpy(ebuf
, "atexit failed",
777 /* Compute the buffersize */
779 mtu
= iface_get_mtu(sock_fd
, device
, ebuf
);
782 handle
->bufsize
= MAX_LINKHEADER_SIZE
+ mtu
;
784 /* All done - fill in the pcap handle */
786 arptype
= iface_get_arptype(sock_fd
, device
, ebuf
);
790 handle
->fd
= sock_fd
;
792 handle
->linktype
= map_arphrd_to_dlt(arptype
);
793 if (handle
->linktype
== -1) {
794 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
795 "interface type of %s not supported", device
);
798 handle
->buffer
= malloc(handle
->bufsize
);
799 if (!handle
->buffer
) {
800 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
801 "malloc: %s", pcap_strerror(errno
));
815 * Bind the socket associated with FD to the given device using the
816 * interface of the old kernels.
819 iface_bind_old(int fd
, const char *device
, char *ebuf
)
821 struct sockaddr saddr
;
823 memset(&saddr
, 0, sizeof(saddr
));
824 strncpy(saddr
.sa_data
, device
, sizeof(saddr
.sa_data
));
825 if (bind(fd
, &saddr
, sizeof(saddr
)) == -1) {
826 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
827 "bind: %s", pcap_strerror(errno
));
835 /* ===== System calls available on all supported kernels ============== */
838 * Query the kernel for the MTU of the given interface.
841 iface_get_mtu(int fd
, const char *device
, char *ebuf
)
846 return BIGGER_THAN_ALL_MTUS
;
848 memset(&ifr
, 0, sizeof(ifr
));
849 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
851 if (ioctl(fd
, SIOCGIFMTU
, &ifr
) == -1) {
852 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
853 "ioctl: %s", pcap_strerror(errno
));
861 * Get the hardware type of the given interface as ARPHRD_xxx constant.
864 iface_get_arptype(int fd
, const char *device
, char *ebuf
)
868 memset(&ifr
, 0, sizeof(ifr
));
869 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
871 if (ioctl(fd
, SIOCGIFHWADDR
, &ifr
) == -1) {
872 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
873 "ioctl: %s", pcap_strerror(errno
));
877 return ifr
.ifr_hwaddr
.sa_family
;