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