1999-10-07 23:46:40 +00:00
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* pcap-linux.c: Packet capture interface to the Linux kernel
|
|
|
|
*
|
|
|
|
* Copyright (c) 2000 Torsten Landschoff <torsten@debian.org>
|
|
|
|
* Sebastian Krahmer <krahmer@cs.uni-potsdam.de>
|
|
|
|
*
|
|
|
|
* License: BSD
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. The names of the authors may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
2000-09-20 07:52:04 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
2001-01-03 01:06:16 +00:00
|
|
|
"@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.51 2001-01-03 01:06:16 guy Exp $ (LBL)";
|
2000-09-20 07:52:04 +00:00
|
|
|
#endif
|
2000-06-08 06:40:09 +00:00
|
|
|
|
|
|
|
/*
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
* Known problems with 2.0[.x] kernels:
|
|
|
|
*
|
|
|
|
* - The loopback device gives every packet twice; on 2.2[.x] kernels,
|
|
|
|
* if we use PF_PACKET, we can filter out the transmitted version
|
|
|
|
* of the packet by using data in the "sockaddr_ll" returned by
|
|
|
|
* "recvfrom()", but, on 2.0[.x] kernels, we have to use
|
|
|
|
* PF_INET/SOCK_PACKET, which means "recvfrom()" supplies a
|
|
|
|
* "sockaddr_pkt" which doesn't give us enough information to let
|
|
|
|
* us do that.
|
|
|
|
*
|
|
|
|
* - We have to set the interface's IFF_PROMISC flag ourselves, if
|
|
|
|
* we're to run in promiscuous mode, which means we have to turn
|
|
|
|
* it off ourselves when we're done; the kernel doesn't keep track
|
|
|
|
* of how many sockets are listening promiscuously, which means
|
|
|
|
* it won't get turned off automatically when no sockets are
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
* listening promiscuously. We catch "pcap_close()" and, for
|
|
|
|
* interfaces we put into promiscuous mode, take them out of
|
|
|
|
* promiscuous mode - which isn't necessarily the right thing to
|
|
|
|
* do, if another socket also requested promiscuous mode between
|
|
|
|
* the time when we opened the socket and the time when we close
|
|
|
|
* the socket.
|
2000-08-13 21:25:26 +00:00
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
|
|
|
|
|
2000-07-11 00:37:04 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
#include "pcap-int.h"
|
2000-12-21 10:29:21 +00:00
|
|
|
#include "sll.h"
|
1999-10-07 23:46:40 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2000-06-08 06:40:09 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
#include <sys/utsname.h>
|
2000-06-08 06:40:09 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <linux/if_ether.h>
|
|
|
|
#include <netinet/if_ether.h>
|
1999-10-07 23:46:40 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
#ifdef HAVE_NETPACKET_PACKET_H
|
|
|
|
#include <netpacket/packet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef SO_ATTACH_FILTER
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/filter.h>
|
|
|
|
#endif
|
1999-10-07 23:46:40 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
#ifndef __GLIBC__
|
|
|
|
typedef int socklen_t;
|
2000-07-11 01:06:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MSG_TRUNC
|
2000-06-08 06:40:09 +00:00
|
|
|
#define MSG_TRUNC 0
|
1999-10-07 23:46:40 +00:00
|
|
|
#endif
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
#define MAX_LINKHEADER_SIZE 256
|
1999-10-07 23:46:40 +00:00
|
|
|
|
2000-09-20 15:10:28 +00:00
|
|
|
/*
|
|
|
|
* When capturing on all interfaces we use this as the buffer size.
|
|
|
|
* Should be bigger then all MTUs that occur in real life.
|
|
|
|
* 64kB should be enough for now.
|
|
|
|
*/
|
|
|
|
#define BIGGER_THAN_ALL_MTUS (64*1024)
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Prototypes for internal functions
|
|
|
|
*/
|
|
|
|
static int map_arphrd_to_dlt(int arptype );
|
|
|
|
static int live_open_old(pcap_t *, char *, int, int, char *);
|
|
|
|
static int live_open_new(pcap_t *, char *, int, int, char *);
|
|
|
|
static int pcap_read_packet(pcap_t *, pcap_handler, u_char *);
|
2000-06-08 06:40:09 +00:00
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
|
|
|
* Wrap some ioctl calls
|
|
|
|
*/
|
2000-10-20 06:55:28 +00:00
|
|
|
#ifdef HAVE_NETPACKET_PACKET_H
|
2000-08-13 21:25:26 +00:00
|
|
|
static int iface_get_id(int fd, const char *device, char *ebuf);
|
2000-10-20 06:55:28 +00:00
|
|
|
#endif
|
2000-08-13 21:25:26 +00:00
|
|
|
static int iface_get_mtu(int fd, const char *device, char *ebuf);
|
|
|
|
static int iface_get_arptype(int fd, const char *device, char *ebuf);
|
2000-10-20 06:55:28 +00:00
|
|
|
#ifdef HAVE_NETPACKET_PACKET_H
|
2000-08-13 21:25:26 +00:00
|
|
|
static int iface_bind(int fd, int ifindex, char *ebuf);
|
2000-10-20 06:55:28 +00:00
|
|
|
#endif
|
2000-08-13 21:25:26 +00:00
|
|
|
static int iface_bind_old(int fd, const char *device, char *ebuf);
|
2000-06-08 06:40:09 +00:00
|
|
|
|
2000-12-21 10:29:21 +00:00
|
|
|
#ifdef SO_ATTACH_FILTER
|
|
|
|
static int fix_program(pcap_t *handle, struct sock_fprog *fcode);
|
|
|
|
static int fix_offset(struct bpf_insn *p);
|
|
|
|
#endif
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
|
|
|
* Get a handle for a live capture from the given device. You can
|
|
|
|
* pass NULL as device to get all packages (without link level
|
|
|
|
* information of course). If you pass 1 as promisc the interface
|
|
|
|
* will be set to promiscous mode (XXX: I think this usage should
|
|
|
|
* be deprecated and functions be added to select that later allow
|
|
|
|
* modification of that values -- Torsten).
|
|
|
|
*
|
|
|
|
* See also pcap(3).
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
pcap_t *
|
2000-08-13 21:25:26 +00:00
|
|
|
pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
|
1999-10-07 23:46:40 +00:00
|
|
|
{
|
2000-07-04 12:24:01 +00:00
|
|
|
/* Allocate a handle for this session. */
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-07-04 12:24:01 +00:00
|
|
|
pcap_t *handle = malloc(sizeof(*handle));
|
2000-08-13 21:25:26 +00:00
|
|
|
if (handle == NULL) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
|
|
|
|
pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize some components of the pcap structure. */
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
memset(handle, 0, sizeof(*handle));
|
2000-06-08 06:40:09 +00:00
|
|
|
handle->snapshot = snaplen;
|
|
|
|
handle->md.timeout = to_ms;
|
2000-09-20 15:10:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NULL and "any" are special devices which give us the hint to
|
|
|
|
* monitor all devices.
|
|
|
|
*/
|
|
|
|
if (!device || strcmp(device, "any") == 0) {
|
|
|
|
device = NULL;
|
|
|
|
handle->md.device = strdup("any");
|
|
|
|
} else
|
|
|
|
handle->md.device = strdup(device);
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (handle->md.device == NULL) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE, "strdup: %s",
|
|
|
|
pcap_strerror(errno) );
|
2000-08-13 21:25:26 +00:00
|
|
|
free(handle);
|
2000-06-08 06:40:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
|
|
|
* Current Linux kernels use the protocol family PF_PACKET to
|
2000-06-08 06:40:09 +00:00
|
|
|
* allow direct access to all packets on the network while
|
|
|
|
* older kernels had a special socket type SOCK_PACKET to
|
|
|
|
* implement this feature.
|
|
|
|
* While this old implementation is kind of obsolete we need
|
|
|
|
* to be compatible with older kernels for a while so we are
|
2000-08-13 21:25:26 +00:00
|
|
|
* trying both methods with the newer method preferred.
|
|
|
|
*/
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (! (live_open_new(handle, device, promisc, to_ms, ebuf) ||
|
2000-06-08 06:40:09 +00:00
|
|
|
live_open_old(handle, device, promisc, to_ms, ebuf)) )
|
|
|
|
{
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
|
|
|
* Both methods to open the packet socket failed. Tidy
|
2000-06-08 06:40:09 +00:00
|
|
|
* up and report our failure (ebuf is expected to be
|
2000-08-13 21:25:26 +00:00
|
|
|
* set by the functions above).
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
|
2000-07-04 12:24:01 +00:00
|
|
|
free(handle->md.device);
|
2000-08-13 21:25:26 +00:00
|
|
|
free(handle);
|
2000-06-08 06:40:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
return handle;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Read at most max_packets from the capture stream and call the callback
|
|
|
|
* for each of them. Returns the number of packets handled or -1 if an
|
|
|
|
* error occured.
|
|
|
|
*/
|
1999-10-07 23:46:40 +00:00
|
|
|
int
|
2000-06-08 06:40:09 +00:00
|
|
|
pcap_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
|
1999-10-07 23:46:40 +00:00
|
|
|
{
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
2001-01-03 01:06:16 +00:00
|
|
|
* Currently, on Linux only one packet is delivered per read,
|
|
|
|
* so we don't loop.
|
2000-08-13 21:25:26 +00:00
|
|
|
*/
|
2001-01-03 01:06:16 +00:00
|
|
|
return pcap_read_packet(handle, callback, user);
|
2000-06-08 06:40:09 +00:00
|
|
|
}
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Read a packet from the socket calling the handler provided by
|
|
|
|
* the user. Returns the number of packets received or -1 if an
|
|
|
|
* error occured.
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
static int
|
2000-08-13 21:25:26 +00:00
|
|
|
pcap_read_packet(pcap_t *handle, pcap_handler callback, u_char *userdata)
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
2000-12-21 10:29:21 +00:00
|
|
|
int offset;
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
#ifdef HAVE_NETPACKET_PACKET_H
|
|
|
|
struct sockaddr_ll from;
|
2000-12-21 10:29:21 +00:00
|
|
|
struct sll_header *hdrp;
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
#else
|
2000-06-08 06:40:09 +00:00
|
|
|
struct sockaddr from;
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
#endif
|
2000-06-08 06:40:09 +00:00
|
|
|
socklen_t fromlen;
|
|
|
|
int packet_len, caplen;
|
|
|
|
struct pcap_pkthdr pcap_header;
|
|
|
|
|
2000-12-21 10:29:21 +00:00
|
|
|
#ifdef HAVE_NETPACKET_PACKET_H
|
|
|
|
/*
|
|
|
|
* If this is a cooked device, leave extra room for a
|
|
|
|
* fake packet header.
|
|
|
|
*/
|
|
|
|
if (handle->md.cooked)
|
|
|
|
offset = SLL_HDR_LEN;
|
|
|
|
else
|
|
|
|
offset = 0;
|
|
|
|
#else
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
2000-12-21 10:29:21 +00:00
|
|
|
* This system doesn't have PF_PACKET sockets, so it doesn't
|
|
|
|
* support cooked devices.
|
2000-08-13 21:25:26 +00:00
|
|
|
*/
|
2000-12-21 10:29:21 +00:00
|
|
|
offset = 0;
|
|
|
|
#endif
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/* Receive a single packet from the kernel */
|
2000-08-13 21:25:26 +00:00
|
|
|
|
1999-10-07 23:46:40 +00:00
|
|
|
do {
|
|
|
|
fromlen = sizeof(from);
|
2000-06-08 06:40:09 +00:00
|
|
|
packet_len = recvfrom(
|
2000-12-21 10:29:21 +00:00
|
|
|
handle->fd, handle->buffer + offset + handle->offset,
|
|
|
|
handle->md.readlen - offset, MSG_TRUNC,
|
2000-08-13 21:25:26 +00:00
|
|
|
(struct sockaddr *) &from, &fromlen);
|
|
|
|
} while (packet_len == -1 && errno == EINTR);
|
2000-06-08 06:40:09 +00:00
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
/* Check if an error occured */
|
|
|
|
|
|
|
|
if (packet_len == -1) {
|
|
|
|
if (errno == EAGAIN)
|
2000-06-08 06:40:09 +00:00
|
|
|
return 0; /* no packet there */
|
|
|
|
else {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(handle->errbuf, sizeof(handle->errbuf),
|
|
|
|
"recvfrom: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
2000-06-08 06:40:09 +00:00
|
|
|
}
|
|
|
|
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
#ifdef HAVE_NETPACKET_PACKET_H
|
|
|
|
/*
|
|
|
|
* If this is from the loopback device, reject outgoing packets;
|
|
|
|
* we'll see the packet as an incoming packet as well, and
|
|
|
|
* we don't want to see it twice.
|
|
|
|
*
|
|
|
|
* We can only do this if we're using PF_PACKET; the address
|
|
|
|
* returned for SOCK_PACKET is a "sockaddr_pkt" which lacks
|
|
|
|
* the relevant packet type information.
|
|
|
|
*/
|
|
|
|
if (!handle->md.sock_packet &&
|
|
|
|
from.sll_ifindex == handle->md.lo_ifindex &&
|
|
|
|
from.sll_pkttype == PACKET_OUTGOING)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2000-12-21 10:29:21 +00:00
|
|
|
#ifdef HAVE_NETPACKET_PACKET_H
|
|
|
|
/*
|
|
|
|
* If this is a cooked device, fill in the fake packet header.
|
|
|
|
*/
|
|
|
|
if (handle->md.cooked) {
|
|
|
|
/*
|
|
|
|
* Add the length of the fake header to the length
|
|
|
|
* of packet data we read.
|
|
|
|
*/
|
|
|
|
packet_len += SLL_HDR_LEN;
|
|
|
|
|
|
|
|
hdrp = (struct sll_header *)handle->buffer;
|
2000-12-22 12:11:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Map the PACKET_ value to a LINUX_SLL_ value; we
|
|
|
|
* want the same numerical value to be used in
|
|
|
|
* the link-layer header even if the numerical values
|
|
|
|
* for the PACKET_ #defines change, so that programs
|
|
|
|
* that look at the packet type field will always be
|
|
|
|
* able to handle DLT_LINUX_SLL captures.
|
|
|
|
*/
|
2000-12-23 03:04:06 +00:00
|
|
|
switch (from.sll_pkttype) {
|
2000-12-22 12:11:36 +00:00
|
|
|
|
|
|
|
case PACKET_HOST:
|
|
|
|
hdrp->sll_pkttype = htons(LINUX_SLL_HOST);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PACKET_BROADCAST:
|
|
|
|
hdrp->sll_pkttype = htons(LINUX_SLL_BROADCAST);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PACKET_MULTICAST:
|
|
|
|
hdrp->sll_pkttype = htons(LINUX_SLL_MULTICAST);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PACKET_OTHERHOST:
|
|
|
|
hdrp->sll_pkttype = htons(LINUX_SLL_OTHERHOST);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PACKET_OUTGOING:
|
|
|
|
hdrp->sll_pkttype = htons(LINUX_SLL_OUTGOING);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
hdrp->sll_pkttype = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-12-21 10:29:21 +00:00
|
|
|
hdrp->sll_hatype = htons(from.sll_hatype);
|
|
|
|
hdrp->sll_halen = htons(from.sll_halen);
|
|
|
|
memcpy(hdrp->sll_addr, from.sll_addr,
|
|
|
|
(from.sll_halen > SLL_ADDRLEN) ?
|
|
|
|
SLL_ADDRLEN :
|
|
|
|
from.sll_halen);
|
2000-12-23 07:50:18 +00:00
|
|
|
hdrp->sll_protocol = from.sll_protocol;
|
2000-12-21 10:29:21 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
|
|
|
* XXX: According to the kernel source we should get the real
|
2000-06-08 06:40:09 +00:00
|
|
|
* packet len if calling recvfrom with MSG_TRUNC set. It does
|
|
|
|
* not seem to work here :(, but it is supported by this code
|
2000-08-04 18:35:48 +00:00
|
|
|
* anyway.
|
|
|
|
* To be honest the code RELIES on that feature so this is really
|
|
|
|
* broken with 2.2.x kernels.
|
|
|
|
* I spend a day to figure out what's going on and I found out
|
|
|
|
* that the following is happening:
|
|
|
|
*
|
|
|
|
* The packet comes from a random interface and the packet_rcv
|
|
|
|
* hook is called with a clone of the packet. That code inserts
|
|
|
|
* the packet into the receive queue of the packet socket.
|
|
|
|
* If a filter is attached to that socket that filter is run
|
|
|
|
* first - and there lies the problem. The default filter always
|
|
|
|
* cuts the packet at the snaplen:
|
|
|
|
*
|
|
|
|
* # tcpdump -d
|
|
|
|
* (000) ret #68
|
|
|
|
*
|
|
|
|
* So the packet filter cuts down the packet. The recvfrom call
|
|
|
|
* says "hey, it's only 68 bytes, it fits into the buffer" with
|
|
|
|
* the result that we don't get the real packet length. This
|
|
|
|
* is valid at least until kernel 2.2.17pre6.
|
|
|
|
*
|
2000-12-21 10:29:21 +00:00
|
|
|
* We currently handle this by making a copy of the filter
|
|
|
|
* program, fixing all "ret" instructions with non-zero
|
|
|
|
* operands to have an operand of 65535 so that the filter
|
|
|
|
* doesn't truncate the packet, and supplying that modified
|
|
|
|
* filter to the kernel.
|
2000-08-13 21:25:26 +00:00
|
|
|
*/
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
caplen = packet_len;
|
2000-08-13 21:25:26 +00:00
|
|
|
if (caplen > handle->snapshot)
|
2000-06-08 06:40:09 +00:00
|
|
|
caplen = handle->snapshot;
|
|
|
|
|
|
|
|
/* Run the packet filter if not using kernel filter */
|
2000-08-13 21:25:26 +00:00
|
|
|
if (!handle->md.use_bpf && handle->fcode.bf_insns) {
|
|
|
|
if (bpf_filter(handle->fcode.bf_insns, handle->buffer,
|
|
|
|
packet_len, caplen) == 0)
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
|
|
|
/* rejected by filter */
|
|
|
|
return 0;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
|
|
|
}
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/* Fill in our own header data */
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (ioctl(handle->fd, SIOCGSTAMP, &pcap_header.ts) == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(handle->errbuf, sizeof(handle->errbuf),
|
|
|
|
"ioctl: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pcap_header.caplen = caplen;
|
|
|
|
pcap_header.len = packet_len;
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/* Call the user supplied callback function */
|
|
|
|
handle->md.stat.ps_recv++;
|
2000-08-13 21:25:26 +00:00
|
|
|
callback(userdata, &pcap_header, handle->buffer + handle->offset);
|
2000-06-08 06:40:09 +00:00
|
|
|
|
|
|
|
return 1;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Get the statistics for the given packet capture handle.
|
|
|
|
* FIXME: Currently does not report the number of dropped packets.
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
int
|
2000-08-13 21:25:26 +00:00
|
|
|
pcap_stats(pcap_t *handle, struct pcap_stat *stats)
|
1999-10-07 23:46:40 +00:00
|
|
|
{
|
2000-06-08 06:40:09 +00:00
|
|
|
*stats = handle->md.stat;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Attach the given BPF code to the packet capture device.
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
int
|
2000-08-13 21:25:26 +00:00
|
|
|
pcap_setfilter(pcap_t *handle, struct bpf_program *filter)
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
|
|
|
#ifdef SO_ATTACH_FILTER
|
|
|
|
struct sock_fprog fcode;
|
2000-12-21 10:29:21 +00:00
|
|
|
int can_filter_in_kernel;
|
2000-06-08 06:40:09 +00:00
|
|
|
#endif
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (!handle)
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
2000-08-13 21:25:26 +00:00
|
|
|
if (!filter) {
|
2000-07-04 12:24:01 +00:00
|
|
|
strncpy(handle->errbuf, "setfilter: No filter specified",
|
|
|
|
sizeof(handle->errbuf));
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
2000-06-08 06:40:09 +00:00
|
|
|
|
|
|
|
/* Make our private copy of the filter */
|
2000-08-13 21:25:26 +00:00
|
|
|
|
2000-10-28 00:01:26 +00:00
|
|
|
if (install_bpf_program(handle, filter) < 0) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(handle->errbuf, sizeof(handle->errbuf),
|
|
|
|
"malloc: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
2000-10-28 00:01:26 +00:00
|
|
|
}
|
2000-06-08 06:40:09 +00:00
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
|
|
|
* Run user level packet filter by default. Will be overriden if
|
|
|
|
* installing a kernel filter succeeds.
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
handle->md.use_bpf = 0;
|
|
|
|
|
2000-10-25 05:59:04 +00:00
|
|
|
/*
|
|
|
|
* If we're reading from a savefile, don't try to install
|
|
|
|
* a kernel filter.
|
|
|
|
*/
|
|
|
|
if (handle->sf.rfile != NULL)
|
|
|
|
return 0;
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/* Install kernel level filter if possible */
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
#ifdef SO_ATTACH_FILTER
|
2000-07-11 01:09:16 +00:00
|
|
|
#ifdef USHRT_MAX
|
2000-12-21 10:29:21 +00:00
|
|
|
if (handle->fcode.bf_len > USHRT_MAX) {
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
|
|
|
* fcode.len is an unsigned short for current kernel.
|
2000-12-21 10:29:21 +00:00
|
|
|
* I have yet to see BPF-Code with that much
|
|
|
|
* instructions but still it is possible. So for the
|
|
|
|
* sake of correctness I added this check.
|
2000-08-13 21:25:26 +00:00
|
|
|
*/
|
2000-12-21 10:29:21 +00:00
|
|
|
fprintf(stderr, "Warning: Filter too complex for kernel\n");
|
|
|
|
fcode.filter = NULL;
|
|
|
|
can_filter_in_kernel = 0;
|
|
|
|
} else
|
|
|
|
#endif /* USHRT_MAX */
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
2000-12-21 10:29:21 +00:00
|
|
|
/*
|
|
|
|
* Oh joy, the Linux kernel uses struct sock_fprog instead
|
|
|
|
* of struct bpf_program and of course the length field is
|
|
|
|
* of different size. Pointed out by Sebastian
|
|
|
|
*
|
|
|
|
* Oh, and we also need to fix it up so that all "ret"
|
|
|
|
* instructions with non-zero operands have 65535 as the
|
|
|
|
* operand, and so that, if we're in cooked mode, all
|
|
|
|
* memory-reference instructions use special magic offsets
|
|
|
|
* in references to the link-layer header and assume that
|
|
|
|
* the link-layer payload begins at 0; "fix_program()"
|
|
|
|
* will do that.
|
2000-08-13 21:25:26 +00:00
|
|
|
*/
|
2000-12-21 10:29:21 +00:00
|
|
|
switch (fix_program(handle, &fcode)) {
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* Fatal error; just quit.
|
|
|
|
* (The "default" case shouldn't happen; we
|
|
|
|
* return -1 for that reason.)
|
|
|
|
*/
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
/*
|
|
|
|
* The program performed checks that we can't make
|
|
|
|
* work in the kernel.
|
|
|
|
*/
|
|
|
|
can_filter_in_kernel = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
/*
|
|
|
|
* We have a filter that'll work in the kernel.
|
|
|
|
*/
|
|
|
|
can_filter_in_kernel = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (can_filter_in_kernel) {
|
|
|
|
if (setsockopt(handle->fd, SOL_SOCKET, SO_ATTACH_FILTER,
|
|
|
|
&fcode, sizeof(fcode)) == 0)
|
|
|
|
{
|
|
|
|
/* Installation succeded - using kernel filter. */
|
|
|
|
handle->md.use_bpf = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Print a warning if we weren't able to install
|
|
|
|
* the filter for a reason other than "this kernel
|
|
|
|
* isn't configured to support socket filters.
|
|
|
|
*/
|
|
|
|
if (errno != ENOPROTOOPT && errno != EOPNOTSUPP) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Warning: Kernel filter failed: %s\n",
|
|
|
|
pcap_strerror(errno));
|
|
|
|
}
|
2000-06-08 06:40:09 +00:00
|
|
|
}
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
2000-12-21 10:29:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free up the copy of the filter that was made by "fix_program()".
|
|
|
|
*/
|
|
|
|
if (fcode.filter != NULL)
|
|
|
|
free(fcode.filter);
|
|
|
|
#endif /* SO_ATTACH_FILTER */
|
2000-06-08 06:40:09 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
1999-10-07 23:46:40 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Linux uses the ARP hardware type to identify the type of an
|
Get rid of the PCAP_ENCAP_ values - if an application uses them, that
application won't build with any other version of libpcap, which means
that a lot of applications won't use them. In addition,
"pcap_linktype()" needs to return DLT_ values, so that platforms that
build libpcap as a shared library won't break binary compatibility if
they update to this version of libpcap.
Instead, we map from DLT_ values to LINKTYPE_ values when writing
savefiles, and map from LINKTYPE_ values to DLT_ values when reading
savefiles, so that savefiles don't have platform-dependent DLT_ values
in the header as the link type, they have platform-independent LINKTYPE_
values.
This means we don't need to make DLT_ATM_RFC1483, DLT_RAW, etc. have
platform-independent values starting at 100 - only the values in the
savefile header need to be like that.
2000-10-12 03:53:57 +00:00
|
|
|
* interface. pcap uses the DLT_xxx constants for this. This
|
2000-08-13 21:25:26 +00:00
|
|
|
* function maps the ARPHRD_xxx constant to an appropriate
|
Get rid of the PCAP_ENCAP_ values - if an application uses them, that
application won't build with any other version of libpcap, which means
that a lot of applications won't use them. In addition,
"pcap_linktype()" needs to return DLT_ values, so that platforms that
build libpcap as a shared library won't break binary compatibility if
they update to this version of libpcap.
Instead, we map from DLT_ values to LINKTYPE_ values when writing
savefiles, and map from LINKTYPE_ values to DLT_ values when reading
savefiles, so that savefiles don't have platform-dependent DLT_ values
in the header as the link type, they have platform-independent LINKTYPE_
values.
This means we don't need to make DLT_ATM_RFC1483, DLT_RAW, etc. have
platform-independent values starting at 100 - only the values in the
savefile header need to be like that.
2000-10-12 03:53:57 +00:00
|
|
|
* DLT_xxx constant.
|
2000-08-13 21:25:26 +00:00
|
|
|
*
|
2000-12-21 10:29:21 +00:00
|
|
|
* Returns -1 if unable to map the type; we print a message and,
|
|
|
|
* if we're using PF_PACKET/SOCK_RAW rather than PF_INET/SOCK_PACKET,
|
|
|
|
* we fall back on using PF_PACKET/SOCK_DGRAM.
|
2000-08-13 21:25:26 +00:00
|
|
|
*/
|
|
|
|
static int map_arphrd_to_dlt(int arptype)
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
2000-08-13 21:25:26 +00:00
|
|
|
switch (arptype) {
|
1999-10-07 23:46:40 +00:00
|
|
|
case ARPHRD_ETHER:
|
|
|
|
case ARPHRD_METRICOM:
|
Get rid of the PCAP_ENCAP_ values - if an application uses them, that
application won't build with any other version of libpcap, which means
that a lot of applications won't use them. In addition,
"pcap_linktype()" needs to return DLT_ values, so that platforms that
build libpcap as a shared library won't break binary compatibility if
they update to this version of libpcap.
Instead, we map from DLT_ values to LINKTYPE_ values when writing
savefiles, and map from LINKTYPE_ values to DLT_ values when reading
savefiles, so that savefiles don't have platform-dependent DLT_ values
in the header as the link type, they have platform-independent LINKTYPE_
values.
This means we don't need to make DLT_ATM_RFC1483, DLT_RAW, etc. have
platform-independent values starting at 100 - only the values in the
savefile header need to be like that.
2000-10-12 03:53:57 +00:00
|
|
|
case ARPHRD_LOOPBACK: return DLT_EN10MB;
|
|
|
|
case ARPHRD_EETHER: return DLT_EN3MB;
|
|
|
|
case ARPHRD_AX25: return DLT_AX25;
|
|
|
|
case ARPHRD_PRONET: return DLT_PRONET;
|
|
|
|
case ARPHRD_CHAOS: return DLT_CHAOS;
|
2000-12-03 09:07:27 +00:00
|
|
|
#ifndef ARPHRD_IEEE802_TR
|
|
|
|
#define ARPHRD_IEEE802_TR 800 /* From Linux 2.4 */
|
|
|
|
#endif
|
|
|
|
case ARPHRD_IEEE802_TR:
|
Get rid of the PCAP_ENCAP_ values - if an application uses them, that
application won't build with any other version of libpcap, which means
that a lot of applications won't use them. In addition,
"pcap_linktype()" needs to return DLT_ values, so that platforms that
build libpcap as a shared library won't break binary compatibility if
they update to this version of libpcap.
Instead, we map from DLT_ values to LINKTYPE_ values when writing
savefiles, and map from LINKTYPE_ values to DLT_ values when reading
savefiles, so that savefiles don't have platform-dependent DLT_ values
in the header as the link type, they have platform-independent LINKTYPE_
values.
This means we don't need to make DLT_ATM_RFC1483, DLT_RAW, etc. have
platform-independent values starting at 100 - only the values in the
savefile header need to be like that.
2000-10-12 03:53:57 +00:00
|
|
|
case ARPHRD_IEEE802: return DLT_IEEE802;
|
|
|
|
case ARPHRD_ARCNET: return DLT_ARCNET;
|
|
|
|
case ARPHRD_FDDI: return DLT_FDDI;
|
Introduce a set of PCAP_ENCAP_ codes to specify packet encapsulations.
For those PCAP_ENCAP_ codes corresponding to DLT_ codes that are
(believed to be) the same in all BSDs, the PCAP_ENCAP_ codes have the
same values as the corresponding DLT_ codes.
For those PCAP_ENCAP_ codes corresponding to DLT_ codes that were added
in libpcap 0.5 as "non-kernel" DLT_ codes, or had their values changed
in libpcap 0.5 in order to cope with the fact that those DLT_ codes
have different values in different systems, the PCAP_ENCAP_ codes have
the same values as the corresponding DLT_ codes.
We add some additional PCAP_ENCAP_ codes to handle IEEE 802.11 (which
currently has its link-layer information turned into an Ethernet header
by at least some of the BSDs, but John Hawkinson at MIT wants to add a
DLT_ value for 802.11 and pass up the full link-layer header) and the
Classical IP encapsulation for ATM on Linux (which isn't always the same
as DLT_ATM_RFC1483, from what I can tell, alas).
"pcap-bpf.c" maps DLT_ codes to PCAP_ENCAP_ codes, so as not to supply
to libpcap's callers any DLT_ codes other than the ones that have the
same values on all platforms; it supplies PCAP_ENCAP_ codes for all
others.
In libpcap's "bpf/net/bpf.h", we define the DLT_ values that aren't the
same on all platforms with the new values starting at 100 (to keep them
out of the way of the values various BSDs might assign to them), as we
did in 0.5, but do so only if they're not already defined; platforms
with <net/bpf.h> headers that come with the kernel (e.g., the BSDs)
should define them with the values that they have always had on that
platform, *not* with the values we used in 0.5.
(Code using this version of libpcap should check for the new PCAP_ENCAP_
codes; those are given the values that the corresponding DLT_ values had
in 0.5, so code that checks for them will handle 0.5 libpcap files
correctly even if the platform defines DLT_RAW, say, as something other
than 101. If that code also checks for DLT_RAW - which means it can't
just use a switch statement, as DLT_RAW might be defined as 101 if the
platform doesn't itself define DLT_RAW with some other value - then it
will also handle old DLT_RAW captures, as long as they were made on the
same platform or on another platform that used the same value for
DLT_RAW. It can't handle captures from a platform that uses that value
for another DLT_ code, but that's always been the case, and isn't easily
fixable.)
The intent here is to decouple the values that are returned by
"pcap_datalink()" and put into the header of tcpdump/libpcap save files
from the DLT_ values returned by BIOCGDLT in BSD kernels, allowing the
BSDs to assign values to DLT_ codes, in their kernels, as they choose,
without creating more incompatibilities between tcpdump/libpcap save
files from different platforms.
2000-09-17 04:04:36 +00:00
|
|
|
|
|
|
|
#ifndef ARPHRD_ATM /* FIXME: How to #include this? */
|
|
|
|
#define ARPHRD_ATM 19
|
|
|
|
#endif
|
Get rid of the PCAP_ENCAP_ values - if an application uses them, that
application won't build with any other version of libpcap, which means
that a lot of applications won't use them. In addition,
"pcap_linktype()" needs to return DLT_ values, so that platforms that
build libpcap as a shared library won't break binary compatibility if
they update to this version of libpcap.
Instead, we map from DLT_ values to LINKTYPE_ values when writing
savefiles, and map from LINKTYPE_ values to DLT_ values when reading
savefiles, so that savefiles don't have platform-dependent DLT_ values
in the header as the link type, they have platform-independent LINKTYPE_
values.
This means we don't need to make DLT_ATM_RFC1483, DLT_RAW, etc. have
platform-independent values starting at 100 - only the values in the
savefile header need to be like that.
2000-10-12 03:53:57 +00:00
|
|
|
case ARPHRD_ATM: return DLT_ATM_CLIP;
|
2000-06-08 06:40:09 +00:00
|
|
|
|
|
|
|
case ARPHRD_PPP:
|
2000-12-22 11:53:27 +00:00
|
|
|
/* Not sure if this is correct for all tunnels, but it
|
|
|
|
* works for CIPE */
|
|
|
|
case ARPHRD_TUNNEL:
|
|
|
|
case ARPHRD_SIT:
|
1999-10-07 23:46:40 +00:00
|
|
|
case ARPHRD_CSLIP:
|
|
|
|
case ARPHRD_SLIP6:
|
|
|
|
case ARPHRD_CSLIP6:
|
Get rid of the PCAP_ENCAP_ values - if an application uses them, that
application won't build with any other version of libpcap, which means
that a lot of applications won't use them. In addition,
"pcap_linktype()" needs to return DLT_ values, so that platforms that
build libpcap as a shared library won't break binary compatibility if
they update to this version of libpcap.
Instead, we map from DLT_ values to LINKTYPE_ values when writing
savefiles, and map from LINKTYPE_ values to DLT_ values when reading
savefiles, so that savefiles don't have platform-dependent DLT_ values
in the header as the link type, they have platform-independent LINKTYPE_
values.
This means we don't need to make DLT_ATM_RFC1483, DLT_RAW, etc. have
platform-independent values starting at 100 - only the values in the
savefile header need to be like that.
2000-10-12 03:53:57 +00:00
|
|
|
case ARPHRD_SLIP: return DLT_RAW;
|
2000-06-08 06:40:09 +00:00
|
|
|
}
|
1999-10-07 23:46:40 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ===== Functions to interface to the newer kernels ================== */
|
|
|
|
|
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Try to open a packet socket using the new kernel interface.
|
|
|
|
* Returns 0 on failure.
|
|
|
|
* FIXME: 0 uses to mean success (Sebastian)
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
static int
|
2000-08-13 21:25:26 +00:00
|
|
|
live_open_new(pcap_t *handle, char *device, int promisc,
|
|
|
|
int to_ms, char *ebuf)
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_NETPACKET_PACKET_H
|
|
|
|
int sock_fd = -1, device_id, mtu, arptype;
|
|
|
|
struct packet_mreq mr;
|
|
|
|
|
|
|
|
/* One shot loop used for error handling - bail out with break */
|
|
|
|
|
|
|
|
do {
|
2000-09-20 15:10:28 +00:00
|
|
|
/*
|
|
|
|
* Open a socket with protocol family packet. If a device is
|
|
|
|
* given we try to open it in raw mode otherwise we use
|
|
|
|
* the cooked interface.
|
|
|
|
*/
|
|
|
|
sock_fd = device ?
|
|
|
|
socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
|
|
|
|
: socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (sock_fd == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE, "socket: %s",
|
|
|
|
pcap_strerror(errno) );
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It seems the kernel supports the new interface. */
|
|
|
|
handle->md.sock_packet = 0;
|
|
|
|
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
/*
|
|
|
|
* Get the interface index of the loopback device.
|
|
|
|
* If the attempt fails, don't fail, just set the
|
|
|
|
* "md.lo_ifindex" to -1.
|
|
|
|
*
|
|
|
|
* XXX - can there be more than one device that loops
|
|
|
|
* packets back, i.e. devices other than "lo"? If so,
|
|
|
|
* we'd need to find them all, and have an array of
|
|
|
|
* indices for them, and check all of them in
|
|
|
|
* "pcap_read_packet()".
|
|
|
|
*/
|
|
|
|
handle->md.lo_ifindex = iface_get_id(sock_fd, "lo", ebuf);
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
|
|
|
* What kind of frames do we have to deal with? Fall back
|
|
|
|
* to cooked mode if we have an unknown interface type.
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
|
2000-09-20 15:10:28 +00:00
|
|
|
if (device) {
|
2000-12-21 10:29:21 +00:00
|
|
|
/* Assume for now we don't need cooked mode. */
|
|
|
|
handle->md.cooked = 0;
|
|
|
|
|
2000-09-20 15:10:28 +00:00
|
|
|
arptype = iface_get_arptype(sock_fd, device, ebuf);
|
|
|
|
if (arptype == -1)
|
|
|
|
break;
|
|
|
|
handle->linktype = map_arphrd_to_dlt(arptype);
|
2000-12-21 10:29:21 +00:00
|
|
|
if (handle->linktype == -1 ||
|
|
|
|
(handle->linktype == DLT_EN10MB &&
|
|
|
|
(strncmp("isdn", device, 4) == 0 ||
|
2000-12-22 12:30:04 +00:00
|
|
|
strncmp("isdY", device, 4) == 0)) ||
|
|
|
|
(handle->linktype == DLT_RAW &&
|
|
|
|
(strncmp("ippp", device, 4) == 0))) {
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
/*
|
2000-12-21 10:29:21 +00:00
|
|
|
* Unknown interface type (-1), or an ISDN
|
|
|
|
* device (whose link-layer type we
|
|
|
|
* can only determine by using APIs
|
|
|
|
* that may be different on different
|
|
|
|
* kernels) - reopen in cooked mode.
|
|
|
|
*
|
|
|
|
* XXX - do that with DLT_RAW as well?
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
*/
|
|
|
|
if (close(sock_fd) == -1) {
|
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"close: %s", pcap_strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sock_fd = socket(PF_PACKET, SOCK_DGRAM,
|
|
|
|
htons(ETH_P_ALL));
|
|
|
|
if (sock_fd == -1) {
|
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"socket: %s", pcap_strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2000-12-21 10:29:21 +00:00
|
|
|
handle->md.cooked = 1;
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
|
2000-12-21 10:29:21 +00:00
|
|
|
if (handle->linktype == -1) {
|
|
|
|
/*
|
|
|
|
* Warn that we're falling back on
|
|
|
|
* cooked mode; we may want to
|
|
|
|
* update "map_arphrd_to_dlt()"
|
|
|
|
* to handle the new type.
|
|
|
|
*/
|
|
|
|
fprintf(stderr,
|
|
|
|
"Warning: arptype %d not "
|
|
|
|
"supported by libpcap - "
|
|
|
|
"falling back to cooked "
|
|
|
|
"socket\n",
|
|
|
|
arptype);
|
|
|
|
}
|
|
|
|
handle->linktype = DLT_LINUX_SLL;
|
2000-06-08 06:40:09 +00:00
|
|
|
}
|
|
|
|
|
2000-09-20 15:10:28 +00:00
|
|
|
device_id = iface_get_id(sock_fd, device, ebuf);
|
|
|
|
if (device_id == -1)
|
|
|
|
break;
|
2000-06-08 06:40:09 +00:00
|
|
|
|
2000-09-20 15:10:28 +00:00
|
|
|
if (iface_bind(sock_fd, device_id, ebuf) == -1)
|
|
|
|
break;
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
} else {
|
2000-12-21 10:29:21 +00:00
|
|
|
/*
|
|
|
|
* This is cooked mode.
|
|
|
|
*/
|
|
|
|
handle->md.cooked = 1;
|
|
|
|
handle->linktype = DLT_LINUX_SLL;
|
If we're receiving packets from a PF_PACKET socket, check whether the
interface index of the interface for the packet is the interface index
of the loopback interface and, if it is, check if the packet is an
outgoing packet; if so, ignore it, as we'll also be seeing that packet
as a received packet.
If we don't handle the arphrd type of an interface, and fall back on
cooked mode, report the arphrd type, so we know what type we should
consider supporting (if that type can't be supported well, e.g. if you
don't get any link-layer header, as happens with PPP, we'd be silent).
2000-10-25 07:46:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX - squelch GCC complaints about
|
|
|
|
* uninitialized variables; if we can't
|
|
|
|
* select promiscuous mode on all interfaces,
|
|
|
|
* we should move the code below into the
|
|
|
|
* "if (device)" branch of the "if" and
|
|
|
|
* get rid of the next statement.
|
|
|
|
*/
|
|
|
|
device_id = -1;
|
2000-09-20 15:10:28 +00:00
|
|
|
}
|
2000-06-08 06:40:09 +00:00
|
|
|
|
2000-10-25 05:59:04 +00:00
|
|
|
/* Select promiscuous mode on/off */
|
2000-06-08 06:40:09 +00:00
|
|
|
|
2000-07-11 01:09:16 +00:00
|
|
|
#ifdef SOL_PACKET
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
2000-09-20 15:10:28 +00:00
|
|
|
* Hmm, how can we set promiscuous mode on all interfaces?
|
|
|
|
* I am not sure if that is possible at all.
|
2000-08-13 21:25:26 +00:00
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
|
2000-09-20 15:10:28 +00:00
|
|
|
if (device) {
|
|
|
|
memset(&mr, 0, sizeof(mr));
|
|
|
|
mr.mr_ifindex = device_id;
|
|
|
|
mr.mr_type = promisc ?
|
|
|
|
PACKET_MR_PROMISC : PACKET_MR_ALLMULTI;
|
|
|
|
if (setsockopt(sock_fd, SOL_PACKET,
|
|
|
|
PACKET_ADD_MEMBERSHIP, &mr, sizeof(mr)) == -1)
|
|
|
|
{
|
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"setsockopt: %s", pcap_strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2000-06-08 06:40:09 +00:00
|
|
|
}
|
2000-07-11 01:09:16 +00:00
|
|
|
#endif
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/* Compute the buffersize */
|
|
|
|
|
|
|
|
mtu = iface_get_mtu(sock_fd, device, ebuf);
|
2000-09-20 15:10:28 +00:00
|
|
|
if (mtu == -1)
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
|
|
|
handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/* Fill in the pcap structure */
|
|
|
|
|
|
|
|
handle->fd = sock_fd;
|
|
|
|
handle->offset = 0;
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
handle->buffer = malloc(handle->bufsize);
|
|
|
|
if (!handle->buffer) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"malloc: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
/*
|
|
|
|
* This is a 2.2 or later kernel, as it has PF_PACKET;
|
|
|
|
* "recvfrom()", when passed the MSG_TRUNC flag, will
|
|
|
|
* return the actual length of the packet, not the
|
|
|
|
* number of bytes from the packet copied to userland,
|
|
|
|
* so we can safely pass it a byte count based on the
|
|
|
|
* snapshot length.
|
|
|
|
*/
|
|
|
|
handle->md.readlen = handle->snapshot;
|
2000-06-08 06:40:09 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
} while(0);
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (sock_fd != -1)
|
|
|
|
close(sock_fd);
|
2000-06-08 06:40:09 +00:00
|
|
|
return 0;
|
|
|
|
#else
|
2000-07-04 12:24:01 +00:00
|
|
|
strncpy(ebuf,
|
|
|
|
"New packet capturing interface not supported by build "
|
|
|
|
"environment", PCAP_ERRBUF_SIZE);
|
2000-06-08 06:40:09 +00:00
|
|
|
return 0;
|
1999-10-07 23:46:40 +00:00
|
|
|
#endif
|
2000-06-08 06:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_NETPACKET_PACKET_H
|
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Return the index of the given device name. Fill ebuf and return
|
|
|
|
* -1 on failure.
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
static int
|
2000-08-13 21:25:26 +00:00
|
|
|
iface_get_id(int fd, const char *device, char *ebuf)
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
|
1999-10-07 23:46:40 +00:00
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"ioctl: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
return ifr.ifr_ifindex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Bind the socket associated with FD to the given device.
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
static int
|
2000-08-13 21:25:26 +00:00
|
|
|
iface_bind(int fd, int ifindex, char *ebuf)
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_ll sll;
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
memset(&sll, 0, sizeof(sll));
|
2000-06-08 06:40:09 +00:00
|
|
|
sll.sll_family = AF_PACKET;
|
|
|
|
sll.sll_ifindex = ifindex;
|
|
|
|
sll.sll_protocol = htons(ETH_P_ALL);
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"bind: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1999-10-07 23:46:40 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/* ===== Functions to interface to the older kernels ================== */
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
/*
|
|
|
|
* With older kernels promiscuous mode is kind of interesting because we
|
2000-06-08 06:40:09 +00:00
|
|
|
* have to reset the interface before exiting. The problem can't really
|
|
|
|
* be solved without some daemon taking care of managing usage counts.
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
* If we put the interface into promiscuous mode, we set a flag indicating
|
|
|
|
* that we must take it out of that mode when the interface is closed,
|
|
|
|
* and, when closing the interface, if that flag is set we take it out
|
|
|
|
* of promiscuous mode.
|
2000-08-13 21:25:26 +00:00
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
/*
|
|
|
|
* List of pcaps for which we turned promiscuous mode on by hand.
|
|
|
|
* If there are any such pcaps, we arrange to call "pcap_close_all()"
|
|
|
|
* when we exit, and have it close all of them to turn promiscuous mode
|
|
|
|
* off.
|
2000-08-13 21:25:26 +00:00
|
|
|
*/
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
static struct pcap *pcaps_to_close;
|
2000-06-08 06:40:09 +00:00
|
|
|
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
/*
|
|
|
|
* TRUE if we've already called "atexit()" to cause "pcap_close_all()" to
|
|
|
|
* be called on exit.
|
|
|
|
*/
|
|
|
|
static int did_atexit;
|
|
|
|
|
|
|
|
static void pcap_close_all(void)
|
|
|
|
{
|
|
|
|
struct pcap *handle;
|
|
|
|
|
|
|
|
while ((handle = pcaps_to_close) != NULL)
|
|
|
|
pcap_close(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcap_close_linux( pcap_t *handle )
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
struct pcap *p, *prevp;
|
|
|
|
struct ifreq ifr;
|
2000-06-08 06:40:09 +00:00
|
|
|
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
if (handle->md.clear_promisc) {
|
|
|
|
/*
|
|
|
|
* We put the interface into promiscuous mode; take
|
|
|
|
* it out of promiscuous mode.
|
|
|
|
*
|
|
|
|
* XXX - if somebody else wants it in promiscuous mode,
|
|
|
|
* this code cannot know that, so it'll take it out
|
|
|
|
* of promiscuous mode. That's not fixable in 2.0[.x]
|
|
|
|
* kernels.
|
|
|
|
*/
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strncpy(ifr.ifr_name, handle->md.device, sizeof(ifr.ifr_name));
|
|
|
|
if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Can't restore interface flags (SIOCGIFFLAGS failed: %s).\n"
|
|
|
|
"Please adjust manually.\n"
|
|
|
|
"Hint: This can't happen with Linux >= 2.2.0.\n",
|
|
|
|
strerror(errno));
|
|
|
|
} else {
|
|
|
|
if (ifr.ifr_flags & IFF_PROMISC) {
|
|
|
|
/*
|
|
|
|
* Promiscuous mode is currently on; turn it
|
|
|
|
* off.
|
|
|
|
*/
|
|
|
|
ifr.ifr_flags &= ~IFF_PROMISC;
|
|
|
|
if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Can't restore interface flags (SIOCSIFFLAGS failed: %s).\n"
|
|
|
|
"Please adjust manually.\n"
|
|
|
|
"Hint: This can't happen with Linux >= 2.2.0.\n",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-06-08 06:40:09 +00:00
|
|
|
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
/*
|
|
|
|
* Take this pcap out of the list of pcaps for which we
|
|
|
|
* have to take the interface out of promiscuous mode.
|
|
|
|
*/
|
|
|
|
for (p = pcaps_to_close, prevp = NULL; p != NULL;
|
|
|
|
prevp = p, p = p->md.next) {
|
|
|
|
if (p == handle) {
|
|
|
|
/*
|
|
|
|
* Found it. Remove it from the list.
|
|
|
|
*/
|
|
|
|
if (prevp == NULL) {
|
|
|
|
/*
|
|
|
|
* It was at the head of the list.
|
|
|
|
*/
|
|
|
|
pcaps_to_close = p->md.next;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* It was in the middle of the list.
|
|
|
|
*/
|
|
|
|
prevp->md.next = p->md.next;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
if (handle->md.device != NULL)
|
|
|
|
free(handle->md.device);
|
2000-06-08 06:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Try to open a packet socket using the old kernel interface.
|
|
|
|
* Returns 0 on failure.
|
|
|
|
* FIXME: 0 uses to mean success (Sebastian)
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
static int
|
2000-08-13 21:25:26 +00:00
|
|
|
live_open_old(pcap_t *handle, char *device, int promisc,
|
|
|
|
int to_ms, char *ebuf)
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
|
|
|
int sock_fd = -1, mtu, arptype;
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
struct utsname utsname;
|
2000-06-08 06:40:09 +00:00
|
|
|
struct ifreq ifr;
|
1999-10-07 23:46:40 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
do {
|
|
|
|
/* Open the socket */
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
|
|
|
|
if (sock_fd == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"socket: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
2000-06-08 06:40:09 +00:00
|
|
|
|
|
|
|
/* It worked - we are using the old interface */
|
|
|
|
handle->md.sock_packet = 1;
|
|
|
|
|
2000-12-21 10:29:21 +00:00
|
|
|
/* ...which means we get the link-layer header. */
|
|
|
|
handle->md.cooked = 0;
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/* Bind to the given device */
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (!device) {
|
2000-12-18 00:20:51 +00:00
|
|
|
strncpy(ebuf, "pcap_open_live: The \"any\" device isn't supported on 2.0[.x]-kernel systems",
|
2000-07-04 12:24:01 +00:00
|
|
|
PCAP_ERRBUF_SIZE);
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2000-08-13 21:25:26 +00:00
|
|
|
if (iface_bind_old(sock_fd, device, ebuf) == -1)
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Go to promisc mode */
|
2000-08-13 21:25:26 +00:00
|
|
|
if (promisc) {
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
|
|
|
|
if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"ioctl: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2000-08-13 21:25:26 +00:00
|
|
|
if ((ifr.ifr_flags & IFF_PROMISC) == 0) {
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
/*
|
|
|
|
* Promiscuous mode isn't currently on,
|
|
|
|
* so turn it on, and remember that
|
|
|
|
* we should turn it off when the
|
|
|
|
* pcap_t is closed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we haven't already done so, arrange
|
|
|
|
* to have "pcap_close_all()" called when
|
|
|
|
* we exit.
|
|
|
|
*/
|
|
|
|
if (!did_atexit) {
|
|
|
|
if (atexit(pcap_close_all) == -1) {
|
|
|
|
/*
|
|
|
|
* "atexit()" failed; don't
|
|
|
|
* put the interface in
|
|
|
|
* promiscuous mode, just
|
|
|
|
* give up.
|
|
|
|
*/
|
|
|
|
strncpy(ebuf, "atexit failed",
|
|
|
|
PCAP_ERRBUF_SIZE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
ifr.ifr_flags |= IFF_PROMISC;
|
2000-08-13 21:25:26 +00:00
|
|
|
if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"ioctl: %s",
|
|
|
|
pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
|
|
|
}
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
handle->md.clear_promisc = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add this to the list of pcaps
|
|
|
|
* to close when we exit.
|
|
|
|
*/
|
|
|
|
handle->md.next = pcaps_to_close;
|
|
|
|
pcaps_to_close = handle;
|
2000-06-08 06:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the buffersize */
|
|
|
|
|
|
|
|
mtu = iface_get_mtu(sock_fd, device, ebuf);
|
2000-08-13 21:25:26 +00:00
|
|
|
if (mtu == -1)
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
|
|
|
handle->bufsize = MAX_LINKHEADER_SIZE + mtu;
|
2000-10-20 06:55:28 +00:00
|
|
|
if (handle->bufsize < handle->snapshot)
|
|
|
|
handle->bufsize = handle->snapshot;
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/* All done - fill in the pcap handle */
|
|
|
|
|
|
|
|
arptype = iface_get_arptype(sock_fd, device, ebuf);
|
2000-08-13 21:25:26 +00:00
|
|
|
if (arptype == -1)
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
handle->fd = sock_fd;
|
|
|
|
handle->offset = 0;
|
2000-08-13 21:25:26 +00:00
|
|
|
handle->linktype = map_arphrd_to_dlt(arptype);
|
2000-12-21 10:29:21 +00:00
|
|
|
/*
|
|
|
|
* XXX - handle ISDN types here? We can't fall back on
|
|
|
|
* cooked sockets, so we'd have to figure out from the
|
|
|
|
* device name what type of link-layer encapsulation
|
|
|
|
* it's using, and map that to an appropriate DLT_
|
|
|
|
* value, meaning we'd map "isdnN" devices to DLT_RAW
|
|
|
|
* (they supply raw IP packets with no link-layer
|
|
|
|
* header) and "isdY" devices to a new DLT_I4L_IP
|
|
|
|
* type that has only an Ethernet packet type as
|
|
|
|
* a link-layer header.
|
|
|
|
*/
|
2000-09-20 15:10:28 +00:00
|
|
|
if (handle->linktype == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"interface type of %s not supported", device);
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2000-08-13 21:25:26 +00:00
|
|
|
handle->buffer = malloc(handle->bufsize);
|
|
|
|
if (!handle->buffer) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"malloc: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
break;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
|
|
|
|
Clean up promiscuous mode, when using SOCK_PACKET, as best we can;
remember which pcap_t's were opened (with SOCK_PACKET) in promiscuous
mode on interfaces not already in promiscuous mode, turn promiscuous
mode off when closing such a pcap_t, and arrange that, when the program
exits, all pcap_t's of that sort not already closed have their
interfaces taken out of promiscuous mode. (It's not sufficient to do
this on exit - applications may close a pcap_t without exiting, e.g.
Ethereal.)
This won't always work right (if somebody else requests promiscuous mode
after it's opened by libpcap, we'll turn promiscuous mode off when we
close the pcap_t, and if the program doesn't exit cleanly, it won't
clean up the interfaces), but neither of those problems are fixable -
the only way to get things to work correctly is to use PF_PACKET
sockets, which requires a 2.2 or later kernel.
On a 2.0[.x] kernel, when doing a "recvfrom()" on a SOCK_PACKET socket
to read a captured packet, don't pass a byte count value based on the
snapshot length - "recvfrom()" won't return the actual packet length if
you do that. (2.2 and later kernels will return the actual packet
length if MSG_TRUNC is passed in.)
2000-12-16 10:43:26 +00:00
|
|
|
/*
|
|
|
|
* This might be a 2.0[.x] kernel - check.
|
|
|
|
*/
|
|
|
|
if (uname(&utsname) < 0 ||
|
|
|
|
strncmp(utsname.release, "2.0", 3) == 0) {
|
|
|
|
/*
|
|
|
|
* Either we couldn't find out what kernel release
|
|
|
|
* this is, or it's a 2.0[.x] kernel.
|
|
|
|
*
|
|
|
|
* In the 2.0[.x] kernel, a "recvfrom()" on
|
|
|
|
* a SOCK_PACKET socket, with MSG_TRUNC set, will
|
|
|
|
* return the number of bytes read, so if we pass
|
|
|
|
* a length based on the snapshot length, it'll
|
|
|
|
* return the number of bytes from the packet
|
|
|
|
* copied to userland, not the actual length
|
|
|
|
* of the packet.
|
|
|
|
*
|
|
|
|
* This means that, for example, the IP dissector
|
|
|
|
* in tcpdump will get handed a packet length less
|
|
|
|
* than the length in the IP header, and will
|
|
|
|
* complain about "truncated-ip".
|
|
|
|
*
|
|
|
|
* So we don't bother trying to copy from the
|
|
|
|
* kernel only the bytes in which we're interested,
|
|
|
|
* but instead copy them all, just as the older
|
|
|
|
* versions of libpcap for Linux did.
|
|
|
|
*
|
|
|
|
* Just one of many problems with packet capture
|
|
|
|
* on 2.0[.x] kernels; you really want a 2.2[.x]
|
|
|
|
* or later kernel if you want packet capture to
|
|
|
|
* work well.
|
|
|
|
*/
|
|
|
|
handle->md.readlen = handle->bufsize;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* This is a 2.2[.x] or later kernel (although
|
|
|
|
* why we're using SOCK_PACKET on such a system
|
|
|
|
* is unknown to me).
|
|
|
|
*
|
|
|
|
* We can safely pass "recvfrom()" a byte count
|
|
|
|
* based on the snapshot length.
|
|
|
|
*/
|
|
|
|
handle->md.readlen = handle->snapshot;
|
|
|
|
}
|
2000-06-08 06:40:09 +00:00
|
|
|
return 1;
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
} while (0);
|
2000-10-25 06:59:09 +00:00
|
|
|
|
2000-09-20 15:10:28 +00:00
|
|
|
if (sock_fd != -1)
|
2000-08-13 21:25:26 +00:00
|
|
|
close(sock_fd);
|
2000-06-08 06:40:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Bind the socket associated with FD to the given device using the
|
|
|
|
* interface of the old kernels.
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
static int
|
2000-08-13 21:25:26 +00:00
|
|
|
iface_bind_old(int fd, const char *device, char *ebuf)
|
2000-06-08 06:40:09 +00:00
|
|
|
{
|
|
|
|
struct sockaddr saddr;
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
memset(&saddr, 0, sizeof(saddr));
|
|
|
|
strncpy(saddr.sa_data, device, sizeof(saddr.sa_data));
|
|
|
|
if (bind(fd, &saddr, sizeof(saddr)) == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"bind: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
2000-06-08 06:40:09 +00:00
|
|
|
|
|
|
|
return 0;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
|
|
|
|
/* ===== System calls available on all supported kernels ============== */
|
|
|
|
|
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Query the kernel for the MTU of the given interface.
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
static int
|
2000-08-13 21:25:26 +00:00
|
|
|
iface_get_mtu(int fd, const char *device, char *ebuf)
|
1999-10-07 23:46:40 +00:00
|
|
|
{
|
2000-06-08 06:40:09 +00:00
|
|
|
struct ifreq ifr;
|
|
|
|
|
2000-09-20 15:10:28 +00:00
|
|
|
if (!device)
|
|
|
|
return BIGGER_THAN_ALL_MTUS;
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
|
1999-10-07 23:46:40 +00:00
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (ioctl(fd, SIOCGIFMTU, &ifr) == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"ioctl: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ifr.ifr_mtu;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
|
|
|
|
2000-06-08 06:40:09 +00:00
|
|
|
/*
|
2000-08-13 21:25:26 +00:00
|
|
|
* Get the hardware type of the given interface as ARPHRD_xxx constant.
|
|
|
|
*/
|
2000-06-08 06:40:09 +00:00
|
|
|
static int
|
2000-08-13 21:25:26 +00:00
|
|
|
iface_get_arptype(int fd, const char *device, char *ebuf)
|
1999-10-07 23:46:40 +00:00
|
|
|
{
|
2000-06-08 06:40:09 +00:00
|
|
|
struct ifreq ifr;
|
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
|
1999-10-07 23:46:40 +00:00
|
|
|
|
2000-08-13 21:25:26 +00:00
|
|
|
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) {
|
2000-07-04 12:24:01 +00:00
|
|
|
snprintf(ebuf, PCAP_ERRBUF_SIZE,
|
|
|
|
"ioctl: %s", pcap_strerror(errno));
|
2000-06-08 06:40:09 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ifr.ifr_hwaddr.sa_family;
|
1999-10-07 23:46:40 +00:00
|
|
|
}
|
2000-12-21 10:29:21 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_NETPACKET_PACKET_H
|
|
|
|
static int
|
|
|
|
fix_program(pcap_t *handle, struct sock_fprog *fcode)
|
|
|
|
{
|
|
|
|
size_t prog_size;
|
|
|
|
register int i;
|
|
|
|
register struct bpf_insn *p;
|
|
|
|
struct bpf_insn *f;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a copy of the filter, and modify that copy if
|
|
|
|
* necessary.
|
|
|
|
*/
|
|
|
|
prog_size = sizeof(*handle->fcode.bf_insns) * handle->fcode.bf_len;
|
|
|
|
len = handle->fcode.bf_len;
|
|
|
|
f = (struct bpf_insn *)malloc(prog_size);
|
|
|
|
if (f == NULL) {
|
|
|
|
snprintf(handle->errbuf, sizeof(handle->errbuf),
|
|
|
|
"malloc: %s", pcap_strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(f, handle->fcode.bf_insns, prog_size);
|
|
|
|
fcode->len = len;
|
|
|
|
fcode->filter = (struct sock_filter *) f;
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
p = &f[i];
|
|
|
|
/*
|
|
|
|
* What type of instruction is this?
|
|
|
|
*/
|
|
|
|
switch (BPF_CLASS(p->code)) {
|
|
|
|
|
|
|
|
case BPF_RET:
|
|
|
|
/*
|
|
|
|
* It's a return instruction; is the snapshot
|
|
|
|
* length a constant, rather than the contents
|
|
|
|
* of the accumulator?
|
|
|
|
*/
|
|
|
|
if (BPF_MODE(p->code) == BPF_K) {
|
|
|
|
/*
|
|
|
|
* Yes - if the value to be returned,
|
|
|
|
* i.e. the snapshot length, is anything
|
|
|
|
* other than 0, make it 65535, so that
|
|
|
|
* the packet is truncated by "recvfrom()",
|
|
|
|
* not by the filter.
|
|
|
|
*
|
|
|
|
* XXX - there's nothing we can easily do
|
|
|
|
* if it's getting the value from the
|
|
|
|
* accumulator; we'd have to insert
|
|
|
|
* code to force non-zero values to be
|
|
|
|
* 65535.
|
|
|
|
*/
|
|
|
|
if (p->k != 0)
|
|
|
|
p->k = 65535;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BPF_LD:
|
|
|
|
case BPF_LDX:
|
|
|
|
/*
|
|
|
|
* It's a load instruction; is it loading
|
|
|
|
* from the packet?
|
|
|
|
*/
|
|
|
|
switch (BPF_MODE(p->code)) {
|
|
|
|
|
|
|
|
case BPF_ABS:
|
|
|
|
case BPF_IND:
|
|
|
|
case BPF_MSH:
|
|
|
|
/*
|
|
|
|
* Yes; are we in cooked mode?
|
|
|
|
*/
|
|
|
|
if (handle->md.cooked) {
|
|
|
|
/*
|
|
|
|
* Yes, so we need to fix this
|
|
|
|
* instruction.
|
|
|
|
*/
|
|
|
|
if (fix_offset(p) < 0) {
|
|
|
|
/*
|
|
|
|
* We failed to do so.
|
|
|
|
* Return 0, so our caller
|
|
|
|
* knows to punt to userland.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1; /* we succeeded */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fix_offset(struct bpf_insn *p)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* What's the offset?
|
|
|
|
*/
|
|
|
|
if (p->k >= SLL_HDR_LEN) {
|
|
|
|
/*
|
|
|
|
* It's within the link-layer payload; that starts at an
|
|
|
|
* offset of 0, as far as the kernel packet filter is
|
|
|
|
* concerned, so subtract the length of the link-layer
|
|
|
|
* header.
|
|
|
|
*/
|
|
|
|
p->k -= SLL_HDR_LEN;
|
|
|
|
} else if (p->k == 2) {
|
|
|
|
/*
|
|
|
|
* It's the protocol field; map it to the special magic
|
|
|
|
* kernel offset for that field.
|
|
|
|
*/
|
|
|
|
p->k = SKF_AD_OFF + SKF_AD_PROTOCOL;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* It's within the header, but it's not one of those
|
|
|
|
* fields; we can't do that in the kernel, so punt
|
|
|
|
* to userland.
|
|
|
|
*/
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|