This page is about strongswan. The old racoon documentation can be found here.

When configuring firewalls, tunnels and zones we always have to keep security in mind. First rule should be: Everything that is not allowed explicitly should be denied automatically. This article provides an easy but quite powerful security concept for your IPsec VPN setup. If you missed the basics please have look over there first.

In the following chapters you will find a detailed description of how to setup firewall rules for IPsec VPN connections. The experienced reader may notice that nowhere iptables IPsec policy rules are used (-m policy –pol ipsec). The reason for that is a special VPN scenario where both tunnel ends use overlapping IP addresses. In this case we have do use source NAT (network address translation) rules. SNAT is only available in the POSTROUTING nat table. At this late firewall stage the system will discover for the first time that the packet has to pass the IPsec tunnel. Any ipsec policy based filter before will ignore the packet.

As in many commercial firewall solutions OpenWrt works with zones. A zone is more or less a bunch of computers that reside in the same network. Common examples are WAN, LAN, WLAN, … Why not introduce a new zone for computers behind tunnels. Here are two facts that should encourage the use of a new zone named VPN.

Conclusion: Create a new zone and call it vpn. It is not required to assign an interface to it. If you want to rename the zone to something else you have to adapt parameter zone in /etc/config/ipsec.

Default Rules

We could build our own VPN firewall ruleset with iptables but why not go with LuCI. The interface should be flexible enough to build rules for our new OpenWrt IPsec enhanced router. The basic "Deny All" configuration can be achieved in the upper two panels. You should start with something like that: Idea behind that is:

Tunnel Endpoints

To allow IPsec communications from a remote VPN Gateway the router must be able to terminate incoming connections. Three rules are required.

The easiest will be to allow all traffic to the Endpoint ports. Although being security paranoid we have to think about road warriors that want to connect from random internet addresses. The input_rule queue is a a good place to activate those rules manually with the following commands.

But we are not interested in manual setup. The solution should automatically integrate itself into the OpenWrt environment. So nothing to do now. We will discuss all different security aspects in detail and at the end of the page you will find a all-in-one script that will take care of everything.

VPN Rule Orders

Rules for the VPN zone require special considerations especially if you want to edit them with LuCI. Think of the IP address/interface overlap. Your routers default outgoing interface is normally the WAN connection. Every packet that does not match your internal network will leave there. But with active security profiles in the kernel packets e.g. to the remote VPN subnet 192.168.10.0/24 will go out through the WAN interface too. Of course they will be encrypted in advance. A simple rule "Allow all LAN Zone to WAN Zone" matches any packet to one of the remote VPN networks. Placed at the wrong position on top of the list it may conflict with other VPN specific rules that follow.

Conclusion: The firewall script of at least version 10 will take care about that. You do not have to bother. Outgoing VPN Packets

Up to here it was easy. Now it is time for a deeper firewall inspection. A packet from zone SRC to any other zone will sometime pass the zone_SRC_forward chain. At this point the firewall checks the packets destination address and forwards or drops it. For each rule we created from SRC to somwhere else an entry has been placed into this chain. The chain sequence is analogous to the order of rules. A comparison of the following picture with the above ruleset should make it clear. First of all two rules to zone VPN afterwards one to WAN and one to LAN. But with a sharp eye there are still two faults in our chain.

Two problems two solutions.

And that what it has to look like afterwards. The zone_VPN_ACCEPT and zone_VPN_REJECT are populated and zone_VPN_REJECT chain inserted at the right position. The VPN networks defined in our /etc/config/ipsec are 192.168.10.0/24 and 22.214.171.124/26. The naming convention follows the LuCI standard so you won't get confused. Once again no action has to be taken because we will use a script.

Incoming VPN Packets

Now that outgoing packets are covered by new policies we take care about the other direction. This is much easyier than before. The forward chain misses the jump into zone_vpn_forward chain as LuCI once again left it out due to missing associated interfaces. Without an interface we cannot insert the link directly but have to do subnet checkings in a new layer in between. As the picture shows we jump along the chains forward → zone_VPN_forward (new) → zone_vpn_forward (existing). In this case a single rule from remote machine 192.168.10.1 to local machine 192.168.213.66 was defined. With the new chain links it will be evaluated by the firewall. This modification will also be accomplished by our script.

Packets To The Device

If we want to reach the device through a VPN tunnel we have to check the correctness of the INPUT chain. It branches into the input (lowercase!) chain where the system checks it for the different zones. The jump over to the vpn_zone is missing by default although the chain itself already exists and is populated. So just put this entry at the beginning. This feature has been introduced in version 9 of our firewall script.

NAT Translation

Some of our interfaces will run in masquerade mode. The source address of packets that will leave through these interfaces will be translated to the interface address itself. This is an unwanted contrast to VPN networks where IP addresses are usually untouched. Maybe sometime later we will have look at overlapping VPN subnets. This time our challenge lies in the NAT POSTROUTING chain. For each interface that you flagged with masquerading in LuCI a rule is inserted there. When taking no action something like this will happen.

So once again we have to fix the queue. Therefore we will put a rule at the first position in the chain. This will ensure that packets to foreign VPN subnets will remain untouched.

Firewall integration

To enable custom firewall rules we hook up with the default firewall mechanism. Ensure that firewall user scripts are loaded and reloaded everytime we (re)start the OpenWrt firewall. Verify/adapt the following lines in /etc/config/firewall

Additionally place the call to the ipsec user firewall script into /etc/firewall.user.

VPN Firewall Script

Finally we have a look at the script. It injects all the additionally required settings according to /etc/config/ipsec into the OpenWrt firewall. Save it as /etc/ipsec/firewall.sh and put a calling line into /etc/firewall.user so it gets loaded automatically. REMARK: This script only enables VPN firewall rules that have been set in the LUCI web interface. There is no guarantee that manually implemented rules in /etc/config/firewall will work!
[null, null, {"document_url": "http://wiki.openwrt.org/doc/howto/vpn.ipsec.firewall", "unformatted_src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_firewall.png", "src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_firewall.png", "formatted_filename": "ipsec firewall"}, null, {"document_url": "http://wiki.openwrt.org/doc/howto/vpn.ipsec.firewall", "unformatted_src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_vpn_rules.png", "src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_vpn_rules.png", "formatted_filename": "ipsec vpn rules"}, null, {"document_url": "http://wiki.openwrt.org/doc/howto/vpn.ipsec.firewall", "unformatted_src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_forward_chain.png", "src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_forward_chain.png", "formatted_filename": "ipsec forward chain"}, null, {"document_url": "http://wiki.openwrt.org/doc/howto/vpn.ipsec.firewall", "unformatted_src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_chain_mod.png", "src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_chain_mod.png", "formatted_filename": "ipsec chain mod"}, null, {"document_url": "http://wiki.openwrt.org/doc/howto/vpn.ipsec.firewall", "unformatted_src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_chain_mod2.png", "src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_chain_mod2.png", "formatted_filename": "ipsec chain mod"}, null, {"document_url": "http://wiki.openwrt.org/doc/howto/vpn.ipsec.firewall", "unformatted_src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_chain_in.png", "src": "http://wiki.openwrt.org/_media/doc/howto/ipsec_chain_in.png", "formatted_filename": "ipsec chain in"}, null]