当前位置: 首页 > 科技观察

Linux和网络设备GRE配置经验总结

时间:2023-03-13 18:31:52 科技观察

本文转载自微信公众号《新钛云服务》,作者侯明明。转载本文请联系新钛云服务公众号。GRE简介及应用场景GRE(GeneralRoutingEncapsulation),即通用路由封装,是一种三层技术。它最大的作用是封装某些网络层协议的报文,比如封装路由协议、语音、视频等组播报文或IPv6报文。同时也可以与IPSec结合解决GRE的安全问题。本文主要介绍Linux与网络设备(华为防火墙、华为路由器、JuniperSRX防火墙)GRE互联的配置方法。GRE报文如下图所示。GRE是根据TCPIP协议栈逐层封装的。新的IP头会封装在原来的IP头中,然后发送出去。封装操作是通过隧道接口完成的,GRE协议通过隧道接口,接口的封装协议会被设置为GRE协议。GRE配置场景CentOS7.6与华为防火墙建立GRE隧道拓扑图实现目的CentOS与华为防火墙建立GRE隧道;华为防火墙后的内网段192.168.1.0/24从CentOS通过GRE隧道到Internet;CentOS配置端口映射,将192.168.1.10的8080端口映射到CentOS公网地址200.1.1.1的8080端口。配置CentOS配置界面和路由[root@CentOS~]#vim/etc/sysconfig/network-scripts/ifcfg-tun0DEVICE=tun0BOOTPROTO=noneONBOOT=yesDEVICETYPE=tunnelTYPE=GREPEER_INNER_IPADDR=172.16.1.2PEER_OUTER_IPADDR=100.1.1.1MY_INNER_IPADDR.1.10.10.1MY_INNER_IPADDR6=MY_OUTER_IPADDR=200.1.1.1[root@CentOS~]#vim/etc/sysconfig/network-scripts/route-tun0192.168.1.0/24via172.16.1.2[root@CentOS~]#ifuptun0Iptables配置#安装iptables管理service[root@CentOS~]#yuminstalliptables-services#INPUT方向,必须释放对端的公网地址[root@CentOS~]#iptables-IINPUT-s100.1.1.1/32-jACCEPT#Configuresource地址转换[root@CentOS~]#iptables-tnat-APOSTROUTING-s192.168.1.0/24-jSNAT--to-source200.1.1.1#端口映射[root@CentOS~]#iptables-tnat-APOREROUTING-d200.1.1.1-ptcp--dport8080-jDNAT--to-dest192.168.1.10:8080#保存iptables[root@CentOS~]#serviceiptablessave启用ipv4转发[root@CentOS~]#echo"net.ipv4.ip_forward=1">>/etc/sysctl.conf[root@CentOS~]#sysctl-p华为防火墙本次以华为USG6300E系列防火墙为例:配置接口,加入安全区interfaceTunnel0ipaddress172.16.1.2255.255.255.0tunnel-protocolgresource100.1.1.1destination200.1.1.1#将接口加入安全区域[USG6300E]firewallzonetunnelfirewallzonenametunnelsetpriority75addinterfaceTunnel0配置安全策略实际实现中可以收紧策略,根据需要限制源地址和目的地址。如果条件允许,可以先设置默认的安全策略为允许,激活后修改安全策略:routerulenamePBRsource-zonetrustsource-address192.168.1.0mask255.255.255.0actionpbregress-interfaceTunnel0配置No-NAT设置到隧道的流量不使用源地址转换:[USG6300E-policy-nat]disthnat-policyrulenameSNATsource-zonetunneldestination-zoneuntrustsource-address192.168.1.0mask255.255.255.0actionno-nat验证主要包括以下测试方法:在CentOS或防火墙上Ping对端隧道地址;通过的路径,确认是否通过隧道转发。Ubuntu18与华为路由器建立GRE隧道拓扑图实现目标Ubuntu18与华为路由器建立GRE隧道;内部网段192.168。1.10的8080端口映射到CentOS公网地址200.1.1.1的8080端口。配置Ubuntunetplan配置root@ubunt18demo:~#vim/etc/netplan/00-installer-config.yamlnetwork:ethernets:ens3:addresses:-200.1.1.1/24gateway4:200.1.1.254nameservers:addresses:-114.114.114.114tunnels:tun0:mode:grelocal:200.1.1.1remote:100.1.1.1addresses:[172.16.1.1/24]routes:-to:192.168.1.0/24via:172.16.1.2#可以先执行netplantry验证一下,如果是没有坏掉,可以按ENTER确认配置#如果与主机的SSH中断,可以等120S,会自动恢复root@ubunt18demo:~#netplaytryiptables设置Ufw为Ubuntu的防火墙配置工具,以及底层还是调用iptables处理:#Enableufwufwenable#ReleaseSSHufwallowssh#ReleaseGREpair端到端流量ufwallowfrom100.1.1.1/32#Configurenatmappingptcp--dport8080-jDNAT--to-dest192.168.1。10:8080#设置ufw启动systemctlenableufw开启ipv4转发:echo"net.ipv4.ip_forward=1">>/etc/sysctl.confsysctl-p华为路由器搭AR1200以系列路由器为例:配置interfaceinterfaceTunnel0/0/1ipaddress172.16.1.2255.255.255.0tunnel-protocolgresource100.1.1.1destination200.1.1.1配置策略路由#ConfigureACL[AR1200]aclnumber3000[AR1200-acl-adv-3000]rule10permitipdestination192.168.1.00.0.0.255#配置流分类[AR1200]trafficclassifiertogretunnel[AR1200-classifier-togretunnel]if-matchacl3000#配置流量行为[AR1200]trafficbehaviorunbehaviortogretunnel0redirect-AR1200-nexthop172.16.1.1#配置流策略[AR1200]trafficpolicytogretunnel[AR1200-trafficpolicy-vlan10]classifiertogretunnelbehaviortogretunnel#在内网口调用流策略[AR1200]interfacegigabitethernet1/0/1[AR1200-GigabitEthernet3/0/0]policytogretunnelinbound验证验证方式同CentOS、华为防火墙建立GRE隧道JuniperSRX防火墙的GRE配置如果SRX防火墙的出接口使用了route-instances,那么在配置tunnel端口的时候一定要注意添加route-instancedestination,如下图:setinterfacesgr-0/0/0unit0tunnelsource100.1.1.1setinterfacesgr-0/0/0unit0tunneldestination200.1.1.1setinterfacesgr-0/0/0unit0tunnelrouting-instancedestinationEXAMPLE-INSTANCEsetinterfacesgr-0/0/0unit0familyinetaddress172.16.1.2/24另外策略路由在SRX中称为FBF,配置no-nat的示例如下:#配置firewallfilter,匹配需要进入的的的流量流量流量流量流量流量流量流量#配置去GreTunnel的路由setrouting-instancesEXAMPLE-INSTANCEinstance-typeforwardingsetrouting-instancesEXAMPLE-INSTANCErouting-optionsinterface-routesrib-groupinetglobalsetrouting-instancesEXAMPLE-INSTANCErouting-optionsstaticroute0.0.0.0/0next-hop172.16.1.1#在内网口上调用firewallfiltersetfilterinputto-GreTunnel#去往隧道口的流量不做SNATsetsecuritynatsourcerule-setGre-snatfromzoneTrustsetsecuritynatsourcerule-setGre-snattozoneEXAMPLE-INSTANCEsetsecuritynatsourcerule-setGre-snatruleto-cn2-no-natmatchsource-address192.168.1.0/24setsecuritynatsourcerule-setGre-snatruleto-cn2-no-natmatchdestination-address0.0.0.0/0setsecuritynatsourcerule-setGre-snatruleto-cn2-no-natthensource-natoffCentOSpolicyrouting如果有Linux作为中转的场景,即华为防火墙与Linux建立GRE隧道,Linux与其他设备建立连接,流量由Linux中转。这种场景可以在Linux上配置策略路由,如下图:#临时配置,重启后消失,可以作为调试iprleadaddfrom192.168.1.0/24table100pref10iprouteadd0.0.0.0/0via200。1.1.254table100#willconfigurepersistentvim/etc/sysconfig/network-scripts/rule-eth0from192.168.1.0/24table100pref10vim/etc/sysconfig/network-scripts/route-eth0defaultvia172.16.1.1devtun0#verifycommandipruleshowiprouteshowtable100summarizeGRE虽然配置比较简单。Linux在实际项目中经常使用,通常需要由系统工程师进行配置,而网络设备通常由网络工程师进行配置。如果连接出现问题,还需要沟通和协调,这是浪费时间。如果一个工程师能够完成两台设备的配置和故障排除,效率会大大提高。