Table of Contents

VPN with Tinc

Tinc is open source software for creating VPNs, virtual private networks over other physical channel such as the Internet, where individual participating hosts (nodes) appear to applications as if connected by wire in LAN.

Overview

Tinc utilizes asymmetric cryptography. Each node has its own private key, a public key and another public key; one for each participating node. These files are, together with a few configuration files, stored in /etc/tinc/<VPN name> directory.

Each node also runs a daemon (or multiple daemons, one for each separate VPN). Daemon listens on set port (default is 655) for incoming connections from other nodes. Only nodes with valid private keys can produce data decipherable with matching public keys and are thus granted access.

Public key file may contain not only key itself, but also public IP address (and port) of node to which it belongs. If set to, daemon will not wait for connections, but will attempt to connect to these known nodes.

Each node has its own IP address (in private address space) which, once the daemon is running, is assigned to virtual network interface. Any traffic coming from VPN is processed by the daemon and made come from that network interface, and any traffic send through that interface is also processed by the daemon and sent to VPN, all behind the scenes, transparent to applications.

Important feature of Tinc is that daemon can (and by default does) forward traffic for other nodes, e.g. if nodes A and B are behind NAT and can directly communicate with only node C, which has unrestricted internet access, or even do not know public key of each other, but C knows them both, C will happily forward traffic between/for them. They just need to know IP addresses (in private address space).

Installation

Compile using SlackBuild

Compile from source

# ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var
# make
# make install
$ make DESTDIR=/somewhere/else install

Configuration

# tinc -n VPNtest init node1
$ tinc -c . generate-keys
$ mkdir -p VPNtest/hosts
$ mv *.priv VPNtest/.
$ cat rsa_key.pub ecdsa_key.pub > VPNtest/hosts/node1
$ rm rsa_key.pub ecdsa_key.pub
tinc.conf
Name = node1
ConnectTo = node2
Interface = vpnNIC
Port = 6655
tinc-up
#!/bin/sh
ip addr add 192.168.1.1/24 dev vpnNIC
ip route add 192.168.1.0/24 dev vpnNIC
ip link set vpnNIC up
tinc-down
#!/bin/sh
ip link set vpnNIC down
ip route del 192.168.1.0/24 dev vpnNIC
ip addr del 192.168.1.1/24 dev vpnNIC
node1
Address = <public IP address> [port]
Subnet = 192.168.1.1/32
-----BEGIN RSA PUBLIC KEY-----
...
# tincd -n VPNtest --debug=5 --logfile=/var/log/VPNtest.log

Windows

For sake of completeness, as you might want to e.g. build a VPN with Linux machine as fileserver accessed by Windows, remote-manage bunch of Windows behind NAT from Linux, play games, whatever, let's cover also Windows (XP, 7 and 8 are known to work).

Installation

C:\path\to\tapinstall.exe remove tap0901
C:\path\to\tapinstall.exe install OemWin2k.inf tap0901

Configuration

There are a few differences in Windows configuration.

netsh interface ip set address name="Local Area Connection number" static <IP address> <mask>
C:\path\to\tincd.exe --debug=5 --logfile=C:\path\to\file.log -n VPNtest
cmd> net start tinc.VPNtest

RC script

Here is some script to start all VPNs on boot. Note that stop command differs between 1.0 and 1.1 (prerelease) branches; 1.0 calls tincd, 1.1 calls tinc (no d).

#!/bin/sh
 
VPNS=$(ls /etc/tinc)
 
start () {
	for VPN in $VPNS; do
		echo "Starting tinc daemon for $VPN..."
		/usr/sbin/tincd -n "$VPN" -d1 --logfile=/var/log/tinc."$VPN"
	done
}
 
stop () {
	for VPN in $VPNS; do
		echo "Stopping tinc daemon for $VPN..."
		/usr/sbin/tinc -n "$VPN" stop
	done
}
 
restart () {
	stop
	sleep 1
	start
}
 
case "$1" in
	("start")
		start
		;;
	("stop")
		stop
		;;
	("restart")
		restart
		;;
	(*)
		echo "Usage: $0 <start|stop|restart>"
		exit 1
esac
 
exit 0

Save it as e.g. /etc/rc.d/rc.tinc, make executable and then add line to rc.local.

rc.local
/etc/rc.d/rc.tinc start

Sources

Tinc website/documentation http://www.tinc-vpn.org