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