本文主要学习在ipsec协议在隧道模式下ESP封装格式,通过调整tcp mss以解决ipsec加密后导致大于接口mtu而导致分片的问题。vpp在最新版本中已经支持mss clamp功能。learning:tcp mss clamp。
在《解决 GRE 和 IPsec 中的 IPv4 分段、MTU、MSS和PMTUD 问题》文章中纯ipsec隧道下性能损耗预估:Note:当使用 IPv4sec 进行硬件加密时,确实需要避免在封装后进行分段。硬件加密会导致约50 Mbs 的吞吐量(取决于硬件),但如果对 IPv4sec 数据包进行分段,则会丢失 50% 至90% 的吞吐量。这是因为分段的 IPv4sec 数据包重组时会经过进程交换,然后传送给硬件加密引擎进行解密。吞吐量的丢失会导致硬件加密吞吐量下降至软件加密的性能级别 (2-10Mbs)。 可见分片对性能损耗还是很严重的。
IPSec协议族是IETF(Internet Engineering Task Force)制定的一系列协议,它为IP数据报提供了高质量的、可互操作的、基于密码学的安全性。 IPSec通过认证头AH(Authentication Header,协议号51)和封装安全载荷ESP(Encapsulating Security Payload)这两个安全协议来实现。AH可提供数据源验证和数据完整性校验功能;ESP除可提供数据验证和完整性校验功能外,还提供对IP报文的加密功能。 IPSec协议有两种封装模式: 传输模式。在传输模式下,AH或ESP被插入到IP头之后但在所有传输层协议之前,或所有其他IPSec协议之前。 隧道模式。在隧道模式下,AH或ESP插在原始IP头之前,另外生成一个新IP头放到AH或ESP之前。 传输模式用于两台主机之间的通讯,或者是一台主机和一个安全网关之间的通讯。在传输模式下,对报文进行加密和解密的两台设备本身必须是报文的原始发送者和最终接收者。 通常,在两个安全网关(路由器)之间的数据流量,绝大部分都不是安全网关本身的通讯量,因此在安全网关之间一般不使用传输模式,而总是使用隧道模式。在一个安全网关被加密的报文,只有另一个安全网关能够解密。因此必须对IP报文进行隧道封装,即增加一个新的IP头,进行隧道封装后的IP报文被发送到另一个安全网关,才能够被解密。
ESP封装及头部格式如下:
主要字段介绍:
字段 | 长度 | 描述 |
---|---|---|
SecurityParameters Index | 32比特 | 安全参数索引。 |
Sequence Number | 32比特 | 序列号。 |
Payload Data* | 变长 | 有效载荷数据(可变)。 |
Padding | 0–255字节 | 填充字段。 |
Pad Length | 8比特 | 填充字段长度。 |
Next Header | 8比特 | 下一个头。 |
Integrity Check Value-ICV | 变长 | 验证数据。 |
案例来源于文章《解决 GRE 和 IPsec 中的 IPv4 分段、MTU、MSS和PMTUD 问题》。 1、IP未设置DF位流程
1.路由器收到发往主机 2 的 1500 字节的数据包(20 字节 IPv4 报头 + 1480 字节 TCP 负载)。 2.1500 字节的数据包经过 IPv4sec 加密,增加了 52 字节的开销(IPv4sec 报头、报尾和另外的IPv4 报头)。现在 IPv4sec 需要发送 1552 字节的数据包。由于出站 MTU 为 1500,因此必须对此数据包进行分段。 3.IPv4sec 数据包被拆分为两个分段。在分段过程中,会给第二个分段另外添加 20 字节的 IPv4报头,从而产生一个 1500 字节的分段和一个 72 字节的 IPv4 分段。 4.IPv4sec 隧道对等路由器接收分段,剥离附加的 IPv4 报头,并将 IPv4 分段合并为原始IPv4sec 数据包。然后 IPv4sec 解密该数据包。 5.最后,路由器将 1500 字节的原始数据包转发到主机 2。
2、IP设置DF位流程 在本场景中可以看到,IPv4sec PMTU 由于分段需要而改为更低的值。请注意,当 IPv4sec 加密数据包时,会将 DF 位从内部 IPv4 报头复制到外部 IPv4 报头。介质 MTU 和 PMTU 值存储在IPv4sec 安全关联 (SA) 中。介质 MTU 基于出站路由器接口的 MTU,而 PMTU 基于 IPv4sec 对等体间路径上看到的最小 MTU。请注意,IPv4sec 在试图对数据包分段前,会封装/加密数据包,如下图所示。
注意:vpp在最新的版本中支持pmtu的但是是否能满足上述场景未知。路径:src/vnet/ip/ip_path_mtu.c
本文是使用wmware虚拟机创建vm1和vm2来搭建ipsec的基本测试环境:(所有的连接都使用虚拟机的lan模式)
1、vm1及vpp1配置
#与vpp2直连口接口配置
set interface state GigabitEthernet13/0/0 up
set interface ip address GigabitEthernet13/0/0 192.168.100.1/24
#ipsec profile配置
ikev2 profile add 1
ikev2 profile set 1 auth shared-key-mic string Vpp123
ikev2 profile set 1 id local fqdn vpp1.home
ikev2 profile set 1 id remote fqdn vpp2.home
ikev2 profile set 1 responder GigabitEthernet13/0/0 192.168.100.2
ikev2 profile set 1 ike-crypto-alg aes-cbc 128 ike-integ-alg sha1-96 ike-dh modp-1024
ikev2 profile set 1 esp-crypto-alg aes-cbc 128 esp-integ-alg sha1-96 esp-dh modp-1024
ikev2 profile set 1 traffic-selector local ip-range 192.168.124.0 - 192.168.124.255 port-range 0 - 65535 protocol 0
ikev2 profile set 1 traffic-selector remote ip-range 192.168.125.0 - 192.168.125.255 port-range 0 - 65535 protocol 0
#需要等vpp2侧ipsec接口配置完成后,发起ping主要是为了方式sa-init初始化失败,原因是未学习到对端的mac地址。
ping 192.168.100.2
#ipsec ikev2协商
ikev2 initiate sa-init 1
#配置ipsec接口状态及ip地址。
set interface state ipsec1 up
set interface ip address ipsec1 192.168.200.1/24
#配置与vm1直连口Lan状态及ip地址
set interface state GigabitEthernetb/0/0 up
set interface ip address GigabitEthernetb/0/0 192.168.101.1/24
#设置到对端lan侧的路由
ip route add 192.168.102.0/24 via ipsec1
#配置vm1内核接口
ifconfig ens161 up
ifconfig ens161 192.168.101.2/24
#配置内核路由。
route add -net 192.168.102.0/24 gw 192.168.101.1
#配置ipv6
set interface ip address ipsec1 2000::1/64
ip route add 2002::0/64 via ipsec1
set interface ip address GigabitEthernetb/0/0 2001::1/64
ifconfig ens161 add 2001::2/64
route -6 add 2002::0/64 gw 2001::1 dev ens161
2、vm2及vpp2配置
set interface state GigabitEthernet13/0/0 up
set interface ip address GigabitEthernet13/0/0 192.168.100.2/24
ikev2 profile add 1
ikev2 profile set 1 auth shared-key-mic string Vpp123
ikev2 profile set 1 id local fqdn vpp2.home
ikev2 profile set 1 id remote fqdn vpp1.home
ikev2 profile set 1 ike-crypto-alg aes-cbc 128 ike-integ-alg sha1-96 ike-dh modp-1024
ikev2 profile set 1 esp-crypto-alg aes-cbc 128 esp-integ-alg sha1-96 esp-dh modp-1024
ikev2 profile set 1 traffic-selector remote ip-range 192.168.124.0 - 192.168.124.255 port-range 0 - 65535 protocol 0
ikev2 profile set 1 traffic-selector local ip-range 192.168.125.0 - 192.168.125.255 port-range 0 - 65535 protocol 0
set interface state ipsec0 up
set interface ip address ipsec0 192.168.200.2/24
set interface state GigabitEthernetb/0/0 up
set interface ip address GigabitEthernetb/0/0 192.168.102.1/24
ip route add 192.168.101.0/24 via ipsec1
ifconfig ens33 up
ifconfig ens33 192.168.102.2/24
route add -net 192.168.101.0/24 gw 192.168.102.1
set interface ip address ipsec1 2000::2/64
ip route add 2001::0/64 via ipsec1
set interface ip address GigabitEthernetb/0/0 2002::1/64
ifconfig ens33 add 2002::2/64
route -6 add 2000::0/64 gw 2002::1 dev ens33
本文验证在vm1虚拟上上安装vsftp服务。在vm2虚拟机上通过sftp vm1 IP地址来建立tcp链接,通过get和put一个大文件,来测试发包包长是否发生改变。 验证过程就不写,验证结果是ok的。
有个问题就是在vm2上验证ipv6报错?:ssh hostname错误。上网搜了一下, sftp root@[2001::2] 需要在ip地址前后加【】。
1、解决 GRE 和 IPsec 中的 IPv4 分段、MTU、MSS和PMTUD 问题 http://www.360doc.com/content/17/0111/22/32940378_621875917.shtml 2、Ipsec VPN + GRE隧道的MTU设置详细解析 https://wenku.baidu.com/view/0361973131126edb6f1a1069.html 3、MTU & MSS 详解记录 https://blog.51cto.com/infotech/123859
本文分享自 DPDK VPP源码分析 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!