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() {
275 local oIFS="$IFS"; IFS=":"; set -- $mac; IFS="$oIFS"
278 [ $off -gt 0 ] && b2mask=0x02
280 printf "%02x:%s:%s:%s:%02x:%02x" \
281 $(( 0x$1 | $b2mask )) $2 $3 $4 \
282 $(( (0x$5 + ($off / 0x100)) % 0x100 )) \
283 $(( (0x$6 + $off) % 0x100 ))
288 config_get channel "$device" channel
289 config_get vifs "$device" vifs
290 config_get txpower "$device" txpower
291 config_get country "$device" country
292 config_get distance "$device" distance
293 config_get txantenna "$device" txantenna all
294 config_get rxantenna "$device" rxantenna all
295 config_get antenna_gain "$device" antenna_gain 0
296 config_get frag "$device" frag
297 config_get rts "$device" rts
298 find_mac80211_phy "$device" || return 0
299 config_get phy "$device" phy
304 local hostapd_ctrl=""
306 [ -n "$country" ] && {
307 iw reg get | grep -q "^country $country:" || {
308 iw reg set "$country"
313 config_get chanbw "$device" chanbw
314 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath9k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw
315 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath5k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode
317 [ "$channel" = "auto" -o "$channel" = "0" ] || {
321 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
322 iw phy "$phy" set antenna_gain $antenna_gain
324 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
325 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
326 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
329 # convert channel to frequency
330 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
332 wifi_fixup_hwmode "$device" "g"
334 config_get ifname "$vif" ifname
335 [ -n "$ifname" ] || {
336 [ $i -gt 0 ] && ifname="wlan${phy#phy}-$i" || ifname="wlan${phy#phy}"
338 config_set "$vif" ifname "$ifname"
340 config_get mode "$vif" mode
341 config_get ssid "$vif" ssid
343 # It is far easier to delete and create the desired interface
346 iw phy "$phy" interface add "$ifname" type adhoc
349 # Hostapd will handle recreating the interface and
350 # it's accompanying monitor
351 apidx="$(($apidx + 1))"
352 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
355 config_get mesh_id "$vif" mesh_id
356 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
359 iw phy "$phy" interface add "$ifname" type monitor
363 config_get_bool wds "$vif" wds 0
364 [ "$wds" -gt 0 ] && wdsflag="4addr on"
365 iw phy "$phy" interface add "$ifname" type managed $wdsflag
366 config_get_bool powersave "$vif" powersave 0
367 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
368 iw "$ifname" set power_save "$powersave"
372 # All interfaces must have unique mac addresses
373 # which can either be explicitly set in the device
374 # section, or automatically generated
375 config_get macaddr "$device" macaddr
376 config_get vif_mac "$vif" macaddr
377 [ -n "$vif_mac" ] || {
378 vif_mac="$(mac80211_generate_mac $macidx $macaddr)"
379 macidx="$(($macidx + 1))"
381 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
382 config_set "$vif" macaddr "$vif_mac"
386 # ALL ap functionality will be passed to hostapd
390 # ALL station functionality will be passed to wpa_supplicant
392 if [ ! "$mode" = "ap" ]; then
393 # We attempt to set the channel for all interfaces, although
394 # mac80211 may not support it or the driver might not yet
395 # for ap mode this is handled by hostapd
396 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
403 rm -f /var/run/hostapd-$phy.conf
405 config_get mode "$vif" mode
406 [ "$mode" = "ap" ] || continue
407 mac80211_hostapd_setup_bss "$phy" "$vif"
411 [ -n "$start_hostapd" ] && {
412 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
413 echo "Failed to start hostapd for $phy"
419 config_get mode "$vif" mode
420 config_get ifname "$vif" ifname
421 [ "$mode" = "ap" ] || continue
422 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
423 mac80211_start_vif "$vif" "$ifname"
428 config_get mode "$vif" mode
429 config_get ifname "$vif" ifname
430 [ "$mode" = "ap" ] || ifconfig "$ifname" up
432 config_get vif_txpower "$vif" txpower
433 # use vif_txpower (from wifi-iface) to override txpower (from
434 # wifi-device) if the latter doesn't exist
435 txpower="${txpower:-$vif_txpower}"
436 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
440 config_get bssid "$vif" bssid
441 config_get ssid "$vif" ssid
442 config_get beacon_int "$device" beacon_int
443 config_get basic_rate_list "$device" basic_rate
444 config_get encryption "$vif" encryption
445 config_get key "$vif" key 1
446 config_get mcast_rate "$vif" mcast_rate
447 config_get htmode "$device" htmode
455 [ "$encryption" == "psk" -o "$encryption" == "psk2" ] && {
456 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
457 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" $freq $htmode || {
458 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
459 # make sure this wifi interface won't accidentally stay open without encryption
460 ifconfig "$ifname" down
462 mac80211_start_vif "$vif" "$ifname"
467 [ "$encryption" == "wep" ] && {
471 for idx in 1 2 3 4; do
473 config_get ikey "$vif" "key$idx"
476 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
477 [ $idx -eq $key ] && ikey="d:$ikey"
478 append keyspec "$ikey"
482 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
486 local br brval brsub brstr
487 [ -n "$basic_rate_list" ] && {
488 for br in $basic_rate_list; do
489 brval="$(($br / 1000))"
490 brsub="$((($br / 100) % 10))"
491 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
492 [ -n "$brstr" ] && brstr="$brstr,"
498 [ -n "$mcast_rate" ] && {
499 mcval="$(($mcast_rate / 1000))"
500 mcsub="$(( ($mcast_rate / 100) % 10 ))"
501 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
504 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
505 ${fixed:+fixed-freq} $bssid \
506 ${beacon_int:+beacon-interval $beacon_int} \
507 ${brstr:+basic-rates $brstr} \
508 ${mcval:+mcast-rate $mcval} \
509 ${keyspec:+keys $keyspec}
512 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
513 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
514 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
515 # make sure this wifi interface won't accidentally stay open without encryption
516 ifconfig "$ifname" down
522 [ "$mode" = "ap" ] || mac80211_start_vif "$vif" "$ifname"
528 check_mac80211_device() {
529 config_get phy "$1" phy
531 find_mac80211_phy "$1" >/dev/null || return 0
532 config_get phy "$1" phy
534 [ "$phy" = "$dev" ] && found=1
541 config_get type "radio$devidx" type
542 [ -n "$type" ] || break
543 devidx=$(($devidx + 1))
545 for dev in $(ls /sys/class/ieee80211); do
547 config_foreach check_mac80211_device wifi-device
548 [ "$found" -gt 0 ] && continue
554 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
555 ht_cap="$(($ht_cap | $cap))"
558 [ "$ht_cap" -gt 0 ] && {
560 append ht_capab " option htmode HT20" "$N"
562 list=" list ht_capab"
563 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
564 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
565 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
566 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
567 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
568 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
569 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
570 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
571 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
573 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
575 if [ -x /usr/bin/readlink ]; then
576 path="$(readlink -f /sys/class/ieee80211/${dev}/device)"
577 path="${path##/sys/devices/}"
578 dev_id=" option path '$path'"
580 dev_id=" option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)"
584 config wifi-device radio$devidx
586 option channel ${channel}
587 option hwmode 11${mode_11n}${mode_band}
590 # REMOVE THIS LINE TO ENABLE WIFI:
594 option device radio$devidx
598 option encryption none
601 devidx=$(($devidx + 1))