010391e35a7b82db3cf809209f66839b26be90fb
[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 # 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 nosbeacon=
82 config_get ifname "$vif" ifname
83 config_get enc "$vif" encryption
84 config_get mode "$vif" mode
85
86 [ "$mode" = sta ] && config_get nosbeacon "$device" nosbeacon
87
88 config_get ifname "$vif" ifname
89 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
90 [ $? -ne 0 ] && {
91 echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
92 continue
93 }
94 config_set "$vif" ifname "$ifname"
95
96 [ "$first" = 1 ] && {
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 *a) agmode=11a;;
107 *) agmode=auto;;
108 esac
109 iwpriv "$ifname" mode "$agmode"
110 iwpriv "$ifname" pureg "$pureg"
111 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
112 }
113
114 config_get_bool hidden "$vif" hidden 0
115 iwpriv "$ifname" hide_ssid "$hidden"
116
117 config_get ff "$vif" ff
118 if [ -n "$ff" ]; then
119 iwpriv "$ifname" ff "$ff"
120 fi
121
122 config_get wdssep "$vif" wdssep
123 [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
124
125 config_get wds "$vif" wds
126 case "$wds" in
127 1|on|enabled) wds=1;;
128 *) wds=0;;
129 esac
130 iwpriv "$ifname" wds "$wds"
131
132 wpa=
133 case "$enc" in
134 WEP|wep)
135 for idx in 1 2 3 4; do
136 config_get key "$vif" "key${idx}"
137 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
138 done
139 config_get key "$vif" key
140 key="${key:-1}"
141 case "$key" in
142 [1234]) iwconfig "$ifname" enc "[$key]";;
143 *) iwconfig "$ifname" enc "$key";;
144 esac
145 ;;
146 PSK|psk|PSK2|psk2)
147 config_get key "$vif" key
148 ;;
149 esac
150
151 case "$mode" in
152 adhoc|ahdemo)
153 config_get addr "$vif" bssid
154 [ -z "$addr" ] || {
155 iwconfig "$ifname" ap "$addr"
156 }
157 ;;
158 esac
159 config_get ssid "$vif" ssid
160
161 config_get_bool bgscan "$vif" bgscan
162 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
163
164 config_get_bool antdiv "$device" diversity
165 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
166
167 config_get antrx "$device" rxantenna
168 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
169
170 config_get anttx "$device" txantenna
171 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
172
173 config_get distance "$device" distance
174 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
175
176 config_get txpwr "$vif" txpower
177 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
178
179 config_get rate "$vif" rate
180 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
181
182 config_get mcast_rate "$vif" mcast_rate
183 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
184
185 config_get frag "$vif" frag
186 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
187
188 config_get rts "$vif" rts
189 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
190
191 config_get_bool comp "$vif" compression
192 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
193
194 config_get_bool minrate "$vif" minrate
195 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
196
197 config_get_bool maxrate "$vif" maxrate
198 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
199
200 config_get_bool burst "$vif" bursting
201 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
202
203 config_get_bool wmm "$vif" wmm
204 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
205
206 config_get_bool xr "$vif" xr
207 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
208
209 config_get_bool ar "$vif" ar
210 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
211
212 config_get_bool turbo "$vif" turbo
213 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
214
215 config_get_bool doth "$vif" doth 0
216 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
217
218 config_get maclist "$vif" maclist
219 [ -n "$maclist" ] && {
220 # flush MAC list
221 iwpriv "$ifname" maccmd 3
222 for mac in $maclist; do
223 iwpriv "$ifname" addmac "$mac"
224 done
225 }
226
227 config_get macpolicy "$vif" macpolicy
228 case "$macpolicy" in
229 allow)
230 iwpriv "$ifname" maccmd 1
231 ;;
232 deny)
233 iwpriv "$ifname" maccmd 2
234 ;;
235 *)
236 # default deny policy if mac list exists
237 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
238 ;;
239 esac
240
241 ifconfig "$ifname" up
242 local net_cfg bridge
243 net_cfg="$(find_net_config "$vif")"
244 [ -z "$net_cfg" ] || {
245 bridge="$(bridge_interface "$net_cfg")"
246 config_set "$vif" bridge "$bridge"
247 start_net "$ifname" "$net_cfg"
248 }
249 iwconfig "$ifname" essid "$ssid"
250 set_wifi_up "$vif" "$ifname"
251 case "$mode" in
252 ap)
253 config_get_bool isolate "$vif" isolate 0
254 iwpriv "$ifname" ap_bridge "$((isolate^1))"
255
256 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
257 hostapd_setup_vif "$vif" madwifi || {
258 echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
259 # make sure this wifi interface won't accidentally stay open without encryption
260 ifconfig "$ifname" down
261 wlanconfig "$ifname" destroy
262 continue
263 }
264 fi
265 ;;
266 wds|sta)
267 config_get_bool usepassphrase "$vif" passphrase 1
268 case "$enc" in
269 PSK|psk|PSK2|psk2)
270 case "$enc" in
271 PSK|psk)
272 proto='proto=WPA'
273 if [ "$usepassphrase" = "1" ]; then
274 passphrase="psk=\"${key}\""
275 else
276 passphrase="psk=${key}"
277 fi
278 ;;
279 PSK2|psk2)
280 proto='proto=RSN'
281 if [ "$usepassphrase" = "1" ]; then
282 passphrase="psk=\"${key}\""
283 else
284 passphrase="psk=${key}"
285 fi
286 ;;
287 esac
288 cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
289 network={
290 scan_ssid=1
291 ssid="$ssid"
292 key_mgmt=WPA-PSK
293 $proto
294 $passphrase
295 }
296 EOF
297 ;;
298 WPA|wpa|WPA2|wpa2)
299 #add wpa_supplicant calls here
300 ;;
301 esac
302 [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -B -D madwifi -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
303 ;;
304 esac
305 first=0
306 done
307 }
308
309
310 detect_atheros() {
311 cd /proc/sys/dev
312 [ -d ath ] || return
313 for dev in $(ls -d wifi* 2>&-); do
314 config_get type "$dev" type
315 [ "$type" = atheros ] && return
316 cat <<EOF
317 config wifi-device $dev
318 option type atheros
319 option channel auto
320
321 # REMOVE THIS LINE TO ENABLE WIFI:
322 option disabled 1
323
324 config wifi-iface
325 option device $dev
326 option network lan
327 option mode ap
328 option ssid OpenWrt
329 option encryption none
330 EOF
331 done
332 }
This page took 0.05727 seconds and 3 git commands to generate.