A few weeks back I upgraded my wireless router and added an racoon based IPsec VPN. When I built my first home router / wireless access point a few years ago I did so to get some experience configuring Linux systems and writing firewall rules. I’ve revisited this script a number of times as I’ve added new functionality to the access point and now I’ve added a few rules for IPsec traffic that I figured were worth mentioning here.
First off the script is a “default drop” policy. This means that any packets that don’t match a rule in the firewall ending in a jump to ACCEPT target are dropped. More specifically the default policy for the input, output and forward chains are DROP:
iptables --policy INPUT DROP iptables --policy OUTPUT DROP iptables --policy FORWARD DROP
This means that any traffic that doesn’t have an explicit rule that allows it to pass will be dropped. The design goal I had in mind is two write all rules that allow traffic to be as exact as possible so as to not match any unintended traffic. This is as close to mandatory access control for network traffic as I can figure.
Rules for IKE exchange
Hopefully I’ll get a chance to publish the whole script but it’s pretty long (600 lines or so) and needs some cleanup. But the rules I’ve added to allow the racoon server are pretty clean and they’re super useful so here they are:
The first stage of establishing the VPN tunnel is direct negotiation between the client and the access point (the wireless router in this case). The racoon server listens on ports 500 and 4500 and it negotiates directly with the clients racoon server on the same ports. I’m including just the raw rules here but you should wrap these up in functions to keep your scripts clean:
iptables --table filter --append INPUT --in-interface extif --proto udp --destination ${PUBLIC_IP} --dport 500 --source 0.0.0.0/ --sport 500 --jump ACCEPT iptables --table filter --append INPUT --in-interface extif --proto udp --destination ${PUBLIC_IP} --dport 4500 --source 0.0.0.0/0 --sport 4500 --jump ACCEPT iptables --table filter --append OUTPUT --out-interface extif --proto udp --destination 0.0.0.0/0 --dport 500 --source ${PUBLIC_IP} --sport 500 --jump ACCEPT iptables --table filter --append OUTPUT --out-interface extif --proto udp --destination 0.0.0.0/0 --dport 4500 --source ${PUBLIC_IP} --sport 4500 --jump ACCEPT }
Notice that I’ve limited the interface through which the IKE traffic can be accepted and transmitted as well as the IP of the server and the source and destination ports. The loosest part is that of the clients IP which we can’t know in advance. These rules are pretty standard, the interesting ones come next.
Rules for VPN traffic
So now our clients can negotiate security associations with the IKE server. This allows them to get IP addresses on the VPN network but they can’t talk to any other systems on the VPN or the other networks connected to the router (my file server etc).
VPN talking to internal network
To enable this we need FOWARD rules allowing traffic from the VPN network (10.0.2.0/24 that arrives on the interface ‘extif’) to go to the other network connected to the router (10.0.1.0/24 that arrives on the interface ‘wirif’). Typically this would look like:
iptables -A FORWARD --in-interface extif --source 10.0.2.0/24 --out-interface wirif --destination 10.0.1.0/24 --jump ACCEPT iptables -A FORWARD --in-interface wirif --source 10.0.1.0/24 --out-interface extif --destination 10.0.2.0/24 --jump ACCEPT
These rules look pretty tight right? We’ve limited the the networks that can communicate and the interfaces on which the traffic should originate. We can do better though.
I’m particularly wary of the interface on the router that’s connected to the internet. The iptables rules above will allow the traffic we want to allow but it doesn’t require that the traffic from the 10.0.2.0/24 network arrive over the VPN! Theoretically an attacker that isn’t on the VPN could spoof traffic with the VPN IP and exchange packets with the protected 10.0.1.0/24 network. This is bad.
Policy Matching
So we need to add to these rules policy that will require the packets come from, and go to the VPN network through the IPsec tunnel. I’d never even heard of the iptables policy module till I was searching for the answer to this problem. Here’s what I came up with:
iptables --append FORWARD --match policy --dir in --pol ipsec --mode tunnel --tunnel-dst ${PUBLIC_IP} --tunnel-src 0.0.0.0/0 --in-interface extif --source 10.0.2.0/24 --out-interface wirif --destination 10.0.1.0/24 --jump ACCEPT iptables --append FORWARD --match policy --dir out --pol ipsec --mode tunnel --tunnel-dst 0.0.0.0/0 --tunnel-src ${PUBLIC_IP} --in-interface wirif --source 10.0.1.0/24 --out-interface extif --destination 10.0.2.0/24 --jump ACCEPT
This allows the two networks to communicate, but this time it specifies that the packets must travel through the IPsec tunnel as we’d expect.
So now our VPN users can connect to the racoon server on the firewall and can talk to hosts behind the firewall. What’s left? Well my router / firewall also happens to have a dnsmasq server that offers clients DHCP leases and DNS service. The DNS service is particularly useful for those connecting to my storage server so they don’t have to memorize IP addresses.
Offering DNS services to VPN users
VPN users should be able to use the DNS service too (they don’t need DHCP since they get their IPs from racoon). The rules to allow this are very similar to the FORWARD rules above:
iptables --append INPUT --match policy --pol ipsec --dir in --mode tunnel --tunnel-dst ${PUBLIC_IP} --tunnel-src 0.0.0.0/0 --protocol udp --in-interface extif --source 10.0.2.0/24 --source-port 1024:65535 --destination 10.0.1.1/24 --destination-port 53 --jump ACCEPT iptables --append OUTPUT --match policy --pol ipsec --dir out --mode tunnel --tunnel-dst 0.0.0.0/0 --tunnel-src ${PUBLIC_IP} --protocol udp --source $10.0.1.1/24 --source-port 53 --out-interface extif --destination 10.0.2.0/24 --destination-port 1024:65535 --jump ACCEPT
These rules are pretty self explanatory once you understand the syntax. There’s lots of info in these rules too, we identify the direction of the packets through the ipsec tunnel, the endpoints of the tunnel and the standard IP stuff: source address and port, destination and port as well as the interfaces.
That’s it. 8 rules are all that’s required to allow VPN users to connect, speak to hosts on my internal network and to talk to the DNS server. Using the policy match these rules are pretty exact … if there’s anyway I can make them better let me know in the comments.