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