1b0c717204d69ec0f89dd02282be6f279a673b6e
[openwrt/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 IP addresses of given logical interface
98 # 1: destination variable
99 # 2: interface
100 network_get_ipaddrs_all() {
101 local __addr
102 local __list=""
103
104 if __network_ifstatus "__addr" "$2" "['ipv4-address','ipv6-address','ipv6-prefix-assignment'][*].address"; then
105 for __addr in $__addr; do
106 case "$__addr" in
107 *:) __list="${__list:+$__list }${__addr}1" ;;
108 *) __list="${__list:+$__list }${__addr}" ;;
109 esac
110 done
111
112 export "$1=$__list"
113 return 0
114 fi
115
116 unset "$1"
117 return 1
118 }
119
120 # determine all IPv4 subnets of given logical interface
121 # 1: destination variable
122 # 2: interface
123 network_get_subnets() {
124 __network_ifstatus "$1" "$2" "['ipv4-address'][*]['address','mask']" "/ "
125 }
126
127 # determine all IPv6 subnets of given logical interface
128 # 1: destination variable
129 # 2: interface
130 network_get_subnets6() {
131 local __addr
132 local __list=""
133
134 if __network_ifstatus "__addr" "$2" "['ipv6-address','ipv6-prefix-assignment'][*]['address','mask']" "/ "; then
135 for __addr in $__addr; do
136 case "$__addr" in
137 *:/*) __list="${__list:+$__list }${__addr%/*}1/${__addr##*/}" ;;
138 *) __list="${__list:+$__list }${__addr}" ;;
139 esac
140 done
141
142 export "$1=$__list"
143 return 0
144 fi
145
146 unset "$1"
147 return 1
148 }
149
150 # determine all IPv6 prefixes of given logical interface
151 # 1: destination variable
152 # 2: interface
153 network_get_prefixes6() {
154 __network_ifstatus "$1" "$2" "['ipv6-prefix'][*]['address','mask']" "/ "
155 }
156
157 # determine IPv4 gateway of given logical interface
158 # 1: destination variable
159 # 2: interface
160 # 3: consider inactive gateway if "true" (optional)
161 network_get_gateway() {
162 __network_ifstatus "$1" "$2" ".route[@.target='0.0.0.0' && !@.table].nexthop" "" 1 && \
163 return 0
164
165 [ "$3" = 1 -o "$3" = "true" ] && \
166 __network_ifstatus "$1" "$2" ".inactive.route[@.target='0.0.0.0' && !@.table].nexthop" "" 1
167 }
168
169 # determine IPv6 gateway of given logical interface
170 # 1: destination variable
171 # 2: interface
172 # 3: consider inactive gateway if "true" (optional)
173 network_get_gateway6() {
174 __network_ifstatus "$1" "$2" ".route[@.target='::' && !@.table].nexthop" "" 1 && \
175 return 0
176
177 [ "$3" = 1 -o "$3" = "true" ] && \
178 __network_ifstatus "$1" "$2" ".inactive.route[@.target='::' && !@.table].nexthop" "" 1
179 }
180
181 # determine the DNS servers of the given logical interface
182 # 1: destination variable
183 # 2: interface
184 # 3: consider inactive servers if "true" (optional)
185 network_get_dnsserver() {
186 __network_ifstatus "$1" "$2" "['dns-server'][*]" && return 0
187
188 [ "$3" = 1 -o "$3" = "true" ] && \
189 __network_ifstatus "$1" "$2" ".inactive['dns-server'][*]"
190 }
191
192 # determine the domains of the given logical interface
193 # 1: destination variable
194 # 2: interface
195 # 3: consider inactive domains if "true" (optional)
196 network_get_dnssearch() {
197 __network_ifstatus "$1" "$2" "['dns-search'][*]" && return 0
198
199 [ "$3" = 1 -o "$3" = "true" ] && \
200 __network_ifstatus "$1" "$2" ".inactive['dns-search'][*]"
201 }
202
203
204 # 1: destination variable
205 # 2: addr
206 # 3: inactive
207 __network_wan()
208 {
209 __network_ifstatus "$1" "" \
210 "[@.route[@.target='$2' && !@.table]].interface" "" 1 && \
211 return 0
212
213 [ "$3" = 1 -o "$3" = "true" ] && \
214 __network_ifstatus "$1" "" \
215 "[@.inactive.route[@.target='$2' && !@.table]].interface" "" 1
216 }
217
218 # find the logical interface which holds the current IPv4 default route
219 # 1: destination variable
220 # 2: consider inactive default routes if "true" (optional)
221 network_find_wan() { __network_wan "$1" "0.0.0.0" "$2"; }
222
223 # find the logical interface which holds the current IPv6 default route
224 # 1: destination variable
225 # 2: consider inactive dafault routes if "true" (optional)
226 network_find_wan6() { __network_wan "$1" "::" "$2"; }
227
228 # test whether the given logical interface is running
229 # 1: interface
230 network_is_up()
231 {
232 local __up
233 __network_ifstatus "__up" "$1" ".up" && [ "$__up" = 1 ]
234 }
235
236 # determine the protocol of the given logical interface
237 # 1: destination variable
238 # 2: interface
239 network_get_protocol() { __network_ifstatus "$1" "$2" ".proto"; }
240
241 # determine the layer 3 linux network device of the given logical interface
242 # 1: destination variable
243 # 2: interface
244 network_get_device() { __network_ifstatus "$1" "$2" ".l3_device"; }
245
246 # determine the layer 2 linux network device of the given logical interface
247 # 1: destination variable
248 # 2: interface
249 network_get_physdev() { __network_ifstatus "$1" "$2" ".device"; }
250
251 # defer netifd actions on the given linux network device
252 # 1: device name
253 network_defer_device()
254 {
255 ubus call network.device set_state \
256 "$(printf '{ "name": "%s", "defer": true }' "$1")" 2>/dev/null
257 }
258
259 # continue netifd actions on the given linux network device
260 # 1: device name
261 network_ready_device()
262 {
263 ubus call network.device set_state \
264 "$(printf '{ "name": "%s", "defer": false }' "$1")" 2>/dev/null
265 }
266
267 # flush the internal value cache to force re-reading values from ubus
268 network_flush_cache() { unset __NETWORK_CACHE; }