[package] sync macs with vif bssids in broadcom.sh, fixes multi-ssid with wpa
[openwrt/svn-archive/archive.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 local vif_pre_up vif_post_up vif_do_up vif_txpower
128
129 _c=0
130 nas="$(which nas)"
131 nas_cmd=
132 if_up=
133
134 [ -z "$slottime" ] && {
135 [ -n "$distance" ] && {
136 # slottime = 9 + (distance / 150) + (distance % 150 ? 1 : 0)
137 slottime="$((9 + ($distance / 150) + 1 - (150 - ($distance % 150)) / 150 ))"
138 }
139 } || {
140 slottime="${slottime:--1}"
141 }
142
143 case "$macfilter" in
144 allow|2)
145 macfilter=2;
146 ;;
147 deny|1)
148 macfilter=1;
149 ;;
150 disable|none|0)
151 macfilter=0;
152 ;;
153 esac
154
155 for vif in $vifs; do
156 config_get vif_txpower "$vif" txpower
157
158 config_get mode "$vif" mode
159 append vif_pre_up "vif $_c" "$N"
160 append vif_post_up "vif $_c" "$N"
161 append vif_do_up "vif $_c" "$N"
162
163 [ "$mode" = "sta" ] || {
164 config_get_bool hidden "$vif" hidden 0
165 append vif_pre_up "closed $hidden" "$N"
166 config_get_bool isolate "$vif" isolate 0
167 append vif_pre_up "ap_isolate $isolate" "$N"
168 }
169
170 wsec_r=0
171 eap_r=0
172 wsec=0
173 auth=0
174 nasopts=
175 config_get enc "$vif" encryption
176 case "$enc" in
177 WEP|wep)
178 wsec_r=1
179 wsec=1
180 defkey=1
181 config_get key "$vif" key
182 case "$key" in
183 [1234])
184 defkey="$key"
185 for knr in 1 2 3 4; do
186 config_get k "$vif" key$knr
187 [ -n "$k" ] || continue
188 [ "$defkey" = "$knr" ] && def="=" || def=""
189 append vif_do_up "wepkey $def$knr,$k" "$N"
190 done
191 ;;
192 "");;
193 *) append vif_do_up "wepkey =1,$key" "$N";;
194 esac
195 ;;
196 *psk*|*PSK*)
197 wsec_r=1
198 config_get key "$vif" key
199 case "$enc" in
200 wpa*+wpa2*|WPA*+WPA2*|*psk+*psk2|*PSK+*PSK2) auth=132; wsec=6;;
201 wpa2*|WPA2*|*PSK2|*psk2) auth=128; wsec=4;;
202 *) auth=4; wsec=2;;
203 esac
204 eval "${vif}_key=\"\$key\""
205 nasopts="-k \"\$${vif}_key\""
206 ;;
207 *wpa*|*WPA*)
208 wsec_r=1
209 eap_r=1
210 config_get key "$vif" key
211 config_get server "$vif" server
212 config_get port "$vif" port
213 case "$enc" in
214 wpa*+wpa2*|WPA*+WPA2*) auth=66; wsec=6;;
215 wpa2*|WPA2*) auth=64; wsec=4;;
216 *) auth=2; wsec=2;;
217 esac
218 eval "${vif}_key=\"\$key\""
219 nasopts="-r \"\$${vif}_key\" -h $server -p ${port:-1812}"
220 ;;
221 esac
222 append vif_do_up "wsec $wsec" "$N"
223 append vif_do_up "wpa_auth $auth" "$N"
224 append vif_do_up "wsec_restrict $wsec_r" "$N"
225 append vif_do_up "eap_restrict $eap_r" "$N"
226
227 config_get ssid "$vif" ssid
228 append vif_post_up "vlan_mode 0" "$N"
229 append vif_post_up "ssid $ssid" "$N"
230 append vif_do_up "ssid $ssid" "$N"
231
232 [ "$mode" = "monitor" ] && {
233 append vif_post_up "monitor $monitor" "$N"
234 append vif_post_up "passive $passive" "$N"
235 }
236
237 [ "$mode" = "adhoc" ] && {
238 config_get bssid "$vif" bssid
239 [ -n "$bssid" ] && {
240 append vif_pre_up "des_bssid $bssid" "$N"
241 append vif_pre_up "allow_mode 1" "$N"
242 }
243 } || append vif_pre_up "allow_mode 0" "$N"
244
245 append vif_post_up "enabled 1" "$N"
246
247 config_get ifname "$vif" ifname
248 #append if_up "ifconfig $ifname up" ";$N"
249
250 local net_cfg bridge
251 net_cfg="$(find_net_config "$vif")"
252 [ -z "$net_cfg" ] || {
253 bridge="$(bridge_interface "$net_cfg")"
254 append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
255 append if_up "start_net '$ifname' '$net_cfg' \$(wlc ifname '$ifname' bssid)" ";$N"
256 }
257 [ -z "$nasopts" ] || {
258 eval "${vif}_ssid=\"\$ssid\""
259 nas_mode="-A"
260 use_nas=1
261 [ "$mode" = "sta" ] && {
262 nas_mode="-S"
263 [ -z "$bridge" ] || {
264 append vif_post_up "supplicant 1" "$N"
265 append vif_post_up "passphrase $key" "$N"
266
267 use_nas=0
268 }
269 }
270 [ -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 &"
271 }
272 _c=$(($_c + 1))
273 done
274 killall -KILL nas >&- 2>&-
275 wlc stdin <<EOF
276 $ifdown
277
278 ap $ap
279 ${mssid:+mssid $mssid}
280 apsta $apsta
281 infra $infra
282 ${wet:+wet 1}
283 802.11d 0
284 802.11h 0
285 rxant ${rxantenna:-3}
286 txant ${txantenna:-3}
287 monitor ${monitor:-0}
288 passive ${passive:-0}
289
290 radio ${radio:-1}
291 macfilter ${macfilter:-0}
292 maclist ${maclist:-none}
293 wds none
294 ${wds:+wds $wds}
295 country ${country:-IL0}
296 ${channel:+channel $channel}
297 maxassoc ${maxassoc:-128}
298 slottime ${slottime:--1}
299 ${frameburst:+frameburst $frameburst}
300
301 $vif_pre_up
302 up
303 $vif_post_up
304 EOF
305 eval "$if_up"
306 wlc stdin <<EOF
307 $vif_do_up
308 EOF
309
310 # use vif_txpower (from last wifi-iface) instead of txpower (from
311 # wifi-device) if the latter does not exist
312 txpower=${txpower:-$vif_txpower}
313 [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
314
315 eval "$nas_cmd"
316 }
317
318
319 detect_broadcom() {
320 [ -f /proc/net/wl0 ] || return
321 config_get type wl0 type
322 [ "$type" = broadcom ] && return
323 cat <<EOF
324 config wifi-device wl0
325 option type broadcom
326 option channel 5
327
328 # REMOVE THIS LINE TO ENABLE WIFI:
329 option disabled 1
330
331 config wifi-iface
332 option device wl0
333 option network lan
334 option mode ap
335 option ssid OpenWrt
336 option encryption none
337 EOF
338 }