5350df44d4d48eb25dfe59759c18121324bfaf07
[openwrt/svn-archive/archive.git] / package / broadcom-wl / files / lib / wifi / broadcom.sh
1 append DRIVERS "broadcom"
2
3 scan_broadcom() {
4 local device="$1"
5 local wds
6 local adhoc sta apmode mon
7 local adhoc_if sta_if ap_if mon_if
8 local _c=0
9
10 config_get vifs "$device" vifs
11 for vif in $vifs; do
12 config_get mode "$vif" mode
13 _c=$(($_c + 1))
14 case "$mode" in
15 adhoc)
16 adhoc=1
17 adhoc_if="$vif"
18 ;;
19 sta)
20 sta=1
21 sta_if="$vif"
22 ;;
23 ap)
24 apmode=1
25 ap_if="${ap_if:+$ap_if }$vif"
26 ;;
27 wds)
28 config_get addr "$vif" bssid
29 [ -z "$addr" ] || {
30 addr=$(echo "$addr" | tr 'A-F' 'a-f')
31 append wds "$addr"
32 }
33 ;;
34 monitor)
35 mon=1
36 mon_if="$vif"
37 ;;
38 *) echo "$device($vif): Invalid mode";;
39 esac
40 done
41 config_set "$device" wds "$wds"
42
43 local _c=
44 for vif in ${adhoc_if:-$sta_if $ap_if $mon_if}; do
45 config_set "$vif" ifname "${device}${_c:+.$_c}"
46 _c=$((${_c:-0} + 1))
47 done
48 config_set "$device" vifs "${adhoc_if:-$sta_if $ap_if $mon_if}"
49
50 ifdown="down"
51 for vif in 0 1 2 3; do
52 append ifdown "vif $vif" "$N"
53 append ifdown "enabled 0" "$N"
54 done
55
56 ap=1
57 infra=1
58 if [ "$_c" -gt 1 ]; then
59 mssid=1
60 else
61 mssid=
62 fi
63 apsta=0
64 radio=1
65 monitor=0
66 passive=0
67 case "$adhoc:$sta:$apmode:$mon" in
68 1*)
69 ap=0
70 mssid=
71 infra=0
72 ;;
73 :1:1:)
74 apsta=1
75 wet=1
76 ;;
77 :1::)
78 wet=1
79 ap=0
80 mssid=
81 ;;
82 :::1)
83 wet=1
84 ap=0
85 mssid=
86 monitor=1
87 passive=1
88 ;;
89 ::)
90 radio=0
91 ;;
92 esac
93 }
94
95 disable_broadcom() {
96 local device="$1"
97 set_wifi_down "$device"
98 wlc ifname "$device" down
99 (
100 include /lib/network
101
102 # make sure the interfaces are down and removed from all bridges
103 for dev in $device ${device}.1 ${device}.2 ${device}.3; do
104 ifconfig "$dev" down 2>/dev/null >/dev/null && {
105 unbridge "$dev"
106 }
107 done
108 )
109 true
110 }
111
112 enable_broadcom() {
113 local device="$1"
114 local _c
115 config_get channel "$device" channel
116 config_get country "$device" country
117 config_get maxassoc "$device" maxassoc
118 config_get wds "$device" wds
119 config_get vifs "$device" vifs
120 config_get distance "$device" distance
121 config_get slottime "$device" slottime
122 config_get rxantenna "$device" rxantenna
123 config_get txantenna "$device" txantenna
124 config_get_bool frameburst "$device" frameburst
125 config_get macfilter "$device" macfilter
126 config_get maclist "$device" maclist
127 config_get macaddr "$device" macaddr
128 config_get txpower "$device" txpower
129 config_get frag "$device" frag
130 config_get rts "$device" rts
131 config_get hwmode "$device" hwmode
132 local vif_pre_up vif_post_up vif_do_up vif_txpower
133 local doth=0
134 local wmm=0
135
136 _c=0
137 nas="$(which nas)"
138 [ -n "$nas" ] && nas="start-stop-daemon -S -b -x $nas -- "
139 nas_cmd=
140 if_up=
141
142 [ -z "$slottime" ] && {
143 [ -n "$distance" ] && {
144 # slottime = 9 + (distance / 150) + (distance % 150 ? 1 : 0)
145 slottime="$((9 + ($distance / 150) + 1 - (150 - ($distance % 150)) / 150 ))"
146 }
147 } || {
148 slottime="${slottime:--1}"
149 }
150
151 case "$macfilter" in
152 allow|2)
153 macfilter=2;
154 ;;
155 deny|1)
156 macfilter=1;
157 ;;
158 disable|none|0)
159 macfilter=0;
160 ;;
161 esac
162
163 case "$hwmode" in
164 *b) hwmode=0;;
165 *g) hwmode=2;;
166 *gst) hwmode=4;;
167 *) hwmode=1;;
168 esac
169
170 for vif in $vifs; do
171 config_get vif_txpower "$vif" txpower
172
173 config_get mode "$vif" mode
174 append vif_pre_up "vif $_c" "$N"
175 append vif_post_up "vif $_c" "$N"
176 append vif_do_up "vif $_c" "$N"
177
178 config_get_bool wmm "$vif" wmm "$wmm"
179 config_get_bool doth "$vif" doth "$doth"
180
181 [ "$mode" = "sta" ] || {
182 config_get_bool hidden "$vif" hidden 0
183 append vif_pre_up "closed $hidden" "$N"
184 config_get_bool isolate "$vif" isolate 0
185 append vif_pre_up "ap_isolate $isolate" "$N"
186 }
187
188 wsec_r=0
189 eap_r=0
190 wsec=0
191 auth=0
192 nasopts=
193 config_get enc "$vif" encryption
194 case "$enc" in
195 *WEP*|*wep*)
196 wsec_r=1
197 wsec=1
198 defkey=1
199 config_get key "$vif" key
200 case "$enc" in
201 *shared*) append vif_do_up "wepauth 1" "$N";;
202 *) append vif_do_up "wepauth 0" "$N";;
203 esac
204 case "$key" in
205 [1234])
206 defkey="$key"
207 for knr in 1 2 3 4; do
208 config_get k "$vif" key$knr
209 [ -n "$k" ] || continue
210 [ "$defkey" = "$knr" ] && def="=" || def=""
211 append vif_do_up "wepkey $def$knr,$k" "$N"
212 done
213 ;;
214 "");;
215 *) append vif_do_up "wepkey =1,$key" "$N";;
216 esac
217 ;;
218 *psk*|*PSK*)
219 wsec_r=1
220 config_get key "$vif" key
221 case "$enc" in
222 wpa*+wpa2*|WPA*+WPA2*|*psk+*psk2|*PSK+*PSK2) auth=132; wsec=6;;
223 wpa2*|WPA2*|*PSK2|*psk2) auth=128; wsec=4;;
224 *aes|*AES) auth=4; wsec=4;;
225 *) auth=4; wsec=2;;
226 esac
227 eval "${vif}_key=\"\$key\""
228 nasopts="-k \"\$${vif}_key\""
229 ;;
230 *wpa*|*WPA*)
231 wsec_r=1
232 eap_r=1
233 config_get key "$vif" key
234 config_get server "$vif" server
235 config_get port "$vif" port
236 case "$enc" in
237 wpa*+wpa2*|WPA*+WPA2*) auth=66; wsec=6;;
238 wpa2*|WPA2*) auth=64; wsec=4;;
239 *) auth=2; wsec=2;;
240 esac
241 eval "${vif}_key=\"\$key\""
242 nasopts="-r \"\$${vif}_key\" -h $server -p ${port:-1812}"
243 ;;
244 esac
245 append vif_do_up "wsec $wsec" "$N"
246 append vif_do_up "wpa_auth $auth" "$N"
247 append vif_do_up "wsec_restrict $wsec_r" "$N"
248 append vif_do_up "eap_restrict $eap_r" "$N"
249
250 config_get ssid "$vif" ssid
251 append vif_post_up "vlan_mode 0" "$N"
252 append vif_post_up "ssid $ssid" "$N"
253 append vif_do_up "ssid $ssid" "$N"
254
255 [ "$mode" = "monitor" ] && {
256 append vif_post_up "monitor $monitor" "$N"
257 append vif_post_up "passive $passive" "$N"
258 }
259
260 [ "$mode" = "adhoc" ] && {
261 config_get bssid "$vif" bssid
262 [ -n "$bssid" ] && {
263 append vif_pre_up "des_bssid $bssid" "$N"
264 append vif_pre_up "allow_mode 1" "$N"
265 }
266 } || append vif_pre_up "allow_mode 0" "$N"
267
268 append vif_post_up "enabled 1" "$N"
269
270 config_get ifname "$vif" ifname
271 #append if_up "ifconfig $ifname up" ";$N"
272
273 local net_cfg bridge
274 net_cfg="$(find_net_config "$vif")"
275 [ -z "$net_cfg" ] || {
276 bridge="$(bridge_interface "$net_cfg")"
277 append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
278 append if_up "start_net '$ifname' '$net_cfg' \$(wlc ifname '$ifname' bssid)" ";$N"
279 }
280 [ -z "$nasopts" ] || {
281 eval "${vif}_ssid=\"\$ssid\""
282 nas_mode="-A"
283 use_nas=1
284 [ "$mode" = "sta" ] && {
285 nas_mode="-S"
286 [ -z "$bridge" ] || {
287 append vif_post_up "supplicant 1" "$N"
288 append vif_post_up "passphrase $key" "$N"
289
290 use_nas=0
291 }
292 }
293 [ -z "$nas" -o "$use_nas" = "0" ] || nas_cmd="${nas_cmd:+$nas_cmd$N}$nas -P /var/run/nas.$ifname.pid -H 34954 ${bridge:+ -l $bridge} -i $ifname $nas_mode -m $auth -w $wsec -s \"\$${vif}_ssid\" -g 3600 $nasopts &"
294 }
295 _c=$(($_c + 1))
296 done
297 killall -KILL nas >&- 2>&-
298 wlc ifname "$device" stdin <<EOF
299 $ifdown
300
301 gmode ${hwmode:-1}
302 apsta $apsta
303 ap $ap
304 ${mssid:+mssid $mssid}
305 infra $infra
306 ${wet:+wet 1}
307 802.11d 0
308 802.11h ${doth:-0}
309 wme ${wmm:-0}
310 rxant ${rxantenna:-3}
311 txant ${txantenna:-3}
312 fragthresh ${frag:-2346}
313 rtsthresh ${rts:-2347}
314 monitor ${monitor:-0}
315 passive ${passive:-0}
316
317 radio ${radio:-1}
318 macfilter ${macfilter:-0}
319 maclist ${maclist:-none}
320 wds none
321 ${wds:+wds $wds}
322 country ${country:-IL0}
323 ${channel:+channel $channel}
324 maxassoc ${maxassoc:-128}
325 slottime ${slottime:--1}
326 ${frameburst:+frameburst $frameburst}
327
328 $vif_pre_up
329 up
330 $vif_post_up
331 EOF
332 eval "$if_up"
333 wlc ifname "$device" stdin <<EOF
334 $vif_do_up
335 EOF
336
337 # use vif_txpower (from last wifi-iface) instead of txpower (from
338 # wifi-device) if the latter does not exist
339 txpower=${txpower:-$vif_txpower}
340 [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
341
342 eval "$nas_cmd"
343 }
344
345
346 detect_broadcom() {
347 local i=-1
348
349 while [ -f /proc/net/wl$((++i)) ]; do
350 config_get type wl${i} type
351 [ "$type" = broadcom ] && continue
352 cat <<EOF
353 config wifi-device wl${i}
354 option type broadcom
355 option channel 5
356
357 # REMOVE THIS LINE TO ENABLE WIFI:
358 option disabled 1
359
360 config wifi-iface
361 option device wl${i}
362 option network lan
363 option mode ap
364 option ssid OpenWrt${i#0}
365 option encryption none
366
367 EOF
368 done
369 }