iptables 新手问一个本地发出流量修改 mss 的问题

2018-03-23 16:21:47 +08:00
 wsycqyz
纯技术讨论,可能无实际用处。

一台 Linux 机器,wget 时,在目的机器上抓包,显示 SYN 的 tcp mss 为 1460,这是正常的行为。

Q: 我只有权限用这台 Linux 机器,在这台 Linux 机器上,我想把从这台 Linux 本地发出的 SYN 包的 tcp mss 改为 1200,可以吗?

搜索过,Linux 做为一个路由器,可以用 iptables 修改流过它的流量的 tcp mss,但是未搜索到如何修改本机发出的流量的 tcp mss。请指各位指教。
6441 次点击
所在节点    Linux
13 条回复
gino86
2018-03-23 16:52:34 +08:00
查一下 iptables-extensions 的手册页,我记得里面好像有关于这方面的资料的,进去之后搜索 mss
lcdtyph
2018-03-23 17:05:11 +08:00
iptables -A $MY_CHAIN -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1400
tomychen
2018-03-23 17:22:26 +08:00
确定要动 mss 不是 mtu?

ip route add 0.0.0.0/0 dev eth0 advmss 1200
wsycqyz
2018-03-23 20:39:20 +08:00
@lcdtyph 请问这个$MY_CHAIN 应该填成什么? OUTPUT? 试了一下好像不行
wsycqyz
2018-03-23 20:42:03 +08:00
@tomychen 我改成 ip route change 0.0.0.0/dev eth0 advmss 1200,机器直接失去连接,重启才行。
wsycqyz
2018-03-23 20:46:54 +08:00
@tomychen ip route change 0.0.0.0/ via xxx.xxx.xxx.xxx advmss 1200 无用,
wget 测试后,web 服务器端收到的 SYN 包 mss 还是 1460
goofool
2018-03-23 21:12:30 +08:00
-I OUTPUT -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1200
lcdtyph
2018-03-23 21:26:55 +08:00
@wsycqyz #4 debian 9 亲测有效,请确认 OUTPUT 链中这条规则之前没有规则被成功匹配。
pheyx
2018-03-23 21:36:31 +08:00
@wsycqyz

看看这样行不行:

iptables -t mangle -I POSTROUTING -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 1200

放在 mangle 表中的 OUTPUT 链也是可以的,但 POSTROUTING 是包最后经过的一个链,所以会覆盖 OUTPUT 里的设置。
另外 OUTPUT 里的设置只针对本机发出的包,而 POSTROUTING 对本机发出的包和经过本机的包都有效。

建议看看这个图:
https://upload.wikimedia.org/wikipedia/commons/3/37/Netfilter-packet-flow.svg
extreme
2018-03-23 21:46:05 +08:00
man iptables:

TCPMSS

This target allows to alter the MSS value of TCP SYN packets, to control the maximum size for that connection (usually limiting it to your outgoing interface's MTU minus 40). Of course, it can only be used in conjunction with -p tcp. It is only valid in the mangle table.
This target is used to overcome criminally braindead ISPs or servers which block ICMP Fragmentation Needed packets. The symptoms of this problem are that everything works fine from your Linux firewall/router, but machines behind it can never exchange large packets:
1)
Web browsers connect, then hang with no data received.

2)

Small mail works fine, but large emails hang.

3)

ssh works fine, but scp hangs after initial handshaking.

Workaround: activate this option and add a rule to your firewall configuration like:
iptables -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN \
-j TCPMSS --clamp-mss-to-pmtu
--set-mss value
Explicitly set MSS option to specified value.
--clamp-mss-to-pmtu
Automatically clamp MSS value to (path_MTU - 40).
These options are mutually exclusive.
TOS

This is used to set the 8-bit Type of Service field in the IP header. It is only valid in the mangle table.
--set-tos tos
You can use a numeric TOS values, or use
iptables -j TOS -h
to see the list of valid TOS names.
tomychen
2018-03-23 21:51:50 +08:00
@wsycqyz 抱歉,没测试,刚测一下确实不行
tomychen
2018-03-23 22:19:57 +08:00
不服气又试了一下...
是可以的
root@ubuntu-virtual-machine:~# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
root@ubuntu-virtual-machine:~# ip route change 0.0.0.0/0 via 192.168.1.1 dev eth0 advmss 1140
root@ubuntu-virtual-machine:~# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.1.1 0.0.0.0 UG 1180 0 0 eth0
192.168.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0

只是在设置为 1140 的时候会 MSS 的值为 1180,但是服务器 tcpdump 得到的 mss 是 1140
wsycqyz
2018-03-24 15:01:54 +08:00
@tomychen 我的 ubuntu16.04 确实不行,不确定问题在哪里

@pheyx 谢谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/440816

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX