Support for setting monitor mode (#1306)
[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
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 monitor)
30 mon=1
31 mon_if="$vif"
32 ;;
33 *) echo "$device($vif): Invalid mode";;
34 esac
35 done
36 config_set "$device" wds "$wds"
37
38 local _c=
39 for vif in ${adhoc_if:-$sta_if $ap_if $mon_if}; do
40 config_set "$vif" ifname "wl0${_c:+.$_c}"
41 _c=$((${_c:-0} + 1))
42 done
43 config_set "$device" vifs "${adhoc_if:-$sta_if $ap_if $mon_if}"
44
45 ifdown="down"
46 for vif in 0 1 2 3; do
47 append ifdown "vif $vif" "$N"
48 append ifdown "enabled 0" "$N"
49 done
50
51 ap=1
52 infra=1
53 mssid=1
54 apsta=0
55 radio=1
56 monitor=0
57 passive=0
58 case "$adhoc:$sta:$apmode:$mon" in
59 1*)
60 ap=0
61 mssid=
62 infra=0
63 ;;
64 :1:1:)
65 apsta=1
66 wet=1
67 ;;
68 :1::)
69 wet=1
70 ap=0
71 mssid=
72 ;;
73 :::1)
74 wet=1
75 ap=0
76 mssid=
77 monitor=1
78 passive=1
79 ;;
80 ::)
81 radio=0
82 ;;
83 esac
84 }
85
86 disable_broadcom() {
87 set_wifi_down "$1"
88 wlc down
89 (
90 include /lib/network
91
92 # make sure the interfaces are down and removed from all bridges
93 for dev in wl0 wl0.1 wl0.2 wl0.3; do
94 ifconfig "$dev" down 2>/dev/null >/dev/null && {
95 unbridge "$dev"
96 }
97 done
98 )
99 true
100 }
101
102 enable_broadcom() {
103 local _c
104 config_get channel "$device" channel
105 config_get country "$device" country
106 config_get maxassoc "$device" maxassoc
107 config_get wds "$device" wds
108 config_get vifs "$device" vifs
109 config_get distance "$device" distance
110 config_get slottime "$device" slottime
111 config_get rxant "$device" rxant
112 config_get txant "$device" txant
113 config_get_bool frameburst "$device" frameburst
114 config_get macfilter "$device" macfilter
115 config_get maclist "$device" maclist
116 local vif_pre_up vif_post_up vif_do_up
117
118 _c=0
119 nas="$(which nas)"
120 nas_cmd=
121 if_up=
122
123 [ -z "$slottime" ] && {
124 [ -n "$distance" ] && {
125 # slottime = 9 + (distance / 150) + (distance % 150 ? 1 : 0)
126 slottime="$((9 + ($distance / 150) + 1 - (150 - ($distance % 150)) / 150 ))"
127 }
128 } || {
129 slottime="${slottime:--1}"
130 }
131
132 case "$macfilter" in
133 allow|2)
134 macfilter=2;
135 ;;
136 deny|1)
137 macfilter=1;
138 ;;
139 disable|none|0)
140 macfilter=0;
141 ;;
142 esac
143
144 for vif in $vifs; do
145 config_get mode "$vif" mode
146 append vif_pre_up "vif $_c" "$N"
147 append vif_post_up "vif $_c" "$N"
148
149 [ "$mode" = "sta" ] || {
150 config_get_bool hidden "$vif" hidden 0
151 append vif_pre_up "closed $hidden" "$N"
152 config_get_bool isolate "$vif" isolate 0
153 append vif_pre_up "ap_isolate $isolate" "$N"
154 }
155
156 wsec_r=0
157 eap_r=0
158 wsec=0
159 auth=0
160 nasopts=
161 config_get enc "$vif" encryption
162 case "$enc" in
163 WEP|wep)
164 wsec_r=1
165 wsec=1
166 defkey=1
167 config_get key "$vif" key
168 case "$key" in
169 [1234])
170 defkey="$key"
171 for knr in 1 2 3 4; do
172 config_get k "$vif" key$knr
173 [ -n "$k" ] || continue
174 [ "$defkey" = "$knr" ] && def="=" || def=""
175 append vif_do_up "wepkey $def$knr,$k" "$N"
176 done
177 ;;
178 "");;
179 *) append vif_do_up "wepkey =1,$key" "$N";;
180 esac
181 ;;
182 *psk*|*PSK*)
183 wsec_r=1
184 config_get key "$vif" key
185 case "$enc" in
186 wpa*+wpa2*|WPA*+WPA2*|*psk+*psk2|*PSK+*PSK2) auth=132; wsec=6;;
187 wpa2*|WPA2*|*PSK2|*psk2) auth=128; wsec=4;;
188 *) auth=4; wsec=2;;
189 esac
190 eval "${vif}_key=\"\$key\""
191 nasopts="-k \"\$${vif}_key\""
192 ;;
193 *wpa*|*WPA*)
194 wsec_r=1
195 eap_r=1
196 config_get key "$vif" key
197 config_get server "$vif" server
198 config_get port "$vif" port
199 case "$enc" in
200 wpa*+wpa2*|WPA*+WPA2*) auth=66; wsec=6;;
201 wpa2*|WPA2*) auth=64; wsec=4;;
202 *) auth=2; wsec=2;;
203 esac
204 eval "${vif}_key=\"\$key\""
205 nasopts="-r \"\$${vif}_key\" -h $server -p $port"
206 ;;
207 esac
208 append vif_do_up "wsec $wsec" "$N"
209 append vif_do_up "wpa_auth $auth" "$N"
210 append vif_do_up "wsec_restrict $wsec_r" "$N"
211 append vif_do_up "eap_restrict $eap_r" "$N"
212
213 config_get ssid "$vif" ssid
214 append vif_post_up "vlan_mode 0" "$N"
215 append vif_post_up "ssid $ssid" "$N"
216 append vif_do_up "ssid $ssid" "$N"
217
218 [ "$mode" = "monitor" ] && {
219 append vif_post_up "monitor $monitor" "$N"
220 append vif_post_up "passive $passive" "$N"
221 }
222
223 append vif_post_up "enabled 1" "$N"
224
225 config_get ifname "$vif" ifname
226 #append if_up "ifconfig $ifname up" ";$N"
227 local net_cfg bridge
228 net_cfg="$(find_net_config "$vif")"
229 [ -z "$net_cfg" ] || {
230 bridge="$(bridge_interface "$net_cfg")"
231 append if_up "start_net '$ifname' '$net_cfg'" ";$N"
232 append if_up "set_wifi_up '$vif' '$ifname'" ";$N"
233 }
234 [ -z "$nasopts" ] || {
235 eval "${vif}_ssid=\"\$ssid\""
236 nas_mode="-A"
237 use_nas=1
238 [ "$mode" = "sta" ] && {
239 nas_mode="-S"
240 [ -z "$bridge" ] || {
241 append vif_post_up "supplicant 1" "$N"
242 append vif_post_up "passphrase $key" "$N"
243
244 use_nas=0
245 }
246 }
247 [ -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 &"
248 }
249 _c=$(($_c + 1))
250 done
251 killall -KILL nas >&- 2>&-
252 wlc stdin <<EOF
253 $ifdown
254
255 ap $ap
256 ${mssid:+mssid $mssid}
257 apsta $apsta
258 infra $infra
259 ${wet:+wet 1}
260 802.11d 0
261 802.11h 0
262 rxant ${rxant:-3}
263 txant ${txant:-3}
264 monitor ${monitor:-0}
265 passive ${passive:-0}
266
267 radio ${radio:-1}
268 macfilter ${macfilter:-0}
269 maclist ${maclist:-none}
270 wds none
271 ${wds:+wds $wds}
272 country ${country:-IL0}
273 ${channel:+channel $channel}
274 maxassoc ${maxassoc:-128}
275 slottime ${slottime:--1}
276 ${frameburst:+frameburst $frameburst}
277
278 $vif_pre_up
279 up
280 $vif_post_up
281 EOF
282 eval "$if_up"
283 wlc stdin <<EOF
284 $vif_do_up
285 EOF
286 eval "$nas_cmd"
287 }
288
289
290 detect_broadcom() {
291 [ -f /proc/net/wl0 ] || return
292 config_get type wl0 type
293 [ "$type" = broadcom ] && return
294 cat <<EOF
295 config wifi-device wl0
296 option type broadcom
297 option channel 5
298
299 # REMOVE THIS LINE TO ENABLE WIFI:
300 option disabled 1
301
302 config wifi-iface
303 option device wl0
304 option network lan
305 option mode ap
306 option ssid OpenWrt
307 option encryption none
308 EOF
309 }