]> git.enpas.org Git - openwrt.git/blob - package/madwifi/files/lib/wifi/madwifi.sh
madwifi init: set the channel after bringing up the interface (sometimes it needs...
[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=11g;;
103                         esac
104                         iwconfig "$ifname" channel 0 
105                         iwpriv "$ifname" mode "$agmode"
106                         iwpriv "$ifname" pureg "$pureg"
107                         iwconfig "$ifname" channel "$channel"
108                 }
109         
110                 config_get_bool hidden "$vif" hidden
111                 iwpriv "$ifname" hide_ssid "$hidden"
112
113                 config_get wds "$vif" wds
114                 case "$wds" in
115                         1|on|enabled) wds=1;;
116                         *) wds=0;;
117                 esac
118                 iwpriv "$ifname" wds "$wds"
119
120                 wpa=
121                 case "$enc" in
122                         WEP|wep)
123                                 for idx in 1 2 3 4; do
124                                         config_get key "$vif" "key${idx}"
125                                         iwconfig "$ifname" enc "[$idx]" "${key:-off}"
126                                 done
127                                 config_get key "$vif" key
128                                 key="${key:-1}"
129                                 case "$key" in
130                                         [1234]) iwconfig "$ifname" enc "[$key]";;
131                                         *) iwconfig "$ifname" enc "$key";;
132                                 esac
133                         ;;
134                         PSK|psk|PSK2|psk2)
135                                 config_get key "$vif" key
136                         ;;
137                 esac
138
139                 case "$mode" in
140                         wds)
141                                 config_get addr "$vif" bssid
142                                 iwpriv "$ifname" wds_add "$addr"
143                         ;;
144                         *)
145                                 config_get ssid "$vif" ssid
146                         ;;
147                         adhoc)
148                                 config_get addr "$vif" bssid
149                                 [ -z "$addr" ] || { 
150                                         iwconfig "$ifname" ap "$addr"
151                                 }
152                         ;;
153                 esac
154
155                 [ "$mode" = "sta" ] && {
156                         config_get_bool bgscan "$vif" bgscan 1
157                         iwpriv "$ifname" bgscan "$bgscan"
158                 }
159
160                 config_get_bool antdiv "$device" diversity 1
161                 sysctl -w dev."$device".diversity="$antdiv" >&-
162
163                 config_get antrx "$device" rxantenna
164                 if [ -n "$antrx" ]; then
165                         sysctl -w dev."$device".rxantenna="$antrx" >&-
166                 fi
167
168                 config_get anttx "$device" txantenna
169                 if [ -n "$anttx" ]; then
170                         sysctl -w dev."$device".txantenna="$anttx" >&-
171                 fi
172
173                 config_get distance "$device" distance
174                 if [ -n "$distance" ]; then
175                         athctrl -i "$device" -d "$distance" >&-
176                 fi
177
178                 config_get txpwr "$vif" txpower
179                 if [ -n "$txpwr" ]; then
180                         iwconfig "$ifname" txpower "${txpwr%%.*}"
181                 fi
182
183                 ifconfig "$ifname" up
184                 iwconfig "$ifname" channel "$channel"
185
186                 local net_cfg bridge
187                 net_cfg="$(find_net_config "$vif")"
188                 [ -z "$net_cfg" ] || {
189                         bridge="$(bridge_interface "$net_cfg")"
190                         config_set "$vif" bridge "$bridge"
191                         start_net "$ifname" "$net_cfg"
192                 }
193                 iwconfig "$ifname" essid "$ssid"
194                 case "$mode" in
195                         ap)
196                                 hostapd_setup_vif "$vif" madwifi || {
197                                         echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
198                                         # make sure this wifi interface won't accidentally stay open without encryption
199                                         ifconfig "$ifname" down
200                                         wlanconfig "$ifname" destroy
201                                         continue
202                                 }
203                         ;;
204                         wds|sta)
205                                 case "$enc" in 
206                                         PSK|psk|PSK2|psk2)
207                                                 case "$enc" in
208                                                         PSK|psk)
209                                                                 proto='proto=WPA';;
210                                                         PSK2|psk2)
211                                                                 proto='proto=RSN';;
212                                                 esac
213                                                 cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
214 ctrl_interface=/var/run/wpa_supplicant
215 network={
216         scan_ssid=1
217         ssid="$ssid"
218         key_mgmt=WPA-PSK
219         $proto
220         psk="$key"
221 }
222 EOF
223                                         ;;
224                                         WPA|wpa|WPA2|wpa2)
225                                                 #add wpa_supplicant calls here
226                                         ;;
227                                 esac
228                                 [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -Bw -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
229                         ;;
230                 esac
231                 first=0
232         done
233 }
234
235
236 detect_atheros() {
237         cd /proc/sys/dev
238         [ -d ath ] || return
239         for dev in $(ls -d wifi* 2>&-); do
240                 config_get type "$dev" type
241                 [ "$type" = atheros ] && return
242                 cat <<EOF
243 config wifi-device  $dev
244         option type     atheros
245         option channel  5
246 #       option diversity 1
247 #       option txantenna 0
248 #       option rxantenna 0
249 #       option distance  2000
250 # disable radio to prevent an open ap after reflashing:
251         option disabled 1
252
253
254 config wifi-iface
255         option device   $dev
256         option network  lan
257         option mode     ap
258         option ssid     OpenWrt
259         option hidden   0
260 #       option txpower  15
261 #       option bgscan   enable
262         option encryption none
263
264 EOF
265         done
266 }