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