Commit | Line | Data |
---|---|---|
f0706e82 JB |
1 | /* |
2 | * BSS client mode implementation | |
3 | * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi> | |
4 | * Copyright 2004, Instant802 Networks, Inc. | |
5 | * Copyright 2005, Devicescape Software, Inc. | |
6 | * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> | |
7 | * Copyright 2007, Michael Wu <flamingice@sourmilk.net> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | */ | |
13 | ||
14 | /* TODO: | |
f0706e82 JB |
15 | * order BSS list by RSSI(?) ("quality of AP") |
16 | * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE, | |
17 | * SSID) | |
18 | */ | |
5b323edb | 19 | #include <linux/delay.h> |
f0706e82 JB |
20 | #include <linux/if_ether.h> |
21 | #include <linux/skbuff.h> | |
22 | #include <linux/netdevice.h> | |
23 | #include <linux/if_arp.h> | |
24 | #include <linux/wireless.h> | |
25 | #include <linux/random.h> | |
26 | #include <linux/etherdevice.h> | |
f0706e82 JB |
27 | #include <net/iw_handler.h> |
28 | #include <asm/types.h> | |
f0706e82 JB |
29 | |
30 | #include <net/mac80211.h> | |
31 | #include "ieee80211_i.h" | |
32 | #include "ieee80211_rate.h" | |
47f0c502 | 33 | #include "ieee80211_led.h" |
f0706e82 JB |
34 | |
35 | #define IEEE80211_AUTH_TIMEOUT (HZ / 5) | |
36 | #define IEEE80211_AUTH_MAX_TRIES 3 | |
37 | #define IEEE80211_ASSOC_TIMEOUT (HZ / 5) | |
38 | #define IEEE80211_ASSOC_MAX_TRIES 3 | |
39 | #define IEEE80211_MONITORING_INTERVAL (2 * HZ) | |
40 | #define IEEE80211_PROBE_INTERVAL (60 * HZ) | |
41 | #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ) | |
42 | #define IEEE80211_SCAN_INTERVAL (2 * HZ) | |
43 | #define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ) | |
44 | #define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ) | |
45 | ||
46 | #define IEEE80211_PROBE_DELAY (HZ / 33) | |
47 | #define IEEE80211_CHANNEL_TIME (HZ / 33) | |
48 | #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5) | |
49 | #define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ) | |
50 | #define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ) | |
51 | #define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ) | |
52 | ||
53 | #define IEEE80211_IBSS_MAX_STA_ENTRIES 128 | |
54 | ||
55 | ||
56 | #define IEEE80211_FC(type, stype) cpu_to_le16(type | stype) | |
57 | ||
58 | #define ERP_INFO_USE_PROTECTION BIT(1) | |
59 | ||
60 | static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst, | |
61 | u8 *ssid, size_t ssid_len); | |
62 | static struct ieee80211_sta_bss * | |
cffdd30d JL |
63 | ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int channel, |
64 | u8 *ssid, u8 ssid_len); | |
f0706e82 JB |
65 | static void ieee80211_rx_bss_put(struct net_device *dev, |
66 | struct ieee80211_sta_bss *bss); | |
67 | static int ieee80211_sta_find_ibss(struct net_device *dev, | |
68 | struct ieee80211_if_sta *ifsta); | |
69 | static int ieee80211_sta_wep_configured(struct net_device *dev); | |
70 | static int ieee80211_sta_start_scan(struct net_device *dev, | |
71 | u8 *ssid, size_t ssid_len); | |
72 | static int ieee80211_sta_config_auth(struct net_device *dev, | |
73 | struct ieee80211_if_sta *ifsta); | |
74 | ||
75 | ||
76 | /* Parsed Information Elements */ | |
77 | struct ieee802_11_elems { | |
5558235c | 78 | /* pointers to IEs */ |
f0706e82 | 79 | u8 *ssid; |
f0706e82 | 80 | u8 *supp_rates; |
f0706e82 | 81 | u8 *fh_params; |
f0706e82 | 82 | u8 *ds_params; |
f0706e82 | 83 | u8 *cf_params; |
f0706e82 | 84 | u8 *tim; |
f0706e82 | 85 | u8 *ibss_params; |
f0706e82 | 86 | u8 *challenge; |
f0706e82 | 87 | u8 *wpa; |
f0706e82 | 88 | u8 *rsn; |
f0706e82 | 89 | u8 *erp_info; |
f0706e82 | 90 | u8 *ext_supp_rates; |
f0706e82 | 91 | u8 *wmm_info; |
f0706e82 | 92 | u8 *wmm_param; |
5558235c JB |
93 | |
94 | /* length of them, respectively */ | |
95 | u8 ssid_len; | |
96 | u8 supp_rates_len; | |
97 | u8 fh_params_len; | |
98 | u8 ds_params_len; | |
99 | u8 cf_params_len; | |
100 | u8 tim_len; | |
101 | u8 ibss_params_len; | |
102 | u8 challenge_len; | |
103 | u8 wpa_len; | |
104 | u8 rsn_len; | |
105 | u8 erp_info_len; | |
106 | u8 ext_supp_rates_len; | |
107 | u8 wmm_info_len; | |
f0706e82 JB |
108 | u8 wmm_param_len; |
109 | }; | |
110 | ||
67a4cce4 JL |
111 | static void ieee802_11_parse_elems(u8 *start, size_t len, |
112 | struct ieee802_11_elems *elems) | |
f0706e82 JB |
113 | { |
114 | size_t left = len; | |
115 | u8 *pos = start; | |
f0706e82 JB |
116 | |
117 | memset(elems, 0, sizeof(*elems)); | |
118 | ||
119 | while (left >= 2) { | |
120 | u8 id, elen; | |
121 | ||
122 | id = *pos++; | |
123 | elen = *pos++; | |
124 | left -= 2; | |
125 | ||
67a4cce4 JL |
126 | if (elen > left) |
127 | return; | |
f0706e82 JB |
128 | |
129 | switch (id) { | |
130 | case WLAN_EID_SSID: | |
131 | elems->ssid = pos; | |
132 | elems->ssid_len = elen; | |
133 | break; | |
134 | case WLAN_EID_SUPP_RATES: | |
135 | elems->supp_rates = pos; | |
136 | elems->supp_rates_len = elen; | |
137 | break; | |
138 | case WLAN_EID_FH_PARAMS: | |
139 | elems->fh_params = pos; | |
140 | elems->fh_params_len = elen; | |
141 | break; | |
142 | case WLAN_EID_DS_PARAMS: | |
143 | elems->ds_params = pos; | |
144 | elems->ds_params_len = elen; | |
145 | break; | |
146 | case WLAN_EID_CF_PARAMS: | |
147 | elems->cf_params = pos; | |
148 | elems->cf_params_len = elen; | |
149 | break; | |
150 | case WLAN_EID_TIM: | |
151 | elems->tim = pos; | |
152 | elems->tim_len = elen; | |
153 | break; | |
154 | case WLAN_EID_IBSS_PARAMS: | |
155 | elems->ibss_params = pos; | |
156 | elems->ibss_params_len = elen; | |
157 | break; | |
158 | case WLAN_EID_CHALLENGE: | |
159 | elems->challenge = pos; | |
160 | elems->challenge_len = elen; | |
161 | break; | |
162 | case WLAN_EID_WPA: | |
163 | if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 && | |
164 | pos[2] == 0xf2) { | |
165 | /* Microsoft OUI (00:50:F2) */ | |
166 | if (pos[3] == 1) { | |
167 | /* OUI Type 1 - WPA IE */ | |
168 | elems->wpa = pos; | |
169 | elems->wpa_len = elen; | |
170 | } else if (elen >= 5 && pos[3] == 2) { | |
171 | if (pos[4] == 0) { | |
172 | elems->wmm_info = pos; | |
173 | elems->wmm_info_len = elen; | |
174 | } else if (pos[4] == 1) { | |
175 | elems->wmm_param = pos; | |
176 | elems->wmm_param_len = elen; | |
177 | } | |
178 | } | |
179 | } | |
180 | break; | |
181 | case WLAN_EID_RSN: | |
182 | elems->rsn = pos; | |
183 | elems->rsn_len = elen; | |
184 | break; | |
185 | case WLAN_EID_ERP_INFO: | |
186 | elems->erp_info = pos; | |
187 | elems->erp_info_len = elen; | |
188 | break; | |
189 | case WLAN_EID_EXT_SUPP_RATES: | |
190 | elems->ext_supp_rates = pos; | |
191 | elems->ext_supp_rates_len = elen; | |
192 | break; | |
193 | default: | |
f0706e82 JB |
194 | break; |
195 | } | |
196 | ||
197 | left -= elen; | |
198 | pos += elen; | |
199 | } | |
f0706e82 JB |
200 | } |
201 | ||
202 | ||
f0706e82 JB |
203 | static int ecw2cw(int ecw) |
204 | { | |
205 | int cw = 1; | |
206 | while (ecw > 0) { | |
207 | cw <<= 1; | |
208 | ecw--; | |
209 | } | |
210 | return cw - 1; | |
211 | } | |
212 | ||
f0706e82 JB |
213 | static void ieee80211_sta_wmm_params(struct net_device *dev, |
214 | struct ieee80211_if_sta *ifsta, | |
215 | u8 *wmm_param, size_t wmm_param_len) | |
216 | { | |
217 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
218 | struct ieee80211_tx_queue_params params; | |
219 | size_t left; | |
220 | int count; | |
221 | u8 *pos; | |
222 | ||
223 | if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1) | |
224 | return; | |
225 | count = wmm_param[6] & 0x0f; | |
226 | if (count == ifsta->wmm_last_param_set) | |
227 | return; | |
228 | ifsta->wmm_last_param_set = count; | |
229 | ||
230 | pos = wmm_param + 8; | |
231 | left = wmm_param_len - 8; | |
232 | ||
233 | memset(¶ms, 0, sizeof(params)); | |
234 | ||
235 | if (!local->ops->conf_tx) | |
236 | return; | |
237 | ||
238 | local->wmm_acm = 0; | |
239 | for (; left >= 4; left -= 4, pos += 4) { | |
240 | int aci = (pos[0] >> 5) & 0x03; | |
241 | int acm = (pos[0] >> 4) & 0x01; | |
242 | int queue; | |
243 | ||
244 | switch (aci) { | |
245 | case 1: | |
246 | queue = IEEE80211_TX_QUEUE_DATA3; | |
247 | if (acm) { | |
248 | local->wmm_acm |= BIT(0) | BIT(3); | |
249 | } | |
250 | break; | |
251 | case 2: | |
252 | queue = IEEE80211_TX_QUEUE_DATA1; | |
253 | if (acm) { | |
254 | local->wmm_acm |= BIT(4) | BIT(5); | |
255 | } | |
256 | break; | |
257 | case 3: | |
258 | queue = IEEE80211_TX_QUEUE_DATA0; | |
259 | if (acm) { | |
260 | local->wmm_acm |= BIT(6) | BIT(7); | |
261 | } | |
262 | break; | |
263 | case 0: | |
264 | default: | |
265 | queue = IEEE80211_TX_QUEUE_DATA2; | |
266 | if (acm) { | |
267 | local->wmm_acm |= BIT(1) | BIT(2); | |
268 | } | |
269 | break; | |
270 | } | |
271 | ||
272 | params.aifs = pos[0] & 0x0f; | |
273 | params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4); | |
274 | params.cw_min = ecw2cw(pos[1] & 0x0f); | |
275 | /* TXOP is in units of 32 usec; burst_time in 0.1 ms */ | |
276 | params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100; | |
277 | printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d " | |
278 | "cWmin=%d cWmax=%d burst=%d\n", | |
279 | dev->name, queue, aci, acm, params.aifs, params.cw_min, | |
280 | params.cw_max, params.burst_time); | |
281 | /* TODO: handle ACM (block TX, fallback to next lowest allowed | |
282 | * AC for now) */ | |
283 | if (local->ops->conf_tx(local_to_hw(local), queue, ¶ms)) { | |
284 | printk(KERN_DEBUG "%s: failed to set TX queue " | |
285 | "parameters for queue %d\n", dev->name, queue); | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
290 | ||
5628221c DD |
291 | static void ieee80211_handle_erp_ie(struct net_device *dev, u8 erp_value) |
292 | { | |
5628221c DD |
293 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
294 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
295 | int use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0; | |
7e9ed188 | 296 | int preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0; |
d9430a32 | 297 | u8 changes = 0; |
0795af57 | 298 | DECLARE_MAC_BUF(mac); |
5628221c | 299 | |
13262ffd | 300 | if (use_protection != !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION)) { |
5628221c DD |
301 | if (net_ratelimit()) { |
302 | printk(KERN_DEBUG "%s: CTS protection %s (BSSID=" | |
0795af57 | 303 | "%s)\n", |
5628221c DD |
304 | dev->name, |
305 | use_protection ? "enabled" : "disabled", | |
0795af57 | 306 | print_mac(mac, ifsta->bssid)); |
5628221c | 307 | } |
13262ffd JS |
308 | if (use_protection) |
309 | sdata->flags |= IEEE80211_SDATA_USE_PROTECTION; | |
310 | else | |
311 | sdata->flags &= ~IEEE80211_SDATA_USE_PROTECTION; | |
d9430a32 | 312 | changes |= IEEE80211_ERP_CHANGE_PROTECTION; |
5628221c | 313 | } |
7e9ed188 | 314 | |
13262ffd | 315 | if (preamble_mode != !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE)) { |
7e9ed188 DD |
316 | if (net_ratelimit()) { |
317 | printk(KERN_DEBUG "%s: switched to %s barker preamble" | |
0795af57 | 318 | " (BSSID=%s)\n", |
7e9ed188 DD |
319 | dev->name, |
320 | (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ? | |
321 | "short" : "long", | |
0795af57 | 322 | print_mac(mac, ifsta->bssid)); |
7e9ed188 | 323 | } |
13262ffd JS |
324 | if (preamble_mode) |
325 | sdata->flags &= ~IEEE80211_SDATA_SHORT_PREAMBLE; | |
326 | else | |
327 | sdata->flags |= IEEE80211_SDATA_SHORT_PREAMBLE; | |
d9430a32 | 328 | changes |= IEEE80211_ERP_CHANGE_PREAMBLE; |
7e9ed188 | 329 | } |
d9430a32 DD |
330 | |
331 | if (changes) | |
332 | ieee80211_erp_info_change_notify(dev, changes); | |
5628221c DD |
333 | } |
334 | ||
335 | ||
f0706e82 JB |
336 | static void ieee80211_sta_send_associnfo(struct net_device *dev, |
337 | struct ieee80211_if_sta *ifsta) | |
338 | { | |
339 | char *buf; | |
340 | size_t len; | |
341 | int i; | |
342 | union iwreq_data wrqu; | |
343 | ||
344 | if (!ifsta->assocreq_ies && !ifsta->assocresp_ies) | |
345 | return; | |
346 | ||
347 | buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len + | |
0ec0b7ac | 348 | ifsta->assocresp_ies_len), GFP_KERNEL); |
f0706e82 JB |
349 | if (!buf) |
350 | return; | |
351 | ||
352 | len = sprintf(buf, "ASSOCINFO("); | |
353 | if (ifsta->assocreq_ies) { | |
354 | len += sprintf(buf + len, "ReqIEs="); | |
355 | for (i = 0; i < ifsta->assocreq_ies_len; i++) { | |
356 | len += sprintf(buf + len, "%02x", | |
357 | ifsta->assocreq_ies[i]); | |
358 | } | |
359 | } | |
360 | if (ifsta->assocresp_ies) { | |
361 | if (ifsta->assocreq_ies) | |
362 | len += sprintf(buf + len, " "); | |
363 | len += sprintf(buf + len, "RespIEs="); | |
364 | for (i = 0; i < ifsta->assocresp_ies_len; i++) { | |
365 | len += sprintf(buf + len, "%02x", | |
366 | ifsta->assocresp_ies[i]); | |
367 | } | |
368 | } | |
369 | len += sprintf(buf + len, ")"); | |
370 | ||
371 | if (len > IW_CUSTOM_MAX) { | |
372 | len = sprintf(buf, "ASSOCRESPIE="); | |
373 | for (i = 0; i < ifsta->assocresp_ies_len; i++) { | |
374 | len += sprintf(buf + len, "%02x", | |
375 | ifsta->assocresp_ies[i]); | |
376 | } | |
377 | } | |
378 | ||
379 | memset(&wrqu, 0, sizeof(wrqu)); | |
380 | wrqu.data.length = len; | |
381 | wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf); | |
382 | ||
383 | kfree(buf); | |
384 | } | |
385 | ||
386 | ||
387 | static void ieee80211_set_associated(struct net_device *dev, | |
d6f2da5b | 388 | struct ieee80211_if_sta *ifsta, |
47f0c502 | 389 | bool assoc) |
f0706e82 | 390 | { |
47f0c502 | 391 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
f0706e82 JB |
392 | union iwreq_data wrqu; |
393 | ||
d6f2da5b | 394 | if (!!(ifsta->flags & IEEE80211_STA_ASSOCIATED) == assoc) |
f0706e82 JB |
395 | return; |
396 | ||
f0706e82 JB |
397 | if (assoc) { |
398 | struct ieee80211_sub_if_data *sdata; | |
5628221c | 399 | struct ieee80211_sta_bss *bss; |
d6f2da5b JS |
400 | |
401 | ifsta->flags |= IEEE80211_STA_ASSOCIATED; | |
402 | ||
f0706e82 JB |
403 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
404 | if (sdata->type != IEEE80211_IF_TYPE_STA) | |
405 | return; | |
5628221c | 406 | |
65c107ab | 407 | bss = ieee80211_rx_bss_get(dev, ifsta->bssid, |
cffdd30d JL |
408 | local->hw.conf.channel, |
409 | ifsta->ssid, ifsta->ssid_len); | |
5628221c DD |
410 | if (bss) { |
411 | if (bss->has_erp_value) | |
412 | ieee80211_handle_erp_ie(dev, bss->erp_value); | |
413 | ieee80211_rx_bss_put(dev, bss); | |
414 | } | |
415 | ||
f0706e82 | 416 | netif_carrier_on(dev); |
d6f2da5b | 417 | ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET; |
f0706e82 JB |
418 | memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN); |
419 | memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN); | |
420 | ieee80211_sta_send_associnfo(dev, ifsta); | |
421 | } else { | |
d6f2da5b JS |
422 | ifsta->flags &= ~IEEE80211_STA_ASSOCIATED; |
423 | ||
f0706e82 | 424 | netif_carrier_off(dev); |
d9430a32 | 425 | ieee80211_reset_erp_info(dev); |
f0706e82 JB |
426 | memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN); |
427 | } | |
428 | wrqu.ap_addr.sa_family = ARPHRD_ETHER; | |
429 | wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL); | |
430 | ifsta->last_probe = jiffies; | |
47f0c502 | 431 | ieee80211_led_assoc(local, assoc); |
f0706e82 JB |
432 | } |
433 | ||
434 | static void ieee80211_set_disassoc(struct net_device *dev, | |
435 | struct ieee80211_if_sta *ifsta, int deauth) | |
436 | { | |
437 | if (deauth) | |
438 | ifsta->auth_tries = 0; | |
439 | ifsta->assoc_tries = 0; | |
440 | ieee80211_set_associated(dev, ifsta, 0); | |
441 | } | |
442 | ||
443 | static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb, | |
444 | int encrypt) | |
445 | { | |
446 | struct ieee80211_sub_if_data *sdata; | |
447 | struct ieee80211_tx_packet_data *pkt_data; | |
448 | ||
449 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
450 | skb->dev = sdata->local->mdev; | |
451 | skb_set_mac_header(skb, 0); | |
452 | skb_set_network_header(skb, 0); | |
453 | skb_set_transport_header(skb, 0); | |
454 | ||
455 | pkt_data = (struct ieee80211_tx_packet_data *) skb->cb; | |
456 | memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data)); | |
457 | pkt_data->ifindex = sdata->dev->ifindex; | |
e8bf9649 JS |
458 | if (!encrypt) |
459 | pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT; | |
f0706e82 JB |
460 | |
461 | dev_queue_xmit(skb); | |
462 | } | |
463 | ||
464 | ||
465 | static void ieee80211_send_auth(struct net_device *dev, | |
466 | struct ieee80211_if_sta *ifsta, | |
467 | int transaction, u8 *extra, size_t extra_len, | |
468 | int encrypt) | |
469 | { | |
470 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
471 | struct sk_buff *skb; | |
472 | struct ieee80211_mgmt *mgmt; | |
473 | ||
474 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + | |
475 | sizeof(*mgmt) + 6 + extra_len); | |
476 | if (!skb) { | |
477 | printk(KERN_DEBUG "%s: failed to allocate buffer for auth " | |
478 | "frame\n", dev->name); | |
479 | return; | |
480 | } | |
481 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
482 | ||
483 | mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6); | |
484 | memset(mgmt, 0, 24 + 6); | |
485 | mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT, | |
486 | IEEE80211_STYPE_AUTH); | |
487 | if (encrypt) | |
488 | mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); | |
489 | memcpy(mgmt->da, ifsta->bssid, ETH_ALEN); | |
490 | memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN); | |
491 | memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN); | |
492 | mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg); | |
493 | mgmt->u.auth.auth_transaction = cpu_to_le16(transaction); | |
494 | ifsta->auth_transaction = transaction + 1; | |
495 | mgmt->u.auth.status_code = cpu_to_le16(0); | |
496 | if (extra) | |
497 | memcpy(skb_put(skb, extra_len), extra, extra_len); | |
498 | ||
499 | ieee80211_sta_tx(dev, skb, encrypt); | |
500 | } | |
501 | ||
502 | ||
503 | static void ieee80211_authenticate(struct net_device *dev, | |
504 | struct ieee80211_if_sta *ifsta) | |
505 | { | |
0795af57 JP |
506 | DECLARE_MAC_BUF(mac); |
507 | ||
f0706e82 JB |
508 | ifsta->auth_tries++; |
509 | if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) { | |
0795af57 | 510 | printk(KERN_DEBUG "%s: authentication with AP %s" |
f0706e82 | 511 | " timed out\n", |
0795af57 | 512 | dev->name, print_mac(mac, ifsta->bssid)); |
f0706e82 JB |
513 | ifsta->state = IEEE80211_DISABLED; |
514 | return; | |
515 | } | |
516 | ||
517 | ifsta->state = IEEE80211_AUTHENTICATE; | |
0795af57 JP |
518 | printk(KERN_DEBUG "%s: authenticate with AP %s\n", |
519 | dev->name, print_mac(mac, ifsta->bssid)); | |
f0706e82 JB |
520 | |
521 | ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0); | |
522 | ||
523 | mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT); | |
524 | } | |
525 | ||
526 | ||
527 | static void ieee80211_send_assoc(struct net_device *dev, | |
528 | struct ieee80211_if_sta *ifsta) | |
529 | { | |
530 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
531 | struct ieee80211_hw_mode *mode; | |
532 | struct sk_buff *skb; | |
533 | struct ieee80211_mgmt *mgmt; | |
534 | u8 *pos, *ies; | |
535 | int i, len; | |
536 | u16 capab; | |
537 | struct ieee80211_sta_bss *bss; | |
538 | int wmm = 0; | |
539 | ||
540 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + | |
541 | sizeof(*mgmt) + 200 + ifsta->extra_ie_len + | |
542 | ifsta->ssid_len); | |
543 | if (!skb) { | |
544 | printk(KERN_DEBUG "%s: failed to allocate buffer for assoc " | |
545 | "frame\n", dev->name); | |
546 | return; | |
547 | } | |
548 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
549 | ||
550 | mode = local->oper_hw_mode; | |
551 | capab = ifsta->capab; | |
552 | if (mode->mode == MODE_IEEE80211G) { | |
553 | capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME | | |
554 | WLAN_CAPABILITY_SHORT_PREAMBLE; | |
555 | } | |
cffdd30d JL |
556 | bss = ieee80211_rx_bss_get(dev, ifsta->bssid, local->hw.conf.channel, |
557 | ifsta->ssid, ifsta->ssid_len); | |
f0706e82 JB |
558 | if (bss) { |
559 | if (bss->capability & WLAN_CAPABILITY_PRIVACY) | |
560 | capab |= WLAN_CAPABILITY_PRIVACY; | |
561 | if (bss->wmm_ie) { | |
562 | wmm = 1; | |
563 | } | |
564 | ieee80211_rx_bss_put(dev, bss); | |
565 | } | |
566 | ||
567 | mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24); | |
568 | memset(mgmt, 0, 24); | |
569 | memcpy(mgmt->da, ifsta->bssid, ETH_ALEN); | |
570 | memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN); | |
571 | memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN); | |
572 | ||
d6f2da5b | 573 | if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) { |
f0706e82 JB |
574 | skb_put(skb, 10); |
575 | mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT, | |
576 | IEEE80211_STYPE_REASSOC_REQ); | |
577 | mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab); | |
578 | mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1); | |
579 | memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid, | |
580 | ETH_ALEN); | |
581 | } else { | |
582 | skb_put(skb, 4); | |
583 | mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT, | |
584 | IEEE80211_STYPE_ASSOC_REQ); | |
585 | mgmt->u.assoc_req.capab_info = cpu_to_le16(capab); | |
586 | mgmt->u.assoc_req.listen_interval = cpu_to_le16(1); | |
587 | } | |
588 | ||
589 | /* SSID */ | |
590 | ies = pos = skb_put(skb, 2 + ifsta->ssid_len); | |
591 | *pos++ = WLAN_EID_SSID; | |
592 | *pos++ = ifsta->ssid_len; | |
593 | memcpy(pos, ifsta->ssid, ifsta->ssid_len); | |
594 | ||
595 | len = mode->num_rates; | |
596 | if (len > 8) | |
597 | len = 8; | |
598 | pos = skb_put(skb, len + 2); | |
599 | *pos++ = WLAN_EID_SUPP_RATES; | |
600 | *pos++ = len; | |
601 | for (i = 0; i < len; i++) { | |
602 | int rate = mode->rates[i].rate; | |
f0706e82 JB |
603 | *pos++ = (u8) (rate / 5); |
604 | } | |
605 | ||
606 | if (mode->num_rates > len) { | |
607 | pos = skb_put(skb, mode->num_rates - len + 2); | |
608 | *pos++ = WLAN_EID_EXT_SUPP_RATES; | |
609 | *pos++ = mode->num_rates - len; | |
610 | for (i = len; i < mode->num_rates; i++) { | |
611 | int rate = mode->rates[i].rate; | |
f0706e82 JB |
612 | *pos++ = (u8) (rate / 5); |
613 | } | |
614 | } | |
615 | ||
616 | if (ifsta->extra_ie) { | |
617 | pos = skb_put(skb, ifsta->extra_ie_len); | |
618 | memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len); | |
619 | } | |
620 | ||
d6f2da5b | 621 | if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) { |
f0706e82 JB |
622 | pos = skb_put(skb, 9); |
623 | *pos++ = WLAN_EID_VENDOR_SPECIFIC; | |
624 | *pos++ = 7; /* len */ | |
625 | *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */ | |
626 | *pos++ = 0x50; | |
627 | *pos++ = 0xf2; | |
628 | *pos++ = 2; /* WME */ | |
629 | *pos++ = 0; /* WME info */ | |
630 | *pos++ = 1; /* WME ver */ | |
631 | *pos++ = 0; | |
632 | } | |
633 | ||
634 | kfree(ifsta->assocreq_ies); | |
635 | ifsta->assocreq_ies_len = (skb->data + skb->len) - ies; | |
0ec0b7ac | 636 | ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL); |
f0706e82 JB |
637 | if (ifsta->assocreq_ies) |
638 | memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len); | |
639 | ||
640 | ieee80211_sta_tx(dev, skb, 0); | |
641 | } | |
642 | ||
643 | ||
644 | static void ieee80211_send_deauth(struct net_device *dev, | |
645 | struct ieee80211_if_sta *ifsta, u16 reason) | |
646 | { | |
647 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
648 | struct sk_buff *skb; | |
649 | struct ieee80211_mgmt *mgmt; | |
650 | ||
651 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt)); | |
652 | if (!skb) { | |
653 | printk(KERN_DEBUG "%s: failed to allocate buffer for deauth " | |
654 | "frame\n", dev->name); | |
655 | return; | |
656 | } | |
657 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
658 | ||
659 | mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24); | |
660 | memset(mgmt, 0, 24); | |
661 | memcpy(mgmt->da, ifsta->bssid, ETH_ALEN); | |
662 | memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN); | |
663 | memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN); | |
664 | mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT, | |
665 | IEEE80211_STYPE_DEAUTH); | |
666 | skb_put(skb, 2); | |
667 | mgmt->u.deauth.reason_code = cpu_to_le16(reason); | |
668 | ||
669 | ieee80211_sta_tx(dev, skb, 0); | |
670 | } | |
671 | ||
672 | ||
673 | static void ieee80211_send_disassoc(struct net_device *dev, | |
674 | struct ieee80211_if_sta *ifsta, u16 reason) | |
675 | { | |
676 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
677 | struct sk_buff *skb; | |
678 | struct ieee80211_mgmt *mgmt; | |
679 | ||
680 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt)); | |
681 | if (!skb) { | |
682 | printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc " | |
683 | "frame\n", dev->name); | |
684 | return; | |
685 | } | |
686 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
687 | ||
688 | mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24); | |
689 | memset(mgmt, 0, 24); | |
690 | memcpy(mgmt->da, ifsta->bssid, ETH_ALEN); | |
691 | memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN); | |
692 | memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN); | |
693 | mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT, | |
694 | IEEE80211_STYPE_DISASSOC); | |
695 | skb_put(skb, 2); | |
696 | mgmt->u.disassoc.reason_code = cpu_to_le16(reason); | |
697 | ||
698 | ieee80211_sta_tx(dev, skb, 0); | |
699 | } | |
700 | ||
701 | ||
702 | static int ieee80211_privacy_mismatch(struct net_device *dev, | |
703 | struct ieee80211_if_sta *ifsta) | |
704 | { | |
65c107ab | 705 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
f0706e82 JB |
706 | struct ieee80211_sta_bss *bss; |
707 | int res = 0; | |
708 | ||
d6f2da5b JS |
709 | if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL) || |
710 | ifsta->key_management_enabled) | |
f0706e82 JB |
711 | return 0; |
712 | ||
cffdd30d JL |
713 | bss = ieee80211_rx_bss_get(dev, ifsta->bssid, local->hw.conf.channel, |
714 | ifsta->ssid, ifsta->ssid_len); | |
f0706e82 JB |
715 | if (!bss) |
716 | return 0; | |
717 | ||
718 | if (ieee80211_sta_wep_configured(dev) != | |
719 | !!(bss->capability & WLAN_CAPABILITY_PRIVACY)) | |
720 | res = 1; | |
721 | ||
722 | ieee80211_rx_bss_put(dev, bss); | |
723 | ||
724 | return res; | |
725 | } | |
726 | ||
727 | ||
728 | static void ieee80211_associate(struct net_device *dev, | |
729 | struct ieee80211_if_sta *ifsta) | |
730 | { | |
0795af57 JP |
731 | DECLARE_MAC_BUF(mac); |
732 | ||
f0706e82 JB |
733 | ifsta->assoc_tries++; |
734 | if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) { | |
0795af57 | 735 | printk(KERN_DEBUG "%s: association with AP %s" |
f0706e82 | 736 | " timed out\n", |
0795af57 | 737 | dev->name, print_mac(mac, ifsta->bssid)); |
f0706e82 JB |
738 | ifsta->state = IEEE80211_DISABLED; |
739 | return; | |
740 | } | |
741 | ||
742 | ifsta->state = IEEE80211_ASSOCIATE; | |
0795af57 JP |
743 | printk(KERN_DEBUG "%s: associate with AP %s\n", |
744 | dev->name, print_mac(mac, ifsta->bssid)); | |
f0706e82 JB |
745 | if (ieee80211_privacy_mismatch(dev, ifsta)) { |
746 | printk(KERN_DEBUG "%s: mismatch in privacy configuration and " | |
747 | "mixed-cell disabled - abort association\n", dev->name); | |
748 | ifsta->state = IEEE80211_DISABLED; | |
749 | return; | |
750 | } | |
751 | ||
752 | ieee80211_send_assoc(dev, ifsta); | |
753 | ||
754 | mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT); | |
755 | } | |
756 | ||
757 | ||
758 | static void ieee80211_associated(struct net_device *dev, | |
759 | struct ieee80211_if_sta *ifsta) | |
760 | { | |
761 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
762 | struct sta_info *sta; | |
763 | int disassoc; | |
0795af57 | 764 | DECLARE_MAC_BUF(mac); |
f0706e82 JB |
765 | |
766 | /* TODO: start monitoring current AP signal quality and number of | |
767 | * missed beacons. Scan other channels every now and then and search | |
768 | * for better APs. */ | |
769 | /* TODO: remove expired BSSes */ | |
770 | ||
771 | ifsta->state = IEEE80211_ASSOCIATED; | |
772 | ||
773 | sta = sta_info_get(local, ifsta->bssid); | |
774 | if (!sta) { | |
0795af57 JP |
775 | printk(KERN_DEBUG "%s: No STA entry for own AP %s\n", |
776 | dev->name, print_mac(mac, ifsta->bssid)); | |
f0706e82 JB |
777 | disassoc = 1; |
778 | } else { | |
779 | disassoc = 0; | |
780 | if (time_after(jiffies, | |
781 | sta->last_rx + IEEE80211_MONITORING_INTERVAL)) { | |
d6f2da5b | 782 | if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) { |
f0706e82 | 783 | printk(KERN_DEBUG "%s: No ProbeResp from " |
0795af57 | 784 | "current AP %s - assume out of " |
f0706e82 | 785 | "range\n", |
0795af57 | 786 | dev->name, print_mac(mac, ifsta->bssid)); |
f0706e82 | 787 | disassoc = 1; |
be8755e1 | 788 | sta_info_free(sta); |
d6f2da5b | 789 | } else |
f0706e82 JB |
790 | ieee80211_send_probe_req(dev, ifsta->bssid, |
791 | local->scan_ssid, | |
792 | local->scan_ssid_len); | |
d6f2da5b | 793 | ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL; |
f0706e82 | 794 | } else { |
d6f2da5b | 795 | ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL; |
f0706e82 JB |
796 | if (time_after(jiffies, ifsta->last_probe + |
797 | IEEE80211_PROBE_INTERVAL)) { | |
798 | ifsta->last_probe = jiffies; | |
799 | ieee80211_send_probe_req(dev, ifsta->bssid, | |
800 | ifsta->ssid, | |
801 | ifsta->ssid_len); | |
802 | } | |
803 | } | |
804 | sta_info_put(sta); | |
805 | } | |
806 | if (disassoc) { | |
807 | union iwreq_data wrqu; | |
808 | memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN); | |
809 | wrqu.ap_addr.sa_family = ARPHRD_ETHER; | |
810 | wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL); | |
811 | mod_timer(&ifsta->timer, jiffies + | |
812 | IEEE80211_MONITORING_INTERVAL + 30 * HZ); | |
813 | } else { | |
814 | mod_timer(&ifsta->timer, jiffies + | |
815 | IEEE80211_MONITORING_INTERVAL); | |
816 | } | |
817 | } | |
818 | ||
819 | ||
820 | static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst, | |
821 | u8 *ssid, size_t ssid_len) | |
822 | { | |
823 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
824 | struct ieee80211_hw_mode *mode; | |
825 | struct sk_buff *skb; | |
826 | struct ieee80211_mgmt *mgmt; | |
827 | u8 *pos, *supp_rates, *esupp_rates = NULL; | |
828 | int i; | |
829 | ||
830 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200); | |
831 | if (!skb) { | |
832 | printk(KERN_DEBUG "%s: failed to allocate buffer for probe " | |
833 | "request\n", dev->name); | |
834 | return; | |
835 | } | |
836 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
837 | ||
838 | mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24); | |
839 | memset(mgmt, 0, 24); | |
840 | mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT, | |
841 | IEEE80211_STYPE_PROBE_REQ); | |
842 | memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN); | |
843 | if (dst) { | |
844 | memcpy(mgmt->da, dst, ETH_ALEN); | |
845 | memcpy(mgmt->bssid, dst, ETH_ALEN); | |
846 | } else { | |
847 | memset(mgmt->da, 0xff, ETH_ALEN); | |
848 | memset(mgmt->bssid, 0xff, ETH_ALEN); | |
849 | } | |
850 | pos = skb_put(skb, 2 + ssid_len); | |
851 | *pos++ = WLAN_EID_SSID; | |
852 | *pos++ = ssid_len; | |
853 | memcpy(pos, ssid, ssid_len); | |
854 | ||
855 | supp_rates = skb_put(skb, 2); | |
856 | supp_rates[0] = WLAN_EID_SUPP_RATES; | |
857 | supp_rates[1] = 0; | |
858 | mode = local->oper_hw_mode; | |
859 | for (i = 0; i < mode->num_rates; i++) { | |
860 | struct ieee80211_rate *rate = &mode->rates[i]; | |
861 | if (!(rate->flags & IEEE80211_RATE_SUPPORTED)) | |
862 | continue; | |
863 | if (esupp_rates) { | |
864 | pos = skb_put(skb, 1); | |
865 | esupp_rates[1]++; | |
866 | } else if (supp_rates[1] == 8) { | |
867 | esupp_rates = skb_put(skb, 3); | |
868 | esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES; | |
869 | esupp_rates[1] = 1; | |
870 | pos = &esupp_rates[2]; | |
871 | } else { | |
872 | pos = skb_put(skb, 1); | |
873 | supp_rates[1]++; | |
874 | } | |
b708e610 | 875 | *pos = rate->rate / 5; |
f0706e82 JB |
876 | } |
877 | ||
878 | ieee80211_sta_tx(dev, skb, 0); | |
879 | } | |
880 | ||
881 | ||
882 | static int ieee80211_sta_wep_configured(struct net_device *dev) | |
883 | { | |
884 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
885 | if (!sdata || !sdata->default_key || | |
8f20fc24 | 886 | sdata->default_key->conf.alg != ALG_WEP) |
f0706e82 JB |
887 | return 0; |
888 | return 1; | |
889 | } | |
890 | ||
891 | ||
892 | static void ieee80211_auth_completed(struct net_device *dev, | |
893 | struct ieee80211_if_sta *ifsta) | |
894 | { | |
895 | printk(KERN_DEBUG "%s: authenticated\n", dev->name); | |
d6f2da5b | 896 | ifsta->flags |= IEEE80211_STA_AUTHENTICATED; |
f0706e82 JB |
897 | ieee80211_associate(dev, ifsta); |
898 | } | |
899 | ||
900 | ||
901 | static void ieee80211_auth_challenge(struct net_device *dev, | |
902 | struct ieee80211_if_sta *ifsta, | |
903 | struct ieee80211_mgmt *mgmt, | |
904 | size_t len) | |
905 | { | |
906 | u8 *pos; | |
907 | struct ieee802_11_elems elems; | |
908 | ||
909 | printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name); | |
910 | pos = mgmt->u.auth.variable; | |
67a4cce4 | 911 | ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems); |
f0706e82 JB |
912 | if (!elems.challenge) { |
913 | printk(KERN_DEBUG "%s: no challenge IE in shared key auth " | |
914 | "frame\n", dev->name); | |
915 | return; | |
916 | } | |
917 | ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2, | |
918 | elems.challenge_len + 2, 1); | |
919 | } | |
920 | ||
921 | ||
922 | static void ieee80211_rx_mgmt_auth(struct net_device *dev, | |
923 | struct ieee80211_if_sta *ifsta, | |
924 | struct ieee80211_mgmt *mgmt, | |
925 | size_t len) | |
926 | { | |
927 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
928 | u16 auth_alg, auth_transaction, status_code; | |
0795af57 | 929 | DECLARE_MAC_BUF(mac); |
f0706e82 JB |
930 | |
931 | if (ifsta->state != IEEE80211_AUTHENTICATE && | |
932 | sdata->type != IEEE80211_IF_TYPE_IBSS) { | |
933 | printk(KERN_DEBUG "%s: authentication frame received from " | |
0795af57 JP |
934 | "%s, but not in authenticate state - ignored\n", |
935 | dev->name, print_mac(mac, mgmt->sa)); | |
f0706e82 JB |
936 | return; |
937 | } | |
938 | ||
939 | if (len < 24 + 6) { | |
940 | printk(KERN_DEBUG "%s: too short (%zd) authentication frame " | |
0795af57 JP |
941 | "received from %s - ignored\n", |
942 | dev->name, len, print_mac(mac, mgmt->sa)); | |
f0706e82 JB |
943 | return; |
944 | } | |
945 | ||
946 | if (sdata->type != IEEE80211_IF_TYPE_IBSS && | |
947 | memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) { | |
948 | printk(KERN_DEBUG "%s: authentication frame received from " | |
0795af57 JP |
949 | "unknown AP (SA=%s BSSID=%s) - " |
950 | "ignored\n", dev->name, print_mac(mac, mgmt->sa), | |
951 | print_mac(mac, mgmt->bssid)); | |
f0706e82 JB |
952 | return; |
953 | } | |
954 | ||
955 | if (sdata->type != IEEE80211_IF_TYPE_IBSS && | |
956 | memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) { | |
957 | printk(KERN_DEBUG "%s: authentication frame received from " | |
0795af57 JP |
958 | "unknown BSSID (SA=%s BSSID=%s) - " |
959 | "ignored\n", dev->name, print_mac(mac, mgmt->sa), | |
960 | print_mac(mac, mgmt->bssid)); | |
f0706e82 JB |
961 | return; |
962 | } | |
963 | ||
964 | auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg); | |
965 | auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction); | |
966 | status_code = le16_to_cpu(mgmt->u.auth.status_code); | |
967 | ||
0795af57 | 968 | printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d " |
f0706e82 | 969 | "transaction=%d status=%d)\n", |
0795af57 | 970 | dev->name, print_mac(mac, mgmt->sa), auth_alg, |
f0706e82 JB |
971 | auth_transaction, status_code); |
972 | ||
973 | if (sdata->type == IEEE80211_IF_TYPE_IBSS) { | |
974 | /* IEEE 802.11 standard does not require authentication in IBSS | |
975 | * networks and most implementations do not seem to use it. | |
976 | * However, try to reply to authentication attempts if someone | |
977 | * has actually implemented this. | |
978 | * TODO: Could implement shared key authentication. */ | |
979 | if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) { | |
980 | printk(KERN_DEBUG "%s: unexpected IBSS authentication " | |
981 | "frame (alg=%d transaction=%d)\n", | |
982 | dev->name, auth_alg, auth_transaction); | |
983 | return; | |
984 | } | |
985 | ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0); | |
986 | } | |
987 | ||
988 | if (auth_alg != ifsta->auth_alg || | |
989 | auth_transaction != ifsta->auth_transaction) { | |
990 | printk(KERN_DEBUG "%s: unexpected authentication frame " | |
991 | "(alg=%d transaction=%d)\n", | |
992 | dev->name, auth_alg, auth_transaction); | |
993 | return; | |
994 | } | |
995 | ||
996 | if (status_code != WLAN_STATUS_SUCCESS) { | |
997 | printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d " | |
998 | "code=%d)\n", dev->name, ifsta->auth_alg, status_code); | |
999 | if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) { | |
1000 | u8 algs[3]; | |
1001 | const int num_algs = ARRAY_SIZE(algs); | |
1002 | int i, pos; | |
1003 | algs[0] = algs[1] = algs[2] = 0xff; | |
1004 | if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN) | |
1005 | algs[0] = WLAN_AUTH_OPEN; | |
1006 | if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY) | |
1007 | algs[1] = WLAN_AUTH_SHARED_KEY; | |
1008 | if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP) | |
1009 | algs[2] = WLAN_AUTH_LEAP; | |
1010 | if (ifsta->auth_alg == WLAN_AUTH_OPEN) | |
1011 | pos = 0; | |
1012 | else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY) | |
1013 | pos = 1; | |
1014 | else | |
1015 | pos = 2; | |
1016 | for (i = 0; i < num_algs; i++) { | |
1017 | pos++; | |
1018 | if (pos >= num_algs) | |
1019 | pos = 0; | |
1020 | if (algs[pos] == ifsta->auth_alg || | |
1021 | algs[pos] == 0xff) | |
1022 | continue; | |
1023 | if (algs[pos] == WLAN_AUTH_SHARED_KEY && | |
1024 | !ieee80211_sta_wep_configured(dev)) | |
1025 | continue; | |
1026 | ifsta->auth_alg = algs[pos]; | |
1027 | printk(KERN_DEBUG "%s: set auth_alg=%d for " | |
1028 | "next try\n", | |
1029 | dev->name, ifsta->auth_alg); | |
1030 | break; | |
1031 | } | |
1032 | } | |
1033 | return; | |
1034 | } | |
1035 | ||
1036 | switch (ifsta->auth_alg) { | |
1037 | case WLAN_AUTH_OPEN: | |
1038 | case WLAN_AUTH_LEAP: | |
1039 | ieee80211_auth_completed(dev, ifsta); | |
1040 | break; | |
1041 | case WLAN_AUTH_SHARED_KEY: | |
1042 | if (ifsta->auth_transaction == 4) | |
1043 | ieee80211_auth_completed(dev, ifsta); | |
1044 | else | |
1045 | ieee80211_auth_challenge(dev, ifsta, mgmt, len); | |
1046 | break; | |
1047 | } | |
1048 | } | |
1049 | ||
1050 | ||
1051 | static void ieee80211_rx_mgmt_deauth(struct net_device *dev, | |
1052 | struct ieee80211_if_sta *ifsta, | |
1053 | struct ieee80211_mgmt *mgmt, | |
1054 | size_t len) | |
1055 | { | |
1056 | u16 reason_code; | |
0795af57 | 1057 | DECLARE_MAC_BUF(mac); |
f0706e82 JB |
1058 | |
1059 | if (len < 24 + 2) { | |
1060 | printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame " | |
0795af57 JP |
1061 | "received from %s - ignored\n", |
1062 | dev->name, len, print_mac(mac, mgmt->sa)); | |
f0706e82 JB |
1063 | return; |
1064 | } | |
1065 | ||
1066 | if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) { | |
1067 | printk(KERN_DEBUG "%s: deauthentication frame received from " | |
0795af57 JP |
1068 | "unknown AP (SA=%s BSSID=%s) - " |
1069 | "ignored\n", dev->name, print_mac(mac, mgmt->sa), | |
1070 | print_mac(mac, mgmt->bssid)); | |
f0706e82 JB |
1071 | return; |
1072 | } | |
1073 | ||
1074 | reason_code = le16_to_cpu(mgmt->u.deauth.reason_code); | |
1075 | ||
0795af57 | 1076 | printk(KERN_DEBUG "%s: RX deauthentication from %s" |
f0706e82 | 1077 | " (reason=%d)\n", |
0795af57 | 1078 | dev->name, print_mac(mac, mgmt->sa), reason_code); |
f0706e82 | 1079 | |
d6f2da5b | 1080 | if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) { |
f0706e82 JB |
1081 | printk(KERN_DEBUG "%s: deauthenticated\n", dev->name); |
1082 | } | |
1083 | ||
1084 | if (ifsta->state == IEEE80211_AUTHENTICATE || | |
1085 | ifsta->state == IEEE80211_ASSOCIATE || | |
1086 | ifsta->state == IEEE80211_ASSOCIATED) { | |
1087 | ifsta->state = IEEE80211_AUTHENTICATE; | |
1088 | mod_timer(&ifsta->timer, jiffies + | |
1089 | IEEE80211_RETRY_AUTH_INTERVAL); | |
1090 | } | |
1091 | ||
1092 | ieee80211_set_disassoc(dev, ifsta, 1); | |
d6f2da5b | 1093 | ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED; |
f0706e82 JB |
1094 | } |
1095 | ||
1096 | ||
1097 | static void ieee80211_rx_mgmt_disassoc(struct net_device *dev, | |
1098 | struct ieee80211_if_sta *ifsta, | |
1099 | struct ieee80211_mgmt *mgmt, | |
1100 | size_t len) | |
1101 | { | |
1102 | u16 reason_code; | |
0795af57 | 1103 | DECLARE_MAC_BUF(mac); |
f0706e82 JB |
1104 | |
1105 | if (len < 24 + 2) { | |
1106 | printk(KERN_DEBUG "%s: too short (%zd) disassociation frame " | |
0795af57 JP |
1107 | "received from %s - ignored\n", |
1108 | dev->name, len, print_mac(mac, mgmt->sa)); | |
f0706e82 JB |
1109 | return; |
1110 | } | |
1111 | ||
1112 | if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) { | |
1113 | printk(KERN_DEBUG "%s: disassociation frame received from " | |
0795af57 JP |
1114 | "unknown AP (SA=%s BSSID=%s) - " |
1115 | "ignored\n", dev->name, print_mac(mac, mgmt->sa), | |
1116 | print_mac(mac, mgmt->bssid)); | |
f0706e82 JB |
1117 | return; |
1118 | } | |
1119 | ||
1120 | reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code); | |
1121 | ||
0795af57 | 1122 | printk(KERN_DEBUG "%s: RX disassociation from %s" |
f0706e82 | 1123 | " (reason=%d)\n", |
0795af57 | 1124 | dev->name, print_mac(mac, mgmt->sa), reason_code); |
f0706e82 | 1125 | |
d6f2da5b | 1126 | if (ifsta->flags & IEEE80211_STA_ASSOCIATED) |
f0706e82 JB |
1127 | printk(KERN_DEBUG "%s: disassociated\n", dev->name); |
1128 | ||
1129 | if (ifsta->state == IEEE80211_ASSOCIATED) { | |
1130 | ifsta->state = IEEE80211_ASSOCIATE; | |
1131 | mod_timer(&ifsta->timer, jiffies + | |
1132 | IEEE80211_RETRY_AUTH_INTERVAL); | |
1133 | } | |
1134 | ||
1135 | ieee80211_set_disassoc(dev, ifsta, 0); | |
1136 | } | |
1137 | ||
1138 | ||
1139 | static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev, | |
1140 | struct ieee80211_if_sta *ifsta, | |
1141 | struct ieee80211_mgmt *mgmt, | |
1142 | size_t len, | |
1143 | int reassoc) | |
1144 | { | |
1145 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1146 | struct ieee80211_hw_mode *mode; | |
1147 | struct sta_info *sta; | |
1148 | u32 rates; | |
1149 | u16 capab_info, status_code, aid; | |
1150 | struct ieee802_11_elems elems; | |
1151 | u8 *pos; | |
1152 | int i, j; | |
0795af57 | 1153 | DECLARE_MAC_BUF(mac); |
f0706e82 JB |
1154 | |
1155 | /* AssocResp and ReassocResp have identical structure, so process both | |
1156 | * of them in this function. */ | |
1157 | ||
1158 | if (ifsta->state != IEEE80211_ASSOCIATE) { | |
1159 | printk(KERN_DEBUG "%s: association frame received from " | |
0795af57 JP |
1160 | "%s, but not in associate state - ignored\n", |
1161 | dev->name, print_mac(mac, mgmt->sa)); | |
f0706e82 JB |
1162 | return; |
1163 | } | |
1164 | ||
1165 | if (len < 24 + 6) { | |
1166 | printk(KERN_DEBUG "%s: too short (%zd) association frame " | |
0795af57 JP |
1167 | "received from %s - ignored\n", |
1168 | dev->name, len, print_mac(mac, mgmt->sa)); | |
f0706e82 JB |
1169 | return; |
1170 | } | |
1171 | ||
1172 | if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) { | |
1173 | printk(KERN_DEBUG "%s: association frame received from " | |
0795af57 JP |
1174 | "unknown AP (SA=%s BSSID=%s) - " |
1175 | "ignored\n", dev->name, print_mac(mac, mgmt->sa), | |
1176 | print_mac(mac, mgmt->bssid)); | |
f0706e82 JB |
1177 | return; |
1178 | } | |
1179 | ||
1180 | capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info); | |
1181 | status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code); | |
1182 | aid = le16_to_cpu(mgmt->u.assoc_resp.aid); | |
f0706e82 | 1183 | |
0795af57 | 1184 | printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x " |
f0706e82 | 1185 | "status=%d aid=%d)\n", |
0795af57 | 1186 | dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa), |
ddd68587 | 1187 | capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14)))); |
f0706e82 JB |
1188 | |
1189 | if (status_code != WLAN_STATUS_SUCCESS) { | |
1190 | printk(KERN_DEBUG "%s: AP denied association (code=%d)\n", | |
1191 | dev->name, status_code); | |
8a69aa93 DD |
1192 | /* if this was a reassociation, ensure we try a "full" |
1193 | * association next time. This works around some broken APs | |
1194 | * which do not correctly reject reassociation requests. */ | |
d6f2da5b | 1195 | ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET; |
f0706e82 JB |
1196 | return; |
1197 | } | |
1198 | ||
1dd84aa2 JB |
1199 | if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14))) |
1200 | printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not " | |
1201 | "set\n", dev->name, aid); | |
1202 | aid &= ~(BIT(15) | BIT(14)); | |
1203 | ||
f0706e82 | 1204 | pos = mgmt->u.assoc_resp.variable; |
67a4cce4 | 1205 | ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems); |
f0706e82 JB |
1206 | |
1207 | if (!elems.supp_rates) { | |
1208 | printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n", | |
1209 | dev->name); | |
1210 | return; | |
1211 | } | |
1212 | ||
5628221c DD |
1213 | /* it probably doesn't, but if the frame includes an ERP value then |
1214 | * update our stored copy */ | |
1215 | if (elems.erp_info && elems.erp_info_len >= 1) { | |
1216 | struct ieee80211_sta_bss *bss | |
65c107ab | 1217 | = ieee80211_rx_bss_get(dev, ifsta->bssid, |
cffdd30d JL |
1218 | local->hw.conf.channel, |
1219 | ifsta->ssid, ifsta->ssid_len); | |
5628221c DD |
1220 | if (bss) { |
1221 | bss->erp_value = elems.erp_info[0]; | |
1222 | bss->has_erp_value = 1; | |
1223 | ieee80211_rx_bss_put(dev, bss); | |
1224 | } | |
1225 | } | |
1226 | ||
f0706e82 JB |
1227 | printk(KERN_DEBUG "%s: associated\n", dev->name); |
1228 | ifsta->aid = aid; | |
1229 | ifsta->ap_capab = capab_info; | |
1230 | ||
1231 | kfree(ifsta->assocresp_ies); | |
1232 | ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt); | |
0ec0b7ac | 1233 | ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL); |
f0706e82 JB |
1234 | if (ifsta->assocresp_ies) |
1235 | memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len); | |
1236 | ||
1237 | ieee80211_set_associated(dev, ifsta, 1); | |
1238 | ||
1239 | /* Add STA entry for the AP */ | |
1240 | sta = sta_info_get(local, ifsta->bssid); | |
1241 | if (!sta) { | |
1242 | struct ieee80211_sta_bss *bss; | |
0ec0b7ac | 1243 | sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL); |
f0706e82 JB |
1244 | if (!sta) { |
1245 | printk(KERN_DEBUG "%s: failed to add STA entry for the" | |
1246 | " AP\n", dev->name); | |
1247 | return; | |
1248 | } | |
65c107ab | 1249 | bss = ieee80211_rx_bss_get(dev, ifsta->bssid, |
cffdd30d JL |
1250 | local->hw.conf.channel, |
1251 | ifsta->ssid, ifsta->ssid_len); | |
f0706e82 JB |
1252 | if (bss) { |
1253 | sta->last_rssi = bss->rssi; | |
1254 | sta->last_signal = bss->signal; | |
1255 | sta->last_noise = bss->noise; | |
1256 | ieee80211_rx_bss_put(dev, bss); | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | sta->dev = dev; | |
c9ee23df | 1261 | sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP; |
f0706e82 JB |
1262 | |
1263 | rates = 0; | |
1264 | mode = local->oper_hw_mode; | |
1265 | for (i = 0; i < elems.supp_rates_len; i++) { | |
1266 | int rate = (elems.supp_rates[i] & 0x7f) * 5; | |
f0706e82 JB |
1267 | for (j = 0; j < mode->num_rates; j++) |
1268 | if (mode->rates[j].rate == rate) | |
1269 | rates |= BIT(j); | |
1270 | } | |
1271 | for (i = 0; i < elems.ext_supp_rates_len; i++) { | |
1272 | int rate = (elems.ext_supp_rates[i] & 0x7f) * 5; | |
f0706e82 JB |
1273 | for (j = 0; j < mode->num_rates; j++) |
1274 | if (mode->rates[j].rate == rate) | |
1275 | rates |= BIT(j); | |
1276 | } | |
1277 | sta->supp_rates = rates; | |
1278 | ||
1279 | rate_control_rate_init(sta, local); | |
1280 | ||
d6f2da5b | 1281 | if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) { |
f0706e82 JB |
1282 | sta->flags |= WLAN_STA_WME; |
1283 | ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param, | |
1284 | elems.wmm_param_len); | |
1285 | } | |
1286 | ||
1287 | ||
1288 | sta_info_put(sta); | |
1289 | ||
1290 | ieee80211_associated(dev, ifsta); | |
1291 | } | |
1292 | ||
1293 | ||
1294 | /* Caller must hold local->sta_bss_lock */ | |
1295 | static void __ieee80211_rx_bss_hash_add(struct net_device *dev, | |
1296 | struct ieee80211_sta_bss *bss) | |
1297 | { | |
1298 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1299 | bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)]; | |
1300 | local->sta_bss_hash[STA_HASH(bss->bssid)] = bss; | |
1301 | } | |
1302 | ||
1303 | ||
1304 | /* Caller must hold local->sta_bss_lock */ | |
1305 | static void __ieee80211_rx_bss_hash_del(struct net_device *dev, | |
1306 | struct ieee80211_sta_bss *bss) | |
1307 | { | |
1308 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1309 | struct ieee80211_sta_bss *b, *prev = NULL; | |
1310 | b = local->sta_bss_hash[STA_HASH(bss->bssid)]; | |
1311 | while (b) { | |
1312 | if (b == bss) { | |
1313 | if (!prev) | |
1314 | local->sta_bss_hash[STA_HASH(bss->bssid)] = | |
1315 | bss->hnext; | |
1316 | else | |
1317 | prev->hnext = bss->hnext; | |
1318 | break; | |
1319 | } | |
1320 | prev = b; | |
1321 | b = b->hnext; | |
1322 | } | |
1323 | } | |
1324 | ||
1325 | ||
1326 | static struct ieee80211_sta_bss * | |
cffdd30d JL |
1327 | ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int channel, |
1328 | u8 *ssid, u8 ssid_len) | |
f0706e82 JB |
1329 | { |
1330 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1331 | struct ieee80211_sta_bss *bss; | |
1332 | ||
dd00cc48 | 1333 | bss = kzalloc(sizeof(*bss), GFP_ATOMIC); |
f0706e82 JB |
1334 | if (!bss) |
1335 | return NULL; | |
f0706e82 JB |
1336 | atomic_inc(&bss->users); |
1337 | atomic_inc(&bss->users); | |
1338 | memcpy(bss->bssid, bssid, ETH_ALEN); | |
65c107ab | 1339 | bss->channel = channel; |
cffdd30d JL |
1340 | if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) { |
1341 | memcpy(bss->ssid, ssid, ssid_len); | |
1342 | bss->ssid_len = ssid_len; | |
1343 | } | |
f0706e82 JB |
1344 | |
1345 | spin_lock_bh(&local->sta_bss_lock); | |
1346 | /* TODO: order by RSSI? */ | |
1347 | list_add_tail(&bss->list, &local->sta_bss_list); | |
1348 | __ieee80211_rx_bss_hash_add(dev, bss); | |
1349 | spin_unlock_bh(&local->sta_bss_lock); | |
1350 | return bss; | |
1351 | } | |
1352 | ||
1353 | ||
1354 | static struct ieee80211_sta_bss * | |
cffdd30d JL |
1355 | ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int channel, |
1356 | u8 *ssid, u8 ssid_len) | |
f0706e82 JB |
1357 | { |
1358 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1359 | struct ieee80211_sta_bss *bss; | |
1360 | ||
1361 | spin_lock_bh(&local->sta_bss_lock); | |
1362 | bss = local->sta_bss_hash[STA_HASH(bssid)]; | |
1363 | while (bss) { | |
cffdd30d JL |
1364 | if (!memcmp(bss->bssid, bssid, ETH_ALEN) && |
1365 | bss->channel == channel && | |
1366 | bss->ssid_len == ssid_len && | |
1367 | (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) { | |
f0706e82 JB |
1368 | atomic_inc(&bss->users); |
1369 | break; | |
1370 | } | |
1371 | bss = bss->hnext; | |
1372 | } | |
1373 | spin_unlock_bh(&local->sta_bss_lock); | |
1374 | return bss; | |
1375 | } | |
1376 | ||
1377 | ||
1378 | static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss) | |
1379 | { | |
1380 | kfree(bss->wpa_ie); | |
1381 | kfree(bss->rsn_ie); | |
1382 | kfree(bss->wmm_ie); | |
1383 | kfree(bss); | |
1384 | } | |
1385 | ||
1386 | ||
1387 | static void ieee80211_rx_bss_put(struct net_device *dev, | |
1388 | struct ieee80211_sta_bss *bss) | |
1389 | { | |
1390 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1391 | if (!atomic_dec_and_test(&bss->users)) | |
1392 | return; | |
1393 | ||
1394 | spin_lock_bh(&local->sta_bss_lock); | |
1395 | __ieee80211_rx_bss_hash_del(dev, bss); | |
1396 | list_del(&bss->list); | |
1397 | spin_unlock_bh(&local->sta_bss_lock); | |
1398 | ieee80211_rx_bss_free(bss); | |
1399 | } | |
1400 | ||
1401 | ||
1402 | void ieee80211_rx_bss_list_init(struct net_device *dev) | |
1403 | { | |
1404 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1405 | spin_lock_init(&local->sta_bss_lock); | |
1406 | INIT_LIST_HEAD(&local->sta_bss_list); | |
1407 | } | |
1408 | ||
1409 | ||
1410 | void ieee80211_rx_bss_list_deinit(struct net_device *dev) | |
1411 | { | |
1412 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1413 | struct ieee80211_sta_bss *bss, *tmp; | |
1414 | ||
1415 | list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list) | |
1416 | ieee80211_rx_bss_put(dev, bss); | |
1417 | } | |
1418 | ||
1419 | ||
1420 | static void ieee80211_rx_bss_info(struct net_device *dev, | |
1421 | struct ieee80211_mgmt *mgmt, | |
1422 | size_t len, | |
1423 | struct ieee80211_rx_status *rx_status, | |
1424 | int beacon) | |
1425 | { | |
1426 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1427 | struct ieee802_11_elems elems; | |
1428 | size_t baselen; | |
67a4cce4 | 1429 | int channel, clen; |
f0706e82 JB |
1430 | struct ieee80211_sta_bss *bss; |
1431 | struct sta_info *sta; | |
1432 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
1433 | u64 timestamp; | |
0795af57 JP |
1434 | DECLARE_MAC_BUF(mac); |
1435 | DECLARE_MAC_BUF(mac2); | |
f0706e82 JB |
1436 | |
1437 | if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN)) | |
1438 | return; /* ignore ProbeResp to foreign address */ | |
1439 | ||
1440 | #if 0 | |
0795af57 | 1441 | printk(KERN_DEBUG "%s: RX %s from %s to %s\n", |
f0706e82 | 1442 | dev->name, beacon ? "Beacon" : "Probe Response", |
0795af57 | 1443 | print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da)); |
f0706e82 JB |
1444 | #endif |
1445 | ||
1446 | baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt; | |
1447 | if (baselen > len) | |
1448 | return; | |
1449 | ||
1450 | timestamp = le64_to_cpu(mgmt->u.beacon.timestamp); | |
1451 | ||
1452 | if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon && | |
1453 | memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) { | |
1454 | #ifdef CONFIG_MAC80211_IBSS_DEBUG | |
1455 | static unsigned long last_tsf_debug = 0; | |
1456 | u64 tsf; | |
1457 | if (local->ops->get_tsf) | |
1458 | tsf = local->ops->get_tsf(local_to_hw(local)); | |
1459 | else | |
1460 | tsf = -1LLU; | |
1461 | if (time_after(jiffies, last_tsf_debug + 5 * HZ)) { | |
0795af57 JP |
1462 | printk(KERN_DEBUG "RX beacon SA=%s BSSID=" |
1463 | "%s TSF=0x%llx BCN=0x%llx diff=%lld " | |
f0706e82 | 1464 | "@%lu\n", |
0795af57 | 1465 | print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->bssid), |
f0706e82 JB |
1466 | (unsigned long long)tsf, |
1467 | (unsigned long long)timestamp, | |
1468 | (unsigned long long)(tsf - timestamp), | |
1469 | jiffies); | |
1470 | last_tsf_debug = jiffies; | |
1471 | } | |
1472 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ | |
1473 | } | |
1474 | ||
67a4cce4 | 1475 | ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems); |
f0706e82 JB |
1476 | |
1477 | if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates && | |
1478 | memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 && | |
1479 | (sta = sta_info_get(local, mgmt->sa))) { | |
1480 | struct ieee80211_hw_mode *mode; | |
1481 | struct ieee80211_rate *rates; | |
1482 | size_t num_rates; | |
1483 | u32 supp_rates, prev_rates; | |
1484 | int i, j; | |
1485 | ||
1486 | mode = local->sta_scanning ? | |
1487 | local->scan_hw_mode : local->oper_hw_mode; | |
1488 | rates = mode->rates; | |
1489 | num_rates = mode->num_rates; | |
1490 | ||
1491 | supp_rates = 0; | |
1492 | for (i = 0; i < elems.supp_rates_len + | |
1493 | elems.ext_supp_rates_len; i++) { | |
1494 | u8 rate = 0; | |
1495 | int own_rate; | |
1496 | if (i < elems.supp_rates_len) | |
1497 | rate = elems.supp_rates[i]; | |
1498 | else if (elems.ext_supp_rates) | |
1499 | rate = elems.ext_supp_rates | |
1500 | [i - elems.supp_rates_len]; | |
1501 | own_rate = 5 * (rate & 0x7f); | |
f0706e82 JB |
1502 | for (j = 0; j < num_rates; j++) |
1503 | if (rates[j].rate == own_rate) | |
1504 | supp_rates |= BIT(j); | |
1505 | } | |
1506 | ||
1507 | prev_rates = sta->supp_rates; | |
1508 | sta->supp_rates &= supp_rates; | |
1509 | if (sta->supp_rates == 0) { | |
1510 | /* No matching rates - this should not really happen. | |
1511 | * Make sure that at least one rate is marked | |
1512 | * supported to avoid issues with TX rate ctrl. */ | |
1513 | sta->supp_rates = sdata->u.sta.supp_rates_bits; | |
1514 | } | |
1515 | if (sta->supp_rates != prev_rates) { | |
1516 | printk(KERN_DEBUG "%s: updated supp_rates set for " | |
0795af57 | 1517 | "%s based on beacon info (0x%x & 0x%x -> " |
f0706e82 | 1518 | "0x%x)\n", |
0795af57 | 1519 | dev->name, print_mac(mac, sta->addr), prev_rates, |
f0706e82 JB |
1520 | supp_rates, sta->supp_rates); |
1521 | } | |
1522 | sta_info_put(sta); | |
1523 | } | |
1524 | ||
1525 | if (!elems.ssid) | |
1526 | return; | |
1527 | ||
1528 | if (elems.ds_params && elems.ds_params_len == 1) | |
1529 | channel = elems.ds_params[0]; | |
1530 | else | |
1531 | channel = rx_status->channel; | |
1532 | ||
cffdd30d JL |
1533 | bss = ieee80211_rx_bss_get(dev, mgmt->bssid, channel, |
1534 | elems.ssid, elems.ssid_len); | |
f0706e82 | 1535 | if (!bss) { |
cffdd30d JL |
1536 | bss = ieee80211_rx_bss_add(dev, mgmt->bssid, channel, |
1537 | elems.ssid, elems.ssid_len); | |
f0706e82 JB |
1538 | if (!bss) |
1539 | return; | |
1540 | } else { | |
1541 | #if 0 | |
1542 | /* TODO: order by RSSI? */ | |
1543 | spin_lock_bh(&local->sta_bss_lock); | |
1544 | list_move_tail(&bss->list, &local->sta_bss_list); | |
1545 | spin_unlock_bh(&local->sta_bss_lock); | |
1546 | #endif | |
1547 | } | |
1548 | ||
1549 | if (bss->probe_resp && beacon) { | |
1550 | /* Do not allow beacon to override data from Probe Response. */ | |
1551 | ieee80211_rx_bss_put(dev, bss); | |
1552 | return; | |
1553 | } | |
1554 | ||
5628221c DD |
1555 | /* save the ERP value so that it is available at association time */ |
1556 | if (elems.erp_info && elems.erp_info_len >= 1) { | |
1557 | bss->erp_value = elems.erp_info[0]; | |
1558 | bss->has_erp_value = 1; | |
1559 | } | |
1560 | ||
f0706e82 JB |
1561 | bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int); |
1562 | bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info); | |
f0706e82 JB |
1563 | |
1564 | bss->supp_rates_len = 0; | |
1565 | if (elems.supp_rates) { | |
1566 | clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len; | |
1567 | if (clen > elems.supp_rates_len) | |
1568 | clen = elems.supp_rates_len; | |
1569 | memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates, | |
1570 | clen); | |
1571 | bss->supp_rates_len += clen; | |
1572 | } | |
1573 | if (elems.ext_supp_rates) { | |
1574 | clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len; | |
1575 | if (clen > elems.ext_supp_rates_len) | |
1576 | clen = elems.ext_supp_rates_len; | |
1577 | memcpy(&bss->supp_rates[bss->supp_rates_len], | |
1578 | elems.ext_supp_rates, clen); | |
1579 | bss->supp_rates_len += clen; | |
1580 | } | |
1581 | ||
1582 | if (elems.wpa && | |
1583 | (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len || | |
1584 | memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) { | |
1585 | kfree(bss->wpa_ie); | |
1586 | bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC); | |
1587 | if (bss->wpa_ie) { | |
1588 | memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2); | |
1589 | bss->wpa_ie_len = elems.wpa_len + 2; | |
1590 | } else | |
1591 | bss->wpa_ie_len = 0; | |
1592 | } else if (!elems.wpa && bss->wpa_ie) { | |
1593 | kfree(bss->wpa_ie); | |
1594 | bss->wpa_ie = NULL; | |
1595 | bss->wpa_ie_len = 0; | |
1596 | } | |
1597 | ||
1598 | if (elems.rsn && | |
1599 | (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len || | |
1600 | memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) { | |
1601 | kfree(bss->rsn_ie); | |
1602 | bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC); | |
1603 | if (bss->rsn_ie) { | |
1604 | memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2); | |
1605 | bss->rsn_ie_len = elems.rsn_len + 2; | |
1606 | } else | |
1607 | bss->rsn_ie_len = 0; | |
1608 | } else if (!elems.rsn && bss->rsn_ie) { | |
1609 | kfree(bss->rsn_ie); | |
1610 | bss->rsn_ie = NULL; | |
1611 | bss->rsn_ie_len = 0; | |
1612 | } | |
1613 | ||
1614 | if (elems.wmm_param && | |
1615 | (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len || | |
1616 | memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) { | |
1617 | kfree(bss->wmm_ie); | |
1618 | bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC); | |
1619 | if (bss->wmm_ie) { | |
1620 | memcpy(bss->wmm_ie, elems.wmm_param - 2, | |
1621 | elems.wmm_param_len + 2); | |
1622 | bss->wmm_ie_len = elems.wmm_param_len + 2; | |
1623 | } else | |
1624 | bss->wmm_ie_len = 0; | |
1625 | } else if (!elems.wmm_param && bss->wmm_ie) { | |
1626 | kfree(bss->wmm_ie); | |
1627 | bss->wmm_ie = NULL; | |
1628 | bss->wmm_ie_len = 0; | |
1629 | } | |
1630 | ||
1631 | ||
1632 | bss->hw_mode = rx_status->phymode; | |
f0706e82 JB |
1633 | bss->freq = rx_status->freq; |
1634 | if (channel != rx_status->channel && | |
1635 | (bss->hw_mode == MODE_IEEE80211G || | |
1636 | bss->hw_mode == MODE_IEEE80211B) && | |
1637 | channel >= 1 && channel <= 14) { | |
1638 | static const int freq_list[] = { | |
1639 | 2412, 2417, 2422, 2427, 2432, 2437, 2442, | |
1640 | 2447, 2452, 2457, 2462, 2467, 2472, 2484 | |
1641 | }; | |
1642 | /* IEEE 802.11g/b mode can receive packets from neighboring | |
1643 | * channels, so map the channel into frequency. */ | |
1644 | bss->freq = freq_list[channel - 1]; | |
1645 | } | |
1646 | bss->timestamp = timestamp; | |
1647 | bss->last_update = jiffies; | |
1648 | bss->rssi = rx_status->ssi; | |
1649 | bss->signal = rx_status->signal; | |
1650 | bss->noise = rx_status->noise; | |
1651 | if (!beacon) | |
1652 | bss->probe_resp++; | |
1653 | ieee80211_rx_bss_put(dev, bss); | |
1654 | } | |
1655 | ||
1656 | ||
1657 | static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev, | |
1658 | struct ieee80211_mgmt *mgmt, | |
1659 | size_t len, | |
1660 | struct ieee80211_rx_status *rx_status) | |
1661 | { | |
1662 | ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0); | |
1663 | } | |
1664 | ||
1665 | ||
1666 | static void ieee80211_rx_mgmt_beacon(struct net_device *dev, | |
1667 | struct ieee80211_mgmt *mgmt, | |
1668 | size_t len, | |
1669 | struct ieee80211_rx_status *rx_status) | |
1670 | { | |
f0706e82 JB |
1671 | struct ieee80211_sub_if_data *sdata; |
1672 | struct ieee80211_if_sta *ifsta; | |
f0706e82 JB |
1673 | size_t baselen; |
1674 | struct ieee802_11_elems elems; | |
1675 | ||
1676 | ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1); | |
1677 | ||
1678 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
1679 | if (sdata->type != IEEE80211_IF_TYPE_STA) | |
1680 | return; | |
1681 | ifsta = &sdata->u.sta; | |
1682 | ||
d6f2da5b | 1683 | if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) || |
f0706e82 JB |
1684 | memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) |
1685 | return; | |
1686 | ||
1687 | /* Process beacon from the current BSS */ | |
1688 | baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt; | |
1689 | if (baselen > len) | |
1690 | return; | |
1691 | ||
67a4cce4 | 1692 | ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems); |
f0706e82 | 1693 | |
5628221c DD |
1694 | if (elems.erp_info && elems.erp_info_len >= 1) |
1695 | ieee80211_handle_erp_ie(dev, elems.erp_info[0]); | |
f0706e82 | 1696 | |
d6f2da5b | 1697 | if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) { |
f0706e82 JB |
1698 | ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param, |
1699 | elems.wmm_param_len); | |
1700 | } | |
1701 | } | |
1702 | ||
1703 | ||
1704 | static void ieee80211_rx_mgmt_probe_req(struct net_device *dev, | |
1705 | struct ieee80211_if_sta *ifsta, | |
1706 | struct ieee80211_mgmt *mgmt, | |
1707 | size_t len, | |
1708 | struct ieee80211_rx_status *rx_status) | |
1709 | { | |
1710 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1711 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
1712 | int tx_last_beacon; | |
1713 | struct sk_buff *skb; | |
1714 | struct ieee80211_mgmt *resp; | |
1715 | u8 *pos, *end; | |
0795af57 JP |
1716 | DECLARE_MAC_BUF(mac); |
1717 | #ifdef CONFIG_MAC80211_IBSS_DEBUG | |
1718 | DECLARE_MAC_BUF(mac2); | |
1719 | DECLARE_MAC_BUF(mac3); | |
1720 | #endif | |
f0706e82 JB |
1721 | |
1722 | if (sdata->type != IEEE80211_IF_TYPE_IBSS || | |
1723 | ifsta->state != IEEE80211_IBSS_JOINED || | |
1724 | len < 24 + 2 || !ifsta->probe_resp) | |
1725 | return; | |
1726 | ||
1727 | if (local->ops->tx_last_beacon) | |
1728 | tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local)); | |
1729 | else | |
1730 | tx_last_beacon = 1; | |
1731 | ||
1732 | #ifdef CONFIG_MAC80211_IBSS_DEBUG | |
0795af57 JP |
1733 | printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID=" |
1734 | "%s (tx_last_beacon=%d)\n", | |
1735 | dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da), | |
1736 | print_mac(mac3, mgmt->bssid), tx_last_beacon); | |
f0706e82 JB |
1737 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ |
1738 | ||
1739 | if (!tx_last_beacon) | |
1740 | return; | |
1741 | ||
1742 | if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 && | |
1743 | memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0) | |
1744 | return; | |
1745 | ||
1746 | end = ((u8 *) mgmt) + len; | |
1747 | pos = mgmt->u.probe_req.variable; | |
1748 | if (pos[0] != WLAN_EID_SSID || | |
1749 | pos + 2 + pos[1] > end) { | |
1750 | if (net_ratelimit()) { | |
1751 | printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq " | |
0795af57 JP |
1752 | "from %s\n", |
1753 | dev->name, print_mac(mac, mgmt->sa)); | |
f0706e82 JB |
1754 | } |
1755 | return; | |
1756 | } | |
1757 | if (pos[1] != 0 && | |
1758 | (pos[1] != ifsta->ssid_len || | |
1759 | memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) { | |
1760 | /* Ignore ProbeReq for foreign SSID */ | |
1761 | return; | |
1762 | } | |
1763 | ||
1764 | /* Reply with ProbeResp */ | |
0ec0b7ac | 1765 | skb = skb_copy(ifsta->probe_resp, GFP_KERNEL); |
f0706e82 JB |
1766 | if (!skb) |
1767 | return; | |
1768 | ||
1769 | resp = (struct ieee80211_mgmt *) skb->data; | |
1770 | memcpy(resp->da, mgmt->sa, ETH_ALEN); | |
1771 | #ifdef CONFIG_MAC80211_IBSS_DEBUG | |
0795af57 JP |
1772 | printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n", |
1773 | dev->name, print_mac(mac, resp->da)); | |
f0706e82 JB |
1774 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ |
1775 | ieee80211_sta_tx(dev, skb, 0); | |
1776 | } | |
1777 | ||
1778 | ||
1779 | void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb, | |
1780 | struct ieee80211_rx_status *rx_status) | |
1781 | { | |
1782 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1783 | struct ieee80211_sub_if_data *sdata; | |
1784 | struct ieee80211_if_sta *ifsta; | |
1785 | struct ieee80211_mgmt *mgmt; | |
1786 | u16 fc; | |
1787 | ||
1788 | if (skb->len < 24) | |
1789 | goto fail; | |
1790 | ||
1791 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
1792 | ifsta = &sdata->u.sta; | |
1793 | ||
1794 | mgmt = (struct ieee80211_mgmt *) skb->data; | |
1795 | fc = le16_to_cpu(mgmt->frame_control); | |
1796 | ||
1797 | switch (fc & IEEE80211_FCTL_STYPE) { | |
1798 | case IEEE80211_STYPE_PROBE_REQ: | |
1799 | case IEEE80211_STYPE_PROBE_RESP: | |
1800 | case IEEE80211_STYPE_BEACON: | |
1801 | memcpy(skb->cb, rx_status, sizeof(*rx_status)); | |
1802 | case IEEE80211_STYPE_AUTH: | |
1803 | case IEEE80211_STYPE_ASSOC_RESP: | |
1804 | case IEEE80211_STYPE_REASSOC_RESP: | |
1805 | case IEEE80211_STYPE_DEAUTH: | |
1806 | case IEEE80211_STYPE_DISASSOC: | |
1807 | skb_queue_tail(&ifsta->skb_queue, skb); | |
1808 | queue_work(local->hw.workqueue, &ifsta->work); | |
1809 | return; | |
1810 | default: | |
1811 | printk(KERN_DEBUG "%s: received unknown management frame - " | |
1812 | "stype=%d\n", dev->name, | |
1813 | (fc & IEEE80211_FCTL_STYPE) >> 4); | |
1814 | break; | |
1815 | } | |
1816 | ||
1817 | fail: | |
1818 | kfree_skb(skb); | |
1819 | } | |
1820 | ||
1821 | ||
1822 | static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev, | |
1823 | struct sk_buff *skb) | |
1824 | { | |
1825 | struct ieee80211_rx_status *rx_status; | |
1826 | struct ieee80211_sub_if_data *sdata; | |
1827 | struct ieee80211_if_sta *ifsta; | |
1828 | struct ieee80211_mgmt *mgmt; | |
1829 | u16 fc; | |
1830 | ||
1831 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
1832 | ifsta = &sdata->u.sta; | |
1833 | ||
1834 | rx_status = (struct ieee80211_rx_status *) skb->cb; | |
1835 | mgmt = (struct ieee80211_mgmt *) skb->data; | |
1836 | fc = le16_to_cpu(mgmt->frame_control); | |
1837 | ||
1838 | switch (fc & IEEE80211_FCTL_STYPE) { | |
1839 | case IEEE80211_STYPE_PROBE_REQ: | |
1840 | ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len, | |
1841 | rx_status); | |
1842 | break; | |
1843 | case IEEE80211_STYPE_PROBE_RESP: | |
1844 | ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status); | |
1845 | break; | |
1846 | case IEEE80211_STYPE_BEACON: | |
1847 | ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status); | |
1848 | break; | |
1849 | case IEEE80211_STYPE_AUTH: | |
1850 | ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len); | |
1851 | break; | |
1852 | case IEEE80211_STYPE_ASSOC_RESP: | |
1853 | ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0); | |
1854 | break; | |
1855 | case IEEE80211_STYPE_REASSOC_RESP: | |
1856 | ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1); | |
1857 | break; | |
1858 | case IEEE80211_STYPE_DEAUTH: | |
1859 | ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len); | |
1860 | break; | |
1861 | case IEEE80211_STYPE_DISASSOC: | |
1862 | ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len); | |
1863 | break; | |
1864 | } | |
1865 | ||
1866 | kfree_skb(skb); | |
1867 | } | |
1868 | ||
1869 | ||
1870 | void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb, | |
1871 | struct ieee80211_rx_status *rx_status) | |
1872 | { | |
1873 | struct ieee80211_mgmt *mgmt; | |
1874 | u16 fc; | |
1875 | ||
1876 | if (skb->len < 24) { | |
1877 | dev_kfree_skb(skb); | |
1878 | return; | |
1879 | } | |
1880 | ||
1881 | mgmt = (struct ieee80211_mgmt *) skb->data; | |
1882 | fc = le16_to_cpu(mgmt->frame_control); | |
1883 | ||
1884 | if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) { | |
1885 | if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) { | |
1886 | ieee80211_rx_mgmt_probe_resp(dev, mgmt, | |
1887 | skb->len, rx_status); | |
1888 | } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) { | |
1889 | ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, | |
1890 | rx_status); | |
1891 | } | |
1892 | } | |
1893 | ||
1894 | dev_kfree_skb(skb); | |
1895 | } | |
1896 | ||
1897 | ||
1898 | static int ieee80211_sta_active_ibss(struct net_device *dev) | |
1899 | { | |
1900 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1901 | int active = 0; | |
1902 | struct sta_info *sta; | |
1903 | ||
be8755e1 | 1904 | read_lock_bh(&local->sta_lock); |
f0706e82 JB |
1905 | list_for_each_entry(sta, &local->sta_list, list) { |
1906 | if (sta->dev == dev && | |
1907 | time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL, | |
1908 | jiffies)) { | |
1909 | active++; | |
1910 | break; | |
1911 | } | |
1912 | } | |
be8755e1 | 1913 | read_unlock_bh(&local->sta_lock); |
f0706e82 JB |
1914 | |
1915 | return active; | |
1916 | } | |
1917 | ||
1918 | ||
1919 | static void ieee80211_sta_expire(struct net_device *dev) | |
1920 | { | |
1921 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1922 | struct sta_info *sta, *tmp; | |
be8755e1 | 1923 | LIST_HEAD(tmp_list); |
0795af57 | 1924 | DECLARE_MAC_BUF(mac); |
f0706e82 | 1925 | |
be8755e1 | 1926 | write_lock_bh(&local->sta_lock); |
f0706e82 JB |
1927 | list_for_each_entry_safe(sta, tmp, &local->sta_list, list) |
1928 | if (time_after(jiffies, sta->last_rx + | |
1929 | IEEE80211_IBSS_INACTIVITY_LIMIT)) { | |
0795af57 JP |
1930 | printk(KERN_DEBUG "%s: expiring inactive STA %s\n", |
1931 | dev->name, print_mac(mac, sta->addr)); | |
be8755e1 MW |
1932 | __sta_info_get(sta); |
1933 | sta_info_remove(sta); | |
1934 | list_add(&sta->list, &tmp_list); | |
f0706e82 | 1935 | } |
be8755e1 MW |
1936 | write_unlock_bh(&local->sta_lock); |
1937 | ||
1938 | list_for_each_entry_safe(sta, tmp, &tmp_list, list) { | |
1939 | sta_info_free(sta); | |
1940 | sta_info_put(sta); | |
1941 | } | |
f0706e82 JB |
1942 | } |
1943 | ||
1944 | ||
1945 | static void ieee80211_sta_merge_ibss(struct net_device *dev, | |
1946 | struct ieee80211_if_sta *ifsta) | |
1947 | { | |
1948 | mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL); | |
1949 | ||
1950 | ieee80211_sta_expire(dev); | |
1951 | if (ieee80211_sta_active_ibss(dev)) | |
1952 | return; | |
1953 | ||
1954 | printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other " | |
1955 | "IBSS networks with same SSID (merge)\n", dev->name); | |
1956 | ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len); | |
1957 | } | |
1958 | ||
1959 | ||
1960 | void ieee80211_sta_timer(unsigned long data) | |
1961 | { | |
1962 | struct ieee80211_sub_if_data *sdata = | |
1963 | (struct ieee80211_sub_if_data *) data; | |
1964 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
1965 | struct ieee80211_local *local = wdev_priv(&sdata->wdev); | |
1966 | ||
1967 | set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request); | |
1968 | queue_work(local->hw.workqueue, &ifsta->work); | |
1969 | } | |
1970 | ||
1971 | ||
1972 | void ieee80211_sta_work(struct work_struct *work) | |
1973 | { | |
1974 | struct ieee80211_sub_if_data *sdata = | |
1975 | container_of(work, struct ieee80211_sub_if_data, u.sta.work); | |
1976 | struct net_device *dev = sdata->dev; | |
1977 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
1978 | struct ieee80211_if_sta *ifsta; | |
1979 | struct sk_buff *skb; | |
1980 | ||
1981 | if (!netif_running(dev)) | |
1982 | return; | |
1983 | ||
1984 | if (local->sta_scanning) | |
1985 | return; | |
1986 | ||
1987 | if (sdata->type != IEEE80211_IF_TYPE_STA && | |
1988 | sdata->type != IEEE80211_IF_TYPE_IBSS) { | |
1989 | printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface " | |
1990 | "(type=%d)\n", dev->name, sdata->type); | |
1991 | return; | |
1992 | } | |
1993 | ifsta = &sdata->u.sta; | |
1994 | ||
1995 | while ((skb = skb_dequeue(&ifsta->skb_queue))) | |
1996 | ieee80211_sta_rx_queued_mgmt(dev, skb); | |
1997 | ||
1998 | if (ifsta->state != IEEE80211_AUTHENTICATE && | |
1999 | ifsta->state != IEEE80211_ASSOCIATE && | |
2000 | test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) { | |
2001 | ieee80211_sta_start_scan(dev, NULL, 0); | |
2002 | return; | |
2003 | } | |
2004 | ||
2005 | if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) { | |
2006 | if (ieee80211_sta_config_auth(dev, ifsta)) | |
2007 | return; | |
2008 | clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request); | |
2009 | } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request)) | |
2010 | return; | |
2011 | ||
2012 | switch (ifsta->state) { | |
2013 | case IEEE80211_DISABLED: | |
2014 | break; | |
2015 | case IEEE80211_AUTHENTICATE: | |
2016 | ieee80211_authenticate(dev, ifsta); | |
2017 | break; | |
2018 | case IEEE80211_ASSOCIATE: | |
2019 | ieee80211_associate(dev, ifsta); | |
2020 | break; | |
2021 | case IEEE80211_ASSOCIATED: | |
2022 | ieee80211_associated(dev, ifsta); | |
2023 | break; | |
2024 | case IEEE80211_IBSS_SEARCH: | |
2025 | ieee80211_sta_find_ibss(dev, ifsta); | |
2026 | break; | |
2027 | case IEEE80211_IBSS_JOINED: | |
2028 | ieee80211_sta_merge_ibss(dev, ifsta); | |
2029 | break; | |
2030 | default: | |
2031 | printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n", | |
2032 | ifsta->state); | |
2033 | break; | |
2034 | } | |
2035 | ||
2036 | if (ieee80211_privacy_mismatch(dev, ifsta)) { | |
2037 | printk(KERN_DEBUG "%s: privacy configuration mismatch and " | |
2038 | "mixed-cell disabled - disassociate\n", dev->name); | |
2039 | ||
2040 | ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED); | |
2041 | ieee80211_set_disassoc(dev, ifsta, 0); | |
2042 | } | |
2043 | } | |
2044 | ||
2045 | ||
2046 | static void ieee80211_sta_reset_auth(struct net_device *dev, | |
2047 | struct ieee80211_if_sta *ifsta) | |
2048 | { | |
2049 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
2050 | ||
2051 | if (local->ops->reset_tsf) { | |
2052 | /* Reset own TSF to allow time synchronization work. */ | |
2053 | local->ops->reset_tsf(local_to_hw(local)); | |
2054 | } | |
2055 | ||
2056 | ifsta->wmm_last_param_set = -1; /* allow any WMM update */ | |
2057 | ||
2058 | ||
2059 | if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN) | |
2060 | ifsta->auth_alg = WLAN_AUTH_OPEN; | |
2061 | else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY) | |
2062 | ifsta->auth_alg = WLAN_AUTH_SHARED_KEY; | |
2063 | else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP) | |
2064 | ifsta->auth_alg = WLAN_AUTH_LEAP; | |
2065 | else | |
2066 | ifsta->auth_alg = WLAN_AUTH_OPEN; | |
2067 | printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name, | |
2068 | ifsta->auth_alg); | |
2069 | ifsta->auth_transaction = -1; | |
d6f2da5b JS |
2070 | ifsta->flags &= ~IEEE80211_STA_ASSOCIATED; |
2071 | ifsta->auth_tries = ifsta->assoc_tries = 0; | |
f0706e82 JB |
2072 | netif_carrier_off(dev); |
2073 | } | |
2074 | ||
2075 | ||
2076 | void ieee80211_sta_req_auth(struct net_device *dev, | |
2077 | struct ieee80211_if_sta *ifsta) | |
2078 | { | |
2079 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
2080 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
2081 | ||
2082 | if (sdata->type != IEEE80211_IF_TYPE_STA) | |
2083 | return; | |
2084 | ||
d6f2da5b JS |
2085 | if ((ifsta->flags & (IEEE80211_STA_BSSID_SET | |
2086 | IEEE80211_STA_AUTO_BSSID_SEL)) && | |
2087 | (ifsta->flags & (IEEE80211_STA_SSID_SET | | |
2088 | IEEE80211_STA_AUTO_SSID_SEL))) { | |
f0706e82 JB |
2089 | set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request); |
2090 | queue_work(local->hw.workqueue, &ifsta->work); | |
2091 | } | |
2092 | } | |
2093 | ||
2094 | static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta, | |
2095 | const char *ssid, int ssid_len) | |
2096 | { | |
2097 | int tmp, hidden_ssid; | |
2098 | ||
48225709 MW |
2099 | if (ssid_len == ifsta->ssid_len && |
2100 | !memcmp(ifsta->ssid, ssid, ssid_len)) | |
f0706e82 JB |
2101 | return 1; |
2102 | ||
d6f2da5b | 2103 | if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) |
f0706e82 JB |
2104 | return 0; |
2105 | ||
2106 | hidden_ssid = 1; | |
2107 | tmp = ssid_len; | |
2108 | while (tmp--) { | |
2109 | if (ssid[tmp] != '\0') { | |
2110 | hidden_ssid = 0; | |
2111 | break; | |
2112 | } | |
2113 | } | |
2114 | ||
2115 | if (hidden_ssid && ifsta->ssid_len == ssid_len) | |
2116 | return 1; | |
2117 | ||
2118 | if (ssid_len == 1 && ssid[0] == ' ') | |
2119 | return 1; | |
2120 | ||
2121 | return 0; | |
2122 | } | |
2123 | ||
2124 | static int ieee80211_sta_config_auth(struct net_device *dev, | |
2125 | struct ieee80211_if_sta *ifsta) | |
2126 | { | |
2127 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
2128 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
2129 | struct ieee80211_sta_bss *bss, *selected = NULL; | |
2130 | int top_rssi = 0, freq; | |
2131 | ||
d6f2da5b JS |
2132 | if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL | |
2133 | IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) { | |
f0706e82 | 2134 | ifsta->state = IEEE80211_AUTHENTICATE; |
f0706e82 JB |
2135 | ieee80211_sta_reset_auth(dev, ifsta); |
2136 | return 0; | |
2137 | } | |
2138 | ||
2139 | spin_lock_bh(&local->sta_bss_lock); | |
2140 | freq = local->oper_channel->freq; | |
2141 | list_for_each_entry(bss, &local->sta_bss_list, list) { | |
2142 | if (!(bss->capability & WLAN_CAPABILITY_ESS)) | |
2143 | continue; | |
2144 | ||
2145 | if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^ | |
2146 | !!sdata->default_key) | |
2147 | continue; | |
2148 | ||
d6f2da5b JS |
2149 | if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) && |
2150 | bss->freq != freq) | |
f0706e82 JB |
2151 | continue; |
2152 | ||
d6f2da5b | 2153 | if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) && |
f0706e82 JB |
2154 | memcmp(bss->bssid, ifsta->bssid, ETH_ALEN)) |
2155 | continue; | |
2156 | ||
d6f2da5b | 2157 | if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) && |
f0706e82 JB |
2158 | !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len)) |
2159 | continue; | |
2160 | ||
2161 | if (!selected || top_rssi < bss->rssi) { | |
2162 | selected = bss; | |
2163 | top_rssi = bss->rssi; | |
2164 | } | |
2165 | } | |
2166 | if (selected) | |
2167 | atomic_inc(&selected->users); | |
2168 | spin_unlock_bh(&local->sta_bss_lock); | |
2169 | ||
2170 | if (selected) { | |
2171 | ieee80211_set_channel(local, -1, selected->freq); | |
d6f2da5b | 2172 | if (!(ifsta->flags & IEEE80211_STA_SSID_SET)) |
f0706e82 JB |
2173 | ieee80211_sta_set_ssid(dev, selected->ssid, |
2174 | selected->ssid_len); | |
2175 | ieee80211_sta_set_bssid(dev, selected->bssid); | |
2176 | ieee80211_rx_bss_put(dev, selected); | |
2177 | ifsta->state = IEEE80211_AUTHENTICATE; | |
f0706e82 JB |
2178 | ieee80211_sta_reset_auth(dev, ifsta); |
2179 | return 0; | |
2180 | } else { | |
2181 | if (ifsta->state != IEEE80211_AUTHENTICATE) { | |
d6f2da5b | 2182 | if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) |
b9bf1e60 JL |
2183 | ieee80211_sta_start_scan(dev, NULL, 0); |
2184 | else | |
2185 | ieee80211_sta_start_scan(dev, ifsta->ssid, | |
2186 | ifsta->ssid_len); | |
f0706e82 JB |
2187 | ifsta->state = IEEE80211_AUTHENTICATE; |
2188 | set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request); | |
2189 | } else | |
2190 | ifsta->state = IEEE80211_DISABLED; | |
2191 | } | |
f0706e82 JB |
2192 | return -1; |
2193 | } | |
2194 | ||
2195 | static int ieee80211_sta_join_ibss(struct net_device *dev, | |
2196 | struct ieee80211_if_sta *ifsta, | |
2197 | struct ieee80211_sta_bss *bss) | |
2198 | { | |
2199 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
2200 | int res, rates, i, j; | |
2201 | struct sk_buff *skb; | |
2202 | struct ieee80211_mgmt *mgmt; | |
2203 | struct ieee80211_tx_control control; | |
2204 | struct ieee80211_rate *rate; | |
2205 | struct ieee80211_hw_mode *mode; | |
2206 | struct rate_control_extra extra; | |
2207 | u8 *pos; | |
2208 | struct ieee80211_sub_if_data *sdata; | |
2209 | ||
2210 | /* Remove possible STA entries from other IBSS networks. */ | |
2211 | sta_info_flush(local, NULL); | |
2212 | ||
2213 | if (local->ops->reset_tsf) { | |
2214 | /* Reset own TSF to allow time synchronization work. */ | |
2215 | local->ops->reset_tsf(local_to_hw(local)); | |
2216 | } | |
2217 | memcpy(ifsta->bssid, bss->bssid, ETH_ALEN); | |
2218 | res = ieee80211_if_config(dev); | |
2219 | if (res) | |
2220 | return res; | |
2221 | ||
2222 | local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10; | |
2223 | ||
2224 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
2225 | sdata->drop_unencrypted = bss->capability & | |
2226 | WLAN_CAPABILITY_PRIVACY ? 1 : 0; | |
2227 | ||
2228 | res = ieee80211_set_channel(local, -1, bss->freq); | |
2229 | ||
2230 | if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) { | |
2231 | printk(KERN_DEBUG "%s: IBSS not allowed on channel %d " | |
2232 | "(%d MHz)\n", dev->name, local->hw.conf.channel, | |
2233 | local->hw.conf.freq); | |
2234 | return -1; | |
2235 | } | |
2236 | ||
2237 | /* Set beacon template based on scan results */ | |
2238 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400); | |
2239 | do { | |
2240 | if (!skb) | |
2241 | break; | |
2242 | ||
2243 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
2244 | ||
2245 | mgmt = (struct ieee80211_mgmt *) | |
2246 | skb_put(skb, 24 + sizeof(mgmt->u.beacon)); | |
2247 | memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon)); | |
2248 | mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT, | |
2249 | IEEE80211_STYPE_BEACON); | |
2250 | memset(mgmt->da, 0xff, ETH_ALEN); | |
2251 | memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN); | |
2252 | memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN); | |
2253 | mgmt->u.beacon.beacon_int = | |
2254 | cpu_to_le16(local->hw.conf.beacon_int); | |
2255 | mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability); | |
2256 | ||
2257 | pos = skb_put(skb, 2 + ifsta->ssid_len); | |
2258 | *pos++ = WLAN_EID_SSID; | |
2259 | *pos++ = ifsta->ssid_len; | |
2260 | memcpy(pos, ifsta->ssid, ifsta->ssid_len); | |
2261 | ||
2262 | rates = bss->supp_rates_len; | |
2263 | if (rates > 8) | |
2264 | rates = 8; | |
2265 | pos = skb_put(skb, 2 + rates); | |
2266 | *pos++ = WLAN_EID_SUPP_RATES; | |
2267 | *pos++ = rates; | |
2268 | memcpy(pos, bss->supp_rates, rates); | |
2269 | ||
2270 | pos = skb_put(skb, 2 + 1); | |
2271 | *pos++ = WLAN_EID_DS_PARAMS; | |
2272 | *pos++ = 1; | |
2273 | *pos++ = bss->channel; | |
2274 | ||
2275 | pos = skb_put(skb, 2 + 2); | |
2276 | *pos++ = WLAN_EID_IBSS_PARAMS; | |
2277 | *pos++ = 2; | |
2278 | /* FIX: set ATIM window based on scan results */ | |
2279 | *pos++ = 0; | |
2280 | *pos++ = 0; | |
2281 | ||
2282 | if (bss->supp_rates_len > 8) { | |
2283 | rates = bss->supp_rates_len - 8; | |
2284 | pos = skb_put(skb, 2 + rates); | |
2285 | *pos++ = WLAN_EID_EXT_SUPP_RATES; | |
2286 | *pos++ = rates; | |
2287 | memcpy(pos, &bss->supp_rates[8], rates); | |
2288 | } | |
2289 | ||
2290 | memset(&control, 0, sizeof(control)); | |
2291 | memset(&extra, 0, sizeof(extra)); | |
2292 | extra.mode = local->oper_hw_mode; | |
2293 | rate = rate_control_get_rate(local, dev, skb, &extra); | |
2294 | if (!rate) { | |
2295 | printk(KERN_DEBUG "%s: Failed to determine TX rate " | |
2296 | "for IBSS beacon\n", dev->name); | |
2297 | break; | |
2298 | } | |
13262ffd JS |
2299 | control.tx_rate = |
2300 | ((sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) && | |
2301 | (rate->flags & IEEE80211_RATE_PREAMBLE2)) ? | |
f0706e82 JB |
2302 | rate->val2 : rate->val; |
2303 | control.antenna_sel_tx = local->hw.conf.antenna_sel_tx; | |
2304 | control.power_level = local->hw.conf.power_level; | |
2305 | control.flags |= IEEE80211_TXCTL_NO_ACK; | |
2306 | control.retry_limit = 1; | |
2307 | ||
2308 | ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC); | |
2309 | if (ifsta->probe_resp) { | |
2310 | mgmt = (struct ieee80211_mgmt *) | |
2311 | ifsta->probe_resp->data; | |
2312 | mgmt->frame_control = | |
2313 | IEEE80211_FC(IEEE80211_FTYPE_MGMT, | |
2314 | IEEE80211_STYPE_PROBE_RESP); | |
2315 | } else { | |
2316 | printk(KERN_DEBUG "%s: Could not allocate ProbeResp " | |
2317 | "template for IBSS\n", dev->name); | |
2318 | } | |
2319 | ||
2320 | if (local->ops->beacon_update && | |
2321 | local->ops->beacon_update(local_to_hw(local), | |
2322 | skb, &control) == 0) { | |
2323 | printk(KERN_DEBUG "%s: Configured IBSS beacon " | |
2324 | "template based on scan results\n", dev->name); | |
2325 | skb = NULL; | |
2326 | } | |
2327 | ||
2328 | rates = 0; | |
2329 | mode = local->oper_hw_mode; | |
2330 | for (i = 0; i < bss->supp_rates_len; i++) { | |
2331 | int bitrate = (bss->supp_rates[i] & 0x7f) * 5; | |
f0706e82 JB |
2332 | for (j = 0; j < mode->num_rates; j++) |
2333 | if (mode->rates[j].rate == bitrate) | |
2334 | rates |= BIT(j); | |
2335 | } | |
2336 | ifsta->supp_rates_bits = rates; | |
2337 | } while (0); | |
2338 | ||
2339 | if (skb) { | |
2340 | printk(KERN_DEBUG "%s: Failed to configure IBSS beacon " | |
2341 | "template\n", dev->name); | |
2342 | dev_kfree_skb(skb); | |
2343 | } | |
2344 | ||
2345 | ifsta->state = IEEE80211_IBSS_JOINED; | |
2346 | mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL); | |
2347 | ||
2348 | ieee80211_rx_bss_put(dev, bss); | |
2349 | ||
2350 | return res; | |
2351 | } | |
2352 | ||
2353 | ||
2354 | static int ieee80211_sta_create_ibss(struct net_device *dev, | |
2355 | struct ieee80211_if_sta *ifsta) | |
2356 | { | |
2357 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
2358 | struct ieee80211_sta_bss *bss; | |
cffdd30d | 2359 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
f0706e82 JB |
2360 | struct ieee80211_hw_mode *mode; |
2361 | u8 bssid[ETH_ALEN], *pos; | |
2362 | int i; | |
0795af57 | 2363 | DECLARE_MAC_BUF(mac); |
f0706e82 JB |
2364 | |
2365 | #if 0 | |
2366 | /* Easier testing, use fixed BSSID. */ | |
2367 | memset(bssid, 0xfe, ETH_ALEN); | |
2368 | #else | |
2369 | /* Generate random, not broadcast, locally administered BSSID. Mix in | |
2370 | * own MAC address to make sure that devices that do not have proper | |
2371 | * random number generator get different BSSID. */ | |
2372 | get_random_bytes(bssid, ETH_ALEN); | |
2373 | for (i = 0; i < ETH_ALEN; i++) | |
2374 | bssid[i] ^= dev->dev_addr[i]; | |
2375 | bssid[0] &= ~0x01; | |
2376 | bssid[0] |= 0x02; | |
2377 | #endif | |
2378 | ||
0795af57 JP |
2379 | printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n", |
2380 | dev->name, print_mac(mac, bssid)); | |
f0706e82 | 2381 | |
cffdd30d JL |
2382 | bss = ieee80211_rx_bss_add(dev, bssid, local->hw.conf.channel, |
2383 | sdata->u.sta.ssid, sdata->u.sta.ssid_len); | |
f0706e82 JB |
2384 | if (!bss) |
2385 | return -ENOMEM; | |
2386 | ||
f0706e82 JB |
2387 | mode = local->oper_hw_mode; |
2388 | ||
2389 | if (local->hw.conf.beacon_int == 0) | |
2390 | local->hw.conf.beacon_int = 100; | |
2391 | bss->beacon_int = local->hw.conf.beacon_int; | |
2392 | bss->hw_mode = local->hw.conf.phymode; | |
f0706e82 JB |
2393 | bss->freq = local->hw.conf.freq; |
2394 | bss->last_update = jiffies; | |
2395 | bss->capability = WLAN_CAPABILITY_IBSS; | |
2396 | if (sdata->default_key) { | |
2397 | bss->capability |= WLAN_CAPABILITY_PRIVACY; | |
2398 | } else | |
2399 | sdata->drop_unencrypted = 0; | |
2400 | bss->supp_rates_len = mode->num_rates; | |
2401 | pos = bss->supp_rates; | |
2402 | for (i = 0; i < mode->num_rates; i++) { | |
2403 | int rate = mode->rates[i].rate; | |
f0706e82 JB |
2404 | *pos++ = (u8) (rate / 5); |
2405 | } | |
2406 | ||
2407 | return ieee80211_sta_join_ibss(dev, ifsta, bss); | |
2408 | } | |
2409 | ||
2410 | ||
2411 | static int ieee80211_sta_find_ibss(struct net_device *dev, | |
2412 | struct ieee80211_if_sta *ifsta) | |
2413 | { | |
2414 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
2415 | struct ieee80211_sta_bss *bss; | |
2416 | int found = 0; | |
2417 | u8 bssid[ETH_ALEN]; | |
2418 | int active_ibss; | |
0795af57 JP |
2419 | DECLARE_MAC_BUF(mac); |
2420 | DECLARE_MAC_BUF(mac2); | |
f0706e82 JB |
2421 | |
2422 | if (ifsta->ssid_len == 0) | |
2423 | return -EINVAL; | |
2424 | ||
2425 | active_ibss = ieee80211_sta_active_ibss(dev); | |
2426 | #ifdef CONFIG_MAC80211_IBSS_DEBUG | |
2427 | printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n", | |
2428 | dev->name, active_ibss); | |
2429 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ | |
2430 | spin_lock_bh(&local->sta_bss_lock); | |
2431 | list_for_each_entry(bss, &local->sta_bss_list, list) { | |
2432 | if (ifsta->ssid_len != bss->ssid_len || | |
2433 | memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0 | |
2434 | || !(bss->capability & WLAN_CAPABILITY_IBSS)) | |
2435 | continue; | |
2436 | #ifdef CONFIG_MAC80211_IBSS_DEBUG | |
0795af57 JP |
2437 | printk(KERN_DEBUG " bssid=%s found\n", |
2438 | print_mac(mac, bss->bssid)); | |
f0706e82 JB |
2439 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ |
2440 | memcpy(bssid, bss->bssid, ETH_ALEN); | |
2441 | found = 1; | |
2442 | if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0) | |
2443 | break; | |
2444 | } | |
2445 | spin_unlock_bh(&local->sta_bss_lock); | |
2446 | ||
2447 | #ifdef CONFIG_MAC80211_IBSS_DEBUG | |
0795af57 JP |
2448 | printk(KERN_DEBUG " sta_find_ibss: selected %s current " |
2449 | "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid)); | |
f0706e82 JB |
2450 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ |
2451 | if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 && | |
cffdd30d JL |
2452 | (bss = ieee80211_rx_bss_get(dev, bssid, local->hw.conf.channel, |
2453 | ifsta->ssid, ifsta->ssid_len))) { | |
0795af57 | 2454 | printk(KERN_DEBUG "%s: Selected IBSS BSSID %s" |
f0706e82 | 2455 | " based on configured SSID\n", |
0795af57 | 2456 | dev->name, print_mac(mac, bssid)); |
f0706e82 JB |
2457 | return ieee80211_sta_join_ibss(dev, ifsta, bss); |
2458 | } | |
2459 | #ifdef CONFIG_MAC80211_IBSS_DEBUG | |
2460 | printk(KERN_DEBUG " did not try to join ibss\n"); | |
2461 | #endif /* CONFIG_MAC80211_IBSS_DEBUG */ | |
2462 | ||
2463 | /* Selected IBSS not found in current scan results - try to scan */ | |
2464 | if (ifsta->state == IEEE80211_IBSS_JOINED && | |
2465 | !ieee80211_sta_active_ibss(dev)) { | |
2466 | mod_timer(&ifsta->timer, jiffies + | |
2467 | IEEE80211_IBSS_MERGE_INTERVAL); | |
2468 | } else if (time_after(jiffies, local->last_scan_completed + | |
2469 | IEEE80211_SCAN_INTERVAL)) { | |
2470 | printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to " | |
2471 | "join\n", dev->name); | |
2472 | return ieee80211_sta_req_scan(dev, ifsta->ssid, | |
2473 | ifsta->ssid_len); | |
2474 | } else if (ifsta->state != IEEE80211_IBSS_JOINED) { | |
2475 | int interval = IEEE80211_SCAN_INTERVAL; | |
2476 | ||
2477 | if (time_after(jiffies, ifsta->ibss_join_req + | |
2478 | IEEE80211_IBSS_JOIN_TIMEOUT)) { | |
d6f2da5b | 2479 | if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) && |
f0706e82 JB |
2480 | local->oper_channel->flag & IEEE80211_CHAN_W_IBSS) |
2481 | return ieee80211_sta_create_ibss(dev, ifsta); | |
d6f2da5b | 2482 | if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) { |
f0706e82 JB |
2483 | printk(KERN_DEBUG "%s: IBSS not allowed on the" |
2484 | " configured channel %d (%d MHz)\n", | |
2485 | dev->name, local->hw.conf.channel, | |
2486 | local->hw.conf.freq); | |
2487 | } | |
2488 | ||
2489 | /* No IBSS found - decrease scan interval and continue | |
2490 | * scanning. */ | |
2491 | interval = IEEE80211_SCAN_INTERVAL_SLOW; | |
2492 | } | |
2493 | ||
2494 | ifsta->state = IEEE80211_IBSS_SEARCH; | |
2495 | mod_timer(&ifsta->timer, jiffies + interval); | |
2496 | return 0; | |
2497 | } | |
2498 | ||
2499 | return 0; | |
2500 | } | |
2501 | ||
2502 | ||
2503 | int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len) | |
2504 | { | |
2505 | struct ieee80211_sub_if_data *sdata; | |
2506 | struct ieee80211_if_sta *ifsta; | |
2507 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
2508 | ||
2509 | if (len > IEEE80211_MAX_SSID_LEN) | |
2510 | return -EINVAL; | |
2511 | ||
2512 | /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is | |
2513 | * not defined. */ | |
2514 | if (local->ops->conf_tx) { | |
2515 | struct ieee80211_tx_queue_params qparam; | |
2516 | int i; | |
2517 | ||
2518 | memset(&qparam, 0, sizeof(qparam)); | |
2519 | /* TODO: are these ok defaults for all hw_modes? */ | |
2520 | qparam.aifs = 2; | |
2521 | qparam.cw_min = | |
2522 | local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15; | |
2523 | qparam.cw_max = 1023; | |
2524 | qparam.burst_time = 0; | |
2525 | for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++) | |
2526 | { | |
2527 | local->ops->conf_tx(local_to_hw(local), | |
2528 | i + IEEE80211_TX_QUEUE_DATA0, | |
2529 | &qparam); | |
2530 | } | |
2531 | /* IBSS uses different parameters for Beacon sending */ | |
2532 | qparam.cw_min++; | |
2533 | qparam.cw_min *= 2; | |
2534 | qparam.cw_min--; | |
2535 | local->ops->conf_tx(local_to_hw(local), | |
2536 | IEEE80211_TX_QUEUE_BEACON, &qparam); | |
2537 | } | |
2538 | ||
2539 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
2540 | ifsta = &sdata->u.sta; | |
2541 | ||
2542 | if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) | |
d6f2da5b | 2543 | ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET; |
f0706e82 JB |
2544 | memcpy(ifsta->ssid, ssid, len); |
2545 | memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len); | |
2546 | ifsta->ssid_len = len; | |
2547 | ||
d6f2da5b JS |
2548 | if (len) |
2549 | ifsta->flags |= IEEE80211_STA_SSID_SET; | |
2550 | else | |
2551 | ifsta->flags &= ~IEEE80211_STA_SSID_SET; | |
2552 | if (sdata->type == IEEE80211_IF_TYPE_IBSS && | |
2553 | !(ifsta->flags & IEEE80211_STA_BSSID_SET)) { | |
f0706e82 JB |
2554 | ifsta->ibss_join_req = jiffies; |
2555 | ifsta->state = IEEE80211_IBSS_SEARCH; | |
2556 | return ieee80211_sta_find_ibss(dev, ifsta); | |
2557 | } | |
2558 | return 0; | |
2559 | } | |
2560 | ||
2561 | ||
2562 | int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len) | |
2563 | { | |
2564 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
2565 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
2566 | memcpy(ssid, ifsta->ssid, ifsta->ssid_len); | |
2567 | *len = ifsta->ssid_len; | |
2568 | return 0; | |
2569 | } | |
2570 | ||
2571 | ||
2572 | int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid) | |
2573 | { | |
2574 | struct ieee80211_sub_if_data *sdata; | |
2575 | struct ieee80211_if_sta *ifsta; | |
2576 | int res; | |
2577 | ||
2578 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
2579 | ifsta = &sdata->u.sta; | |
2580 | ||
2581 | if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) { | |
2582 | memcpy(ifsta->bssid, bssid, ETH_ALEN); | |
2583 | res = ieee80211_if_config(dev); | |
2584 | if (res) { | |
2585 | printk(KERN_DEBUG "%s: Failed to config new BSSID to " | |
2586 | "the low-level driver\n", dev->name); | |
2587 | return res; | |
2588 | } | |
2589 | } | |
2590 | ||
d6f2da5b JS |
2591 | if (is_valid_ether_addr(bssid)) |
2592 | ifsta->flags |= IEEE80211_STA_BSSID_SET; | |
f0706e82 | 2593 | else |
d6f2da5b JS |
2594 | ifsta->flags &= ~IEEE80211_STA_BSSID_SET; |
2595 | ||
f0706e82 JB |
2596 | return 0; |
2597 | } | |
2598 | ||
2599 | ||
2600 | static void ieee80211_send_nullfunc(struct ieee80211_local *local, | |
2601 | struct ieee80211_sub_if_data *sdata, | |
2602 | int powersave) | |
2603 | { | |
2604 | struct sk_buff *skb; | |
2605 | struct ieee80211_hdr *nullfunc; | |
2606 | u16 fc; | |
2607 | ||
2608 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24); | |
2609 | if (!skb) { | |
2610 | printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc " | |
2611 | "frame\n", sdata->dev->name); | |
2612 | return; | |
2613 | } | |
2614 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
2615 | ||
2616 | nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24); | |
2617 | memset(nullfunc, 0, 24); | |
2618 | fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC | | |
2619 | IEEE80211_FCTL_TODS; | |
2620 | if (powersave) | |
2621 | fc |= IEEE80211_FCTL_PM; | |
2622 | nullfunc->frame_control = cpu_to_le16(fc); | |
2623 | memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN); | |
2624 | memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN); | |
2625 | memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN); | |
2626 | ||
2627 | ieee80211_sta_tx(sdata->dev, skb, 0); | |
2628 | } | |
2629 | ||
2630 | ||
2631 | void ieee80211_scan_completed(struct ieee80211_hw *hw) | |
2632 | { | |
2633 | struct ieee80211_local *local = hw_to_local(hw); | |
2634 | struct net_device *dev = local->scan_dev; | |
2635 | struct ieee80211_sub_if_data *sdata; | |
2636 | union iwreq_data wrqu; | |
2637 | ||
2638 | local->last_scan_completed = jiffies; | |
2639 | wmb(); | |
2640 | local->sta_scanning = 0; | |
2641 | ||
2642 | if (ieee80211_hw_config(local)) | |
2643 | printk(KERN_DEBUG "%s: failed to restore operational" | |
2644 | "channel after scan\n", dev->name); | |
2645 | ||
4150c572 JB |
2646 | |
2647 | netif_tx_lock_bh(local->mdev); | |
2648 | local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC; | |
2649 | local->ops->configure_filter(local_to_hw(local), | |
2650 | FIF_BCN_PRBRESP_PROMISC, | |
2651 | &local->filter_flags, | |
2652 | local->mdev->mc_count, | |
2653 | local->mdev->mc_list); | |
2654 | ||
2655 | netif_tx_unlock_bh(local->mdev); | |
f0706e82 JB |
2656 | |
2657 | memset(&wrqu, 0, sizeof(wrqu)); | |
2658 | wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL); | |
2659 | ||
79010420 JB |
2660 | rcu_read_lock(); |
2661 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { | |
14042cbe MN |
2662 | |
2663 | /* No need to wake the master device. */ | |
2664 | if (sdata->dev == local->mdev) | |
2665 | continue; | |
2666 | ||
f0706e82 | 2667 | if (sdata->type == IEEE80211_IF_TYPE_STA) { |
d6f2da5b | 2668 | if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) |
f0706e82 JB |
2669 | ieee80211_send_nullfunc(local, sdata, 0); |
2670 | ieee80211_sta_timer((unsigned long)sdata); | |
2671 | } | |
14042cbe | 2672 | |
f0706e82 JB |
2673 | netif_wake_queue(sdata->dev); |
2674 | } | |
79010420 | 2675 | rcu_read_unlock(); |
f0706e82 JB |
2676 | |
2677 | sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
2678 | if (sdata->type == IEEE80211_IF_TYPE_IBSS) { | |
2679 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
d6f2da5b | 2680 | if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) || |
f0706e82 JB |
2681 | (!ifsta->state == IEEE80211_IBSS_JOINED && |
2682 | !ieee80211_sta_active_ibss(dev))) | |
2683 | ieee80211_sta_find_ibss(dev, ifsta); | |
2684 | } | |
2685 | } | |
2686 | EXPORT_SYMBOL(ieee80211_scan_completed); | |
2687 | ||
2688 | void ieee80211_sta_scan_work(struct work_struct *work) | |
2689 | { | |
2690 | struct ieee80211_local *local = | |
2691 | container_of(work, struct ieee80211_local, scan_work.work); | |
2692 | struct net_device *dev = local->scan_dev; | |
2693 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
2694 | struct ieee80211_hw_mode *mode; | |
2695 | struct ieee80211_channel *chan; | |
2696 | int skip; | |
2697 | unsigned long next_delay = 0; | |
2698 | ||
2699 | if (!local->sta_scanning) | |
2700 | return; | |
2701 | ||
2702 | switch (local->scan_state) { | |
2703 | case SCAN_SET_CHANNEL: | |
2704 | mode = local->scan_hw_mode; | |
2705 | if (local->scan_hw_mode->list.next == &local->modes_list && | |
2706 | local->scan_channel_idx >= mode->num_channels) { | |
2707 | ieee80211_scan_completed(local_to_hw(local)); | |
2708 | return; | |
2709 | } | |
2710 | skip = !(local->enabled_modes & (1 << mode->mode)); | |
2711 | chan = &mode->channels[local->scan_channel_idx]; | |
2712 | if (!(chan->flag & IEEE80211_CHAN_W_SCAN) || | |
2713 | (sdata->type == IEEE80211_IF_TYPE_IBSS && | |
2714 | !(chan->flag & IEEE80211_CHAN_W_IBSS)) || | |
2715 | (local->hw_modes & local->enabled_modes & | |
2716 | (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B)) | |
2717 | skip = 1; | |
2718 | ||
2719 | if (!skip) { | |
2720 | #if 0 | |
2721 | printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n", | |
2722 | dev->name, chan->chan, chan->freq); | |
2723 | #endif | |
2724 | ||
2725 | local->scan_channel = chan; | |
2726 | if (ieee80211_hw_config(local)) { | |
2727 | printk(KERN_DEBUG "%s: failed to set channel " | |
2728 | "%d (%d MHz) for scan\n", dev->name, | |
2729 | chan->chan, chan->freq); | |
2730 | skip = 1; | |
2731 | } | |
2732 | } | |
2733 | ||
2734 | local->scan_channel_idx++; | |
2735 | if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) { | |
2736 | if (local->scan_hw_mode->list.next != &local->modes_list) { | |
2737 | local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next, | |
2738 | struct ieee80211_hw_mode, | |
2739 | list); | |
2740 | local->scan_channel_idx = 0; | |
2741 | } | |
2742 | } | |
2743 | ||
2744 | if (skip) | |
2745 | break; | |
2746 | ||
2747 | next_delay = IEEE80211_PROBE_DELAY + | |
2748 | usecs_to_jiffies(local->hw.channel_change_time); | |
2749 | local->scan_state = SCAN_SEND_PROBE; | |
2750 | break; | |
2751 | case SCAN_SEND_PROBE: | |
2752 | if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) { | |
2753 | ieee80211_send_probe_req(dev, NULL, local->scan_ssid, | |
2754 | local->scan_ssid_len); | |
2755 | next_delay = IEEE80211_CHANNEL_TIME; | |
2756 | } else | |
2757 | next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; | |
2758 | local->scan_state = SCAN_SET_CHANNEL; | |
2759 | break; | |
2760 | } | |
2761 | ||
2762 | if (local->sta_scanning) | |
2763 | queue_delayed_work(local->hw.workqueue, &local->scan_work, | |
2764 | next_delay); | |
2765 | } | |
2766 | ||
2767 | ||
2768 | static int ieee80211_sta_start_scan(struct net_device *dev, | |
2769 | u8 *ssid, size_t ssid_len) | |
2770 | { | |
2771 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
2772 | struct ieee80211_sub_if_data *sdata; | |
2773 | ||
2774 | if (ssid_len > IEEE80211_MAX_SSID_LEN) | |
2775 | return -EINVAL; | |
2776 | ||
2777 | /* MLME-SCAN.request (page 118) page 144 (11.1.3.1) | |
2778 | * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS | |
2779 | * BSSID: MACAddress | |
2780 | * SSID | |
2781 | * ScanType: ACTIVE, PASSIVE | |
2782 | * ProbeDelay: delay (in microseconds) to be used prior to transmitting | |
2783 | * a Probe frame during active scanning | |
2784 | * ChannelList | |
2785 | * MinChannelTime (>= ProbeDelay), in TU | |
2786 | * MaxChannelTime: (>= MinChannelTime), in TU | |
2787 | */ | |
2788 | ||
2789 | /* MLME-SCAN.confirm | |
2790 | * BSSDescriptionSet | |
2791 | * ResultCode: SUCCESS, INVALID_PARAMETERS | |
2792 | */ | |
2793 | ||
2794 | if (local->sta_scanning) { | |
2795 | if (local->scan_dev == dev) | |
2796 | return 0; | |
2797 | return -EBUSY; | |
2798 | } | |
2799 | ||
2800 | if (local->ops->hw_scan) { | |
2801 | int rc = local->ops->hw_scan(local_to_hw(local), | |
2802 | ssid, ssid_len); | |
2803 | if (!rc) { | |
2804 | local->sta_scanning = 1; | |
2805 | local->scan_dev = dev; | |
2806 | } | |
2807 | return rc; | |
2808 | } | |
2809 | ||
2810 | local->sta_scanning = 1; | |
2811 | ||
79010420 JB |
2812 | rcu_read_lock(); |
2813 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { | |
14042cbe MN |
2814 | |
2815 | /* Don't stop the master interface, otherwise we can't transmit | |
2816 | * probes! */ | |
2817 | if (sdata->dev == local->mdev) | |
2818 | continue; | |
2819 | ||
f0706e82 JB |
2820 | netif_stop_queue(sdata->dev); |
2821 | if (sdata->type == IEEE80211_IF_TYPE_STA && | |
d6f2da5b | 2822 | (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)) |
f0706e82 JB |
2823 | ieee80211_send_nullfunc(local, sdata, 1); |
2824 | } | |
79010420 | 2825 | rcu_read_unlock(); |
f0706e82 JB |
2826 | |
2827 | if (ssid) { | |
2828 | local->scan_ssid_len = ssid_len; | |
2829 | memcpy(local->scan_ssid, ssid, ssid_len); | |
2830 | } else | |
2831 | local->scan_ssid_len = 0; | |
2832 | local->scan_state = SCAN_SET_CHANNEL; | |
2833 | local->scan_hw_mode = list_entry(local->modes_list.next, | |
2834 | struct ieee80211_hw_mode, | |
2835 | list); | |
2836 | local->scan_channel_idx = 0; | |
2837 | local->scan_dev = dev; | |
2838 | ||
4150c572 JB |
2839 | netif_tx_lock_bh(local->mdev); |
2840 | local->filter_flags |= FIF_BCN_PRBRESP_PROMISC; | |
2841 | local->ops->configure_filter(local_to_hw(local), | |
2842 | FIF_BCN_PRBRESP_PROMISC, | |
2843 | &local->filter_flags, | |
2844 | local->mdev->mc_count, | |
2845 | local->mdev->mc_list); | |
2846 | netif_tx_unlock_bh(local->mdev); | |
f0706e82 JB |
2847 | |
2848 | /* TODO: start scan as soon as all nullfunc frames are ACKed */ | |
2849 | queue_delayed_work(local->hw.workqueue, &local->scan_work, | |
2850 | IEEE80211_CHANNEL_TIME); | |
2851 | ||
2852 | return 0; | |
2853 | } | |
2854 | ||
2855 | ||
2856 | int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len) | |
2857 | { | |
2858 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
2859 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
2860 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
2861 | ||
2862 | if (sdata->type != IEEE80211_IF_TYPE_STA) | |
2863 | return ieee80211_sta_start_scan(dev, ssid, ssid_len); | |
2864 | ||
2865 | if (local->sta_scanning) { | |
2866 | if (local->scan_dev == dev) | |
2867 | return 0; | |
2868 | return -EBUSY; | |
2869 | } | |
2870 | ||
2871 | set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request); | |
2872 | queue_work(local->hw.workqueue, &ifsta->work); | |
2873 | return 0; | |
2874 | } | |
2875 | ||
2876 | static char * | |
2877 | ieee80211_sta_scan_result(struct net_device *dev, | |
2878 | struct ieee80211_sta_bss *bss, | |
2879 | char *current_ev, char *end_buf) | |
2880 | { | |
2881 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
2882 | struct iw_event iwe; | |
2883 | ||
2884 | if (time_after(jiffies, | |
2885 | bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE)) | |
2886 | return current_ev; | |
2887 | ||
2888 | if (!(local->enabled_modes & (1 << bss->hw_mode))) | |
2889 | return current_ev; | |
2890 | ||
2891 | if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY && | |
2892 | !bss->wpa_ie && !bss->rsn_ie) | |
2893 | return current_ev; | |
2894 | ||
2895 | if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID && | |
2896 | (local->scan_ssid_len != bss->ssid_len || | |
2897 | memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0)) | |
2898 | return current_ev; | |
2899 | ||
2900 | memset(&iwe, 0, sizeof(iwe)); | |
2901 | iwe.cmd = SIOCGIWAP; | |
2902 | iwe.u.ap_addr.sa_family = ARPHRD_ETHER; | |
2903 | memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN); | |
2904 | current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, | |
2905 | IW_EV_ADDR_LEN); | |
2906 | ||
2907 | memset(&iwe, 0, sizeof(iwe)); | |
2908 | iwe.cmd = SIOCGIWESSID; | |
2909 | iwe.u.data.length = bss->ssid_len; | |
2910 | iwe.u.data.flags = 1; | |
2911 | current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, | |
2912 | bss->ssid); | |
2913 | ||
2914 | if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) { | |
2915 | memset(&iwe, 0, sizeof(iwe)); | |
2916 | iwe.cmd = SIOCGIWMODE; | |
2917 | if (bss->capability & WLAN_CAPABILITY_ESS) | |
2918 | iwe.u.mode = IW_MODE_MASTER; | |
2919 | else | |
2920 | iwe.u.mode = IW_MODE_ADHOC; | |
2921 | current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, | |
2922 | IW_EV_UINT_LEN); | |
2923 | } | |
2924 | ||
2925 | memset(&iwe, 0, sizeof(iwe)); | |
2926 | iwe.cmd = SIOCGIWFREQ; | |
2927 | iwe.u.freq.m = bss->channel; | |
2928 | iwe.u.freq.e = 0; | |
2929 | current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, | |
2930 | IW_EV_FREQ_LEN); | |
2931 | iwe.u.freq.m = bss->freq * 100000; | |
2932 | iwe.u.freq.e = 1; | |
2933 | current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, | |
2934 | IW_EV_FREQ_LEN); | |
2935 | ||
2936 | memset(&iwe, 0, sizeof(iwe)); | |
2937 | iwe.cmd = IWEVQUAL; | |
2938 | iwe.u.qual.qual = bss->signal; | |
2939 | iwe.u.qual.level = bss->rssi; | |
2940 | iwe.u.qual.noise = bss->noise; | |
2941 | iwe.u.qual.updated = local->wstats_flags; | |
2942 | current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe, | |
2943 | IW_EV_QUAL_LEN); | |
2944 | ||
2945 | memset(&iwe, 0, sizeof(iwe)); | |
2946 | iwe.cmd = SIOCGIWENCODE; | |
2947 | if (bss->capability & WLAN_CAPABILITY_PRIVACY) | |
2948 | iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY; | |
2949 | else | |
2950 | iwe.u.data.flags = IW_ENCODE_DISABLED; | |
2951 | iwe.u.data.length = 0; | |
2952 | current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, ""); | |
2953 | ||
2954 | if (bss && bss->wpa_ie) { | |
2955 | memset(&iwe, 0, sizeof(iwe)); | |
2956 | iwe.cmd = IWEVGENIE; | |
2957 | iwe.u.data.length = bss->wpa_ie_len; | |
2958 | current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, | |
2959 | bss->wpa_ie); | |
2960 | } | |
2961 | ||
2962 | if (bss && bss->rsn_ie) { | |
2963 | memset(&iwe, 0, sizeof(iwe)); | |
2964 | iwe.cmd = IWEVGENIE; | |
2965 | iwe.u.data.length = bss->rsn_ie_len; | |
2966 | current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, | |
2967 | bss->rsn_ie); | |
2968 | } | |
2969 | ||
2970 | if (bss && bss->supp_rates_len > 0) { | |
2971 | /* display all supported rates in readable format */ | |
2972 | char *p = current_ev + IW_EV_LCP_LEN; | |
2973 | int i; | |
2974 | ||
2975 | memset(&iwe, 0, sizeof(iwe)); | |
2976 | iwe.cmd = SIOCGIWRATE; | |
2977 | /* Those two flags are ignored... */ | |
2978 | iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0; | |
2979 | ||
2980 | for (i = 0; i < bss->supp_rates_len; i++) { | |
2981 | iwe.u.bitrate.value = ((bss->supp_rates[i] & | |
2982 | 0x7f) * 500000); | |
2983 | p = iwe_stream_add_value(current_ev, p, | |
2984 | end_buf, &iwe, IW_EV_PARAM_LEN); | |
2985 | } | |
2986 | current_ev = p; | |
2987 | } | |
2988 | ||
2989 | if (bss) { | |
2990 | char *buf; | |
2991 | buf = kmalloc(30, GFP_ATOMIC); | |
2992 | if (buf) { | |
2993 | memset(&iwe, 0, sizeof(iwe)); | |
2994 | iwe.cmd = IWEVCUSTOM; | |
2995 | sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp)); | |
2996 | iwe.u.data.length = strlen(buf); | |
2997 | current_ev = iwe_stream_add_point(current_ev, end_buf, | |
2998 | &iwe, buf); | |
2999 | kfree(buf); | |
3000 | } | |
3001 | } | |
3002 | ||
3003 | do { | |
3004 | char *buf; | |
3005 | ||
3006 | if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO)) | |
3007 | break; | |
3008 | ||
3009 | buf = kmalloc(100, GFP_ATOMIC); | |
3010 | if (!buf) | |
3011 | break; | |
3012 | ||
3013 | memset(&iwe, 0, sizeof(iwe)); | |
3014 | iwe.cmd = IWEVCUSTOM; | |
3015 | sprintf(buf, "bcn_int=%d", bss->beacon_int); | |
3016 | iwe.u.data.length = strlen(buf); | |
3017 | current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, | |
3018 | buf); | |
3019 | ||
3020 | memset(&iwe, 0, sizeof(iwe)); | |
3021 | iwe.cmd = IWEVCUSTOM; | |
3022 | sprintf(buf, "capab=0x%04x", bss->capability); | |
3023 | iwe.u.data.length = strlen(buf); | |
3024 | current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, | |
3025 | buf); | |
3026 | ||
3027 | kfree(buf); | |
3028 | break; | |
3029 | } while (0); | |
3030 | ||
3031 | return current_ev; | |
3032 | } | |
3033 | ||
3034 | ||
3035 | int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len) | |
3036 | { | |
3037 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
3038 | char *current_ev = buf; | |
3039 | char *end_buf = buf + len; | |
3040 | struct ieee80211_sta_bss *bss; | |
3041 | ||
3042 | spin_lock_bh(&local->sta_bss_lock); | |
3043 | list_for_each_entry(bss, &local->sta_bss_list, list) { | |
3044 | if (buf + len - current_ev <= IW_EV_ADDR_LEN) { | |
3045 | spin_unlock_bh(&local->sta_bss_lock); | |
3046 | return -E2BIG; | |
3047 | } | |
3048 | current_ev = ieee80211_sta_scan_result(dev, bss, current_ev, | |
3049 | end_buf); | |
3050 | } | |
3051 | spin_unlock_bh(&local->sta_bss_lock); | |
3052 | return current_ev - buf; | |
3053 | } | |
3054 | ||
3055 | ||
3056 | int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len) | |
3057 | { | |
3058 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
3059 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
3060 | kfree(ifsta->extra_ie); | |
3061 | if (len == 0) { | |
3062 | ifsta->extra_ie = NULL; | |
3063 | ifsta->extra_ie_len = 0; | |
3064 | return 0; | |
3065 | } | |
3066 | ifsta->extra_ie = kmalloc(len, GFP_KERNEL); | |
3067 | if (!ifsta->extra_ie) { | |
3068 | ifsta->extra_ie_len = 0; | |
3069 | return -ENOMEM; | |
3070 | } | |
3071 | memcpy(ifsta->extra_ie, ie, len); | |
3072 | ifsta->extra_ie_len = len; | |
3073 | return 0; | |
3074 | } | |
3075 | ||
3076 | ||
3077 | struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev, | |
3078 | struct sk_buff *skb, u8 *bssid, | |
3079 | u8 *addr) | |
3080 | { | |
3081 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
3082 | struct sta_info *sta; | |
91fa558b | 3083 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); |
0795af57 | 3084 | DECLARE_MAC_BUF(mac); |
f0706e82 JB |
3085 | |
3086 | /* TODO: Could consider removing the least recently used entry and | |
3087 | * allow new one to be added. */ | |
3088 | if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) { | |
3089 | if (net_ratelimit()) { | |
3090 | printk(KERN_DEBUG "%s: No room for a new IBSS STA " | |
0795af57 | 3091 | "entry %s\n", dev->name, print_mac(mac, addr)); |
f0706e82 JB |
3092 | } |
3093 | return NULL; | |
3094 | } | |
3095 | ||
0795af57 | 3096 | printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n", |
dd1cd4c6 | 3097 | wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name); |
f0706e82 JB |
3098 | |
3099 | sta = sta_info_add(local, dev, addr, GFP_ATOMIC); | |
3100 | if (!sta) | |
3101 | return NULL; | |
3102 | ||
3103 | sta->supp_rates = sdata->u.sta.supp_rates_bits; | |
3104 | ||
3105 | rate_control_rate_init(sta, local); | |
3106 | ||
3107 | return sta; /* caller will call sta_info_put() */ | |
3108 | } | |
3109 | ||
3110 | ||
3111 | int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason) | |
3112 | { | |
3113 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
3114 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
3115 | ||
3116 | printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n", | |
3117 | dev->name, reason); | |
3118 | ||
3119 | if (sdata->type != IEEE80211_IF_TYPE_STA && | |
3120 | sdata->type != IEEE80211_IF_TYPE_IBSS) | |
3121 | return -EINVAL; | |
3122 | ||
3123 | ieee80211_send_deauth(dev, ifsta, reason); | |
3124 | ieee80211_set_disassoc(dev, ifsta, 1); | |
3125 | return 0; | |
3126 | } | |
3127 | ||
3128 | ||
3129 | int ieee80211_sta_disassociate(struct net_device *dev, u16 reason) | |
3130 | { | |
3131 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
3132 | struct ieee80211_if_sta *ifsta = &sdata->u.sta; | |
3133 | ||
3134 | printk(KERN_DEBUG "%s: disassociate(reason=%d)\n", | |
3135 | dev->name, reason); | |
3136 | ||
3137 | if (sdata->type != IEEE80211_IF_TYPE_STA) | |
3138 | return -EINVAL; | |
3139 | ||
d6f2da5b | 3140 | if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED)) |
f0706e82 JB |
3141 | return -1; |
3142 | ||
3143 | ieee80211_send_disassoc(dev, ifsta, reason); | |
3144 | ieee80211_set_disassoc(dev, ifsta, 0); | |
3145 | return 0; | |
3146 | } |