Simplest Port Forwarding | IPsec VPN + PBR + virtualization

How can I get this finally worked??

config redirect
        option target 'DNAT'
        option name '2222'
        list proto 'tcp'
        option src 'wan'
        option src_dport '2222'
        option dest_ip '192.168.1.20'
        option dest_port '80'
        option dest '*'

The most primitive port redirection didn't work on 18.06, neither on 22.03.

On 18.06 solved by custom iptables rules:

iptables -t nat -A PREROUTING -p tcp --dport 1111 -j DNAT --to-destination 192.168.1.20:80

iptables -t nat -A POSTROUTING -p tcp -d 192.168.1.20 --dport 80 -j SNAT --to-source 192.168.1.1

On 22.03 box iptables issues errors. Can anyone help with nftables rules please?

1 Like

Have you tried putting the appropriate firewall zone here? Instead of *?

2 Likes

Seeing your custom rules led me to believe this might not be a router in normal gateway mode?

A normal port forward consists of a DNAT rule and a FORWARD ACCEPT rule.

Please share how this router is setup

2 Likes

Yes. I did.

(I suppose we're talking about 18.06 box) You mean it should have a port FORWARDING rule AND a normal firewall rule to accept packages to that port? If so, I tried all possible/impossible combinations in firewall section. Didn't work. Only thing worked is those custom rules. And it works without any forwarding rule configured in LuCi.

And I don't understand what you mean by "normal" gateway. Three interfaces WAN, LAN, IPSEC. All is up and working. Trying to open a port on WAN and send traffic to/from LAN web-server. Worked (with those custom rules).

On 22.03 trying to open port on WAN and send traffic to/from a web-server in IPSEC. (Server is reachable and pinging).

It's be helpful at this point to see the complete network and firewall configs (/etc/config/network and /etc/config/firewall). There's nothing particularly special about the port forward you're trying to use and a uci rule should work just fine.

3 Likes

22.03 BOX, 192.168.1.1 can ping 192.168.10.20 in ipsec

Firewall

 cat /etc/config/firewall

config defaults
        option synflood_protect '1'
        option input 'REJECT'
        option output 'REJECT'
        option forward 'REJECT'

config zone
        option name 'lan'
        option output 'ACCEPT'
        option forward 'REJECT'
        option input 'ACCEPT'
        list network 'lan'

config rule
        option name 'Allow-DHCP-Renew'
        option src 'wan'
        option proto 'udp'
        option dest_port '68'
        option target 'ACCEPT'
        option family 'ipv4'

config rule
        option name 'Allow-Ping'
        option proto 'icmp'
        option family 'ipv4'
        list icmp_type 'echo-request'
        option target 'ACCEPT'
        option src 'wan'
        option enabled '0'

config rule
        option name 'Allow-IGMP'
        option src 'wan'
        option proto 'igmp'
        option family 'ipv4'
        option target 'ACCEPT'

config rule
        option name 'Allow-DHCPv6'
        option src 'wan'
        option proto 'udp'
        option dest_port '546'
        option family 'ipv6'
        option target 'ACCEPT'

config rule
        option name 'Allow-MLD'
        option src 'wan'
        option proto 'icmp'
        option family 'ipv6'
        list src_ip 'fe80::/10'
        option target 'ACCEPT'

config rule
        option name 'Allow-ICMPv6-Input'
        option src 'wan'
        option proto 'icmp'
        option limit '1000/sec'
        option family 'ipv6'
        list icmp_type 'bad-header'
        list icmp_type 'destination-unreachable'
        list icmp_type 'echo-reply'
        list icmp_type 'echo-request'
        list icmp_type 'neighbour-advertisement'
        list icmp_type 'neighbour-solicitation'
        list icmp_type 'packet-too-big'
        list icmp_type 'router-advertisement'
        list icmp_type 'router-solicitation'
        list icmp_type 'time-exceeded'
        list icmp_type 'unknown-header-type'
        option target 'ACCEPT'
        option enabled '0'

config rule
        option name 'Allow-ICMPv6-Forward'
        option src 'wan'
        option dest '*'
        option proto 'icmp'
        option limit '1000/sec'
        option family 'ipv6'
        list icmp_type 'bad-header'
        list icmp_type 'destination-unreachable'
        list icmp_type 'echo-reply'
        list icmp_type 'echo-request'
        list icmp_type 'packet-too-big'
        list icmp_type 'time-exceeded'
        list icmp_type 'unknown-header-type'
        option target 'ACCEPT'
        option enabled '0'

config rule
        option name 'Allow-IPSec-ESP'
        option src 'wan'
        option proto 'esp'
        option target 'ACCEPT'

config rule
        option name 'Allow-ISAKMP'
        option src 'wan'
        option target 'ACCEPT'
        option dest_port '500 4500'
        list proto 'udp'

config rule
        option name 'SSH/SCP'
        option target 'ACCEPT'
        option dest_port '22'
        list proto 'tcp'
        option src 'wan'

config rule
        option name 'LuCi'
        list proto 'tcp'
        option target 'ACCEPT'
        option dest_port '7745'
        option src 'lan'
        option enabled '0'

config zone
        option name 'wan'
        option output 'ACCEPT'
        option mtu_fix '1'
        option forward 'REJECT'
        option input 'REJECT'
        option masq '1'
        list network 'wan'
        list network 'wan6'

config zone
        option name 'ipsec0'
        option forward 'REJECT'
        list device 'ipsec0'
        option family 'ipv4'
        option input 'ACCEPT'
        option output 'ACCEPT'

config forwarding
        option src 'lan'
        option dest 'wan'

config forwarding
        option src 'ipsec0'
        option dest 'wan'

config rule
        option name 'DNS'
        option src 'ipsec0'
        option target 'ACCEPT'
        list proto 'tcp'
        list proto 'udp'
        option dest_port '53'

config redirect
        option target 'DNAT'
        option name '2222'
        list proto 'tcp'
        option src 'wan'
        option src_dport '2222'
        option dest_ip '192.168.10.20'
        option dest_port '80'
        option dest '*'

Network

 cat /etc/config/network

config interface 'loopback'
        option device 'lo'
        option proto 'static'
        option ipaddr '127.0.0.1'
        option netmask '255.0.0.0'

config globals 'globals'
        option packet_steering '1'
        option ula_prefix 'aaaa:9999:0000::/48'

config device
        option name 'br-lan'
        option type 'bridge'
        list ports 'lan2'
        list ports 'lan3'
        list ports 'lan4'
        list ports 'lan5'

config device
        option name 'lan2'
        option macaddr '11:22:33:44:55:66'

config device
        option name 'lan3'
        option macaddr '11:22:33:44:55:66'

config device
        option name 'lan4'
        option macaddr '11:22:33:44:55:66'

config device
        option name 'lan5'
        option macaddr '11:22:33:44:55:66'

config interface 'lan'
        option proto 'static'
        option ip6assign '60'
        option ipaddr '192.168.1.1'
        option netmask '255.255.255.0'
        option device 'lan5'

config device
        option name 'wan'
        option ipv6 '0'
        option macaddr '00:xx:xx:xx:xx:xx'

config interface 'wan'
        option device 'wan'
        option proto 'static'
        option ipaddr '1x4.1x4.1x4.17'
        option netmask '255.255.255.0'
        option gateway '1x4.1x4.1x4.1'
        list dns '1x4.1x4.9.9'
        list dns '1x4.1x4.10.10'
        list dns '8.8.8.8'

config interface 'wan6'
        option device 'wan'
        option proto 'dhcpv6'
        option auto '0'
        option disabled '1'

Yes. Every router I used from cheapest TP-link to Cisco could do it. But according to what numerous users reported, this is a problem in OpwnWrt.

What reports? I'm not aware of any particular issues with OpenWRT and port forwarding. I know that a uci rule consisting of all the same elements as yours (with different variables obviously) does work fine.

As it is, a fairly obvious issue would seem to be that your uci rule has a src_dport of 2222, but your custom rule is using 1111. Which is it?

And option dest should be lan rather than *.

2 Likes

Custom rule (and 1111 port) was for 18.06

These configs use 2222 and are for 22.03. And I tried for lan zone as well.

And one of hundreds report... (please notice topic name) )))

