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