broadcom-wl: check iface pid file when invoking nas, fixes starting nas for additiona...
[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 "${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 config_get hwmode "$device" hwmode
132 local vif_pre_up vif_post_up vif_do_up vif_txpower
133 local doth=0
134 local wmm=0
135
136 _c=0
137 nas="$(which 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 case "$hwmode" in
163 *b) hwmode=0;;
164 *bg) hwmode=1;;
165 *g) hwmode=2;;
166 *gst) hwmode=4;;
167 *lrs) hwmode=5;;
168 *) hwmode=1;;
169 esac
170
171 for vif in $vifs; do
172 config_get vif_txpower "$vif" txpower
173
174 config_get mode "$vif" mode
175 append vif_pre_up "vif $_c" "$N"
176 append vif_post_up "vif $_c" "$N"
177 append vif_do_up "vif $_c" "$N"
178
179 config_get_bool wmm "$vif" wmm "$wmm"
180 config_get_bool doth "$vif" doth "$doth"
181
182 [ "$mode" = "sta" ] || {
183 config_get_bool hidden "$vif" hidden 0
184 append vif_pre_up "closed $hidden" "$N"
185 config_get_bool isolate "$vif" isolate 0
186 append vif_pre_up "ap_isolate $isolate" "$N"
187 }
188
189 wsec_r=0
190 eap_r=0
191 wsec=0
192 auth=0
193 nasopts=
194 config_get enc "$vif" encryption
195 case "$enc" in
196 *WEP*|*wep*)
197 wsec_r=1
198 wsec=1
199 defkey=1
200 config_get key "$vif" key
201 case "$enc" in
202 *shared*) append vif_do_up "wepauth 1" "$N";;
203 *) append vif_do_up "wepauth 0" "$N";;
204 esac
205 case "$key" in
206 [1234])
207 defkey="$key"
208 for knr in 1 2 3 4; do
209 config_get k "$vif" key$knr
210 [ -n "$k" ] || continue
211 [ "$defkey" = "$knr" ] && def="=" || def=""
212 append vif_do_up "wepkey $def$knr,$k" "$N"
213 done
214 ;;
215 "");;
216 *) append vif_do_up "wepkey =1,$key" "$N";;
217 esac
218 ;;
219 *psk*|*PSK*)
220 wsec_r=1
221 config_get key "$vif" key
222 case "$enc" in
223 wpa*+wpa2*|WPA*+WPA2*|*psk+*psk2|*PSK+*PSK2) auth=132; wsec=6;;
224 wpa2*|WPA2*|*PSK2|*psk2) auth=128; wsec=4;;
225 *aes|*AES) auth=4; wsec=4;;
226 *) auth=4; wsec=2;;
227 esac
228 eval "${vif}_key=\"\$key\""
229 nasopts="-k \"\$${vif}_key\""
230 ;;
231 *wpa*|*WPA*)
232 wsec_r=1
233 eap_r=1
234 config_get key "$vif" key
235 config_get server "$vif" server
236 config_get port "$vif" port
237 case "$enc" in
238 wpa*+wpa2*|WPA*+WPA2*) auth=66; wsec=6;;
239 wpa2*|WPA2*) auth=64; wsec=4;;
240 *) auth=2; wsec=2;;
241 esac
242 eval "${vif}_key=\"\$key\""
243 nasopts="-r \"\$${vif}_key\" -h $server -p ${port:-1812}"
244 ;;
245 esac
246 append vif_do_up "wsec $wsec" "$N"
247 append vif_do_up "wpa_auth $auth" "$N"
248 append vif_do_up "wsec_restrict $wsec_r" "$N"
249 append vif_do_up "eap_restrict $eap_r" "$N"
250
251 config_get ssid "$vif" ssid
252 append vif_post_up "vlan_mode 0" "$N"
253 append vif_post_up "ssid $ssid" "$N"
254 append vif_do_up "ssid $ssid" "$N"
255
256 [ "$mode" = "monitor" ] && {
257 append vif_post_up "monitor $monitor" "$N"
258 append vif_post_up "passive $passive" "$N"
259 }
260
261 [ "$mode" = "adhoc" ] && {
262 config_get bssid "$vif" bssid
263 [ -n "$bssid" ] && {
264 append vif_pre_up "des_bssid $bssid" "$N"
265 append vif_pre_up "allow_mode 1" "$N"
266 }
267 } || append vif_pre_up "allow_mode 0" "$N"
268
269 append vif_post_up "enabled 1" "$N"
270
271 config_get ifname "$vif" ifname
272 #append if_up "ifconfig $ifname up" ";$N"
273
274 local net_cfg bridge
275 net_cfg="$(find_net_config "$vif")"
276 [ -z "$net_cfg" ] || {
277 bridge="$(bridge_interface "$net_cfg")"
278 append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
279 append if_up "start_net '$ifname' '$net_cfg' \$(wlc ifname '$ifname' bssid)" ";$N"
280 }
281 [ -z "$nasopts" ] || {
282 eval "${vif}_ssid=\"\$ssid\""
283 nas_mode="-A"
284 use_nas=1
285 [ "$mode" = "sta" ] && {
286 nas_mode="-S"
287 [ -z "$bridge" ] || {
288 append vif_post_up "supplicant 1" "$N"
289 append vif_post_up "passphrase $key" "$N"
290
291 use_nas=0
292 }
293 }
294 [ -z "$nas" -o "$use_nas" = "0" ] || {
295 nas_cmd="${nas_cmd:+$nas_cmd$N}start-stop-daemon -S -b -p /var/run/nas.$ifname.pid -x $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"
296 }
297 }
298 _c=$(($_c + 1))
299 done
300 killall -KILL nas >&- 2>&-
301 wlc ifname "$device" stdin <<EOF
302 $ifdown
303
304 gmode ${hwmode:-1}
305 apsta $apsta
306 ap $ap
307 ${mssid:+mssid $mssid}
308 infra $infra
309 ${wet:+wet 1}
310 802.11d 0
311 802.11h ${doth:-0}
312 wme ${wmm:-0}
313 rxant ${rxantenna:-3}
314 txant ${txantenna:-3}
315 fragthresh ${frag:-2346}
316 rtsthresh ${rts:-2347}
317 monitor ${monitor:-0}
318 passive ${passive:-0}
319
320 radio ${radio:-1}
321 macfilter ${macfilter:-0}
322 maclist ${maclist:-none}
323 wds none
324 ${wds:+wds $wds}
325 country ${country:-IL0}
326 ${channel:+channel $channel}
327 maxassoc ${maxassoc:-128}
328 slottime ${slottime:--1}
329 ${frameburst:+frameburst $frameburst}
330
331 $vif_pre_up
332 up
333 $vif_post_up
334 EOF
335 eval "$if_up"
336 wlc ifname "$device" stdin <<EOF
337 $vif_do_up
338 EOF
339
340 # use vif_txpower (from last wifi-iface) instead of txpower (from
341 # wifi-device) if the latter does not exist
342 txpower=${txpower:-$vif_txpower}
343 [ -z "$txpower" ] || iwconfig $device txpower ${txpower}dBm
344
345 eval "$nas_cmd"
346 }
347
348
349 detect_broadcom() {
350 local i=-1
351
352 while [ -f /proc/net/wl$((++i)) ]; do
353 config_get type wl${i} type
354 [ "$type" = broadcom ] && continue
355 cat <<EOF
356 config wifi-device wl${i}
357 option type broadcom
358 option channel 5
359
360 # REMOVE THIS LINE TO ENABLE WIFI:
361 option disabled 1
362
363 config wifi-iface
364 option device wl${i}
365 option network lan
366 option mode ap
367 option ssid OpenWrt${i#0}
368 option encryption none
369
370 EOF
371 done
372 }