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