SoftEtherによるVPNの導入

Posted on November 14, 2021 by nobiruwa

VPNサーバー (Raspberry Pi 4) のセットアップ

「SoftEther VPN」のSecureNATが気になり、自宅で常に稼動しているRaspberry Pi 4をVPNサーバー化するセットアップを行いました。

SecureNAT機能について

SecureNAT機能について理解するために、公式サイトの3.7 仮想 NAT および仮想 DHCP サーバーと、VPNFAQ036. SecureNAT の動作モードにはどのような違いがありますか。を見る必要がありました。

FAQにあるように、SecureNAT起動時にDHCPサーバーへACK要求を行います。

利用モードの選択

SecureNAT は仮想 NAT 機能の利用時に、ホスト OS に接続されたイーサネットインターフェイスと Raw IP ソケットで、 DHCP 要求を送信してみます。この DHCP 要求で IP アドレスを取得できたら、インターネット上の Web サーバーの名前解決が DNS で行えることと、解決された IP アドレスへの HTTP での接続が可能であることを確認します。全てに成功した場合のみ、そのインターフェイスが仮想 NAT の WAN 側として使用されます。イーサネットインターフェイスでの通信に成功した場合はカーネルモード SecureNAT が使用され、Raw IP ソケットでの通信に成功した場合は Raw IP モード SecureNAT が使用されます。いずれも成功しなかった場合はユーザモード SecureNAT が使用されます。

VPNサーバーのログ /var/log/softether/server_log/vpn_yyyymmdd.log に結果が記録されます。

yyyy-MM-dd HH:mm:ss.fff [HUB "vpn"] SecureNAT: It has been detected that the Kernel-mode NAT for SecureNAT can be run on the interface "eth0". The Kernel-mode NAT is starting. The TCP, UDP and ICMP NAT processings will be performed with high-performance via Kernel-Mode hereafter. The parameters of Kernel-mode NAT: IP Address = "<払い出されたIPアドレス>", Subnet Mask = "255.255.255.0", Default Gateway = "<デフォルトゲートウェイのIPアドレス>", Broadcast Address = "<ブロードキャストのIPアドレス>", Virtual MAC Address: "<仮想MACアドレス>", DHCP Server Address: "<DHCPサーバーのIPアドレス>", DNS Server Address: "<DNSサーバーのIPアドレス>"

DHCPサーバーのログを見ると、Virtual MAC Addressの仮想MACアドレスにIPアドレスがリースされていました。

必要なパッケージのインストール

# apt install softether-common softether-vpnserver softether-vpncmd

softether-vpnserver.serviceが自動起動されました。

なお、VPN Bridge接続をする場合はsoftether-vpnbridgeも合わせてインストールする必要があります。

ファイアウォールでのポート開放

VPN接続をするために、VPNサーバーの443/TCPポートへの接続を許可します。

Allow web traffic in iptables software firewall - rackspaceを参考にしました。

# iptables -I INPUT -p tcp -m tcp --dport 443 -j ACCEPT
# iptables-save

ルータの静的NAPT設定 (PR-500KI)

続いて、WAN側からVPNサーバーの443/TCPポートへ接続できるよう、ルータに静的NAPTによるルーティングを設定します。

自宅ではPR-500KIを使っており、この機器では、機器ごとに利用可能なWAN側ポートが制限されております。設定にあたってはv6プラスでポートを開放する方法を参考にしました。

Raspberry PiにSSH接続し、ルータ設定用にインストールしておいたWebブラウザ(midori)を使って配信済事業者ソフトウェア一覧を開き、IPv4設定をクリックします。

IPv4設定には利用可能ポートが列挙されており、静的NAPT設定に利用可能なポート番号を2つ選びます。

1つはSSL通信のためのポートです。

もう1つはOpenVPN接続のポートです。

仮想HUBの作成

VPNサーバーの操作をvpncmdから行ってみました。

まずはVPNサーバーの管理パスワードの設定と仮想HUBの作成です。 仮想HUBの名前だけでなく仮想HUBのパスワードもここで決定します。

# vpncmd
vpncmd command - SoftEther VPN Command Line Management Utility Developer Edition
SoftEther VPN Command Line Management Utility (vpncmd command)
Developer Edition
Version 5.01 Build 9674   (English)
Compiled 2020/12/03 09:35:09 by Unknown at Unknown
Copyright (c) all contributors on SoftEther VPN project in GitHub.
Copyright (c) Daiyuu Nobori, SoftEther Project at University of Tsukuba, and SoftEther Corporation.
All rights reserved.
5
By using vpncmd program, the following can be achieved.

1. Management of VPN Server or VPN Bridge
2. Management of VPN Client
3. Use of VPN Tools (certificate creation and Network Traffic Speed Test Tool)

