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