「L2TP」- 通过 xl2tpd 搭建 L2TP VPN 隧道

  CREATED BY JENKINSBOT

问题描述

xl2tpd 是 L2TP 协议(RFC 2661)的软件实现,该项目从 l2tpd 中 fork 出来,由 Xelerance 公司维护。

该笔记将记录:如何配置 xl2tpd 服务,以实现 VPN 功能,以及常见问题解决方案。

解决方案

其用途是:通过隧道在 IP Network 中发送 PPP 帧。它实现 L2TP Network 的 LAC(作为 L2TP Client 连接 L2TP Server)和 LNS(提供 L2TP VPN 服务)设备。

xl2tpd 通过打开 伪 tty 与 pppd 进行通信来工作。它完全在用户空间运行,但支持内核模式 L2TP。

# pstree -a $(pidof xl2tpd)
xl2tpd
	└─pppd passive nodetach 192.168.145.1:192.168.145.2 debug file /etc/ppp/options.xl2tpd.k4nz ipparam 189.34.59.128 /dev/pts/0

# pstree -a $(pidof xl2tpd)
xl2tpd -D -c /run/nm-l2tp-xl2tpd-7f15330c-10a0-4880-927f-3a1f815b397b.conf -C /run/nm-l2tp-xl2tpd-control-7f15330c-10a0-4880-927f-3a1f815b397b -p /run/nm-l2tp-xl2tpd-7f15330c-10a0-4880-927f-3a1f815b397b.pid
  └─pppd plugin pppol2tp.so pppol2tp 7 passive nodetach : file /run/nm-l2tp-ppp-options-7f15330c-10a0-4880-927f-3a1f815b397b
      └─2*[{pppd}]

package -> pppX -> tty -> xl2tpd -> ... -> xl2tpd -> tty -> pppX -> package

服务部署(LNS)

附加说明

这里的 L2TP 仅开启 PPP 认证,而未开启隧道认证,原因是 Network Manager L2TP 无法设置隧道认证密钥(或是我们未找到)。

该部署没有使用 IPSec 进行加密,所以该笔记不涉及 IPSec 相关内容。

环境的原因,我们不能保证配置 100% 成功。在我们第一次配置时,根本不懂 L2TP 协议的工作原理,所以并不清楚各个属性的含义。现在我们对 L2TP 的工作原理有了一定程度的了解,因此遇到问题,能够通过日志和抓包进行一定程序的排查。

环境概述

系统:CentOS Linux release 7.3.1611 (Core)
软件:xl2tpd 1.3.8

第一步、安装服务

# yum install -y xl2tpd.x86_64

# apt-get install xl2tpd

# apt-file list xl2tpd 
...
xl2tpd: /usr/bin/pfc
xl2tpd: /usr/sbin/xl2tpd
xl2tpd: /usr/sbin/xl2tpd-control
...

第二步、配置修改(xl2tpd.conf)

/etc/xl2tpd/xl2tpd.conf

[global]
listen-addr = 0.0.0.0
port = 1701
access control = yes
; auth file = /etc/xl2tpd/l2tp-secrets
; max retries = 5

debug avp = yes
debug network = yes
debug packet = yes
debug state = yes
debug tunnel = yes

; ipsec saref = no
; saref refinfo = 30

[lns default]
;;;
exclusive = no
ip range = 192.168.1.2 - 192.168.1.10
assign ip = yes
; (no) lac =
; hidden bit =
local ip = 192.169.1.1
; local ip range =
; length bit =
require chap = yes
require pap = no
require authentication = no
unix authentication = no
hostname = Your Hostname
ppp debug = yes
pass peer = yes
pppoptfile = /etc/ppp/options.xl2tpd
; call rws =
; tunnel rws =
; flow bits =
challenge = no
; rx bps =
; tx bps =

该配置文件的ip rangelocal ippppoptfile可能需要根据自己的情况修改。

指令challenge = no用于关闭L2TP认证。如果设置challenge = yes,则需要配置auth file指令来指定认证密钥。

第三步、配置修改(options.xl2tpd)

/etc/ppp/options.xl2tpd

+mschap-v2
ipcp-accept-local
ipcp-accept-remote

# 分配给 LAC 的 DNS 地址
ms-dns 8.8.8.8
ms-dns 8.8.4.4

noccp
auth
mtu 1280
mru 1280
proxyarp
lcp-echo-failure 4
lcp-echo-interval 30
connect-delay 5000
name EnterYourUsername

第四步、配置修改(chap-secrets)

/etc/ppp/chap-secrets

# Secrets for authentication using CHAP
# client        server  secret                  IP addresses
"username 01" "EnterYourUsername" "password 01" *
"username 02" "EnterYourUsername" "password 02" *

配置 chap-secrets 的 EnterYourUsername 要与 options.xl2tpd 的 name 保持相同;
该文件在创建连接时动态读取,所以xiu该其内容后,无需重启服务;

第五步、启动服务

systemctl start xl2tpd.service
systemctl enable xl2tpd.service

第六步、客户端连接

WIP

第七步、查看服务状态

# xl2tpd-control -d status-lns default
xl2tpd-control: command to be passed:
@/var/run/xl2tpd/xl2tpd-control-850849.out y default
xl2tpd-control: command response:
...

