c444ce8fd58fa4276ff6dfadae6936857b49905a
[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 "${device}${_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         local device="$1"
97         set_wifi_down "$device"
98         wlc ifname "$device" down
99         (
100                 include /lib/network
101
102                 # make sure the interfaces are down and removed from all bridges
103                 for dev in $device ${device}.1 ${device}.2 ${device}.3; do
104                         ifconfig "$dev" down 2>/dev/null >/dev/null && {
105                                 unbridge "$dev"
106                         }
107                 done
108         )
109         true
110 }
111
112 enable_broadcom() {
113         local device="$1"
114         local _c
115         config_get channel "$device" channel
116         config_get country "$device" country
117         config_get maxassoc "$device" maxassoc
118         config_get wds "$device" wds
119         config_get vifs "$device" vifs
120         config_get distance "$device" distance
121         config_get slottime "$device" slottime
122         config_get rxantenna "$device" rxantenna
123         config_get txantenna "$device" txantenna
124         config_get_bool frameburst "$device" frameburst
125         config_get macfilter "$device" macfilter
126         config_get maclist "$device" maclist
127         config_get macaddr "$device" macaddr
128         config_get txpower "$device" txpower
129         config_get frag "$device" frag
130         config_get rts "$device" rts
131         local vif_pre_up vif_post_up vif_do_up vif_txpower
132         local doth=0
133         local wmm=0
134
135         _c=0
136         nas="$(which nas)"
137         [ -n "$nas" ] && nas="start-stop-daemon -S -b -x $nas -- "
138         nas_cmd=
139         if_up=
140
141         [ -z "$slottime" ] && {
142                 [ -n "$distance" ] && {
143                         # slottime = 9 + (distance / 150) + (distance % 150 ? 1 : 0)
144                         slottime="$((9 + ($distance / 150) + 1 - (150 - ($distance % 150)) / 150 ))"
145                 }
146         } || {
147                 slottime="${slottime:--1}"
148         }
149
150         case "$macfilter" in
151                 allow|2)
152                         macfilter=2;
153                 ;;
154                 deny|1)
155                         macfilter=1;
156                 ;;
157                 disable|none|0)
158                         macfilter=0;
159                 ;;
160         esac
161
162         for vif in $vifs; do
163                 config_get vif_txpower "$vif" txpower
164
165                 config_get mode "$vif" mode
166                 append vif_pre_up "vif $_c" "$N"
167                 append vif_post_up "vif $_c" "$N"
168                 append vif_do_up "vif $_c" "$N"
169
170                 config_get_bool wmm "$vif" wmm "$wmm"
171                 config_get_bool doth "$vif" doth "$doth"
172
173                 [ "$mode" = "sta" ] || {
174                         config_get_bool hidden "$vif" hidden 0
175                         append vif_pre_up "closed $hidden" "$N"
176                         config_get_bool isolate "$vif" isolate 0
177                         append vif_pre_up "ap_isolate $isolate" "$N"
178                 }
179
180                 wsec_r=0
181                 eap_r=0
182                 wsec=0
183                 auth=0
184                 nasopts=
185                 config_get enc "$vif" encryption
186                 case "$enc" in
187                         *WEP*|*wep*)
188                                 wsec_r=1
189                                 wsec=1
190                                 defkey=1
191                                 config_get key "$vif" key
192                                 case "$enc" in
193                                         *shared*) append vif_do_up "wepauth 1" "$N";;
194                                         *) append vif_do_up "wepauth 0" "$N";;
195                                 esac
196                                 case "$key" in
197                                         [1234])
198                                                 defkey="$key"
199                                                 for knr in 1 2 3 4; do
200                                                         config_get k "$vif" key$knr
201                                                         [ -n "$k" ] || continue
202                                                         [ "$defkey" = "$knr" ] && def="=" || def=""
203                                                         append vif_do_up "wepkey $def$knr,$k" "$N"
204                                                 done
205                                         ;;
206                                         "");;
207                                         *) append vif_do_up "wepkey =1,$key" "$N";;
208                                 esac
209                         ;;
210                         *psk*|*PSK*)
211                                 wsec_r=1
212                                 config_get key "$vif" key
213                                 case "$enc" in
214                                         wpa*+wpa2*|WPA*+WPA2*|*psk+*psk2|*PSK+*PSK2) auth=132; wsec=6;;
215                                         wpa2*|WPA2*|*PSK2|*psk2) auth=128; wsec=4;;
216                                         *aes|*AES) auth=4; wsec=4;;
217                                         *) auth=4; wsec=2;;
218                                 esac
219                                 eval "${vif}_key=\"\$key\""
220                                 nasopts="-k \"\$${vif}_key\""
221                         ;;
222                         *wpa*|*WPA*)
223                                 wsec_r=1
224                                 eap_r=1
225                                 config_get key "$vif" key
226                                 config_get server "$vif" server
227                                 config_get port "$vif" port
228                                 case "$enc" in
229                                         wpa*+wpa2*|WPA*+WPA2*) auth=66; wsec=6;;
230                                         wpa2*|WPA2*) auth=64; wsec=4;;
231                                         *) auth=2; wsec=2;;
232                                 esac
233                                 eval "${vif}_key=\"\$key\""
234                                 nasopts="-r \"\$${vif}_key\" -h $server -p ${port:-1812}"
235                         ;;
236                 esac
237                 append vif_do_up "wsec $wsec" "$N"
238                 append vif_do_up "wpa_auth $auth" "$N"
239                 append vif_do_up "wsec_restrict $wsec_r" "$N"
240                 append vif_do_up "eap_restrict $eap_r" "$N"
241                 
242                 config_get ssid "$vif" ssid
243                 append vif_post_up "vlan_mode 0" "$N"
244                 append vif_post_up "ssid $ssid" "$N"
245                 append vif_do_up "ssid $ssid" "$N"
246
247                 [ "$mode" = "monitor" ] && {
248                         append vif_post_up "monitor $monitor" "$N"
249                         append vif_post_up "passive $passive" "$N"
250                 }
251
252                 [ "$mode" = "adhoc" ] && {
253                         config_get bssid "$vif" bssid
254                         [ -n "$bssid" ] && {
255                                 append vif_pre_up "des_bssid $bssid" "$N"
256                                 append vif_pre_up "allow_mode 1" "$N"
257                         }
258                 } || append vif_pre_up "allow_mode 0" "$N"
259                 
260                 append vif_post_up "enabled 1" "$N"
261                 
262                 config_get ifname "$vif" ifname
263                 #append if_up "ifconfig $ifname up" ";$N"
264
265                 local net_cfg bridge
266                 net_cfg="$(find_net_config "$vif")"
267                 [ -z "$net_cfg" ] || {
268                         bridge="$(bridge_interface "$net_cfg")"
269                         append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
270                         append if_up "start_net '$ifname' '$net_cfg' \$(wlc ifname '$ifname' bssid)" ";$N"
271                 }
272                 [ -z "$nasopts" ] || {
273                         eval "${vif}_ssid=\"\$ssid\""
274                         nas_mode="-A"
275                         use_nas=1
276                         [ "$mode" = "sta" ] && {
277                                 nas_mode="-S"
278                                 [ -z "$bridge" ] || {
279                                         append vif_post_up "supplicant 1" "$N"
280                                         append vif_post_up "passphrase $key" "$N"
281                                         
282                                         use_nas=0
283                                 }
284                         }
285                         [ -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 &"
286                 }
287                 _c=$(($_c + 1))
288         done
289         killall -KILL nas >&- 2>&-
290         wlc ifname "$device" stdin <<EOF
291 $ifdown
292
293 apsta $apsta
294 ap $ap
295 ${mssid:+mssid $mssid}
296 infra $infra
297 ${wet:+wet 1}
298 802.11d 0
299 802.11h ${doth:-0}
300 wme ${wmm:-0}
301 rxant ${rxantenna:-3}
302 txant ${txantenna:-3}
303 fragthresh ${frag:-2346}
304 rtsthresh ${rts:-2347}
305 monitor ${monitor:-0}
306 passive ${passive:-0}
307
308 radio ${radio:-1}
309 macfilter ${macfilter:-0}
310 maclist ${maclist:-none}
311 wds none
312 ${wds:+wds $wds}
313 country ${country:-IL0}
314 ${channel:+channel $channel}
315 maxassoc ${maxassoc:-128}
316 slottime ${slottime:--1}
317 ${frameburst:+frameburst $frameburst}
318
319 $vif_pre_up
320 up
321 $vif_post_up
322 EOF
323         eval "$if_up"
324         wlc ifname "$device" stdin <<EOF
325 $vif_do_up
326 EOF
327
328         # use vif_txpower (from last wifi-iface) instead of txpower (from
329         # wifi-device) if the latter does not exist
330         txpower=${txpower:-$vif_txpower}
331         [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
332
333         eval "$nas_cmd"
334 }
335
336
337 detect_broadcom() {
338         local i=-1
339
340         while [ -f /proc/net/wl$((++i)) ]; do
341                 config_get type wl${i} type
342                 [ "$type" = broadcom ] && continue
343                 cat <<EOF
344 config wifi-device  wl${i}
345         option type     broadcom
346         option channel  5
347
348         # REMOVE THIS LINE TO ENABLE WIFI:
349         option disabled 1
350
351 config wifi-iface
352         option device   wl${i}
353         option network  lan
354         option mode     ap
355         option ssid     OpenWrt${i#0}
356         option encryption none
357
358 EOF
359         done
360 }