fef63cbcc25aaa0cd1256d8e57ae22cc8d7de1f5
[15.05/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)
18                                 append $mode "$vif"
19                         ;;
20                         wds)
21                                 config_get addr "$vif" bssid
22                                 config_get ssid "$vif" ssid
23                                 [ -z "$addr" -a -n "$ssid" ] && {
24                                         config_set "$vif" wds 1
25                                         config_set "$vif" mode sta
26                                         mode="sta"
27                                         addr="$ssid"
28                                 }
29                                 ${addr:+append $mode "$vif"}
30                         ;;
31                         *) echo "$device($vif): Invalid mode, ignored."; continue;;
32                 esac
33         done
34
35         case "${adhoc:+1}:${sta:+1}:${ap+1}" in
36                 # valid mode combinations
37                 1::) wds="";;
38                 1::1);;
39                 :1:1)config_set "$device" nosbeacon 1;; # AP+STA, can't use beacon timers for STA
40                 :1:);;
41                 ::1);;
42                 ::);;
43                 *) echo "$device: Invalid mode combination in config"; return 1;;
44         esac
45
46         config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${ahdemo:+$ahdemo }${sta:+$sta }${wds:+$wds }"
47 }
48
49
50 disable_atheros() (
51         local device="$1"
52
53         set_wifi_down "$device"
54         # kill all running hostapd and wpa_supplicant processes that
55         # are running on atheros vifs 
56         for pid in `pidof hostapd wpa_supplicant`; do
57                 grep ath /proc/$pid/cmdline >/dev/null && \
58                         kill $pid
59         done
60         
61         include /lib/network
62         cd /proc/sys/net
63         for dev in *; do
64                 grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
65                         ifconfig "$dev" down 
66                         unbridge "$dev"
67                         wlanconfig "$dev" destroy
68                 }
69         done
70         return 0
71 )
72
73 enable_atheros() {
74         local device="$1"
75         config_get channel "$device" channel
76         config_get vifs "$device" vifs
77
78         [ auto = "$channel" ] && channel=0
79
80         local first=1
81         for vif in $vifs; do
82                 nosbeacon=
83                 config_get ifname "$vif" ifname
84                 config_get enc "$vif" encryption
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                 [ "$first" = 1 ] && {
98                         # only need to change freq band and channel on the first vif
99                         config_get agmode "$device" agmode
100                         pureg=0
101                         case "$agmode" in
102                                 *b) agmode=11b;;
103                                 *bg) agmode=11g;;
104                                 *g) agmode=11g; pureg=1;;
105                                 *a) agmode=11a;;
106                                 *) agmode=auto;;
107                         esac
108                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
109                         ifconfig "$ifname" up
110                         sleep 1
111                         iwpriv "$ifname" mode "$agmode"
112                         iwpriv "$ifname" pureg "$pureg"
113                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
114                 }
115         
116                 config_get_bool hidden "$vif" hidden 0
117                 iwpriv "$ifname" hide_ssid "$hidden"
118
119                 config_get ff "$vif" ff
120                 if [ -n "$ff" ]; then
121                         iwpriv "$ifname" ff "$ff"
122                 fi
123
124                 config_get wds "$vif" wds
125                 case "$wds" in
126                         1|on|enabled) wds=1;;
127                         *) wds=0;;
128                 esac
129                 iwpriv "$ifname" wds "$wds"
130
131                 wpa=
132                 case "$enc" in
133                         WEP|wep)
134                                 for idx in 1 2 3 4; do
135                                         config_get key "$vif" "key${idx}"
136                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
137                                 done
138                                 config_get key "$vif" key
139                                 key="${key:-1}"
140                                 case "$key" in
141                                         [1234]) iwconfig "$ifname" enc "[$key]";;
142                                         *) iwconfig "$ifname" enc "$key";;
143                                 esac
144                         ;;
145                         PSK|psk|PSK2|psk2)
146                                 config_get key "$vif" key
147                         ;;
148                 esac
149
150                 case "$mode" in
151                         wds)
152                                 config_get addr "$vif" bssid
153                                 iwpriv "$ifname" wds_add "$addr"
154                         ;;
155                         adhoc|ahdemo)
156                                 config_get addr "$vif" bssid
157                                 [ -z "$addr" ] || { 
158                                         iwconfig "$ifname" ap "$addr"
159                                 }
160                         ;;
161                 esac
162                 config_get ssid "$vif" ssid
163
164                 config_get_bool bgscan "$vif" bgscan
165                 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
166
167                 config_get_bool antdiv "$device" diversity
168                 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
169
170                 config_get antrx "$device" rxantenna
171                 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
172
173                 config_get anttx "$device" txantenna
174                 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
175
176                 config_get distance "$device" distance
177                 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
178
179                 config_get txpwr "$vif" txpower
180                 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
181
182                 config_get rate "$vif" rate
183                 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
184
185                 config_get mcast_rate "$vif" mcast_rate
186                 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
187
188                 config_get frag "$vif" frag
189                 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
190
191                 config_get rts "$vif" rts
192                 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
193
194                 config_get_bool doth "$vif" 80211h
195                 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
196
197                 config_get_bool comp "$vif" compression
198                 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
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                 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
243
244                 local net_cfg bridge
245                 net_cfg="$(find_net_config "$vif")"
246                 [ -z "$net_cfg" ] || {
247                         bridge="$(bridge_interface "$net_cfg")"
248                         config_set "$vif" bridge "$bridge"
249                         start_net "$ifname" "$net_cfg"
250                 }
251                 iwconfig "$ifname" essid "$ssid"
252                 set_wifi_up "$vif" "$ifname"
253                 case "$mode" in
254                         ap)
255                                 config_get_bool isolate "$vif" isolate 0
256                                 iwpriv "$ifname" ap_bridge "$((isolate^1))"
257
258                                 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
259                                         hostapd_setup_vif "$vif" madwifi || {
260                                                 echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
261                                                 # make sure this wifi interface won't accidentally stay open without encryption
262                                                 ifconfig "$ifname" down
263                                                 wlanconfig "$ifname" destroy
264                                                 continue
265                                         }
266                                 fi
267                         ;;
268                         wds|sta)
269                                 config_get_bool usepassphrase "$vif" passphrase 1
270                                 case "$enc" in
271                                         PSK|psk|PSK2|psk2)
272                                                 case "$enc" in
273                                                         PSK|psk)
274                                                                 proto='proto=WPA'
275                                                                 if [ "$usepassphrase" = "1" ]; then
276                                                                         passphrase="psk=\"${key}\""
277                                                                 else
278                                                                         passphrase="psk=${key}"
279                                                                 fi
280                                                                 ;;
281                                                         PSK2|psk2)
282                                                                 proto='proto=RSN'
283                                                                 if [ "$usepassphrase" = "1" ]; then
284                                                                         passphrase="psk=\"${key}\""
285                                                                 else
286                                                                         passphrase="psk=${key}"
287                                                                 fi
288                                                                 ;;
289                                                 esac
290                                                 cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
291 network={
292         scan_ssid=1
293         ssid="$ssid"
294         key_mgmt=WPA-PSK
295         $proto
296         $passphrase
297 }
298 EOF
299                                         ;;
300                                         WPA|wpa|WPA2|wpa2)
301                                                 #add wpa_supplicant calls here
302                                         ;;
303                                 esac
304                                 [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -B -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
305                         ;;
306                 esac
307                 first=0
308         done
309 }
310
311
312 detect_atheros() {
313         cd /proc/sys/dev
314         [ -d ath ] || return
315         for dev in $(ls -d wifi* 2>&-); do
316                 config_get type "$dev" type
317                 [ "$type" = atheros ] && return
318                 cat <<EOF
319 config wifi-device  $dev
320         option type     atheros
321         option channel  auto
322
323         # REMOVE THIS LINE TO ENABLE WIFI:
324         option disabled 1
325
326 config wifi-iface
327         option device   $dev
328         option network  lan
329         option mode     ap
330         option ssid     OpenWrt
331         option encryption none
332 EOF
333         done
334 }