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