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