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