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