Skip to content

Instantly share code, notes, and snippets.

@lixin9311
Forked from miio/FixDS-Lite.bash
Last active November 14, 2024 05:38
Show Gist options
  • Select an option

  • Save lixin9311/173fc8983095ca520776e772b1e0df43 to your computer and use it in GitHub Desktop.

Select an option

Save lixin9311/173fc8983095ca520776e772b1e0df43 to your computer and use it in GitHub Desktop.
UDM Pro で固定IPv4を使いたい場合やDS-Liteが挙動おかしいときに作ったスクリプト
#!/bin/sh
# hotplug script for openwrt
. /lib/functions.sh
. /lib/functions/network.sh
set -eu
user="user"
pass="pass"
# Check if curl is available
if ! command -v curl >/dev/null 2>&1; then
logger -t "dslite-ddns" "curl command not found"
exit 1
fi
if ! network_find_wan6 wanif; then
exit 0
fi
if [ "${INTERFACE:-}" != "${wanif}" ]; then
exit 0
fi
case ${ACTION} in
ifup)
;;
ifupdate)
if [ "${IFUPDATE_ADDRESSES:-}" != "1" ]; then
exit 0
fi
;;
*) exit 0 ;;
esac
if ! network_get_ipaddr6 ip6addr "$wanif"; then
exit 0
fi
logger -t "dslite-ddns" "ddns trying to update: ${DEVICE} ipaddr=${ip6addr}"
# curl with --fail, and handle failure messages
if ! response=$(curl --fail --interface "${ip6addr}" -6 -s "http://fcs.enabler.ne.jp/update?user=${user}&pass=${pass}" 2>&1); then
logger -t "dslite-ddns" "Failed to update ddns: ${response}"
exit 1
elif [ "${response}" != "OK" ]; then
logger -t "dslite-ddns" "Failed to update ddns: ${response}"
exit 1
fi
logger -t "dslite-ddns" "ddns updated to ${ip6addr}"
# DS-Lite修正
# 参考
# https://techlog.iij.ad.jp/contents/dslite-raspi
REMOTE=''
# SLAAC シングルの人は多分これでいける
# LOCAL=`ip addr show br0 | grep 'mngtmpaddr' | awk '{print $2}' | awk -F/ '{print $1}'`
# DHCP-PDv6の人はこっち
LOCAL=`ip addr show br0 | grep 'inet6' | grep 'dynamic' | awk '{print $2}' | awk -F/ '{print $1}'`
# IPIP6 tunnel linkup
ip -6 tunnel delete ip6tnl1
ip -6 tunnel add ip6tnl1 mode ip4ip6 remote ${REMOTE} local ${LOCAL} dev br0
ip link set dev ip6tnl1 up
## IPIP設定 (DHCP-PD方式)
# https://www.rtpro.yamaha.co.jp/RT/docs/v6connect/index.html
# https://xmms.jp/blog/index.php?entry=entry200812-194208
# ASAHINETの固定IPの詳細ページからとれる情報
AFTR=''
INTERFACE_ID='' # 下64bitでOK
UPDATE_UID=''
UPDATE_PASSWORD=''
FIXED_IPV4=''
INTERNET_ETH='eth8'
LOCAL_PREFIX=`ip addr show br0 | grep 'inet6' | grep 'dynamic' | awk '{print $2}' | awk -F/ '{print $1}' | sed 's/::1//'`
LOCAL_INET6="${LOCAL_PREFIX}:${INTERFACE_ID}"
# 新しいv6アドレスを用意
ip -6 add add ${LOCAL_INET6}/64 dev br0
# IPv4アドレスをセット
`ip a add dev ${INTERNET_ETH} ${FIXED_IPV4}`
# accept_raを2にしないとtokenセットできないらしい
# https://ral-arturo.org/2021/04/01/ip-token.html
sysctl -w net.ipv6.conf.br0.accept_ra=2
ip token set ::${INTERFACE_ID} dev br0
ip route add default dev ip6tnl1
# アップデートサーバーに通知
curl "https://v6update.asahi-net.or.jp/prefix?key=${UPDATE_UID}&pass=${UPDATE_PASSWORD}"
# IPIP6 tunnel 再構築
ip -6 tunnel delete ip6tnl1
ip -6 tunnel add ip6tnl1 mode ip4ip6 remote ${AFTR} local ${LOCAL_INET6} dev br0
ip link set dev ip6tnl1 up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment