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