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