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