1 # 1: destination variable
9 [ -z "$__NETWORK_CACHE" ] && {
10 __tmp
="$(ubus call network.interface dump 2>&1)"
13 0) export __NETWORK_CACHE
="$__tmp" ;;
14 *) echo "$__tmp" >&2 ;;
18 __tmp
="$(jsonfilter ${4:+-F "$4"} ${5:+-l "$5"} -s "${__NETWORK_CACHE:-{}}" -e "$1=@.interface${2:+[@.interface='$2']}$3")"
27 # determine first IPv4 address of given logical interface
28 # 1: destination variable
30 network_get_ipaddr() {
31 __network_ifstatus "$1" "$2" "['ipv4-address'][0].address
";
34 # determine first IPv6 address of given logical interface
35 # 1: destination variable
37 network_get_ipaddr6() {
38 __network_ifstatus "$1" "$2" "['ipv6-address'][0].address
" || \
39 __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][0]['local-address'].address
" || \
43 # determine first IPv4 subnet of given logical interface
44 # 1: destination variable
46 network_get_subnet() {
47 __network_ifstatus "$1" "$2" "['ipv4-address'][0]['address','mask']" "/"
50 # determine first IPv6 subnet of given logical interface
51 # 1: destination variable
53 network_get_subnet6() {
56 if network_get_subnets6 __nets "$2"; then
57 # Attempt to return first non-fe80::/10, non-fc::/7 range
58 for __addr in $__nets; do
59 case "$__addr" in fe[8ab]?:*|f[cd]??:*)
66 # Attempt to return first non-fe80::/10 range
67 for __addr in $__nets; do
68 case "$__addr" in fe[8ab]?:*)
76 for __addr in $__nets; do
86 # determine first IPv6 prefix of given logical interface
87 # 1: destination variable
89 network_get_prefix6() {
90 __network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/"
93 # determine all IPv4 addresses of given logical interface
94 # 1: destination variable
96 network_get_ipaddrs() {
97 __network_ifstatus "$1" "$2" "['ipv4-address'][*].address
"
100 # determine all IPv6 addresses of given logical interface
101 # 1: destination variable
103 network_get_ipaddrs6() {
107 if __network_ifstatus "__addr
" "$2" "['ipv6-address'][*].address
"; then
108 for __addr in $__addr; do
109 __list="${__list:+$__list }${__addr}"
113 if __network_ifstatus "__addr
" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address
"; then
114 for __addr in $__addr; do
115 __list="${__list:+$__list }${__addr}"
119 if [ -n "$__list" ]; then
128 # determine all IP addresses of given logical interface
129 # 1: destination variable
131 network_get_ipaddrs_all() {
134 network_get_ipaddrs __addr "$2"
135 network_get_ipaddrs6 __addr6 "$2"
137 if [ -n "$__addr" -o -n "$__addr6" ]; then
138 export "$1=${__addr:+$__addr }$__addr6"
146 # determine all IPv4 subnets of given logical interface
147 # 1: destination variable
149 network_get_subnets() {
150 __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
153 # determine all IPv6 subnets of given logical interface
154 # 1: destination variable
156 network_get_subnets6() {
160 if __network_ifstatus "__addr
" "$2" "['ipv6-address'][*]['address','mask']" "/ "; then
161 for __addr in $__addr; do
162 __list="${__list:+$__list }${__addr}"
166 if __network_ifstatus "__addr
" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address
" && \
167 __network_ifstatus "__mask
" "$2" "['ipv6-prefix-assignment'][*].mask
"; then
168 for __addr in $__addr; do
169 __list="${__list:+$__list }${__addr}/${__mask%% *}"
170 __mask="${__mask#* }"
174 if [ -n "$__list" ]; then
183 # determine all IPv6 prefixes of given logical interface
184 # 1: destination variable
186 network_get_prefixes6() {
187 __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
190 # determine IPv4 gateway of given logical interface
191 # 1: destination variable
193 # 3: consider inactive gateway if "true
" (optional)
194 network_get_gateway() {
195 __network_ifstatus "$1" "$2" ".route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1 && \
198 [ "$3" = 1 -o "$3" = "true
" ] && \
199 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1
202 # determine IPv6 gateway of given logical interface
203 # 1: destination variable
205 # 3: consider inactive gateway if "true
" (optional)
206 network_get_gateway6() {
207 __network_ifstatus "$1" "$2" ".route
[@.target
='::' && !@.table
].nexthop
" "" 1 && \
210 [ "$3" = 1 -o "$3" = "true
" ] && \
211 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='::' && !@.table
].nexthop
" "" 1
214 # determine the DNS servers of the given logical interface
215 # 1: destination variable
217 # 3: consider inactive servers if "true
" (optional)
218 network_get_dnsserver() {
219 __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
221 [ "$3" = 1 -o "$3" = "true
" ] && \
222 __network_ifstatus "$1" "$2" ".inactive
['dns-server'][*]"
225 # determine the domains of the given logical interface
226 # 1: destination variable
228 # 3: consider inactive domains if "true
" (optional)
229 network_get_dnssearch() {
230 __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
232 [ "$3" = 1 -o "$3" = "true
" ] && \
233 __network_ifstatus "$1" "$2" ".inactive
['dns-search'][*]"
237 # 1: destination variable
242 __network_ifstatus "$1" "" \
243 "[@.route
[@.target
='$2' && !@.table
]].interface
" "" 1 && \
246 [ "$3" = 1 -o "$3" = "true
" ] && \
247 __network_ifstatus "$1" "" \
248 "[@.inactive.route
[@.target
='$2' && !@.table
]].interface
" "" 1
251 # find the logical interface which holds the current IPv4 default route
252 # 1: destination variable
253 # 2: consider inactive default routes if "true
" (optional)
254 network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
256 # find the logical interface which holds the current IPv6 default route
257 # 1: destination variable
258 # 2: consider inactive dafault routes if "true
" (optional)
259 network_find_wan6() { __network_wan "$1" "::" "$2"; }
261 # test whether the given logical interface is running
266 __network_ifstatus "__up
" "$1" ".up
" && [ "$__up" = 1 ]
269 # determine the protocol of the given logical interface
270 # 1: destination variable
272 network_get_protocol() { __network_ifstatus "$1" "$2" ".proto
"; }
274 # determine the uptime of the given logical interface
275 # 1: destination variable
277 network_get_uptime() { __network_ifstatus "$1" "$2" ".uptime
"; }
279 # determine the metric of the given logical interface
280 # 1: destination variable
282 network_get_metric() { __network_ifstatus "$1" "$2" ".metric
"; }
284 # determine the layer 3 linux network device of the given logical interface
285 # 1: destination variable
287 network_get_device() { __network_ifstatus "$1" "$2" ".l3_device
"; }
289 # determine the layer 2 linux network device of the given logical interface
290 # 1: destination variable
292 network_get_physdev() { __network_ifstatus "$1" "$2" ".device
"; }
294 # defer netifd actions on the given linux network device
296 network_defer_device()
298 ubus call network.device set_state \
299 "$
(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
302 # continue netifd actions on the given linux network device
304 network_ready_device()
306 ubus call network.device set_state \
307 "$
(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
310 # flush the internal value cache to force re-reading values from ubus
311 network_flush_cache() { unset __NETWORK_CACHE; }