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}"
28 __network_export __tmp "${__key}__parsed" && return 0
29 __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
30 [ -n "$__tmp" ] || return 1
32 json_set_namespace "network" __old_ns
35 __network_set_cache "${__key}__parsed" "" "1"
37 for __tmp in "" "_inactive"; do
39 __key="${__key}${__tmp}"
43 for __family in 4 6; do
47 if json_is_a "ipv${__family}_address" array; then
49 json_select "ipv${__family}_address"
53 while json_is_a "$__idx" object; do
55 json_select "$((__idx++))"
56 json_get_var __tmp "address" && __list="${__list:+$__list }$__tmp"
57 json_get_var __tmp "mask" && __list="${__list:+$__list/}$__tmp"
66 if json_is_a "ipv${__family}_prefix_assignment" array; then
68 json_select "ipv${__family}_prefix_assignment"
72 while json_is_a "$__idx" object; do
74 json_select "$((__idx++))"
75 json_get_var __tmp "address" && __list="${__list:+$__list }${__tmp}1"
76 json_get_var __tmp "mask" && __list="${__list:+$__list/}$__tmp"
85 if [ -n "$__list" ]; then
86 __network_set_cache "${__key}_address${__family}" "" "$__list"
92 if json_is_a "ipv6_prefix" array; then
93 json_select "ipv6_prefix"
98 while json_is_a "$__idx" object; do
100 json_select "$((__idx++))"
101 json_get_var __tmp "address" && __list="${__list:+$__list }$__tmp"
102 json_get_var __tmp "mask" && __list="${__list:+$__list/}$__tmp"
110 if [ -n "$__list" ]; then
111 __network_set_cache "${__key}_prefix6" "" "$__list"
117 if json_is_a route array; then
122 while json_is_a "$__idx" object; do
124 json_select "$((__idx++))"
125 json_get_var __tmp table
127 if [ -z "$__tmp" ]; then
128 json_get_var __tmp target
132 __network_set_cache "${__key}_gateway4" nexthop
135 __network_set_cache "${__key}_gateway6" nexthop
150 for __field in "dns_server" "dns_search"; do
151 if json_is_a "$__field" array; then
153 json_select "$__field"
158 while json_is_a "$__idx" string; do
160 json_get_var __tmp "$((__idx++))"
161 __list="${__list:+$__list }$__tmp"
167 if [ -n "$__list" ]; then
168 __network_set_cache "${__key}_${__field}" "" "$__list"
173 # parse up state, device and physdev
174 for __field in "up" "l3_device" "device"; do
175 if json_get_type __tmp "$__field"; then
176 __network_set_cache "${__key}_${__field}" "$__field"
180 # descend into inactive table
181 json_is_a "inactive" object && json_select "inactive"
186 json_set_namespace "$__old_ns"
201 __network_parse_ifstatus "$__iface" || return 1
203 if [ $__list = 1 ] && [ -n "$__subst" ]; then
204 __network_export "__list" "${__iface}_${__field}"
206 for __list in $__list; do
207 eval "__tmp=\"${__tmp:+$__tmp }\${__list$__subst}\""
210 export -- "$__var=$__tmp"; [ -n "$__tmp" ]
214 __network_export "$__var" "${__iface}_${__field}" "" "$__subst"
218 # determine first IPv4 address of given logical interface
219 # 1: destination variable
221 network_get_ipaddr() { __network_ipaddr "$1" "$2" "address4" "%%/*" 0; }
223 # determine first IPv6 address of given logical interface
224 # 1: destination variable
226 network_get_ipaddr6() { __network_ipaddr "$1" "$2" "address6" "%%/*" 0; }
228 # determine first IPv4 subnet of given logical interface
229 # 1: destination variable
231 network_get_subnet() { __network_ipaddr "$1" "$2" "address4" "%% *" 0; }
233 # determine first IPv6 subnet of given logical interface
234 # 1: destination variable
236 network_get_subnet6() { __network_ipaddr "$1" "$2" "address6" "%% *" 0; }
238 # determine first IPv6 prefix of given logical interface
239 # 1: destination variable
241 network_get_prefix6() { __network_ipaddr "$1" "$2" "prefix6" "%% *" 0; }
243 # determine all IPv4 addresses of given logical interface
244 # 1: destination variable
246 network_get_ipaddrs() { __network_ipaddr "$1" "$2" "address4" "%%/*" 1; }
248 # determine all IPv6 addresses of given logical interface
249 # 1: destination variable
251 network_get_ipaddrs6() { __network_ipaddr "$1" "$2" "address6" "%%/*" 1; }
253 # determine all IPv4 subnets of given logical interface
254 # 1: destination variable
256 network_get_subnets() { __network_ipaddr "$1" "$2" "address4" "" 1; }
258 # determine all IPv6 subnets of given logical interface
259 # 1: destination variable
261 network_get_subnets6() { __network_ipaddr "$1" "$2" "address6" "" 1; }
263 # determine all IPv6 prefixes of given logical interface
264 # 1: destination variable
266 network_get_prefixes6() { __network_ipaddr "$1" "$2" "prefix6" "" 1; }
274 local __inactive="$4"
276 __network_parse_ifstatus "$__iface" || return 1
278 if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then
279 __network_export "$__var" "${__iface}_inactive_gateway${__family}" && \
283 __network_export "$__var" "${__iface}_gateway${__family}"
287 # determine IPv4 gateway of given logical interface
288 # 1: destination variable
290 # 3: consider inactive gateway if "true" (optional)
291 network_get_gateway() { __network_gateway "$1" "$2" 4 "${3:-0}"; }
293 # determine IPv6 gateway of given logical interface
294 # 1: destination variable
296 # 3: consider inactive gateway if "true" (optional)
297 network_get_gateway6() { __network_gateway "$1" "$2" 6 "${3:-0}"; }
304 local __inactive="$4"
306 __network_parse_ifstatus "$__iface" || return 1
308 if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then
309 __network_export "$__var" "${__iface}_inactive_${__field}" && \
313 __network_export "$__var" "${__iface}_${__field}"
317 # determine the DNS servers of the given logical interface
318 # 1: destination variable
320 # 3: consider inactive servers if "true" (optional)
321 network_get_dnsserver() { __network_dns "$1" "$2" dns_server "${3:-0}"; }
323 # determine the domains of the given logical interface
324 # 1: destination variable
326 # 3: consider inactive domains if "true" (optional)
327 network_get_dnssearch() { __network_dns "$1" "$2" dns_search "${3:-0}"; }
334 local __inactive="$3"
337 for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do
338 if [ "$__iface" != loopback ]; then
339 if __network_gateway "$__var" "$__iface" "$__family" "$__inactive"; then
340 eval "export -- \"$__var=$__iface\""
346 eval "export -- \"$__var=\""
350 # find the logical interface which holds the current IPv4 default route
351 # 1: destination variable
352 # 2: consider inactive default routes if "true" (optional)
353 network_find_wan() { __network_wan "$1" 4 "${2:-0}"; }
355 # find the logical interface which holds the current IPv6 default route
356 # 1: destination variable
357 # 2: consider inactive dafault routes if "true" (optional)
358 network_find_wan6() { __network_wan "$1" 6 "${2:-0}"; }
367 __network_parse_ifstatus "$__iface" || return 1
368 __network_export "$__var" "${__iface}_${__field}"
372 # test whether the given logical interface is running
377 __network_device __up "$1" up && [ $__up -eq 1 ]
380 # determine the layer 3 linux network device of the given logical interface
381 # 1: destination variable
383 network_get_device() { __network_device "$1" "$2" l3_device; }
385 # determine the layer 2 linux network device of the given logical interface
386 # 1: destination variable
388 network_get_physdev() { __network_device "$1" "$2" device; }
397 json_add_string name "$__device"
398 json_add_boolean defer "$__defer"
400 ubus call network.device set_state "$(json_dump)" 2>/dev/null
403 # defer netifd actions on the given linux network device
405 network_defer_device() { __network_defer "$1" 1; }
407 # continue netifd actions on the given linux network device
409 network_ready_device() { __network_defer "$1" 0; }
411 # flush the internal value cache to force re-reading values from ubus
412 network_flush_cache()
415 for __tmp in $__NETWORK_CACHE __NETWORK_CACHE; do