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"
39 config_get macfilter "$vif" macfilter
42 append base_cfg "macaddr_acl=1" "$N"
43 append base_cfg "accept_mac_file=$macfile" "$N"
46 append base_cfg "macaddr_acl=0" "$N"
47 append base_cfg "deny_mac_file=$macfile" "$N"
50 config_get maclist "$vif" maclist
51 [ -n "$maclist" ] && {
52 for mac in $maclist; do
53 echo "$mac" >> $macfile
58 [ -n "$basic_rate_list" ] && {
59 for br in $basic_rate_list; do
60 brval="$(($br / 100))"
61 [ -n "$brstr" ] && brstr="$brstr "
66 cat >> "$cfgfile" <<EOF
67 ctrl_interface=/var/run/hostapd-$phy
72 wmm_ac_bk_txop_limit=0
77 wmm_ac_be_txop_limit=0
82 wmm_ac_vi_txop_limit=94
87 wmm_ac_vo_txop_limit=47
90 tx_queue_data3_cwmin=15
91 tx_queue_data3_cwmax=1023
92 tx_queue_data3_burst=0
94 tx_queue_data2_cwmin=15
95 tx_queue_data2_cwmax=63
96 tx_queue_data2_burst=0
98 tx_queue_data1_cwmin=7
99 tx_queue_data1_cwmax=15
100 tx_queue_data1_burst=3.0
101 tx_queue_data0_aifs=1
102 tx_queue_data0_cwmin=3
103 tx_queue_data0_cwmax=7
104 tx_queue_data0_burst=1.5
105 ${hwmode:+hw_mode=$hwmode}
106 ${channel:+channel=$channel}
107 ${beacon_int:+beacon_int=$beacon_int}
108 ${country:+country_code=$country}
109 ${noscan:+noscan=$noscan}
110 ${brstr:+basic_rates=$brstr}
116 mac80211_hostapd_setup_bss() {
121 cfgfile="/var/run/hostapd-$phy.conf"
122 config_get ifname "$vif" ifname
124 if [ -f "$cfgfile" ]; then
125 append hostapd_cfg "bss=$ifname" "$N"
127 mac80211_hostapd_setup_base "$phy" "$ifname"
128 append hostapd_cfg "interface=$ifname" "$N"
132 net_cfg="$(find_net_config "$vif")"
133 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
134 config_set "$vif" bridge "$bridge"
136 hostapd_set_bss_options hostapd_cfg "$vif"
138 config_get_bool wds "$vif" wds 0
139 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
141 local macaddr hidden maxassoc wmm
142 config_get macaddr "$vif" macaddr
143 config_get maxassoc "$vif" maxassoc
144 config_get dtim_period "$vif" dtim_period
145 config_get max_listen_int "$vif" max_listen_int
146 config_get_bool hidden "$vif" hidden 0
147 config_get_bool wmm "$vif" wmm 1
148 cat >> /var/run/hostapd-$phy.conf <<EOF
152 ignore_broadcast_ssid=$hidden
153 ${dtim_period:+dtim_period=$dtim_period}
154 ${max_listen_int:+max_listen_interval=$max_listen_int}
155 ${maxassoc:+max_num_sta=$maxassoc}
159 mac80211_start_vif() {
164 net_cfg="$(find_net_config "$vif")"
165 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
167 set_wifi_up "$vif" "$ifname"
170 find_mac80211_phy() {
173 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
174 config_get phy "$device" phy
175 [ -z "$phy" -a -n "$macaddr" ] && {
176 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
177 [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
178 config_set "$device" phy "$phy"
181 config_get phy "$device" phy
183 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
184 echo "PHY for wifi device $1 not found"
187 [ -z "$macaddr" ] && {
188 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
195 local adhoc sta ap monitor mesh
197 config_get vifs "$device" vifs
199 config_get mode "$vif" mode
201 adhoc|sta|ap|monitor|mesh)
204 *) echo "$device($vif): Invalid mode, ignored."; continue;;
208 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
211 list_phy_interfaces() {
213 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
214 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
216 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
223 find_mac80211_phy "$device" || return 0
224 config_get phy "$device" phy
226 set_wifi_down "$device"
227 # kill all running hostapd and wpa_supplicant processes that
228 # are running on atheros/mac80211 vifs
229 for pid in `pidof hostapd`; do
230 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
235 for wdev in $(list_phy_interfaces "$phy"); do
236 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
237 for pid in `pidof wpa_supplicant`; do
238 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
241 ifconfig "$wdev" down 2>/dev/null
251 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
255 config_get channel "$device" channel
256 config_get vifs "$device" vifs
257 config_get txpower "$device" txpower
258 config_get country "$device" country
259 config_get distance "$device" distance
260 config_get txantenna "$device" txantenna all
261 config_get rxantenna "$device" rxantenna all
262 config_get frag "$device" frag
263 config_get rts "$device" rts
264 find_mac80211_phy "$device" || return 0
265 config_get phy "$device" phy
270 local hostapd_ctrl=""
272 [ -n "$country" ] && iw reg set "$country"
273 [ "$channel" = "auto" -o "$channel" = "0" ] || {
277 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
279 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
280 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
281 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
284 # convert channel to frequency
285 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
287 wifi_fixup_hwmode "$device" "g"
289 while [ -d "/sys/class/net/wlan$i" ]; do
293 config_get ifname "$vif" ifname
294 [ -n "$ifname" ] || {
297 config_set "$vif" ifname "$ifname"
299 config_get mode "$vif" mode
300 config_get ssid "$vif" ssid
302 # It is far easier to delete and create the desired interface
305 iw phy "$phy" interface add "$ifname" type adhoc
308 # Hostapd will handle recreating the interface and
309 # it's accompanying monitor
310 apidx="$(($apidx + 1))"
312 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
315 config_get mesh_id "$vif" mesh_id
316 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
319 iw phy "$phy" interface add "$ifname" type monitor
323 config_get_bool wds "$vif" wds 0
324 [ "$wds" -gt 0 ] && wdsflag="4addr on"
325 iw phy "$phy" interface add "$ifname" type managed $wdsflag
326 config_get_bool powersave "$vif" powersave 0
327 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
328 iwconfig "$ifname" power "$powersave"
332 # All interfaces must have unique mac addresses
333 # which can either be explicitly set in the device
334 # section, or automatically generated
335 config_get macaddr "$device" macaddr
336 local mac_1="${macaddr%%:*}"
337 local mac_2="${macaddr#*:}"
339 config_get vif_mac "$vif" macaddr
340 [ -n "$vif_mac" ] || {
341 if [ "$macidx" -gt 0 ]; then
342 offset="$(( 2 + $macidx * 4 ))"
346 vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
347 macidx="$(($macidx + 1))"
349 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
350 config_set "$vif" macaddr "$vif_mac"
354 # ALL ap functionality will be passed to hostapd
358 # ALL station functionality will be passed to wpa_supplicant
360 if [ ! "$mode" = "ap" ]; then
361 # We attempt to set the channel for all interfaces, although
362 # mac80211 may not support it or the driver might not yet
363 # for ap mode this is handled by hostapd
364 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
367 config_get vif_txpower "$vif" txpower
368 # use vif_txpower (from wifi-iface) to override txpower (from
369 # wifi-device) if the latter doesn't exist
370 txpower="${txpower:-$vif_txpower}"
371 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
375 rm -f /var/run/hostapd-$phy.conf
377 config_get mode "$vif" mode
378 [ "$mode" = "ap" ] || continue
379 mac80211_hostapd_setup_bss "$phy" "$vif"
383 [ -n "$start_hostapd" ] && {
384 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
385 echo "Failed to start hostapd for $phy"
391 config_get mode "$vif" mode
392 config_get ifname "$vif" ifname
393 [ "$mode" = "ap" ] || continue
394 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
395 mac80211_start_vif "$vif" "$ifname"
400 config_get mode "$vif" mode
401 config_get ifname "$vif" ifname
402 [ ! "$mode" = "ap" ] || continue
403 ifconfig "$ifname" up
405 if [ ! "$mode" = "ap" ]; then
406 ifconfig "$ifname" up
409 config_get bssid "$vif" bssid
410 config_get ssid "$vif" ssid
411 config_get beacon_int "$device" beacon_int
412 config_get basic_rate_list "$device" basic_rate
413 config_get encryption "$vif" encryption
414 config_get key "$vif" key 1
415 config_get mcast_rate "$vif" mcast_rate
418 [ "$encryption" == "wep" ] && {
422 for idx in 1 2 3 4; do
424 config_get ikey "$vif" "key$idx"
427 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
428 [ $idx -eq $key ] && ikey="d:$ikey"
429 append keyspec "$ikey"
433 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
437 local br brval brsub brstr
438 [ -n "$basic_rate_list" ] && {
439 for br in $basic_rate_list; do
440 brval="$(($br / 1000))"
441 brsub="$((($br / 100) % 10))"
442 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
443 [ -n "$brstr" ] && brstr="$brstr,"
449 [ -n "$mcast_rate" ] && {
450 mcval="$(($mcast_rate / 1000))"
451 mcsub="$(( ($mcast_rate / 100) % 10 ))"
452 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
455 iw dev "$ifname" ibss join "$ssid" $freq \
456 ${fixed:+fixed-freq} $bssid \
457 ${beacon_int:+beacon-interval $beacon_int} \
458 ${brstr:+basic-rates $brstr} \
459 ${mcval:+mcast-rate $mcval} \
460 ${keyspec:+keys $keyspec}
463 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
464 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
465 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
466 # make sure this wifi interface won't accidentally stay open without encryption
467 ifconfig "$ifname" down
473 mac80211_start_vif "$vif" "$ifname"
481 config_get phy "$1" phy
483 find_mac80211_phy "$1" >/dev/null || return 0
484 config_get phy "$1" phy
486 [ "$phy" = "$dev" ] && found=1
493 config_get type "radio$devidx" type
494 [ -n "$type" ] || break
495 devidx=$(($devidx + 1))
497 for dev in $(ls /sys/class/ieee80211); do
499 config_foreach check_device wifi-device
500 [ "$found" -gt 0 ] && continue
506 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
507 ht_cap="$(($ht_cap | $cap))"
510 [ "$ht_cap" -gt 0 ] && {
512 append ht_capab " option htmode HT20" "$N"
514 list=" list ht_capab"
515 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
516 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
517 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
518 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
519 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
520 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
521 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
522 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
523 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
525 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
528 config wifi-device radio$devidx
530 option channel ${channel}
531 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
532 option hwmode 11${mode_11n}${mode_band}
534 # REMOVE THIS LINE TO ENABLE WIFI:
538 option device radio$devidx
542 option encryption none
545 devidx=$(($devidx + 1))