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