mac80211: force upper channel for HT40 when autochannel is enabled
[openwrt/svn-archive/archive.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 drv_mac80211_init_device_config() {
18 hostapd_common_add_device_config
19
20 config_add_string path phy 'macaddr:macaddr'
21 config_add_string hwmode
22 config_add_int beacon_int chanbw frag rts
23 config_add_int rxantenna txantenna antenna_gain txpower distance
24 config_add_boolean noscan
25 config_add_array ht_capab
26 config_add_boolean \
27 rxldpc \
28 short_gi_80 \
29 short_gi_160 \
30 tx_stbc_2by1 \
31 su_beamformer \
32 su_beamformee \
33 mu_beamformer \
34 mu_beamformee \
35 vht_txop_ps \
36 htc_vht \
37 rx_antenna_pattern \
38 tx_antenna_pattern
39 config_add_int vht_max_mpdu vht_max_rx_stbc vht_link_adapt vht160
40 }
41
42 drv_mac80211_init_iface_config() {
43 hostapd_common_add_bss_config
44
45 config_add_string 'macaddr:macaddr' ifname
46
47 config_add_boolean wds powersave
48 config_add_int maxassoc
49 config_add_int max_listen_int
50 config_add_int dtim_interval
51
52 # mesh
53 config_add_string mesh_id
54 config_add_int $MP_CONFIG_INT
55 config_add_boolean $MP_CONFIG_BOOL
56 config_add_string $MP_CONFIG_STRING
57 }
58
59 mac80211_add_capabilities() {
60 local __var="$1"; shift
61 local __mask="$1"; shift
62 local __out= oifs
63
64 oifs="$IFS"
65 IFS=:
66 for capab in "$@"; do
67 set -- $capab
68
69 [ "$(($4))" -gt 0 ] || continue
70 [ "$(($__mask & $2))" -eq "$((${3:-$2}))" ] || continue
71 __out="$__out[$1]"
72 done
73 IFS="$oifs"
74
75 export -n -- "$__var=$__out"
76 }
77
78 mac80211_hostapd_setup_base() {
79 local phy="$1"
80
81 json_select config
82
83 [ "$auto_channel" -gt 0 ] && channel=acs_survey
84
85 [ "$enable_ht" -gt 0 ] && {
86 json_get_vars noscan htmode
87 json_get_values ht_capab_list ht_capab
88
89 ieee80211n=1
90 ht_capab=
91 case "$htmode" in
92 HT20) ;;
93 HT40*|VHT40|VHT80|VHT160)
94 case "$hwmode" in
95 a)
96 case "$(( ($channel / 4) % 2 ))" in
97 1) ht_capab="[HT40+]";;
98 0) ht_capab="[HT40-]";;
99 esac
100 ;;
101 *)
102 case "$htmode" in
103 HT40+) ht_capab="[HT40+]";;
104 HT40-) ht_capab="[HT40-]";;
105 *)
106 if [ "$channel" -lt 7 ]; then
107 ht_capab="[HT40+]"
108 else
109 ht_capab="[HT40-]"
110 fi
111 ;;
112 esac
113 ;;
114 esac
115 [ "$auto_channel" -gt 0 ] && ht_capab="[HT40+]"
116 ;;
117 *) ieee80211n= ;;
118 esac
119
120 [ -n "$ieee80211n" ] && {
121 append base_cfg "ieee80211n=1" "$N"
122
123 json_get_vars \
124 ldpc:1 \
125 greenfield:1 \
126 short_gi_20:1 \
127 short_gi_40:1 \
128 tx_stbc:1 \
129 rx_stbc:3 \
130 dsss_cck_40:1
131
132 ht_cap_mask=0
133 for cap in $(iw phy "$phy" info | grep 'Capabilities:' | cut -d: -f2); do
134 ht_cap_mask="$(($ht_cap_mask | $cap))"
135 done
136
137 cap_rx_stbc=$((($ht_cap_mask >> 8) & 3))
138 [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
139 ht_cap_mask="$(( ($ht_cap_mask & ~(0x300)) | ($cap_rx_stbc << 8) ))"
140
141 mac80211_add_capabilities ht_capab_flags $ht_cap_mask \
142 LDPC:0x1::$ldpc \
143 GF:0x10::$greenfield \
144 SHORT-GI-20:0x20::$short_gi_20 \
145 SHORT-GI-40:0x40::$short_gi_40 \
146 TX-STBC:0x80::$max_tx_stbc \
147 RX-STBC1:0x300:0x100:1 \
148 RX-STBC12:0x300:0x200:1 \
149 RX-STBC123:0x300:0x300:1 \
150 DSSS_CCK-40:0x1000::$dsss_cck_40
151
152 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab$ht_capab_flags" "$N"
153 }
154
155 # 802.11ac
156 enable_ac=0
157 idx="$channel"
158 case "$htmode" in
159 VHT40)
160 case "$(( ($channel / 4) % 2 ))" in
161 1) idx=$(($channel + 2));;
162 0) idx=$(($channel - 2));;
163 esac
164 enable_ac=1
165 append base_cfg "vht_oper_chwidth=0" "$N"
166 append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
167 ;;
168 VHT80)
169 case "$(( ($channel / 4) % 2 ))" in
170 1) idx=$(($channel + 6));;
171 2) idx=$(($channel + 2));;
172 3) idx=$(($channel - 2));;
173 0) idx=$(($channel - 6));;
174 esac
175 enable_ac=1
176 append base_cfg "vht_oper_chwidth=1" "$N"
177 append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
178 ;;
179 VHT160)
180 case "$channel" in
181 36|40|44|48|52|56|60|64) idx=50;;
182 100|104|108|112|116|120|124|128) idx=114;;
183 esac
184 enable_ac=1
185 append base_cfg "vht_oper_chwidth=2" "$N"
186 append base_cfg "vht_oper_centr_freq_seg0_idx=$idx" "$N"
187 ;;
188 esac
189
190 if [ "$enable_ac" != "0" ]; then
191 json_get_vars \
192 rxldpc:1 \
193 short_gi_80:1 \
194 short_gi_160:1 \
195 tx_stbc_2by1:1 \
196 su_beamformer:1 \
197 su_beamformee:1 \
198 mu_beamformer:1 \
199 mu_beamformee:1 \
200 vht_txop_ps:1 \
201 htc_vht:1 \
202 rx_antenna_pattern:1 \
203 tx_antenna_pattern:1 \
204 vht_max_mpdu:11454 \
205 rx_stbc:4 \
206 vht_link_adapt:3 \
207 vht160:2
208
209 append base_cfg "ieee80211ac=1" "$N"
210 vht_cap=0
211 for cap in $(iw phy "$phy" info | awk -F "[()]" '/VHT Capabilities/ { print $2 }'); do
212 vht_cap="$(($vht_cap | $cap))"
213 done
214
215 cap_rx_stbc=$((($vht_cap >> 8) & 7))
216 [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc="$rx_stbc"
217 ht_cap_mask="$(( ($vht_cap & ~(0x700)) | ($cap_rx_stbc << 8) ))"
218
219 mac80211_add_capabilities vht_capab $vht_cap \
220 RXLDPC:0x10::$rxldpc \
221 SHORT-GI-80:0x20::$short_gi_80 \
222 SHORT-GI-160:0x40::$short_gi_160 \
223 TX-STBC-2BY1:0x80::$tx_stbc \
224 SU-BEAMFORMER:0x800::$su_beamformer \
225 SU-BEAMFORMEE:0x1000::$su_beamformee \
226 MU-BEAMFORMER:0x80000::$mu_beamformer \
227 MU-BEAMFORMEE:0x100000::$mu_beamformee \
228 VHT-TXOP-PS:0x200000::$vht_txop_ps \
229 HTC-VHT:0x400000::$htc_vht \
230 RX-ANTENNA-PATTERN:0x10000000::$rx_antenna_pattern \
231 TX-ANTENNA-PATTERN:0x20000000::$tx_antenna_pattern \
232 RX-STBC1:0x700:0x100:1 \
233 RX-STBC12:0x700:0x200:1 \
234 RX-STBC123:0x700:0x300:1 \
235 RX-STBC1234:0x700:0x400:1 \
236
237 # supported Channel widths
238 vht160_hw=0
239 [ "$(($vht_cap & 12))" -eq 4 -a 1 -le "$vht160" ] && \
240 vht160_hw=1
241 [ "$(($vht_cap & 12))" -eq 8 -a 2 -le "$vht160" ] && \
242 vht160_hw=2
243 [ "$vht160_hw" = 1 ] && vht_capab="$vht_capab[VHT160]"
244 [ "$vht160_hw" = 2 ] && vht_capab="$vht_capab[VHT160-80PLUS80]"
245
246 # maximum MPDU length
247 vht_max_mpdu_hw=3895
248 [ "$(($vht_cap & 3))" -ge 1 -a 7991 -le "$vht_max_mpdu" ] && \
249 vht_max_mpdu_hw=7991
250 [ "$(($vht_cap & 3))" -ge 2 -a 11454 -le "$vht_max_mpdu" ] && \
251 vht_max_mpdu_hw=11454
252 [ "$vht_max_mpdu_hw" != 3895 ] && \
253 vht_capab="$vht_capab[MAX-MPDU-$vht_max_mpdu_hw]"
254
255 # whether or not the STA supports link adaptation using VHT variant
256 vht_link_adapt_hw=0
257 [ "$(($vht_cap & 201326592))" -ge 134217728 -a 2 -le "$vht_link_adapt" ] && \
258 vht_link_adapt_hw=2
259 [ "$(($vht_cap & 201326592))" -ge 201326592 -a 3 -le "$vht_link_adapt" ] && \
260 vht_link_adapt_hw=3
261 [ "$vht_link_adapt_hw" != 0 ] && \
262 vht_capab="$vht_capab[VHT-LINK-ADAPT-$vht_link_adapt_hw]"
263
264 [ -n "$vht_capab" ] && append base_cfg "vht_capab=$vht_capab" "$N"
265 fi
266 }
267
268 hostapd_prepare_device_config "$hostapd_conf_file" nl80211
269 cat >> "$hostapd_conf_file" <<EOF
270 ${channel:+channel=$channel}
271 ${noscan:+noscan=$noscan}
272 $base_cfg
273
274 EOF
275 json_select ..
276 }
277
278 mac80211_hostapd_setup_bss() {
279 local phy="$1"
280 local ifname="$2"
281 local macaddr="$3"
282 local type="$4"
283
284 hostapd_cfg=
285 append hostapd_cfg "$type=$ifname" "$N"
286
287 hostapd_set_bss_options hostapd_cfg "$vif" || return 1
288 json_get_vars wds dtim_period max_listen_int
289
290 set_default wds 0
291
292 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
293 [ "$staidx" -gt 0 ] && append hostapd_cfg "start_disabled=1" "$N"
294
295 cat >> /var/run/hostapd-$phy.conf <<EOF
296 $hostapd_cfg
297 bssid=$macaddr
298 ${dtim_period:+dtim_period=$dtim_period}
299 ${max_listen_int:+max_listen_interval=$max_listen_int}
300 EOF
301 }
302
303 mac80211_generate_mac() {
304 local phy="$1"
305 local id="${macidx:-0}"
306
307 local ref="$(cat /sys/class/ieee80211/${phy}/macaddress)"
308 local mask="$(cat /sys/class/ieee80211/${phy}/address_mask)"
309
310 [ "$mask" = "00:00:00:00:00:00" ] && mask="ff:ff:ff:ff:ff:ff";
311 local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
312
313 local mask1=$1
314 local mask6=$6
315
316 local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
317
318 macidx=$(($id + 1))
319 [ "$((0x$mask1))" -gt 0 ] && {
320 b1="0x$1"
321 [ "$id" -gt 0 ] && \
322 b1=$(($b1 ^ ((($id - 1) << 2) | 0x2)))
323 printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
324 return
325 }
326
327 [ "$((0x$mask6))" -lt 255 ] && {
328 printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
329 return
330 }
331
332 off2=$(( (0x$6 + $id) / 0x100 ))
333 printf "%s:%s:%s:%s:%02x:%02x" \
334 $1 $2 $3 $4 \
335 $(( (0x$5 + $off2) % 0x100 )) \
336 $(( (0x$6 + $id) % 0x100 ))
337 }
338
339 find_phy() {
340 [ -n "$phy" -a -d /sys/class/ieee80211/$phy ] && return 0
341 [ -n "$path" -a -d "/sys/devices/$path/ieee80211" ] && {
342 phy="$(ls /sys/devices/$path/ieee80211 | grep -m 1 phy)"
343 [ -n "$phy" ] && return 0
344 }
345 [ -n "$macaddr" ] && {
346 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
347 grep -i -q "$macaddr" "/sys/class/ieee80211/${phy}/macaddress" && return 0
348 done
349 }
350 return 1
351 }
352
353 mac80211_check_ap() {
354 has_ap=1
355 }
356
357 mac80211_prepare_vif() {
358 json_select config
359
360 json_get_vars ifname mode ssid wds powersave macaddr
361
362 [ -n "$ifname" ] || ifname="wlan${phy#phy}${if_idx:+-$if_idx}"
363 if_idx=$((${if_idx:-0} + 1))
364
365 set_default wds 0
366 set_default powersave 0
367
368 json_select ..
369
370 [ -n "$macaddr" ] || {
371 macaddr="$(mac80211_generate_mac $phy)"
372 macidx="$(($macidx + 1))"
373 }
374
375 json_add_object data
376 json_add_string ifname "$ifname"
377 json_close_object
378 json_select config
379
380 # It is far easier to delete and create the desired interface
381 case "$mode" in
382 adhoc)
383 iw phy "$phy" interface add "$ifname" type adhoc
384 ;;
385 ap)
386 # Hostapd will handle recreating the interface and
387 # subsequent virtual APs belonging to the same PHY
388 if [ -n "$hostapd_ctrl" ]; then
389 type=bss
390 else
391 type=interface
392 fi
393
394 mac80211_hostapd_setup_bss "$phy" "$ifname" "$macaddr" "$type" || return
395
396 [ -n "$hostapd_ctrl" ] || {
397 iw phy "$phy" interface add "$ifname" type managed
398 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
399 }
400 ;;
401 mesh)
402 json_get_vars key mesh_id
403 if [ -n "$key" ]; then
404 iw phy "$phy" interface add "$ifname" type mp
405 else
406 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
407 fi
408 ;;
409 monitor)
410 iw phy "$phy" interface add "$ifname" type monitor
411 ;;
412 sta)
413 local wdsflag=
414 staidx="$(($staidx + 1))"
415 [ "$wds" -gt 0 ] && wdsflag="4addr on"
416 iw phy "$phy" interface add "$ifname" type managed $wdsflag
417 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
418 iw "$ifname" set power_save "$powersave"
419 ;;
420 esac
421
422 case "$mode" in
423 monitor|mesh)
424 [ "$auto_channel" -gt 0 ] || iw dev "$ifname" set channel "$channel" $htmode
425 ;;
426 esac
427
428 if [ "$mode" != "ap" ]; then
429 # ALL ap functionality will be passed to hostapd
430 # All interfaces must have unique mac addresses
431 # which can either be explicitly set in the device
432 # section, or automatically generated
433 ifconfig "$ifname" hw ether "$macaddr"
434 fi
435
436 json_select ..
437 }
438
439 mac80211_setup_supplicant() {
440 wpa_supplicant_prepare_interface "$ifname" nl80211 || return 1
441 wpa_supplicant_add_network "$ifname"
442 wpa_supplicant_run "$ifname" ${hostapd_ctrl:+-H $hostapd_ctrl}
443 }
444
445 mac80211_setup_adhoc() {
446 json_get_vars bssid ssid key mcast_rate
447
448 keyspec=
449 [ "$auth_type" == "wep" ] && {
450 set_default key 1
451 case "$key" in
452 [1234])
453 local idx
454 for idx in 1 2 3 4; do
455 json_get_var ikey "key$idx"
456
457 [ -n "$ikey" ] && {
458 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
459 [ $idx -eq $key ] && ikey="d:$ikey"
460 append keyspec "$ikey"
461 }
462 done
463 ;;
464 *)
465 append keyspec "d:0:$(prepare_key_wep "$key")"
466 ;;
467 esac
468 }
469
470 brstr=
471 for br in $basic_rate_list; do
472 hostapd_add_rate brstr "$br"
473 done
474
475 mcval=
476 [ -n "$mcast_rate" ] && hostapd_add_rate mcval "$mcast_rate"
477
478 iw dev "$ifname" ibss join "$ssid" $freq $htmode fixed-freq $bssid \
479 ${beacon_int:+beacon-interval $beacon_int} \
480 ${brstr:+basic-rates $brstr} \
481 ${mcval:+mcast-rate $mcval} \
482 ${keyspec:+keys $keyspec}
483 }
484
485 mac80211_setup_vif() {
486 local name="$1"
487 local failed
488
489 json_select data
490 json_get_vars ifname
491 json_select ..
492
493 json_select config
494 json_get_vars mode
495 json_get_var vif_txpower txpower
496
497 ifconfig "$ifname" up || {
498 wireless_setup_vif_failed IFUP_ERROR
499 json_select ..
500 return
501 }
502
503 set_default vif_txpower "$txpower"
504 [ -z "$vif_txpower" ] || iw dev "$ifname" set txpower fixed "${vif_txpower%%.*}00"
505
506 case "$mode" in
507 mesh)
508 for var in $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING; do
509 json_get_var mp_val "$var"
510 [ -n "$mp_val" ] && iw dev "$ifname" set mesh_param "$var" "$mp_val"
511 done
512
513 # authsae
514 json_get_vars key
515 if [ -n "$key" ]; then
516 if [ -e "/lib/wifi/authsae.sh" ]; then
517 . /lib/wifi/authsae.sh
518 authsae_start_interface || failed=1
519 else
520 wireless_setup_vif_failed AUTHSAE_NOT_INSTALLED
521 json_select ..
522 return
523 fi
524 fi
525 ;;
526 adhoc)
527 wireless_vif_parse_encryption
528 if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
529 mac80211_setup_supplicant || failed=1
530 else
531 mac80211_setup_adhoc
532 fi
533 ;;
534 sta)
535 mac80211_setup_supplicant || failed=1
536 ;;
537 esac
538
539 json_select ..
540 [ -n "$failed" ] || wireless_add_vif "$name" "$ifname"
541 }
542
543 get_freq() {
544 local phy="$1"
545 local chan="$2"
546 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
547 }
548
549 mac80211_interface_cleanup() {
550 local phy="$1"
551
552 for wdev in $(list_phy_interfaces "$phy"); do
553 ifconfig "$wdev" down 2>/dev/null
554 iw dev "$wdev" del
555 done
556 }
557
558 drv_mac80211_cleanup() {
559 hostapd_common_cleanup
560 }
561
562 drv_mac80211_setup() {
563 json_select config
564 json_get_vars \
565 phy macaddr path \
566 country chanbw distance \
567 txpower antenna_gain \
568 rxantenna txantenna \
569 frag rts beacon_int
570 json_get_values basic_rate_list basic_rate
571 json_select ..
572
573 find_phy || {
574 echo "Could not find PHY for device '$1'"
575 wireless_set_retry 0
576 return 1
577 }
578
579 wireless_set_data phy="$phy"
580 mac80211_interface_cleanup "$phy"
581
582 # convert channel to frequency
583 [ "$auto_channel" -gt 0 ] || freq="$(get_freq "$phy" "$channel")"
584
585 [ -n "$country" ] && {
586 iw reg get | grep -q "^country $country:" || {
587 iw reg set "$country"
588 sleep 1
589 }
590 }
591
592 hostapd_conf_file="/var/run/hostapd-$phy.conf"
593
594 no_ap=1
595 macidx=0
596 staidx=0
597
598 [ -n "$chanbw" ] && {
599 for file in /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
600 [ -f "$file" ] && echo "$chanbw" > "$file"
601 done
602 }
603
604 set_default rxantenna all
605 set_default txantenna all
606 set_default distance 0
607 set_default antenna_gain 0
608
609 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
610 iw phy "$phy" set antenna_gain $antenna_gain
611 iw phy "$phy" set distance "$distance"
612
613 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
614 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
615
616 has_ap=
617 hostapd_ctrl=
618 for_each_interface "ap" mac80211_check_ap
619
620 rm -f "$hostapd_conf_file"
621 [ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
622
623 for_each_interface "sta adhoc mesh monitor" mac80211_prepare_vif
624 for_each_interface "ap" mac80211_prepare_vif
625
626 [ -n "$hostapd_ctrl" ] && {
627 /usr/sbin/hostapd -P /var/run/wifi-$phy.pid -B "$hostapd_conf_file"
628 ret="$?"
629 wireless_add_process "$(cat /var/run/wifi-$phy.pid)" "/usr/sbin/hostapd" 1
630 [ "$ret" != 0 ] && {
631 wireless_setup_failed HOSTAPD_START_FAILED
632 return
633 }
634 }
635
636 for_each_interface "ap sta adhoc mesh monitor" mac80211_setup_vif
637
638 wireless_set_up
639 }
640
641 list_phy_interfaces() {
642 local phy="$1"
643 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
644 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
645 else
646 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
647 fi
648 }
649
650 drv_mac80211_teardown() {
651 wireless_process_kill_all
652
653 json_select data
654 json_get_vars phy
655 json_select ..
656
657 mac80211_interface_cleanup "$phy"
658 }
659
660 add_driver mac80211