wifi-scripts: Support HE Iftypes with multiple entries
[openwrt/staging/stintel.git] / package / network / config / wifi-scripts / files / lib / netifd / wireless / mac80211.sh
1 #!/bin/sh
2 . /lib/netifd/netifd-wireless.sh
3 . /lib/netifd/hostapd.sh
4 . /lib/functions/system.sh
5
6 init_wireless_driver "$@"
7
8 MP_CONFIG_INT="mesh_retry_timeout mesh_confirm_timeout mesh_holding_timeout mesh_max_peer_links
9 mesh_max_retries mesh_ttl mesh_element_ttl mesh_hwmp_max_preq_retries
10 mesh_path_refresh_time mesh_min_discovery_timeout mesh_hwmp_active_path_timeout
11 mesh_hwmp_preq_min_interval mesh_hwmp_net_diameter_traversal_time mesh_hwmp_rootmode
12 mesh_hwmp_rann_interval mesh_gate_announcements mesh_sync_offset_max_neighor
13 mesh_rssi_threshold mesh_hwmp_active_path_to_root_timeout mesh_hwmp_root_interval
14 mesh_hwmp_confirmation_interval mesh_awake_window mesh_plink_timeout"
15 MP_CONFIG_BOOL="mesh_auto_open_plinks mesh_fwding"
16 MP_CONFIG_STRING="mesh_power_mode"
17
18 wdev_tool() {
19 ucode /usr/share/hostap/wdev.uc "$@"
20 }
21
22 ubus_call() {
23 flock /var/run/hostapd.lock ubus call "$@"
24 }
25
26 drv_mac80211_init_device_config() {
27 hostapd_common_add_device_config
28
29 config_add_string path phy 'macaddr:macaddr'
30 config_add_string tx_burst
31 config_add_string distance
32 config_add_int beacon_int chanbw frag rts
33 config_add_int rxantenna txantenna txpower min_tx_power
34 config_add_int num_global_macaddr multiple_bssid
35 config_add_boolean noscan ht_coex acs_exclude_dfs background_radar
36 config_add_array ht_capab
37 config_add_array channels
38 config_add_array scan_list
39 config_add_boolean \
40 rxldpc \
41 short_gi_80 \
42 short_gi_160 \
43 tx_stbc_2by1 \
44 su_beamformer \
45 su_beamformee \
46 mu_beamformer \
47 mu_beamformee \
48 he_su_beamformer \
49 he_su_beamformee \
50 he_mu_beamformer \
51 vht_txop_ps \
52 htc_vht \
53 rx_antenna_pattern \
54 tx_antenna_pattern \
55 he_spr_sr_control \
56 he_spr_psr_enabled \
57 he_bss_color_enabled \
58 he_twt_required
59 config_add_int \
60 beamformer_antennas \
61 beamformee_antennas \
62 vht_max_a_mpdu_len_exp \
63 vht_max_mpdu \
64 vht_link_adapt \
65 vht160 \
66 rx_stbc \
67 tx_stbc \
68 he_bss_color \
69 he_spr_non_srg_obss_pd_max_offset
70 config_add_boolean \
71 ldpc \
72 greenfield \
73 short_gi_20 \
74 short_gi_40 \
75 max_amsdu \
76 dsss_cck_40
77 }
78
79 drv_mac80211_init_iface_config() {
80 hostapd_common_add_bss_config
81
82 config_add_string 'macaddr:macaddr' ifname
83
84 config_add_boolean wds powersave enable
85 config_add_string wds_bridge
86 config_add_int maxassoc
87 config_add_int max_listen_int
88 config_add_int dtim_period
89 config_add_int start_disabled
90
91 # mesh
92 config_add_string mesh_id
93 config_add_int $MP_CONFIG_INT
94 config_add_boolean $MP_CONFIG_BOOL
95 config_add_string $MP_CONFIG_STRING
96 }
97
98 mac80211_add_capabilities() {
99 local __var="$1"; shift
100 local __mask="$1"; shift
101 local __out= oifs
102
103 oifs="$IFS"
104 IFS=:
105 for capab in "$@"; do
106 set -- $capab
107
108 [ "$(($4))" -gt 0 ] || continue
109 [ "$(($__mask & $2))" -eq "$((${3:-$2}))" ] || continue
110 __out="$__out[$1]"
111 done
112 IFS="$oifs"
113
114 export -n -- "$__var=$__out"
115 }
116
117 mac80211_add_he_capabilities() {
118 local __out= oifs
119
120 oifs="$IFS"
121 IFS=:
122 for capab in "$@"; do
123 set -- $capab
124 [ "$(($4))" -gt 0 ] || continue
125 [ "$(((0x$2) & $3))" -gt 0 ] || {
126 eval "$1=0"
127 continue
128 }
129 append base_cfg "$1=1" "$N"
130 done
131 IFS="$oifs"
132 }
133
134 mac80211_hostapd_setup_base() {
135 local phy="$1"
136
137 json_select config
138
139 [ "$auto_channel" -gt 0 ] && channel=acs_survey
140
141 [ "$auto_channel" -gt 0 ] && json_get_vars acs_exclude_dfs
142 [ -n "$acs_exclude_dfs" ] && [ "$acs_exclude_dfs" -gt 0 ] &&
143 append base_cfg "acs_exclude_dfs=1" "$N"
144
145 json_get_vars noscan ht_coex min_tx_power:0 tx_burst
146 json_get_values ht_capab_list ht_capab
147 json_get_values channel_list channels
148
149 [ "$auto_channel" = 0 ] && [ -z "$channel_list" ] && \
150 channel_list="$channel"
151
152 [ "$min_tx_power" -gt 0 ] && append base_cfg "min_tx_power=$min_tx_power" "$N"
153
154 set_default noscan 0
155
156 [ "$noscan" -gt 0 ] && hostapd_noscan=1
157 [ "$tx_burst" = 0 ] && tx_burst=
158
159 chan_ofs=0
160 [ "$band" = "6g" ] && chan_ofs=1
161
162 ieee80211n=1
163 ht_capab=
164 case "$htmode" in
165 VHT20|HT20|HE20) ;;
166 HT40*|VHT40|VHT80|VHT160|HE40|HE80|HE160)
167 case "$hwmode" in
168 a)
169 case "$(( (($channel / 4) + $chan_ofs) % 2 ))" in
170 1) ht_capab="[HT40+]";;
171 0) ht_capab="[HT40-]";;
172 esac
173 ;;
174 *)
175 case "$htmode" in
176 HT40+) ht_capab="[HT40+]";;
177 HT40-) ht_capab="[HT40-]";;
178 *)
179 if [ "$channel" -lt 7 ]; then
180 ht_capab="[HT40+]"
181 else
182 ht_capab="[HT40-]"
183 fi
184 ;;
185 esac
186 ;;
187 esac
188 [ "$auto_channel" -gt 0 ] && ht_capab="[HT40+]"
189 ;;
190 *) ieee80211n= ;;
191 esac
192
193 [ -n "$ieee80211n" ] && {
194 append base_cfg "ieee80211n=1" "$N"
195
196 set_default ht_coex 0
197 append base_cfg "ht_coex=$ht_coex" "$N"
198
199 json_get_vars \
200 ldpc:1 \
201 greenfield:0 \
202 short_gi_20:1 \
203 short_gi_40:1 \
204 tx_stbc:1 \
205 rx_stbc:3 \
206 max_amsdu:1 \
207 dsss_cck_40:1
208
209 ht_cap_mask=0
210 for cap in $(iw phy "$phy" info | grep 'Capabilities:' | cut -d: -f2); do
211 ht_cap_mask="$(($ht_cap_mask | $cap))"
212 done
213
214 cap_rx_stbc=$((($ht_cap_mask >> 8) & 3))
215 [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
216 ht_cap_mask="$(( ($ht_cap_mask & ~(0x300)) | ($cap_rx_stbc << 8) ))"
217
218 mac80211_add_capabilities ht_capab_flags $ht_cap_mask \
219 LDPC:0x1::$ldpc \
220 GF:0x10::$greenfield \
221 SHORT-GI-20:0x20::$short_gi_20 \
222 SHORT-GI-40:0x40::$short_gi_40 \
223 TX-STBC:0x80::$tx_stbc \
224 RX-STBC1:0x300:0x100:1 \
225 RX-STBC12:0x300:0x200:1 \
226 RX-STBC123:0x300:0x300:1 \
227 MAX-AMSDU-7935:0x800::$max_amsdu \
228 DSSS_CCK-40:0x1000::$dsss_cck_40
229
230 ht_capab="$ht_capab$ht_capab_flags"
231 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
232 }
233
234 # 802.11ac
235 enable_ac=0
236 vht_oper_chwidth=0
237 vht_center_seg0=
238
239 idx="$channel"
240 case "$htmode" in
241 VHT20|HE20) enable_ac=1;;
242 VHT40|HE40)
243 case "$(( (($channel / 4) + $chan_ofs) % 2 ))" in
244 1) idx=$(($channel + 2));;
245 0) idx=$(($channel - 2));;
246 esac
247 enable_ac=1
248 vht_center_seg0=$idx
249 ;;
250 VHT80|HE80)
251 case "$(( (($channel / 4) + $chan_ofs) % 4 ))" in
252 1) idx=$(($channel + 6));;
253 2) idx=$(($channel + 2));;
254 3) idx=$(($channel - 2));;
255 0) idx=$(($channel - 6));;
256 esac
257 enable_ac=1
258 vht_oper_chwidth=1
259 vht_center_seg0=$idx
260 ;;
261 VHT160|HE160)
262 if [ "$band" = "6g" ]; then
263 case "$channel" in
264 1|5|9|13|17|21|25|29) idx=15;;
265 33|37|41|45|49|53|57|61) idx=47;;
266 65|69|73|77|81|85|89|93) idx=79;;
267 97|101|105|109|113|117|121|125) idx=111;;
268 129|133|137|141|145|149|153|157) idx=143;;
269 161|165|169|173|177|181|185|189) idx=175;;
270 193|197|201|205|209|213|217|221) idx=207;;
271 esac
272 else
273 case "$channel" in
274 36|40|44|48|52|56|60|64) idx=50;;
275 100|104|108|112|116|120|124|128) idx=114;;
276 esac
277 fi
278 enable_ac=1
279 vht_oper_chwidth=2
280 vht_center_seg0=$idx
281 ;;
282 esac
283 [ "$band" = "5g" ] && {
284 json_get_vars background_radar:0
285
286 [ "$background_radar" -eq 1 ] && append base_cfg "enable_background_radar=1" "$N"
287 }
288 [ "$band" = "6g" ] && {
289 op_class=
290 case "$htmode" in
291 HE20) op_class=131;;
292 HE*) op_class=$((132 + $vht_oper_chwidth))
293 esac
294 [ -n "$op_class" ] && append base_cfg "op_class=$op_class" "$N"
295 }
296 [ "$hwmode" = "a" ] || enable_ac=0
297
298 if [ "$enable_ac" != "0" ]; then
299 json_get_vars \
300 rxldpc:1 \
301 short_gi_80:1 \
302 short_gi_160:1 \
303 tx_stbc_2by1:1 \
304 su_beamformer:1 \
305 su_beamformee:1 \
306 mu_beamformer:1 \
307 mu_beamformee:1 \
308 vht_txop_ps:1 \
309 htc_vht:1 \
310 beamformee_antennas:4 \
311 beamformer_antennas:4 \
312 rx_antenna_pattern:1 \
313 tx_antenna_pattern:1 \
314 vht_max_a_mpdu_len_exp:7 \
315 vht_max_mpdu:11454 \
316 rx_stbc:4 \
317 vht_link_adapt:3 \
318 vht160:2
319
320 set_default tx_burst 2.0
321 append base_cfg "ieee80211ac=1" "$N"
322 vht_cap=0
323 for cap in $(iw phy "$phy" info | awk -F "[()]" '/VHT Capabilities/ { print $2 }'); do
324 vht_cap="$(($vht_cap | $cap))"
325 done
326
327 append base_cfg "vht_oper_chwidth=$vht_oper_chwidth" "$N"
328 append base_cfg "vht_oper_centr_freq_seg0_idx=$vht_center_seg0" "$N"
329
330 cap_rx_stbc=$((($vht_cap >> 8) & 7))
331 [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
332 vht_cap="$(( ($vht_cap & ~(0x700)) | ($cap_rx_stbc << 8) ))"
333
334 [ "$vht_oper_chwidth" -lt 2 ] && {
335 vht160=0
336 short_gi_160=0
337 }
338
339 mac80211_add_capabilities vht_capab $vht_cap \
340 RXLDPC:0x10::$rxldpc \
341 SHORT-GI-80:0x20::$short_gi_80 \
342 SHORT-GI-160:0x40::$short_gi_160 \
343 TX-STBC-2BY1:0x80::$tx_stbc_2by1 \
344 SU-BEAMFORMER:0x800::$su_beamformer \
345 SU-BEAMFORMEE:0x1000::$su_beamformee \
346 MU-BEAMFORMER:0x80000::$mu_beamformer \
347 MU-BEAMFORMEE:0x100000::$mu_beamformee \
348 VHT-TXOP-PS:0x200000::$vht_txop_ps \
349 HTC-VHT:0x400000::$htc_vht \
350 RX-ANTENNA-PATTERN:0x10000000::$rx_antenna_pattern \
351 TX-ANTENNA-PATTERN:0x20000000::$tx_antenna_pattern \
352 RX-STBC-1:0x700:0x100:1 \
353 RX-STBC-12:0x700:0x200:1 \
354 RX-STBC-123:0x700:0x300:1 \
355 RX-STBC-1234:0x700:0x400:1 \
356
357 [ "$(($vht_cap & 0x800))" -gt 0 -a "$su_beamformer" -gt 0 ] && {
358 cap_ant="$(( ( ($vht_cap >> 16) & 3 ) + 1 ))"
359 [ "$cap_ant" -gt "$beamformer_antennas" ] && cap_ant="$beamformer_antennas"
360 [ "$cap_ant" -gt 1 ] && vht_capab="$vht_capab[SOUNDING-DIMENSION-$cap_ant]"
361 }
362
363 [ "$(($vht_cap & 0x1000))" -gt 0 -a "$su_beamformee" -gt 0 ] && {
364 cap_ant="$(( ( ($vht_cap >> 13) & 3 ) + 1 ))"
365 [ "$cap_ant" -gt "$beamformee_antennas" ] && cap_ant="$beamformee_antennas"
366 [ "$cap_ant" -gt 1 ] && vht_capab="$vht_capab[BF-ANTENNA-$cap_ant]"
367 }
368
369 # supported Channel widths
370 vht160_hw=0
371 [ "$(($vht_cap & 12))" -eq 4 -a 1 -le "$vht160" ] && \
372 vht160_hw=1
373 [ "$(($vht_cap & 12))" -eq 8 -a 2 -le "$vht160" ] && \
374 vht160_hw=2
375 [ "$vht160_hw" = 1 ] && vht_capab="$vht_capab[VHT160]"
376 [ "$vht160_hw" = 2 ] && vht_capab="$vht_capab[VHT160-80PLUS80]"
377
378 # maximum MPDU length
379 vht_max_mpdu_hw=3895
380 [ "$(($vht_cap & 3))" -ge 1 -a 7991 -le "$vht_max_mpdu" ] && \
381 vht_max_mpdu_hw=7991
382 [ "$(($vht_cap & 3))" -ge 2 -a 11454 -le "$vht_max_mpdu" ] && \
383 vht_max_mpdu_hw=11454
384 [ "$vht_max_mpdu_hw" != 3895 ] && \
385 vht_capab="$vht_capab[MAX-MPDU-$vht_max_mpdu_hw]"
386
387 # maximum A-MPDU length exponent
388 vht_max_a_mpdu_len_exp_hw=0
389 [ "$(($vht_cap & 58720256))" -ge 8388608 -a 1 -le "$vht_max_a_mpdu_len_exp" ] && \
390 vht_max_a_mpdu_len_exp_hw=1
391 [ "$(($vht_cap & 58720256))" -ge 16777216 -a 2 -le "$vht_max_a_mpdu_len_exp" ] && \
392 vht_max_a_mpdu_len_exp_hw=2
393 [ "$(($vht_cap & 58720256))" -ge 25165824 -a 3 -le "$vht_max_a_mpdu_len_exp" ] && \
394 vht_max_a_mpdu_len_exp_hw=3
395 [ "$(($vht_cap & 58720256))" -ge 33554432 -a 4 -le "$vht_max_a_mpdu_len_exp" ] && \
396 vht_max_a_mpdu_len_exp_hw=4
397 [ "$(($vht_cap & 58720256))" -ge 41943040 -a 5 -le "$vht_max_a_mpdu_len_exp" ] && \
398 vht_max_a_mpdu_len_exp_hw=5
399 [ "$(($vht_cap & 58720256))" -ge 50331648 -a 6 -le "$vht_max_a_mpdu_len_exp" ] && \
400 vht_max_a_mpdu_len_exp_hw=6
401 [ "$(($vht_cap & 58720256))" -ge 58720256 -a 7 -le "$vht_max_a_mpdu_len_exp" ] && \
402 vht_max_a_mpdu_len_exp_hw=7
403 vht_capab="$vht_capab[MAX-A-MPDU-LEN-EXP$vht_max_a_mpdu_len_exp_hw]"
404
405 # whether or not the STA supports link adaptation using VHT variant
406 vht_link_adapt_hw=0
407 [ "$(($vht_cap & 201326592))" -ge 134217728 -a 2 -le "$vht_link_adapt" ] && \
408 vht_link_adapt_hw=2
409 [ "$(($vht_cap & 201326592))" -ge 201326592 -a 3 -le "$vht_link_adapt" ] && \
410 vht_link_adapt_hw=3
411 [ "$vht_link_adapt_hw" != 0 ] && \
412 vht_capab="$vht_capab[VHT-LINK-ADAPT-$vht_link_adapt_hw]"
413
414 [ -n "$vht_capab" ] && append base_cfg "vht_capab=$vht_capab" "$N"
415 fi
416
417 # 802.11ax
418 enable_ax=0
419 case "$htmode" in
420 HE*) enable_ax=1 ;;
421 esac
422
423 if [ "$enable_ax" != "0" ]; then
424 json_get_vars \
425 he_su_beamformer:1 \
426 he_su_beamformee:1 \
427 he_mu_beamformer:1 \
428 he_twt_required:0 \
429 he_spr_sr_control:3 \
430 he_spr_psr_enabled:0 \
431 he_spr_non_srg_obss_pd_max_offset:0 \
432 he_bss_color:128 \
433 he_bss_color_enabled:1
434
435 he_phy_cap=$(iw phy "$phy" info | sed -n '/HE Iftypes: .*AP/,$p' | awk -F "[()]" '/HE PHY Capabilities/ { print $2 }' | head -1)
436 he_phy_cap=${he_phy_cap:2}
437 he_mac_cap=$(iw phy "$phy" info | sed -n '/HE Iftypes: .*AP/,$p' | awk -F "[()]" '/HE MAC Capabilities/ { print $2 }' | head -1)
438 he_mac_cap=${he_mac_cap:2}
439
440 append base_cfg "ieee80211ax=1" "$N"
441 [ "$hwmode" = "a" ] && {
442 append base_cfg "he_oper_chwidth=$vht_oper_chwidth" "$N"
443 append base_cfg "he_oper_centr_freq_seg0_idx=$vht_center_seg0" "$N"
444 }
445
446 mac80211_add_he_capabilities \
447 he_su_beamformer:${he_phy_cap:6:2}:0x80:$he_su_beamformer \
448 he_su_beamformee:${he_phy_cap:8:2}:0x1:$he_su_beamformee \
449 he_mu_beamformer:${he_phy_cap:8:2}:0x2:$he_mu_beamformer \
450 he_spr_psr_enabled:${he_phy_cap:14:2}:0x1:$he_spr_psr_enabled \
451 he_twt_required:${he_mac_cap:0:2}:0x6:$he_twt_required
452
453 if [ "$he_bss_color_enabled" -gt 0 ]; then
454 append base_cfg "he_bss_color=$he_bss_color" "$N"
455 [ "$he_spr_non_srg_obss_pd_max_offset" -gt 0 ] && { \
456 append base_cfg "he_spr_non_srg_obss_pd_max_offset=$he_spr_non_srg_obss_pd_max_offset" "$N"
457 he_spr_sr_control=$((he_spr_sr_control | (1 << 2)))
458 }
459 [ "$he_spr_psr_enabled" -gt 0 ] || he_spr_sr_control=$((he_spr_sr_control | (1 << 0)))
460 append base_cfg "he_spr_sr_control=$he_spr_sr_control" "$N"
461 else
462 append base_cfg "he_bss_color_disabled=1" "$N"
463 fi
464
465
466 append base_cfg "he_default_pe_duration=4" "$N"
467 append base_cfg "he_rts_threshold=1023" "$N"
468 append base_cfg "he_mu_edca_qos_info_param_count=0" "$N"
469 append base_cfg "he_mu_edca_qos_info_q_ack=0" "$N"
470 append base_cfg "he_mu_edca_qos_info_queue_request=0" "$N"
471 append base_cfg "he_mu_edca_qos_info_txop_request=0" "$N"
472 append base_cfg "he_mu_edca_ac_be_aifsn=8" "$N"
473 append base_cfg "he_mu_edca_ac_be_aci=0" "$N"
474 append base_cfg "he_mu_edca_ac_be_ecwmin=9" "$N"
475 append base_cfg "he_mu_edca_ac_be_ecwmax=10" "$N"
476 append base_cfg "he_mu_edca_ac_be_timer=255" "$N"
477 append base_cfg "he_mu_edca_ac_bk_aifsn=15" "$N"
478 append base_cfg "he_mu_edca_ac_bk_aci=1" "$N"
479 append base_cfg "he_mu_edca_ac_bk_ecwmin=9" "$N"
480 append base_cfg "he_mu_edca_ac_bk_ecwmax=10" "$N"
481 append base_cfg "he_mu_edca_ac_bk_timer=255" "$N"
482 append base_cfg "he_mu_edca_ac_vi_ecwmin=5" "$N"
483 append base_cfg "he_mu_edca_ac_vi_ecwmax=7" "$N"
484 append base_cfg "he_mu_edca_ac_vi_aifsn=5" "$N"
485 append base_cfg "he_mu_edca_ac_vi_aci=2" "$N"
486 append base_cfg "he_mu_edca_ac_vi_timer=255" "$N"
487 append base_cfg "he_mu_edca_ac_vo_aifsn=5" "$N"
488 append base_cfg "he_mu_edca_ac_vo_aci=3" "$N"
489 append base_cfg "he_mu_edca_ac_vo_ecwmin=5" "$N"
490 append base_cfg "he_mu_edca_ac_vo_ecwmax=7" "$N"
491 append base_cfg "he_mu_edca_ac_vo_timer=255" "$N"
492 fi
493
494 hostapd_prepare_device_config "$hostapd_conf_file" nl80211
495 cat >> "$hostapd_conf_file" <<EOF
496 ${channel:+channel=$channel}
497 ${channel_list:+chanlist=$channel_list}
498 ${hostapd_noscan:+noscan=1}
499 ${tx_burst:+tx_queue_data2_burst=$tx_burst}
500 ${multiple_bssid:+mbssid=$multiple_bssid}
501 #num_global_macaddr=$num_global_macaddr
502 $base_cfg
503
504 EOF
505 json_select ..
506 }
507
508 mac80211_hostapd_setup_bss() {
509 local phy="$1"
510 local ifname="$2"
511 local macaddr="$3"
512 local type="$4"
513
514 hostapd_cfg=
515 append hostapd_cfg "$type=$ifname" "$N"
516
517 hostapd_set_bss_options hostapd_cfg "$phy" "$vif" || return 1
518 json_get_vars wds wds_bridge dtim_period max_listen_int start_disabled
519
520 set_default wds 0
521 set_default start_disabled 0
522
523 [ "$wds" -gt 0 ] && {
524 append hostapd_cfg "wds_sta=1" "$N"
525 [ -n "$wds_bridge" ] && append hostapd_cfg "wds_bridge=$wds_bridge" "$N"
526 }
527 [ "$staidx" -gt 0 -o "$start_disabled" -eq 1 ] && append hostapd_cfg "start_disabled=1" "$N"
528
529 cat >> /var/run/hostapd-$phy.conf <<EOF
530 $hostapd_cfg
531 bssid=$macaddr
532 ${default_macaddr:+#default_macaddr}
533 ${dtim_period:+dtim_period=$dtim_period}
534 ${max_listen_int:+max_listen_interval=$max_listen_int}
535 EOF
536 }
537
538 mac80211_get_addr() {
539 local phy="$1"
540 local idx="$(($2 + 1))"
541
542 head -n $idx /sys/class/ieee80211/${phy}/addresses | tail -n1
543 }
544
545 mac80211_generate_mac() {
546 local phy="$1"
547 local id="${macidx:-0}"
548
549 wdev_tool "$phy" get_macaddr id=$id num_global=$num_global_macaddr mbssid=${multiple_bssid:-0}
550 }
551
552 get_board_phy_name() (
553 local path="$1"
554 local fallback_phy=""
555
556 __check_phy() {
557 local val="$1"
558 local key="$2"
559 local ref_path="$3"
560
561 json_select "$key"
562 json_get_vars path
563 json_select ..
564
565 [ "${ref_path%+*}" = "$path" ] && fallback_phy=$key
566 [ "$ref_path" = "$path" ] || return 0
567
568 echo "$key"
569 exit
570 }
571
572 json_load_file /etc/board.json
573 json_for_each_item __check_phy wlan "$path"
574 [ -n "$fallback_phy" ] && echo "${fallback_phy}.${path##*+}"
575 )
576
577 rename_board_phy_by_path() {
578 local path="$1"
579
580 local new_phy="$(get_board_phy_name "$path")"
581 [ -z "$new_phy" -o "$new_phy" = "$phy" ] && return
582
583 iw "$phy" set name "$new_phy" && phy="$new_phy"
584 }
585
586 rename_board_phy_by_name() (
587 local phy="$1"
588 local suffix="${phy##*.}"
589 [ "$suffix" = "$phy" ] && suffix=
590
591 json_load_file /etc/board.json
592 json_select wlan
593 json_select "${phy%.*}" || return 0
594 json_get_vars path
595
596 prev_phy="$(iwinfo nl80211 phyname "path=$path${suffix:++$suffix}")"
597 [ -n "$prev_phy" ] || return 0
598
599 [ "$prev_phy" = "$phy" ] && return 0
600
601 iw "$prev_phy" set name "$phy"
602 )
603
604 find_phy() {
605 [ -n "$phy" ] && {
606 rename_board_phy_by_name "$phy"
607 [ -d /sys/class/ieee80211/$phy ] && return 0
608 }
609 [ -n "$path" ] && {
610 phy="$(iwinfo nl80211 phyname "path=$path")"
611 [ -n "$phy" ] && {
612 rename_board_phy_by_path "$path"
613 return 0
614 }
615 }
616 [ -n "$macaddr" ] && {
617 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
618 grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && {
619 path="$(iwinfo nl80211 path "$phy")"
620 rename_board_phy_by_path "$path"
621 return 0
622 }
623 done
624 }
625 return 1
626 }
627
628 mac80211_check_ap() {
629 has_ap=1
630 }
631
632 mac80211_set_ifname() {
633 local phy="$1"
634 local prefix="$2"
635 eval "ifname=\"$phy-$prefix\${idx_$prefix:-0}\"; idx_$prefix=\$((\${idx_$prefix:-0 } + 1))"
636 }
637
638 mac80211_prepare_vif() {
639 json_select config
640
641 json_get_vars ifname mode ssid wds powersave macaddr enable wpa_psk_file vlan_file
642
643 [ -n "$ifname" ] || {
644 local prefix;
645
646 case "$mode" in
647 ap|sta|mesh) prefix=$mode;;
648 adhoc) prefix=ibss;;
649 monitor) prefix=mon;;
650 esac
651
652 mac80211_set_ifname "$phy" "$prefix"
653 }
654
655 append active_ifnames "$ifname"
656 set_default wds 0
657 set_default powersave 0
658 json_add_string _ifname "$ifname"
659
660 default_macaddr=
661 if [ -z "$macaddr" ]; then
662 macaddr="$(mac80211_generate_mac $phy)"
663 macidx="$(($macidx + 1))"
664 default_macaddr=1
665 elif [ "$macaddr" = 'random' ]; then
666 macaddr="$(macaddr_random)"
667 fi
668 json_add_string _macaddr "$macaddr"
669 json_add_string _default_macaddr "$default_macaddr"
670 json_select ..
671
672
673 [ "$mode" == "ap" ] && {
674 [ -z "$wpa_psk_file" ] && hostapd_set_psk "$ifname"
675 [ -z "$vlan_file" ] && hostapd_set_vlan "$ifname"
676 }
677
678 json_select config
679
680 # It is far easier to delete and create the desired interface
681 case "$mode" in
682 ap)
683 # Hostapd will handle recreating the interface and
684 # subsequent virtual APs belonging to the same PHY
685 if [ -n "$hostapd_ctrl" ]; then
686 type=bss
687 else
688 type=interface
689 fi
690
691 mac80211_hostapd_setup_bss "$phy" "$ifname" "$macaddr" "$type" || return
692
693 [ -n "$hostapd_ctrl" ] || {
694 ap_ifname="${ifname}"
695 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
696 }
697 ;;
698 esac
699
700 json_select ..
701 }
702
703 mac80211_prepare_iw_htmode() {
704 case "$htmode" in
705 VHT20|HT20|HE20) iw_htmode=HT20;;
706 HT40*|VHT40|VHT160|HE40)
707 case "$band" in
708 2g)
709 case "$htmode" in
710 HT40+) iw_htmode="HT40+";;
711 HT40-) iw_htmode="HT40-";;
712 *)
713 if [ "$channel" -lt 7 ]; then
714 iw_htmode="HT40+"
715 else
716 iw_htmode="HT40-"
717 fi
718 ;;
719 esac
720 ;;
721 *)
722 case "$(( ($channel / 4) % 2 ))" in
723 1) iw_htmode="HT40+" ;;
724 0) iw_htmode="HT40-";;
725 esac
726 ;;
727 esac
728 [ "$auto_channel" -gt 0 ] && iw_htmode="HT40+"
729 ;;
730 VHT80|HE80)
731 iw_htmode="80MHZ"
732 ;;
733 NONE|NOHT)
734 iw_htmode="NOHT"
735 ;;
736 *) iw_htmode="" ;;
737 esac
738 }
739
740 mac80211_add_mesh_params() {
741 for var in $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING; do
742 eval "mp_val=\"\$$var\""
743 [ -n "$mp_val" ] && json_add_string "$var" "$mp_val"
744 done
745 }
746
747 mac80211_setup_adhoc() {
748 local enable=$1
749 json_get_vars bssid ssid key mcast_rate
750
751 NEWUMLIST="${NEWUMLIST}$ifname "
752
753 [ "$enable" = 0 ] && {
754 ip link set dev "$ifname" down
755 return 0
756 }
757
758 keyspec=
759 [ "$auth_type" = "wep" ] && {
760 set_default key 1
761 case "$key" in
762 [1234])
763 local idx
764 for idx in 1 2 3 4; do
765 json_get_var ikey "key$idx"
766
767 [ -n "$ikey" ] && {
768 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
769 [ $idx -eq $key ] && ikey="d:$ikey"
770 append keyspec "$ikey"
771 }
772 done
773 ;;
774 *)
775 append keyspec "d:0:$(prepare_key_wep "$key")"
776 ;;
777 esac
778 }
779
780 brstr=
781 for br in $basic_rate_list; do
782 wpa_supplicant_add_rate brstr "$br"
783 done
784
785 mcval=
786 [ -n "$mcast_rate" ] && wpa_supplicant_add_rate mcval "$mcast_rate"
787
788 local prev
789 json_set_namespace wdev_uc prev
790
791 json_add_object "$ifname"
792 json_add_string mode adhoc
793 [ -n "$default_macaddr" ] || json_add_string macaddr "$macaddr"
794 json_add_string ssid "$ssid"
795 json_add_string freq "$freq"
796 json_add_string htmode "$iw_htmode"
797 [ -n "$bssid" ] && json_add_string bssid "$bssid"
798 json_add_int beacon-interval "$beacon_int"
799 [ -n "$brstr" ] && json_add_string basic-rates "$brstr"
800 [ -n "$mcval" ] && json_add_string mcast-rate "$mcval"
801 [ -n "$keyspec" ] && json_add_string keys "$keyspec"
802 json_close_object
803
804 json_set_namespace "$prev"
805 }
806
807 mac80211_setup_mesh() {
808 json_get_vars ssid mesh_id mcast_rate
809
810 mcval=
811 [ -n "$mcast_rate" ] && wpa_supplicant_add_rate mcval "$mcast_rate"
812 [ -n "$mesh_id" ] && ssid="$mesh_id"
813
814 local prev
815 json_set_namespace wdev_uc prev
816
817 json_add_object "$ifname"
818 json_add_string mode mesh
819 [ -n "$default_macaddr" ] || json_add_string macaddr "$macaddr"
820 json_add_string ssid "$ssid"
821 json_add_string freq "$freq"
822 json_add_string htmode "$iw_htmode"
823 [ -n "$mcval" ] && json_add_string mcast-rate "$mcval"
824 json_add_int beacon-interval "$beacon_int"
825 mac80211_add_mesh_params
826
827 json_close_object
828
829 json_set_namespace "$prev"
830 }
831
832 mac80211_setup_monitor() {
833 local prev
834 json_set_namespace wdev_uc prev
835
836 json_add_object "$ifname"
837 json_add_string mode monitor
838 [ -n "$freq" ] && json_add_string freq "$freq"
839 json_add_string htmode "$iw_htmode"
840 json_close_object
841
842 json_set_namespace "$prev"
843 }
844
845 mac80211_set_vif_txpower() {
846 local name="$1"
847
848 json_select config
849 json_get_var ifname _ifname
850 json_get_vars vif_txpower
851 json_select ..
852
853 [ -z "$vif_txpower" ] || iw dev "$ifname" set txpower fixed "${vif_txpower%%.*}00"
854 }
855
856 wpa_supplicant_init_config() {
857 json_set_namespace wpa_supp prev
858
859 json_init
860 json_add_array config
861
862 json_set_namespace "$prev"
863 }
864
865 wpa_supplicant_add_interface() {
866 local ifname="$1"
867 local mode="$2"
868 local prev
869
870 _wpa_supplicant_common "$ifname"
871
872 json_set_namespace wpa_supp prev
873
874 json_add_object
875 json_add_string ctrl "$_rpath"
876 json_add_string iface "$ifname"
877 json_add_string mode "$mode"
878 json_add_string config "$_config"
879 [ -n "$default_macaddr" ] || json_add_string macaddr "$macaddr"
880 [ -n "$network_bridge" ] && json_add_string bridge "$network_bridge"
881 [ -n "$wds" ] && json_add_boolean 4addr "$wds"
882 json_add_boolean powersave "$powersave"
883 [ "$mode" = "mesh" ] && mac80211_add_mesh_params
884 json_close_object
885
886 json_set_namespace "$prev"
887
888 wpa_supp_init=1
889 }
890
891 wpa_supplicant_set_config() {
892 local phy="$1"
893 local prev
894
895 json_set_namespace wpa_supp prev
896 json_close_array
897 json_add_string phy "$phy"
898 json_add_boolean defer 1
899 local data="$(json_dump)"
900
901 json_cleanup
902 json_set_namespace "$prev"
903
904 ubus -S -t 0 wait_for wpa_supplicant || {
905 [ -n "$wpa_supp_init" ] || return 0
906
907 ubus wait_for wpa_supplicant
908 }
909
910 local supplicant_res="$(ubus_call wpa_supplicant config_set "$data")"
911 ret="$?"
912 [ "$ret" != 0 -o -z "$supplicant_res" ] && wireless_setup_vif_failed WPA_SUPPLICANT_FAILED
913
914 wireless_add_process "$(jsonfilter -s "$supplicant_res" -l 1 -e @.pid)" "/usr/sbin/wpa_supplicant" 1 1
915
916 }
917
918 hostapd_set_config() {
919 [ -n "$hostapd_ctrl" ] || {
920 ubus_call hostapd config_set '{ "phy": "'"$phy"'", "config": "", "prev_config": "'"${hostapd_conf_file}.prev"'" }' > /dev/null
921 return 0;
922 }
923
924 ubus wait_for hostapd
925 local hostapd_res="$(ubus_call hostapd config_set "{ \"phy\": \"$phy\", \"config\":\"${hostapd_conf_file}\", \"prev_config\": \"${hostapd_conf_file}.prev\"}")"
926 ret="$?"
927 [ "$ret" != 0 -o -z "$hostapd_res" ] && {
928 wireless_setup_failed HOSTAPD_START_FAILED
929 return
930 }
931 wireless_add_process "$(jsonfilter -s "$hostapd_res" -l 1 -e @.pid)" "/usr/sbin/hostapd" 1 1
932 }
933
934
935 wpa_supplicant_start() {
936 local phy="$1"
937
938 [ -n "$wpa_supp_init" ] || return 0
939
940 ubus_call wpa_supplicant config_set '{ "phy": "'"$phy"'" }' > /dev/null
941 }
942
943 mac80211_setup_supplicant() {
944 local enable=$1
945 local add_sp=0
946
947 wpa_supplicant_prepare_interface "$ifname" nl80211 || return 1
948
949 if [ "$mode" = "sta" ]; then
950 wpa_supplicant_add_network "$ifname"
951 else
952 wpa_supplicant_add_network "$ifname" "$freq" "$htmode" "$hostapd_noscan"
953 fi
954
955 wpa_supplicant_add_interface "$ifname" "$mode"
956
957 return 0
958 }
959
960 mac80211_setup_vif() {
961 local name="$1"
962 local failed
963
964 json_select config
965 json_get_var ifname _ifname
966 json_get_var macaddr _macaddr
967 json_get_var default_macaddr _default_macaddr
968 json_get_vars mode wds powersave
969
970 set_default powersave 0
971 set_default wds 0
972
973 case "$mode" in
974 mesh)
975 json_get_vars $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING
976 wireless_vif_parse_encryption
977 [ -z "$htmode" ] && htmode="NOHT";
978 if wpa_supplicant -vmesh; then
979 mac80211_setup_supplicant || failed=1
980 else
981 mac80211_setup_mesh
982 fi
983 ;;
984 adhoc)
985 wireless_vif_parse_encryption
986 if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
987 mac80211_setup_supplicant || failed=1
988 else
989 mac80211_setup_adhoc
990 fi
991 ;;
992 sta)
993 mac80211_setup_supplicant || failed=1
994 ;;
995 monitor)
996 mac80211_setup_monitor
997 ;;
998 esac
999
1000 json_select ..
1001 [ -n "$failed" ] || wireless_add_vif "$name" "$ifname"
1002 }
1003
1004 get_freq() {
1005 local phy="$1"
1006 local channel="$2"
1007 local band="$3"
1008
1009 case "$band" in
1010 2g) band="1:";;
1011 5g) band="2:";;
1012 60g) band="3:";;
1013 6g) band="4:";;
1014 esac
1015
1016 iw "$phy" info | awk -v band="$band" -v channel="[$channel]" '
1017
1018 $1 ~ /Band/ {
1019 band_match = band == $2
1020 }
1021
1022 band_match && $3 == "MHz" && $4 == channel {
1023 print $2
1024 exit
1025 }
1026 '
1027 }
1028
1029 chan_is_dfs() {
1030 local phy="$1"
1031 local chan="$2"
1032 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep -q "MHz.*radar detection"
1033 return $!
1034 }
1035
1036 mac80211_set_noscan() {
1037 hostapd_noscan=1
1038 }
1039
1040 drv_mac80211_cleanup() {
1041 :
1042 }
1043
1044 mac80211_reset_config() {
1045 local phy="$1"
1046
1047 hostapd_conf_file="/var/run/hostapd-$phy.conf"
1048 ubus_call hostapd config_set '{ "phy": "'"$phy"'", "config": "", "prev_config": "'"$hostapd_conf_file"'" }' > /dev/null
1049 ubus_call wpa_supplicant config_set '{ "phy": "'"$phy"'", "config": [] }' > /dev/null
1050 wdev_tool "$phy" set_config '{}'
1051 }
1052
1053 drv_mac80211_setup() {
1054 json_select config
1055 json_get_vars \
1056 phy macaddr path \
1057 country chanbw distance \
1058 txpower \
1059 rxantenna txantenna \
1060 frag rts beacon_int:100 htmode \
1061 num_global_macaddr:1 multiple_bssid
1062 json_get_values basic_rate_list basic_rate
1063 json_get_values scan_list scan_list
1064 json_select ..
1065
1066 json_select data && {
1067 json_get_var prev_rxantenna rxantenna
1068 json_get_var prev_txantenna txantenna
1069 json_select ..
1070 }
1071
1072 find_phy || {
1073 echo "Could not find PHY for device '$1'"
1074 wireless_set_retry 0
1075 return 1
1076 }
1077
1078 local wdev
1079 local cwdev
1080 local found
1081
1082 # convert channel to frequency
1083 [ "$auto_channel" -gt 0 ] || freq="$(get_freq "$phy" "$channel" "$band")"
1084
1085 [ -n "$country" ] && {
1086 iw reg get | grep -q "^country $country:" || {
1087 iw reg set "$country"
1088 sleep 1
1089 }
1090 }
1091
1092 hostapd_conf_file="/var/run/hostapd-$phy.conf"
1093
1094 macidx=0
1095 staidx=0
1096
1097 [ -n "$chanbw" ] && {
1098 for file in /sys/kernel/debug/ieee80211/$phy/ath9k*/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
1099 [ -f "$file" ] && echo "$chanbw" > "$file"
1100 done
1101 }
1102
1103 set_default rxantenna 0xffffffff
1104 set_default txantenna 0xffffffff
1105 set_default distance 0
1106
1107 [ "$txantenna" = "all" ] && txantenna=0xffffffff
1108 [ "$rxantenna" = "all" ] && rxantenna=0xffffffff
1109
1110 [ "$rxantenna" = "$prev_rxantenna" -a "$txantenna" = "$prev_txantenna" ] || mac80211_reset_config "$phy"
1111 wireless_set_data phy="$phy" txantenna="$txantenna" rxantenna="$rxantenna"
1112
1113 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
1114 iw phy "$phy" set distance "$distance" >/dev/null 2>&1
1115
1116 if [ -n "$txpower" ]; then
1117 iw phy "$phy" set txpower fixed "${txpower%%.*}00"
1118 else
1119 iw phy "$phy" set txpower auto
1120 fi
1121
1122 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
1123 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
1124
1125 has_ap=
1126 hostapd_ctrl=
1127 ap_ifname=
1128 hostapd_noscan=
1129 wpa_supp_init=
1130 for_each_interface "ap" mac80211_check_ap
1131
1132 [ -f "$hostapd_conf_file" ] && mv "$hostapd_conf_file" "$hostapd_conf_file.prev"
1133
1134 for_each_interface "sta adhoc mesh" mac80211_set_noscan
1135 [ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
1136
1137 local prev
1138 json_set_namespace wdev_uc prev
1139 json_init
1140 json_set_namespace "$prev"
1141
1142 wpa_supplicant_init_config
1143
1144 mac80211_prepare_iw_htmode
1145 active_ifnames=
1146 for_each_interface "ap sta adhoc mesh monitor" mac80211_prepare_vif
1147 for_each_interface "ap sta adhoc mesh monitor" mac80211_setup_vif
1148
1149 [ -x /usr/sbin/wpa_supplicant ] && wpa_supplicant_set_config "$phy"
1150 [ -x /usr/sbin/hostapd ] && hostapd_set_config "$phy"
1151
1152 [ -x /usr/sbin/wpa_supplicant ] && wpa_supplicant_start "$phy"
1153
1154 json_set_namespace wdev_uc prev
1155 wdev_tool "$phy" set_config "$(json_dump)" $active_ifnames
1156 json_set_namespace "$prev"
1157
1158 for_each_interface "ap sta adhoc mesh monitor" mac80211_set_vif_txpower
1159 wireless_set_up
1160 }
1161
1162 _list_phy_interfaces() {
1163 local phy="$1"
1164 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
1165 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
1166 else
1167 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
1168 fi
1169 }
1170
1171 list_phy_interfaces() {
1172 local phy="$1"
1173
1174 for dev in $(_list_phy_interfaces "$phy"); do
1175 readlink "/sys/class/net/${dev}/phy80211" | grep -q "/${phy}\$" || continue
1176 echo "$dev"
1177 done
1178 }
1179
1180 drv_mac80211_teardown() {
1181 json_select data
1182 json_get_vars phy
1183 json_select ..
1184 [ -n "$phy" ] || {
1185 echo "Bug: PHY is undefined for device '$1'"
1186 return 1
1187 }
1188
1189 mac80211_reset_config "$phy"
1190
1191 for wdev in $(list_phy_interfaces "$phy"); do
1192 ip link set dev "$wdev" down
1193 iw dev "$wdev" del
1194 done
1195 }
1196
1197 add_driver mac80211