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)