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 first IPv6 prefix assignment of given logical interface
94 # 1: destination variable
96 network_get_prefix_assignment6() {
97 __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][0]['address','mask']" "/"
100 # determine all IPv4 addresses of given logical interface
101 # 1: destination variable
103 network_get_ipaddrs() {
104 __network_ifstatus "$1" "$2" "['ipv4-address'][*].address
"
107 # determine all IPv6 addresses of given logical interface
108 # 1: destination variable
110 network_get_ipaddrs6() {
114 if __network_ifstatus "__addr
" "$2" "['ipv6-address'][*].address
"; then
115 for __addr in $__addr; do
116 __list="${__list:+$__list }${__addr}"
120 if __network_ifstatus "__addr
" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address
"; then
121 for __addr in $__addr; do
122 __list="${__list:+$__list }${__addr}"
126 if [ -n "$__list" ]; then
135 # determine all IP addresses of given logical interface
136 # 1: destination variable
138 network_get_ipaddrs_all() {
141 network_get_ipaddrs __addr "$2"
142 network_get_ipaddrs6 __addr6 "$2"
144 if [ -n "$__addr" -o -n "$__addr6" ]; then
145 export "$1=${__addr:+$__addr }$__addr6"
153 # determine all IPv4 subnets of given logical interface
154 # 1: destination variable
156 network_get_subnets() {
157 __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
160 # determine all IPv6 subnets of given logical interface
161 # 1: destination variable
163 network_get_subnets6() {
167 if __network_ifstatus "__addr
" "$2" "['ipv6-address'][*]['address','mask']" "/ "; then
168 for __addr in $__addr; do
169 __list="${__list:+$__list }${__addr}"
173 if __network_ifstatus "__addr
" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address
" && \
174 __network_ifstatus "__mask
" "$2" "['ipv6-prefix-assignment'][*].mask
"; then
175 for __addr in $__addr; do
176 __list="${__list:+$__list }${__addr}/${__mask%% *}"
177 __mask="${__mask#* }"
181 if [ -n "$__list" ]; then
190 # determine all IPv6 prefixes of given logical interface
191 # 1: destination variable
193 network_get_prefixes6() {
194 __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
197 # determine all IPv6 prefix assignments of given logical interface
198 # 1: destination variable
200 network_get_prefix_assignments6() {
201 __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][*]['address','mask']" "/ "
204 # determine IPv4 gateway of given logical interface
205 # 1: destination variable
207 # 3: consider inactive gateway if "true
" (optional)
208 network_get_gateway() {
209 __network_ifstatus "$1" "$2" ".route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1 && \
212 [ "$3" = 1 -o "$3" = "true
" ] && \
213 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1
216 # determine IPv6 gateway of given logical interface
217 # 1: destination variable
219 # 3: consider inactive gateway if "true
" (optional)
220 network_get_gateway6() {
221 __network_ifstatus "$1" "$2" ".route
[@.target
='::' && !@.table
].nexthop
" "" 1 && \
224 [ "$3" = 1 -o "$3" = "true
" ] && \
225 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='::' && !@.table
].nexthop
" "" 1
228 # determine the DNS servers of the given logical interface
229 # 1: destination variable
231 # 3: consider inactive servers if "true
" (optional)
232 network_get_dnsserver() {
233 __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
235 [ "$3" = 1 -o "$3" = "true
" ] && \
236 __network_ifstatus "$1" "$2" ".inactive
['dns-server'][*]"
239 # determine the domains of the given logical interface
240 # 1: destination variable
242 # 3: consider inactive domains if "true
" (optional)
243 network_get_dnssearch() {
244 __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
246 [ "$3" = 1 -o "$3" = "true
" ] && \
247 __network_ifstatus "$1" "$2" ".inactive
['dns-search'][*]"
251 # 1: destination variable
256 __network_ifstatus "$1" "" \
257 "[@.route
[@.target
='$2' && !@.table
]].interface
" "" 1 && \
260 [ "$3" = 1 -o "$3" = "true
" ] && \
261 __network_ifstatus "$1" "" \
262 "[@.inactive.route
[@.target
='$2' && !@.table
]].interface
" "" 1
265 # find the logical interface which holds the current IPv4 default route
266 # 1: destination variable
267 # 2: consider inactive default routes if "true
" (optional)
268 network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
270 # find the logical interface which holds the current IPv6 default route
271 # 1: destination variable
272 # 2: consider inactive default routes if "true
" (optional)
273 network_find_wan6() { __network_wan "$1" "::" "$2"; }
275 # test whether the given logical interface is running
280 __network_ifstatus "__up
" "$1" ".up
" && [ "$__up" = 1 ]
283 # determine the protocol of the given logical interface
284 # 1: destination variable
286 network_get_protocol() { __network_ifstatus "$1" "$2" ".proto
"; }
288 # determine the uptime of the given logical interface
289 # 1: destination variable
291 network_get_uptime() { __network_ifstatus "$1" "$2" ".uptime
"; }
293 # determine the metric of the given logical interface
294 # 1: destination variable
296 network_get_metric() { __network_ifstatus "$1" "$2" ".metric
"; }
298 # determine the layer 3 linux network device of the given logical interface
299 # 1: destination variable
301 network_get_device() { __network_ifstatus "$1" "$2" ".l3_device
"; }
303 # determine the layer 2 linux network device of the given logical interface
304 # 1: destination variable
306 network_get_physdev() { __network_ifstatus "$1" "$2" ".device
"; }
308 # defer netifd actions on the given linux network device
310 network_defer_device()
312 ubus call network.device set_state \
313 "$
(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
316 # continue netifd actions on the given linux network device
318 network_ready_device()
320 ubus call network.device set_state \
321 "$
(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
324 # flush the internal value cache to force re-reading values from ubus
325 network_flush_cache() { unset __NETWORK_CACHE; }