LoginSignup
10
10

More than 3 years have passed since last update.

SoftetherとVPSで自宅ネットワークにアクセスする

Last updated at Posted at 2019-04-21

はじめに

備忘録です
何番煎じかはわからない記事
本記事は,引越ししたところ,自宅にグローバルIPが付与されない環境だったので,怒りに任せてVPSを借りてSoftether VPNを構築したお話.

目標

  • グローバルIPが付与されない自宅ローカルネットワークに外からアクセスする
  • プロトコルはL2TP/IPsec
    • SSTPで良いならSoftether標準機能のVPN Azureを使えば秒で設定可能

システム概要

要約すると 10.11 SecureNAT 機能を用いた権限不要のリモートアクセス を実現したい

VPS側(固定IP)にSoftether VPN Serverをインストール.
自宅ネットワークに参加しているラズパイくんにVPN Bridgeをインストールし,VPSのVPN serverにカスケード接続.

Linuxの制約のため,ラズパイのVPN Bridgeはtapデバイスに接続し,物理NICとブリッジをかけ,自分自身との通信ができるよう設定する.

SecureNATは使用しない

環境

  • VPS: Webarena (CentOS release 6.10 (Final))
  • Raspberry pi 3 model B+
    • 自宅側に置くVPN Bridgeをインストール

VPSの設定

  • Softether VPN Serverをインストール

公式サイト通りインストールを進める

  • VPN Serverの自動起動を設定

cf.) さくらVPS上にSoftEtherを使ってVPNサーバ(L2TP/IPSec and OpenVPN)を立てる方法

/etc/init.d/vpnserver
#!/bin/sh
# chkconfig: 2345 99 01
# description: SoftEther VPN Server
DAEMON=/usr/local/vpnserver/vpnserver
LOCK=/var/lock/subsys/vpnserver
test -x $DAEMON || exit 0
case "$1" in
start)
$DAEMON start
touch $LOCK
;;
stop)
$DAEMON stop
rm $LOCK
;;
restart)
$DAEMON stop
sleep 3
$DAEMON start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
chmod +x /etc/init.d/vpnserver

CentOS7以降は以下のように設定ファイルを用意する

/etc/systemd/system/softethervpn.service
[Unit]
Description=Softether VPN Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/vpnserver/vpnserver start
ExecStop=/usr/local/vpnserver/vpnserver stop

[Install]
WantedBy=multi-user.target
systemctl enable softethervpn
  • ファイアウォールの設定

ESPはTCP50番ポート

/etc/sysconfig/iptables
-A INPUT -p udp -m udp --dport 500 -j ACCEPT
-A INPUT -p udp -m udp --dport 4500 -j ACCEPT
-A INPUT -p udp -m udp --dport 1701 -j ACCEPT
-A INPUT -p esp -j ACCEPT
-A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
-A FORWARD -o ppp+ -j ACCEPT
-A FORWARD -i ppp+ -j ACCEPT
[root@xxxx ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere            udp dpt:isakmp 
ACCEPT     udp  --  anywhere             anywhere            udp dpt:ipsec-nat-t 
ACCEPT     udp  --  anywhere             anywhere            udp dpt:l2tp 
ACCEPT     esp  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

Raspberry piの設定

  • VPN Bridgeのインストール

公式サイト参照

  • ブリッジの設定
/etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

auto br0
iface br0 inet dhcp
    bridge_ports eth0
  • VPN Bridgeの自動起動
/usr/local/vpnbridge.d/vpnbridge
#!/bin/sh
# chkconfig: 2345 99 01
# description: SoftEther VPN Bridge
DAEMON=/usr/local/vpnbridge/vpnbridge
LOCK=/var/lock/subsys/vpnbridge
modprobe tun
test -x $DAEMON || exit 0
case "$1" in
start)
$DAEMON start
touch $LOCK

sleep 3
brctl addif br0 tap_vpnbr
;;
stop)
$DAEMON stop
rm $LOCK
;;
restart)
$DAEMON stop
sleep 3
$DAEMON start
sleep 3
brctl addif br0 tap_vpnbr
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
/etc/systemd/system/vpnbridge.service
[Unit]
Description=Softether VPN Bridge
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/vpnbridge.d/vpnbridge start
ExecStop=/usr/local/vpnbridge.d/vpnbridge stop

[Install]
WantedBy=multi-user.target

Softether VPN ServerとVPN Bridgeの設定

vpnsmgr.exeから設定

  • Serverに接続用のユーザとカスケード接続用のユーザを用意
  • BridgeからServerへカスケード接続する
  • Bridge側はローカルブリッジ設定からtapデバイス用意.

追記・修正

  1. 初出時iptablesの設定方法に誤りがあったため,修正
10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10