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