【问题标题】:Allow incoming connection on Port 502 with bidirectional port forwarding通过双向端口转发允许端口 502 上的传入连接
【发布时间】:2017-12-25 10:49:03
【问题描述】:

在嵌入式 Linux 系统上工作(资源有限),我当前的应用程序要求外部设备通过以太网 TCP/IP 在端口 502 上与该板通信。默认情况下,在 *nix 环境下,1024 以下的端口被阻止。

那么,假设外部设备在 502 上打开了一个端口,而嵌入式设备在 8502 上侦听。我怎样才能透明地允许这些设备之间的双向通信?

好消息是授予 ROOT 访问权限以实施此端口转发解决方案,该解决方案将驻留在嵌入式 Linux 设备中持续运行,但尚未找到正确的命令。

我尝试过使用iptables,以及以下命令的不同组合:

iptables -A INPUT -p tcp --dport 502 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 502 -j ACCEPT
iptables -A INPUT -p tcp --dport 8502 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 8502 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 502 -j DNAT --to 0.0.0.0:8502
iptables -A INPUT -p tcp --dport 502 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 502 -m state --state ESTABLISHED -j ACCEPT
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 502 -j DNAT --to 127.0.0.1:8502
iptables -t nat -I OUTPUT -p tcp -o eth0 --dport 8502 -j REDIRECT --to-ports 502

最后,执行iptables-save,以便执行规则。

一些限制:

  • 由于它是受限制的设备,因此无法安装新软件包(其中远没有 apt-get...);
  • 无法预测外部设备的 IP 地址,因为它可以从不同的设备各自建立连接。

对如何继续这项任务有任何想法吗?提前致谢。

编辑:socatnetcap 都不可用...

【问题讨论】:

    标签: linux sockets iptables portforwarding


    【解决方案1】:

    我有一个用于在网关和服务器之间创建重定向到端口 502 的脚本功能:

    iptables -t nat -A POSTROUTING -p tcp -d $ip --dport 502 -j MASQUERADE
    iptables -t nat -A PREROUTING -i br0 -p tcp --dport 502 -j DNAT --to $ip:502
    iptables -t nat -A PREROUTING -d $vpnip -p tcp -m multiport --dports 502 -j DNAT --to-destination $ip:502
    

    它的作品。

    【讨论】:

      【解决方案2】:

      以防其他人偶然发现这个问题:不幸的是,我的 iptables 版本不支持 REDIRECT,这对于使其正常工作至关重要。 所以解决方案是为我的硬件编译另一个应用程序。在这种情况下socat

      使我的应用程序工作的命令是:

      socat TCP-LISTEN:502,fork TCP-CONNECT:127.0.0.1:8502

      希望对以后的需求有所帮助。

      【讨论】:

        猜你喜欢
        • 2016-07-07
        • 2018-07-01
        • 2021-08-24
        • 1970-01-01
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        • 2017-02-10
        • 2012-04-13
        相关资源
        最近更新 更多