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