use pidfiles for hostapd/wpa_supplicant instead of killing all processes when bringin...
[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 "${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
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 agmode "$device" agmode
99 [ -z "$agmode" ] && config_get agmode "$device" mode
100
101 pureg=0
102 case "$agmode" in
103 *b) agmode=11b;;
104 *bg) agmode=11g;;
105 *g) agmode=11g; pureg=1;;
106 *gdt) agmode=11gdt;;
107 *a) agmode=11a;;
108 *adt) agmode=11adt;;
109 *ast) agmode=11ast;;
110 *fh) agmode=fh;;
111 *) agmode=auto;;
112 esac
113 iwpriv "$ifname" mode "$agmode"
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 txpwr "$vif" txpower
215 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
216
217 config_get rate "$vif" rate
218 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
219
220 config_get mcast_rate "$vif" mcast_rate
221 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
222
223 config_get frag "$vif" frag
224 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
225
226 config_get rts "$vif" rts
227 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
228
229 config_get_bool comp "$vif" compression
230 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
231
232 config_get_bool minrate "$vif" minrate
233 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
234
235 config_get_bool maxrate "$vif" maxrate
236 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
237
238 config_get_bool burst "$vif" bursting
239 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
240
241 config_get_bool wmm "$vif" wmm
242 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
243
244 config_get_bool xr "$vif" xr
245 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
246
247 config_get_bool ar "$vif" ar
248 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
249
250 config_get_bool turbo "$vif" turbo
251 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
252
253 config_get_bool doth "$vif" doth 0
254 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
255
256 config_get maclist "$vif" maclist
257 [ -n "$maclist" ] && {
258 # flush MAC list
259 iwpriv "$ifname" maccmd 3
260 for mac in $maclist; do
261 iwpriv "$ifname" addmac "$mac"
262 done
263 }
264
265 config_get macpolicy "$vif" macpolicy
266 case "$macpolicy" in
267 allow)
268 iwpriv "$ifname" maccmd 1
269 ;;
270 deny)
271 iwpriv "$ifname" maccmd 2
272 ;;
273 *)
274 # default deny policy if mac list exists
275 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
276 ;;
277 esac
278
279 ifconfig "$ifname" up
280 local net_cfg bridge
281 net_cfg="$(find_net_config "$vif")"
282 [ -z "$net_cfg" ] || {
283 bridge="$(bridge_interface "$net_cfg")"
284 config_set "$vif" bridge "$bridge"
285 start_net "$ifname" "$net_cfg"
286 }
287 [ -n "$ssid" ] && iwconfig "$ifname" essid on
288 iwconfig "$ifname" essid "$ssid"
289 set_wifi_up "$vif" "$ifname"
290 case "$mode" in
291 ap)
292 config_get_bool isolate "$vif" isolate 0
293 iwpriv "$ifname" ap_bridge "$((isolate^1))"
294
295 if [ -n "$start_hostapd" ] && eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
296 hostapd_setup_vif "$vif" madwifi || {
297 echo "enable_atheros($device): Failed to set up hostapd for interface $ifname" >&2
298 # make sure this wifi interface won't accidentally stay open without encryption
299 ifconfig "$ifname" down
300 wlanconfig "$ifname" destroy
301 continue
302 }
303 fi
304 ;;
305 wds|sta)
306 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
307 wpa_supplicant_setup_vif "$vif" madwifi || {
308 echo "enable_atheros($device): Failed to set up wpa_supplicant for interface $ifname" >&2
309 ifconfig "$ifname" down
310 wlanconfig "$ifname" destroy
311 continue
312 }
313 fi
314 ;;
315 esac
316 first=0
317 done
318 }
319
320
321 detect_atheros() {
322 cd /proc/sys/dev
323 [ -d ath ] || return
324 for dev in $(ls -d wifi* 2>&-); do
325 config_get type "$dev" type
326 devname="$(cat /proc/sys/dev/$dev/dev_name)"
327 case "$devname" in
328 NanoStation*)
329 EXTRA_DEV="
330 # Ubiquiti NanoStation features
331 option antenna auto # (auto|horizontal|vertical|external)
332 "
333 ;;
334 esac
335 [ "$type" = atheros ] && return
336 cat <<EOF
337 config wifi-device $dev
338 option type atheros
339 option channel auto
340 $EXTRA_DEV
341 # REMOVE THIS LINE TO ENABLE WIFI:
342 option disabled 1
343
344 config wifi-iface
345 option device $dev
346 option network lan
347 option mode ap
348 option ssid OpenWrt
349 option encryption none
350 EOF
351 done
352 }