mattintosh note

どこかのエンジニアモドキの備忘録

自宅の Rasberry Pi に iPhone から VPN で接続する

Raspberry Pi にインストールしている Arch LinuxVPN サーバを構築したときのでメモ。

OpenVPN を使用して接続することも考えたが、iPhone 標準の機能で接続したかったので PPTP で接続することにした。

pptpd パッケージをインストールする。同時に pppd パッケージがインストールされる。

pacman -S pptpd

ネット上で検索してみると pptpd の設定は /etc/ppp/options.pptpd といったファイルで行ったりすることがあるようだが、今回インストールした pptpd のマニュアルを読んでみると /etc/pptpd.conf で設定をするらしい。

ひとまず /etc/pptpd.conf に払い出しの IP アドレスを設定する。

# Read man pptpd.conf, see samples in /usr/share/doc/pptpd
# and write your pptpd configuration here
localip  192.168.1.200
remoteip 192.168.1.201-210

/etc/ppp/chap-secrets に使用するユーザ情報を書き込む。pptpd について調べているときに「フィールドをタブで区切ると動作しない」というようなコメントもあったが、試してみたところ自分の環境ではタブで区切っても動作した。ここでの server は後に設定する /etc/ppp/optionsname と同じ名前にする。

# Secrets for authentication using CHAP
# client        server  secret                  IP addresses
foo             pptpd   bar                     *

パスワードを平文で書いているので root のみ読み書きできるようにしておく。

chmod 0600 /etc/ppp/chap-secrets

フォワーディングを有効に。再起動後も有効にするために後で /etc/sysctl.conf にも追記しておく。

sysctl -w net.ipv4.ip_forward=1

Firewall ルールを追加する。以下のコマンドは man 5 pptpd.conf に記載されている。

iptables --append INPUT --protocol 47 --jump ACCEPT
iptables --append INPUT --protocol tcp --match tcp --destination-port 1723 --jump ACCEPT

pptpd を自動起動するようにして起動する。

systemctl enable pptpd
systemctl start pptpd

自宅のルータの PPTP パススルーを有効にしておき、4G 回線を使用して iPhone から接続してみる。暗号化 none ではつながるが、Auto または Maximum を選択すると繋がらない。セキュリティ関係の設定は /etc/pptpd.conf ではなく /etc/ppp/options で設定するらしい。設定の詳細は man 8 pppd に記載されている。

セキュリティの設定で refuse-NAME となっているものは -NAMErequire-NAME となっているものは +NAME と書けるそうだ。mtu を少し小さく、deflate は 8 から 15 までの値を設定できるようなのでとりあえず 10 にしておいた。

--- options.orig 2015-03-02 09:55:56.671083963 +0900
+++ options      2015-03-05 00:16:54.844675136 +0900
@@ -10,8 +10,8 @@
 
 # Specify which DNS Servers the incoming Win95 or WinNT Connection should use
 # Two Servers can be remotely configured
-# ms-dns 192.168.1.1
-# ms-dns 192.168.1.2
+ms-dns 209.244.0.3
+ms-dns 209.244.0.4
 
 # Specify which WINS Servers the incoming connection Win95 or WinNT should use
 # ms-wins 192.168.1.50
@@ -78,7 +78,7 @@
 # minimum MRU value is 128.  The default MRU value is 1500.  A value of
 # 296 is recommended for slow links (40 bytes for TCP/IP header + 256
 # bytes of data).
-#mru 542
+mru 1280
 
 # Set the interface netmask to <n>, a 32 bit netmask in "decimal dot"
 # notation (e.g. 255.255.255.0).
@@ -183,7 +183,7 @@
 # requests a smaller value via MRU negotiation, pppd will request that
 # the kernel networking code send data packets of no more than n bytes
 # through the PPP network interface.
-#mtu <n>
+mtu 1280
 
 # Set the name of the local system for authentication purposes to <n>.
 # This is a privileged option. With this option, pppd will use lines in the
@@ -192,7 +192,7 @@
 # with the user option, <n> will be used as the name to send to the peer
 # when authenticating the local system to the peer. (Note that pppd does
 # not append the domain name to <n>.)
-#name <n>
+name pptpd
 
 # Enforce the use of the hostname as the name of the local system for
 # authentication purposes (overrides the name option).
@@ -349,4 +349,11 @@
 # uncomment the line below this if you use PPPoE
 #plugin /usr/lib/pppd/plugins/pppoe.so
 
+-chap
+-mschap
+-pap
++mschap-v2
++mppe-128
+deflate 10
+
 # ---<End of File>---

pptpd を再起動し、暗号化を maximum にした上で iPhone から VPN 接続すると無事に繋がった。SSH クライアントアプリでポートフォワーディングするよりも楽なのでいろいろ捗りそうだ。

Raspberry Pi 2 Model B (1)

Raspberry Pi 2 Model B (1)

Raspberry Piユーザーガイド 第2版

Raspberry Piユーザーガイド 第2版

Raspberry Pi 2 ラズベリー・パイ 超小型パソコン Pi 2 MODEL B

Raspberry Pi 2 ラズベリー・パイ 超小型パソコン Pi 2 MODEL B