merge r14306, r14307, r14309, and r14311 to 8.09:
[openwrt/svn-archive/archive.git] / package / madwifi / files / lib / wifi / madwifi.sh
1 #!/bin/sh
2 append DRIVERS "atheros"
3
4 scan_atheros() {
5 local device="$1"
6 local wds
7 local adhoc sta ap
8
9 config_get vifs "$device" vifs
10 for vif in $vifs; do
11
12 config_get ifname "$vif" ifname
13 config_set "$vif" ifname "${ifname:-ath}"
14
15 config_get mode "$vif" mode
16 case "$mode" in
17 adhoc|ahdemo|sta|ap|monitor)
18 append $mode "$vif"
19 ;;
20 wds)
21 config_get ssid "$vif" ssid
22 [ -z "$ssid" ] && continue
23
24 config_set "$vif" wds 1
25 config_set "$vif" mode sta
26 mode="sta"
27 addr="$ssid"
28 ${addr:+append $mode "$vif"}
29 ;;
30 *) echo "$device($vif): Invalid mode, ignored."; continue;;
31 esac
32 done
33
34 case "${adhoc:+1}:${sta:+1}:${ap:+1}" in
35 # valid mode combinations
36 1::) wds="";;
37 1::1);;
38 :1:1)config_set "$device" nosbeacon 1;; # AP+STA, can't use beacon timers for STA
39 :1:);;
40 ::1);;
41 ::);;
42 *) echo "$device: Invalid mode combination in config"; return 1;;
43 esac
44
45 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${ahdemo:+$ahdemo }${sta:+$sta }${wds:+$wds }${monitor:+$monitor}"
46 }
47
48
49 disable_atheros() (
50 local device="$1"
51
52 set_wifi_down "$device"
53
54 include /lib/network
55 cd /proc/sys/net
56 for dev in *; do
57 grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
58 [ -f "/var/run/wifi-${dev}.pid" ] &&
59 kill "$(cat "/var/run/wifi-${dev}.pid")"
60 ifconfig "$dev" down
61 unbridge "$dev"
62 wlanconfig "$dev" destroy
63 }
64 done
65 return 0
66 )
67
68 enable_atheros() {
69 local device="$1"
70 config_get channel "$device" channel
71 config_get vifs "$device" vifs
72 config_get txpower "$device" txpower
73
74 [ auto = "$channel" ] && channel=0
75
76 local first=1
77 for vif in $vifs; do
78 local start_hostapd vif_txpower
79 nosbeacon=
80 config_get ifname "$vif" ifname
81 config_get enc "$vif" encryption
82 config_get eap_type "$vif" eap_type
83 config_get mode "$vif" mode
84
85 case "$mode" in
86 sta) config_get nosbeacon "$device" nosbeacon;;
87 adhoc) config_get nosbeacon "$vif" sw_merge;;
88 esac
89
90 config_get ifname "$vif" ifname
91 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
92 [ $? -ne 0 ] && {
93 echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
94 continue
95 }
96 config_set "$vif" ifname "$ifname"
97
98 # only need to change freq band and channel on the first vif
99 config_get hwmode "$device" hwmode
100 [ -z "$hwmode" ] && config_get hwmode "$device" mode
101
102 pureg=0
103 case "$hwmode" in
104 *b) hwmode=11b;;
105 *bg) hwmode=11g;;
106 *g) hwmode=11g; pureg=1;;
107 *gdt) hwmode=11gdt;;
108 *a) hwmode=11a;;
109 *adt) hwmode=11adt;;
110 *ast) hwmode=11ast;;
111 *fh) hwmode=fh;;
112 *) hwmode=auto;;
113 esac
114 iwpriv "$ifname" mode "$hwmode"
115 iwpriv "$ifname" pureg "$pureg"
116
117 [ "$first" = 1 ] && {
118 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
119 }
120
121 config_get_bool hidden "$vif" hidden 0
122 iwpriv "$ifname" hide_ssid "$hidden"
123
124 config_get ff "$vif" ff
125 if [ -n "$ff" ]; then
126 iwpriv "$ifname" ff "$ff"
127 fi
128
129 config_get wds "$vif" wds
130 case "$wds" in
131 1|on|enabled) wds=1;;
132 *) wds=0;;
133 esac
134 iwpriv "$ifname" wds "$wds"
135
136 [ "$mode" = ap -a "$wds" = 1 ] && {
137 config_get_bool wdssep "$vif" wdssep 1
138 [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
139 }
140
141 case "$enc" in
142 WEP|wep)
143 for idx in 1 2 3 4; do
144 config_get key "$vif" "key${idx}"
145 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
146 done
147 config_get key "$vif" key
148 key="${key:-1}"
149 case "$key" in
150 [1234]) iwconfig "$ifname" enc "[$key]";;
151 *) iwconfig "$ifname" enc "$key";;
152 esac
153 ;;
154 psk*|wpa*)
155 start_hostapd=1
156 config_get key "$vif" key
157 ;;
158 esac
159
160 case "$mode" in
161 adhoc|ahdemo)
162 config_get addr "$vif" bssid
163 [ -z "$addr" ] || {
164 iwconfig "$ifname" ap "$addr"
165 }
166 ;;
167 esac
168 config_get ssid "$vif" ssid
169
170 config_get_bool bgscan "$vif" bgscan
171 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
172
173 config_get_bool antdiv "$device" diversity
174 config_get antrx "$device" rxantenna
175 config_get anttx "$device" txantenna
176 config_get_bool softled "$device" softled 1
177
178 devname="$(cat /proc/sys/dev/$device/dev_name)"
179 antgpio=
180 case "$devname" in
181 NanoStation2) antgpio=7;;
182 NanoStation5) antgpio=1;;
183 esac
184 if [ -n "$antgpio" ]; then
185 softled=0
186 config_get antenna "$device" antenna
187 case "$antenna" in
188 external) antdiv=0; antrx=1; anttx=1 ;;
189 horizontal) antdiv=0; antrx=1; anttx=1 ;;
190 vertical) antdiv=0; antrx=2; anttx=2 ;;
191 auto) antdiv=1; antrx=0; anttx=0 ;;
192 esac
193
194 [ -x "$(which gpioctl 2>/dev/null)" ] || antenna=
195 case "$antenna" in
196 horizontal|vertical|auto)
197 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
198 gpioctl "set" "$antgpio" >/dev/null 2>&1
199 ;;
200 external)
201 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
202 gpioctl "clear" "$antgpio" >/dev/null 2>&1
203 ;;
204 esac
205 fi
206
207 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
208 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
209 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
210 [ -n "$softled" ] && sysctl -w dev."$device".softled="$softled" >&-
211
212 config_get distance "$device" distance
213 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
214
215 config_get txpwr "$vif" txpower
216 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
217
218 config_get rate "$vif" rate
219 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
220
221 config_get mcast_rate "$vif" mcast_rate
222 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
223
224 config_get frag "$vif" frag
225 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
226
227 config_get rts "$vif" rts
228 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
229
230 config_get_bool comp "$vif" compression
231 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
232
233 config_get_bool minrate "$vif" minrate
234 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
235
236 config_get_bool maxrate "$vif" maxrate
237 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
238
239 config_get_bool burst "$vif" bursting
240 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
241
242 config_get_bool wmm "$vif" wmm
243 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
244
245 config_get_bool xr "$vif" xr
246 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
247
248 config_get_bool ar "$vif" ar
249 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
250
251 config_get_bool turbo "$vif" turbo
252 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
253
254 config_get_bool doth "$vif" doth 0
255 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
256
257 config_get_bool probereq "$vif" probereq
258 [ -n "$probereq" ] && iwpriv "$ifname" probereq "$probereq"
259
260 config_get maclist "$vif" maclist
261 [ -n "$maclist" ] && {
262 # flush MAC list
263 iwpriv "$ifname" maccmd 3
264 for mac in $maclist; do
265 iwpriv "$ifname" addmac "$mac"
266 done
267 }
268
269 config_get macpolicy "$vif" macpolicy
270 case "$macpolicy" in
271 allow)
272 iwpriv "$ifname" maccmd 1
273 ;;
274 deny)
275 iwpriv "$ifname" maccmd 2
276 ;;
277 *)
278 # default deny policy if mac list exists
279 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
280 ;;
281 esac
282
283 ifconfig "$ifname" up
284 local net_cfg bridge
285 net_cfg="$(find_net_config "$vif")"
286 [ -z "$net_cfg" ] || {
287 bridge="$(bridge_interface "$net_cfg")"
288 config_set "$vif" bridge "$bridge"
289 start_net "$ifname" "$net_cfg"
290 }
291 [ -n "$ssid" ] && iwconfig "$ifname" essid on
292 iwconfig "$ifname" essid "$ssid"
293 set_wifi_up "$vif" "$ifname"
294
295 # TXPower settings only work if device is up already
296 # while atheros hardware theoretically is capable of per-vif (even per-packet) txpower
297 # adjustment it does not work with the current atheros hal/madwifi driver
298
299 config_get vif_txpower "$vif" txpower
300 # use vif_txpower (from wifi-iface) instead of txpower (from wifi-device) if
301 # the latter doesn't exist
302 txpower="${txpower:-$vif_txpower}"
303 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
304
305 case "$mode" in
306 ap)
307 config_get_bool isolate "$vif" isolate 0
308 iwpriv "$ifname" ap_bridge "$((isolate^1))"
309
310 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
311 hostapd_setup_vif "$vif" madwifi || {
312 echo "enable_atheros($device): Failed to set up hostapd for interface $ifname" >&2
313 # make sure this wifi interface won't accidentally stay open without encryption
314 ifconfig "$ifname" down
315 wlanconfig "$ifname" destroy
316 continue
317 }
318 fi
319 ;;
320 wds|sta)
321 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
322 wpa_supplicant_setup_vif "$vif" madwifi || {
323 echo "enable_atheros($device): Failed to set up wpa_supplicant for interface $ifname" >&2
324 ifconfig "$ifname" down
325 wlanconfig "$ifname" destroy
326 continue
327 }
328 fi
329 ;;
330 esac
331 first=0
332 done
333 }
334
335
336 detect_atheros() {
337 cd /proc/sys/dev
338 [ -d ath ] || return
339 for dev in $(ls -d wifi* 2>&-); do
340 config_get type "$dev" type
341 devname="$(cat /proc/sys/dev/$dev/dev_name)"
342 case "$devname" in
343 NanoStation*)
344 EXTRA_DEV="
345 # Ubiquiti NanoStation features
346 option antenna auto # (auto|horizontal|vertical|external)
347 "
348 ;;
349 esac
350 [ "$type" = atheros ] && return
351 cat <<EOF
352 config wifi-device $dev
353 option type atheros
354 option channel auto
355 $EXTRA_DEV
356 # REMOVE THIS LINE TO ENABLE WIFI:
357 option disabled 1
358
359 config wifi-iface
360 option device $dev
361 option network lan
362 option mode ap
363 option ssid OpenWrt
364 option encryption none
365 EOF
366 done
367 }