379ec0747b437db4cdbcb635363b1305d9dcc302
[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|monitor)
18 append $mode "$vif"
19 ;;
20 wds)
21 config_get ssid "$vif" ssid
22 [ -z "$ssid" ] && continue
23
24 config_set "$vif" wds 1
25 config_set "$vif" mode sta
26 mode="sta"
27 addr="$ssid"
28 ${addr:+append $mode "$vif"}
29 ;;
30 *) echo "$device($vif): Invalid mode, ignored."; continue;;
31 esac
32 done
33
34 case "${adhoc:+1}:${sta:+1}:${ap+1}" in
35 # valid mode combinations
36 1::) wds="";;
37 1::1);;
38 :1:1)config_set "$device" nosbeacon 1;; # AP+STA, can't use beacon timers for STA
39 :1:);;
40 ::1);;
41 ::);;
42 *) echo "$device: Invalid mode combination in config"; return 1;;
43 esac
44
45 config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${ahdemo:+$ahdemo }${sta:+$sta }${wds:+$wds }${monitor:+$monitor}"
46 }
47
48
49 disable_atheros() (
50 local device="$1"
51
52 set_wifi_down "$device"
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 local device="$1"
74 config_get channel "$device" channel
75 config_get vifs "$device" vifs
76
77 [ auto = "$channel" ] && channel=0
78
79 local first=1
80 for vif in $vifs; do
81 local start_hostapd
82 nosbeacon=
83 config_get ifname "$vif" ifname
84 config_get enc "$vif" encryption
85 config_get eap_type "$vif" eap_type
86 config_get mode "$vif" mode
87
88 case "$mode" in
89 sta) config_get nosbeacon "$device" nosbeacon;;
90 adhoc) config_get nosbeacon "$vif" sw_merge;;
91 esac
92
93 config_get ifname "$vif" ifname
94 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
95 [ $? -ne 0 ] && {
96 echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
97 continue
98 }
99 config_set "$vif" ifname "$ifname"
100
101 # only need to change freq band and channel on the first vif
102 config_get agmode "$device" agmode
103 [ -z "$agmode" ] && config_get agmode "$device" mode
104
105 pureg=0
106 case "$agmode" in
107 *b) agmode=11b;;
108 *bg) agmode=11g;;
109 *g) agmode=11g; pureg=1;;
110 *gdt) agmode=11gdt;;
111 *a) agmode=11a;;
112 *adt) agmode=11adt;;
113 *ast) agmode=11ast;;
114 *fh) agmode=fh;;
115 *) agmode=auto;;
116 esac
117 iwpriv "$ifname" mode "$agmode"
118 iwpriv "$ifname" pureg "$pureg"
119
120 [ "$first" = 1 ] && {
121 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
122 }
123
124 config_get_bool hidden "$vif" hidden 0
125 iwpriv "$ifname" hide_ssid "$hidden"
126
127 config_get ff "$vif" ff
128 if [ -n "$ff" ]; then
129 iwpriv "$ifname" ff "$ff"
130 fi
131
132 config_get wds "$vif" wds
133 case "$wds" in
134 1|on|enabled) wds=1;;
135 *) wds=0;;
136 esac
137 iwpriv "$ifname" wds "$wds"
138
139 [ "$mode" = ap -a "$wds" = 1 ] && {
140 config_get_bool wdssep "$vif" wdssep 1
141 [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
142 }
143
144 case "$enc" in
145 WEP|wep)
146 for idx in 1 2 3 4; do
147 config_get key "$vif" "key${idx}"
148 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
149 done
150 config_get key "$vif" key
151 key="${key:-1}"
152 case "$key" in
153 [1234]) iwconfig "$ifname" enc "[$key]";;
154 *) iwconfig "$ifname" enc "$key";;
155 esac
156 ;;
157 psk*|wpa*)
158 start_hostapd=1
159 config_get key "$vif" key
160 ;;
161 esac
162
163 case "$mode" in
164 adhoc|ahdemo)
165 config_get addr "$vif" bssid
166 [ -z "$addr" ] || {
167 iwconfig "$ifname" ap "$addr"
168 }
169 ;;
170 esac
171 config_get ssid "$vif" ssid
172
173 config_get_bool bgscan "$vif" bgscan
174 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
175
176 config_get_bool antdiv "$device" diversity
177 config_get antrx "$device" rxantenna
178 config_get anttx "$device" txantenna
179 config_get_bool softled "$device" softled 1
180
181 devname="$(cat /proc/sys/dev/$device/dev_name)"
182 antgpio=
183 case "$devname" in
184 NanoStation2) antgpio=7;;
185 NanoStation5) antgpio=1;;
186 esac
187 if [ -n "$antgpio" ]; then
188 softled=0
189 config_get polarity "$device" polarity
190 case "$polarity" in
191 horizontal) antdiv=0; antrx=1; anttx=1 ;;
192 vertical) antdiv=0; antrx=2; anttx=2 ;;
193 auto) antdiv=1; antrx=0; anttx=0 ;;
194 esac
195 config_get antenna "$device" antenna
196 [ -x "$(which gpioctl 2>/dev/null)" ] || antenna=
197 case "$antenna" in
198 internal)
199 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
200 gpioctl "set" "$antgpio" >/dev/null 2>&1
201 ;;
202 external)
203 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
204 gpioctl "clear" "$antgpio" >/dev/null 2>&1
205 antdiv=0
206 antrx=1
207 anttx=1
208 ;;
209 esac
210 fi
211
212 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
213 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
214 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
215 [ -n "$softled" ] && sysctl -w dev."$device".softled="$softled" >&-
216
217 config_get distance "$device" distance
218 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
219
220 config_get txpwr "$vif" txpower
221 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
222
223 config_get rate "$vif" rate
224 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
225
226 config_get mcast_rate "$vif" mcast_rate
227 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
228
229 config_get frag "$vif" frag
230 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
231
232 config_get rts "$vif" rts
233 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
234
235 config_get_bool comp "$vif" compression
236 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
237
238 config_get_bool minrate "$vif" minrate
239 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
240
241 config_get_bool maxrate "$vif" maxrate
242 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
243
244 config_get_bool burst "$vif" bursting
245 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
246
247 config_get_bool wmm "$vif" wmm
248 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
249
250 config_get_bool xr "$vif" xr
251 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
252
253 config_get_bool ar "$vif" ar
254 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
255
256 config_get_bool turbo "$vif" turbo
257 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
258
259 config_get_bool doth "$vif" doth 0
260 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
261
262 config_get maclist "$vif" maclist
263 [ -n "$maclist" ] && {
264 # flush MAC list
265 iwpriv "$ifname" maccmd 3
266 for mac in $maclist; do
267 iwpriv "$ifname" addmac "$mac"
268 done
269 }
270
271 config_get macpolicy "$vif" macpolicy
272 case "$macpolicy" in
273 allow)
274 iwpriv "$ifname" maccmd 1
275 ;;
276 deny)
277 iwpriv "$ifname" maccmd 2
278 ;;
279 *)
280 # default deny policy if mac list exists
281 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
282 ;;
283 esac
284
285 ifconfig "$ifname" up
286 local net_cfg bridge
287 net_cfg="$(find_net_config "$vif")"
288 [ -z "$net_cfg" ] || {
289 bridge="$(bridge_interface "$net_cfg")"
290 config_set "$vif" bridge "$bridge"
291 start_net "$ifname" "$net_cfg"
292 }
293 [ -n "$ssid" ] && iwconfig "$ifname" essid on
294 iwconfig "$ifname" essid "$ssid"
295 set_wifi_up "$vif" "$ifname"
296 case "$mode" in
297 ap)
298 config_get_bool isolate "$vif" isolate 0
299 iwpriv "$ifname" ap_bridge "$((isolate^1))"
300
301 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
302 hostapd_setup_vif "$vif" madwifi || {
303 echo "enable_atheros($device): Failed to set up hostapd for interface $ifname" >&2
304 # make sure this wifi interface won't accidentally stay open without encryption
305 ifconfig "$ifname" down
306 wlanconfig "$ifname" destroy
307 continue
308 }
309 fi
310 ;;
311 wds|sta)
312 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
313 wpa_supplicant_setup_vif "$vif" madwifi || {
314 echo "enable_atheros($device): Failed to set up wpa_supplicant for interface $ifname" >&2
315 ifconfig "$ifname" down
316 wlanconfig "$ifname" destroy
317 continue
318 }
319 fi
320 ;;
321 esac
322 first=0
323 done
324 }
325
326
327 detect_atheros() {
328 cd /proc/sys/dev
329 [ -d ath ] || return
330 for dev in $(ls -d wifi* 2>&-); do
331 config_get type "$dev" type
332 devname="$(cat /proc/sys/dev/$dev/dev_name)"
333 case "$devname" in
334 NanoStation*)
335 EXTRA_DEV="
336 # Ubiquiti NanoStation features
337 option antenna internal
338 option polarity auto # (auto|horizontal|vertical)
339 "
340 ;;
341 esac
342 [ "$type" = atheros ] && return
343 cat <<EOF
344 config wifi-device $dev
345 option type atheros
346 option channel auto
347 $EXTRA_DEV
348 # REMOVE THIS LINE TO ENABLE WIFI:
349 option disabled 1
350
351 config wifi-iface
352 option device $dev
353 option network lan
354 option mode ap
355 option ssid OpenWrt
356 option encryption none
357 EOF
358 done
359 }