1 . /usr/share/libubox/jshn.sh
6 eval "export -- __NETWORK_CV_$1='$3'"
7 __NETWORK_CACHE="${__NETWORK_CACHE:+$__NETWORK_CACHE }__NETWORK_CV_$1"
8 elif json_get_var "__NETWORK_CV_$1" "$2"; then
9 __NETWORK_CACHE="${__NETWORK_CACHE:+$__NETWORK_CACHE }__NETWORK_CV_$1"
15 local __v="__NETWORK_CV_$2"
16 eval "export -- \"$1=\${$__v:+\${$__v$4}$3}\"; [ -n \"\${$__v+x}\" ]"
19 __network_parse_ifstatus()
22 local __key="${__iface}"
29 *[^a-zA-Z0-9_]*) return 1 ;;
32 __network_export __tmp "${__key}__parsed" && return 0
33 __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
34 [ -n "$__tmp" ] || return 1
36 json_set_namespace "network" __old_ns
39 __network_set_cache "${__key}__parsed" "" "1"
41 for __tmp in "" "_inactive"; do
43 __key="${__key}${__tmp}"
47 for __family in 4 6; do
51 if json_is_a "ipv${__family}_address" array; then
53 json_select "ipv${__family}_address"
57 while json_is_a "$__idx" object; do
59 json_select "$((__idx++))"
60 json_get_var __tmp "address" && __list="${__list:+$__list }$__tmp"
61 json_get_var __tmp "mask" && __list="${__list:+$__list/}$__tmp"
70 if json_is_a "ipv${__family}_prefix_assignment" array; then
72 json_select "ipv${__family}_prefix_assignment"
76 while json_is_a "$__idx" object; do
78 json_select "$((__idx++))"
79 json_get_var __tmp "address" && __list="${__list:+$__list }${__tmp}1"
80 json_get_var __tmp "mask" && __list="${__list:+$__list/}$__tmp"
89 if [ -n "$__list" ]; then
90 __network_set_cache "${__key}_address${__family}" "" "$__list"
96 if json_is_a "ipv6_prefix" array; then
97 json_select "ipv6_prefix"
102 while json_is_a "$__idx" object; do
104 json_select "$((__idx++))"
105 json_get_var __tmp "address" && __list="${__list:+$__list }$__tmp"
106 json_get_var __tmp "mask" && __list="${__list:+$__list/}$__tmp"
114 if [ -n "$__list" ]; then
115 __network_set_cache "${__key}_prefix6" "" "$__list"
121 if json_is_a route array; then
126 while json_is_a "$__idx" object; do
128 json_select "$((__idx++))"
129 json_get_var __tmp table
131 if [ -z "$__tmp" ]; then
132 json_get_var __tmp target
136 __network_set_cache "${__key}_gateway4" nexthop
139 __network_set_cache "${__key}_gateway6" nexthop
154 for __field in "dns_server" "dns_search"; do
155 if json_is_a "$__field" array; then
157 json_select "$__field"
162 while json_is_a "$__idx" string; do
164 json_get_var __tmp "$((__idx++))"
165 __list="${__list:+$__list }$__tmp"
171 if [ -n "$__list" ]; then
172 __network_set_cache "${__key}_${__field}" "" "$__list"
177 # parse up state, device and physdev
178 for __field in "up" "l3_device" "device"; do
179 if json_get_type __tmp "$__field"; then
180 __network_set_cache "${__key}_${__field}" "$__field"
184 # descend into inactive table
185 json_is_a "inactive" object && json_select "inactive"
190 json_set_namespace "$__old_ns"
205 __network_parse_ifstatus "$__iface" || return 1
207 if [ $__list = 1 ] && [ -n "$__subst" ]; then
208 __network_export "__list" "${__iface}_${__field}"
210 for __list in $__list; do
211 eval "__tmp=\"${__tmp:+$__tmp }\${__list$__subst}\""
214 export -- "$__var=$__tmp"; [ -n "$__tmp" ]
218 __network_export "$__var" "${__iface}_${__field}" "" "$__subst"
222 # determine first IPv4 address of given logical interface
223 # 1: destination variable
225 network_get_ipaddr() { __network_ipaddr "$1" "$2" "address4" "%%/*" 0; }
227 # determine first IPv6 address of given logical interface
228 # 1: destination variable
230 network_get_ipaddr6() { __network_ipaddr "$1" "$2" "address6" "%%/*" 0; }
232 # determine first IPv4 subnet of given logical interface
233 # 1: destination variable
235 network_get_subnet() { __network_ipaddr "$1" "$2" "address4" "%% *" 0; }
237 # determine first IPv6 subnet of given logical interface
238 # 1: destination variable
240 network_get_subnet6() { __network_ipaddr "$1" "$2" "address6" "%% *" 0; }
242 # determine first IPv6 prefix of given logical interface
243 # 1: destination variable
245 network_get_prefix6() { __network_ipaddr "$1" "$2" "prefix6" "%% *" 0; }
247 # determine all IPv4 addresses of given logical interface
248 # 1: destination variable
250 network_get_ipaddrs() { __network_ipaddr "$1" "$2" "address4" "%%/*" 1; }
252 # determine all IPv6 addresses of given logical interface
253 # 1: destination variable
255 network_get_ipaddrs6() { __network_ipaddr "$1" "$2" "address6" "%%/*" 1; }
257 # determine all IPv4 subnets of given logical interface
258 # 1: destination variable
260 network_get_subnets() { __network_ipaddr "$1" "$2" "address4" "" 1; }
262 # determine all IPv6 subnets of given logical interface
263 # 1: destination variable
265 network_get_subnets6() { __network_ipaddr "$1" "$2" "address6" "" 1; }
267 # determine all IPv6 prefixes of given logical interface
268 # 1: destination variable
270 network_get_prefixes6() { __network_ipaddr "$1" "$2" "prefix6" "" 1; }
278 local __inactive="$4"
280 __network_parse_ifstatus "$__iface" || return 1
282 if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then
283 __network_export "$__var" "${__iface}_inactive_gateway${__family}" && \
287 __network_export "$__var" "${__iface}_gateway${__family}"
291 # determine IPv4 gateway of given logical interface
292 # 1: destination variable
294 # 3: consider inactive gateway if "true" (optional)
295 network_get_gateway() { __network_gateway "$1" "$2" 4 "${3:-0}"; }
297 # determine IPv6 gateway of given logical interface
298 # 1: destination variable
300 # 3: consider inactive gateway if "true" (optional)
301 network_get_gateway6() { __network_gateway "$1" "$2" 6 "${3:-0}"; }
308 local __inactive="$4"
310 __network_parse_ifstatus "$__iface" || return 1
312 if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then
313 __network_export "$__var" "${__iface}_inactive_${__field}" && \
317 __network_export "$__var" "${__iface}_${__field}"
321 # determine the DNS servers of the given logical interface
322 # 1: destination variable
324 # 3: consider inactive servers if "true" (optional)
325 network_get_dnsserver() { __network_dns "$1" "$2" dns_server "${3:-0}"; }
327 # determine the domains of the given logical interface
328 # 1: destination variable
330 # 3: consider inactive domains if "true" (optional)
331 network_get_dnssearch() { __network_dns "$1" "$2" dns_search "${3:-0}"; }
338 local __inactive="$3"
341 for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do
342 if [ "$__iface" != loopback ]; then
343 if __network_gateway "$__var" "$__iface" "$__family" "$__inactive"; then
344 eval "export -- \"$__var=$__iface\""
350 eval "export -- \"$__var=\""
354 # find the logical interface which holds the current IPv4 default route
355 # 1: destination variable
356 # 2: consider inactive default routes if "true" (optional)
357 network_find_wan() { __network_wan "$1" 4 "${2:-0}"; }
359 # find the logical interface which holds the current IPv6 default route
360 # 1: destination variable
361 # 2: consider inactive dafault routes if "true" (optional)
362 network_find_wan6() { __network_wan "$1" 6 "${2:-0}"; }
371 __network_parse_ifstatus "$__iface" || return 1
372 __network_export "$__var" "${__iface}_${__field}"
376 # test whether the given logical interface is running
381 __network_device __up "$1" up && [ $__up -eq 1 ]
384 # determine the layer 3 linux network device of the given logical interface
385 # 1: destination variable
387 network_get_device() { __network_device "$1" "$2" l3_device; }
389 # determine the layer 2 linux network device of the given logical interface
390 # 1: destination variable
392 network_get_physdev() { __network_device "$1" "$2" device; }
401 json_add_string name "$__device"
402 json_add_boolean defer "$__defer"
404 ubus call network.device set_state "$(json_dump)" 2>/dev/null
407 # defer netifd actions on the given linux network device
409 network_defer_device() { __network_defer "$1" 1; }
411 # continue netifd actions on the given linux network device
413 network_ready_device() { __network_defer "$1" 0; }
415 # flush the internal value cache to force re-reading values from ubus
416 network_flush_cache()
419 for __tmp in $__NETWORK_CACHE __NETWORK_CACHE; do