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