大网数据广告活动
服务器运维问答

iptables redirect rule for udp not working

· 52CE 我爱测

I'm trying to create redirection for syslog UDP traffic from incoming IP:PORT to other port on same linux box. Purpose for that is to route traffic comming to different IP addresses on server to services running on higher port nu…

问题原文

原帖链接 · mrbeat · CC BY-SA 3.0

I'm trying to create redirection for syslog UDP traffic from incoming IP:PORT to other port on same linux box. Purpose for that is to route traffic comming to different IP addresses on server to services running on higher port numbers depending on destination address.

OS: RHEL 7.2

Rule:

iptables -A PREROUTING -t nat -p udp --dport 514 -d A.D.D.R -j REDIRECT --to-port 1514

for debugging i tested also:

iptables -A PREROUTING -t nat -p udp --dport 514 -d A.D.D.R -j LOG --log-prefix "SYSLOG_REDIRECT"

iptables -t nat -L -v -n 

shows 0 for counter, no logs recorded

tcpdump shows traffic incoming on ingress port 514

destination IP address (A.D.D.R) is assigned to subinterface/alias interface enoXXXX:1

Does someone had similar case?

已采纳解答

原帖链接 · Khaled · CC BY-SA 3.0

Your NAT rule seems to be correct and should do what you expected. However, there is one point to verify that I believe it is the reason why your UDP traffic is not redirected.

Redirecting UDP traffic can be more trickier than redirecting TCP. This is mainly because TCP is a connection-oriented protocol and UDP is connection-less.

NAT rules rely on connection tracking module which keeps track of the state of any "connection". In TCP case, it is clear what it means as there is special TCP packets to indicate new connections. In UDP, there is no connection establishment phase. The first packet your box will see it is considered as initiating a "connection" and the reply packet indicates that the "connection is established".

How about closing the connection? In TCP, it is also clear because TCP uses special flags to indication connection close. When seen, you box can make use of them and clear the connection state accordingly. However, you will never know when UDP "connection" is terminated. So, the connection track entry will not be cleared unless the timeout is reached (on my machine it is set to 180 seconds) without any UDP packet.

How this is related to your issue? When your box is continuously receiving UDP traffic for syslog, the UDP "connection" timeout will not expire and the connection state will be kept all the time. NAT rule will not be applied as long as there is a state in connection tracking table.

How to force new NAT rule? You can try to remove the connection tracking entry manually using the below command (don't use it without any other parameters as it will empty the whole table interrupting all your current connections). For more parameters, you can check the manual.

$ sudo conntrack -D

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-09-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。