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
";
35 # determine first IPv4 subnet of given logical interface
36 # 1: destination variable
38 network_get_subnet() {
39 __network_ifstatus "$1" "$2" "['ipv4-address'][0]['address','mask']" "/"
42 # determine first IPv6 subnet of given logical interface
43 # 1: destination variable
45 network_get_subnet6() {
46 __network_ifstatus "$1" "$2" "['ipv6-address'][0]['address','mask']" "/"
49 # determine first IPv6 prefix of given logical interface
50 # 1: destination variable
52 network_get_prefix6() {
53 __network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/"
56 # determine all IPv4 addresses of given logical interface
57 # 1: destination variable
59 network_get_ipaddrs() {
60 __network_ifstatus "$1" "$2" "['ipv4-address'][*].address
"
63 # determine all IPv6 addresses of given logical interface
64 # 1: destination variable
66 network_get_ipaddrs6() {
70 if __network_ifstatus "__addr
" "$2" "['ipv6-address','ipv6-prefix-assignment'][*].address
"; then
71 for __addr in $__addr; do
73 *:) __list="${__list:+$__list }${__addr}1" ;;
74 *) __list="${__list:+$__list }${__addr}" ;;
86 # determine all IPv4 subnets of given logical interface
87 # 1: destination variable
89 network_get_subnets() {
90 __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
93 # determine all IPv6 subnets of given logical interface
94 # 1: destination variable
96 network_get_subnets6() {
100 if __network_ifstatus "__addr
" "$2" "['ipv6-address','ipv6-prefix-assignment'][*]['address','mask']" "/ "; then
101 for __addr in $__addr; do
103 *:/*) __list="${__list:+$__list }${__addr%/*}1/${__addr##*/}" ;;
104 *) __list="${__list:+$__list }${__addr}" ;;
116 # determine all IPv6 prefixes of given logical interface
117 # 1: destination variable
119 network_get_prefixes6() {
120 __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
123 # determine IPv4 gateway of given logical interface
124 # 1: destination variable
126 # 3: consider inactive gateway if "true
" (optional)
127 network_get_gateway() {
128 __network_ifstatus "$1" "$2" ".route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1 && \
131 [ "$3" = 1 -o "$3" = "true
" ] && \
132 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1
135 # determine IPv6 gateway of given logical interface
136 # 1: destination variable
138 # 3: consider inactive gateway if "true
" (optional)
139 network_get_gateway6() {
140 __network_ifstatus "$1" "$2" ".route
[@.target
='::' && !@.table
].nexthop
" "" 1 && \
143 [ "$3" = 1 -o "$3" = "true
" ] && \
144 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='::' && !@.table
].nexthop
" "" 1
147 # determine the DNS servers of the given logical interface
148 # 1: destination variable
150 # 3: consider inactive servers if "true
" (optional)
151 network_get_dnsserver() {
152 __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
154 [ "$3" = 1 -o "$3" = "true
" ] && \
155 __network_ifstatus "$1" "$2" ".inactive
['dns-server'][*]"
158 # determine the domains of the given logical interface
159 # 1: destination variable
161 # 3: consider inactive domains if "true
" (optional)
162 network_get_dnssearch() {
163 __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
165 [ "$3" = 1 -o "$3" = "true
" ] && \
166 __network_ifstatus "$1" "$2" ".inactive
['dns-search'][*]"
170 # 1: destination variable
175 __network_ifstatus "$1" "" \
176 "[@.route
[@.target
='$2' && !@.table
]].interface
" "" 1 && \
179 [ "$3" = 1 -o "$3" = "true
" ] && \
180 __network_ifstatus "$1" "" \
181 "[@.inactive.route
[@.target
='$2' && !@.table
]].interface
" "" 1
184 # find the logical interface which holds the current IPv4 default route
185 # 1: destination variable
186 # 2: consider inactive default routes if "true
" (optional)
187 network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
189 # find the logical interface which holds the current IPv6 default route
190 # 1: destination variable
191 # 2: consider inactive dafault routes if "true
" (optional)
192 network_find_wan6() { __network_wan "$1" "::" "$2"; }
194 # test whether the given logical interface is running
199 __network_ifstatus "__up
" "$1" ".up
" && [ "$__up" = 1 ]
202 # determine the protocol of the given logical interface
203 # 1: destination variable
205 network_get_protocol() { __network_ifstatus "$1" "$2" ".proto
"; }
207 # determine the layer 3 linux network device of the given logical interface
208 # 1: destination variable
210 network_get_device() { __network_ifstatus "$1" "$2" ".l3_device
"; }
212 # determine the layer 2 linux network device of the given logical interface
213 # 1: destination variable
215 network_get_physdev() { __network_ifstatus "$1" "$2" ".device
"; }
217 # defer netifd actions on the given linux network device
219 network_defer_device()
221 ubus call network.device set_state \
222 "$
(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
225 # continue netifd actions on the given linux network device
227 network_ready_device()
229 ubus call network.device set_state \
230 "$
(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
233 # flush the internal value cache to force re-reading values from ubus
234 network_flush_cache() { unset __NETWORK_CACHE; }