ba31518c1f97efb036a2bfa39f35d0f5fe5ced16
[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                 append if_up "ifconfig '$ifname' up" ";$N"
362
363                 local net_cfg="$(find_net_config "$vif")"
364                 [ -z "$net_cfg" ] || {
365                         append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
366                         append if_up "start_net '$ifname' '$net_cfg'" ";$N"
367                 }
368                 [ -z "$nas" -o -z "$nasopts" ] || {
369                         eval "${vif}_ssid=\"\$ssid\""
370                         local nas_mode="-A"
371                         [ "$mode" = "sta" ] && nas_mode="-S"
372                         [ -z "$nas_cmd" ] && {
373                                 local pid_file=/var/run/nas.$device.pid
374                                 nas_cmd="start-stop-daemon -S -b -p $pid_file -x $nas -- -P $pid_file -H 34954"
375                         }
376                         append nas_cmd "-i $ifname $nas_mode -m $auth -w $wsec -s \"\$${vif}_ssid\" -g 3600 -F $nasopts"
377                 }
378                 _c=$(($_c + 1))
379         done
380         wlc ifname "$device" stdin <<EOF
381 ${macaddr:+bssid $macaddr}
382 ${macaddr:+cur_etheraddr $macaddr}
383 band ${band:-0}
384 ${nmode:+nmode $nmode}
385 ${nmode:+${nreqd:+nreqd $nreqd}}
386 ${gmode:+gmode $gmode}
387 apsta $apsta
388 ap $ap
389 ${mssid:+mssid $mssid}
390 infra $infra
391 ${wet:+wet 1}
392 802.11d 0
393 802.11h ${doth:-0}
394 wme ${wmm:-1}
395 rxant ${rxantenna:-3}
396 txant ${txantenna:-3}
397 fragthresh ${frag:-2346}
398 rtsthresh ${rts:-2347}
399 monitor ${monitor:-0}
400
401 radio ${radio:-1}
402 macfilter ${macfilter:-0}
403 maclist ${maclist:-none}
404 ${wds:+wds $wds}
405 country ${country:-US}
406 ${channel:+channel $channel}
407 ${chanspec:+chanspec $chanspec}
408 maxassoc ${maxassoc:-128}
409 slottime ${slottime:--1}
410 ${frameburst:+frameburst $frameburst}
411
412 $vif_pre_up
413 EOF
414         eval "$if_pre_up"
415         wlc ifname "$device" stdin <<EOF
416 up
417 $vif_post_up
418 EOF
419         eval "$if_up"
420         wlc ifname "$device" stdin <<EOF
421 $vif_do_up
422 EOF
423
424         # use vif_txpower (from last wifi-iface) instead of txpower (from
425         # wifi-device) if the latter does not exist
426         txpower=${txpower:-$vif_txpower}
427         [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
428
429         eval "$nas_cmd"
430 }
431
432
433 detect_broadcom() {
434         local i=-1
435
436         while grep -qs "^ *wl$((++i)):" /proc/net/dev; do
437                 local channel type
438
439                 config_get type wl${i} type
440                 [ "$type" = broadcom ] && continue
441                 channel=`wlc ifname wl${i} channel`
442                 cat <<EOF
443 config wifi-device  wl${i}
444         option type     broadcom
445         option channel  ${channel:-11}
446
447         # REMOVE THIS LINE TO ENABLE WIFI:
448         option disabled 1
449
450 config wifi-iface
451         option device   wl${i}
452         option network  lan
453         option mode     ap
454         option ssid     OpenWrt${i#0}
455         option encryption none
456
457 EOF
458         done
459 }