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