OpenWrtの初期設定の状態から、楽天ひかりでクロスパスとPPPoEを併用して、PPPoE側で自宅サーバーを立てる構成を一発で構築するシェルです。
これを使えば、ポートフォワーディングできないと言われるクロスパス接続でポートフォワーディングできますと言うか、
PPPoEのルートでポートフォワーディングしてるわけですが。
OpenWrtは25.12.5です。
apkコマンドでモジュールインストールしてますので、古いやつだとそこでコケるはずです。
シェルはルーターのIPアドレスをデフォルトの192.168.1.1から192.168.1.254に変更するようにしてます。
IP変えたくない人は最後の方にあるIP変更部分を削ってください。
uci set network.lan.ipaddr=”${ROUTER_ADDRESS}”
uci set network.lan.netmask=’255.255.255.0′
自己責任でお願いします。

# =====================================================================
# OpenWrt セットアップスクリプト
#  参考資料   https://nabe.adiary.jp/0633


# ---------------------------------------------------------------------
# 認証情報・固定値(変更したいときはここだけ触れば良いようにまとめてある)
# ---------------------------------------------------------------------

ROOT_PASSWORD='wrtpasswd'

PPPOE_DEVICE='eth1'
PPPOE_USERNAME='rakutenhikari_userID@m.rakutenbb.jp'
PPPOE_PASSWORD='rakutenhikari_pass'

DSLITE_PEERADDR='dgw.xpass.jp'

DDNS_DOMAIN='noip_host.sytes.net'
DDNS_USERNAME='noip_id@yahoo.co.jp'
DDNS_PASSWORD='noip_pass'

ROUTER_ADDRESS='192.168.1.254'
LAN_SUBNET='192.168.1.0/24'
ROUTE_TABLE_ID='200'
ROUTE_TABLE_NAME='pppoe_table'
MARK_VALUE='0x20'


echo  1. 基本設定(ホスト名 / DHCP範囲 / rootパスワード)

uci set system.@system[0].hostname='vmOpenWrt'

uci set dhcp.lan.start='150'
uci set dhcp.lan.limit='30'

uci set system.@system[0].timezone='JST-9'
uci set system.@system[0].zonename='Asia/Tokyo'


cat << EOF | passwd root
${ROOT_PASSWORD}
${ROOT_PASSWORD}
EOF


echo  2. DHCPの静的アドレス割り当て

cat << 'EOF' | awk '
NF==3 {
        printf "\nconfig host\n"
        printf "\toption name \x27%s\x27\n", $1
        printf "\toption dns \x271\x27\n"
        printf "\toption mac \x27%s\x27\n", $2
        printf "\toption ip \x27%s\x27\n", $3
    }' >> /etc/config/dhcp
Amazon       B4:2E:99:39:AA:CE 192.168.1.103
Debian       00:0C:29:DF:AA:BD 192.168.1.100
TCL          08:C3:B3:8D:AA:E2 192.168.1.109
android1     88:D7:F6:97:AA:FB 192.168.1.114
EOF


echo  3. ポート転送 & ポリシールーティング用マークの設定

# ルーティングテーブル定義を追加(重複防止)
grep -q "${ROUTE_TABLE_ID} ${ROUTE_TABLE_NAME}" /etc/iproute2/rt_tables \
|| echo "${ROUTE_TABLE_ID} ${ROUTE_TABLE_NAME}" >> /etc/iproute2/rt_tables

# 外部宛て(LAN→WAN)トラフィックへのマーキング + 外部→内部へのDNAT設定
cat << EOF | awk -v subnet="${LAN_SUBNET}" -v mark="${MARK_VALUE}" '
NF==3 {
        print "config rule"
        print "\toption src \x27lan\x27"
        print "\toption dest \x27*\x27"
        print "\toption family \x27ipv4\x27"
        print "\tlist proto \x27tcp\x27"
        print "\tlist dest_ip \x27!" subnet "\x27"
        print "\toption target \x27MARK\x27"
        print "\toption set_mark \x27" mark "\x27"
        print "\toption name \x27" $1 "\x27"
        print "\tlist src_ip \x27" $2 "\x27"
        print "\toption src_port \x27" $3 "\x27"
        print ""
        print "config redirect"
        print "\toption dest \x27lan\x27"
        print "\toption target \x27DNAT\x27"
        print "\toption family \x27ipv4\x27"
        print "\tlist proto \x27tcp\x27"
        print "\toption src \x27wan\x27"
        print "\toption src_dport \x27" $3 "\x27"
        print "\toption name \x27" $1 "\x27"
        print "\toption dest_ip \x27" $2 "\x27"
        print "\toption dest_port \x27" $3 "\x27\n\n"
    }' >> /etc/config/firewall
HTTP    192.168.1.100   80
HTTP443 192.168.1.100  443
tvrock  192.168.1.103  8969
EOF

# 0x20マークが付いたパケットのみ pppoe_table を参照させる
uci add network rule
uci set network.@rule[-1].mark="${MARK_VALUE}"
uci set network.@rule[-1].lookup="${ROUTE_TABLE_NAME}"
uci set network.@rule[-1].priority="${ROUTE_TABLE_ID}"
uci set network.@rule[-1].in='lan'
# ※ LAN全体を強制ルーティングする不要なルールは追加しない
#   (通常通信は主回線=DS-Liteに流す)


echo  4. WAN6側 IPv6 DNS(ポート53)の許可

