packages: ipv6/tayga: move static mappings to 'firewall' config
[openwrt/svn-archive/archive.git] / ipv6 / tayga / files / tayga.sh
1 # tayga.sh - NAT64 backend
2
3 find_tayga_wanif4() {
4 local if=$(ip -4 r l e 0.0.0.0/0); if="${if#default* dev }"; if="${if%% *}"
5 [ -n "$if" ] && grep -qs "^ *$if:" /proc/net/dev && echo "$if"
6 }
7
8 find_tayga_wanip4() {
9 local ip=$(ip -4 a s dev "$1"); ip="${ip#*inet }"
10 echo "${ip%%[^0-9.]*}"
11 }
12
13 find_tayga_wanif6() {
14 local if=$(ip -6 r l e ::/0); if="${if#default* dev }"; if="${if%% *}"
15 [ -n "$if" ] && grep -qs "^ *$if:" /proc/net/dev && echo "$if"
16 }
17
18 find_tayga_wanip6() {
19 local ip=$(ip -6 a s dev "$1"); ip="${ip#*inet6 }"
20 echo "${ip%%[^0-9A-Fa-f:]*}"
21 }
22
23 # Hook into scan_interfaces() to synthesize a .device option
24 # This is needed for /sbin/ifup to properly dispatch control
25 # to setup_interface_tayga() even if no .ifname is set in
26 # the configuration.
27 scan_tayga() {
28 config_set "$1" device "tayga-$1"
29 }
30
31 coldplug_interface_tayga() {
32 setup_interface_tayga "tayga-$1" "$1"
33 }
34
35 tayga_add_static_mappings() {
36 local tmpconf="$1"
37
38 (
39 . /etc/functions.sh
40 config_load firewall
41
42 tayga_map_rule_add() {
43 local cfg="$1"
44 local tmpconf="$2"
45 local ipv4_addr ipv6_addr
46 config_get ipv4_addr "$cfg" ipv4_addr ""
47 config_get ipv6_addr "$cfg" ipv6_addr ""
48 [ -n "$ipv4_addr" ] && [ -n "$ipv6_addr" ] &&
49 echo "map $ipv4_addr $ipv6_addr" >>$tmpconf
50 }
51
52 config_foreach tayga_map_rule_add nat64 "$tmpconf"
53 )
54 }
55
56 setup_interface_tayga() {
57 local iface="$1"
58 local cfg="$2"
59 local link="tayga-$cfg"
60
61 local ipv4_addr ipv6_addr prefix dynamic_pool
62
63 config_get ipv4_addr "$cfg" ipv4_addr
64 config_get ipv6_addr "$cfg" ipv6_addr
65 config_get prefix "$cfg" prefix
66 config_get dynamic_pool "$cfg" dynamic_pool
67
68 local args
69
70 include /lib/network
71 scan_interfaces
72
73 local wanip4=$(uci_get network "$cfg" ipv4addr)
74 local wanip6=$(uci_get network "$cfg" ipv6addr)
75
76 local wanif4=$(find_tayga_wanif4)
77 local wanif6=$(find_tayga_wanif6)
78
79 [ -z "$wanip4" ] && {
80 [ -n "$wanif4" ] && {
81 wanip4=$(find_tayga_wanip4 "$wanif4")
82 uci_set_state network "$cfg" wan4_device "$wanif4"
83 }
84 }
85
86 [ -z "$wanip6" ] && {
87 [ -n "$wanif6" ] && {
88 wanip6=$(find_tayga_wanip6 "$wanif6")
89 uci_set_state network "$cfg" wan6_device "$wanif6"
90 }
91 }
92
93 [ -n "$wanip4" ] && [ -n "$wanip6" ] || {
94 echo "Cannot determine local IPv4 and IPv6 addressed for tayga NAT64 $cfg - skipping"
95 return 1
96 }
97
98 local tmpconf="/var/etc/tayga-$cfg.conf"
99 args="-c $tmpconf"
100 mkdir -p /var/etc
101 mkdir -p /var/run/tayga/$cfg
102
103 echo "tun-device $link" >$tmpconf
104 echo "ipv4-addr $ipv4_addr" >>$tmpconf
105 [ -n "$ipv6_addr" ] &&
106 echo "ipv6-addr $ipv6_addr" >>$tmpconf
107 [ -n "$prefix" ] &&
108 echo "prefix $prefix" >>$tmpconf
109
110 tayga_add_static_mappings "$tmpconf"
111
112 [ -n "$dynamic_pool" ] &&
113 echo "dynamic-pool $dynamic_pool" >>$tmpconf
114 echo "data-dir /var/run/tayga/$cfg" >>$tmpconf
115
116 # creating the tunnel below will trigger a net subsystem event
117 # prevent it from touching or iface by disabling .auto here
118 uci_set_state network "$cfg" ifname $link
119 uci_set_state network "$cfg" auto 0
120
121 # here we create TUN device and check configuration
122 tayga $args --mktun || return 1
123
124 ip link set "$link" up
125
126 ip addr add "$wanip4" dev "$link"
127 ip addr add "$wanip6" dev "$link"
128
129 [ -n "$dynamic_pool" ] &&
130 ip -4 route add "$dynamic_pool" dev "$link"
131 [ -n "$prefix" ] &&
132 ip -6 route add "$prefix" dev "$link"
133
134 start-stop-daemon -S -x tayga -- $args -p /var/run/$link.pid
135
136 env -i ACTION="ifup" DEVICE="$link" INTERFACE="$cfg" PROTO="tayga" \
137 /sbin/hotplug-call iface
138 }
139
140 stop_interface_tayga() {
141 local cfg="$1"
142 local link="tayga-$cfg"
143
144 env -i ACTION="ifdown" DEVICE="$link" INTERFACE="$cfg" PROTO="tayga" \
145 /sbin/hotplug-call iface
146
147 service_kill tayga "/var/run/$link.pid"
148
149 ip link set "$link" down
150 ip addr flush dev "$link"
151 ip route flush dev "$link"
152 }