Skip to content

Commit

Permalink
Merge branch 'master' into 4.1.1-beta1
Browse files Browse the repository at this point in the history
  • Loading branch information
fklassen committed Nov 22, 2015
2 parents 6a0aaf2 + 4019ed1 commit 515b7f4
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 4 deletions.
29 changes: 28 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,25 @@ if test x$inet_addr = no ; then
AC_MSG_ERROR([We need inet_addr. See bug 26])
fi

dnl #####################################################
dnl Checks for tuntap device support
dnl #####################################################
have_tuntap=no
AC_ARG_ENABLE([tuntap],
AS_HELP_STRING([--disable-tuntap], [Disable tuntap support]), [:],
[case "$build_os" in
linux*)
AC_CHECK_HEADER([linux/if_tun.h], [have_tuntap=yes])
;;
freebsd*)
AC_CHECK_HEADER([net/if_tun.h], [have_tuntap=yes])
;;
esac])
if test $have_tuntap = yes ; then
AC_DEFINE([HAVE_TUNTAP], [1],
[Do we have TUNTAP device support?])
fi

dnl #####################################################
dnl Checks for libpcap
dnl #####################################################
Expand All @@ -494,7 +513,7 @@ AC_ARG_WITH(libpcap,
LPCAPINC="${testdir}/pcap.h"
LPCAPINCDIR="${testdir}"
if test $dynamic_link = yes; then
for ext in .dylib .so ; do
for ext in .dylib .so .tbd ; do
if test -f "${withval}/libpcap${ext}" ; then
LPCAPLIB="-L${withval}/ -lpcap"
elif test -f "${withval}/lib/libpcap${ext}" ; then
Expand Down Expand Up @@ -1619,6 +1638,13 @@ case $host in
AC_MSG_RESULT(OpenBSD)
;;
*-*-freebsd*)
nic1=em0
nic2=em0
AC_DEFINE([HAVE_FREEBSD], [1], [Building Free BSD])
AC_MSG_RESULT(FreeBSD)
;;
*-*-cygwin)
AC_MSG_RESULT(Win32/Cygwin)
nic1=%0
Expand Down Expand Up @@ -1718,6 +1744,7 @@ pcap_sendpacket: ${have_pcap_sendpacket} **
pcap_netmap ${have_pcap_netmap}
Linux Quick TX: ${have_linux_quick_tx} ${kerneldir}
Linux/BSD netmap: ${have_netmap}
Tuntap device support: ${have_tuntap}

* In order of preference; see configure --help to override
** Required for tcpbridge
Expand Down
86 changes: 85 additions & 1 deletion src/common/sendpacket.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ static int get_iface_index(int fd, const char *device, char *);

#endif /* HAVE_PF_PACKET */

#ifdef HAVE_TUNTAP
#ifdef HAVE_LINUX
#include <linux/if_tun.h>
#elif defined(HAVE_FREEBSD)
#define TUNTAP_DEVICE_PREFIX "/dev/"
#endif
static sendpacket_t *sendpacket_open_tuntap(const char *, char *);
#endif

#if defined HAVE_BPF && ! defined INJECT_METHOD
#undef INJECT_METHOD
#define INJECT_METHOD "bpf send()"
Expand Down Expand Up @@ -289,6 +298,10 @@ sendpacket(sendpacket_t *sp, const u_char *data, size_t len, struct pcap_pkthdr

break;

case SP_TYPE_TUNTAP:
retcode = write(sp->handle.fd, (void *)data, len);
break;

/* Linux PF_PACKET and TX_RING */
case SP_TYPE_PF_PACKET:
case SP_TYPE_TX_RING:
Expand Down Expand Up @@ -510,6 +523,10 @@ sendpacket_open(const char *device, char *errbuf, tcpr_dir_t direction,
break;
}
}
#ifdef HAVE_TUNTAP
} else if (strncmp(device, "tap", 3) == 0) {
sp = sendpacket_open_tuntap(device, errbuf);
#endif
} else {
#ifdef HAVE_QUICK_TX
if (sendpacket_type == SP_TYPE_QUICK_TX)
Expand Down Expand Up @@ -633,6 +650,11 @@ sendpacket_close(sendpacket_t *sp)
#endif /* HAVE_NETMAP */
break;

case SP_TYPE_TUNTAP:
#ifdef HAVE_TUNTAP
close(sp->handle.fd);
#endif
break;
case SP_TYPE_NONE:
err(-1, "no injector selected!");
break;
Expand Down Expand Up @@ -800,6 +822,67 @@ sendpacket_get_hwaddr_libdnet(sendpacket_t *sp)
}
#endif /* HAVE_LIBDNET */

