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