vti: add vti specific settings as nested json object
[openwrt/openwrt.git] / package / network / config / vti / files / vti.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4 . /lib/functions.sh
5 . /lib/functions/network.sh
6 . ../netifd-proto.sh
7 init_proto "$@"
8 }
9
10 vti_generic_setup() {
11 local cfg="$1"
12 local mode="$2"
13 local local="$3"
14 local remote="$4"
15 local link="$5"
16 local mtu zone ikey
17 json_get_vars mtu zone ikey okey
18
19 [ -z "$zone" ] && zone="wan"
20
21 proto_init_update "$link" 1
22
23 proto_add_tunnel
24 json_add_string mode "$mode"
25 json_add_int mtu "${mtu:-1280}"
26 json_add_string local "$local"
27 json_add_string remote "$remote"
28 [ -n "$tunlink" ] && json_add_string link "$tunlink"
29
30 json_add_object 'data'
31 [ -n "$ikey" ] && json_add_int ikey "$ikey"
32 [ -n "$okey" ] && json_add_int okey "$okey"
33 json_close_object
34
35 proto_close_tunnel
36
37 proto_add_data
38 [ -n "$zone" ] && json_add_string zone "$zone"
39 proto_close_data
40
41 proto_send_update "$cfg"
42 }
43
44 vti_setup() {
45 local cfg="$1"
46 local mode="$2"
47
48 local ipaddr peeraddr
49 json_get_vars df ipaddr peeraddr tunlink
50
51 [ -z "$peeraddr" ] && {
52 proto_notify_error "$cfg" "MISSING_ADDRESS"
53 proto_block_restart "$cfg"
54 exit
55 }
56
57 ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
58
59 [ -z "$ipaddr" ] && {
60 local wanif="$tunlink"
61 if [ -z $wanif ] && ! network_find_wan wanif; then
62 proto_notify_error "$cfg" "NO_WAN_LINK"
63 exit
64 fi
65
66 if ! network_get_ipaddr ipaddr "$wanif"; then
67 proto_notify_error "$cfg" "NO_WAN_LINK"
68 exit
69 fi
70 }
71
72 vti_generic_setup $cfg $mode $ipaddr $peeraddr "vti-$cfg"
73 }
74
75 proto_vti_setup() {
76 local cfg="$1"
77
78 vti_setup $cfg "vtiip"
79 }
80
81 vti6_setup() {
82 local cfg="$1"
83 local mode="$2"
84
85 local ip6addr peer6addr weakif
86 json_get_vars ip6addr peer6addr tunlink weakif
87
88 [ -z "$peer6addr" ] && {
89 proto_notify_error "$cfg" "MISSING_ADDRESS"
90 proto_block_restart "$cfg"
91 exit
92 }
93
94 ( proto_add_host_dependency "$cfg" "$peer6addr" "$tunlink" )
95
96 [ -z "$ip6addr" ] && {
97 local wanif="$tunlink"
98 if [ -z $wanif ] && ! network_find_wan6 wanif; then
99 proto_notify_error "$cfg" "NO_WAN_LINK"
100 exit
101 fi
102
103 if ! network_get_ipaddr6 ip6addr "$wanif"; then
104 [ -z "$weakif" ] && weakif="lan"
105 if ! network_get_ipaddr6 ip6addr "$weakif"; then
106 proto_notify_error "$cfg" "NO_WAN_LINK"
107 exit
108 fi
109 fi
110 }
111
112 vti_generic_setup $cfg $mode $ip6addr $peer6addr "vti6-$cfg"
113 }
114
115 proto_vti6_setup() {
116 local cfg="$1"
117
118 vti6_setup $cfg "vtiip6"
119 }
120
121 proto_vti_teardown() {
122 local cfg="$1"
123 }
124
125 proto_vti6_teardown() {
126 local cfg="$1"
127 }
128
129 vti_generic_init_config() {
130 no_device=1
131 available=1
132
133 proto_config_add_int "mtu"
134 proto_config_add_string "tunlink"
135 proto_config_add_string "zone"
136 proto_config_add_int "ikey"
137 proto_config_add_int "okey"
138 }
139
140 proto_vti_init_config() {
141 vti_generic_init_config
142 proto_config_add_string "ipaddr"
143 proto_config_add_string "peeraddr"
144 }
145
146 proto_vti6_init_config() {
147 vti_generic_init_config
148 proto_config_add_string "ip6addr"
149 proto_config_add_string "peer6addr"
150 proto_config_add_string "weakif"
151 }
152
153 [ -n "$INCLUDE_ONLY" ] || {
154 [ -f /lib/modules/$(uname -r)/ip_vti.ko ] && add_protocol vti
155 [ -f /lib/modules/$(uname -r)/ip6_vti.ko ] && add_protocol vti6
156 }