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