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