broadcom-wl: implement wepauth in wlc and support it in broadcom.sh, allows switching...
[openwrt/openwrt.git] / package / 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
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 mode "$vif" mode
13 _c=$(($_c + 1))
14 case "$mode" in
15 adhoc)
16 adhoc=1
17 adhoc_if="$vif"
18 ;;
19 sta)
20 sta=1
21 sta_if="$vif"
22 ;;
23 ap)
24 apmode=1
25 ap_if="${ap_if:+$ap_if }$vif"
26 ;;
27 wds)
28 config_get addr "$vif" bssid
29 [ -z "$addr" ] || {
30 addr=$(echo "$addr" | tr 'A-F' 'a-f')
31 append wds "$addr"
32 }
33 ;;
34 monitor)
35 mon=1
36 mon_if="$vif"
37 ;;
38 *) echo "$device($vif): Invalid mode";;
39 esac
40 done
41 config_set "$device" wds "$wds"
42
43 local _c=
44 for vif in ${adhoc_if:-$sta_if $ap_if $mon_if}; do
45 config_set "$vif" ifname "wl0${_c:+.$_c}"
46 _c=$((${_c:-0} + 1))
47 done
48 config_set "$device" vifs "${adhoc_if:-$sta_if $ap_if $mon_if}"
49
50 ifdown="down"
51 for vif in 0 1 2 3; do
52 append ifdown "vif $vif" "$N"
53 append ifdown "enabled 0" "$N"
54 done
55
56 ap=1
57 infra=1
58 if [ "$_c" -gt 1 ]; then
59 mssid=1
60 else
61 mssid=
62 fi
63 apsta=0
64 radio=1
65 monitor=0
66 passive=0
67 case "$adhoc:$sta:$apmode:$mon" in
68 1*)
69 ap=0
70 mssid=
71 infra=0
72 ;;
73 :1:1:)
74 apsta=1
75 wet=1
76 ;;
77 :1::)
78 wet=1
79 ap=0
80 mssid=
81 ;;
82 :::1)
83 wet=1
84 ap=0
85 mssid=
86 monitor=1
87 passive=1
88 ;;
89 ::)
90 radio=0
91 ;;
92 esac
93 }
94
95 disable_broadcom() {
96 set_wifi_down "$1"
97 wlc down
98 (
99 include /lib/network
100
101 # make sure the interfaces are down and removed from all bridges
102 for dev in wl0 wl0.1 wl0.2 wl0.3; do
103 ifconfig "$dev" down 2>/dev/null >/dev/null && {
104 unbridge "$dev"
105 }
106 done
107 )
108 true
109 }
110
111 enable_broadcom() {
112 local _c
113 config_get channel "$device" channel
114 config_get country "$device" country
115 config_get maxassoc "$device" maxassoc
116 config_get wds "$device" wds
117 config_get vifs "$device" vifs
118 config_get distance "$device" distance
119 config_get slottime "$device" slottime
120 config_get rxantenna "$device" rxantenna
121 config_get txantenna "$device" txantenna
122 config_get_bool frameburst "$device" frameburst
123 config_get macfilter "$device" macfilter
124 config_get maclist "$device" maclist
125 config_get macaddr "$device" macaddr
126 config_get txpower "$device" txpower
127 config_get frag "$device" frag
128 config_get rts "$device" rts
129 local vif_pre_up vif_post_up vif_do_up vif_txpower
130 local doth=0
131 local wmm=0
132
133 _c=0
134 nas="$(which nas)"
135 nas_cmd=
136 if_up=
137
138 [ -z "$slottime" ] && {
139 [ -n "$distance" ] && {
140 # slottime = 9 + (distance / 150) + (distance % 150 ? 1 : 0)
141 slottime="$((9 + ($distance / 150) + 1 - (150 - ($distance % 150)) / 150 ))"
142 }
143 } || {
144 slottime="${slottime:--1}"
145 }
146
147 case "$macfilter" in
148 allow|2)
149 macfilter=2;
150 ;;
151 deny|1)
152 macfilter=1;
153 ;;
154 disable|none|0)
155 macfilter=0;
156 ;;
157 esac
158
159 for vif in $vifs; do
160 config_get vif_txpower "$vif" txpower
161
162 config_get mode "$vif" mode
163 append vif_pre_up "vif $_c" "$N"
164 append vif_post_up "vif $_c" "$N"
165 append vif_do_up "vif $_c" "$N"
166
167 config_get_bool wmm "$vif" wmm "$wmm"
168 config_get_bool doth "$vif" doth "$doth"
169
170 [ "$mode" = "sta" ] || {
171 config_get_bool hidden "$vif" hidden 0
172 append vif_pre_up "closed $hidden" "$N"
173 config_get_bool isolate "$vif" isolate 0
174 append vif_pre_up "ap_isolate $isolate" "$N"
175 }
176
177 wsec_r=0
178 eap_r=0
179 wsec=0
180 auth=0
181 nasopts=
182 config_get enc "$vif" encryption
183 case "$enc" in
184 *WEP*|*wep*)
185 wsec_r=1
186 wsec=1
187 defkey=1
188 config_get key "$vif" key
189 case "$enc" in
190 *shared*) append vif_do_up "wepauth 1" "$N";;
191 *) append vif_do_up "wepauth 0" "$N";;
192 esac
193 case "$key" in
194 [1234])
195 defkey="$key"
196 for knr in 1 2 3 4; do
197 config_get k "$vif" key$knr
198 [ -n "$k" ] || continue
199 [ "$defkey" = "$knr" ] && def="=" || def=""
200 append vif_do_up "wepkey $def$knr,$k" "$N"
201 done
202 ;;
203 "");;
204 *) append vif_do_up "wepkey =1,$key" "$N";;
205 esac
206 ;;
207 *psk*|*PSK*)
208 wsec_r=1
209 config_get key "$vif" key
210 case "$enc" in
211 wpa*+wpa2*|WPA*+WPA2*|*psk+*psk2|*PSK+*PSK2) auth=132; wsec=6;;
212 wpa2*|WPA2*|*PSK2|*psk2) auth=128; wsec=4;;
213 *aes|*AES) auth=4; wsec=4;;
214 *) auth=4; wsec=2;;
215 esac
216 eval "${vif}_key=\"\$key\""
217 nasopts="-k \"\$${vif}_key\""
218 ;;
219 *wpa*|*WPA*)
220 wsec_r=1
221 eap_r=1
222 config_get key "$vif" key
223 config_get server "$vif" server
224 config_get port "$vif" port
225 case "$enc" in
226 wpa*+wpa2*|WPA*+WPA2*) auth=66; wsec=6;;
227 wpa2*|WPA2*) auth=64; wsec=4;;
228 *) auth=2; wsec=2;;
229 esac
230 eval "${vif}_key=\"\$key\""
231 nasopts="-r \"\$${vif}_key\" -h $server -p ${port:-1812}"
232 ;;
233 esac
234 append vif_do_up "wsec $wsec" "$N"
235 append vif_do_up "wpa_auth $auth" "$N"
236 append vif_do_up "wsec_restrict $wsec_r" "$N"
237 append vif_do_up "eap_restrict $eap_r" "$N"
238
239 config_get ssid "$vif" ssid
240 append vif_post_up "vlan_mode 0" "$N"
241 append vif_post_up "ssid $ssid" "$N"
242 append vif_do_up "ssid $ssid" "$N"
243
244 [ "$mode" = "monitor" ] && {
245 append vif_post_up "monitor $monitor" "$N"
246 append vif_post_up "passive $passive" "$N"
247 }
248
249 [ "$mode" = "adhoc" ] && {
250 config_get bssid "$vif" bssid
251 [ -n "$bssid" ] && {
252 append vif_pre_up "des_bssid $bssid" "$N"
253 append vif_pre_up "allow_mode 1" "$N"
254 }
255 } || append vif_pre_up "allow_mode 0" "$N"
256
257 append vif_post_up "enabled 1" "$N"
258
259 config_get ifname "$vif" ifname
260 #append if_up "ifconfig $ifname up" ";$N"
261
262 local net_cfg bridge
263 net_cfg="$(find_net_config "$vif")"
264 [ -z "$net_cfg" ] || {
265 bridge="$(bridge_interface "$net_cfg")"
266 append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
267 append if_up "start_net '$ifname' '$net_cfg' \$(wlc ifname '$ifname' bssid)" ";$N"
268 }
269 [ -z "$nasopts" ] || {
270 eval "${vif}_ssid=\"\$ssid\""
271 nas_mode="-A"
272 use_nas=1
273 [ "$mode" = "sta" ] && {
274 nas_mode="-S"
275 [ -z "$bridge" ] || {
276 append vif_post_up "supplicant 1" "$N"
277 append vif_post_up "passphrase $key" "$N"
278
279 use_nas=0
280 }
281 }
282 [ -z "$nas" -o "$use_nas" = "0" ] || nas_cmd="${nas_cmd:+$nas_cmd$N}$nas -P /var/run/nas.$ifname.pid -H 34954 ${bridge:+ -l $bridge} -i $ifname $nas_mode -m $auth -w $wsec -s \"\$${vif}_ssid\" -g 3600 $nasopts &"
283 }
284 _c=$(($_c + 1))
285 done
286 killall -KILL nas >&- 2>&-
287 wlc stdin <<EOF
288 $ifdown
289
290 apsta $apsta
291 ap $ap
292 ${mssid:+mssid $mssid}
293 infra $infra
294 ${wet:+wet 1}
295 802.11d 0
296 802.11h ${doth:-0}
297 wme ${wmm:-0}
298 rxant ${rxantenna:-3}
299 txant ${txantenna:-3}
300 fragthresh ${frag:-2346}
301 rtsthresh ${rts:-2347}
302 monitor ${monitor:-0}
303 passive ${passive:-0}
304
305 radio ${radio:-1}
306 macfilter ${macfilter:-0}
307 maclist ${maclist:-none}
308 wds none
309 ${wds:+wds $wds}
310 country ${country:-IL0}
311 ${channel:+channel $channel}
312 maxassoc ${maxassoc:-128}
313 slottime ${slottime:--1}
314 ${frameburst:+frameburst $frameburst}
315
316 $vif_pre_up
317 up
318 $vif_post_up
319 EOF
320 eval "$if_up"
321 wlc stdin <<EOF
322 $vif_do_up
323 EOF
324
325 # use vif_txpower (from last wifi-iface) instead of txpower (from
326 # wifi-device) if the latter does not exist
327 txpower=${txpower:-$vif_txpower}
328 [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
329
330 eval "$nas_cmd"
331 }
332
333
334 detect_broadcom() {
335 [ -f /proc/net/wl0 ] || return
336 config_get type wl0 type
337 [ "$type" = broadcom ] && return
338 cat <<EOF
339 config wifi-device wl0
340 option type broadcom
341 option channel 5
342
343 # REMOVE THIS LINE TO ENABLE WIFI:
344 option disabled 1
345
346 config wifi-iface
347 option device wl0
348 option network lan
349 option mode ap
350 option ssid OpenWrt
351 option encryption none
352 EOF
353 }