2 .
/lib
/netifd
/netifd-wireless.sh
3 .
/lib
/netifd
/hostapd.sh
5 init_wireless_driver
"$@"
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"
17 drv_mac80211_init_device_config
() {
18 hostapd_common_add_device_config
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_array channels
40 config_add_int vht_max_a_mpdu_len_exp vht_max_mpdu vht_link_adapt vht160 rx_stbc tx_stbc
50 drv_mac80211_init_iface_config
() {
51 hostapd_common_add_bss_config
53 config_add_string
'macaddr:macaddr' ifname
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 config_add_int start_disabled
62 config_add_string mesh_id
63 config_add_int
$MP_CONFIG_INT
64 config_add_boolean
$MP_CONFIG_BOOL
65 config_add_string
$MP_CONFIG_STRING
68 mac80211_add_capabilities
() {
69 local __var
="$1"; shift
70 local __mask
="$1"; shift
78 [ "$(($4))" -gt 0 ] ||
continue
79 [ "$(($__mask & $2))" -eq "$((${3:-$2}))" ] ||
continue
84 export -n -- "$__var=$__out"
87 mac80211_hostapd_setup_base
() {
92 [ "$auto_channel" -gt 0 ] && channel
=acs_survey
93 [ "$auto_channel" -gt 0 ] && json_get_values channel_list channels
95 json_get_vars noscan ht_coex
96 json_get_values ht_capab_list ht_capab
102 HT40
*|VHT40|VHT80|VHT160
)
105 case "$(( ($channel / 4) % 2 ))" in
106 1) ht_capab
="[HT40+]";;
107 0) ht_capab
="[HT40-]";;
112 HT40
+) ht_capab
="[HT40+]";;
113 HT40-
) ht_capab
="[HT40-]";;
115 if [ "$channel" -lt 7 ]; then
124 [ "$auto_channel" -gt 0 ] && ht_capab
="[HT40+]"
129 [ -n "$ieee80211n" ] && {
130 append base_cfg
"ieee80211n=1" "$N"
132 set_default ht_coex
0
133 append base_cfg
"ht_coex=$ht_coex" "$N"
146 for cap
in $
(iw phy
"$phy" info |
grep 'Capabilities:' | cut
-d: -f2); do
147 ht_cap_mask
="$(($ht_cap_mask | $cap))"
150 cap_rx_stbc
=$
((($ht_cap_mask >> 8) & 3))
151 [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc
="$rx_stbc"
152 ht_cap_mask
="$(( ($ht_cap_mask & ~(0x300)) | ($cap_rx_stbc << 8) ))"
154 mac80211_add_capabilities ht_capab_flags
$ht_cap_mask \
156 GF
:0x10::$greenfield \
157 SHORT-GI-20
:0x20::$short_gi_20 \
158 SHORT-GI-40
:0x40::$short_gi_40 \
159 TX-STBC
:0x80::$tx_stbc \
160 RX-STBC1
:0x300:0x100:1 \
161 RX-STBC12
:0x300:0x200:1 \
162 RX-STBC123
:0x300:0x300:1 \
163 MAX-AMSDU-7935
:0x800::$max_amsdu \
164 DSSS_CCK-40
:0x1000::$dsss_cck_40
166 ht_capab
="$ht_capab$ht_capab_flags"
167 [ -n "$ht_capab" ] && append base_cfg
"ht_capab=$ht_capab" "$N"
176 case "$(( ($channel / 4) % 2 ))" in
177 1) idx
=$
(($channel + 2));;
178 0) idx
=$
(($channel - 2));;
181 append base_cfg
"vht_oper_chwidth=0" "$N"
182 append base_cfg
"vht_oper_centr_freq_seg0_idx=$idx" "$N"
185 case "$(( ($channel / 4) % 4 ))" in
186 1) idx
=$
(($channel + 6));;
187 2) idx
=$
(($channel + 2));;
188 3) idx
=$
(($channel - 2));;
189 0) idx
=$
(($channel - 6));;
192 append base_cfg
"vht_oper_chwidth=1" "$N"
193 append base_cfg
"vht_oper_centr_freq_seg0_idx=$idx" "$N"
197 36|
40|
44|
48|
52|
56|
60|
64) idx
=50;;
198 100|
104|
108|
112|
116|
120|
124|
128) idx
=114;;
201 append base_cfg
"vht_oper_chwidth=2" "$N"
202 append base_cfg
"vht_oper_centr_freq_seg0_idx=$idx" "$N"
206 if [ "$enable_ac" != "0" ]; then
218 rx_antenna_pattern
:1 \
219 tx_antenna_pattern
:1 \
220 vht_max_a_mpdu_len_exp
:7 \
227 append base_cfg
"ieee80211ac=1" "$N"
229 for cap
in $
(iw phy
"$phy" info |
awk -F "[()]" '/VHT Capabilities/ { print $2 }'); do
230 vht_cap
="$(($vht_cap | $cap))"
233 cap_rx_stbc
=$
((($vht_cap >> 8) & 7))
234 [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc
="$rx_stbc"
235 ht_cap_mask
="$(( ($vht_cap & ~(0x700)) | ($cap_rx_stbc << 8) ))"
237 mac80211_add_capabilities vht_capab
$vht_cap \
238 RXLDPC
:0x10::$rxldpc \
239 SHORT-GI-80
:0x20::$short_gi_80 \
240 SHORT-GI-160
:0x40::$short_gi_160 \
241 TX-STBC-2BY1
:0x80::$tx_stbc \
242 SU-BEAMFORMER
:0x800::$su_beamformer \
243 SU-BEAMFORMEE
:0x1000::$su_beamformee \
244 MU-BEAMFORMER
:0x80000::$mu_beamformer \
245 MU-BEAMFORMEE
:0x100000::$mu_beamformee \
246 VHT-TXOP-PS
:0x200000::$vht_txop_ps \
247 HTC-VHT
:0x400000::$htc_vht \
248 RX-ANTENNA-PATTERN
:0x10000000::$rx_antenna_pattern \
249 TX-ANTENNA-PATTERN
:0x20000000::$tx_antenna_pattern \
250 RX-STBC1
:0x700:0x100:1 \
251 RX-STBC12
:0x700:0x200:1 \
252 RX-STBC123
:0x700:0x300:1 \
253 RX-STBC1234
:0x700:0x400:1 \
255 # supported Channel widths
257 [ "$(($vht_cap & 12))" -eq 4 -a 1 -le "$vht160" ] && \
259 [ "$(($vht_cap & 12))" -eq 8 -a 2 -le "$vht160" ] && \
261 [ "$vht160_hw" = 1 ] && vht_capab
="$vht_capab[VHT160]"
262 [ "$vht160_hw" = 2 ] && vht_capab
="$vht_capab[VHT160-80PLUS80]"
264 # maximum MPDU length
266 [ "$(($vht_cap & 3))" -ge 1 -a 7991 -le "$vht_max_mpdu" ] && \
268 [ "$(($vht_cap & 3))" -ge 2 -a 11454 -le "$vht_max_mpdu" ] && \
269 vht_max_mpdu_hw
=11454
270 [ "$vht_max_mpdu_hw" != 3895 ] && \
271 vht_capab
="$vht_capab[MAX-MPDU-$vht_max_mpdu_hw]"
273 # maximum A-MPDU length exponent
274 vht_max_a_mpdu_len_exp_hw
=0
275 [ "$(($vht_cap & 58720256))" -ge 8388608 -a 1 -le "$vht_max_a_mpdu_len_exp" ] && \
276 vht_max_a_mpdu_len_exp_hw
=1
277 [ "$(($vht_cap & 58720256))" -ge 16777216 -a 2 -le "$vht_max_a_mpdu_len_exp" ] && \
278 vht_max_a_mpdu_len_exp_hw
=2
279 [ "$(($vht_cap & 58720256))" -ge 25165824 -a 3 -le "$vht_max_a_mpdu_len_exp" ] && \
280 vht_max_a_mpdu_len_exp_hw
=3
281 [ "$(($vht_cap & 58720256))" -ge 33554432 -a 4 -le "$vht_max_a_mpdu_len_exp" ] && \
282 vht_max_a_mpdu_len_exp_hw
=4
283 [ "$(($vht_cap & 58720256))" -ge 41943040 -a 5 -le "$vht_max_a_mpdu_len_exp" ] && \
284 vht_max_a_mpdu_len_exp_hw
=5
285 [ "$(($vht_cap & 58720256))" -ge 50331648 -a 6 -le "$vht_max_a_mpdu_len_exp" ] && \
286 vht_max_a_mpdu_len_exp_hw
=6
287 [ "$(($vht_cap & 58720256))" -ge 58720256 -a 7 -le "$vht_max_a_mpdu_len_exp" ] && \
288 vht_max_a_mpdu_len_exp_hw
=7
289 vht_capab
="$vht_capab[MAX-A-MPDU-LEN-EXP$vht_max_a_mpdu_len_exp_hw]"
291 # whether or not the STA supports link adaptation using VHT variant
293 [ "$(($vht_cap & 201326592))" -ge 134217728 -a 2 -le "$vht_link_adapt" ] && \
295 [ "$(($vht_cap & 201326592))" -ge 201326592 -a 3 -le "$vht_link_adapt" ] && \
297 [ "$vht_link_adapt_hw" != 0 ] && \
298 vht_capab
="$vht_capab[VHT-LINK-ADAPT-$vht_link_adapt_hw]"
300 [ -n "$vht_capab" ] && append base_cfg
"vht_capab=$vht_capab" "$N"
303 hostapd_prepare_device_config
"$hostapd_conf_file" nl80211
304 cat >> "$hostapd_conf_file" <<EOF
305 ${channel:+channel=$channel}
306 ${channel_list:+chanlist=$channel_list}
307 ${noscan:+noscan=$noscan}
314 mac80211_hostapd_setup_bss
() {
321 append hostapd_cfg
"$type=$ifname" "$N"
323 hostapd_set_bss_options hostapd_cfg
"$vif" ||
return 1
324 json_get_vars wds dtim_period max_listen_int start_disabled
327 set_default start_disabled
0
329 [ "$wds" -gt 0 ] && append hostapd_cfg
"wds_sta=1" "$N"
330 [ "$staidx" -gt 0 -o "$start_disabled" -eq 1 ] && append hostapd_cfg
"start_disabled=1" "$N"
332 cat >> /var
/run
/hostapd-
$phy.conf
<<EOF
335 ${dtim_period:+dtim_period=$dtim_period}
336 ${max_listen_int:+max_listen_interval=$max_listen_int}
340 mac80211_get_addr
() {
342 local idx
="$(($2 + 1))"
344 head -n $
(($macidx + 1)) /sys
/class
/ieee80211
/${phy}/addresses |
tail -n1
347 mac80211_generate_mac
() {
349 local id
="${macidx:-0}"
351 local ref
="$(cat /sys/class/ieee80211/${phy}/macaddress)"
352 local mask
="$(cat /sys/class/ieee80211/${phy}/address_mask)"
354 [ "$mask" = "00:00:00:00:00:00" ] && {
355 mask
="ff:ff:ff:ff:ff:ff";
357 [ "$(wc -l < /sys/class/ieee80211/${phy}/addresses)" -gt 1 ] && {
358 addr
="$(mac80211_get_addr "$phy" "$id")"
366 local oIFS
="$IFS"; IFS
=":"; set -- $mask; IFS
="$oIFS"
371 local oIFS
="$IFS"; IFS
=":"; set -- $ref; IFS
="$oIFS"
374 [ "$((0x$mask1))" -gt 0 ] && {
377 b1
=$
(($b1 ^
((($id - 1) << 2) | 0x2)))
378 printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
382 [ "$((0x$mask6))" -lt 255 ] && {
383 printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
387 off2=$(( (0x$6 + $id) / 0x100 ))
388 printf "%s:%s:%s:%s:%02x:%02x" \
390 $(( (0x$5 + $off2) % 0x100 )) \
391 $(( (0x$6 + $id) % 0x100 ))
395 [ -n "$phy" -a -d /sys/class/ieee80211/$phy ] && return 0
397 for phy in /sys/devices/$path/ieee80211/phy*; do
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
412 mac80211_check_ap
() {
416 mac80211_prepare_vif
() {
419 json_get_vars ifname mode ssid wds powersave macaddr
421 [ -n "$ifname" ] || ifname
="wlan${phy#phy}${if_idx:+-$if_idx}"
422 if_idx
=$
((${if_idx:-0} + 1))
425 set_default powersave
0
429 [ -n "$macaddr" ] ||
{
430 macaddr
="$(mac80211_generate_mac $phy)"
431 macidx
="$(($macidx + 1))"
435 json_add_string ifname
"$ifname"
439 # It is far easier to delete and create the desired interface
442 iw phy
"$phy" interface add
"$ifname" type adhoc
445 # Hostapd will handle recreating the interface and
446 # subsequent virtual APs belonging to the same PHY
447 if [ -n "$hostapd_ctrl" ]; then
453 mac80211_hostapd_setup_bss
"$phy" "$ifname" "$macaddr" "$type" ||
return
455 [ -n "$hostapd_ctrl" ] ||
{
456 iw phy
"$phy" interface add
"$ifname" type __ap
457 hostapd_ctrl
="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
461 iw phy
"$phy" interface add
"$ifname" type mp
464 iw phy
"$phy" interface add
"$ifname" type monitor
468 staidx
="$(($staidx + 1))"
469 [ "$wds" -gt 0 ] && wdsflag
="4addr on"
470 iw phy
"$phy" interface add
"$ifname" type managed
$wdsflag
471 [ "$powersave" -gt 0 ] && powersave
="on" || powersave
="off"
472 iw
"$ifname" set power_save
"$powersave"
478 [ "$auto_channel" -gt 0 ] || iw dev
"$ifname" set channel
"$channel" $htmode
482 if [ "$mode" != "ap" ]; then
483 # ALL ap functionality will be passed to hostapd
484 # All interfaces must have unique mac addresses
485 # which can either be explicitly set in the device
486 # section, or automatically generated
487 ip link
set dev
"$ifname" address
"$macaddr"
493 mac80211_setup_supplicant
() {
494 wpa_supplicant_prepare_interface
"$ifname" nl80211 ||
return 1
495 wpa_supplicant_add_network
"$ifname"
496 wpa_supplicant_run
"$ifname" ${hostapd_ctrl:+-H $hostapd_ctrl}
499 mac80211_setup_adhoc_htmode
() {
501 VHT20|HT20
) ibss_htmode
=HT20
;;
505 case "$(( ($channel / 4) % 2 ))" in
506 1) ibss_htmode
="HT40+" ;;
507 0) ibss_htmode
="HT40-";;
512 HT40
+) ibss_htmode
="HT40+";;
513 HT40-
) ibss_htmode
="HT40-";;
515 if [ "$channel" -lt 7 ]; then
524 [ "$auto_channel" -gt 0 ] && ibss_htmode
="HT40+"
537 mac80211_setup_adhoc
() {
538 json_get_vars bssid ssid key mcast_rate
541 [ "$auth_type" = "wep" ] && {
546 for idx
in 1 2 3 4; do
547 json_get_var ikey
"key$idx"
550 ikey
="$(($idx - 1)):$(prepare_key_wep "$ikey")"
551 [ $idx -eq $key ] && ikey
="d:$ikey"
552 append keyspec
"$ikey"
557 append keyspec
"d:0:$(prepare_key_wep "$key")"
563 for br
in $basic_rate_list; do
564 wpa_supplicant_add_rate brstr
"$br"
568 [ -n "$mcast_rate" ] && wpa_supplicant_add_rate mcval
"$mcast_rate"
570 iw dev
"$ifname" ibss
join "$ssid" $freq $ibss_htmode fixed-freq
$bssid \
571 ${beacon_int:+beacon-interval $beacon_int} \
572 ${brstr:+basic-rates $brstr} \
573 ${mcval:+mcast-rate $mcval} \
574 ${keyspec:+keys $keyspec}
577 mac80211_setup_vif
() {
587 json_get_var vif_txpower txpower
589 ip link
set dev
"$ifname" up ||
{
590 wireless_setup_vif_failed IFUP_ERROR
595 set_default vif_txpower
"$txpower"
596 [ -z "$vif_txpower" ] || iw dev
"$ifname" set txpower fixed
"${vif_txpower%%.*}00"
600 # authsae or wpa_supplicant
602 if [ -n "$key" ]; then
603 if [ -e "/lib/wifi/authsae.sh" ]; then
604 .
/lib
/wifi
/authsae.sh
605 authsae_start_interface || failed
=1
607 wireless_vif_parse_encryption
608 mac80211_setup_supplicant || failed
=1
611 json_get_vars mesh_id mcast_rate
614 [ -n "$mcast_rate" ] && wpa_supplicant_add_rate mcval
"$mcast_rate"
616 iw dev
"$ifname" mesh
join "$mesh_id" ${mcval:+mcast-rate $mcval}
619 for var
in $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING; do
620 json_get_var mp_val
"$var"
621 [ -n "$mp_val" ] && iw dev
"$ifname" set mesh_param
"$var" "$mp_val"
625 wireless_vif_parse_encryption
626 mac80211_setup_adhoc_htmode
627 if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
628 mac80211_setup_supplicant || failed
=1
634 mac80211_setup_supplicant || failed
=1
639 [ -n "$failed" ] || wireless_add_vif
"$name" "$ifname"
645 iw
"$phy" info |
grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
648 mac80211_interface_cleanup() {
651 for wdev in $(list_phy_interfaces "$phy"); do
652 ip link set dev "$wdev" down 2>/dev/null
657 drv_mac80211_cleanup() {
658 hostapd_common_cleanup
661 drv_mac80211_setup() {
665 country chanbw distance \
666 txpower antenna_gain \
667 rxantenna txantenna \
668 frag rts beacon_int htmode
669 json_get_values basic_rate_list basic_rate
673 echo "Could not
find PHY
for device
'$1'"
678 wireless_set_data phy="$phy"
679 mac80211_interface_cleanup "$phy"
681 # convert channel to frequency
682 [ "$auto_channel" -gt 0 ] || freq="$
(get_freq
"$phy" "$channel")"
684 [ -n "$country" ] && {
685 iw reg get | grep -q "^country
$country:" || {
686 iw reg set "$country"
691 hostapd_conf_file="/var
/run
/hostapd-
$phy.conf
"
697 [ -n "$chanbw" ] && {
698 for file in /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode; do
699 [ -f "$file" ] && echo "$chanbw" > "$file"
703 set_default rxantenna all
704 set_default txantenna all
705 set_default distance 0
706 set_default antenna_gain 0
708 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
709 iw phy "$phy" set antenna_gain $antenna_gain
710 iw phy "$phy" set distance "$distance"
712 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
713 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
717 for_each_interface "ap
" mac80211_check_ap
719 rm -f "$hostapd_conf_file"
720 [ -n "$has_ap" ] && mac80211_hostapd_setup_base "$phy"
722 for_each_interface "sta adhoc mesh monitor
" mac80211_prepare_vif
723 for_each_interface "ap
" mac80211_prepare_vif
725 [ -n "$hostapd_ctrl" ] && {
726 /usr/sbin/hostapd -P /var/run/wifi-$phy.pid -B "$hostapd_conf_file"
728 wireless_add_process "$
(cat /var
/run
/wifi-
$phy.pid
)" "/usr
/sbin
/hostapd
" 1
730 wireless_setup_failed HOSTAPD_START_FAILED
735 for_each_interface "ap sta adhoc mesh monitor
" mac80211_setup_vif
740 list_phy_interfaces() {
742 if [ -d "/sys
/class
/ieee80211
/${phy}/device
/net
" ]; then
743 ls "/sys
/class
/ieee80211
/${phy}/device
/net
" 2>/dev/null;
745 ls "/sys
/class
/ieee80211
/${phy}/device
" 2>/dev/null | grep net: | sed -e 's,net:,,g'
749 drv_mac80211_teardown() {
750 wireless_process_kill_all
756 mac80211_interface_cleanup "$phy"