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