blob: baa68575b98a3b534c622fc341a6ab0f60fb414f [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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:
Jiri Bencf0706e82007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#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>
Johannes Bergd0709a62008-02-25 16:27:46 +010027#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
33#include "ieee80211_rate.h"
Michael Buesch47f0c502007-09-27 15:10:44 +020034#include "ieee80211_led.h"
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010035#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
37#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40#define IEEE80211_ASSOC_MAX_TRIES 3
41#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010042#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070043#define IEEE80211_PROBE_INTERVAL (60 * HZ)
44#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45#define IEEE80211_SCAN_INTERVAL (2 * HZ)
46#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47#define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
48
49#define IEEE80211_PROBE_DELAY (HZ / 33)
50#define IEEE80211_CHANNEL_TIME (HZ / 33)
51#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010055#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070056
57#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
60#define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
61
62#define ERP_INFO_USE_PROTECTION BIT(1)
63
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020064/* mgmt header + 1 byte action code */
65#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
66
67#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
68#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
69#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020070#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
71#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020072
Ron Rindjunsky07db2182007-12-25 17:00:33 +020073/* next values represent the buffer size for A-MPDU frame.
74 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
75#define IEEE80211_MIN_AMPDU_BUF 0x8
76#define IEEE80211_MAX_AMPDU_BUF 0x40
77
Jiri Bencf0706e82007-05-05 11:45:53 -070078static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
79 u8 *ssid, size_t ssid_len);
80static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +010081ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -040082 u8 *ssid, u8 ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070083static void ieee80211_rx_bss_put(struct net_device *dev,
84 struct ieee80211_sta_bss *bss);
85static int ieee80211_sta_find_ibss(struct net_device *dev,
86 struct ieee80211_if_sta *ifsta);
87static int ieee80211_sta_wep_configured(struct net_device *dev);
88static int ieee80211_sta_start_scan(struct net_device *dev,
89 u8 *ssid, size_t ssid_len);
90static int ieee80211_sta_config_auth(struct net_device *dev,
91 struct ieee80211_if_sta *ifsta);
92
93
Luis Carlos Coboee385852008-02-23 15:17:11 +010094void ieee802_11_parse_elems(u8 *start, size_t len,
95 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -070096{
97 size_t left = len;
98 u8 *pos = start;
Jiri Bencf0706e82007-05-05 11:45:53 -070099
100 memset(elems, 0, sizeof(*elems));
101
102 while (left >= 2) {
103 u8 id, elen;
104
105 id = *pos++;
106 elen = *pos++;
107 left -= 2;
108
John W. Linville67a4cce2007-10-12 16:40:37 -0400109 if (elen > left)
110 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700111
112 switch (id) {
113 case WLAN_EID_SSID:
114 elems->ssid = pos;
115 elems->ssid_len = elen;
116 break;
117 case WLAN_EID_SUPP_RATES:
118 elems->supp_rates = pos;
119 elems->supp_rates_len = elen;
120 break;
121 case WLAN_EID_FH_PARAMS:
122 elems->fh_params = pos;
123 elems->fh_params_len = elen;
124 break;
125 case WLAN_EID_DS_PARAMS:
126 elems->ds_params = pos;
127 elems->ds_params_len = elen;
128 break;
129 case WLAN_EID_CF_PARAMS:
130 elems->cf_params = pos;
131 elems->cf_params_len = elen;
132 break;
133 case WLAN_EID_TIM:
134 elems->tim = pos;
135 elems->tim_len = elen;
136 break;
137 case WLAN_EID_IBSS_PARAMS:
138 elems->ibss_params = pos;
139 elems->ibss_params_len = elen;
140 break;
141 case WLAN_EID_CHALLENGE:
142 elems->challenge = pos;
143 elems->challenge_len = elen;
144 break;
145 case WLAN_EID_WPA:
146 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
147 pos[2] == 0xf2) {
148 /* Microsoft OUI (00:50:F2) */
149 if (pos[3] == 1) {
150 /* OUI Type 1 - WPA IE */
151 elems->wpa = pos;
152 elems->wpa_len = elen;
153 } else if (elen >= 5 && pos[3] == 2) {
154 if (pos[4] == 0) {
155 elems->wmm_info = pos;
156 elems->wmm_info_len = elen;
157 } else if (pos[4] == 1) {
158 elems->wmm_param = pos;
159 elems->wmm_param_len = elen;
160 }
161 }
162 }
163 break;
164 case WLAN_EID_RSN:
165 elems->rsn = pos;
166 elems->rsn_len = elen;
167 break;
168 case WLAN_EID_ERP_INFO:
169 elems->erp_info = pos;
170 elems->erp_info_len = elen;
171 break;
172 case WLAN_EID_EXT_SUPP_RATES:
173 elems->ext_supp_rates = pos;
174 elems->ext_supp_rates_len = elen;
175 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200176 case WLAN_EID_HT_CAPABILITY:
177 elems->ht_cap_elem = pos;
178 elems->ht_cap_elem_len = elen;
179 break;
180 case WLAN_EID_HT_EXTRA_INFO:
181 elems->ht_info_elem = pos;
182 elems->ht_info_elem_len = elen;
183 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100184 case WLAN_EID_MESH_ID:
185 elems->mesh_id = pos;
186 elems->mesh_id_len = elen;
187 break;
188 case WLAN_EID_MESH_CONFIG:
189 elems->mesh_config = pos;
190 elems->mesh_config_len = elen;
191 break;
192 case WLAN_EID_PEER_LINK:
193 elems->peer_link = pos;
194 elems->peer_link_len = elen;
195 break;
196 case WLAN_EID_PREQ:
197 elems->preq = pos;
198 elems->preq_len = elen;
199 break;
200 case WLAN_EID_PREP:
201 elems->prep = pos;
202 elems->prep_len = elen;
203 break;
204 case WLAN_EID_PERR:
205 elems->perr = pos;
206 elems->perr_len = elen;
207 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700208 default:
Jiri Bencf0706e82007-05-05 11:45:53 -0700209 break;
210 }
211
212 left -= elen;
213 pos += elen;
214 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700215}
216
217
Jiri Bencf0706e82007-05-05 11:45:53 -0700218static int ecw2cw(int ecw)
219{
Johannes Bergac2bf322008-02-14 00:30:35 +0100220 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700221}
222
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100223
224static void ieee80211_sta_def_wmm_params(struct net_device *dev,
225 struct ieee80211_sta_bss *bss,
226 int ibss)
227{
228 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
229 struct ieee80211_local *local = sdata->local;
230 int i, have_higher_than_11mbit = 0;
231
232
233 /* cf. IEEE 802.11 9.2.12 */
234 for (i = 0; i < bss->supp_rates_len; i++)
235 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
236 have_higher_than_11mbit = 1;
237
238 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
239 have_higher_than_11mbit)
240 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
241 else
242 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
243
244
245 if (local->ops->conf_tx) {
246 struct ieee80211_tx_queue_params qparam;
247 int i;
248
249 memset(&qparam, 0, sizeof(qparam));
250
251 qparam.aifs = 2;
252
253 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
254 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
255 qparam.cw_min = 31;
256 else
257 qparam.cw_min = 15;
258
259 qparam.cw_max = 1023;
260 qparam.txop = 0;
261
262 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
263 local->ops->conf_tx(local_to_hw(local),
264 i + IEEE80211_TX_QUEUE_DATA0,
265 &qparam);
266
267 if (ibss) {
268 /* IBSS uses different parameters for Beacon sending */
269 qparam.cw_min++;
270 qparam.cw_min *= 2;
271 qparam.cw_min--;
272 local->ops->conf_tx(local_to_hw(local),
273 IEEE80211_TX_QUEUE_BEACON, &qparam);
274 }
275 }
276}
277
Jiri Bencf0706e82007-05-05 11:45:53 -0700278static void ieee80211_sta_wmm_params(struct net_device *dev,
279 struct ieee80211_if_sta *ifsta,
280 u8 *wmm_param, size_t wmm_param_len)
281{
282 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
283 struct ieee80211_tx_queue_params params;
284 size_t left;
285 int count;
286 u8 *pos;
287
288 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
289 return;
290 count = wmm_param[6] & 0x0f;
291 if (count == ifsta->wmm_last_param_set)
292 return;
293 ifsta->wmm_last_param_set = count;
294
295 pos = wmm_param + 8;
296 left = wmm_param_len - 8;
297
298 memset(&params, 0, sizeof(params));
299
300 if (!local->ops->conf_tx)
301 return;
302
303 local->wmm_acm = 0;
304 for (; left >= 4; left -= 4, pos += 4) {
305 int aci = (pos[0] >> 5) & 0x03;
306 int acm = (pos[0] >> 4) & 0x01;
307 int queue;
308
309 switch (aci) {
310 case 1:
311 queue = IEEE80211_TX_QUEUE_DATA3;
312 if (acm) {
313 local->wmm_acm |= BIT(0) | BIT(3);
314 }
315 break;
316 case 2:
317 queue = IEEE80211_TX_QUEUE_DATA1;
318 if (acm) {
319 local->wmm_acm |= BIT(4) | BIT(5);
320 }
321 break;
322 case 3:
323 queue = IEEE80211_TX_QUEUE_DATA0;
324 if (acm) {
325 local->wmm_acm |= BIT(6) | BIT(7);
326 }
327 break;
328 case 0:
329 default:
330 queue = IEEE80211_TX_QUEUE_DATA2;
331 if (acm) {
332 local->wmm_acm |= BIT(1) | BIT(2);
333 }
334 break;
335 }
336
337 params.aifs = pos[0] & 0x0f;
338 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
339 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100340 params.txop = pos[2] | (pos[3] << 8);
341#ifdef CONFIG_MAC80211_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700342 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100343 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e82007-05-05 11:45:53 -0700344 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100345 params.cw_max, params.txop);
346#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700347 /* TODO: handle ACM (block TX, fallback to next lowest allowed
348 * AC for now) */
349 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
350 printk(KERN_DEBUG "%s: failed to set TX queue "
351 "parameters for queue %d\n", dev->name, queue);
352 }
353 }
354}
355
356
Johannes Berg471b3ef2007-12-28 14:32:58 +0100357static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
358 u8 erp_value)
Daniel Drake56282212007-07-10 19:32:10 +0200359{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100360 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Daniel Drake56282212007-07-10 19:32:10 +0200361 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100362 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
363 bool preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
Joe Perches0795af52007-10-03 17:59:30 -0700364 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100365 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200366
Johannes Berg471b3ef2007-12-28 14:32:58 +0100367 if (use_protection != bss_conf->use_cts_prot) {
Daniel Drake56282212007-07-10 19:32:10 +0200368 if (net_ratelimit()) {
369 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700370 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100371 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200372 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700373 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200374 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100375 bss_conf->use_cts_prot = use_protection;
376 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200377 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200378
Johannes Berg471b3ef2007-12-28 14:32:58 +0100379 if (preamble_mode != bss_conf->use_short_preamble) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200380 if (net_ratelimit()) {
381 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700382 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100383 sdata->dev->name,
Daniel Drake7e9ed182007-07-27 15:43:24 +0200384 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
385 "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700386 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200387 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100388 bss_conf->use_short_preamble = preamble_mode;
389 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200390 }
Daniel Draked9430a32007-07-27 15:43:24 +0200391
Johannes Berg471b3ef2007-12-28 14:32:58 +0100392 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200393}
394
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200395int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
396 struct ieee80211_ht_info *ht_info)
397{
398
399 if (ht_info == NULL)
400 return -EINVAL;
401
402 memset(ht_info, 0, sizeof(*ht_info));
403
404 if (ht_cap_ie) {
405 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
406
407 ht_info->ht_supported = 1;
408 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
409 ht_info->ampdu_factor =
410 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
411 ht_info->ampdu_density =
412 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
413 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
414 } else
415 ht_info->ht_supported = 0;
416
417 return 0;
418}
419
420int ieee80211_ht_addt_info_ie_to_ht_bss_info(
421 struct ieee80211_ht_addt_info *ht_add_info_ie,
422 struct ieee80211_ht_bss_info *bss_info)
423{
424 if (bss_info == NULL)
425 return -EINVAL;
426
427 memset(bss_info, 0, sizeof(*bss_info));
428
429 if (ht_add_info_ie) {
430 u16 op_mode;
431 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
432
433 bss_info->primary_channel = ht_add_info_ie->control_chan;
434 bss_info->bss_cap = ht_add_info_ie->ht_param;
435 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
436 }
437
438 return 0;
439}
Daniel Drake56282212007-07-10 19:32:10 +0200440
Jiri Bencf0706e82007-05-05 11:45:53 -0700441static void ieee80211_sta_send_associnfo(struct net_device *dev,
442 struct ieee80211_if_sta *ifsta)
443{
444 char *buf;
445 size_t len;
446 int i;
447 union iwreq_data wrqu;
448
449 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
450 return;
451
452 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200453 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700454 if (!buf)
455 return;
456
457 len = sprintf(buf, "ASSOCINFO(");
458 if (ifsta->assocreq_ies) {
459 len += sprintf(buf + len, "ReqIEs=");
460 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
461 len += sprintf(buf + len, "%02x",
462 ifsta->assocreq_ies[i]);
463 }
464 }
465 if (ifsta->assocresp_ies) {
466 if (ifsta->assocreq_ies)
467 len += sprintf(buf + len, " ");
468 len += sprintf(buf + len, "RespIEs=");
469 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
470 len += sprintf(buf + len, "%02x",
471 ifsta->assocresp_ies[i]);
472 }
473 }
474 len += sprintf(buf + len, ")");
475
476 if (len > IW_CUSTOM_MAX) {
477 len = sprintf(buf, "ASSOCRESPIE=");
478 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
479 len += sprintf(buf + len, "%02x",
480 ifsta->assocresp_ies[i]);
481 }
482 }
483
484 memset(&wrqu, 0, sizeof(wrqu));
485 wrqu.data.length = len;
486 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
487
488 kfree(buf);
489}
490
491
492static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400493 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200494 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700495{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100496 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
497 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700498 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100499 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700500
Jiri Bencf0706e82007-05-05 11:45:53 -0700501 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200502 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400503
504 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
505
Johannes Berg51fb61e2007-12-19 01:31:27 +0100506 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700507 return;
Daniel Drake56282212007-07-10 19:32:10 +0200508
John W. Linville65c107a2007-10-05 14:23:27 -0400509 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +0100510 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400511 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200512 if (bss) {
513 if (bss->has_erp_value)
Johannes Berg471b3ef2007-12-28 14:32:58 +0100514 changed |= ieee80211_handle_erp_ie(
515 sdata, bss->erp_value);
Daniel Drake56282212007-07-10 19:32:10 +0200516 ieee80211_rx_bss_put(dev, bss);
517 }
518
Jiri Bencf0706e82007-05-05 11:45:53 -0700519 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400520 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700521 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
522 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
523 ieee80211_sta_send_associnfo(dev, ifsta);
524 } else {
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700525 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400526 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700527 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200528 ieee80211_reset_erp_info(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700529 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
530 }
531 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
532 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
533 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200534 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100535
Tomas Winklerb2205252008-01-27 16:18:22 +0200536 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100537 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -0700538}
539
540static void ieee80211_set_disassoc(struct net_device *dev,
541 struct ieee80211_if_sta *ifsta, int deauth)
542{
543 if (deauth)
544 ifsta->auth_tries = 0;
545 ifsta->assoc_tries = 0;
546 ieee80211_set_associated(dev, ifsta, 0);
547}
548
Luis Carlos Coboee385852008-02-23 15:17:11 +0100549void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
550 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700551{
552 struct ieee80211_sub_if_data *sdata;
553 struct ieee80211_tx_packet_data *pkt_data;
554
555 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
556 skb->dev = sdata->local->mdev;
557 skb_set_mac_header(skb, 0);
558 skb_set_network_header(skb, 0);
559 skb_set_transport_header(skb, 0);
560
561 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
562 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
563 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400564 if (!encrypt)
565 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700566
567 dev_queue_xmit(skb);
568}
569
570
571static void ieee80211_send_auth(struct net_device *dev,
572 struct ieee80211_if_sta *ifsta,
573 int transaction, u8 *extra, size_t extra_len,
574 int encrypt)
575{
576 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
577 struct sk_buff *skb;
578 struct ieee80211_mgmt *mgmt;
579
580 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
581 sizeof(*mgmt) + 6 + extra_len);
582 if (!skb) {
583 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
584 "frame\n", dev->name);
585 return;
586 }
587 skb_reserve(skb, local->hw.extra_tx_headroom);
588
589 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
590 memset(mgmt, 0, 24 + 6);
591 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
592 IEEE80211_STYPE_AUTH);
593 if (encrypt)
594 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
595 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
596 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
597 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
598 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
599 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
600 ifsta->auth_transaction = transaction + 1;
601 mgmt->u.auth.status_code = cpu_to_le16(0);
602 if (extra)
603 memcpy(skb_put(skb, extra_len), extra, extra_len);
604
605 ieee80211_sta_tx(dev, skb, encrypt);
606}
607
608
609static void ieee80211_authenticate(struct net_device *dev,
610 struct ieee80211_if_sta *ifsta)
611{
Joe Perches0795af52007-10-03 17:59:30 -0700612 DECLARE_MAC_BUF(mac);
613
Jiri Bencf0706e82007-05-05 11:45:53 -0700614 ifsta->auth_tries++;
615 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700616 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700617 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700618 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700619 ifsta->state = IEEE80211_DISABLED;
620 return;
621 }
622
623 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700624 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
625 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700626
627 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
628
629 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
630}
631
632
633static void ieee80211_send_assoc(struct net_device *dev,
634 struct ieee80211_if_sta *ifsta)
635{
636 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700637 struct sk_buff *skb;
638 struct ieee80211_mgmt *mgmt;
639 u8 *pos, *ies;
640 int i, len;
641 u16 capab;
642 struct ieee80211_sta_bss *bss;
643 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100644 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700645
646 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
647 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
648 ifsta->ssid_len);
649 if (!skb) {
650 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
651 "frame\n", dev->name);
652 return;
653 }
654 skb_reserve(skb, local->hw.extra_tx_headroom);
655
Johannes Berg8318d782008-01-24 19:38:38 +0100656 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
657
Jiri Bencf0706e82007-05-05 11:45:53 -0700658 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100659
660 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
661 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
662 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
663 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
664 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700665 }
Johannes Berg8318d782008-01-24 19:38:38 +0100666
667 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
668 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400669 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700670 if (bss) {
671 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
672 capab |= WLAN_CAPABILITY_PRIVACY;
673 if (bss->wmm_ie) {
674 wmm = 1;
675 }
676 ieee80211_rx_bss_put(dev, bss);
677 }
678
679 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
680 memset(mgmt, 0, 24);
681 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
682 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
683 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
684
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400685 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700686 skb_put(skb, 10);
687 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
688 IEEE80211_STYPE_REASSOC_REQ);
689 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
690 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
691 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
692 ETH_ALEN);
693 } else {
694 skb_put(skb, 4);
695 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
696 IEEE80211_STYPE_ASSOC_REQ);
697 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
698 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
699 }
700
701 /* SSID */
702 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
703 *pos++ = WLAN_EID_SSID;
704 *pos++ = ifsta->ssid_len;
705 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
706
Johannes Berg8318d782008-01-24 19:38:38 +0100707 len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700708 if (len > 8)
709 len = 8;
710 pos = skb_put(skb, len + 2);
711 *pos++ = WLAN_EID_SUPP_RATES;
712 *pos++ = len;
713 for (i = 0; i < len; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100714 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700715 *pos++ = (u8) (rate / 5);
716 }
717
Johannes Berg8318d782008-01-24 19:38:38 +0100718 if (sband->n_bitrates > len) {
719 pos = skb_put(skb, sband->n_bitrates - len + 2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700720 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Johannes Berg8318d782008-01-24 19:38:38 +0100721 *pos++ = sband->n_bitrates - len;
722 for (i = len; i < sband->n_bitrates; i++) {
723 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700724 *pos++ = (u8) (rate / 5);
725 }
726 }
727
728 if (ifsta->extra_ie) {
729 pos = skb_put(skb, ifsta->extra_ie_len);
730 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
731 }
732
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400733 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700734 pos = skb_put(skb, 9);
735 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
736 *pos++ = 7; /* len */
737 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
738 *pos++ = 0x50;
739 *pos++ = 0xf2;
740 *pos++ = 2; /* WME */
741 *pos++ = 0; /* WME info */
742 *pos++ = 1; /* WME ver */
743 *pos++ = 0;
744 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200745 /* wmm support is a must to HT */
Johannes Berg8318d782008-01-24 19:38:38 +0100746 if (wmm && sband->ht_info.ht_supported) {
747 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200748 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
749 *pos++ = WLAN_EID_HT_CAPABILITY;
750 *pos++ = sizeof(struct ieee80211_ht_cap);
751 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
752 memcpy(pos, &tmp, sizeof(u16));
753 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100754 /* TODO: needs a define here for << 2 */
755 *pos++ = sband->ht_info.ampdu_factor |
756 (sband->ht_info.ampdu_density << 2);
757 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200758 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700759
760 kfree(ifsta->assocreq_ies);
761 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200762 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700763 if (ifsta->assocreq_ies)
764 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
765
766 ieee80211_sta_tx(dev, skb, 0);
767}
768
769
770static void ieee80211_send_deauth(struct net_device *dev,
771 struct ieee80211_if_sta *ifsta, u16 reason)
772{
773 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
774 struct sk_buff *skb;
775 struct ieee80211_mgmt *mgmt;
776
777 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
778 if (!skb) {
779 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
780 "frame\n", dev->name);
781 return;
782 }
783 skb_reserve(skb, local->hw.extra_tx_headroom);
784
785 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
786 memset(mgmt, 0, 24);
787 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
788 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
789 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
790 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
791 IEEE80211_STYPE_DEAUTH);
792 skb_put(skb, 2);
793 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
794
795 ieee80211_sta_tx(dev, skb, 0);
796}
797
798
799static void ieee80211_send_disassoc(struct net_device *dev,
800 struct ieee80211_if_sta *ifsta, u16 reason)
801{
802 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
803 struct sk_buff *skb;
804 struct ieee80211_mgmt *mgmt;
805
806 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
807 if (!skb) {
808 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
809 "frame\n", dev->name);
810 return;
811 }
812 skb_reserve(skb, local->hw.extra_tx_headroom);
813
814 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
815 memset(mgmt, 0, 24);
816 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
817 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
818 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
819 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
820 IEEE80211_STYPE_DISASSOC);
821 skb_put(skb, 2);
822 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
823
824 ieee80211_sta_tx(dev, skb, 0);
825}
826
827
828static int ieee80211_privacy_mismatch(struct net_device *dev,
829 struct ieee80211_if_sta *ifsta)
830{
John W. Linville65c107a2007-10-05 14:23:27 -0400831 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700832 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000833 int bss_privacy;
834 int wep_privacy;
835 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700836
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000837 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700838 return 0;
839
Johannes Berg8318d782008-01-24 19:38:38 +0100840 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
841 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400842 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700843 if (!bss)
844 return 0;
845
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000846 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
847 wep_privacy = !!ieee80211_sta_wep_configured(dev);
848 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700849
850 ieee80211_rx_bss_put(dev, bss);
851
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000852 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
853 return 0;
854
855 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700856}
857
858
859static void ieee80211_associate(struct net_device *dev,
860 struct ieee80211_if_sta *ifsta)
861{
Joe Perches0795af52007-10-03 17:59:30 -0700862 DECLARE_MAC_BUF(mac);
863
Jiri Bencf0706e82007-05-05 11:45:53 -0700864 ifsta->assoc_tries++;
865 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700866 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700867 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700868 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700869 ifsta->state = IEEE80211_DISABLED;
870 return;
871 }
872
873 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700874 printk(KERN_DEBUG "%s: associate with AP %s\n",
875 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700876 if (ieee80211_privacy_mismatch(dev, ifsta)) {
877 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
878 "mixed-cell disabled - abort association\n", dev->name);
879 ifsta->state = IEEE80211_DISABLED;
880 return;
881 }
882
883 ieee80211_send_assoc(dev, ifsta);
884
885 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
886}
887
888
889static void ieee80211_associated(struct net_device *dev,
890 struct ieee80211_if_sta *ifsta)
891{
892 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
893 struct sta_info *sta;
894 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700895 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700896
897 /* TODO: start monitoring current AP signal quality and number of
898 * missed beacons. Scan other channels every now and then and search
899 * for better APs. */
900 /* TODO: remove expired BSSes */
901
902 ifsta->state = IEEE80211_ASSOCIATED;
903
Johannes Bergd0709a62008-02-25 16:27:46 +0100904 rcu_read_lock();
905
Jiri Bencf0706e82007-05-05 11:45:53 -0700906 sta = sta_info_get(local, ifsta->bssid);
907 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700908 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
909 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700910 disassoc = 1;
911 } else {
912 disassoc = 0;
913 if (time_after(jiffies,
914 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400915 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700916 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700917 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -0700918 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700919 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700920 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +0100921 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400922 } else
Jiri Bencf0706e82007-05-05 11:45:53 -0700923 ieee80211_send_probe_req(dev, ifsta->bssid,
924 local->scan_ssid,
925 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400926 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700927 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400928 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700929 if (time_after(jiffies, ifsta->last_probe +
930 IEEE80211_PROBE_INTERVAL)) {
931 ifsta->last_probe = jiffies;
932 ieee80211_send_probe_req(dev, ifsta->bssid,
933 ifsta->ssid,
934 ifsta->ssid_len);
935 }
936 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700937 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100938
939 rcu_read_unlock();
940
941 if (disassoc && sta) {
942 synchronize_rcu();
943 rtnl_lock();
944 sta_info_destroy(sta);
945 rtnl_unlock();
946 }
947
Jiri Bencf0706e82007-05-05 11:45:53 -0700948 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -0500949 ifsta->state = IEEE80211_DISABLED;
950 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700951 } else {
952 mod_timer(&ifsta->timer, jiffies +
953 IEEE80211_MONITORING_INTERVAL);
954 }
955}
956
957
958static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
959 u8 *ssid, size_t ssid_len)
960{
961 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +0100962 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700963 struct sk_buff *skb;
964 struct ieee80211_mgmt *mgmt;
965 u8 *pos, *supp_rates, *esupp_rates = NULL;
966 int i;
967
968 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
969 if (!skb) {
970 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
971 "request\n", dev->name);
972 return;
973 }
974 skb_reserve(skb, local->hw.extra_tx_headroom);
975
976 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
977 memset(mgmt, 0, 24);
978 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
979 IEEE80211_STYPE_PROBE_REQ);
980 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
981 if (dst) {
982 memcpy(mgmt->da, dst, ETH_ALEN);
983 memcpy(mgmt->bssid, dst, ETH_ALEN);
984 } else {
985 memset(mgmt->da, 0xff, ETH_ALEN);
986 memset(mgmt->bssid, 0xff, ETH_ALEN);
987 }
988 pos = skb_put(skb, 2 + ssid_len);
989 *pos++ = WLAN_EID_SSID;
990 *pos++ = ssid_len;
991 memcpy(pos, ssid, ssid_len);
992
993 supp_rates = skb_put(skb, 2);
994 supp_rates[0] = WLAN_EID_SUPP_RATES;
995 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100996 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
997
998 for (i = 0; i < sband->n_bitrates; i++) {
999 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07001000 if (esupp_rates) {
1001 pos = skb_put(skb, 1);
1002 esupp_rates[1]++;
1003 } else if (supp_rates[1] == 8) {
1004 esupp_rates = skb_put(skb, 3);
1005 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1006 esupp_rates[1] = 1;
1007 pos = &esupp_rates[2];
1008 } else {
1009 pos = skb_put(skb, 1);
1010 supp_rates[1]++;
1011 }
Johannes Berg8318d782008-01-24 19:38:38 +01001012 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -07001013 }
1014
1015 ieee80211_sta_tx(dev, skb, 0);
1016}
1017
1018
1019static int ieee80211_sta_wep_configured(struct net_device *dev)
1020{
1021 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1022 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001023 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -07001024 return 0;
1025 return 1;
1026}
1027
1028
1029static void ieee80211_auth_completed(struct net_device *dev,
1030 struct ieee80211_if_sta *ifsta)
1031{
1032 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001033 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001034 ieee80211_associate(dev, ifsta);
1035}
1036
1037
1038static void ieee80211_auth_challenge(struct net_device *dev,
1039 struct ieee80211_if_sta *ifsta,
1040 struct ieee80211_mgmt *mgmt,
1041 size_t len)
1042{
1043 u8 *pos;
1044 struct ieee802_11_elems elems;
1045
1046 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1047 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001048 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001049 if (!elems.challenge) {
1050 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1051 "frame\n", dev->name);
1052 return;
1053 }
1054 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1055 elems.challenge_len + 2, 1);
1056}
1057
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001058static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1059 u8 dialog_token, u16 status, u16 policy,
1060 u16 buf_size, u16 timeout)
1061{
1062 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1063 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1064 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1065 struct sk_buff *skb;
1066 struct ieee80211_mgmt *mgmt;
1067 u16 capab;
1068
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001069 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1070 sizeof(mgmt->u.action.u.addba_resp));
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001071 if (!skb) {
1072 printk(KERN_DEBUG "%s: failed to allocate buffer "
1073 "for addba resp frame\n", dev->name);
1074 return;
1075 }
1076
1077 skb_reserve(skb, local->hw.extra_tx_headroom);
1078 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1079 memset(mgmt, 0, 24);
1080 memcpy(mgmt->da, da, ETH_ALEN);
1081 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001082 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001083 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1084 else
1085 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1086 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1087 IEEE80211_STYPE_ACTION);
1088
1089 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1090 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1091 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1092 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1093
1094 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1095 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1096 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1097
1098 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1099 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1100 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1101
1102 ieee80211_sta_tx(dev, skb, 0);
1103
1104 return;
1105}
1106
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001107void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1108 u16 tid, u8 dialog_token, u16 start_seq_num,
1109 u16 agg_size, u16 timeout)
1110{
1111 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1112 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1113 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1114 struct sk_buff *skb;
1115 struct ieee80211_mgmt *mgmt;
1116 u16 capab;
1117
1118 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1119 sizeof(mgmt->u.action.u.addba_req));
1120
1121
1122 if (!skb) {
1123 printk(KERN_ERR "%s: failed to allocate buffer "
1124 "for addba request frame\n", dev->name);
1125 return;
1126 }
1127 skb_reserve(skb, local->hw.extra_tx_headroom);
1128 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1129 memset(mgmt, 0, 24);
1130 memcpy(mgmt->da, da, ETH_ALEN);
1131 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1132 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1133 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1134 else
1135 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1136
1137 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1138 IEEE80211_STYPE_ACTION);
1139
1140 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1141
1142 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1143 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1144
1145 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1146 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1147 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1148 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1149
1150 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1151
1152 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1153 mgmt->u.action.u.addba_req.start_seq_num =
1154 cpu_to_le16(start_seq_num << 4);
1155
1156 ieee80211_sta_tx(dev, skb, 0);
1157}
1158
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001159static void ieee80211_sta_process_addba_request(struct net_device *dev,
1160 struct ieee80211_mgmt *mgmt,
1161 size_t len)
1162{
1163 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001164 struct ieee80211_hw *hw = &local->hw;
1165 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001166 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001167 struct tid_ampdu_rx *tid_agg_rx;
1168 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001169 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001170 int ret = -EOPNOTSUPP;
1171 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001172
Johannes Bergd0709a62008-02-25 16:27:46 +01001173 rcu_read_lock();
1174
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001175 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001176 if (!sta) {
1177 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001178 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001179 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001180
1181 /* extract session parameters from addba request frame */
1182 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1183 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001184 start_seq_num =
1185 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001186
1187 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1188 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1189 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1190 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1191
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001192 status = WLAN_STATUS_REQUEST_DECLINED;
1193
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001194 /* sanity check for incoming parameters:
1195 * check if configuration can support the BA policy
1196 * and if buffer size does not exceeds max value */
1197 if (((ba_policy != 1)
1198 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1199 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1200 status = WLAN_STATUS_INVALID_QOS_PARAM;
1201#ifdef CONFIG_MAC80211_HT_DEBUG
1202 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001203 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001204 "%s on tid %u. policy %d, buffer size %d\n",
1205 print_mac(mac, mgmt->sa), tid, ba_policy,
1206 buf_size);
1207#endif /* CONFIG_MAC80211_HT_DEBUG */
1208 goto end_no_lock;
1209 }
1210 /* determine default buffer size */
1211 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001212 struct ieee80211_supported_band *sband;
1213
1214 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001215 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001216 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001217 }
1218
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001219
1220 /* examine state machine */
1221 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1222
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001223 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001224#ifdef CONFIG_MAC80211_HT_DEBUG
1225 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001226 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001227 "%s on tid %u\n",
1228 print_mac(mac, mgmt->sa), tid);
1229#endif /* CONFIG_MAC80211_HT_DEBUG */
1230 goto end;
1231 }
1232
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001233 /* prepare A-MPDU MLME for Rx aggregation */
1234 sta->ampdu_mlme.tid_rx[tid] =
1235 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1236 if (!sta->ampdu_mlme.tid_rx[tid]) {
1237 if (net_ratelimit())
1238 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1239 tid);
1240 goto end;
1241 }
1242 /* rx timer */
1243 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1244 sta_rx_agg_session_timer_expired;
1245 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1246 (unsigned long)&sta->timer_to_tid[tid];
1247 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1248
1249 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1250
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001251 /* prepare reordering buffer */
1252 tid_agg_rx->reorder_buf =
1253 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001254 if (!tid_agg_rx->reorder_buf) {
1255 if (net_ratelimit())
1256 printk(KERN_ERR "can not allocate reordering buffer "
1257 "to tid %d\n", tid);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001258 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001259 goto end;
1260 }
1261 memset(tid_agg_rx->reorder_buf, 0,
1262 buf_size * sizeof(struct sk_buf *));
1263
1264 if (local->ops->ampdu_action)
1265 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001266 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001267#ifdef CONFIG_MAC80211_HT_DEBUG
1268 printk(KERN_DEBUG "Rx A-MPDU on tid %d result %d", tid, ret);
1269#endif /* CONFIG_MAC80211_HT_DEBUG */
1270
1271 if (ret) {
1272 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001273 kfree(tid_agg_rx);
1274 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001275 goto end;
1276 }
1277
1278 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001279 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001280 tid_agg_rx->dialog_token = dialog_token;
1281 tid_agg_rx->ssn = start_seq_num;
1282 tid_agg_rx->head_seq_num = start_seq_num;
1283 tid_agg_rx->buf_size = buf_size;
1284 tid_agg_rx->timeout = timeout;
1285 tid_agg_rx->stored_mpdu_num = 0;
1286 status = WLAN_STATUS_SUCCESS;
1287end:
1288 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1289
1290end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001291 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1292 dialog_token, status, 1, buf_size, timeout);
1293 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001294}
Jiri Bencf0706e82007-05-05 11:45:53 -07001295
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001296static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1297 struct ieee80211_mgmt *mgmt,
1298 size_t len)
1299{
1300 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1301 struct ieee80211_hw *hw = &local->hw;
1302 struct sta_info *sta;
1303 u16 capab;
1304 u16 tid;
1305 u8 *state;
1306
Johannes Bergd0709a62008-02-25 16:27:46 +01001307 rcu_read_lock();
1308
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001309 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001310 if (!sta) {
1311 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001312 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001313 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001314
1315 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1316 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1317
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001318 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001319
1320 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1321
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001322 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1323 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1324 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1325 "%d\n", *state);
1326 goto addba_resp_exit;
1327 }
1328
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001329 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001330 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001331 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1332#ifdef CONFIG_MAC80211_HT_DEBUG
1333 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1334#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001335 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001336 }
1337
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001338 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001339#ifdef CONFIG_MAC80211_HT_DEBUG
1340 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1341#endif /* CONFIG_MAC80211_HT_DEBUG */
1342 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1343 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001344 if (*state & HT_ADDBA_RECEIVED_MSK)
1345 printk(KERN_DEBUG "double addBA response\n");
1346
1347 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001348 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001349
1350 if (*state == HT_AGG_STATE_OPERATIONAL) {
1351 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1352 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1353 }
1354
1355 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1356 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1357 } else {
1358 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1359
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001360 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001361 /* this will allow the state check in stop_BA_session */
1362 *state = HT_AGG_STATE_OPERATIONAL;
1363 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1364 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1365 WLAN_BACK_INITIATOR);
1366 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001367
1368addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001369 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001370}
1371
1372void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1373 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001374{
1375 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1376 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1377 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1378 struct sk_buff *skb;
1379 struct ieee80211_mgmt *mgmt;
1380 u16 params;
1381
1382 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1383 sizeof(mgmt->u.action.u.delba));
1384
1385 if (!skb) {
1386 printk(KERN_ERR "%s: failed to allocate buffer "
1387 "for delba frame\n", dev->name);
1388 return;
1389 }
1390
1391 skb_reserve(skb, local->hw.extra_tx_headroom);
1392 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1393 memset(mgmt, 0, 24);
1394 memcpy(mgmt->da, da, ETH_ALEN);
1395 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001396 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001397 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1398 else
1399 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1400 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1401 IEEE80211_STYPE_ACTION);
1402
1403 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1404
1405 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1406 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1407 params = (u16)(initiator << 11); /* bit 11 initiator */
1408 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1409
1410 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1411 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1412
1413 ieee80211_sta_tx(dev, skb, 0);
1414}
1415
1416void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1417 u16 initiator, u16 reason)
1418{
1419 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1420 struct ieee80211_hw *hw = &local->hw;
1421 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001422 int ret, i;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001423
Johannes Bergd0709a62008-02-25 16:27:46 +01001424 rcu_read_lock();
1425
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001426 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001427 if (!sta) {
1428 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001429 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001430 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001431
1432 /* check if TID is in operational state */
1433 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001434 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001435 != HT_AGG_STATE_OPERATIONAL) {
1436 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Johannes Bergd0709a62008-02-25 16:27:46 +01001437 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001438 return;
1439 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001440 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001441 HT_AGG_STATE_REQ_STOP_BA_MSK |
1442 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1443 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1444
1445 /* stop HW Rx aggregation. ampdu_action existence
1446 * already verified in session init so we add the BUG_ON */
1447 BUG_ON(!local->ops->ampdu_action);
1448
1449 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001450 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001451 if (ret)
1452 printk(KERN_DEBUG "HW problem - can not stop rx "
1453 "aggergation for tid %d\n", tid);
1454
1455 /* shutdown timer has not expired */
1456 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001457 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001458
1459 /* check if this is a self generated aggregation halt */
1460 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1461 ieee80211_send_delba(dev, ra, tid, 0, reason);
1462
1463 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001464 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1465 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001466 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001467 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1468 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1469 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001470 }
1471 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001472 /* free resources */
1473 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1474 kfree(sta->ampdu_mlme.tid_rx[tid]);
1475 sta->ampdu_mlme.tid_rx[tid] = NULL;
1476 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001477
Johannes Bergd0709a62008-02-25 16:27:46 +01001478 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001479}
1480
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001481
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001482static void ieee80211_sta_process_delba(struct net_device *dev,
1483 struct ieee80211_mgmt *mgmt, size_t len)
1484{
1485 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1486 struct sta_info *sta;
1487 u16 tid, params;
1488 u16 initiator;
1489 DECLARE_MAC_BUF(mac);
1490
Johannes Bergd0709a62008-02-25 16:27:46 +01001491 rcu_read_lock();
1492
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001493 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001494 if (!sta) {
1495 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001496 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001497 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001498
1499 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1500 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1501 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1502
1503#ifdef CONFIG_MAC80211_HT_DEBUG
1504 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001505 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1506 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001507 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001508 mgmt->u.action.u.delba.reason_code);
1509#endif /* CONFIG_MAC80211_HT_DEBUG */
1510
1511 if (initiator == WLAN_BACK_INITIATOR)
1512 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1513 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001514 else { /* WLAN_BACK_RECIPIENT */
1515 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001516 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001517 HT_AGG_STATE_OPERATIONAL;
1518 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1519 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1520 WLAN_BACK_RECIPIENT);
1521 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001522 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001523}
1524
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001525/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001526 * After sending add Block Ack request we activated a timer until
1527 * add Block Ack response will arrive from the recipient.
1528 * If this timer expires sta_addba_resp_timer_expired will be executed.
1529 */
1530void sta_addba_resp_timer_expired(unsigned long data)
1531{
1532 /* not an elegant detour, but there is no choice as the timer passes
1533 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001534 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001535 * array gives the sta through container_of */
1536 u16 tid = *(int *)data;
1537 struct sta_info *temp_sta = container_of((void *)data,
1538 struct sta_info, timer_to_tid[tid]);
1539
1540 struct ieee80211_local *local = temp_sta->local;
1541 struct ieee80211_hw *hw = &local->hw;
1542 struct sta_info *sta;
1543 u8 *state;
1544
Johannes Bergd0709a62008-02-25 16:27:46 +01001545 rcu_read_lock();
1546
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001547 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001548 if (!sta) {
1549 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001550 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001551 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001552
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001553 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001554 /* check if the TID waits for addBA response */
1555 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1556 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1557 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1558 *state = HT_AGG_STATE_IDLE;
1559 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1560 "expecting addBA response there", tid);
1561 goto timer_expired_exit;
1562 }
1563
1564 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1565
1566 /* go through the state check in stop_BA_session */
1567 *state = HT_AGG_STATE_OPERATIONAL;
1568 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1569 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1570 WLAN_BACK_INITIATOR);
1571
1572timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001573 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001574}
1575
1576/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001577 * After accepting the AddBA Request we activated a timer,
1578 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001579 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1580 */
1581void sta_rx_agg_session_timer_expired(unsigned long data)
1582{
1583 /* not an elegant detour, but there is no choice as the timer passes
1584 * only one argument, and verious sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001585 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001586 * array gives the sta through container_of */
1587 u8 *ptid = (u8 *)data;
1588 u8 *timer_to_id = ptid - *ptid;
1589 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1590 timer_to_tid[0]);
1591
1592 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergd0709a62008-02-25 16:27:46 +01001593 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1594 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001595 WLAN_REASON_QSTA_TIMEOUT);
1596}
1597
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001598void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1599{
1600 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1601 int i;
1602
1603 for (i = 0; i < STA_TID_NUM; i++) {
1604 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1605 WLAN_BACK_INITIATOR);
1606 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1607 WLAN_BACK_RECIPIENT,
1608 WLAN_REASON_QSTA_LEAVE_QBSS);
1609 }
1610}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001611
Jiri Bencf0706e82007-05-05 11:45:53 -07001612static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1613 struct ieee80211_if_sta *ifsta,
1614 struct ieee80211_mgmt *mgmt,
1615 size_t len)
1616{
1617 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1618 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001619 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001620
1621 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Berg51fb61e2007-12-19 01:31:27 +01001622 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001623 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001624 "%s, but not in authenticate state - ignored\n",
1625 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001626 return;
1627 }
1628
1629 if (len < 24 + 6) {
1630 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001631 "received from %s - ignored\n",
1632 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001633 return;
1634 }
1635
Johannes Berg51fb61e2007-12-19 01:31:27 +01001636 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001637 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1638 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001639 "unknown AP (SA=%s BSSID=%s) - "
1640 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1641 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001642 return;
1643 }
1644
Johannes Berg51fb61e2007-12-19 01:31:27 +01001645 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001646 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1647 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001648 "unknown BSSID (SA=%s BSSID=%s) - "
1649 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1650 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001651 return;
1652 }
1653
1654 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1655 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1656 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1657
Joe Perches0795af52007-10-03 17:59:30 -07001658 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e82007-05-05 11:45:53 -07001659 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001660 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e82007-05-05 11:45:53 -07001661 auth_transaction, status_code);
1662
Johannes Berg51fb61e2007-12-19 01:31:27 +01001663 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001664 /* IEEE 802.11 standard does not require authentication in IBSS
1665 * networks and most implementations do not seem to use it.
1666 * However, try to reply to authentication attempts if someone
1667 * has actually implemented this.
1668 * TODO: Could implement shared key authentication. */
1669 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1670 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1671 "frame (alg=%d transaction=%d)\n",
1672 dev->name, auth_alg, auth_transaction);
1673 return;
1674 }
1675 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1676 }
1677
1678 if (auth_alg != ifsta->auth_alg ||
1679 auth_transaction != ifsta->auth_transaction) {
1680 printk(KERN_DEBUG "%s: unexpected authentication frame "
1681 "(alg=%d transaction=%d)\n",
1682 dev->name, auth_alg, auth_transaction);
1683 return;
1684 }
1685
1686 if (status_code != WLAN_STATUS_SUCCESS) {
1687 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1688 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1689 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1690 u8 algs[3];
1691 const int num_algs = ARRAY_SIZE(algs);
1692 int i, pos;
1693 algs[0] = algs[1] = algs[2] = 0xff;
1694 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1695 algs[0] = WLAN_AUTH_OPEN;
1696 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1697 algs[1] = WLAN_AUTH_SHARED_KEY;
1698 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1699 algs[2] = WLAN_AUTH_LEAP;
1700 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1701 pos = 0;
1702 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1703 pos = 1;
1704 else
1705 pos = 2;
1706 for (i = 0; i < num_algs; i++) {
1707 pos++;
1708 if (pos >= num_algs)
1709 pos = 0;
1710 if (algs[pos] == ifsta->auth_alg ||
1711 algs[pos] == 0xff)
1712 continue;
1713 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1714 !ieee80211_sta_wep_configured(dev))
1715 continue;
1716 ifsta->auth_alg = algs[pos];
1717 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1718 "next try\n",
1719 dev->name, ifsta->auth_alg);
1720 break;
1721 }
1722 }
1723 return;
1724 }
1725
1726 switch (ifsta->auth_alg) {
1727 case WLAN_AUTH_OPEN:
1728 case WLAN_AUTH_LEAP:
1729 ieee80211_auth_completed(dev, ifsta);
1730 break;
1731 case WLAN_AUTH_SHARED_KEY:
1732 if (ifsta->auth_transaction == 4)
1733 ieee80211_auth_completed(dev, ifsta);
1734 else
1735 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1736 break;
1737 }
1738}
1739
1740
1741static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1742 struct ieee80211_if_sta *ifsta,
1743 struct ieee80211_mgmt *mgmt,
1744 size_t len)
1745{
1746 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001747 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001748
1749 if (len < 24 + 2) {
1750 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001751 "received from %s - ignored\n",
1752 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001753 return;
1754 }
1755
1756 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1757 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001758 "unknown AP (SA=%s BSSID=%s) - "
1759 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1760 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001761 return;
1762 }
1763
1764 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1765
Joe Perches0795af52007-10-03 17:59:30 -07001766 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001767 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001768 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001769
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001770 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001771 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1772 }
1773
1774 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1775 ifsta->state == IEEE80211_ASSOCIATE ||
1776 ifsta->state == IEEE80211_ASSOCIATED) {
1777 ifsta->state = IEEE80211_AUTHENTICATE;
1778 mod_timer(&ifsta->timer, jiffies +
1779 IEEE80211_RETRY_AUTH_INTERVAL);
1780 }
1781
1782 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001783 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001784}
1785
1786
1787static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1788 struct ieee80211_if_sta *ifsta,
1789 struct ieee80211_mgmt *mgmt,
1790 size_t len)
1791{
1792 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001793 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001794
1795 if (len < 24 + 2) {
1796 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001797 "received from %s - ignored\n",
1798 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001799 return;
1800 }
1801
1802 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1803 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001804 "unknown AP (SA=%s BSSID=%s) - "
1805 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1806 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001807 return;
1808 }
1809
1810 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1811
Joe Perches0795af52007-10-03 17:59:30 -07001812 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001813 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001814 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001815
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001816 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001817 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1818
1819 if (ifsta->state == IEEE80211_ASSOCIATED) {
1820 ifsta->state = IEEE80211_ASSOCIATE;
1821 mod_timer(&ifsta->timer, jiffies +
1822 IEEE80211_RETRY_AUTH_INTERVAL);
1823 }
1824
1825 ieee80211_set_disassoc(dev, ifsta, 0);
1826}
1827
1828
Johannes Berg471b3ef2007-12-28 14:32:58 +01001829static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001830 struct ieee80211_if_sta *ifsta,
1831 struct ieee80211_mgmt *mgmt,
1832 size_t len,
1833 int reassoc)
1834{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001835 struct ieee80211_local *local = sdata->local;
1836 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01001837 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001838 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001839 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001840 u16 capab_info, status_code, aid;
1841 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001842 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001843 u8 *pos;
1844 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001845 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001846 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001847
1848 /* AssocResp and ReassocResp have identical structure, so process both
1849 * of them in this function. */
1850
1851 if (ifsta->state != IEEE80211_ASSOCIATE) {
1852 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001853 "%s, but not in associate state - ignored\n",
1854 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001855 return;
1856 }
1857
1858 if (len < 24 + 6) {
1859 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001860 "received from %s - ignored\n",
1861 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001862 return;
1863 }
1864
1865 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1866 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001867 "unknown AP (SA=%s BSSID=%s) - "
1868 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1869 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001870 return;
1871 }
1872
1873 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1874 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1875 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001876
Joe Perches0795af52007-10-03 17:59:30 -07001877 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001878 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001879 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001880 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001881
1882 if (status_code != WLAN_STATUS_SUCCESS) {
1883 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1884 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001885 /* if this was a reassociation, ensure we try a "full"
1886 * association next time. This works around some broken APs
1887 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001888 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07001889 return;
1890 }
1891
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001892 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1893 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1894 "set\n", dev->name, aid);
1895 aid &= ~(BIT(15) | BIT(14));
1896
Jiri Bencf0706e82007-05-05 11:45:53 -07001897 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001898 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001899
1900 if (!elems.supp_rates) {
1901 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1902 dev->name);
1903 return;
1904 }
1905
1906 printk(KERN_DEBUG "%s: associated\n", dev->name);
1907 ifsta->aid = aid;
1908 ifsta->ap_capab = capab_info;
1909
1910 kfree(ifsta->assocresp_ies);
1911 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001912 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001913 if (ifsta->assocresp_ies)
1914 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1915
Johannes Bergd0709a62008-02-25 16:27:46 +01001916 rcu_read_lock();
1917
Jiri Bencf0706e82007-05-05 11:45:53 -07001918 /* Add STA entry for the AP */
1919 sta = sta_info_get(local, ifsta->bssid);
1920 if (!sta) {
1921 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01001922 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01001923
Johannes Berg73651ee2008-02-25 16:27:47 +01001924 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1925 if (!sta) {
1926 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1927 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01001928 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001929 return;
1930 }
John W. Linville65c107a2007-10-05 14:23:27 -04001931 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001932 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001933 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001934 if (bss) {
1935 sta->last_rssi = bss->rssi;
1936 sta->last_signal = bss->signal;
1937 sta->last_noise = bss->noise;
1938 ieee80211_rx_bss_put(dev, bss);
1939 }
Johannes Berg73651ee2008-02-25 16:27:47 +01001940
1941 err = sta_info_insert(sta);
1942 if (err) {
1943 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1944 " the AP (error %d)\n", dev->name, err);
1945 sta_info_destroy(sta);
1946 rcu_read_unlock();
1947 return;
1948 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001949 }
1950
Johannes Berg73651ee2008-02-25 16:27:47 +01001951 /*
1952 * FIXME: Do we really need to update the sta_info's information here?
1953 * We already know about the AP (we found it in our list) so it
1954 * should already be filled with the right info, no?
1955 * As is stands, all this is racy because typically we assume
1956 * the information that is filled in here (except flags) doesn't
1957 * change while a STA structure is alive. As such, it should move
1958 * to between the sta_info_alloc() and sta_info_insert() above.
1959 */
1960
Johannes Berg238814f2008-01-28 17:19:37 +01001961 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1962 WLAN_STA_AUTHORIZED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001963
1964 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001965 basic_rates = 0;
1966 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1967
Jiri Bencf0706e82007-05-05 11:45:53 -07001968 for (i = 0; i < elems.supp_rates_len; i++) {
1969 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001970
1971 if (rate > 110)
1972 have_higher_than_11mbit = true;
1973
1974 for (j = 0; j < sband->n_bitrates; j++) {
1975 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001976 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001977 if (elems.supp_rates[i] & 0x80)
1978 basic_rates |= BIT(j);
1979 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001980 }
Johannes Berg8318d782008-01-24 19:38:38 +01001981
Jiri Bencf0706e82007-05-05 11:45:53 -07001982 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1983 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001984
1985 if (rate > 110)
1986 have_higher_than_11mbit = true;
1987
1988 for (j = 0; j < sband->n_bitrates; j++) {
1989 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001990 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001991 if (elems.ext_supp_rates[i] & 0x80)
1992 basic_rates |= BIT(j);
1993 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001994 }
Johannes Berg8318d782008-01-24 19:38:38 +01001995
1996 sta->supp_rates[local->hw.conf.channel->band] = rates;
1997 sdata->basic_rates = basic_rates;
1998
1999 /* cf. IEEE 802.11 9.2.12 */
2000 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2001 have_higher_than_11mbit)
2002 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2003 else
2004 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002005
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002006 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2007 local->ops->conf_ht) {
2008 struct ieee80211_ht_bss_info bss_info;
2009
2010 ieee80211_ht_cap_ie_to_ht_info(
2011 (struct ieee80211_ht_cap *)
2012 elems.ht_cap_elem, &sta->ht_info);
2013 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2014 (struct ieee80211_ht_addt_info *)
2015 elems.ht_info_elem, &bss_info);
2016 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
2017 }
2018
Jiri Bencf0706e82007-05-05 11:45:53 -07002019 rate_control_rate_init(sta, local);
2020
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002021 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002022 sta->flags |= WLAN_STA_WME;
Johannes Berge5f98f22008-03-05 20:39:31 +01002023 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002024 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2025 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002026 } else
2027 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002028
Johannes Berg8318d782008-01-24 19:38:38 +01002029 /* set AID, ieee80211_set_associated() will tell the driver */
2030 bss_conf->aid = aid;
2031 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002032
Jiri Bencf0706e82007-05-05 11:45:53 -07002033 ieee80211_associated(dev, ifsta);
2034}
2035
2036
2037/* Caller must hold local->sta_bss_lock */
2038static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2039 struct ieee80211_sta_bss *bss)
2040{
2041 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002042 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002043
2044 if (bss_mesh_cfg(bss))
2045 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2046 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002047 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002048 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002049
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002050 bss->hnext = local->sta_bss_hash[hash_idx];
2051 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002052}
2053
2054
2055/* Caller must hold local->sta_bss_lock */
2056static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2057 struct ieee80211_sta_bss *bss)
2058{
2059 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2060 struct ieee80211_sta_bss *b, *prev = NULL;
2061 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2062 while (b) {
2063 if (b == bss) {
2064 if (!prev)
2065 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2066 bss->hnext;
2067 else
2068 prev->hnext = bss->hnext;
2069 break;
2070 }
2071 prev = b;
2072 b = b->hnext;
2073 }
2074}
2075
2076
2077static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002078ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002079 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002080{
2081 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2082 struct ieee80211_sta_bss *bss;
2083
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002084 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002085 if (!bss)
2086 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002087 atomic_inc(&bss->users);
2088 atomic_inc(&bss->users);
2089 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002090 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002091 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2092 memcpy(bss->ssid, ssid, ssid_len);
2093 bss->ssid_len = ssid_len;
2094 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002095
2096 spin_lock_bh(&local->sta_bss_lock);
2097 /* TODO: order by RSSI? */
2098 list_add_tail(&bss->list, &local->sta_bss_list);
2099 __ieee80211_rx_bss_hash_add(dev, bss);
2100 spin_unlock_bh(&local->sta_bss_lock);
2101 return bss;
2102}
2103
Jiri Bencf0706e82007-05-05 11:45:53 -07002104static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002105ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002106 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002107{
2108 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2109 struct ieee80211_sta_bss *bss;
2110
2111 spin_lock_bh(&local->sta_bss_lock);
2112 bss = local->sta_bss_hash[STA_HASH(bssid)];
2113 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002114 if (!bss_mesh_cfg(bss) &&
2115 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002116 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002117 bss->ssid_len == ssid_len &&
2118 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002119 atomic_inc(&bss->users);
2120 break;
2121 }
2122 bss = bss->hnext;
2123 }
2124 spin_unlock_bh(&local->sta_bss_lock);
2125 return bss;
2126}
2127
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002128#ifdef CONFIG_MAC80211_MESH
2129static struct ieee80211_sta_bss *
2130ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2131 u8 *mesh_cfg, int freq)
2132{
2133 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2134 struct ieee80211_sta_bss *bss;
2135
2136 spin_lock_bh(&local->sta_bss_lock);
2137 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2138 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002139 if (bss_mesh_cfg(bss) &&
2140 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002141 bss->freq == freq &&
2142 mesh_id_len == bss->mesh_id_len &&
2143 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2144 mesh_id_len))) {
2145 atomic_inc(&bss->users);
2146 break;
2147 }
2148 bss = bss->hnext;
2149 }
2150 spin_unlock_bh(&local->sta_bss_lock);
2151 return bss;
2152}
2153
2154static struct ieee80211_sta_bss *
2155ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2156 u8 *mesh_cfg, int freq)
2157{
2158 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2159 struct ieee80211_sta_bss *bss;
2160
2161 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2162 if (!bss)
2163 return NULL;
2164
2165 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2166 if (!bss->mesh_cfg) {
2167 kfree(bss);
2168 return NULL;
2169 }
2170
2171 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2172 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2173 if (!bss->mesh_id) {
2174 kfree(bss->mesh_cfg);
2175 kfree(bss);
2176 return NULL;
2177 }
2178 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2179 }
2180
2181 atomic_inc(&bss->users);
2182 atomic_inc(&bss->users);
2183 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2184 bss->mesh_id_len = mesh_id_len;
2185 bss->freq = freq;
2186 spin_lock_bh(&local->sta_bss_lock);
2187 /* TODO: order by RSSI? */
2188 list_add_tail(&bss->list, &local->sta_bss_list);
2189 __ieee80211_rx_bss_hash_add(dev, bss);
2190 spin_unlock_bh(&local->sta_bss_lock);
2191 return bss;
2192}
2193#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002194
2195static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2196{
2197 kfree(bss->wpa_ie);
2198 kfree(bss->rsn_ie);
2199 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002200 kfree(bss->ht_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002201 kfree(bss_mesh_id(bss));
2202 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002203 kfree(bss);
2204}
2205
2206
2207static void ieee80211_rx_bss_put(struct net_device *dev,
2208 struct ieee80211_sta_bss *bss)
2209{
2210 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2211 if (!atomic_dec_and_test(&bss->users))
2212 return;
2213
2214 spin_lock_bh(&local->sta_bss_lock);
2215 __ieee80211_rx_bss_hash_del(dev, bss);
2216 list_del(&bss->list);
2217 spin_unlock_bh(&local->sta_bss_lock);
2218 ieee80211_rx_bss_free(bss);
2219}
2220
2221
2222void ieee80211_rx_bss_list_init(struct net_device *dev)
2223{
2224 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2225 spin_lock_init(&local->sta_bss_lock);
2226 INIT_LIST_HEAD(&local->sta_bss_list);
2227}
2228
2229
2230void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2231{
2232 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2233 struct ieee80211_sta_bss *bss, *tmp;
2234
2235 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2236 ieee80211_rx_bss_put(dev, bss);
2237}
2238
2239
Bruno Randolfa6072682008-02-18 11:21:15 +09002240static int ieee80211_sta_join_ibss(struct net_device *dev,
2241 struct ieee80211_if_sta *ifsta,
2242 struct ieee80211_sta_bss *bss)
2243{
2244 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2245 int res, rates, i, j;
2246 struct sk_buff *skb;
2247 struct ieee80211_mgmt *mgmt;
2248 struct ieee80211_tx_control control;
2249 struct rate_selection ratesel;
2250 u8 *pos;
2251 struct ieee80211_sub_if_data *sdata;
2252 struct ieee80211_supported_band *sband;
2253
2254 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2255
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002256 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2257
Bruno Randolfa6072682008-02-18 11:21:15 +09002258 /* Remove possible STA entries from other IBSS networks. */
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002259 sta_info_flush(local, sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002260
2261 if (local->ops->reset_tsf) {
2262 /* Reset own TSF to allow time synchronization work. */
2263 local->ops->reset_tsf(local_to_hw(local));
2264 }
2265 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2266 res = ieee80211_if_config(dev);
2267 if (res)
2268 return res;
2269
2270 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2271
Bruno Randolfa6072682008-02-18 11:21:15 +09002272 sdata->drop_unencrypted = bss->capability &
2273 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2274
2275 res = ieee80211_set_freq(local, bss->freq);
2276
2277 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2278 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2279 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2280 return -1;
2281 }
2282
2283 /* Set beacon template */
2284 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2285 do {
2286 if (!skb)
2287 break;
2288
2289 skb_reserve(skb, local->hw.extra_tx_headroom);
2290
2291 mgmt = (struct ieee80211_mgmt *)
2292 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2293 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2294 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2295 IEEE80211_STYPE_BEACON);
2296 memset(mgmt->da, 0xff, ETH_ALEN);
2297 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2298 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2299 mgmt->u.beacon.beacon_int =
2300 cpu_to_le16(local->hw.conf.beacon_int);
2301 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2302
2303 pos = skb_put(skb, 2 + ifsta->ssid_len);
2304 *pos++ = WLAN_EID_SSID;
2305 *pos++ = ifsta->ssid_len;
2306 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2307
2308 rates = bss->supp_rates_len;
2309 if (rates > 8)
2310 rates = 8;
2311 pos = skb_put(skb, 2 + rates);
2312 *pos++ = WLAN_EID_SUPP_RATES;
2313 *pos++ = rates;
2314 memcpy(pos, bss->supp_rates, rates);
2315
2316 if (bss->band == IEEE80211_BAND_2GHZ) {
2317 pos = skb_put(skb, 2 + 1);
2318 *pos++ = WLAN_EID_DS_PARAMS;
2319 *pos++ = 1;
2320 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2321 }
2322
2323 pos = skb_put(skb, 2 + 2);
2324 *pos++ = WLAN_EID_IBSS_PARAMS;
2325 *pos++ = 2;
2326 /* FIX: set ATIM window based on scan results */
2327 *pos++ = 0;
2328 *pos++ = 0;
2329
2330 if (bss->supp_rates_len > 8) {
2331 rates = bss->supp_rates_len - 8;
2332 pos = skb_put(skb, 2 + rates);
2333 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2334 *pos++ = rates;
2335 memcpy(pos, &bss->supp_rates[8], rates);
2336 }
2337
2338 memset(&control, 0, sizeof(control));
2339 rate_control_get_rate(dev, sband, skb, &ratesel);
2340 if (!ratesel.rate) {
2341 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2342 "for IBSS beacon\n", dev->name);
2343 break;
2344 }
2345 control.vif = &sdata->vif;
2346 control.tx_rate = ratesel.rate;
2347 if (sdata->bss_conf.use_short_preamble &&
2348 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2349 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2350 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2351 control.flags |= IEEE80211_TXCTL_NO_ACK;
2352 control.retry_limit = 1;
2353
2354 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2355 if (ifsta->probe_resp) {
2356 mgmt = (struct ieee80211_mgmt *)
2357 ifsta->probe_resp->data;
2358 mgmt->frame_control =
2359 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2360 IEEE80211_STYPE_PROBE_RESP);
2361 } else {
2362 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2363 "template for IBSS\n", dev->name);
2364 }
2365
2366 if (local->ops->beacon_update &&
2367 local->ops->beacon_update(local_to_hw(local),
2368 skb, &control) == 0) {
2369 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2370 "template\n", dev->name);
2371 skb = NULL;
2372 }
2373
2374 rates = 0;
2375 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2376 for (i = 0; i < bss->supp_rates_len; i++) {
2377 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2378 for (j = 0; j < sband->n_bitrates; j++)
2379 if (sband->bitrates[j].bitrate == bitrate)
2380 rates |= BIT(j);
2381 }
2382 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Vladimir Koutnye2839d82008-03-18 21:14:07 +01002383
2384 ieee80211_sta_def_wmm_params(dev, bss, 1);
Bruno Randolfa6072682008-02-18 11:21:15 +09002385 } while (0);
2386
2387 if (skb) {
2388 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2389 "template\n", dev->name);
2390 dev_kfree_skb(skb);
2391 }
2392
2393 ifsta->state = IEEE80211_IBSS_JOINED;
2394 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2395
2396 ieee80211_rx_bss_put(dev, bss);
2397
2398 return res;
2399}
2400
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002401u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2402 struct ieee802_11_elems *elems,
2403 enum ieee80211_band band)
2404{
2405 struct ieee80211_supported_band *sband;
2406 struct ieee80211_rate *bitrates;
2407 size_t num_rates;
2408 u64 supp_rates;
2409 int i, j;
2410 sband = local->hw.wiphy->bands[band];
2411
2412 if (!sband) {
2413 WARN_ON(1);
2414 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2415 }
2416
2417 bitrates = sband->bitrates;
2418 num_rates = sband->n_bitrates;
2419 supp_rates = 0;
2420 for (i = 0; i < elems->supp_rates_len +
2421 elems->ext_supp_rates_len; i++) {
2422 u8 rate = 0;
2423 int own_rate;
2424 if (i < elems->supp_rates_len)
2425 rate = elems->supp_rates[i];
2426 else if (elems->ext_supp_rates)
2427 rate = elems->ext_supp_rates
2428 [i - elems->supp_rates_len];
2429 own_rate = 5 * (rate & 0x7f);
2430 for (j = 0; j < num_rates; j++)
2431 if (bitrates[j].bitrate == own_rate)
2432 supp_rates |= BIT(j);
2433 }
2434 return supp_rates;
2435}
2436
Bruno Randolfa6072682008-02-18 11:21:15 +09002437
Jiri Bencf0706e82007-05-05 11:45:53 -07002438static void ieee80211_rx_bss_info(struct net_device *dev,
2439 struct ieee80211_mgmt *mgmt,
2440 size_t len,
2441 struct ieee80211_rx_status *rx_status,
2442 int beacon)
2443{
2444 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2445 struct ieee802_11_elems elems;
2446 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002447 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002448 struct ieee80211_sta_bss *bss;
2449 struct sta_info *sta;
2450 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002451 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002452 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002453 DECLARE_MAC_BUF(mac);
2454 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002455
2456 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2457 return; /* ignore ProbeResp to foreign address */
2458
2459#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002460 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07002461 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002462 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002463#endif
2464
2465 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2466 if (baselen > len)
2467 return;
2468
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002469 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
John W. Linville67a4cce2007-10-12 16:40:37 -04002470 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002471
Johannes Berg902acc72008-02-23 15:17:19 +01002472 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2473 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2474 u64 rates = ieee80211_sta_get_rates(local, &elems,
2475 rx_status->band);
2476
2477 mesh_neighbour_update(mgmt->sa, rates, dev,
2478 mesh_peer_accepts_plinks(&elems, dev));
2479 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002480
Johannes Bergd0709a62008-02-25 16:27:46 +01002481 rcu_read_lock();
2482
Johannes Berg51fb61e2007-12-19 01:31:27 +01002483 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002484 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2485 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002486 u64 prev_rates;
2487 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2488 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002489
Johannes Berg8318d782008-01-24 19:38:38 +01002490 prev_rates = sta->supp_rates[rx_status->band];
2491 sta->supp_rates[rx_status->band] &= supp_rates;
2492 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002493 /* No matching rates - this should not really happen.
2494 * Make sure that at least one rate is marked
2495 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002496 sta->supp_rates[rx_status->band] =
2497 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002498 }
Johannes Berg8318d782008-01-24 19:38:38 +01002499 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002500 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002501 "%s based on beacon info (0x%llx & 0x%llx -> "
2502 "0x%llx)\n",
2503 dev->name, print_mac(mac, sta->addr),
2504 (unsigned long long) prev_rates,
2505 (unsigned long long) supp_rates,
2506 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002507 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002508 }
2509
Johannes Bergd0709a62008-02-25 16:27:46 +01002510 rcu_read_unlock();
2511
Jiri Bencf0706e82007-05-05 11:45:53 -07002512 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002513 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002514 else
Johannes Berg8318d782008-01-24 19:38:38 +01002515 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002516
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002517 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2518
2519 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2520 return;
2521
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002522#ifdef CONFIG_MAC80211_MESH
2523 if (elems.mesh_config)
2524 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2525 elems.mesh_id_len, elems.mesh_config, freq);
2526 else
2527#endif
2528 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002529 elems.ssid, elems.ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002530 if (!bss) {
2531#ifdef CONFIG_MAC80211_MESH
2532 if (elems.mesh_config)
2533 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
2534 elems.mesh_id_len, elems.mesh_config, freq);
2535 else
2536#endif
2537 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2538 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002539 if (!bss)
2540 return;
2541 } else {
2542#if 0
2543 /* TODO: order by RSSI? */
2544 spin_lock_bh(&local->sta_bss_lock);
2545 list_move_tail(&bss->list, &local->sta_bss_list);
2546 spin_unlock_bh(&local->sta_bss_lock);
2547#endif
2548 }
2549
Johannes Berg8318d782008-01-24 19:38:38 +01002550 bss->band = rx_status->band;
2551
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002552 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2553 bss->probe_resp && beacon) {
2554 /* STA mode:
2555 * Do not allow beacon to override data from Probe Response. */
Jiri Bencf0706e82007-05-05 11:45:53 -07002556 ieee80211_rx_bss_put(dev, bss);
2557 return;
2558 }
2559
Daniel Drake56282212007-07-10 19:32:10 +02002560 /* save the ERP value so that it is available at association time */
2561 if (elems.erp_info && elems.erp_info_len >= 1) {
2562 bss->erp_value = elems.erp_info[0];
2563 bss->has_erp_value = 1;
2564 }
2565
Jiri Bencf0706e82007-05-05 11:45:53 -07002566 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2567 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002568
2569 bss->supp_rates_len = 0;
2570 if (elems.supp_rates) {
2571 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2572 if (clen > elems.supp_rates_len)
2573 clen = elems.supp_rates_len;
2574 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2575 clen);
2576 bss->supp_rates_len += clen;
2577 }
2578 if (elems.ext_supp_rates) {
2579 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2580 if (clen > elems.ext_supp_rates_len)
2581 clen = elems.ext_supp_rates_len;
2582 memcpy(&bss->supp_rates[bss->supp_rates_len],
2583 elems.ext_supp_rates, clen);
2584 bss->supp_rates_len += clen;
2585 }
2586
2587 if (elems.wpa &&
2588 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2589 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2590 kfree(bss->wpa_ie);
2591 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2592 if (bss->wpa_ie) {
2593 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2594 bss->wpa_ie_len = elems.wpa_len + 2;
2595 } else
2596 bss->wpa_ie_len = 0;
2597 } else if (!elems.wpa && bss->wpa_ie) {
2598 kfree(bss->wpa_ie);
2599 bss->wpa_ie = NULL;
2600 bss->wpa_ie_len = 0;
2601 }
2602
2603 if (elems.rsn &&
2604 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2605 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2606 kfree(bss->rsn_ie);
2607 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2608 if (bss->rsn_ie) {
2609 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2610 bss->rsn_ie_len = elems.rsn_len + 2;
2611 } else
2612 bss->rsn_ie_len = 0;
2613 } else if (!elems.rsn && bss->rsn_ie) {
2614 kfree(bss->rsn_ie);
2615 bss->rsn_ie = NULL;
2616 bss->rsn_ie_len = 0;
2617 }
2618
2619 if (elems.wmm_param &&
2620 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2621 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2622 kfree(bss->wmm_ie);
2623 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2624 if (bss->wmm_ie) {
2625 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2626 elems.wmm_param_len + 2);
2627 bss->wmm_ie_len = elems.wmm_param_len + 2;
2628 } else
2629 bss->wmm_ie_len = 0;
2630 } else if (!elems.wmm_param && bss->wmm_ie) {
2631 kfree(bss->wmm_ie);
2632 bss->wmm_ie = NULL;
2633 bss->wmm_ie_len = 0;
2634 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002635 if (elems.ht_cap_elem &&
2636 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2637 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2638 kfree(bss->ht_ie);
2639 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2640 if (bss->ht_ie) {
2641 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2642 elems.ht_cap_elem_len + 2);
2643 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2644 } else
2645 bss->ht_ie_len = 0;
2646 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2647 kfree(bss->ht_ie);
2648 bss->ht_ie = NULL;
2649 bss->ht_ie_len = 0;
2650 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002651
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002652 bss->timestamp = beacon_timestamp;
Jiri Bencf0706e82007-05-05 11:45:53 -07002653 bss->last_update = jiffies;
2654 bss->rssi = rx_status->ssi;
2655 bss->signal = rx_status->signal;
2656 bss->noise = rx_status->noise;
2657 if (!beacon)
2658 bss->probe_resp++;
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002659
2660 /* check if we need to merge IBSS */
2661 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2662 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002663 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002664 bss->freq == local->oper_channel->center_freq &&
2665 elems.ssid_len == sdata->u.sta.ssid_len &&
2666 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2667 if (rx_status->flag & RX_FLAG_TSFT) {
2668 /* in order for correct IBSS merging we need mactime
2669 *
2670 * since mactime is defined as the time the first data
2671 * symbol of the frame hits the PHY, and the timestamp
2672 * of the beacon is defined as "the time that the data
2673 * symbol containing the first bit of the timestamp is
2674 * transmitted to the PHY plus the transmitting STA’s
2675 * delays through its local PHY from the MAC-PHY
2676 * interface to its interface with the WM"
2677 * (802.11 11.1.2) - equals the time this bit arrives at
2678 * the receiver - we have to take into account the
2679 * offset between the two.
2680 * e.g: at 1 MBit that means mactime is 192 usec earlier
2681 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2682 */
2683 int rate = local->hw.wiphy->bands[rx_status->band]->
2684 bitrates[rx_status->rate_idx].bitrate;
2685 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2686 } else if (local && local->ops && local->ops->get_tsf)
2687 /* second best option: get current TSF */
2688 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2689 else
2690 /* can't merge without knowing the TSF */
2691 rx_timestamp = -1LLU;
2692#ifdef CONFIG_MAC80211_IBSS_DEBUG
2693 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2694 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2695 print_mac(mac, mgmt->sa),
2696 print_mac(mac2, mgmt->bssid),
2697 (unsigned long long)rx_timestamp,
2698 (unsigned long long)beacon_timestamp,
2699 (unsigned long long)(rx_timestamp - beacon_timestamp),
2700 jiffies);
2701#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2702 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002703#ifndef CONFIG_MAC80211_IBSS_DEBUG
Pavel Roskind97cf012008-02-21 11:33:58 -05002704 if (net_ratelimit())
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002705#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002706 printk(KERN_DEBUG "%s: beacon TSF higher than "
2707 "local TSF - IBSS merge with BSSID %s\n",
2708 dev->name, print_mac(mac, mgmt->bssid));
2709 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2710 ieee80211_ibss_add_sta(dev, NULL,
2711 mgmt->bssid, mgmt->sa);
2712 }
2713 }
2714
Jiri Bencf0706e82007-05-05 11:45:53 -07002715 ieee80211_rx_bss_put(dev, bss);
2716}
2717
2718
2719static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2720 struct ieee80211_mgmt *mgmt,
2721 size_t len,
2722 struct ieee80211_rx_status *rx_status)
2723{
2724 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2725}
2726
2727
2728static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2729 struct ieee80211_mgmt *mgmt,
2730 size_t len,
2731 struct ieee80211_rx_status *rx_status)
2732{
Jiri Bencf0706e82007-05-05 11:45:53 -07002733 struct ieee80211_sub_if_data *sdata;
2734 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002735 size_t baselen;
2736 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002737 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2738 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002739 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002740
2741 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2742
2743 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002744 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002745 return;
2746 ifsta = &sdata->u.sta;
2747
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002748 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002749 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2750 return;
2751
2752 /* Process beacon from the current BSS */
2753 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2754 if (baselen > len)
2755 return;
2756
John W. Linville67a4cce2007-10-12 16:40:37 -04002757 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002758
Daniel Drake56282212007-07-10 19:32:10 +02002759 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002760 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002761
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002762 if (elems.ht_cap_elem && elems.ht_info_elem &&
2763 elems.wmm_param && local->ops->conf_ht &&
2764 conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2765 struct ieee80211_ht_bss_info bss_info;
2766
2767 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2768 (struct ieee80211_ht_addt_info *)
2769 elems.ht_info_elem, &bss_info);
2770 /* check if AP changed bss inforamation */
2771 if ((conf->ht_bss_conf.primary_channel !=
2772 bss_info.primary_channel) ||
2773 (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
2774 (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
2775 ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
2776 &bss_info);
2777 }
2778
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002779 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002780 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2781 elems.wmm_param_len);
2782 }
Johannes Berg471b3ef2007-12-28 14:32:58 +01002783
2784 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002785}
2786
2787
2788static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2789 struct ieee80211_if_sta *ifsta,
2790 struct ieee80211_mgmt *mgmt,
2791 size_t len,
2792 struct ieee80211_rx_status *rx_status)
2793{
2794 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2795 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2796 int tx_last_beacon;
2797 struct sk_buff *skb;
2798 struct ieee80211_mgmt *resp;
2799 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002800 DECLARE_MAC_BUF(mac);
2801#ifdef CONFIG_MAC80211_IBSS_DEBUG
2802 DECLARE_MAC_BUF(mac2);
2803 DECLARE_MAC_BUF(mac3);
2804#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002805
Johannes Berg51fb61e2007-12-19 01:31:27 +01002806 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002807 ifsta->state != IEEE80211_IBSS_JOINED ||
2808 len < 24 + 2 || !ifsta->probe_resp)
2809 return;
2810
2811 if (local->ops->tx_last_beacon)
2812 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2813 else
2814 tx_last_beacon = 1;
2815
2816#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002817 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2818 "%s (tx_last_beacon=%d)\n",
2819 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2820 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002821#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2822
2823 if (!tx_last_beacon)
2824 return;
2825
2826 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2827 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2828 return;
2829
2830 end = ((u8 *) mgmt) + len;
2831 pos = mgmt->u.probe_req.variable;
2832 if (pos[0] != WLAN_EID_SSID ||
2833 pos + 2 + pos[1] > end) {
2834 if (net_ratelimit()) {
2835 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002836 "from %s\n",
2837 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002838 }
2839 return;
2840 }
2841 if (pos[1] != 0 &&
2842 (pos[1] != ifsta->ssid_len ||
2843 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2844 /* Ignore ProbeReq for foreign SSID */
2845 return;
2846 }
2847
2848 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002849 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002850 if (!skb)
2851 return;
2852
2853 resp = (struct ieee80211_mgmt *) skb->data;
2854 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2855#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002856 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2857 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002858#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2859 ieee80211_sta_tx(dev, skb, 0);
2860}
2861
Johannes Berg4e20cb22007-12-19 01:31:24 +01002862static void ieee80211_rx_mgmt_action(struct net_device *dev,
2863 struct ieee80211_if_sta *ifsta,
2864 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002865 size_t len,
2866 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002867{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002868 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002869
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002870 if (len < IEEE80211_MIN_ACTION_SIZE)
2871 return;
2872
2873 switch (mgmt->u.action.category) {
2874 case WLAN_CATEGORY_BACK:
2875 switch (mgmt->u.action.u.addba_req.action_code) {
2876 case WLAN_ACTION_ADDBA_REQ:
2877 if (len < (IEEE80211_MIN_ACTION_SIZE +
2878 sizeof(mgmt->u.action.u.addba_req)))
2879 break;
2880 ieee80211_sta_process_addba_request(dev, mgmt, len);
2881 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002882 case WLAN_ACTION_ADDBA_RESP:
2883 if (len < (IEEE80211_MIN_ACTION_SIZE +
2884 sizeof(mgmt->u.action.u.addba_resp)))
2885 break;
2886 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2887 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002888 case WLAN_ACTION_DELBA:
2889 if (len < (IEEE80211_MIN_ACTION_SIZE +
2890 sizeof(mgmt->u.action.u.delba)))
2891 break;
2892 ieee80211_sta_process_delba(dev, mgmt, len);
2893 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002894 default:
2895 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002896 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002897 dev->name);
2898 break;
2899 }
2900 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002901 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002902 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002903 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
2904 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002905 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002906 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002907 mesh_rx_path_sel_frame(dev, mgmt, len);
2908 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002909 default:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002910 if (net_ratelimit())
2911 printk(KERN_DEBUG "%s: Rx unknown action frame - "
2912 "category=%d\n", dev->name, mgmt->u.action.category);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002913 break;
2914 }
2915}
Jiri Bencf0706e82007-05-05 11:45:53 -07002916
2917void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2918 struct ieee80211_rx_status *rx_status)
2919{
2920 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2921 struct ieee80211_sub_if_data *sdata;
2922 struct ieee80211_if_sta *ifsta;
2923 struct ieee80211_mgmt *mgmt;
2924 u16 fc;
2925
2926 if (skb->len < 24)
2927 goto fail;
2928
2929 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2930 ifsta = &sdata->u.sta;
2931
2932 mgmt = (struct ieee80211_mgmt *) skb->data;
2933 fc = le16_to_cpu(mgmt->frame_control);
2934
2935 switch (fc & IEEE80211_FCTL_STYPE) {
2936 case IEEE80211_STYPE_PROBE_REQ:
2937 case IEEE80211_STYPE_PROBE_RESP:
2938 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002939 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07002940 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2941 case IEEE80211_STYPE_AUTH:
2942 case IEEE80211_STYPE_ASSOC_RESP:
2943 case IEEE80211_STYPE_REASSOC_RESP:
2944 case IEEE80211_STYPE_DEAUTH:
2945 case IEEE80211_STYPE_DISASSOC:
2946 skb_queue_tail(&ifsta->skb_queue, skb);
2947 queue_work(local->hw.workqueue, &ifsta->work);
2948 return;
2949 default:
2950 printk(KERN_DEBUG "%s: received unknown management frame - "
2951 "stype=%d\n", dev->name,
2952 (fc & IEEE80211_FCTL_STYPE) >> 4);
2953 break;
2954 }
2955
2956 fail:
2957 kfree_skb(skb);
2958}
2959
2960
2961static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2962 struct sk_buff *skb)
2963{
2964 struct ieee80211_rx_status *rx_status;
2965 struct ieee80211_sub_if_data *sdata;
2966 struct ieee80211_if_sta *ifsta;
2967 struct ieee80211_mgmt *mgmt;
2968 u16 fc;
2969
2970 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2971 ifsta = &sdata->u.sta;
2972
2973 rx_status = (struct ieee80211_rx_status *) skb->cb;
2974 mgmt = (struct ieee80211_mgmt *) skb->data;
2975 fc = le16_to_cpu(mgmt->frame_control);
2976
2977 switch (fc & IEEE80211_FCTL_STYPE) {
2978 case IEEE80211_STYPE_PROBE_REQ:
2979 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2980 rx_status);
2981 break;
2982 case IEEE80211_STYPE_PROBE_RESP:
2983 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2984 break;
2985 case IEEE80211_STYPE_BEACON:
2986 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2987 break;
2988 case IEEE80211_STYPE_AUTH:
2989 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2990 break;
2991 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002992 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002993 break;
2994 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002995 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002996 break;
2997 case IEEE80211_STYPE_DEAUTH:
2998 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2999 break;
3000 case IEEE80211_STYPE_DISASSOC:
3001 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3002 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003003 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003004 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003005 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07003006 }
3007
3008 kfree_skb(skb);
3009}
3010
3011
Johannes Berg9ae54c82008-01-31 19:48:20 +01003012ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003013ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3014 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003015{
3016 struct ieee80211_mgmt *mgmt;
3017 u16 fc;
3018
Zhu Yiece8edd2007-11-22 10:53:21 +08003019 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003020 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003021
3022 mgmt = (struct ieee80211_mgmt *) skb->data;
3023 fc = le16_to_cpu(mgmt->frame_control);
3024
Zhu Yiece8edd2007-11-22 10:53:21 +08003025 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01003026 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003027
3028 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003029 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003030
Jiri Bencf0706e82007-05-05 11:45:53 -07003031 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3032 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3033 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3034 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003035 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003036 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003037 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3038 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3039 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003040 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003041 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003042 }
3043 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01003044 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003045}
3046
3047
3048static int ieee80211_sta_active_ibss(struct net_device *dev)
3049{
3050 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3051 int active = 0;
3052 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003053 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003054
Johannes Bergd0709a62008-02-25 16:27:46 +01003055 rcu_read_lock();
3056
3057 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3058 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003059 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3060 jiffies)) {
3061 active++;
3062 break;
3063 }
3064 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003065
3066 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003067
3068 return active;
3069}
3070
3071
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003072static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003073{
3074 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3075 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003076 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003077 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003078 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003079
Johannes Bergd0709a62008-02-25 16:27:46 +01003080 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003081 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003082 if (time_after(jiffies, sta->last_rx + exp_time)) {
Joe Perches0795af52007-10-03 17:59:30 -07003083 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3084 dev->name, print_mac(mac, sta->addr));
Johannes Bergd0709a62008-02-25 16:27:46 +01003085 sta_info_unlink(&sta);
3086 if (sta)
3087 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003088 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003089 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003090
Johannes Bergd0709a62008-02-25 16:27:46 +01003091 synchronize_rcu();
3092
3093 rtnl_lock();
3094 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3095 sta_info_destroy(sta);
3096 rtnl_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003097}
3098
3099
3100static void ieee80211_sta_merge_ibss(struct net_device *dev,
3101 struct ieee80211_if_sta *ifsta)
3102{
3103 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3104
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003105 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07003106 if (ieee80211_sta_active_ibss(dev))
3107 return;
3108
3109 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3110 "IBSS networks with same SSID (merge)\n", dev->name);
3111 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3112}
3113
3114
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003115#ifdef CONFIG_MAC80211_MESH
3116static void ieee80211_mesh_housekeeping(struct net_device *dev,
3117 struct ieee80211_if_sta *ifsta)
3118{
3119 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3120 bool free_plinks;
3121
3122 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3123 mesh_path_expire(dev);
3124
3125 free_plinks = mesh_plink_availables(sdata);
3126 if (free_plinks != sdata->u.sta.accepting_plinks)
3127 ieee80211_if_config_beacon(dev);
3128
3129 mod_timer(&ifsta->timer, jiffies +
3130 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3131}
3132
3133
3134void ieee80211_start_mesh(struct net_device *dev)
3135{
3136 struct ieee80211_if_sta *ifsta;
3137 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3138 ifsta = &sdata->u.sta;
3139 ifsta->state = IEEE80211_MESH_UP;
3140 ieee80211_sta_timer((unsigned long)sdata);
3141}
3142#endif
3143
3144
Jiri Bencf0706e82007-05-05 11:45:53 -07003145void ieee80211_sta_timer(unsigned long data)
3146{
3147 struct ieee80211_sub_if_data *sdata =
3148 (struct ieee80211_sub_if_data *) data;
3149 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3150 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3151
3152 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3153 queue_work(local->hw.workqueue, &ifsta->work);
3154}
3155
Jiri Bencf0706e82007-05-05 11:45:53 -07003156void ieee80211_sta_work(struct work_struct *work)
3157{
3158 struct ieee80211_sub_if_data *sdata =
3159 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3160 struct net_device *dev = sdata->dev;
3161 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3162 struct ieee80211_if_sta *ifsta;
3163 struct sk_buff *skb;
3164
3165 if (!netif_running(dev))
3166 return;
3167
Zhu Yiece8edd2007-11-22 10:53:21 +08003168 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003169 return;
3170
Johannes Berg51fb61e2007-12-19 01:31:27 +01003171 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003172 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3173 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003174 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01003175 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e82007-05-05 11:45:53 -07003176 return;
3177 }
3178 ifsta = &sdata->u.sta;
3179
3180 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3181 ieee80211_sta_rx_queued_mgmt(dev, skb);
3182
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003183#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003184 if (ifsta->preq_queue_len &&
3185 time_after(jiffies,
3186 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003187 mesh_path_start_discovery(dev);
3188#endif
3189
Jiri Bencf0706e82007-05-05 11:45:53 -07003190 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3191 ifsta->state != IEEE80211_ASSOCIATE &&
3192 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003193 if (ifsta->scan_ssid_len)
3194 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3195 else
3196 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003197 return;
3198 }
3199
3200 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3201 if (ieee80211_sta_config_auth(dev, ifsta))
3202 return;
3203 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3204 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3205 return;
3206
3207 switch (ifsta->state) {
3208 case IEEE80211_DISABLED:
3209 break;
3210 case IEEE80211_AUTHENTICATE:
3211 ieee80211_authenticate(dev, ifsta);
3212 break;
3213 case IEEE80211_ASSOCIATE:
3214 ieee80211_associate(dev, ifsta);
3215 break;
3216 case IEEE80211_ASSOCIATED:
3217 ieee80211_associated(dev, ifsta);
3218 break;
3219 case IEEE80211_IBSS_SEARCH:
3220 ieee80211_sta_find_ibss(dev, ifsta);
3221 break;
3222 case IEEE80211_IBSS_JOINED:
3223 ieee80211_sta_merge_ibss(dev, ifsta);
3224 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003225#ifdef CONFIG_MAC80211_MESH
3226 case IEEE80211_MESH_UP:
3227 ieee80211_mesh_housekeeping(dev, ifsta);
3228 break;
3229#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003230 default:
3231 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3232 ifsta->state);
3233 break;
3234 }
3235
3236 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3237 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3238 "mixed-cell disabled - disassociate\n", dev->name);
3239
3240 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3241 ieee80211_set_disassoc(dev, ifsta, 0);
3242 }
3243}
3244
3245
3246static void ieee80211_sta_reset_auth(struct net_device *dev,
3247 struct ieee80211_if_sta *ifsta)
3248{
3249 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3250
3251 if (local->ops->reset_tsf) {
3252 /* Reset own TSF to allow time synchronization work. */
3253 local->ops->reset_tsf(local_to_hw(local));
3254 }
3255
3256 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3257
3258
3259 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3260 ifsta->auth_alg = WLAN_AUTH_OPEN;
3261 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3262 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3263 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3264 ifsta->auth_alg = WLAN_AUTH_LEAP;
3265 else
3266 ifsta->auth_alg = WLAN_AUTH_OPEN;
3267 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3268 ifsta->auth_alg);
3269 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003270 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3271 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003272 netif_carrier_off(dev);
3273}
3274
3275
3276void ieee80211_sta_req_auth(struct net_device *dev,
3277 struct ieee80211_if_sta *ifsta)
3278{
3279 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3280 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3281
Johannes Berg51fb61e2007-12-19 01:31:27 +01003282 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003283 return;
3284
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003285 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3286 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3287 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3288 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003289 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3290 queue_work(local->hw.workqueue, &ifsta->work);
3291 }
3292}
3293
3294static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3295 const char *ssid, int ssid_len)
3296{
3297 int tmp, hidden_ssid;
3298
Michael Wu48225702007-10-19 17:14:36 -04003299 if (ssid_len == ifsta->ssid_len &&
3300 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003301 return 1;
3302
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003303 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003304 return 0;
3305
3306 hidden_ssid = 1;
3307 tmp = ssid_len;
3308 while (tmp--) {
3309 if (ssid[tmp] != '\0') {
3310 hidden_ssid = 0;
3311 break;
3312 }
3313 }
3314
3315 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3316 return 1;
3317
3318 if (ssid_len == 1 && ssid[0] == ' ')
3319 return 1;
3320
3321 return 0;
3322}
3323
3324static int ieee80211_sta_config_auth(struct net_device *dev,
3325 struct ieee80211_if_sta *ifsta)
3326{
3327 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3328 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3329 struct ieee80211_sta_bss *bss, *selected = NULL;
3330 int top_rssi = 0, freq;
3331
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003332 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3333 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003334 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003335 ieee80211_sta_reset_auth(dev, ifsta);
3336 return 0;
3337 }
3338
3339 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003340 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003341 list_for_each_entry(bss, &local->sta_bss_list, list) {
3342 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3343 continue;
3344
3345 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3346 !!sdata->default_key)
3347 continue;
3348
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003349 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3350 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003351 continue;
3352
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003353 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003354 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3355 continue;
3356
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003357 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003358 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3359 continue;
3360
3361 if (!selected || top_rssi < bss->rssi) {
3362 selected = bss;
3363 top_rssi = bss->rssi;
3364 }
3365 }
3366 if (selected)
3367 atomic_inc(&selected->users);
3368 spin_unlock_bh(&local->sta_bss_lock);
3369
3370 if (selected) {
Johannes Berg8318d782008-01-24 19:38:38 +01003371 ieee80211_set_freq(local, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003372 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003373 ieee80211_sta_set_ssid(dev, selected->ssid,
3374 selected->ssid_len);
3375 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003376 ieee80211_sta_def_wmm_params(dev, selected, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003377 ieee80211_rx_bss_put(dev, selected);
3378 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003379 ieee80211_sta_reset_auth(dev, ifsta);
3380 return 0;
3381 } else {
3382 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003383 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003384 ieee80211_sta_start_scan(dev, NULL, 0);
3385 else
3386 ieee80211_sta_start_scan(dev, ifsta->ssid,
3387 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003388 ifsta->state = IEEE80211_AUTHENTICATE;
3389 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3390 } else
3391 ifsta->state = IEEE80211_DISABLED;
3392 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003393 return -1;
3394}
3395
Jiri Bencf0706e82007-05-05 11:45:53 -07003396
3397static int ieee80211_sta_create_ibss(struct net_device *dev,
3398 struct ieee80211_if_sta *ifsta)
3399{
3400 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3401 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003402 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003403 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003404 u8 bssid[ETH_ALEN], *pos;
3405 int i;
Joe Perches0795af52007-10-03 17:59:30 -07003406 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003407
3408#if 0
3409 /* Easier testing, use fixed BSSID. */
3410 memset(bssid, 0xfe, ETH_ALEN);
3411#else
3412 /* Generate random, not broadcast, locally administered BSSID. Mix in
3413 * own MAC address to make sure that devices that do not have proper
3414 * random number generator get different BSSID. */
3415 get_random_bytes(bssid, ETH_ALEN);
3416 for (i = 0; i < ETH_ALEN; i++)
3417 bssid[i] ^= dev->dev_addr[i];
3418 bssid[0] &= ~0x01;
3419 bssid[0] |= 0x02;
3420#endif
3421
Joe Perches0795af52007-10-03 17:59:30 -07003422 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3423 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003424
Johannes Berg8318d782008-01-24 19:38:38 +01003425 bss = ieee80211_rx_bss_add(dev, bssid,
3426 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003427 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003428 if (!bss)
3429 return -ENOMEM;
3430
Johannes Berg8318d782008-01-24 19:38:38 +01003431 bss->band = local->hw.conf.channel->band;
3432 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003433
3434 if (local->hw.conf.beacon_int == 0)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003435 local->hw.conf.beacon_int = 10000;
Jiri Bencf0706e82007-05-05 11:45:53 -07003436 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003437 bss->last_update = jiffies;
3438 bss->capability = WLAN_CAPABILITY_IBSS;
3439 if (sdata->default_key) {
3440 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3441 } else
3442 sdata->drop_unencrypted = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003443 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003444 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003445 for (i = 0; i < sband->n_bitrates; i++) {
3446 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003447 *pos++ = (u8) (rate / 5);
3448 }
3449
3450 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3451}
3452
3453
3454static int ieee80211_sta_find_ibss(struct net_device *dev,
3455 struct ieee80211_if_sta *ifsta)
3456{
3457 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3458 struct ieee80211_sta_bss *bss;
3459 int found = 0;
3460 u8 bssid[ETH_ALEN];
3461 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003462 DECLARE_MAC_BUF(mac);
3463 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003464
3465 if (ifsta->ssid_len == 0)
3466 return -EINVAL;
3467
3468 active_ibss = ieee80211_sta_active_ibss(dev);
3469#ifdef CONFIG_MAC80211_IBSS_DEBUG
3470 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3471 dev->name, active_ibss);
3472#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3473 spin_lock_bh(&local->sta_bss_lock);
3474 list_for_each_entry(bss, &local->sta_bss_list, list) {
3475 if (ifsta->ssid_len != bss->ssid_len ||
3476 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3477 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3478 continue;
3479#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003480 printk(KERN_DEBUG " bssid=%s found\n",
3481 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003482#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3483 memcpy(bssid, bss->bssid, ETH_ALEN);
3484 found = 1;
3485 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3486 break;
3487 }
3488 spin_unlock_bh(&local->sta_bss_lock);
3489
3490#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003491 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3492 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003493#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3494 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003495 (bss = ieee80211_rx_bss_get(dev, bssid,
3496 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003497 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07003498 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003499 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003500 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003501 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3502 }
3503#ifdef CONFIG_MAC80211_IBSS_DEBUG
3504 printk(KERN_DEBUG " did not try to join ibss\n");
3505#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3506
3507 /* Selected IBSS not found in current scan results - try to scan */
3508 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3509 !ieee80211_sta_active_ibss(dev)) {
3510 mod_timer(&ifsta->timer, jiffies +
3511 IEEE80211_IBSS_MERGE_INTERVAL);
3512 } else if (time_after(jiffies, local->last_scan_completed +
3513 IEEE80211_SCAN_INTERVAL)) {
3514 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3515 "join\n", dev->name);
3516 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3517 ifsta->ssid_len);
3518 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3519 int interval = IEEE80211_SCAN_INTERVAL;
3520
3521 if (time_after(jiffies, ifsta->ibss_join_req +
3522 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003523 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003524 (!(local->oper_channel->flags &
3525 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003526 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003527 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003528 printk(KERN_DEBUG "%s: IBSS not allowed on"
3529 " %d MHz\n", dev->name,
3530 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003531 }
3532
3533 /* No IBSS found - decrease scan interval and continue
3534 * scanning. */
3535 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3536 }
3537
3538 ifsta->state = IEEE80211_IBSS_SEARCH;
3539 mod_timer(&ifsta->timer, jiffies + interval);
3540 return 0;
3541 }
3542
3543 return 0;
3544}
3545
3546
3547int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3548{
Johannes Berg8318d782008-01-24 19:38:38 +01003549 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003550 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07003551
3552 if (len > IEEE80211_MAX_SSID_LEN)
3553 return -EINVAL;
3554
Jiri Bencf0706e82007-05-05 11:45:53 -07003555 ifsta = &sdata->u.sta;
3556
3557 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003558 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003559 memcpy(ifsta->ssid, ssid, len);
3560 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3561 ifsta->ssid_len = len;
3562
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003563 if (len)
3564 ifsta->flags |= IEEE80211_STA_SSID_SET;
3565 else
3566 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003567 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003568 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003569 ifsta->ibss_join_req = jiffies;
3570 ifsta->state = IEEE80211_IBSS_SEARCH;
3571 return ieee80211_sta_find_ibss(dev, ifsta);
3572 }
3573 return 0;
3574}
3575
3576
3577int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3578{
3579 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3580 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3581 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3582 *len = ifsta->ssid_len;
3583 return 0;
3584}
3585
3586
3587int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3588{
3589 struct ieee80211_sub_if_data *sdata;
3590 struct ieee80211_if_sta *ifsta;
3591 int res;
3592
3593 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3594 ifsta = &sdata->u.sta;
3595
3596 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3597 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3598 res = ieee80211_if_config(dev);
3599 if (res) {
3600 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3601 "the low-level driver\n", dev->name);
3602 return res;
3603 }
3604 }
3605
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003606 if (is_valid_ether_addr(bssid))
3607 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003608 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003609 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3610
Jiri Bencf0706e82007-05-05 11:45:53 -07003611 return 0;
3612}
3613
3614
3615static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3616 struct ieee80211_sub_if_data *sdata,
3617 int powersave)
3618{
3619 struct sk_buff *skb;
3620 struct ieee80211_hdr *nullfunc;
3621 u16 fc;
3622
3623 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3624 if (!skb) {
3625 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3626 "frame\n", sdata->dev->name);
3627 return;
3628 }
3629 skb_reserve(skb, local->hw.extra_tx_headroom);
3630
3631 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3632 memset(nullfunc, 0, 24);
3633 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3634 IEEE80211_FCTL_TODS;
3635 if (powersave)
3636 fc |= IEEE80211_FCTL_PM;
3637 nullfunc->frame_control = cpu_to_le16(fc);
3638 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3639 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3640 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3641
3642 ieee80211_sta_tx(sdata->dev, skb, 0);
3643}
3644
3645
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003646static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3647{
3648 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3649 ieee80211_vif_is_mesh(&sdata->vif))
3650 ieee80211_sta_timer((unsigned long)sdata);
3651}
3652
Jiri Bencf0706e82007-05-05 11:45:53 -07003653void ieee80211_scan_completed(struct ieee80211_hw *hw)
3654{
3655 struct ieee80211_local *local = hw_to_local(hw);
3656 struct net_device *dev = local->scan_dev;
3657 struct ieee80211_sub_if_data *sdata;
3658 union iwreq_data wrqu;
3659
3660 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003661 memset(&wrqu, 0, sizeof(wrqu));
3662 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003663
Zhu Yiece8edd2007-11-22 10:53:21 +08003664 if (local->sta_hw_scanning) {
3665 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003666 if (ieee80211_hw_config(local))
3667 printk(KERN_DEBUG "%s: failed to restore operational "
3668 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003669 /* Restart STA timer for HW scan case */
3670 rcu_read_lock();
3671 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3672 ieee80211_restart_sta_timer(sdata);
3673 rcu_read_unlock();
3674
Zhu Yiece8edd2007-11-22 10:53:21 +08003675 goto done;
3676 }
3677
3678 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003679 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003680 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003681 "channel after scan\n", dev->name);
3682
Johannes Berg4150c572007-09-17 01:29:23 -04003683
3684 netif_tx_lock_bh(local->mdev);
3685 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3686 local->ops->configure_filter(local_to_hw(local),
3687 FIF_BCN_PRBRESP_PROMISC,
3688 &local->filter_flags,
3689 local->mdev->mc_count,
3690 local->mdev->mc_list);
3691
3692 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003693
Johannes Berg79010422007-09-18 17:29:21 -04003694 rcu_read_lock();
3695 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003696
3697 /* No need to wake the master device. */
3698 if (sdata->dev == local->mdev)
3699 continue;
3700
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003701 /* Tell AP we're back */
3702 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3703 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3704 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003705
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003706 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003707
Jiri Bencf0706e82007-05-05 11:45:53 -07003708 netif_wake_queue(sdata->dev);
3709 }
Johannes Berg79010422007-09-18 17:29:21 -04003710 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003711
Zhu Yiece8edd2007-11-22 10:53:21 +08003712done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003713 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003714 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003715 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003716 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003717 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3718 !ieee80211_sta_active_ibss(dev)))
3719 ieee80211_sta_find_ibss(dev, ifsta);
3720 }
3721}
3722EXPORT_SYMBOL(ieee80211_scan_completed);
3723
3724void ieee80211_sta_scan_work(struct work_struct *work)
3725{
3726 struct ieee80211_local *local =
3727 container_of(work, struct ieee80211_local, scan_work.work);
3728 struct net_device *dev = local->scan_dev;
3729 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003730 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003731 struct ieee80211_channel *chan;
3732 int skip;
3733 unsigned long next_delay = 0;
3734
Zhu Yiece8edd2007-11-22 10:53:21 +08003735 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003736 return;
3737
3738 switch (local->scan_state) {
3739 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003740 /*
3741 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3742 * after we successfully scanned the last channel of the last
3743 * band (and the last band is supported by the hw)
3744 */
Johannes Berg8318d782008-01-24 19:38:38 +01003745 if (local->scan_band < IEEE80211_NUM_BANDS)
3746 sband = local->hw.wiphy->bands[local->scan_band];
3747 else
3748 sband = NULL;
3749
Johannes Berg69d464d2008-01-28 17:08:56 +01003750 /*
3751 * If we are at an unsupported band and have more bands
3752 * left to scan, advance to the next supported one.
3753 */
3754 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003755 local->scan_band++;
3756 sband = local->hw.wiphy->bands[local->scan_band];
3757 local->scan_channel_idx = 0;
3758 }
3759
Johannes Berg69d464d2008-01-28 17:08:56 +01003760 /* if no more bands/channels left, complete scan */
3761 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003762 ieee80211_scan_completed(local_to_hw(local));
3763 return;
3764 }
Johannes Berg8318d782008-01-24 19:38:38 +01003765 skip = 0;
3766 chan = &sband->channels[local->scan_channel_idx];
3767
3768 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003769 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003770 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003771 skip = 1;
3772
3773 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003774 local->scan_channel = chan;
3775 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003776 printk(KERN_DEBUG "%s: failed to set freq to "
3777 "%d MHz for scan\n", dev->name,
3778 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003779 skip = 1;
3780 }
3781 }
3782
Johannes Berg69d464d2008-01-28 17:08:56 +01003783 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003784 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003785 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003786 /*
3787 * scan_band may end up == IEEE80211_NUM_BANDS, but
3788 * we'll catch that case above and complete the scan
3789 * if that is the case.
3790 */
Johannes Berg8318d782008-01-24 19:38:38 +01003791 local->scan_band++;
3792 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003793 }
3794
3795 if (skip)
3796 break;
3797
3798 next_delay = IEEE80211_PROBE_DELAY +
3799 usecs_to_jiffies(local->hw.channel_change_time);
3800 local->scan_state = SCAN_SEND_PROBE;
3801 break;
3802 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003803 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003804 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003805
3806 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3807 break;
3808 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3809 local->scan_ssid_len);
3810 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003811 break;
3812 }
3813
Zhu Yiece8edd2007-11-22 10:53:21 +08003814 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003815 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3816 next_delay);
3817}
3818
3819
3820static int ieee80211_sta_start_scan(struct net_device *dev,
3821 u8 *ssid, size_t ssid_len)
3822{
3823 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3824 struct ieee80211_sub_if_data *sdata;
3825
3826 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3827 return -EINVAL;
3828
3829 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3830 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3831 * BSSID: MACAddress
3832 * SSID
3833 * ScanType: ACTIVE, PASSIVE
3834 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3835 * a Probe frame during active scanning
3836 * ChannelList
3837 * MinChannelTime (>= ProbeDelay), in TU
3838 * MaxChannelTime: (>= MinChannelTime), in TU
3839 */
3840
3841 /* MLME-SCAN.confirm
3842 * BSSDescriptionSet
3843 * ResultCode: SUCCESS, INVALID_PARAMETERS
3844 */
3845
Zhu Yiece8edd2007-11-22 10:53:21 +08003846 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003847 if (local->scan_dev == dev)
3848 return 0;
3849 return -EBUSY;
3850 }
3851
3852 if (local->ops->hw_scan) {
3853 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003854 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003855 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003856 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003857 local->scan_dev = dev;
3858 }
3859 return rc;
3860 }
3861
Zhu Yiece8edd2007-11-22 10:53:21 +08003862 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003863
Johannes Berg79010422007-09-18 17:29:21 -04003864 rcu_read_lock();
3865 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003866
3867 /* Don't stop the master interface, otherwise we can't transmit
3868 * probes! */
3869 if (sdata->dev == local->mdev)
3870 continue;
3871
Jiri Bencf0706e82007-05-05 11:45:53 -07003872 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003873 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003874 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003875 ieee80211_send_nullfunc(local, sdata, 1);
3876 }
Johannes Berg79010422007-09-18 17:29:21 -04003877 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003878
3879 if (ssid) {
3880 local->scan_ssid_len = ssid_len;
3881 memcpy(local->scan_ssid, ssid, ssid_len);
3882 } else
3883 local->scan_ssid_len = 0;
3884 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07003885 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003886 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07003887 local->scan_dev = dev;
3888
Johannes Berg4150c572007-09-17 01:29:23 -04003889 netif_tx_lock_bh(local->mdev);
3890 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3891 local->ops->configure_filter(local_to_hw(local),
3892 FIF_BCN_PRBRESP_PROMISC,
3893 &local->filter_flags,
3894 local->mdev->mc_count,
3895 local->mdev->mc_list);
3896 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003897
3898 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3899 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3900 IEEE80211_CHANNEL_TIME);
3901
3902 return 0;
3903}
3904
3905
3906int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3907{
3908 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3909 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3910 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3911
Johannes Berg51fb61e2007-12-19 01:31:27 +01003912 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003913 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3914
Zhu Yiece8edd2007-11-22 10:53:21 +08003915 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003916 if (local->scan_dev == dev)
3917 return 0;
3918 return -EBUSY;
3919 }
3920
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003921 ifsta->scan_ssid_len = ssid_len;
3922 if (ssid_len)
3923 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003924 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3925 queue_work(local->hw.workqueue, &ifsta->work);
3926 return 0;
3927}
3928
3929static char *
3930ieee80211_sta_scan_result(struct net_device *dev,
3931 struct ieee80211_sta_bss *bss,
3932 char *current_ev, char *end_buf)
3933{
3934 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3935 struct iw_event iwe;
3936
3937 if (time_after(jiffies,
3938 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3939 return current_ev;
3940
Jiri Bencf0706e82007-05-05 11:45:53 -07003941 memset(&iwe, 0, sizeof(iwe));
3942 iwe.cmd = SIOCGIWAP;
3943 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3944 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3945 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3946 IW_EV_ADDR_LEN);
3947
3948 memset(&iwe, 0, sizeof(iwe));
3949 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01003950 if (bss_mesh_cfg(bss)) {
3951 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003952 iwe.u.data.flags = 1;
3953 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
Johannes Berg902acc72008-02-23 15:17:19 +01003954 bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003955 } else {
3956 iwe.u.data.length = bss->ssid_len;
3957 iwe.u.data.flags = 1;
3958 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3959 bss->ssid);
3960 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003961
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08003962 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
3963 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003964 memset(&iwe, 0, sizeof(iwe));
3965 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01003966 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003967 iwe.u.mode = IW_MODE_MESH;
3968 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07003969 iwe.u.mode = IW_MODE_MASTER;
3970 else
3971 iwe.u.mode = IW_MODE_ADHOC;
3972 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3973 IW_EV_UINT_LEN);
3974 }
3975
3976 memset(&iwe, 0, sizeof(iwe));
3977 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01003978 iwe.u.freq.m = bss->freq;
3979 iwe.u.freq.e = 6;
Jiri Bencf0706e82007-05-05 11:45:53 -07003980 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3981 IW_EV_FREQ_LEN);
Johannes Berg8318d782008-01-24 19:38:38 +01003982
3983 memset(&iwe, 0, sizeof(iwe));
3984 iwe.cmd = SIOCGIWFREQ;
3985 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3986 iwe.u.freq.e = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003987 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3988 IW_EV_FREQ_LEN);
3989
3990 memset(&iwe, 0, sizeof(iwe));
3991 iwe.cmd = IWEVQUAL;
3992 iwe.u.qual.qual = bss->signal;
3993 iwe.u.qual.level = bss->rssi;
3994 iwe.u.qual.noise = bss->noise;
3995 iwe.u.qual.updated = local->wstats_flags;
3996 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3997 IW_EV_QUAL_LEN);
3998
3999 memset(&iwe, 0, sizeof(iwe));
4000 iwe.cmd = SIOCGIWENCODE;
4001 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4002 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4003 else
4004 iwe.u.data.flags = IW_ENCODE_DISABLED;
4005 iwe.u.data.length = 0;
4006 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4007
4008 if (bss && bss->wpa_ie) {
4009 memset(&iwe, 0, sizeof(iwe));
4010 iwe.cmd = IWEVGENIE;
4011 iwe.u.data.length = bss->wpa_ie_len;
4012 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4013 bss->wpa_ie);
4014 }
4015
4016 if (bss && bss->rsn_ie) {
4017 memset(&iwe, 0, sizeof(iwe));
4018 iwe.cmd = IWEVGENIE;
4019 iwe.u.data.length = bss->rsn_ie_len;
4020 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4021 bss->rsn_ie);
4022 }
4023
4024 if (bss && bss->supp_rates_len > 0) {
4025 /* display all supported rates in readable format */
4026 char *p = current_ev + IW_EV_LCP_LEN;
4027 int i;
4028
4029 memset(&iwe, 0, sizeof(iwe));
4030 iwe.cmd = SIOCGIWRATE;
4031 /* Those two flags are ignored... */
4032 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4033
4034 for (i = 0; i < bss->supp_rates_len; i++) {
4035 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4036 0x7f) * 500000);
4037 p = iwe_stream_add_value(current_ev, p,
4038 end_buf, &iwe, IW_EV_PARAM_LEN);
4039 }
4040 current_ev = p;
4041 }
4042
4043 if (bss) {
4044 char *buf;
4045 buf = kmalloc(30, GFP_ATOMIC);
4046 if (buf) {
4047 memset(&iwe, 0, sizeof(iwe));
4048 iwe.cmd = IWEVCUSTOM;
4049 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4050 iwe.u.data.length = strlen(buf);
4051 current_ev = iwe_stream_add_point(current_ev, end_buf,
4052 &iwe, buf);
4053 kfree(buf);
4054 }
4055 }
4056
Johannes Berg902acc72008-02-23 15:17:19 +01004057 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004058 char *buf;
Johannes Berg902acc72008-02-23 15:17:19 +01004059 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004060 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004061 if (buf) {
4062 memset(&iwe, 0, sizeof(iwe));
4063 iwe.cmd = IWEVCUSTOM;
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004064 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4065 iwe.u.data.length = strlen(buf);
4066 current_ev = iwe_stream_add_point(current_ev, end_buf,
4067 &iwe, buf);
4068 sprintf(buf, "Path Selection Protocol ID: "
4069 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4070 cfg[4]);
4071 iwe.u.data.length = strlen(buf);
4072 current_ev = iwe_stream_add_point(current_ev, end_buf,
4073 &iwe, buf);
4074 sprintf(buf, "Path Selection Metric ID: "
4075 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4076 cfg[8]);
4077 iwe.u.data.length = strlen(buf);
4078 current_ev = iwe_stream_add_point(current_ev, end_buf,
4079 &iwe, buf);
4080 sprintf(buf, "Congestion Control Mode ID: "
4081 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4082 cfg[11], cfg[12]);
4083 iwe.u.data.length = strlen(buf);
4084 current_ev = iwe_stream_add_point(current_ev, end_buf,
4085 &iwe, buf);
4086 sprintf(buf, "Channel Precedence: "
4087 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4088 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004089 iwe.u.data.length = strlen(buf);
4090 current_ev = iwe_stream_add_point(current_ev, end_buf,
4091 &iwe, buf);
4092 kfree(buf);
4093 }
4094 }
4095
Jiri Bencf0706e82007-05-05 11:45:53 -07004096 return current_ev;
4097}
4098
4099
4100int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4101{
4102 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4103 char *current_ev = buf;
4104 char *end_buf = buf + len;
4105 struct ieee80211_sta_bss *bss;
4106
4107 spin_lock_bh(&local->sta_bss_lock);
4108 list_for_each_entry(bss, &local->sta_bss_list, list) {
4109 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4110 spin_unlock_bh(&local->sta_bss_lock);
4111 return -E2BIG;
4112 }
4113 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4114 end_buf);
4115 }
4116 spin_unlock_bh(&local->sta_bss_lock);
4117 return current_ev - buf;
4118}
4119
4120
4121int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4122{
4123 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4124 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4125 kfree(ifsta->extra_ie);
4126 if (len == 0) {
4127 ifsta->extra_ie = NULL;
4128 ifsta->extra_ie_len = 0;
4129 return 0;
4130 }
4131 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4132 if (!ifsta->extra_ie) {
4133 ifsta->extra_ie_len = 0;
4134 return -ENOMEM;
4135 }
4136 memcpy(ifsta->extra_ie, ie, len);
4137 ifsta->extra_ie_len = len;
4138 return 0;
4139}
4140
4141
4142struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4143 struct sk_buff *skb, u8 *bssid,
4144 u8 *addr)
4145{
4146 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4147 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004148 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004149 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07004150
4151 /* TODO: Could consider removing the least recently used entry and
4152 * allow new one to be added. */
4153 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4154 if (net_ratelimit()) {
4155 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004156 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004157 }
4158 return NULL;
4159 }
4160
Joe Perches0795af52007-10-03 17:59:30 -07004161 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004162 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07004163
Johannes Berg73651ee2008-02-25 16:27:47 +01004164 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4165 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004166 return NULL;
4167
Johannes Berg238814f2008-01-28 17:19:37 +01004168 sta->flags |= WLAN_STA_AUTHORIZED;
4169
Johannes Berg8318d782008-01-24 19:38:38 +01004170 sta->supp_rates[local->hw.conf.channel->band] =
4171 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004172
4173 rate_control_rate_init(sta, local);
4174
Johannes Berg73651ee2008-02-25 16:27:47 +01004175 if (sta_info_insert(sta)) {
4176 sta_info_destroy(sta);
4177 return NULL;
4178 }
4179
Johannes Bergd0709a62008-02-25 16:27:46 +01004180 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004181}
4182
4183
4184int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4185{
4186 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4187 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4188
4189 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4190 dev->name, reason);
4191
Johannes Berg51fb61e2007-12-19 01:31:27 +01004192 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4193 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004194 return -EINVAL;
4195
4196 ieee80211_send_deauth(dev, ifsta, reason);
4197 ieee80211_set_disassoc(dev, ifsta, 1);
4198 return 0;
4199}
4200
4201
4202int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4203{
4204 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4205 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4206
4207 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4208 dev->name, reason);
4209
Johannes Berg51fb61e2007-12-19 01:31:27 +01004210 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004211 return -EINVAL;
4212
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004213 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004214 return -1;
4215
4216 ieee80211_send_disassoc(dev, ifsta, reason);
4217 ieee80211_set_disassoc(dev, ifsta, 0);
4218 return 0;
4219}