ポート開放に UPnP を使用しないといけない状況になったので、OpenWrt のルーターに miniupnpd をインストールして、特定の IP にのみ UPnP の利用を許可するようにしました。
コンテンツ
実行環境
- ルーター
- Linksys E8450
- ファームウェア
- OpenWrt 24.10.2
パッケージ | バージョン |
---|---|
miniupnpd-nftables | 2022-08-31-68c8ec50-1 |
必要なパッケージをインストール
- ルーターの Web コンソールへアクセス
- メニューの System -> Software をクリック
- “Update lists…” をクリック
- Filter に “miniupnpd” と入力
- リストから “miniupnpd-nftables” をインストール
設定を編集
以下がインストール時に作成される設定ファイルです。/etc/config/upnpd
config upnpd config
option enabled 0
option enable_natpmp 1
option enable_upnp 1
option secure_mode 1
option log_output 0
option download 1024
option upload 512
#by default, looked up dynamically from ubus
# option external_iface wan
option internal_iface lan
option port 5000
option upnp_lease_file /var/run/miniupnpd.leases
option igdv1 1
config perm_rule
option action allow
option ext_ports 1024-65535
option int_addr 0.0.0.0/0 # Does not override secure_mode
option int_ports 1024-65535
option comment "Allow high ports"
config perm_rule
option action deny
option ext_ports 0-65535
option int_addr 0.0.0.0/0
option int_ports 0-65535
option comment "Default deny"
option enabled を変更して UPnP を有効にします。
config upnpd config
option enabled 1
このままではルーターに接続されたすべてのデバイスが UPnP を使用できてしまうので、最初の perm_rule セクションを以下の様に変更します。この場合 192.168.1.101 にのみ UPnP の使用を許可しています。
config perm_rule
option action allow
option ext_ports 1024-65535
option int_addr 192.168.1.101/32
option int_ports 1024-65535
option comment "Allow high ports"
設定ファイルを変更後、以下のコマンドで miniupnpd を有効化します。
/etc/init.d/miniupnpd restart
/etc/init.d/miniupnpd enable
/etc/init.d/miniupnpd restart 実行時に以下のメッセージが表示されます。
Automatically including '/usr/share/nftables.d/table-post/20-miniupnpd.nft'
Automatically including '/usr/share/nftables.d/chain-post/dstnat/20-miniupnpd.nft'
Automatically including '/usr/share/nftables.d/chain-post/forward/20-miniupnpd.nft'
Automatically including '/usr/share/nftables.d/chain-post/srcnat/20-miniupnpd.nft'
動作しているか確認
Windows マシンで PowerShell と UPnPCJ を使って動作確認をします。一時的にファイアウォールをオフにしておきます。
以下のコマンドを PowerShell で実行してエコーサーバーを立てます。
$port = 2222
$listener = [System.Net.Sockets.TcpListener]::new([Net.IPAddress]::Any, $port)
$listener.Start()
Write-Host "Listening on TCP $port ..."
while ($true) {
$client = $listener.AcceptTcpClient()
$stream = $client.GetStream()
$reader = New-Object IO.StreamReader($stream)
$writer = New-Object IO.StreamWriter($stream); $writer.AutoFlush = $true
$writer.WriteLine("echo server ready on $(hostname)")
while (($line = $reader.ReadLine()) -ne $null) { $writer.WriteLine("echo: $line") }
$client.Close()
}
UPnPCJ で外部の 2222 TCP ポートをサーバーを立てたマシンの 2222 TCP ポートにマップします。
WAN 側から以下のコマンドで 2222 ポートにアクセスしてテストします。
nc hogepiyo.com 2222