We will do IPv4 tunneling using GRE. GRE is a tunneling protocol that was originally developed by Cisco, and it can do a few more things than IP-in-IP tunneling. For example, you can also transport multicast traffic and IPv6 through a GRE tunnel.
We are using Debian with linux kernel 2.4.26. In Linux, you'll need the ip_gre.o module.
We have 2 routers X and Y, and intermediate network C (or let's say, Internet).
Router X is connected to the Internet on interface eth0 and network A on eth1.
interface eth0 :: address 188.8.131.52 on the Internet (or network C) interface eth1 :: address 10.0.2.1, network 10.0.2.0/24 (network A)
Router Y is connected to the Internet on interface eth0, network B on eth1 and network C on eth2.
interface eth0 :: address 184.108.40.206 on the Internet (or network C) interface eth1 :: address 10.0.3.1, network 10.0.3.0/24 (network B) interface eth2 :: address 10.0.4.1, network 10.0.4.0/24 (network C)
As far as network C is concerned, we assume that it will pass any packet sent from X to Y and vice versa. How and why, we do not care.
Create a tunnel between router X and Y, such that we can route traffic from network A (connected to X) to networks B and C (connected to Y). This tunnel will look just like a wire between the two routers with its own subnet (10.0.201.0/24)
On router X, commands are
iptunnel add tunX mode gre remote 220.127.116.11 local 18.104.22.168 ttl 225 ifconfig tunX 10.0.201.1/24 ifconfig tunX up ifconfig tunX pointopoint 10.0.201.2 ifconfig tunX multicast
In line 1, we added a tunnel device, and called it tunX. Furthermore we told it to use the GRE protocol (mode gre), that the remote address is 22.214.171.124 (the router Y at the other end), that our tunneling packets should originate from 126.96.36.199 (which allows your router to have several interfaces and choose which one to use for tunneling) and that the TTL field of the packet should be set to 255 (ttl 255).
Line 2 gives the newly born interface tunY the address 10.0.201.1.
Line 3 enables the device.
Line 4 is necessary to set the IP address of the peer. Need when using dynamic routing with RIP/OSPF with Zebra. Refer to Routing HOWTO for more details.
Line 5 is necessary to enable multicast - so that routing with Zebra works (they normally multicast routing updates).
One router Y, commands are
iptunnel add tunY mode gre local 188.8.131.52 remote 184.108.40.206 ttl 225 ifconfig tunY 10.0.201.2/24 ifconfig tunY up ifconfig tunY pointopoint 10.0.201.1 ifconfig tunY multicast
Tunnel X<->Y Now we created a tunnel on the 10.0.201.0/24 network from router X to Y and vice versa.
routerX ----------------tunnel-----------------routerY 10.0.201.1 10.0.201.2 (tunX) (tunY)
We can send packets on the 10.0.201.0/24 network from router X to Y and vice versa. So we can ping router X from Y on the tunnel interface.
routerX# ping 10.0.201.2 routerY# ping 10.0.201.1
However, if we to send packets to network B or C from router X, we need to add routes so that traffic for these networks is sent on the tunnelling interface.
On router X:
route add -net 10.0.3.1/24 gw 10.0.201.1 dev tunX route add -net 10.0.4.1/24 gw 10.0.201.1 dev tunX
Similarily, to send packets to network A from router Y, we need to add a route.
On router Y:
route add -net 10.0.2.1/24 gw 10.0.201.2 dev tunY
On router X:
ifconfig tunX down iptunnel del tunX
(network A) 10.0.2.1, eth1 | ___|_________ | Router X | |_____________| | 220.127.116.11 (eth0) | (Internet or network C) | | | | 10.0.201.1 (tunX) | | | | | | (gre tunnel: 18.104.22.168 <-> 22.214.171.124) | | | | | | 10.0.201.2 (tunY) | | (Internet or network C) | 126.96.36.199 (eth0) ___|___________ | Router Y | |_______________| | | | | 10.0.3.1 10.0.4.1 eth1 eth2 (network B) (network C)
auto tun0 iface tun0 inet static address 10.0.201.1 netmask 255.255.255.0 broadcast 10.0.201.255 up ifconfig tun0 multicast pre-up iptunnel add tun0 mode gre remote 188.8.131.52 local 184.108.40.206 ttl 255 pointopoint 10.0.201.2 post-down iptunnel del tun0
auto tun0 iface tun0 inet static address 10.0.201.2 netmask 255.255.255.0 broadcast 10.0.201.255 up ifconfig tun0 multicast pre-up iptunnel add tun0 mode gre local 220.127.116.11 remote 18.104.22.168 ttl 255 pointopoint 10.0.201.1 post-down iptunnel del tun0