Select 1, 2 or 3: 1 # 1を入力してEnter

Specify the host name or IP address of the computer that the destination VPN Server or VPN Bridge is operating on.
By specifying according to the format 'host name:port number', you can also specify the port number.
(When the port number is unspecified, 443 is used.)
If nothing is input and the Enter key is pressed, the connection will be made to the port number 443 of localhost (this computer).
Hostname of IP Address of Destination: # Enter

If connecting to the server by Virtual Hub Admin Mode, please input the Virtual Hub name.
If connecting by server admin mode, please press Enter without inputting anything.
Specify Virtual Hub Name: # Enter
Connection has been established with VPN Server "localhost" (port 443).

You have administrator privileges for the entire VPN Server.
VPN Server>ServerPasswordSet
ServerPasswordSet command - Set VPN Server Administrator Password
Please enter the password. To cancel press the Ctrl+D key.

Password: ****** # VPNサーバーの管理パスワードを入力してEnter
Confirm input: ****** # VPNサーバーの管理パスワードを再度入力してEnter

The command completed successfully.
VPN Server>HubCreate # 仮想HUBを作成するためにHubCreateコマンドを実行
HubCreate command - Create New Virtual Hub
Name of Virtual Hub to be created: vpn # 仮想HUB名に与える名前を入力してEnter

Please enter the password. To cancel press the Ctrl+D key.

Password: ****** # 仮想HUBのパスワードを入力してEnter
Confirm input: ****** # 仮想HUBのパスワードを再度入力してEnter

The command completed successfully.
VPN Server>exit

次に仮想HUBのSecureNATを有効にします。

# vpncmd
vpncmd command - SoftEther VPN Command Line Management Utility Developer Edition
SoftEther VPN Command Line Management Utility (vpncmd command)
Developer Edition
Version 5.01 Build 9674   (English)
Compiled 2020/12/03 09:35:09 by Unknown at Unknown
Copyright (c) all contributors on SoftEther VPN project in GitHub.
Copyright (c) Daiyuu Nobori, SoftEther Project at University of Tsukuba, and SoftEther Corporation.
All rights reserved.

By using vpncmd program, the following can be achieved.

1. Management of VPN Server or VPN Bridge
2. Management of VPN Client
3. Use of VPN Tools (certificate creation and Network Traffic Speed Test Tool)

Select 1, 2 or 3: 1 # 1を選択してEnter

Specify the host name or IP address of the computer that the destination VPN Server or VPN Bridge is operating on.
By specifying according to the format 'host name:port number', you can also specify the port number.
(When the port number is unspecified, 443 is used.)
If nothing is input and the Enter key is pressed, the connection will be made to the port number 443 of localhost (this computer).
Hostname of IP Address of Destination: # Enter

If connecting to the server by Virtual Hub Admin Mode, please input the Virtual Hub name.
If connecting by server admin mode, please press Enter without inputting anything.
Specify Virtual Hub Name: vpn # 仮想HUBの名前を入力してEnter
Password: ********* # 仮想HUBのパスワードを入力してEnter

Connection has been established with VPN Server "localhost" (port 443).

You have administrator privileges for Virtual Hub 'vpn' on the VPN Server.

VPN Server/vpn>SecureNATEnable
SecureNatEnable command - Enable the Virtual NAT and DHCP Server Function (SecureNat Function)
The command completed successfully.

VPN Server/vpn>exit

外部からSoftEther VPN サーバー管理マネージャで設定を行う

次はインターネットからVPNサーバーに接続して残りの設定を行ってみます。

接続の作成

SoftEther VPN サーバー管理マネージャウィンドウから[新しい接続設定]ボタンを選択します。

新しい接続の作成ダイアログでは以下のように設定しました。

[OK]ボタンを押すとSoftEther VPN Server への接続設定ダイアログに接続設定が追加されました。

VPN接続ユーザーの作成

仮想HUBのVPN接続ユーザーを作成します。

SoftEther VPN サーバー管理マネージャウィンドウで接続設定自宅を選択した状態で[接続]ボタンを押して接続に成功すると、自宅 - SoftEther VPN サーバー管理マネージャダイアログが開きます。

仮想HUBvpnを選択した状態で[仮想 HUB の管理]ボタンを押して、仮想 HUB の管理 - vpnダイアログを開きます。

[ユーザーの管理]ボタンを押して、ユーザーの管理ダイアログを開きます。

[新規作成]ボタンを押して、ユーザーの新規作成ダイアログを開きます。グループ参加なし、パスワード認証を行うユーザーを作成します。

[OK]ボタンを押すと、ユーザーの管理ダイアログにユーザーが追加されます。

IPsec/L2TPの設定

IPsec/L2TP自体は使用しませんが、後述するOpenVPNのための設定を行います。

