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