iptables 端口转发和强制端口上的接口

iptables 端口转发和强制端口上的接口

DDWRT 正在运行 VPN 客户端守护程序(接口 tun1),所有流量(有几个例外)都通过该接口路由。

第一个例外是端口 12600 上的流量应通过 进行路由vlan2,这是常规的非 VPN 互联网连接。这允许我在外出时通过 SSH 连接到我的路由器。

# Create table 202 via the Gateway Ip on the Interface VLAN2
ip route add default via $(nvram get wan_gateway) dev vlan2 table 202
# Apply the rule on table 202 to packages marked with 22
ip rule add fwmark 22 table 202
# Tag with 22 every output package on port 12600 not coming from any machine in the local network
iptables -t mangle -I OUTPUT -p tcp --sport 12600 -d ! 192.168.1.0/24 -j MARK --set-mark 22

这就像魅力一样有效:-)

现在,第二个问题是将端口 16001 转发到我的 Raspberry PI(本地 IP 192.168.1.132),并通过该vlan2接口路由该流量,而不是tun1.

转发部分正在工作:

iptables -t nat -I PREROUTING -p tcp --dport 12601 -j DNAT --to 192.168.`.132:12601
iptables -I FORWARD -i vlan2 -d 192.168.1.132 -p tcp --dport 12601 -j ACCEPT

此时,如果我停用路由器上的 OpenVPN 守护进程,则通过 SSH 进入 RPI 即可正常工作。问题是,当守护程序处于活动状态时,程序包会vlan2按预期通过接口到达 RPI,RPI 发回响应,并通过 进行路由tun1,从而使 SSH 无法进行。

我尝试添加规则:

iptables -t mangle -I OUTPUT -p tcp -s 192.168.1.132 -j MARK --set-mark 22

但这并没有奏效。我看到没有软件包被标记为该规则。

有人指的是rp_filter,但是我的都设置为0(这肯定是DD-WRT的默认值,因为我没有修改它):

root@DD-WRT:~# sysctl -a | grep \\.rp_filter
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.br0.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.eth0.rp_filter = 0
net.ipv4.conf.eth1.rp_filter = 0
net.ipv4.conf.eth2.rp_filter = 0
net.ipv4.conf.imq0.rp_filter = 0
net.ipv4.conf.imq1.rp_filter = 0
net.ipv4.conf.lo.rp_filter = 0
net.ipv4.conf.teql0.rp_filter = 0
net.ipv4.conf.tun1.rp_filter = 0
net.ipv4.conf.vlan1.rp_filter = 0
net.ipv4.conf.vlan2.rp_filter = 0

这些是我的 iptables:

root@DD-WRT:~# iptables -vnL -t nat
Chain PREROUTING (policy ACCEPT 183 packets, 30508 bytes)
 pkts bytes target     prot opt in     out     source               destination
    4   256 DNAT       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:12601 to:192.168.1.132:12601
   53  3644 DNAT       udp  --  br0    *       0.0.0.0/0            0.0.0.0/0           udp dpt:53 to:192.168.1.1
    0     0 DNAT       tcp  --  br0    *       0.0.0.0/0            0.0.0.0/0           tcp dpt:53 to:192.168.1.1
    0     0 DROP       tcp  --  *      *       192.168.1.1          192.168.1.1         tcp dpt:443
    0     0 ACCEPT     tcp  --  *      *       192.168.1.0/24       192.168.1.1         tcp dpt:443
    3   180 DNAT       tcp  --  *      *       0.0.0.0/0           !73.93.100.100       tcp dpt:80 to:192.168.1.1:8118
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            73.93.100.100       tcp dpt:8080 to:192.168.1.1:443
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            73.93.100.100       tcp dpt:12600 to:192.168.1.1:12600
    0     0 DNAT       icmp --  *      *       0.0.0.0/0            73.93.100.100       to:192.168.1.1
  126 21928 TRIGGER    0    --  *      *       0.0.0.0/0            73.93.100.100       TRIGGER type:dnat match:0 relate:0

Chain INPUT (policy ACCEPT 11131 packets, 732K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 60 packets, 6022 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 23 packets, 4522 bytes)
 pkts bytes target     prot opt in     out     source               destination
   92  9920 MASQUERADE  0    --  *      tun1    0.0.0.0/0            0.0.0.0/0
    2   120 SNAT       0    --  *      vlan2   192.168.1.0/24       0.0.0.0/0           to:73.93.100.100



root@DD-WRT:~# iptables -vnL -t mangle
Chain PREROUTING (policy ACCEPT 1787 packets, 398K bytes)
 pkts bytes target     prot opt in     out     source               destination
 1787  398K FILTER_IN  0    --  *      *       0.0.0.0/0            0.0.0.0/0
  385  101K VPN_IN     0    --  tun+   *       0.0.0.0/0            0.0.0.0/0

Chain INPUT (policy ACCEPT 529K packets, 462M bytes)
 pkts bytes target     prot opt in     out     source               destination
   53  7298 IMQ        0    --  tun+   *       0.0.0.0/0            0.0.0.0/0           IMQ: todev 0
  559  161K IMQ        0    --  vlan2  *       0.0.0.0/0            0.0.0.0/0           IMQ: todev 0
  293 35826 IMQ        0    --  !vlan2 *       0.0.0.0/0            0.0.0.0/0           IMQ: todev 1

Chain FORWARD (policy ACCEPT 487K packets, 318M bytes)
 pkts bytes target     prot opt in     out     source               destination
  332 94003 IMQ        0    --  tun+   *       0.0.0.0/0            0.0.0.0/0           IMQ: todev 0
   98  5984 TCPMSS     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:0x06/0x02 TCPMSS clamp to PMTU
   39  6701 IMQ        0    --  vlan2  *       0.0.0.0/0            0.0.0.0/0           IMQ: todev 0
  918  204K IMQ        0    --  !vlan2 !vlan2  0.0.0.0/0            0.0.0.0/0           IMQ: todev 1

Chain OUTPUT (policy ACCEPT 823 packets, 196K bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 MARK       tcp  --  *      *       0.0.0.0/0           !192.168.1.0/24      tcp spt:12600  MARK set 0x16
   21 16378 IMQ        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp spt:8118 IMQ: todev 0
    0     0 MARK       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:12601  MARK set 0x16

Chain POSTROUTING (policy ACCEPT 928K packets, 531M bytes)
 pkts bytes target     prot opt in     out     source               destination
  525 89419 VPN_OUT    0    --  *      tun+    0.0.0.0/0            0.0.0.0/0
 1827  412K FILTER_OUT  0    --  *      *       0.0.0.0/0            0.0.0.0/0
    0     0 DSCP       0    --  *      *       0.0.0.0/0            0.0.0.0/0           DSCP match !0x00 DSCP set 0x00

Chain FILTER_IN (1 references)
 pkts bytes target     prot opt in     out     source               destination
 1787  398K CONNMARK   0    --  *      *       0.0.0.0/0            0.0.0.0/0           CONNMARK restore
 1413  324K SVQOS_SVCS  0    --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x0/0x7ffc00
 1787  398K CONNMARK   0    --  *      *       0.0.0.0/0            0.0.0.0/0           CONNMARK save
 1787  398K RETURN     0    --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FILTER_OUT (1 references)
 pkts bytes target     prot opt in     out     source               destination
 1827  412K CONNMARK   0    --  *      *       0.0.0.0/0            0.0.0.0/0           CONNMARK restore
 1488  328K SVQOS_SVCS  0    --  *      *       0.0.0.0/0            0.0.0.0/0           mark match 0x0/0x7ffc00
 1827  412K VPN_DSCP   0    --  *      *       0.0.0.0/0            0.0.0.0/0
 1827  412K CONNMARK   0    --  *      *       0.0.0.0/0            0.0.0.0/0           CONNMARK save
 1827  412K RETURN     0    --  *      *       0.0.0.0/0            0.0.0.0/0

Chain SVQOS_SVCS (2 references)
 pkts bytes target     prot opt in     out     source               destination
 2901  653K RETURN     0    --  *      *       0.0.0.0/0            0.0.0.0/0

Chain VPN_DSCP (1 references)
 pkts bytes target     prot opt in     out     source               destination
    0     0 MARK       0    --  *      *       0.0.0.0/0            0.0.0.0/0           DSCP match 0x0a  MARK xset 0x19000/0x7ffc00
    0     0 MARK       0    --  *      *       0.0.0.0/0            0.0.0.0/0           DSCP match 0x01  MARK xset 0x2800/0x7ffc00
  126 32463 MARK       0    --  *      *       0.0.0.0/0            0.0.0.0/0           DSCP match 0x02  MARK xset 0x5000/0x7ffc00
    0     0 MARK       0    --  *      *       0.0.0.0/0            0.0.0.0/0           DSCP match 0x03  MARK xset 0x7800/0x7ffc00
   77 20422 MARK       0    --  *      *       0.0.0.0/0            0.0.0.0/0           DSCP match 0x04  MARK xset 0xa000/0x7ffc00
  313 73735 DSCP       0    --  *      *       0.0.0.0/0            0.0.0.0/0           DSCP match !0x00 DSCP set 0x00
 1827  412K RETURN     0    --  *      *       0.0.0.0/0            0.0.0.0/0

Chain VPN_IN (1 references)
 pkts bytes target     prot opt in     out     source               destination
  385  101K CONNMARK   0    --  *      *       0.0.0.0/0            0.0.0.0/0           CONNMARK save

Chain VPN_OUT (1 references)
 pkts bytes target     prot opt in     out     source               destination




root@DD-WRT:~# iptables -vnL -t filter
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
   95 20753 ACCEPT     0    --  tun1   *       0.0.0.0/0            0.0.0.0/0
  864  291K ACCEPT     0    --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
    0     0 ACCEPT     udp  --  vlan2  *       0.0.0.0/0            0.0.0.0/0           udp spt:67 dpt:68
    0     0 ACCEPT     udp  --  vlan2  *       0.0.0.0/0            0.0.0.0/0           udp dpt:520
    0     0 DROP       udp  --  br0    *       0.0.0.0/0            0.0.0.0/0           udp dpt:520
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:520
  146 12000 ACCEPT     0    --  br0    *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.1.1         tcp dpt:443
    0     0 logbrute   tcp  --  vlan2  *       0.0.0.0/0            192.168.1.1         tcp dpt:12600
    0     0 ACCEPT     tcp  --  vlan2  *       0.0.0.0/0            192.168.1.1         tcp dpt:12600
    0     0 DROP       icmp --  vlan2  *       0.0.0.0/0            0.0.0.0/0
    0     0 DROP       2    --  *      *       0.0.0.0/0            0.0.0.0/0
    4   293 ACCEPT     0    --  lo     *       0.0.0.0/0            0.0.0.0/0           state NEW
    0     0 ACCEPT     0    --  br0    *       0.0.0.0/0            0.0.0.0/0           state NEW
  162 27477 DROP       0    --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
  731  143K ACCEPT     0    --  *      tun1    0.0.0.0/0            0.0.0.0/0
  544  189K ACCEPT     0    --  tun1   *       0.0.0.0/0            0.0.0.0/0
   11   688 ACCEPT     tcp  --  vlan2  *       0.0.0.0/0            192.168.1.132       tcp dpt:12601
   86 12596 ACCEPT     0    --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
  158 37906 lan2wan    0    --  *      *       0.0.0.0/0            0.0.0.0/0
  156 37786 ACCEPT     0    --  br0    br0     0.0.0.0/0            0.0.0.0/0
    2   120 ACCEPT     0    --  br0    vlan2   0.0.0.0/0            0.0.0.0/0
    0     0 DROP       tcp  --  *      vlan2   0.0.0.0/0            0.0.0.0/0           tcp dpt:1723
    0     0 DROP       udp  --  *      vlan2   0.0.0.0/0            0.0.0.0/0           udp dpt:1701
    0     0 DROP       udp  --  *      vlan2   0.0.0.0/0            0.0.0.0/0           udp dpt:500
    0     0 TRIGGER    0    --  vlan2  br0     0.0.0.0/0            0.0.0.0/0           TRIGGER type:in match:0 relate:0
    0     0 trigger_out  0    --  br0    *       0.0.0.0/0            0.0.0.0/0
    0     0 ACCEPT     0    --  br0    *       0.0.0.0/0            0.0.0.0/0           state NEW
    0     0 DROP       0    --  *      *       0.0.0.0/0            0.0.0.0/0

tcpdump可以看到端口 12601 上流向 RPI 的入站流量正在通过,vlan2但出站流量路由于tun1

root@DD-WRT:~# tcpdump -n -i vlan2 port 12601
listening on vlan2, link-type EN10MB (Ethernet), capture size 65535 bytes
20:49:19.983896 IP 208.54.5.222.47184 > 73.93.100.100.12601: Flags [S], seq 1024682508, win 65535, options [mss 1380,nop,wscale 5,nop,nop,TS val 336905559 ecr 0,sackOK,eol], length 0
20:49:21.055561 IP 208.54.5.222.47184 > 73.93.100.100.12601: Flags [S], seq 1024682508, win 65535, options [mss 1380,nop,wscale 5,nop,nop,TS val 336906559 ecr 0,sackOK,eol], length 0
20:49:22.092159 IP 208.54.5.222.47184 > 73.93.100.100.12601: Flags [S], seq 1024682508, win 65535, options [mss 1380,nop,wscale 5,nop,nop,TS val 336907559 ecr 0,sackOK,eol], length 0
20:49:23.055189 IP 208.54.5.222.47184 > 73.93.100.100.12601: Flags [S], seq 1024682508, win 65535, options [mss 1380,nop,wscale 5,nop,nop,TS val 336908559 ecr 0,sackOK,eol], length 0

root@DD-WRT:~# tcpdump -n -i tun1 port 12601
listening on tun1, link-type RAW (Raw IP), capture size 65535 bytes
20:49:37.052293 IP 73.93.100.100.12601 > 208.54.5.222.47184: Flags [S.], seq 3536123446, ack 1024682509, win 28960, options [mss 1460,sackOK,TS val 2958399 ecr 336905559,nop,wscale 7], length 0
20:49:38.062308 IP 73.93.100.100.12601 > 208.54.5.222.59028: Flags [S.], seq 2196420762, ack 1024682509, win 28960, options [mss 1460,sackOK,TS val 2958500 ecr 336916559,nop,wscale 7], length 0
20:49:38.995709 IP 73.93.100.100.12601 > 208.54.5.222.41591: Flags [S.], seq 4130650631, ack 1024682509, win 28960, options [mss 1460,sackOK,TS val 2958593 ecr 336924559,nop,wscale 7], length 0

3 packets captured
3 packets received by filter
0 packets dropped by kernel

答案1

成功了。看起来 iptables 发生了变化,即使ip route flush cache在 DD-WRT 刷新之前不会发生;我所说的刷新是指重新启动所有主要服务,更改某些设置并单击后会发生什么Apply Changes

作为参考,我希望能够从外部网络通过 SSH 访问我的 Raspberry Pi。 RPI 连接到作为客户端运行 OpenVPN 的 DD-WRT 路由器。 DD-WRT 需要将端口 12601 (SSH) 转发到 RPI,并避免通过 VPN 接口tun1(默认)发送响应包,因为响应包是通过 Internet 网关传入的vlan2

这是我的防火墙脚本的样子:

# Create a rule to skip the VPN
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter  
iptables -t mangle -F PREROUTING  
ip route add default table 200 via $(nvram get wan_gateway)  
ip rule add fwmark 1 table 200  
ip route flush cache

## SSH to RPI (port 12601)
# First the port forwarding part
iptables -t nat -I PREROUTING -p tcp --dport 12601 -j DNAT --to 192.168.4.132:12601
iptables -I FORWARD -i vlan2 -d 192.168.1.132 -p tcp --dport 12601 -j ACCEPT
# Now mark packages from RPI and source port 12601 with tag 1. The rule above will direct packages marked with 1 through the wan gateway
iptables -t mangle -I PREROUTING -i br0 -p tcp -s 192.168.1.132 --sport 12601 -j MARK --set-mark 1

相关内容