base-files: rework cache handling in network.sh to keep the entire parsed ifstatus...
[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$3}\"; [ -n \"\${$__v+x}\" ]"
17 }
18
19 __network_parse_ifstatus()
20 {
21         local __iface="$1"
22         local __key="${__iface}"
23         local __tmp
24         local __old_ns
25
26         __network_export __tmp "${__key}__parsed" && return 0
27         __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
28         [ -n "$__tmp" ] || return 1
29
30         json_set_namespace "network" __old_ns
31         json_load "$__tmp"
32
33         __network_set_cache "${__key}__parsed" "" "1"
34
35         for __tmp in "" "_inactive"; do
36
37                 __key="${__key}${__tmp}"
38
39                 # parse addresses
40                 local __family
41                 for __family in 4 6; do
42                         if json_get_type __tmp "ipv${__family}_address" && [ "$__tmp" = array ]; then
43
44                                 json_select "ipv${__family}_address"
45
46                                 if json_get_type __tmp 1 && [ "$__tmp" = object ]; then
47
48                                         json_select 1
49                                         __network_set_cache "${__key}_address${__family}" address
50                                         __network_set_cache "${__key}_mask${__family}"    mask
51                                         json_select ".."
52
53                                 fi
54
55                                 json_select ".."
56
57                         fi
58                 done
59
60                 # parse routes
61                 if json_get_type __tmp route && [ "$__tmp" = array ]; then
62
63                         json_select "route"
64
65                         local __idx=1
66                         while json_get_type __tmp "$__idx" && [ "$__tmp" = object ]; do
67
68                                 json_select "$((__idx++))"
69                                 json_get_var __tmp target
70
71                                 case "${__tmp}" in
72                                         0.0.0.0)
73                                                 __network_set_cache "${__key}_gateway4" nexthop
74                                         ;;
75                                         ::)
76                                                 __network_set_cache "${__key}_gateway6" nexthop
77                                         ;;
78                                 esac
79
80                                 json_select ".."
81
82                         done
83
84                         json_select ".."
85
86                 fi
87
88                 # parse dns info
89                 local __field
90                 for __field in "dns_server" "dns_search"; do
91                         if json_get_type __tmp "$__field" && [ "$__tmp" = array ]; then
92
93                                 json_select "$__field"
94
95                                 local __idx=1
96                                 local __dns=""
97
98                                 while json_get_type __tmp "$__idx" && [ "$__tmp" = string ]; do
99
100                                         json_get_var __tmp "$((__idx++))"
101                                         __dns="${__dns:+$__dns }$__tmp"
102
103                                 done
104
105                                 json_select ".."
106
107                                 if [ -n "$__dns" ]; then
108                                         __network_set_cache "${__key}_${__field}" "" "$__dns"
109                                 fi
110                         fi
111                 done
112
113                 # parse up state, device and physdev
114                 for __field in "up" "l3_device" "device"; do
115                         if json_get_type __tmp "$__field"; then
116                                 __network_set_cache "${__key}_${__field}" "$__field"
117                         fi
118                 done
119
120                 # descend into inactive table
121                 json_get_type __tmp "inactive" && [ "$__tmp" = object ] && json_select "inactive"
122
123         done
124
125         json_cleanup
126         json_set_namespace "$__old_ns"
127
128         return 0
129 }
130
131
132 __network_ipaddr()
133 {
134         local __var="$1"
135         local __iface="$2"
136         local __family="$3"
137         local __prefix="$4"
138         local __tmp
139
140         __network_parse_ifstatus "$__iface" || return 1
141
142         if [ $__prefix -eq 1 ]; then
143                 __network_export __tmp "${__iface}_mask${__family}" && \
144                         __network_export "$__var" "${__iface}_address${__family}" "/$__tmp"
145                 return $?
146         fi
147
148         __network_export "$__var" "${__iface}_address${__family}"
149         return $?
150
151 }
152
153 # determine IPv4 address of given logical interface
154 # 1: destination variable
155 # 2: interface
156 network_get_ipaddr()  { __network_ipaddr "$1" "$2" 4 0; }
157
158 # determine IPv6 address of given logical interface
159 # 1: destination variable
160 # 2: interface
161 network_get_ipaddr6() { __network_ipaddr "$1" "$2" 6 0; }
162
163 # determine IPv4 subnet of given logical interface
164 # 1: destination variable
165 # 2: interface
166 network_get_subnet()  { __network_ipaddr "$1" "$2" 4 1; }
167
168 # determine IPv6 subnet of given logical interface
169 # 1: destination variable
170 # 2: interface
171 network_get_subnet6() { __network_ipaddr "$1" "$2" 6 1; }
172
173
174 __network_gateway()
175 {
176         local __var="$1"
177         local __iface="$2"
178         local __family="$3"
179         local __inactive="$4"
180
181         __network_parse_ifstatus "$__iface" || return 1
182
183         if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then
184                 __network_export "$__var" "${__iface}_inactive_gateway${__family}" && \
185                         return 0
186         fi
187
188         __network_export "$__var" "${__iface}_gateway${__family}"
189         return $?
190 }
191
192 # determine IPv4 gateway of given logical interface
193 # 1: destination variable
194 # 2: interface
195 # 3: consider inactive gateway if "true" (optional)
196 network_get_gateway()  { __network_gateway "$1" "$2" 4 "${3:-0}"; }
197
198 # determine  IPv6 gateway of given logical interface
199 # 1: destination variable
200 # 2: interface
201 # 3: consider inactive gateway if "true" (optional)
202 network_get_gateway6() { __network_gateway "$1" "$2" 6 "${3:-0}"; }
203
204
205 __network_dns() {
206         local __var="$1"
207         local __iface="$2"
208         local __field="$3"
209         local __inactive="$4"
210
211         __network_parse_ifstatus "$__iface" || return 1
212
213         if [ "$__inactive" = 1 -o "$__inactive" = "true" ]; then
214                 __network_export "$__var" "${__iface}_inactive_${__field}" && \
215                         return 0
216         fi
217
218         __network_export "$__var" "${__iface}_${__field}"
219         return $?
220 }
221
222 # determine the DNS servers of the given logical interface
223 # 1: destination variable
224 # 2: interface
225 # 3: consider inactive servers if "true" (optional)
226 network_get_dnsserver() { __network_dns "$1" "$2" dns_server "${3:-0}"; }
227
228 # determine the domains of the given logical interface
229 # 1: destination variable
230 # 2: interface
231 # 3: consider inactive domains if "true" (optional)
232 network_get_dnssearch() { __network_dns "$1" "$2" dns_search "${3:-0}"; }
233
234
235 __network_wan()
236 {
237         local __var="$1"
238         local __family="$2"
239         local __inactive="$3"
240         local __iface
241
242         for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do
243                 if [ "$__iface" != loopback ]; then
244                         if __network_gateway "$__var" "$__iface" "$__family" "$__inactive"; then
245                                 eval "export -- \"$__var=$__iface\""
246                                 return 0
247                         fi
248                 fi
249         done
250
251         eval "export -- \"$__var=\""
252         return 1
253 }
254
255 # find the logical interface which holds the current IPv4 default route
256 # 1: destination variable
257 # 2: consider inactive default routes if "true" (optional)
258 network_find_wan()  { __network_wan "$1" 4 "${2:-0}"; }
259
260 # find the logical interface which holds the current IPv6 default route
261 # 1: destination variable
262 # 2: consider inactive dafault routes if "true" (optional)
263 network_find_wan6() { __network_wan "$1" 6 "${2:-0}"; }
264
265
266 __network_device()
267 {
268         local __var="$1"
269         local __iface="$2"
270         local __field="$3"
271
272         __network_parse_ifstatus "$__iface" || return 1
273         __network_export "$__var" "${__iface}_${__field}"
274         return $?
275 }
276
277 # test whether the given logical interface is running
278 # 1: interface
279 network_is_up()
280 {
281         local __up
282         __network_device __up "$1" up && [ $__up -eq 1 ]
283 }
284
285 # determine the layer 3 linux network device of the given logical interface
286 # 1: destination variable
287 # 2: interface
288 network_get_device()  { __network_device "$1" "$2" l3_device; }
289
290 # determine the layer 2 linux network device of the given logical interface
291 # 1: destination variable
292 # 2: interface
293 network_get_physdev() { __network_device "$1" "$2" device;    }
294
295
296 __network_defer()
297 {
298         local __device="$1"
299         local __defer="$2"
300
301         json_init
302         json_add_string name "$__device"
303         json_add_boolean defer "$__defer"
304
305         ubus call network.device set_state "$(json_dump)" 2>/dev/null
306 }
307
308 # defer netifd actions on the given linux network device
309 # 1: device name
310 network_defer_device() { __network_defer "$1" 1; }
311
312 # continue netifd actions on the given linux network device
313 # 1: device name
314 network_ready_device() { __network_defer "$1" 0; }
315
316 # flush the internal value cache to force re-reading values from ubus
317 network_flush_cache()
318 {
319         local __tmp
320         for __tmp in $__NETWORK_CACHE __NETWORK_CACHE; do
321                 unset "$__tmp"
322         done
323 }