From where are you testing the rule?

From outside WAN, https://canyouseeme.org/ service.
Connection is not refused, but timed out.

Have you checked that the target device doesn't have a firewall that's blocking the connection? If the port forward rule wasn't working then I'd expect you to get a connection refused rather than a timeout.

And as for the thread you posted, if you read through it you'd have realised that the device in question wasn't actually using vanilla OpenWRT. It was using a version that had been customised (with god knows what changed) by a third party vendor. So again, there's no specific issue (as far as I'm aware) with OpenWRT and port forwarding.

Yes, I can connect to it from other WAN (by the way it's exactly 18.06 with those custom rules set).

And it was the same situation with 18.06 (connection timeout), until I also added custom SNAT back reply rule. It was no way to get it worked via LuCi. (And I suppose after I add forwarding rule reply-back rule should have be added automatically, but it's not).

So I think I need something like I did with iptables, but for newer nftables...

Notice the topic name. These 200 issues mentioned are not about vanilla OWrt. I say that because I read most of them to find a solution to my case. I understand that every case is individual, but numerous cases talk itself on that issue persists for a very simple router operation. (But let's forget about it)

I've just tested two rules:

config redirect
        option dest '*'
        option target 'DNAT'
        option name 'test'
        option src 'wan'
        option src_dport '2222'
        option dest_ip '192.168.1.48'
        option dest_port '1111'
        list proto 'tcp'

and

config redirect
        option dest 'lan'
        option target 'DNAT'
        option name 'test'
        option src 'wan'
        option src_dport '2222'
        option dest_ip '192.168.1.48'
        option dest_port '1111'
        list proto 'tcp'

Both work exactly as expected, i.e. trying to connect externally to my public IP on port 2222 reaches the listener on my local device using port 1111.

Port forwarding works. The fact that whatever you're connecting to doesn't appear to be able to respond without SNAT is not an issue with OpenWRT itself. It's either an issue with whatever you're connecting to or your wider setup.

2 Likes

It's an Apache server (in LAN zone for 18.06). Nothing special. If OpenWrt handles it WITH SNAT, I don't see why it doesn't WITHOUT it. So probably OpenWrt is responsible? Still digging...

Probably because it's something to do with how you've setup the Apache server. I would guess something like it's been set up to only respond to requests from 192.168.1.1 (or possibly a subnet containing that address). That'd be why it responds when the incoming packets (with a public IP) have had their source address rewritten, but doesn't when they haven't.

2 Likes

Exacrly, the apache server has its own firewall setup to only allow its own subnet.
Classic mistake :wink:

2 Likes

Same story with port 22. Sorry, it's not [only] Apache...