• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/netifd/scripts/netifd-proto.sh

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt