From 4c33e2a03f3eeb5bd82b22b6a222cf07dc024937 Mon Sep 17 00:00:00 2001 From: Zhang Huanjie Date: Wed, 20 Feb 2019 12:24:42 +0800 Subject: [PATCH 1/4] tcpdump mode --- EthUDP.c | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/EthUDP.c b/EthUDP.c index b5edb5f..363248a 100644 --- a/EthUDP.c +++ b/EthUDP.c @@ -37,6 +37,7 @@ #include #include #include +#include #define MAXLEN 2048 #define MAX_PACKET_SIZE 2048 @@ -50,6 +51,7 @@ #define MODEE 0 // raw ether bridge mode #define MODEI 1 // tap interface mode #define MODEB 2 // bridge mode +#define MODET 3 // tcpdump //#define DEBUGPINGPONG 1 //#define DEBUGSSL 1 @@ -129,6 +131,7 @@ int enc_key_len = 0; int fdudp[2], fdraw; int nat[2]; +pcap_t *pcap_handle; int lz4 = 0; volatile long long udp_total = 0; @@ -1193,7 +1196,13 @@ void process_raw_to_udp(void) // used by mode==0 & mode==1 err_msg("recv long pkt from raw, len=%d", len); len = MAX_PACKET_SIZE; } - } else + } else if (mode == MODET) { + struct pcap_pkthdr *header; + int r = pcap_next_ex(pcap_handle, &header, (const u_char **)&buf); + if (r <= 0) + continue; + len = header->len; + } return; if (len <= 0) @@ -1592,6 +1601,7 @@ void usage(void) printf(" [ localip localport remoteip remoteport ]\n"); printf("./EthUDP -b [ options ] localip localport remoteip remoteport bridge \\\n"); printf(" [ localip localport remoteip remoteport ]\n"); + printf("./EthUDP -t localip localport remoteip remoteport eth? [ pcap_filter_string ]\n"); printf(" options:\n"); printf(" -p password\n"); printf(" -enc [ xor|aes-128|aes-192|aes-256 ]\n"); @@ -1673,6 +1683,8 @@ int main(int argc, char *argv[]) mode = MODEI; else if (strcmp(argv[i], "-b") == 0) mode = MODEB; + else if (strcmp(argv[i], "-t") == 0) + mode = MODET; else if (strcmp(argv[i], "-d") == 0) debug = 1; else if (strcmp(argv[i], "-r") == 0) { @@ -1778,11 +1790,15 @@ int main(int argc, char *argv[]) else if (argc - i != 6) usage(); } + if (mode == MODET ) { + if (argc -i < 5) + usage(); + } if (mode == -1) usage(); if (debug) { printf(" debug = 1\n"); - printf(" mode = %d (0 raw eth bridge, 1 interface, 2 bridge)\n", mode); + printf(" mode = %d (0 raw eth bridge, 1 interface, 2 bridge, 3 tcpdump)\n", mode); printf(" password = %s\n", mypassword); printf(" enc_algorithm = %s\n", enc_algorithm == XOR ? "xor" #ifdef ENABLE_OPENSSL @@ -1884,6 +1900,22 @@ int main(int argc, char *argv[]) if (system(buf) != 0) printf(" run cmd: %s returned not 0\n", buf); } + } else if (mode == MODET) { // tcpdump mode + char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */ + read_only = 1; + fdudp[MASTER] = udp_xconnect(argv[i], argv[i + 1], argv[i + 2], argv[i + 3], MASTER); + pcap_handle = pcap_open_live(argv[i+4], MAX_PACKET_SIZE, 0, 1000, errbuf); + if( argc - i == 6) { + struct bpf_program pgm; + if (pcap_compile(pcap_handle, &pgm, argv[i+5], 1, PCAP_NETMASK_UNKNOWN) == -1) { + err_msg("pcap_filter compile error\n"); + exit(0); + } + if (pcap_setfilter(pcap_handle, &pgm) == -1) { + err_msg("pcap_setfilter error\n"); + exit(0); + } + } } if (run_cmd[0]) { // run command when tunnel connected if (debug) @@ -1891,15 +1923,14 @@ int main(int argc, char *argv[]) if (system(run_cmd) != 0) printf(" run cmd: %s returned not 0\n", run_cmd); } + // create a pthread to forward packets from master udp to raw - if (pthread_create(&tid, NULL, (void *)process_udp_to_raw_master, NULL) - != 0) + if (pthread_create(&tid, NULL, (void *)process_udp_to_raw_master, NULL) != 0) err_sys("pthread_create udp_to_raw_master error"); // create a pthread to forward packets from slave udp to raw if (master_slave) - if (pthread_create(&tid, NULL, (void *)process_udp_to_raw_slave, NULL) - != 0) + if (pthread_create(&tid, NULL, (void *)process_udp_to_raw_slave, NULL) != 0) err_sys("pthread_create udp_to_raw_slave error"); if (pthread_create(&tid, NULL, (void *)send_keepalive_to_udp, NULL) != 0) // send keepalive to remote From 571dcb931a1cda3fd8cfba941548cb8e8d01a1b7 Mon Sep 17 00:00:00 2001 From: Zhang Huanjie Date: Wed, 20 Feb 2019 18:00:46 +0800 Subject: [PATCH 2/4] fix compile --- Makefile | 4 ++-- README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index a090639..0c00765 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ GIT_VERSION := $(shell git describe --abbrev=4 --dirty --always --tags) EthUDP:EthUDP.c - gcc -g -Wall -DVERSION=\"$(GIT_VERSION)\" -o EthUDP EthUDP.c -lpthread -lssl -llz4 -lcrypto -D_GNU_SOURCE + gcc -g -Wall -DVERSION=\"$(GIT_VERSION)\" -o EthUDP EthUDP.c -lpthread -lssl -llz4 -lcrypto -lpcap -D_GNU_SOURCE run: - gcc -Wall -DVERSION=\"$(GIT_VERSION)-O3\" -Wunused-result -fno-strict-aliasing -O2 -o EthUDP EthUDP.c -lpthread -lssl -llz4 -lcrypto -D_GNU_SOURCE + gcc -Wall -DVERSION=\"$(GIT_VERSION)-O3\" -Wunused-result -fno-strict-aliasing -O2 -o EthUDP EthUDP.c -lpthread -lssl -llz4 -lcrypto -lpcap -D_GNU_SOURCE indent: EthUDP.c indent EthUDP.c -nbad -bap -nbc -bbo -hnl -br -brs -c33 -cd33 -ncdb -ce -ci4 \ diff --git a/README.md b/README.md index b421e33..e944aff 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,11 @@ Ethernet over UDP, similar of VXLAN, transport Ethernet packet via UDP, support CentOS: ```` -openssl-devel lz4-devel +openssl-devel lz4-devel libpcap-devel ```` Debian ```` -libssl-dev liblz4-dev +libssl-dev liblz4-dev libpcap-dev ```` and Debian liblz4 miss LZ4_compress_fast, you need rebuild it as https://github.com/facebook/mcrouter/issues/149 ```` From ead956fb58a1dac6e65f36d21162a622dec881ce Mon Sep 17 00:00:00 2001 From: Zhang Huanjie Date: Wed, 20 Feb 2019 18:11:06 +0800 Subject: [PATCH 3/4] fix bug --- EthUDP.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EthUDP.c b/EthUDP.c index 363248a..35cbb11 100644 --- a/EthUDP.c +++ b/EthUDP.c @@ -1203,7 +1203,7 @@ void process_raw_to_udp(void) // used by mode==0 & mode==1 continue; len = header->len; } - return; + else return; if (len <= 0) continue; From c6ebab116bfe8430cf60f5efd49f5c9daacbddd9 Mon Sep 17 00:00:00 2001 From: Zhang Huanjie Date: Thu, 7 Mar 2019 19:15:56 +0800 Subject: [PATCH 4/4] buf, mybuf --- EthUDP.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/EthUDP.c b/EthUDP.c index 35cbb11..a35d90d 100644 --- a/EthUDP.c +++ b/EthUDP.c @@ -1103,7 +1103,7 @@ void send_keepalive_to_udp(void) // send keepalive to remote void process_raw_to_udp(void) // used by mode==0 & mode==1 { - u_int8_t buf[MAX_PACKET_SIZE + VLAN_TAG_LEN]; + u_int8_t *buf, mybuf[MAX_PACKET_SIZE + VLAN_TAG_LEN]; u_int8_t nbuf[MAX_PACKET_SIZE + VLAN_TAG_LEN + EVP_MAX_BLOCK_LENGTH + LZ4_SPACE]; u_int8_t *pbuf; int len; @@ -1111,6 +1111,7 @@ void process_raw_to_udp(void) // used by mode==0 & mode==1 while (1) { // read from eth rawsocket if (mode == MODEE) { + buf = mybuf; #ifdef HAVE_PACKET_AUXDATA struct sockaddr from; struct iovec iov; @@ -1191,6 +1192,7 @@ void process_raw_to_udp(void) // used by mode==0 & mode==1 len = recv(fdraw, buf, MAX_PACKET_SIZE, 0); #endif } else if ((mode == MODEI) || (mode == MODEB)) { + buf = mybuf; len = read(fdraw, buf, MAX_PACKET_SIZE); if (len >= MAX_PACKET_SIZE) { err_msg("recv long pkt from raw, len=%d", len);