IP in IP tunneling
5.1 A few general remarks about tunn
Tunnels can be used to do some very unusual and very cool stuff. They can also make things go horribly wrong when you don't configure them right. Don't point your default route to a tunnel device unless you know exactly what you are doing :-). Furthermore, tunneling increases overhead, because it needs an extra set of IP headers. Typically this is 20 bytes per packet, so if the normal packet size (MTU) on a network is 1500 bytes, a packet that is sent through a tunnel can only be 1480 bytes big. This is not necessarily a problem, but be sure to read up on IP packet fragmentation/reassembly when you plan to connect large networks with tunnels. Oh, and of course, the fastest way to dig a tunnel is to dig at both sides.
This kind of tunneling has been available in Linux for a long time. It requires 2 kernel modules, ipip.o and new_tunnel.o.
Let's say you have 3 networks: Internal networks A and B, and intermediate network C (or let's say, Internet). So we have network A:
The router has address 172.16.17.18 on network C.
network 10.0.1.0 netmask 255.255.255.0 router 10.0.1.1
and network B:
The router has address 172.19.20.21 on network C.
network 10.0.2.0 netmask 255.255.255.0 router 10.0.2.1
As far as network C is concerned, we assume that it will pass any packet sent from A to B and vice versa. You might even use the Internet for this.
Here's what you do:
First, make sure the modules are installed:
Then, on the router of network A, you do the following:
insmod ipip.o insmod new_tunnel.o
And on the router of network B:
ifconfig tunl0 10.0.1.1 pointopoint 172.19.20.21 route add -net 10.0.2.0 netmask 255.255.255.0 dev tunl0
And if you're finished with your tunnel:
ifconfig tunl0 10.0.2.1 pointopoint 172.16.17.18 route add -net 10.0.1.0 netmask 255.255.255.0 dev tunl0
Presto, you're done. You can't forward broadcast or IPv6 traffic through an IP-in-IP tunnel, though. You just connect 2 IPv4 networks that normally wouldn't be able to talk to each other, that's all. As far as compatibility goes, this code has been around a long time, so it's compatible all the way back to 1.3 kernels. Linux IP-in-IP tunneling doesn't work with other Operating Systems or routers, as far as I know. It's simple, it works. Use it if you have to, otherwise use GRE.
ifconfig tunl0 down
5.3 GRE tun
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.
In Linux, you'll need the ip_gre.o module.
IPv4 Tunneling
Let's do IPv4 tunneling first:
Let's say you have 3 networks: Internal networks A and B, and intermediate network C (or let's say, Internet).
So we have network A:
The router has address 172.16.17.18 on network C. Let's call this network neta (ok, hardly original)
network 10.0.1.0 netmask 255.255.255.0 router 10.0.1.1
and network B:
The router has address 172.19.20.21 on network C. Let's call this network netb (still not original)
network 10.0.2.0 netmask 255.255.255.0 router 10.0.2.1
As far as network C is concerned, we assume that it will pass any packet sent from A to B and vice versa. How and why, we do not care.
On the router of network A, you do the following:
ip tunnel add netb mode gre remote 172.19.20.21 local 172.16.17.18 ttl 255 ip addr add 10.0.1.1 dev netb ip route add 10.0.2.0/24 dev netb
Let's discuss this for a bit. In line 1, we added a tunnel device, and called it netb (which is kind of obvious because that's where we want it to go). Furthermore we told it to use the GRE protocol (mode gre), that the remote address is 172.19.20.21 (the router at the other end), that our tunneling packets should originate from 172.16.17.18 (which allows your router to have several IP addresses on network C and let you decide which one to use for tunneling) and that the TTL field of the packet should be set to 255 (ttl 255).
In the second line we gave the newly born interface netb the address 10.0.1.1. This is OK for smaller networks, but when you're starting up a mining expedition (LOTS of tunnels), you might want to consider using another IP range for tunneling interfaces (in this example, you could use 10.0.3.0).
In the third line we set the route for network B. Note the different notation for the netmask. If you're not familiar with this notation, here's how it works: you write out the netmask in binary form, and you count all the ones. If you don't know how to do that, just remember that 255.0.0.0 is /8, 255.255.0.0 is /16 and 255.255.255.0 is /24. Oh, and 255.255.254.0 is /23, in case you were wondering.
But enough about this, let's go on with the router of network B.
And when you want to remove the tunnelon router A:
ip tunnel add neta mode gre remote 172.16.17.18 local 172.19.20.21 ttl 255 ip addr add 10.0.2.1 dev neta ip route add 10.0.1.0/24 dev neta
Of course, you can replace netb with neta for router B.
ip link set netb down ip tunnel del netb
IPv6 Tunneling
A short bit about IPv6 addresses:
IPv6 addresses are, compared to IPv4 addresses, monstrously big. An example:
3ffe:2502:200:40:281:48fe:dcfe:d9bc
So, to make writing them down easier, there are a few rules:
- Don't use leading zeroes. Same as in IPv4.
- Use colons to separate every 16 bits or two bytes.
- When you have lots of consecutive zeroes, you can write this down as ::. You can only do this once in an address and only for quantities of 16 bits, though.
On with the tunnels.
Let's assume that you have the following IPv6 network, and you want to connect it to 6bone, or a friend.
Your IPv4 address is 172.16.17.18, and the 6bone router has IPv4 address 172.22.23.24.
Network 3ffe:406:5:1:5:a:2:1/96
ip tunnel add sixbone mode sit remote 172.22.23.24 local 172.16.17.18 ttl 255 ip link set sixbone up ip addr add 3ffe:406:5:1:5:a:2:1/96 dev sixbone ip route add 3ffe::/15 dev sixbone
Let's discuss this. In the first line, we created a tunnel device called sixbone. We gave it mode sit (which is IPv6 in IPv4 tunneling) and told it where to go to (remote) and where to come from (local). TTL is set to maximum, 255. Next, we made the device active (up). After that, we added our own network address, and set a route for 3ffe::/15 (which is currently all of 6bone) through the tunnel.
GRE tunnels are currently the preferred type of tunneling. It's a standard that's also widely adopted outside the Linux community and therefore a Good Thing.
相關文章
- IP協議&&IP首部協議
- 【TCP/IP】IP地址分類和特殊IP地址TCP
- TCP/IP、UDP/IP協議TCPUDP協議
- Centos 7 檢視本機IP 編輯IP 修改ipCentOS
- ip addr沒有ip顯示?
- 解析原生IP和廣播IP
- 蘋果IP:如何換IP?換IP最簡單的方法分享蘋果
- 4. 自動封IP和解IP
- 什麼是IP地址、IP協議?協議
- 彈性公網IP(Elastic IP,EIP)AST
- 11g RAC 修改PUBLIC-IP、VIP、PRIV-IP、SCAN-IP
- IP地址
- Oracle RAC修改Scan IP,Public IP的方法Oracle
- 如何區分原生IP跟廣播IP
- 切換代理IP時如何檢查IP?
- IP是怎麼執行的如何查到IP找出IP具體位置資訊!
- 靜態IP與動態IP有什麼區別?海外代理IP詳解
- IP根來了,假IP再難坑你
- 【TCP/IP】IP地址的劃分及其分類TCP
- Linux多ip地址如何刪除多餘ipLinux
- Java中的TCP/IP協議和IP地址JavaTCP協議
- 伺服器忘記IP後找回IP地址伺服器
- IP地址的概念及IP子網劃分
- 如何來區分原生IP跟廣播IP
- 獨享ip與共享ip的選擇技巧
- (轉)芝麻代理趣解:什麼是動態ip、內網IP、以及外網ip內網
- TCP/IP族TCP
- TCP/IP模型TCP模型
- 7、IP隧道
- IP組播
- IP塊拆分
- docker 指定 ipDocker
- 新增SCAN IP
- openstack指定IP
- TCP / IP AT命令TCP
- 使用代理IP輕鬆獲得韓國IP地址
- 在海外代理IP服務中,如何使用代理IP
- 系列TCP/IP協議-靜態IP選路(007)TCP協議
- 獨享IP與共享IP的三大區別