1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Update libpcap to 1.9.0 (no patches applied yet)

This commit is contained in:
dmiller
2019-03-30 03:24:44 +00:00
parent 7d860b04e5
commit a2442ea29f
197 changed files with 26419 additions and 22082 deletions

View File

@@ -1,19 +1,23 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#include <config.h>
#endif
#ifndef _WIN32
#include <sys/param.h>
#endif /* !_WIN32 */
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#ifndef _WIN32
#include <netinet/in.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#endif /* !_WIN32 */
#include <snf.h>
#if SNF_VERSION_API >= 0x0003
@@ -30,10 +34,10 @@ struct pcap_snf {
snf_handle_t snf_handle; /* opaque device handle */
snf_ring_t snf_ring; /* opaque device ring handle */
#ifdef SNF_HAVE_INJECT_API
snf_inject_t snf_inj; /* inject handle, if inject is used */
snf_inject_t snf_inj; /* inject handle, if inject is used */
#endif
int snf_timeout;
int snf_boardnum;
int snf_timeout;
int snf_boardnum;
};
static int
@@ -51,8 +55,8 @@ snf_pcap_stats(pcap_t *p, struct pcap_stat *ps)
int rc;
if ((rc = snf_ring_getstats(snfps->snf_ring, &stats))) {
pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_get_stats: %s",
pcap_strerror(rc));
pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
rc, "snf_get_stats");
return -1;
}
ps->ps_recv = stats.ring_pkt_recv + stats.ring_pkt_overflow;
@@ -67,8 +71,8 @@ snf_platform_cleanup(pcap_t *p)
struct pcap_snf *ps = p->priv;
#ifdef SNF_HAVE_INJECT_API
if (ps->snf_inj)
snf_inject_close(ps->snf_inj);
if (ps->snf_inj)
snf_inject_close(ps->snf_inj);
#endif
snf_ring_close(ps->snf_ring);
snf_close(ps->snf_handle);
@@ -76,7 +80,7 @@ snf_platform_cleanup(pcap_t *p)
}
static int
snf_getnonblock(pcap_t *p, char *errbuf)
snf_getnonblock(pcap_t *p)
{
struct pcap_snf *ps = p->priv;
@@ -84,7 +88,7 @@ snf_getnonblock(pcap_t *p, char *errbuf)
}
static int
snf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
snf_setnonblock(pcap_t *p, int nonblock)
{
struct pcap_snf *ps = p->priv;
@@ -107,9 +111,10 @@ snf_timestamp_to_timeval(const int64_t ts_nanosec, const int tstamp_precision)
{
struct timeval tv;
long tv_nsec;
const static struct timeval zero_timeval;
if (ts_nanosec == 0)
return (struct timeval) { 0, 0 };
if (ts_nanosec == 0)
return zero_timeval;
tv.tv_sec = ts_nanosec / _NSEC_PER_SEC;
tv_nsec = (ts_nanosec % _NSEC_PER_SEC);
@@ -161,8 +166,8 @@ snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
continue;
}
else {
pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_read: %s",
pcap_strerror(err));
pcap_fmt_errmsg_for_errno(p->errbuf,
PCAP_ERRBUF_SIZE, err, "snf_read");
return -1;
}
}
@@ -212,25 +217,25 @@ snf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
{
#ifdef SNF_HAVE_INJECT_API
struct pcap_snf *ps = p->priv;
int rc;
if (ps->snf_inj == NULL) {
rc = snf_inject_open(ps->snf_boardnum, 0, &ps->snf_inj);
if (rc) {
pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
"snf_inject_open: %s", pcap_strerror(rc));
return (-1);
}
}
int rc;
if (ps->snf_inj == NULL) {
rc = snf_inject_open(ps->snf_boardnum, 0, &ps->snf_inj);
if (rc) {
pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
rc, "snf_inject_open");
return (-1);
}
}
rc = snf_inject_send(ps->snf_inj, -1, 0, buf, size);
if (!rc) {
return (size);
}
else {
pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_inject_send: %s",
pcap_strerror(rc));
return (-1);
}
rc = snf_inject_send(ps->snf_inj, -1, 0, buf, size);
if (!rc) {
return (size);
}
else {
pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
rc, "snf_inject_send");
return (-1);
}
#else
strlcpy(p->errbuf, "Sending packets isn't supported with this snf version",
PCAP_ERRBUF_SIZE);
@@ -248,8 +253,7 @@ snf_activate(pcap_t* p)
int flags = -1, ring_id = -1;
if (device == NULL) {
pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
"device is NULL: %s", pcap_strerror(errno));
pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "device is NULL");
return -1;
}
@@ -262,15 +266,22 @@ snf_activate(pcap_t* p)
else
nr = NULL;
/* Allow pcap_set_buffer_size() to set dataring_size.
* Default is zero which allows setting from env SNF_DATARING_SIZE.
* pcap_set_buffer_size() is in bytes while snf_open() accepts values
* between 0 and 1048576 in Megabytes. Values in this range are
* mapped to 1MB.
*/
err = snf_open(ps->snf_boardnum,
0, /* let SNF API parse SNF_NUM_RINGS, if set */
NULL, /* default RSS, or use SNF_RSS_FLAGS env */
0, /* default to SNF_DATARING_SIZE from env */
(p->opt.buffer_size > 0 && p->opt.buffer_size < 1048576) ? 1048576 : p->opt.buffer_size, /* default to SNF_DATARING_SIZE from env */
flags, /* may want pshared */
&ps->snf_handle);
if (err != 0) {
pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
"snf_open failed: %s", pcap_strerror(err));
pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
err, "snf_open failed");
return -1;
}
@@ -279,12 +290,22 @@ snf_activate(pcap_t* p)
}
err = snf_ring_open_id(ps->snf_handle, ring_id, &ps->snf_ring);
if (err != 0) {
pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
"snf_ring_open_id(ring=%d) failed: %s",
ring_id, pcap_strerror(err));
pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
err, "snf_ring_open_id(ring=%d) failed", ring_id);
return -1;
}
/*
* Turn a negative snapshot value (invalid), a snapshot value of
* 0 (unspecified), or a value bigger than the normal maximum
* value, into the maximum allowed value.
*
* If some application really *needs* a bigger snapshot
* length, we should just increase MAXIMUM_SNAPLEN.
*/
if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
p->snapshot = MAXIMUM_SNAPLEN;
if (p->opt.timeout <= 0)
ps->snf_timeout = -1;
else
@@ -292,15 +313,17 @@ snf_activate(pcap_t* p)
err = snf_start(ps->snf_handle);
if (err != 0) {
pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
"snf_start failed: %s", pcap_strerror(err));
pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
err, "snf_start failed");
return -1;
}
/*
* "select()" and "poll()" don't work on snf descriptors.
*/
#ifndef _WIN32
p->selectable_fd = -1;
#endif /* !_WIN32 */
p->linktype = DLT_EN10MB;
p->read_op = snf_read;
p->inject_op = snf_inject;
@@ -312,107 +335,177 @@ snf_activate(pcap_t* p)
p->stats_op = snf_pcap_stats;
p->cleanup_op = snf_platform_cleanup;
#ifdef SNF_HAVE_INJECT_API
ps->snf_inj = NULL;
ps->snf_inj = NULL;
#endif
return 0;
}
#define MAX_DESC_LENGTH 128
int
snf_findalldevs(pcap_if_t **devlistp, char *errbuf)
snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf)
{
pcap_if_t *devlist = NULL,*curdev,*prevdev;
pcap_addr_t *curaddr;
pcap_if_t *dev;
#ifdef _WIN32
struct sockaddr_in addr;
#endif
struct snf_ifaddrs *ifaddrs, *ifa;
char name[MAX_DESC_LENGTH];
char desc[MAX_DESC_LENGTH];
int ret;
int ret, allports = 0, merge = 0;
const char *nr = NULL;
if (snf_init(SNF_VERSION_API))
if (snf_init(SNF_VERSION_API)) {
(void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
"snf_getifaddrs: snf_init failed");
return (-1);
}
if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL)
{
(void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
"snf_getifaddrs: %s", pcap_strerror(errno));
pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
errno, "snf_getifaddrs");
return (-1);
}
ifa = ifaddrs;
while (ifa)
{
/*
* Allocate a new entry
*/
curdev = (pcap_if_t *)malloc(sizeof(pcap_if_t));
if (curdev == NULL) {
(void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
"snf_findalldevs malloc: %s", pcap_strerror(errno));
return (-1);
}
if (devlist == NULL) /* save first entry */
devlist = curdev;
else
prevdev->next = curdev;
/*
* Fill in the entry.
*/
curdev->next = NULL;
curdev->name = strdup(ifa->snf_ifa_name);
if (curdev->name == NULL) {
if ((nr = getenv("SNF_FLAGS")) && *nr) {
errno = 0;
merge = strtol(nr, NULL, 0);
if (errno) {
(void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
"snf_findalldevs strdup: %s", pcap_strerror(errno));
free(curdev);
"snf_getifaddrs: SNF_FLAGS is not a valid number");
return (-1);
}
(void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom snf%d",
ifa->snf_ifa_portnum);
curdev->description = strdup(desc);
if (curdev->description == NULL) {
(void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
"snf_findalldevs strdup1: %s", pcap_strerror(errno));
free(curdev->name);
free(curdev);
return (-1);
}
curdev->addresses = NULL;
curdev->flags = 0;
merge = merge & SNF_F_AGGREGATE_PORTMASK;
}
curaddr = (pcap_addr_t *)malloc(sizeof(pcap_addr_t));
if (curaddr == NULL) {
(void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
"snf_findalldevs malloc1: %s", pcap_strerror(errno));
free(curdev->description);
free(curdev->name);
free(curdev);
return (-1);
}
curdev->addresses = curaddr;
curaddr->next = NULL;
curaddr->addr = (struct sockaddr*)malloc(sizeof(struct sockaddr_storage));
if (curaddr->addr == NULL) {
(void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
"malloc2: %s", pcap_strerror(errno));
free(curdev->description);
free(curdev->name);
free(curaddr);
free(curdev);
return (-1);
}
curaddr->addr->sa_family = AF_INET;
curaddr->netmask = NULL;
curaddr->broadaddr = NULL;
curaddr->dstaddr = NULL;
curaddr->next = NULL;
for (ifa = ifaddrs; ifa != NULL; ifa = ifa->snf_ifa_next) {
/*
* Myricom SNF adapter ports may appear as regular
* network interfaces, which would already have been
* added to the list of adapters by pcap_platform_finddevs()
* if this isn't an SNF-only version of libpcap.
*
* Our create routine intercepts pcap_create() calls for
* those interfaces and arranges that they will be
* opened using the SNF API instead.
*
* So if we already have an entry for the device, we
* don't add an additional entry for it, we just
* update the description for it, if any, to indicate
* which snfN device it is. Otherwise, we add an entry
* for it.
*
* In either case, if SNF_F_AGGREGATE_PORTMASK is set
* in SNF_FLAGS, we add this port to the bitmask
* of ports, which we use to generate a device
* we can use to capture on all ports.
*
* Generate the description string. If port aggregation
* is set, use 2^{port number} as the unit number,
* rather than {port number}.
*
* XXX - do entries in this list have IP addresses for
* the port? If so, should we add them to the
* entry for the device, if they're not already in the
* list of IP addresses for the device?
*/
(void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom %ssnf%d",
merge ? "Merge Bitmask Port " : "",
merge ? 1 << ifa->snf_ifa_portnum : ifa->snf_ifa_portnum);
/*
* Add the port to the bitmask.
*/
if (merge)
allports |= 1 << ifa->snf_ifa_portnum;
/*
* See if there's already an entry for the device
* with the name ifa->snf_ifa_name.
*/
dev = find_dev(devlistp, ifa->snf_ifa_name);
if (dev != NULL) {
/*
* Yes. Update its description.
*/
char *desc_str;
prevdev = curdev;
ifa = ifa->snf_ifa_next;
desc_str = strdup(desc);
if (desc_str == NULL) {
pcap_fmt_errmsg_for_errno(errbuf,
PCAP_ERRBUF_SIZE, errno,
"snf_findalldevs strdup");
return -1;
}
free(dev->description);
dev->description = desc_str;
} else {
/*
* No. Add an entry for it.
*
* XXX - is there a notion of "up" or "running",
* and can we determine whether something's
* plugged into the adapter and set
* PCAP_IF_CONNECTION_STATUS_CONNECTED or
* PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
*/
dev = add_dev(devlistp, ifa->snf_ifa_name, 0, desc,
errbuf);
if (dev == NULL)
return -1;
#ifdef _WIN32
/*
* On Windows, fill in IP# from device name
*/
ret = inet_pton(AF_INET, dev->name, &addr.sin_addr);
if (ret == 1) {
/*
* Successful conversion of device name
* to IPv4 address.
*/
addr.sin_family = AF_INET;
if (add_addr_to_dev(dev, &addr, sizeof(addr),
NULL, 0, NULL, 0, NULL, 0, errbuf) == -1)
return -1;
} else if (ret == -1) {
/*
* Error.
*/
pcap_fmt_errmsg_for_errno(errbuf,
PCAP_ERRBUF_SIZE, errno,
"sinf_findalldevs inet_pton");
return -1;
}
#endif _WIN32
}
}
snf_freeifaddrs(ifaddrs);
*devlistp = devlist;
/*
* There are no platform-specific devices since each device
* exists as a regular Ethernet device.
*/
* Create a snfX entry if port aggregation is enabled
*/
if (merge) {
/*
* Add a new entry with all ports bitmask
*/
(void)pcap_snprintf(name,MAX_DESC_LENGTH,"snf%d",allports);
(void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom Merge Bitmask All Ports snf%d",
allports);
/*
* XXX - is there any notion of "up" and "running" that
* would apply to this device, given that it handles
* multiple ports?
*
* Presumably, there's no notion of "connected" vs.
* "disconnected", as "is this plugged into a network?"
* would be a per-port property.
*/
if (add_dev(devlistp, name,
PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, desc,
errbuf) == NULL)
return (-1);
/*
* XXX - should we give it a list of addresses with all
* the addresses for all the ports?
*/
}
return 0;
}
@@ -443,7 +536,7 @@ snf_create(const char *device, char *ebuf, int *is_ours)
devlen = strlen(device) + 1;
ifa = ifaddrs;
while (ifa) {
if (!strncmp(device, ifa->snf_ifa_name, devlen)) {
if (strncmp(device, ifa->snf_ifa_name, devlen) == 0) {
boardnum = ifa->snf_ifa_boardnum;
break;
}
@@ -461,7 +554,7 @@ snf_create(const char *device, char *ebuf, int *is_ours)
/* Nope, not a supported name */
*is_ours = 0;
return NULL;
}
}
}
/* OK, it's probably ours. */
@@ -478,8 +571,8 @@ snf_create(const char *device, char *ebuf, int *is_ours)
p->tstamp_precision_count = 2;
p->tstamp_precision_list = malloc(2 * sizeof(u_int));
if (p->tstamp_precision_list == NULL) {
pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
pcap_strerror(errno));
pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno,
"malloc");
pcap_close(p);
return NULL;
}
@@ -498,12 +591,11 @@ snf_create(const char *device, char *ebuf, int *is_ours)
*/
/*
* There are no regular interfaces, just DAG interfaces.
* There are no regular interfaces, just SNF interfaces.
*/
int
pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
{
*alldevsp = NULL;
return (0);
}
@@ -517,4 +609,13 @@ pcap_create_interface(const char *device, char *errbuf)
"This version of libpcap only supports SNF cards");
return NULL;
}
/*
* Libpcap version string.
*/
const char *
pcap_lib_version(void)
{
return (PCAP_VERSION_STRING " (SNF-only)");
}
#endif