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