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 IPv4 subnets of given logical interface
98 # 1: destination variable
100 network_get_subnets() {
101 __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
104 # determine all IPv6 subnets of given logical interface
105 # 1: destination variable
107 network_get_subnets6() {
111 if __network_ifstatus "__addr" "$2" "['ipv6-address','ipv6-prefix-assignment'][*]['address','mask']" "/ "; then
112 for __addr in $__addr; do
114 *:/*) __list="${__list:+$__list }${__addr%/*}1/${__addr##*/}" ;;
115 *) __list="${__list:+$__list }${__addr}" ;;
127 # determine all IPv6 prefixes of given logical interface
128 # 1: destination variable
130 network_get_prefixes6() {
131 __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
134 # determine IPv4 gateway of given logical interface
135 # 1: destination variable
137 # 3: consider inactive gateway if "true" (optional)
138 network_get_gateway() {
139 __network_ifstatus "$1" "$2" ".route[@.target='0.0.0.0' && !@.table].nexthop" "" 1 && \
142 [ "$3" = 1 -o "$3" = "true" ] && \
143 __network_ifstatus "$1" "$2" ".inactive.route[@.target='0.0.0.0' && !@.table].nexthop" "" 1
146 # determine IPv6 gateway of given logical interface
147 # 1: destination variable
149 # 3: consider inactive gateway if "true" (optional)
150 network_get_gateway6() {
151 __network_ifstatus "$1" "$2" ".route[@.target='::' && !@.table].nexthop" "" 1 && \
154 [ "$3" = 1 -o "$3" = "true" ] && \
155 __network_ifstatus "$1" "$2" ".inactive.route[@.target='::' && !@.table].nexthop" "" 1
158 # determine the DNS servers of the given logical interface
159 # 1: destination variable
161 # 3: consider inactive servers if "true" (optional)
162 network_get_dnsserver() {
163 __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
165 [ "$3" = 1 -o "$3" = "true" ] && \
166 __network_ifstatus "$1" "$2" ".inactive['dns-server'][*]"
169 # determine the domains of the given logical interface
170 # 1: destination variable
172 # 3: consider inactive domains if "true" (optional)
173 network_get_dnssearch() {
174 __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
176 [ "$3" = 1 -o "$3" = "true" ] && \
177 __network_ifstatus "$1" "$2" ".inactive['dns-search'][*]"
181 # 1: destination variable
186 __network_ifstatus "$1" "" \
187 "[@.route[@.target='$2' && !@.table]].interface" "" 1 && \
190 [ "$3" = 1 -o "$3" = "true" ] && \
191 __network_ifstatus "$1" "" \
192 "[@.inactive.route[@.target='$2' && !@.table]].interface" "" 1
195 # find the logical interface which holds the current IPv4 default route
196 # 1: destination variable
197 # 2: consider inactive default routes if "true" (optional)
198 network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
200 # find the logical interface which holds the current IPv6 default route
201 # 1: destination variable
202 # 2: consider inactive dafault routes if "true" (optional)
203 network_find_wan6() { __network_wan "$1" "::" "$2"; }
205 # test whether the given logical interface is running
210 __network_ifstatus "__up" "$1" ".up" && [ "$__up" = 1 ]
213 # determine the protocol of the given logical interface
214 # 1: destination variable
216 network_get_protocol() { __network_ifstatus "$1" "$2" ".proto"; }
218 # determine the layer 3 linux network device of the given logical interface
219 # 1: destination variable
221 network_get_device() { __network_ifstatus "$1" "$2" ".l3_device"; }
223 # determine the layer 2 linux network device of the given logical interface
224 # 1: destination variable
226 network_get_physdev() { __network_ifstatus "$1" "$2" ".device"; }
228 # defer netifd actions on the given linux network device
230 network_defer_device()
232 ubus call network.device set_state \
233 "$(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
236 # continue netifd actions on the given linux network device
238 network_ready_device()
240 ubus call network.device set_state \
241 "$(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
244 # flush the internal value cache to force re-reading values from ubus
245 network_flush_cache() { unset __NETWORK_CACHE; }