mac80211: Improve ath5k/ar71xx PCI bug WAR
[openwrt/svn-archive/archive.git] / package / mac80211 / files / lib / wifi / mac80211.sh
1 #!/bin/sh
2 append DRIVERS "mac80211"
3
4 mac80211_hostapd_setup_base() {
5 local phy="$1"
6 local ifname="$2"
7
8 cfgfile="/var/run/hostapd-$phy.conf"
9 config_get device "$vif" device
10 config_get country "$device" country
11 config_get hwmode "$device" hwmode
12 config_get channel "$device" channel
13 config_get_bool noscan "$device" noscan
14 [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device"
15 [ "$channel" = auto ] && channel=
16 [ -n "$hwmode" ] && {
17 config_get hwmode_11n "$device" hwmode_11n
18 [ -n "$hwmode_11n" ] && {
19 hwmode="$hwmode_11n"
20 append base_cfg "ieee80211n=1" "$N"
21 config_get htmode "$device" htmode
22 config_get ht_capab_list "$device" ht_capab
23 case "$htmode" in
24 HT20|HT40+|HT40-) ht_capab="[$htmode]";;
25 *)ht_capab=;;
26 esac
27 for cap in $ht_capab_list; do
28 ht_capab="$ht_capab[$cap]"
29 done
30 [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N"
31 }
32 }
33 cat > "$cfgfile" <<EOF
34 ctrl_interface=/var/run/hostapd-$phy
35 driver=nl80211
36 wmm_ac_bk_cwmin=4
37 wmm_ac_bk_cwmax=10
38 wmm_ac_bk_aifs=7
39 wmm_ac_bk_txop_limit=0
40 wmm_ac_bk_acm=0
41 wmm_ac_be_aifs=3
42 wmm_ac_be_cwmin=4
43 wmm_ac_be_cwmax=10
44 wmm_ac_be_txop_limit=0
45 wmm_ac_be_acm=0
46 wmm_ac_vi_aifs=2
47 wmm_ac_vi_cwmin=3
48 wmm_ac_vi_cwmax=4
49 wmm_ac_vi_txop_limit=94
50 wmm_ac_vi_acm=0
51 wmm_ac_vo_aifs=2
52 wmm_ac_vo_cwmin=2
53 wmm_ac_vo_cwmax=3
54 wmm_ac_vo_txop_limit=47
55 wmm_ac_vo_acm=0
56 tx_queue_data3_aifs=7
57 tx_queue_data3_cwmin=15
58 tx_queue_data3_cwmax=1023
59 tx_queue_data3_burst=0
60 tx_queue_data2_aifs=3
61 tx_queue_data2_cwmin=15
62 tx_queue_data2_cwmax=63
63 tx_queue_data2_burst=0
64 tx_queue_data1_aifs=1
65 tx_queue_data1_cwmin=7
66 tx_queue_data1_cwmax=15
67 tx_queue_data1_burst=3.0
68 tx_queue_data0_aifs=1
69 tx_queue_data0_cwmin=3
70 tx_queue_data0_cwmax=7
71 tx_queue_data0_burst=1.5
72 ${hwmode:+hw_mode=$hwmode}
73 ${channel:+channel=$channel}
74 ${country:+country_code=$country}
75 ${noscan:+noscan=$noscan}
76 $base_cfg
77
78 EOF
79 }
80
81 mac80211_hostapd_setup_bss() {
82 local phy="$1"
83 local vif="$2"
84
85 hostapd_cfg=
86 cfgfile="/var/run/hostapd-$phy.conf"
87 config_get ifname "$vif" ifname
88
89 if [ -f "$cfgfile" ]; then
90 append hostapd_cfg "bss=$ifname" "$N"
91 else
92 mac80211_hostapd_setup_base "$phy" "$ifname"
93 append hostapd_cfg "interface=$ifname" "$N"
94 fi
95
96 local net_cfg bridge
97 net_cfg="$(find_net_config "$vif")"
98 [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")"
99 config_set "$vif" bridge "$bridge"
100
101 hostapd_set_bss_options hostapd_cfg "$vif"
102
103 config_get_bool wds "$vif" wds 0
104 [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N"
105
106 local macaddr hidden maxassoc wmm
107 config_get macaddr "$vif" macaddr
108 config_get maxassoc "$vif" maxassoc
109 config_get_bool hidden "$vif" hidden 0
110 config_get_bool wmm "$vif" wmm 1
111 cat >> /var/run/hostapd-$phy.conf <<EOF
112 $hostapd_cfg
113 wmm_enabled=$wmm
114 bssid=$macaddr
115 ignore_broadcast_ssid=$hidden
116 ${maxassoc:+max_num_sta=$maxassoc}
117 EOF
118 }
119
120 mac80211_start_vif() {
121 local vif="$1"
122 local ifname="$2"
123
124 local net_cfg
125 net_cfg="$(find_net_config "$vif")"
126 [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg"
127
128 set_wifi_up "$vif" "$ifname"
129 }
130
131 find_mac80211_phy() {
132 local device="$1"
133
134 local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')"
135 config_get phy "$device" phy
136 [ -z "$phy" -a -n "$macaddr" ] && {
137 for phy in $(ls /sys/class/ieee80211 2>/dev/null); do
138 [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue
139 config_set "$device" phy "$phy"
140 break
141 done
142 config_get phy "$device" phy
143 }
144 [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || {
145 echo "PHY for wifi device $1 not found"
146 return 1
147 }
148 [ -z "$macaddr" ] && {
149 config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)"
150 }
151 return 0
152 }
153
154 scan_mac80211() {
155 local device="$1"
156 local adhoc sta ap monitor mesh
157
158 config_get vifs "$device" vifs
159 for vif in $vifs; do
160 config_get mode "$vif" mode
161 case "$mode" in
162 adhoc|sta|ap|monitor|mesh)
163 append $mode "$vif"
164 ;;
165 *) echo "$device($vif): Invalid mode, ignored."; continue;;
166 esac
167 done
168
169 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}"
170 }
171
172 list_phy_interfaces() {
173 local phy="$1"
174 if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then
175 ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null;
176 else
177 ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g'
178 fi
179 }
180
181 disable_mac80211() (
182 local device="$1"
183
184 find_mac80211_phy "$device" || return 0
185 config_get phy "$device" phy
186
187 set_wifi_down "$device"
188 # kill all running hostapd and wpa_supplicant processes that
189 # are running on atheros/mac80211 vifs
190 for pid in `pidof hostapd`; do
191 grep -E "$phy" /proc/$pid/cmdline >/dev/null && \
192 kill $pid
193 done
194
195 include /lib/network
196 for wdev in $(list_phy_interfaces "$phy"); do
197 [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1
198 for pid in `pidof wpa_supplicant`; do
199 grep "$wdev" /proc/$pid/cmdline >/dev/null && \
200 kill $pid
201 done
202 ifconfig "$wdev" down 2>/dev/null
203 unbridge "$dev"
204 iw dev "$wdev" del
205 done
206
207 return 0
208 )
209 get_freq() {
210 local phy="$1"
211 local chan="$2"
212 iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}'
213 }
214 enable_mac80211() {
215 local device="$1"
216 config_get channel "$device" channel
217 config_get vifs "$device" vifs
218 config_get txpower "$device" txpower
219 config_get country "$device" country
220 config_get distance "$device" distance
221 config_get frag "$device" frag
222 config_get rts "$device" rts
223 find_mac80211_phy "$device" || return 0
224 config_get phy "$device" phy
225 local i=0
226 local macidx=0
227 local apidx=0
228 fixed=""
229 local hostapd_ctrl=""
230
231 [ -n "$country" ] && iw reg set "$country"
232 [ "$channel" = "auto" -o "$channel" = "0" ] || {
233 fixed=1
234 }
235
236 [ -n "$distance" ] && iw phy "$phy" set distance "$distance"
237 [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}"
238 [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}"
239
240 export channel fixed
241 # convert channel to frequency
242 local freq="$(get_freq "$phy" "${fixed:+$channel}")"
243
244 wifi_fixup_hwmode "$device" "g"
245 for vif in $vifs; do
246 while [ -d "/sys/class/net/wlan$i" ]; do
247 i=$(($i + 1))
248 done
249
250 config_get ifname "$vif" ifname
251 [ -n "$ifname" ] || {
252 ifname="wlan$i"
253 }
254 config_set "$vif" ifname "$ifname"
255
256 config_get mode "$vif" mode
257 config_get ssid "$vif" ssid
258
259 # It is far easier to delete and create the desired interface
260 case "$mode" in
261 adhoc)
262 iw phy "$phy" interface add "$ifname" type adhoc
263 ;;
264 ap)
265 # Hostapd will handle recreating the interface and
266 # it's accompanying monitor
267 apidx="$(($apidx + 1))"
268 i=$(($i + 1))
269 [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed
270 ;;
271 mesh)
272 config_get mesh_id "$vif" mesh_id
273 iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id"
274 ;;
275 monitor)
276 iw phy "$phy" interface add "$ifname" type monitor
277 ;;
278 sta)
279 local wdsflag
280 config_get_bool wds "$vif" wds 0
281 [ "$wds" -gt 0 ] && wdsflag="4addr on"
282 iw phy "$phy" interface add "$ifname" type managed $wdsflag
283 config_get_bool powersave "$vif" powersave 0
284 [ "$powersave" -gt 0 ] && powersave="on" || powersave="off"
285 iwconfig "$ifname" power "$powersave"
286 ;;
287 esac
288
289 # All interfaces must have unique mac addresses
290 # which can either be explicitly set in the device
291 # section, or automatically generated
292 config_get macaddr "$device" macaddr
293 local mac_1="${macaddr%%:*}"
294 local mac_2="${macaddr#*:}"
295
296 config_get vif_mac "$vif" macaddr
297 [ -n "$vif_mac" ] || {
298 if [ "$macidx" -gt 0 ]; then
299 offset="$(( 2 + $macidx * 4 ))"
300 else
301 offset="0"
302 fi
303 vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2"
304 macidx="$(($macidx + 1))"
305 }
306 [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac"
307 config_set "$vif" macaddr "$vif_mac"
308
309 # !! ap !!
310 #
311 # ALL ap functionality will be passed to hostapd
312 #
313 # !! station !!
314 #
315 # ALL station functionality will be passed to wpa_supplicant
316 #
317 if [ ! "$mode" = "ap" ]; then
318 # We attempt to set the channel for all interfaces, although
319 # mac80211 may not support it or the driver might not yet
320 # for ap mode this is handled by hostapd
321 [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel"
322 fi
323
324 config_get vif_txpower "$vif" txpower
325 # use vif_txpower (from wifi-iface) to override txpower (from
326 # wifi-device) if the latter doesn't exist
327 txpower="${txpower:-$vif_txpower}"
328 [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00"
329 done
330
331 local start_hostapd=
332 rm -f /var/run/hostapd-$phy.conf
333 for vif in $vifs; do
334 config_get mode "$vif" mode
335 [ "$mode" = "ap" ] || continue
336 mac80211_hostapd_setup_bss "$phy" "$vif"
337 start_hostapd=1
338 done
339
340 [ -n "$start_hostapd" ] && {
341 hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || {
342 echo "Failed to start hostapd for $phy"
343 return
344 }
345 sleep 2
346
347 for vif in $vifs; do
348 config_get mode "$vif" mode
349 config_get ifname "$vif" ifname
350 [ "$mode" = "ap" ] || continue
351 hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}"
352 mac80211_start_vif "$vif" "$ifname"
353 done
354 }
355
356 for vif in $vifs; do
357 config_get mode "$vif" mode
358 config_get ifname "$vif" ifname
359 [ ! "$mode" = "ap" ] || continue
360 ifconfig "$ifname" up
361
362 if [ ! "$mode" = "ap" ]; then
363 ifconfig "$ifname" up
364 case "$mode" in
365 adhoc)
366 config_get bssid "$vif" bssid
367 config_get ssid "$vif" ssid
368 config_get mcast_rate "$vif" mcast_rate
369 local mcval=""
370 [ -n "$mcast_rate" ] && {
371 mcval="$(($mcast_rate / 1000))"
372 mcsub="$(( ($mcast_rate / 100) % 10 ))"
373 [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub"
374 }
375 iw dev "$ifname" ibss join "$ssid" $freq ${fixed:+fixed-freq} $bssid ${mcval:+mcast-rate $mcval}
376 ;;
377 sta)
378 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
379 wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || {
380 echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2
381 # make sure this wifi interface won't accidentally stay open without encryption
382 ifconfig "$ifname" down
383 continue
384 }
385 fi
386 ;;
387 esac
388 mac80211_start_vif "$vif" "$ifname"
389 fi
390 done
391
392 }
393
394
395 check_device() {
396 config_get phy "$1" phy
397 [ -z "$phy" ] && {
398 find_mac80211_phy "$1" >/dev/null || return 0
399 config_get phy "$1" phy
400 }
401 [ "$phy" = "$dev" ] && found=1
402 }
403
404 detect_mac80211() {
405 devidx=0
406 config_load wireless
407 while :; do
408 config_get type "radio$devidx" type
409 [ -n "$type" ] || break
410 devidx=$(($devidx + 1))
411 done
412 for dev in $(ls /sys/class/ieee80211); do
413 found=0
414 config_foreach check_device wifi-device
415 [ "$found" -gt 0 ] && continue
416
417 mode_11n=""
418 mode_band="g"
419 channel="11"
420 ht_cap=0
421 for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do
422 ht_cap="$(($ht_cap | $cap))"
423 done
424 ht_capab="";
425 [ "$ht_cap" -gt 0 ] && {
426 mode_11n="n"
427 append ht_capab " option htmode HT20" "$N"
428
429 list=" list ht_capab"
430 [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N"
431 [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N"
432 [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N"
433 [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N"
434 [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N"
435 [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N"
436 [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N"
437 [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N"
438 [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N"
439 }
440 iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; }
441
442 cat <<EOF
443 config wifi-device radio$devidx
444 option type mac80211
445 option channel ${channel}
446 option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress)
447 option hwmode 11${mode_11n}${mode_band}
448 $ht_capab
449 # REMOVE THIS LINE TO ENABLE WIFI:
450 option disabled 1
451
452 config wifi-iface
453 option device radio$devidx
454 option network lan
455 option mode ap
456 option ssid OpenWrt
457 option encryption none
458
459 EOF
460 devidx=$(($devidx + 1))
461 done
462 }
463