5adeee44be32651b2ed0a51d3cf0af99163a9346
[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         [ auto = "$channel" ] && channel=0
79
80         local first=1
81         for vif in $vifs; do
82                 nosbeacon=
83                 config_get ifname "$vif" ifname
84                 config_get enc "$vif" encryption
85                 config_get mode "$vif" mode
86                 
87                 [ "$mode" = sta ] && config_get nosbeacon "$device" nosbeacon
88                 
89                 config_get ifname "$vif" ifname
90                 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
91                 [ $? -ne 0 ] && {
92                         echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
93                         continue
94                 }
95                 config_set "$vif" ifname "$ifname"
96
97                 [ "$first" = 1 ] && {
98                         # only need to change freq band and channel on the first vif
99                         config_get agmode "$device" agmode
100                         pureg=0
101                         case "$agmode" in
102                                 *b) agmode=11b;;
103                                 *bg) agmode=11g;;
104                                 *g) agmode=11g; pureg=1;;
105                                 *a) agmode=11a;;
106                                 *) agmode=auto;;
107                         esac
108                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
109                         iwpriv "$ifname" mode "$agmode"
110                         iwpriv "$ifname" pureg "$pureg"
111                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
112                         ifconfig "$ifname" up
113                 }
114         
115                 config_get_bool hidden "$vif" hidden 0
116                 iwpriv "$ifname" hide_ssid "$hidden"
117
118                 config_get ff "$vif" ff
119                 if [ -n "$ff" ]; then
120                         iwpriv "$ifname" ff "$ff"
121                 fi
122
123                 config_get wds "$vif" wds
124                 case "$wds" in
125                         1|on|enabled) wds=1;;
126                         *) wds=0;;
127                 esac
128                 iwpriv "$ifname" wds "$wds"
129
130                 wpa=
131                 case "$enc" in
132                         WEP|wep)
133                                 for idx in 1 2 3 4; do
134                                         config_get key "$vif" "key${idx}"
135                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
136                                 done
137                                 config_get key "$vif" key
138                                 key="${key:-1}"
139                                 case "$key" in
140                                         [1234]) iwconfig "$ifname" enc "[$key]";;
141                                         *) iwconfig "$ifname" enc "$key";;
142                                 esac
143                         ;;
144                         PSK|psk|PSK2|psk2)
145                                 config_get key "$vif" key
146                         ;;
147                 esac
148
149                 case "$mode" in
150                         wds)
151                                 config_get addr "$vif" bssid
152                                 iwpriv "$ifname" wds_add "$addr"
153                         ;;
154                         adhoc|ahdemo)
155                                 config_get addr "$vif" bssid
156                                 [ -z "$addr" ] || { 
157                                         iwconfig "$ifname" ap "$addr"
158                                 }
159                         ;;
160                 esac
161                 config_get ssid "$vif" ssid
162
163                 config_get_bool bgscan "$vif" bgscan
164                 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
165
166                 config_get_bool antdiv "$device" diversity
167                 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
168
169                 config_get antrx "$device" rxantenna
170                 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
171
172                 config_get anttx "$device" txantenna
173                 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
174
175                 config_get distance "$device" distance
176                 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
177
178                 config_get txpwr "$vif" txpower
179                 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
180
181                 config_get rate "$vif" rate
182                 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
183
184                 config_get mcast_rate "$vif" mcast_rate
185                 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
186
187                 config_get frag "$vif" frag
188                 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
189
190                 config_get rts "$vif" rts
191                 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
192
193                 config_get_bool doth "$vif" 80211h
194                 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
195
196                 config_get_bool comp "$vif" compression
197                 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
198
199                 config_get_bool minrate "$vif" minrate
200                 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
201
202                 config_get_bool maxrate "$vif" maxrate
203                 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
204
205                 config_get_bool burst "$vif" bursting
206                 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
207
208                 config_get_bool wmm "$vif" wmm
209                 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
210
211                 config_get_bool xr "$vif" xr
212                 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
213
214                 config_get_bool ar "$vif" ar
215                 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
216
217                 config_get_bool turbo "$vif" turbo
218                 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
219
220                 config_get_bool doth "$vif" doth 0
221                 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
222
223                 config_get maclist "$vif" maclist
224                 [ -n "$maclist" ] && {
225                         # flush MAC list
226                         iwpriv "$ifname" maccmd 3
227                         for mac in $maclist; do
228                                 iwpriv "$ifname" addmac "$mac"
229                         done
230                 }
231
232                 config_get macpolicy "$vif" macpolicy
233                 case "$macpolicy" in
234                         allow)
235                                 iwpriv "$ifname" maccmd 1
236                         ;;
237                         deny)
238                                 iwpriv "$ifname" maccmd 2
239                         ;;
240                         *)
241                                 # default deny policy if mac list exists
242                                 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
243                         ;;
244                 esac
245
246                 ifconfig "$ifname" up
247                 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
248
249                 local net_cfg bridge
250                 net_cfg="$(find_net_config "$vif")"
251                 [ -z "$net_cfg" ] || {
252                         bridge="$(bridge_interface "$net_cfg")"
253                         config_set "$vif" bridge "$bridge"
254                         start_net "$ifname" "$net_cfg"
255                 }
256                 iwconfig "$ifname" essid "$ssid"
257                 set_wifi_up "$vif" "$ifname"
258                 case "$mode" in
259                         ap)
260                                 config_get_bool isolate "$vif" isolate 0
261                                 iwpriv "$ifname" ap_bridge "$((isolate^1))"
262
263                                 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
264                                         hostapd_setup_vif "$vif" madwifi || {
265                                                 echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
266                                                 # make sure this wifi interface won't accidentally stay open without encryption
267                                                 ifconfig "$ifname" down
268                                                 wlanconfig "$ifname" destroy
269                                                 continue
270                                         }
271                                 fi
272                         ;;
273                         wds|sta)
274                                 config_get_bool usepassphrase "$vif" passphrase 1
275                                 case "$enc" in
276                                         PSK|psk|PSK2|psk2)
277                                                 case "$enc" in
278                                                         PSK|psk)
279                                                                 proto='proto=WPA'
280                                                                 if [ "$usepassphrase" = "1" ]; then
281                                                                         passphrase="psk=\"${key}\""
282                                                                 else
283                                                                         passphrase="psk=${key}"
284                                                                 fi
285                                                                 ;;
286                                                         PSK2|psk2)
287                                                                 proto='proto=RSN'
288                                                                 if [ "$usepassphrase" = "1" ]; then
289                                                                         passphrase="psk=\"${key}\""
290                                                                 else
291                                                                         passphrase="psk=${key}"
292                                                                 fi
293                                                                 ;;
294                                                 esac
295                                                 cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
296 network={
297         scan_ssid=1
298         ssid="$ssid"
299         key_mgmt=WPA-PSK
300         $proto
301         $passphrase
302 }
303 EOF
304                                         ;;
305                                         WPA|wpa|WPA2|wpa2)
306                                                 #add wpa_supplicant calls here
307                                         ;;
308                                 esac
309                                 [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -B -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
310                         ;;
311                 esac
312                 first=0
313         done
314 }
315
316
317 detect_atheros() {
318         cd /proc/sys/dev
319         [ -d ath ] || return
320         for dev in $(ls -d wifi* 2>&-); do
321                 config_get type "$dev" type
322                 [ "$type" = atheros ] && return
323                 cat <<EOF
324 config wifi-device  $dev
325         option type     atheros
326         option channel  auto
327
328         # REMOVE THIS LINE TO ENABLE WIFI:
329         option disabled 1
330
331 config wifi-iface
332         option device   $dev
333         option network  lan
334         option mode     ap
335         option ssid     OpenWrt
336         option encryption none
337 EOF
338         done
339 }