OpenWRT の Wireguard によるサイト間VPNを確立させる前に、今回はまずメインルータの下にいる APモード のOpenWRT を Wireguard ゲートウェイに仕立て、Androidクライアントから接続できるようにします。
APモード(Dumb AP Mode)とは
WiFiのないNTT光ルータPR-S300SEを補完するために、GL-iNet社のトラベルルータGL-AR750S SlateをいわゆるAPモード(Dumb AP Mode)で使用しています。
OpenWRT上のネットワークインターフェイスは実質LANのみ、GL-AR750S上の物理ポートはWANを含め全てブリッジLANに属しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
config interface 'lan' option type 'bridge' option ifname 'eth0 eth0.1 eth0.2' option proto 'static' option ipaddr '192.168.1.250' option netmask '255.255.255.0' option gateway '192.168.1.1' option broadcast '192.168.1.255' option dns '192.168.1.1 8.8.8.8' option macaddr '##:##:##:##:##:##' config switch option name 'switch0' option reset '1' config switch_vlan option device 'switch0' option vlan '1' option ports '0t 1 2 3' option vid '1' config switch_vlan option device 'switch0' option vlan '2' option ports '0t' option vid '2' |
OpenWRT公式に解説のある「Dumb APの作り方」では、DNSやDHCP、Firewallも全て無効にしましょうとありますが、サービスは止めずにFirewallはLANゾーンのみ、DHCPはメインルータとは配布レンジを違えてひっそり残していました。
ルータではなくAPをVPNゲートウェイに
このネットワークへ外部からVPNアクセスする環境を構築するに当たり、光ルータにはそうした機能は一切ないことから、OpenWRTベースのこのWiFi APにVPNゲートウェイの役割を果たしてもらおうと思うのですが、
存外そうした構築例は少ないためか、公式フォーラムでも「できる」「いや、できない」が平行線を辿っていました。
Wireguardパッケージの導入
いずれはサイト間VPNを構築したいことから、以前から興味のあったWireguardベースのメッシュ型VPN、tailscaleを導入しようと思い、あらためて調べてみると無料版で接続できるサブネットは1つまで、という制限があり導入を断念しました(結局その後のプラン改定で、無料版でも無制限のサブネットを扱えるようになりましたが…)。
そこで今回は構築のしやすさと速度に定評のあるWireguardを採用したいと思います。
このGL-AR750Sは、OpenWRTをベースにGL-iNetがカスタマイズしたメーカー謹製ファームウェアで動いているので、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#uname -a Linux GL-AR750S_BA 4.9.120 #0 Thu Aug 16 07:51:15 2018 mips GNU/Linux # cat /etc/openwrt_release DISTRIB_ID='OpenWrt' DISTRIB_RELEASE='18.06.1' DISTRIB_REVISION='r7258-5eb055306f' DISTRIB_TARGET='ar71xx/nand' DISTRIB_ARCH='mips_24kc' DISTRIB_DESCRIPTION='OpenWrt 18.06.1 r7258-5eb055306f' DISTRIB_TAINTS='busybox override' # cat /etc/openwrt_version r7258-5eb055306f # cat /proc/cpuinfo system type : Qualcomm Atheros QCA956X ver 1 rev 0 machine : GL-AR750S processor : 0 cpu model : MIPS 74Kc V5.0 BogoMIPS : 385.84 wait instruction : yes microsecond timers : yes tlb_entries : 32 extra interrupt vector : yes hardware watchpoint : yes, count: 4, address/irw mask: [0x0ffc, 0x0ffc, 0x0ffb, 0x0ffb] isa : mips1 mips2 mips32r1 mips32r2 ASEs implemented : mips16 dsp dsp2 shadow register sets : 1 kscratch registers : 0 package : 0 core : 0 VCED exceptions : not available VCEI exceptions : not available |
元々ある程度のWireguard関連パッケージは既にインストール済。さらにLuCIから利用できるように、以下の2つのパッケージをインストールします。
- luci-app-wireguard
- luci-proto-wireguard
鍵の生成
ターミナルから次のコマンドで、Wireguardサーバの鍵ペアを生成します。
1 2 3 4 5 6 7 |
# cd /etc/config/ # wg genkey | tee privatekey | wg pubkey > publickey # chmod 600 privatekey # ls -l /etc/config/p*key -rw------- 1 root root 45 Apr 26 16:12 /etc/config/privatekey -rw-r--r-- 1 root root 45 Apr 26 16:12 /etc/config/publickey |
クライアントであるPeer用の鍵ペアもここで作りたい場合は、次の要領で。
1 2 3 4 5 6 |
# wg genkey > wgclient1.key # wg pubkey < wgclient1.key > wgclient1.pub # ls -l /etc/config/wgclient* -rw-r--r-- 1 root root 45 Apr 26 17:03 /etc/config/wgclient1.key -rw-r--r-- 1 root root 45 Apr 26 17:03 /etc/config/wgclient1.pub |
いずれにおいても秘密鍵、公開鍵のペアを生成したら秘密鍵は各自が大切に保管し、公開鍵を接続相手と交換し合うのがこのVPNの基本です。
インターフェイスの作成
再びWebGUIのLuCIに戻り、 Network >> Interfaces ページでWireguard VPNをプロトコルとするインターフェイスを新規作成します。
インターフェイスの一般設定では、先ほどターミナルで生成した秘密鍵の中身をPrivate Key欄へ貼付け、待ち受けUDPポートと、トンネル端のIPアドレスを設定します。
インターフェイスの詳細では、 Bring up on boot にチェックが入っていることを確認するのみ。
インターフェイスが属するファイアウォールゾーンは、新たに wgvpn と言うゾーンを当てました。
ここまで、インターフェイス関連の設定を抜粋すると、次の通りです。
1 2 3 4 5 6 |
config interface 'wgvpn' option proto 'wireguard' option delegate '0' option private_key 'OH##########################################' option listen_port '51820' list addresses '192.168.99.250/24' |
ファイアウォールゾーンの設定
Network >> Firewall ページで作成した wgvpn ゾーンの設定は以下の通りで、基本全て accept とし、マスカレードにはチェックを入れずにおきました。
設定ファイルの追記部は以下の通り。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
config zone option name 'wgvpn' option network 'wgvpn' option input 'ACCEPT' option output 'ACCEPT' option forward 'ACCEPT' config forwarding option dest 'lan' option src 'wgvpn' config forwarding option dest 'wgvpn' option src 'lan' |
メインルータの静的NATとルーティング設定
ここでGL-AR750Sの設定は一時中断してメインルータに移り、Wireguardが利用する 51820/UDP の静的NATを開けます。
そして、Wireguardのトンネルサブネット 192.168.99.0/24 へのトラフィックが迷子にならないよう、VPNゲートウェイであるGL-AR750Sへの静的ルーティングを設定します。
Androidクライアント
VPNクライアントにするAndroidスマートフォン ASUS Zenfone 8 Flip ZS672KSにWireguardクライアントをインストールしたら、接続プロファイルを 空白の状態から作成 します。
設定画面の上側は自身のWireguardインターフェイスに関するもの。適当な名前をつけ、生成された鍵ペアの公海鍵はOpenWRT側に渡す必要があるので、なんとかしてコピーします。

