http://www.ttlsa.com/linux/create-a-gre-tunnel-linux/
其他國家的互聯網如同一個孤島。要想訪問國外網站異常的緩慢,甚至被和諧了。可以建立一條隧道來避免這種情況,下面說說GRE隧道如何建立。
1. GRE介紹
GRE隧道是一種IP-over-IP的隧道,是通用路由封裝協議,可以對某些網路層協議的數據報進行封裝,使這些被封裝的數據報能夠在IPv4/IPv6 網絡中傳輸。
Tunnel 是一個虛擬的點對點的連接,提供了一條通路使封裝的數據報文能夠在這個通路上傳輸,並且在一個Tunnel 的兩端分別對數據報進行封裝及解封裝。 一個X協議的報文要想穿越IP網絡在Tunnel中傳輸,必須要經過加封裝與解封裝兩個過程。
要在Linux上創建GRE隧道,需要ip_gre內核模塊,它是GRE通過IPv4隧道的驅動程序。
2. 查看是否有加載ip_gre模塊
1 |
# modprobe ip_gre |
2 |
# lsmod | grep gre |
3 |
ip_gre 22432 0 |
4 |
gre 12989 1 ip_gre |
3. 創建步驟
環境如下:
host A : 121.207.22.123
host B: 111.2.33.28
在host A上面:
1 |
# ip tunnel add gre1 mode gre remote 111.2 . 33.28 local 121.207 . 22.123 ttl 255 |
2 |
# ip link set gre1 up |
3 |
# ip addr add 10.10 . 10.1 peer 10.10 . 10.2 dev gre1 |
創建一個GRE類型隧道設備gre0, 並設置對端IP為111.2.33.28。隧道數據包將被從121.207.22.123也就是本地IP地址發起,其TTL字段被設置為255。隧道設備分配的IP地址為10.10.10.1,掩碼為255.255.255.0。
在host B上面:
1 |
# ip tunnel add gre1 mode gre remote 121.207.22.123 local 111.2.33.28 ttl 255 |
2 |
# ip link set gre1 up |
3 |
# ip addr add 10.10.10.2 peer 10.10.10.1 dev gre1 |
此時,host A 和 host B 建立起GRE隧道了。
4. 檢測連通性
1 |
# ping 10.10.10.2 (host A) |
2 |
PING 10.10.10.2 (10.10.10.2) 56(84) bytes of data. |
3 |
64 bytes from 10.10.10.2: icmp_req=1 ttl=64 time =0.319 ms |
4 |
64 bytes from 10.10.10.2: icmp_req=2 ttl=64 time =0.296 ms |
5 |
64 bytes from 10.10.10.2: icmp_req=3 ttl=64 time =0.287 ms |
5. 撤銷GRE隧道
在任一一端操作下面命令
1 |
# ip link set gre1 down |
2 |
# ip tunnel del gre1 |
轉載請注明來自運維生存時間: http://www.ttlsa.com/html/4138.html
http://supercisco.blog.51cto.com/672109/293313/
R1(config-line)#int s1/1
R1(config-if)#no sh
R1(config-if)#ip add 202.101.172.37 255.255.255.252
R1(config-if)#int lo0
R1(config-if)#ip add 172.16.1.1 255.255.255.0
R1(config-if)#ip add 172.16.2.1 255.255.255.0 se
R1(config-if)#exit
R1(config)#ip route 0.0.0.0 0.0.0.0 202.101.172.38 (配置一條默認路由)
R3(config-line)#int s1/0
R3(config-if)#no sh
R3(config-if)#ip add 218.108.248.202 255.255.255.252
R3(config-if)#int lo0
R3(config-if)#ip add 192.168.1.1 255.255.255.0
R3(config-if)#ip add 192.168.2.1 255.255.255.0 se
R3(config-if)#exit
R3(config)#ip route 0.0.0.0 0.0.0.0 218.108.248.201
|
R1(config)#do ping 218.108.248.202
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 218.108.248.202, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 12/27/64 ms
|
R1(config)#do ping 192.168.1.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.1, timeout is 2 seconds:
U.U.U
Success rate is 0 percent (0/5)
R1(config)#
|
R1(config)#interface tunnel 0 (啟用GRE隧道)
R1(config-if)#ip add 10.1.1.1 255.255.255.0 (為隧道配置IP地址)
R1(config-if)#tunnel source serial1/1(配置隧道的本地源端口)
R1(config-if)#tunnel destination 218.108.248.202 (配置隧道的目標出口,目的IP的可達性,是通過之前配置的本地默認路由保證的)
R3(config)#int tunnel 0
R3(config-if)#ip add 10.1.1.2 255.255.255.0
R3(config-if)#tunnel source 218.108.248.202 (也可以指定隧道本地源IP)
R3(config-if)#tunnel destination 202.101.172.37
|
R3(config-if)#
*Mar 1 01:06:52.095: %LINEPROTO-5-UPDOWN: Line protocol on Interface Tunnel0, changed state to up
|
R1#sh interfaces tunnel0
Tunnel0 is up, line protocol is up (狀態為UP)
Hardware is Tunnel (基於隧道的接口)
Internet address is 10.1.1.1/24
MTU 1514 bytes, BW 9 Kbit, DLY 500000 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation TUNNEL, loopback not set (在原始的數據包上再封裝了一個GRE報頭)
Keepalive not set
Tunnel source 202.101.172.37 (Serial1/1), destination 218.108.248.202
Tunnel protocol/transport GRE/IP (隧道協議為GRE)
Key disabled, sequencing disabled
Checksumming of packets disabled
Tunnel TTL 255
………………
|
R1#sh ip route
…………
Gateway of last resort is 202.101.172.38 to network 0.0.0.0
202.101.172.0/30 is subnetted, 1 subnets
C 202.101.172.36 is directly connected, Serial1/1
172.16.0.0/24 is subnetted, 2 subnets
C 172.16.1.0 is directly connected, Loopback0
C 172.16.2.0 is directly connected, Loopback0
10.0.0.0/24 is subnetted, 1 subnets
C 10.1.1.0 is directly connected, Tunnel0 (顯示隧道接口為直連路由)
S* 0.0.0.0/0 [1/0] via 202.101.172.38 (之前配置的默認路由)
|
R1#ping 10.1.1.2
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.1.1.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 12/34/68 ms
R1#
|
R1(config)#ip route 192.168.0.0 255.255.0.0 tunnel 0 (下一跳可以指定隧道接口)
R3(config)#ip route 172.16.0.0 255.255.0.0 10.1.1.1 (也可以指定隧道接口對端的IP)
|
R1(config)#do ping 192.168.1.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.1.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 8/38/108 ms
R1(config)#do ping 192.168.2.1
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 192.168.2.1, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 8/36/84 ms
R1(config)#
|
R1#sh interface tunnel 0 stats
Tunnel0
Switching path Pkts In Chars In Pkts Out Chars Out
Processor 30 3720 30 3720
Route cache 0 0 0 0
Total 30 3720 30 3720
|
本文出自 “超越技術@成就價值” 博客,請務必保留此出處http://supercisco.blog.51cto.com/672109/293313
http://www.bacysoft.cn/ebook-arv180-a-017-1.html
Q:在路由器上配置GRE隧道時tunnel接口上要配置三個IP地址:ip、source ip、destination ip,它們各起什么的作用?
A:GRE是一種VPN隧道技術,其原理為本端路由器將3層報文封裝到IP報文里,通過IP網絡(例如Internet)送到對端路由器后再解開還原。可以把tunnel想象成一條DDN專線,tunnel口上配置的ip地址就相當於連接DDN專線的串口的IP地址。這個地址一般是內部的IP,Internet上是不認的。而tunnel source和tunnel destination地址是Internet上可以路由的IP地址,用於建立tunnel。
例如,本端路由器地址規划為:
eth0:10.1.1.1/24(連接內部局域網)
tunnel0:10.2.1.1/30(tunnel source:202.38.160.1;tunnel destination:192.15.135.80)
serial0:202.38.160.1/24(連接Internet)
ip route 10.3.1.0 255.255.255.0 10.2.1.2(到對端以太網的路由)
對端路由器地址規划為:
eth0:10.3.1.1/24(連接內部局域網)
tunnel0:10.2.1.2/30(tunnel source:192.15.135.80;tunnel destination:202.38.160.1)
serial0:192.15.135.80/24(連接Internet)
ip route 10.1.1.0 255.255.255.0 10.2.1.1(到對端以太網的路由)
http://biancheng.dnbcw.info/linux/308588.html
1 GRE隧道的開啟方法
1.首先要先把內核模塊ip_gre.o加載上,以前的版本的kernal中的GRE模塊都是ip_gre.o,而RHEL5中叫ip_gre.ko
modprobe -l |grep ip_gre.ko
上面這個命令是看看這個模塊
insmode /lib/modules/2.4.20-8/kernel/net/ipv4/ip_gre.ko
上面這個命令是加載ip_gre.ko模塊
而且注意每次重起或者注銷后都要再加載一遍.
2.啟動GRE隧道
ip tunnel add 新的tunnel的名稱 mode gre remote 對端物理IP local 本端物理IP ttl 255
ip link set 新的tunnel的名稱 up
ip addr add 新的tunnel的名稱的IP dev 新的tunnel的名稱
ip route add 對端的內網私有網段/掩碼 dev 新的tunnel的名稱
二、實例
讓我們先來做一做IPv4隧道:
比如說你有3個網絡:內部網A和B,中間網C(比如說:Internet).A網絡的情況:
網絡地址
10.0.1.0
子網掩碼
255.255.255.0
路由器
10.0.1.1
路由器在C網絡上的地址是172.16.17.18.我們稱之為neta.
B網絡的情況:
網絡地址
10.0.2.0
子網掩碼
255.255.255.0
路由器
10.0.2.1
路由器在C網絡上的IP地址是 172.19.20.21.我們稱之為netb.
已知C網絡已經連通,我們假定它會將所有的數據包從A傳到B,反之亦然.
至於原因,我們不考慮.
在A網絡的路由器上,輸入:
ip tunnel add netb mode gre remote 172.19.20.21 local 172.16.17.18 ttl 255
ip link set netb up
ip addr add 10.0.1.1 dev netb
ip route add 10.0.2.0/24 dev netb
讓我們稍微討論一下.第1行,我們添加了一個隧道設備,並且稱之為netb(為了能夠表示出這個隧道通向哪里).並且表示要使用GRE協議 (mode gre),對端地址是172.19.20.21(另一端的路由器),我們的隧道數據包發源於172.16.17.18(以便當你的路由器在C網絡中擁有多個地址的時候,你可以指定哪一個應用於隧道)並且包的TTL字段應設置為255(ttl 255).
第2行,啟用該隧道.
第3行,我們給這個新生的網卡配置了一個IP:10.0.1.1.對於小網絡來說足夠了,但如果你網絡中的隧道多得象無證運營的小煤窯一樣,你可能就要考慮給你的隧道規划一個單獨的IP地址范圍(在本例中,你可以使用10.0.3.0).
第4行,我們為B網絡設置了一條路由.注意子網掩碼的另一種表示方法.如果你不熟悉這種表示,我就來解釋一下:你把你的子網掩碼寫成二進制形式,數數里面由多少個1.如果你連這個也不會做,不妨就簡單地記住:255.0.0.0 就是/8,255.255.0.0 就是 /16, 255.255.255.0 就是 /24.
讓我們再看看B網絡的路由器.
ip tunnel add neta mode gre remote 172.16.17.18 local 172.19.20.21 ttl 255
ip link set neta up
ip addr add 10.0.2.1 dev neta
ip route add 10.0.1.0/24 dev neta
如果你想從A路由器中停止隧道,輸入:
ip link set netb down
ip tunnel del netb
當然,你可以把netb換成neta,在B路由器上操作
例:
rc151.local
iptables -t nat -A POSTROUTING -s 192.168.254.0/24 -d ! 192.168.0.0/16 -o eth1 -j MASQUERADE
start151_gre.sh
#!/bin/bash
modprobe ip_gre
ip tunnel add netHY mode gre remote 60.28.33.42 local 60.28.39.66 ttl 255
ip link set netHY up
ip addr add 192.168.0.2 dev netHY
ip addr add 192.168.1.1 dev netHY
ip route add 192.168.254.0/24 dev netHY
stop151_gre.sh
#!/bin/bash
ip link set netHY down
ip tunnel del netHY
start42_gre.sh
#!/bin/bash
modprobe ip_gre
ip tunnel add netTY mode gre remote 60.28.39.66 local 60.28.33.42 ttl 255
ip link set netTY up
ip addr add 192.168.254.1 dev netTY
ip route add 192.168.1.0/24 dev netTY
ip route add 192.168.0.0/24 dev netTY
stop42_gre.sh
#!/bin/bash
ip link set netTY down
http://www.net527.cn/a/luyoujiaohuan/Ciscojishu/2009/1119/304.html
一種技術(協議)或者策略的兩個或多個子網穿過另一種技術(協議)或者策略的網絡實現互聯,稱之為overlay topology,這一技術是電信技術的永恆主題之一。
電信技術在發展,多種網絡技術並存,一種技術的網絡孤島可能需要穿過另一種技術的網絡實現互聯,這種情況如果發生在高層協議的PDU封裝於低層協議PDU 中時通常稱之為復用,特別地三層PDU穿過二層網絡地技術稱為租用鏈路或虛電路;而如果穿越發生在一種協議PDU封裝在同一層協議的PDU中,或者封裝在 高層協議的PDU中時,人們通常稱之為隧道。
隧道提供了一種某一特定網絡技術的PDU穿過不具備該技術轉發能力的網絡的手段,如組播數據包穿過不支持組播的網絡;另一種情況是有時因為管理策略的原 因,一個管理者(策略)的子網不能通過和另一個管理者(策略)的網絡互聯而連接,而是要穿過另一個管理者(策略)的網絡實現連接,這就是所謂的 VPN(Virtual Private Networks),不管是L2 VPN還是L3 VPN都需要利用隧道技術實現。因此隧道某種意義上可以概括為穿越不同的網絡的技術,不同既可以是技術方面的,也可以是管理策略方面的。
隧道可以作為一個虛擬接口來實現。隧道接口並不指定特定的“乘客”或“傳輸”協議連接,而是一種結構,可以實現任何標准點到點封裝的服務。由於隧道是點到點連接,因此對每個連接必須配置一個單獨的隧道。
GRE是一種應用較為廣泛的一種網絡層協議PDU封裝於任一種網絡層協議PDU中的技術,經常被用來構造GRE隧道穿越各種三層網絡,並得到了大多數電信設備廠商的支持。
GRE(通用路由協議封裝)是由Cisco和Net-smiths等公司於1994年提交給IETF的,標號為RFC1701和RFC1702。目前有多 數廠商的網絡設備均支持GRE隧道協議。GRE 規定了如何用一種網絡協議去封裝另一種網絡協議的方法。GRE的隧道由兩端的源IP地址和目的IP地址來定義,允許用戶使用IP包封裝IP、IPX、 AppleTalk包,並支持全部的路由協議(如RIP2、OSPF等)。通過GRE,用戶可以利用公共IP網絡連接IPX網絡、AppleTalk網 絡,還可以使用保留地址進行網絡互連,或者對公網隱藏企業網的IP地址。
封裝后一個GRE數據包的格式如下:
a --------------------------------- | | | Delivery Header | | | --------------------------------- | | | GRE Header | | | --------------------------------- | | | Payload packet | | | --------------------------------- The GRE packet header has the form: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |C|R|K|S|s|Recur| Flags | Ver | Protocol Type | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Checksum (optional) | Offset (optional) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Key (optional) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Sequence Number (optional) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Routing (optional) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
----GRE在包頭中包含了協議類型,這用於標明乘客協議的類型;校驗和包括了GRE的包頭和完整的乘客協議與數據;密鑰用於接收端驗證接收的數據;序列號用於接收端數據包的排序和差錯控制;路由用於本數據包的路由。
----GRE只提供了數據包的封裝,它並沒有加密功能來防止網絡偵聽和攻擊。所以在實際環境中它常和IPsec在一起使用,由IPsec提供用戶數據的加密,從而給用戶提供更好的安全性。
GRE協議的主要用途有兩個:企業內部協議封裝和私有地址封裝。在國內,由於企業網幾乎全部采用的是TCP/IP協議,因此在中國建立隧道時沒有對企業內 部協議封裝的市場需求。企業使用GRE的唯一理由應該是對內部地址的封裝。當運營商向多個用戶提供這種方式的VPN業務時會存在地址沖突的可能性。
路由封裝(GRE)最早是由Cisco提出的,而目前它已經成為了一種標准,被定義在RFC 1701, RFC 1702, 以及RFC 2784中。簡單來說,GRE就是一種隧道協議,用來從一個網絡向另一個網絡傳輸數據包。
如果你覺得它和虛擬專用網(VPN)有些類似,那只是因為:從技術上講,GRE隧道是某一類型的VPN,但是並不是一個安全隧道方式。不過你也可以使用某種加密協議對GRE隧道進行加密,比如VPN網絡中常用的IPSec協議。
實際上,點到點隧道協議(PPTP)就是使用了GRE來創建VPN隧道。比如,如果你要創建Microsoft VPN隧道,默認情況下會使用PPTP,這時就會用到GRE。
為什么要用GRE?
為什么要使用GRE進行隧道傳輸呢?原因如下:
有時你需要加密的多播傳輸。GRE隧道可以像真實的網絡接口那樣傳遞多播數據包,而單獨使用IPSec,則無法對多播傳輸進行加密。多播傳輸的例子包括 OSPF, EIGRP, 以及RIPV2。另外,大量的視頻、VoIP以及音樂流程序使用多播。
你所采用的某種協議無法進行路由,比如NetBIOS或在IP網絡上進行非IP傳輸。比如,你可以在IP網絡中使用GRE支持IPX或AppleTalk協議。
你需要用一個IP地址不同的網絡將另外兩個類似的網絡連接起來。
如何配置GRE隧道?
在Cisco路由器上配置GRE隧道是一個簡單的工作,只需要輸入幾行命令即可實現。以下是一個簡單的例子。
路由器A:
interface Ethernet0/1
ip address 10.2.2.1 255.255.255.0
interface Serial0/0
ip address 192.168.4.1 255.255.255.0
interface Tunnel0
ip address 1.1.1.2 255.255.255.0 //這個地址是否可以不配置?
tunnel source Serial0/0
tunnel destination 192.168.4.2
路由器B:
interface FastEthernet0/1
ip address 10.1.1.1 255.255.255.0
interface Serial0/0
ip address 192.168.4.2 255.255.255.0
interface Tunnel0
ip address 1.1.1.1 255.255.255.0
tunnel source Serial0/0
tunnel destination 192.168.4.1
在這個例子中,兩個路由器均擁有虛擬接口,即隧道接口。這一接口屬於各自的網絡,就好像一個點到點的T1環路。跨越隧道網絡的數據采用串行網絡方式傳輸。
對於每個路由器都有兩種途徑將數據傳遞到另一端,即通過串行接口以及通過隧道接口(通過隧道傳遞數據)。該隧道可以傳輸非路由協議的數據,如NetBIOS或AppleTalk。如果數據需要通過互聯網,你可以使用IPSec對其進行加密。
從下面的信息反饋可以看出,路由器B上的隧道接口和其他網絡接口沒有什么不同:
RouterB# sh ip int brie
Interface IP-Address OK? Method Status Protocol
Ethernet0 10.1.1.1 YES manual up down
Serial0 192.168.4.2 YES manual up up
Serial1 unassigned YES unset administratively down down
Tunnel0 1.1.1.1 YES manual up up
RouterB#
[Tunnel就是一個接口,如果使用還需另外的策略。]
解決GRE隧道的問題
由於GRE是將一個數據包封裝到另一個數據包中,因此你可能會遇到GRE的數據報大於網絡接口所設定的數據包最大尺寸的情況。解決這種問題的方法是在隧道 接口上配置ip tcp adjust-mss 1436。另外,雖然GRE並不支持加密,但是你可以通過tunnel key命令在隧道的兩頭各設置一個密鑰。這個密鑰其實就是一個明文的密碼。由於GRE隧道沒有狀態控制,可能隧道的一端已經關閉,而另一端仍然開啟。這一 問題的解決方案就是在隧道兩端開啟keepalive數據包。它可以讓隧道一端定時向另一端發送keepalive數據,確認端口保持開啟狀態。如果隧道 的某一端沒有按時收到keepalive數據,那么這一側的隧道端口 也會關閉。
-------------------------------------------
in RFC1701:
Forwarding of GRE packets
Normally, a system which is forwarding delivery layer packets will not differentiate GRE packets from other packets in any way. However, a GRE packet may be received by a system. In this case, the system should use some delivery-specific means to determine that this is a GRE packet. Once this is determined, the Key, Sequence Number and Checksum fields if they contain valid information as indicated by the corresponding flags may be checked. If the Routing Present bit is set to 1, then the Address Family field should be checked to determine the semantics and use of the SRE Length, SRE Offset and Routing Information fields. The exact semantics for processing a SRE for each Address Family is defined in other documents.
Once all SREs have been processed, then the source route is complete, the GRE header should be removed, the payload's TTL MUST be decremented (if one exists) and the payload packet should be forwarded as a normal packet. The exact forwarding method depends on the Protocol Type field.
(責任編輯:admin)