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 config_get_bool short_preamble "$device" short_preamble "0"
21 hostapd_set_log_options base_cfg "$device"
23 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
25 [ "$channel" = auto ] && {
26 channel=$(iw phy "$phy" info | \
27 sed -ne '/MHz/ { /disabled\|passive\|radar/d; s/.*\[//; s/\].*//; p; q }')
28 config_set "$device" channel "$channel"
32 config_get hwmode_11n "$device" hwmode_11n
33 [ -n "$hwmode_11n" ] && {
35 append base_cfg "ieee80211n=1" "$N"
36 config_get htmode "$device" htmode
37 config_get ht_capab_list "$device" ht_capab
39 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
42 for cap in $ht_capab_list; do
43 ht_capab="$ht_capab[$cap]"
45 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
50 [ -n "$country" ] && country_ie=1
51 config_get_bool country_ie "$device" country_ie "$country_ie"
52 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
54 config_get macfilter "$vif" macfilter
57 append base_cfg "macaddr_acl=1" "$N"
58 append base_cfg "accept_mac_file=$macfile" "$N"
61 append base_cfg "macaddr_acl=0" "$N"
62 append base_cfg "deny_mac_file=$macfile" "$N"
65 config_get maclist "$vif" maclist
66 [ -n "$maclist" ] && {
67 for mac in $maclist; do
68 echo "$mac" >> $macfile
73 [ -n "$basic_rate_list" ] && {
74 for br in $basic_rate_list; do
75 brval="$(($br / 100))"
76 [ -n "$brstr" ] && brstr="$brstr "
81 append base_cfg "preamble=$short_preamble" "$N"
83 cat >> "$cfgfile" <<EOF
84 ctrl_interface=/var/run/hostapd-$phy
89 wmm_ac_bk_txop_limit=0
94 wmm_ac_be_txop_limit=0
99 wmm_ac_vi_txop_limit=94
104 wmm_ac_vo_txop_limit=47
106 tx_queue_data3_aifs=7
107 tx_queue_data3_cwmin=15
108 tx_queue_data3_cwmax=1023
109 tx_queue_data3_burst=0
110 tx_queue_data2_aifs=3
111 tx_queue_data2_cwmin=15
112 tx_queue_data2_cwmax=63
113 tx_queue_data2_burst=0
114 tx_queue_data1_aifs=1
115 tx_queue_data1_cwmin=7
116 tx_queue_data1_cwmax=15
117 tx_queue_data1_burst=3.0
118 tx_queue_data0_aifs=1
119 tx_queue_data0_cwmin=3
120 tx_queue_data0_cwmax=7
121 tx_queue_data0_burst=1.5
122 ${hwmode:+hw_mode=$hwmode}
123 ${channel:+channel=$channel}
124 ${beacon_int:+beacon_int=$beacon_int}
125 ${country:+country_code=$country}
126 ${noscan:+noscan=$noscan}
127 ${brstr:+basic_rates=$brstr}
133 mac80211_hostapd_setup_bss() {
138 cfgfile="/var/run/hostapd-$phy.conf"
139 config_get ifname "$vif" ifname
141 if [ -f "$cfgfile" ]; then
142 append hostapd_cfg "bss=$ifname" "$N"
144 mac80211_hostapd_setup_base "$phy" "$ifname"
145 append hostapd_cfg "interface=$ifname" "$N"
149 net_cfg="$(find_net_config "$vif")"
150 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
151 config_set "$vif" bridge "$bridge"
153 hostapd_set_bss_options hostapd_cfg "$vif"
155 config_get_bool wds "$vif" wds 0
156 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
158 local macaddr hidden maxassoc wmm
159 config_get macaddr "$vif" macaddr
160 config_get maxassoc "$vif" maxassoc
161 config_get dtim_period "$vif" dtim_period
162 config_get max_listen_int "$vif" max_listen_int
163 config_get_bool hidden "$vif" hidden 0
164 config_get_bool wmm "$vif" wmm 1
165 cat >> /var/run/hostapd-$phy.conf <<EOF
169 ignore_broadcast_ssid=$hidden
170 ${dtim_period:+dtim_period=$dtim_period}
171 ${max_listen_int:+max_listen_interval=$max_listen_int}
172 ${maxassoc:+max_num_sta=$maxassoc}
176 mac80211_start_vif() {
181 net_cfg="$(find_net_config "$vif")"
182 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
184 set_wifi_up "$vif" "$ifname"
187 find_mac80211_phy() {
190 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
191 config_get phy "$device" phy
192 [ -z "$phy" -a -n "$macaddr" ] && {
193 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
194 [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
195 config_set "$device" phy "$phy"
198 config_get phy "$device" phy
200 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
201 echo "PHY for wifi device $1 not found"
204 [ -z "$macaddr" ] && {
205 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
212 local adhoc sta ap monitor mesh disabled
214 config_get vifs "$device" vifs
216 config_get_bool disabled "$vif" disabled 0
217 [ $disabled = 0 ] || continue
219 config_get mode "$vif" mode
221 adhoc|sta|ap|monitor|mesh)
224 *) echo "$device($vif): Invalid mode, ignored."; continue;;
228 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
231 list_phy_interfaces() {
233 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
234 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
236 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
243 find_mac80211_phy "$device" || return 0
244 config_get phy "$device" phy
246 set_wifi_down "$device"
247 # kill all running hostapd and wpa_supplicant processes that
248 # are running on atheros/mac80211 vifs
249 for pid in `pidof hostapd`; do
250 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
255 for wdev in $(list_phy_interfaces "$phy"); do
256 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
257 for pid in `pidof wpa_supplicant`; do
258 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
261 ifconfig "$wdev" down 2>/dev/null
272 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
275 mac80211_generate_mac() {
280 [ "$mask" = "00:00:00:00:00:00" ] && mask="ff:ff:ff:ff:ff:ff";
281 local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
286 local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
287 [ "$((0x$mask1))" -gt 0 ] && {
290 b1=$((($b1 | 0x2) ^ (($id - 1) << 2)))
291 printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
295 [ "$((0x$mask6))" -lt 255 ] && {
296 printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
300 off2=$(( (0x$6 + $id) / 0x100 ))
301 printf "%s:%s:%s:%s:%02x:%02x" \
303 $(( (0x$5 + $off2) % 0x100 )) \
304 $(( (0x$6 + $id) % 0x100 ))
309 config_get channel "$device" channel
310 config_get vifs "$device" vifs
311 config_get txpower "$device" txpower
312 config_get country "$device" country
313 config_get distance "$device" distance
314 config_get txantenna "$device" txantenna all
315 config_get rxantenna "$device" rxantenna all
316 config_get antenna_gain "$device" antenna_gain 0
317 config_get frag "$device" frag
318 config_get rts "$device" rts
319 find_mac80211_phy "$device" || return 0
320 config_get phy "$device" phy
325 local hostapd_ctrl=""
327 [ -n "$country" ] && {
328 iw reg get | grep -q "^country $country:" || {
329 iw reg set "$country"
334 config_get chanbw "$device" chanbw
335 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath9k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw
336 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath5k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode
338 [ "$channel" = "auto" -o "$channel" = "0" ] || {
342 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
343 iw phy "$phy" set antenna_gain $antenna_gain
345 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
346 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
347 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
350 # convert channel to frequency
351 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
353 wifi_fixup_hwmode "$device" "g"
355 config_get ifname "$vif" ifname
356 [ -n "$ifname" ] || {
357 [ $i -gt 0 ] && ifname="wlan${phy#phy}-$i" || ifname="wlan${phy#phy}"
359 config_set "$vif" ifname "$ifname"
361 config_get mode "$vif" mode
362 config_get ssid "$vif" ssid
364 # It is far easier to delete and create the desired interface
367 iw phy "$phy" interface add "$ifname" type adhoc
370 # Hostapd will handle recreating the interface and
371 # it's accompanying monitor
372 apidx="$(($apidx + 1))"
373 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
376 config_get mesh_id "$vif" mesh_id
377 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
380 iw phy "$phy" interface add "$ifname" type monitor
384 config_get_bool wds "$vif" wds 0
385 [ "$wds" -gt 0 ] && wdsflag="4addr on"
386 iw phy "$phy" interface add "$ifname" type managed $wdsflag
387 config_get_bool powersave "$vif" powersave 0
388 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
389 iw "$ifname" set power_save "$powersave"
393 # All interfaces must have unique mac addresses
394 # which can either be explicitly set in the device
395 # section, or automatically generated
396 config_get macaddr "$device" macaddr
397 config_get vif_mac "$vif" macaddr
398 [ -n "$vif_mac" ] || {
399 vif_mac="$(mac80211_generate_mac $macidx $macaddr $(cat /sys/class/ieee80211/${phy}/address_mask))"
400 macidx="$(($macidx + 1))"
402 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
403 config_set "$vif" macaddr "$vif_mac"
407 # ALL ap functionality will be passed to hostapd
411 # ALL station functionality will be passed to wpa_supplicant
413 if [ ! "$mode" = "ap" ]; then
414 # We attempt to set the channel for all interfaces, although
415 # mac80211 may not support it or the driver might not yet
416 # for ap mode this is handled by hostapd
417 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
424 rm -f /var/run/hostapd-$phy.conf
426 config_get mode "$vif" mode
427 [ "$mode" = "ap" ] || continue
428 mac80211_hostapd_setup_bss "$phy" "$vif"
432 [ -n "$start_hostapd" ] && {
433 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
434 echo "Failed to start hostapd for $phy"
440 config_get mode "$vif" mode
441 config_get ifname "$vif" ifname
442 [ "$mode" = "ap" ] || continue
443 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
444 mac80211_start_vif "$vif" "$ifname"
449 config_get mode "$vif" mode
450 config_get ifname "$vif" ifname
451 [ "$mode" = "ap" ] || ifconfig "$ifname" up
453 config_get vif_txpower "$vif" txpower
454 # use vif_txpower (from wifi-iface) to override txpower (from
455 # wifi-device) if the latter doesn't exist
456 txpower="${txpower:-$vif_txpower}"
457 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
461 config_get bssid "$vif" bssid
462 config_get ssid "$vif" ssid
463 config_get beacon_int "$device" beacon_int
464 config_get basic_rate_list "$device" basic_rate
465 config_get encryption "$vif" encryption
466 config_get key "$vif" key 1
467 config_get mcast_rate "$vif" mcast_rate
468 config_get htmode "$device" htmode
476 [ "$encryption" == "psk" -o "$encryption" == "psk2" ] && {
477 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
478 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" $freq $htmode || {
479 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
480 # make sure this wifi interface won't accidentally stay open without encryption
481 ifconfig "$ifname" down
483 mac80211_start_vif "$vif" "$ifname"
488 [ "$encryption" == "wep" ] && {
492 for idx in 1 2 3 4; do
494 config_get ikey "$vif" "key$idx"
497 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
498 [ $idx -eq $key ] && ikey="d:$ikey"
499 append keyspec "$ikey"
503 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
507 local br brval brsub brstr
508 [ -n "$basic_rate_list" ] && {
509 for br in $basic_rate_list; do
510 brval="$(($br / 1000))"
511 brsub="$((($br / 100) % 10))"
512 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
513 [ -n "$brstr" ] && brstr="$brstr,"
519 [ -n "$mcast_rate" ] && {
520 mcval="$(($mcast_rate / 1000))"
521 mcsub="$(( ($mcast_rate / 100) % 10 ))"
522 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
525 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
526 ${fixed:+fixed-freq} $bssid \
527 ${beacon_int:+beacon-interval $beacon_int} \
528 ${brstr:+basic-rates $brstr} \
529 ${mcval:+mcast-rate $mcval} \
530 ${keyspec:+keys $keyspec}
533 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
534 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
535 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
536 # make sure this wifi interface won't accidentally stay open without encryption
537 ifconfig "$ifname" down
543 [ "$mode" = "ap" ] || mac80211_start_vif "$vif" "$ifname"
549 check_mac80211_device() {
550 config_get phy "$1" phy
552 find_mac80211_phy "$1" >/dev/null || return 0
553 config_get phy "$1" phy
555 [ "$phy" = "$dev" ] && found=1
562 config_get type "radio$devidx" type
563 [ -n "$type" ] || break
564 devidx=$(($devidx + 1))
566 for dev in $(ls /sys/class/ieee80211); do
568 config_foreach check_mac80211_device wifi-device
569 [ "$found" -gt 0 ] && continue
575 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
576 ht_cap="$(($ht_cap | $cap))"
579 [ "$ht_cap" -gt 0 ] && {
581 append ht_capab " option htmode HT20" "$N"
583 list=" list ht_capab"
584 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
585 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
586 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
587 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
588 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
589 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
590 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
591 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
592 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
594 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
597 config wifi-device radio$devidx
599 option channel ${channel}
600 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
601 option hwmode 11${mode_11n}${mode_band}
603 # REMOVE THIS LINE TO ENABLE WIFI:
607 option device radio$devidx
611 option encryption none
614 devidx=$(($devidx + 1))