V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
myliyifei
V2EX  ›  问与答

Iptables 能否限制对于某个指定端口,入站的连接只有一个 IP。

  •  
  •   myliyifei · 2015-01-31 12:21:36 +08:00 · 3356 次点击
    这是一个创建于 3399 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如我对我开一个端口10000,有一个IP连入,但是不限制连接并发数。然后要限制不能再有新的其他源IP连接建立,直到前面的源IP连接都释放掉。

    8 条回复    2015-01-31 22:54:33 +08:00
    extreme
        1
    extreme  
       2015-01-31 14:41:12 +08:00
    之前没事做,看iptables的man page,记得看过。
    这是man page里面的内容:
    connlimit
    Allows you to restrict the number of parallel connections to a server per client IP address (or client address block).

    --connlimit-upto n
    Match if the number of existing connections is below or equal n.

    --connlimit-above n
    Match if the number of existing connections is above n.

    --connlimit-mask prefix_length
    Group hosts using the prefix length. For IPv4, this must be a number between (including) 0 and 32. For IPv6, between 0 and 128. If not specified, the maximum prefix length for the applicable protocol is used.

    --connlimit-saddr
    Apply the limit onto the source group. This is the default if --connlimit-daddr is not specified.

    --connlimit-daddr
    Apply the limit onto the destination group.

    Examples:

    # allow 2 telnet connections per client host
    iptables -A INPUT -p tcp --syn --dport 23 -m connlimit --connlimit-above 2 -j REJECT

    # you can also match the other way around:
    iptables -A INPUT -p tcp --syn --dport 23 -m connlimit --connlimit-upto 2 -j ACCEPT

    # limit the number of parallel HTTP requests to 16 per class C sized source network (24 bit netmask)
    iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 16 --connlimit-mask 24 -j REJECT

    # limit the number of parallel HTTP requests to 16 for the link local network
    (ipv6) ip6tables -p tcp --syn --dport 80 -s fe80::/64 -m connlimit --connlimit-above 16 --connlimit-mask 64 -j REJECT

    # Limit the number of connections to a particular host:
    ip6tables -p tcp --syn --dport 49152:65535 -d 2001:db8::1 -m connlimit --connlimit-above 100 -j REJECT
    extreme
        2
    extreme  
       2015-01-31 14:43:00 +08:00   ❤️ 1
    根据man page的信息,此规则应该可以满足你的需求:
    iptables -I INPUT -p tcp --syn --dport 10000 -m connlimit --connlimit-above 1 --connlimit-mask 0 -j REJECT
    myliyifei
        3
    myliyifei  
    OP
       2015-01-31 15:03:26 +08:00
    @extreme 你的意思是syn发起新的TCP握手只允许一个IP?这倒是个不错的思路,回头试试。
    myliyifei
        4
    myliyifei  
    OP
       2015-01-31 16:49:01 +08:00
    @extreme 这个好像还是不行,只有IP建立了一条连接,其他的新TCP连接就无法建立了,相当于并发连接也为1了。

    我的需要就是限制IP,但是不限制TCP并发连接。
    kxmp
        5
    kxmp  
       2015-01-31 17:25:41 +08:00
    @extreme 这是并发限制.
    Draplater
        6
    Draplater  
       2015-01-31 20:19:59 +08:00
    写一个转发程序,在用户态控制?
    ryd994
        7
    ryd994  
       2015-01-31 22:54:07 +08:00 via Android
    可以这样,
    --connlimit-above 1 --connlimit-mask 32 -j ALLOW
    --connlimit-above 1 --connlimit-mask 0 -j REJECT
    最后再allow一个
    这样的话第一个连接会匹配3,允许
    同IP的其他连接匹配1,允许。
    其他IP的连接会匹配2,拒绝。
    ryd994
        8
    ryd994  
       2015-01-31 22:54:33 +08:00 via Android
    另外建议加上new,这样性能会好点
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1764 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:45 · PVG 00:45 · LAX 09:45 · JFK 12:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.