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