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