blob: 00fde111c268daf60149a865151dcde62f4ad2eb [file] [log] [blame]
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -070096{
97 size_t left = len;
98 u8 *pos = start;
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700208 default:
Jiri Bencf0706e822007-05-05 11:45:53 -0700209 break;
210 }
211
212 left -= elen;
213 pos += elen;
214 }
Jiri Bencf0706e822007-05-05 11:45:53 -0700215}
216
217
Jiri Bencf0706e822007-05-05 11:45:53 -0700218static int ecw2cw(int ecw)
219{
Johannes Bergac2bf322008-02-14 00:30:35 +0100220 return (1 << ecw) - 1;
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700498 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100499 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e822007-05-05 11:45:53 -0700500
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700527 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200528 ieee80211_reset_erp_info(dev);
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700836
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000837 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700918 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700919 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700927 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400928 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -07001769
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001770 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -07001815
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001816 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -07001838 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001839 u64 rates, basic_rates;
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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);
Johannes Berg73651ee2008-02-25 16:27:47 +01001945 rcu_read_unlock();
1946 return;
1947 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001948 }
1949
Johannes Berg73651ee2008-02-25 16:27:47 +01001950 /*
1951 * FIXME: Do we really need to update the sta_info's information here?
1952 * We already know about the AP (we found it in our list) so it
1953 * should already be filled with the right info, no?
1954 * As is stands, all this is racy because typically we assume
1955 * the information that is filled in here (except flags) doesn't
1956 * change while a STA structure is alive. As such, it should move
1957 * to between the sta_info_alloc() and sta_info_insert() above.
1958 */
1959
Johannes Berg238814f2008-01-28 17:19:37 +01001960 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1961 WLAN_STA_AUTHORIZED;
Jiri Bencf0706e822007-05-05 11:45:53 -07001962
1963 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001964 basic_rates = 0;
1965 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1966
Jiri Bencf0706e822007-05-05 11:45:53 -07001967 for (i = 0; i < elems.supp_rates_len; i++) {
1968 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001969
1970 if (rate > 110)
1971 have_higher_than_11mbit = true;
1972
1973 for (j = 0; j < sband->n_bitrates; j++) {
1974 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e822007-05-05 11:45:53 -07001975 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001976 if (elems.supp_rates[i] & 0x80)
1977 basic_rates |= BIT(j);
1978 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001979 }
Johannes Berg8318d782008-01-24 19:38:38 +01001980
Jiri Bencf0706e822007-05-05 11:45:53 -07001981 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1982 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001983
1984 if (rate > 110)
1985 have_higher_than_11mbit = true;
1986
1987 for (j = 0; j < sband->n_bitrates; j++) {
1988 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e822007-05-05 11:45:53 -07001989 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001990 if (elems.ext_supp_rates[i] & 0x80)
1991 basic_rates |= BIT(j);
1992 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001993 }
Johannes Berg8318d782008-01-24 19:38:38 +01001994
1995 sta->supp_rates[local->hw.conf.channel->band] = rates;
1996 sdata->basic_rates = basic_rates;
1997
1998 /* cf. IEEE 802.11 9.2.12 */
1999 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2000 have_higher_than_11mbit)
2001 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2002 else
2003 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e822007-05-05 11:45:53 -07002004
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002005 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2006 local->ops->conf_ht) {
2007 struct ieee80211_ht_bss_info bss_info;
2008
2009 ieee80211_ht_cap_ie_to_ht_info(
2010 (struct ieee80211_ht_cap *)
2011 elems.ht_cap_elem, &sta->ht_info);
2012 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2013 (struct ieee80211_ht_addt_info *)
2014 elems.ht_info_elem, &bss_info);
2015 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
2016 }
2017
Jiri Bencf0706e822007-05-05 11:45:53 -07002018 rate_control_rate_init(sta, local);
2019
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002020 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002021 sta->flags |= WLAN_STA_WME;
Johannes Berge5f98f22008-03-05 20:39:31 +01002022 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07002023 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2024 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002025 } else
2026 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07002027
Johannes Berg8318d782008-01-24 19:38:38 +01002028 /* set AID, ieee80211_set_associated() will tell the driver */
2029 bss_conf->aid = aid;
2030 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e822007-05-05 11:45:53 -07002031
Jiri Bencf0706e822007-05-05 11:45:53 -07002032 ieee80211_associated(dev, ifsta);
2033}
2034
2035
2036/* Caller must hold local->sta_bss_lock */
2037static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2038 struct ieee80211_sta_bss *bss)
2039{
2040 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002041 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002042
2043 if (bss_mesh_cfg(bss))
2044 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2045 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002046 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002047 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002048
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002049 bss->hnext = local->sta_bss_hash[hash_idx];
2050 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e822007-05-05 11:45:53 -07002051}
2052
2053
2054/* Caller must hold local->sta_bss_lock */
2055static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2056 struct ieee80211_sta_bss *bss)
2057{
2058 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2059 struct ieee80211_sta_bss *b, *prev = NULL;
2060 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2061 while (b) {
2062 if (b == bss) {
2063 if (!prev)
2064 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2065 bss->hnext;
2066 else
2067 prev->hnext = bss->hnext;
2068 break;
2069 }
2070 prev = b;
2071 b = b->hnext;
2072 }
2073}
2074
2075
2076static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002077ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002078 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e822007-05-05 11:45:53 -07002079{
2080 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2081 struct ieee80211_sta_bss *bss;
2082
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002083 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e822007-05-05 11:45:53 -07002084 if (!bss)
2085 return NULL;
Jiri Bencf0706e822007-05-05 11:45:53 -07002086 atomic_inc(&bss->users);
2087 atomic_inc(&bss->users);
2088 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002089 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002090 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2091 memcpy(bss->ssid, ssid, ssid_len);
2092 bss->ssid_len = ssid_len;
2093 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002094
2095 spin_lock_bh(&local->sta_bss_lock);
2096 /* TODO: order by RSSI? */
2097 list_add_tail(&bss->list, &local->sta_bss_list);
2098 __ieee80211_rx_bss_hash_add(dev, bss);
2099 spin_unlock_bh(&local->sta_bss_lock);
2100 return bss;
2101}
2102
Jiri Bencf0706e822007-05-05 11:45:53 -07002103static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002104ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002105 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e822007-05-05 11:45:53 -07002106{
2107 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2108 struct ieee80211_sta_bss *bss;
2109
2110 spin_lock_bh(&local->sta_bss_lock);
2111 bss = local->sta_bss_hash[STA_HASH(bssid)];
2112 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002113 if (!bss_mesh_cfg(bss) &&
2114 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002115 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002116 bss->ssid_len == ssid_len &&
2117 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002118 atomic_inc(&bss->users);
2119 break;
2120 }
2121 bss = bss->hnext;
2122 }
2123 spin_unlock_bh(&local->sta_bss_lock);
2124 return bss;
2125}
2126
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002127#ifdef CONFIG_MAC80211_MESH
2128static struct ieee80211_sta_bss *
2129ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2130 u8 *mesh_cfg, int freq)
2131{
2132 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2133 struct ieee80211_sta_bss *bss;
2134
2135 spin_lock_bh(&local->sta_bss_lock);
2136 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2137 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002138 if (bss_mesh_cfg(bss) &&
2139 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002140 bss->freq == freq &&
2141 mesh_id_len == bss->mesh_id_len &&
2142 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2143 mesh_id_len))) {
2144 atomic_inc(&bss->users);
2145 break;
2146 }
2147 bss = bss->hnext;
2148 }
2149 spin_unlock_bh(&local->sta_bss_lock);
2150 return bss;
2151}
2152
2153static struct ieee80211_sta_bss *
2154ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2155 u8 *mesh_cfg, int freq)
2156{
2157 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2158 struct ieee80211_sta_bss *bss;
2159
2160 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2161 if (!bss)
2162 return NULL;
2163
2164 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2165 if (!bss->mesh_cfg) {
2166 kfree(bss);
2167 return NULL;
2168 }
2169
2170 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2171 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2172 if (!bss->mesh_id) {
2173 kfree(bss->mesh_cfg);
2174 kfree(bss);
2175 return NULL;
2176 }
2177 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2178 }
2179
2180 atomic_inc(&bss->users);
2181 atomic_inc(&bss->users);
2182 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2183 bss->mesh_id_len = mesh_id_len;
2184 bss->freq = freq;
2185 spin_lock_bh(&local->sta_bss_lock);
2186 /* TODO: order by RSSI? */
2187 list_add_tail(&bss->list, &local->sta_bss_list);
2188 __ieee80211_rx_bss_hash_add(dev, bss);
2189 spin_unlock_bh(&local->sta_bss_lock);
2190 return bss;
2191}
2192#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07002193
2194static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2195{
2196 kfree(bss->wpa_ie);
2197 kfree(bss->rsn_ie);
2198 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002199 kfree(bss->ht_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002200 kfree(bss_mesh_id(bss));
2201 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e822007-05-05 11:45:53 -07002202 kfree(bss);
2203}
2204
2205
2206static void ieee80211_rx_bss_put(struct net_device *dev,
2207 struct ieee80211_sta_bss *bss)
2208{
2209 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2210 if (!atomic_dec_and_test(&bss->users))
2211 return;
2212
2213 spin_lock_bh(&local->sta_bss_lock);
2214 __ieee80211_rx_bss_hash_del(dev, bss);
2215 list_del(&bss->list);
2216 spin_unlock_bh(&local->sta_bss_lock);
2217 ieee80211_rx_bss_free(bss);
2218}
2219
2220
2221void ieee80211_rx_bss_list_init(struct net_device *dev)
2222{
2223 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2224 spin_lock_init(&local->sta_bss_lock);
2225 INIT_LIST_HEAD(&local->sta_bss_list);
2226}
2227
2228
2229void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2230{
2231 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2232 struct ieee80211_sta_bss *bss, *tmp;
2233
2234 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2235 ieee80211_rx_bss_put(dev, bss);
2236}
2237
2238
Bruno Randolfa6072682008-02-18 11:21:15 +09002239static int ieee80211_sta_join_ibss(struct net_device *dev,
2240 struct ieee80211_if_sta *ifsta,
2241 struct ieee80211_sta_bss *bss)
2242{
2243 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2244 int res, rates, i, j;
2245 struct sk_buff *skb;
2246 struct ieee80211_mgmt *mgmt;
2247 struct ieee80211_tx_control control;
2248 struct rate_selection ratesel;
2249 u8 *pos;
2250 struct ieee80211_sub_if_data *sdata;
2251 struct ieee80211_supported_band *sband;
2252
2253 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2254
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002255 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2256
Bruno Randolfa6072682008-02-18 11:21:15 +09002257 /* Remove possible STA entries from other IBSS networks. */
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002258 sta_info_flush(local, sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002259
2260 if (local->ops->reset_tsf) {
2261 /* Reset own TSF to allow time synchronization work. */
2262 local->ops->reset_tsf(local_to_hw(local));
2263 }
2264 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2265 res = ieee80211_if_config(dev);
2266 if (res)
2267 return res;
2268
2269 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2270
Bruno Randolfa6072682008-02-18 11:21:15 +09002271 sdata->drop_unencrypted = bss->capability &
2272 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2273
2274 res = ieee80211_set_freq(local, bss->freq);
2275
2276 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2277 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2278 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2279 return -1;
2280 }
2281
2282 /* Set beacon template */
2283 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2284 do {
2285 if (!skb)
2286 break;
2287
2288 skb_reserve(skb, local->hw.extra_tx_headroom);
2289
2290 mgmt = (struct ieee80211_mgmt *)
2291 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2292 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2293 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2294 IEEE80211_STYPE_BEACON);
2295 memset(mgmt->da, 0xff, ETH_ALEN);
2296 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2297 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2298 mgmt->u.beacon.beacon_int =
2299 cpu_to_le16(local->hw.conf.beacon_int);
2300 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2301
2302 pos = skb_put(skb, 2 + ifsta->ssid_len);
2303 *pos++ = WLAN_EID_SSID;
2304 *pos++ = ifsta->ssid_len;
2305 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2306
2307 rates = bss->supp_rates_len;
2308 if (rates > 8)
2309 rates = 8;
2310 pos = skb_put(skb, 2 + rates);
2311 *pos++ = WLAN_EID_SUPP_RATES;
2312 *pos++ = rates;
2313 memcpy(pos, bss->supp_rates, rates);
2314
2315 if (bss->band == IEEE80211_BAND_2GHZ) {
2316 pos = skb_put(skb, 2 + 1);
2317 *pos++ = WLAN_EID_DS_PARAMS;
2318 *pos++ = 1;
2319 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2320 }
2321
2322 pos = skb_put(skb, 2 + 2);
2323 *pos++ = WLAN_EID_IBSS_PARAMS;
2324 *pos++ = 2;
2325 /* FIX: set ATIM window based on scan results */
2326 *pos++ = 0;
2327 *pos++ = 0;
2328
2329 if (bss->supp_rates_len > 8) {
2330 rates = bss->supp_rates_len - 8;
2331 pos = skb_put(skb, 2 + rates);
2332 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2333 *pos++ = rates;
2334 memcpy(pos, &bss->supp_rates[8], rates);
2335 }
2336
2337 memset(&control, 0, sizeof(control));
2338 rate_control_get_rate(dev, sband, skb, &ratesel);
2339 if (!ratesel.rate) {
2340 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2341 "for IBSS beacon\n", dev->name);
2342 break;
2343 }
2344 control.vif = &sdata->vif;
2345 control.tx_rate = ratesel.rate;
2346 if (sdata->bss_conf.use_short_preamble &&
2347 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2348 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2349 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2350 control.flags |= IEEE80211_TXCTL_NO_ACK;
2351 control.retry_limit = 1;
2352
2353 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2354 if (ifsta->probe_resp) {
2355 mgmt = (struct ieee80211_mgmt *)
2356 ifsta->probe_resp->data;
2357 mgmt->frame_control =
2358 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2359 IEEE80211_STYPE_PROBE_RESP);
2360 } else {
2361 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2362 "template for IBSS\n", dev->name);
2363 }
2364
2365 if (local->ops->beacon_update &&
2366 local->ops->beacon_update(local_to_hw(local),
2367 skb, &control) == 0) {
2368 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2369 "template\n", dev->name);
2370 skb = NULL;
2371 }
2372
2373 rates = 0;
2374 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2375 for (i = 0; i < bss->supp_rates_len; i++) {
2376 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2377 for (j = 0; j < sband->n_bitrates; j++)
2378 if (sband->bitrates[j].bitrate == bitrate)
2379 rates |= BIT(j);
2380 }
2381 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Vladimir Koutnye2839d82008-03-18 21:14:07 +01002382
2383 ieee80211_sta_def_wmm_params(dev, bss, 1);
Bruno Randolfa6072682008-02-18 11:21:15 +09002384 } while (0);
2385
2386 if (skb) {
2387 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2388 "template\n", dev->name);
2389 dev_kfree_skb(skb);
2390 }
2391
2392 ifsta->state = IEEE80211_IBSS_JOINED;
2393 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2394
2395 ieee80211_rx_bss_put(dev, bss);
2396
2397 return res;
2398}
2399
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002400u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2401 struct ieee802_11_elems *elems,
2402 enum ieee80211_band band)
2403{
2404 struct ieee80211_supported_band *sband;
2405 struct ieee80211_rate *bitrates;
2406 size_t num_rates;
2407 u64 supp_rates;
2408 int i, j;
2409 sband = local->hw.wiphy->bands[band];
2410
2411 if (!sband) {
2412 WARN_ON(1);
2413 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2414 }
2415
2416 bitrates = sband->bitrates;
2417 num_rates = sband->n_bitrates;
2418 supp_rates = 0;
2419 for (i = 0; i < elems->supp_rates_len +
2420 elems->ext_supp_rates_len; i++) {
2421 u8 rate = 0;
2422 int own_rate;
2423 if (i < elems->supp_rates_len)
2424 rate = elems->supp_rates[i];
2425 else if (elems->ext_supp_rates)
2426 rate = elems->ext_supp_rates
2427 [i - elems->supp_rates_len];
2428 own_rate = 5 * (rate & 0x7f);
2429 for (j = 0; j < num_rates; j++)
2430 if (bitrates[j].bitrate == own_rate)
2431 supp_rates |= BIT(j);
2432 }
2433 return supp_rates;
2434}
2435
Bruno Randolfa6072682008-02-18 11:21:15 +09002436
Jiri Bencf0706e822007-05-05 11:45:53 -07002437static void ieee80211_rx_bss_info(struct net_device *dev,
2438 struct ieee80211_mgmt *mgmt,
2439 size_t len,
2440 struct ieee80211_rx_status *rx_status,
2441 int beacon)
2442{
2443 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2444 struct ieee802_11_elems elems;
2445 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002446 int freq, clen;
Jiri Bencf0706e822007-05-05 11:45:53 -07002447 struct ieee80211_sta_bss *bss;
2448 struct sta_info *sta;
2449 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002450 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002451 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002452 DECLARE_MAC_BUF(mac);
2453 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e822007-05-05 11:45:53 -07002454
2455 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2456 return; /* ignore ProbeResp to foreign address */
2457
2458#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002459 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e822007-05-05 11:45:53 -07002460 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002461 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e822007-05-05 11:45:53 -07002462#endif
2463
2464 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2465 if (baselen > len)
2466 return;
2467
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002468 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
John W. Linville67a4cce2007-10-12 16:40:37 -04002469 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -07002470
Johannes Berg902acc72008-02-23 15:17:19 +01002471 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2472 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2473 u64 rates = ieee80211_sta_get_rates(local, &elems,
2474 rx_status->band);
2475
2476 mesh_neighbour_update(mgmt->sa, rates, dev,
2477 mesh_peer_accepts_plinks(&elems, dev));
2478 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002479
Johannes Bergd0709a62008-02-25 16:27:46 +01002480 rcu_read_lock();
2481
Johannes Berg51fb61e2007-12-19 01:31:27 +01002482 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e822007-05-05 11:45:53 -07002483 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2484 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002485 u64 prev_rates;
2486 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2487 rx_status->band);
Jiri Bencf0706e822007-05-05 11:45:53 -07002488
Johannes Berg8318d782008-01-24 19:38:38 +01002489 prev_rates = sta->supp_rates[rx_status->band];
2490 sta->supp_rates[rx_status->band] &= supp_rates;
2491 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002492 /* No matching rates - this should not really happen.
2493 * Make sure that at least one rate is marked
2494 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002495 sta->supp_rates[rx_status->band] =
2496 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e822007-05-05 11:45:53 -07002497 }
Johannes Berg8318d782008-01-24 19:38:38 +01002498 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002499 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002500 "%s based on beacon info (0x%llx & 0x%llx -> "
2501 "0x%llx)\n",
2502 dev->name, print_mac(mac, sta->addr),
2503 (unsigned long long) prev_rates,
2504 (unsigned long long) supp_rates,
2505 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e822007-05-05 11:45:53 -07002506 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002507 }
2508
Johannes Bergd0709a62008-02-25 16:27:46 +01002509 rcu_read_unlock();
2510
Jiri Bencf0706e822007-05-05 11:45:53 -07002511 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002512 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e822007-05-05 11:45:53 -07002513 else
Johannes Berg8318d782008-01-24 19:38:38 +01002514 freq = rx_status->freq;
Jiri Bencf0706e822007-05-05 11:45:53 -07002515
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002516 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2517
2518 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2519 return;
2520
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002521#ifdef CONFIG_MAC80211_MESH
2522 if (elems.mesh_config)
2523 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2524 elems.mesh_id_len, elems.mesh_config, freq);
2525 else
2526#endif
2527 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002528 elems.ssid, elems.ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002529 if (!bss) {
2530#ifdef CONFIG_MAC80211_MESH
2531 if (elems.mesh_config)
2532 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
2533 elems.mesh_id_len, elems.mesh_config, freq);
2534 else
2535#endif
2536 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2537 elems.ssid, elems.ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07002538 if (!bss)
2539 return;
2540 } else {
2541#if 0
2542 /* TODO: order by RSSI? */
2543 spin_lock_bh(&local->sta_bss_lock);
2544 list_move_tail(&bss->list, &local->sta_bss_list);
2545 spin_unlock_bh(&local->sta_bss_lock);
2546#endif
2547 }
2548
Johannes Berg8318d782008-01-24 19:38:38 +01002549 bss->band = rx_status->band;
2550
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002551 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2552 bss->probe_resp && beacon) {
2553 /* STA mode:
2554 * Do not allow beacon to override data from Probe Response. */
Jiri Bencf0706e822007-05-05 11:45:53 -07002555 ieee80211_rx_bss_put(dev, bss);
2556 return;
2557 }
2558
Daniel Drake56282212007-07-10 19:32:10 +02002559 /* save the ERP value so that it is available at association time */
2560 if (elems.erp_info && elems.erp_info_len >= 1) {
2561 bss->erp_value = elems.erp_info[0];
2562 bss->has_erp_value = 1;
2563 }
2564
Jiri Bencf0706e822007-05-05 11:45:53 -07002565 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2566 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e822007-05-05 11:45:53 -07002567
2568 bss->supp_rates_len = 0;
2569 if (elems.supp_rates) {
2570 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2571 if (clen > elems.supp_rates_len)
2572 clen = elems.supp_rates_len;
2573 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2574 clen);
2575 bss->supp_rates_len += clen;
2576 }
2577 if (elems.ext_supp_rates) {
2578 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2579 if (clen > elems.ext_supp_rates_len)
2580 clen = elems.ext_supp_rates_len;
2581 memcpy(&bss->supp_rates[bss->supp_rates_len],
2582 elems.ext_supp_rates, clen);
2583 bss->supp_rates_len += clen;
2584 }
2585
2586 if (elems.wpa &&
2587 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2588 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2589 kfree(bss->wpa_ie);
2590 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2591 if (bss->wpa_ie) {
2592 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2593 bss->wpa_ie_len = elems.wpa_len + 2;
2594 } else
2595 bss->wpa_ie_len = 0;
2596 } else if (!elems.wpa && bss->wpa_ie) {
2597 kfree(bss->wpa_ie);
2598 bss->wpa_ie = NULL;
2599 bss->wpa_ie_len = 0;
2600 }
2601
2602 if (elems.rsn &&
2603 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2604 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2605 kfree(bss->rsn_ie);
2606 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2607 if (bss->rsn_ie) {
2608 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2609 bss->rsn_ie_len = elems.rsn_len + 2;
2610 } else
2611 bss->rsn_ie_len = 0;
2612 } else if (!elems.rsn && bss->rsn_ie) {
2613 kfree(bss->rsn_ie);
2614 bss->rsn_ie = NULL;
2615 bss->rsn_ie_len = 0;
2616 }
2617
2618 if (elems.wmm_param &&
2619 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2620 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2621 kfree(bss->wmm_ie);
2622 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2623 if (bss->wmm_ie) {
2624 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2625 elems.wmm_param_len + 2);
2626 bss->wmm_ie_len = elems.wmm_param_len + 2;
2627 } else
2628 bss->wmm_ie_len = 0;
2629 } else if (!elems.wmm_param && bss->wmm_ie) {
2630 kfree(bss->wmm_ie);
2631 bss->wmm_ie = NULL;
2632 bss->wmm_ie_len = 0;
2633 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002634 if (elems.ht_cap_elem &&
2635 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2636 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2637 kfree(bss->ht_ie);
2638 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2639 if (bss->ht_ie) {
2640 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2641 elems.ht_cap_elem_len + 2);
2642 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2643 } else
2644 bss->ht_ie_len = 0;
2645 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2646 kfree(bss->ht_ie);
2647 bss->ht_ie = NULL;
2648 bss->ht_ie_len = 0;
2649 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002650
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002651 bss->timestamp = beacon_timestamp;
Jiri Bencf0706e822007-05-05 11:45:53 -07002652 bss->last_update = jiffies;
2653 bss->rssi = rx_status->ssi;
2654 bss->signal = rx_status->signal;
2655 bss->noise = rx_status->noise;
2656 if (!beacon)
2657 bss->probe_resp++;
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002658
2659 /* check if we need to merge IBSS */
2660 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2661 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002662 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002663 bss->freq == local->oper_channel->center_freq &&
2664 elems.ssid_len == sdata->u.sta.ssid_len &&
2665 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2666 if (rx_status->flag & RX_FLAG_TSFT) {
2667 /* in order for correct IBSS merging we need mactime
2668 *
2669 * since mactime is defined as the time the first data
2670 * symbol of the frame hits the PHY, and the timestamp
2671 * of the beacon is defined as "the time that the data
2672 * symbol containing the first bit of the timestamp is
2673 * transmitted to the PHY plus the transmitting STA’s
2674 * delays through its local PHY from the MAC-PHY
2675 * interface to its interface with the WM"
2676 * (802.11 11.1.2) - equals the time this bit arrives at
2677 * the receiver - we have to take into account the
2678 * offset between the two.
2679 * e.g: at 1 MBit that means mactime is 192 usec earlier
2680 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2681 */
2682 int rate = local->hw.wiphy->bands[rx_status->band]->
2683 bitrates[rx_status->rate_idx].bitrate;
2684 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2685 } else if (local && local->ops && local->ops->get_tsf)
2686 /* second best option: get current TSF */
2687 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2688 else
2689 /* can't merge without knowing the TSF */
2690 rx_timestamp = -1LLU;
2691#ifdef CONFIG_MAC80211_IBSS_DEBUG
2692 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2693 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2694 print_mac(mac, mgmt->sa),
2695 print_mac(mac2, mgmt->bssid),
2696 (unsigned long long)rx_timestamp,
2697 (unsigned long long)beacon_timestamp,
2698 (unsigned long long)(rx_timestamp - beacon_timestamp),
2699 jiffies);
2700#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2701 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002702#ifndef CONFIG_MAC80211_IBSS_DEBUG
Pavel Roskind97cf012008-02-21 11:33:58 -05002703 if (net_ratelimit())
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002704#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002705 printk(KERN_DEBUG "%s: beacon TSF higher than "
2706 "local TSF - IBSS merge with BSSID %s\n",
2707 dev->name, print_mac(mac, mgmt->bssid));
2708 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2709 ieee80211_ibss_add_sta(dev, NULL,
2710 mgmt->bssid, mgmt->sa);
2711 }
2712 }
2713
Jiri Bencf0706e822007-05-05 11:45:53 -07002714 ieee80211_rx_bss_put(dev, bss);
2715}
2716
2717
2718static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2719 struct ieee80211_mgmt *mgmt,
2720 size_t len,
2721 struct ieee80211_rx_status *rx_status)
2722{
2723 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2724}
2725
2726
2727static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2728 struct ieee80211_mgmt *mgmt,
2729 size_t len,
2730 struct ieee80211_rx_status *rx_status)
2731{
Jiri Bencf0706e822007-05-05 11:45:53 -07002732 struct ieee80211_sub_if_data *sdata;
2733 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e822007-05-05 11:45:53 -07002734 size_t baselen;
2735 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002736 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2737 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002738 u32 changed = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07002739
2740 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2741
2742 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002743 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07002744 return;
2745 ifsta = &sdata->u.sta;
2746
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002747 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e822007-05-05 11:45:53 -07002748 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2749 return;
2750
2751 /* Process beacon from the current BSS */
2752 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2753 if (baselen > len)
2754 return;
2755
John W. Linville67a4cce2007-10-12 16:40:37 -04002756 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -07002757
Daniel Drake56282212007-07-10 19:32:10 +02002758 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002759 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
Jiri Bencf0706e822007-05-05 11:45:53 -07002760
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002761 if (elems.ht_cap_elem && elems.ht_info_elem &&
2762 elems.wmm_param && local->ops->conf_ht &&
2763 conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2764 struct ieee80211_ht_bss_info bss_info;
2765
2766 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2767 (struct ieee80211_ht_addt_info *)
2768 elems.ht_info_elem, &bss_info);
2769 /* check if AP changed bss inforamation */
2770 if ((conf->ht_bss_conf.primary_channel !=
2771 bss_info.primary_channel) ||
2772 (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
2773 (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
2774 ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
2775 &bss_info);
2776 }
2777
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002778 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002779 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2780 elems.wmm_param_len);
2781 }
Johannes Berg471b3ef2007-12-28 14:32:58 +01002782
2783 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e822007-05-05 11:45:53 -07002784}
2785
2786
2787static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2788 struct ieee80211_if_sta *ifsta,
2789 struct ieee80211_mgmt *mgmt,
2790 size_t len,
2791 struct ieee80211_rx_status *rx_status)
2792{
2793 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2794 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2795 int tx_last_beacon;
2796 struct sk_buff *skb;
2797 struct ieee80211_mgmt *resp;
2798 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002799 DECLARE_MAC_BUF(mac);
2800#ifdef CONFIG_MAC80211_IBSS_DEBUG
2801 DECLARE_MAC_BUF(mac2);
2802 DECLARE_MAC_BUF(mac3);
2803#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07002804
Johannes Berg51fb61e2007-12-19 01:31:27 +01002805 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e822007-05-05 11:45:53 -07002806 ifsta->state != IEEE80211_IBSS_JOINED ||
2807 len < 24 + 2 || !ifsta->probe_resp)
2808 return;
2809
2810 if (local->ops->tx_last_beacon)
2811 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2812 else
2813 tx_last_beacon = 1;
2814
2815#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002816 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2817 "%s (tx_last_beacon=%d)\n",
2818 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2819 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e822007-05-05 11:45:53 -07002820#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2821
2822 if (!tx_last_beacon)
2823 return;
2824
2825 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2826 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2827 return;
2828
2829 end = ((u8 *) mgmt) + len;
2830 pos = mgmt->u.probe_req.variable;
2831 if (pos[0] != WLAN_EID_SSID ||
2832 pos + 2 + pos[1] > end) {
2833 if (net_ratelimit()) {
2834 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002835 "from %s\n",
2836 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07002837 }
2838 return;
2839 }
2840 if (pos[1] != 0 &&
2841 (pos[1] != ifsta->ssid_len ||
2842 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2843 /* Ignore ProbeReq for foreign SSID */
2844 return;
2845 }
2846
2847 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002848 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07002849 if (!skb)
2850 return;
2851
2852 resp = (struct ieee80211_mgmt *) skb->data;
2853 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2854#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002855 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2856 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e822007-05-05 11:45:53 -07002857#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2858 ieee80211_sta_tx(dev, skb, 0);
2859}
2860
Johannes Berg4e20cb22007-12-19 01:31:24 +01002861static void ieee80211_rx_mgmt_action(struct net_device *dev,
2862 struct ieee80211_if_sta *ifsta,
2863 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002864 size_t len,
2865 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002866{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002867 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002868
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002869 if (len < IEEE80211_MIN_ACTION_SIZE)
2870 return;
2871
2872 switch (mgmt->u.action.category) {
2873 case WLAN_CATEGORY_BACK:
2874 switch (mgmt->u.action.u.addba_req.action_code) {
2875 case WLAN_ACTION_ADDBA_REQ:
2876 if (len < (IEEE80211_MIN_ACTION_SIZE +
2877 sizeof(mgmt->u.action.u.addba_req)))
2878 break;
2879 ieee80211_sta_process_addba_request(dev, mgmt, len);
2880 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002881 case WLAN_ACTION_ADDBA_RESP:
2882 if (len < (IEEE80211_MIN_ACTION_SIZE +
2883 sizeof(mgmt->u.action.u.addba_resp)))
2884 break;
2885 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2886 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002887 case WLAN_ACTION_DELBA:
2888 if (len < (IEEE80211_MIN_ACTION_SIZE +
2889 sizeof(mgmt->u.action.u.delba)))
2890 break;
2891 ieee80211_sta_process_delba(dev, mgmt, len);
2892 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002893 default:
2894 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002895 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002896 dev->name);
2897 break;
2898 }
2899 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002900 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002901 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002902 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
2903 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002904 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002905 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002906 mesh_rx_path_sel_frame(dev, mgmt, len);
2907 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002908 default:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002909 if (net_ratelimit())
2910 printk(KERN_DEBUG "%s: Rx unknown action frame - "
2911 "category=%d\n", dev->name, mgmt->u.action.category);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002912 break;
2913 }
2914}
Jiri Bencf0706e822007-05-05 11:45:53 -07002915
2916void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2917 struct ieee80211_rx_status *rx_status)
2918{
2919 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2920 struct ieee80211_sub_if_data *sdata;
2921 struct ieee80211_if_sta *ifsta;
2922 struct ieee80211_mgmt *mgmt;
2923 u16 fc;
2924
2925 if (skb->len < 24)
2926 goto fail;
2927
2928 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2929 ifsta = &sdata->u.sta;
2930
2931 mgmt = (struct ieee80211_mgmt *) skb->data;
2932 fc = le16_to_cpu(mgmt->frame_control);
2933
2934 switch (fc & IEEE80211_FCTL_STYPE) {
2935 case IEEE80211_STYPE_PROBE_REQ:
2936 case IEEE80211_STYPE_PROBE_RESP:
2937 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002938 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e822007-05-05 11:45:53 -07002939 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2940 case IEEE80211_STYPE_AUTH:
2941 case IEEE80211_STYPE_ASSOC_RESP:
2942 case IEEE80211_STYPE_REASSOC_RESP:
2943 case IEEE80211_STYPE_DEAUTH:
2944 case IEEE80211_STYPE_DISASSOC:
2945 skb_queue_tail(&ifsta->skb_queue, skb);
2946 queue_work(local->hw.workqueue, &ifsta->work);
2947 return;
2948 default:
2949 printk(KERN_DEBUG "%s: received unknown management frame - "
2950 "stype=%d\n", dev->name,
2951 (fc & IEEE80211_FCTL_STYPE) >> 4);
2952 break;
2953 }
2954
2955 fail:
2956 kfree_skb(skb);
2957}
2958
2959
2960static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2961 struct sk_buff *skb)
2962{
2963 struct ieee80211_rx_status *rx_status;
2964 struct ieee80211_sub_if_data *sdata;
2965 struct ieee80211_if_sta *ifsta;
2966 struct ieee80211_mgmt *mgmt;
2967 u16 fc;
2968
2969 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2970 ifsta = &sdata->u.sta;
2971
2972 rx_status = (struct ieee80211_rx_status *) skb->cb;
2973 mgmt = (struct ieee80211_mgmt *) skb->data;
2974 fc = le16_to_cpu(mgmt->frame_control);
2975
2976 switch (fc & IEEE80211_FCTL_STYPE) {
2977 case IEEE80211_STYPE_PROBE_REQ:
2978 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2979 rx_status);
2980 break;
2981 case IEEE80211_STYPE_PROBE_RESP:
2982 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2983 break;
2984 case IEEE80211_STYPE_BEACON:
2985 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2986 break;
2987 case IEEE80211_STYPE_AUTH:
2988 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2989 break;
2990 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002991 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -07002992 break;
2993 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002994 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e822007-05-05 11:45:53 -07002995 break;
2996 case IEEE80211_STYPE_DEAUTH:
2997 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2998 break;
2999 case IEEE80211_STYPE_DISASSOC:
3000 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3001 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003002 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003003 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003004 break;
Jiri Bencf0706e822007-05-05 11:45:53 -07003005 }
3006
3007 kfree_skb(skb);
3008}
3009
3010
Johannes Berg9ae54c82008-01-31 19:48:20 +01003011ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003012ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3013 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e822007-05-05 11:45:53 -07003014{
3015 struct ieee80211_mgmt *mgmt;
3016 u16 fc;
3017
Zhu Yiece8edd2007-11-22 10:53:21 +08003018 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003019 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -07003020
3021 mgmt = (struct ieee80211_mgmt *) skb->data;
3022 fc = le16_to_cpu(mgmt->frame_control);
3023
Zhu Yiece8edd2007-11-22 10:53:21 +08003024 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01003025 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003026
3027 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003028 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003029
Jiri Bencf0706e822007-05-05 11:45:53 -07003030 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3031 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3032 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3033 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003034 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003035 return RX_QUEUED;
Jiri Bencf0706e822007-05-05 11:45:53 -07003036 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3037 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3038 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003039 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003040 return RX_QUEUED;
Jiri Bencf0706e822007-05-05 11:45:53 -07003041 }
3042 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01003043 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -07003044}
3045
3046
3047static int ieee80211_sta_active_ibss(struct net_device *dev)
3048{
3049 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3050 int active = 0;
3051 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003052 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003053
Johannes Bergd0709a62008-02-25 16:27:46 +01003054 rcu_read_lock();
3055
3056 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3057 if (sta->sdata == sdata &&
Jiri Bencf0706e822007-05-05 11:45:53 -07003058 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3059 jiffies)) {
3060 active++;
3061 break;
3062 }
3063 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003064
3065 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07003066
3067 return active;
3068}
3069
3070
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003071static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e822007-05-05 11:45:53 -07003072{
3073 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3074 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003075 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003076 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003077 unsigned long flags;
Jiri Bencf0706e822007-05-05 11:45:53 -07003078
Johannes Bergd0709a62008-02-25 16:27:46 +01003079 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e822007-05-05 11:45:53 -07003080 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003081 if (time_after(jiffies, sta->last_rx + exp_time)) {
Joe Perches0795af52007-10-03 17:59:30 -07003082 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3083 dev->name, print_mac(mac, sta->addr));
Johannes Bergd0709a62008-02-25 16:27:46 +01003084 sta_info_unlink(&sta);
3085 if (sta)
3086 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e822007-05-05 11:45:53 -07003087 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003088 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003089
Johannes Bergd0709a62008-02-25 16:27:46 +01003090 synchronize_rcu();
3091
3092 rtnl_lock();
3093 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3094 sta_info_destroy(sta);
3095 rtnl_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07003096}
3097
3098
3099static void ieee80211_sta_merge_ibss(struct net_device *dev,
3100 struct ieee80211_if_sta *ifsta)
3101{
3102 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3103
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003104 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e822007-05-05 11:45:53 -07003105 if (ieee80211_sta_active_ibss(dev))
3106 return;
3107
3108 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3109 "IBSS networks with same SSID (merge)\n", dev->name);
3110 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3111}
3112
3113
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003114#ifdef CONFIG_MAC80211_MESH
3115static void ieee80211_mesh_housekeeping(struct net_device *dev,
3116 struct ieee80211_if_sta *ifsta)
3117{
3118 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3119 bool free_plinks;
3120
3121 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3122 mesh_path_expire(dev);
3123
3124 free_plinks = mesh_plink_availables(sdata);
3125 if (free_plinks != sdata->u.sta.accepting_plinks)
3126 ieee80211_if_config_beacon(dev);
3127
3128 mod_timer(&ifsta->timer, jiffies +
3129 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3130}
3131
3132
3133void ieee80211_start_mesh(struct net_device *dev)
3134{
3135 struct ieee80211_if_sta *ifsta;
3136 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3137 ifsta = &sdata->u.sta;
3138 ifsta->state = IEEE80211_MESH_UP;
3139 ieee80211_sta_timer((unsigned long)sdata);
3140}
3141#endif
3142
3143
Jiri Bencf0706e822007-05-05 11:45:53 -07003144void ieee80211_sta_timer(unsigned long data)
3145{
3146 struct ieee80211_sub_if_data *sdata =
3147 (struct ieee80211_sub_if_data *) data;
3148 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3149 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3150
3151 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3152 queue_work(local->hw.workqueue, &ifsta->work);
3153}
3154
Jiri Bencf0706e822007-05-05 11:45:53 -07003155void ieee80211_sta_work(struct work_struct *work)
3156{
3157 struct ieee80211_sub_if_data *sdata =
3158 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3159 struct net_device *dev = sdata->dev;
3160 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3161 struct ieee80211_if_sta *ifsta;
3162 struct sk_buff *skb;
3163
3164 if (!netif_running(dev))
3165 return;
3166
Zhu Yiece8edd2007-11-22 10:53:21 +08003167 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07003168 return;
3169
Johannes Berg51fb61e2007-12-19 01:31:27 +01003170 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003171 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3172 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003173 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01003174 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e822007-05-05 11:45:53 -07003175 return;
3176 }
3177 ifsta = &sdata->u.sta;
3178
3179 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3180 ieee80211_sta_rx_queued_mgmt(dev, skb);
3181
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003182#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003183 if (ifsta->preq_queue_len &&
3184 time_after(jiffies,
3185 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003186 mesh_path_start_discovery(dev);
3187#endif
3188
Jiri Bencf0706e822007-05-05 11:45:53 -07003189 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3190 ifsta->state != IEEE80211_ASSOCIATE &&
3191 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003192 if (ifsta->scan_ssid_len)
3193 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3194 else
3195 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -07003196 return;
3197 }
3198
3199 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3200 if (ieee80211_sta_config_auth(dev, ifsta))
3201 return;
3202 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3203 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3204 return;
3205
3206 switch (ifsta->state) {
3207 case IEEE80211_DISABLED:
3208 break;
3209 case IEEE80211_AUTHENTICATE:
3210 ieee80211_authenticate(dev, ifsta);
3211 break;
3212 case IEEE80211_ASSOCIATE:
3213 ieee80211_associate(dev, ifsta);
3214 break;
3215 case IEEE80211_ASSOCIATED:
3216 ieee80211_associated(dev, ifsta);
3217 break;
3218 case IEEE80211_IBSS_SEARCH:
3219 ieee80211_sta_find_ibss(dev, ifsta);
3220 break;
3221 case IEEE80211_IBSS_JOINED:
3222 ieee80211_sta_merge_ibss(dev, ifsta);
3223 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003224#ifdef CONFIG_MAC80211_MESH
3225 case IEEE80211_MESH_UP:
3226 ieee80211_mesh_housekeeping(dev, ifsta);
3227 break;
3228#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07003229 default:
3230 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3231 ifsta->state);
3232 break;
3233 }
3234
3235 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3236 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3237 "mixed-cell disabled - disassociate\n", dev->name);
3238
3239 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3240 ieee80211_set_disassoc(dev, ifsta, 0);
3241 }
3242}
3243
3244
3245static void ieee80211_sta_reset_auth(struct net_device *dev,
3246 struct ieee80211_if_sta *ifsta)
3247{
3248 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3249
3250 if (local->ops->reset_tsf) {
3251 /* Reset own TSF to allow time synchronization work. */
3252 local->ops->reset_tsf(local_to_hw(local));
3253 }
3254
3255 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3256
3257
3258 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3259 ifsta->auth_alg = WLAN_AUTH_OPEN;
3260 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3261 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3262 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3263 ifsta->auth_alg = WLAN_AUTH_LEAP;
3264 else
3265 ifsta->auth_alg = WLAN_AUTH_OPEN;
3266 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3267 ifsta->auth_alg);
3268 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003269 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3270 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07003271 netif_carrier_off(dev);
3272}
3273
3274
3275void ieee80211_sta_req_auth(struct net_device *dev,
3276 struct ieee80211_if_sta *ifsta)
3277{
3278 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3279 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3280
Johannes Berg51fb61e2007-12-19 01:31:27 +01003281 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07003282 return;
3283
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003284 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3285 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3286 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3287 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003288 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3289 queue_work(local->hw.workqueue, &ifsta->work);
3290 }
3291}
3292
3293static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3294 const char *ssid, int ssid_len)
3295{
3296 int tmp, hidden_ssid;
3297
Michael Wu48225702007-10-19 17:14:36 -04003298 if (ssid_len == ifsta->ssid_len &&
3299 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e822007-05-05 11:45:53 -07003300 return 1;
3301
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003302 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e822007-05-05 11:45:53 -07003303 return 0;
3304
3305 hidden_ssid = 1;
3306 tmp = ssid_len;
3307 while (tmp--) {
3308 if (ssid[tmp] != '\0') {
3309 hidden_ssid = 0;
3310 break;
3311 }
3312 }
3313
3314 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3315 return 1;
3316
3317 if (ssid_len == 1 && ssid[0] == ' ')
3318 return 1;
3319
3320 return 0;
3321}
3322
3323static int ieee80211_sta_config_auth(struct net_device *dev,
3324 struct ieee80211_if_sta *ifsta)
3325{
3326 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3327 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3328 struct ieee80211_sta_bss *bss, *selected = NULL;
3329 int top_rssi = 0, freq;
3330
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003331 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3332 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003333 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e822007-05-05 11:45:53 -07003334 ieee80211_sta_reset_auth(dev, ifsta);
3335 return 0;
3336 }
3337
3338 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003339 freq = local->oper_channel->center_freq;
Jiri Bencf0706e822007-05-05 11:45:53 -07003340 list_for_each_entry(bss, &local->sta_bss_list, list) {
3341 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3342 continue;
3343
3344 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3345 !!sdata->default_key)
3346 continue;
3347
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003348 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3349 bss->freq != freq)
Jiri Bencf0706e822007-05-05 11:45:53 -07003350 continue;
3351
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003352 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07003353 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3354 continue;
3355
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003356 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07003357 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3358 continue;
3359
3360 if (!selected || top_rssi < bss->rssi) {
3361 selected = bss;
3362 top_rssi = bss->rssi;
3363 }
3364 }
3365 if (selected)
3366 atomic_inc(&selected->users);
3367 spin_unlock_bh(&local->sta_bss_lock);
3368
3369 if (selected) {
Johannes Berg8318d782008-01-24 19:38:38 +01003370 ieee80211_set_freq(local, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003371 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e822007-05-05 11:45:53 -07003372 ieee80211_sta_set_ssid(dev, selected->ssid,
3373 selected->ssid_len);
3374 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003375 ieee80211_sta_def_wmm_params(dev, selected, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -07003376 ieee80211_rx_bss_put(dev, selected);
3377 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e822007-05-05 11:45:53 -07003378 ieee80211_sta_reset_auth(dev, ifsta);
3379 return 0;
3380 } else {
3381 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003382 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003383 ieee80211_sta_start_scan(dev, NULL, 0);
3384 else
3385 ieee80211_sta_start_scan(dev, ifsta->ssid,
3386 ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003387 ifsta->state = IEEE80211_AUTHENTICATE;
3388 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3389 } else
3390 ifsta->state = IEEE80211_DISABLED;
3391 }
Jiri Bencf0706e822007-05-05 11:45:53 -07003392 return -1;
3393}
3394
Jiri Bencf0706e822007-05-05 11:45:53 -07003395
3396static int ieee80211_sta_create_ibss(struct net_device *dev,
3397 struct ieee80211_if_sta *ifsta)
3398{
3399 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3400 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003401 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003402 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -07003403 u8 bssid[ETH_ALEN], *pos;
3404 int i;
Joe Perches0795af52007-10-03 17:59:30 -07003405 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07003406
3407#if 0
3408 /* Easier testing, use fixed BSSID. */
3409 memset(bssid, 0xfe, ETH_ALEN);
3410#else
3411 /* Generate random, not broadcast, locally administered BSSID. Mix in
3412 * own MAC address to make sure that devices that do not have proper
3413 * random number generator get different BSSID. */
3414 get_random_bytes(bssid, ETH_ALEN);
3415 for (i = 0; i < ETH_ALEN; i++)
3416 bssid[i] ^= dev->dev_addr[i];
3417 bssid[0] &= ~0x01;
3418 bssid[0] |= 0x02;
3419#endif
3420
Joe Perches0795af52007-10-03 17:59:30 -07003421 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3422 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003423
Johannes Berg8318d782008-01-24 19:38:38 +01003424 bss = ieee80211_rx_bss_add(dev, bssid,
3425 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003426 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003427 if (!bss)
3428 return -ENOMEM;
3429
Johannes Berg8318d782008-01-24 19:38:38 +01003430 bss->band = local->hw.conf.channel->band;
3431 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e822007-05-05 11:45:53 -07003432
3433 if (local->hw.conf.beacon_int == 0)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003434 local->hw.conf.beacon_int = 10000;
Jiri Bencf0706e822007-05-05 11:45:53 -07003435 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e822007-05-05 11:45:53 -07003436 bss->last_update = jiffies;
3437 bss->capability = WLAN_CAPABILITY_IBSS;
3438 if (sdata->default_key) {
3439 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3440 } else
3441 sdata->drop_unencrypted = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003442 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e822007-05-05 11:45:53 -07003443 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003444 for (i = 0; i < sband->n_bitrates; i++) {
3445 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e822007-05-05 11:45:53 -07003446 *pos++ = (u8) (rate / 5);
3447 }
3448
3449 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3450}
3451
3452
3453static int ieee80211_sta_find_ibss(struct net_device *dev,
3454 struct ieee80211_if_sta *ifsta)
3455{
3456 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3457 struct ieee80211_sta_bss *bss;
3458 int found = 0;
3459 u8 bssid[ETH_ALEN];
3460 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003461 DECLARE_MAC_BUF(mac);
3462 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e822007-05-05 11:45:53 -07003463
3464 if (ifsta->ssid_len == 0)
3465 return -EINVAL;
3466
3467 active_ibss = ieee80211_sta_active_ibss(dev);
3468#ifdef CONFIG_MAC80211_IBSS_DEBUG
3469 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3470 dev->name, active_ibss);
3471#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3472 spin_lock_bh(&local->sta_bss_lock);
3473 list_for_each_entry(bss, &local->sta_bss_list, list) {
3474 if (ifsta->ssid_len != bss->ssid_len ||
3475 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3476 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3477 continue;
3478#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003479 printk(KERN_DEBUG " bssid=%s found\n",
3480 print_mac(mac, bss->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003481#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3482 memcpy(bssid, bss->bssid, ETH_ALEN);
3483 found = 1;
3484 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3485 break;
3486 }
3487 spin_unlock_bh(&local->sta_bss_lock);
3488
3489#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003490 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3491 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003492#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3493 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003494 (bss = ieee80211_rx_bss_get(dev, bssid,
3495 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003496 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07003497 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07003498 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003499 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003500 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3501 }
3502#ifdef CONFIG_MAC80211_IBSS_DEBUG
3503 printk(KERN_DEBUG " did not try to join ibss\n");
3504#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3505
3506 /* Selected IBSS not found in current scan results - try to scan */
3507 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3508 !ieee80211_sta_active_ibss(dev)) {
3509 mod_timer(&ifsta->timer, jiffies +
3510 IEEE80211_IBSS_MERGE_INTERVAL);
3511 } else if (time_after(jiffies, local->last_scan_completed +
3512 IEEE80211_SCAN_INTERVAL)) {
3513 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3514 "join\n", dev->name);
3515 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3516 ifsta->ssid_len);
3517 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3518 int interval = IEEE80211_SCAN_INTERVAL;
3519
3520 if (time_after(jiffies, ifsta->ibss_join_req +
3521 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003522 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003523 (!(local->oper_channel->flags &
3524 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e822007-05-05 11:45:53 -07003525 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003526 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003527 printk(KERN_DEBUG "%s: IBSS not allowed on"
3528 " %d MHz\n", dev->name,
3529 local->hw.conf.channel->center_freq);
Jiri Bencf0706e822007-05-05 11:45:53 -07003530 }
3531
3532 /* No IBSS found - decrease scan interval and continue
3533 * scanning. */
3534 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3535 }
3536
3537 ifsta->state = IEEE80211_IBSS_SEARCH;
3538 mod_timer(&ifsta->timer, jiffies + interval);
3539 return 0;
3540 }
3541
3542 return 0;
3543}
3544
3545
3546int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3547{
Johannes Berg8318d782008-01-24 19:38:38 +01003548 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003549 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e822007-05-05 11:45:53 -07003550
3551 if (len > IEEE80211_MAX_SSID_LEN)
3552 return -EINVAL;
3553
Jiri Bencf0706e822007-05-05 11:45:53 -07003554 ifsta = &sdata->u.sta;
3555
3556 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003557 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07003558 memcpy(ifsta->ssid, ssid, len);
3559 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3560 ifsta->ssid_len = len;
3561
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003562 if (len)
3563 ifsta->flags |= IEEE80211_STA_SSID_SET;
3564 else
3565 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003566 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003567 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003568 ifsta->ibss_join_req = jiffies;
3569 ifsta->state = IEEE80211_IBSS_SEARCH;
3570 return ieee80211_sta_find_ibss(dev, ifsta);
3571 }
3572 return 0;
3573}
3574
3575
3576int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3577{
3578 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3579 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3580 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3581 *len = ifsta->ssid_len;
3582 return 0;
3583}
3584
3585
3586int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3587{
3588 struct ieee80211_sub_if_data *sdata;
3589 struct ieee80211_if_sta *ifsta;
3590 int res;
3591
3592 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3593 ifsta = &sdata->u.sta;
3594
3595 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3596 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3597 res = ieee80211_if_config(dev);
3598 if (res) {
3599 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3600 "the low-level driver\n", dev->name);
3601 return res;
3602 }
3603 }
3604
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003605 if (is_valid_ether_addr(bssid))
3606 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07003607 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003608 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3609
Jiri Bencf0706e822007-05-05 11:45:53 -07003610 return 0;
3611}
3612
3613
3614static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3615 struct ieee80211_sub_if_data *sdata,
3616 int powersave)
3617{
3618 struct sk_buff *skb;
3619 struct ieee80211_hdr *nullfunc;
3620 u16 fc;
3621
3622 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3623 if (!skb) {
3624 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3625 "frame\n", sdata->dev->name);
3626 return;
3627 }
3628 skb_reserve(skb, local->hw.extra_tx_headroom);
3629
3630 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3631 memset(nullfunc, 0, 24);
3632 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3633 IEEE80211_FCTL_TODS;
3634 if (powersave)
3635 fc |= IEEE80211_FCTL_PM;
3636 nullfunc->frame_control = cpu_to_le16(fc);
3637 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3638 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3639 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3640
3641 ieee80211_sta_tx(sdata->dev, skb, 0);
3642}
3643
3644
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003645static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3646{
3647 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3648 ieee80211_vif_is_mesh(&sdata->vif))
3649 ieee80211_sta_timer((unsigned long)sdata);
3650}
3651
Jiri Bencf0706e822007-05-05 11:45:53 -07003652void ieee80211_scan_completed(struct ieee80211_hw *hw)
3653{
3654 struct ieee80211_local *local = hw_to_local(hw);
3655 struct net_device *dev = local->scan_dev;
3656 struct ieee80211_sub_if_data *sdata;
3657 union iwreq_data wrqu;
3658
3659 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003660 memset(&wrqu, 0, sizeof(wrqu));
3661 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e822007-05-05 11:45:53 -07003662
Zhu Yiece8edd2007-11-22 10:53:21 +08003663 if (local->sta_hw_scanning) {
3664 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003665 if (ieee80211_hw_config(local))
3666 printk(KERN_DEBUG "%s: failed to restore operational "
3667 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003668 /* Restart STA timer for HW scan case */
3669 rcu_read_lock();
3670 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3671 ieee80211_restart_sta_timer(sdata);
3672 rcu_read_unlock();
3673
Zhu Yiece8edd2007-11-22 10:53:21 +08003674 goto done;
3675 }
3676
3677 local->sta_sw_scanning = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07003678 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003679 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e822007-05-05 11:45:53 -07003680 "channel after scan\n", dev->name);
3681
Johannes Berg4150c572007-09-17 01:29:23 -04003682
3683 netif_tx_lock_bh(local->mdev);
3684 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3685 local->ops->configure_filter(local_to_hw(local),
3686 FIF_BCN_PRBRESP_PROMISC,
3687 &local->filter_flags,
3688 local->mdev->mc_count,
3689 local->mdev->mc_list);
3690
3691 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003692
Johannes Berg79010422007-09-18 17:29:21 -04003693 rcu_read_lock();
3694 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cb2007-06-08 15:31:13 +02003695
3696 /* No need to wake the master device. */
3697 if (sdata->dev == local->mdev)
3698 continue;
3699
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003700 /* Tell AP we're back */
3701 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3702 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3703 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cb2007-06-08 15:31:13 +02003704
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003705 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003706
Jiri Bencf0706e822007-05-05 11:45:53 -07003707 netif_wake_queue(sdata->dev);
3708 }
Johannes Berg79010422007-09-18 17:29:21 -04003709 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07003710
Zhu Yiece8edd2007-11-22 10:53:21 +08003711done:
Jiri Bencf0706e822007-05-05 11:45:53 -07003712 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003713 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003714 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003715 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e822007-05-05 11:45:53 -07003716 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3717 !ieee80211_sta_active_ibss(dev)))
3718 ieee80211_sta_find_ibss(dev, ifsta);
3719 }
3720}
3721EXPORT_SYMBOL(ieee80211_scan_completed);
3722
3723void ieee80211_sta_scan_work(struct work_struct *work)
3724{
3725 struct ieee80211_local *local =
3726 container_of(work, struct ieee80211_local, scan_work.work);
3727 struct net_device *dev = local->scan_dev;
3728 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003729 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -07003730 struct ieee80211_channel *chan;
3731 int skip;
3732 unsigned long next_delay = 0;
3733
Zhu Yiece8edd2007-11-22 10:53:21 +08003734 if (!local->sta_sw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07003735 return;
3736
3737 switch (local->scan_state) {
3738 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003739 /*
3740 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3741 * after we successfully scanned the last channel of the last
3742 * band (and the last band is supported by the hw)
3743 */
Johannes Berg8318d782008-01-24 19:38:38 +01003744 if (local->scan_band < IEEE80211_NUM_BANDS)
3745 sband = local->hw.wiphy->bands[local->scan_band];
3746 else
3747 sband = NULL;
3748
Johannes Berg69d464d2008-01-28 17:08:56 +01003749 /*
3750 * If we are at an unsupported band and have more bands
3751 * left to scan, advance to the next supported one.
3752 */
3753 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003754 local->scan_band++;
3755 sband = local->hw.wiphy->bands[local->scan_band];
3756 local->scan_channel_idx = 0;
3757 }
3758
Johannes Berg69d464d2008-01-28 17:08:56 +01003759 /* if no more bands/channels left, complete scan */
3760 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003761 ieee80211_scan_completed(local_to_hw(local));
3762 return;
3763 }
Johannes Berg8318d782008-01-24 19:38:38 +01003764 skip = 0;
3765 chan = &sband->channels[local->scan_channel_idx];
3766
3767 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003768 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003769 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e822007-05-05 11:45:53 -07003770 skip = 1;
3771
3772 if (!skip) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003773 local->scan_channel = chan;
3774 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003775 printk(KERN_DEBUG "%s: failed to set freq to "
3776 "%d MHz for scan\n", dev->name,
3777 chan->center_freq);
Jiri Bencf0706e822007-05-05 11:45:53 -07003778 skip = 1;
3779 }
3780 }
3781
Johannes Berg69d464d2008-01-28 17:08:56 +01003782 /* advance state machine to next channel/band */
Jiri Bencf0706e822007-05-05 11:45:53 -07003783 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003784 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003785 /*
3786 * scan_band may end up == IEEE80211_NUM_BANDS, but
3787 * we'll catch that case above and complete the scan
3788 * if that is the case.
3789 */
Johannes Berg8318d782008-01-24 19:38:38 +01003790 local->scan_band++;
3791 local->scan_channel_idx = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07003792 }
3793
3794 if (skip)
3795 break;
3796
3797 next_delay = IEEE80211_PROBE_DELAY +
3798 usecs_to_jiffies(local->hw.channel_change_time);
3799 local->scan_state = SCAN_SEND_PROBE;
3800 break;
3801 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003802 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e822007-05-05 11:45:53 -07003803 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003804
3805 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3806 break;
3807 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3808 local->scan_ssid_len);
3809 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e822007-05-05 11:45:53 -07003810 break;
3811 }
3812
Zhu Yiece8edd2007-11-22 10:53:21 +08003813 if (local->sta_sw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07003814 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3815 next_delay);
3816}
3817
3818
3819static int ieee80211_sta_start_scan(struct net_device *dev,
3820 u8 *ssid, size_t ssid_len)
3821{
3822 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3823 struct ieee80211_sub_if_data *sdata;
3824
3825 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3826 return -EINVAL;
3827
3828 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3829 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3830 * BSSID: MACAddress
3831 * SSID
3832 * ScanType: ACTIVE, PASSIVE
3833 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3834 * a Probe frame during active scanning
3835 * ChannelList
3836 * MinChannelTime (>= ProbeDelay), in TU
3837 * MaxChannelTime: (>= MinChannelTime), in TU
3838 */
3839
3840 /* MLME-SCAN.confirm
3841 * BSSDescriptionSet
3842 * ResultCode: SUCCESS, INVALID_PARAMETERS
3843 */
3844
Zhu Yiece8edd2007-11-22 10:53:21 +08003845 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003846 if (local->scan_dev == dev)
3847 return 0;
3848 return -EBUSY;
3849 }
3850
3851 if (local->ops->hw_scan) {
3852 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003853 ssid, ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003854 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003855 local->sta_hw_scanning = 1;
Jiri Bencf0706e822007-05-05 11:45:53 -07003856 local->scan_dev = dev;
3857 }
3858 return rc;
3859 }
3860
Zhu Yiece8edd2007-11-22 10:53:21 +08003861 local->sta_sw_scanning = 1;
Jiri Bencf0706e822007-05-05 11:45:53 -07003862
Johannes Berg79010422007-09-18 17:29:21 -04003863 rcu_read_lock();
3864 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cb2007-06-08 15:31:13 +02003865
3866 /* Don't stop the master interface, otherwise we can't transmit
3867 * probes! */
3868 if (sdata->dev == local->mdev)
3869 continue;
3870
Jiri Bencf0706e822007-05-05 11:45:53 -07003871 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003872 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003873 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e822007-05-05 11:45:53 -07003874 ieee80211_send_nullfunc(local, sdata, 1);
3875 }
Johannes Berg79010422007-09-18 17:29:21 -04003876 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07003877
3878 if (ssid) {
3879 local->scan_ssid_len = ssid_len;
3880 memcpy(local->scan_ssid, ssid, ssid_len);
3881 } else
3882 local->scan_ssid_len = 0;
3883 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e822007-05-05 11:45:53 -07003884 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003885 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e822007-05-05 11:45:53 -07003886 local->scan_dev = dev;
3887
Johannes Berg4150c572007-09-17 01:29:23 -04003888 netif_tx_lock_bh(local->mdev);
3889 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3890 local->ops->configure_filter(local_to_hw(local),
3891 FIF_BCN_PRBRESP_PROMISC,
3892 &local->filter_flags,
3893 local->mdev->mc_count,
3894 local->mdev->mc_list);
3895 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003896
3897 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3898 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3899 IEEE80211_CHANNEL_TIME);
3900
3901 return 0;
3902}
3903
3904
3905int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3906{
3907 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3908 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3909 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3910
Johannes Berg51fb61e2007-12-19 01:31:27 +01003911 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07003912 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3913
Zhu Yiece8edd2007-11-22 10:53:21 +08003914 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003915 if (local->scan_dev == dev)
3916 return 0;
3917 return -EBUSY;
3918 }
3919
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003920 ifsta->scan_ssid_len = ssid_len;
3921 if (ssid_len)
3922 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003923 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3924 queue_work(local->hw.workqueue, &ifsta->work);
3925 return 0;
3926}
3927
3928static char *
3929ieee80211_sta_scan_result(struct net_device *dev,
3930 struct ieee80211_sta_bss *bss,
3931 char *current_ev, char *end_buf)
3932{
3933 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3934 struct iw_event iwe;
3935
3936 if (time_after(jiffies,
3937 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3938 return current_ev;
3939
Jiri Bencf0706e822007-05-05 11:45:53 -07003940 memset(&iwe, 0, sizeof(iwe));
3941 iwe.cmd = SIOCGIWAP;
3942 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3943 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3944 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3945 IW_EV_ADDR_LEN);
3946
3947 memset(&iwe, 0, sizeof(iwe));
3948 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01003949 if (bss_mesh_cfg(bss)) {
3950 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003951 iwe.u.data.flags = 1;
3952 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
Johannes Berg902acc72008-02-23 15:17:19 +01003953 bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003954 } else {
3955 iwe.u.data.length = bss->ssid_len;
3956 iwe.u.data.flags = 1;
3957 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3958 bss->ssid);
3959 }
Jiri Bencf0706e822007-05-05 11:45:53 -07003960
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08003961 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
3962 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003963 memset(&iwe, 0, sizeof(iwe));
3964 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01003965 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003966 iwe.u.mode = IW_MODE_MESH;
3967 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e822007-05-05 11:45:53 -07003968 iwe.u.mode = IW_MODE_MASTER;
3969 else
3970 iwe.u.mode = IW_MODE_ADHOC;
3971 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3972 IW_EV_UINT_LEN);
3973 }
3974
3975 memset(&iwe, 0, sizeof(iwe));
3976 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01003977 iwe.u.freq.m = bss->freq;
3978 iwe.u.freq.e = 6;
Jiri Bencf0706e822007-05-05 11:45:53 -07003979 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3980 IW_EV_FREQ_LEN);
Johannes Berg8318d782008-01-24 19:38:38 +01003981
3982 memset(&iwe, 0, sizeof(iwe));
3983 iwe.cmd = SIOCGIWFREQ;
3984 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3985 iwe.u.freq.e = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07003986 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3987 IW_EV_FREQ_LEN);
3988
3989 memset(&iwe, 0, sizeof(iwe));
3990 iwe.cmd = IWEVQUAL;
3991 iwe.u.qual.qual = bss->signal;
3992 iwe.u.qual.level = bss->rssi;
3993 iwe.u.qual.noise = bss->noise;
3994 iwe.u.qual.updated = local->wstats_flags;
3995 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3996 IW_EV_QUAL_LEN);
3997
3998 memset(&iwe, 0, sizeof(iwe));
3999 iwe.cmd = SIOCGIWENCODE;
4000 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4001 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4002 else
4003 iwe.u.data.flags = IW_ENCODE_DISABLED;
4004 iwe.u.data.length = 0;
4005 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4006
4007 if (bss && bss->wpa_ie) {
4008 memset(&iwe, 0, sizeof(iwe));
4009 iwe.cmd = IWEVGENIE;
4010 iwe.u.data.length = bss->wpa_ie_len;
4011 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4012 bss->wpa_ie);
4013 }
4014
4015 if (bss && bss->rsn_ie) {
4016 memset(&iwe, 0, sizeof(iwe));
4017 iwe.cmd = IWEVGENIE;
4018 iwe.u.data.length = bss->rsn_ie_len;
4019 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4020 bss->rsn_ie);
4021 }
4022
4023 if (bss && bss->supp_rates_len > 0) {
4024 /* display all supported rates in readable format */
4025 char *p = current_ev + IW_EV_LCP_LEN;
4026 int i;
4027
4028 memset(&iwe, 0, sizeof(iwe));
4029 iwe.cmd = SIOCGIWRATE;
4030 /* Those two flags are ignored... */
4031 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4032
4033 for (i = 0; i < bss->supp_rates_len; i++) {
4034 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4035 0x7f) * 500000);
4036 p = iwe_stream_add_value(current_ev, p,
4037 end_buf, &iwe, IW_EV_PARAM_LEN);
4038 }
4039 current_ev = p;
4040 }
4041
4042 if (bss) {
4043 char *buf;
4044 buf = kmalloc(30, GFP_ATOMIC);
4045 if (buf) {
4046 memset(&iwe, 0, sizeof(iwe));
4047 iwe.cmd = IWEVCUSTOM;
4048 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4049 iwe.u.data.length = strlen(buf);
4050 current_ev = iwe_stream_add_point(current_ev, end_buf,
4051 &iwe, buf);
4052 kfree(buf);
4053 }
4054 }
4055
Johannes Berg902acc72008-02-23 15:17:19 +01004056 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004057 char *buf;
Johannes Berg902acc72008-02-23 15:17:19 +01004058 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004059 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004060 if (buf) {
4061 memset(&iwe, 0, sizeof(iwe));
4062 iwe.cmd = IWEVCUSTOM;
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004063 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4064 iwe.u.data.length = strlen(buf);
4065 current_ev = iwe_stream_add_point(current_ev, end_buf,
4066 &iwe, buf);
4067 sprintf(buf, "Path Selection Protocol ID: "
4068 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4069 cfg[4]);
4070 iwe.u.data.length = strlen(buf);
4071 current_ev = iwe_stream_add_point(current_ev, end_buf,
4072 &iwe, buf);
4073 sprintf(buf, "Path Selection Metric ID: "
4074 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4075 cfg[8]);
4076 iwe.u.data.length = strlen(buf);
4077 current_ev = iwe_stream_add_point(current_ev, end_buf,
4078 &iwe, buf);
4079 sprintf(buf, "Congestion Control Mode ID: "
4080 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4081 cfg[11], cfg[12]);
4082 iwe.u.data.length = strlen(buf);
4083 current_ev = iwe_stream_add_point(current_ev, end_buf,
4084 &iwe, buf);
4085 sprintf(buf, "Channel Precedence: "
4086 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4087 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004088 iwe.u.data.length = strlen(buf);
4089 current_ev = iwe_stream_add_point(current_ev, end_buf,
4090 &iwe, buf);
4091 kfree(buf);
4092 }
4093 }
4094
Jiri Bencf0706e822007-05-05 11:45:53 -07004095 return current_ev;
4096}
4097
4098
4099int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4100{
4101 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4102 char *current_ev = buf;
4103 char *end_buf = buf + len;
4104 struct ieee80211_sta_bss *bss;
4105
4106 spin_lock_bh(&local->sta_bss_lock);
4107 list_for_each_entry(bss, &local->sta_bss_list, list) {
4108 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4109 spin_unlock_bh(&local->sta_bss_lock);
4110 return -E2BIG;
4111 }
4112 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4113 end_buf);
4114 }
4115 spin_unlock_bh(&local->sta_bss_lock);
4116 return current_ev - buf;
4117}
4118
4119
4120int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4121{
4122 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4123 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4124 kfree(ifsta->extra_ie);
4125 if (len == 0) {
4126 ifsta->extra_ie = NULL;
4127 ifsta->extra_ie_len = 0;
4128 return 0;
4129 }
4130 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4131 if (!ifsta->extra_ie) {
4132 ifsta->extra_ie_len = 0;
4133 return -ENOMEM;
4134 }
4135 memcpy(ifsta->extra_ie, ie, len);
4136 ifsta->extra_ie_len = len;
4137 return 0;
4138}
4139
4140
4141struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4142 struct sk_buff *skb, u8 *bssid,
4143 u8 *addr)
4144{
4145 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4146 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004147 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004148 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07004149
4150 /* TODO: Could consider removing the least recently used entry and
4151 * allow new one to be added. */
4152 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4153 if (net_ratelimit()) {
4154 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004155 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e822007-05-05 11:45:53 -07004156 }
4157 return NULL;
4158 }
4159
Joe Perches0795af52007-10-03 17:59:30 -07004160 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004161 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e822007-05-05 11:45:53 -07004162
Johannes Berg73651ee2008-02-25 16:27:47 +01004163 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4164 if (!sta)
Jiri Bencf0706e822007-05-05 11:45:53 -07004165 return NULL;
4166
Johannes Berg238814f2008-01-28 17:19:37 +01004167 sta->flags |= WLAN_STA_AUTHORIZED;
4168
Johannes Berg8318d782008-01-24 19:38:38 +01004169 sta->supp_rates[local->hw.conf.channel->band] =
4170 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e822007-05-05 11:45:53 -07004171
4172 rate_control_rate_init(sta, local);
4173
Johannes Berg93e5deb2008-04-01 15:21:00 +02004174 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004175 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004176
Johannes Bergd0709a62008-02-25 16:27:46 +01004177 return sta;
Jiri Bencf0706e822007-05-05 11:45:53 -07004178}
4179
4180
4181int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4182{
4183 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4184 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4185
4186 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4187 dev->name, reason);
4188
Johannes Berg51fb61e2007-12-19 01:31:27 +01004189 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4190 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e822007-05-05 11:45:53 -07004191 return -EINVAL;
4192
4193 ieee80211_send_deauth(dev, ifsta, reason);
4194 ieee80211_set_disassoc(dev, ifsta, 1);
4195 return 0;
4196}
4197
4198
4199int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4200{
4201 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4202 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4203
4204 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4205 dev->name, reason);
4206
Johannes Berg51fb61e2007-12-19 01:31:27 +01004207 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07004208 return -EINVAL;
4209
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004210 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e822007-05-05 11:45:53 -07004211 return -1;
4212
4213 ieee80211_send_disassoc(dev, ifsta, reason);
4214 ieee80211_set_disassoc(dev, ifsta, 0);
4215 return 0;
4216}