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