1 # 1: destination variable
9 [ -z "$__NETWORK_CACHE" ] && \
10 export __NETWORK_CACHE
="$(ubus call network.interface dump)"
12 __tmp
="$(jsonfilter ${4:+-F "$4"} ${5:+-l "$5"} -s "$__NETWORK_CACHE" -e "$1=@.interface${2:+[@.interface='$2']}$3")"
21 # determine first IPv4 address of given logical interface
22 # 1: destination variable
24 network_get_ipaddr() {
25 __network_ifstatus "$1" "$2" "['ipv4-address'][0].address
";
28 # determine first IPv6 address of given logical interface
29 # 1: destination variable
31 network_get_ipaddr6() {
32 __network_ifstatus "$1" "$2" "['ipv6-address'][0].address
" || \
33 __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][0]['local-address'].address
" || \
37 # determine first IPv4 subnet of given logical interface
38 # 1: destination variable
40 network_get_subnet() {
41 __network_ifstatus "$1" "$2" "['ipv4-address'][0]['address','mask']" "/"
44 # determine first IPv6 subnet of given logical interface
45 # 1: destination variable
47 network_get_subnet6() {
50 if network_get_subnets6 __nets "$2"; then
51 # Attempt to return first non-fe80::/10, non-fc::/7 range
52 for __addr in $__nets; do
53 case "$__addr" in fe[8ab]?:*|f[cd]??:*)
60 # Attempt to return first non-fe80::/10 range
61 for __addr in $__nets; do
62 case "$__addr" in fe[8ab]?:*)
70 for __addr in $__nets; do
80 # determine first IPv6 prefix of given logical interface
81 # 1: destination variable
83 network_get_prefix6() {
84 __network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/"
87 # determine all IPv4 addresses of given logical interface
88 # 1: destination variable
90 network_get_ipaddrs() {
91 __network_ifstatus "$1" "$2" "['ipv4-address'][*].address
"
94 # determine all IPv6 addresses of given logical interface
95 # 1: destination variable
97 network_get_ipaddrs6() {
101 if __network_ifstatus "__addr
" "$2" "['ipv6-address'][*].address
"; then
102 for __addr in $__addr; do
103 __list="${__list:+$__list }${__addr}"
107 if __network_ifstatus "__addr
" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address
"; then
108 for __addr in $__addr; do
109 __list="${__list:+$__list }${__addr}"
113 if [ -n "$__list" ]; then
122 # determine all IP addresses of given logical interface
123 # 1: destination variable
125 network_get_ipaddrs_all() {
128 network_get_ipaddrs __addr "$2"
129 network_get_ipaddrs6 __addr6 "$2"
131 if [ -n "$__addr" -o -n "$__addr6" ]; then
132 export "$1=${__addr:+$__addr }$__addr6"
140 # determine all IPv4 subnets of given logical interface
141 # 1: destination variable
143 network_get_subnets() {
144 __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
147 # determine all IPv6 subnets of given logical interface
148 # 1: destination variable
150 network_get_subnets6() {
154 if __network_ifstatus "__addr
" "$2" "['ipv6-address'][*]['address','mask']" "/ "; then
155 for __addr in $__addr; do
156 __list="${__list:+$__list }${__addr}"
160 if __network_ifstatus "__addr
" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address
" && \
161 __network_ifstatus "__mask
" "$2" "['ipv6-prefix-assignment'][*].mask
"; then
162 for __addr in $__addr; do
163 __list="${__list:+$__list }${__addr}/${__mask%% *}"
164 __mask="${__mask#* }"
168 if [ -n "$__list" ]; then
177 # determine all IPv6 prefixes of given logical interface
178 # 1: destination variable
180 network_get_prefixes6() {
181 __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
184 # determine IPv4 gateway of given logical interface
185 # 1: destination variable
187 # 3: consider inactive gateway if "true
" (optional)
188 network_get_gateway() {
189 __network_ifstatus "$1" "$2" ".route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1 && \
192 [ "$3" = 1 -o "$3" = "true
" ] && \
193 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1
196 # determine IPv6 gateway of given logical interface
197 # 1: destination variable
199 # 3: consider inactive gateway if "true
" (optional)
200 network_get_gateway6() {
201 __network_ifstatus "$1" "$2" ".route
[@.target
='::' && !@.table
].nexthop
" "" 1 && \
204 [ "$3" = 1 -o "$3" = "true
" ] && \
205 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='::' && !@.table
].nexthop
" "" 1
208 # determine the DNS servers of the given logical interface
209 # 1: destination variable
211 # 3: consider inactive servers if "true
" (optional)
212 network_get_dnsserver() {
213 __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
215 [ "$3" = 1 -o "$3" = "true
" ] && \
216 __network_ifstatus "$1" "$2" ".inactive
['dns-server'][*]"
219 # determine the domains of the given logical interface
220 # 1: destination variable
222 # 3: consider inactive domains if "true
" (optional)
223 network_get_dnssearch() {
224 __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
226 [ "$3" = 1 -o "$3" = "true
" ] && \
227 __network_ifstatus "$1" "$2" ".inactive
['dns-search'][*]"
231 # 1: destination variable
236 __network_ifstatus "$1" "" \
237 "[@.route
[@.target
='$2' && !@.table
]].interface
" "" 1 && \
240 [ "$3" = 1 -o "$3" = "true
" ] && \
241 __network_ifstatus "$1" "" \
242 "[@.inactive.route
[@.target
='$2' && !@.table
]].interface
" "" 1
245 # find the logical interface which holds the current IPv4 default route
246 # 1: destination variable
247 # 2: consider inactive default routes if "true
" (optional)
248 network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
250 # find the logical interface which holds the current IPv6 default route
251 # 1: destination variable
252 # 2: consider inactive dafault routes if "true
" (optional)
253 network_find_wan6() { __network_wan "$1" "::" "$2"; }
255 # test whether the given logical interface is running
260 __network_ifstatus "__up
" "$1" ".up
" && [ "$__up" = 1 ]
263 # determine the protocol of the given logical interface
264 # 1: destination variable
266 network_get_protocol() { __network_ifstatus "$1" "$2" ".proto
"; }
268 # determine the layer 3 linux network device of the given logical interface
269 # 1: destination variable
271 network_get_device() { __network_ifstatus "$1" "$2" ".l3_device
"; }
273 # determine the layer 2 linux network device of the given logical interface
274 # 1: destination variable
276 network_get_physdev() { __network_ifstatus "$1" "$2" ".device
"; }
278 # defer netifd actions on the given linux network device
280 network_defer_device()
282 ubus call network.device set_state \
283 "$
(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
286 # continue netifd actions on the given linux network device
288 network_ready_device()
290 ubus call network.device set_state \
291 "$
(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
294 # flush the internal value cache to force re-reading values from ubus
295 network_flush_cache() { unset __NETWORK_CACHE; }