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