From 03221f0d051e0e22c4ef9c2e4f8403e0fe166ce3 Mon Sep 17 00:00:00 2001 From: Tomas 'ZeXx86' Jedrzejek Date: Sun, 13 Jun 2010 18:39:10 +0200 Subject: [PATCH] New revision of the Tunnel6 client with improvements and routed prefix support --- client/INSTALL-linux | 2 +- client/INSTALL-win.txt | 60 +++++++++++------------ client/README-linux | 8 ++-- client/README-win.txt | 36 +++++++------- client/src/config.c | 0 client/src/contrib/tunnel6.conf | 12 ++--- client/src/contrib/{tunnel6.conf => tunnel6.txt} | 0 client/src/defaults.h | 2 + client/src/ipv6.c | 61 +++++++++++++++++++----- client/src/ipv6.h | 7 ++- client/src/main.c | 4 ++ client/src/makefile | 6 +-- client/src/proto.c | 32 ++++++++++--- client/src/tun/linux.c | 11 +++-- client/src/tun/win.c | 2 +- client/src/tunnel.c | 3 -- 16 files changed, 154 insertions(+), 92 deletions(-) mode change 100755 => 100644 client/src/config.c mode change 100755 => 100644 client/src/contrib/tunnel6.conf copy client/src/contrib/{tunnel6.conf => tunnel6.txt} (100%) mode change 100755 => 100644 diff --git a/client/INSTALL-linux b/client/INSTALL-linux index faa46bd..192ac93 100644 --- a/client/INSTALL-linux +++ b/client/INSTALL-linux @@ -25,7 +25,7 @@ Installation process: 6. Edit name, password and server in file /etc/tunnel6.conf: nano /etc/tunnel6.conf 7. Start client with command: t6_client -Alternative you could start t6_client without your config file +Alternatively you could start t6_client without your config file with program parameters: t6_client name password server [port] diff --git a/client/INSTALL-win.txt b/client/INSTALL-win.txt index 01f0ac5..d9100d2 100644 --- a/client/INSTALL-win.txt +++ b/client/INSTALL-win.txt @@ -1,30 +1,30 @@ - _ _ ____ -| | | |/ ___| -| |_ _ _ _ __ _ __ ___| / /___ -| __| | | | '_ \| '_ \ / _ \ | ___ \ -| |_| |_| | | | | | | | __/ | \_/ | - \__|\__,_|_| |_|_| |_|\___|_\_____/ - --=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- - -Tunnel6 client require TUN/TAP driver which is in directory "tap-driver" - -Tested versions: - * Windows XP SP2/3 - * Windows Vista - * Windows 7 - -Installation process: - - 1. Extract archive "tap-driver-32_64.zip" - 2. Go to created directory and choose correct version (32bit/64bit) - depend on your OS - 3. Start "addtap.bat" and accept driver installation (it should create virtual TAP device) - 4. Go to "t6_client-*-win32", where * is version. - 5. Edit name, password and server in file tunnel6.txt (for example with notepad) - 6. Start client "t6_client.exe" - -Alternative you could start t6_client without your config file -with program parameters: - t6_client name password server [port] - -Port is used by default 6060 and is not required in config or parameter. + _ _ ____ +| | | |/ ___| +| |_ _ _ _ __ _ __ ___| / /___ +| __| | | | '_ \| '_ \ / _ \ | ___ \ +| |_| |_| | | | | | | | __/ | \_/ | + \__|\__,_|_| |_|_| |_|\___|_\_____/ + +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + +Tunnel6 client require TUN/TAP driver which is in directory "tap-driver" + +Tested versions: + * Windows XP SP2/3 + * Windows Vista + * Windows 7 + +Installation process: + + 1. Extract archive "tap-driver-32_64.zip" + 2. Go to created directory and choose correct version (32bit/64bit) - depend on your OS + 3. Start "addtap.bat" and accept driver installation (it should create virtual TAP device) + 4. Go to "t6_client-*-win32", where * is version. + 5. Edit name, password and server in file tunnel6.txt (for example with notepad) + 6. Start client "t6_client.exe" + +Alternatively you could start t6_client without your config file +with program parameters: + t6_client name password server [port] + +Port is used by default 6060 and is not required in config or parameter. diff --git a/client/README-linux b/client/README-linux index c22e89f..f9e2503 100644 --- a/client/README-linux +++ b/client/README-linux @@ -8,13 +8,13 @@ -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Tunnel6 client is opensource software distributed under GNU/GPL3 licence -which provides you connection over the IPv6 protocol. +which provides you connectivity over the IPv6 protocol. -It use your IPv4 connection to create tunnel between client and server +Client uses your IPv4 connectivity to create tunnel between client and server over the UDP protocol with default port 6060. -It should also pass trough all NATs and classic firewalls.. +It should also go through all NATs and classic firewalls.. -Client use TUN device, which is standard Linux component for creating +It uses TUN device, which is standard Linux component for creating virtual interface and transfering data. diff --git a/client/README-win.txt b/client/README-win.txt index c38c3cc..4325117 100644 --- a/client/README-win.txt +++ b/client/README-win.txt @@ -1,18 +1,18 @@ - _ _ ____ -| | | |/ ___| -| |_ _ _ _ __ _ __ ___| / /___ -| __| | | | '_ \| '_ \ / _ \ | ___ \ -| |_| |_| | | | | | | | __/ | \_/ | - \__|\__,_|_| |_|_| |_|\___|_\_____/ - --=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- - -Tunnel6 client is opensource software distributed under GNU/GPL3 licence -which provides you connection over the IPv6 protocol. - -It use your IPv4 connection to create tunnel between client and server -over the UDP protocol with default port 6060. -It should also pass trough all NATs and classic firewalls.. - -Client use TAP-WIN32 device, which is component developed by -the OpenVPN stuff for creating virtual interface and transfering data. + _ _ ____ +| | | |/ ___| +| |_ _ _ _ __ _ __ ___| / /___ +| __| | | | '_ \| '_ \ / _ \ | ___ \ +| |_| |_| | | | | | | | __/ | \_/ | + \__|\__,_|_| |_|_| |_|\___|_\_____/ + +-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- + +Tunnel6 client is opensource software distributed under GNU/GPL3 licence +which provides you connectivity over the IPv6 protocol. + +Client uses your IPv4 connectivity to create tunnel between client and server +over the UDP protocol with default port 6060. +It should also go through all NATs and classic firewalls.. + +It uses TAP-WIN32 device, which is component developed by +the OpenVPN stuff for creating virtual interface and transfering data. diff --git a/client/src/config.c b/client/src/config.c old mode 100755 new mode 100644 diff --git a/client/src/contrib/tunnel6.conf b/client/src/contrib/tunnel6.conf old mode 100755 new mode 100644 index c800aac..466ee54 --- a/client/src/contrib/tunnel6.conf +++ b/client/src/contrib/tunnel6.conf @@ -1,7 +1,7 @@ -# This is config of the tunnel6 client -# Please rewrite default values (UPPERCASE TEXT) with ones you got from tunnel provider -# Valid variables are - name, password, server and port. Default port is 6060 - -name YOURNAME -password YOURPASSWORD +# This is config of the tunnel6 client +# Please rewrite default values (UPPERCASE TEXT) with ones you got from tunnel provider +# Valid variables are - name, password, server and port. Default port is 6060 + +name YOURNAME +password YOURPASSWORD server TUNNELSERVER \ No newline at end of file diff --git a/client/src/contrib/tunnel6.conf b/client/src/contrib/tunnel6.txt old mode 100755 new mode 100644 similarity index 100% copy from client/src/contrib/tunnel6.conf copy to client/src/contrib/tunnel6.txt diff --git a/client/src/defaults.h b/client/src/defaults.h index 08c580e..37edace 100644 --- a/client/src/defaults.h +++ b/client/src/defaults.h @@ -20,6 +20,8 @@ #ifndef _DEFAULTS_H #define _DEFAULTS_H +#define DEFAULT_VERSION "0.3" + #define DEFAULT_ETHDEV "eth0" #define DEFAULT_PORT 6060 diff --git a/client/src/ipv6.c b/client/src/ipv6.c index 8f195a4..746baac 100644 --- a/client/src/ipv6.c +++ b/client/src/ipv6.c @@ -22,12 +22,19 @@ # include #else # include +# include #endif #include #include "ipv6.h" #include "packet.h" #include "defaults.h" +static int fd; /* FD for IPv6 packet sender */ +static char ipv6[64]; /* temporary buffer used by ipv6_print () */ +#ifdef __WIN32__ +extern const char *inet_ntop (int af, const void *src, char *dst, socklen_t cnt); +#endif + /* compare two IPv6 address */ unsigned ipv6_cmp (ipv6_addr_t ip, ipv6_addr_t ip2) { @@ -39,26 +46,56 @@ unsigned ipv6_cmp (ipv6_addr_t ip, ipv6_addr_t ip2) return 1; } +/* compare two IPv6 prefix address */ +unsigned ipv6_cmp_prefix (ipv6_prefix_t pr, ipv6_prefix_t ip) +{ + /* check valid prefix length */ + if (!pr[15]) + return 0; + + unsigned i; + for (i = 0; i < (pr[15]/8); i ++) + if (pr[i] != ip[i]) + return 0; + + return 1; +} + /* print IPv6 address onto console */ -void ipv6_print (ipv6_addr_t ip) +char *ipv6_print (ipv6_addr_t ip) { - unsigned i = 0; + if (!inet_ntop (AF_INET6, ip, ipv6, 64)) + return 0; + + return ipv6; +} - for (i = 0; i < 7; i ++) { - if (ip[i]) - printf ("%04x:", htons (ip[i])); - else - printf ("0:"); - } +int ipv6_send (char *data, int len) +{ + struct sockaddr_in6 sockaddr; + struct proto_ipv6_t *ip = (struct proto_ipv6_t *) data; - if (ip[7]) - printf ("%x", htons (ip[7])); - else - printf ("0"); + /* prepare data for RAW socket */ + sockaddr.sin6_family = AF_INET6; + sockaddr.sin6_port = 0; + memcpy (&sockaddr.sin6_addr.s6_addr, ip->dest, sizeof (ipv6_addr_t)); + + /* send the packet */ + if (sendto (fd, data, len, 0, (struct sockaddr *) &sockaddr, sizeof (struct sockaddr_in6)) != len) { + perror ("sendto()"); + return -1; + } + + return 0; } int ipv6_init () { + /* initialize raw socket */ + if ((fd = socket (AF_INET6, SOCK_RAW, IPPROTO_RAW)) == -1) { + perror ("socket()"); + return -1; + } return 0; } diff --git a/client/src/ipv6.h b/client/src/ipv6.h index 1e94cef..dd8e569 100644 --- a/client/src/ipv6.h +++ b/client/src/ipv6.h @@ -23,7 +23,8 @@ #define IPV6_TYPE_ICMPv6 0x3a /* IPv6 address */ -typedef unsigned short ipv6_addr_t[8]; +typedef unsigned short ipv6_addr_t[8]; /* used to store ipv6 address */ +typedef unsigned char ipv6_prefix_t[16];/* used to store the prefix address - last byte is length */ /* IPv6 header structure */ struct proto_ipv6_t { @@ -42,7 +43,9 @@ struct proto_ipv6_t { }; extern unsigned ipv6_cmp (ipv6_addr_t ip, ipv6_addr_t ip2); -extern void ipv6_print (ipv6_addr_t ip); +extern unsigned ipv6_cmp_prefix (ipv6_prefix_t pr, ipv6_prefix_t ip); +extern char *ipv6_print (ipv6_addr_t ip); +extern int ipv6_send (char *data, int len); extern int ipv6_init (); #endif diff --git a/client/src/main.c b/client/src/main.c index fdf826c..1dccc24 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -21,6 +21,7 @@ #include #include #include "sig.h" +#include "ipv6.h" #include "poll.h" #include "proto.h" #include "config.h" @@ -42,6 +43,9 @@ int init (char *addr, unsigned short port, char *name, char *pwd) if (tunnel_init (addr, port, name, pwd) == -1) return -1; + if (ipv6_init () == -1) + return -1; + if (tundev_setup () == -1) return -1; diff --git a/client/src/makefile b/client/src/makefile index 54e8425..0dcd45b 100644 --- a/client/src/makefile +++ b/client/src/makefile @@ -12,16 +12,16 @@ endif all: $(OBJS) $(BIN) clean: - $(Q)rm -f $(BIN) $(OBJS) + $(Q)rm -f $(BIN)* $(OBJS) @printf " CLEAN\n"; install: - $(Q)cp $(BIN) /usr/bin/ + $(Q)cp $(BIN) /usr/local/sbin/ $(Q)cp -n contrib/tunnel6.conf /etc/ @printf " INSTALL\n"; uninstall: - $(Q)rm /usr/bin/$(BIN) + $(Q)rm /usr/local/sbin/$(BIN) $(Q)rm /etc/tunnel6.conf @printf " UNINSTALL\n"; .c.o: diff --git a/client/src/proto.c b/client/src/proto.c index 3aa327e..739bded 100644 --- a/client/src/proto.c +++ b/client/src/proto.c @@ -33,8 +33,9 @@ #include "defaults.h" extern int cont; /* Continue in tunnel processing ? 1 - yes / 0 - no */ -extern ipv6_addr_t tunip; /* IPv6 address obtained from tunnel server */ -extern ipv6_addr_t tungw; /* IPv6 gateway obtained from tunnel server */ +ipv6_addr_t tunip; /* IPv6 address obtained from tunnel server */ +ipv6_addr_t tungw; /* IPv6 gateway obtained from tunnel server */ +ipv6_prefix_t tunpr; /* IPv6 prefix obtained from tunnel server */ /* PROTOCOL Command - LOGIN */ int proto_login (char *data, char *name, char *pwd) @@ -86,12 +87,21 @@ int proto_login (char *data, char *name, char *pwd) /* save obtained IPv6 address */ memcpy (tunip, data+1, sizeof (ipv6_addr_t)); /* save obtained IPv6 gateway */ - memcpy (tungw, data+1+ sizeof (ipv6_addr_t), sizeof (ipv6_addr_t)); - - printf ("> Login succesfull !\n> Obtained IPv6: "); - ipv6_print (tunip); - printf ("\n"); + memcpy (tungw, data+1+sizeof (ipv6_addr_t), sizeof (ipv6_addr_t)); + /* save obtained IPv6 prefix for routing */ + memcpy (tunpr, data+1+2*sizeof (ipv6_prefix_t), sizeof (ipv6_prefix_t)); + + printf ("> Login succesfull !\n> Obtained IPv6: %s\n", ipv6_print (tunip)); + /* Display routed prefix when available */ + if (tunpr[15]) { + ipv6_addr_t tmp; + memcpy (tmp, (void *) tunpr, sizeof (ipv6_addr_t)-1); + memset ((char *) tmp + 15, 0, 1); + + printf ("> Routed IPv6 prefix: %s/%u\n", ipv6_print (tmp), (unsigned char) tunpr[15]); + } + return 0; } @@ -134,6 +144,14 @@ int proto_cmd (char *data, unsigned len) /* TUNNEL COMMUNICATION HANDLER */ int proto_comm (char *data, unsigned len) { +#ifndef __WIN32__ + struct proto_ipv6_t *ip = (struct proto_ipv6_t *) data; + + /* we need check incoming destination address + because it could be one of our routed client packet */ + if (ipv6_cmp_prefix (tunpr, (unsigned char *) ip->dest)) + return ipv6_send (data, len); +#endif /* send received data to virtual TUN device */ return tundev_send (data, len); } diff --git a/client/src/tun/linux.c b/client/src/tun/linux.c index efc533e..0005820 100644 --- a/client/src/tun/linux.c +++ b/client/src/tun/linux.c @@ -36,14 +36,15 @@ #include "packet.h" #include "defaults.h" -static int fd; /* FD of the TUN/TAP device */ +static int fd; /* FD of the TUN/TAP device */ static char data[DEFAULT_MTU+1]; static char dev[IFNAMSIZ+1]; -extern int fd_tun; /* polling FD of the TUN/TAP device */ +extern int fd_tun; /* polling FD of the TUN/TAP device */ -extern ipv6_addr_t tunip; -extern ipv6_addr_t tungw; +extern ipv6_addr_t tunip; /* IPv6 address obtained from tunnel server */ +extern ipv6_addr_t tungw; /* IPv6 gateway obtained from tunnel server */ +extern ipv6_prefix_t tunpr; /* IPv6 prefix obtained from tunnel server */ static int tundev_arch_alloc (int flags) { @@ -101,7 +102,7 @@ int tundev_arch_poll () struct proto_ipv6_t *ip = (struct proto_ipv6_t *) &data; - if (!ipv6_cmp (ip->src, tunip)) { + if (!ipv6_cmp (ip->src, tunip) && !ipv6_cmp_prefix (tunpr, (unsigned char *) ip->src)) { printf ("WARNING -> This is not our packet\n"); return 0; } diff --git a/client/src/tun/win.c b/client/src/tun/win.c index 61fdd1b..481ace4 100644 --- a/client/src/tun/win.c +++ b/client/src/tun/win.c @@ -411,7 +411,7 @@ int tundev_arch_send (char *packet, unsigned len) } #ifndef inet_ntop -const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) +const char *inet_ntop (int af, const void *src, char *dst, socklen_t cnt) { if (af == AF_INET) { struct sockaddr_in in; diff --git a/client/src/tunnel.c b/client/src/tunnel.c index 51ef602..ac9397a 100644 --- a/client/src/tunnel.c +++ b/client/src/tunnel.c @@ -46,9 +46,6 @@ extern SOCKET fd_udp; /* Polling socket */ static struct sockaddr_in sockname; /* Session information */ static char data[DEFAULT_MTU+1]; /* Receiver data buffer */ -ipv6_addr_t tunip; /* IPv6 address obtained from tunnel server */ -ipv6_addr_t tungw; /* IPv6 gateway obtained from tunnel server */ - /* receive data from tunnel server */ int tunnel_recv () { -- 2.11.4.GIT