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$3}\"; [ -n \"\${$__v+x}\" ]"
19 __network_parse_ifstatus()
22 local __key="${__iface}"
26 __network_export __tmp "${__key}__parsed" && return 0
27 __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
28 [ -n "$__tmp" ] || return 1
30 json_set_namespace "network" __old_ns
33 __network_set_cache "${__key}__parsed" "" "1"
35 for __tmp in "" "_inactive"; do
37 __key="${__key}${__tmp}"
41 for __family in 4 6; do
42 if json_is_a "ipv${__family}_address" array; then
44 json_select "ipv${__family}_address"
46 if json_is_a 1 object; then
49 __network_set_cache "${__key}_address${__family}" address
50 __network_set_cache "${__key}_mask${__family}" mask
61 if json_is_a "ipv6_prefix" array; then
62 json_select "ipv6_prefix"
64 if json_is_a 1 object; then
66 __network_set_cache "${__key}_prefix6_address" address
67 __network_set_cache "${__key}_prefix6_mask" mask
75 if json_is_a route array; then
80 while json_is_a "$__idx" object; do
82 json_select "$((__idx++))"
83 json_get_var __tmp target
87 __network_set_cache "${__key}_gateway4" nexthop
90 __network_set_cache "${__key}_gateway6" nexthop
104 for __field in "dns_server" "dns_search"; do
105 if json_is_a "$__field" array; then
107 json_select "$__field"
112 while json_is_a "$__idx" string; do
114 json_get_var __tmp "$((__idx++))"
115 __dns="${__dns:+$__dns }$__tmp"
121 if [ -n "$__dns" ]; then
122 __network_set_cache "${__key}_${__field}" "" "$__dns"
127 # parse up state, device and physdev
128 for __field in "up" "l3_device" "device"; do
129 if json_get_type __tmp "$__field"; then
130 __network_set_cache "${__key}_${__field}" "$__field"
134 # descend into inactive table
135 json_is_a "inactive" object && json_select "inactive"
140 json_set_namespace "$__old_ns"
154 __network_parse_ifstatus "$__iface" || return 1
156 if [ $__prefix -eq 1 ]; then
157 __network_export __tmp "${__iface}_mask${__family}" && \
158 __network_export "$__var" "${__iface}_address${__family}" "/$__tmp"
162 __network_export "$__var" "${__iface}_address${__family}"
167 # determine IPv4 address of given logical interface
168 # 1: destination variable
170 network_get_ipaddr() { __network_ipaddr "$1" "$2" 4 0; }
172 # determine IPv6 address of given logical interface
173 # 1: destination variable
175 network_get_ipaddr6() { __network_ipaddr "$1" "$2" 6 0; }
177 # determine IPv4 subnet of given logical interface
178 # 1: destination variable
180 network_get_subnet() { __network_ipaddr "$1" "$2" 4 1; }
182 # determine IPv6 subnet of given logical interface
183 # 1: destination variable
185 network_get_subnet6() { __network_ipaddr "$1" "$2" 6 1; }
187 # determine IPv6 prefix
188 network_get_prefix6() {
194 __network_parse_ifstatus "$__iface" || return 1
195 __network_export __mask "${__iface}_prefix6_mask" || return 1
196 __network_export __var "${__iface}_prefix6_address" "/$__mask"
206 local __inactive="$4"
208 __network_parse_ifstatus "$__iface" || return 1
210 if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then
211 __network_export "$__var" "${__iface}_inactive_gateway${__family}" && \
215 __network_export "$__var" "${__iface}_gateway${__family}"
219 # determine IPv4 gateway of given logical interface
220 # 1: destination variable
222 # 3: consider inactive gateway if "true" (optional)
223 network_get_gateway() { __network_gateway "$1" "$2" 4 "${3:-0}"; }
225 # determine IPv6 gateway of given logical interface
226 # 1: destination variable
228 # 3: consider inactive gateway if "true" (optional)
229 network_get_gateway6() { __network_gateway "$1" "$2" 6 "${3:-0}"; }
236 local __inactive="$4"
238 __network_parse_ifstatus "$__iface" || return 1
240 if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then
241 __network_export "$__var" "${__iface}_inactive_${__field}" && \
245 __network_export "$__var" "${__iface}_${__field}"
249 # determine the DNS servers of the given logical interface
250 # 1: destination variable
252 # 3: consider inactive servers if "true" (optional)
253 network_get_dnsserver() { __network_dns "$1" "$2" dns_server "${3:-0}"; }
255 # determine the domains of the given logical interface
256 # 1: destination variable
258 # 3: consider inactive domains if "true" (optional)
259 network_get_dnssearch() { __network_dns "$1" "$2" dns_search "${3:-0}"; }
266 local __inactive="$3"
269 for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do
270 if [ "$__iface" != loopback ]; then
271 if __network_gateway "$__var" "$__iface" "$__family" "$__inactive"; then
272 eval "export -- \"$__var=$__iface\""
278 eval "export -- \"$__var=\""
282 # find the logical interface which holds the current IPv4 default route
283 # 1: destination variable
284 # 2: consider inactive default routes if "true" (optional)
285 network_find_wan() { __network_wan "$1" 4 "${2:-0}"; }
287 # find the logical interface which holds the current IPv6 default route
288 # 1: destination variable
289 # 2: consider inactive dafault routes if "true" (optional)
290 network_find_wan6() { __network_wan "$1" 6 "${2:-0}"; }
299 __network_parse_ifstatus "$__iface" || return 1
300 __network_export "$__var" "${__iface}_${__field}"
304 # test whether the given logical interface is running
309 __network_device __up "$1" up && [ $__up -eq 1 ]
312 # determine the layer 3 linux network device of the given logical interface
313 # 1: destination variable
315 network_get_device() { __network_device "$1" "$2" l3_device; }
317 # determine the layer 2 linux network device of the given logical interface
318 # 1: destination variable
320 network_get_physdev() { __network_device "$1" "$2" device; }
329 json_add_string name "$__device"
330 json_add_boolean defer "$__defer"
332 ubus call network.device set_state "$(json_dump)" 2>/dev/null
335 # defer netifd actions on the given linux network device
337 network_defer_device() { __network_defer "$1" 1; }
339 # continue netifd actions on the given linux network device
341 network_ready_device() { __network_defer "$1" 0; }
343 # flush the internal value cache to force re-reading values from ubus
344 network_flush_cache()
347 for __tmp in $__NETWORK_CACHE __NETWORK_CACHE; do