AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 473269
Accepted
Felipe Alcacibar
Felipe Alcacibar
Asked: 2013-01-29 17:41:35 +0800 CST2013-01-29 17:41:35 +0800 CST 2013-01-29 17:41:35 +0800 CST

iptables:使用 fwmark 和路由表进行内部端口转发

  • 772

我有 2 台服务器,我需要转发到内部端口。

为了解释,我使用具有 2 个接口(内部/外部)的服务器作为服务器 A,将内部服务器用作服务器 B。

在服务器 A 中我使用

iptables -A PREROUTING -t nat -i $extif -p tcp --dport $extif_port -j DNAT --to-destination $dst_ip:$dst_port"
iptables -A FORWARD -t filter -d $dst_ip -j ACCEPT"
iptables -A POSTROUTING -t nat -p tcp -s $dst_ip --sport $dst_port -j SNAT --to-source $extif_ip"

在服务器 BI 中使用 fwmark 并使用 fwmark 和 iproute2 路由到服务器 A:

ip rule  $command  fwmark 1 lookup 1
ip route $command  default via $rt_ip table 1

--

iptables -A PREROUTING -t mangle -p tcp -i $intif ! -s $intif_net --dport $port -j MARK --set-mark 1
iptables -A PREROUTING -t mangle -p tcp -i $intif -s $intif_ip --sport $port -j MARK --set-mark 1
iptables -A POSTROUTING -t mangle -p tcp -o $intif -s $intif_ip --sport $port -j MARK --set-mark 1

在服务器 B 中使用上面的三个规则,连接只停留在 SYN_RECV 状态,并且在日志中没有在连接中发送 ACK,在某些部分内部数据包丢失,但我不知道为什么。

欢迎任何帮助,祝你有美好的一天。

iptables
  • 2 2 个回答
  • 8380 Views

2 个回答

  • Voted
  1. Best Answer
    Felipe Alcacibar
    2013-02-08T12:12:02+08:002013-02-08T12:12:02+08:00

    我在 B 服务器中使用 CONNMARK 解决了这个问题。

    使用伪装它工作得很快,但因为是一个网络服务器,我需要原始 IP 地址和伪装将源 IP 更改为我的 A 服务器的 $intif_ip,所以我不能使用伪装​​。

    因为我没有使用 iptables 做防火墙,所以我放弃了 FOWRARD 链规则,所以我在 A 服务器中只有 DNAT 和 SNAT 规则。

    iptables -A PREROUTING -t nat -i $extif -p tcp --dport $extif_port -d $extif_ip -j DNAT --to-destination $dst_ip:$dst_port"
    iptables -A POSTROUTING -t nat -s $dst_ip -p tcp --sport $dst_port -j SNAT --to-source $extif_ip:$extif_port"
    

    然后在 B 服务器中我遇到了问题,因为它没有路由数据包,我尝试了 1001 种方法来强制使用服务器 A 的 $intif_ip 作为网关,但最终我找到了!

    理论是将 MARK 应用于数据包,并且他的流进入被 fwmark 统治的网关的 ip,但是它在路由决策中失去了 mar,所以我开始使用 CONNMARK 来保存这个 fwmark。

    当使用 CONNMARK 时它无论如何都不起作用,直到 y 记录所有链。自从我读到 CONNMARK 后,这些示例恢复了 mangle 的 POSTROUTING 链中的规则,但遍历的第一个链是 mangle 的输出。因此,我将 CONNMARK 更改为 mangle 的 OUTPUT 链,并且效果很好。

    这是服务器 B 的规则集。

    iptables -A PREROUTING -t mangle ! -s $int_net -p tcp -i $int_if --dport $int_port -j MARK --set-mark 1
    iptables -A PREROUTING -t mangle -m mark --mark 0x1 -j CONNMARK --save-mark
    iptables -A OUTPUT -t mangle -j CONNMARK --restore-mark
    

    和 iproute2 命令(服务器 B)

    ip route flush table 1
    ip rule add fwmark 1 lookup 1
    ip route add default via $intif_ip_serverA table 1
    ip route flush cache
    

    希望这可以帮助您解决这个奇怪的问题。祝你今天过得愉快。

    • 1
  2. Slobodan Logan
    2018-11-07T16:52:02+08:002018-11-07T16:52:02+08:00

    我看到了一些错误使用 CONNMARK 目标的答案。在 mangle 链中使用 MARK 目标,它应该可以工作。

    • 1

相关问题

  • OpenVPN 的 Linux IP 转发 - 正确的防火墙设置?

  • iptables 单个规则中的多个源 IP

  • 存储 iptables 规则的规范方法是什么

  • 使用 iptables 和 dhcpd 进行端口转发

  • 根据 Apache 日志数据自动修改 iptables 以阻止行为不良的客户端

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve