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