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