2 append DRIVERS "mac80211"
4 mac80211_hostapd_setup_base() {
8 cfgfile="/var/run/hostapd-$phy.conf"
9 macfile="/var/run/hostapd-$phy.maclist"
10 [ -e "$macfile" ] && rm -f "$macfile"
12 config_get device "$vif" device
13 config_get country "$device" country
14 config_get hwmode "$device" hwmode
15 config_get channel "$device" channel
16 config_get beacon_int "$device" beacon_int
17 config_get basic_rate_list "$device" basic_rate
18 config_get_bool noscan "$device" noscan
19 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
21 [ "$channel" = auto ] && {
22 channel=$(iw phy "$phy" info | \
23 sed -ne '/MHz/ { /disabled\|passive\|radar/d; s/.*\[//; s/\].*//; p; q }')
24 config_set "$device" channel "$channel"
28 config_get hwmode_11n "$device" hwmode_11n
29 [ -n "$hwmode_11n" ] && {
31 append base_cfg "ieee80211n=1" "$N"
32 config_get htmode "$device" htmode
33 config_get ht_capab_list "$device" ht_capab
35 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
38 for cap in $ht_capab_list; do
39 ht_capab="$ht_capab[$cap]"
41 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
46 [ -n "$country" ] && country_ie=1
47 config_get_bool country_ie "$device" country_ie "$country_ie"
48 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
50 config_get macfilter "$vif" macfilter
53 append base_cfg "macaddr_acl=1" "$N"
54 append base_cfg "accept_mac_file=$macfile" "$N"
57 append base_cfg "macaddr_acl=0" "$N"
58 append base_cfg "deny_mac_file=$macfile" "$N"
61 config_get maclist "$vif" maclist
62 [ -n "$maclist" ] && {
63 for mac in $maclist; do
64 echo "$mac" >> $macfile
69 [ -n "$basic_rate_list" ] && {
70 for br in $basic_rate_list; do
71 brval="$(($br / 100))"
72 [ -n "$brstr" ] && brstr="$brstr "
77 cat >> "$cfgfile" <<EOF
78 ctrl_interface=/var/run/hostapd-$phy
83 wmm_ac_bk_txop_limit=0
88 wmm_ac_be_txop_limit=0
93 wmm_ac_vi_txop_limit=94
98 wmm_ac_vo_txop_limit=47
100 tx_queue_data3_aifs=7
101 tx_queue_data3_cwmin=15
102 tx_queue_data3_cwmax=1023
103 tx_queue_data3_burst=0
104 tx_queue_data2_aifs=3
105 tx_queue_data2_cwmin=15
106 tx_queue_data2_cwmax=63
107 tx_queue_data2_burst=0
108 tx_queue_data1_aifs=1
109 tx_queue_data1_cwmin=7
110 tx_queue_data1_cwmax=15
111 tx_queue_data1_burst=3.0
112 tx_queue_data0_aifs=1
113 tx_queue_data0_cwmin=3
114 tx_queue_data0_cwmax=7
115 tx_queue_data0_burst=1.5
116 ${hwmode:+hw_mode=$hwmode}
117 ${channel:+channel=$channel}
118 ${beacon_int:+beacon_int=$beacon_int}
119 ${country:+country_code=$country}
120 ${noscan:+noscan=$noscan}
121 ${brstr:+basic_rates=$brstr}
127 mac80211_hostapd_setup_bss() {
132 cfgfile="/var/run/hostapd-$phy.conf"
133 config_get ifname "$vif" ifname
135 if [ -f "$cfgfile" ]; then
136 append hostapd_cfg "bss=$ifname" "$N"
138 mac80211_hostapd_setup_base "$phy" "$ifname"
139 append hostapd_cfg "interface=$ifname" "$N"
143 net_cfg="$(find_net_config "$vif")"
144 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
145 config_set "$vif" bridge "$bridge"
147 hostapd_set_bss_options hostapd_cfg "$vif"
149 config_get_bool wds "$vif" wds 0
150 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
152 local macaddr hidden maxassoc wmm
153 config_get macaddr "$vif" macaddr
154 config_get maxassoc "$vif" maxassoc
155 config_get dtim_period "$vif" dtim_period
156 config_get max_listen_int "$vif" max_listen_int
157 config_get_bool hidden "$vif" hidden 0
158 config_get_bool wmm "$vif" wmm 1
159 cat >> /var/run/hostapd-$phy.conf <<EOF
163 ignore_broadcast_ssid=$hidden
164 ${dtim_period:+dtim_period=$dtim_period}
165 ${max_listen_int:+max_listen_interval=$max_listen_int}
166 ${maxassoc:+max_num_sta=$maxassoc}
170 mac80211_start_vif() {
175 net_cfg="$(find_net_config "$vif")"
176 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
178 set_wifi_up "$vif" "$ifname"
181 find_mac80211_phy() {
184 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
185 config_get phy "$device" phy
186 [ -z "$phy" -a -n "$macaddr" ] && {
187 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
188 [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
189 config_set "$device" phy "$phy"
192 config_get phy "$device" phy
194 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
195 echo "PHY for wifi device $1 not found"
198 [ -z "$macaddr" ] && {
199 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
206 local adhoc sta ap monitor mesh
208 config_get vifs "$device" vifs
210 config_get mode "$vif" mode
212 adhoc|sta|ap|monitor|mesh)
215 *) echo "$device($vif): Invalid mode, ignored."; continue;;
219 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
222 list_phy_interfaces() {
224 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
225 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
227 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
234 find_mac80211_phy "$device" || return 0
235 config_get phy "$device" phy
237 set_wifi_down "$device"
238 # kill all running hostapd and wpa_supplicant processes that
239 # are running on atheros/mac80211 vifs
240 for pid in `pidof hostapd`; do
241 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
246 for wdev in $(list_phy_interfaces "$phy"); do
247 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
248 for pid in `pidof wpa_supplicant`; do
249 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
252 ifconfig "$wdev" down 2>/dev/null
262 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
266 config_get channel "$device" channel
267 config_get vifs "$device" vifs
268 config_get txpower "$device" txpower
269 config_get country "$device" country
270 config_get distance "$device" distance
271 config_get txantenna "$device" txantenna all
272 config_get rxantenna "$device" rxantenna all
273 config_get frag "$device" frag
274 config_get rts "$device" rts
275 find_mac80211_phy "$device" || return 0
276 config_get phy "$device" phy
281 local hostapd_ctrl=""
283 config_get ath9k_chanbw "$device" ath9k_chanbw
284 [ -n "$ath9k_chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath9k ] && echo "$ath9k_chanbw" > /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw
286 [ -n "$country" ] && iw reg set "$country"
287 [ "$channel" = "auto" -o "$channel" = "0" ] || {
291 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
293 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
294 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
295 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
298 # convert channel to frequency
299 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
301 wifi_fixup_hwmode "$device" "g"
303 while [ -d "/sys/class/net/wlan$i" ]; do
307 config_get ifname "$vif" ifname
308 [ -n "$ifname" ] || {
311 config_set "$vif" ifname "$ifname"
313 config_get mode "$vif" mode
314 config_get ssid "$vif" ssid
316 # It is far easier to delete and create the desired interface
319 iw phy "$phy" interface add "$ifname" type adhoc
322 # Hostapd will handle recreating the interface and
323 # it's accompanying monitor
324 apidx="$(($apidx + 1))"
326 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
329 config_get mesh_id "$vif" mesh_id
330 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
333 iw phy "$phy" interface add "$ifname" type monitor
337 config_get_bool wds "$vif" wds 0
338 [ "$wds" -gt 0 ] && wdsflag="4addr on"
339 iw phy "$phy" interface add "$ifname" type managed $wdsflag
340 config_get_bool powersave "$vif" powersave 0
341 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
342 iwconfig "$ifname" power "$powersave"
346 # All interfaces must have unique mac addresses
347 # which can either be explicitly set in the device
348 # section, or automatically generated
349 config_get macaddr "$device" macaddr
350 local mac_1="${macaddr%%:*}"
351 local mac_2="${macaddr#*:}"
353 config_get vif_mac "$vif" macaddr
354 [ -n "$vif_mac" ] || {
355 if [ "$macidx" -gt 0 ]; then
356 offset="$(( 2 + $macidx * 4 ))"
360 vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
361 macidx="$(($macidx + 1))"
363 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
364 config_set "$vif" macaddr "$vif_mac"
368 # ALL ap functionality will be passed to hostapd
372 # ALL station functionality will be passed to wpa_supplicant
374 if [ ! "$mode" = "ap" ]; then
375 # We attempt to set the channel for all interfaces, although
376 # mac80211 may not support it or the driver might not yet
377 # for ap mode this is handled by hostapd
378 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
381 config_get vif_txpower "$vif" txpower
382 # use vif_txpower (from wifi-iface) to override txpower (from
383 # wifi-device) if the latter doesn't exist
384 txpower="${txpower:-$vif_txpower}"
385 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
389 rm -f /var/run/hostapd-$phy.conf
391 config_get mode "$vif" mode
392 [ "$mode" = "ap" ] || continue
393 mac80211_hostapd_setup_bss "$phy" "$vif"
397 [ -n "$start_hostapd" ] && {
398 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
399 echo "Failed to start hostapd for $phy"
405 config_get mode "$vif" mode
406 config_get ifname "$vif" ifname
407 [ "$mode" = "ap" ] || continue
408 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
409 mac80211_start_vif "$vif" "$ifname"
414 config_get mode "$vif" mode
415 config_get ifname "$vif" ifname
416 [ ! "$mode" = "ap" ] || continue
417 ifconfig "$ifname" up
419 if [ ! "$mode" = "ap" ]; then
420 ifconfig "$ifname" up
423 config_get bssid "$vif" bssid
424 config_get ssid "$vif" ssid
425 config_get beacon_int "$device" beacon_int
426 config_get basic_rate_list "$device" basic_rate
427 config_get encryption "$vif" encryption
428 config_get key "$vif" key 1
429 config_get mcast_rate "$vif" mcast_rate
432 [ "$encryption" == "wep" ] && {
436 for idx in 1 2 3 4; do
438 config_get ikey "$vif" "key$idx"
441 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
442 [ $idx -eq $key ] && ikey="d:$ikey"
443 append keyspec "$ikey"
447 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
451 local br brval brsub brstr
452 [ -n "$basic_rate_list" ] && {
453 for br in $basic_rate_list; do
454 brval="$(($br / 1000))"
455 brsub="$((($br / 100) % 10))"
456 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
457 [ -n "$brstr" ] && brstr="$brstr,"
463 [ -n "$mcast_rate" ] && {
464 mcval="$(($mcast_rate / 1000))"
465 mcsub="$(( ($mcast_rate / 100) % 10 ))"
466 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
469 config_get htmode "$device" htmode
475 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
476 ${fixed:+fixed-freq} $bssid \
477 ${beacon_int:+beacon-interval $beacon_int} \
478 ${brstr:+basic-rates $brstr} \
479 ${mcval:+mcast-rate $mcval} \
480 ${keyspec:+keys $keyspec}
483 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
484 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
485 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
486 # make sure this wifi interface won't accidentally stay open without encryption
487 ifconfig "$ifname" down
493 mac80211_start_vif "$vif" "$ifname"
500 check_mac80211_device() {
501 config_get phy "$1" phy
503 find_mac80211_phy "$1" >/dev/null || return 0
504 config_get phy "$1" phy
506 [ "$phy" = "$dev" ] && found=1
513 config_get type "radio$devidx" type
514 [ -n "$type" ] || break
515 devidx=$(($devidx + 1))
517 for dev in $(ls /sys/class/ieee80211); do
519 config_foreach check_mac80211_device wifi-device
520 [ "$found" -gt 0 ] && continue
526 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
527 ht_cap="$(($ht_cap | $cap))"
530 [ "$ht_cap" -gt 0 ] && {
532 append ht_capab " option htmode HT20" "$N"
534 list=" list ht_capab"
535 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
536 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
537 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
538 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
539 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
540 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
541 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
542 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
543 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
545 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
548 config wifi-device radio$devidx
550 option channel ${channel}
551 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
552 option hwmode 11${mode_11n}${mode_band}
554 # REMOVE THIS LINE TO ENABLE WIFI:
558 option device radio$devidx
562 option encryption none
565 devidx=$(($devidx + 1))