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