図14.Wireguardクライアントのインターフェイス
同じ画面の下半分はクライアント側から見たピア、つまりWireguardサーバの設定です。上述で生成したGL-AR750Sの公開鍵を貼付け、キープアライブを25秒に設定、エンドポイントはサーバ側のDDNSドメイン名の後ろにポート番号を入力します。

図15.Wireguardクライアントのピア設定
Allowed IPs 項は、全トラフィックをトンネル経由にするのであれば 0.0.0.0/0 を、接続先のLANのみにトンネルを利用するのであれば、LANサブネット 192.168.1.0/24 を入れます。
ゲートウェイにピアを登録
最後にクライアント情報をGL-AR750SのWireguardインターフェイスへ、ピアとして登録すれば完成です。
1 2 3 4 5 |
config wireguard_wgvpn option public_key 'gu##########################################' list allowed_ips '192.168.99.101/32 192.168.1.0/24' option persistent_keepalive '25' option description 'ZS672KS' |
疎通の確認
Wireguardクライアントから接続してみます(うまく繋がらない場合はデバッグログを確認しましょう)。
OpenWRT側は、LuCIの Status >> Wireguard ページで簡単な接続状態を確認することができます。
ターミナルからCLIベースなら wg コマンドから。
1 2 3 4 5 6 7 8 9 10 11 12 |
# wg show interface: wgvpn public key: jp########################################## private key: (hidden) listening port: 51820 peer: gu########################################## endpoint: ###.###.###.###:46292 allowed ips: 192.168.99.101/32 latest handshake: 1 hour, 29 minutes, 44 seconds ago transfer: 1.11 MiB received, 3.88 MiB sent persistent keepalive: every 25 seconds |
こうしてWireguardゲートウェイは完成したので、次回は別拠点とのサイト間VPNを確立へプロジェクトを進めます。