add ssize_t definition to site files, fix up size for x86_64
[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 ssid "$vif" ssid
22 [ -z "$ssid" ] && continue
23
24 config_set "$vif" wds 1
25 config_set "$vif" mode sta
26 mode="sta"
27 addr="$ssid"
28 ${addr:+append $mode "$vif"}
29 ;;
30 *) echo "$device($vif): Invalid mode, ignored."; continue;;
31 esac
32 done
33
34 case "${adhoc:+1}:${sta:+1}:${ap+1}" in
35 # valid mode combinations
36 1::) wds="";;
37 1::1);;
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 }${ahdemo:+$ahdemo }${sta:+$sta }${wds:+$wds }${monitor:+$monitor}"
46 }
47
48
49 disable_atheros() (
50 local device="$1"
51
52 set_wifi_down "$device"
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 local device="$1"
74 config_get channel "$device" channel
75 config_get vifs "$device" vifs
76
77 [ auto = "$channel" ] && channel=0
78
79 local first=1
80 for vif in $vifs; do
81 nosbeacon=
82 config_get ifname "$vif" ifname
83 config_get enc "$vif" encryption
84 config_get mode "$vif" mode
85
86 [ "$mode" = sta ] && config_get nosbeacon "$device" nosbeacon
87
88 config_get ifname "$vif" ifname
89 ifname=$(wlanconfig "$ifname" create wlandev "$device" wlanmode "$mode" ${nosbeacon:+nosbeacon})
90 [ $? -ne 0 ] && {
91 echo "enable_atheros($device): Failed to set up $mode vif $ifname" >&2
92 continue
93 }
94 config_set "$vif" ifname "$ifname"
95
96 [ "$first" = 1 ] && {
97 # only need to change freq band and channel on the first vif
98 config_get agmode "$device" agmode
99 [ -z "$agmode" ] && config_get agmode "$device" mode
100
101 pureg=0
102 case "$agmode" in
103 *b) agmode=11b;;
104 *bg) agmode=11g;;
105 *g) agmode=11g; pureg=1;;
106 *a) agmode=11a;;
107 *) agmode=auto;;
108 esac
109 iwpriv "$ifname" mode "$agmode"
110 iwpriv "$ifname" pureg "$pureg"
111 iwconfig "$ifname" channel "$channel" >/dev/null 2>/dev/null
112 }
113
114 config_get_bool hidden "$vif" hidden 0
115 iwpriv "$ifname" hide_ssid "$hidden"
116
117 config_get ff "$vif" ff
118 if [ -n "$ff" ]; then
119 iwpriv "$ifname" ff "$ff"
120 fi
121
122 config_get wds "$vif" wds
123 case "$wds" in
124 1|on|enabled) wds=1;;
125 *) wds=0;;
126 esac
127 iwpriv "$ifname" wds "$wds"
128
129 [ "$mode" = ap -a "$wds" = 1 ] && {
130 config_get_bool wdssep "$vif" wdssep 1
131 [ -n "$wdssep" ] && iwpriv "$ifname" wdssep "$wdssep"
132 }
133
134 wpa=
135 case "$enc" in
136 WEP|wep)
137 for idx in 1 2 3 4; do
138 config_get key "$vif" "key${idx}"
139 iwconfig "$ifname" enc "[$idx]" "${key:-off}"
140 done
141 config_get key "$vif" key
142 key="${key:-1}"
143 case "$key" in
144 [1234]) iwconfig "$ifname" enc "[$key]";;
145 *) iwconfig "$ifname" enc "$key";;
146 esac
147 ;;
148 PSK|psk|PSK2|psk2)
149 config_get key "$vif" key
150 ;;
151 esac
152
153 case "$mode" in
154 adhoc|ahdemo)
155 config_get addr "$vif" bssid
156 [ -z "$addr" ] || {
157 iwconfig "$ifname" ap "$addr"
158 }
159 ;;
160 esac
161 config_get ssid "$vif" ssid
162
163 config_get_bool bgscan "$vif" bgscan
164 [ -n "$bgscan" ] && iwpriv "$ifname" bgscan "$bgscan"
165
166 config_get_bool antdiv "$device" diversity
167 [ -n "$antdiv" ] && sysctl -w dev."$device".diversity="$antdiv" >&-
168
169 config_get antrx "$device" rxantenna
170 [ -n "$antrx" ] && sysctl -w dev."$device".rxantenna="$antrx" >&-
171
172 config_get anttx "$device" txantenna
173 [ -n "$anttx" ] && sysctl -w dev."$device".txantenna="$anttx" >&-
174
175 config_get distance "$device" distance
176 [ -n "$distance" ] && athctrl -i "$device" -d "$distance" >&-
177
178 config_get txpwr "$vif" txpower
179 [ -n "$txpwr" ] && iwconfig "$ifname" txpower "${txpwr%%.*}"
180
181 config_get rate "$vif" rate
182 [ -n "$rate" ] && iwconfig "$ifname" rate "${rate%%.*}"
183
184 config_get mcast_rate "$vif" mcast_rate
185 [ -n "$mcast_rate" ] && iwpriv "$ifname" mcast_rate "${mcast_rate%%.*}"
186
187 config_get frag "$vif" frag
188 [ -n "$frag" ] && iwconfig "$ifname" frag "${frag%%.*}"
189
190 config_get rts "$vif" rts
191 [ -n "$rts" ] && iwconfig "$ifname" rts "${rts%%.*}"
192
193 config_get_bool comp "$vif" compression
194 [ -n "$comp" ] && iwpriv "$ifname" compression "$comp"
195
196 config_get_bool minrate "$vif" minrate
197 [ -n "$minrate" ] && iwpriv "$ifname" minrate "$minrate"
198
199 config_get_bool maxrate "$vif" maxrate
200 [ -n "$maxrate" ] && iwpriv "$ifname" maxrate "$maxrate"
201
202 config_get_bool burst "$vif" bursting
203 [ -n "$burst" ] && iwpriv "$ifname" burst "$burst"
204
205 config_get_bool wmm "$vif" wmm
206 [ -n "$wmm" ] && iwpriv "$ifname" wmm "$wmm"
207
208 config_get_bool xr "$vif" xr
209 [ -n "$xr" ] && iwpriv "$ifname" xr "$xr"
210
211 config_get_bool ar "$vif" ar
212 [ -n "$ar" ] && iwpriv "$ifname" ar "$ar"
213
214 config_get_bool turbo "$vif" turbo
215 [ -n "$turbo" ] && iwpriv "$ifname" turbo "$turbo"
216
217 config_get_bool doth "$vif" doth 0
218 [ -n "$doth" ] && iwpriv "$ifname" doth "$doth"
219
220 config_get maclist "$vif" maclist
221 [ -n "$maclist" ] && {
222 # flush MAC list
223 iwpriv "$ifname" maccmd 3
224 for mac in $maclist; do
225 iwpriv "$ifname" addmac "$mac"
226 done
227 }
228
229 config_get macpolicy "$vif" macpolicy
230 case "$macpolicy" in
231 allow)
232 iwpriv "$ifname" maccmd 1
233 ;;
234 deny)
235 iwpriv "$ifname" maccmd 2
236 ;;
237 *)
238 # default deny policy if mac list exists
239 [ -n "$maclist" ] && iwpriv "$ifname" maccmd 2
240 ;;
241 esac
242
243 ifconfig "$ifname" up
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 madwifi -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 }
This page took 0.081856 seconds and 5 git commands to generate.