0dd25a39b319a6f70eca56dd866ccd6448ada68b
[openwrt/staging/wigyori.git] / package / kernel / broadcom-wl / files / lib / wifi / broadcom.sh
1 append DRIVERS "broadcom"
2
3 scan_broadcom() {
4 local device="$1"
5 local vif vifs wds
6 local adhoc sta apmode mon disabled
7 local adhoc_if sta_if ap_if mon_if
8
9 config_get vifs "$device" vifs
10 for vif in $vifs; do
11 config_get_bool disabled "$vif" disabled 0
12 [ $disabled -eq 0 ] || continue
13
14 local mode
15 config_get mode "$vif" mode
16 case "$mode" in
17 adhoc)
18 adhoc=1
19 adhoc_if="$vif"
20 ;;
21 sta)
22 sta=1
23 sta_if="$vif"
24 ;;
25 ap)
26 apmode=1
27 ap_if="${ap_if:+$ap_if }$vif"
28 ;;
29 wds)
30 local addr
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 local pid_file=/var/run/nas.$device.pid
105 [ -e $pid_file ] && start-stop-daemon -K -q -s SIGKILL -p $pid_file && rm $pid_file
106
107 # make sure the interfaces are down and removed from all bridges
108 local dev
109 for dev in $device ${device}-1 ${device}-2 ${device}-3; do
110 ifconfig "$dev" down 2>/dev/null >/dev/null && {
111 unbridge "$dev"
112 }
113 done
114 )
115 true
116 }
117
118 enable_broadcom() {
119 local device="$1"
120 local channel country maxassoc wds vifs distance slottime rxantenna txantenna
121 local frameburst macfilter maclist macaddr txpower frag rts hwmode htmode
122 config_get channel "$device" channel
123 config_get country "$device" country
124 config_get maxassoc "$device" maxassoc
125 config_get wds "$device" wds
126 config_get vifs "$device" vifs
127 config_get distance "$device" distance
128 config_get slottime "$device" slottime
129 config_get rxantenna "$device" rxantenna
130 config_get txantenna "$device" txantenna
131 config_get_bool frameburst "$device" frameburst
132 config_get macfilter "$device" macfilter
133 config_get maclist "$device" maclist
134 config_get macaddr "$device" macaddr
135 config_get txpower "$device" txpower
136 config_get frag "$device" frag
137 config_get rts "$device" rts
138 config_get hwmode "$device" hwmode
139 config_get htmode "$device" htmode
140 local doth=0
141 local wmm=1
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 local gmode=2 nmode=0 nreqd=
165 case "$hwmode" in
166 *a) gmode=;;
167 *b) gmode=0;;
168 *bg) gmode=1;;
169 *g) gmode=2;;
170 *gst) gmode=4;;
171 *lrs) gmode=5;;
172 *) nmode=1; nreqd=0;;
173 esac
174
175 case "$hwmode" in
176 n|11n) nmode=1; nreqd=1;;
177 *n*) nmode=1; nreqd=0;;
178 esac
179
180 # Use 'nmode' for N-Phy only
181 [ "$(wlc ifname "$device" phytype)" = 4 ] || nmode=
182
183 local band chanspec
184 [ ${channel:-0} -ge 1 -a ${channel:-0} -le 14 ] && band=2
185 [ ${channel:-0} -ge 36 ] && {
186 band=1
187 gmode=
188 }
189
190 # Use 'chanspec' instead of 'channel' for 'N' modes (See bcmwifi.h)
191 [ ${nmode:-0} -ne 0 -a -n "$band" -a -n "$channel" ] && {
192 case "$htmode" in
193 HT40-) chanspec=$(printf 0x%x%x%02x $band 0xe $(($channel - 2))); channel=;;
194 HT40+) chanspec=$(printf 0x%x%x%02x $band 0xd $(($channel + 2))); channel=;;
195 HT20) chanspec=$(printf 0x%x%x%02x $band 0xb $channel); channel=;;
196 *) ;;
197 esac
198 }
199
200 local _c=0
201 local nas="$(which nas)"
202 local if_up nas_cmd
203 local vif vif_pre_up vif_post_up vif_do_up vif_txpower
204
205 for vif in $vifs; do
206 config_get vif_txpower "$vif" txpower
207
208 local mode
209 config_get mode "$vif" mode
210 append vif_pre_up "vif $_c" "$N"
211 append vif_post_up "vif $_c" "$N"
212 append vif_do_up "vif $_c" "$N"
213
214 config_get_bool wmm "$vif" wmm "$wmm"
215 config_get_bool doth "$vif" doth "$doth"
216
217 [ "$mode" = "sta" ] || {
218 local hidden isolate
219 config_get_bool hidden "$vif" hidden 0
220 append vif_pre_up "closed $hidden" "$N"
221 config_get_bool isolate "$vif" isolate 0
222 append vif_pre_up "ap_isolate $isolate" "$N"
223 }
224
225 local wsec_r=0
226 local eap_r=0
227 local wsec=0
228 local auth=0
229 local nasopts=
230 local enc key rekey
231
232 config_get enc "$vif" encryption
233 case "$enc" in
234 *wep*)
235 local def defkey k knr
236 wsec_r=1
237 wsec=1
238 defkey=1
239 config_get key "$vif" key
240 case "$enc" in
241 *shared*) append vif_do_up "wepauth 1" "$N";;
242 *) append vif_do_up "wepauth 0" "$N";;
243 esac
244 case "$key" in
245 [1234])
246 defkey="$key"
247 for knr in 1 2 3 4; do
248 config_get k "$vif" key$knr
249 [ -n "$k" ] || continue
250 [ "$defkey" = "$knr" ] && def="=" || def=""
251 append vif_do_up "wepkey $def$knr,$k" "$N"
252 done
253 ;;
254 "");;
255 *) append vif_do_up "wepkey =1,$key" "$N";;
256 esac
257 ;;
258 *psk*)
259 wsec_r=1
260 config_get key "$vif" key
261
262 # psk version + default cipher
263 case "$enc" in
264 *mixed*|*psk+psk2*) auth=132; wsec=6;;
265 *psk2*) auth=128; wsec=4;;
266 *) auth=4; wsec=2;;
267 esac
268
269 # cipher override
270 case "$enc" in
271 *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
272 *aes*|*ccmp*) wsec=4;;
273 *tkip*) wsec=2;;
274 esac
275
276 # group rekey interval
277 config_get rekey "$vif" wpa_group_rekey
278
279 eval "${vif}_key=\"\$key\""
280 nasopts="-k \"\$${vif}_key\"${rekey:+ -g $rekey}"
281 ;;
282 *wpa*)
283 local auth_port auth_secret auth_server
284 wsec_r=1
285 eap_r=1
286 config_get auth_server "$vif" auth_server
287 [ -z "$auth_server" ] && config_get auth_server "$vif" server
288 config_get auth_port "$vif" auth_port
289 [ -z "$auth_port" ] && config_get auth_port "$vif" port
290 config_get auth_secret "$vif" auth_secret
291 [ -z "$auth_secret" ] && config_get auth_secret "$vif" key
292
293 # wpa version + default cipher
294 case "$enc" in
295 *mixed*|*wpa+wpa2*) auth=66; wsec=6;;
296 *wpa2*) auth=64; wsec=4;;
297 *) auth=2; wsec=2;;
298 esac
299
300 # cipher override
301 case "$enc" in
302 *tkip+aes*|*tkip+ccmp*|*aes+tkip*|*ccmp+tkip*) wsec=6;;
303 *aes*|*ccmp*) wsec=4;;
304 *tkip*) wsec=2;;
305 esac
306
307 # group rekey interval
308 config_get rekey "$vif" wpa_group_rekey
309
310 eval "${vif}_key=\"\$auth_secret\""
311 nasopts="-r \"\$${vif}_key\" -h $auth_server -p ${auth_port:-1812}${rekey:+ -g $rekey}"
312 ;;
313 esac
314 append vif_do_up "wsec $wsec" "$N"
315 append vif_do_up "wpa_auth $auth" "$N"
316 append vif_do_up "wsec_restrict $wsec_r" "$N"
317 append vif_do_up "eap_restrict $eap_r" "$N"
318
319 local ssid
320 config_get ssid "$vif" ssid
321 append vif_post_up "vlan_mode 0" "$N"
322 append vif_pre_up "ssid $ssid" "$N"
323
324 [ "$mode" = "monitor" ] && {
325 append vif_post_up "monitor $monitor" "$N"
326 }
327
328 [ "$mode" = "adhoc" ] && {
329 local bssid
330 config_get bssid "$vif" bssid
331 [ -n "$bssid" ] && {
332 append vif_pre_up "bssid $bssid" "$N"
333 append vif_pre_up "ibss_merge 0" "$N"
334 } || {
335 append vif_pre_up "ibss_merge 1" "$N"
336 }
337 }
338
339 append vif_post_up "enabled 1" "$N"
340
341 local ifname
342 config_get ifname "$vif" ifname
343 #append if_up "ifconfig $ifname up" ";$N"
344
345 local net_cfg="$(find_net_config "$vif")"
346 [ -z "$net_cfg" ] || {
347 append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
348 append if_up "start_net '$ifname' '$net_cfg'" ";$N"
349 }
350 [ -z "$nas" -o -z "$nasopts" ] || {
351 eval "${vif}_ssid=\"\$ssid\""
352 local nas_mode="-A"
353 [ "$mode" = "sta" ] && nas_mode="-S"
354 [ -z "$nas_cmd" ] && {
355 local pid_file=/var/run/nas.$device.pid
356 nas_cmd="start-stop-daemon -S -b -p $pid_file -x $nas -- -P $pid_file -H 34954"
357 }
358 append nas_cmd "-i $ifname $nas_mode -m $auth -w $wsec -s \"\$${vif}_ssid\" -g 3600 -F $nasopts"
359 }
360 _c=$(($_c + 1))
361 done
362 wlc ifname "$device" stdin <<EOF
363 $ifdown
364
365 band ${band:-0}
366 ${nmode:+nmode $nmode}
367 ${nmode:+${nreqd:+nreqd $nreqd}}
368 ${gmode:+gmode $gmode}
369 apsta $apsta
370 ap $ap
371 ${mssid:+mssid $mssid}
372 infra $infra
373 ${wet:+wet 1}
374 802.11d 0
375 802.11h ${doth:-0}
376 wme ${wmm:-1}
377 rxant ${rxantenna:-3}
378 txant ${txantenna:-3}
379 fragthresh ${frag:-2346}
380 rtsthresh ${rts:-2347}
381 monitor ${monitor:-0}
382
383 radio ${radio:-1}
384 macfilter ${macfilter:-0}
385 maclist ${maclist:-none}
386 wds none
387 ${wds:+wds $wds}
388 country ${country:-US}
389 ${channel:+channel $channel}
390 ${chanspec:+chanspec $chanspec}
391 maxassoc ${maxassoc:-128}
392 slottime ${slottime:--1}
393 ${frameburst:+frameburst $frameburst}
394
395 $vif_pre_up
396 up
397 $vif_post_up
398 EOF
399 eval "$if_up"
400 wlc ifname "$device" stdin <<EOF
401 $vif_do_up
402 EOF
403
404 # use vif_txpower (from last wifi-iface) instead of txpower (from
405 # wifi-device) if the latter does not exist
406 txpower=${txpower:-$vif_txpower}
407 [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
408
409 eval "$nas_cmd"
410 }
411
412
413 detect_broadcom() {
414 local i=-1
415
416 while grep -qs "^ *wl$((++i)):" /proc/net/dev; do
417 local channel type
418
419 config_get type wl${i} type
420 [ "$type" = broadcom ] && continue
421 channel=`wlc ifname wl${i} channel`
422 cat <<EOF
423 config wifi-device wl${i}
424 option type broadcom
425 option channel ${channel:-11}
426
427 # REMOVE THIS LINE TO ENABLE WIFI:
428 option disabled 1
429
430 config wifi-iface
431 option device wl${i}
432 option network lan
433 option mode ap
434 option ssid OpenWrt${i#0}
435 option encryption none
436
437 EOF
438 done
439 }