fc26d076eaa9c191be76dc3f215cf1286399fba6
[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
7 local adhoc_if sta_if ap_if
8
9 config_get vifs "$device" vifs
10 for vif in $vifs; do
11 config_get mode "$vif" mode
12 case "$mode" in
13 adhoc)
14 adhoc=1
15 adhoc_if="$vif"
16 ;;
17 sta)
18 sta=1
19 sta_if="$vif"
20 ;;
21 ap)
22 apmode=1
23 ap_if="${ap_if:+$ap_if }$vif"
24 ;;
25 wds)
26 config_get addr "$vif" bssid
27 [ -z "$addr" ] || append wds "$addr"
28 ;;
29 *) echo "$device($vif): Invalid mode";;
30 esac
31 done
32 config_set "$device" wds "$wds"
33
34 local _c=
35 for vif in ${adhoc_if:-$sta_if $ap_if}; do
36 config_set "$vif" ifname "wl0${_c:+.$_c}"
37 _c=$((${_c:-0} + 1))
38 done
39 config_set "$device" vifs "${adhoc_if:-$sta_if $ap_if}"
40
41 ifdown="down"
42 for vif in 0 1 2 3; do
43 append ifdown "vif $vif" "$N"
44 append ifdown "enabled 0" "$N"
45 done
46
47 ap=1
48 infra=1
49 mssid=1
50 apsta=0
51 radio=1
52 case "$adhoc:$sta:$apmode" in
53 1*)
54 ap=0
55 mssid=0
56 infra=0
57 ;;
58 :1:1)
59 apsta=1
60 wet=1
61 ;;
62 :1:)
63 wet=1
64 ap=0
65 mssid=0
66 ;;
67 ::)
68 radio=0
69 ;;
70 esac
71 }
72
73 disable_broadcom() {
74 wlc down
75 (
76 include /lib/network
77
78 # make sure the interfaces are down and removed from all bridges
79 for dev in wl0 wl0.1 wl0.2 wl0.3; do
80 ifconfig "$dev" down 2>/dev/null >/dev/null && {
81 unbridge "$dev"
82 }
83 done
84 )
85 true
86 }
87
88 enable_broadcom() {
89 local _c
90 config_get channel "$device" channel
91 config_get country "$device" country
92 config_get maxassoc "$device" maxassoc
93 config_get wds "$device" wds
94 config_get vifs "$device" vifs
95 config_get distance "$device" distance
96 config_get slottime "$device" slottime
97 config_get rxant "$device" rxant
98 config_get txant "$device" txant
99 local vif_pre_up vif_post_up vif_do_up
100
101 _c=0
102 nas="$(which nas)"
103 nas_cmd=
104 if_up=
105
106 [ -z "$slottime" ] && {
107 [ -n "$distance" ] && {
108 # slottime = 9 + (distance / 150) + (distance % 150 ? 1 : 0)
109 slottime="$((9 + ($distance / 150) + 1 - (150 - ($distance % 150)) / 150 ))"
110 }
111 } || {
112 slottime="${slottime:--1}"
113 }
114
115 for vif in $vifs; do
116 config_get mode "$vif" mode
117 append vif_pre_up "vif $_c" "$N"
118 append vif_post_up "vif $_c" "$N"
119
120 [ "$mode" = "sta" ] || {
121 config_get_bool hidden "$vif" hidden 0
122 append vif_pre_up "closed $hidden" "$N"
123 config_get_bool isolate "$vif" isolate 0
124 append vif_pre_up "ap_isolate $isolate" "$N"
125 }
126
127 wsec_r=0
128 eap_r=0
129 wsec=0
130 auth=0
131 nasopts=
132 config_get enc "$vif" encryption
133 case "$enc" in
134 WEP|wep)
135 wsec_r=1
136 wsec=1
137 defkey=1
138 config_get key "$vif" key
139 case "$key" in
140 [1234])
141 defkey="$key"
142 for knr in 1 2 3 4; do
143 config_get k "$vif" key$knr
144 [ -n "$k" ] || continue
145 [ "$defkey" = "$knr" ] && def="=" || def=""
146 append vif_pre_up "wepkey $def$knr,$k" "$N"
147 done
148 ;;
149 "");;
150 *) append vif_pre_up "wepkey =1,$key" "$N";;
151 esac
152 ;;
153 *psk*|*PSK*)
154 wsec_r=1
155 config_get key "$vif" key
156 case "$enc" in
157 wpa*+wpa2*|WPA*+WPA2*|*psk+*psk2|*PSK+*PSK2) auth=132; wsec=6;;
158 wpa2*|WPA2*|*PSK2|*psk2) auth=128; wsec=4;;
159 *) auth=4; wsec=2;;
160 esac
161 eval "${vif}_key=\"\$key\""
162 nasopts="-k \"\$${vif}_key\""
163 ;;
164 *wpa*|*WPA*)
165 wsec_r=1
166 eap_r=1
167 config_get key "$vif" key
168 config_get server "$vif" server
169 config_get port "$vif" port
170 case "$enc" in
171 wpa*+wpa2*|WPA*+WPA2*) auth=66; wsec=6;;
172 wpa2*|WPA2*) auth=64; wsec=4;;
173 *) auth=2; wsec=2;;
174 esac
175 eval "${vif}_key=\"\$key\""
176 nasopts="-r \"\$${vif}_key\" -h $server -p $port"
177 ;;
178 esac
179 append vif_post_up "wsec $wsec" "$N"
180 append vif_post_up "wpa_auth $auth" "$N"
181 append vif_post_up "wsec_restrict $wsec_r" "$N"
182 append vif_post_up "eap_restrict $eap_r" "$N"
183
184 config_get ssid "$vif" ssid
185 append vif_post_up "vlan_mode 0" "$N"
186 append vif_post_up "ssid $ssid" "$N"
187 case "$mode" in
188 sta|adhoc) append vif_do_up "ssid $ssid" "$N";;
189 esac
190
191 append vif_post_up "enabled 1" "$N"
192
193 config_get ifname "$vif" ifname
194 append if_up "ifconfig $ifname up" ";$N"
195 local net_cfg bridge
196 net_cfg="$(find_net_config "$vif")"
197 [ -z "$net_cfg" ] || {
198 bridge="$(bridge_interface "$net_cfg")"
199 append if_up "start_net '$ifname' '$net_cfg'" ";$N"
200 }
201 [ -z "$nasopts" ] || {
202 eval "${vif}_ssid=\"\$ssid\""
203 mode="-A"
204 [ "$vif" = "$sta_if" ] && mode="-S"
205 [ -z "$nas" ] || nas_cmd="${nas_cmd:+$nas_cmd$N}$nas -P /var/run/nas.$ifname.pid -H 34954 ${bridge:+ -l $bridge} -i $ifname $mode -m $auth -w $wsec -s \"\$${vif}_ssid\" -g 3600 $nasopts &"
206 }
207 _c=$(($_c + 1))
208 done
209 killall -KILL nas >&- 2>&-
210 wlc stdin <<EOF
211 $ifdown
212
213 ap $ap
214 mssid $mssid
215 apsta $apsta
216 infra $infra
217 ${wet:+wet 1}
218 802.11d 0
219 802.11h 0
220 rxant ${rxant:-3}
221 txant ${txant:-3}
222
223 radio ${radio:-1}
224 macfilter 0
225 maclist none
226 wds ${wds:-none}
227 ${channel:+channel $channel}
228 country ${country:-IL0}
229 maxassoc ${maxassoc:-128}
230 slottime ${slottime:--1}
231
232 $vif_pre_up
233 up
234 $vif_post_up
235 EOF
236 eval "$if_up"
237 wlc stdin <<EOF
238 $vif_do_up
239 EOF
240 eval "$nas_cmd"
241 }
242
243
244 detect_broadcom() {
245 [ -f /proc/net/wl0 ] || return
246 config_get type wl0 type
247 [ "$type" = broadcom ] && return
248 cat <<EOF
249 config wifi-device wl0
250 option type broadcom
251 option channel 5
252 # disable radio to prevent an open ap after reflashing:
253 option disabled 1
254
255 config wifi-iface
256 option device wl0
257 option network lan
258 option mode ap
259 option ssid OpenWrt
260 option hidden 0
261 option encryption none
262
263 EOF
264 }