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