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