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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt