2 append DRIVERS "mac80211"
4 mac80211_hostapd_setup_base() {
8 cfgfile="/var/run/hostapd-$phy.conf"
10 config_get device "$vif" device
11 config_get country "$device" country
12 config_get hwmode "$device" hwmode
13 config_get channel "$device" channel
14 config_get beacon_int "$device" beacon_int
15 config_get basic_rate_list "$device" basic_rate
16 config_get_bool noscan "$device" noscan
18 hostapd_set_log_options base_cfg "$device"
20 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
22 [ "$channel" = auto ] && {
23 channel=$(iw phy "$phy" info | \
24 sed -ne '/MHz/ { /disabled\|passive\|radar/d; s/.*\[//; s/\].*//; p; q }')
25 config_set "$device" channel "$channel"
29 config_get hwmode_11n "$device" hwmode_11n
30 [ -n "$hwmode_11n" ] && {
32 append base_cfg "ieee80211n=1" "$N"
33 config_get htmode "$device" htmode
34 config_get ht_capab_list "$device" ht_capab
36 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
39 for cap in $ht_capab_list; do
40 ht_capab="$ht_capab[$cap]"
42 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
47 [ -n "$country" ] && country_ie=1
48 config_get_bool country_ie "$device" country_ie "$country_ie"
49 [ "$country_ie" -gt 0 ] && append base_cfg "ieee80211d=1" "$N"
52 [ -n "$basic_rate_list" ] && {
53 for br in $basic_rate_list; do
54 brval="$(($br / 100))"
55 [ -n "$brstr" ] && brstr="$brstr "
60 cat >> "$cfgfile" <<EOF
61 ctrl_interface=/var/run/hostapd-$phy
66 wmm_ac_bk_txop_limit=0
71 wmm_ac_be_txop_limit=0
76 wmm_ac_vi_txop_limit=94
81 wmm_ac_vo_txop_limit=47
84 tx_queue_data3_cwmin=15
85 tx_queue_data3_cwmax=1023
86 tx_queue_data3_burst=0
88 tx_queue_data2_cwmin=15
89 tx_queue_data2_cwmax=63
90 tx_queue_data2_burst=0
92 tx_queue_data1_cwmin=7
93 tx_queue_data1_cwmax=15
94 tx_queue_data1_burst=3.0
96 tx_queue_data0_cwmin=3
97 tx_queue_data0_cwmax=7
98 tx_queue_data0_burst=1.5
99 ${hwmode:+hw_mode=$hwmode}
100 ${channel:+channel=$channel}
101 ${beacon_int:+beacon_int=$beacon_int}
102 ${country:+country_code=$country}
103 ${noscan:+noscan=$noscan}
104 ${brstr:+basic_rates=$brstr}
110 mac80211_hostapd_setup_bss() {
115 cfgfile="/var/run/hostapd-$phy.conf"
116 config_get ifname "$vif" ifname
118 if [ -f "$cfgfile" ]; then
119 append hostapd_cfg "bss=$ifname" "$N"
121 mac80211_hostapd_setup_base "$phy" "$ifname"
122 append hostapd_cfg "interface=$ifname" "$N"
126 net_cfg="$(find_net_config "$vif")"
127 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
128 config_set "$vif" bridge "$bridge"
130 hostapd_set_bss_options hostapd_cfg "$vif"
132 config_get_bool wds "$vif" wds 0
133 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
135 local macaddr hidden maxassoc wmm
136 config_get macaddr "$vif" macaddr
137 config_get maxassoc "$vif" maxassoc
138 config_get dtim_period "$vif" dtim_period
139 config_get max_listen_int "$vif" max_listen_int
140 config_get_bool hidden "$vif" hidden 0
141 config_get_bool wmm "$vif" wmm 1
142 cat >> /var/run/hostapd-$phy.conf <<EOF
146 ignore_broadcast_ssid=$hidden
147 ${dtim_period:+dtim_period=$dtim_period}
148 ${max_listen_int:+max_listen_interval=$max_listen_int}
149 ${maxassoc:+max_num_sta=$maxassoc}
153 mac80211_start_vif() {
158 net_cfg="$(find_net_config "$vif")"
159 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
161 set_wifi_up "$vif" "$ifname"
166 [ -d /sys/class/ieee80211/$phy ] && return
170 config_get devpath "$device" path
171 [ -n "$devpath" -a -d "/sys/devices/$devpath/ieee80211" ] && {
172 phy="$(ls /sys/devices/$devpath/ieee80211 | grep -m 1 phy)"
173 [ -n "$phy" ] && return
176 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
177 [ -n "$macaddr" ] && {
178 for _phy in $(ls /sys/class/ieee80211 2>/dev/null); do
179 [ "$macaddr" = "$(cat /sys/class/ieee80211/${_phy}/macaddress)" ] || continue
188 find_mac80211_phy() {
191 config_get phy "$device" phy
193 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
194 echo "PHY for wifi device $1 not found"
197 config_set "$device" phy "$phy"
199 config_get macaddr "$device" macaddr
200 [ -z "$macaddr" ] && {
201 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
209 local adhoc sta ap monitor mesh disabled
211 config_get vifs "$device" vifs
213 config_get_bool disabled "$vif" disabled 0
214 [ $disabled = 0 ] || continue
216 config_get mode "$vif" mode
218 adhoc|sta|ap|monitor|mesh)
221 *) echo "$device($vif): Invalid mode, ignored."; continue;;
225 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
228 list_phy_interfaces() {
230 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
231 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
233 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
240 find_mac80211_phy "$device" || return 0
241 config_get phy "$device" phy
243 set_wifi_down "$device"
244 # kill all running hostapd and wpa_supplicant processes that
245 # are running on atheros/mac80211 vifs
246 for pid in `pidof hostapd`; do
247 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
252 for wdev in $(list_phy_interfaces "$phy"); do
253 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
254 for pid in `pidof wpa_supplicant`; do
255 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
258 ifconfig "$wdev" down 2>/dev/null
269 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
272 mac80211_generate_mac() {
277 [ "$mask" = "00:00:00:00:00:00" ] && mask="ff:ff:ff:ff:ff:ff";
278 local oIFS="$IFS"; IFS=":"; set -- $mask; IFS="$oIFS"
283 local oIFS="$IFS"; IFS=":"; set -- $ref; IFS="$oIFS"
284 [ "$((0x$mask1))" -gt 0 ] && {
287 b1=$((($b1 | 0x2) ^ (($id - 1) << 2)))
288 printf "%02x:%s:%s:%s:%s:%s" $b1 $2 $3 $4 $5 $6
292 [ "$((0x$mask6))" -lt 255 ] && {
293 printf "%s:%s:%s:%s:%s:%02x" $1 $2 $3 $4 $5 $(( 0x$6 ^ $id ))
297 off2=$(( (0x$6 + $id) / 0x100 ))
298 printf "%s:%s:%s:%s:%02x:%02x" \
300 $(( (0x$5 + $off2) % 0x100 )) \
301 $(( (0x$6 + $id) % 0x100 ))
306 config_get channel "$device" channel
307 config_get vifs "$device" vifs
308 config_get txpower "$device" txpower
309 config_get country "$device" country
310 config_get distance "$device" distance
311 config_get txantenna "$device" txantenna all
312 config_get rxantenna "$device" rxantenna all
313 config_get antenna_gain "$device" antenna_gain 0
314 config_get frag "$device" frag
315 config_get rts "$device" rts
316 find_mac80211_phy "$device" || return 0
317 config_get phy "$device" phy
322 local hostapd_ctrl=""
324 [ -n "$country" ] && {
325 iw reg get | grep -q "^country $country:" || {
326 iw reg set "$country"
331 config_get chanbw "$device" chanbw
332 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath9k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath9k/chanbw
333 [ -n "$chanbw" -a -d /sys/kernel/debug/ieee80211/$phy/ath5k ] && echo "$chanbw" > /sys/kernel/debug/ieee80211/$phy/ath5k/bwmode
335 [ "$channel" = "auto" -o "$channel" = "0" ] || {
339 iw phy "$phy" set antenna $txantenna $rxantenna >/dev/null 2>&1
340 iw phy "$phy" set antenna_gain $antenna_gain
342 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
343 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
344 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
347 # convert channel to frequency
348 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
350 wifi_fixup_hwmode "$device" "g"
352 config_get ifname "$vif" ifname
353 [ -n "$ifname" ] || {
354 [ $i -gt 0 ] && ifname="wlan${phy#phy}-$i" || ifname="wlan${phy#phy}"
356 config_set "$vif" ifname "$ifname"
358 config_get mode "$vif" mode
359 config_get ssid "$vif" ssid
361 # It is far easier to delete and create the desired interface
364 iw phy "$phy" interface add "$ifname" type adhoc
367 # Hostapd will handle recreating the interface and
368 # it's accompanying monitor
369 apidx="$(($apidx + 1))"
370 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
373 config_get mesh_id "$vif" mesh_id
374 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
377 iw phy "$phy" interface add "$ifname" type monitor
381 config_get_bool wds "$vif" wds 0
382 [ "$wds" -gt 0 ] && wdsflag="4addr on"
383 iw phy "$phy" interface add "$ifname" type managed $wdsflag
384 config_get_bool powersave "$vif" powersave 0
385 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
386 iw "$ifname" set power_save "$powersave"
390 # All interfaces must have unique mac addresses
391 # which can either be explicitly set in the device
392 # section, or automatically generated
393 config_get macaddr "$device" macaddr
394 config_get vif_mac "$vif" macaddr
395 [ -n "$vif_mac" ] || {
396 vif_mac="$(mac80211_generate_mac $macidx $macaddr $(cat /sys/class/ieee80211/${phy}/address_mask))"
397 macidx="$(($macidx + 1))"
399 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
400 config_set "$vif" macaddr "$vif_mac"
404 # ALL ap functionality will be passed to hostapd
408 # ALL station functionality will be passed to wpa_supplicant
410 if [ ! "$mode" = "ap" ]; then
411 # We attempt to set the channel for all interfaces, although
412 # mac80211 may not support it or the driver might not yet
413 # for ap mode this is handled by hostapd
414 config_get htmode "$device" htmode
419 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel" $htmode
426 rm -f /var/run/hostapd-$phy.conf
428 config_get mode "$vif" mode
429 [ "$mode" = "ap" ] || continue
430 mac80211_hostapd_setup_bss "$phy" "$vif"
434 [ -n "$start_hostapd" ] && {
435 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
436 echo "Failed to start hostapd for $phy"
442 config_get mode "$vif" mode
443 config_get ifname "$vif" ifname
444 [ "$mode" = "ap" ] || continue
445 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
446 mac80211_start_vif "$vif" "$ifname"
451 config_get mode "$vif" mode
452 config_get ifname "$vif" ifname
453 [ "$mode" = "ap" ] || ifconfig "$ifname" up
455 config_get vif_txpower "$vif" txpower
456 # use vif_txpower (from wifi-iface) to override txpower (from
457 # wifi-device) if the latter doesn't exist
458 txpower="${txpower:-$vif_txpower}"
459 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
463 config_get bssid "$vif" bssid
464 config_get ssid "$vif" ssid
465 config_get beacon_int "$device" beacon_int
466 config_get basic_rate_list "$device" basic_rate
467 config_get encryption "$vif" encryption
468 config_get key "$vif" key 1
469 config_get mcast_rate "$vif" mcast_rate
470 config_get htmode "$device" htmode
478 [ "$encryption" == "psk" -o "$encryption" == "psk2" ] && {
479 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
480 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" $freq $htmode || {
481 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
482 # make sure this wifi interface won't accidentally stay open without encryption
483 ifconfig "$ifname" down
485 mac80211_start_vif "$vif" "$ifname"
490 [ "$encryption" == "wep" ] && {
494 for idx in 1 2 3 4; do
496 config_get ikey "$vif" "key$idx"
499 ikey="$(($idx - 1)):$(prepare_key_wep "$ikey")"
500 [ $idx -eq $key ] && ikey="d:$ikey"
501 append keyspec "$ikey"
505 *) append keyspec "d:0:$(prepare_key_wep "$key")" ;;
509 local br brval brsub brstr
510 [ -n "$basic_rate_list" ] && {
511 for br in $basic_rate_list; do
512 brval="$(($br / 1000))"
513 brsub="$((($br / 100) % 10))"
514 [ "$brsub" -gt 0 ] && brval="$brval.$brsub"
515 [ -n "$brstr" ] && brstr="$brstr,"
521 [ -n "$mcast_rate" ] && {
522 mcval="$(($mcast_rate / 1000))"
523 mcsub="$(( ($mcast_rate / 100) % 10 ))"
524 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
527 iw dev "$ifname" ibss join "$ssid" $freq $htmode \
528 ${fixed:+fixed-freq} $bssid \
529 ${beacon_int:+beacon-interval $beacon_int} \
530 ${brstr:+basic-rates $brstr} \
531 ${mcval:+mcast-rate $mcval} \
532 ${keyspec:+keys $keyspec}
535 mp_list="mesh_retry_timeout mesh_confirm_timeout mesh_holding_timeout mesh_max_peer_links
536 mesh_max_retries mesh_ttl mesh_element_ttl mesh_auto_open_plinks mesh_hwmp_max_preq_retries
537 mesh_path_refresh_time mesh_min_discovery_timeout mesh_hwmp_active_path_timeout
538 mesh_hwmp_preq_min_interval mesh_hwmp_net_diameter_traversal_time mesh_hwmp_rootmode
539 mesh_hwmp_rann_interval mesh_gate_announcements mesh_fwding mesh_sync_offset_max_neighor
540 mesh_rssi_threshold mesh_hwmp_active_path_to_root_timeout mesh_hwmp_root_interval
541 mesh_hwmp_confirmation_interval mesh_power_mode mesh_awake_window"
544 config_get mp_val "$vif" "$mp" ""
545 [ -n "$mp_val" ] && iw dev "$ifname" set mesh_param "$mp" "$mp_val"
549 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
550 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
551 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
552 # make sure this wifi interface won't accidentally stay open without encryption
553 ifconfig "$ifname" down
559 [ "$mode" = "ap" ] || mac80211_start_vif "$vif" "$ifname"
565 check_mac80211_device() {
566 config_get phy "$1" phy
568 find_mac80211_phy "$1" >/dev/null || return 0
569 config_get phy "$1" phy
571 [ "$phy" = "$dev" ] && found=1
578 config_get type "radio$devidx" type
579 [ -n "$type" ] || break
580 devidx=$(($devidx + 1))
582 for dev in $(ls /sys/class/ieee80211); do
584 config_foreach check_mac80211_device wifi-device
585 [ "$found" -gt 0 ] && continue
591 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
592 ht_cap="$(($ht_cap | $cap))"
595 [ "$ht_cap" -gt 0 ] && {
597 append ht_capab " option htmode HT20" "$N"
599 list=" list ht_capab"
600 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
601 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
602 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
603 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
604 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
605 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
606 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
607 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
608 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
610 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
612 if [ -x /usr/bin/readlink ]; then
613 path="$(readlink -f /sys/class/ieee80211/${dev}/device)"
614 path="${path##/sys/devices/}"
615 dev_id=" option path '$path'"
617 dev_id=" option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)"
621 config wifi-device radio$devidx
623 option channel ${channel}
624 option hwmode 11${mode_11n}${mode_band}
627 # REMOVE THIS LINE TO ENABLE WIFI:
631 option device radio$devidx
635 option encryption none
638 devidx=$(($devidx + 1))