netifd: wireless: add support for tracking wifi-vlan sections
[project/netifd.git] / scripts / netifd-wireless.sh
1 NETIFD_MAIN_DIR="${NETIFD_MAIN_DIR:-/lib/netifd}"
2
3 . /usr/share/libubox/jshn.sh
4 . $NETIFD_MAIN_DIR/utils.sh
5
6 CMD_UP=0
7 CMD_SET_DATA=1
8 CMD_PROCESS_ADD=2
9 CMD_PROCESS_KILL_ALL=3
10 CMD_SET_RETRY=4
11
12 add_driver() {
13 return
14 }
15
16 wireless_setup_vif_failed() {
17 local error="$1"
18 echo "Interface $_w_iface setup failed: $error"
19 }
20
21 wireless_setup_failed() {
22 local error="$1"
23
24 echo "Device setup failed: $error"
25 wireless_set_retry 0
26 }
27
28 prepare_key_wep() {
29 local key="$1"
30 local hex=1
31
32 echo -n "$key" | grep -qE "[^a-fA-F0-9]" && hex=0
33 [ "${#key}" -eq 10 -a $hex -eq 1 ] || \
34 [ "${#key}" -eq 26 -a $hex -eq 1 ] || {
35 [ "${key:0:2}" = "s:" ] && key="${key#s:}"
36 key="$(echo -n "$key" | hexdump -ve '1/1 "%02x" ""')"
37 }
38 echo "$key"
39 }
40
41 _wdev_prepare_channel() {
42 json_get_vars channel hwmode
43
44 auto_channel=0
45 enable_ht=0
46 htmode=
47 hwmode="${hwmode##11}"
48 hwmode_n="${hwmode##n}"
49
50 case "$channel" in
51 ""|0|auto)
52 channel=0
53 auto_channel=1
54 ;;
55 [0-9]*) ;;
56 *)
57 wireless_setup_failed "INVALID_CHANNEL"
58 ;;
59 esac
60
61 [[ "$hwmode_n" = "$hwmode" ]] || {
62 enable_ht=1
63 hwmode="$hwmode_n"
64
65 json_get_vars htmode
66 case "$htmode" in
67 HT20|HT40+|HT40-);;
68 *) htmode= ;;
69 esac
70 }
71
72 case "$hwmode" in
73 a|b|g|ad) ;;
74 *)
75 if [ "$channel" -gt 14 ]; then
76 hwmode=a
77 else
78 hwmode=g
79 fi
80 ;;
81 esac
82 }
83
84 _wdev_handler() {
85 json_load "$data"
86
87 json_select config
88 _wdev_prepare_channel
89 json_select ..
90
91 eval "drv_$1_$2 \"$interface\""
92 }
93
94 _wdev_msg_call() {
95 local old_cb
96
97 json_set_namespace wdev old_cb
98 "$@"
99 json_set_namespace $old_cb
100 }
101
102 _wdev_wrapper() {
103 while [ -n "$1" ]; do
104 eval "$1() { _wdev_msg_call _$1 \"\$@\"; }"
105 shift
106 done
107 }
108
109 _wdev_notify_init() {
110 local command="$1"
111 local name="$2"
112 local value="$3"
113
114 json_init
115 json_add_int "command" "$command"
116 json_add_string "device" "$__netifd_device"
117 [ -n "$name" -a -n "$value" ] && json_add_string "$name" "$value"
118 json_add_object "data"
119 }
120
121 _wdev_notify() {
122 local options="$1"
123
124 json_close_object
125 ubus $options call network.wireless notify "$(json_dump)"
126 }
127
128 _wdev_add_variables() {
129 while [ -n "$1" ]; do
130 local var="${1%%=*}"
131 local val="$1"
132 shift
133 [[ "$var" = "$val" ]] && continue
134 val="${val#*=}"
135 json_add_string "$var" "$val"
136 done
137 }
138
139 _wireless_add_vif() {
140 local name="$1"; shift
141 local ifname="$1"; shift
142
143 _wdev_notify_init $CMD_SET_DATA "interface" "$name"
144 json_add_string "ifname" "$ifname"
145 _wdev_add_variables "$@"
146 _wdev_notify
147 }
148
149 _wireless_add_vlan() {
150 local name="$1"; shift
151 local ifname="$1"; shift
152
153 _wdev_notify_init $CMD_SET_DATA "vlan" "$name"
154 json_add_string "ifname" "$ifname"
155 _wdev_add_variables "$@"
156 _wdev_notify
157 }
158
159 _wireless_set_up() {
160 _wdev_notify_init $CMD_UP
161 _wdev_notify
162 }
163
164 _wireless_set_data() {
165 _wdev_notify_init $CMD_SET_DATA
166 _wdev_add_variables "$@"
167 _wdev_notify
168 }
169
170 _wireless_add_process() {
171 _wdev_notify_init $CMD_PROCESS_ADD
172 local exe="$2"
173 [ -L "$exe" ] && exe="$(readlink -f "$exe")"
174 json_add_int pid "$1"
175 json_add_string exe "$exe"
176 [ -n "$3" ] && json_add_boolean required 1
177 exe2="$(readlink -f /proc/$1/exe)"
178 [ "$exe" != "$exe2" ] && echo "WARNING (wireless_add_process): executable path $exe does not match process $1 path ($exe2)"
179 _wdev_notify
180 }
181
182 _wireless_process_kill_all() {
183 _wdev_notify_init $CMD_PROCESS_KILL_ALL
184 [ -n "$1" ] && json_add_int signal "$1"
185 _wdev_notify
186 }
187
188 _wireless_set_retry() {
189 _wdev_notify_init $CMD_SET_RETRY
190 json_add_int retry "$1"
191 _wdev_notify
192 }
193
194 _wdev_wrapper \
195 wireless_add_vif \
196 wireless_add_vlan \
197 wireless_set_up \
198 wireless_set_data \
199 wireless_add_process \
200 wireless_process_kill_all \
201 wireless_set_retry \
202
203 wireless_vif_parse_encryption() {
204 json_get_vars encryption
205 set_default encryption none
206
207 auth_mode_open=1
208 auth_mode_shared=0
209 auth_type=none
210 wpa_cipher=CCMP
211 case "$encryption" in
212 *tkip+aes|*tkip+ccmp|*aes+tkip|*ccmp+tkip) wpa_cipher="CCMP TKIP";;
213 *aes|*ccmp) wpa_cipher="CCMP";;
214 *tkip) wpa_cipher="TKIP";;
215 *gcmp) wpa_cipher="GCMP";;
216 esac
217
218 # 802.11n requires CCMP for WPA
219 [ "$enable_ht:$wpa_cipher" = "1:TKIP" ] && wpa_cipher="CCMP TKIP"
220
221 # Examples:
222 # psk-mixed/tkip => WPA1+2 PSK, TKIP
223 # wpa-psk2/tkip+aes => WPA2 PSK, CCMP+TKIP
224 # wpa2/tkip+aes => WPA2 RADIUS, CCMP+TKIP
225
226 case "$encryption" in
227 wpa2*|wpa3*|*psk2*|psk3*|sae*|owe*)
228 wpa=2
229 ;;
230 wpa*mixed*|*psk*mixed*)
231 wpa=3
232 ;;
233 wpa*|*psk*)
234 wpa=1
235 ;;
236 *)
237 wpa=0
238 wpa_cipher=
239 ;;
240 esac
241 wpa_pairwise="$wpa_cipher"
242
243 case "$encryption" in
244 owe*)
245 auth_type=owe
246 ;;
247 wpa3-mixed*)
248 auth_type=eap-eap192
249 ;;
250 wpa3*)
251 auth_type=eap192
252 ;;
253 psk3-mixed*|sae-mixed*)
254 auth_type=psk-sae
255 ;;
256 psk3*|sae*)
257 auth_type=sae
258 ;;
259 *psk*)
260 auth_type=psk
261 ;;
262 *wpa*|*8021x*)
263 auth_type=eap
264 ;;
265 *wep*)
266 auth_type=wep
267 case "$encryption" in
268 *shared*)
269 auth_mode_open=0
270 auth_mode_shared=1
271 ;;
272 *mixed*)
273 auth_mode_shared=1
274 ;;
275 esac
276 ;;
277 esac
278 }
279
280 _wireless_set_brsnoop_isolation() {
281 local multicast_to_unicast="$1"
282 local isolate
283
284 json_get_var isolate isolate
285
286 [ ${isolate:-0} -gt 0 -o -z "$network_bridge" ] && return
287 [ ${multicast_to_unicast:-1} -gt 0 ] && json_add_boolean isolate 1
288 }
289
290 for_each_interface() {
291 local _w_types="$1"; shift
292 local _w_ifaces _w_iface
293 local _w_type
294 local _w_found
295
296 local multicast_to_unicast
297
298 json_get_keys _w_ifaces interfaces
299 json_select interfaces
300 for _w_iface in $_w_ifaces; do
301 json_select "$_w_iface"
302 if [ -n "$_w_types" ]; then
303 json_get_var network_bridge bridge
304 json_get_var multicast_to_unicast multicast_to_unicast
305 json_select config
306 _wireless_set_brsnoop_isolation "$multicast_to_unicast"
307 json_get_var _w_type mode
308 json_select ..
309 _w_types=" $_w_types "
310 [[ "${_w_types%$_w_type*}" = "$_w_types" ]] && {
311 json_select ..
312 continue
313 }
314 fi
315 "$@" "$_w_iface"
316 json_select ..
317 done
318 json_select ..
319 }
320
321 for_each_vlan() {
322 local _w_vlans _w_vlan
323
324 json_get_keys _w_vlans vlans
325 json_select vlans
326 for _w_vlan in $_w_vlans; do
327 json_select "$_w_vlan"
328 json_select config
329 "$@" "$_w_vlan"
330 json_select ..
331 json_select ..
332 done
333 json_select ..
334 }
335
336 _wdev_common_device_config() {
337 config_add_string channel hwmode htmode noscan
338 }
339
340 _wdev_common_iface_config() {
341 config_add_string mode ssid encryption 'key:wpakey'
342 }
343
344 _wdev_common_vlan_config() {
345 config_add_string name vid iface
346 }
347
348 init_wireless_driver() {
349 name="$1"; shift
350 cmd="$1"; shift
351
352 case "$cmd" in
353 dump)
354 add_driver() {
355 eval "drv_$1_cleanup"
356
357 json_init
358 json_add_string name "$1"
359
360 json_add_array device
361 _wdev_common_device_config
362 eval "drv_$1_init_device_config"
363 json_close_array
364
365 json_add_array iface
366 _wdev_common_iface_config
367 eval "drv_$1_init_iface_config"
368 json_close_array
369
370 json_add_array vlan
371 _wdev_common_vlan_config
372 eval "drv_$1_init_vlan_config"
373 json_close_array
374
375 json_dump
376 }
377 ;;
378 setup|teardown)
379 interface="$1"; shift
380 data="$1"; shift
381 export __netifd_device="$interface"
382
383 add_driver() {
384 [[ "$name" == "$1" ]] || return 0
385 _wdev_handler "$1" "$cmd"
386 }
387 ;;
388 esac
389 }