V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
wsycqyz
V2EX  ›  Linux

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

  •  
  •   wsycqyz · 2018-03-23 16:21:47 +08:00 · 6400 次点击
    这是一个创建于 2197 天前的主题,其中的信息可能已经有所发展或是发生改变。
    纯技术讨论,可能无实际用处。

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

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

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

    ip route add 0.0.0.0/0 dev eth0 advmss 1200
    wsycqyz
        4
    wsycqyz  
    OP
       2018-03-23 20:39:20 +08:00
    @lcdtyph 请问这个$MY_CHAIN 应该填成什么? OUTPUT? 试了一下好像不行
    wsycqyz
        5
    wsycqyz  
    OP
       2018-03-23 20:42:03 +08:00
    @tomychen 我改成 ip route change 0.0.0.0/dev eth0 advmss 1200,机器直接失去连接,重启才行。
    wsycqyz
        6
    wsycqyz  
    OP
       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
        7
    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
        8
    lcdtyph  
       2018-03-23 21:26:55 +08:00
    @wsycqyz #4 debian 9 亲测有效,请确认 OUTPUT 链中这条规则之前没有规则被成功匹配。
    pheyx
        9
    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
        10
    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
        11
    tomychen  
       2018-03-23 21:51:50 +08:00
    @wsycqyz 抱歉,没测试,刚测一下确实不行
    tomychen
        12
    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
        13
    wsycqyz  
    OP
       2018-03-24 15:01:54 +08:00
    @tomychen 我的 ubuntu16.04 确实不行,不确定问题在哪里

    @pheyx 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3013 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:03 · PVG 23:03 · LAX 08:03 · JFK 11:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.