VPN(virtual private network)
VPN(virtual private network)
How VPN works?
Background: 3 properties of the packet- user authenticated
- content protected
- integrity preserved
To achieve the goal, we have to encrypt the packet, but we cannot simply encrypt all the IP packet because the header will be encrypted so that the router cannot read the header of the packets and change the header(TTL and checksum).
To solve this problem, we have IP tunneling.
- IPSec Tunneling. Based on IP layer, and encapsulated the old IP packet into a new IP packet. This implement in the kernel.
- TLS/SSL Tunneling. Based on transport layer, and encapsulated inside a TCP or UDP packet. Both end of the tunnel utilize the TLS/SSL protocol on top of TCP/UDP.
TLS/SSL is more popular because update application is much easier than update tha OS.
What is tun/tap: tun is at layer 3 and tap is at layer 2, Ethernet.
TUN/TAP provides packet reception and transmission for user space programs
e.g. : The packet from 10.0.7.0/24 want to send to 10.0.8.0/24. All the packet will send to VPN client first.
Step 2: The IP packet wants to go to 10.0.8.0/24 will go through tun interface and proceed by openVPN program, then send out through eth0 interface. All this happened in the user space on the client-side. The new IP header and UDP/TCP header added at this time. The destination IP and source IP are VPN server's and VPN client's IP.
Step 3: When the packet arrives at the VPN server, the new IP header and the UDP/TCP header will be stripped away, and the original IP packet will be sent to the destination computer according to the destination IP in the header.
Both VPN-Client and VPN-server has at least two Virtual network interface(VIF) which are tun/tap and eth0. They are in different network mask, such as '10.0.7.0' and '192.230.2.0'.
Problem: We are going to ping '192.168.60.101' from '10.0.2.7'
Create a VPN tunnel via TUN/TAP
Make a tunnel and set its network segment as 192.168.53.0/24.- On VPN server(10.0.2.10/192.168.60.1)
sudo ifconfig tun0 192.168.53.1/24 up
make it transmit the packet
sudo sysctl net.ipv4.ip_forward=1
Add route entry that all the traffic related to '192.168.53.0' direct to 'tun0' interface.
sudo route add -net 192.168.53.0/24 tun0
- Set the VPN client (10.0.2.7)
sudo ifconfig tun0 192.168.53.5/24 up
Set all the traffic related to '192.168.60.0' go through 'tun0'.
sudo route add -net 192.168.60.0/24 tun0
Try to ping '192.168.60.101' from '10.0.2.7'.