base-files: network.sh: extend network_get_ipaddr6() / network_get_subnet6() to take...
[openwrt.git] / package / base-files / files / lib / functions / network.sh
1 . /usr/share/libubox/jshn.sh
2
3 __network_set_cache()
4 {
5         if [ -n "$3" ]; then
6                 eval "export -- __NETWORK_CV_$1='$3'"
7                 __NETWORK_CACHE="${__NETWORK_CACHE:+$__NETWORK_CACHE }__NETWORK_CV_$1"
8         elif json_get_var "__NETWORK_CV_$1" "$2"; then
9                 __NETWORK_CACHE="${__NETWORK_CACHE:+$__NETWORK_CACHE }__NETWORK_CV_$1"
10         fi
11 }
12
13 __network_export()
14 {
15         local __v="__NETWORK_CV_$2"
16         eval "export -- \"$1=\${$__v:+\${$__v$4}$3}\"; [ -n \"\${$__v+x}\" ]"
17 }
18
19 __network_parse_ifstatus()
20 {
21         local __iface="$1"
22         local __key="${__iface}"
23         local __tmp
24         local __idx
25         local __list
26         local __old_ns
27
28         __network_export __tmp "${__key}__parsed" && return 0
29         __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
30         [ -n "$__tmp" ] || return 1
31
32         json_set_namespace "network" __old_ns
33         json_load "$__tmp"
34
35         __network_set_cache "${__key}__parsed" "" "1"
36
37         for __tmp in "" "_inactive"; do
38
39                 __key="${__key}${__tmp}"
40
41                 # parse addresses
42                 local __family
43                 for __family in 4 6; do
44
45                         __list=""
46
47                         if json_is_a "ipv${__family}_address" array; then
48
49                                 json_select "ipv${__family}_address"
50
51                                 __idx=1
52
53                                 while json_is_a "$__idx" object; do
54
55                                         json_select "$((__idx++))"
56                                         json_get_var __tmp "address" && __list="${__list:+$__list }$__tmp"
57                                         json_get_var __tmp "mask"    && __list="${__list:+$__list/}$__tmp"
58                                         json_select ".."
59
60                                 done
61
62                                 json_select ".."
63
64                         fi
65
66                         if json_is_a "ipv${__family}_prefix_assignment" array; then
67
68                                 json_select "ipv${__family}_prefix_assignment"
69
70                                 __idx=1
71
72                                 while json_is_a "$__idx" object; do
73
74                                         json_select "$((__idx++))"
75                                         json_get_var __tmp "address" && __list="${__list:+$__list }${__tmp}1"
76                                         json_get_var __tmp "mask"    && __list="${__list:+$__list/}$__tmp"
77                                         json_select ".."
78
79                                 done
80
81                                 json_select ".."
82
83                         fi
84
85                         if [ -n "$__list" ]; then
86                                 __network_set_cache "${__key}_address${__family}" "" "$__list"
87                         fi
88
89                 done
90
91                 # parse prefixes
92                 if json_is_a "ipv6_prefix" array; then
93                         json_select "ipv6_prefix"
94
95                         __idx=1
96                         __list=""
97
98                         while json_is_a "$__idx" object; do
99
100                                 json_select "$((__idx++))"
101                                 json_get_var __tmp "address" && __list="${__list:+$__list }$__tmp"
102                                 json_get_var __tmp "mask"    && __list="${__list:+$__list/}$__tmp"
103                                 json_select ".."
104
105                         done
106
107                         json_select ".."
108
109
110                         if [ -n "$__list" ]; then
111                                 __network_set_cache "${__key}_prefix6" "" "$__list"
112                         fi
113
114                 fi
115
116                 # parse routes
117                 if json_is_a route array; then
118
119                         json_select "route"
120
121                         local __idx=1
122                         while json_is_a "$__idx" object; do
123
124                                 json_select "$((__idx++))"
125                                 json_get_var __tmp table
126
127                                 if [ -z "$__tmp" ]; then
128                                         json_get_var __tmp target
129
130                                         case "${__tmp}" in
131                                                 0.0.0.0)
132                                                         __network_set_cache "${__key}_gateway4" nexthop
133                                                 ;;
134                                                 ::)
135                                                         __network_set_cache "${__key}_gateway6" nexthop
136                                                 ;;
137                                         esac
138                                 fi
139
140                                 json_select ".."
141
142                         done
143
144                         json_select ".."
145
146                 fi
147
148                 # parse dns info
149                 local __field
150                 for __field in "dns_server" "dns_search"; do
151                         if json_is_a "$__field" array; then
152
153                                 json_select "$__field"
154
155                                 __idx=1
156                                 __list=""
157
158                                 while json_is_a "$__idx" string; do
159
160                                         json_get_var __tmp "$((__idx++))"
161                                         __list="${__list:+$__list }$__tmp"
162
163                                 done
164
165                                 json_select ".."
166
167                                 if [ -n "$__list" ]; then
168                                         __network_set_cache "${__key}_${__field}" "" "$__list"
169                                 fi
170                         fi
171                 done
172
173                 # parse up state, device and physdev
174                 for __field in "up" "l3_device" "device"; do
175                         if json_get_type __tmp "$__field"; then
176                                 __network_set_cache "${__key}_${__field}" "$__field"
177                         fi
178                 done
179
180                 # descend into inactive table
181                 json_is_a "inactive" object && json_select "inactive"
182
183         done
184
185         json_cleanup
186         json_set_namespace "$__old_ns"
187
188         return 0
189 }
190
191
192 __network_ipaddr()
193 {
194         local __var="$1"
195         local __iface="$2"
196         local __field="$3"
197         local __subst="$4"
198         local __list="$5"
199         local __tmp=""
200
201         __network_parse_ifstatus "$__iface" || return 1
202
203         if [ $__list = 1 ] && [ -n "$__subst" ]; then
204                 __network_export "__list" "${__iface}_${__field}"
205
206                 for __list in $__list; do
207                         eval "__tmp=\"${__tmp:+$__tmp }\${__list$__subst}\""
208                 done
209
210                 export -- "$__var=$__tmp"; [ -n "$__tmp" ]
211                 return $?
212         fi
213
214         __network_export "$__var" "${__iface}_${__field}" "" "$__subst"
215         return $?
216 }
217
218 # determine first IPv4 address of given logical interface
219 # 1: destination variable
220 # 2: interface
221 network_get_ipaddr()  { __network_ipaddr "$1" "$2" "address4" "%%/*" 0; }
222
223 # determine first IPv6 address of given logical interface
224 # 1: destination variable
225 # 2: interface
226 network_get_ipaddr6() { __network_ipaddr "$1" "$2" "address6" "%%/*" 0; }
227
228 # determine first IPv4 subnet of given logical interface
229 # 1: destination variable
230 # 2: interface
231 network_get_subnet()  { __network_ipaddr "$1" "$2" "address4" "%% *" 0; }
232
233 # determine first IPv6 subnet of given logical interface
234 # 1: destination variable
235 # 2: interface
236 network_get_subnet6() { __network_ipaddr "$1" "$2" "address6" "%% *" 0; }
237
238 # determine first IPv6 prefix of given logical interface
239 # 1: destination variable
240 # 2: interface
241 network_get_prefix6() { __network_ipaddr "$1" "$2" "prefix6" "%% *" 0; }
242
243 # determine all IPv4 addresses of given logical interface
244 # 1: destination variable
245 # 2: interface
246 network_get_ipaddrs()  { __network_ipaddr "$1" "$2" "address4" "%%/*" 1; }
247
248 # determine all IPv6 addresses of given logical interface
249 # 1: destination variable
250 # 2: interface
251 network_get_ipaddrs6() { __network_ipaddr "$1" "$2" "address6" "%%/*" 1; }
252
253 # determine all IPv4 subnets of given logical interface
254 # 1: destination variable
255 # 2: interface
256 network_get_subnets()  { __network_ipaddr "$1" "$2" "address4" "" 1; }
257
258 # determine all IPv6 subnets of given logical interface
259 # 1: destination variable
260 # 2: interface
261 network_get_subnets6() { __network_ipaddr "$1" "$2" "address6" "" 1; }
262
263 # determine all IPv6 prefixes of given logical interface
264 # 1: destination variable
265 # 2: interface
266 network_get_prefixes6() { __network_ipaddr "$1" "$2" "prefix6" "" 1; }
267
268
269 __network_gateway()
270 {
271         local __var="$1"
272         local __iface="$2"
273         local __family="$3"
274         local __inactive="$4"
275
276         __network_parse_ifstatus "$__iface" || return 1
277
278         if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then
279                 __network_export "$__var" "${__iface}_inactive_gateway${__family}" && \
280                         return 0
281         fi
282
283         __network_export "$__var" "${__iface}_gateway${__family}"
284         return $?
285 }
286
287 # determine IPv4 gateway of given logical interface
288 # 1: destination variable
289 # 2: interface
290 # 3: consider inactive gateway if "true" (optional)
291 network_get_gateway()  { __network_gateway "$1" "$2" 4 "${3:-0}"; }
292
293 # determine  IPv6 gateway of given logical interface
294 # 1: destination variable
295 # 2: interface
296 # 3: consider inactive gateway if "true" (optional)
297 network_get_gateway6() { __network_gateway "$1" "$2" 6 "${3:-0}"; }
298
299
300 __network_dns() {
301         local __var="$1"
302         local __iface="$2"
303         local __field="$3"
304         local __inactive="$4"
305
306         __network_parse_ifstatus "$__iface" || return 1
307
308         if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then
309                 __network_export "$__var" "${__iface}_inactive_${__field}" && \
310                         return 0
311         fi
312
313         __network_export "$__var" "${__iface}_${__field}"
314         return $?
315 }
316
317 # determine the DNS servers of the given logical interface
318 # 1: destination variable
319 # 2: interface
320 # 3: consider inactive servers if "true" (optional)
321 network_get_dnsserver() { __network_dns "$1" "$2" dns_server "${3:-0}"; }
322
323 # determine the domains of the given logical interface
324 # 1: destination variable
325 # 2: interface
326 # 3: consider inactive domains if "true" (optional)
327 network_get_dnssearch() { __network_dns "$1" "$2" dns_search "${3:-0}"; }
328
329
330 __network_wan()
331 {
332         local __var="$1"
333         local __family="$2"
334         local __inactive="$3"
335         local __iface
336
337         for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do
338                 if [ "$__iface" != loopback ]; then
339                         if __network_gateway "$__var" "$__iface" "$__family" "$__inactive"; then
340                                 eval "export -- \"$__var=$__iface\""
341                                 return 0
342                         fi
343                 fi
344         done
345
346         eval "export -- \"$__var=\""
347         return 1
348 }
349
350 # find the logical interface which holds the current IPv4 default route
351 # 1: destination variable
352 # 2: consider inactive default routes if "true" (optional)
353 network_find_wan()  { __network_wan "$1" 4 "${2:-0}"; }
354
355 # find the logical interface which holds the current IPv6 default route
356 # 1: destination variable
357 # 2: consider inactive dafault routes if "true" (optional)
358 network_find_wan6() { __network_wan "$1" 6 "${2:-0}"; }
359
360
361 __network_device()
362 {
363         local __var="$1"
364         local __iface="$2"
365         local __field="$3"
366
367         __network_parse_ifstatus "$__iface" || return 1
368         __network_export "$__var" "${__iface}_${__field}"
369         return $?
370 }
371
372 # test whether the given logical interface is running
373 # 1: interface
374 network_is_up()
375 {
376         local __up
377         __network_device __up "$1" up && [ $__up -eq 1 ]
378 }
379
380 # determine the layer 3 linux network device of the given logical interface
381 # 1: destination variable
382 # 2: interface
383 network_get_device()  { __network_device "$1" "$2" l3_device; }
384
385 # determine the layer 2 linux network device of the given logical interface
386 # 1: destination variable
387 # 2: interface
388 network_get_physdev() { __network_device "$1" "$2" device;    }
389
390
391 __network_defer()
392 {
393         local __device="$1"
394         local __defer="$2"
395
396         json_init
397         json_add_string name "$__device"
398         json_add_boolean defer "$__defer"
399
400         ubus call network.device set_state "$(json_dump)" 2>/dev/null
401 }
402
403 # defer netifd actions on the given linux network device
404 # 1: device name
405 network_defer_device() { __network_defer "$1" 1; }
406
407 # continue netifd actions on the given linux network device
408 # 1: device name
409 network_ready_device() { __network_defer "$1" 0; }
410
411 # flush the internal value cache to force re-reading values from ubus
412 network_flush_cache()
413 {
414         local __tmp
415         for __tmp in $__NETWORK_CACHE __NETWORK_CACHE; do
416                 unset "$__tmp"
417         done
418 }