1. 程式人生 > >在Ubuntu上部署L2TP服務

在Ubuntu上部署L2TP服務

現在流行的ipsec來自openswan,l2tp來自xl2tpd。本文將以這兩個軟體包進行講解如何配置。當提及openswan的時候就是指ipsec,當說到xl2tpd的時候就是指l2tp。希望讀者能夠明白。

1.搭建環境

  • Ubuntu 10.04
  • Kernel 2.6.32
  • Openswan 2.6.37
  • xl2tpd-1.2.5

/!\重要:筆者一開始在Ubuntu 11.04上進行實驗,Kernel 3.0,IPSec版本2.6.23。這裡有幾個嚴重問題:

  1. openswan尚未對3.0核心提供良好支援,某些情況下用iPhone或者win7客戶端連線會導致核心奔潰!
  2. 2.6.23版本的openswan尚有諸多BUG,即使在Kernel 2.6的環境下,也無法保證100%連線成功。

因此,請至少使用openswan 2.6.37的版本,當前最新版本是2.6.38。原始碼可在這裡下載:http://download.openswan.org/openswan/,之後需要通過編譯完成安裝。對於openswan的安裝我們將會提供兩種不同的方法。

2.通過新增軟體庫安裝openswan

apt-get install python-software-properties
add-apt-repository ppa:openswan/ppa
apt-get update
apt-get install openswan

如果出現Do you want to create a RSA public/private keypair for this host?

。我們選擇No,這種模式的IPSec通過證書進行驗證,而非PSK。

3.通過編譯原始碼包安裝openswan

獲取最新軟體包:

wget http://download.openswan.org/openswan/openswan-2.6.38.tar.gz
tar -xzf openswan-2.6.38.tar.gz

編譯不同的版本可能會依賴不同的庫檔案,因此如果你正在編譯更高版本的openswan,請務必先閱讀README檔案。這裡我們安裝如下庫檔案:

apt-get install libgmp3-dev flex bison

安裝這些庫會佔用大量空間,而且在伺服器上編譯二進位制檔案通常不是明智之舉!但是有時候伺服器架構會與你的測試機完全不同,因此可能你仍然需要在伺服器上做這些工作。接下來我們開始編譯:

make programs install

如果一切順利,編譯工作完成後配置檔案及服務會自動安裝到合適的地方,也就是和通過apt-get安裝沒什麼兩樣了。

4.安裝xl2tpd

Ubuntu中xl2tpd的版本目前沒有發現嚴重問題。因此安裝起來也比較方便:

apt-get install xl2tpd

三、開始配置

配置工作分為三部分:IPSec配置、L2TP配置、PPP配置

1.IPSec配置

# /etc/ipsec.conf

config setup
    nat_traversal=yes
    virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:25.0.0.0/8,%v6:fd00::/8,%v6:fe80::/10
    oe=off
    protostack=netkey

conn L2TP-PSK-NAT
        rightsubnet=vhost:%priv
        also=L2TP-PSK-noNAT

conn L2TP-PSK-noNAT
        authby=secret
        pfs=no
        auto=add
        keyingtries=3
        # we cannot rekey for %any, let client rekey
        rekey=no
        # Apple iOS doesn't send delete notify so we need dead peer detection
        # to detect vanishing clients
        dpddelay=10
        dpdtimeout=90
        dpdaction=clear
        # Set ikelifetime and keylife to same defaults windows has
        ikelifetime=8h
        keylife=1h
        # l2tp-over-ipsec is transport mode
        type=transport
        #
        left=$IP
        #
        # For updated Windows 2000/XP clients,
        # to support old clients as well, use leftprotoport=17/%any
        leftprotoport=17/1701
        #
        # The remote user.
        #
        right=%any
        # Using the magic port of "%any" means "any one single port". This is
        # a work around required for Apple OSX clients that use a randomly
        # high port.
        rightprotoport=17/%any

conn passthrough-for-non-l2tp
        type=passthrough
        left=$IP
        leftnexthop=$GATEWAY
        right=0.0.0.0
        rightsubnet=0.0.0.0/0
        auto=route

其中,$IP改成你主機乙太網卡的IP,$GATEWAY改成該IP所在網段的閘道器地址,下同。對於下面的金鑰檔案,你需要將金鑰引在引號中。

# /etc/ipsec.secrets
$IP %any: PSK "Your Preshared Key Here"

最後我們需要修改某些網路策略,讓ipsec正常執行:

