![]()
|
目錄
0. 前言
1. Linux核心支援的隧道類型
2. GRE隧道跨(公)網連接配接相同子網位址主機
2.1 拓撲環境:
2.1 ubantu配置:
2.1.1 添加GRE隧道
2.1.2 配置GRE隧道接口IP
2.1.3 激活GRE隧道接口IP
2.2 CentOS配置:
2.3 ping包測試通訊鍊路:
3. GRE隧道跨(公)網連接配接不同子網位址主機
3.1 拓撲環境
3.2 Ubantu配置
3.2.1 配置ens33子接口IP
3.2.2 添加另一個GRE隧道接口并up
3.2.3 配置隧道接口IP
3.2.4 添加對端子網路由表
3.3 CentOS配置
3.3.1 配置ens33子接口IP
3.3.2 添加另一個GRE隧道接口并up
3.3.3 配置隧道接口IP
3.3.4 添加對端子網路由表
3.4 ping測試鍊路連通性
0. 前言
在學習ipsec過程中,一般都會涉及到ipsec的局限性:ipsec協定是一種點對點協定,不支援多點傳播,也不能保護多點傳播、廣播封包。是以ipsec協定無法用于音視訊會議等場合,此時通常的解決辦法是采用GRE Over IPSec. 給出的解釋是:GRE協定可以封裝多點傳播、廣播封包,但是無法對業務内容進行加密;而ipsec可以對封包進行加密,但是無法封裝多點傳播和廣播封包。是以将兩種協定結合,因而GRE over IPSec協定應運而生。 但是我找了很多資料(其實沒有多少),都沒有找到為什麼GRE協定支援封裝多點傳播和廣播封包,而ipsec不行;他們作為點對點協定,為什麼GRE可以而IPsec不行呢? 因為沒有找到答案,是以不能證明自己的想法正确與否,于是通過搭建GRE隧道環境,學習Linux核心中GRE隧道的操作配置原則,希望能從中得到些許啟發。
搭建GRE隧道環境實際上是很簡單的,因為Linux核心已經支援了GRE隧道,是以直接在虛拟機(ubantu和CentOS)裡進行簡單的配置即可完成操作。
1. Linux核心支援的隧道類型
目前Linux核心已經支援多種隧道類型,包括:IPIP隧道,GRE隧道,... 。其餘這幾個我也沒見過。當然除了這幾種,還有ipsec協定,l2tp協定,可以的是我目前都還沒有用過,實在是暴殄天物,罪過罪過
下面通過搭建兩組拓撲環境,來學習GRE隧道的基本規則,然後在此基礎上分析下GRE和IPSEC在多點傳播和廣播封包封裝的表現出不同行為的可能原因(另寫一遍文章喽)。
家裡裝置有限,隻有一台電腦,還是通過無線網卡來上網的,是以拓撲環境比較寒酸,其實這些并不重要,隻需明白我的兩台虛拟機Ubantu和CentOS通過橋接的方式連在一起,網絡上可以通訊即可。
現在,我準備将兩個位址分别為10.1.2.1/24、10.1.2.2/24的兩台主機(假的,隻是個IP位址而已)通過GRE隧道連接配接起來進行通訊。原則上這兩個位址屬于不同的子網中(Ubantu中的子網和CentOS中的子網,隻是這兩個子網的網段相同僅此而已),無法直接通訊。
首先,我确定了下該虛拟機中是否存在gre相關的接口,結果當然是沒有了。
root@ubantu:/home/toney# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 2409:8a00:18e9:810:141:383c:ac6f:8b11 prefixlen 64 scopeid 0x0<global>
inet6 2409:8a00:18e9:810:f8dd:500e:b1a:463b prefixlen 64 scopeid 0x0<global>
inet6 fe80::8e84:574c:7a8b:440a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fb:db:ad txqueuelen 1000 (Ethernet)
RX packets 3581 bytes 1030105 (1.0 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1177 bytes 114532 (114.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 160 bytes 12920 (12.9 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 160 bytes 12920 (12.9 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip tunnel add Tunnel-1 mode gre remote 192.168.1.13 local 192.168.1.10
添加GRE隧道時,需要指定隧道的名稱,我這裡叫做Tunnel-1; 因為要搭建GRE類型隧道,是以mode為gre。 而GRE隧道的位址為192.168.1.13<========>192.168.1.10。
由于是第一次執行此指令,底層實際上安裝上了gre隧道相關的驅動。使用者隻有在此基礎上才能配置GRE隧道,不過,無需擔心,核心自動完成。
如果在添加隧道時不指定remote和local位址,也是可以的,它實際上是隻是安裝gre隧道相關驅動。下面是添加隧道時沒有指定remote和local位址時的結果:
root@ubantu:/home/toney# ip tunnel add Tunnel-1
cannot determine tunnel mode (ipip, gre, vti or sit)
root@ubantu:/home/toney# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 2409:8a00:18e9:810:141:383c:ac6f:8b11 prefixlen 64 scopeid 0x0<global>
inet6 2409:8a00:18e9:810:200d:99e2:4f3d:cb6f prefixlen 64 scopeid 0x0<global>
inet6 fe80::8e84:574c:7a8b:440a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fb:db:ad txqueuelen 1000 (Ethernet)
RX packets 5038 bytes 5896155 (5.8 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2744 bytes 309264 (309.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 164 bytes 15044 (15.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 164 bytes 15044 (15.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@ubantu:/home/toney# ip tunnel add Tunnel-1 mode gre
add tunnel "gre0" failed: File exists
root@ubantu:/home/toney# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 2409:8a00:18e9:810:141:383c:ac6f:8b11 prefixlen 64 scopeid 0x0<global>
inet6 2409:8a00:18e9:810:200d:99e2:4f3d:cb6f prefixlen 64 scopeid 0x0<global>
inet6 fe80::8e84:574c:7a8b:440a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fb:db:ad txqueuelen 1000 (Ethernet)
RX packets 5097 bytes 5900501 (5.9 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2766 bytes 311496 (311.4 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
erspan0: flags=4098<BROADCAST,MULTICAST> mtu 1450
ether 00:00:00:00:00:00 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
gre0: flags=128<NOARP> mtu 1452
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 1000 (UNSPEC)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
gretap0: flags=4098<BROADCAST,MULTICAST> mtu 1462
ether 00:00:00:00:00:00 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 164 bytes 15044 (15.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 164 bytes 15044 (15.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@ubantu:/home/toney#
root@ubantu:/home/toney# lsmod | grep gre
ip_gre 28672 0
ip_tunnel 24576 1 ip_gre
gre 16384 1 ip_gre
root@ubantu:/home/toney#
如果輸入完整的指令,則會成功添加上Tunnel-1隧道:
root@ubantu:/home/toney# ifconfig -a
Tunnel-1: flags=144<POINTOPOINT,NOARP> mtu 1476
unspec C0-A8-01-0A-00-00-00-87-00-00-00-00-00-00-00-00 txqueuelen 1000 (UNSPEC)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 2409:8a00:18e9:810:141:383c:ac6f:8b11 prefixlen 64 scopeid 0x0<global>
inet6 2409:8a00:18e9:810:200d:99e2:4f3d:cb6f prefixlen 64 scopeid 0x0<global>
inet6 fe80::8e84:574c:7a8b:440a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:fb:db:ad txqueuelen 1000 (Ethernet)
RX packets 5745 bytes 5947674 (5.9 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2965 bytes 329731 (329.7 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
erspan0: flags=4098<BROADCAST,MULTICAST> mtu 1450
ether 00:00:00:00:00:00 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
gre0: flags=128<NOARP> mtu 1452
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 1000 (UNSPEC)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
gretap0: flags=4098<BROADCAST,MULTICAST> mtu 1462
ether 00:00:00:00:00:00 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 169 bytes 15509 (15.5 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 169 bytes 15509 (15.5 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip addr add 10.1.2.1/24 dev Tunnel-1
或者
ifconfig Tunnel-1 10.1.2.1/24
從添加隧道結果來看,Tunnel-1接口雖然已經成功添加,但是處于down狀态,此外也沒有IP位址。 不,等等,那我們在添加隧道時指定的remote和local是什麼呢?
它是經過GRE隧道封裝後的封包IP位址,但是針對什麼封包進行封裝,目前我們尚未配置。
配置此接口IP的目的是:為了确定哪些封包需要進入GRE接口,然後進行隧道封裝。為什麼需要添加IP呢? 因為我們是通過路由将封包引入到Tunnel-1接口的, 如果不填IP,那麼我路由的下一條該寫成什麼呢, 是吧。
ifconfig Tunnel-1 up
ip link set Tunnel-1 up
至于配置接口IP和是否up接口,沒有什麼先後順序,把他們當做不同的eth接口處理即可。
CentOS虛拟機的配置和Ubantu的配置完全相同,安照此步驟操作即可。
ip tunnel add Tunnel-1 mode gre local 192.168.1.13 remote 192.168.1.10
ip link set Tunnel-1 up
ip addr add 10.1.2.2/24 dev Tunnel-1
檢視配置結果r如下:
[root@toney toney]#
[root@toney toney]#
[root@toney toney]# ifconfig
Tunnel-1 Link encap:UNSPEC HWaddr C0-A8-01-0D-FF-FF-90-6D-00-00-00-00-00-00-00-00
inet addr:10.1.2.2 P-t-P:10.1.2.2 Mask:255.255.255.0
UP POINTOPOINT RUNNING NOARP MTU:1476 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
eth0 Link encap:Ethernet HWaddr 00:0C:29:DA:34:3C
inet addr:192.168.1.13 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: 2409:8a00:18e9:810:20c:29ff:feda:343c/64 Scope:Global
inet6 addr: fe80::20c:29ff:feda:343c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3571 errors:0 dropped:0 overruns:0 frame:0
TX packets:781 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:236685 (231.1 KiB) TX bytes:71021 (69.3 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:240 (240.0 b) TX bytes:240 (240.0 b)
路由表資訊如下:
2.3 ping包測試通訊鍊路:
這裡有一點需要注意:Linux系統可能開了iptables過濾功能,是以在ping時出現了類似" ICMP host 192.168.1.13 unreachable - admin prohibited, length 116"資訊,詳情如下:
百度一下,在兩台虛拟機上都執行如下操作即可:
iptables -F
然後在ping包測試,資料可通:
至此,基本GRE隧道環境搭建成功。
目的: 通過GRE隧道将20.1.2.1/24、20.1.3.2/24兩個子網連接配接起來進行通訊。
ifconfig eth0:1 192.168.100.1/24
檢視接口配置如下:
ip tunnel add Tunnel-2 mode gre local 192.168.100.1 remote 192.168.100.2
ifconfig Tunnel-2 up
ifconfig Tunnel-2 20.1.2.1/24
由于本端沒有對端子網20.1.3.0/24的路由,是以需要添加路由,将該網段封包引入到Tunnel-2接口,這樣便可以通過GRE隧道進行封裝。
route add -net 20.1.3.0/24 gw 20.1.2.1
ifconfig ens33:1 192.168.100.2/24
ip tunnel add Tunnel-2 mode gre remote 192.168.100.1 local 192.168.100.2
結果如下:
ifconfig Tunnel-2 20.1.3.1/24
由于本端沒有對端子網20.1.2.0/24的路由,是以需要添加路由,将該網段封包引入到Tunnel-2接口,這樣便可以通過GRE隧道進行封裝。
route add -net 20.1.2.0/24 gw 20.1.3.1