# ルーター自身がWAN6経由で上流のIPv6 DNSサーバーと通信できるようにする
# (参考: https://nabe.adiary.jp/0633 個別ルールの設定「DNS」)
uci add firewall rule
uci set firewall.@rule[-1].name='Allow-WAN6-DNS'
uci set firewall.@rule[-1].src='wan'
uci set firewall.@rule[-1].family='ipv6'
uci add_list firewall.@rule[-1].proto='tcp'
uci add_list firewall.@rule[-1].proto='udp'
uci set firewall.@rule[-1].dest_port='53'
uci set firewall.@rule[-1].target='ACCEPT'


echo  5. LAN側 IPv6 をリレーモードに変更

uci set dhcp.lan.ra='relay'
uci set dhcp.lan.dhcpv6='relay'
uci set dhcp.lan.ndp='relay'
uci set dhcp.lan.learn_routes='1'


echo  6. WAN6 の DHCPv6 リレー設定
uci set dhcp.wan6=dhcp
uci set dhcp.wan6.interface='wan6'
uci set dhcp.wan6.ignore='1'
uci set dhcp.wan6.master='1'
uci set dhcp.wan6.ra='relay'
uci set dhcp.wan6.dhcpv6='relay'
uci set dhcp.wan6.ndp='relay'

uci commit

/etc/init.d/odhcpd restart


echo "downloads.openwrt.orgへの接続の確立を待っています..."
for i in $(seq 1 30); do
    if ping -c 1 -W 2 downloads.openwrt.org ; then
        echo "インターネット接続を確認しました。"
        break
    fi
    sleep 5
done


echo  7. 必要パッケージのインストール


apk update
apk add ds-lite ddns-scripts-noip luci-i18n-ddns-ja luci-i18n-base-ja tcpdump

echo モジュールを最新にする。
apk info | cut -d ' ' -f 1 | xargs apk upgrade


uci commit

echo  8. ネットワーク再起動
/etc/init.d/network restart
/etc/init.d/odhcpd restart



echo  9. WAN を DS-Lite(IPv6トンネル経由のIPv4)に設定
echo "wanをDS-LITEに設定"
uci set network.wan.proto='dslite'
uci set network.wan.peeraddr="${DSLITE_PEERADDR}"
uci set network.wan.encaplimit='ignore'
uci set network.wan.multipath='off'
uci set network.wan.tunlink='wan6'
uci set network.wan.mtu='1460'
uci set network.wan.peerdns='0'



echo  10. PPPoE インターフェース(実IPv4取得用)の作成

echo "pppoeインターフェイス(PPPoE認証接続)を作成する"
uci delete network.pppoe 2>/dev/null
uci set network.pppoe=interface
uci set network.pppoe.proto='pppoe'
uci set network.pppoe.device="${PPPOE_DEVICE}"
uci set network.pppoe.username="${PPPOE_USERNAME}"
uci set network.pppoe.password="${PPPOE_PASSWORD}"
uci set network.pppoe.ipv6='auto'   # 無効にするとwan6のIPv6取得に影響するため

echo "ルーティングテーブルの設定"
uci set network.pppoe.defaultroute='1'
uci set network.pppoe.ip4table="${ROUTE_TABLE_NAME}"
uci set network.pppoe.peerdns='0'

echo "RPフィルター(非対称ルーティング許可)の設定"
uci set network.pppoe.rpfilter='0'
uci set network.wan.rpfilter='0'



echo  11. ファイアウォール: wanゾーンへ pppoe を割り当て、masquerade設定

echo "ファイアウォール:wanゾーンを特定して名前を付与し、masqueradeとpppoeを割り当て"
# 無名セクション(@zone[n])のままだと firewall.wan で参照できないため、
# name='wan' を持つセクションを検出して明示的に 'wan' という名前を付ける

WANZONE_IDX=$(uci show firewall | grep "\.name='wan'$" | head -n1 | sed -E "s/firewall\.(@zone\[[0-9]+\])\.name=.*/\1/")
if [ -n "$WANZONE_IDX" ]; then
        uci rename firewall.${WANZONE_IDX}='wan' 2>/dev/null
fi
uci set firewall.wan.masq='1'
uci del_list firewall.wan.network='pppoe' 2>/dev/null
uci add_list firewall.wan.network='pppoe'



echo  12. DHCP設定(pppoe側のDHCPサーバは無効化)

echo "DHCP設定のセットアップ"
uci set dhcp.pppoe=dhcp
uci set dhcp.pppoe.interface='pppoe'
uci set dhcp.pppoe.ignore='1'



echo 13. DDNS設定(ddns-scripts-noip のインストールが前提)

echo "DDNS設定の作成"
uci set ddns.myddns_ipv4=service
uci set ddns.myddns_ipv4.enabled='1'
uci set ddns.myddns_ipv4.use_ipv6='0'
uci set ddns.myddns_ipv4.service_name='no-ip.com'
uci set ddns.myddns_ipv4.domain="${DDNS_DOMAIN}"
uci set ddns.myddns_ipv4.username="${DDNS_USERNAME}"
uci set ddns.myddns_ipv4.password="${DDNS_PASSWORD}"
uci set ddns.myddns_ipv4.lookup_host="${DDNS_DOMAIN}"
uci set ddns.myddns_ipv4.interface='pppoe'
uci set ddns.myddns_ipv4.ip_network='pppoe'

uci set network.lan.ipaddr="${ROUTER_ADDRESS}"
uci set network.lan.netmask='255.255.255.0'
uci commit 

echo  14. 再起動


echo NTT側とのセッションを確実に切るためeth1をダウンして10秒後にreboot
ifconfig eth1 down
sleep 10
reboot


カテゴリー: OenWrt

0件のコメント

コメントを残す

アバタープレースホルダー

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください