source: packages/ipv6/tayga/files/tayga.sh @ 28013

Last change on this file since 28013 was 28013, checked in by florian, 5 years ago

[package] ipv6: add tayga

TAYGA is an out-of-kernel stateless NAT64 implementation for
Linux. It uses the TUN driver to exchange packets with the
kernel, which is the same driver used by OpenVPN and QEMU/KVM.

Signed-off-by: Alexey I. Froloff <raorn@…>

File size: 3.4 KB
Line 
1# tayga.sh - NAT64 backend
2
3find_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
8find_tayga_wanip4() {
9        local ip=$(ip -4 a s dev "$1"); ip="${ip#*inet }"
10        echo "${ip%%[^0-9.]*}"
11}
12
13find_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
18find_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.
27scan_tayga() {
28        config_set "$1" device "tayga-$1"
29}
30
31coldplug_interface_tayga() {
32        setup_interface_tayga "tayga-$1" "$1"
33}
34
35conf_rule_add() {
36        local cfg="$1"
37        local tmpconf="$2"
38        local ipv4_addr ipv6_addr
39        config_get ipv4_addr "$cfg" ipv4_addr ""
40        config_get ipv6_addr "$cfg" ipv6_addr ""
41        [ -n "$ipv4_addr" ] && [ -n "$ipv6_addr" ] &&
42                echo "map $ipv4_addr $ipv6_addr" >>$tmpconf
43}
44
45setup_interface_tayga() {
46        local iface="$1"
47        local cfg="$2"
48        local link="tayga-$cfg"
49
50        local ipv4_addr ipv6_addr prefix dynamic_pool
51
52        config_get ipv4_addr "$cfg" ipv4_addr
53        config_get ipv6_addr "$cfg" ipv6_addr
54        config_get prefix "$cfg" prefix
55        config_get dynamic_pool "$cfg" dynamic_pool
56
57        local args
58
59        include /lib/network
60        scan_interfaces
61
62        local wanip4=$(uci_get network "$cfg" ipv4addr)
63        local wanip6=$(uci_get network "$cfg" ipv6addr)
64
65        local wanif4=$(find_tayga_wanif4)
66        local wanif6=$(find_tayga_wanif6)
67
68        [ -z "$wanip4" ] && {
69                [ -n "$wanif4" ] && {
70                        wanip4=$(find_tayga_wanip4 "$wanif4")
71                        uci_set_state network "$cfg" wan4_device "$wanif4"
72                }
73        }
74
75        [ -z "$wanip6" ] && {
76                [ -n "$wanif6" ] && {
77                        wanip6=$(find_tayga_wanip6 "$wanif6")
78                        uci_set_state network "$cfg" wan6_device "$wanif6"
79                }
80        }
81
82        [ -n "$wanip4" ] && [ -n "$wanip6" ] || {
83                echo "Cannot determine local IPv4 and IPv6 addressed for tayga NAT64 $cfg - skipping"
84                return
85        }
86
87        local tmpconf="/var/etc/tayga-$cfg.conf"
88        args="-c $tmpconf"
89        mkdir -p /var/etc
90        mkdir -p /var/run/tayga/$cfg
91
92        echo "tun-device $link" >$tmpconf
93        echo "ipv4-addr $ipv4_addr" >>$tmpconf
94        [ -n "$ipv6_addr" ] &&
95                echo "ipv6-addr $ipv6_addr" >>$tmpconf
96        [ -n "$prefix" ] &&
97                echo "prefix $prefix" >>$tmpconf
98
99        config_foreach conf_rule_add map_rule "$tmpconf"
100
101        [ -n "$dynamic_pool" ] &&
102                echo "dynamic-pool $dynamic_pool" >>$tmpconf
103        echo "data-dir /var/run/tayga/$cfg" >>$tmpconf
104
105        # creating the tunnel below will trigger a net subsystem event
106        # prevent it from touching or iface by disabling .auto here
107        uci_set_state network "$cfg" ifname $link
108        uci_set_state network "$cfg" auto 0
109
110        tayga $args --mktun
111        ip link set "$link" up
112
113        ip addr add "$wanip4" dev "$link"
114        ip addr add "$wanip6" dev "$link"
115
116        [ -n "$dynamic_pool" ] &&
117                ip -4 route add "$dynamic_pool" dev "$link"
118        [ -n "$prefix" ] &&
119                ip -6 route add "$prefix" dev "$link"
120
121        start-stop-daemon -S -x tayga -- $args -p /var/run/$link.pid
122
123        env -i ACTION="ifup" DEVICE="$link" INTERFACE="$cfg" PROTO="tayga" \
124                /sbin/hotplug-call iface
125}
126
127stop_interface_tayga() {
128        local cfg="$1"
129        local link="tayga-$cfg"
130
131        env -i ACTION="ifdown" DEVICE="$link" INTERFACE="$cfg" PROTO="tayga" \
132                /sbin/hotplug-call iface
133
134        service_kill tayga "/var/run/$link.pid"
135
136        ip link set "$link" down
137        ip addr flush dev "$link"
138        ip route flush dev "$link"
139}
Note: See TracBrowser for help on using the repository browser.