]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-linux.c
b5be6ce2fd90f7ff281800bbd4e590e0184965c2
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.51 2001-01-03 01:06:16 guy Exp $ (LBL)";
33 * Known problems with 2.0[.x] kernels:
35 * - The loopback device gives every packet twice; on 2.2[.x] kernels,
36 * if we use PF_PACKET, we can filter out the transmitted version
37 * of the packet by using data in the "sockaddr_ll" returned by
38 * "recvfrom()", but, on 2.0[.x] kernels, we have to use
39 * PF_INET/SOCK_PACKET, which means "recvfrom()" supplies a
40 * "sockaddr_pkt" which doesn't give us enough information to let
43 * - We have to set the interface's IFF_PROMISC flag ourselves, if
44 * we're to run in promiscuous mode, which means we have to turn
45 * it off ourselves when we're done; the kernel doesn't keep track
46 * of how many sockets are listening promiscuously, which means
47 * it won't get turned off automatically when no sockets are
48 * listening promiscuously. We catch "pcap_close()" and, for
49 * interfaces we put into promiscuous mode, take them out of
50 * promiscuous mode - which isn't necessarily the right thing to
51 * do, if another socket also requested promiscuous mode between
52 * the time when we opened the socket and the time when we close
69 #include <sys/socket.h>
70 #include <sys/ioctl.h>
71 #include <sys/utsname.h>
73 #include <netinet/in.h>
74 #include <linux/if_ether.h>
75 #include <netinet/if_ether.h>
77 #ifdef HAVE_NETPACKET_PACKET_H
78 #include <netpacket/packet.h>
80 #ifdef SO_ATTACH_FILTER
81 #include <linux/types.h>
82 #include <linux/filter.h>
86 typedef int socklen_t
;
93 #define MAX_LINKHEADER_SIZE 256
96 * When capturing on all interfaces we use this as the buffer size.
97 * Should be bigger then all MTUs that occur in real life.
98 * 64kB should be enough for now.
100 #define BIGGER_THAN_ALL_MTUS (64*1024)
103 * Prototypes for internal functions
105 static int map_arphrd_to_dlt(int arptype
);
106 static int live_open_old(pcap_t
*, char *, int, int, char *);
107 static int live_open_new(pcap_t
*, char *, int, int, char *);
108 static int pcap_read_packet(pcap_t
*, pcap_handler
, u_char
*);
111 * Wrap some ioctl calls
113 #ifdef HAVE_NETPACKET_PACKET_H
114 static int iface_get_id(int fd
, const char *device
, char *ebuf
);
116 static int iface_get_mtu(int fd
, const char *device
, char *ebuf
);
117 static int iface_get_arptype(int fd
, const char *device
, char *ebuf
);
118 #ifdef HAVE_NETPACKET_PACKET_H
119 static int iface_bind(int fd
, int ifindex
, char *ebuf
);
121 static int iface_bind_old(int fd
, const char *device
, char *ebuf
);
123 #ifdef SO_ATTACH_FILTER
124 static int fix_program(pcap_t
*handle
, struct sock_fprog
*fcode
);
125 static int fix_offset(struct bpf_insn
*p
);
129 * Get a handle for a live capture from the given device. You can
130 * pass NULL as device to get all packages (without link level
131 * information of course). If you pass 1 as promisc the interface
132 * will be set to promiscous mode (XXX: I think this usage should
133 * be deprecated and functions be added to select that later allow
134 * modification of that values -- Torsten).
139 pcap_open_live(char *device
, int snaplen
, int promisc
, int to_ms
, char *ebuf
)
141 /* Allocate a handle for this session. */
143 pcap_t
*handle
= malloc(sizeof(*handle
));
144 if (handle
== NULL
) {
145 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
146 pcap_strerror(errno
));
150 /* Initialize some components of the pcap structure. */
152 memset(handle
, 0, sizeof(*handle
));
153 handle
->snapshot
= snaplen
;
154 handle
->md
.timeout
= to_ms
;
157 * NULL and "any" are special devices which give us the hint to
158 * monitor all devices.
160 if (!device
|| strcmp(device
, "any") == 0) {
162 handle
->md
.device
= strdup("any");
164 handle
->md
.device
= strdup(device
);
166 if (handle
->md
.device
== NULL
) {
167 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
168 pcap_strerror(errno
) );
174 * Current Linux kernels use the protocol family PF_PACKET to
175 * allow direct access to all packets on the network while
176 * older kernels had a special socket type SOCK_PACKET to
177 * implement this feature.
178 * While this old implementation is kind of obsolete we need
179 * to be compatible with older kernels for a while so we are
180 * trying both methods with the newer method preferred.
183 if (! (live_open_new(handle
, device
, promisc
, to_ms
, ebuf
) ||
184 live_open_old(handle
, device
, promisc
, to_ms
, ebuf
)) )
187 * Both methods to open the packet socket failed. Tidy
188 * up and report our failure (ebuf is expected to be
189 * set by the functions above).
192 free(handle
->md
.device
);
201 * Read at most max_packets from the capture stream and call the callback
202 * for each of them. Returns the number of packets handled or -1 if an
206 pcap_read(pcap_t
*handle
, int max_packets
, pcap_handler callback
, u_char
*user
)
209 * Currently, on Linux only one packet is delivered per read,
212 return pcap_read_packet(handle
, callback
, user
);
216 * Read a packet from the socket calling the handler provided by
217 * the user. Returns the number of packets received or -1 if an
221 pcap_read_packet(pcap_t
*handle
, pcap_handler callback
, u_char
*userdata
)
224 #ifdef HAVE_NETPACKET_PACKET_H
225 struct sockaddr_ll from
;
226 struct sll_header
*hdrp
;
228 struct sockaddr from
;
231 int packet_len
, caplen
;
232 struct pcap_pkthdr pcap_header
;
234 #ifdef HAVE_NETPACKET_PACKET_H
236 * If this is a cooked device, leave extra room for a
237 * fake packet header.
239 if (handle
->md
.cooked
)
240 offset
= SLL_HDR_LEN
;
245 * This system doesn't have PF_PACKET sockets, so it doesn't
246 * support cooked devices.
251 /* Receive a single packet from the kernel */
254 fromlen
= sizeof(from
);
255 packet_len
= recvfrom(
256 handle
->fd
, handle
->buffer
+ offset
+ handle
->offset
,
257 handle
->md
.readlen
- offset
, MSG_TRUNC
,
258 (struct sockaddr
*) &from
, &fromlen
);
259 } while (packet_len
== -1 && errno
== EINTR
);
261 /* Check if an error occured */
263 if (packet_len
== -1) {
265 return 0; /* no packet there */
267 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
268 "recvfrom: %s", pcap_strerror(errno
));
273 #ifdef HAVE_NETPACKET_PACKET_H
275 * If this is from the loopback device, reject outgoing packets;
276 * we'll see the packet as an incoming packet as well, and
277 * we don't want to see it twice.
279 * We can only do this if we're using PF_PACKET; the address
280 * returned for SOCK_PACKET is a "sockaddr_pkt" which lacks
281 * the relevant packet type information.
283 if (!handle
->md
.sock_packet
&&
284 from
.sll_ifindex
== handle
->md
.lo_ifindex
&&
285 from
.sll_pkttype
== PACKET_OUTGOING
)
289 #ifdef HAVE_NETPACKET_PACKET_H
291 * If this is a cooked device, fill in the fake packet header.
293 if (handle
->md
.cooked
) {
295 * Add the length of the fake header to the length
296 * of packet data we read.
298 packet_len
+= SLL_HDR_LEN
;
300 hdrp
= (struct sll_header
*)handle
->buffer
;
303 * Map the PACKET_ value to a LINUX_SLL_ value; we
304 * want the same numerical value to be used in
305 * the link-layer header even if the numerical values
306 * for the PACKET_ #defines change, so that programs
307 * that look at the packet type field will always be
308 * able to handle DLT_LINUX_SLL captures.
310 switch (from
.sll_pkttype
) {
313 hdrp
->sll_pkttype
= htons(LINUX_SLL_HOST
);
316 case PACKET_BROADCAST
:
317 hdrp
->sll_pkttype
= htons(LINUX_SLL_BROADCAST
);
320 case PACKET_MULTICAST
:
321 hdrp
->sll_pkttype
= htons(LINUX_SLL_MULTICAST
);
324 case PACKET_OTHERHOST
:
325 hdrp
->sll_pkttype
= htons(LINUX_SLL_OTHERHOST
);
328 case PACKET_OUTGOING
:
329 hdrp
->sll_pkttype
= htons(LINUX_SLL_OUTGOING
);
333 hdrp
->sll_pkttype
= -1;
337 hdrp
->sll_hatype
= htons(from
.sll_hatype
);
338 hdrp
->sll_halen
= htons(from
.sll_halen
);
339 memcpy(hdrp
->sll_addr
, from
.sll_addr
,
340 (from
.sll_halen
> SLL_ADDRLEN
) ?
343 hdrp
->sll_protocol
= from
.sll_protocol
;
348 * XXX: According to the kernel source we should get the real
349 * packet len if calling recvfrom with MSG_TRUNC set. It does
350 * not seem to work here :(, but it is supported by this code
352 * To be honest the code RELIES on that feature so this is really
353 * broken with 2.2.x kernels.
354 * I spend a day to figure out what's going on and I found out
355 * that the following is happening:
357 * The packet comes from a random interface and the packet_rcv
358 * hook is called with a clone of the packet. That code inserts
359 * the packet into the receive queue of the packet socket.
360 * If a filter is attached to that socket that filter is run
361 * first - and there lies the problem. The default filter always
362 * cuts the packet at the snaplen:
367 * So the packet filter cuts down the packet. The recvfrom call
368 * says "hey, it's only 68 bytes, it fits into the buffer" with
369 * the result that we don't get the real packet length. This
370 * is valid at least until kernel 2.2.17pre6.
372 * We currently handle this by making a copy of the filter
373 * program, fixing all "ret" instructions with non-zero
374 * operands to have an operand of 65535 so that the filter
375 * doesn't truncate the packet, and supplying that modified
376 * filter to the kernel.
380 if (caplen
> handle
->snapshot
)
381 caplen
= handle
->snapshot
;
383 /* Run the packet filter if not using kernel filter */
384 if (!handle
->md
.use_bpf
&& handle
->fcode
.bf_insns
) {
385 if (bpf_filter(handle
->fcode
.bf_insns
, handle
->buffer
,
386 packet_len
, caplen
) == 0)
388 /* rejected by filter */
393 /* Fill in our own header data */
395 if (ioctl(handle
->fd
, SIOCGSTAMP
, &pcap_header
.ts
) == -1) {
396 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
397 "ioctl: %s", pcap_strerror(errno
));
400 pcap_header
.caplen
= caplen
;
401 pcap_header
.len
= packet_len
;
403 /* Call the user supplied callback function */
404 handle
->md
.stat
.ps_recv
++;
405 callback(userdata
, &pcap_header
, handle
->buffer
+ handle
->offset
);
411 * Get the statistics for the given packet capture handle.
412 * FIXME: Currently does not report the number of dropped packets.
415 pcap_stats(pcap_t
*handle
, struct pcap_stat
*stats
)
417 *stats
= handle
->md
.stat
;
422 * Attach the given BPF code to the packet capture device.
425 pcap_setfilter(pcap_t
*handle
, struct bpf_program
*filter
)
427 #ifdef SO_ATTACH_FILTER
428 struct sock_fprog fcode
;
429 int can_filter_in_kernel
;
435 strncpy(handle
->errbuf
, "setfilter: No filter specified",
436 sizeof(handle
->errbuf
));
440 /* Make our private copy of the filter */
442 if (install_bpf_program(handle
, filter
) < 0) {
443 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
444 "malloc: %s", pcap_strerror(errno
));
449 * Run user level packet filter by default. Will be overriden if
450 * installing a kernel filter succeeds.
452 handle
->md
.use_bpf
= 0;
455 * If we're reading from a savefile, don't try to install
458 if (handle
->sf
.rfile
!= NULL
)
461 /* Install kernel level filter if possible */
463 #ifdef SO_ATTACH_FILTER
465 if (handle
->fcode
.bf_len
> USHRT_MAX
) {
467 * fcode.len is an unsigned short for current kernel.
468 * I have yet to see BPF-Code with that much
469 * instructions but still it is possible. So for the
470 * sake of correctness I added this check.
472 fprintf(stderr
, "Warning: Filter too complex for kernel\n");
474 can_filter_in_kernel
= 0;
476 #endif /* USHRT_MAX */
479 * Oh joy, the Linux kernel uses struct sock_fprog instead
480 * of struct bpf_program and of course the length field is
481 * of different size. Pointed out by Sebastian
483 * Oh, and we also need to fix it up so that all "ret"
484 * instructions with non-zero operands have 65535 as the
485 * operand, and so that, if we're in cooked mode, all
486 * memory-reference instructions use special magic offsets
487 * in references to the link-layer header and assume that
488 * the link-layer payload begins at 0; "fix_program()"
491 switch (fix_program(handle
, &fcode
)) {
496 * Fatal error; just quit.
497 * (The "default" case shouldn't happen; we
498 * return -1 for that reason.)
504 * The program performed checks that we can't make
505 * work in the kernel.
507 can_filter_in_kernel
= 0;
512 * We have a filter that'll work in the kernel.
514 can_filter_in_kernel
= 1;
519 if (can_filter_in_kernel
) {
520 if (setsockopt(handle
->fd
, SOL_SOCKET
, SO_ATTACH_FILTER
,
521 &fcode
, sizeof(fcode
)) == 0)
523 /* Installation succeded - using kernel filter. */
524 handle
->md
.use_bpf
= 1;
529 * Print a warning if we weren't able to install
530 * the filter for a reason other than "this kernel
531 * isn't configured to support socket filters.
533 if (errno
!= ENOPROTOOPT
&& errno
!= EOPNOTSUPP
) {
535 "Warning: Kernel filter failed: %s\n",
536 pcap_strerror(errno
));
542 * Free up the copy of the filter that was made by "fix_program()".
544 if (fcode
.filter
!= NULL
)
546 #endif /* SO_ATTACH_FILTER */
552 * Linux uses the ARP hardware type to identify the type of an
553 * interface. pcap uses the DLT_xxx constants for this. This
554 * function maps the ARPHRD_xxx constant to an appropriate
557 * Returns -1 if unable to map the type; we print a message and,
558 * if we're using PF_PACKET/SOCK_RAW rather than PF_INET/SOCK_PACKET,
559 * we fall back on using PF_PACKET/SOCK_DGRAM.
561 static int map_arphrd_to_dlt(int arptype
)
565 case ARPHRD_METRICOM
:
566 case ARPHRD_LOOPBACK
: return DLT_EN10MB
;
567 case ARPHRD_EETHER
: return DLT_EN3MB
;
568 case ARPHRD_AX25
: return DLT_AX25
;
569 case ARPHRD_PRONET
: return DLT_PRONET
;
570 case ARPHRD_CHAOS
: return DLT_CHAOS
;
571 #ifndef ARPHRD_IEEE802_TR
572 #define ARPHRD_IEEE802_TR 800 /* From Linux 2.4 */
574 case ARPHRD_IEEE802_TR
:
575 case ARPHRD_IEEE802
: return DLT_IEEE802
;
576 case ARPHRD_ARCNET
: return DLT_ARCNET
;
577 case ARPHRD_FDDI
: return DLT_FDDI
;
579 #ifndef ARPHRD_ATM /* FIXME: How to #include this? */
580 #define ARPHRD_ATM 19
582 case ARPHRD_ATM
: return DLT_ATM_CLIP
;
585 /* Not sure if this is correct for all tunnels, but it
592 case ARPHRD_SLIP
: return DLT_RAW
;
598 /* ===== Functions to interface to the newer kernels ================== */
601 * Try to open a packet socket using the new kernel interface.
602 * Returns 0 on failure.
603 * FIXME: 0 uses to mean success (Sebastian)
606 live_open_new(pcap_t
*handle
, char *device
, int promisc
,
607 int to_ms
, char *ebuf
)
609 #ifdef HAVE_NETPACKET_PACKET_H
610 int sock_fd
= -1, device_id
, mtu
, arptype
;
611 struct packet_mreq mr
;
613 /* One shot loop used for error handling - bail out with break */
617 * Open a socket with protocol family packet. If a device is
618 * given we try to open it in raw mode otherwise we use
619 * the cooked interface.
622 socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
))
623 : socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_ALL
));
626 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "socket: %s",
627 pcap_strerror(errno
) );
631 /* It seems the kernel supports the new interface. */
632 handle
->md
.sock_packet
= 0;
635 * Get the interface index of the loopback device.
636 * If the attempt fails, don't fail, just set the
637 * "md.lo_ifindex" to -1.
639 * XXX - can there be more than one device that loops
640 * packets back, i.e. devices other than "lo"? If so,
641 * we'd need to find them all, and have an array of
642 * indices for them, and check all of them in
643 * "pcap_read_packet()".
645 handle
->md
.lo_ifindex
= iface_get_id(sock_fd
, "lo", ebuf
);
648 * What kind of frames do we have to deal with? Fall back
649 * to cooked mode if we have an unknown interface type.
653 /* Assume for now we don't need cooked mode. */
654 handle
->md
.cooked
= 0;
656 arptype
= iface_get_arptype(sock_fd
, device
, ebuf
);
659 handle
->linktype
= map_arphrd_to_dlt(arptype
);
660 if (handle
->linktype
== -1 ||
661 (handle
->linktype
== DLT_EN10MB
&&
662 (strncmp("isdn", device
, 4) == 0 ||
663 strncmp("isdY", device
, 4) == 0)) ||
664 (handle
->linktype
== DLT_RAW
&&
665 (strncmp("ippp", device
, 4) == 0))) {
667 * Unknown interface type (-1), or an ISDN
668 * device (whose link-layer type we
669 * can only determine by using APIs
670 * that may be different on different
671 * kernels) - reopen in cooked mode.
673 * XXX - do that with DLT_RAW as well?
675 if (close(sock_fd
) == -1) {
676 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
677 "close: %s", pcap_strerror(errno
));
680 sock_fd
= socket(PF_PACKET
, SOCK_DGRAM
,
683 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
684 "socket: %s", pcap_strerror(errno
));
687 handle
->md
.cooked
= 1;
689 if (handle
->linktype
== -1) {
691 * Warn that we're falling back on
692 * cooked mode; we may want to
693 * update "map_arphrd_to_dlt()"
694 * to handle the new type.
697 "Warning: arptype %d not "
698 "supported by libpcap - "
699 "falling back to cooked "
703 handle
->linktype
= DLT_LINUX_SLL
;
706 device_id
= iface_get_id(sock_fd
, device
, ebuf
);
710 if (iface_bind(sock_fd
, device_id
, ebuf
) == -1)
714 * This is cooked mode.
716 handle
->md
.cooked
= 1;
717 handle
->linktype
= DLT_LINUX_SLL
;
720 * XXX - squelch GCC complaints about
721 * uninitialized variables; if we can't
722 * select promiscuous mode on all interfaces,
723 * we should move the code below into the
724 * "if (device)" branch of the "if" and
725 * get rid of the next statement.
730 /* Select promiscuous mode on/off */
734 * Hmm, how can we set promiscuous mode on all interfaces?
735 * I am not sure if that is possible at all.
739 memset(&mr
, 0, sizeof(mr
));
740 mr
.mr_ifindex
= device_id
;
741 mr
.mr_type
= promisc
?
742 PACKET_MR_PROMISC
: PACKET_MR_ALLMULTI
;
743 if (setsockopt(sock_fd
, SOL_PACKET
,
744 PACKET_ADD_MEMBERSHIP
, &mr
, sizeof(mr
)) == -1)
746 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
747 "setsockopt: %s", pcap_strerror(errno
));
753 /* Compute the buffersize */
755 mtu
= iface_get_mtu(sock_fd
, device
, ebuf
);
758 handle
->bufsize
= MAX_LINKHEADER_SIZE
+ mtu
;
760 /* Fill in the pcap structure */
762 handle
->fd
= sock_fd
;
765 handle
->buffer
= malloc(handle
->bufsize
);
766 if (!handle
->buffer
) {
767 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
768 "malloc: %s", pcap_strerror(errno
));
773 * This is a 2.2 or later kernel, as it has PF_PACKET;
774 * "recvfrom()", when passed the MSG_TRUNC flag, will
775 * return the actual length of the packet, not the
776 * number of bytes from the packet copied to userland,
777 * so we can safely pass it a byte count based on the
780 handle
->md
.readlen
= handle
->snapshot
;
790 "New packet capturing interface not supported by build "
791 "environment", PCAP_ERRBUF_SIZE
);
796 #ifdef HAVE_NETPACKET_PACKET_H
798 * Return the index of the given device name. Fill ebuf and return
802 iface_get_id(int fd
, const char *device
, char *ebuf
)
806 memset(&ifr
, 0, sizeof(ifr
));
807 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
809 if (ioctl(fd
, SIOCGIFINDEX
, &ifr
) == -1) {
810 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
811 "ioctl: %s", pcap_strerror(errno
));
815 return ifr
.ifr_ifindex
;
819 * Bind the socket associated with FD to the given device.
822 iface_bind(int fd
, int ifindex
, char *ebuf
)
824 struct sockaddr_ll sll
;
826 memset(&sll
, 0, sizeof(sll
));
827 sll
.sll_family
= AF_PACKET
;
828 sll
.sll_ifindex
= ifindex
;
829 sll
.sll_protocol
= htons(ETH_P_ALL
);
831 if (bind(fd
, (struct sockaddr
*) &sll
, sizeof(sll
)) == -1) {
832 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
833 "bind: %s", pcap_strerror(errno
));
843 /* ===== Functions to interface to the older kernels ================== */
846 * With older kernels promiscuous mode is kind of interesting because we
847 * have to reset the interface before exiting. The problem can't really
848 * be solved without some daemon taking care of managing usage counts.
849 * If we put the interface into promiscuous mode, we set a flag indicating
850 * that we must take it out of that mode when the interface is closed,
851 * and, when closing the interface, if that flag is set we take it out
852 * of promiscuous mode.
856 * List of pcaps for which we turned promiscuous mode on by hand.
857 * If there are any such pcaps, we arrange to call "pcap_close_all()"
858 * when we exit, and have it close all of them to turn promiscuous mode
861 static struct pcap
*pcaps_to_close
;
864 * TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
867 static int did_atexit
;
869 static void pcap_close_all(void)
873 while ((handle
= pcaps_to_close
) != NULL
)
877 void pcap_close_linux( pcap_t
*handle
)
879 struct pcap
*p
, *prevp
;
882 if (handle
->md
.clear_promisc
) {
884 * We put the interface into promiscuous mode; take
885 * it out of promiscuous mode.
887 * XXX - if somebody else wants it in promiscuous mode,
888 * this code cannot know that, so it'll take it out
889 * of promiscuous mode. That's not fixable in 2.0[.x]
892 memset(&ifr
, 0, sizeof(ifr
));
893 strncpy(ifr
.ifr_name
, handle
->md
.device
, sizeof(ifr
.ifr_name
));
894 if (ioctl(handle
->fd
, SIOCGIFFLAGS
, &ifr
) == -1) {
896 "Can't restore interface flags (SIOCGIFFLAGS failed: %s).\n"
897 "Please adjust manually.\n"
898 "Hint: This can't happen with Linux >= 2.2.0.\n",
901 if (ifr
.ifr_flags
& IFF_PROMISC
) {
903 * Promiscuous mode is currently on; turn it
906 ifr
.ifr_flags
&= ~IFF_PROMISC
;
907 if (ioctl(handle
->fd
, SIOCSIFFLAGS
, &ifr
) == -1) {
909 "Can't restore interface flags (SIOCSIFFLAGS failed: %s).\n"
910 "Please adjust manually.\n"
911 "Hint: This can't happen with Linux >= 2.2.0.\n",
918 * Take this pcap out of the list of pcaps for which we
919 * have to take the interface out of promiscuous mode.
921 for (p
= pcaps_to_close
, prevp
= NULL
; p
!= NULL
;
922 prevp
= p
, p
= p
->md
.next
) {
925 * Found it. Remove it from the list.
929 * It was at the head of the list.
931 pcaps_to_close
= p
->md
.next
;
934 * It was in the middle of the list.
936 prevp
->md
.next
= p
->md
.next
;
942 if (handle
->md
.device
!= NULL
)
943 free(handle
->md
.device
);
947 * Try to open a packet socket using the old kernel interface.
948 * Returns 0 on failure.
949 * FIXME: 0 uses to mean success (Sebastian)
952 live_open_old(pcap_t
*handle
, char *device
, int promisc
,
953 int to_ms
, char *ebuf
)
955 int sock_fd
= -1, mtu
, arptype
;
956 struct utsname utsname
;
960 /* Open the socket */
962 sock_fd
= socket(PF_INET
, SOCK_PACKET
, htons(ETH_P_ALL
));
964 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
965 "socket: %s", pcap_strerror(errno
));
969 /* It worked - we are using the old interface */
970 handle
->md
.sock_packet
= 1;
972 /* ...which means we get the link-layer header. */
973 handle
->md
.cooked
= 0;
975 /* Bind to the given device */
978 strncpy(ebuf
, "pcap_open_live: The \"any\" device isn't supported on 2.0[.x]-kernel systems",
982 if (iface_bind_old(sock_fd
, device
, ebuf
) == -1)
985 /* Go to promisc mode */
987 memset(&ifr
, 0, sizeof(ifr
));
988 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
989 if (ioctl(sock_fd
, SIOCGIFFLAGS
, &ifr
) == -1) {
990 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
991 "ioctl: %s", pcap_strerror(errno
));
994 if ((ifr
.ifr_flags
& IFF_PROMISC
) == 0) {
996 * Promiscuous mode isn't currently on,
997 * so turn it on, and remember that
998 * we should turn it off when the
1003 * If we haven't already done so, arrange
1004 * to have "pcap_close_all()" called when
1008 if (atexit(pcap_close_all
) == -1) {
1010 * "atexit()" failed; don't
1011 * put the interface in
1012 * promiscuous mode, just
1015 strncpy(ebuf
, "atexit failed",
1021 ifr
.ifr_flags
|= IFF_PROMISC
;
1022 if (ioctl(sock_fd
, SIOCSIFFLAGS
, &ifr
) == -1) {
1023 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1025 pcap_strerror(errno
));
1028 handle
->md
.clear_promisc
= 1;
1031 * Add this to the list of pcaps
1032 * to close when we exit.
1034 handle
->md
.next
= pcaps_to_close
;
1035 pcaps_to_close
= handle
;
1039 /* Compute the buffersize */
1041 mtu
= iface_get_mtu(sock_fd
, device
, ebuf
);
1044 handle
->bufsize
= MAX_LINKHEADER_SIZE
+ mtu
;
1045 if (handle
->bufsize
< handle
->snapshot
)
1046 handle
->bufsize
= handle
->snapshot
;
1048 /* All done - fill in the pcap handle */
1050 arptype
= iface_get_arptype(sock_fd
, device
, ebuf
);
1054 handle
->fd
= sock_fd
;
1056 handle
->linktype
= map_arphrd_to_dlt(arptype
);
1058 * XXX - handle ISDN types here? We can't fall back on
1059 * cooked sockets, so we'd have to figure out from the
1060 * device name what type of link-layer encapsulation
1061 * it's using, and map that to an appropriate DLT_
1062 * value, meaning we'd map "isdnN" devices to DLT_RAW
1063 * (they supply raw IP packets with no link-layer
1064 * header) and "isdY" devices to a new DLT_I4L_IP
1065 * type that has only an Ethernet packet type as
1066 * a link-layer header.
1068 if (handle
->linktype
== -1) {
1069 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1070 "interface type of %s not supported", device
);
1073 handle
->buffer
= malloc(handle
->bufsize
);
1074 if (!handle
->buffer
) {
1075 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1076 "malloc: %s", pcap_strerror(errno
));
1081 * This might be a 2.0[.x] kernel - check.
1083 if (uname(&utsname
) < 0 ||
1084 strncmp(utsname
.release
, "2.0", 3) == 0) {
1086 * Either we couldn't find out what kernel release
1087 * this is, or it's a 2.0[.x] kernel.
1089 * In the 2.0[.x] kernel, a "recvfrom()" on
1090 * a SOCK_PACKET socket, with MSG_TRUNC set, will
1091 * return the number of bytes read, so if we pass
1092 * a length based on the snapshot length, it'll
1093 * return the number of bytes from the packet
1094 * copied to userland, not the actual length
1097 * This means that, for example, the IP dissector
1098 * in tcpdump will get handed a packet length less
1099 * than the length in the IP header, and will
1100 * complain about "truncated-ip".
1102 * So we don't bother trying to copy from the
1103 * kernel only the bytes in which we're interested,
1104 * but instead copy them all, just as the older
1105 * versions of libpcap for Linux did.
1107 * Just one of many problems with packet capture
1108 * on 2.0[.x] kernels; you really want a 2.2[.x]
1109 * or later kernel if you want packet capture to
1112 handle
->md
.readlen
= handle
->bufsize
;
1115 * This is a 2.2[.x] or later kernel (although
1116 * why we're using SOCK_PACKET on such a system
1117 * is unknown to me).
1119 * We can safely pass "recvfrom()" a byte count
1120 * based on the snapshot length.
1122 handle
->md
.readlen
= handle
->snapshot
;
1134 * Bind the socket associated with FD to the given device using the
1135 * interface of the old kernels.
1138 iface_bind_old(int fd
, const char *device
, char *ebuf
)
1140 struct sockaddr saddr
;
1142 memset(&saddr
, 0, sizeof(saddr
));
1143 strncpy(saddr
.sa_data
, device
, sizeof(saddr
.sa_data
));
1144 if (bind(fd
, &saddr
, sizeof(saddr
)) == -1) {
1145 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1146 "bind: %s", pcap_strerror(errno
));
1154 /* ===== System calls available on all supported kernels ============== */
1157 * Query the kernel for the MTU of the given interface.
1160 iface_get_mtu(int fd
, const char *device
, char *ebuf
)
1165 return BIGGER_THAN_ALL_MTUS
;
1167 memset(&ifr
, 0, sizeof(ifr
));
1168 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
1170 if (ioctl(fd
, SIOCGIFMTU
, &ifr
) == -1) {
1171 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1172 "ioctl: %s", pcap_strerror(errno
));
1180 * Get the hardware type of the given interface as ARPHRD_xxx constant.
1183 iface_get_arptype(int fd
, const char *device
, char *ebuf
)
1187 memset(&ifr
, 0, sizeof(ifr
));
1188 strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
1190 if (ioctl(fd
, SIOCGIFHWADDR
, &ifr
) == -1) {
1191 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1192 "ioctl: %s", pcap_strerror(errno
));
1196 return ifr
.ifr_hwaddr
.sa_family
;
1199 #ifdef HAVE_NETPACKET_PACKET_H
1201 fix_program(pcap_t
*handle
, struct sock_fprog
*fcode
)
1205 register struct bpf_insn
*p
;
1210 * Make a copy of the filter, and modify that copy if
1213 prog_size
= sizeof(*handle
->fcode
.bf_insns
) * handle
->fcode
.bf_len
;
1214 len
= handle
->fcode
.bf_len
;
1215 f
= (struct bpf_insn
*)malloc(prog_size
);
1217 snprintf(handle
->errbuf
, sizeof(handle
->errbuf
),
1218 "malloc: %s", pcap_strerror(errno
));
1221 memcpy(f
, handle
->fcode
.bf_insns
, prog_size
);
1223 fcode
->filter
= (struct sock_filter
*) f
;
1225 for (i
= 0; i
< len
; ++i
) {
1228 * What type of instruction is this?
1230 switch (BPF_CLASS(p
->code
)) {
1234 * It's a return instruction; is the snapshot
1235 * length a constant, rather than the contents
1236 * of the accumulator?
1238 if (BPF_MODE(p
->code
) == BPF_K
) {
1240 * Yes - if the value to be returned,
1241 * i.e. the snapshot length, is anything
1242 * other than 0, make it 65535, so that
1243 * the packet is truncated by "recvfrom()",
1244 * not by the filter.
1246 * XXX - there's nothing we can easily do
1247 * if it's getting the value from the
1248 * accumulator; we'd have to insert
1249 * code to force non-zero values to be
1260 * It's a load instruction; is it loading
1263 switch (BPF_MODE(p
->code
)) {
1269 * Yes; are we in cooked mode?
1271 if (handle
->md
.cooked
) {
1273 * Yes, so we need to fix this
1276 if (fix_offset(p
) < 0) {
1278 * We failed to do so.
1279 * Return 0, so our caller
1280 * knows to punt to userland.
1290 return 1; /* we succeeded */
1294 fix_offset(struct bpf_insn
*p
)
1297 * What's the offset?
1299 if (p
->k
>= SLL_HDR_LEN
) {
1301 * It's within the link-layer payload; that starts at an
1302 * offset of 0, as far as the kernel packet filter is
1303 * concerned, so subtract the length of the link-layer
1306 p
->k
-= SLL_HDR_LEN
;
1307 } else if (p
->k
== 2) {
1309 * It's the protocol field; map it to the special magic
1310 * kernel offset for that field.
1312 p
->k
= SKF_AD_OFF
+ SKF_AD_PROTOCOL
;
1315 * It's within the header, but it's not one of those
1316 * fields; we can't do that in the kernel, so punt