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