2 append DRIVERS
"mac80211"
4 mac80211_hostapd_setup_base
() {
8 cfgfile
="/var/run/hostapd-$phy.conf"
10 config_get device
"$vif" device
11 config_get country
"$device" country
12 config_get hwmode
"$device" hwmode
13 config_get channel
"$device" channel
14 config_get beacon_int
"$device" beacon_int
15 config_get basic_rate_list
"$device" basic_rate
16 config_get_bool noscan
"$device" noscan
18 hostapd_set_log_options base_cfg
"$device"
20 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode
"$device"
22 [ "$channel" = auto
] && {
23 channel
=$
(iw phy
"$phy" info | \
24 sed -ne '/MHz/ { /disabled\|passive\|radar/d; s/.*\[//; s/\].*//; p; q }')
25 config_set
"$device" channel
"$channel"
29 config_get hwmode_11n
"$device" hwmode_11n
30 [ -n "$hwmode_11n" ] && {
32 append base_cfg
"ieee80211n=1" "$N"
33 config_get htmode
"$device" htmode
34 config_get ht_capab_list
"$device" ht_capab
36 HT20|HT40
+|HT40-
) ht_capab
="[$htmode]";;
39 for cap
in $ht_capab_list; do
40 ht_capab
="$ht_capab[$cap]"
42 [ -n "$ht_capab" ] && append base_cfg
"ht_capab=$ht_capab" "$N"
47 [ -n "$country" ] && country_ie
=1
48 config_get_bool country_ie
"$device" country_ie
"$country_ie"
49 [ "$country_ie" -gt 0 ] && append base_cfg
"ieee80211d=1" "$N"
52 [ -n "$basic_rate_list" ] && {
53 for br
in $basic_rate_list; do
54 brval
="$(($br / 100))"
55 [ -n "$brstr" ] && brstr
="$brstr "
60 cat >> "$cfgfile" <<EOF
61 ctrl_interface=/var/run/hostapd-$phy
66 wmm_ac_bk_txop_limit=0
71 wmm_ac_be_txop_limit=0
76 wmm_ac_vi_txop_limit=94
81 wmm_ac_vo_txop_limit=47
84 tx_queue_data3_cwmin=15
85 tx_queue_data3_cwmax=1023
86 tx_queue_data3_burst=0
88 tx_queue_data2_cwmin=15
89 tx_queue_data2_cwmax=63
90 tx_queue_data2_burst=0
92 tx_queue_data1_cwmin=7
93 tx_queue_data1_cwmax=15
94 tx_queue_data1_burst=3.0
96 tx_queue_data0_cwmin=3
97 tx_queue_data0_cwmax=7
98 tx_queue_data0_burst=1.5
99 ${hwmode:+hw_mode=$hwmode}
100 ${channel:+channel=$channel}
101 ${beacon_int:+beacon_int=$beacon_int}
102 ${country:+country_code=$country}
103 ${noscan:+noscan=$noscan}
104 ${brstr:+basic_rates=$brstr}
110 mac80211_hostapd_setup_bss
() {
115 cfgfile
="/var/run/hostapd-$phy.conf"
116 config_get ifname
"$vif" ifname
118 if [ -f "$cfgfile" ]; then
119 append hostapd_cfg
"bss=$ifname" "$N"
121 mac80211_hostapd_setup_base
"$phy" "$ifname"
122 append hostapd_cfg
"interface=$ifname" "$N"
126 net_cfg
="$(find_net_config "$vif")"
127 [ -z "$net_cfg" ] || bridge
="$(bridge_interface "$net_cfg")"
128 config_set
"$vif" bridge
"$bridge"
130 hostapd_set_bss_options hostapd_cfg
"$vif"
132 config_get_bool wds
"$vif" wds
0
133 [ "$wds" -gt 0 ] && append hostapd_cfg
"wds_sta=1" "$N"
135 local macaddr hidden maxassoc wmm
136 config_get macaddr
"$vif" macaddr
137 config_get maxassoc
"$vif" maxassoc
138 config_get dtim_period
"$vif" dtim_period
139 config_get max_listen_int
"$vif" max_listen_int
140 config_get_bool hidden
"$vif" hidden
0
141 config_get_bool wmm
"$vif" wmm
1
142 cat >> /var
/run
/hostapd-
$phy.conf
<<EOF
146 ignore_broadcast_ssid=$hidden
147 ${dtim_period:+dtim_period=$dtim_period}
148 ${max_listen_int:+max_listen_interval=$max_listen_int}
149 ${maxassoc:+max_num_sta=$maxassoc}
153 mac80211_start_vif
() {
158 net_cfg
="$(find_net_config "$vif")"
159 [ -z "$net_cfg" ] || start_net
"$ifname" "$net_cfg"
161 set_wifi_up
"$vif" "$ifname"
166 [ -d /sys
/class
/ieee80211
/$phy ] && return
170 config_get devpath
"$device" path
171 [ -n "$devpath" -a -d "/sys/devices/$devpath/ieee80211" ] && {
172 phy
="$(ls /sys/devices/$devpath/ieee80211 | grep -m 1 phy)"
173 [ -n "$phy" ] && return
176 local macaddr
="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
177 [ -n "$macaddr" ] && {
178 for _phy
in $
(ls /sys
/class
/ieee80211
2>/dev
/null
); do
179 [ "$macaddr" = "$(cat /sys/class/ieee80211/${_phy}/macaddress)" ] ||
continue
188 find_mac80211_phy
() {
191 config_get phy
"$device" phy
193 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] ||
{
194 echo "PHY for wifi device $1 not found"
197 config_set
"$device" phy
"$phy"
199 config_get macaddr
"$device" macaddr
200 [ -z "$macaddr" ] && {
201 config_set
"$device" macaddr
"$(cat /sys/class/ieee80211/${phy}/macaddress)"
209 local adhoc sta ap monitor mesh disabled
211 config_get vifs
"$device" vifs
213 config_get_bool disabled
"$vif" disabled
0
214 [ $disabled = 0 ] ||
continue
216 config_get mode
"$vif" mode
218 adhoc|sta|ap|monitor|mesh
)
221 *) echo "$device($vif): Invalid mode, ignored."; continue;;
225 config_set
"$device" vifs
"${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
228 list_phy_interfaces
() {
230 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
231 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev
/null
;
233 ls "/sys/class/ieee80211/${phy}/device" 2>/dev
/null |
grep net
: |
sed -e 's,net:,,g'
240 find_mac80211_phy
"$device" ||
return 0
241 config_get phy
"$device" phy
243 set_wifi_down
"$device"
244 # kill all running hostapd and wpa_supplicant processes that
245 # are running on atheros/mac80211 vifs
246 for pid
in `pidof hostapd`; do
247 grep -E "$phy" /proc
/$pid/cmdline
>/dev
/null
&& \
252 for wdev
in $
(list_phy_interfaces
"$phy"); do
253 [ -f "/var/run/$wdev.pid" ] && kill $
(cat /var
/run
/$wdev.pid
) >&/dev
/null
2>&1
254 for pid
in `pidof wpa_supplicant`; do
255 grep "$wdev" /proc
/$pid/cmdline
>/dev
/null
&& \
258 ifconfig
"$wdev" down
2>/dev
/null
269 iw
"$phy" info |
grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
272 mac80211_generate_mac() {
276 local oIFS="$IFS"; IFS=":"; set -- $mac; IFS="$oIFS"
280 [ "$mask" = "00:00:00:00:00:00" -o $(( 0x${mask%%:*} & 0x2 )) -gt 0 ] && b2mask=0x02
282 printf "%02x
:%s
:%s
:%s
:%02x
:%02x
" \
283 $(( 0x$1 | $b2mask )) $2 $3 $4 \
284 $(( (0x$5 + ($off / 0x100)) % 0x100 )) \
285 $(( (0x$6 + $off) % 0x100 ))
290 config_get channel "$device" channel
291 config_get vifs "$device" vifs
292 config_get txpower "$device" txpower
293 config_get country "$device" country
294 config_get distance "$device" distance
295 config_get txantenna "$device" txantenna all
296 config_get rxantenna "$device" rxantenna all
297 config_get antenna_gain "$device" antenna_gain 0
298 config_get frag "$device" frag
299 config_get rts "$device" rts
300 find_mac80211_phy "$device" || return 0
301 config_get phy "$device" phy
306 local hostapd_ctrl=""
308 [ -n "$country" ] && {
309 iw reg get | grep -q "^country
$country:" || {
310 iw reg set "$country"
315 config_get chanbw "$device" chanbw
316 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath9k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw
317 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath5k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode
319 [ "$channel" = "auto
" -o "$channel" = "0" ] || {
323 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
324 iw phy "$phy" set antenna_gain $antenna_gain
326 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
327 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
328 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
331 # convert channel to frequency
332 local freq="$
(get_freq
"$phy" "${fixed:+$channel}")"
334 wifi_fixup_hwmode "$device" "g
"
336 config_get ifname "$vif" ifname
337 [ -n "$ifname" ] || {
338 [ $i -gt 0 ] && ifname="wlan
${phy#phy}-$i" || ifname="wlan
${phy#phy}"
340 config_set "$vif" ifname "$ifname"
342 config_get mode "$vif" mode
343 config_get ssid "$vif" ssid
345 # It is far easier to delete and create the desired interface
348 iw phy "$phy" interface add "$ifname" type adhoc
351 # Hostapd will handle recreating the interface and
352 # it's accompanying monitor
353 apidx="$
(($apidx + 1))"
354 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
357 config_get mesh_id "$vif" mesh_id
358 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
361 iw phy "$phy" interface add "$ifname" type monitor
365 config_get_bool wds "$vif" wds 0
366 [ "$wds" -gt 0 ] && wdsflag="4addr on
"
367 iw phy "$phy" interface add "$ifname" type managed $wdsflag
368 config_get_bool powersave "$vif" powersave 0
369 [ "$powersave" -gt 0 ] && powersave="on
" || powersave="off
"
370 iw "$ifname" set power_save "$powersave"
374 # All interfaces must have unique mac addresses
375 # which can either be explicitly set in the device
376 # section, or automatically generated
377 config_get macaddr "$device" macaddr
378 config_get vif_mac "$vif" macaddr
379 [ -n "$vif_mac" ] || {
380 vif_mac="$
(mac80211_generate_mac
$macidx $macaddr $
(cat /sys
/class
/ieee80211
/${phy}/address_mask
))"
381 macidx="$
(($macidx + 1))"
383 [ "$mode" = "ap
" ] || ifconfig "$ifname" hw ether "$vif_mac"
384 config_set "$vif" macaddr "$vif_mac"
388 # ALL ap functionality will be passed to hostapd
392 # ALL station functionality will be passed to wpa_supplicant
394 if [ ! "$mode" = "ap
" ]; then
395 # We attempt to set the channel for all interfaces, although
396 # mac80211 may not support it or the driver might not yet
397 # for ap mode this is handled by hostapd
398 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
405 rm -f /var/run/hostapd-$phy.conf
407 config_get mode "$vif" mode
408 [ "$mode" = "ap
" ] || continue
409 mac80211_hostapd_setup_bss "$phy" "$vif"
413 [ -n "$start_hostapd" ] && {
414 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
415 echo "Failed to start hostapd
for $phy"
421 config_get mode "$vif" mode
422 config_get ifname "$vif" ifname
423 [ "$mode" = "ap
" ] || continue
424 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
425 mac80211_start_vif "$vif" "$ifname"
430 config_get mode "$vif" mode
431 config_get ifname "$vif" ifname
432 [ "$mode" = "ap
" ] || ifconfig "$ifname" up
434 config_get vif_txpower "$vif" txpower
435 # use vif_txpower (from wifi-iface) to override txpower (from
436 # wifi-device) if the latter doesn't exist
437 txpower="${txpower:-$vif_txpower}"
438 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
442 config_get bssid "$vif" bssid
443 config_get ssid "$vif" ssid
444 config_get beacon_int "$device" beacon_int
445 config_get basic_rate_list "$device" basic_rate
446 config_get encryption "$vif" encryption
447 config_get key "$vif" key 1
448 config_get mcast_rate "$vif" mcast_rate
449 config_get htmode "$device" htmode
457 [ "$encryption" == "psk
" -o "$encryption" == "psk2
" ] && {
458 if eval "type wpa_supplicant_setup_vif
" 2>/dev/null >/dev/null; then
459 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" $freq $htmode || {
460 echo "enable_mac80211
($device): Failed to
set up wpa_supplicant
for interface
$ifname" >&2
461 # make sure this wifi interface won't accidentally stay open without encryption
462 ifconfig "$ifname" down
464 mac80211_start_vif "$vif" "$ifname"
469 [ "$encryption" == "wep
" ] && {
473 for idx in 1 2 3 4; do
475 config_get ikey "$vif" "key
$idx"
478 ikey="$
(($idx - 1)):$
(prepare_key_wep
"$ikey")"
479 [ $idx -eq $key ] && ikey="d
:$ikey"
480 append keyspec "$ikey"
484 *) append keyspec "d
:0:$
(prepare_key_wep
"$key")" ;;
488 local br brval brsub brstr
489 [ -n "$basic_rate_list" ] && {
490 for br in $basic_rate_list; do
491 brval="$
(($br / 1000))"
492 brsub="$
((($br / 100) % 10))"
493 [ "$brsub" -gt 0 ] && brval="$brval.
$brsub"
494 [ -n "$brstr" ] && brstr="$brstr,"
500 [ -n "$mcast_rate" ] && {
501 mcval="$
(($mcast_rate / 1000))"
502 mcsub="$
(( ($mcast_rate / 100) % 10 ))"
503 [ "$mcsub" -gt 0 ] && mcval="$mcval.
$mcsub"
506 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
507 ${fixed:+fixed-freq} $bssid \
508 ${beacon_int:+beacon-interval $beacon_int} \
509 ${brstr:+basic-rates $brstr} \
510 ${mcval:+mcast-rate $mcval} \
511 ${keyspec:+keys $keyspec}
514 if eval "type wpa_supplicant_setup_vif
" 2>/dev/null >/dev/null; then
515 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
516 echo "enable_mac80211
($device): Failed to
set up wpa_supplicant
for interface
$ifname" >&2
517 # make sure this wifi interface won't accidentally stay open without encryption
518 ifconfig "$ifname" down
524 [ "$mode" = "ap
" ] || mac80211_start_vif "$vif" "$ifname"
530 check_mac80211_device() {
531 config_get phy "$1" phy
533 find_mac80211_phy "$1" >/dev/null || return 0
534 config_get phy "$1" phy
536 [ "$phy" = "$dev" ] && found=1
543 config_get type "radio
$devidx" type
544 [ -n "$type" ] || break
545 devidx=$(($devidx + 1))
547 for dev in $(ls /sys/class/ieee80211); do
549 config_foreach check_mac80211_device wifi-device
550 [ "$found" -gt 0 ] && continue
556 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
557 ht_cap="$
(($ht_cap |
$cap))"
560 [ "$ht_cap" -gt 0 ] && {
562 append ht_capab " option htmode HT20
" "$N"
564 list=" list ht_capab
"
565 [ "$
(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC
" "$N"
566 [ "$
(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF
" "$N"
567 [ "$
(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20
" "$N"
568 [ "$
(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40
" "$N"
569 [ "$
(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC
" "$N"
570 [ "$
(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1
" "$N"
571 [ "$
(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12
" "$N"
572 [ "$
(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123
" "$N"
573 [ "$
(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40
" "$N"
575 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a
"; channel="36"; }
577 if [ -x /usr/bin/readlink ]; then
578 path="$
(readlink
-f /sys
/class
/ieee80211
/${dev}/device
)"
579 path="${path##/sys/devices/}"
580 dev_id=" option path
'$path'"
582 dev_id=" option macaddr $
(cat /sys
/class
/ieee80211
/${dev}/macaddress
)"
586 config wifi-device radio$devidx
588 option channel ${channel}
589 option hwmode 11${mode_11n}${mode_band}
592 # REMOVE THIS LINE TO ENABLE WIFI:
596 option device radio$devidx
600 option encryption none
603 devidx=$(($devidx + 1))