ca11e87cfd4502e547d1c255ec25223325d43a40
[openwrt/openwrt.git] / package / network / config / gre / files / gre.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 gre_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 ttl tos zone ikey okey icsum ocsum iseqno oseqno multicast
17 json_get_vars mtu ttl tos zone ikey okey icsum ocsum iseqno oseqno multicast
18
19 [ -z "$zone" ] && zone="wan"
20 [ -z "$multicast" ] && multicast=1
21
22 proto_init_update "$link" 1
23
24 proto_add_tunnel
25 json_add_string mode "$mode"
26 json_add_int mtu "${mtu:-1280}"
27 [ -n "$df" ] && json_add_boolean df "$df"
28 [ -n "$ttl" ] && json_add_int ttl "$ttl"
29 [ -n "$tos" ] && json_add_string tos "$tos"
30 json_add_boolean multicast "$multicast"
31 json_add_string local "$local"
32 json_add_string remote "$remote"
33 [ -n "$tunlink" ] && json_add_string link "$tunlink"
34
35 json_add_object 'data'
36 [ -n "$ikey" ] && json_add_int ikey "$ikey"
37 [ -n "$okey" ] && json_add_int okey "$okey"
38 [ -n "$icsum" ] && json_add_boolean icsum "$icsum"
39 [ -n "$ocsum" ] && json_add_boolean ocsum "$ocsum"
40 [ -n "$iseqno" ] && json_add_boolean iseqno "$iseqno"
41 [ -n "$oseqno" ] && json_add_boolean oseqno "$oseqno"
42 [ -n "$encaplimit" ] && json_add_string encaplimit "$encaplimit"
43 json_close_object
44
45 proto_close_tunnel
46
47 proto_add_data
48 [ -n "$zone" ] && json_add_string zone "$zone"
49 proto_close_data
50
51 proto_send_update "$cfg"
52 }
53
54 gre_setup() {
55 local cfg="$1"
56 local mode="$2"
57 local remoteip
58
59 local ipaddr peeraddr
60 json_get_vars df ipaddr peeraddr tunlink
61
62 [ -z "$peeraddr" ] && {
63 proto_notify_error "$cfg" "MISSING_PEER_ADDRESS"
64 proto_block_restart "$cfg"
65 exit
66 }
67
68 remoteip=$(resolveip -t 10 -4 "$peeraddr")
69
70 if [ -z "$remoteip" ]; then
71 proto_notify_error "$cfg" "PEER_RESOLVE_FAIL"
72 exit
73 fi
74
75 for ip in $remoteip; do
76 peeraddr=$ip
77 break
78 done
79
80 ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
81
82 [ -z "$ipaddr" ] && {
83 local wanif="$tunlink"
84 if [ -z $wanif ] && ! network_find_wan wanif; then
85 proto_notify_error "$cfg" "NO_WAN_LINK"
86 exit
87 fi
88
89 if ! network_get_ipaddr ipaddr "$wanif"; then
90 proto_notify_error "$cfg" "NO_WAN_LINK"
91 exit
92 fi
93 }
94
95 [ -z "$df" ] && df="1"
96
97 case "$mode" in
98 gretapip)
99 gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre4t-$cfg"
100 ;;
101 *)
102 gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre4-$cfg"
103 ;;
104 esac
105 }
106
107 proto_gre_setup() {
108 local cfg="$1"
109
110 gre_setup $cfg "greip"
111 }
112
113 proto_gretap_setup() {
114 local cfg="$1"
115
116 local network
117 json_get_vars network
118
119 gre_setup $cfg "gretapip"
120
121 json_init
122 json_add_string name "gre4t-$cfg"
123 json_add_boolean link-ext 0
124 json_close_object
125
126 for i in $network; do
127 ubus call network.interface."$i" add_device "$(json_dump)"
128 done
129 }
130
131 grev6_setup() {
132 local cfg="$1"
133 local mode="$2"
134 local remoteip6
135
136 local ip6addr peer6addr weakif
137 json_get_vars ip6addr peer6addr tunlink weakif encaplimit
138
139 [ -z "$peer6addr" ] && {
140 proto_notify_error "$cfg" "MISSING_PEER_ADDRESS"
141 proto_block_restart "$cfg"
142 exit
143 }
144
145 remoteip6=$(resolveip -t 10 -6 "$peer6addr")
146
147 if [ -z "$remoteip6" ]; then
148 proto_notify_error "$cfg" "PEER_RESOLVE_FAIL"
149 exit
150 fi
151
152 for ip6 in $remoteip6; do
153 peer6addr=$ip6
154 break
155 done
156
157 ( proto_add_host_dependency "$cfg" "$peer6addr" "$tunlink" )
158
159 [ -z "$ip6addr" ] && {
160 local wanif="$tunlink"
161 if [ -z $wanif ] && ! network_find_wan6 wanif; then
162 proto_notify_error "$cfg" "NO_WAN_LINK"
163 exit
164 fi
165
166 if ! network_get_ipaddr6 ip6addr "$wanif"; then
167 [ -z "$weakif" ] && weakif="lan"
168 if ! network_get_ipaddr6 ip6addr "$weakif"; then
169 proto_notify_error "$cfg" "NO_WAN_LINK"
170 exit
171 fi
172 fi
173 }
174
175 case "$mode" in
176 gretapip6)
177 gre_generic_setup $cfg $mode $ip6addr $peer6addr "gre6t-$cfg"
178 ;;
179 *)
180 gre_generic_setup $cfg $mode $ip6addr $peer6addr "gre6-$cfg"
181 ;;
182 esac
183 }
184
185 proto_grev6_setup() {
186 local cfg="$1"
187
188 grev6_setup $cfg "greip6"
189 }
190
191 proto_grev6tap_setup() {
192 local cfg="$1"
193
194 local network
195 json_get_vars network
196
197 grev6_setup $cfg "gretapip6"
198
199 json_init
200 json_add_string name "gre6t-$cfg"
201 json_add_boolean link-ext 0
202 json_close_object
203
204 for i in $network; do
205 ubus call network.interface."$i" add_device "$(json_dump)"
206 done
207 }
208
209 gretap_generic_teardown() {
210 local network
211 json_get_vars network
212
213 json_init
214 json_add_string name "$1"
215 json_add_boolean link-ext 0
216 json_close_object
217
218 for i in $network; do
219 ubus call network.interface."$i" remove_device "$(json_dump)"
220 done
221 }
222
223 proto_gre_teardown() {
224 local cfg="$1"
225 }
226
227 proto_gretap_teardown() {
228 local cfg="$1"
229
230 gretap_generic_teardown "gre4t-$cfg"
231 }
232
233 proto_grev6_teardown() {
234 local cfg="$1"
235 }
236
237 proto_grev6tap_teardown() {
238 local cfg="$1"
239
240 gretap_generic_teardown "gre6t-$cfg"
241 }
242
243 gre_generic_init_config() {
244 no_device=1
245 available=1
246
247 proto_config_add_int "mtu"
248 proto_config_add_int "ttl"
249 proto_config_add_string "tos"
250 proto_config_add_string "tunlink"
251 proto_config_add_string "zone"
252 proto_config_add_int "ikey"
253 proto_config_add_int "okey"
254 proto_config_add_boolean "icsum"
255 proto_config_add_boolean "ocsum"
256 proto_config_add_boolean "iseqno"
257 proto_config_add_boolean "oseqno"
258 proto_config_add_boolean "multicast"
259 }
260
261 proto_gre_init_config() {
262 gre_generic_init_config
263 proto_config_add_string "ipaddr"
264 proto_config_add_string "peeraddr"
265 proto_config_add_boolean "df"
266 }
267
268 proto_gretap_init_config() {
269 proto_gre_init_config
270 proto_config_add_string "network"
271 }
272
273 proto_grev6_init_config() {
274 gre_generic_init_config
275 proto_config_add_string "ip6addr"
276 proto_config_add_string "peer6addr"
277 proto_config_add_string "weakif"
278 proto_config_add_string "encaplimit"
279 }
280
281 proto_grev6tap_init_config() {
282 proto_grev6_init_config
283 proto_config_add_string "network"
284 }
285
286 [ -n "$INCLUDE_ONLY" ] || {
287 [ -f /lib/modules/$(uname -r)/gre.ko ] && add_protocol gre
288 [ -f /lib/modules/$(uname -r)/gre.ko ] && add_protocol gretap
289 [ -f /lib/modules/$(uname -r)/ip6_gre.ko ] && add_protocol grev6
290 [ -f /lib/modules/$(uname -r)/ip6_gre.ko ] && add_protocol grev6tap
291 }