2 .
/lib
/netifd
/netifd-wireless.sh
3 .
/lib
/netifd
/hostapd.sh
4 .
/lib
/functions
/system.sh
6 init_wireless_driver
"$@"
8 MP_CONFIG_INT
="mesh_retry_timeout mesh_confirm_timeout mesh_holding_timeout mesh_max_peer_links
9 mesh_max_retries mesh_ttl mesh_element_ttl mesh_hwmp_max_preq_retries
10 mesh_path_refresh_time mesh_min_discovery_timeout mesh_hwmp_active_path_timeout
11 mesh_hwmp_preq_min_interval mesh_hwmp_net_diameter_traversal_time mesh_hwmp_rootmode
12 mesh_hwmp_rann_interval mesh_gate_announcements mesh_sync_offset_max_neighor
13 mesh_rssi_threshold mesh_hwmp_active_path_to_root_timeout mesh_hwmp_root_interval
14 mesh_hwmp_confirmation_interval mesh_awake_window mesh_plink_timeout"
15 MP_CONFIG_BOOL
="mesh_auto_open_plinks mesh_fwding"
16 MP_CONFIG_STRING
="mesh_power_mode"
25 drv_mac80211_init_device_config
() {
26 hostapd_common_add_device_config
28 config_add_string path phy
'macaddr:macaddr'
29 config_add_string tx_burst
30 config_add_string distance
31 config_add_int beacon_int chanbw frag rts
32 config_add_int rxantenna txantenna antenna_gain txpower min_tx_power
33 config_add_boolean noscan ht_coex acs_exclude_dfs background_radar
34 config_add_array ht_capab
35 config_add_array channels
36 config_add_array scan_list
55 he_bss_color_enabled \
60 vht_max_a_mpdu_len_exp \
67 he_spr_non_srg_obss_pd_max_offset
77 drv_mac80211_init_iface_config
() {
78 hostapd_common_add_bss_config
80 config_add_string
'macaddr:macaddr' ifname
82 config_add_boolean wds powersave
enable
83 config_add_string wds_bridge
84 config_add_int maxassoc
85 config_add_int max_listen_int
86 config_add_int dtim_period
87 config_add_int start_disabled
90 config_add_string mesh_id
91 config_add_int
$MP_CONFIG_INT
92 config_add_boolean
$MP_CONFIG_BOOL
93 config_add_string
$MP_CONFIG_STRING
96 mac80211_add_capabilities
() {
97 local __var
="$1"; shift
98 local __mask
="$1"; shift
103 for capab
in "$@"; do
106 [ "$(($4))" -gt 0 ] ||
continue
107 [ "$(($__mask & $2))" -eq "$((${3:-$2}))" ] ||
continue
112 export -n -- "$__var=$__out"
115 mac80211_add_he_capabilities
() {
120 for capab
in "$@"; do
122 [ "$(($4))" -gt 0 ] ||
continue
123 [ "$(((0x$2) & $3))" -gt 0 ] ||
{
127 append base_cfg
"$1=1" "$N"
132 mac80211_hostapd_setup_base
() {
137 [ "$auto_channel" -gt 0 ] && channel
=acs_survey
139 [ "$auto_channel" -gt 0 ] && json_get_vars acs_exclude_dfs
140 [ -n "$acs_exclude_dfs" ] && [ "$acs_exclude_dfs" -gt 0 ] &&
141 append base_cfg
"acs_exclude_dfs=1" "$N"
143 json_get_vars noscan ht_coex min_tx_power
:0 tx_burst
144 json_get_values ht_capab_list ht_capab
145 json_get_values channel_list channels
147 [ "$auto_channel" = 0 ] && [ -z "$channel_list" ] && \
148 channel_list
="$channel"
150 [ "$min_tx_power" -gt 0 ] && append base_cfg
"min_tx_power=$min_tx_power"
154 [ "$noscan" -gt 0 ] && hostapd_noscan
=1
155 [ "$tx_burst" = 0 ] && tx_burst
=
158 [ "$band" = "6g" ] && chan_ofs
=1
164 HT40
*|VHT40|VHT80|VHT160|HE40|HE80|HE160
)
167 case "$(( (($channel / 4) + $chan_ofs) % 2 ))" in
168 1) ht_capab
="[HT40+]";;
169 0) ht_capab
="[HT40-]";;
174 HT40
+) ht_capab
="[HT40+]";;
175 HT40-
) ht_capab
="[HT40-]";;
177 if [ "$channel" -lt 7 ]; then
186 [ "$auto_channel" -gt 0 ] && ht_capab
="[HT40+]"
191 [ -n "$ieee80211n" ] && {
192 append base_cfg
"ieee80211n=1" "$N"
194 set_default ht_coex
0
195 append base_cfg
"ht_coex=$ht_coex" "$N"
208 for cap
in $
(iw phy
"$phy" info |
grep 'Capabilities:' | cut
-d: -f2); do
209 ht_cap_mask
="$(($ht_cap_mask | $cap))"
212 cap_rx_stbc
=$
((($ht_cap_mask >> 8) & 3))
213 [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc
="$rx_stbc"
214 ht_cap_mask
="$(( ($ht_cap_mask & ~(0x300)) | ($cap_rx_stbc << 8) ))"
216 mac80211_add_capabilities ht_capab_flags
$ht_cap_mask \
218 GF
:0x10::$greenfield \
219 SHORT-GI-20
:0x20::$short_gi_20 \
220 SHORT-GI-40
:0x40::$short_gi_40 \
221 TX-STBC
:0x80::$tx_stbc \
222 RX-STBC1
:0x300:0x100:1 \
223 RX-STBC12
:0x300:0x200:1 \
224 RX-STBC123
:0x300:0x300:1 \
225 MAX-AMSDU-7935
:0x800::$max_amsdu \
226 DSSS_CCK-40
:0x1000::$dsss_cck_40
228 ht_capab
="$ht_capab$ht_capab_flags"
229 [ -n "$ht_capab" ] && append base_cfg
"ht_capab=$ht_capab" "$N"
239 VHT20|HE20
) enable_ac
=1;;
241 case "$(( (($channel / 4) + $chan_ofs) % 2 ))" in
242 1) idx
=$
(($channel + 2));;
243 0) idx
=$
(($channel - 2));;
249 case "$(( (($channel / 4) + $chan_ofs) % 4 ))" in
250 1) idx
=$
(($channel + 6));;
251 2) idx
=$
(($channel + 2));;
252 3) idx
=$
(($channel - 2));;
253 0) idx
=$
(($channel - 6));;
260 if [ "$band" = "6g" ]; then
262 1|
5|
9|
13|
17|
21|
25|
29) idx
=15;;
263 33|
37|
41|
45|
49|
53|
57|
61) idx
=47;;
264 65|
69|
73|
77|
81|
85|
89|
93) idx
=79;;
265 97|
101|
105|
109|
113|
117|
121|
125) idx
=111;;
266 129|
133|
137|
141|
145|
149|
153|
157) idx
=143;;
267 161|
165|
169|
173|
177|
181|
185|
189) idx
=175;;
268 193|
197|
201|
205|
209|
213|
217|
221) idx
=207;;
272 36|
40|
44|
48|
52|
56|
60|
64) idx
=50;;
273 100|
104|
108|
112|
116|
120|
124|
128) idx
=114;;
281 [ "$band" = "5g" ] && {
282 json_get_vars background_radar
:0
284 [ "$background_radar" -eq 1 ] && append base_cfg
"enable_background_radar=1" "$N"
286 [ "$band" = "6g" ] && {
290 HE
*) op_class
=$
((132 + $vht_oper_chwidth))
292 [ -n "$op_class" ] && append base_cfg
"op_class=$op_class" "$N"
294 [ "$hwmode" = "a" ] || enable_ac
=0
296 if [ "$enable_ac" != "0" ]; then
308 beamformee_antennas
:4 \
309 beamformer_antennas
:4 \
310 rx_antenna_pattern
:1 \
311 tx_antenna_pattern
:1 \
312 vht_max_a_mpdu_len_exp
:7 \
318 set_default tx_burst
2.0
319 append base_cfg
"ieee80211ac=1" "$N"
321 for cap
in $
(iw phy
"$phy" info |
awk -F "[()]" '/VHT Capabilities/ { print $2 }'); do
322 vht_cap
="$(($vht_cap | $cap))"
325 append base_cfg
"vht_oper_chwidth=$vht_oper_chwidth" "$N"
326 append base_cfg
"vht_oper_centr_freq_seg0_idx=$vht_center_seg0" "$N"
328 cap_rx_stbc
=$
((($vht_cap >> 8) & 7))
329 [ "$rx_stbc" -lt "$cap_rx_stbc" ] && cap_rx_stbc
="$rx_stbc"
330 vht_cap
="$(( ($vht_cap & ~(0x700)) | ($cap_rx_stbc << 8) ))"
332 mac80211_add_capabilities vht_capab
$vht_cap \
333 RXLDPC
:0x10::$rxldpc \
334 SHORT-GI-80
:0x20::$short_gi_80 \
335 SHORT-GI-160
:0x40::$short_gi_160 \
336 TX-STBC-2BY1
:0x80::$tx_stbc_2by1 \
337 SU-BEAMFORMER
:0x800::$su_beamformer \
338 SU-BEAMFORMEE
:0x1000::$su_beamformee \
339 MU-BEAMFORMER
:0x80000::$mu_beamformer \
340 MU-BEAMFORMEE
:0x100000::$mu_beamformee \
341 VHT-TXOP-PS
:0x200000::$vht_txop_ps \
342 HTC-VHT
:0x400000::$htc_vht \
343 RX-ANTENNA-PATTERN
:0x10000000::$rx_antenna_pattern \
344 TX-ANTENNA-PATTERN
:0x20000000::$tx_antenna_pattern \
345 RX-STBC-1
:0x700:0x100:1 \
346 RX-STBC-12
:0x700:0x200:1 \
347 RX-STBC-123
:0x700:0x300:1 \
348 RX-STBC-1234
:0x700:0x400:1 \
350 [ "$(($vht_cap & 0x800))" -gt 0 -a "$su_beamformer" -gt 0 ] && {
351 cap_ant
="$(( ( ($vht_cap >> 16) & 3 ) + 1 ))"
352 [ "$cap_ant" -gt "$beamformer_antennas" ] && cap_ant
="$beamformer_antennas"
353 [ "$cap_ant" -gt 1 ] && vht_capab
="$vht_capab[SOUNDING-DIMENSION-$cap_ant]"
356 [ "$(($vht_cap & 0x1000))" -gt 0 -a "$su_beamformee" -gt 0 ] && {
357 cap_ant
="$(( ( ($vht_cap >> 13) & 3 ) + 1 ))"
358 [ "$cap_ant" -gt "$beamformee_antennas" ] && cap_ant
="$beamformee_antennas"
359 [ "$cap_ant" -gt 1 ] && vht_capab
="$vht_capab[BF-ANTENNA-$cap_ant]"
362 # supported Channel widths
364 [ "$(($vht_cap & 12))" -eq 4 -a 1 -le "$vht160" ] && \
366 [ "$(($vht_cap & 12))" -eq 8 -a 2 -le "$vht160" ] && \
368 [ "$vht160_hw" = 1 ] && vht_capab
="$vht_capab[VHT160]"
369 [ "$vht160_hw" = 2 ] && vht_capab
="$vht_capab[VHT160-80PLUS80]"
371 # maximum MPDU length
373 [ "$(($vht_cap & 3))" -ge 1 -a 7991 -le "$vht_max_mpdu" ] && \
375 [ "$(($vht_cap & 3))" -ge 2 -a 11454 -le "$vht_max_mpdu" ] && \
376 vht_max_mpdu_hw
=11454
377 [ "$vht_max_mpdu_hw" != 3895 ] && \
378 vht_capab
="$vht_capab[MAX-MPDU-$vht_max_mpdu_hw]"
380 # maximum A-MPDU length exponent
381 vht_max_a_mpdu_len_exp_hw
=0
382 [ "$(($vht_cap & 58720256))" -ge 8388608 -a 1 -le "$vht_max_a_mpdu_len_exp" ] && \
383 vht_max_a_mpdu_len_exp_hw
=1
384 [ "$(($vht_cap & 58720256))" -ge 16777216 -a 2 -le "$vht_max_a_mpdu_len_exp" ] && \
385 vht_max_a_mpdu_len_exp_hw
=2
386 [ "$(($vht_cap & 58720256))" -ge 25165824 -a 3 -le "$vht_max_a_mpdu_len_exp" ] && \
387 vht_max_a_mpdu_len_exp_hw
=3
388 [ "$(($vht_cap & 58720256))" -ge 33554432 -a 4 -le "$vht_max_a_mpdu_len_exp" ] && \
389 vht_max_a_mpdu_len_exp_hw
=4
390 [ "$(($vht_cap & 58720256))" -ge 41943040 -a 5 -le "$vht_max_a_mpdu_len_exp" ] && \
391 vht_max_a_mpdu_len_exp_hw
=5
392 [ "$(($vht_cap & 58720256))" -ge 50331648 -a 6 -le "$vht_max_a_mpdu_len_exp" ] && \
393 vht_max_a_mpdu_len_exp_hw
=6
394 [ "$(($vht_cap & 58720256))" -ge 58720256 -a 7 -le "$vht_max_a_mpdu_len_exp" ] && \
395 vht_max_a_mpdu_len_exp_hw
=7
396 vht_capab
="$vht_capab[MAX-A-MPDU-LEN-EXP$vht_max_a_mpdu_len_exp_hw]"
398 # whether or not the STA supports link adaptation using VHT variant
400 [ "$(($vht_cap & 201326592))" -ge 134217728 -a 2 -le "$vht_link_adapt" ] && \
402 [ "$(($vht_cap & 201326592))" -ge 201326592 -a 3 -le "$vht_link_adapt" ] && \
404 [ "$vht_link_adapt_hw" != 0 ] && \
405 vht_capab
="$vht_capab[VHT-LINK-ADAPT-$vht_link_adapt_hw]"
407 [ -n "$vht_capab" ] && append base_cfg
"vht_capab=$vht_capab" "$N"
416 if [ "$enable_ax" != "0" ]; then
422 he_spr_sr_control
:3 \
423 he_spr_psr_enabled
:0 \
424 he_spr_non_srg_obss_pd_max_offset
:0 \
426 he_bss_color_enabled
:1
428 he_phy_cap
=$
(iw phy
"$phy" info |
sed -n '/HE Iftypes: AP/,$p' |
awk -F "[()]" '/HE PHY Capabilities/ { print $2 }' |
head -1)
429 he_phy_cap
=${he_phy_cap:2}
430 he_mac_cap
=$
(iw phy
"$phy" info |
sed -n '/HE Iftypes: AP/,$p' |
awk -F "[()]" '/HE MAC Capabilities/ { print $2 }' |
head -1)
431 he_mac_cap
=${he_mac_cap:2}
433 append base_cfg
"ieee80211ax=1" "$N"
434 [ "$hwmode" = "a" ] && {
435 append base_cfg
"he_oper_chwidth=$vht_oper_chwidth" "$N"
436 append base_cfg
"he_oper_centr_freq_seg0_idx=$vht_center_seg0" "$N"
439 mac80211_add_he_capabilities \
440 he_su_beamformer
:${he_phy_cap:6:2}:0x80:$he_su_beamformer \
441 he_su_beamformee
:${he_phy_cap:8:2}:0x1:$he_su_beamformee \
442 he_mu_beamformer
:${he_phy_cap:8:2}:0x2:$he_mu_beamformer \
443 he_spr_psr_enabled
:${he_phy_cap:14:2}:0x1:$he_spr_psr_enabled \
444 he_twt_required
:${he_mac_cap:0:2}:0x6:$he_twt_required
446 if [ "$he_bss_color_enabled" -gt 0 ]; then
447 append base_cfg
"he_bss_color=$he_bss_color" "$N"
448 [ "$he_spr_non_srg_obss_pd_max_offset" -gt 0 ] && { \
449 append base_cfg
"he_spr_non_srg_obss_pd_max_offset=$he_spr_non_srg_obss_pd_max_offset" "$N"
450 he_spr_sr_control
=$
((he_spr_sr_control |
(1 << 2)))
452 [ "$he_spr_psr_enabled" -gt 0 ] || he_spr_sr_control=$((he_spr_sr_control | (1 << 0)))
453 append base_cfg "he_spr_sr_control=$he_spr_sr_control" "$N"
455 append base_cfg "he_bss_color_disabled=1" "$N"
459 append base_cfg "he_default_pe_duration=4" "$N"
460 append base_cfg "he_rts_threshold=1023" "$N"
461 append base_cfg "he_mu_edca_qos_info_param_count=0" "$N"
462 append base_cfg "he_mu_edca_qos_info_q_ack=0" "$N"
463 append base_cfg "he_mu_edca_qos_info_queue_request=0" "$N"
464 append base_cfg "he_mu_edca_qos_info_txop_request=0" "$N"
465 append base_cfg "he_mu_edca_ac_be_aifsn=8" "$N"
466 append base_cfg "he_mu_edca_ac_be_aci=0" "$N"
467 append base_cfg "he_mu_edca_ac_be_ecwmin=9" "$N"
468 append base_cfg "he_mu_edca_ac_be_ecwmax=10" "$N"
469 append base_cfg "he_mu_edca_ac_be_timer=255" "$N"
470 append base_cfg "he_mu_edca_ac_bk_aifsn=15" "$N"
471 append base_cfg "he_mu_edca_ac_bk_aci=1" "$N"
472 append base_cfg "he_mu_edca_ac_bk_ecwmin=9" "$N"
473 append base_cfg "he_mu_edca_ac_bk_ecwmax=10" "$N"
474 append base_cfg "he_mu_edca_ac_bk_timer=255" "$N"
475 append base_cfg "he_mu_edca_ac_vi_ecwmin=5" "$N"
476 append base_cfg "he_mu_edca_ac_vi_ecwmax=7" "$N"
477 append base_cfg "he_mu_edca_ac_vi_aifsn=5" "$N"
478 append base_cfg "he_mu_edca_ac_vi_aci=2" "$N"
479 append base_cfg "he_mu_edca_ac_vi_timer
=255" "$N"
480 append base_cfg "he_mu_edca_ac_vo_aifsn
=5" "$N"
481 append base_cfg "he_mu_edca_ac_vo_aci
=3" "$N"
482 append base_cfg "he_mu_edca_ac_vo_ecwmin
=5" "$N"
483 append base_cfg "he_mu_edca_ac_vo_ecwmax
=7" "$N"
484 append base_cfg "he_mu_edca_ac_vo_timer
=255" "$N"
487 hostapd_prepare_device_config "$hostapd_conf_file" nl80211
488 cat >> "$hostapd_conf_file" <<EOF
489 ${channel:+channel=$channel}
490 ${channel_list:+chanlist=$channel_list}
491 ${hostapd_noscan:+noscan=1}
492 ${tx_burst:+tx_queue_data2_burst=$tx_burst}
497 radio_md5sum=$(md5sum $hostapd_conf_file | cut -d" " -f1)
498 echo "radio_config_id
=${radio_md5sum}" >> $hostapd_conf_file
501 mac80211_hostapd_setup_bss() {
508 append hostapd_cfg "$type=$ifname" "$N"
510 hostapd_set_bss_options hostapd_cfg "$phy" "$vif" || return 1
511 json_get_vars wds wds_bridge dtim_period max_listen_int start_disabled
514 set_default start_disabled 0
516 [ "$wds" -gt 0 ] && {
517 append hostapd_cfg "wds_sta
=1" "$N"
518 [ -n "$wds_bridge" ] && append hostapd_cfg "wds_bridge
=$wds_bridge" "$N"
520 [ "$staidx" -gt 0 -o "$start_disabled" -eq 1 ] && append hostapd_cfg "start_disabled
=1" "$N"
522 cat >> /var/run/hostapd-$phy.conf <<EOF
525 ${dtim_period:+dtim_period=$dtim_period}
526 ${max_listen_int:+max_listen_interval=$max_listen_int}
530 mac80211_get_addr() {
532 local idx="$
(($2 + 1))"
534 head -n $idx /sys/class/ieee80211/${phy}/addresses | tail -n1
537 mac80211_generate_mac() {
539 local id="${macidx:-0}"
541 local ref="$
(cat /sys
/class
/ieee80211
/${phy}/macaddress
)"
542 local mask="$
(cat /sys
/class
/ieee80211
/${phy}/address_mask
)"
544 [ "$mask" = "00:00:00:00:00:00" ] && {
545 mask="ff
:ff
:ff
:ff
:ff
:ff
";
547 [ "$
(wc -l < /sys
/class
/ieee80211
/${phy}/addresses
)" -gt $id ] && {
548 addr="$
(mac80211_get_addr
"$phy" "$id")"
556 local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
561 local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
564 [ "$
((0x
$mask1))" -gt 0 ] && {
567 b1=$(($b1 ^ ((($id - !($b1 & 2)) << 2)) | 0x2))
568 printf "%02x
:%s
:%s
:%s
:%s
:%s
" $b1 $2 $3 $4 $5 $6
572 [ "$
((0x
$mask6))" -lt 255 ] && {
573 printf "%s
:%s
:%s
:%s
:%s
:%02x
" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
577 off2=$(( (0x$6 + $id) / 0x100 ))
578 printf "%s
:%s
:%s
:%s
:%02x
:%02x
" \
580 $(( (0x$5 + $off2) % 0x100 )) \
581 $(( (0x$6 + $id) % 0x100 ))
584 get_board_phy_name() (
586 local fallback_phy=""
597 [ "${ref_path%+*}" = "$path" ] && fallback_phy=$key
598 [ "$ref_path" = "$path" ] || return 0
604 json_load_file /etc/board.json
605 json_for_each_item __check_phy wlan "$path"
606 [ -n "$fallback_phy" ] && echo "${fallback_phy}.
${path##*+}"
609 rename_board_phy_by_path() {
612 local new_phy="$
(get_board_phy_name
"$path")"
613 [ -z "$new_phy" -o "$new_phy" = "$phy" ] && return
615 iw "$phy" set name "$new_phy" && phy="$new_phy"
618 rename_board_phy_by_name() (
620 local suffix="${phy##*.}"
621 [ "$suffix" = "$phy" ] && suffix=
623 json_load_file /etc/board.json
625 json_select "${phy%.*}" || return 0
628 prev_phy="$
(iwinfo nl80211 phyname
"path=$path${suffix:++$suffix}")"
629 [ -n "$prev_phy" ] || return 0
631 [ "$prev_phy" = "$phy" ] && return 0
633 iw "$prev_phy" set name "$phy"
638 rename_board_phy_by_name "$phy"
639 [ -d /sys/class/ieee80211/$phy ] && return 0
642 phy="$
(iwinfo nl80211 phyname
"path=$path")"
644 rename_board_phy_by_path "$path"
648 [ -n "$macaddr" ] && {
649 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
650 grep -i -q "$macaddr" "/sys
/class
/ieee80211
/${phy}/macaddress
" && {
651 path="$
(iwinfo nl80211 path
"$phy")"
652 rename_board_phy_by_path "$path"
660 mac80211_check_ap() {
664 mac80211_iw_interface_add() {
672 iw phy "$phy" interface add "$ifname" type "$type" $wdsflag >/dev/null 2>&1
676 # Device might have just been deleted, give the kernel some time to finish cleaning it up
679 iw phy "$phy" interface add "$ifname" type "$type" $wdsflag >/dev/null 2>&1
684 # Keep matching pre-existing interface
685 [ -d "/sys
/class
/ieee80211
/${phy}/device
/net
/${ifname}" ] && \
686 case "$
(iw dev
$ifname info |
grep "^\ttype" | cut
-d' ' -f2- 2>/dev
/null
)" in
688 [ "$type" = "__ap
" ] && rc=0
691 [ "$type" = "adhoc
" ] && rc=0
694 [ "$type" = "managed
" ] && rc=0
697 [ "$type" = "mp
" ] && rc=0
700 [ "$type" = "monitor
" ] && rc=0
706 iw dev "$ifname" del >/dev/null 2>&1
710 iw phy "$phy" interface add "$ifname" type "$type" $wdsflag >/dev/null 2>&1
716 # Device might not support virtual interfaces, so the interface never got deleted in the first place.
717 # Check if the interface already exists, and avoid failing in this case.
718 [ -d "/sys
/class
/ieee80211
/${phy}/device
/net
/${ifname}" ] && rc=0
722 # Device doesn't support virtual interfaces and may have existing interface other than ifname.
723 oldifname="$
(basename "/sys/class/ieee80211/${phy}/device/net"/* 2>/dev
/null
)"
724 [ "$oldifname" ] && ip link set "$oldifname" name "$ifname" 1>/dev/null 2>&1
728 [ "$rc" != 0 ] && echo "Failed to create interface
$ifname"
732 mac80211_set_ifname() {
735 eval "ifname
=\"$phy-$prefix\
${idx_$prefix:-0}\"; idx_
$prefix=\$
((\
${idx_$prefix:-0 } + 1))"
738 mac80211_prepare_vif() {
741 json_get_vars ifname mode ssid wds powersave macaddr enable wpa_psk_file vlan_file
743 [ -n "$ifname" ] || {
747 ap|sta|mesh) prefix=$mode;;
749 monitor) prefix=mon;;
752 mac80211_set_ifname "$phy" "$prefix"
756 set_default powersave 0
760 if [ -z "$macaddr" ]; then
761 macaddr="$
(mac80211_generate_mac
$phy)"
762 macidx="$
(($macidx + 1))"
763 elif [ "$macaddr" = 'random' ]; then
764 macaddr="$
(macaddr_random
)"
768 json_add_string ifname "$ifname"
771 [ "$mode" == "ap
" ] && {
772 [ -z "$wpa_psk_file" ] && hostapd_set_psk "$ifname"
773 [ -z "$vlan_file" ] && hostapd_set_vlan "$ifname"
778 # It is far easier to delete and create the desired interface
781 mac80211_iw_interface_add "$phy" "$ifname" adhoc || return
784 # Hostapd will handle recreating the interface and
785 # subsequent virtual APs belonging to the same PHY
786 if [ -n "$hostapd_ctrl" ]; then
792 mac80211_hostapd_setup_bss "$phy" "$ifname" "$macaddr" "$type" || return
794 NEWAPLIST="${NEWAPLIST}$ifname "
795 [ -n "$hostapd_ctrl" ] || {
796 ap_ifname="${ifname}"
797 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd/$ifname}"
801 mac80211_iw_interface_add "$phy" "$ifname" mp || return
804 mac80211_iw_interface_add "$phy" "$ifname" monitor || return
808 [ "$enable" = 0 ] || staidx="$
(($staidx + 1))"
809 [ "$wds" -gt 0 ] && wdsflag="4addr on
"
810 mac80211_iw_interface_add "$phy" "$ifname" managed "$wdsflag" || return
811 if [ "$wds" -gt 0 ]; then
812 iw "$ifname" set 4addr on
814 iw "$ifname" set 4addr off
816 [ "$powersave" -gt 0 ] && powersave="on
" || powersave="off
"
817 iw "$ifname" set power_save "$powersave"
823 [ "$auto_channel" -gt 0 ] || iw dev "$ifname" set channel "$channel" $iw_htmode
827 if [ "$mode" != "ap
" ]; then
828 # ALL ap functionality will be passed to hostapd
829 # All interfaces must have unique mac addresses
830 # which can either be explicitly set in the device
831 # section, or automatically generated
832 ip link set dev "$ifname" address "$macaddr"
838 mac80211_setup_supplicant() {
841 local spobj="$
(ubus
-S list |
grep wpa_supplicant.
${ifname})"
843 [ "$enable" = 0 ] && {
844 ubus call wpa_supplicant.${phy} config_remove "{\"iface
\":\"$ifname\"}"
845 ip link set dev "$ifname" down
850 wpa_supplicant_prepare_interface "$ifname" nl80211 || {
854 if [ "$mode" = "sta
" ]; then
855 wpa_supplicant_add_network "$ifname"
857 wpa_supplicant_add_network "$ifname" "$freq" "$htmode" "$noscan"
860 NEWSPLIST="${NEWSPLIST}$ifname "
862 if [ "${NEWAPLIST%% *}" != "${OLDAPLIST%% *}" ]; then
863 [ "$spobj" ] && ubus call wpa_supplicant config_remove "{\"iface
\":\"$ifname\"}"
866 [ -z "$spobj" ] && add_sp=1
868 NEW_MD5_SP=$(test -e "${_config}" && md5sum ${_config})
869 OLD_MD5_SP=$(uci -q -P /var/state get wireless._${phy}.md5_${ifname})
870 if [ "$add_sp" = "1" ]; then
871 wpa_supplicant_run "$ifname" "$hostapd_ctrl"
873 [ "${NEW_MD5_SP}" == "${OLD_MD5_SP}" ] || ubus call $spobj reload
875 uci -q -P /var/state set wireless._${phy}.md5_${ifname}="${NEW_MD5_SP}"
879 mac80211_setup_supplicant_noctl
() {
881 local spobj
="$(ubus -S list | grep wpa_supplicant.${ifname})"
882 wpa_supplicant_prepare_interface
"$ifname" nl80211 ||
{
887 wpa_supplicant_add_network
"$ifname" "$freq" "$htmode" "$noscan"
889 NEWSPLIST
="${NEWSPLIST}$ifname "
890 [ "$enable" = 0 ] && {
891 ubus call wpa_supplicant config_remove
"{\"iface\":\"$ifname\"}"
892 ip link
set dev
"$ifname" down
895 if [ -z "$spobj" ]; then
896 wpa_supplicant_run
"$ifname"
898 ubus call
$spobj reload
902 mac80211_prepare_iw_htmode
() {
904 VHT20|HT20|HE20
) iw_htmode
=HT20
;;
905 HT40
*|VHT40|VHT160|HE40
)
909 HT40
+) iw_htmode
="HT40+";;
910 HT40-
) iw_htmode
="HT40-";;
912 if [ "$channel" -lt 7 ]; then
921 case "$(( ($channel / 4) % 2 ))" in
922 1) iw_htmode
="HT40+" ;;
923 0) iw_htmode
="HT40-";;
927 [ "$auto_channel" -gt 0 ] && iw_htmode
="HT40+"
939 mac80211_setup_adhoc
() {
941 json_get_vars bssid ssid key mcast_rate
943 NEWUMLIST
="${NEWUMLIST}$ifname "
945 [ "$enable" = 0 ] && {
946 ip link
set dev
"$ifname" down
951 [ "$auth_type" = "wep" ] && {
956 for idx
in 1 2 3 4; do
957 json_get_var ikey
"key$idx"
960 ikey
="$(($idx - 1)):$(prepare_key_wep "$ikey")"
961 [ $idx -eq $key ] && ikey
="d:$ikey"
962 append keyspec
"$ikey"
967 append keyspec
"d:0:$(prepare_key_wep "$key")"
973 for br
in $basic_rate_list; do
974 wpa_supplicant_add_rate brstr
"$br"
978 [ -n "$mcast_rate" ] && wpa_supplicant_add_rate mcval
"$mcast_rate"
980 iw dev
"$ifname" set type ibss
981 iw dev
"$ifname" ibss
join "$ssid" $freq $iw_htmode fixed-freq
$bssid \
982 beacon-interval
$beacon_int \
983 ${brstr:+basic-rates $brstr} \
984 ${mcval:+mcast-rate $mcval} \
985 ${keyspec:+keys $keyspec}
988 mac80211_setup_mesh
() {
990 json_get_vars ssid mesh_id mcast_rate
992 NEWUMLIST
="${NEWUMLIST}$ifname "
994 [ "$enable" = 0 ] && {
995 ip link
set dev
"$ifname" down
1000 [ -n "$mcast_rate" ] && wpa_supplicant_add_rate mcval
"$mcast_rate"
1001 [ -n "$mesh_id" ] && ssid
="$mesh_id"
1003 iw dev
"$ifname" mesh
join "$ssid" freq
$freq $iw_htmode \
1004 ${mcval:+mcast-rate $mcval} \
1005 beacon-interval
$beacon_int
1008 mac80211_setup_vif
() {
1014 json_get_vars ifname
1019 json_get_var vif_txpower
1020 json_get_var vif_enable
enable 1
1022 [ "$vif_enable" = 1 ] || action
=down
1023 if [ "$mode" != "ap" ] ||
[ "$ifname" = "$ap_ifname" ]; then
1024 ip link
set dev
"$ifname" "$action" ||
{
1025 wireless_setup_vif_failed IFUP_ERROR
1029 [ -z "$vif_txpower" ] || iw dev
"$ifname" set txpower fixed
"${vif_txpower%%.*}00"
1034 wireless_vif_parse_encryption
1035 [ -z "$htmode" ] && htmode
="NOHT";
1036 if wpa_supplicant
-vmesh ||
[ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ] || chan_is_dfs
"$phy" "$channel"; then
1037 mac80211_setup_supplicant
$vif_enable || failed
=1
1039 mac80211_setup_mesh
$vif_enable
1041 for var
in $MP_CONFIG_INT $MP_CONFIG_BOOL $MP_CONFIG_STRING; do
1042 json_get_var mp_val
"$var"
1043 [ -n "$mp_val" ] && iw dev
"$ifname" set mesh_param
"$var" "$mp_val"
1047 wireless_vif_parse_encryption
1048 if [ "$wpa" -gt 0 -o "$auto_channel" -gt 0 ]; then
1049 mac80211_setup_supplicant_noctl
$vif_enable || failed
=1
1051 mac80211_setup_adhoc
$vif_enable
1055 mac80211_setup_supplicant
$vif_enable || failed
=1
1060 [ -n "$failed" ] || wireless_add_vif
"$name" "$ifname"
1075 iw
"$phy" info |
awk -v band
="$band" -v channel
="[$channel]" '
1078 band_match = band == $2
1081 band_match && $3 == "MHz" && $4 == channel {
1092 iw
"$phy" info |
grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" |
grep -q "MHz.*radar detection"
1096 mac80211_vap_cleanup
() {
1100 for wdev
in $vaps; do
1101 [ "$service" != "none" ] && ubus call
${service} config_remove
"{\"iface\":\"$wdev\"}"
1102 ip link
set dev
"$wdev" down
2>/dev
/null
1107 mac80211_interface_cleanup
() {
1109 local primary_ap
=$
(uci
-q -P /var
/state get wireless._
${phy}.aplist
)
1110 primary_ap
=${primary_ap%% *}
1112 mac80211_vap_cleanup hostapd
"${primary_ap}"
1113 mac80211_vap_cleanup wpa_supplicant
"$(uci -q -P /var/state get wireless._${phy}.splist)"
1114 mac80211_vap_cleanup none
"$(uci -q -P /var/state get wireless._${phy}.umlist)"
1117 mac80211_set_noscan
() {
1121 drv_mac80211_cleanup
() {
1122 hostapd_common_cleanup
1125 drv_mac80211_setup
() {
1129 country chanbw distance \
1130 txpower antenna_gain \
1131 rxantenna txantenna \
1132 frag rts beacon_int
:100 htmode
1133 json_get_values basic_rate_list basic_rate
1134 json_get_values scan_list scan_list
1138 echo "Could not find PHY for device '$1'"
1139 wireless_set_retry
0
1143 wireless_set_data phy
="$phy"
1144 [ -z "$(uci -q -P /var/state show wireless._${phy})" ] && uci
-q -P /var
/state
set wireless._
${phy}=phy
1146 OLDAPLIST
=$
(uci
-q -P /var
/state get wireless._
${phy}.aplist
)
1147 OLDSPLIST
=$
(uci
-q -P /var
/state get wireless._
${phy}.splist
)
1148 OLDUMLIST
=$
(uci
-q -P /var
/state get wireless._
${phy}.umlist
)
1154 for wdev
in $
(list_phy_interfaces
"$phy"); do
1156 for cwdev
in $OLDAPLIST $OLDSPLIST $OLDUMLIST; do
1157 if [ "$wdev" = "$cwdev" ]; then
1162 if [ "$found" = "0" ]; then
1163 ip link
set dev
"$wdev" down
1168 # convert channel to frequency
1169 [ "$auto_channel" -gt 0 ] || freq
="$(get_freq "$phy" "$channel" "$band")"
1171 [ -n "$country" ] && {
1172 iw reg get |
grep -q "^country $country:" ||
{
1173 iw reg
set "$country"
1178 hostapd_conf_file
="/var/run/hostapd-$phy.conf"
1184 [ -n "$chanbw" ] && {
1185 for file in /sys
/kernel
/debug
/ieee80211
/$phy/ath9k
*/chanbw
/sys
/kernel
/debug
/ieee80211
/$phy/ath5k
/bwmode
; do
1186 [ -f "$file" ] && echo "$chanbw" > "$file"
1190 set_default rxantenna
0xffffffff
1191 set_default txantenna
0xffffffff
1192 set_default distance
0
1193 set_default antenna_gain
0
1195 [ "$txantenna" = "all" ] && txantenna
=0xffffffff
1196 [ "$rxantenna" = "all" ] && rxantenna
=0xffffffff
1198 iw phy
"$phy" set antenna
$txantenna $rxantenna >/dev
/null
2>&1
1199 iw phy
"$phy" set antenna_gain
$antenna_gain >/dev
/null
2>&1
1200 iw phy
"$phy" set distance
"$distance" >/dev
/null
2>&1
1202 if [ -n "$txpower" ]; then
1203 iw phy
"$phy" set txpower fixed
"${txpower%%.*}00"
1205 iw phy
"$phy" set txpower auto
1208 [ -n "$frag" ] && iw phy
"$phy" set frag
"${frag%%.*}"
1209 [ -n "$rts" ] && iw phy
"$phy" set rts
"${rts%%.*}"
1215 for_each_interface
"ap" mac80211_check_ap
1217 rm -f "$hostapd_conf_file"
1219 for_each_interface
"sta adhoc mesh" mac80211_set_noscan
1220 [ -n "$has_ap" ] && mac80211_hostapd_setup_base
"$phy"
1222 mac80211_prepare_iw_htmode
1223 for_each_interface
"sta adhoc mesh monitor" mac80211_prepare_vif
1225 for_each_interface
"ap" mac80211_prepare_vif
1226 NEW_MD5
=$
(test -e "${hostapd_conf_file}" && md5sum ${hostapd_conf_file})
1227 OLD_MD5
=$
(uci
-q -P /var
/state get wireless._
${phy}.md5
)
1228 if [ "${NEWAPLIST}" != "${OLDAPLIST}" ]; then
1229 mac80211_vap_cleanup hostapd
"${OLDAPLIST}"
1231 [ -n "${NEWAPLIST}" ] && mac80211_iw_interface_add
"$phy" "${NEWAPLIST%% *}" __ap
1233 local primary_ap
=${NEWAPLIST%% *}
1234 [ -n "$hostapd_ctrl" ] && {
1236 if [ -n "$(ubus list | grep hostapd.$primary_ap)" ]; then
1238 [ "${NEW_MD5}" = "${OLD_MD5}" ] ||
{
1239 ubus call hostapd.
$primary_ap reload
1241 if [ "$no_reload" != "0" ]; then
1242 mac80211_vap_cleanup hostapd
"${OLDAPLIST}"
1243 mac80211_vap_cleanup wpa_supplicant
"$(uci -q -P /var/state get wireless._${phy}.splist)"
1244 mac80211_vap_cleanup none
"$(uci -q -P /var/state get wireless._${phy}.umlist)"
1246 mac80211_iw_interface_add
"$phy" "${NEWAPLIST%% *}" __ap
1247 for_each_interface
"sta adhoc mesh monitor" mac80211_prepare_vif
1251 if [ "$no_reload" != "0" ]; then
1253 ubus wait_for hostapd
1254 local hostapd_res
="$(ubus call hostapd config_add "{\"iface
\":\"$primary_ap\", \"config
\":\"${hostapd_conf_file}\"}")"
1256 [ "$ret" != 0 -o -z "$hostapd_res" ] && {
1257 wireless_setup_failed HOSTAPD_START_FAILED
1260 wireless_add_process
"$(jsonfilter -s "$hostapd_res" -l 1 -e @.pid)" "/usr/sbin/hostapd" 1 1
1263 uci
-q -P /var
/state
set wireless._
${phy}.aplist
="${NEWAPLIST}"
1264 uci
-q -P /var
/state
set wireless._
${phy}.md5
="${NEW_MD5}"
1266 [ "${add_ap}" = 1 ] && sleep 1
1267 for_each_interface
"ap" mac80211_setup_vif
1272 for_each_interface
"sta adhoc mesh monitor" mac80211_setup_vif
1274 uci
-q -P /var
/state
set wireless._
${phy}.splist
="${NEWSPLIST}"
1275 uci
-q -P /var
/state
set wireless._
${phy}.umlist
="${NEWUMLIST}"
1279 for oldvap
in $OLDSPLIST; do
1281 for newvap
in $NEWSPLIST; do
1282 [ "$oldvap" = "$newvap" ] && foundvap
=1
1284 [ "$foundvap" = "0" ] && dropvap
="$dropvap $oldvap"
1286 [ -n "$dropvap" ] && mac80211_vap_cleanup wpa_supplicant
"$dropvap"
1290 _list_phy_interfaces
() {
1292 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
1293 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev
/null
;
1295 ls "/sys/class/ieee80211/${phy}/device" 2>/dev
/null |
grep net
: |
sed -e 's,net:,,g'
1299 list_phy_interfaces
() {
1302 for dev
in $
(_list_phy_interfaces
"$phy"); do
1303 readlink
"/sys/class/net/${dev}/phy80211" |
grep -q "/${phy}\$" ||
continue
1308 drv_mac80211_teardown
() {
1313 echo "Bug: PHY is undefined for device '$1'"
1317 mac80211_interface_cleanup
"$phy"
1318 uci
-q -P /var
/state revert wireless._
${phy}