Allow the use of psk+psk2 (ap mode only). Thanks ragorn
[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 local vif_pre_up vif_post_up vif_do_up
98
99 _c=0
100 nas="$(which nas)"
101 nas_cmd=
102 if_up=
103
104 [ -z "$slottime" ] && {
105 [ -n "$distance" ] && {
106 # slottime = 9 + (distance / 150) + (distance % 150 ? 1 : 0)
107 slottime="$((9 + ($distance / 150) + 1 - (150 - ($distance % 150)) / 150 ))"
108 }
109 } || {
110 slottime="${slottime:--1}"
111 }
112
113 for vif in $vifs; do
114 config_get mode "$vif" mode
115 append vif_pre_up "vif $_c" "$N"
116 append vif_post_up "vif $_c" "$N"
117
118 [ "$mode" = "sta" ] || {
119 config_get_bool hidden "$vif" hidden 0
120 append vif_pre_up "closed $hidden" "$N"
121 config_get_bool isolate "$vif" isolate 0
122 append vif_pre_up "ap_isolate $isolate" "$N"
123 }
124
125 wsec_r=0
126 eap_r=0
127 wsec=0
128 auth=0
129 nasopts=
130 config_get enc "$vif" encryption
131 case "$enc" in
132 WEP|wep)
133 wsec_r=1
134 wsec=1
135 defkey=1
136 config_get key "$vif" key
137 case "$key" in
138 [1234])
139 defkey="$key"
140 for knr in 1 2 3 4; do
141 config_get k "$vif" key$knr
142 [ -n "$k" ] || continue
143 [ "$defkey" = "$knr" ] && def="=" || def=""
144 append vif_pre_up "wepkey $def$knr,$k" "$N"
145 done
146 ;;
147 "");;
148 *) append vif_pre_up "wepkey =1,$key" "$N";;
149 esac
150 ;;
151 *psk*|*PSK*)
152 wsec_r=1
153 config_get key "$vif" key
154 case "$enc" in
155 wpa*+wpa2*|WPA*+WPA2*|*psk+*psk2|*PSK+*PSK2) auth=132; wsec=6;;
156 wpa2*|WPA2*|*PSK2|*psk2) auth=128; wsec=4;;
157 *) auth=4; wsec=2;;
158 esac
159 eval "${vif}_key=\"\$key\""
160 nasopts="-k \"\$${vif}_key\""
161 ;;
162 *wpa*|*WPA*)
163 wsec_r=1
164 eap_r=1
165 config_get key "$vif" key
166 config_get server "$vif" server
167 config_get port "$vif" port
168 case "$enc" in
169 wpa*+wpa2*|WPA*+WPA2*) auth=66; wsec=6;;
170 wpa2*|WPA2*) auth=64; wsec=4;;
171 *) auth=2; wsec=2;;
172 esac
173 eval "${vif}_key=\"\$key\""
174 nasopts="-r \"\$${vif}_key\" -h $server -p $port"
175 ;;
176 esac
177 append vif_post_up "wsec $wsec" "$N"
178 append vif_post_up "wpa_auth $auth" "$N"
179 append vif_post_up "wsec_restrict $wsec_r" "$N"
180 append vif_post_up "eap_restrict $eap_r" "$N"
181
182 config_get ssid "$vif" ssid
183 append vif_post_up "vlan_mode 0" "$N"
184 append vif_post_up "ssid $ssid" "$N"
185 case "$mode" in
186 sta|adhoc) append vif_do_up "ssid $ssid" "$N";;
187 esac
188
189 append vif_post_up "enabled 1" "$N"
190
191 config_get ifname "$vif" ifname
192 append if_up "ifconfig $ifname up" ";$N"
193 local net_cfg bridge
194 net_cfg="$(find_net_config "$vif")"
195 [ -z "$net_cfg" ] || {
196 bridge="$(bridge_interface "$net_cfg")"
197 append if_up "start_net '$ifname' '$net_cfg'" ";$N"
198 }
199 [ -z "$nasopts" ] || {
200 eval "${vif}_ssid=\"\$ssid\""
201 mode="-A"
202 [ "$vif" = "$sta_if" ] && mode="-S"
203 [ -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 &"
204 }
205 _c=$(($_c + 1))
206 done
207 killall -KILL nas >&- 2>&-
208 wlc stdin <<EOF
209 $ifdown
210
211 ap $ap
212 mssid $mssid
213 apsta $apsta
214 infra $infra
215 ${wet:+wet 1}
216 802.11d 0
217 802.11h 0
218
219 radio ${radio:-1}
220 macfilter 0
221 maclist none
222 wds ${wds:-none}
223 ${channel:+channel $channel}
224 country ${country:-IL0}
225 maxassoc ${maxassoc:-128}
226 slottime ${slottime:--1}
227
228 $vif_pre_up
229 up
230 $vif_post_up
231 EOF
232 eval "$if_up"
233 wlc stdin <<EOF
234 $vif_do_up
235 EOF
236 eval "$nas_cmd"
237 }
238
239
240 detect_broadcom() {
241 [ -f /proc/net/wl0 ] || return
242 config_get type wl0 type
243 [ "$type" = broadcom ] && return
244 cat <<EOF
245 config wifi-device wl0
246 option type broadcom
247 option channel 5
248 # disable radio to prevent an open ap after reflashing:
249 option disabled 1
250
251 config wifi-iface
252 option device wl0
253 option network lan
254 option mode ap
255 option ssid OpenWrt
256 option hidden 0
257 option encryption none
258
259 EOF
260 }