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