2fe866410eb2f21f4697ce7569cdb87d12d43786
[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|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 "${sta:+$sta }${ap:+$ap }${adhoc:+$adhoc }${ahdemo:+$ahdemo }${wds:+$wds }${monitor:+$monitor}"
46 }
47
48
49 disable_atheros() (
50 local device="$1"
51
52 set_wifi_down "$device"
53
54 include /lib/network
55 cd /proc/sys/net
56 for dev in *; do
57 grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
58 [ -f "/var/run/wifi-${dev}.pid" ] &&
59 kill "$(cat "/var/run/wifi-${dev}.pid")"
60 ifconfig "$dev" down
61 unbridge "$dev"
62 wlanconfig "$dev" destroy
63 }
64 done
65 return 0
66 )
67
68 enable_atheros() {
69 local device="$1"
70
71 config_get regdomain "$device" regdomain
72 [ -n "$regdomain" ] && echo "$regdomain" > /proc/sys/dev/$device/regdomain
73
74 config_get country "$device" country
75 [ -z "$country" ] && country="0"
76 echo "$country" > /proc/sys/dev/$device/countrycode
77
78 config_get outdoor "$device" outdoor
79 [ -z "$outdoor" ] && outdoor="0"
80 echo "$outdoor" > /proc/sys/dev/$device/outdoor
81
82 config_get channel "$device" channel
83 config_get vifs "$device" vifs
84 config_get txpower "$device" txpower
85
86 [ auto = "$channel" ] && channel=0
87
88 local first=1
89 for vif in $vifs; do
90 local start_hostapd vif_txpower
91 nosbeacon=
92 config_get ifname "$vif" ifname
93 config_get enc "$vif" encryption
94 config_get eap_type "$vif" eap_type
95 config_get mode "$vif" mode
96
97 case "$mode" in
98 sta) config_get nosbeacon "$device" nosbeacon;;
99 adhoc) config_get nosbeacon "$vif" sw_merge;;
100 esac
101
102 config_get ifname "$vif" ifname
103 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
104 [ $? -ne 0 ] && {
105 echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
106 continue
107 }
108 config_set "$vif" ifname "$ifname"
109
110 # only need to change freq band and channel on the first vif
111 config_get hwmode "$device" hwmode
112 [ -z "$hwmode" ] && config_get hwmode "$device" mode
113
114 pureg=0
115 case "$hwmode" in
116 *b) hwmode=11b;;
117 *bg) hwmode=11g;;
118 *g) hwmode=11g; pureg=1;;
119 *gdt) hwmode=11gdt;;
120 *a) hwmode=11a;;
121 *adt) hwmode=11adt;;
122 *ast) hwmode=11ast;;
123 *fh) hwmode=fh;;
124 *) hwmode=auto;;
125 esac
126 iwpriv "$ifname" pureg "$pureg"
127
128 [ "$first" = 1 ] && {
129 iwpriv "$ifname" mode "$hwmode"
130 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
131 }
132
133 config_get_bool hidden "$vif" hidden 0
134 iwpriv "$ifname" hide_ssid "$hidden"
135
136 config_get ff "$vif" ff
137 if [ -n "$ff" ]; then
138 iwpriv "$ifname" ff "$ff"
139 fi
140
141 config_get wds "$vif" wds
142 case "$wds" in
143 1|on|enabled) wds=1;;
144 *) wds=0;;
145 esac
146 iwpriv "$ifname" wds "$wds"
147
148 [ "$mode" = ap -a "$wds" = 1 ] && {
149 config_get_bool wdssep "$vif" wdssep 1
150 [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
151 }
152
153 case "$enc" in
154 WEP|wep)
155 for idx in 1 2 3 4; do
156 config_get key "$vif" "key${idx}"
157 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
158 done
159 config_get key "$vif" key
160 key="${key:-1}"
161 case "$key" in
162 [1234]) iwconfig "$ifname" enc "[$key]";;
163 *) iwconfig "$ifname" enc "$key";;
164 esac
165 ;;
166 psk*|wpa*)
167 start_hostapd=1
168 config_get key "$vif" key
169 ;;
170 esac
171
172 case "$mode" in
173 sta|adhoc|ahdemo)
174 config_get addr "$vif" bssid
175 [ -z "$addr" ] || {
176 iwconfig "$ifname" ap "$addr"
177 }
178 ;;
179 esac
180 config_get ssid "$vif" ssid
181
182 config_get_bool bgscan "$vif" bgscan
183 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
184
185 config_get_bool antdiv "$device" diversity
186 config_get antrx "$device" rxantenna
187 config_get anttx "$device" txantenna
188 config_get_bool softled "$device" softled 1
189
190 devname="$(cat /proc/sys/dev/$device/dev_name)"
191 antgpio=
192 case "$devname" in
193 NanoStation2) antgpio=7;;
194 NanoStation5) antgpio=1;;
195 esac
196 if [ -n "$antgpio" ]; then
197 softled=0
198 config_get antenna "$device" antenna
199 case "$antenna" in
200 external) antdiv=0; antrx=1; anttx=1 ;;
201 horizontal) antdiv=0; antrx=1; anttx=1 ;;
202 vertical) antdiv=0; antrx=2; anttx=2 ;;
203 auto) antdiv=1; antrx=0; anttx=0 ;;
204 esac
205
206 [ -x "$(which gpioctl 2>/dev/null)" ] || antenna=
207 case "$antenna" in
208 horizontal|vertical|auto)
209 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
210 gpioctl "set" "$antgpio" >/dev/null 2>&1
211 ;;
212 external)
213 gpioctl "dirout" "$antgpio" >/dev/null 2>&1
214 gpioctl "clear" "$antgpio" >/dev/null 2>&1
215 ;;
216 esac
217 fi
218
219 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
220 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
221 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
222 [ -n "$softled" ] && sysctl -w dev."$device".softled="$softled" >&-
223
224 config_get distance "$device" distance
225 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
226
227 config_get rate "$vif" rate
228 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
229
230 config_get mcast_rate "$vif" mcast_rate
231 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
232
233 config_get frag "$vif" frag
234 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
235
236 config_get rts "$vif" rts
237 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
238
239 config_get_bool comp "$vif" compression
240 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
241
242 config_get_bool minrate "$vif" minrate
243 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
244
245 config_get_bool maxrate "$vif" maxrate
246 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
247
248 config_get_bool burst "$vif" bursting
249 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
250
251 config_get_bool wmm "$vif" wmm
252 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
253
254 config_get_bool xr "$vif" xr
255 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
256
257 config_get_bool ar "$vif" ar
258 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
259
260 config_get_bool turbo "$vif" turbo
261 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
262
263 config_get_bool beacon_power "$vif" beacon_power
264 [ -n "$beacon_power" ] && iwpriv "$ifname" beacon_pwr "$beacon_power"
265
266 config_get_bool doth "$vif" doth 0
267 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
268
269 config_get_bool probereq "$vif" probereq
270 [ -n "$probereq" ] && iwpriv "$ifname" probereq "$probereq"
271
272 config_get maclist "$vif" maclist
273 [ -n "$maclist" ] && {
274 # flush MAC list
275 iwpriv "$ifname" maccmd 3
276 for mac in $maclist; do
277 iwpriv "$ifname" addmac "$mac"
278 done
279 }
280
281 config_get macpolicy "$vif" macpolicy
282 case "$macpolicy" in
283 allow)
284 iwpriv "$ifname" maccmd 1
285 ;;
286 deny)
287 iwpriv "$ifname" maccmd 2
288 ;;
289 *)
290 # default deny policy if mac list exists
291 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
292 ;;
293 esac
294
295 ifconfig "$ifname" up
296
297 local net_cfg bridge
298 net_cfg="$(find_net_config "$vif")"
299 [ -z "$net_cfg" ] || {
300 bridge="$(bridge_interface "$net_cfg")"
301 config_set "$vif" bridge "$bridge"
302 start_net "$ifname" "$net_cfg"
303 }
304 [ -n "$ssid" ] && iwconfig "$ifname" essid on
305 iwconfig "$ifname" essid "$ssid"
306 set_wifi_up "$vif" "$ifname"
307
308 # TXPower settings only work if device is up already
309 # while atheros hardware theoretically is capable of per-vif (even per-packet) txpower
310 # adjustment it does not work with the current atheros hal/madwifi driver
311
312 config_get vif_txpower "$vif" txpower
313 # use vif_txpower (from wifi-iface) instead of txpower (from wifi-device) if
314 # the latter doesn't exist
315 txpower="${txpower:-$vif_txpower}"
316 [ -z "$txpower" ] || iwconfig "$ifname" txpower "${txpower%%.*}"
317
318 case "$mode" in
319 ap)
320 config_get_bool isolate "$vif" isolate 0
321 iwpriv "$ifname" ap_bridge "$((isolate^1))"
322
323 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
324 hostapd_setup_vif "$vif" madwifi || {
325 echo "enable_atheros($device): Failed to set up hostapd for interface $ifname" >&2
326 # make sure this wifi interface won't accidentally stay open without encryption
327 ifconfig "$ifname" down
328 wlanconfig "$ifname" destroy
329 continue
330 }
331 fi
332 ;;
333 wds|sta)
334 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
335 wpa_supplicant_setup_vif "$vif" madwifi || {
336 echo "enable_atheros($device): Failed to set up wpa_supplicant for interface $ifname" >&2
337 ifconfig "$ifname" down
338 wlanconfig "$ifname" destroy
339 continue
340 }
341 fi
342 ;;
343 esac
344 first=0
345 done
346 }
347
348
349 detect_atheros() {
350 cd /proc/sys/dev
351 [ -d ath ] || return
352 for dev in $(ls -d wifi* 2>&-); do
353 config_get type "$dev" type
354 devname="$(cat /proc/sys/dev/$dev/dev_name)"
355 case "$devname" in
356 NanoStation*)
357 EXTRA_DEV="
358 # Ubiquiti NanoStation features
359 option antenna auto # (auto|horizontal|vertical|external)
360 "
361 ;;
362 esac
363 [ "$type" = atheros ] && return
364 cat <<EOF
365 config wifi-device $dev
366 option type atheros
367 option channel auto
368 $EXTRA_DEV
369 # REMOVE THIS LINE TO ENABLE WIFI:
370 option disabled 1
371
372 config wifi-iface
373 option device $dev
374 option network lan
375 option mode ap
376 option ssid OpenWrt
377 option encryption none
378 EOF
379 done
380 }
This page took 0.070703 seconds and 3 git commands to generate.