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() {
34 if __network_ifstatus "__addr
" "$2" "['ipv6-address','ipv6-prefix-assignment'][0].address
"; then
36 *:) export "$1=${__addr}1" ;;
37 *) export "$1=${__addr}" ;;
46 # determine first IPv4 subnet of given logical interface
47 # 1: destination variable
49 network_get_subnet() {
50 __network_ifstatus "$1" "$2" "['ipv4-address'][0]['address','mask']" "/"
53 # determine first IPv6 subnet of given logical interface
54 # 1: destination variable
56 network_get_subnet6() {
57 __network_ifstatus "$1" "$2" "['ipv6-address'][0]['address','mask']" "/"
60 # determine first IPv6 prefix of given logical interface
61 # 1: destination variable
63 network_get_prefix6() {
64 __network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/"
67 # determine all IPv4 addresses of given logical interface
68 # 1: destination variable
70 network_get_ipaddrs() {
71 __network_ifstatus "$1" "$2" "['ipv4-address'][*].address
"
74 # determine all IPv6 addresses of given logical interface
75 # 1: destination variable
77 network_get_ipaddrs6() {
81 if __network_ifstatus "__addr
" "$2" "['ipv6-address','ipv6-prefix-assignment'][*].address
"; then
82 for __addr in $__addr; do
84 *:) __list="${__list:+$__list }${__addr}1" ;;
85 *) __list="${__list:+$__list }${__addr}" ;;
97 # determine all IP addresses of given logical interface
98 # 1: destination variable
100 network_get_ipaddrs_all() {
104 if __network_ifstatus "__addr
" "$2" "['ipv4-address','ipv6-address','ipv6-prefix-assignment'][*].address
"; then
105 for __addr in $__addr; do
107 *:) __list="${__list:+$__list }${__addr}1" ;;
108 *) __list="${__list:+$__list }${__addr}" ;;
120 # determine all IPv4 subnets of given logical interface
121 # 1: destination variable
123 network_get_subnets() {
124 __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
127 # determine all IPv6 subnets of given logical interface
128 # 1: destination variable
130 network_get_subnets6() {
134 if __network_ifstatus "__addr
" "$2" "['ipv6-address','ipv6-prefix-assignment'][*]['address','mask']" "/ "; then
135 for __addr in $__addr; do
137 *:/*) __list="${__list:+$__list }${__addr%/*}1/${__addr##*/}" ;;
138 *) __list="${__list:+$__list }${__addr}" ;;
150 # determine all IPv6 prefixes of given logical interface
151 # 1: destination variable
153 network_get_prefixes6() {
154 __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
157 # determine IPv4 gateway of given logical interface
158 # 1: destination variable
160 # 3: consider inactive gateway if "true
" (optional)
161 network_get_gateway() {
162 __network_ifstatus "$1" "$2" ".route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1 && \
165 [ "$3" = 1 -o "$3" = "true
" ] && \
166 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='0.0.0.0' && !@.table
].nexthop
" "" 1
169 # determine IPv6 gateway of given logical interface
170 # 1: destination variable
172 # 3: consider inactive gateway if "true
" (optional)
173 network_get_gateway6() {
174 __network_ifstatus "$1" "$2" ".route
[@.target
='::' && !@.table
].nexthop
" "" 1 && \
177 [ "$3" = 1 -o "$3" = "true
" ] && \
178 __network_ifstatus "$1" "$2" ".inactive.route
[@.target
='::' && !@.table
].nexthop
" "" 1
181 # determine the DNS servers of the given logical interface
182 # 1: destination variable
184 # 3: consider inactive servers if "true
" (optional)
185 network_get_dnsserver() {
186 __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
188 [ "$3" = 1 -o "$3" = "true
" ] && \
189 __network_ifstatus "$1" "$2" ".inactive
['dns-server'][*]"
192 # determine the domains of the given logical interface
193 # 1: destination variable
195 # 3: consider inactive domains if "true
" (optional)
196 network_get_dnssearch() {
197 __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
199 [ "$3" = 1 -o "$3" = "true
" ] && \
200 __network_ifstatus "$1" "$2" ".inactive
['dns-search'][*]"
204 # 1: destination variable
209 __network_ifstatus "$1" "" \
210 "[@.route
[@.target
='$2' && !@.table
]].interface
" "" 1 && \
213 [ "$3" = 1 -o "$3" = "true
" ] && \
214 __network_ifstatus "$1" "" \
215 "[@.inactive.route
[@.target
='$2' && !@.table
]].interface
" "" 1
218 # find the logical interface which holds the current IPv4 default route
219 # 1: destination variable
220 # 2: consider inactive default routes if "true
" (optional)
221 network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
223 # find the logical interface which holds the current IPv6 default route
224 # 1: destination variable
225 # 2: consider inactive dafault routes if "true
" (optional)
226 network_find_wan6() { __network_wan "$1" "::" "$2"; }
228 # test whether the given logical interface is running
233 __network_ifstatus "__up
" "$1" ".up
" && [ "$__up" = 1 ]
236 # determine the protocol of the given logical interface
237 # 1: destination variable
239 network_get_protocol() { __network_ifstatus "$1" "$2" ".proto
"; }
241 # determine the layer 3 linux network device of the given logical interface
242 # 1: destination variable
244 network_get_device() { __network_ifstatus "$1" "$2" ".l3_device
"; }
246 # determine the layer 2 linux network device of the given logical interface
247 # 1: destination variable
249 network_get_physdev() { __network_ifstatus "$1" "$2" ".device
"; }
251 # defer netifd actions on the given linux network device
253 network_defer_device()
255 ubus call network.device set_state \
256 "$
(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
259 # continue netifd actions on the given linux network device
261 network_ready_device()
263 ubus call network.device set_state \
264 "$
(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
267 # flush the internal value cache to force re-reading values from ubus
268 network_flush_cache() { unset __NETWORK_CACHE; }