iptables encaminhamento de porta e forçando a interface na porta

1

Eu esperava obter alguma ajuda para configurar meu DD-WRT. Minha configuração é relativamente simples, o DDWRT está executando um daemon de cliente VPN (interface tun1) e todo o tráfego, com algumas exceções sendo roteadas por essa interface.

A primeira exceção é que o tráfego na porta 12600 deve ser encaminhado por meio de vlan2 , que é a conexão de Internet normal não configurada. Isso me permite acessar o SSH no roteador quando sair de casa.

# 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

Isso está funcionando como um charme: -)

Agora, a segunda questão é encaminhar a porta 16001 para meu PI de framboesa (IP local 192.168.1.132) e também rotear esse tráfego pela interface vlan2 e não tun1 .

A parte de encaminhamento está funcionando:

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

Neste ponto, se eu desativar o daemon do OpenVPN no roteador, o SSH no RPI está funcionando. O problema é que quando o daemon está ativo, os pacotes chegam ao RPI através da interface vlan2 como esperado, o RPI envia a resposta de volta, ele é roteado através de tun1 , tornando o SSH impossível.

Eu tentei adicionar a regra:

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

Mas isso não funcionou. Vejo que nenhum pacote está sendo marcado com essa regra. Eu tenho cavado o StackOverflow e até agora nada ajudou.

Alguém apontou para o rp_filter, mas as minas estão todas configuradas para 0 (deve ser o valor padrão para o DD-WRT porque eu não o modifiquei):

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

Estes são meus 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

Com tcpdump , posso ver que o tráfego de entrada em direção ao RPI na porta 12601 está passando por vlan2 , mas o tráfego de saída é roteado em 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

Aprecie sua ajuda.

    
por tokenizer_fsj 30.12.2016 / 05:52

1 resposta

1

Entendi. Parece que o iptables muda, mesmo depois de um ip route flush cache não ocorrer até que o DD-WRT seja atualizado; por refresh quero dizer reiniciar todos os serviços principais, o que acontece depois de alterar algumas configurações e clicar em Apply Changes .

Para referência, eu queria poder conectar o SSH ao meu Raspberry Pi a partir de uma rede externa. O RPI está conectado a um roteador DD-WRT que está executando o OpenVPN como um cliente. O DD-WRT precisa encaminhar a porta 12601 (SSH) para o RPI e evitar o envio dos pacotes de resposta pela interface da VPN tun1 (padrão) à medida que os pacotes chegam pelo gateway da Internet vlan2 .

É assim que meu script de firewall se parece:

# 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
    
por 02.01.2017 / 19:44

Tags