Merge pull request #878 from jow-/lxc-autoconf-fix
[feed/packages.git] / ipv6 / tayga / files / tayga-proto.sh
1 #!/bin/sh
2 # tayga.sh - TAYGA proto
3 # Copyright (c) 2014 OpenWrt.org
4
5 [ -n "$INCLUDE_ONLY" ] || {
6 . /lib/functions.sh
7 . /lib/functions/network.sh
8 . ../netifd-proto.sh
9 init_proto "$@"
10 }
11
12 proto_tayga_setup() {
13 local cfg="$1"
14 local iface="$2"
15 local link="tayga-$cfg"
16
17 local ipv4_addr ipv6_addr prefix dynamic_pool ipaddr ip6addr noroutes
18 json_get_vars ipv4_addr ipv6_addr prefix dynamic_pool ipaddr ip6addr noroutes
19 [ -z "$ipv4_addr" -o -z "$prefix" ] && {
20 proto_notify_error "$cfg" "REQUIRED_PARAMETERS_MISSING"
21 proto_block_restart "$cfg"
22 return
23 }
24
25 local tmpconf="/var/etc/tayga-$cfg.conf"
26 mkdir -p /var/etc
27 mkdir -p /var/run/tayga/$cfg
28
29 echo "tun-device $link" >$tmpconf
30 echo "ipv4-addr $ipv4_addr" >>$tmpconf
31 [ -n "$ipv6_addr" ] &&
32 echo "ipv6-addr $ipv6_addr" >>$tmpconf
33 [ -n "$prefix" ] &&
34 echo "prefix $prefix" >>$tmpconf
35 [ -n "$dynamic_pool" ] &&
36 echo "dynamic-pool $dynamic_pool" >>$tmpconf
37 echo "data-dir /var/run/tayga/$cfg" >>$tmpconf
38 #TODO: Support static mapping of IPv4 <-> IPv6
39
40 # here we create TUN device and check configuration
41 tayga -c $tmpconf --mktun
42 [ "$?" -ne 0 ] && {
43 proto_notify_error "$cfg" "TAYGA_FAILED"
44 proto_block_restart "$cfg"
45 return
46 }
47
48 proto_init_update "$link" 1
49
50 [ -n "$ipaddr" ] && proto_add_ipv4_address "$ipaddr" "255.255.255.255"
51 [ -n "$ip6addr" ] && proto_add_ipv6_address "$ip6addr" "128"
52
53 [ "$noroutes" != 1 ] && {
54 [ -n "$ipv6_addr" ] && proto_add_ipv6_route "$ipv6_addr" "128"
55 [ -n "$dynamic_pool" ] && {
56 local pool="${dynamic_pool%%/*}"
57 local mask="${dynamic_pool##*/}"
58 proto_add_ipv4_route "$pool" "$mask"
59 }
60 [ -n "$prefix" ] && {
61 local prefix6="${prefix%%/*}"
62 local mask6="${prefix##*/}"
63 proto_add_ipv6_route "$prefix6" "$mask6"
64 }
65 }
66
67 proto_send_update "$cfg"
68
69 proto_run_command "$cfg" tayga -n -c $tmpconf \
70 -p /var/run/$link.pid
71
72 }
73
74 proto_tayga_teardown() {
75 local cfg="$1"
76 local tmpconf="/var/etc/tayga-$cfg.conf"
77 proto_kill_command "$cfg"
78 sleep 1
79 tayga -c $tmpconf --rmtun
80 }
81
82 proto_tayga_init_config() {
83 no_device=1
84 available=1
85 proto_config_add_string "ipv4_addr"
86 proto_config_add_string "ipv6_addr"
87 proto_config_add_string "prefix"
88 proto_config_add_string "dynamic_pool"
89 proto_config_add_string "ipaddr"
90 proto_config_add_string "ip6addr:ip6addr"
91 proto_config_add_boolean "noroutes"
92 }
93
94 [ -n "$INCLUDE_ONLY" ] || {
95 add_protocol tayga
96 }