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() {
48 __network_ifstatus "$1" "$2" "['ipv6-address'][0]['address','mask']" "/"
51 # determine first IPv6 prefix of given logical interface
52 # 1: destination variable
54 network_get_prefix6() {
55 __network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/"
58 # determine all IPv4 addresses of given logical interface
59 # 1: destination variable
61 network_get_ipaddrs() {
62 __network_ifstatus "$1" "$2" "['ipv4-address'][*].address
"
65 # determine all IPv6 addresses of given logical interface
66 # 1: destination variable
68 network_get_ipaddrs6() {
72 if __network_ifstatus "__addr
" "$2" "['ipv6-address'][*].address
"; then
73 for __addr in $__addr; do
74 __list="${__list:+$__list }${__addr}"
78 if __network_ifstatus "__addr
" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address
"; then
79 for __addr in $__addr; do
80 __list="${__list:+$__list }${__addr}"
84 if [ -n "$__list" ]; then
93 # determine all IP addresses of given logical interface
94 # 1: destination variable
96 network_get_ipaddrs_all() {
100 if __network_ifstatus "__addr
" "$2" "['ipv4-address','ipv6-address','ipv6-prefix-assignment'][*].address
"; then
101 for __addr in $__addr; do
103 *:) __list="${__list:+$__list }${__addr}1" ;;
104 *) __list="${__list:+$__list }${__addr}" ;;
116 # determine all IPv4 subnets of given logical interface
117 # 1: destination variable
119 network_get_subnets() {
120 __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
123 # determine all IPv6 subnets of given logical interface
124 # 1: destination variable
126 network_get_subnets6() {
130 if __network_ifstatus "__addr
" "$2" "['ipv6-address','ipv6-prefix-assignment'][*]['address','mask']" "/ "; then
131 for __addr in $__addr; do
133 *:/*) __list="${__list:+$__list }${__addr%/*}1/${__addr##*/}" ;;
134 *) __list="${__list:+$__list }${__addr}" ;;
146 # determine all IPv6 prefixes of given logical interface
147 # 1: destination variable
149 network_get_prefixes6() {
150 __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
153 # determine IPv4 gateway of given logical interface
154 # 1: destination variable
156 # 3: consider inactive gateway if "true
" (optional)
157 network_get_gateway() {
158 __network_ifstatus "$1" "$2" ".route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1 && \
161 [ "$3" = 1 -o "$3" = "true
" ] && \
162 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1
165 # determine IPv6 gateway of given logical interface
166 # 1: destination variable
168 # 3: consider inactive gateway if "true
" (optional)
169 network_get_gateway6() {
170 __network_ifstatus "$1" "$2" ".route
[@.target
='::' && !@.table
].nexthop
" "" 1 && \
173 [ "$3" = 1 -o "$3" = "true
" ] && \
174 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='::' && !@.table
].nexthop
" "" 1
177 # determine the DNS servers of the given logical interface
178 # 1: destination variable
180 # 3: consider inactive servers if "true
" (optional)
181 network_get_dnsserver() {
182 __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
184 [ "$3" = 1 -o "$3" = "true
" ] && \
185 __network_ifstatus "$1" "$2" ".inactive
['dns-server'][*]"
188 # determine the domains of the given logical interface
189 # 1: destination variable
191 # 3: consider inactive domains if "true
" (optional)
192 network_get_dnssearch() {
193 __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
195 [ "$3" = 1 -o "$3" = "true
" ] && \
196 __network_ifstatus "$1" "$2" ".inactive
['dns-search'][*]"
200 # 1: destination variable
205 __network_ifstatus "$1" "" \
206 "[@.route
[@.target
='$2' && !@.table
]].interface
" "" 1 && \
209 [ "$3" = 1 -o "$3" = "true
" ] && \
210 __network_ifstatus "$1" "" \
211 "[@.inactive.route
[@.target
='$2' && !@.table
]].interface
" "" 1
214 # find the logical interface which holds the current IPv4 default route
215 # 1: destination variable
216 # 2: consider inactive default routes if "true
" (optional)
217 network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
219 # find the logical interface which holds the current IPv6 default route
220 # 1: destination variable
221 # 2: consider inactive dafault routes if "true
" (optional)
222 network_find_wan6() { __network_wan "$1" "::" "$2"; }
224 # test whether the given logical interface is running
229 __network_ifstatus "__up
" "$1" ".up
" && [ "$__up" = 1 ]
232 # determine the protocol of the given logical interface
233 # 1: destination variable
235 network_get_protocol() { __network_ifstatus "$1" "$2" ".proto
"; }
237 # determine the layer 3 linux network device of the given logical interface
238 # 1: destination variable
240 network_get_device() { __network_ifstatus "$1" "$2" ".l3_device
"; }
242 # determine the layer 2 linux network device of the given logical interface
243 # 1: destination variable
245 network_get_physdev() { __network_ifstatus "$1" "$2" ".device
"; }
247 # defer netifd actions on the given linux network device
249 network_defer_device()
251 ubus call network.device set_state \
252 "$
(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
255 # continue netifd actions on the given linux network device
257 network_ready_device()
259 ubus call network.device set_state \
260 "$
(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
263 # flush the internal value cache to force re-reading values from ubus
264 network_flush_cache() { unset __NETWORK_CACHE; }