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