madwifi: add patch for doing ad-hoc and master mode at the same (including beacon...
[openwrt/svn-archive/archive.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 # 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 config_get channel "$device" channel
74 config_get vifs "$device" vifs
75
76 disable_atheros "$device"
77 local first=1
78 for vif in $vifs; do
79 nosbeacon=
80 config_get ifname "$vif" ifname
81 config_get enc "$vif" encryption
82 config_get mode "$vif" mode
83
84 [ "$mode" = sta ] && config_get nosbeacon "$device" nosbeacon
85
86 config_get ifname "$vif" ifname
87 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
88 [ $? -ne 0 ] && {
89 echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
90 continue
91 }
92 config_set "$vif" ifname "$ifname"
93
94 [ "$first" = 1 ] && {
95 # only need to change freq band and channel on the first vif
96 config_get agmode "$device" mode
97 pureg=0
98 case "$agmode" in
99 *b) agmode=11b;;
100 *bg) agmode=11g;;
101 *g) agmode=11g; pureg=1;;
102 *a) agmode=11a;;
103 *) agmode=auto;;
104 esac
105 iwconfig "$ifname" channel 0 >/dev/null 2>/dev/null
106 ifconfig "$ifname" up
107 iwpriv "$ifname" mode "$agmode"
108 iwpriv "$ifname" pureg "$pureg"
109 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
110 }
111
112 config_get_bool hidden "$vif" hidden
113 iwpriv "$ifname" hide_ssid "$hidden"
114
115 config_get wds "$vif" wds
116 case "$wds" in
117 1|on|enabled) wds=1;;
118 *) wds=0;;
119 esac
120 iwpriv "$ifname" wds "$wds"
121
122 wpa=
123 case "$enc" in
124 WEP|wep)
125 for idx in 1 2 3 4; do
126 config_get key "$vif" "key${idx}"
127 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
128 done
129 config_get key "$vif" key
130 key="${key:-1}"
131 case "$key" in
132 [1234]) iwconfig "$ifname" enc "[$key]";;
133 *) iwconfig "$ifname" enc "$key";;
134 esac
135 ;;
136 PSK|psk|PSK2|psk2)
137 config_get key "$vif" key
138 ;;
139 esac
140
141 case "$mode" in
142 wds)
143 config_get addr "$vif" bssid
144 iwpriv "$ifname" wds_add "$addr"
145 ;;
146 adhoc|ahdemo)
147 config_get addr "$vif" bssid
148 [ -z "$addr" ] || {
149 iwconfig "$ifname" ap "$addr"
150 }
151 ;;
152 esac
153 config_get ssid "$vif" ssid
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" >/dev/null 2>/dev/null
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 config_get_bool isolate "$vif" isolate 0
197 iwpriv "$ifname" ap_bridge "$((isolate^1))"
198
199 if eval "type hostapd_setup_vif" 2>/dev/null >/dev/null; then
200 hostapd_setup_vif "$vif" madwifi || {
201 echo "enable_atheros($device): Failed to set up wpa for interface $ifname" >&2
202 # make sure this wifi interface won't accidentally stay open without encryption
203 ifconfig "$ifname" down
204 wlanconfig "$ifname" destroy
205 continue
206 }
207 fi
208 ;;
209 wds|sta)
210 case "$enc" in
211 PSK|psk|PSK2|psk2)
212 case "$enc" in
213 PSK|psk)
214 proto='proto=WPA';;
215 PSK2|psk2)
216 proto='proto=RSN';;
217 esac
218 cat > /var/run/wpa_supplicant-$ifname.conf <<EOF
219 ctrl_interface=/var/run/wpa_supplicant
220 network={
221 scan_ssid=1
222 ssid="$ssid"
223 key_mgmt=WPA-PSK
224 $proto
225 psk="$key"
226 }
227 EOF
228 ;;
229 WPA|wpa|WPA2|wpa2)
230 #add wpa_supplicant calls here
231 ;;
232 esac
233 [ -z "$proto" ] || wpa_supplicant ${bridge:+ -b $bridge} -Bw -D wext -i "$ifname" -c /var/run/wpa_supplicant-$ifname.conf
234 ;;
235 esac
236 first=0
237 done
238 }
239
240
241 detect_atheros() {
242 cd /proc/sys/dev
243 [ -d ath ] || return
244 for dev in $(ls -d wifi* 2>&-); do
245 config_get type "$dev" type
246 [ "$type" = atheros ] && return
247 cat <<EOF
248 config wifi-device $dev
249 option type atheros
250 option channel 5
251 # option diversity 1
252 # option txantenna 0
253 # option rxantenna 0
254 # option distance 2000
255 # disable radio to prevent an open ap after reflashing:
256 option disabled 1
257
258
259 config wifi-iface
260 option device $dev
261 option network lan
262 option mode ap
263 option ssid OpenWrt
264 option hidden 0
265 # option txpower 15
266 # option bgscan enable
267 option encryption none
268
269 EOF
270 done
271 }