madwifi: change the network scripts to use the new wds sta separation feature
[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 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 }${monitor:+$monitor}"
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                         [ -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                                 *a) agmode=11a;;
108                                 *) agmode=auto;;
109                         esac
110                         iwpriv "$ifname" mode "$agmode"
111                         iwpriv "$ifname" pureg "$pureg"
112                         iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null 
113                 }
114         
115                 config_get_bool hidden "$vif" hidden 0
116                 iwpriv "$ifname" hide_ssid "$hidden"
117
118                 config_get ff "$vif" ff
119                 if [ -n "$ff" ]; then
120                         iwpriv "$ifname" ff "$ff"
121                 fi
122
123                 config_get wdssep "$vif" wdssep
124                 [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
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                 wpa=
134                 case "$enc" in
135                         WEP|wep)
136                                 for idx in 1 2 3 4; do
137                                         config_get key "$vif" "key${idx}"
138                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
139                                 done
140                                 config_get key "$vif" key
141                                 key="${key:-1}"
142                                 case "$key" in
143                                         [1234]) iwconfig "$ifname" enc "[$key]";;
144                                         *) iwconfig "$ifname" enc "$key";;
145                                 esac
146                         ;;
147                         PSK|psk|PSK2|psk2)
148                                 config_get key "$vif" key
149                         ;;
150                 esac
151
152                 case "$mode" in
153                         wds)
154                                 config_get addr "$vif" bssid
155                                 iwpriv "$ifname" wds_add "$addr"
156                         ;;
157                         adhoc|ahdemo)
158                                 config_get addr "$vif" bssid
159                                 [ -z "$addr" ] || { 
160                                         iwconfig "$ifname" ap "$addr"
161                                 }
162                         ;;
163                 esac
164                 config_get ssid "$vif" ssid
165
166                 config_get_bool bgscan "$vif" bgscan
167                 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
168
169                 config_get_bool antdiv "$device" diversity
170                 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
171
172                 config_get antrx "$device" rxantenna
173                 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
174
175                 config_get anttx "$device" txantenna
176                 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
177
178                 config_get distance "$device" distance
179                 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
180
181                 config_get txpwr "$vif" txpower
182                 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
183
184                 config_get rate "$vif" rate
185                 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
186
187                 config_get mcast_rate "$vif" mcast_rate
188                 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
189
190                 config_get frag "$vif" frag
191                 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
192
193                 config_get rts "$vif" rts
194                 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
195
196                 config_get_bool doth "$vif" 80211h
197                 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
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 wpa 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                                 config_get_bool usepassphrase "$vif" passphrase 1
276                                 case "$enc" in
277                                         PSK|psk|PSK2|psk2)
278                                                 case "$enc" in
279                                                         PSK|psk)
280                                                                 proto='proto=WPA'
281                                                                 if [ "$usepassphrase" = "1" ]; then
282                                                                         passphrase="psk=\"${key}\""
283                                                                 else
284                                                                         passphrase="psk=${key}"
285                                                                 fi
286                                                                 ;;
287                                                         PSK2|psk2)
288                                                                 proto='proto=RSN'
289                                                                 if [ "$usepassphrase" = "1" ]; then
290                                                                         passphrase="psk=\"${key}\""
291                                                                 else
292                                                                         passphrase="psk=${key}"
293                                                                 fi
294                                                                 ;;
295                                                 esac
296                                                 cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
297 network={
298         scan_ssid=1
299         ssid="$ssid"
300         key_mgmt=WPA-PSK
301         $proto
302         $passphrase
303 }
304 EOF
305                                         ;;
306                                         WPA|wpa|WPA2|wpa2)
307                                                 #add wpa_supplicant calls here
308                                         ;;
309                                 esac
310                                 [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -B -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
311                         ;;
312                 esac
313                 first=0
314         done
315 }
316
317
318 detect_atheros() {
319         cd /proc/sys/dev
320         [ -d ath ] || return
321         for dev in $(ls -d wifi* 2>&-); do
322                 config_get type "$dev" type
323                 [ "$type" = atheros ] && return
324                 cat <<EOF
325 config wifi-device  $dev
326         option type     atheros
327         option channel  auto
328
329         # REMOVE THIS LINE TO ENABLE WIFI:
330         option disabled 1
331
332 config wifi-iface
333         option device   $dev
334         option network  lan
335         option mode     ap
336         option ssid     OpenWrt
337         option encryption none
338 EOF
339         done
340 }