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 [ -n "$country" ] && iw reg set "$country"
284 [ "$channel" = "auto" -o "$channel" = "0" ] || {
288 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
290 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
291 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
292 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
295 # convert channel to frequency
296 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
298 wifi_fixup_hwmode "$device" "g"
300 while [ -d "/sys/class/net/wlan$i" ]; do
304 config_get ifname "$vif" ifname
305 [ -n "$ifname" ] || {
308 config_set "$vif" ifname "$ifname"
310 config_get mode "$vif" mode
311 config_get ssid "$vif" ssid
313 # It is far easier to delete and create the desired interface
316 iw phy "$phy" interface add "$ifname" type adhoc
319 # Hostapd will handle recreating the interface and
320 # it's accompanying monitor
321 apidx="$(($apidx + 1))"
323 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
326 config_get mesh_id "$vif" mesh_id
327 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
330 iw phy "$phy" interface add "$ifname" type monitor
334 config_get_bool wds "$vif" wds 0
335 [ "$wds" -gt 0 ] && wdsflag="4addr on"
336 iw phy "$phy" interface add "$ifname" type managed $wdsflag
337 config_get_bool powersave "$vif" powersave 0
338 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
339 iwconfig "$ifname" power "$powersave"
343 # All interfaces must have unique mac addresses
344 # which can either be explicitly set in the device
345 # section, or automatically generated
346 config_get macaddr "$device" macaddr
347 local mac_1="${macaddr%%:*}"
348 local mac_2="${macaddr#*:}"
350 config_get vif_mac "$vif" macaddr
351 [ -n "$vif_mac" ] || {
352 if [ "$macidx" -gt 0 ]; then
353 offset="$(( 2 + $macidx * 4 ))"
357 vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
358 macidx="$(($macidx + 1))"
360 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
361 config_set "$vif" macaddr "$vif_mac"
365 # ALL ap functionality will be passed to hostapd
369 # ALL station functionality will be passed to wpa_supplicant
371 if [ ! "$mode" = "ap" ]; then
372 # We attempt to set the channel for all interfaces, although
373 # mac80211 may not support it or the driver might not yet
374 # for ap mode this is handled by hostapd
375 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
378 config_get vif_txpower "$vif" txpower
379 # use vif_txpower (from wifi-iface) to override txpower (from
380 # wifi-device) if the latter doesn't exist
381 txpower="${txpower:-$vif_txpower}"
382 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
386 rm -f /var/run/hostapd-$phy.conf
388 config_get mode "$vif" mode
389 [ "$mode" = "ap" ] || continue
390 mac80211_hostapd_setup_bss "$phy" "$vif"
394 [ -n "$start_hostapd" ] && {
395 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
396 echo "Failed to start hostapd for $phy"
402 config_get mode "$vif" mode
403 config_get ifname "$vif" ifname
404 [ "$mode" = "ap" ] || continue
405 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
406 mac80211_start_vif "$vif" "$ifname"
411 config_get mode "$vif" mode
412 config_get ifname "$vif" ifname
413 [ ! "$mode" = "ap" ] || continue
414 ifconfig "$ifname" up
416 if [ ! "$mode" = "ap" ]; then
417 ifconfig "$ifname" up
420 config_get bssid "$vif" bssid
421 config_get ssid "$vif" ssid
422 config_get beacon_int "$device" beacon_int
423 config_get basic_rate_list "$device" basic_rate
424 config_get encryption "$vif" encryption
425 config_get key "$vif" key 1
426 config_get mcast_rate "$vif" mcast_rate
429 [ "$encryption" == "wep" ] && {
433 for idx in 1 2 3 4; do
435 config_get ikey "$vif" "key$idx"
438 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
439 [ $idx -eq $key ] && ikey="d:$ikey"
440 append keyspec "$ikey"
444 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
448 local br brval brsub brstr
449 [ -n "$basic_rate_list" ] && {
450 for br in $basic_rate_list; do
451 brval="$(($br / 1000))"
452 brsub="$((($br / 100) % 10))"
453 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
454 [ -n "$brstr" ] && brstr="$brstr,"
460 [ -n "$mcast_rate" ] && {
461 mcval="$(($mcast_rate / 1000))"
462 mcsub="$(( ($mcast_rate / 100) % 10 ))"
463 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
466 config_get htmode "$device" htmode
472 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
473 ${fixed:+fixed-freq} $bssid \
474 ${beacon_int:+beacon-interval $beacon_int} \
475 ${brstr:+basic-rates $brstr} \
476 ${mcval:+mcast-rate $mcval} \
477 ${keyspec:+keys $keyspec}
480 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
481 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
482 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
483 # make sure this wifi interface won't accidentally stay open without encryption
484 ifconfig "$ifname" down
490 mac80211_start_vif "$vif" "$ifname"
497 check_mac80211_device() {
498 config_get phy "$1" phy
500 find_mac80211_phy "$1" >/dev/null || return 0
501 config_get phy "$1" phy
503 [ "$phy" = "$dev" ] && found=1
510 config_get type "radio$devidx" type
511 [ -n "$type" ] || break
512 devidx=$(($devidx + 1))
514 for dev in $(ls /sys/class/ieee80211); do
516 config_foreach check_mac80211_device wifi-device
517 [ "$found" -gt 0 ] && continue
523 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
524 ht_cap="$(($ht_cap | $cap))"
527 [ "$ht_cap" -gt 0 ] && {
529 append ht_capab " option htmode HT20" "$N"
531 list=" list ht_capab"
532 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
533 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
534 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
535 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
536 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
537 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
538 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
539 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
540 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
542 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
545 config wifi-device radio$devidx
547 option channel ${channel}
548 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
549 option hwmode 11${mode_11n}${mode_band}
551 # REMOVE THIS LINE TO ENABLE WIFI:
555 option device radio$devidx
559 option encryption none
562 devidx=$(($devidx + 1))