IPTABLES规则配置不当导致的路径MTU无法更新

发布于:2023-01-20 ⋅ 阅读:(435) ⋅ 点赞:(0)

问题主机Linux系统的iptables配置如下,旨在仅开放http和https的80和443端口,拒绝所有其他的连接。第三条规则允许状态为建立的数据包进入本机,即本机主动发起的连接,允许回复报文进入。

iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p all -m state --state ESTABLISHED -j ACCEPT
配置后,iptables规则如下:

# iptables -t filter -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
如上的规则配置在实际使用中引起了严重的问题。具体显示为在系统发送大小为网卡MTU值长度(1500)的数据包时,发送失败,显示如下,系统使用源IP地址192.168.1.12发送数据包到目的192.168.20.1,中间经过路由设备192.168.1.203。由于中间路由设备的MTU值小于1500,并且发送的数据包设置了DF不允许分片标志,路由设备回复了ICMP不可达消息,通告其MTU为1480,问题出在,接收到此ICMP报文之后,系统并未改变其PMTU,仍然使用1500作为分片大小发送数据包,导致发送失败。

Out e4:3a:6e:0a:51:31 ethertype IPv4 (0x0800), length 2852: (tos 0x0, ttl 64, id 27800, offset 0, flags [DF], proto TCP (6), length 2836)
    192.168.1.12.40066 > 192.168.20.1.443: Flags [.], cksum 0x9857 (incorrect -> 0x4a30), seq 6661:9445, ack 1009, win 242, options [nop,nop,TS val 306534 ecr 188847049], length 2784
In 00:90:27:fe:d0:01 ethertype IPv4 (0x0800), length 592: (tos 0xc0, ttl 64, id 19294, offset 0, flags [none], proto ICMP (1), length 576)
    192.168.1.203 > 192.168.1.12: ICMP 192.168.10.128 unreachable - need to frag (mtu 1480), length 556
        (tos 0x0, ttl 64, id 27791, offset 0, flags [DF], proto TCP (6), length 1500)
    192.168.1.12.40066 > 192.168.20.1.443: Flags [P.], seq 5213:6661, ack 1009, win 242, options [nop,nop,TS val 306534 ecr 188847049], length 1448
其原因在于,配置的iptables的INPUT策略drop丢弃了此ICMP数据包,为进入系统处理。增加如下命令,对于相关related状态的数据包允许其进入本地系统,问题得到解决。

# iptables -A INPUT -p all -m state --state RELATED -j ACCEPT


之后的报文,长度使用新的MTU:1480在发送了。

Out e4:3a:6e:0a:51:31 ethertype IPv4 (0x0800), length 1496: (tos 0x0, ttl 64, id 30401, offset 0, flags [DF], proto TCP (6), length 1480)
    192.168.1.12.40154 > 192.168.20.1.443: Flags [.], cksum 0x930b (incorrect -> 0x793d), seq 869:2297, ack 1009, win 242, options [nop,nop,TS val 754527 ecr 189026230], length 1428
Out e4:3a:6e:0a:51:31 ethertype IPv4 (0x0800), length 1496: (tos 0x0, ttl 64, id 30402, offset 0, flags [DF], proto TCP (6), length 1480)
    192.168.1.12.40154 > 192.168.20.1.443: Flags [.], cksum 0x930b (incorrect -> 0x0812), seq 2297:3725, ack 1009, win 242, options [nop,nop,TS val 754527 ecr 189026230], length 1428
另外,关闭系统的PMTU发现功能,系统发包时将不再设置DF标志,中间设备也可正常处理。

# echo 1 > /proc/sys/net/ipv4/ip_no_pmtu_disc
 

本文含有隐藏内容,请 开通VIP 后查看