自宅 - SoftEther VPN サーバー管理マネージャダイアログで[IPsec / L2TP 設定]ボタンを押して、IPsec / L2TP / L2TPv3 設定ダイアログを開きます。

接続時のユーザー名で仮想 HUB 名が省略された場合に接続する仮想 HUB の選択vpnを選択します。

[OK]ボタンを押してIPsec / L2TP / L2TPv3 設定ダイアログを閉じます。

OpenVPNの設定

AndroidではIPsec/L2TPは使えずOpenVPNを使用します。そのための設定を行います。

自宅 - SoftEther VPN サーバー管理マネージャダイアログで[OpenVPN / MS-SSTP 設定]ボタンを押して、OpenVPN / MS-SSTP 設定ダイアログを開きます。

[OK]ボタンを押してOpenVPN / MS-SSTP 設定ダイアログを閉じます。

VPNクライアントのセットアップ (Windows)

SoftEther VPN クライアント接続マネージャを使って、VPNサーバーに接続します。

仮想VLANカードの作成

[新しい接続設定の作成]を選択すると、初めに新しい仮想 LAN カードの作成ダイアログが表示されます。仮想 LAN カードの名前に適当な名称(例: VPN)を入力し[OK]ボタンを押します。

新しい接続設定の作成

[新しい接続設定の作成]を選択して新しい接続設定のプロパティダイアログを開きます。

VPNサーバーの設定に合わせて以下の通り入力します。

[OK]ボタンを押すと、SoftEther VPN クライアント接続マネージャウィンドウに接続設定が追加されました。

接続 (Windows)

SoftEther VPN クライアント接続マネージャウィンドウで接続設定をダブルクリックすると接続が試行されます。接続に成功すると状態接続完了と表示されます。

接続 (Android)

OpenVPN設定ファイルの作成

SoftEther VPN サーバー管理マネージャOpenVPN / MS-SSTP 設定ダイアログからダウンロードしたzipファイルから<VPNサーバーホスト名>_openvpn_remote_access_l3.ovpnを取り出し、以下のように変更します。

@@ -60,7 +60,7 @@
 #
 # Specify either 'proto tcp' or 'proto udp'.
 
-proto udp
+proto tcp
 
 
 ###############################################################################
@@ -86,7 +86,7 @@
 #       the Dynamic DNS hostname, replace it to either IP address or
 #       other domain's hostname.
 
-remote <VPNサーバーの管理番号>.v4.softether.net 1194
+remote <VPNサーバーの管理番号>.v4.softether.net <静的NAPT設定でOpenVPN通信用に割り当てたWAN側ポート番号>
 
 
 ###############################################################################

Android端末のOpenVPNセットアップ

<VPNサーバーホスト名>_openvpn_remote_access_l3.ovpnをAndroid端末のストレージにコピーしておきます。

AndroidOpenVPNクライアントをインストールします。

Import Profile<VPNサーバーホスト名>_openvpn_remote_access_l3.ovpnを選択して[IMPORT]ボタンを押します。

Usernameには仮想HUBのユーザー名称を入力します。Save passwordチェックボックスを有効にして、Passwordにパスワードを入力して、[ADD]ボタンを押します。

インポートしたプロファイルをタップすると接続が試行され、成功すればVPN接続が開始されます。

Linux端末

VPNサーバーを動かしているRaspberry Pi 4もVPNクライアントとしてVPN接続することで、Raspberry Pi 4のWebサーバーにも接続できるようにします。

試行錯誤を繰り返したので順序は正しくない可能性があります。

まずは必要なパッケージをインストールします。

# apt install softether-vpnclient

vpncmdを使って(localhostの)VPNクライアントをセットアップします。

仮想NICvpn0と仮想HUBvpnを接続するアカウントvpnconnを作成します。

$ vpncmd /client
Hostname of IP Address of Destination: # Enter

Connected to VPN Client "localhost".

VPN Client>NicCreate vpn0
NicCreate command - Create New Virtual Network Adapter
The command completed successfully.

VPN Client>AccountCreate vpnconn /SERVER:<VPNサーバーのIPアドレス>:443 /HUB:vpn /USERNAME:<仮想HUBのユーザー名称> /NICNAME:vpn0
AccountCreate command - Create New VPN Connection Setting
The command completed successfully.

VPN Client>NicList
NicList command - Get List of Virtual Network Adapters
Item                        |Value
----------------------------+-----------------------------------
Virtual Network Adapter Name|vpn0
Status                      |Enabled
MAC Address                 |5EE40FF922AC
Version                     |Version 5.01 Build 9674   (English)
The command completed successfully.

作成したアカウントが仮想HUBvpnに作成したユーザーを用いて接続すよう、ユーザー名とパスワードを設定します。

