netifd: Reload proto on topology change
[project/netifd.git] / scripts / netifd-proto.sh
1 NETIFD_MAIN_DIR="${NETIFD_MAIN_DIR:-/lib/netifd}"
2
3 . /usr/share/libubox/jshn.sh
4 . $NETIFD_MAIN_DIR/utils.sh
5
6 proto_config_add_int() {
7         config_add_int "$@"
8 }
9
10 proto_config_add_string() {
11         config_add_string "$@"
12 }
13
14 proto_config_add_boolean() {
15         config_add_boolean "$@"
16 }
17
18 _proto_do_teardown() {
19         json_load "$data"
20         eval "proto_$1_teardown \"$interface\" \"$ifname\""
21 }
22
23 _proto_do_renew() {
24         json_load "$data"
25         eval "proto_$1_renew \"$interface\" \"$ifname\""
26 }
27
28 _proto_do_setup() {
29         json_load "$data"
30         _EXPORT_VAR=0
31         _EXPORT_VARS=
32         eval "proto_$1_setup \"$interface\" \"$ifname\""
33 }
34
35 proto_init_update() {
36         local ifname="$1"
37         local up="$2"
38         local external="$3"
39
40         PROTO_KEEP=0
41         PROTO_INIT=1
42         PROTO_TUNNEL_OPEN=
43         PROTO_IPADDR=
44         PROTO_IP6ADDR=
45         PROTO_ROUTE=
46         PROTO_ROUTE6=
47         PROTO_PREFIX6=
48         PROTO_DNS=
49         PROTO_DNS_SEARCH=
50         json_init
51         json_add_int action 0
52         [ -n "$ifname" -a "*" != "$ifname" ] && json_add_string "ifname" "$ifname"
53         json_add_boolean "link-up" "$up"
54         [ -n "$3" ] && json_add_boolean "address-external" "$external"
55 }
56
57 proto_set_keep() {
58         PROTO_KEEP="$1"
59 }
60
61 proto_close_nested() {
62         [ -n "$PROTO_NESTED_OPEN" ] && json_close_object
63         PROTO_NESTED_OPEN=
64 }
65
66 proto_add_nested() {
67         PROTO_NESTED_OPEN=1
68         json_add_object "$1"
69 }
70
71 proto_add_tunnel() {
72         proto_add_nested "tunnel"
73 }
74
75 proto_close_tunnel() {
76         proto_close_nested
77 }
78
79 proto_add_data() {
80         proto_add_nested "data"
81 }
82
83 proto_close_data() {
84         proto_close_nested
85 }
86
87 proto_add_dns_server() {
88         local address="$1"
89
90         append PROTO_DNS "$address"
91 }
92
93 proto_add_dns_search() {
94         local address="$1"
95
96         append PROTO_DNS_SEARCH "$address"
97 }
98
99 proto_add_ipv4_address() {
100         local address="$1"
101         local mask="$2"
102         local broadcast="$3"
103         local ptp="$4"
104
105         append PROTO_IPADDR "$address/$mask/$broadcast/$ptp"
106 }
107
108 proto_add_ipv6_address() {
109         local address="$1"
110         local mask="$2"
111         local preferred="$3"
112         local valid="$4"
113         local offlink="$5"
114         local class="$6"
115
116         append PROTO_IP6ADDR "$address/$mask/$preferred/$valid/$offlink/$class"
117 }
118
119 proto_add_ipv4_route() {
120         local target="$1"
121         local mask="$2"
122         local gw="$3"
123
124         append PROTO_ROUTE "$target/$mask/$gw//"
125 }
126
127 proto_add_ipv6_route() {
128         local target="$1"
129         local mask="$2"
130         local gw="$3"
131         local metric="$4"
132         local valid="$5"
133         local source="$6"
134
135         append PROTO_ROUTE6 "$target/$mask/$gw/$metric/$valid/$source"
136 }
137
138 proto_add_ipv6_prefix() {
139         local prefix="$1"
140         local valid="$2"
141         local preferred="$3"
142
143         if [ -z "$valid" ]; then
144                 append PROTO_PREFIX6 "$prefix"
145         else
146                 [ -z "$preferred" ] && preferred="$valid"
147                 append PROTO_PREFIX6 "$prefix,$valid,$preferred"
148         fi
149 }
150
151 _proto_push_ipv4_addr() {
152         local str="$1"
153         local address mask broadcast ptp
154
155         address="${str%%/*}"
156         str="${str#*/}"
157         mask="${str%%/*}"
158         str="${str#*/}"
159         broadcast="${str%%/*}"
160         str="${str#*/}"
161         ptp="$str"
162
163         json_add_object ""
164         json_add_string ipaddr "$address"
165         [ -n "$mask" ] && json_add_string mask "$mask"
166         [ -n "$broadcast" ] && json_add_string broadcast "$broadcast"
167         [ -n "$ptp" ] && json_add_string ptp "$ptp"
168         json_close_object
169 }
170
171 _proto_push_ipv6_addr() {
172         local str="$1"
173         local address mask preferred valid offlink
174
175         address="${str%%/*}"
176         str="${str#*/}"
177         mask="${str%%/*}"
178         str="${str#*/}"
179         preferred="${str%%/*}"
180         str="${str#*/}"
181         valid="${str%%/*}"
182         str="${str#*/}"
183         offlink="${str%%/*}"
184         str="${str#*/}"
185         class="${str%%/*}"
186
187         json_add_object ""
188         json_add_string ipaddr "$address"
189         [ -n "$mask" ] && json_add_string mask "$mask"
190         [ -n "$preferred" ] && json_add_int preferred "$preferred"
191         [ -n "$valid" ] && json_add_int valid "$valid"
192         [ -n "$offlink" ] && json_add_boolean offlink "$offlink"
193         [ -n "$class" ] && json_add_string class "$class"
194         json_close_object
195 }
196
197 _proto_push_string() {
198         json_add_string "" "$1"
199 }
200
201 _proto_push_route() {
202         local str="$1";
203         local target="${str%%/*}"
204         str="${str#*/}"
205         local mask="${str%%/*}"
206         str="${str#*/}"
207         local gw="${str%%/*}"
208         str="${str#*/}"
209         local metric="${str%%/*}"
210         str="${str#*/}"
211         local valid="${str%%/*}"
212         str="${str#*/}"
213         local source="${str}"
214
215         json_add_object ""
216         json_add_string target "$target"
217         json_add_string netmask "$mask"
218         [ -n "$gw" ] && json_add_string gateway "$gw"
219         [ -n "$metric" ] && json_add_int metric "$metric"
220         [ -n "$valid" ] && json_add_int valid "$valid"
221         [ -n "$source" ] && json_add_string source "$source"
222         json_close_object
223 }
224
225 _proto_push_array() {
226         local name="$1"
227         local val="$2"
228         local cb="$3"
229
230         [ -n "$val" ] || return 0
231         json_add_array "$name"
232         for item in $val; do
233                 eval "$cb \"\$item\""
234         done
235         json_close_array
236 }
237
238 _proto_notify() {
239         local interface="$1"
240         local options="$2"
241         json_add_string "interface" "$interface"
242         ubus $options call network.interface notify_proto "$(json_dump)"
243 }
244
245 proto_send_update() {
246         local interface="$1"
247
248         proto_close_nested
249         json_add_boolean keep "$PROTO_KEEP"
250         _proto_push_array "ipaddr" "$PROTO_IPADDR" _proto_push_ipv4_addr
251         _proto_push_array "ip6addr" "$PROTO_IP6ADDR" _proto_push_ipv6_addr
252         _proto_push_array "routes" "$PROTO_ROUTE" _proto_push_route
253         _proto_push_array "routes6" "$PROTO_ROUTE6" _proto_push_route
254         _proto_push_array "ip6prefix" "$PROTO_PREFIX6" _proto_push_string
255         _proto_push_array "dns" "$PROTO_DNS" _proto_push_string
256         _proto_push_array "dns_search" "$PROTO_DNS_SEARCH" _proto_push_string
257         _proto_notify "$interface"
258 }
259
260 proto_export() {
261         local var="VAR${_EXPORT_VAR}"
262         _EXPORT_VAR="$(($_EXPORT_VAR + 1))"
263         export -- "$var=$1"
264         append _EXPORT_VARS "$var"
265 }
266
267 proto_run_command() {
268         local interface="$1"; shift
269
270         json_init
271         json_add_int action 1
272         json_add_array command
273         while [ $# -gt 0 ]; do
274                 json_add_string "" "$1"
275                 shift
276         done
277         json_close_array
278         [ -n "$_EXPORT_VARS" ] && {
279                 json_add_array env
280                 for var in $_EXPORT_VARS; do
281                         eval "json_add_string \"\" \"\${$var}\""
282                 done
283                 json_close_array
284         }
285         _proto_notify "$interface"
286 }
287
288 proto_kill_command() {
289         local interface="$1"; shift
290
291         json_init
292         json_add_int action 2
293         [ -n "$1" ] && json_add_int signal "$1"
294         _proto_notify "$interface"
295 }
296
297 proto_notify_error() {
298         local interface="$1"; shift
299
300         json_init
301         json_add_int action 3
302         json_add_array error
303         while [ $# -gt 0 ]; do
304                 json_add_string "" "$1"
305                 shift
306         done
307         json_close_array
308         _proto_notify "$interface"
309 }
310
311 proto_block_restart() {
312         local interface="$1"; shift
313
314         json_init
315         json_add_int action 4
316         _proto_notify "$interface"
317 }
318
319 proto_set_available() {
320         local interface="$1"
321         local state="$2"
322         json_init
323         json_add_int action 5
324         json_add_boolean available "$state"
325         _proto_notify "$interface"
326 }
327
328 proto_add_host_dependency() {
329         local interface="$1"
330         local host="$2"
331         local ifname="$3"
332
333         # execute in subshell to not taint callers env
334         # see tickets #11046, #11545, #11570
335         (
336                 json_init
337                 json_add_int action 6
338                 json_add_string host "$host"
339                 [ -n "$ifname" ] && json_add_string ifname "$ifname"
340                 _proto_notify "$interface" -S
341         )
342 }
343
344 proto_setup_failed() {
345         local interface="$1"
346         json_init
347         json_add_int action 7
348         _proto_notify "$interface"
349 }
350
351 init_proto() {
352         proto="$1"; shift
353         cmd="$1"; shift
354
355         case "$cmd" in
356                 dump)
357                         add_protocol() {
358                                 no_device=0
359                                 available=0
360                                 renew_handler=0
361
362                                 add_default_handler "proto_$1_init_config"
363
364                                 json_init
365                                 json_add_string "name" "$1"
366                                 json_add_array "config"
367                                 eval "proto_$1_init_config"
368                                 json_close_array
369                                 json_add_boolean no-device "$no_device"
370                                 json_add_boolean available "$available"
371                                 json_add_boolean renew-handler "$renew_handler"
372                                 json_dump
373                         }
374                 ;;
375                 setup|teardown|renew)
376                         interface="$1"; shift
377                         data="$1"; shift
378                         ifname="$1"; shift
379
380                         add_protocol() {
381                                 [[ "$proto" == "$1" ]] || return 0
382
383                                 case "$cmd" in
384                                         setup) _proto_do_setup "$1";;
385                                         teardown) _proto_do_teardown "$1" ;;
386                                         renew) _proto_do_renew "$1" ;;
387                                         *) return 1 ;;
388                                 esac
389                         }
390                 ;;
391         esac
392 }