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"
20 [ "$channel" = auto ] && channel=
22 config_get hwmode_11n "$device" hwmode_11n
23 [ -n "$hwmode_11n" ] && {
25 append base_cfg "ieee80211n=1" "$N"
26 config_get htmode "$device" htmode
27 config_get ht_capab_list "$device" ht_capab
29 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
32 for cap in $ht_capab_list; do
33 ht_capab="$ht_capab[$cap]"
35 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
40 [ -n "$country" ] && country_ie=1
41 config_get_bool country_ie "$device" country_ie "$country_ie"
42 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
44 config_get macfilter "$vif" macfilter
47 append base_cfg "macaddr_acl=1" "$N"
48 append base_cfg "accept_mac_file=$macfile" "$N"
51 append base_cfg "macaddr_acl=0" "$N"
52 append base_cfg "deny_mac_file=$macfile" "$N"
55 config_get maclist "$vif" maclist
56 [ -n "$maclist" ] && {
57 for mac in $maclist; do
58 echo "$mac" >> $macfile
63 [ -n "$basic_rate_list" ] && {
64 for br in $basic_rate_list; do
65 brval="$(($br / 100))"
66 [ -n "$brstr" ] && brstr="$brstr "
71 cat >> "$cfgfile" <<EOF
72 ctrl_interface=/var/run/hostapd-$phy
77 wmm_ac_bk_txop_limit=0
82 wmm_ac_be_txop_limit=0
87 wmm_ac_vi_txop_limit=94
92 wmm_ac_vo_txop_limit=47
95 tx_queue_data3_cwmin=15
96 tx_queue_data3_cwmax=1023
97 tx_queue_data3_burst=0
99 tx_queue_data2_cwmin=15
100 tx_queue_data2_cwmax=63
101 tx_queue_data2_burst=0
102 tx_queue_data1_aifs=1
103 tx_queue_data1_cwmin=7
104 tx_queue_data1_cwmax=15
105 tx_queue_data1_burst=3.0
106 tx_queue_data0_aifs=1
107 tx_queue_data0_cwmin=3
108 tx_queue_data0_cwmax=7
109 tx_queue_data0_burst=1.5
110 ${hwmode:+hw_mode=$hwmode}
111 ${channel:+channel=$channel}
112 ${beacon_int:+beacon_int=$beacon_int}
113 ${country:+country_code=$country}
114 ${noscan:+noscan=$noscan}
115 ${brstr:+basic_rates=$brstr}
121 mac80211_hostapd_setup_bss() {
126 cfgfile="/var/run/hostapd-$phy.conf"
127 config_get ifname "$vif" ifname
129 if [ -f "$cfgfile" ]; then
130 append hostapd_cfg "bss=$ifname" "$N"
132 mac80211_hostapd_setup_base "$phy" "$ifname"
133 append hostapd_cfg "interface=$ifname" "$N"
137 net_cfg="$(find_net_config "$vif")"
138 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
139 config_set "$vif" bridge "$bridge"
141 hostapd_set_bss_options hostapd_cfg "$vif"
143 config_get_bool wds "$vif" wds 0
144 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
146 local macaddr hidden maxassoc wmm
147 config_get macaddr "$vif" macaddr
148 config_get maxassoc "$vif" maxassoc
149 config_get dtim_period "$vif" dtim_period
150 config_get max_listen_int "$vif" max_listen_int
151 config_get_bool hidden "$vif" hidden 0
152 config_get_bool wmm "$vif" wmm 1
153 cat >> /var/run/hostapd-$phy.conf <<EOF
157 ignore_broadcast_ssid=$hidden
158 ${dtim_period:+dtim_period=$dtim_period}
159 ${max_listen_int:+max_listen_interval=$max_listen_int}
160 ${maxassoc:+max_num_sta=$maxassoc}
164 mac80211_start_vif() {
169 net_cfg="$(find_net_config "$vif")"
170 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
172 set_wifi_up "$vif" "$ifname"
175 find_mac80211_phy() {
178 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
179 config_get phy "$device" phy
180 [ -z "$phy" -a -n "$macaddr" ] && {
181 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
182 [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
183 config_set "$device" phy "$phy"
186 config_get phy "$device" phy
188 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
189 echo "PHY for wifi device $1 not found"
192 [ -z "$macaddr" ] && {
193 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
200 local adhoc sta ap monitor mesh
202 config_get vifs "$device" vifs
204 config_get mode "$vif" mode
206 adhoc|sta|ap|monitor|mesh)
209 *) echo "$device($vif): Invalid mode, ignored."; continue;;
213 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
216 list_phy_interfaces() {
218 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
219 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
221 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
228 find_mac80211_phy "$device" || return 0
229 config_get phy "$device" phy
231 set_wifi_down "$device"
232 # kill all running hostapd and wpa_supplicant processes that
233 # are running on atheros/mac80211 vifs
234 for pid in `pidof hostapd`; do
235 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
240 for wdev in $(list_phy_interfaces "$phy"); do
241 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
242 for pid in `pidof wpa_supplicant`; do
243 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
246 ifconfig "$wdev" down 2>/dev/null
256 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
260 config_get channel "$device" channel
261 config_get vifs "$device" vifs
262 config_get txpower "$device" txpower
263 config_get country "$device" country
264 config_get distance "$device" distance
265 config_get txantenna "$device" txantenna all
266 config_get rxantenna "$device" rxantenna all
267 config_get frag "$device" frag
268 config_get rts "$device" rts
269 find_mac80211_phy "$device" || return 0
270 config_get phy "$device" phy
275 local hostapd_ctrl=""
277 [ -n "$country" ] && iw reg set "$country"
278 [ "$channel" = "auto" -o "$channel" = "0" ] || {
282 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
284 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
285 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
286 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
289 # convert channel to frequency
290 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
292 wifi_fixup_hwmode "$device" "g"
294 while [ -d "/sys/class/net/wlan$i" ]; do
298 config_get ifname "$vif" ifname
299 [ -n "$ifname" ] || {
302 config_set "$vif" ifname "$ifname"
304 config_get mode "$vif" mode
305 config_get ssid "$vif" ssid
307 # It is far easier to delete and create the desired interface
310 iw phy "$phy" interface add "$ifname" type adhoc
313 # Hostapd will handle recreating the interface and
314 # it's accompanying monitor
315 apidx="$(($apidx + 1))"
317 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
320 config_get mesh_id "$vif" mesh_id
321 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
324 iw phy "$phy" interface add "$ifname" type monitor
328 config_get_bool wds "$vif" wds 0
329 [ "$wds" -gt 0 ] && wdsflag="4addr on"
330 iw phy "$phy" interface add "$ifname" type managed $wdsflag
331 config_get_bool powersave "$vif" powersave 0
332 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
333 iwconfig "$ifname" power "$powersave"
337 # All interfaces must have unique mac addresses
338 # which can either be explicitly set in the device
339 # section, or automatically generated
340 config_get macaddr "$device" macaddr
341 local mac_1="${macaddr%%:*}"
342 local mac_2="${macaddr#*:}"
344 config_get vif_mac "$vif" macaddr
345 [ -n "$vif_mac" ] || {
346 if [ "$macidx" -gt 0 ]; then
347 offset="$(( 2 + $macidx * 4 ))"
351 vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
352 macidx="$(($macidx + 1))"
354 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
355 config_set "$vif" macaddr "$vif_mac"
359 # ALL ap functionality will be passed to hostapd
363 # ALL station functionality will be passed to wpa_supplicant
365 if [ ! "$mode" = "ap" ]; then
366 # We attempt to set the channel for all interfaces, although
367 # mac80211 may not support it or the driver might not yet
368 # for ap mode this is handled by hostapd
369 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
372 config_get vif_txpower "$vif" txpower
373 # use vif_txpower (from wifi-iface) to override txpower (from
374 # wifi-device) if the latter doesn't exist
375 txpower="${txpower:-$vif_txpower}"
376 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
380 rm -f /var/run/hostapd-$phy.conf
382 config_get mode "$vif" mode
383 [ "$mode" = "ap" ] || continue
384 mac80211_hostapd_setup_bss "$phy" "$vif"
388 [ -n "$start_hostapd" ] && {
389 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
390 echo "Failed to start hostapd for $phy"
396 config_get mode "$vif" mode
397 config_get ifname "$vif" ifname
398 [ "$mode" = "ap" ] || continue
399 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
400 mac80211_start_vif "$vif" "$ifname"
405 config_get mode "$vif" mode
406 config_get ifname "$vif" ifname
407 [ ! "$mode" = "ap" ] || continue
408 ifconfig "$ifname" up
410 if [ ! "$mode" = "ap" ]; then
411 ifconfig "$ifname" up
414 config_get bssid "$vif" bssid
415 config_get ssid "$vif" ssid
416 config_get beacon_int "$device" beacon_int
417 config_get basic_rate_list "$device" basic_rate
418 config_get encryption "$vif" encryption
419 config_get key "$vif" key 1
420 config_get mcast_rate "$vif" mcast_rate
423 [ "$encryption" == "wep" ] && {
427 for idx in 1 2 3 4; do
429 config_get ikey "$vif" "key$idx"
432 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
433 [ $idx -eq $key ] && ikey="d:$ikey"
434 append keyspec "$ikey"
438 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
442 local br brval brsub brstr
443 [ -n "$basic_rate_list" ] && {
444 for br in $basic_rate_list; do
445 brval="$(($br / 1000))"
446 brsub="$((($br / 100) % 10))"
447 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
448 [ -n "$brstr" ] && brstr="$brstr,"
454 [ -n "$mcast_rate" ] && {
455 mcval="$(($mcast_rate / 1000))"
456 mcsub="$(( ($mcast_rate / 100) % 10 ))"
457 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
460 config_get htmode "$device" htmode
466 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
467 ${fixed:+fixed-freq} $bssid \
468 ${beacon_int:+beacon-interval $beacon_int} \
469 ${brstr:+basic-rates $brstr} \
470 ${mcval:+mcast-rate $mcval} \
471 ${keyspec:+keys $keyspec}
474 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
475 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
476 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
477 # make sure this wifi interface won't accidentally stay open without encryption
478 ifconfig "$ifname" down
484 mac80211_start_vif "$vif" "$ifname"
491 check_mac80211_device() {
492 config_get phy "$1" phy
494 find_mac80211_phy "$1" >/dev/null || return 0
495 config_get phy "$1" phy
497 [ "$phy" = "$dev" ] && found=1
504 config_get type "radio$devidx" type
505 [ -n "$type" ] || break
506 devidx=$(($devidx + 1))
508 for dev in $(ls /sys/class/ieee80211); do
510 config_foreach check_mac80211_device wifi-device
511 [ "$found" -gt 0 ] && continue
517 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
518 ht_cap="$(($ht_cap | $cap))"
521 [ "$ht_cap" -gt 0 ] && {
523 append ht_capab " option htmode HT20" "$N"
525 list=" list ht_capab"
526 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
527 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
528 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
529 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
530 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
531 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
532 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
533 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
534 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
536 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
539 config wifi-device radio$devidx
541 option channel ${channel}
542 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
543 option hwmode 11${mode_11n}${mode_band}
545 # REMOVE THIS LINE TO ENABLE WIFI:
549 option device radio$devidx
553 option encryption none
556 devidx=$(($devidx + 1))