重定向特定的 UDP 流量?

重定向特定的 UDP 流量?

我正在尝试将 UDP 流量转发到特定 IP 或从特定 IP 转发到伪接口:

iptables -I FORWARD -s [WAN-IP] -p udp -j ifb0

我也尝试过(没有成功):

iptables -A OUTPUT -d [WAN-IP] -p udp -j ifb0

我正在尝试重定向流量并将延迟应用于流量,我所拥有的:

ip link set dev ifb0 up
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -A FORWARD -p udp -i eth0 -o ifb0 -j ACCEPT
iptables -A FORWARD -p udp -i eth0 -o ifb0 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -o ifb0 -j MASQUERADE
tc qdisc add dev ifb0 root netem delay 100ms

网络信息:

ip route
default via 195.***.***.*** dev pppoe-wan  proto static
192.168.1.0/24 dev br-lan  proto kernel  scope link  src 192.168.1.1
[195.***.***.***] dev pppoe-wan  proto kernel  scope link  src **.***.***.***

route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         195.***.***.*** 0.0.0.0         UG    0      0        0 pppoe-wan
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 br-lan
195.***.***.*** 0.0.0.0         255.255.255.255 UH    0      0        0 pppoe-wan

ifconfig
br-lan    Link encap:Ethernet  HWaddr 30:B5:C2:D7:**:**
          inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fda5:a926:****::1/60 Scope:Global
          inet6 addr: fe80::32b5:c2ff:fed7:****/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3924668 errors:0 dropped:5 overruns:0 frame:0
          TX packets:6856064 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:285489896 (272.2 MiB)  TX bytes:9801376990 (9.1 GiB)

eth0      Link encap:Ethernet  HWaddr 30:B5:C2:D7:**:**
          inet6 addr: fe80::32b5:c2ff:fed7:****/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6817769 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3926298 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1243564002 (1.1 GiB)  TX bytes:353441588 (337.0 MiB)
          Interrupt:4

eth1      Link encap:Ethernet  HWaddr 30:B5:C2:D7:**:**
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3922867 errors:0 dropped:68 overruns:0 frame:0
          TX packets:6805869 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:340296313 (324.5 MiB)  TX bytes:1166540824 (1.0 GiB)
          Interrupt:5

ifb0      Link encap:Ethernet  HWaddr 2E:13:67:**:**:**
          inet6 addr: fe80::2c13:67ff:****:9593/64 Scope:Link
          UP BROADCAST RUNNING NOARP  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:32
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:292 errors:0 dropped:0 overruns:0 frame:0
          TX packets:292 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:21661 (21.1 KiB)  TX bytes:21661 (21.1 KiB)

pppoe-wan Link encap:Point-to-Point Protocol
          inet addr:**.***.***.***  P-t-P:195.***.***.***  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1492  Metric:1
          RX packets:6801543 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3909873 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:9682888295 (9.0 GiB)  TX bytes:266900462 (254.5 MiB)

什么都没发生。

答案1

这个问题已经在 ServerFault 上得到了回答:

适应那里给出的解决方案根据您的情况产生以下结果(注意:这里我们从接口转发eth0ifb0接口)。

启用转发:

echo 1 > /proc/sys/net/ipv4/ip_forward

允许流量通过防火墙(如果有):

iptables -A FORWARD -p udp -i eth0 -o ifb0 -j ACCEPT

允许已建立的连接的流量:

iptables -A FORWARD -p udp -i eth0 -o ifb0 -m state --state ESTABLISHED,RELATED -j ACCEPT

更改发送到互联网的数据包的源地址:

iptables -t nat -A POSTROUTING -o ifb0 -j MASQUERADE

答案2

确保在内核中启用转发。您可以按如下方式启用转发

sudo echo 1 > /proc/sys/net/ipv4/ip_forwar

然后以 root 用户身份尝试以下操作

iptables -I FORWARD -s [WAN-IP] -p udp -i source_interface -o ifb0 -j ACCEPT
iptables -A FORWARD -d [WAN-IP] -i ifb0 -o source_interface -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -o ifb0 -j MASQUERADE

说明:首先将流量从源接口转发到 ifb0。将返回流量转发回源接口。用规则重写source IP传出数据包的地址POSTROUTING

也可以参考这个帖子https://serverfault.com/questions/431593/iptables-forwarding- Between-two-interface

相关内容