blob: 4851a5817ada074ee57b3a95a712253c4d34ecf1 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001# 1: destination variable
2# 2: interface
3# 3: path
4# 4: separator
5# 5: limit
6__network_ifstatus() {
7 local __tmp
8
9 [ -z "$__NETWORK_CACHE" ] && {
10 __tmp="$(ubus call network.interface dump 2>&1)"
11 case "$?" in
12 4) : ;;
13 0) export __NETWORK_CACHE="$__tmp" ;;
14 *) echo "$__tmp" >&2 ;;
15 esac
16 }
17
18 __tmp="$(jsonfilter ${4:+-F "$4"} ${5:+-l "$5"} -s "${__NETWORK_CACHE:-{}}" -e "$1=@.interface${2:+[@.interface='$2']}$3")"
19
20 [ -z "$__tmp" ] && \
21 unset "$1" && \
22 return 1
23
24 eval "$__tmp"
25}
26
27# determine first IPv4 address of given logical interface
28# 1: destination variable
29# 2: interface
30network_get_ipaddr() {
31 __network_ifstatus "$1" "$2" "['ipv4-address'][0].address";
32}
33
34# determine first IPv6 address of given logical interface
35# 1: destination variable
36# 2: interface
37network_get_ipaddr6() {
38 __network_ifstatus "$1" "$2" "['ipv6-address'][0].address" || \
39 __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][0]['local-address'].address" || \
40 return 1
41}
42
43# determine first IPv4 subnet of given logical interface
44# 1: destination variable
45# 2: interface
46network_get_subnet() {
47 __network_ifstatus "$1" "$2" "['ipv4-address'][0]['address','mask']" "/"
48}
49
50# determine first IPv6 subnet of given logical interface
51# 1: destination variable
52# 2: interface
53network_get_subnet6() {
54 local __nets __addr
55
56 if network_get_subnets6 __nets "$2"; then
57 # Attempt to return first non-fe80::/10, non-fc::/7 range
58 for __addr in $__nets; do
59 case "$__addr" in fe[8ab]?:*|f[cd]??:*)
60 continue
61 esac
62 export "$1=$__addr"
63 return 0
64 done
65
66 # Attempt to return first non-fe80::/10 range
67 for __addr in $__nets; do
68 case "$__addr" in fe[8ab]?:*)
69 continue
70 esac
71 export "$1=$__addr"
72 return 0
73 done
74
75 # Return first item
76 for __addr in $__nets; do
77 export "$1=$__addr"
78 return 0
79 done
80 fi
81
82 unset "$1"
83 return 1
84}
85
86# determine first IPv6 prefix of given logical interface
87# 1: destination variable
88# 2: interface
89network_get_prefix6() {
90 __network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/"
91}
92
93# determine first IPv6 prefix assignment of given logical interface
94# 1: destination variable
95# 2: interface
96network_get_prefix_assignment6() {
97 __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][0]['address','mask']" "/"
98}
99
100# determine all IPv4 addresses of given logical interface
101# 1: destination variable
102# 2: interface
103network_get_ipaddrs() {
104 __network_ifstatus "$1" "$2" "['ipv4-address'][*].address"
105}
106
107# determine all IPv6 addresses of given logical interface
108# 1: destination variable
109# 2: interface
110network_get_ipaddrs6() {
111 local __addr
112 local __list=""
113
114 if __network_ifstatus "__addr" "$2" "['ipv6-address'][*].address"; then
115 for __addr in $__addr; do
116 __list="${__list:+$__list }${__addr}"
117 done
118 fi
119
120 if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address"; then
121 for __addr in $__addr; do
122 __list="${__list:+$__list }${__addr}"
123 done
124 fi
125
126 if [ -n "$__list" ]; then
127 export "$1=$__list"
128 return 0
129 fi
130
131 unset "$1"
132 return 1
133}
134
135# determine all IP addresses of given logical interface
136# 1: destination variable
137# 2: interface
138network_get_ipaddrs_all() {
139 local __addr __addr6
140
141 network_get_ipaddrs __addr "$2"
142 network_get_ipaddrs6 __addr6 "$2"
143
144 if [ -n "$__addr" -o -n "$__addr6" ]; then
145 export "$1=${__addr:+$__addr }$__addr6"
146 return 0
147 fi
148
149 unset "$1"
150 return 1
151}
152
153# determine all IPv4 subnets of given logical interface
154# 1: destination variable
155# 2: interface
156network_get_subnets() {
157 __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
158}
159
160# determine all IPv6 subnets of given logical interface
161# 1: destination variable
162# 2: interface
163network_get_subnets6() {
164 local __addr __mask
165 local __list=""
166
167 if __network_ifstatus "__addr" "$2" "['ipv6-address'][*]['address','mask']" "/ "; then
168 for __addr in $__addr; do
169 __list="${__list:+$__list }${__addr}"
170 done
171 fi
172
173 if __network_ifstatus "__addr" "$2" "['ipv6-prefix-assignment'][*]['local-address'].address" && \
174 __network_ifstatus "__mask" "$2" "['ipv6-prefix-assignment'][*].mask"; then
175 for __addr in $__addr; do
176 __list="${__list:+$__list }${__addr}/${__mask%% *}"
177 __mask="${__mask#* }"
178 done
179 fi
180
181 if [ -n "$__list" ]; then
182 export "$1=$__list"
183 return 0
184 fi
185
186 unset "$1"
187 return 1
188}
189
190# determine all IPv6 prefixes of given logical interface
191# 1: destination variable
192# 2: interface
193network_get_prefixes6() {
194 __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
195}
196
197# determine all IPv6 prefix assignments of given logical interface
198# 1: destination variable
199# 2: interface
200network_get_prefix_assignments6() {
201 __network_ifstatus "$1" "$2" "['ipv6-prefix-assignment'][*]['address','mask']" "/ "
202}
203
204# determine IPv4 gateway of given logical interface
205# 1: destination variable
206# 2: interface
207# 3: consider inactive gateway if "true" (optional)
208network_get_gateway() {
209 __network_ifstatus "$1" "$2" ".route[@.target='0.0.0.0' && !@.table].nexthop" "" 1 && \
210 return 0
211
212 [ "$3" = 1 -o "$3" = "true" ] && \
213 __network_ifstatus "$1" "$2" ".inactive.route[@.target='0.0.0.0' && !@.table].nexthop" "" 1
214}
215
216# determine IPv6 gateway of given logical interface
217# 1: destination variable
218# 2: interface
219# 3: consider inactive gateway if "true" (optional)
220network_get_gateway6() {
221 __network_ifstatus "$1" "$2" ".route[@.target='::' && !@.table].nexthop" "" 1 && \
222 return 0
223
224 [ "$3" = 1 -o "$3" = "true" ] && \
225 __network_ifstatus "$1" "$2" ".inactive.route[@.target='::' && !@.table].nexthop" "" 1
226}
227
228# determine the DNS servers of the given logical interface
229# 1: destination variable
230# 2: interface
231# 3: consider inactive servers if "true" (optional)
232network_get_dnsserver() {
233 __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
234
235 [ "$3" = 1 -o "$3" = "true" ] && \
236 __network_ifstatus "$1" "$2" ".inactive['dns-server'][*]"
237}
238
239# determine the domains of the given logical interface
240# 1: destination variable
241# 2: interface
242# 3: consider inactive domains if "true" (optional)
243network_get_dnssearch() {
244 __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
245
246 [ "$3" = 1 -o "$3" = "true" ] && \
247 __network_ifstatus "$1" "$2" ".inactive['dns-search'][*]"
248}
249
250
251# 1: destination variable
252# 2: addr
253# 3: inactive
254__network_wan()
255{
256 __network_ifstatus "$1" "" \
257 "[@.route[@.target='$2' && !@.table]].interface" "" 1 && \
258 return 0
259
260 [ "$3" = 1 -o "$3" = "true" ] && \
261 __network_ifstatus "$1" "" \
262 "[@.inactive.route[@.target='$2' && !@.table]].interface" "" 1
263}
264
265# find the logical interface which holds the current IPv4 default route
266# 1: destination variable
267# 2: consider inactive default routes if "true" (optional)
268network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
269
270# find the logical interface which holds the current IPv6 default route
271# 1: destination variable
272# 2: consider inactive default routes if "true" (optional)
273network_find_wan6() { __network_wan "$1" "::" "$2"; }
274
275# test whether the given logical interface is running
276# 1: interface
277network_is_up()
278{
279 local __up
280 __network_ifstatus "__up" "$1" ".up" && [ "$__up" = 1 ]
281}
282
283# determine the protocol of the given logical interface
284# 1: destination variable
285# 2: interface
286network_get_protocol() { __network_ifstatus "$1" "$2" ".proto"; }
287
288# determine the uptime of the given logical interface
289# 1: destination variable
290# 2: interface
291network_get_uptime() { __network_ifstatus "$1" "$2" ".uptime"; }
292
293# determine the metric of the given logical interface
294# 1: destination variable
295# 2: interface
296network_get_metric() { __network_ifstatus "$1" "$2" ".metric"; }
297
298# determine the layer 3 linux network device of the given logical interface
299# 1: destination variable
300# 2: interface
301network_get_device() { __network_ifstatus "$1" "$2" ".l3_device"; }
302
303# determine the layer 2 linux network device of the given logical interface
304# 1: destination variable
305# 2: interface
306network_get_physdev() { __network_ifstatus "$1" "$2" ".device"; }
307
308# defer netifd actions on the given linux network device
309# 1: device name
310network_defer_device()
311{
312 ubus call network.device set_state \
313 "$(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
314}
315
316# continue netifd actions on the given linux network device
317# 1: device name
318network_ready_device()
319{
320 ubus call network.device set_state \
321 "$(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
322}
323
324# flush the internal value cache to force re-reading values from ubus
325network_flush_cache() { unset __NETWORK_CACHE; }