add a missing variable assignment
[openwrt/svn-archive/archive.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" mode
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 0 >/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_bool ff "$vif" ff 0
118 iwpriv "$ifname" ff "$ff"
119
120 config_get wds "$vif" wds
121 case "$wds" in
122 1|on|enabled) wds=1;;
123 *) wds=0;;
124 esac
125 iwpriv "$ifname" wds "$wds"
126
127 wpa=
128 case "$enc" in
129 WEP|wep)
130 for idx in 1 2 3 4; do
131 config_get key "$vif" "key${idx}"
132 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
133 done
134 config_get key "$vif" key
135 key="${key:-1}"
136 case "$key" in
137 [1234]) iwconfig "$ifname" enc "[$key]";;
138 *) iwconfig "$ifname" enc "$key";;
139 esac
140 ;;
141 PSK|psk|PSK2|psk2)
142 config_get key "$vif" key
143 ;;
144 esac
145
146 case "$mode" in
147 wds)
148 config_get addr "$vif" bssid
149 iwpriv "$ifname" wds_add "$addr"
150 ;;
151 adhoc|ahdemo)
152 config_get addr "$vif" bssid
153 [ -z "$addr" ] || {
154 iwconfig "$ifname" ap "$addr"
155 }
156 ;;
157 esac
158 config_get ssid "$vif" ssid
159
160 [ "$mode" = "sta" ] && {
161 config_get_bool bgscan "$vif" bgscan 0
162 iwpriv "$ifname" bgscan "$bgscan"
163 }
164
165 config_get_bool antdiv "$device" diversity 1
166 sysctl -w dev."$device".diversity="$antdiv" >&-
167
168 config_get antrx "$device" rxantenna
169 if [ -n "$antrx" ]; then
170 sysctl -w dev."$device".rxantenna="$antrx" >&-
171 fi
172
173 config_get anttx "$device" txantenna
174 if [ -n "$anttx" ]; then
175 sysctl -w dev."$device".txantenna="$anttx" >&-
176 fi
177
178 config_get distance "$device" distance
179 if [ -n "$distance" ]; then
180 athctrl -i "$device" -d "$distance" >&-
181 fi
182
183 config_get txpwr "$vif" txpower
184 if [ -n "$txpwr" ]; then
185 iwconfig "$ifname" txpower "${txpwr%%.*}"
186 fi
187
188 config_get frag "$vif" frag
189 if [ -n "$frag" ]; then
190 iwconfig "$ifname" frag "${frag%%.*}"
191 fi
192
193 config_get rts "$vif" rts
194 if [ -n "$rts" ]; then
195 iwconfig "$ifname" rts "${rts%%.*}"
196 fi
197
198 ifconfig "$ifname" up
199 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
200
201 local net_cfg bridge
202 net_cfg="$(find_net_config "$vif")"
203 [ -z "$net_cfg" ] || {
204 bridge="$(bridge_interface "$net_cfg")"
205 config_set "$vif" bridge "$bridge"
206 start_net "$ifname" "$net_cfg"
207 }
208 iwconfig "$ifname" essid "$ssid"
209 set_wifi_up "$vif" "$ifname"
210 case "$mode" in
211 ap)
212 config_get_bool isolate "$vif" isolate 0
213 iwpriv "$ifname" ap_bridge "$((isolate^1))"
214
215 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
216 hostapd_setup_vif "$vif" madwifi || {
217 echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
218 # make sure this wifi interface won't accidentally stay open without encryption
219 ifconfig "$ifname" down
220 wlanconfig "$ifname" destroy
221 continue
222 }
223 fi
224 ;;
225 wds|sta)
226 case "$enc" in
227 PSK|psk|PSK2|psk2)
228 case "$enc" in
229 PSK|psk)
230 proto='proto=WPA';;
231 PSK2|psk2)
232 proto='proto=RSN';;
233 esac
234 cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
235 ctrl_interface=/var/run/wpa_supplicant
236 network={
237 scan_ssid=1
238 ssid="$ssid"
239 key_mgmt=WPA-PSK
240 $proto
241 psk="$key"
242 }
243 EOF
244 ;;
245 WPA|wpa|WPA2|wpa2)
246 #add wpa_supplicant calls here
247 ;;
248 esac
249 [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -B -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
250 ;;
251 esac
252 first=0
253 done
254 }
255
256
257 detect_atheros() {
258 cd /proc/sys/dev
259 [ -d ath ] || return
260 for dev in $(ls -d wifi* 2>&-); do
261 config_get type "$dev" type
262 [ "$type" = atheros ] && return
263 cat <<EOF
264 config wifi-device $dev
265 option type atheros
266 option channel 5
267
268 # REMOVE THIS LINE TO ENABLE WIFI:
269 option disabled 1
270
271 config wifi-iface
272 option device $dev
273 option network lan
274 option mode ap
275 option ssid OpenWrt
276 option encryption none
277 EOF
278 done
279 }