带有环回 IP、IPFW 和 natd 的 FreeBSD 监狱 - 出站连接从监狱失败

Chr*_*s S 6 freebsd jail ipfw

我有一个 FreeBSD 9.0 服务器。它有几个监狱,但他们都有同样的问题。他们无法发起与外界的连接。他们彼此沟通,主机很好。

相关rc.conf设置:

firewall_enable="YES"                   # IPFW
firewall_type="/etc/ipfw.rules"         # Rule script for IPFW

natd_enable="YES"                       # NAT for Internet Routing
natd_interface="wan0"                   # NAT Card
natd_flags="-f /etc/natd.conf -dynamic" # NAT Conf

ifconfig_lo1_name="jail1"
ifconfig_jail1="inet 192.168.1.101/32"

jail_asdf_rootdir="/jails/asdf"
jail_asdf_hostname="asdf.example.net"
jail_asdf_ip="192.168.1.101"
jail_asdf_devfs_enable="YES"
Run Code Online (Sandbox Code Playgroud)

sysctl.conf

security.jail.allow_raw_sockets=1
Run Code Online (Sandbox Code Playgroud)

ipfw.rules

# XXX 00050 divert natd ip4 from any to any via wan0
add 00060 check-state

# Allow me out
add 00135 allow ip from me to any keep-state
add 00136 allow ip6 from me6 to any keep-state

# HTTP
add 11010 allow tcp from any to me http setup keep-state
add 11011 allow tcp from any to me6 http setup keep-state
add 11012 allow tcp from any to me https setup keep-state
add 11013 allow tcp from any to me6 https setup keep-state
.... lots more rules like the above ....

# General Network - ICMP
add 61001 allow icmp from any to any

# XXX last rule is deny everything
Run Code Online (Sandbox Code Playgroud)

natd.conf

redirect_port tcp 192.168.1.101:80 80
redirect_port tcp 192.168.1.101:443 443
Run Code Online (Sandbox Code Playgroud)

这对于传入连接非常有用。我已经在多台计算机上测试过,我可以正常访问该网站。

当我jexec 1 csh在监狱中获得 shell 时,我无法创建传出连接。Jailresolv.conf指向主机服务器,它可以很好地执行名称解析。由于 ICMP 仍然无一例外地通过,我可以从监狱中 ping 通。

我可以tcpdump -i wan0 host 1.2.3.4在主机上做一个,并在它通过时观察流量。我看到 SYN 出去了,然后一个 SYN ACK 回来了。然后几秒钟后再次与监狱重试相同。

如何允许来自我的监狱的传出连接?

更新
我相信我理解这个问题。传出数据包开始通过防火墙规则,进行 NAT 转换,允许传出并记录为进行传出连接的外部 IP。当返回数据包返回时,它会进行转换,但现在不匹配检查状态规则,因为数据包具有内部 IP。仍在寻找解决方案。

Chr*_*s S 2

从地址转换总是在检查状态规则之前发生的问题来看,解决方案应该是显而易见的。地址转换需要拆分。

上面找到的规则的更正版本是:

add 00050 divert natd ip4 from any to any via wan0 in
add 00060 check-state

# Talking to myself
add 00200 allow ip from me to me keep-state

# HTTP
add 11010 skipto 63000 tcp from any to me http,https setup keep-state
add 11011 skipto 63000 tcp from any to me6 http,https setup keep-state

# General Network - ICMP
add 61001 allow icmp from any to any

# Last rule of "normal" traffic
add 62000 deny ip from any to any

# Only for my outbound and specifically allowed incoming
add 63000 divert natd ip from any to any via wan0 out
add 63001 allow ip from any to any

# XXX last rule is deny everything
Run Code Online (Sandbox Code Playgroud)