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