scripts/netifd-wireless.sh: add support for specifying the operating band
[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 band hwmode
43
44 auto_channel=0
45 enable_ht=0
46 htmode=
47 hwmode="${hwmode##11}"
48
49 case "$channel" in
50 ""|0|auto)
51 channel=0
52 auto_channel=1
53 ;;
54 [0-9]*) ;;
55 *)
56 wireless_setup_failed "INVALID_CHANNEL"
57 ;;
58 esac
59
60 case "$hwmode" in
61 a|b|g|ad) ;;
62 *)
63 if [ "$channel" -gt 14 ]; then
64 hwmode=a
65 else
66 hwmode=g
67 fi
68 ;;
69 esac
70
71 case "$band" in
72 2g) hwmode=g;;
73 5g|6g|60g) hwmode=a;;
74 *)
75 case "$hwmode" in
76 *a) band=5g;;
77 *ad) band=60g;;
78 *b|*g) band=2g;;
79 esac
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 [ -n "$4" ] && json_add_boolean keep 1
178 exe2="$(readlink -f /proc/$1/exe)"
179 [ "$exe" != "$exe2" ] && echo "WARNING (wireless_add_process): executable path $exe does not match process $1 path ($exe2)"
180 _wdev_notify
181 }
182
183 _wireless_process_kill_all() {
184 _wdev_notify_init $CMD_PROCESS_KILL_ALL
185 [ -n "$1" ] && json_add_int signal "$1"
186 _wdev_notify
187 }
188
189 _wireless_set_retry() {
190 _wdev_notify_init $CMD_SET_RETRY
191 json_add_int retry "$1"
192 _wdev_notify
193 }
194
195 _wdev_wrapper \
196 wireless_add_vif \
197 wireless_add_vlan \
198 wireless_set_up \
199 wireless_set_data \
200 wireless_add_process \
201 wireless_process_kill_all \
202 wireless_set_retry \
203
204 wireless_vif_parse_encryption() {
205 json_get_vars encryption
206 set_default encryption none
207
208 auth_mode_open=1
209 auth_mode_shared=0
210 auth_type=none
211
212 if [ "$hwmode" = "ad" ]; then
213 wpa_cipher="GCMP"
214 else
215 wpa_cipher="CCMP"
216 fi
217
218 case "$encryption" in
219 *tkip+aes|*tkip+ccmp|*aes+tkip|*ccmp+tkip) wpa_cipher="CCMP TKIP";;
220 *aes|*ccmp) wpa_cipher="CCMP";;
221 *tkip) wpa_cipher="TKIP";;
222 *gcmp) wpa_cipher="GCMP";;
223 esac
224
225 # 802.11n requires CCMP for WPA
226 [ "$enable_ht:$wpa_cipher" = "1:TKIP" ] && wpa_cipher="CCMP TKIP"
227
228 # Examples:
229 # psk-mixed/tkip => WPA1+2 PSK, TKIP
230 # wpa-psk2/tkip+aes => WPA2 PSK, CCMP+TKIP
231 # wpa2/tkip+aes => WPA2 RADIUS, CCMP+TKIP
232
233 case "$encryption" in
234 wpa2*|wpa3*|*psk2*|psk3*|sae*|owe*)
235 wpa=2
236 ;;
237 wpa*mixed*|*psk*mixed*)
238 wpa=3
239 ;;
240 wpa*|*psk*)
241 wpa=1
242 ;;
243 *)
244 wpa=0
245 wpa_cipher=
246 ;;
247 esac
248 wpa_pairwise="$wpa_cipher"
249
250 case "$encryption" in
251 owe*)
252 auth_type=owe
253 ;;
254 wpa3-mixed*)
255 auth_type=eap-eap192
256 ;;
257 wpa3*)
258 auth_type=eap192
259 ;;
260 psk3-mixed*|sae-mixed*)
261 auth_type=psk-sae
262 ;;
263 psk3*|sae*)
264 auth_type=sae
265 ;;
266 *psk*)
267 auth_type=psk
268 ;;
269 *wpa*|*8021x*)
270 auth_type=eap
271 ;;
272 *wep*)
273 auth_type=wep
274 case "$encryption" in
275 *shared*)
276 auth_mode_open=0
277 auth_mode_shared=1
278 ;;
279 *mixed*)
280 auth_mode_shared=1
281 ;;
282 esac
283 ;;
284 esac
285
286 case "$encryption" in
287 *osen*)
288 auth_osen=1
289 ;;
290 esac
291 }
292
293 _wireless_set_brsnoop_isolation() {
294 local multicast_to_unicast="$1"
295 local isolate
296
297 json_get_var isolate isolate
298
299 [ ${isolate:-0} -gt 0 -o -z "$network_bridge" ] && return
300 [ ${multicast_to_unicast:-1} -gt 0 ] && json_add_boolean isolate 1
301 }
302
303 for_each_interface() {
304 local _w_types="$1"; shift
305 local _w_ifaces _w_iface
306 local _w_type
307 local _w_found
308
309 local multicast_to_unicast
310
311 json_get_keys _w_ifaces interfaces
312 json_select interfaces
313 for _w_iface in $_w_ifaces; do
314 json_select "$_w_iface"
315 if [ -n "$_w_types" ]; then
316 json_get_var network_bridge bridge
317 json_get_var multicast_to_unicast multicast_to_unicast
318 json_select config
319 _wireless_set_brsnoop_isolation "$multicast_to_unicast"
320 json_get_var _w_type mode
321 json_select ..
322 _w_types=" $_w_types "
323 [[ "${_w_types%$_w_type*}" = "$_w_types" ]] && {
324 json_select ..
325 continue
326 }
327 fi
328 "$@" "$_w_iface"
329 json_select ..
330 done
331 json_select ..
332 }
333
334 for_each_vlan() {
335 local _w_vlans _w_vlan
336
337 json_get_keys _w_vlans vlans
338 json_select vlans
339 for _w_vlan in $_w_vlans; do
340 json_select "$_w_vlan"
341 json_select config
342 "$@" "$_w_vlan"
343 json_select ..
344 json_select ..
345 done
346 json_select ..
347 }
348
349 for_each_station() {
350 local _w_stas _w_sta
351
352 json_get_keys _w_stas stas
353 json_select stas
354 for _w_sta in $_w_stas; do
355 json_select "$_w_sta"
356 json_select config
357 "$@" "$_w_sta"
358 json_select ..
359 json_select ..
360 done
361 json_select ..
362 }
363
364 _wdev_common_device_config() {
365 config_add_string channel hwmode band htmode noscan
366 }
367
368 _wdev_common_iface_config() {
369 config_add_string mode ssid encryption 'key:wpakey'
370 }
371
372 _wdev_common_vlan_config() {
373 config_add_string name vid iface
374 }
375
376 _wdev_common_station_config() {
377 config_add_string mac key vid iface
378 }
379
380 init_wireless_driver() {
381 name="$1"; shift
382 cmd="$1"; shift
383
384 case "$cmd" in
385 dump)
386 add_driver() {
387 eval "drv_$1_cleanup"
388
389 json_init
390 json_add_string name "$1"
391
392 json_add_array device
393 _wdev_common_device_config
394 eval "drv_$1_init_device_config"
395 json_close_array
396
397 json_add_array iface
398 _wdev_common_iface_config
399 eval "drv_$1_init_iface_config"
400 json_close_array
401
402 json_add_array vlan
403 _wdev_common_vlan_config
404 eval "drv_$1_init_vlan_config"
405 json_close_array
406
407 json_add_array station
408 _wdev_common_station_config
409 eval "drv_$1_init_station_config"
410 json_close_array
411
412 json_dump
413 }
414 ;;
415 setup|teardown)
416 interface="$1"; shift
417 data="$1"; shift
418 export __netifd_device="$interface"
419
420 add_driver() {
421 [[ "$name" == "$1" ]] || return 0
422 _wdev_handler "$1" "$cmd"
423 }
424 ;;
425 esac
426 }