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