VPN Client>AccountUsernameSet vpnconn
AccountUsernameSet command - Set User Name of User to Use Connection of VPN Connection Setting
Connecting User Name: <ユーザー名> # `ユーザーの管理`で作成したユーザーのユーザー名を入力

The command completed successfully.

VPN Client>AccountPasswordSet vpnconn
AccountPasswordSet command - Set User Authentication Type of VPN Connection Setting to Password Authentication
Please enter the password. To cancel press the Ctrl+D key.

Password: ****** # `ユーザーの管理`で作成したユーザーのパスワードを入力
Confirm input: ****** # `ユーザーの管理`で作成したユーザーのパスワードを入力


Specify standard or radius: standard

The command completed successfully.
VPN Client>exit

vpncmdで接続します。

VPN Client>AccountConnect vpnconn
AccountConnect command - Start Connection to VPN Server using VPN Connection Setting
The command completed successfully.

AccountStatusGetコマンドでアカウントの状態を確認してください。

VPN Client>AccountStatusGet vpnconn
AccountStatusGet command - Get Current VPN Connection Setting Status
Item                                      |Value
------------------------------------------+-------------------------------------------------------------
VPN Connection Setting Name               |vpnconn
Session Status                            |Connection Completed (Session Established)
VLAN ID                                   |-
Server Name                               |<IPアドレス>
Port Number                               |TCP Port 443
Server Product Name                       |SoftEther VPN Server Developer Edition (32 bit) (Open Source)
...[snip]...
The command completed successfully.

セッションが確立していない場合は、AccountCreateAccountUsernameSetAccountPasswordSetの設定を再度実行してください。

また、クライアント側のログ/var/log/softether/client_log/client_YYYYmmdd.logと、サーバー側のログ/var/log/softether/server_log/vpn_YYYYmmdd.logに接続失敗の理由が記載されているかを確認してください。

AccountStatusGetコマンドでセッションが確立されていることを確認したら、最後に仮想DHCPサーバーからIPアドレスを取得するためにdhcpcdコマンドを実行してください。

# dhcpcd vpn_vpn0 # サーバー用途の場合は静的割り当てのほうがよい

ただし、Raspberry Pi 4はサーバー用途であるため、dhcpcdコマンド(dhcpcd-baseパッケージが必要です)を実行するかわりにIPアドレスの静的割り当てとルーティング設定を行います。

仮想DHCPサーバーの管理外のIPアドレスを割り当てます。

# ip addr add 192.168.30.2/24 dev vpn_vpn0
# ip route add 192.168.30.0/24 via 192.168.30.2 dev vpn_vpn0

Raspberry Pi 4でのVPN接続自動化

Raspbery Pi 4の起動時、前節のVPN接続が自動で行われるようにします。

/home/pi/bin/vpn0-start.sh/home/pi/bin/vpn0-stop.sh/home/pi/.config/systemd/system/user/vpn0.serviceを作成します。内容はそれぞれ以下の通りです。

/home/pi/bin/vpn0-start.sh

#!/usr/bin/env bash

# SoftEther
vpncmd /client localhost /cmd AccountConnect vpnconn

# Network
DEVICE_NAME=vpn_vpn0
ADDRESS=192.168.30.2
MASK=24 
NETWORK_ADDRESS=192.168.30.0/24

sudo ip addr add ${ADDRESS}/${MASK} dev ${DEVICE_NAME}

sudo ip route add ${NETWORK_ADDRESS} via ${ADDRESS} dev ${DEVICE_NAME}

exit 0

/home/pi/bin/vpn0-stop.sh

#!/usr/bin/env bash

# Network
DEVICE_NAME=vpn_vpn0
ADDRESS=192.168.30.2
MASK=24
NETWORK_ADDRESS=192.168.30.0/24

routes=`ip route | egrep ${DEVICE_NAME}`

while IFS= read -r line
do
  if [[ $line =~ ([0-9\.\/]+) ]]; then
    network_address=${BASH_REMATCH[1]}
    echo delete route ${network_address}
    sudo ip route delete ${network_address}
  fi
done <<< "$routes"

sudo ip addr del ${ADDRESS}/${MASK} dev ${DEVICE_NAME}

# SoftEther
vpncmd /client localhost /cmd AccountDisconnect vpnconn

exit 0

/home/pi/.config/systemd/system/user/vpn0.service

[Unit]
Description=assign a static ip address to the softether client.
After=softether-vpnserver.service softether-vpnclient.service

[Service]
Type=oneshot
ExecStart=/home/pi/bin/vpn0-start.sh
ExecStop=/home/pi/bin/vpn0-stop.sh
TimeoutStopSec=5
RemainAfterExit=yes

[Install]
WantedBy=default.target

サービスの有効化

systemctl --user daemon-reload
systemctl --user enable vpn0.service

参考文献