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