0%

中華電信 PPPoE IPv6 Dual Stack

前鎮子申請了中華電信的 PPPoE IPv6 Dual Stack 服務,在我的 Linux Router 上只需要在 /etc/ppp/options 中加入

/etc/ppp/option
1
+ipv6 ipv6cp-use-ipaddr

接著 poff/pon 之後就能夠正確取得 IPv6,router 也能夠正確連上 IPv6 網站,但麻煩的是中華電信只配了一段 prefix 64 的 subnet,如果要把這段 IP 分配給其他台電腦使用,就要花費一些功夫。索性就把它寫成一個 script,並且讓它在連上 PPPoE 後自動執行。

在開始之前請先安裝 radvd,這個 daemon 用來在內網的 interface 上面發送 router advertisement,讓底下的電腦能夠進行 SLAAC。

v6_dual_stack_pd.sh
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
WANIF=ppp0
LANIF=enp2s0

get_global_addr() {
IF=$1
ip -6 addr show dev ${IF} | grep -i global | awk '{print $2}'
}

get_prefix_addr() {
ADDR=$1
echo $ADDR | awk -F ":" '{print $1":"$2":"$3":"$4}'
}

flush_global_addr() {
IF=$1
ip -6 addr flush dev $IF scope global
}

gen_radvd_conf() {
IF=$1
PREFIX=$2
cat <<EOF
interface ${IF}
{
AdvSendAdvert on;
prefix ${PREFIX}::/64
{
AdvOnLink on;
AdvAutonomous on;
};
};
EOF
}

WANADDR=$(get_global_addr $WANIF)

if [ "$WANADDR" == "" ]; then
echo "No WAN Address found. This script probably has already been executed."
exit 1;
fi

PREFIXADDR=$(get_prefix_addr $WANADDR)

echo "WAN Global Addr:" $WANADDR
echo "Prefix:" $PREFIXADDR

## Disable the accept_ra on WANIF
sysctl net.ipv6.conf.ppp0.accept_ra=0

## Delete the global address of wan interface
echo "Removing the global address of WAN interface and LAN interface"
flush_global_addr ${WANIF}
flush_global_addr ${LANIF}

echo "Deleting prefix route on WAN interface"
ip -6 route del ${PREFIXADDR}::/64 dev ${WANIF}

## Move the global address to LAN interface
echo "Adding back global address to LAN interface"
ip -6 addr add ${PREFIXADDR}::FFFF/64 dev ${LANIF}

## Generate radvd.conf for LAN interface
echo "Generate radvd.conf for LAN interface."
gen_radvd_conf ${LANIF} ${PREFIXADDR} | tee /etc/radvd.conf
systemctl restart radvd

將這隻 script 存下來之後加上執行權限 chmod +x v6_dual_stack_pd.sh,接著編輯 /etc/network/interfaces,把這隻 script 放到 post-up 就完成了。

/etc/network/interfaces
1
2
3
4
5
6
# DSL
auto dsl-provider
iface dsl-provider inet ppp
pre-up /sbin/ifconfig enp1s0 up # line maintained by pppoeconf
post-up /home/frank/v6_dual_stack_pd.sh
provider dsl-provider

目前只有想到這個方法,如果有更好的方法拜託留言告訴我! Orz