lua: remove an unnecessary check and replace switch (GET_OPCODE(i)) as well (thx...
[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 eap_type "$vif" eap_type
85 config_get mode "$vif" mode
86
87 [ "$mode" = sta ] && config_get nosbeacon "$device" nosbeacon
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 wpa=
141 case "$enc" in
142 WEP|wep)
143 for idx in 1 2 3 4; do
144 config_get key "$vif" "key${idx}"
145 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
146 done
147 config_get key "$vif" key
148 key="${key:-1}"
149 case "$key" in
150 [1234]) iwconfig "$ifname" enc "[$key]";;
151 *) iwconfig "$ifname" enc "$key";;
152 esac
153 ;;
154 PSK|psk|PSK2|psk2)
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 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
174
175 config_get antrx "$device" rxantenna
176 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
177
178 config_get anttx "$device" txantenna
179 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
180
181 config_get distance "$device" distance
182 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
183
184 config_get txpwr "$vif" txpower
185 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
186
187 config_get rate "$vif" rate
188 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
189
190 config_get mcast_rate "$vif" mcast_rate
191 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
192
193 config_get frag "$vif" frag
194 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
195
196 config_get rts "$vif" rts
197 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
198
199 config_get_bool comp "$vif" compression
200 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
201
202 config_get_bool minrate "$vif" minrate
203 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
204
205 config_get_bool maxrate "$vif" maxrate
206 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
207
208 config_get_bool burst "$vif" bursting
209 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
210
211 config_get_bool wmm "$vif" wmm
212 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
213
214 config_get_bool xr "$vif" xr
215 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
216
217 config_get_bool ar "$vif" ar
218 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
219
220 config_get_bool turbo "$vif" turbo
221 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
222
223 config_get_bool doth "$vif" doth 0
224 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
225
226 config_get maclist "$vif" maclist
227 [ -n "$maclist" ] && {
228 # flush MAC list
229 iwpriv "$ifname" maccmd 3
230 for mac in $maclist; do
231 iwpriv "$ifname" addmac "$mac"
232 done
233 }
234
235 config_get macpolicy "$vif" macpolicy
236 case "$macpolicy" in
237 allow)
238 iwpriv "$ifname" maccmd 1
239 ;;
240 deny)
241 iwpriv "$ifname" maccmd 2
242 ;;
243 *)
244 # default deny policy if mac list exists
245 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
246 ;;
247 esac
248
249 ifconfig "$ifname" up
250 local net_cfg bridge
251 net_cfg="$(find_net_config "$vif")"
252 [ -z "$net_cfg" ] || {
253 bridge="$(bridge_interface "$net_cfg")"
254 config_set "$vif" bridge "$bridge"
255 start_net "$ifname" "$net_cfg"
256 }
257 iwconfig "$ifname" essid "$ssid"
258 set_wifi_up "$vif" "$ifname"
259 case "$mode" in
260 ap)
261 config_get_bool isolate "$vif" isolate 0
262 iwpriv "$ifname" ap_bridge "$((isolate^1))"
263
264 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
265 hostapd_setup_vif "$vif" madwifi || {
266 echo "enable_atheros($device): Failed to set up hostapd for interface $ifname" >&2
267 # make sure this wifi interface won't accidentally stay open without encryption
268 ifconfig "$ifname" down
269 wlanconfig "$ifname" destroy
270 continue
271 }
272 fi
273 ;;
274 wds|sta)
275 if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then
276 wpa_supplicant_setup_vif "$vif" madwifi || {
277 echo "enable_atheros($device): Failed to set up wpa_supplicant for interface $ifname" >&2
278 ifconfig "$ifname" down
279 wlanconfig "$ifname" destroy
280 continue
281 }
282 fi
283 ;;
284 esac
285 first=0
286 done
287 }
288
289
290 detect_atheros() {
291 cd /proc/sys/dev
292 [ -d ath ] || return
293 for dev in $(ls -d wifi* 2>&-); do
294 config_get type "$dev" type
295 [ "$type" = atheros ] && return
296 cat <<EOF
297 config wifi-device $dev
298 option type atheros
299 option channel auto
300
301 # REMOVE THIS LINE TO ENABLE WIFI:
302 option disabled 1
303
304 config wifi-iface
305 option device $dev
306 option network lan
307 option mode ap
308 option ssid OpenWrt
309 option encryption none
310 EOF
311 done
312 }
This page took 0.071623 seconds and 5 git commands to generate.