2 # 464xlat.sh - 464xlat CLAT
4 # Copyright (c) 2015 Steven Barth <cyrus@openwrt.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 2
8 # as published by the Free Software Foundation
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 [ -n "$INCLUDE_ONLY" ] || {
17 . /lib/functions/network.sh
22 proto_464xlat_setup() {
27 local ip6addr ip6prefix tunlink zone
28 json_get_vars ip6addr ip6prefix tunlink zone
30 [ -z "$zone" ] && zone="wan"
32 ( proto_add_host_dependency "$cfg" "::" "$tunlink" )
34 if [ -z "$tunlink" ] && ! network_find_wan6 tunlink; then
35 proto_notify_error "$cfg" "NO_WAN_LINK"
38 network_get_device tundev "$tunlink"
40 ip6addr=$(464xlatcfg "$link" "$tundev" "$ip6prefix" 192.0.0.1 $ip6addr)
41 if [ -z "$ip6addr" ]; then
42 proto_notify_error "$cfg" "CLAT_CONFIG_FAILED"
46 ip -6 rule del from all lookup local
47 ip -6 rule add from all lookup local pref 1
48 ip -6 rule add to $ip6addr lookup prelocal pref 0
49 echo "$ip6addr" > /tmp/464-$cfg-anycast
51 proto_init_update "$link" 1
52 proto_add_ipv4_route "0.0.0.0" 0 "" "" 2048
53 proto_add_ipv6_route $ip6addr 128 "" "" "" "" 128
56 [ "$zone" != "-" ] && json_add_string zone "$zone"
58 json_add_array firewall
60 json_add_string type nat
61 json_add_string target SNAT
62 json_add_string family inet
63 json_add_string snat_ip 192.0.0.1
66 json_add_string type rule
67 json_add_string family inet6
68 json_add_string proto all
69 json_add_string direction in
70 json_add_string dest "$zone"
71 json_add_string src "$zone"
72 json_add_string src_ip $ip6addr
73 json_add_string target ACCEPT
78 proto_send_update "$cfg"
81 proto_464xlat_teardown() {
85 [ -f /tmp/464-$cfg-anycast ] || return
86 local ip6addr=$(cat /tmp/464-$cfg-anycast)
90 rm -rf /tmp/464-$cfg-anycast
91 [ -n "$ip6addr" ] && ip -6 rule del to $ip6addr lookup prelocal
93 if [ -z "$(ls /tmp/464-*-anycast 2>&-)" ]; then
94 ip -6 rule del from all lookup local
95 ip -6 rule add from all lookup local pref 0
99 proto_464xlat_init_config() {
103 proto_config_add_string "ip6prefix"
104 proto_config_add_string "ip6addr"
105 proto_config_add_string "tunlink"
106 proto_config_add_string "zone"
109 [ -n "$INCLUDE_ONLY" ] || {