服务连接(LAC)

问题描述:我们现在需要通过 xl2tpd 程序来连接对端提供的 L2TP LNS 服务。

环境信息:对端提供 L2TP 服务,并且没有隧道认证,仅是最基本的 L2TP LNS 服务(也没有 IPSec 保护)。

#1 本地安装 xl2tpd 服务

yum install -y xl2tpd.x86_64

#2 修改配置文件:/etc/xl2tpd/xl2tpd.conf

[global]
debug avp = yes
debug network = yes
debug packet = yes
debug state = yes
debug tunnel = yes
max retries = 32

[lac <remoteHost>]
ppp debug = yes
lns = <remoteHostAddress>
pppoptfile = /etc/ppp/options.xl2tpd.remoteHost
length bit = yes

将上述配置的 <remoteHost> 需要替换成自己的值,就是个字符串,起标识作用,不改也没有问题。

配置文件中的 <remoteHostAddress> 要替换成远程主机的IP地址或者域名。

#3 修改配置文件:/etc/ppp/options.xl2tpd.remoteHost

ipcp-accept-local
ipcp-accept-remote
refuse-eap
require-chap
noccp
noauth
mtu 1280
mru 1280
noipdefault
defaultroute
usepeerdns
connect-delay 5000
name vpnUsername

上述配置中指令name的值vpnUsername需要替换成远程主机L2TP设置的用户名。

#4 修改配置文件:/etc/ppp/chap-secrets

"vpnUsername" * "enterUserPassword" *

上述配置中vpnUsername需要替换成与/etc/ppp/options.xl2tpd/remoteHost中name指令相同的值,它们两个之间是为了进行关联,以获取连接L2TP的密钥enterUserPassword

#5 启动服务

systemctl start xl2tpd.service
systemctl enable xl2tpd.service

#6 测试连接

在本地主机上,ping LNS 的地址以检查网络连通性。

常见问题处理

1)熟悉 L2TP 报文交互过程;
2)通过 tcpdump 抓包:tcpdump -n -i any port 1701

Getting L2TP No Authorization errors

For no obvious reason connections are being denied by xl2tpd without any apparent checking of CHAP credentials, and setting access control and/or require authentication to no in xl2tpd.conf does not alleviate the problem. Log messages are also somewhat terse:

	xl2tpd[xxxxx]: control_finish: Denied connection to unauthorized peer xxx.xxx.xxx.xxx
	xl2tpd[xxxxx]: Connection 40447 closed to xxx.xxx.xxx.xxx, port 1701 (No Authorization)

This was caused by changing [lns default] in xl2tpd.conf to use a name other than default. Easiest thing is to change it back.

control_finish: Denied connection to unauthorized peer

在 xl2tpd.conf 中,设置 access control = no 即可。

我们未找到关于 access control 的更多说明,手册里说的也很简单。

当网络重新连接后(或连接断开),能够自动重连

由于网络的问题可能会导致 LAC 离线,当网络恢复的时候希望它可以重新连接。

修改xl2tpd.conf文件,在LAC部分的redial、redial timeout、max redials可以解决这个问题。

使用autodial可以实现服务启动或者重启时自动连接。

Google: multiple l2tp clients behind nat -ipsec

More than one L2TP VPN connection from behind a NAT device fails with error 809 when TMG 2010 has been configured as a VPN Server
Multiple VPN connections behind NAT
L2TP VPN – Multiple connections from same WAN IP (NAT) don’t work simultaneously

[WIP] network thread select timeout

[SOLVED]Connecting to L2TP/IPSec VPN problem: pppd seems not starting
xl2tp + strongswan ipsec — xl2tp timeout

在 xl2tpd 的 log 中,存在如下错误:

Mar 11 18:07:25 laptop xl2tpd[24486]: network_thread: select timeout with max retries: 32 for tunnel: 64115

错误的意思就是“在最大32次重试之后,隧道64115的选择超时”。其中 max retries: 32 这个是配置文件中指定的,可不太可能是错误的原因。

首先,我们我们需要先了解以下 L2TP 的工作流程,然后通过抓包排查问题:

tcpdump -r xl2tpd.lns.pcap -n -i any -X -vvv 'port 1701'                        # 在LNS中执行的抓包命令

xl2tpd-control disconnect k4nz                                                  # 在LAC中执行的抓包命令
tcpdump -w xl2tpd.lac.pcap -n -i any -X -vvv 'port 1701'
xl2tpd-control connect k4nz
tcpdump -r xl2tpd.lac.pcap -n -i any -X -vvv 'port 1701'

参考文献

domain name system – How to resolve internet hostnames through ipsec/l2tpd vpn and a remote dns server? – Server Fault
/etc/ppp/chap-secrets File (System Administration Guide: Resource Management and Network Services)
GitHub – xelerance/xl2tpd: Official Xelerance fork of L2TPd
L2TP/IPSec VPN server setup
L2TPServer – Community Help Wiki
[SOLVED] suddenly no more l2tpd connection are allowed, but ipsec tunnel is still up
xL2TPd on Arch Linux and Ubuntu.
Set VPN server of L2TP/IPSec on Ubuntu 14.04 | Hongwei Qin’s Blog