5742c68e5ac1abc37507140a3dbaa3e9df476513
[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)
18                                 append $mode "$vif"
19                         ;;
20                         wds)
21                                 config_get addr "$vif" bssid
22                                 config_get ssid "$vif" ssid
23                                 [ -z "$addr" -a -n "$ssid" ] && {
24                                         config_set "$vif" wds 1
25                                         config_set "$vif" mode sta
26                                         mode="sta"
27                                         addr="$ssid"
28                                 }
29                                 ${addr:+append $mode "$vif"}
30                         ;;
31                         *) echo "$device($vif): Invalid mode, ignored."; continue;;
32                 esac
33         done
34
35         case "${adhoc:+1}:${sta:+1}:${ap+1}" in
36                 # valid mode combinations
37                 1::) wds="";;
38                 1::1);;
39                 :1:1)config_set "$device" nosbeacon 1;; # AP+STA, can't use beacon timers for STA
40                 :1:);;
41                 ::1);;
42                 ::);;
43                 *) echo "$device: Invalid mode combination in config"; return 1;;
44         esac
45
46         config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${ahdemo:+$ahdemo }${sta:+$sta }${wds:+$wds }"
47 }
48
49
50 disable_atheros() (
51         local device="$1"
52
53         set_wifi_down "$device"
54         # kill all running hostapd and wpa_supplicant processes that
55         # are running on atheros vifs 
56         for pid in `pidof hostapd wpa_supplicant`; do
57                 grep ath /proc/$pid/cmdline >/dev/null && \
58                         kill $pid
59         done
60         
61         include /lib/network
62         cd /proc/sys/net
63         for dev in *; do
64                 grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
65                         ifconfig "$dev" down 
66                         unbridge "$dev"
67                         wlanconfig "$dev" destroy
68                 }
69         done
70         return 0
71 )
72
73 enable_atheros() {
74         local device="$1"
75         config_get channel "$device" channel
76         config_get vifs "$device" vifs
77         
78         local first=1
79         for vif in $vifs; do
80                 nosbeacon=
81                 config_get ifname "$vif" ifname
82                 config_get enc "$vif" encryption
83                 config_get mode "$vif" mode
84                 
85                 [ "$mode" = sta ] && config_get nosbeacon "$device" nosbeacon
86                 
87                 config_get ifname "$vif" ifname
88                 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
89                 [ $? -ne 0 ] && {
90                         echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
91                         continue
92                 }
93                 config_set "$vif" ifname "$ifname"
94
95                 [ "$first" = 1 ] && {
96                         # only need to change freq band and channel on the first vif
97                         config_get agmode "$device" agmode
98                         pureg=0
99                         case "$agmode" in
100                                 *b) agmode=11b;;
101                                 *bg) agmode=11g;;
102                                 *g) agmode=11g; pureg=1;;
103                                 *a) agmode=11a;;
104                                 *) agmode=auto;;
105                         esac
106                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
107                         ifconfig "$ifname" up
108                         sleep 1
109                         iwpriv "$ifname" mode "$agmode"
110                         iwpriv "$ifname" pureg "$pureg"
111                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
112                 }
113         
114                 config_get_bool hidden "$vif" hidden 0
115                 iwpriv "$ifname" hide_ssid "$hidden"
116
117                 config_get ff "$vif" ff
118                 if [ -n "$ff" ]; then
119                         iwpriv "$ifname" ff "$ff"
120                 fi
121
122                 config_get wds "$vif" wds
123                 case "$wds" in
124                         1|on|enabled) wds=1;;
125                         *) wds=0;;
126                 esac
127                 iwpriv "$ifname" wds "$wds"
128
129                 wpa=
130                 case "$enc" in
131                         WEP|wep)
132                                 for idx in 1 2 3 4; do
133                                         config_get key "$vif" "key${idx}"
134                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
135                                 done
136                                 config_get key "$vif" key
137                                 key="${key:-1}"
138                                 case "$key" in
139                                         [1234]) iwconfig "$ifname" enc "[$key]";;
140                                         *) iwconfig "$ifname" enc "$key";;
141                                 esac
142                         ;;
143                         PSK|psk|PSK2|psk2)
144                                 config_get key "$vif" key
145                         ;;
146                 esac
147
148                 case "$mode" in
149                         wds)
150                                 config_get addr "$vif" bssid
151                                 iwpriv "$ifname" wds_add "$addr"
152                         ;;
153                         adhoc|ahdemo)
154                                 config_get addr "$vif" bssid
155                                 [ -z "$addr" ] || { 
156                                         iwconfig "$ifname" ap "$addr"
157                                 }
158                         ;;
159                 esac
160                 config_get ssid "$vif" ssid
161
162                 config_get_bool bgscan "$vif" bgscan
163                 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
164
165                 config_get_bool antdiv "$device" diversity
166                 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
167
168                 config_get antrx "$device" rxantenna
169                 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
170
171                 config_get anttx "$device" txantenna
172                 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
173
174                 config_get distance "$device" distance
175                 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
176
177                 config_get txpwr "$vif" txpower
178                 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
179
180                 config_get frag "$vif" frag
181                 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
182
183                 config_get rts "$vif" rts
184                 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
185
186                 ifconfig "$ifname" up
187                 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
188
189                 local net_cfg bridge
190                 net_cfg="$(find_net_config "$vif")"
191                 [ -z "$net_cfg" ] || {
192                         bridge="$(bridge_interface "$net_cfg")"
193                         config_set "$vif" bridge "$bridge"
194                         start_net "$ifname" "$net_cfg"
195                 }
196                 iwconfig "$ifname" essid "$ssid"
197                 set_wifi_up "$vif" "$ifname"
198                 case "$mode" in
199                         ap)
200                                 config_get_bool isolate "$vif" isolate 0
201                                 iwpriv "$ifname" ap_bridge "$((isolate^1))"
202
203                                 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
204                                         hostapd_setup_vif "$vif" madwifi || {
205                                                 echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
206                                                 # make sure this wifi interface won't accidentally stay open without encryption
207                                                 ifconfig "$ifname" down
208                                                 wlanconfig "$ifname" destroy
209                                                 continue
210                                         }
211                                 fi
212                         ;;
213                         wds|sta)
214                                 case "$enc" in 
215                                         PSK|psk|PSK2|psk2)
216                                                 case "$enc" in
217                                                         PSK|psk)
218                                                                 proto='proto=WPA'
219                                                                 passphrase="${key}"
220                                                                 ;;
221                                                         PSK2|psk2)
222                                                                 proto='proto=RSN'
223                                                                 passphrase=`wpa_passphrase ${ssid} "${key}" | grep psk | grep -v \#| cut -d= -f2`
224                                                                 ;;
225                                                 esac
226                                                 
227                                                 cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
228 ctrl_interface=/var/run/wpa_supplicant
229 network={
230         scan_ssid=1
231         ssid="$ssid"
232         key_mgmt=WPA-PSK
233         $proto
234         psk="$passphrase"
235 }
236 EOF
237                                         ;;
238                                         WPA|wpa|WPA2|wpa2)
239                                                 #add wpa_supplicant calls here
240                                         ;;
241                                 esac
242                                 [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -B -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
243                         ;;
244                 esac
245                 first=0
246         done
247 }
248
249
250 detect_atheros() {
251         cd /proc/sys/dev
252         [ -d ath ] || return
253         for dev in $(ls -d wifi* 2>&-); do
254                 config_get type "$dev" type
255                 [ "$type" = atheros ] && return
256                 cat <<EOF
257 config wifi-device  $dev
258         option type     atheros
259         option channel  5
260
261         # REMOVE THIS LINE TO ENABLE WIFI:
262         option disabled 1
263
264 config wifi-iface
265         option device   $dev
266         option network  lan
267         option mode     ap
268         option ssid     OpenWrt
269         option encryption none
270 EOF
271         done
272 }