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