Gentoo: IPSec, L2TP VPN for iOS

There are thousands of guides out there on this subject, however I still struggled to set up an IPSEC VPN at first. This is a HOWTO for my own benefit – maybe someone else will use it too. I struggled because most of the guides involved setting up the VPN on a NAT’d host and connecting to the VPN inside the network. I didn’t do that on my linode, which has a static public IP.

My objectives were clear:

  1. Create a connection point that was semi-secure while connecting to open wifi networks
  2. Bypass some “You are not in the US” restrictions while on the road

Step 1: Install applications, net-misc/openswan, net-dialup/xl2tpd
Step 2: Configure openswan:

# cat /etc/ipsec.conf 
config setup


conn L2TP-PSK-noNAT
# cat /etc/ipsec.secrets %any: PSK "TestSecret"

Where is your public eth0 address and is the subnet that xl2tpd will assign IPs from (can be anything, I picked this at the advice of a guide because it is unlikely to be assigned from a router on a public network)

Step 3: Configure xl2tpd:

# cat /etc/xl2tpd/xl2tpd.conf
ipsec saref = no

[lns default]
ip range =
local ip =
require chap = yes
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes

The local IP must be inside the subnet but outside the IP range above.

# cat /etc/ppp/options.xl2tpd
asyncmap 0
name l2tpd
lcp-echo-interval 30
lcp-echo-failure 4

The ms-dns lines are configurable to any DNS server you have access to.

# cat /etc/ppp/chap-secrets
# Format:
# client server secret IP-addresses
# Two lines are needed since it is two-sided auth
test l2tpd testpass *
l2tpd test testpass *

Step 4: Configure kernel parameters (sysctl)

# cat /etc/sysctl.conf
# only values specific for ipsec/l2tp functioning are shown here. merge with
# existing file
# iPad VPN
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.icmp_ignore_bogus_error_responses = 1

Remember that sysctl.conf is evaluated at boot so run sysctl -p to get the settings enabled now as well.

Step 5: Configure firewall (iptables):
This is the critical step that I wasn’t grokking from the existing guides in the wild. Even when bringing the firewall down to test, you need the NAT/forwarding rules:

# iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
# iptables -A FORWARD -s -j ACCEPT
# iptables -A FORWARD -j REJECT
# iptables -t nat -A POSTROUTING -s -o eth0 -j MASQUERADE

Step 6: Configure the device/client:
Settings -> General -> Network -> VPN -> Add VPN Configuration

Description: Description
Server: (or the hostname)
Account: test
Password: testpass
Secret: TestSecret
Send All Traffic=On

Step 7: Verify it works by going to some IP display webpage and it should show

Conclusion: The above examples should be enough to get the VPN working. There are some tweaking oppurtunities that I didn’t document or elaborate on. There is plenty of examples out there to look at or research, however. This was all setup without the firewall configuration and the client would connect but there would be no onward internet activity. It acted just like there was a invalid DNS server configured, at that point I looked into setting up a NAT, dnsmasq on the local interface, and other wierd things. In the end, just needed to forward the traffic properly.

With that knowledge of the firewall issue, the ultimate instructions would probably be this page:

Leave a comment ?


  1. Thank you for this. I’ve been trying to set something similar on a Gentoo vps and got lost in all the different ways. I can try again.

  2. Gentoo really needs an official OpenL2TP ebuild…

    • It looks like there is an enthusiastic user out there maintaining it. Do you know if it is a drop-in replacement for xl2tpd?

      • Not sure if it is totally as simple as a drop-in replacement (setting up the config is no doubt different) but from what I understand, it utilizes the kernel L2TP functionality, while xl2tpd does not. Also has more advanced features.

        • Makes sense. I knew about that kernel vs userspace already. Also, since I only will ever have one client (me) accessing the VPN at a time, I’m not sure I’m worried about the performance penalty. I probably won’t pursue OpenL2TP to be honest.

          • I used it on my OpenWRT image that I load onto my little router. The userspace L2TP was a total dog on the little MIPS chip. I used it to connect home with my android phone, but have not had it installed for over a year. (After installing cyanogen on the phone, I switched to OpenVPN)

  3. Bjarke I. Pedersen

    Just wondering, why did you choose openswan instead of strongswan?

    It looks like development of openswan has stopped?

  4. Dropping Celluar, Using Google Voice only | Jeremy Weblog - pingback on September 26, 2012 at 8:46 pm
  5. Use Hotspot Shield Free VPN.

    Easy to install.

Leave a Reply

Trackbacks and Pingbacks: