Declare some missing local variables in madwifi.sh Signed-off-by: Vasilis Tsiligianni...
[oweals/openwrt.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 ahdemo sta ap monitor
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 "${sta:+$sta }${ap:+$ap }${adhoc:+$adhoc }${ahdemo:+$ahdemo }${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
71         config_get regdomain "$device" regdomain
72         [ -n "$regdomain" ] && echo "$regdomain" > /proc/sys/dev/$device/regdomain
73
74         config_get country "$device" country
75         [ -z "$country" ] && country="0"
76         echo "$country" > /proc/sys/dev/$device/countrycode
77
78         config_get_bool outdoor "$device" outdoor "0"
79         echo "$outdoor" > /proc/sys/dev/$device/outdoor
80
81         config_get channel "$device" channel
82         config_get vifs "$device" vifs
83         config_get txpower "$device" txpower
84
85         [ auto = "$channel" ] && channel=0
86
87         config_get_bool antdiv "$device" diversity
88         config_get antrx "$device" rxantenna
89         config_get anttx "$device" txantenna
90         config_get_bool softled "$device" softled 1
91
92         devname="$(cat /proc/sys/dev/$device/dev_name)"
93         antgpio=
94         case "$devname" in
95                 NanoStation2) antgpio=7;;
96                 NanoStation5) antgpio=1;;
97         esac
98         if [ -n "$antgpio" ]; then
99                 softled=0
100                 config_get antenna "$device" antenna
101                 case "$antenna" in
102                         external) antdiv=0; antrx=1; anttx=1 ;;
103                         horizontal) antdiv=0; antrx=1; anttx=1 ;;
104                         vertical) antdiv=0; antrx=2; anttx=2 ;;
105                         auto) antdiv=1; antrx=0; anttx=0 ;;
106                 esac
107                         
108                 [ -x "$(which gpioctl 2>/dev/null)" ] || antenna=
109                 case "$antenna" in
110                         horizontal|vertical|auto)
111                                 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
112                                 gpioctl "set" "$antgpio" >/dev/null 2>&1
113                         ;;
114                         external)
115                                 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
116                                 gpioctl "clear" "$antgpio" >/dev/null 2>&1
117                         ;;
118                 esac
119         fi
120
121         [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
122         [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
123         [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
124         [ -n "$softled" ] && sysctl -w dev."$device".softled="$softled" >&-
125
126         config_get distance "$device" distance
127         [ -n "$distance" ] && sysctl -w dev."$device".distance="$distance" >&-
128
129         local first=1
130         for vif in $vifs; do
131                 local start_hostapd vif_txpower
132                 nosbeacon=
133                 config_get ifname "$vif" ifname
134                 config_get enc "$vif" encryption
135                 config_get eap_type "$vif" eap_type
136                 config_get mode "$vif" mode
137                 
138                 case "$mode" in
139                         sta) config_get nosbeacon "$device" nosbeacon;;
140                         adhoc) config_get nosbeacon "$vif" sw_merge;;
141                 esac
142                 
143                 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
144                 [ $? -ne 0 ] && {
145                         echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
146                         continue
147                 }
148                 config_set "$vif" ifname "$ifname"
149
150                 # only need to change freq band and channel on the first vif
151                 [ "$first" = 1 ] && {
152                 config_get hwmode "$device" hwmode
153                 [ -z "$hwmode" ] && config_get hwmode "$device" mode
154
155                         pureg=0
156                         case "$hwmode" in
157                                 *b) hwmode=11b;;
158                                 *bg) hwmode=11g;;
159                                 *g) hwmode=11g; pureg=1;;
160                                 *gdt) hwmode=11gdt;;
161                                 *a) hwmode=11a;;
162                                 *adt) hwmode=11adt;;
163                                 *ast) hwmode=11ast;;
164                                 *fh) hwmode=fh;;
165                                 *) hwmode=auto;;
166                         esac
167                         iwpriv "$ifname" mode "$hwmode"
168                         iwpriv "$ifname" pureg "$pureg"
169
170                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
171                 }
172         
173                 config_get_bool hidden "$vif" hidden 0
174                 iwpriv "$ifname" hide_ssid "$hidden"
175
176                 config_get ff "$vif" ff
177                 if [ -n "$ff" ]; then
178                         iwpriv "$ifname" ff "$ff"
179                 fi
180
181                 config_get wds "$vif" wds
182                 case "$wds" in
183                         1|on|enabled) wds=1;;
184                         *) wds=0;;
185                 esac
186                 iwpriv "$ifname" wds "$wds"
187
188                 [ "$mode" = ap -a "$wds" = 1 ] && {
189                         config_get_bool wdssep "$vif" wdssep 1
190                         [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
191                 }
192
193                 case "$enc" in
194                         WEP|wep)
195                                 for idx in 1 2 3 4; do
196                                         config_get key "$vif" "key${idx}"
197                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
198                                 done
199                                 config_get key "$vif" key
200                                 key="${key:-1}"
201                                 case "$key" in
202                                         [1234]) iwconfig "$ifname" enc "[$key]";;
203                                         *) iwconfig "$ifname" enc "$key";;
204                                 esac
205                         ;;
206                         psk*|wpa*)
207                                 start_hostapd=1
208                                 config_get key "$vif" key
209                         ;;
210                 esac
211
212                 case "$mode" in
213                         sta|adhoc|ahdemo)
214                                 config_get addr "$vif" bssid
215                                 [ -z "$addr" ] || { 
216                                         iwconfig "$ifname" ap "$addr"
217                                 }
218                         ;;
219                 esac
220                 config_get ssid "$vif" ssid
221
222                 config_get_bool bgscan "$vif" bgscan
223                 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
224
225                 config_get_bool antdiv "$device" diversity
226                 config_get antrx "$device" rxantenna
227                 config_get anttx "$device" txantenna
228                 config_get_bool softled "$device" softled 1
229
230                 devname="$(cat /proc/sys/dev/$device/dev_name)"
231                 antgpio=
232                 case "$devname" in
233                         NanoStation2) antgpio=7;;
234                         NanoStation5) antgpio=1;;
235                 esac
236                 if [ -n "$antgpio" ]; then
237                         softled=0
238                         config_get antenna "$device" antenna
239                         case "$antenna" in
240                                 external) antdiv=0; antrx=1; anttx=1 ;;
241                                 horizontal) antdiv=0; antrx=1; anttx=1 ;;
242                                 vertical) antdiv=0; antrx=2; anttx=2 ;;
243                                 auto) antdiv=1; antrx=0; anttx=0 ;;
244                         esac
245                         
246                         [ -x "$(which gpioctl 2>/dev/null)" ] || antenna=
247                         case "$antenna" in
248                                 horizontal|vertical|auto)
249                                         gpioctl "dirout" "$antgpio" >/dev/null 2>&1
250                                         gpioctl "set" "$antgpio" >/dev/null 2>&1
251                                 ;;
252                                 external)
253                                         gpioctl "dirout" "$antgpio" >/dev/null 2>&1
254                                         gpioctl "clear" "$antgpio" >/dev/null 2>&1
255                                 ;;
256                         esac
257                 fi
258
259                 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
260                 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
261                 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
262                 [ -n "$softled" ] && sysctl -w dev."$device".softled="$softled" >&-
263
264                 config_get distance "$device" distance
265                 [ -n "$distance" ] && sysctl -w dev."$device".distance="$distance" >&-
266
267                 config_get rate "$vif" rate
268                 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
269
270                 config_get mcast_rate "$vif" mcast_rate
271                 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
272
273                 config_get frag "$vif" frag
274                 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
275
276                 config_get rts "$vif" rts
277                 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
278
279                 config_get_bool comp "$vif" compression
280                 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
281
282                 config_get_bool minrate "$vif" minrate
283                 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
284
285                 config_get_bool maxrate "$vif" maxrate
286                 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
287
288                 config_get_bool burst "$vif" bursting
289                 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
290
291                 config_get_bool wmm "$vif" wmm
292                 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
293
294                 config_get_bool xr "$vif" xr
295                 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
296
297                 config_get_bool ar "$vif" ar
298                 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
299
300                 config_get_bool turbo "$vif" turbo
301                 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
302
303                 config_get_bool beacon_power "$vif" beacon_power
304                 [ -n "$beacon_power" ] && iwpriv "$ifname" beacon_pwr "$beacon_power"
305
306                 config_get_bool doth "$vif" doth 0
307                 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
308
309                 config_get_bool probereq "$vif" probereq
310                 [ -n "$probereq" ] && iwpriv "$ifname" probereq "$probereq"
311
312                 config_get maclist "$vif" maclist
313                 [ -n "$maclist" ] && {
314                         # flush MAC list
315                         iwpriv "$ifname" maccmd 3
316                         for mac in $maclist; do
317                                 iwpriv "$ifname" addmac "$mac"
318                         done
319                 }
320
321                 config_get macpolicy "$vif" macpolicy
322                 case "$macpolicy" in
323                         allow)
324                                 iwpriv "$ifname" maccmd 1
325                         ;;
326                         deny)
327                                 iwpriv "$ifname" maccmd 2
328                         ;;
329                         *)
330                                 # default deny policy if mac list exists
331                                 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
332                         ;;
333                 esac
334
335                 ifconfig "$ifname" up
336
337                 local net_cfg bridge
338                 net_cfg="$(find_net_config "$vif")"
339                 [ -z "$net_cfg" ] || {
340                         bridge="$(bridge_interface "$net_cfg")"
341                         config_set "$vif" bridge "$bridge"
342                         start_net "$ifname" "$net_cfg"
343                 }
344                 [ -n "$ssid" ] && {
345                         iwconfig "$ifname" essid on
346                         iwconfig "$ifname" essid "$ssid"
347                 }
348                 set_wifi_up "$vif" "$ifname"
349
350                 # TXPower settings only work if device is up already
351                 # while atheros hardware theoretically is capable of per-vif (even per-packet) txpower
352                 # adjustment it does not work with the current atheros hal/madwifi driver
353
354                 config_get vif_txpower "$vif" txpower
355                 # use vif_txpower (from wifi-iface) instead of txpower (from wifi-device) if
356                 # the latter doesn't exist
357                 txpower="${txpower:-$vif_txpower}"
358                 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
359
360                 case "$mode" in
361                         ap)
362                                 config_get_bool isolate "$vif" isolate 0
363                                 iwpriv "$ifname" ap_bridge "$((isolate^1))"
364
365                                 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
366                                         hostapd_setup_vif "$vif" madwifi || {
367                                                 echo "enable_atheros($device): Failed to set up hostapd for interface $ifname" >&2
368                                                 # make sure this wifi interface won't accidentally stay open without encryption
369                                                 ifconfig "$ifname" down
370                                                 wlanconfig "$ifname" destroy
371                                                 continue
372                                         }
373                                 fi
374                         ;;
375                         wds|sta)
376                                 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
377                                         wpa_supplicant_setup_vif "$vif" madwifi || {
378                                                 echo "enable_atheros($device): Failed to set up wpa_supplicant for interface $ifname" >&2
379                                                 ifconfig "$ifname" down
380                                                 wlanconfig "$ifname" destroy
381                                                 continue
382                                         }
383                                 fi
384                         ;;
385                 esac
386                 first=0
387         done
388 }
389
390
391 detect_atheros() {
392         cd /proc/sys/dev
393         [ -d ath ] || return
394         for dev in $(ls -d wifi* 2>&-); do
395                 config_get type "$dev" type
396                 devname="$(cat /proc/sys/dev/$dev/dev_name)"
397                 case "$devname" in
398                         NanoStation*)
399                                 EXTRA_DEV="
400 # Ubiquiti NanoStation features
401         option antenna  auto # (auto|horizontal|vertical|external)
402 "
403                         ;;
404                 esac
405                 [ "$type" = atheros ] && continue
406                 cat <<EOF
407 config wifi-device  $dev
408         option type     atheros
409         option channel  auto
410 $EXTRA_DEV
411         # REMOVE THIS LINE TO ENABLE WIFI:
412         option disabled 1
413
414 config wifi-iface
415         option device   $dev
416         option network  lan
417         option mode     ap
418         option ssid     OpenWrt
419         option encryption none
420 EOF
421         done
422 }