base-files: whitespace fixes
[openwrt.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         local __addr
33
34         if __network_ifstatus "__addr" "$2" "['ipv6-address','ipv6-prefix-assignment'][0].address"; then
35                 case "$__addr" in
36                         *:)     export "$1=${__addr}1" ;;
37                         *)      export "$1=${__addr}" ;;
38                 esac
39                 return 0
40         fi
41
42         unset $1
43         return 1
44 }
45
46 # determine first IPv4 subnet of given logical interface
47 # 1: destination variable
48 # 2: interface
49 network_get_subnet() {
50         __network_ifstatus "$1" "$2" "['ipv4-address'][0]['address','mask']" "/"
51 }
52
53 # determine first IPv6 subnet of given logical interface
54 # 1: destination variable
55 # 2: interface
56 network_get_subnet6() {
57         __network_ifstatus "$1" "$2" "['ipv6-address'][0]['address','mask']" "/"
58 }
59
60 # determine first IPv6 prefix of given logical interface
61 # 1: destination variable
62 # 2: interface
63 network_get_prefix6() {
64         __network_ifstatus "$1" "$2" "['ipv6-prefix'][0]['address','mask']" "/"
65 }
66
67 # determine all IPv4 addresses of given logical interface
68 # 1: destination variable
69 # 2: interface
70 network_get_ipaddrs() {
71         __network_ifstatus "$1" "$2" "['ipv4-address'][*].address"
72 }
73
74 # determine all IPv6 addresses of given logical interface
75 # 1: destination variable
76 # 2: interface
77 network_get_ipaddrs6() {
78         local __addr
79         local __list=""
80
81         if __network_ifstatus "__addr" "$2" "['ipv6-address','ipv6-prefix-assignment'][*].address"; then
82                 for __addr in $__addr; do
83                         case "$__addr" in
84                                 *:) __list="${__list:+$__list }${__addr}1" ;;
85                                 *)  __list="${__list:+$__list }${__addr}"  ;;
86                         esac
87                 done
88
89                 export "$1=$__list"
90                 return 0
91         fi
92
93         unset "$1"
94         return 1
95 }
96
97 # determine all IPv4 subnets of given logical interface
98 # 1: destination variable
99 # 2: interface
100 network_get_subnets() {
101         __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
102 }
103
104 # determine all IPv6 subnets of given logical interface
105 # 1: destination variable
106 # 2: interface
107 network_get_subnets6() {
108         local __addr
109         local __list=""
110
111         if __network_ifstatus "__addr" "$2" "['ipv6-address','ipv6-prefix-assignment'][*]['address','mask']" "/ "; then
112                 for __addr in $__addr; do
113                         case "$__addr" in
114                                 *:/*) __list="${__list:+$__list }${__addr%/*}1/${__addr##*/}" ;;
115                                 *)    __list="${__list:+$__list }${__addr}"                   ;;
116                         esac
117                 done
118
119                 export "$1=$__list"
120                 return 0
121         fi
122
123         unset "$1"
124         return 1
125 }
126
127 # determine all IPv6 prefixes of given logical interface
128 # 1: destination variable
129 # 2: interface
130 network_get_prefixes6() {
131         __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
132 }
133
134 # determine IPv4 gateway of given logical interface
135 # 1: destination variable
136 # 2: interface
137 # 3: consider inactive gateway if "true" (optional)
138 network_get_gateway() {
139         __network_ifstatus "$1" "$2" ".route[@.target='0.0.0.0' && !@.table].nexthop" "" 1 && \
140                 return 0
141
142         [ "$3" = 1 -o "$3" = "true" ] && \
143                 __network_ifstatus "$1" "$2" ".inactive.route[@.target='0.0.0.0' && !@.table].nexthop" "" 1
144 }
145
146 # determine IPv6 gateway of given logical interface
147 # 1: destination variable
148 # 2: interface
149 # 3: consider inactive gateway if "true" (optional)
150 network_get_gateway6() {
151         __network_ifstatus "$1" "$2" ".route[@.target='::' && !@.table].nexthop" "" 1 && \
152                 return 0
153
154         [ "$3" = 1 -o "$3" = "true" ] && \
155                 __network_ifstatus "$1" "$2" ".inactive.route[@.target='::' && !@.table].nexthop" "" 1
156 }
157
158 # determine the DNS servers of the given logical interface
159 # 1: destination variable
160 # 2: interface
161 # 3: consider inactive servers if "true" (optional)
162 network_get_dnsserver() {
163         __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
164
165         [ "$3" = 1 -o "$3" = "true" ] && \
166                 __network_ifstatus "$1" "$2" ".inactive['dns-server'][*]"
167 }
168
169 # determine the domains of the given logical interface
170 # 1: destination variable
171 # 2: interface
172 # 3: consider inactive domains if "true" (optional)
173 network_get_dnssearch() {
174         __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
175
176         [ "$3" = 1 -o "$3" = "true" ] && \
177                 __network_ifstatus "$1" "$2" ".inactive['dns-search'][*]"
178 }
179
180
181 # 1: destination variable
182 # 2: addr
183 # 3: inactive
184 __network_wan()
185 {
186         __network_ifstatus "$1" "" \
187                 "[@.route[@.target='$2' && !@.table]].interface" "" 1 && \
188                         return 0
189
190         [ "$3" = 1 -o "$3" = "true" ] && \
191                 __network_ifstatus "$1" "" \
192                         "[@.inactive.route[@.target='$2' && !@.table]].interface" "" 1
193 }
194
195 # find the logical interface which holds the current IPv4 default route
196 # 1: destination variable
197 # 2: consider inactive default routes if "true" (optional)
198 network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
199
200 # find the logical interface which holds the current IPv6 default route
201 # 1: destination variable
202 # 2: consider inactive dafault routes if "true" (optional)
203 network_find_wan6() { __network_wan "$1" "::" "$2"; }
204
205 # test whether the given logical interface is running
206 # 1: interface
207 network_is_up()
208 {
209         local __up
210         __network_ifstatus "__up" "$1" ".up" && [ "$__up" = 1 ]
211 }
212
213 # determine the protocol of the given logical interface
214 # 1: destination variable
215 # 2: interface
216 network_get_protocol() { __network_ifstatus "$1" "$2" ".proto"; }
217
218 # determine the layer 3 linux network device of the given logical interface
219 # 1: destination variable
220 # 2: interface
221 network_get_device() { __network_ifstatus "$1" "$2" ".l3_device"; }
222
223 # determine the layer 2 linux network device of the given logical interface
224 # 1: destination variable
225 # 2: interface
226 network_get_physdev() { __network_ifstatus "$1" "$2" ".device"; }
227
228 # defer netifd actions on the given linux network device
229 # 1: device name
230 network_defer_device()
231 {
232         ubus call network.device set_state \
233                 "$(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
234 }
235
236 # continue netifd actions on the given linux network device
237 # 1: device name
238 network_ready_device()
239 {
240         ubus call network.device set_state \
241                 "$(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
242 }
243
244 # flush the internal value cache to force re-reading values from ubus
245 network_flush_cache() { unset __NETWORK_CACHE; }