fix isolate mode
[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|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)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 }${sta:+$sta }${wds:+$wds }"
46 }
47
48
49 disable_atheros() (
50         local device="$1"
51
52         # kill all running hostapd and wpa_supplicant processes that
53         # are running on atheros vifs 
54         for pid in `pidof hostapd wpa_supplicant`; do
55                 grep ath /proc/$pid/cmdline >/dev/null && \
56                         kill $pid
57         done
58         
59         include /lib/network
60         cd /proc/sys/net
61         for dev in *; do
62                 grep "$device" "$dev/%parent" >/dev/null 2>/dev/null && {
63                         ifconfig "$dev" down 
64                         unbridge "$dev"
65                         wlanconfig "$dev" destroy
66                 }
67         done
68         return 0
69 )
70
71 enable_atheros() {
72         config_get channel "$device" channel
73         config_get vifs "$device" vifs
74         
75         disable_atheros "$device"
76         local first=1
77         for vif in $vifs; do
78                 nosbeacon=
79                 config_get ifname "$vif" ifname
80                 config_get enc "$vif" encryption
81                 config_get mode "$vif" mode
82                 
83                 [ "$mode" = sta ] && config_get nosbeacon "$device" nosbeacon
84                 
85                 config_get ifname "$vif" ifname
86                 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
87                 [ $? -ne 0 ] && {
88                         echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
89                         continue
90                 }
91                 config_set "$vif" ifname "$ifname"
92
93                 [ "$first" = 1 ] && {
94                         # only need to change freq band and channel on the first vif
95                         config_get agmode "$device" mode
96                         pureg=0
97                         case "$agmode" in
98                                 *b) agmode=11b;;
99                                 *bg) agmode=11g;;
100                                 *g) agmode=11g; pureg=1;;
101                                 *a) agmode=11a;;
102                                 *) agmode=auto;;
103                         esac
104                         iwconfig "$ifname" channel 0 >/dev/null 2>/dev/null 
105                         ifconfig "$ifname" up
106                         iwpriv "$ifname" mode "$agmode"
107                         iwpriv "$ifname" pureg "$pureg"
108                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
109                 }
110         
111                 config_get_bool hidden "$vif" hidden
112                 iwpriv "$ifname" hide_ssid "$hidden"
113
114                 config_get wds "$vif" wds
115                 case "$wds" in
116                         1|on|enabled) wds=1;;
117                         *) wds=0;;
118                 esac
119                 iwpriv "$ifname" wds "$wds"
120
121                 wpa=
122                 case "$enc" in
123                         WEP|wep)
124                                 for idx in 1 2 3 4; do
125                                         config_get key "$vif" "key${idx}"
126                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
127                                 done
128                                 config_get key "$vif" key
129                                 key="${key:-1}"
130                                 case "$key" in
131                                         [1234]) iwconfig "$ifname" enc "[$key]";;
132                                         *) iwconfig "$ifname" enc "$key";;
133                                 esac
134                         ;;
135                         PSK|psk|PSK2|psk2)
136                                 config_get key "$vif" key
137                         ;;
138                 esac
139
140                 case "$mode" in
141                         wds)
142                                 config_get addr "$vif" bssid
143                                 iwpriv "$ifname" wds_add "$addr"
144                         ;;
145                         adhoc)
146                                 config_get addr "$vif" bssid
147                                 [ -z "$addr" ] || { 
148                                         iwconfig "$ifname" ap "$addr"
149                                 }
150                         ;;
151                         *)
152                                 config_get ssid "$vif" ssid
153                         ;;
154                 esac
155
156                 [ "$mode" = "sta" ] && {
157                         config_get_bool bgscan "$vif" bgscan 1
158                         iwpriv "$ifname" bgscan "$bgscan"
159                 }
160
161                 config_get_bool antdiv "$device" diversity 1
162                 sysctl -w dev."$device".diversity="$antdiv" >&-
163
164                 config_get antrx "$device" rxantenna
165                 if [ -n "$antrx" ]; then
166                         sysctl -w dev."$device".rxantenna="$antrx" >&-
167                 fi
168
169                 config_get anttx "$device" txantenna
170                 if [ -n "$anttx" ]; then
171                         sysctl -w dev."$device".txantenna="$anttx" >&-
172                 fi
173
174                 config_get distance "$device" distance
175                 if [ -n "$distance" ]; then
176                         athctrl -i "$device" -d "$distance" >&-
177                 fi
178
179                 config_get txpwr "$vif" txpower
180                 if [ -n "$txpwr" ]; then
181                         iwconfig "$ifname" txpower "${txpwr%%.*}"
182                 fi
183
184                 ifconfig "$ifname" up
185                 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
186
187                 local net_cfg bridge
188                 net_cfg="$(find_net_config "$vif")"
189                 [ -z "$net_cfg" ] || {
190                         bridge="$(bridge_interface "$net_cfg")"
191                         config_set "$vif" bridge "$bridge"
192                         start_net "$ifname" "$net_cfg"
193                 }
194                 iwconfig "$ifname" essid "$ssid"
195                 case "$mode" in
196                         ap)
197                                 config_get_bool isolate "$vif" isolate 0
198                                 iwpriv "$ifname" ap_bridge "$((isolate^1))"
199
200                                 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
201                                         hostapd_setup_vif "$vif" madwifi || {
202                                                 echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
203                                                 # make sure this wifi interface won't accidentally stay open without encryption
204                                                 ifconfig "$ifname" down
205                                                 wlanconfig "$ifname" destroy
206                                                 continue
207                                         }
208                                 fi
209                         ;;
210                         wds|sta)
211                                 case "$enc" in 
212                                         PSK|psk|PSK2|psk2)
213                                                 case "$enc" in
214                                                         PSK|psk)
215                                                                 proto='proto=WPA';;
216                                                         PSK2|psk2)
217                                                                 proto='proto=RSN';;
218                                                 esac
219                                                 cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
220 ctrl_interface=/var/run/wpa_supplicant
221 network={
222         scan_ssid=1
223         ssid="$ssid"
224         key_mgmt=WPA-PSK
225         $proto
226         psk="$key"
227 }
228 EOF
229                                         ;;
230                                         WPA|wpa|WPA2|wpa2)
231                                                 #add wpa_supplicant calls here
232                                         ;;
233                                 esac
234                                 [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -Bw -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
235                         ;;
236                 esac
237                 first=0
238         done
239 }
240
241
242 detect_atheros() {
243         cd /proc/sys/dev
244         [ -d ath ] || return
245         for dev in $(ls -d wifi* 2>&-); do
246                 config_get type "$dev" type
247                 [ "$type" = atheros ] && return
248                 cat <<EOF
249 config wifi-device  $dev
250         option type     atheros
251         option channel  5
252 #       option diversity 1
253 #       option txantenna 0
254 #       option rxantenna 0
255 #       option distance  2000
256 # disable radio to prevent an open ap after reflashing:
257         option disabled 1
258
259
260 config wifi-iface
261         option device   $dev
262         option network  lan
263         option mode     ap
264         option ssid     OpenWrt
265         option hidden   0
266 #       option txpower  15
267 #       option bgscan   enable
268         option encryption none
269
270 EOF
271         done
272 }