freebsd-dev/contrib/libpcap/pcap-libdlpi.c

496 lines
12 KiB
C
Raw Normal View History

2009-03-21 20:43:56 +00:00
/*
* Copyright (c) 1993, 1994, 1995, 1996, 1997
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution, and (3) all advertising materials mentioning
* features or use of this software display the following acknowledgement:
* ``This product includes software developed by the University of California,
* Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
* the University nor the names of its contributors may 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.
*
* This code contributed by Sagun Shakya (sagun.shakya@sun.com)
*/
/*
* Packet capture routines for DLPI using libdlpi under SunOS 5.11.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
2009-03-21 20:43:56 +00:00
#endif
#include <sys/types.h>
#include <sys/time.h>
#include <sys/bufmod.h>
#include <sys/stream.h>
#include <libdlpi.h>
#include <errno.h>
#include <memory.h>
#include <stropts.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pcap-int.h"
#include "dlpisubs.h"
/* Forwards. */
static int dlpromiscon(pcap_t *, bpf_u_int32);
2009-03-21 20:43:56 +00:00
static int pcap_read_libdlpi(pcap_t *, int, pcap_handler, u_char *);
static int pcap_inject_libdlpi(pcap_t *, const void *, size_t);
static void pcap_libdlpi_err(const char *, const char *, int, char *);
Update libpcap to 1.1.1. Changes: Thu. April 1, 2010. guy@alum.mit.edu. Summary for 1.1.1 libpcap release Update CHANGES to reflect more of the changes in 1.1.0. Fix build on RHEL5. Fix shared library build on AIX. Thu. March 11, 2010. ken@netfunctional.ca/guy@alum.mit.edu. Summary for 1.1.0 libpcap release Add SocketCAN capture support Add Myricom SNF API support Update Endace DAG and ERF support Add support for shared libraries on Solaris, HP-UX, and AIX Build, install, and un-install shared libraries by default; don't build/install shared libraries on platforms we don't support Fix building from a directory other than the source directory Fix compiler warnings and builds on some platforms Update config.guess and config.sub Support monitor mode on mac80211 devices on Linux Fix USB memory-mapped capturing on Linux; it requires a new DLT_ value On Linux, scan /sys/class/net for devices if we have it; scan it, or /proc/net/dev if we don't have /sys/class/net, even if we have getifaddrs(), as it'll find interfaces with no addresses Add limited support for reading pcap-ng files Fix BPF driver-loading error handling on AIX Support getting the full-length interface description on FreeBSD In the lexical analyzer, free up any addrinfo structure we got back from getaddrinfo(). Add support for BPF and libdlpi in OpenSolaris (and SXCE) Hyphenate "link-layer" everywhere Add /sys/kernel/debug/usb/usbmon to the list of usbmon locations In pcap_read_linux_mmap(), if there are no frames available, call poll() even if we're in non-blocking mode, so we pick up errors, and check for the errors in question. Note that poll() works on BPF devices is Snow Leopard If an ENXIO or ENETDOWN is received, it may mean the device has gone away. Deal with it. For BPF, raise the default capture buffer size to from 32k to 512k Support ps_ifdrop on Linux Added a bunch of #ifdef directives to make wpcap.dll (WinPcap) compile under cygwin. Changes to Linux mmapped captures. Fix bug where create_ring would fail for particular snaplen and buffer size combinations Update pcap-config so that it handles libpcap requiring additional libraries Add workaround for threadsafeness on Windows Add missing mapping for DLT_ENC <-> LINKTYPE_ENC DLT: Add DLT_CAN_SOCKETCAN DLT: Add Solaris ipnet Don't check for DLT_IPNET if it's not defined Add link-layer types for Fibre Channel FC-2 Add link-layer types for Wireless HART Add link-layer types for AOS Add link-layer types for DECT Autoconf fixes (AIX, HP-UX, OSF/1, Tru64 cleanups) Install headers unconditionally, and include vlan.h/bluetooth.h if enabled Autoconf fixes+cleanup Support enabling/disabling bluetooth (--{en,dis}able-bluetooth) Support disabling SITA support (--without-sita) Return -1 on failure to create packet ring (if supported but creation failed) Fix handling of 'any' device, so that it can be opened, and no longer attempt to open it in Monitor mode Add support for snapshot length for USB Memory-Mapped Interface Fix configure and build on recent Linux kernels Fix memory-mapped Linux capture to support pcap_next() and pcap_next_ex() Fixes for Linux USB capture DLT: Add DLT_LINUX_EVDEV DLT: Add DLT_GSMTAP_UM DLT: Add DLT_GSMTAP_ABIS
2010-10-28 16:22:13 +00:00
static void pcap_cleanup_libdlpi(pcap_t *);
2009-03-21 20:43:56 +00:00
/*
* list_interfaces() will list all the network links that are
* available on a system.
*/
static boolean_t list_interfaces(const char *, void *);
typedef struct linknamelist {
char linkname[DLPI_LINKNAME_MAX];
struct linknamelist *lnl_next;
} linknamelist_t;
typedef struct linkwalk {
linknamelist_t *lw_list;
int lw_err;
} linkwalk_t;
/*
* The caller of this function should free the memory allocated
* for each linknamelist_t "entry" allocated.
*/
static boolean_t
list_interfaces(const char *linkname, void *arg)
{
linkwalk_t *lwp = arg;
linknamelist_t *entry;
if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) {
lwp->lw_err = ENOMEM;
return (B_TRUE);
}
2019-10-06 04:26:37 +00:00
(void) pcap_strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX);
2009-03-21 20:43:56 +00:00
if (lwp->lw_list == NULL) {
lwp->lw_list = entry;
} else {
entry->lnl_next = lwp->lw_list;
lwp->lw_list = entry;
}
return (B_FALSE);
}
static int
pcap_activate_libdlpi(pcap_t *p)
{
2015-01-06 18:58:31 +00:00
struct pcap_dlpi *pd = p->priv;
int status = 0;
2009-03-21 20:43:56 +00:00
int retv;
dlpi_handle_t dh;
dlpi_info_t dlinfo;
/*
* Enable Solaris raw and passive DLPI extensions;
* dlpi_open() will not fail if the underlying link does not support
* passive mode. See dlpi(7P) for details.
*/
2017-02-12 07:04:44 +00:00
retv = dlpi_open(p->opt.device, &dh, DLPI_RAW|DLPI_PASSIVE);
2009-03-21 20:43:56 +00:00
if (retv != DLPI_SUCCESS) {
if (retv == DLPI_ELINKNAMEINVAL || retv == DLPI_ENOLINK)
2015-01-06 18:58:31 +00:00
status = PCAP_ERROR_NO_SUCH_DEVICE;
else if (retv == DL_SYSERR &&
(errno == EPERM || errno == EACCES))
2015-01-06 18:58:31 +00:00
status = PCAP_ERROR_PERM_DENIED;
else
status = PCAP_ERROR;
2017-02-12 07:04:44 +00:00
pcap_libdlpi_err(p->opt.device, "dlpi_open", retv,
2009-03-21 20:43:56 +00:00
p->errbuf);
2015-01-06 18:58:31 +00:00
return (status);
2009-03-21 20:43:56 +00:00
}
2015-01-06 18:58:31 +00:00
pd->dlpi_hd = dh;
2009-03-21 20:43:56 +00:00
if (p->opt.rfmon) {
/*
* This device exists, but we don't support monitor mode
* any platforms that support DLPI.
*/
2015-01-06 18:58:31 +00:00
status = PCAP_ERROR_RFMON_NOTSUP;
2009-03-21 20:43:56 +00:00
goto bad;
}
/* Bind with DLPI_ANY_SAP. */
2015-01-06 18:58:31 +00:00
if ((retv = dlpi_bind(pd->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) {
status = PCAP_ERROR;
2017-02-12 07:04:44 +00:00
pcap_libdlpi_err(p->opt.device, "dlpi_bind", retv, p->errbuf);
2009-03-21 20:43:56 +00:00
goto bad;
}
/*
* 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;
2009-03-21 20:43:56 +00:00
/* Enable promiscuous mode. */
if (p->opt.promisc) {
2015-01-06 18:58:31 +00:00
retv = dlpromiscon(p, DL_PROMISC_PHYS);
if (retv < 0) {
/*
* "You don't have permission to capture on
* this device" and "you don't have permission
* to capture in promiscuous mode on this
* device" are different; let the user know,
* so if they can't get permission to
* capture in promiscuous mode, they can at
* least try to capture in non-promiscuous
* mode.
*
* XXX - you might have to capture in
* promiscuous mode to see outgoing packets.
*/
2015-01-06 18:58:31 +00:00
if (retv == PCAP_ERROR_PERM_DENIED)
status = PCAP_ERROR_PROMISC_PERM_DENIED;
else
status = retv;
2009-03-21 20:43:56 +00:00
goto bad;
}
} else {
/* Try to enable multicast. */
2015-01-06 18:58:31 +00:00
retv = dlpromiscon(p, DL_PROMISC_MULTI);
if (retv < 0) {
status = retv;
2009-03-21 20:43:56 +00:00
goto bad;
2015-01-06 18:58:31 +00:00
}
2009-03-21 20:43:56 +00:00
}
/* Try to enable SAP promiscuity. */
2015-01-06 18:58:31 +00:00
retv = dlpromiscon(p, DL_PROMISC_SAP);
if (retv < 0) {
/*
* Not fatal, since the DL_PROMISC_PHYS mode worked.
* Report it as a warning, however.
*/
if (p->opt.promisc)
2015-01-06 18:58:31 +00:00
status = PCAP_WARNING;
else {
status = retv;
2009-03-21 20:43:56 +00:00
goto bad;
2015-01-06 18:58:31 +00:00
}
2009-03-21 20:43:56 +00:00
}
/* Determine link type. */
2015-01-06 18:58:31 +00:00
if ((retv = dlpi_info(pd->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) {
status = PCAP_ERROR;
2017-02-12 07:04:44 +00:00
pcap_libdlpi_err(p->opt.device, "dlpi_info", retv, p->errbuf);
2009-03-21 20:43:56 +00:00
goto bad;
}
2015-01-06 18:58:31 +00:00
if (pcap_process_mactype(p, dlinfo.di_mactype) != 0) {
status = PCAP_ERROR;
2009-03-21 20:43:56 +00:00
goto bad;
2015-01-06 18:58:31 +00:00
}
2009-03-21 20:43:56 +00:00
2015-01-06 18:58:31 +00:00
p->fd = dlpi_fd(pd->dlpi_hd);
2009-03-21 20:43:56 +00:00
/* Push and configure bufmod. */
2015-01-06 18:58:31 +00:00
if (pcap_conf_bufmod(p, p->snapshot) != 0) {
status = PCAP_ERROR;
2009-03-21 20:43:56 +00:00
goto bad;
2015-01-06 18:58:31 +00:00
}
2009-03-21 20:43:56 +00:00
/*
* Flush the read side.
*/
if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
2015-01-06 18:58:31 +00:00
status = PCAP_ERROR;
pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
errno, "FLUSHR");
2009-03-21 20:43:56 +00:00
goto bad;
}
/* Allocate data buffer. */
2015-01-06 18:58:31 +00:00
if (pcap_alloc_databuf(p) != 0) {
status = PCAP_ERROR;
2009-03-21 20:43:56 +00:00
goto bad;
2015-01-06 18:58:31 +00:00
}
2009-03-21 20:43:56 +00:00
/*
* "p->fd" is a FD for a STREAMS device, so "select()" and
* "poll()" should work on it.
*/
p->selectable_fd = p->fd;
p->read_op = pcap_read_libdlpi;
p->inject_op = pcap_inject_libdlpi;
p->setfilter_op = install_bpf_program; /* No kernel filtering */
p->setdirection_op = NULL; /* Not implemented */
p->set_datalink_op = NULL; /* Can't change data link type */
p->getnonblock_op = pcap_getnonblock_fd;
p->setnonblock_op = pcap_setnonblock_fd;
p->stats_op = pcap_stats_dlpi;
p->cleanup_op = pcap_cleanup_libdlpi;
2015-01-06 18:58:31 +00:00
return (status);
2009-03-21 20:43:56 +00:00
bad:
pcap_cleanup_libdlpi(p);
2015-01-06 18:58:31 +00:00
return (status);
2009-03-21 20:43:56 +00:00
}
#define STRINGIFY(n) #n
static int
dlpromiscon(pcap_t *p, bpf_u_int32 level)
{
2015-01-06 18:58:31 +00:00
struct pcap_dlpi *pd = p->priv;
int retv;
int err;
2015-01-06 18:58:31 +00:00
retv = dlpi_promiscon(pd->dlpi_hd, level);
if (retv != DLPI_SUCCESS) {
if (retv == DL_SYSERR &&
(errno == EPERM || errno == EACCES))
err = PCAP_ERROR_PERM_DENIED;
else
err = PCAP_ERROR;
2017-02-12 07:04:44 +00:00
pcap_libdlpi_err(p->opt.device, "dlpi_promiscon" STRINGIFY(level),
retv, p->errbuf);
return (err);
}
return (0);
}
2017-02-12 07:04:44 +00:00
/*
* Presumably everything returned by dlpi_walk() is a DLPI device,
* so there's no work to be done here to check whether name refers
* to a DLPI device.
*/
static int
is_dlpi_interface(const char *name _U_)
{
return (1);
}
static int
get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
{
/*
* Nothing we can do other than mark loopback devices as "the
* connected/disconnected status doesn't apply".
*
* XXX - on Solaris, can we do what the dladm command does,
* i.e. get a connected/disconnected indication from a kstat?
* (Note that you can also get the link speed, and possibly
* other information, from a kstat as well.)
*/
if (*flags & PCAP_IF_LOOPBACK) {
/*
* Loopback devices aren't wireless, and "connected"/
* "disconnected" doesn't apply to them.
*/
*flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
return (0);
}
return (0);
}
2009-03-21 20:43:56 +00:00
/*
* In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
* network links that are plumbed and are up. dlpi_walk(3DLPI) will find
* additional network links present in the system.
*/
int
pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
2009-03-21 20:43:56 +00:00
{
int retv = 0;
linknamelist_t *entry, *next;
linkwalk_t lw = {NULL, 0};
int save_errno;
2017-02-12 07:04:44 +00:00
/*
* Get the list of regular interfaces first.
*/
if (pcap_findalldevs_interfaces(devlistp, errbuf,
is_dlpi_interface, get_if_flags) == -1)
2017-02-12 07:04:44 +00:00
return (-1); /* failure */
2009-03-21 20:43:56 +00:00
/* dlpi_walk() for loopback will be added here. */
/*
* Find all DLPI devices in the current zone.
*
* XXX - will pcap_findalldevs_interfaces() find any devices
* outside the current zone? If not, the only reason to call
* it would be to get the interface addresses.
*/
2009-03-21 20:43:56 +00:00
dlpi_walk(list_interfaces, &lw, 0);
if (lw.lw_err != 0) {
pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
lw.lw_err, "dlpi_walk");
2009-03-21 20:43:56 +00:00
retv = -1;
goto done;
}
/* Add linkname if it does not exist on the list. */
for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) {
/*
* If it isn't already in the list of devices, try to
* add it.
*/
if (find_or_add_dev(devlistp, entry->linkname, 0, get_if_flags,
NULL, errbuf) == NULL)
2009-03-21 20:43:56 +00:00
retv = -1;
}
done:
save_errno = errno;
for (entry = lw.lw_list; entry != NULL; entry = next) {
next = entry->lnl_next;
free(entry);
}
errno = save_errno;
return (retv);
}
/*
* Read data received on DLPI handle. Returns -2 if told to terminate, else
* returns the number of packets read.
*/
static int
pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user)
{
2015-01-06 18:58:31 +00:00
struct pcap_dlpi *pd = p->priv;
2009-03-21 20:43:56 +00:00
int len;
u_char *bufp;
size_t msglen;
int retv;
len = p->cc;
if (len != 0) {
bufp = p->bp;
goto process_pkts;
}
do {
/* Has "pcap_breakloop()" been called? */
if (p->break_loop) {
/*
* Yes - clear the flag that indicates that it has,
* and return -2 to indicate that we were told to
* break out of the loop.
*/
p->break_loop = 0;
return (-2);
}
msglen = p->bufsize;
2017-02-12 07:04:44 +00:00
bufp = (u_char *)p->buffer + p->offset;
2009-03-21 20:43:56 +00:00
2015-01-06 18:58:31 +00:00
retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp,
2009-03-21 20:43:56 +00:00
&msglen, -1, NULL);
if (retv != DLPI_SUCCESS) {
/*
* This is most likely a call to terminate out of the
* loop. So, do not return an error message, instead
* check if "pcap_breakloop()" has been called above.
*/
if (retv == DL_SYSERR && errno == EINTR) {
len = 0;
continue;
}
2015-01-06 18:58:31 +00:00
pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd),
2009-03-21 20:43:56 +00:00
"dlpi_recv", retv, p->errbuf);
return (-1);
}
len = msglen;
} while (len == 0);
process_pkts:
return (pcap_process_pkts(p, callback, user, count, bufp, len));
}
static int
pcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size)
{
2015-01-06 18:58:31 +00:00
struct pcap_dlpi *pd = p->priv;
2009-03-21 20:43:56 +00:00
int retv;
2015-01-06 18:58:31 +00:00
retv = dlpi_send(pd->dlpi_hd, NULL, 0, buf, size, NULL);
2009-03-21 20:43:56 +00:00
if (retv != DLPI_SUCCESS) {
2015-01-06 18:58:31 +00:00
pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), "dlpi_send", retv,
2009-03-21 20:43:56 +00:00
p->errbuf);
return (-1);
}
/*
* dlpi_send(3DLPI) does not provide a way to return the number of
* bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
* returned we are assuming 'size' bytes were sent.
*/
return (size);
}
/*
* Close dlpi handle.
*/
static void
pcap_cleanup_libdlpi(pcap_t *p)
{
2015-01-06 18:58:31 +00:00
struct pcap_dlpi *pd = p->priv;
if (pd->dlpi_hd != NULL) {
dlpi_close(pd->dlpi_hd);
pd->dlpi_hd = NULL;
2009-03-21 20:43:56 +00:00
p->fd = -1;
}
pcap_cleanup_live_common(p);
}
/*
* Write error message to buffer.
*/
static void
pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf)
{
2017-02-12 07:04:44 +00:00
pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s",
2009-03-21 20:43:56 +00:00
func, linkname, dlpi_strerror(err));
}
pcap_t *
2017-02-12 07:04:44 +00:00
pcap_create_interface(const char *device _U_, char *ebuf)
2009-03-21 20:43:56 +00:00
{
pcap_t *p;
2017-02-12 07:04:44 +00:00
p = pcap_create_common(ebuf, sizeof (struct pcap_dlpi));
2009-03-21 20:43:56 +00:00
if (p == NULL)
return (NULL);
p->activate_op = pcap_activate_libdlpi;
return (p);
}
/*
* Libpcap version string.
*/
const char *
pcap_lib_version(void)
{
return (PCAP_VERSION_STRING);
}