获取IPTables正确转发NTPstream量

我有以下设置:

NTP 10.21.3.169 | | 10.21.3.160 (eth1) Linux 10.0.0.67 (eth0) | | 10.0.0.65 (pcn1) OpenBSD 

这个想法是允许OpenBSD盒子上的NTPD客户端(不是OpenNTP)通过使用OpenBSD盒子上的端口转发和Linux盒子上的iptables从NTP服务器获得时间。

我处于以下情况的阶段:

  • OpenBSD盒子的pf.conf已经正确设置了,当我运行ntpd -d -s时,在Linux框上运行tcpdump udp显示了来自 OpenBSD盒子的正确stream量
  • Linux的iptablesconfiguration已经正确设置,在OpenBSD上运行ntpd -d -s ,在NTP服务器上使用tcpdump udp显示stream量

但是没有任何东西可以回来 – 在任何一台机器上都没有交通。

我在Linux机器上使用的iptables规则是:

 iptables -A PREROUTING -t nat -i eth0 -p udp --dport 123 -j DNAT --to-destination 10.21.3.169:123 iptables -A FORWARD -t filter -p udp -d 10.21.3.169 --dport 123 -j ACCEPT 

这对我来说似乎是正确的。 在我开始这项工作之前,在IPTablesconfiguration中还有一个“接受状态RELATED / ESTABLISHED”规则。

我究竟做错了什么? 有什么遗漏吗? 也许一些额外的规则的答复?

编辑

我遵循@ gromit的回答和@ MadHatter的意见,并有以下信息添加:

在Linux上,运行cat /proc/sys/net/ipv4/ip_forward会给出1 。 使用tcpdump monitoring eth0和Linux机器上的"eth1 (同时在不同的terminal)在OpenBSD上运行ntpd -d -s ,得到如下输出。

OpenBSD系统

 [root@OpenBSDBox ~]# ntpd -d -s ntp engine ready no reply received in time, skipping initial time setting no reply from 10.0.0.67 received in time, next query 3227s 

Linux Box

tcpdump -n -n -i eth0 port 123

 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes 10:05:26.448943 IP 10.0.0.65.63822 > 10.0.0.67.123: NTPv4, Client, length 48 

tcpdump -n -n -i eth1 port 123

 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on eth1, link-type EN10MB (Ethernet), capture size 96 bytes 10:05:26.449220 IP 10.21.3.160.63822 > 10.21.3.169.123: NTPv4, Client, length 48 10:05:26.449148 IP 10.21.3.169.123 > 10.21.3.160.63822: NTPv4, Server, length 48 

所以看起来路由在Linux上依然不正确 – 答复从NTP盒子回来,但是没有被发送到OpenBSD。

为了清楚起见,我现在添加的iptables路由如下所示:

 iptables -t nat -A PREROUTING -i eth0 -p udp --dport 123 -j DNAT --to-destination 10.21.3.169:123 iptables -t filter -A FORWARD -p udp -d 10.21.3.169 --dport 123 -j ACCEPT iptables -t nat -A POSTROUTING -o eth1 -p udp --dport 123 -j MASQUERADE //for the final line, I changed @gromit's suggestion slightly, as the --from option wasn't recognised iptables -t nat -A POSTROUTING -p udp --sport 123 -j SNAT --to-source 10.21.3.169 

留下最后的iptables线似乎对tcpdump输出没有任何影响。

编辑2

我现在有了下面的IPtables条目,我可以在OpenBSD上更新时钟:

 iptables -t nat -A PREROUTING -i eth0 -p udp --dport 123 -j DNAT --to-destination 10.21.3.169:123 iptables -t nat -A PREROUTING -i eth1 -p udp --sport 123 -j DNAT --to-destination 10.0.0.65:123 iptables -t filter -A FORWARD -p udp -d 10.21.3.169 --dport 123 -j ACCEPT iptables -t filter -A FORWARD -p udp -d 10.0.0.65 --sport 123 -j ACCEPT iptables -t nat -A POSTROUTING -o eth1 -p udp --dport 123 -j MASQUERADE iptables -t nat -A POSTROUTING -p udp --sport 123 -j SNAT --to-source 10.21.3.169 

然而,第二个预路由和第二个filter命令对我来说似乎有点过分,因为据我所知,它们转发来自NTP服务器端口123的所有UDP数据包。 我有一种感觉,这意味着所有进入Linux机器的NTP回复(即包括Linux机器本身请求的时间)将被转发到OpenBSD机器。

这看起来像你有路由问题。

服务时间的系统(3.169)是否知道如何连接到请求系统?

如果不是将以下内容添加到linux盒子的iptables中:

 // this rule, redirects the packets for NTP to the NTP server on the host iptables -t nat -A PREROUTING -i eth0 -p udp --dport 123 -j DNAT --to 10.21.3.169 // this rule make it look for the ntp server as if the linux box is requesting the packets, which // makes routing not necessary and lets the packet flow back to the linux box iptables -t nat -A POSTROUTING -o eth1 -p udp --dport 123 -j MASQUERADE // now the linux box has to check, that the packets coming back are looking like they are from the // main source iptables -t nat -A POSTROUTING -i eth1 -p udp --sport 123 -j SNAT --from $ip_the_client_requested_the_time_from 

现在看起来ntp软件包回来了,时间也同步了。

如果ntp守护进程和redirect将在同一个系统上运行,那将会更容易一些。 然后,你可以使用iptables的“-j REDIRECT”function来完成所有的魔法,但是它只能在本地主机上运行。

KR,

金属扣眼

最快的方法来查找是否是阻止请求的iptables规则:在右边的表上运行“watch iptables -L -n -v”(使用-t nat /无论哪个),然后在客户端重复运行ntpdate命令。 你应该看到iptables计数器在阻塞数据包的行上递增,如果确实是iptables BLOCK问题或类似的话。