for each in /proc/sys/net/ipv4/conf/*
do
    echo 0 > $each/accept_redirects
    echo 0 > $each/send_redirects
done

2.L2TP配置

# /etc/xl2tpd/xl2tpd.conf

[global]
ipsec saref = yes

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

3.PPP配置

# /etc/ppp/options.xl2tpd

refuse-mschap-v2
refuse-mschap
ms-dns 8.8.8.8
ms-dns 8.8.4.4
asyncmap 0
auth
lock
hide-password
local
#debug
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
mtu 1404
mru 1404

5.啟用轉發

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

對於OpenVZ的主機,可能不支援MASQUERADE,此時需要使用SNAT:

iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source $IP

其中$IP就是你主機的eth0介面的IP地址。

四、檢查ipsec是否工作正常

我們通過執行ipsec verify命令進行驗證ipsec服務是否工作正常:

錯誤1:pluto is running [FAILED]

Checking your system to see if IPsec got installed and started correctly:
Version check and ipsec on-path                                 [OK]
Linux Openswan U2.6.38/K(no kernel code presently loaded)
Checking for IPsec support in kernel                            [FAILED]
 SAref kernel support                                           [N/A]
Checking that pluto is running                                  [FAILED]
  whack: Pluto is not running (no "/var/run/pluto/pluto.ctl")
Checking for 'ip' command                                       [OK]
Checking /bin/sh is not /bin/dash                               [WARNING]
Checking for 'iptables' command                                 [OK]
Opportunistic Encryption Support                                [DISABLED]

這是因為你還沒有啟動ipsec服務!

/etc/init.d/ipsec start

錯誤2:NETKEY: Testing XFRM related proc values [FAILED]

Checking your system to see if IPsec got installed and started correctly:
Version check and ipsec on-path                                 [OK]
Linux Openswan U2.6.38/K2.6.32-38-generic-pae (netkey)
Checking for IPsec support in kernel                            [OK]
 SAref kernel support                                           [N/A]
 NETKEY:  Testing XFRM related proc values                      [FAILED]

  Please disable /proc/sys/net/ipv4/conf/*/send_redirects
  or NETKEY will cause the sending of bogus ICMP redirects!

        [FAILED]

  Please disable /proc/sys/net/ipv4/conf/*/accept_redirects
  or NETKEY will accept bogus ICMP redirects!

        [OK]
Checking that pluto is running                                  [OK]
 Pluto listening for IKE on udp 500                             [OK]
 Pluto listening for NAT-T on udp 4500                          [OK]
Checking for 'ip' command                                       [OK]
Checking /bin/sh is not /bin/dash                               [WARNING]
Checking for 'iptables' command                                 [OK]
Opportunistic Encryption Support                                [DISABLED]

你可能忘記修改網路策略:

for each in /proc/sys/net/ipv4/conf/*
do
    echo 0 > $each/accept_redirects
    echo 0 > $each/send_redirects
done

錯誤3:Pluto listening for IKE on udp 500 [FAILED]

Pluto listening for IKE on udp 500                              [FAILED]
  Cannot execute command "lsof -i UDP:500": No such file or directory
Pluto listening for NAT-T on udp 4500                           [FAILED]
  Cannot execute command "lsof -i UDP:4500": No such file or directory

那麼你需要安裝lsof:

apt-get install lsof

五、測試與除錯

1.啟用測試使用者

編輯/etc/ppp/chap-secrets檔案,加入如下行:

# Secrets for authentication using CHAP
# client        server  secret                  IP addresses
guest           l2tpd   password                *

該行建立一個使用者,使用者名稱為guest,密碼為password(明文)
server欄位與options.xl2tpd配置檔案中的name屬性對應。還可以星號(*)代替,表示不限呼叫服務。
IP address表示來源IP,這裡星號(*)表示接受所有連線。
對來源IP的過濾應該由iptables防火牆來完成。讀者可以查閱本部落格關於iptables的相關文章進行設定。

2.連線

現在就可以嘗試連線,如果連線失敗,需要通過檢視日誌資訊排錯。

3.檢視日誌

ipsec日誌記錄在/var/log/auth中,如果發現該行:

STATE_MAIN_R3: sent MR3, ISAKMP SA established

則表示IPSec認證或連線沒有問題,可以檢查其他日誌。但是有時候即使出現established,也會在稍後的行中出現錯誤資訊,這是需要使用者留意的地方。
xl2tpd和pppd的日誌記錄在/var/log/syslog中。