#if defined HAVE_TUNTAP
/**
* Inner sendpacket_open() method for tuntap devices
*/
static sendpacket_t *
sendpacket_open_tuntap(const char *device, char *errbuf)
{
sendpacket_t *sp;
struct ifreq ifr;
int flags = 0;
int tapfd;

assert(device);
assert(errbuf);

#if defined HAVE_LINUX
if ((tapfd = open("/dev/net/tun", O_RDWR)) < 0) {
snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Could not open /dev/net/tun control file: %s", strerror(errno));
return NULL;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = (IFF_TAP | IFF_NO_PI);
strncpy(ifr.ifr_name, device, strlen(device));

if (ioctl(tapfd, TUNSETIFF, (void *) &ifr) < 0) {
snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to create tuntap interface: %s", device);
return NULL;
}
#elif defined(HAVE_FREEBSD)
if (*device == '/') {
if ((tapfd = open(device, O_RDWR)) < 0) {
snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Could not open device %s: %s", device, strerror(errno));
return NULL;
}
} else {
/* full path needed */
char *path;
int prefix_length = strlen(TUNTAP_DEVICE_PREFIX);
if ((path = malloc(strlen(device) + prefix_length + 1)) == NULL) {
snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Malloc error: %s", strerror(errno));
return NULL;
}
snprintf(path, strlen(device) + prefix_length + 1, "%s%s", TUNTAP_DEVICE_PREFIX, device);
if ((tapfd = open(path, O_RDWR)) < 0) {
snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Could not open device %s: %s", path, strerror(errno));
free(path);
return NULL;
}
free(path);
}
#endif

/* prep & return our sp handle */
sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));
strlcpy(sp->device, device, sizeof(sp->device));
sp->handle.fd = tapfd;
sp->handle_type = SP_TYPE_TUNTAP;
return sp;
}
#endif

#if defined HAVE_PF_PACKET
/**
* Inner sendpacket_open() method for using Linux's PF_PACKET or TX_RING
Expand Down Expand Up @@ -1162,7 +1245,8 @@ sendpacket_get_dlt(sendpacket_t *sp)

if (sp->handle_type == SP_TYPE_KHIAL ||
sp->handle_type == SP_TYPE_NETMAP ||
sp->handle_type == SP_TYPE_QUICK_TX) {
sp->handle_type == SP_TYPE_QUICK_TX ||
sp->handle_type == SP_TYPE_TUNTAP) {
/* always EN10MB */
;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/common/sendpacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ typedef enum sendpacket_type_e {
SP_TYPE_KHIAL,
SP_TYPE_NETMAP,
SP_TYPE_QUICK_TX,
SP_TYPE_TUNTAP
} sendpacket_type_t;

/* these are the file_operations ioctls */
Expand Down
6 changes: 6 additions & 0 deletions src/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
/* Define to 1 if you have the `fork' function. */
#undef HAVE_FORK

/* Building Free BSD */
#undef HAVE_FREEBSD

/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
#undef HAVE_FSEEKO

Expand Down Expand Up @@ -518,6 +521,9 @@
/* Do we have tcpdump? */
#undef HAVE_TCPDUMP

/* Do we have TUNTAP device support? */
#undef HAVE_TUNTAP

/* Do we have Linux TX_RING socket support? */
#undef HAVE_TX_RING

Expand Down
6 changes: 4 additions & 2 deletions src/tcpedit/plugins/dlt_linuxsll/linuxsll.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ dlt_linuxsll_parse_opts(tcpeditdlt_t *ctx)
int
dlt_linuxsll_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
{
int type;
linux_sll_header_t *linux_sll;
assert(ctx);
assert(packet);
Expand All @@ -185,10 +186,11 @@ dlt_linuxsll_decode(tcpeditdlt_t *ctx, const u_char *packet, const int pktlen)
ctx->l2len = sizeof(linux_sll_header_t);


if (ntohs(linux_sll->type) == ARPHRD_ETHER) { /* ethernet */
type = ntohs(linux_sll->type);
if (type == ARPHRD_ETHER || type == ARPHRD_LOOPBACK) { /* ethernet or loopback */
memcpy(&(ctx->srcaddr), linux_sll->address, ETHER_ADDR_LEN);
} else {
tcpedit_seterr(ctx->tcpedit, "%s", "DLT_LINUX_SLL pcap's must contain only ethernet packets");
tcpedit_seterr(ctx->tcpedit, "%s", "DLT_LINUX_SLL pcap's must contain only ethernet or loopback packets");
return TCPEDIT_ERROR;
}

Expand Down
1 change: 1 addition & 0 deletions src/tcpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ struct tcpr_arp_hdr
#define ARPHRD_ATM 19 /* ATM */
#define ARPHRD_METRICOM 23 /* Metricom STRIP (new IANA id) */
#define ARPHRD_IPSEC 31 /* IPsec tunnel */
#define ARPHRD_LOOPBACK 772 /* Loopback device */
uint16_t ar_pro; /* format of protocol address */
uint8_t ar_hln; /* length of hardware address */
uint8_t ar_pln; /* length of protocol addres */
Expand Down

0 comments on commit 515b7f4

Please sign in to comment.