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