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_bool noscan "$device" noscan
17 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
18 [ "$channel" = auto ] && channel=
20 config_get hwmode_11n "$device" hwmode_11n
21 [ -n "$hwmode_11n" ] && {
23 append base_cfg "ieee80211n=1" "$N"
24 config_get htmode "$device" htmode
25 config_get ht_capab_list "$device" ht_capab
27 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
30 for cap in $ht_capab_list; do
31 ht_capab="$ht_capab[$cap]"
33 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
37 config_get macfilter "$vif" macfilter
40 append base_cfg "macaddr_acl=1" "$N"
41 append base_cfg "accept_mac_file=$macfile" "$N"
44 append base_cfg "macaddr_acl=0" "$N"
45 append base_cfg "deny_mac_file=$macfile" "$N"
48 config_get maclist "$vif" maclist
49 [ -n "$maclist" ] && {
50 for mac in $maclist; do
51 echo "$mac" >> $macfile
54 cat >> "$cfgfile" <<EOF
55 ctrl_interface=/var/run/hostapd-$phy
60 wmm_ac_bk_txop_limit=0
65 wmm_ac_be_txop_limit=0
70 wmm_ac_vi_txop_limit=94
75 wmm_ac_vo_txop_limit=47
78 tx_queue_data3_cwmin=15
79 tx_queue_data3_cwmax=1023
80 tx_queue_data3_burst=0
82 tx_queue_data2_cwmin=15
83 tx_queue_data2_cwmax=63
84 tx_queue_data2_burst=0
86 tx_queue_data1_cwmin=7
87 tx_queue_data1_cwmax=15
88 tx_queue_data1_burst=3.0
90 tx_queue_data0_cwmin=3
91 tx_queue_data0_cwmax=7
92 tx_queue_data0_burst=1.5
93 ${hwmode:+hw_mode=$hwmode}
94 ${channel:+channel=$channel}
95 ${country:+country_code=$country}
96 ${noscan:+noscan=$noscan}
102 mac80211_hostapd_setup_bss() {
107 cfgfile="/var/run/hostapd-$phy.conf"
108 config_get ifname "$vif" ifname
110 if [ -f "$cfgfile" ]; then
111 append hostapd_cfg "bss=$ifname" "$N"
113 mac80211_hostapd_setup_base "$phy" "$ifname"
114 append hostapd_cfg "interface=$ifname" "$N"
118 net_cfg="$(find_net_config "$vif")"
119 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
120 config_set "$vif" bridge "$bridge"
122 hostapd_set_bss_options hostapd_cfg "$vif"
124 config_get_bool wds "$vif" wds 0
125 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
127 local macaddr hidden maxassoc wmm
128 config_get macaddr "$vif" macaddr
129 config_get maxassoc "$vif" maxassoc
130 config_get_bool hidden "$vif" hidden 0
131 config_get_bool wmm "$vif" wmm 1
132 cat >> /var/run/hostapd-$phy.conf <<EOF
136 ignore_broadcast_ssid=$hidden
137 ${maxassoc:+max_num_sta=$maxassoc}
141 mac80211_start_vif() {
146 net_cfg="$(find_net_config "$vif")"
147 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
149 set_wifi_up "$vif" "$ifname"
152 find_mac80211_phy() {
155 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
156 config_get phy "$device" phy
157 [ -z "$phy" -a -n "$macaddr" ] && {
158 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
159 [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
160 config_set "$device" phy "$phy"
163 config_get phy "$device" phy
165 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
166 echo "PHY for wifi device $1 not found"
169 [ -z "$macaddr" ] && {
170 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
177 local adhoc sta ap monitor mesh
179 config_get vifs "$device" vifs
181 config_get mode "$vif" mode
183 adhoc|sta|ap|monitor|mesh)
186 *) echo "$device($vif): Invalid mode, ignored."; continue;;
190 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
193 list_phy_interfaces() {
195 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
196 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
198 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
205 find_mac80211_phy "$device" || return 0
206 config_get phy "$device" phy
208 set_wifi_down "$device"
209 # kill all running hostapd and wpa_supplicant processes that
210 # are running on atheros/mac80211 vifs
211 for pid in `pidof hostapd`; do
212 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
217 for wdev in $(list_phy_interfaces "$phy"); do
218 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
219 for pid in `pidof wpa_supplicant`; do
220 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
223 ifconfig "$wdev" down 2>/dev/null
233 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
237 config_get channel "$device" channel
238 config_get vifs "$device" vifs
239 config_get txpower "$device" txpower
240 config_get country "$device" country
241 config_get distance "$device" distance
242 config_get antenna "$device" antenna
243 config_get frag "$device" frag
244 config_get rts "$device" rts
245 find_mac80211_phy "$device" || return 0
246 config_get phy "$device" phy
251 local hostapd_ctrl=""
253 [ -n "$country" ] && iw reg set "$country"
254 [ "$channel" = "auto" -o "$channel" = "0" ] || {
258 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
259 [ -n "$antenna" ] && iw phy "$phy" set antenna $antenna
260 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
261 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
264 # convert channel to frequency
265 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
267 wifi_fixup_hwmode "$device" "g"
269 while [ -d "/sys/class/net/wlan$i" ]; do
273 config_get ifname "$vif" ifname
274 [ -n "$ifname" ] || {
277 config_set "$vif" ifname "$ifname"
279 config_get mode "$vif" mode
280 config_get ssid "$vif" ssid
282 # It is far easier to delete and create the desired interface
285 iw phy "$phy" interface add "$ifname" type adhoc
288 # Hostapd will handle recreating the interface and
289 # it's accompanying monitor
290 apidx="$(($apidx + 1))"
292 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
295 config_get mesh_id "$vif" mesh_id
296 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
299 iw phy "$phy" interface add "$ifname" type monitor
303 config_get_bool wds "$vif" wds 0
304 [ "$wds" -gt 0 ] && wdsflag="4addr on"
305 iw phy "$phy" interface add "$ifname" type managed $wdsflag
306 config_get_bool powersave "$vif" powersave 0
307 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
308 iwconfig "$ifname" power "$powersave"
312 # All interfaces must have unique mac addresses
313 # which can either be explicitly set in the device
314 # section, or automatically generated
315 config_get macaddr "$device" macaddr
316 local mac_1="${macaddr%%:*}"
317 local mac_2="${macaddr#*:}"
319 config_get vif_mac "$vif" macaddr
320 [ -n "$vif_mac" ] || {
321 if [ "$macidx" -gt 0 ]; then
322 offset="$(( 2 + $macidx * 4 ))"
326 vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
327 macidx="$(($macidx + 1))"
329 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
330 config_set "$vif" macaddr "$vif_mac"
334 # ALL ap functionality will be passed to hostapd
338 # ALL station functionality will be passed to wpa_supplicant
340 if [ ! "$mode" = "ap" ]; then
341 # We attempt to set the channel for all interfaces, although
342 # mac80211 may not support it or the driver might not yet
343 # for ap mode this is handled by hostapd
344 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
347 config_get vif_txpower "$vif" txpower
348 # use vif_txpower (from wifi-iface) to override txpower (from
349 # wifi-device) if the latter doesn't exist
350 txpower="${txpower:-$vif_txpower}"
351 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
355 rm -f /var/run/hostapd-$phy.conf
357 config_get mode "$vif" mode
358 [ "$mode" = "ap" ] || continue
359 mac80211_hostapd_setup_bss "$phy" "$vif"
363 [ -n "$start_hostapd" ] && {
364 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
365 echo "Failed to start hostapd for $phy"
371 config_get mode "$vif" mode
372 config_get ifname "$vif" ifname
373 [ "$mode" = "ap" ] || continue
374 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
375 mac80211_start_vif "$vif" "$ifname"
380 config_get mode "$vif" mode
381 config_get ifname "$vif" ifname
382 [ ! "$mode" = "ap" ] || continue
383 ifconfig "$ifname" up
385 if [ ! "$mode" = "ap" ]; then
386 ifconfig "$ifname" up
389 config_get bssid "$vif" bssid
390 config_get ssid "$vif" ssid
391 config_get bintval "$vif" bintval
392 config_get basicrates "$vif" basicrates
393 config_get encryption "$vif" encryption
394 config_get key "$vif" key
395 config_get mcast_rate "$vif" mcast_rate
396 [ -n "$bintval" ] && BINTVAL="beacon-interval $bintval"
397 [ -n "$basicrates" ] && BRATES="basic-rates $basicrates"
398 [ "$encryption" == "wep" ] && [ -n "$key" ] && KEY="key d:0:$key"
400 [ -n "$mcast_rate" ] && {
401 mcval="$(($mcast_rate / 1000))"
402 mcsub="$(( ($mcast_rate / 100) % 10 ))"
403 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
405 iw dev "$ifname" ibss join "$ssid" $freq ${fixed:+fixed-freq} $bssid ${mcval:+mcast-rate $mcval} $BINTVAL $BRATES $KEY
408 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
409 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
410 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
411 # make sure this wifi interface won't accidentally stay open without encryption
412 ifconfig "$ifname" down
418 mac80211_start_vif "$vif" "$ifname"
426 config_get phy "$1" phy
428 find_mac80211_phy "$1" >/dev/null || return 0
429 config_get phy "$1" phy
431 [ "$phy" = "$dev" ] && found=1
438 config_get type "radio$devidx" type
439 [ -n "$type" ] || break
440 devidx=$(($devidx + 1))
442 for dev in $(ls /sys/class/ieee80211); do
444 config_foreach check_device wifi-device
445 [ "$found" -gt 0 ] && continue
451 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
452 ht_cap="$(($ht_cap | $cap))"
455 [ "$ht_cap" -gt 0 ] && {
457 append ht_capab " option htmode HT20" "$N"
459 list=" list ht_capab"
460 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
461 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
462 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
463 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
464 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
465 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
466 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
467 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
468 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
470 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
473 config wifi-device radio$devidx
475 option channel ${channel}
476 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
477 option hwmode 11${mode_11n}${mode_band}
479 # REMOVE THIS LINE TO ENABLE WIFI:
483 option device radio$devidx
487 option encryption none
490 devidx=$(($devidx + 1))