Networking

我的 GRE 隧道數據包是如何路由的?

  • February 27, 2014

我試圖弄清楚創建 GRE 隧道時到底發生了什麼。

我的網路看起來像這樣(-> 表示直接連接):

  • 電腦 A (eth0: 10.0.1.1) ->
  • (eth0:10.0.1.2)路由器B(eth1:10.0.2.1)->
  • (eth0:10.0.2.2)路由器C(eth1:10.0.3.1)->
  • (eth0: 10.0.3.2) 路由器 D (eth1: 10.0.4.1) ->
  • (eth-: 10.0.4.2) 電腦 E

我在路由器 B 上執行了以下命令:

ip tunnel add Tunnel5 mode gre local 10.0.2.1 remote 10.0.3.2
ifconfig Tunnel5 192.168.33.2 netmask 255.255.255.0 up
ip route add 10.0.4.2/32 via 192.168.33.3

具有以下連接資訊:

conn routerD_eth0
   type=tunnel
   authby=secret
   left=10.0.2.1
   leftsubnet=10.0.2.1/32
   right=10.0.3.2
   rightsubnet=10.0.3.2/32
   auto=start

和路由器 D 上的等價物:

ip tunnel add Tunnel5 mode gre local 10.0.3.2 remote 10.0.2.1
ifconfig Tunnel5 192.168.33.3 netmask 255.255.255.0 up
ip route add 10.0.1.1/32 via 192.168.33.2

conn routerb_eth1
   type=tunnel
   authby=secret
   left=10.0.3.2
   leftsubnet=10.0.3.2/32
   right=10.0.2.1
   rightsubnet=10.0.2.1/32
   auto=start

如果我從電腦 A ping 到電腦 B,這就是我在路由器 A 上可以觀察到的情況:

  1. 流量進入 eth0,目的地為 10.0.4.2。
  2. 流量被路由到新的 Tunnel5 介面:由我添加的路由規則引起(ip route add 10.0.4.2/32 via 192.168.33.3)
  3. ???魔法 ???不知何故,流量被封裝並路由迴路由器,新的目標地址為 10.0.3.2
  4. 正常的 OSPF 路由規則會導致流量從 eth1 流出並到達目的地。

第 3 步會發生什麼?

附加資訊

一些命令及其輸出都在路由器 A 上執行:

$ ip tunnel show
Tunnel5: gre/ip remote 10.0.3.2 local 10.0.2.1

$ setkey -DP
10.0.3.2[any] 10.0.2.1[any] 255
   ...
   /esp/tunnel/10.0.3.2-10.0.2.1/unique:3
   ...

10.0.2.1[any] 10.0.3.2[any] 255
   ...
   /esp/tunnel/10.0.2.1-10.0.3.2/unique:3
   ...

理論

路由器只是根據“ip tunnel show”中的資訊知道,路由到 Tunnel5 的流量應該用新的源地址和目標地址封裝。

封裝的數據包應該像正常一樣路由。在這種情況下,IPSec 策略匹配並加密數據包,保留源地址和目標地址。

然後根據路由表將數據包路由到路由器 C。

只是一個猜測。

當數據包進入路由器時,由於您的靜態路由,它被路由出隧道介面。路由器將數據包封裝在 GRE 數據包中,目的地為 10.0.3.2。然後路由器根據路由表路由這個數據包(即out eth 1)。當它到達路由器 D 時,數據包被解封裝,然後正常路由。

引用自:https://serverfault.com/questions/578519