blob: b404537c0bcd1a470f4f7e305ab18c1e263c0b34 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
Jiri Bencf0706e82007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010027#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040033#include "rate.h"
34#include "led.h"
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010035#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
37#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40#define IEEE80211_ASSOC_MAX_TRIES 3
41#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010042#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070043#define IEEE80211_PROBE_INTERVAL (60 * HZ)
44#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45#define IEEE80211_SCAN_INTERVAL (2 * HZ)
46#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
Dan Williams872ba532008-06-04 13:59:34 -040047#define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070048
49#define IEEE80211_PROBE_DELAY (HZ / 33)
50#define IEEE80211_CHANNEL_TIME (HZ / 33)
51#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010055#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070056
57#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
Jiri Bencf0706e82007-05-05 11:45:53 -070060#define ERP_INFO_USE_PROTECTION BIT(1)
61
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020062/* mgmt header + 1 byte action code */
63#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
64
65#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
66#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
67#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020068#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
69#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020070
Ron Rindjunsky07db2182007-12-25 17:00:33 +020071/* next values represent the buffer size for A-MPDU frame.
72 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
73#define IEEE80211_MIN_AMPDU_BUF 0x8
74#define IEEE80211_MAX_AMPDU_BUF 0x40
75
Jiri Bencf0706e82007-05-05 11:45:53 -070076static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
77 u8 *ssid, size_t ssid_len);
78static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +010079ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -040080 u8 *ssid, u8 ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070081static void ieee80211_rx_bss_put(struct net_device *dev,
82 struct ieee80211_sta_bss *bss);
83static int ieee80211_sta_find_ibss(struct net_device *dev,
84 struct ieee80211_if_sta *ifsta);
85static int ieee80211_sta_wep_configured(struct net_device *dev);
86static int ieee80211_sta_start_scan(struct net_device *dev,
87 u8 *ssid, size_t ssid_len);
88static int ieee80211_sta_config_auth(struct net_device *dev,
89 struct ieee80211_if_sta *ifsta);
90
91
Luis Carlos Coboee385852008-02-23 15:17:11 +010092void ieee802_11_parse_elems(u8 *start, size_t len,
93 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -070094{
95 size_t left = len;
96 u8 *pos = start;
Jiri Bencf0706e82007-05-05 11:45:53 -070097
98 memset(elems, 0, sizeof(*elems));
99
100 while (left >= 2) {
101 u8 id, elen;
102
103 id = *pos++;
104 elen = *pos++;
105 left -= 2;
106
John W. Linville67a4cce2007-10-12 16:40:37 -0400107 if (elen > left)
108 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700109
110 switch (id) {
111 case WLAN_EID_SSID:
112 elems->ssid = pos;
113 elems->ssid_len = elen;
114 break;
115 case WLAN_EID_SUPP_RATES:
116 elems->supp_rates = pos;
117 elems->supp_rates_len = elen;
118 break;
119 case WLAN_EID_FH_PARAMS:
120 elems->fh_params = pos;
121 elems->fh_params_len = elen;
122 break;
123 case WLAN_EID_DS_PARAMS:
124 elems->ds_params = pos;
125 elems->ds_params_len = elen;
126 break;
127 case WLAN_EID_CF_PARAMS:
128 elems->cf_params = pos;
129 elems->cf_params_len = elen;
130 break;
131 case WLAN_EID_TIM:
132 elems->tim = pos;
133 elems->tim_len = elen;
134 break;
135 case WLAN_EID_IBSS_PARAMS:
136 elems->ibss_params = pos;
137 elems->ibss_params_len = elen;
138 break;
139 case WLAN_EID_CHALLENGE:
140 elems->challenge = pos;
141 elems->challenge_len = elen;
142 break;
143 case WLAN_EID_WPA:
144 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
145 pos[2] == 0xf2) {
146 /* Microsoft OUI (00:50:F2) */
147 if (pos[3] == 1) {
148 /* OUI Type 1 - WPA IE */
149 elems->wpa = pos;
150 elems->wpa_len = elen;
151 } else if (elen >= 5 && pos[3] == 2) {
152 if (pos[4] == 0) {
153 elems->wmm_info = pos;
154 elems->wmm_info_len = elen;
155 } else if (pos[4] == 1) {
156 elems->wmm_param = pos;
157 elems->wmm_param_len = elen;
158 }
159 }
160 }
161 break;
162 case WLAN_EID_RSN:
163 elems->rsn = pos;
164 elems->rsn_len = elen;
165 break;
166 case WLAN_EID_ERP_INFO:
167 elems->erp_info = pos;
168 elems->erp_info_len = elen;
169 break;
170 case WLAN_EID_EXT_SUPP_RATES:
171 elems->ext_supp_rates = pos;
172 elems->ext_supp_rates_len = elen;
173 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200174 case WLAN_EID_HT_CAPABILITY:
175 elems->ht_cap_elem = pos;
176 elems->ht_cap_elem_len = elen;
177 break;
178 case WLAN_EID_HT_EXTRA_INFO:
179 elems->ht_info_elem = pos;
180 elems->ht_info_elem_len = elen;
181 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100182 case WLAN_EID_MESH_ID:
183 elems->mesh_id = pos;
184 elems->mesh_id_len = elen;
185 break;
186 case WLAN_EID_MESH_CONFIG:
187 elems->mesh_config = pos;
188 elems->mesh_config_len = elen;
189 break;
190 case WLAN_EID_PEER_LINK:
191 elems->peer_link = pos;
192 elems->peer_link_len = elen;
193 break;
194 case WLAN_EID_PREQ:
195 elems->preq = pos;
196 elems->preq_len = elen;
197 break;
198 case WLAN_EID_PREP:
199 elems->prep = pos;
200 elems->prep_len = elen;
201 break;
202 case WLAN_EID_PERR:
203 elems->perr = pos;
204 elems->perr_len = elen;
205 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700206 default:
Jiri Bencf0706e82007-05-05 11:45:53 -0700207 break;
208 }
209
210 left -= elen;
211 pos += elen;
212 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700213}
214
215
Jiri Bencf0706e82007-05-05 11:45:53 -0700216static int ecw2cw(int ecw)
217{
Johannes Bergac2bf322008-02-14 00:30:35 +0100218 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700219}
220
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100221
222static void ieee80211_sta_def_wmm_params(struct net_device *dev,
223 struct ieee80211_sta_bss *bss,
224 int ibss)
225{
226 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
227 struct ieee80211_local *local = sdata->local;
228 int i, have_higher_than_11mbit = 0;
229
230
231 /* cf. IEEE 802.11 9.2.12 */
232 for (i = 0; i < bss->supp_rates_len; i++)
233 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
234 have_higher_than_11mbit = 1;
235
236 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
237 have_higher_than_11mbit)
238 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
239 else
240 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
241
242
243 if (local->ops->conf_tx) {
244 struct ieee80211_tx_queue_params qparam;
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100245
246 memset(&qparam, 0, sizeof(qparam));
247
248 qparam.aifs = 2;
249
250 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
251 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
252 qparam.cw_min = 31;
253 else
254 qparam.cw_min = 15;
255
256 qparam.cw_max = 1023;
257 qparam.txop = 0;
258
259 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
260 local->ops->conf_tx(local_to_hw(local),
261 i + IEEE80211_TX_QUEUE_DATA0,
262 &qparam);
263
264 if (ibss) {
265 /* IBSS uses different parameters for Beacon sending */
266 qparam.cw_min++;
267 qparam.cw_min *= 2;
268 qparam.cw_min--;
269 local->ops->conf_tx(local_to_hw(local),
270 IEEE80211_TX_QUEUE_BEACON, &qparam);
271 }
272 }
273}
274
Jiri Bencf0706e82007-05-05 11:45:53 -0700275static void ieee80211_sta_wmm_params(struct net_device *dev,
276 struct ieee80211_if_sta *ifsta,
277 u8 *wmm_param, size_t wmm_param_len)
278{
279 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
280 struct ieee80211_tx_queue_params params;
281 size_t left;
282 int count;
283 u8 *pos;
284
285 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
286 return;
287 count = wmm_param[6] & 0x0f;
288 if (count == ifsta->wmm_last_param_set)
289 return;
290 ifsta->wmm_last_param_set = count;
291
292 pos = wmm_param + 8;
293 left = wmm_param_len - 8;
294
295 memset(&params, 0, sizeof(params));
296
297 if (!local->ops->conf_tx)
298 return;
299
300 local->wmm_acm = 0;
301 for (; left >= 4; left -= 4, pos += 4) {
302 int aci = (pos[0] >> 5) & 0x03;
303 int acm = (pos[0] >> 4) & 0x01;
304 int queue;
305
306 switch (aci) {
307 case 1:
308 queue = IEEE80211_TX_QUEUE_DATA3;
309 if (acm) {
310 local->wmm_acm |= BIT(0) | BIT(3);
311 }
312 break;
313 case 2:
314 queue = IEEE80211_TX_QUEUE_DATA1;
315 if (acm) {
316 local->wmm_acm |= BIT(4) | BIT(5);
317 }
318 break;
319 case 3:
320 queue = IEEE80211_TX_QUEUE_DATA0;
321 if (acm) {
322 local->wmm_acm |= BIT(6) | BIT(7);
323 }
324 break;
325 case 0:
326 default:
327 queue = IEEE80211_TX_QUEUE_DATA2;
328 if (acm) {
329 local->wmm_acm |= BIT(1) | BIT(2);
330 }
331 break;
332 }
333
334 params.aifs = pos[0] & 0x0f;
335 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
336 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100337 params.txop = pos[2] | (pos[3] << 8);
338#ifdef CONFIG_MAC80211_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700339 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100340 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e82007-05-05 11:45:53 -0700341 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100342 params.cw_max, params.txop);
343#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700344 /* TODO: handle ACM (block TX, fallback to next lowest allowed
345 * AC for now) */
346 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
347 printk(KERN_DEBUG "%s: failed to set TX queue "
348 "parameters for queue %d\n", dev->name, queue);
349 }
350 }
351}
352
John W. Linville50c4afb2008-04-15 14:09:27 -0400353static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
354 bool use_protection,
355 bool use_short_preamble)
Daniel Drake56282212007-07-10 19:32:10 +0200356{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100357 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Daniel Drake56282212007-07-10 19:32:10 +0200358 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Joe Perches0795af52007-10-03 17:59:30 -0700359 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100360 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200361
Johannes Berg471b3ef2007-12-28 14:32:58 +0100362 if (use_protection != bss_conf->use_cts_prot) {
Daniel Drake56282212007-07-10 19:32:10 +0200363 if (net_ratelimit()) {
364 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700365 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100366 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200367 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700368 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200369 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100370 bss_conf->use_cts_prot = use_protection;
371 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200372 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200373
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200374 if (use_short_preamble != bss_conf->use_short_preamble) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200375 if (net_ratelimit()) {
376 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700377 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100378 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200379 use_short_preamble ? "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700380 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200381 }
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200382 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100383 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200384 }
Daniel Draked9430a32007-07-27 15:43:24 +0200385
Johannes Berg471b3ef2007-12-28 14:32:58 +0100386 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200387}
388
John W. Linville50c4afb2008-04-15 14:09:27 -0400389static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
390 u8 erp_value)
391{
392 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
393 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
394
395 return ieee80211_handle_protect_preamb(sdata,
396 use_protection, use_short_preamble);
397}
398
399static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
400 struct ieee80211_sta_bss *bss)
401{
402 u32 changed = 0;
403
404 if (bss->has_erp_value)
405 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
406 else {
407 u16 capab = bss->capability;
408 changed |= ieee80211_handle_protect_preamb(sdata, false,
409 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
410 }
411
412 return changed;
413}
414
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200415int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
416 struct ieee80211_ht_info *ht_info)
417{
418
419 if (ht_info == NULL)
420 return -EINVAL;
421
422 memset(ht_info, 0, sizeof(*ht_info));
423
424 if (ht_cap_ie) {
425 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
426
427 ht_info->ht_supported = 1;
428 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
429 ht_info->ampdu_factor =
430 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
431 ht_info->ampdu_density =
432 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
433 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
434 } else
435 ht_info->ht_supported = 0;
436
437 return 0;
438}
439
440int ieee80211_ht_addt_info_ie_to_ht_bss_info(
441 struct ieee80211_ht_addt_info *ht_add_info_ie,
442 struct ieee80211_ht_bss_info *bss_info)
443{
444 if (bss_info == NULL)
445 return -EINVAL;
446
447 memset(bss_info, 0, sizeof(*bss_info));
448
449 if (ht_add_info_ie) {
450 u16 op_mode;
451 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
452
453 bss_info->primary_channel = ht_add_info_ie->control_chan;
454 bss_info->bss_cap = ht_add_info_ie->ht_param;
455 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
456 }
457
458 return 0;
459}
Daniel Drake56282212007-07-10 19:32:10 +0200460
Jiri Bencf0706e82007-05-05 11:45:53 -0700461static void ieee80211_sta_send_associnfo(struct net_device *dev,
462 struct ieee80211_if_sta *ifsta)
463{
464 char *buf;
465 size_t len;
466 int i;
467 union iwreq_data wrqu;
468
469 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
470 return;
471
472 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200473 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700474 if (!buf)
475 return;
476
477 len = sprintf(buf, "ASSOCINFO(");
478 if (ifsta->assocreq_ies) {
479 len += sprintf(buf + len, "ReqIEs=");
480 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
481 len += sprintf(buf + len, "%02x",
482 ifsta->assocreq_ies[i]);
483 }
484 }
485 if (ifsta->assocresp_ies) {
486 if (ifsta->assocreq_ies)
487 len += sprintf(buf + len, " ");
488 len += sprintf(buf + len, "RespIEs=");
489 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
490 len += sprintf(buf + len, "%02x",
491 ifsta->assocresp_ies[i]);
492 }
493 }
494 len += sprintf(buf + len, ")");
495
496 if (len > IW_CUSTOM_MAX) {
497 len = sprintf(buf, "ASSOCRESPIE=");
498 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
499 len += sprintf(buf + len, "%02x",
500 ifsta->assocresp_ies[i]);
501 }
502 }
503
504 memset(&wrqu, 0, sizeof(wrqu));
505 wrqu.data.length = len;
506 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
507
508 kfree(buf);
509}
510
511
512static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400513 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200514 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700515{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100516 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
517 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700518 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700519 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100520 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700521
Jiri Bencf0706e82007-05-05 11:45:53 -0700522 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200523 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400524
525 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
526
Johannes Berg51fb61e2007-12-19 01:31:27 +0100527 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700528 return;
Daniel Drake56282212007-07-10 19:32:10 +0200529
John W. Linville65c107a2007-10-05 14:23:27 -0400530 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Tomas Winkler38668c02008-03-28 16:33:32 -0700531 conf->channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400532 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200533 if (bss) {
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700534 /* set timing information */
535 sdata->bss_conf.beacon_int = bss->beacon_int;
536 sdata->bss_conf.timestamp = bss->timestamp;
537
John W. Linville50c4afb2008-04-15 14:09:27 -0400538 changed |= ieee80211_handle_bss_capability(sdata, bss);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700539
Daniel Drake56282212007-07-10 19:32:10 +0200540 ieee80211_rx_bss_put(dev, bss);
541 }
542
Tomas Winkler38668c02008-03-28 16:33:32 -0700543 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
544 changed |= BSS_CHANGED_HT;
545 sdata->bss_conf.assoc_ht = 1;
546 sdata->bss_conf.ht_conf = &conf->ht_conf;
547 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
548 }
549
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400550 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700551 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
552 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
553 ieee80211_sta_send_associnfo(dev, ifsta);
554 } else {
Guy Cohen8db93692008-07-03 19:56:13 +0300555 netif_carrier_off(dev);
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700556 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400557 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Daniel Draked9430a32007-07-27 15:43:24 +0200558 ieee80211_reset_erp_info(dev);
Tomas Winkler38668c02008-03-28 16:33:32 -0700559
560 sdata->bss_conf.assoc_ht = 0;
561 sdata->bss_conf.ht_conf = NULL;
562 sdata->bss_conf.ht_bss_conf = NULL;
563
Jiri Bencf0706e82007-05-05 11:45:53 -0700564 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
565 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700566 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200567 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100568
Tomas Winklerb2205252008-01-27 16:18:22 +0200569 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100570 ieee80211_bss_info_change_notify(sdata, changed);
Guy Cohen8db93692008-07-03 19:56:13 +0300571
572 if (assoc)
573 netif_carrier_on(dev);
574
Reinette Chatre41a7be42008-04-03 16:08:49 -0700575 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
576 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700577}
578
579static void ieee80211_set_disassoc(struct net_device *dev,
580 struct ieee80211_if_sta *ifsta, int deauth)
581{
582 if (deauth)
583 ifsta->auth_tries = 0;
584 ifsta->assoc_tries = 0;
585 ieee80211_set_associated(dev, ifsta, 0);
586}
587
Luis Carlos Coboee385852008-02-23 15:17:11 +0100588void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
589 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700590{
591 struct ieee80211_sub_if_data *sdata;
592 struct ieee80211_tx_packet_data *pkt_data;
593
594 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
595 skb->dev = sdata->local->mdev;
596 skb_set_mac_header(skb, 0);
597 skb_set_network_header(skb, 0);
598 skb_set_transport_header(skb, 0);
599
600 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
601 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
602 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400603 if (!encrypt)
604 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700605
606 dev_queue_xmit(skb);
607}
608
609
610static void ieee80211_send_auth(struct net_device *dev,
611 struct ieee80211_if_sta *ifsta,
612 int transaction, u8 *extra, size_t extra_len,
613 int encrypt)
614{
615 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
616 struct sk_buff *skb;
617 struct ieee80211_mgmt *mgmt;
618
619 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
620 sizeof(*mgmt) + 6 + extra_len);
621 if (!skb) {
622 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
623 "frame\n", dev->name);
624 return;
625 }
626 skb_reserve(skb, local->hw.extra_tx_headroom);
627
628 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
629 memset(mgmt, 0, 24 + 6);
630 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
631 IEEE80211_STYPE_AUTH);
632 if (encrypt)
633 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
634 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
635 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
636 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
637 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
638 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
639 ifsta->auth_transaction = transaction + 1;
640 mgmt->u.auth.status_code = cpu_to_le16(0);
641 if (extra)
642 memcpy(skb_put(skb, extra_len), extra, extra_len);
643
644 ieee80211_sta_tx(dev, skb, encrypt);
645}
646
647
648static void ieee80211_authenticate(struct net_device *dev,
649 struct ieee80211_if_sta *ifsta)
650{
Joe Perches0795af52007-10-03 17:59:30 -0700651 DECLARE_MAC_BUF(mac);
652
Jiri Bencf0706e82007-05-05 11:45:53 -0700653 ifsta->auth_tries++;
654 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700655 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700656 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700657 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700658 ifsta->state = IEEE80211_DISABLED;
659 return;
660 }
661
662 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700663 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
664 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700665
666 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
667
668 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
669}
670
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200671static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
672 struct ieee80211_supported_band *sband,
673 u64 *rates)
674{
675 int i, j, count;
676 *rates = 0;
677 count = 0;
678 for (i = 0; i < bss->supp_rates_len; i++) {
679 int rate = (bss->supp_rates[i] & 0x7F) * 5;
680
681 for (j = 0; j < sband->n_bitrates; j++)
682 if (sband->bitrates[j].bitrate == rate) {
683 *rates |= BIT(j);
684 count++;
685 break;
686 }
687 }
688
689 return count;
690}
Jiri Bencf0706e82007-05-05 11:45:53 -0700691
692static void ieee80211_send_assoc(struct net_device *dev,
693 struct ieee80211_if_sta *ifsta)
694{
695 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700696 struct sk_buff *skb;
697 struct ieee80211_mgmt *mgmt;
698 u8 *pos, *ies;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200699 int i, len, count, rates_len, supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700700 u16 capab;
701 struct ieee80211_sta_bss *bss;
702 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100703 struct ieee80211_supported_band *sband;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200704 u64 rates = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700705
706 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
707 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
708 ifsta->ssid_len);
709 if (!skb) {
710 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
711 "frame\n", dev->name);
712 return;
713 }
714 skb_reserve(skb, local->hw.extra_tx_headroom);
715
Johannes Berg8318d782008-01-24 19:38:38 +0100716 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
717
Jiri Bencf0706e82007-05-05 11:45:53 -0700718 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100719
720 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
721 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
722 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
723 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
724 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700725 }
Johannes Berg8318d782008-01-24 19:38:38 +0100726
727 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
728 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400729 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700730 if (bss) {
731 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
732 capab |= WLAN_CAPABILITY_PRIVACY;
733 if (bss->wmm_ie) {
734 wmm = 1;
735 }
Helmut Schaa0d580a72008-05-20 09:56:37 +0200736
737 /* get all rates supported by the device and the AP as
738 * some APs don't like getting a superset of their rates
739 * in the association request (e.g. D-Link DAP 1353 in
740 * b-only mode) */
741 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
742
Jiri Bencf0706e82007-05-05 11:45:53 -0700743 ieee80211_rx_bss_put(dev, bss);
Helmut Schaa0d580a72008-05-20 09:56:37 +0200744 } else {
745 rates = ~0;
746 rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700747 }
748
749 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
750 memset(mgmt, 0, 24);
751 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
752 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
753 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
754
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400755 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700756 skb_put(skb, 10);
757 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
758 IEEE80211_STYPE_REASSOC_REQ);
759 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
760 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
761 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
762 ETH_ALEN);
763 } else {
764 skb_put(skb, 4);
765 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
766 IEEE80211_STYPE_ASSOC_REQ);
767 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
768 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
769 }
770
771 /* SSID */
772 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
773 *pos++ = WLAN_EID_SSID;
774 *pos++ = ifsta->ssid_len;
775 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
776
Helmut Schaa0d580a72008-05-20 09:56:37 +0200777 /* add all rates which were marked to be used above */
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200778 supp_rates_len = rates_len;
779 if (supp_rates_len > 8)
780 supp_rates_len = 8;
Jiri Bencf0706e82007-05-05 11:45:53 -0700781
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200782 len = sband->n_bitrates;
783 pos = skb_put(skb, supp_rates_len + 2);
784 *pos++ = WLAN_EID_SUPP_RATES;
785 *pos++ = supp_rates_len;
786
787 count = 0;
788 for (i = 0; i < sband->n_bitrates; i++) {
789 if (BIT(i) & rates) {
Johannes Berg8318d782008-01-24 19:38:38 +0100790 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700791 *pos++ = (u8) (rate / 5);
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200792 if (++count == 8)
793 break;
794 }
795 }
796
797 if (count == 8) {
798 pos = skb_put(skb, rates_len - count + 2);
799 *pos++ = WLAN_EID_EXT_SUPP_RATES;
800 *pos++ = rates_len - count;
801
802 for (i++; i < sband->n_bitrates; i++) {
803 if (BIT(i) & rates) {
804 int rate = sband->bitrates[i].bitrate;
805 *pos++ = (u8) (rate / 5);
806 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700807 }
808 }
809
810 if (ifsta->extra_ie) {
811 pos = skb_put(skb, ifsta->extra_ie_len);
812 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
813 }
814
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400815 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700816 pos = skb_put(skb, 9);
817 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
818 *pos++ = 7; /* len */
819 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
820 *pos++ = 0x50;
821 *pos++ = 0xf2;
822 *pos++ = 2; /* WME */
823 *pos++ = 0; /* WME info */
824 *pos++ = 1; /* WME ver */
825 *pos++ = 0;
826 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200827 /* wmm support is a must to HT */
Johannes Berg8318d782008-01-24 19:38:38 +0100828 if (wmm && sband->ht_info.ht_supported) {
829 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200830 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
831 *pos++ = WLAN_EID_HT_CAPABILITY;
832 *pos++ = sizeof(struct ieee80211_ht_cap);
833 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
834 memcpy(pos, &tmp, sizeof(u16));
835 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100836 /* TODO: needs a define here for << 2 */
837 *pos++ = sband->ht_info.ampdu_factor |
838 (sband->ht_info.ampdu_density << 2);
839 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200840 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700841
842 kfree(ifsta->assocreq_ies);
843 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200844 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700845 if (ifsta->assocreq_ies)
846 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
847
848 ieee80211_sta_tx(dev, skb, 0);
849}
850
851
852static void ieee80211_send_deauth(struct net_device *dev,
853 struct ieee80211_if_sta *ifsta, u16 reason)
854{
855 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
856 struct sk_buff *skb;
857 struct ieee80211_mgmt *mgmt;
858
859 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
860 if (!skb) {
861 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
862 "frame\n", dev->name);
863 return;
864 }
865 skb_reserve(skb, local->hw.extra_tx_headroom);
866
867 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
868 memset(mgmt, 0, 24);
869 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
870 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
871 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
872 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
873 IEEE80211_STYPE_DEAUTH);
874 skb_put(skb, 2);
875 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
876
877 ieee80211_sta_tx(dev, skb, 0);
878}
879
880
881static void ieee80211_send_disassoc(struct net_device *dev,
882 struct ieee80211_if_sta *ifsta, u16 reason)
883{
884 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
885 struct sk_buff *skb;
886 struct ieee80211_mgmt *mgmt;
887
888 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
889 if (!skb) {
890 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
891 "frame\n", dev->name);
892 return;
893 }
894 skb_reserve(skb, local->hw.extra_tx_headroom);
895
896 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
897 memset(mgmt, 0, 24);
898 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
899 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
900 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
901 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
902 IEEE80211_STYPE_DISASSOC);
903 skb_put(skb, 2);
904 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
905
906 ieee80211_sta_tx(dev, skb, 0);
907}
908
909
910static int ieee80211_privacy_mismatch(struct net_device *dev,
911 struct ieee80211_if_sta *ifsta)
912{
John W. Linville65c107a2007-10-05 14:23:27 -0400913 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700914 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000915 int bss_privacy;
916 int wep_privacy;
917 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700918
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000919 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700920 return 0;
921
Johannes Berg8318d782008-01-24 19:38:38 +0100922 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
923 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400924 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700925 if (!bss)
926 return 0;
927
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000928 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
929 wep_privacy = !!ieee80211_sta_wep_configured(dev);
930 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700931
932 ieee80211_rx_bss_put(dev, bss);
933
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000934 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
935 return 0;
936
937 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700938}
939
940
941static void ieee80211_associate(struct net_device *dev,
942 struct ieee80211_if_sta *ifsta)
943{
Joe Perches0795af52007-10-03 17:59:30 -0700944 DECLARE_MAC_BUF(mac);
945
Jiri Bencf0706e82007-05-05 11:45:53 -0700946 ifsta->assoc_tries++;
947 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700948 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700949 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700950 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700951 ifsta->state = IEEE80211_DISABLED;
952 return;
953 }
954
955 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700956 printk(KERN_DEBUG "%s: associate with AP %s\n",
957 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700958 if (ieee80211_privacy_mismatch(dev, ifsta)) {
959 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
960 "mixed-cell disabled - abort association\n", dev->name);
961 ifsta->state = IEEE80211_DISABLED;
962 return;
963 }
964
965 ieee80211_send_assoc(dev, ifsta);
966
967 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
968}
969
970
971static void ieee80211_associated(struct net_device *dev,
972 struct ieee80211_if_sta *ifsta)
973{
974 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
975 struct sta_info *sta;
976 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700977 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700978
979 /* TODO: start monitoring current AP signal quality and number of
980 * missed beacons. Scan other channels every now and then and search
981 * for better APs. */
982 /* TODO: remove expired BSSes */
983
984 ifsta->state = IEEE80211_ASSOCIATED;
985
Johannes Bergd0709a62008-02-25 16:27:46 +0100986 rcu_read_lock();
987
Jiri Bencf0706e82007-05-05 11:45:53 -0700988 sta = sta_info_get(local, ifsta->bssid);
989 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700990 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
991 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700992 disassoc = 1;
993 } else {
994 disassoc = 0;
995 if (time_after(jiffies,
996 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400997 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700998 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700999 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -07001000 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -07001001 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001002 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +01001003 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001004 } else
Jiri Bencf0706e82007-05-05 11:45:53 -07001005 ieee80211_send_probe_req(dev, ifsta->bssid,
1006 local->scan_ssid,
1007 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001008 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001009 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001010 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001011 if (time_after(jiffies, ifsta->last_probe +
1012 IEEE80211_PROBE_INTERVAL)) {
1013 ifsta->last_probe = jiffies;
1014 ieee80211_send_probe_req(dev, ifsta->bssid,
1015 ifsta->ssid,
1016 ifsta->ssid_len);
1017 }
1018 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001019 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001020
1021 rcu_read_unlock();
1022
Johannes Berg3b967662008-04-08 17:56:52 +02001023 if (disassoc && sta)
Johannes Bergd0709a62008-02-25 16:27:46 +01001024 sta_info_destroy(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01001025
Jiri Bencf0706e82007-05-05 11:45:53 -07001026 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -05001027 ifsta->state = IEEE80211_DISABLED;
1028 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001029 } else {
1030 mod_timer(&ifsta->timer, jiffies +
1031 IEEE80211_MONITORING_INTERVAL);
1032 }
1033}
1034
1035
1036static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1037 u8 *ssid, size_t ssid_len)
1038{
1039 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01001040 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001041 struct sk_buff *skb;
1042 struct ieee80211_mgmt *mgmt;
1043 u8 *pos, *supp_rates, *esupp_rates = NULL;
1044 int i;
1045
1046 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1047 if (!skb) {
1048 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1049 "request\n", dev->name);
1050 return;
1051 }
1052 skb_reserve(skb, local->hw.extra_tx_headroom);
1053
1054 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1055 memset(mgmt, 0, 24);
1056 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1057 IEEE80211_STYPE_PROBE_REQ);
1058 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1059 if (dst) {
1060 memcpy(mgmt->da, dst, ETH_ALEN);
1061 memcpy(mgmt->bssid, dst, ETH_ALEN);
1062 } else {
1063 memset(mgmt->da, 0xff, ETH_ALEN);
1064 memset(mgmt->bssid, 0xff, ETH_ALEN);
1065 }
1066 pos = skb_put(skb, 2 + ssid_len);
1067 *pos++ = WLAN_EID_SSID;
1068 *pos++ = ssid_len;
1069 memcpy(pos, ssid, ssid_len);
1070
1071 supp_rates = skb_put(skb, 2);
1072 supp_rates[0] = WLAN_EID_SUPP_RATES;
1073 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001074 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1075
1076 for (i = 0; i < sband->n_bitrates; i++) {
1077 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07001078 if (esupp_rates) {
1079 pos = skb_put(skb, 1);
1080 esupp_rates[1]++;
1081 } else if (supp_rates[1] == 8) {
1082 esupp_rates = skb_put(skb, 3);
1083 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1084 esupp_rates[1] = 1;
1085 pos = &esupp_rates[2];
1086 } else {
1087 pos = skb_put(skb, 1);
1088 supp_rates[1]++;
1089 }
Johannes Berg8318d782008-01-24 19:38:38 +01001090 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -07001091 }
1092
1093 ieee80211_sta_tx(dev, skb, 0);
1094}
1095
1096
1097static int ieee80211_sta_wep_configured(struct net_device *dev)
1098{
1099 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1100 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001101 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -07001102 return 0;
1103 return 1;
1104}
1105
1106
1107static void ieee80211_auth_completed(struct net_device *dev,
1108 struct ieee80211_if_sta *ifsta)
1109{
1110 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001111 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001112 ieee80211_associate(dev, ifsta);
1113}
1114
1115
1116static void ieee80211_auth_challenge(struct net_device *dev,
1117 struct ieee80211_if_sta *ifsta,
1118 struct ieee80211_mgmt *mgmt,
1119 size_t len)
1120{
1121 u8 *pos;
1122 struct ieee802_11_elems elems;
1123
1124 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1125 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001126 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001127 if (!elems.challenge) {
1128 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1129 "frame\n", dev->name);
1130 return;
1131 }
1132 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1133 elems.challenge_len + 2, 1);
1134}
1135
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001136static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1137 u8 dialog_token, u16 status, u16 policy,
1138 u16 buf_size, u16 timeout)
1139{
1140 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1141 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1142 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1143 struct sk_buff *skb;
1144 struct ieee80211_mgmt *mgmt;
1145 u16 capab;
1146
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001147 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1148 sizeof(mgmt->u.action.u.addba_resp));
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001149 if (!skb) {
1150 printk(KERN_DEBUG "%s: failed to allocate buffer "
1151 "for addba resp frame\n", dev->name);
1152 return;
1153 }
1154
1155 skb_reserve(skb, local->hw.extra_tx_headroom);
1156 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1157 memset(mgmt, 0, 24);
1158 memcpy(mgmt->da, da, ETH_ALEN);
1159 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001160 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001161 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1162 else
1163 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1164 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1165 IEEE80211_STYPE_ACTION);
1166
1167 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1168 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1169 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1170 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1171
1172 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1173 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1174 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1175
1176 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1177 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1178 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1179
1180 ieee80211_sta_tx(dev, skb, 0);
1181
1182 return;
1183}
1184
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001185void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1186 u16 tid, u8 dialog_token, u16 start_seq_num,
1187 u16 agg_size, u16 timeout)
1188{
1189 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1190 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1191 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1192 struct sk_buff *skb;
1193 struct ieee80211_mgmt *mgmt;
1194 u16 capab;
1195
1196 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1197 sizeof(mgmt->u.action.u.addba_req));
1198
1199
1200 if (!skb) {
1201 printk(KERN_ERR "%s: failed to allocate buffer "
1202 "for addba request frame\n", dev->name);
1203 return;
1204 }
1205 skb_reserve(skb, local->hw.extra_tx_headroom);
1206 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1207 memset(mgmt, 0, 24);
1208 memcpy(mgmt->da, da, ETH_ALEN);
1209 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1210 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1211 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1212 else
1213 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1214
1215 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1216 IEEE80211_STYPE_ACTION);
1217
1218 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1219
1220 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1221 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1222
1223 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1224 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1225 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1226 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1227
1228 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1229
1230 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1231 mgmt->u.action.u.addba_req.start_seq_num =
1232 cpu_to_le16(start_seq_num << 4);
1233
1234 ieee80211_sta_tx(dev, skb, 0);
1235}
1236
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001237static void ieee80211_sta_process_addba_request(struct net_device *dev,
1238 struct ieee80211_mgmt *mgmt,
1239 size_t len)
1240{
1241 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001242 struct ieee80211_hw *hw = &local->hw;
1243 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001244 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001245 struct tid_ampdu_rx *tid_agg_rx;
1246 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001247 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001248 int ret = -EOPNOTSUPP;
1249 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001250
Johannes Bergd0709a62008-02-25 16:27:46 +01001251 rcu_read_lock();
1252
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001253 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001254 if (!sta) {
1255 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001256 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001257 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001258
1259 /* extract session parameters from addba request frame */
1260 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1261 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001262 start_seq_num =
1263 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001264
1265 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1266 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1267 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1268 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1269
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001270 status = WLAN_STATUS_REQUEST_DECLINED;
1271
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001272 /* sanity check for incoming parameters:
1273 * check if configuration can support the BA policy
1274 * and if buffer size does not exceeds max value */
1275 if (((ba_policy != 1)
1276 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1277 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1278 status = WLAN_STATUS_INVALID_QOS_PARAM;
1279#ifdef CONFIG_MAC80211_HT_DEBUG
1280 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001281 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001282 "%s on tid %u. policy %d, buffer size %d\n",
1283 print_mac(mac, mgmt->sa), tid, ba_policy,
1284 buf_size);
1285#endif /* CONFIG_MAC80211_HT_DEBUG */
1286 goto end_no_lock;
1287 }
1288 /* determine default buffer size */
1289 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001290 struct ieee80211_supported_band *sband;
1291
1292 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001293 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001294 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001295 }
1296
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001297
1298 /* examine state machine */
1299 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1300
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001301 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001302#ifdef CONFIG_MAC80211_HT_DEBUG
1303 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001304 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001305 "%s on tid %u\n",
1306 print_mac(mac, mgmt->sa), tid);
1307#endif /* CONFIG_MAC80211_HT_DEBUG */
1308 goto end;
1309 }
1310
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001311 /* prepare A-MPDU MLME for Rx aggregation */
1312 sta->ampdu_mlme.tid_rx[tid] =
1313 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1314 if (!sta->ampdu_mlme.tid_rx[tid]) {
1315 if (net_ratelimit())
1316 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1317 tid);
1318 goto end;
1319 }
1320 /* rx timer */
1321 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1322 sta_rx_agg_session_timer_expired;
1323 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1324 (unsigned long)&sta->timer_to_tid[tid];
1325 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1326
1327 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1328
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001329 /* prepare reordering buffer */
1330 tid_agg_rx->reorder_buf =
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301331 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001332 if (!tid_agg_rx->reorder_buf) {
1333 if (net_ratelimit())
1334 printk(KERN_ERR "can not allocate reordering buffer "
1335 "to tid %d\n", tid);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001336 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001337 goto end;
1338 }
1339 memset(tid_agg_rx->reorder_buf, 0,
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301340 buf_size * sizeof(struct sk_buff *));
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001341
1342 if (local->ops->ampdu_action)
1343 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001344 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001345#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001346 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001347#endif /* CONFIG_MAC80211_HT_DEBUG */
1348
1349 if (ret) {
1350 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001351 kfree(tid_agg_rx);
1352 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001353 goto end;
1354 }
1355
1356 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001357 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001358 tid_agg_rx->dialog_token = dialog_token;
1359 tid_agg_rx->ssn = start_seq_num;
1360 tid_agg_rx->head_seq_num = start_seq_num;
1361 tid_agg_rx->buf_size = buf_size;
1362 tid_agg_rx->timeout = timeout;
1363 tid_agg_rx->stored_mpdu_num = 0;
1364 status = WLAN_STATUS_SUCCESS;
1365end:
1366 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1367
1368end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001369 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1370 dialog_token, status, 1, buf_size, timeout);
1371 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001372}
Jiri Bencf0706e82007-05-05 11:45:53 -07001373
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001374static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1375 struct ieee80211_mgmt *mgmt,
1376 size_t len)
1377{
1378 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1379 struct ieee80211_hw *hw = &local->hw;
1380 struct sta_info *sta;
1381 u16 capab;
1382 u16 tid;
1383 u8 *state;
1384
Johannes Bergd0709a62008-02-25 16:27:46 +01001385 rcu_read_lock();
1386
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001387 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001388 if (!sta) {
1389 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001390 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001391 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001392
1393 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1394 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1395
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001396 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001397
1398 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1399
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001400 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1401 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1402 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1403 "%d\n", *state);
1404 goto addba_resp_exit;
1405 }
1406
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001407 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001408 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001409 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1410#ifdef CONFIG_MAC80211_HT_DEBUG
1411 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1412#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001413 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001414 }
1415
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001416 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001417#ifdef CONFIG_MAC80211_HT_DEBUG
1418 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1419#endif /* CONFIG_MAC80211_HT_DEBUG */
1420 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1421 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001422 if (*state & HT_ADDBA_RECEIVED_MSK)
1423 printk(KERN_DEBUG "double addBA response\n");
1424
1425 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001426 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001427
1428 if (*state == HT_AGG_STATE_OPERATIONAL) {
1429 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1430 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1431 }
1432
1433 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1434 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1435 } else {
1436 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1437
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001438 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001439 /* this will allow the state check in stop_BA_session */
1440 *state = HT_AGG_STATE_OPERATIONAL;
1441 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1442 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1443 WLAN_BACK_INITIATOR);
1444 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001445
1446addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001447 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001448}
1449
1450void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1451 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001452{
1453 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1454 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1455 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1456 struct sk_buff *skb;
1457 struct ieee80211_mgmt *mgmt;
1458 u16 params;
1459
1460 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1461 sizeof(mgmt->u.action.u.delba));
1462
1463 if (!skb) {
1464 printk(KERN_ERR "%s: failed to allocate buffer "
1465 "for delba frame\n", dev->name);
1466 return;
1467 }
1468
1469 skb_reserve(skb, local->hw.extra_tx_headroom);
1470 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1471 memset(mgmt, 0, 24);
1472 memcpy(mgmt->da, da, ETH_ALEN);
1473 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001474 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001475 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1476 else
1477 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1478 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1479 IEEE80211_STYPE_ACTION);
1480
1481 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1482
1483 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1484 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1485 params = (u16)(initiator << 11); /* bit 11 initiator */
1486 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1487
1488 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1489 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1490
1491 ieee80211_sta_tx(dev, skb, 0);
1492}
1493
1494void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1495 u16 initiator, u16 reason)
1496{
1497 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1498 struct ieee80211_hw *hw = &local->hw;
1499 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001500 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001501 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001502
Johannes Bergd0709a62008-02-25 16:27:46 +01001503 rcu_read_lock();
1504
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001505 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001506 if (!sta) {
1507 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001508 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001509 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001510
1511 /* check if TID is in operational state */
1512 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001513 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001514 != HT_AGG_STATE_OPERATIONAL) {
1515 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Johannes Bergd0709a62008-02-25 16:27:46 +01001516 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001517 return;
1518 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001519 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001520 HT_AGG_STATE_REQ_STOP_BA_MSK |
1521 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001522 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001523
1524 /* stop HW Rx aggregation. ampdu_action existence
1525 * already verified in session init so we add the BUG_ON */
1526 BUG_ON(!local->ops->ampdu_action);
1527
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001528#ifdef CONFIG_MAC80211_HT_DEBUG
1529 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1530 print_mac(mac, ra), tid);
1531#endif /* CONFIG_MAC80211_HT_DEBUG */
1532
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001533 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001534 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001535 if (ret)
1536 printk(KERN_DEBUG "HW problem - can not stop rx "
1537 "aggergation for tid %d\n", tid);
1538
1539 /* shutdown timer has not expired */
1540 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001541 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001542
1543 /* check if this is a self generated aggregation halt */
1544 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1545 ieee80211_send_delba(dev, ra, tid, 0, reason);
1546
1547 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001548 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1549 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001550 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001551 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1552 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1553 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001554 }
1555 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001556 /* free resources */
1557 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1558 kfree(sta->ampdu_mlme.tid_rx[tid]);
1559 sta->ampdu_mlme.tid_rx[tid] = NULL;
1560 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001561
Johannes Bergd0709a62008-02-25 16:27:46 +01001562 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001563}
1564
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001565
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001566static void ieee80211_sta_process_delba(struct net_device *dev,
1567 struct ieee80211_mgmt *mgmt, size_t len)
1568{
1569 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1570 struct sta_info *sta;
1571 u16 tid, params;
1572 u16 initiator;
1573 DECLARE_MAC_BUF(mac);
1574
Johannes Bergd0709a62008-02-25 16:27:46 +01001575 rcu_read_lock();
1576
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001577 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001578 if (!sta) {
1579 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001580 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001581 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001582
1583 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1584 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1585 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1586
1587#ifdef CONFIG_MAC80211_HT_DEBUG
1588 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001589 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1590 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001591 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001592 mgmt->u.action.u.delba.reason_code);
1593#endif /* CONFIG_MAC80211_HT_DEBUG */
1594
1595 if (initiator == WLAN_BACK_INITIATOR)
1596 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1597 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001598 else { /* WLAN_BACK_RECIPIENT */
1599 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001600 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001601 HT_AGG_STATE_OPERATIONAL;
1602 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1603 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1604 WLAN_BACK_RECIPIENT);
1605 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001606 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001607}
1608
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001609/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001610 * After sending add Block Ack request we activated a timer until
1611 * add Block Ack response will arrive from the recipient.
1612 * If this timer expires sta_addba_resp_timer_expired will be executed.
1613 */
1614void sta_addba_resp_timer_expired(unsigned long data)
1615{
1616 /* not an elegant detour, but there is no choice as the timer passes
1617 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001618 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001619 * array gives the sta through container_of */
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301620 u16 tid = *(u8 *)data;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001621 struct sta_info *temp_sta = container_of((void *)data,
1622 struct sta_info, timer_to_tid[tid]);
1623
1624 struct ieee80211_local *local = temp_sta->local;
1625 struct ieee80211_hw *hw = &local->hw;
1626 struct sta_info *sta;
1627 u8 *state;
1628
Johannes Bergd0709a62008-02-25 16:27:46 +01001629 rcu_read_lock();
1630
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001631 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001632 if (!sta) {
1633 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001634 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001635 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001636
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001637 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001638 /* check if the TID waits for addBA response */
1639 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1640 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1641 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1642 *state = HT_AGG_STATE_IDLE;
1643 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1644 "expecting addBA response there", tid);
1645 goto timer_expired_exit;
1646 }
1647
1648 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1649
1650 /* go through the state check in stop_BA_session */
1651 *state = HT_AGG_STATE_OPERATIONAL;
1652 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1653 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1654 WLAN_BACK_INITIATOR);
1655
1656timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001657 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001658}
1659
1660/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001661 * After accepting the AddBA Request we activated a timer,
1662 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001663 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1664 */
1665void sta_rx_agg_session_timer_expired(unsigned long data)
1666{
1667 /* not an elegant detour, but there is no choice as the timer passes
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301668 * only one argument, and various sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001669 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001670 * array gives the sta through container_of */
1671 u8 *ptid = (u8 *)data;
1672 u8 *timer_to_id = ptid - *ptid;
1673 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1674 timer_to_tid[0]);
1675
1676 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergd0709a62008-02-25 16:27:46 +01001677 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1678 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001679 WLAN_REASON_QSTA_TIMEOUT);
1680}
1681
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001682void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1683{
1684 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1685 int i;
1686
1687 for (i = 0; i < STA_TID_NUM; i++) {
1688 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1689 WLAN_BACK_INITIATOR);
1690 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1691 WLAN_BACK_RECIPIENT,
1692 WLAN_REASON_QSTA_LEAVE_QBSS);
1693 }
1694}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001695
Jiri Bencf0706e82007-05-05 11:45:53 -07001696static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1697 struct ieee80211_if_sta *ifsta,
1698 struct ieee80211_mgmt *mgmt,
1699 size_t len)
1700{
1701 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1702 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001703 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001704
1705 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Berg51fb61e2007-12-19 01:31:27 +01001706 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001707 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001708 "%s, but not in authenticate state - ignored\n",
1709 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001710 return;
1711 }
1712
1713 if (len < 24 + 6) {
1714 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001715 "received from %s - ignored\n",
1716 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001717 return;
1718 }
1719
Johannes Berg51fb61e2007-12-19 01:31:27 +01001720 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001721 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1722 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001723 "unknown AP (SA=%s BSSID=%s) - "
1724 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1725 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001726 return;
1727 }
1728
Johannes Berg51fb61e2007-12-19 01:31:27 +01001729 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001730 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1731 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001732 "unknown BSSID (SA=%s BSSID=%s) - "
1733 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1734 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001735 return;
1736 }
1737
1738 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1739 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1740 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1741
Joe Perches0795af52007-10-03 17:59:30 -07001742 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e82007-05-05 11:45:53 -07001743 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001744 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e82007-05-05 11:45:53 -07001745 auth_transaction, status_code);
1746
Johannes Berg51fb61e2007-12-19 01:31:27 +01001747 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001748 /* IEEE 802.11 standard does not require authentication in IBSS
1749 * networks and most implementations do not seem to use it.
1750 * However, try to reply to authentication attempts if someone
1751 * has actually implemented this.
1752 * TODO: Could implement shared key authentication. */
1753 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1754 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1755 "frame (alg=%d transaction=%d)\n",
1756 dev->name, auth_alg, auth_transaction);
1757 return;
1758 }
1759 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1760 }
1761
1762 if (auth_alg != ifsta->auth_alg ||
1763 auth_transaction != ifsta->auth_transaction) {
1764 printk(KERN_DEBUG "%s: unexpected authentication frame "
1765 "(alg=%d transaction=%d)\n",
1766 dev->name, auth_alg, auth_transaction);
1767 return;
1768 }
1769
1770 if (status_code != WLAN_STATUS_SUCCESS) {
1771 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1772 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1773 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1774 u8 algs[3];
1775 const int num_algs = ARRAY_SIZE(algs);
1776 int i, pos;
1777 algs[0] = algs[1] = algs[2] = 0xff;
1778 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1779 algs[0] = WLAN_AUTH_OPEN;
1780 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1781 algs[1] = WLAN_AUTH_SHARED_KEY;
1782 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1783 algs[2] = WLAN_AUTH_LEAP;
1784 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1785 pos = 0;
1786 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1787 pos = 1;
1788 else
1789 pos = 2;
1790 for (i = 0; i < num_algs; i++) {
1791 pos++;
1792 if (pos >= num_algs)
1793 pos = 0;
1794 if (algs[pos] == ifsta->auth_alg ||
1795 algs[pos] == 0xff)
1796 continue;
1797 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1798 !ieee80211_sta_wep_configured(dev))
1799 continue;
1800 ifsta->auth_alg = algs[pos];
1801 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1802 "next try\n",
1803 dev->name, ifsta->auth_alg);
1804 break;
1805 }
1806 }
1807 return;
1808 }
1809
1810 switch (ifsta->auth_alg) {
1811 case WLAN_AUTH_OPEN:
1812 case WLAN_AUTH_LEAP:
1813 ieee80211_auth_completed(dev, ifsta);
1814 break;
1815 case WLAN_AUTH_SHARED_KEY:
1816 if (ifsta->auth_transaction == 4)
1817 ieee80211_auth_completed(dev, ifsta);
1818 else
1819 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1820 break;
1821 }
1822}
1823
1824
1825static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1826 struct ieee80211_if_sta *ifsta,
1827 struct ieee80211_mgmt *mgmt,
1828 size_t len)
1829{
1830 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001831 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001832
1833 if (len < 24 + 2) {
1834 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001835 "received from %s - ignored\n",
1836 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001837 return;
1838 }
1839
1840 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1841 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001842 "unknown AP (SA=%s BSSID=%s) - "
1843 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1844 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001845 return;
1846 }
1847
1848 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1849
Joe Perches0795af52007-10-03 17:59:30 -07001850 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001851 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001852 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001853
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001854 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001855 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1856 }
1857
1858 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1859 ifsta->state == IEEE80211_ASSOCIATE ||
1860 ifsta->state == IEEE80211_ASSOCIATED) {
1861 ifsta->state = IEEE80211_AUTHENTICATE;
1862 mod_timer(&ifsta->timer, jiffies +
1863 IEEE80211_RETRY_AUTH_INTERVAL);
1864 }
1865
1866 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001867 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001868}
1869
1870
1871static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1872 struct ieee80211_if_sta *ifsta,
1873 struct ieee80211_mgmt *mgmt,
1874 size_t len)
1875{
1876 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001877 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001878
1879 if (len < 24 + 2) {
1880 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001881 "received from %s - ignored\n",
1882 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001883 return;
1884 }
1885
1886 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1887 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001888 "unknown AP (SA=%s BSSID=%s) - "
1889 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1890 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001891 return;
1892 }
1893
1894 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1895
Joe Perches0795af52007-10-03 17:59:30 -07001896 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001897 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001898 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001899
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001900 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001901 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1902
1903 if (ifsta->state == IEEE80211_ASSOCIATED) {
1904 ifsta->state = IEEE80211_ASSOCIATE;
1905 mod_timer(&ifsta->timer, jiffies +
1906 IEEE80211_RETRY_AUTH_INTERVAL);
1907 }
1908
1909 ieee80211_set_disassoc(dev, ifsta, 0);
1910}
1911
1912
Johannes Berg471b3ef2007-12-28 14:32:58 +01001913static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001914 struct ieee80211_if_sta *ifsta,
1915 struct ieee80211_mgmt *mgmt,
1916 size_t len,
1917 int reassoc)
1918{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001919 struct ieee80211_local *local = sdata->local;
1920 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01001921 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001922 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001923 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001924 u16 capab_info, status_code, aid;
1925 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001926 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001927 u8 *pos;
1928 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001929 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001930 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001931
1932 /* AssocResp and ReassocResp have identical structure, so process both
1933 * of them in this function. */
1934
1935 if (ifsta->state != IEEE80211_ASSOCIATE) {
1936 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001937 "%s, but not in associate state - ignored\n",
1938 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001939 return;
1940 }
1941
1942 if (len < 24 + 6) {
1943 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001944 "received from %s - ignored\n",
1945 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001946 return;
1947 }
1948
1949 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1950 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001951 "unknown AP (SA=%s BSSID=%s) - "
1952 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1953 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001954 return;
1955 }
1956
1957 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1958 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1959 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001960
Joe Perches0795af52007-10-03 17:59:30 -07001961 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001962 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001963 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001964 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001965
1966 if (status_code != WLAN_STATUS_SUCCESS) {
1967 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1968 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001969 /* if this was a reassociation, ensure we try a "full"
1970 * association next time. This works around some broken APs
1971 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001972 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07001973 return;
1974 }
1975
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001976 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1977 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1978 "set\n", dev->name, aid);
1979 aid &= ~(BIT(15) | BIT(14));
1980
Jiri Bencf0706e82007-05-05 11:45:53 -07001981 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001982 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001983
1984 if (!elems.supp_rates) {
1985 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1986 dev->name);
1987 return;
1988 }
1989
1990 printk(KERN_DEBUG "%s: associated\n", dev->name);
1991 ifsta->aid = aid;
1992 ifsta->ap_capab = capab_info;
1993
1994 kfree(ifsta->assocresp_ies);
1995 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001996 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001997 if (ifsta->assocresp_ies)
1998 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1999
Johannes Bergd0709a62008-02-25 16:27:46 +01002000 rcu_read_lock();
2001
Jiri Bencf0706e82007-05-05 11:45:53 -07002002 /* Add STA entry for the AP */
2003 sta = sta_info_get(local, ifsta->bssid);
2004 if (!sta) {
2005 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01002006 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01002007
Johannes Berg73651ee2008-02-25 16:27:47 +01002008 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2009 if (!sta) {
2010 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2011 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01002012 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002013 return;
2014 }
John W. Linville65c107a2007-10-05 14:23:27 -04002015 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01002016 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002017 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002018 if (bss) {
2019 sta->last_rssi = bss->rssi;
2020 sta->last_signal = bss->signal;
2021 sta->last_noise = bss->noise;
2022 ieee80211_rx_bss_put(dev, bss);
2023 }
Johannes Berg73651ee2008-02-25 16:27:47 +01002024
2025 err = sta_info_insert(sta);
2026 if (err) {
2027 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2028 " the AP (error %d)\n", dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01002029 rcu_read_unlock();
2030 return;
2031 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002032 }
2033
Johannes Berg73651ee2008-02-25 16:27:47 +01002034 /*
2035 * FIXME: Do we really need to update the sta_info's information here?
2036 * We already know about the AP (we found it in our list) so it
2037 * should already be filled with the right info, no?
2038 * As is stands, all this is racy because typically we assume
2039 * the information that is filled in here (except flags) doesn't
2040 * change while a STA structure is alive. As such, it should move
2041 * to between the sta_info_alloc() and sta_info_insert() above.
2042 */
2043
Johannes Berg238814f2008-01-28 17:19:37 +01002044 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2045 WLAN_STA_AUTHORIZED;
Jiri Bencf0706e82007-05-05 11:45:53 -07002046
2047 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002048 basic_rates = 0;
2049 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2050
Jiri Bencf0706e82007-05-05 11:45:53 -07002051 for (i = 0; i < elems.supp_rates_len; i++) {
2052 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002053
2054 if (rate > 110)
2055 have_higher_than_11mbit = true;
2056
2057 for (j = 0; j < sband->n_bitrates; j++) {
2058 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002059 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002060 if (elems.supp_rates[i] & 0x80)
2061 basic_rates |= BIT(j);
2062 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002063 }
Johannes Berg8318d782008-01-24 19:38:38 +01002064
Jiri Bencf0706e82007-05-05 11:45:53 -07002065 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2066 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002067
2068 if (rate > 110)
2069 have_higher_than_11mbit = true;
2070
2071 for (j = 0; j < sband->n_bitrates; j++) {
2072 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002073 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002074 if (elems.ext_supp_rates[i] & 0x80)
2075 basic_rates |= BIT(j);
2076 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002077 }
Johannes Berg8318d782008-01-24 19:38:38 +01002078
2079 sta->supp_rates[local->hw.conf.channel->band] = rates;
2080 sdata->basic_rates = basic_rates;
2081
2082 /* cf. IEEE 802.11 9.2.12 */
2083 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2084 have_higher_than_11mbit)
2085 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2086 else
2087 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002088
Tomas Winkler38668c02008-03-28 16:33:32 -07002089 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002090 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002091 ieee80211_ht_cap_ie_to_ht_info(
2092 (struct ieee80211_ht_cap *)
2093 elems.ht_cap_elem, &sta->ht_info);
2094 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2095 (struct ieee80211_ht_addt_info *)
2096 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002097 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002098 }
2099
Jiri Bencf0706e82007-05-05 11:45:53 -07002100 rate_control_rate_init(sta, local);
2101
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002102 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002103 sta->flags |= WLAN_STA_WME;
Johannes Berge5f98f22008-03-05 20:39:31 +01002104 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002105 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2106 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002107 } else
2108 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002109
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002110 /* set AID and assoc capability,
2111 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002112 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002113 bss_conf->assoc_capability = capab_info;
Johannes Berg8318d782008-01-24 19:38:38 +01002114 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002115
Jiri Bencf0706e82007-05-05 11:45:53 -07002116 ieee80211_associated(dev, ifsta);
2117}
2118
2119
2120/* Caller must hold local->sta_bss_lock */
2121static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2122 struct ieee80211_sta_bss *bss)
2123{
2124 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002125 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002126
2127 if (bss_mesh_cfg(bss))
2128 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2129 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002130 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002131 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002132
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002133 bss->hnext = local->sta_bss_hash[hash_idx];
2134 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002135}
2136
2137
2138/* Caller must hold local->sta_bss_lock */
2139static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2140 struct ieee80211_sta_bss *bss)
2141{
2142 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2143 struct ieee80211_sta_bss *b, *prev = NULL;
2144 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2145 while (b) {
2146 if (b == bss) {
2147 if (!prev)
2148 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2149 bss->hnext;
2150 else
2151 prev->hnext = bss->hnext;
2152 break;
2153 }
2154 prev = b;
2155 b = b->hnext;
2156 }
2157}
2158
2159
2160static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002161ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002162 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002163{
2164 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2165 struct ieee80211_sta_bss *bss;
2166
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002167 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002168 if (!bss)
2169 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002170 atomic_inc(&bss->users);
2171 atomic_inc(&bss->users);
2172 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002173 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002174 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2175 memcpy(bss->ssid, ssid, ssid_len);
2176 bss->ssid_len = ssid_len;
2177 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002178
2179 spin_lock_bh(&local->sta_bss_lock);
2180 /* TODO: order by RSSI? */
2181 list_add_tail(&bss->list, &local->sta_bss_list);
2182 __ieee80211_rx_bss_hash_add(dev, bss);
2183 spin_unlock_bh(&local->sta_bss_lock);
2184 return bss;
2185}
2186
Jiri Bencf0706e82007-05-05 11:45:53 -07002187static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002188ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002189 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002190{
2191 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2192 struct ieee80211_sta_bss *bss;
2193
2194 spin_lock_bh(&local->sta_bss_lock);
2195 bss = local->sta_bss_hash[STA_HASH(bssid)];
2196 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002197 if (!bss_mesh_cfg(bss) &&
2198 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002199 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002200 bss->ssid_len == ssid_len &&
2201 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002202 atomic_inc(&bss->users);
2203 break;
2204 }
2205 bss = bss->hnext;
2206 }
2207 spin_unlock_bh(&local->sta_bss_lock);
2208 return bss;
2209}
2210
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002211#ifdef CONFIG_MAC80211_MESH
2212static struct ieee80211_sta_bss *
2213ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2214 u8 *mesh_cfg, int freq)
2215{
2216 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2217 struct ieee80211_sta_bss *bss;
2218
2219 spin_lock_bh(&local->sta_bss_lock);
2220 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2221 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002222 if (bss_mesh_cfg(bss) &&
2223 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002224 bss->freq == freq &&
2225 mesh_id_len == bss->mesh_id_len &&
2226 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2227 mesh_id_len))) {
2228 atomic_inc(&bss->users);
2229 break;
2230 }
2231 bss = bss->hnext;
2232 }
2233 spin_unlock_bh(&local->sta_bss_lock);
2234 return bss;
2235}
2236
2237static struct ieee80211_sta_bss *
2238ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002239 u8 *mesh_cfg, int mesh_config_len, int freq)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002240{
2241 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2242 struct ieee80211_sta_bss *bss;
2243
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002244 if (mesh_config_len != MESH_CFG_LEN)
2245 return NULL;
2246
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002247 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2248 if (!bss)
2249 return NULL;
2250
John W. Linville24736702008-04-08 14:15:46 -04002251 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002252 if (!bss->mesh_cfg) {
2253 kfree(bss);
2254 return NULL;
2255 }
2256
2257 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2258 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2259 if (!bss->mesh_id) {
2260 kfree(bss->mesh_cfg);
2261 kfree(bss);
2262 return NULL;
2263 }
2264 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2265 }
2266
2267 atomic_inc(&bss->users);
2268 atomic_inc(&bss->users);
John W. Linville24736702008-04-08 14:15:46 -04002269 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002270 bss->mesh_id_len = mesh_id_len;
2271 bss->freq = freq;
2272 spin_lock_bh(&local->sta_bss_lock);
2273 /* TODO: order by RSSI? */
2274 list_add_tail(&bss->list, &local->sta_bss_list);
2275 __ieee80211_rx_bss_hash_add(dev, bss);
2276 spin_unlock_bh(&local->sta_bss_lock);
2277 return bss;
2278}
2279#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002280
2281static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2282{
2283 kfree(bss->wpa_ie);
2284 kfree(bss->rsn_ie);
2285 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002286 kfree(bss->ht_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002287 kfree(bss_mesh_id(bss));
2288 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002289 kfree(bss);
2290}
2291
2292
2293static void ieee80211_rx_bss_put(struct net_device *dev,
2294 struct ieee80211_sta_bss *bss)
2295{
2296 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -07002297
Pavel Emelyanov1ebebea2008-04-23 11:47:15 +04002298 local_bh_disable();
2299 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2300 local_bh_enable();
2301 return;
2302 }
2303
Jiri Bencf0706e82007-05-05 11:45:53 -07002304 __ieee80211_rx_bss_hash_del(dev, bss);
2305 list_del(&bss->list);
2306 spin_unlock_bh(&local->sta_bss_lock);
2307 ieee80211_rx_bss_free(bss);
2308}
2309
2310
2311void ieee80211_rx_bss_list_init(struct net_device *dev)
2312{
2313 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2314 spin_lock_init(&local->sta_bss_lock);
2315 INIT_LIST_HEAD(&local->sta_bss_list);
2316}
2317
2318
2319void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2320{
2321 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2322 struct ieee80211_sta_bss *bss, *tmp;
2323
2324 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2325 ieee80211_rx_bss_put(dev, bss);
2326}
2327
2328
Bruno Randolfa6072682008-02-18 11:21:15 +09002329static int ieee80211_sta_join_ibss(struct net_device *dev,
2330 struct ieee80211_if_sta *ifsta,
2331 struct ieee80211_sta_bss *bss)
2332{
2333 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2334 int res, rates, i, j;
2335 struct sk_buff *skb;
2336 struct ieee80211_mgmt *mgmt;
2337 struct ieee80211_tx_control control;
2338 struct rate_selection ratesel;
2339 u8 *pos;
2340 struct ieee80211_sub_if_data *sdata;
2341 struct ieee80211_supported_band *sband;
Dan Williams507b06d2008-06-03 23:39:55 -04002342 union iwreq_data wrqu;
Bruno Randolfa6072682008-02-18 11:21:15 +09002343
2344 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2345
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002346 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2347
Bruno Randolfa6072682008-02-18 11:21:15 +09002348 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002349 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002350
2351 if (local->ops->reset_tsf) {
2352 /* Reset own TSF to allow time synchronization work. */
2353 local->ops->reset_tsf(local_to_hw(local));
2354 }
2355 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2356 res = ieee80211_if_config(dev);
2357 if (res)
2358 return res;
2359
2360 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2361
Bruno Randolfa6072682008-02-18 11:21:15 +09002362 sdata->drop_unencrypted = bss->capability &
2363 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2364
Assaf Kraussbe038b32008-06-05 19:55:21 +03002365 res = ieee80211_set_freq(dev, bss->freq);
Bruno Randolfa6072682008-02-18 11:21:15 +09002366
Assaf Kraussbe038b32008-06-05 19:55:21 +03002367 if (res)
2368 return res;
Bruno Randolfa6072682008-02-18 11:21:15 +09002369
2370 /* Set beacon template */
2371 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2372 do {
2373 if (!skb)
2374 break;
2375
2376 skb_reserve(skb, local->hw.extra_tx_headroom);
2377
2378 mgmt = (struct ieee80211_mgmt *)
2379 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2380 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2381 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2382 IEEE80211_STYPE_BEACON);
2383 memset(mgmt->da, 0xff, ETH_ALEN);
2384 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2385 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2386 mgmt->u.beacon.beacon_int =
2387 cpu_to_le16(local->hw.conf.beacon_int);
2388 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2389
2390 pos = skb_put(skb, 2 + ifsta->ssid_len);
2391 *pos++ = WLAN_EID_SSID;
2392 *pos++ = ifsta->ssid_len;
2393 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2394
2395 rates = bss->supp_rates_len;
2396 if (rates > 8)
2397 rates = 8;
2398 pos = skb_put(skb, 2 + rates);
2399 *pos++ = WLAN_EID_SUPP_RATES;
2400 *pos++ = rates;
2401 memcpy(pos, bss->supp_rates, rates);
2402
2403 if (bss->band == IEEE80211_BAND_2GHZ) {
2404 pos = skb_put(skb, 2 + 1);
2405 *pos++ = WLAN_EID_DS_PARAMS;
2406 *pos++ = 1;
2407 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2408 }
2409
2410 pos = skb_put(skb, 2 + 2);
2411 *pos++ = WLAN_EID_IBSS_PARAMS;
2412 *pos++ = 2;
2413 /* FIX: set ATIM window based on scan results */
2414 *pos++ = 0;
2415 *pos++ = 0;
2416
2417 if (bss->supp_rates_len > 8) {
2418 rates = bss->supp_rates_len - 8;
2419 pos = skb_put(skb, 2 + rates);
2420 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2421 *pos++ = rates;
2422 memcpy(pos, &bss->supp_rates[8], rates);
2423 }
2424
2425 memset(&control, 0, sizeof(control));
2426 rate_control_get_rate(dev, sband, skb, &ratesel);
2427 if (!ratesel.rate) {
2428 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2429 "for IBSS beacon\n", dev->name);
2430 break;
2431 }
2432 control.vif = &sdata->vif;
2433 control.tx_rate = ratesel.rate;
2434 if (sdata->bss_conf.use_short_preamble &&
2435 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2436 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2437 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2438 control.flags |= IEEE80211_TXCTL_NO_ACK;
2439 control.retry_limit = 1;
2440
2441 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2442 if (ifsta->probe_resp) {
2443 mgmt = (struct ieee80211_mgmt *)
2444 ifsta->probe_resp->data;
2445 mgmt->frame_control =
2446 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2447 IEEE80211_STYPE_PROBE_RESP);
2448 } else {
2449 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2450 "template for IBSS\n", dev->name);
2451 }
2452
2453 if (local->ops->beacon_update &&
2454 local->ops->beacon_update(local_to_hw(local),
2455 skb, &control) == 0) {
2456 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2457 "template\n", dev->name);
2458 skb = NULL;
2459 }
2460
2461 rates = 0;
2462 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2463 for (i = 0; i < bss->supp_rates_len; i++) {
2464 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2465 for (j = 0; j < sband->n_bitrates; j++)
2466 if (sband->bitrates[j].bitrate == bitrate)
2467 rates |= BIT(j);
2468 }
2469 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Vladimir Koutnye2839d82008-03-18 21:14:07 +01002470
2471 ieee80211_sta_def_wmm_params(dev, bss, 1);
Bruno Randolfa6072682008-02-18 11:21:15 +09002472 } while (0);
2473
2474 if (skb) {
2475 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2476 "template\n", dev->name);
2477 dev_kfree_skb(skb);
2478 }
2479
2480 ifsta->state = IEEE80211_IBSS_JOINED;
2481 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2482
Dan Williams507b06d2008-06-03 23:39:55 -04002483 memset(&wrqu, 0, sizeof(wrqu));
2484 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2485 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
2486
Bruno Randolfa6072682008-02-18 11:21:15 +09002487 return res;
2488}
2489
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002490u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2491 struct ieee802_11_elems *elems,
2492 enum ieee80211_band band)
2493{
2494 struct ieee80211_supported_band *sband;
2495 struct ieee80211_rate *bitrates;
2496 size_t num_rates;
2497 u64 supp_rates;
2498 int i, j;
2499 sband = local->hw.wiphy->bands[band];
2500
2501 if (!sband) {
2502 WARN_ON(1);
2503 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2504 }
2505
2506 bitrates = sband->bitrates;
2507 num_rates = sband->n_bitrates;
2508 supp_rates = 0;
2509 for (i = 0; i < elems->supp_rates_len +
2510 elems->ext_supp_rates_len; i++) {
2511 u8 rate = 0;
2512 int own_rate;
2513 if (i < elems->supp_rates_len)
2514 rate = elems->supp_rates[i];
2515 else if (elems->ext_supp_rates)
2516 rate = elems->ext_supp_rates
2517 [i - elems->supp_rates_len];
2518 own_rate = 5 * (rate & 0x7f);
2519 for (j = 0; j < num_rates; j++)
2520 if (bitrates[j].bitrate == own_rate)
2521 supp_rates |= BIT(j);
2522 }
2523 return supp_rates;
2524}
2525
Bruno Randolfa6072682008-02-18 11:21:15 +09002526
Jiri Bencf0706e82007-05-05 11:45:53 -07002527static void ieee80211_rx_bss_info(struct net_device *dev,
2528 struct ieee80211_mgmt *mgmt,
2529 size_t len,
2530 struct ieee80211_rx_status *rx_status,
2531 int beacon)
2532{
2533 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2534 struct ieee802_11_elems elems;
2535 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002536 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002537 struct ieee80211_sta_bss *bss;
2538 struct sta_info *sta;
2539 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002540 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002541 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002542 DECLARE_MAC_BUF(mac);
2543 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002544
2545 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2546 return; /* ignore ProbeResp to foreign address */
2547
2548#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002549 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07002550 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002551 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002552#endif
2553
2554 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2555 if (baselen > len)
2556 return;
2557
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002558 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
John W. Linville67a4cce2007-10-12 16:40:37 -04002559 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002560
Johannes Berg902acc72008-02-23 15:17:19 +01002561 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2562 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2563 u64 rates = ieee80211_sta_get_rates(local, &elems,
2564 rx_status->band);
2565
2566 mesh_neighbour_update(mgmt->sa, rates, dev,
2567 mesh_peer_accepts_plinks(&elems, dev));
2568 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002569
Johannes Bergd0709a62008-02-25 16:27:46 +01002570 rcu_read_lock();
2571
Johannes Berg51fb61e2007-12-19 01:31:27 +01002572 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002573 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2574 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002575 u64 prev_rates;
2576 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2577 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002578
Johannes Berg8318d782008-01-24 19:38:38 +01002579 prev_rates = sta->supp_rates[rx_status->band];
2580 sta->supp_rates[rx_status->band] &= supp_rates;
2581 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002582 /* No matching rates - this should not really happen.
2583 * Make sure that at least one rate is marked
2584 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002585 sta->supp_rates[rx_status->band] =
2586 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002587 }
Johannes Berg8318d782008-01-24 19:38:38 +01002588 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002589 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002590 "%s based on beacon info (0x%llx & 0x%llx -> "
2591 "0x%llx)\n",
2592 dev->name, print_mac(mac, sta->addr),
2593 (unsigned long long) prev_rates,
2594 (unsigned long long) supp_rates,
2595 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002596 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002597 }
2598
Johannes Bergd0709a62008-02-25 16:27:46 +01002599 rcu_read_unlock();
2600
Jiri Bencf0706e82007-05-05 11:45:53 -07002601 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002602 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002603 else
Johannes Berg8318d782008-01-24 19:38:38 +01002604 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002605
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002606 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2607
2608 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2609 return;
2610
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002611#ifdef CONFIG_MAC80211_MESH
2612 if (elems.mesh_config)
2613 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2614 elems.mesh_id_len, elems.mesh_config, freq);
2615 else
2616#endif
2617 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002618 elems.ssid, elems.ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002619 if (!bss) {
2620#ifdef CONFIG_MAC80211_MESH
2621 if (elems.mesh_config)
2622 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002623 elems.mesh_id_len, elems.mesh_config,
2624 elems.mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002625 else
2626#endif
2627 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2628 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002629 if (!bss)
2630 return;
2631 } else {
2632#if 0
2633 /* TODO: order by RSSI? */
2634 spin_lock_bh(&local->sta_bss_lock);
2635 list_move_tail(&bss->list, &local->sta_bss_list);
2636 spin_unlock_bh(&local->sta_bss_lock);
2637#endif
2638 }
2639
Daniel Drake56282212007-07-10 19:32:10 +02002640 /* save the ERP value so that it is available at association time */
2641 if (elems.erp_info && elems.erp_info_len >= 1) {
2642 bss->erp_value = elems.erp_info[0];
2643 bss->has_erp_value = 1;
2644 }
2645
Johannes Berg30b89b02008-04-16 17:43:20 +02002646 if (elems.ht_cap_elem &&
2647 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2648 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2649 kfree(bss->ht_ie);
2650 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2651 if (bss->ht_ie) {
2652 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2653 elems.ht_cap_elem_len + 2);
2654 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2655 } else
2656 bss->ht_ie_len = 0;
2657 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2658 kfree(bss->ht_ie);
2659 bss->ht_ie = NULL;
2660 bss->ht_ie_len = 0;
2661 }
2662
Jiri Bencf0706e82007-05-05 11:45:53 -07002663 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2664 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002665
2666 bss->supp_rates_len = 0;
2667 if (elems.supp_rates) {
2668 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2669 if (clen > elems.supp_rates_len)
2670 clen = elems.supp_rates_len;
2671 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2672 clen);
2673 bss->supp_rates_len += clen;
2674 }
2675 if (elems.ext_supp_rates) {
2676 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2677 if (clen > elems.ext_supp_rates_len)
2678 clen = elems.ext_supp_rates_len;
2679 memcpy(&bss->supp_rates[bss->supp_rates_len],
2680 elems.ext_supp_rates, clen);
2681 bss->supp_rates_len += clen;
2682 }
2683
Johannes Berg30b89b02008-04-16 17:43:20 +02002684 bss->band = rx_status->band;
2685
2686 bss->timestamp = beacon_timestamp;
2687 bss->last_update = jiffies;
2688 bss->rssi = rx_status->ssi;
2689 bss->signal = rx_status->signal;
2690 bss->noise = rx_status->noise;
2691 if (!beacon && !bss->probe_resp)
2692 bss->probe_resp = true;
2693
2694 /*
2695 * In STA mode, the remaining parameters should not be overridden
2696 * by beacons because they're not necessarily accurate there.
2697 */
2698 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2699 bss->probe_resp && beacon) {
2700 ieee80211_rx_bss_put(dev, bss);
2701 return;
2702 }
2703
Jiri Bencf0706e82007-05-05 11:45:53 -07002704 if (elems.wpa &&
2705 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2706 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2707 kfree(bss->wpa_ie);
2708 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2709 if (bss->wpa_ie) {
2710 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2711 bss->wpa_ie_len = elems.wpa_len + 2;
2712 } else
2713 bss->wpa_ie_len = 0;
2714 } else if (!elems.wpa && bss->wpa_ie) {
2715 kfree(bss->wpa_ie);
2716 bss->wpa_ie = NULL;
2717 bss->wpa_ie_len = 0;
2718 }
2719
2720 if (elems.rsn &&
2721 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2722 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2723 kfree(bss->rsn_ie);
2724 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2725 if (bss->rsn_ie) {
2726 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2727 bss->rsn_ie_len = elems.rsn_len + 2;
2728 } else
2729 bss->rsn_ie_len = 0;
2730 } else if (!elems.rsn && bss->rsn_ie) {
2731 kfree(bss->rsn_ie);
2732 bss->rsn_ie = NULL;
2733 bss->rsn_ie_len = 0;
2734 }
2735
Johannes Berg30b89b02008-04-16 17:43:20 +02002736 /*
2737 * Cf.
2738 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2739 *
2740 * quoting:
2741 *
2742 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2743 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2744 * Alliance (September 1, 2004) is incorporated by reference herein.
2745 * The inclusion of the WMM Parameters in probe responses and
2746 * association responses is mandatory for WMM enabled networks. The
2747 * inclusion of the WMM Parameters in beacons, however, is optional.
2748 */
2749
Jiri Bencf0706e82007-05-05 11:45:53 -07002750 if (elems.wmm_param &&
2751 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2752 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2753 kfree(bss->wmm_ie);
2754 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2755 if (bss->wmm_ie) {
2756 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2757 elems.wmm_param_len + 2);
2758 bss->wmm_ie_len = elems.wmm_param_len + 2;
2759 } else
2760 bss->wmm_ie_len = 0;
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002761 } else if (elems.wmm_info &&
2762 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_info_len ||
2763 memcmp(bss->wmm_ie, elems.wmm_info, elems.wmm_info_len))) {
2764 /* As for certain AP's Fifth bit is not set in WMM IE in
2765 * beacon frames.So while parsing the beacon frame the
2766 * wmm_info structure is used instead of wmm_param.
2767 * wmm_info structure was never used to set bss->wmm_ie.
2768 * This code fixes this problem by copying the WME
2769 * information from wmm_info to bss->wmm_ie and enabling
2770 * n-band association.
2771 */
2772 kfree(bss->wmm_ie);
2773 bss->wmm_ie = kmalloc(elems.wmm_info_len + 2, GFP_ATOMIC);
2774 if (bss->wmm_ie) {
2775 memcpy(bss->wmm_ie, elems.wmm_info - 2,
2776 elems.wmm_info_len + 2);
2777 bss->wmm_ie_len = elems.wmm_info_len + 2;
2778 } else
2779 bss->wmm_ie_len = 0;
2780 } else if (!elems.wmm_param && !elems.wmm_info && bss->wmm_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002781 kfree(bss->wmm_ie);
2782 bss->wmm_ie = NULL;
2783 bss->wmm_ie_len = 0;
2784 }
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002785
2786 /* check if we need to merge IBSS */
2787 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2788 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002789 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002790 bss->freq == local->oper_channel->center_freq &&
2791 elems.ssid_len == sdata->u.sta.ssid_len &&
2792 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2793 if (rx_status->flag & RX_FLAG_TSFT) {
2794 /* in order for correct IBSS merging we need mactime
2795 *
2796 * since mactime is defined as the time the first data
2797 * symbol of the frame hits the PHY, and the timestamp
2798 * of the beacon is defined as "the time that the data
2799 * symbol containing the first bit of the timestamp is
2800 * transmitted to the PHY plus the transmitting STA’s
2801 * delays through its local PHY from the MAC-PHY
2802 * interface to its interface with the WM"
2803 * (802.11 11.1.2) - equals the time this bit arrives at
2804 * the receiver - we have to take into account the
2805 * offset between the two.
2806 * e.g: at 1 MBit that means mactime is 192 usec earlier
2807 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2808 */
2809 int rate = local->hw.wiphy->bands[rx_status->band]->
2810 bitrates[rx_status->rate_idx].bitrate;
2811 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2812 } else if (local && local->ops && local->ops->get_tsf)
2813 /* second best option: get current TSF */
2814 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2815 else
2816 /* can't merge without knowing the TSF */
2817 rx_timestamp = -1LLU;
2818#ifdef CONFIG_MAC80211_IBSS_DEBUG
2819 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2820 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2821 print_mac(mac, mgmt->sa),
2822 print_mac(mac2, mgmt->bssid),
2823 (unsigned long long)rx_timestamp,
2824 (unsigned long long)beacon_timestamp,
2825 (unsigned long long)(rx_timestamp - beacon_timestamp),
2826 jiffies);
2827#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2828 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002829#ifndef CONFIG_MAC80211_IBSS_DEBUG
Pavel Roskind97cf012008-02-21 11:33:58 -05002830 if (net_ratelimit())
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002831#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002832 printk(KERN_DEBUG "%s: beacon TSF higher than "
2833 "local TSF - IBSS merge with BSSID %s\n",
2834 dev->name, print_mac(mac, mgmt->bssid));
2835 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2836 ieee80211_ibss_add_sta(dev, NULL,
2837 mgmt->bssid, mgmt->sa);
2838 }
2839 }
2840
Jiri Bencf0706e82007-05-05 11:45:53 -07002841 ieee80211_rx_bss_put(dev, bss);
2842}
2843
2844
2845static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2846 struct ieee80211_mgmt *mgmt,
2847 size_t len,
2848 struct ieee80211_rx_status *rx_status)
2849{
2850 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2851}
2852
2853
2854static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2855 struct ieee80211_mgmt *mgmt,
2856 size_t len,
2857 struct ieee80211_rx_status *rx_status)
2858{
Jiri Bencf0706e82007-05-05 11:45:53 -07002859 struct ieee80211_sub_if_data *sdata;
2860 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002861 size_t baselen;
2862 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002863 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2864 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002865 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002866
2867 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2868
2869 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002870 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002871 return;
2872 ifsta = &sdata->u.sta;
2873
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002874 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002875 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2876 return;
2877
2878 /* Process beacon from the current BSS */
2879 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2880 if (baselen > len)
2881 return;
2882
John W. Linville67a4cce2007-10-12 16:40:37 -04002883 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002884
Reinette Chatred18ef292008-04-09 16:56:15 -07002885 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2886 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2887 elems.wmm_param_len);
2888 }
2889
2890 /* Do not send changes to driver if we are scanning. This removes
2891 * requirement that driver's bss_info_changed function needs to be
2892 * atomic. */
2893 if (local->sta_sw_scanning || local->sta_hw_scanning)
2894 return;
2895
Daniel Drake56282212007-07-10 19:32:10 +02002896 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002897 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
John W. Linville50c4afb2008-04-15 14:09:27 -04002898 else {
2899 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2900 changed |= ieee80211_handle_protect_preamb(sdata, false,
2901 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2902 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002903
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002904 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07002905 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002906 struct ieee80211_ht_bss_info bss_info;
2907
2908 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2909 (struct ieee80211_ht_addt_info *)
2910 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002911 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2912 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002913 }
2914
Johannes Berg471b3ef2007-12-28 14:32:58 +01002915 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002916}
2917
2918
2919static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2920 struct ieee80211_if_sta *ifsta,
2921 struct ieee80211_mgmt *mgmt,
2922 size_t len,
2923 struct ieee80211_rx_status *rx_status)
2924{
2925 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2926 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2927 int tx_last_beacon;
2928 struct sk_buff *skb;
2929 struct ieee80211_mgmt *resp;
2930 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002931 DECLARE_MAC_BUF(mac);
2932#ifdef CONFIG_MAC80211_IBSS_DEBUG
2933 DECLARE_MAC_BUF(mac2);
2934 DECLARE_MAC_BUF(mac3);
2935#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002936
Johannes Berg51fb61e2007-12-19 01:31:27 +01002937 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002938 ifsta->state != IEEE80211_IBSS_JOINED ||
2939 len < 24 + 2 || !ifsta->probe_resp)
2940 return;
2941
2942 if (local->ops->tx_last_beacon)
2943 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2944 else
2945 tx_last_beacon = 1;
2946
2947#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002948 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2949 "%s (tx_last_beacon=%d)\n",
2950 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2951 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002952#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2953
2954 if (!tx_last_beacon)
2955 return;
2956
2957 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2958 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2959 return;
2960
2961 end = ((u8 *) mgmt) + len;
2962 pos = mgmt->u.probe_req.variable;
2963 if (pos[0] != WLAN_EID_SSID ||
2964 pos + 2 + pos[1] > end) {
2965 if (net_ratelimit()) {
2966 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002967 "from %s\n",
2968 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002969 }
2970 return;
2971 }
2972 if (pos[1] != 0 &&
2973 (pos[1] != ifsta->ssid_len ||
2974 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2975 /* Ignore ProbeReq for foreign SSID */
2976 return;
2977 }
2978
2979 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002980 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002981 if (!skb)
2982 return;
2983
2984 resp = (struct ieee80211_mgmt *) skb->data;
2985 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2986#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002987 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2988 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002989#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2990 ieee80211_sta_tx(dev, skb, 0);
2991}
2992
Johannes Berg4e20cb22007-12-19 01:31:24 +01002993static void ieee80211_rx_mgmt_action(struct net_device *dev,
2994 struct ieee80211_if_sta *ifsta,
2995 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002996 size_t len,
2997 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002998{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002999 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003000
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003001 if (len < IEEE80211_MIN_ACTION_SIZE)
3002 return;
3003
3004 switch (mgmt->u.action.category) {
3005 case WLAN_CATEGORY_BACK:
3006 switch (mgmt->u.action.u.addba_req.action_code) {
3007 case WLAN_ACTION_ADDBA_REQ:
3008 if (len < (IEEE80211_MIN_ACTION_SIZE +
3009 sizeof(mgmt->u.action.u.addba_req)))
3010 break;
3011 ieee80211_sta_process_addba_request(dev, mgmt, len);
3012 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02003013 case WLAN_ACTION_ADDBA_RESP:
3014 if (len < (IEEE80211_MIN_ACTION_SIZE +
3015 sizeof(mgmt->u.action.u.addba_resp)))
3016 break;
3017 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3018 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02003019 case WLAN_ACTION_DELBA:
3020 if (len < (IEEE80211_MIN_ACTION_SIZE +
3021 sizeof(mgmt->u.action.u.delba)))
3022 break;
3023 ieee80211_sta_process_delba(dev, mgmt, len);
3024 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003025 default:
3026 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02003027 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003028 dev->name);
3029 break;
3030 }
3031 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003032 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003033 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003034 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3035 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003036 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003037 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003038 mesh_rx_path_sel_frame(dev, mgmt, len);
3039 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003040 default:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003041 if (net_ratelimit())
3042 printk(KERN_DEBUG "%s: Rx unknown action frame - "
3043 "category=%d\n", dev->name, mgmt->u.action.category);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003044 break;
3045 }
3046}
Jiri Bencf0706e82007-05-05 11:45:53 -07003047
3048void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3049 struct ieee80211_rx_status *rx_status)
3050{
3051 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3052 struct ieee80211_sub_if_data *sdata;
3053 struct ieee80211_if_sta *ifsta;
3054 struct ieee80211_mgmt *mgmt;
3055 u16 fc;
3056
3057 if (skb->len < 24)
3058 goto fail;
3059
3060 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3061 ifsta = &sdata->u.sta;
3062
3063 mgmt = (struct ieee80211_mgmt *) skb->data;
3064 fc = le16_to_cpu(mgmt->frame_control);
3065
3066 switch (fc & IEEE80211_FCTL_STYPE) {
3067 case IEEE80211_STYPE_PROBE_REQ:
3068 case IEEE80211_STYPE_PROBE_RESP:
3069 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003070 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07003071 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3072 case IEEE80211_STYPE_AUTH:
3073 case IEEE80211_STYPE_ASSOC_RESP:
3074 case IEEE80211_STYPE_REASSOC_RESP:
3075 case IEEE80211_STYPE_DEAUTH:
3076 case IEEE80211_STYPE_DISASSOC:
3077 skb_queue_tail(&ifsta->skb_queue, skb);
3078 queue_work(local->hw.workqueue, &ifsta->work);
3079 return;
3080 default:
3081 printk(KERN_DEBUG "%s: received unknown management frame - "
3082 "stype=%d\n", dev->name,
3083 (fc & IEEE80211_FCTL_STYPE) >> 4);
3084 break;
3085 }
3086
3087 fail:
3088 kfree_skb(skb);
3089}
3090
3091
3092static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3093 struct sk_buff *skb)
3094{
3095 struct ieee80211_rx_status *rx_status;
3096 struct ieee80211_sub_if_data *sdata;
3097 struct ieee80211_if_sta *ifsta;
3098 struct ieee80211_mgmt *mgmt;
3099 u16 fc;
3100
3101 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3102 ifsta = &sdata->u.sta;
3103
3104 rx_status = (struct ieee80211_rx_status *) skb->cb;
3105 mgmt = (struct ieee80211_mgmt *) skb->data;
3106 fc = le16_to_cpu(mgmt->frame_control);
3107
3108 switch (fc & IEEE80211_FCTL_STYPE) {
3109 case IEEE80211_STYPE_PROBE_REQ:
3110 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3111 rx_status);
3112 break;
3113 case IEEE80211_STYPE_PROBE_RESP:
3114 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3115 break;
3116 case IEEE80211_STYPE_BEACON:
3117 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3118 break;
3119 case IEEE80211_STYPE_AUTH:
3120 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3121 break;
3122 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003123 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003124 break;
3125 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003126 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003127 break;
3128 case IEEE80211_STYPE_DEAUTH:
3129 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3130 break;
3131 case IEEE80211_STYPE_DISASSOC:
3132 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3133 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003134 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003135 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003136 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07003137 }
3138
3139 kfree_skb(skb);
3140}
3141
3142
Johannes Berg9ae54c82008-01-31 19:48:20 +01003143ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003144ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3145 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003146{
3147 struct ieee80211_mgmt *mgmt;
3148 u16 fc;
3149
Zhu Yiece8edd2007-11-22 10:53:21 +08003150 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003151 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003152
3153 mgmt = (struct ieee80211_mgmt *) skb->data;
3154 fc = le16_to_cpu(mgmt->frame_control);
3155
Zhu Yiece8edd2007-11-22 10:53:21 +08003156 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01003157 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003158
3159 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003160 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003161
Jiri Bencf0706e82007-05-05 11:45:53 -07003162 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3163 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3164 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3165 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003166 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003167 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003168 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3169 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3170 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003171 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003172 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003173 }
3174 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01003175 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003176}
3177
3178
3179static int ieee80211_sta_active_ibss(struct net_device *dev)
3180{
3181 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3182 int active = 0;
3183 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003184 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003185
Johannes Bergd0709a62008-02-25 16:27:46 +01003186 rcu_read_lock();
3187
3188 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3189 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003190 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3191 jiffies)) {
3192 active++;
3193 break;
3194 }
3195 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003196
3197 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003198
3199 return active;
3200}
3201
3202
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003203static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003204{
3205 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3206 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003207 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003208 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003209 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003210
Johannes Bergd0709a62008-02-25 16:27:46 +01003211 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003212 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003213 if (time_after(jiffies, sta->last_rx + exp_time)) {
Joe Perches0795af52007-10-03 17:59:30 -07003214 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3215 dev->name, print_mac(mac, sta->addr));
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003216 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003217 if (sta)
3218 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003219 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003220 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003221
Johannes Bergd0709a62008-02-25 16:27:46 +01003222 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3223 sta_info_destroy(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003224}
3225
3226
3227static void ieee80211_sta_merge_ibss(struct net_device *dev,
3228 struct ieee80211_if_sta *ifsta)
3229{
3230 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3231
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003232 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07003233 if (ieee80211_sta_active_ibss(dev))
3234 return;
3235
3236 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3237 "IBSS networks with same SSID (merge)\n", dev->name);
3238 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3239}
3240
3241
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003242#ifdef CONFIG_MAC80211_MESH
3243static void ieee80211_mesh_housekeeping(struct net_device *dev,
3244 struct ieee80211_if_sta *ifsta)
3245{
3246 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3247 bool free_plinks;
3248
3249 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3250 mesh_path_expire(dev);
3251
3252 free_plinks = mesh_plink_availables(sdata);
3253 if (free_plinks != sdata->u.sta.accepting_plinks)
3254 ieee80211_if_config_beacon(dev);
3255
3256 mod_timer(&ifsta->timer, jiffies +
3257 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3258}
3259
3260
3261void ieee80211_start_mesh(struct net_device *dev)
3262{
3263 struct ieee80211_if_sta *ifsta;
3264 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3265 ifsta = &sdata->u.sta;
3266 ifsta->state = IEEE80211_MESH_UP;
3267 ieee80211_sta_timer((unsigned long)sdata);
3268}
3269#endif
3270
3271
Jiri Bencf0706e82007-05-05 11:45:53 -07003272void ieee80211_sta_timer(unsigned long data)
3273{
3274 struct ieee80211_sub_if_data *sdata =
3275 (struct ieee80211_sub_if_data *) data;
3276 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3277 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3278
3279 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3280 queue_work(local->hw.workqueue, &ifsta->work);
3281}
3282
Jiri Bencf0706e82007-05-05 11:45:53 -07003283void ieee80211_sta_work(struct work_struct *work)
3284{
3285 struct ieee80211_sub_if_data *sdata =
3286 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3287 struct net_device *dev = sdata->dev;
3288 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3289 struct ieee80211_if_sta *ifsta;
3290 struct sk_buff *skb;
3291
3292 if (!netif_running(dev))
3293 return;
3294
Zhu Yiece8edd2007-11-22 10:53:21 +08003295 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003296 return;
3297
Johannes Berg51fb61e2007-12-19 01:31:27 +01003298 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003299 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3300 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003301 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01003302 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e82007-05-05 11:45:53 -07003303 return;
3304 }
3305 ifsta = &sdata->u.sta;
3306
3307 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3308 ieee80211_sta_rx_queued_mgmt(dev, skb);
3309
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003310#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003311 if (ifsta->preq_queue_len &&
3312 time_after(jiffies,
3313 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003314 mesh_path_start_discovery(dev);
3315#endif
3316
Jiri Bencf0706e82007-05-05 11:45:53 -07003317 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3318 ifsta->state != IEEE80211_ASSOCIATE &&
3319 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003320 if (ifsta->scan_ssid_len)
3321 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3322 else
3323 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003324 return;
3325 }
3326
3327 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3328 if (ieee80211_sta_config_auth(dev, ifsta))
3329 return;
3330 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3331 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3332 return;
3333
3334 switch (ifsta->state) {
3335 case IEEE80211_DISABLED:
3336 break;
3337 case IEEE80211_AUTHENTICATE:
3338 ieee80211_authenticate(dev, ifsta);
3339 break;
3340 case IEEE80211_ASSOCIATE:
3341 ieee80211_associate(dev, ifsta);
3342 break;
3343 case IEEE80211_ASSOCIATED:
3344 ieee80211_associated(dev, ifsta);
3345 break;
3346 case IEEE80211_IBSS_SEARCH:
3347 ieee80211_sta_find_ibss(dev, ifsta);
3348 break;
3349 case IEEE80211_IBSS_JOINED:
3350 ieee80211_sta_merge_ibss(dev, ifsta);
3351 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003352#ifdef CONFIG_MAC80211_MESH
3353 case IEEE80211_MESH_UP:
3354 ieee80211_mesh_housekeeping(dev, ifsta);
3355 break;
3356#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003357 default:
3358 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3359 ifsta->state);
3360 break;
3361 }
3362
3363 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3364 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3365 "mixed-cell disabled - disassociate\n", dev->name);
3366
3367 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3368 ieee80211_set_disassoc(dev, ifsta, 0);
3369 }
3370}
3371
3372
3373static void ieee80211_sta_reset_auth(struct net_device *dev,
3374 struct ieee80211_if_sta *ifsta)
3375{
3376 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3377
3378 if (local->ops->reset_tsf) {
3379 /* Reset own TSF to allow time synchronization work. */
3380 local->ops->reset_tsf(local_to_hw(local));
3381 }
3382
3383 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3384
3385
3386 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3387 ifsta->auth_alg = WLAN_AUTH_OPEN;
3388 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3389 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3390 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3391 ifsta->auth_alg = WLAN_AUTH_LEAP;
3392 else
3393 ifsta->auth_alg = WLAN_AUTH_OPEN;
3394 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3395 ifsta->auth_alg);
3396 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003397 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3398 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003399 netif_carrier_off(dev);
3400}
3401
3402
3403void ieee80211_sta_req_auth(struct net_device *dev,
3404 struct ieee80211_if_sta *ifsta)
3405{
3406 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3407 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3408
Johannes Berg51fb61e2007-12-19 01:31:27 +01003409 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003410 return;
3411
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003412 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3413 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3414 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3415 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003416 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3417 queue_work(local->hw.workqueue, &ifsta->work);
3418 }
3419}
3420
3421static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3422 const char *ssid, int ssid_len)
3423{
3424 int tmp, hidden_ssid;
3425
Michael Wu48225702007-10-19 17:14:36 -04003426 if (ssid_len == ifsta->ssid_len &&
3427 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003428 return 1;
3429
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003430 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003431 return 0;
3432
3433 hidden_ssid = 1;
3434 tmp = ssid_len;
3435 while (tmp--) {
3436 if (ssid[tmp] != '\0') {
3437 hidden_ssid = 0;
3438 break;
3439 }
3440 }
3441
3442 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3443 return 1;
3444
3445 if (ssid_len == 1 && ssid[0] == ' ')
3446 return 1;
3447
3448 return 0;
3449}
3450
3451static int ieee80211_sta_config_auth(struct net_device *dev,
3452 struct ieee80211_if_sta *ifsta)
3453{
3454 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3455 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3456 struct ieee80211_sta_bss *bss, *selected = NULL;
3457 int top_rssi = 0, freq;
3458
Jiri Bencf0706e82007-05-05 11:45:53 -07003459 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003460 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003461 list_for_each_entry(bss, &local->sta_bss_list, list) {
3462 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3463 continue;
3464
Abhijeet Kolekar34a961f2008-05-09 09:35:41 -07003465 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3466 IEEE80211_STA_AUTO_BSSID_SEL |
3467 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3468 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3469 !!sdata->default_key))
Jiri Bencf0706e82007-05-05 11:45:53 -07003470 continue;
3471
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003472 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3473 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003474 continue;
3475
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003476 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003477 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3478 continue;
3479
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003480 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003481 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3482 continue;
3483
3484 if (!selected || top_rssi < bss->rssi) {
3485 selected = bss;
3486 top_rssi = bss->rssi;
3487 }
3488 }
3489 if (selected)
3490 atomic_inc(&selected->users);
3491 spin_unlock_bh(&local->sta_bss_lock);
3492
3493 if (selected) {
Assaf Kraussbe038b32008-06-05 19:55:21 +03003494 ieee80211_set_freq(dev, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003495 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003496 ieee80211_sta_set_ssid(dev, selected->ssid,
3497 selected->ssid_len);
3498 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003499 ieee80211_sta_def_wmm_params(dev, selected, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003500 ieee80211_rx_bss_put(dev, selected);
3501 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003502 ieee80211_sta_reset_auth(dev, ifsta);
3503 return 0;
3504 } else {
3505 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003506 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003507 ieee80211_sta_start_scan(dev, NULL, 0);
3508 else
3509 ieee80211_sta_start_scan(dev, ifsta->ssid,
3510 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003511 ifsta->state = IEEE80211_AUTHENTICATE;
3512 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3513 } else
3514 ifsta->state = IEEE80211_DISABLED;
3515 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003516 return -1;
3517}
3518
Jiri Bencf0706e82007-05-05 11:45:53 -07003519
3520static int ieee80211_sta_create_ibss(struct net_device *dev,
3521 struct ieee80211_if_sta *ifsta)
3522{
3523 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3524 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003525 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003526 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003527 u8 bssid[ETH_ALEN], *pos;
3528 int i;
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003529 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003530 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003531
3532#if 0
3533 /* Easier testing, use fixed BSSID. */
3534 memset(bssid, 0xfe, ETH_ALEN);
3535#else
3536 /* Generate random, not broadcast, locally administered BSSID. Mix in
3537 * own MAC address to make sure that devices that do not have proper
3538 * random number generator get different BSSID. */
3539 get_random_bytes(bssid, ETH_ALEN);
3540 for (i = 0; i < ETH_ALEN; i++)
3541 bssid[i] ^= dev->dev_addr[i];
3542 bssid[0] &= ~0x01;
3543 bssid[0] |= 0x02;
3544#endif
3545
Joe Perches0795af52007-10-03 17:59:30 -07003546 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3547 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003548
Johannes Berg8318d782008-01-24 19:38:38 +01003549 bss = ieee80211_rx_bss_add(dev, bssid,
3550 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003551 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003552 if (!bss)
3553 return -ENOMEM;
3554
Johannes Berg8318d782008-01-24 19:38:38 +01003555 bss->band = local->hw.conf.channel->band;
3556 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003557
3558 if (local->hw.conf.beacon_int == 0)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003559 local->hw.conf.beacon_int = 10000;
Jiri Bencf0706e82007-05-05 11:45:53 -07003560 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003561 bss->last_update = jiffies;
3562 bss->capability = WLAN_CAPABILITY_IBSS;
3563 if (sdata->default_key) {
3564 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3565 } else
3566 sdata->drop_unencrypted = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003567 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003568 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003569 for (i = 0; i < sband->n_bitrates; i++) {
3570 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003571 *pos++ = (u8) (rate / 5);
3572 }
3573
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003574 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3575 ieee80211_rx_bss_put(dev, bss);
3576 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003577}
3578
3579
3580static int ieee80211_sta_find_ibss(struct net_device *dev,
3581 struct ieee80211_if_sta *ifsta)
3582{
3583 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3584 struct ieee80211_sta_bss *bss;
3585 int found = 0;
3586 u8 bssid[ETH_ALEN];
3587 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003588 DECLARE_MAC_BUF(mac);
3589 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003590
3591 if (ifsta->ssid_len == 0)
3592 return -EINVAL;
3593
3594 active_ibss = ieee80211_sta_active_ibss(dev);
3595#ifdef CONFIG_MAC80211_IBSS_DEBUG
3596 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3597 dev->name, active_ibss);
3598#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3599 spin_lock_bh(&local->sta_bss_lock);
3600 list_for_each_entry(bss, &local->sta_bss_list, list) {
3601 if (ifsta->ssid_len != bss->ssid_len ||
3602 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3603 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3604 continue;
3605#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003606 printk(KERN_DEBUG " bssid=%s found\n",
3607 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003608#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3609 memcpy(bssid, bss->bssid, ETH_ALEN);
3610 found = 1;
3611 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3612 break;
3613 }
3614 spin_unlock_bh(&local->sta_bss_lock);
3615
3616#ifdef CONFIG_MAC80211_IBSS_DEBUG
Vladimir Koutny6e438292008-07-07 14:23:01 +02003617 if (found)
3618 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3619 "%s\n", print_mac(mac, bssid),
3620 print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003621#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3622 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003623 (bss = ieee80211_rx_bss_get(dev, bssid,
3624 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003625 ifsta->ssid, ifsta->ssid_len))) {
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003626 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003627 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003628 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003629 dev->name, print_mac(mac, bssid));
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003630 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3631 ieee80211_rx_bss_put(dev, bss);
3632 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003633 }
3634#ifdef CONFIG_MAC80211_IBSS_DEBUG
3635 printk(KERN_DEBUG " did not try to join ibss\n");
3636#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3637
3638 /* Selected IBSS not found in current scan results - try to scan */
3639 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3640 !ieee80211_sta_active_ibss(dev)) {
3641 mod_timer(&ifsta->timer, jiffies +
3642 IEEE80211_IBSS_MERGE_INTERVAL);
3643 } else if (time_after(jiffies, local->last_scan_completed +
3644 IEEE80211_SCAN_INTERVAL)) {
3645 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3646 "join\n", dev->name);
3647 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3648 ifsta->ssid_len);
3649 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3650 int interval = IEEE80211_SCAN_INTERVAL;
3651
3652 if (time_after(jiffies, ifsta->ibss_join_req +
3653 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003654 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003655 (!(local->oper_channel->flags &
3656 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003657 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003658 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003659 printk(KERN_DEBUG "%s: IBSS not allowed on"
3660 " %d MHz\n", dev->name,
3661 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003662 }
3663
3664 /* No IBSS found - decrease scan interval and continue
3665 * scanning. */
3666 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3667 }
3668
3669 ifsta->state = IEEE80211_IBSS_SEARCH;
3670 mod_timer(&ifsta->timer, jiffies + interval);
3671 return 0;
3672 }
3673
3674 return 0;
3675}
3676
3677
3678int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3679{
Johannes Berg8318d782008-01-24 19:38:38 +01003680 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003681 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07003682
3683 if (len > IEEE80211_MAX_SSID_LEN)
3684 return -EINVAL;
3685
Jiri Bencf0706e82007-05-05 11:45:53 -07003686 ifsta = &sdata->u.sta;
3687
3688 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003689 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003690 memcpy(ifsta->ssid, ssid, len);
3691 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3692 ifsta->ssid_len = len;
3693
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003694 if (len)
3695 ifsta->flags |= IEEE80211_STA_SSID_SET;
3696 else
3697 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003698 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003699 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003700 ifsta->ibss_join_req = jiffies;
3701 ifsta->state = IEEE80211_IBSS_SEARCH;
3702 return ieee80211_sta_find_ibss(dev, ifsta);
3703 }
3704 return 0;
3705}
3706
3707
3708int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3709{
3710 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3711 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3712 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3713 *len = ifsta->ssid_len;
3714 return 0;
3715}
3716
3717
3718int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3719{
3720 struct ieee80211_sub_if_data *sdata;
3721 struct ieee80211_if_sta *ifsta;
3722 int res;
3723
3724 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3725 ifsta = &sdata->u.sta;
3726
3727 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3728 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3729 res = ieee80211_if_config(dev);
3730 if (res) {
3731 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3732 "the low-level driver\n", dev->name);
3733 return res;
3734 }
3735 }
3736
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003737 if (is_valid_ether_addr(bssid))
3738 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003739 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003740 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3741
Jiri Bencf0706e82007-05-05 11:45:53 -07003742 return 0;
3743}
3744
3745
3746static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3747 struct ieee80211_sub_if_data *sdata,
3748 int powersave)
3749{
3750 struct sk_buff *skb;
3751 struct ieee80211_hdr *nullfunc;
3752 u16 fc;
3753
3754 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3755 if (!skb) {
3756 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3757 "frame\n", sdata->dev->name);
3758 return;
3759 }
3760 skb_reserve(skb, local->hw.extra_tx_headroom);
3761
3762 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3763 memset(nullfunc, 0, 24);
3764 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3765 IEEE80211_FCTL_TODS;
3766 if (powersave)
3767 fc |= IEEE80211_FCTL_PM;
3768 nullfunc->frame_control = cpu_to_le16(fc);
3769 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3770 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3771 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3772
3773 ieee80211_sta_tx(sdata->dev, skb, 0);
3774}
3775
3776
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003777static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3778{
3779 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3780 ieee80211_vif_is_mesh(&sdata->vif))
3781 ieee80211_sta_timer((unsigned long)sdata);
3782}
3783
Jiri Bencf0706e82007-05-05 11:45:53 -07003784void ieee80211_scan_completed(struct ieee80211_hw *hw)
3785{
3786 struct ieee80211_local *local = hw_to_local(hw);
3787 struct net_device *dev = local->scan_dev;
3788 struct ieee80211_sub_if_data *sdata;
3789 union iwreq_data wrqu;
3790
3791 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003792 memset(&wrqu, 0, sizeof(wrqu));
3793 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003794
Zhu Yiece8edd2007-11-22 10:53:21 +08003795 if (local->sta_hw_scanning) {
3796 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003797 if (ieee80211_hw_config(local))
3798 printk(KERN_DEBUG "%s: failed to restore operational "
3799 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003800 /* Restart STA timer for HW scan case */
3801 rcu_read_lock();
3802 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3803 ieee80211_restart_sta_timer(sdata);
3804 rcu_read_unlock();
3805
Zhu Yiece8edd2007-11-22 10:53:21 +08003806 goto done;
3807 }
3808
3809 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003810 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003811 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003812 "channel after scan\n", dev->name);
3813
Johannes Berg4150c572007-09-17 01:29:23 -04003814
3815 netif_tx_lock_bh(local->mdev);
3816 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3817 local->ops->configure_filter(local_to_hw(local),
3818 FIF_BCN_PRBRESP_PROMISC,
3819 &local->filter_flags,
3820 local->mdev->mc_count,
3821 local->mdev->mc_list);
3822
3823 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003824
Johannes Berg79010422007-09-18 17:29:21 -04003825 rcu_read_lock();
3826 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003827
3828 /* No need to wake the master device. */
3829 if (sdata->dev == local->mdev)
3830 continue;
3831
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003832 /* Tell AP we're back */
3833 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3834 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3835 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003836
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003837 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003838
Jiri Bencf0706e82007-05-05 11:45:53 -07003839 netif_wake_queue(sdata->dev);
3840 }
Johannes Berg79010422007-09-18 17:29:21 -04003841 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003842
Zhu Yiece8edd2007-11-22 10:53:21 +08003843done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003844 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003845 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003846 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003847 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003848 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3849 !ieee80211_sta_active_ibss(dev)))
3850 ieee80211_sta_find_ibss(dev, ifsta);
3851 }
3852}
3853EXPORT_SYMBOL(ieee80211_scan_completed);
3854
3855void ieee80211_sta_scan_work(struct work_struct *work)
3856{
3857 struct ieee80211_local *local =
3858 container_of(work, struct ieee80211_local, scan_work.work);
3859 struct net_device *dev = local->scan_dev;
3860 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003861 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003862 struct ieee80211_channel *chan;
3863 int skip;
3864 unsigned long next_delay = 0;
3865
Zhu Yiece8edd2007-11-22 10:53:21 +08003866 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003867 return;
3868
3869 switch (local->scan_state) {
3870 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003871 /*
3872 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3873 * after we successfully scanned the last channel of the last
3874 * band (and the last band is supported by the hw)
3875 */
Johannes Berg8318d782008-01-24 19:38:38 +01003876 if (local->scan_band < IEEE80211_NUM_BANDS)
3877 sband = local->hw.wiphy->bands[local->scan_band];
3878 else
3879 sband = NULL;
3880
Johannes Berg69d464d2008-01-28 17:08:56 +01003881 /*
3882 * If we are at an unsupported band and have more bands
3883 * left to scan, advance to the next supported one.
3884 */
3885 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003886 local->scan_band++;
3887 sband = local->hw.wiphy->bands[local->scan_band];
3888 local->scan_channel_idx = 0;
3889 }
3890
Johannes Berg69d464d2008-01-28 17:08:56 +01003891 /* if no more bands/channels left, complete scan */
3892 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003893 ieee80211_scan_completed(local_to_hw(local));
3894 return;
3895 }
Johannes Berg8318d782008-01-24 19:38:38 +01003896 skip = 0;
3897 chan = &sband->channels[local->scan_channel_idx];
3898
3899 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003900 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003901 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003902 skip = 1;
3903
3904 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003905 local->scan_channel = chan;
3906 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003907 printk(KERN_DEBUG "%s: failed to set freq to "
3908 "%d MHz for scan\n", dev->name,
3909 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003910 skip = 1;
3911 }
3912 }
3913
Johannes Berg69d464d2008-01-28 17:08:56 +01003914 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003915 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003916 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003917 /*
3918 * scan_band may end up == IEEE80211_NUM_BANDS, but
3919 * we'll catch that case above and complete the scan
3920 * if that is the case.
3921 */
Johannes Berg8318d782008-01-24 19:38:38 +01003922 local->scan_band++;
3923 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003924 }
3925
3926 if (skip)
3927 break;
3928
3929 next_delay = IEEE80211_PROBE_DELAY +
3930 usecs_to_jiffies(local->hw.channel_change_time);
3931 local->scan_state = SCAN_SEND_PROBE;
3932 break;
3933 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003934 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003935 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003936
3937 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3938 break;
3939 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3940 local->scan_ssid_len);
3941 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003942 break;
3943 }
3944
Zhu Yiece8edd2007-11-22 10:53:21 +08003945 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003946 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3947 next_delay);
3948}
3949
3950
3951static int ieee80211_sta_start_scan(struct net_device *dev,
3952 u8 *ssid, size_t ssid_len)
3953{
3954 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3955 struct ieee80211_sub_if_data *sdata;
3956
3957 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3958 return -EINVAL;
3959
3960 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3961 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3962 * BSSID: MACAddress
3963 * SSID
3964 * ScanType: ACTIVE, PASSIVE
3965 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3966 * a Probe frame during active scanning
3967 * ChannelList
3968 * MinChannelTime (>= ProbeDelay), in TU
3969 * MaxChannelTime: (>= MinChannelTime), in TU
3970 */
3971
3972 /* MLME-SCAN.confirm
3973 * BSSDescriptionSet
3974 * ResultCode: SUCCESS, INVALID_PARAMETERS
3975 */
3976
Zhu Yiece8edd2007-11-22 10:53:21 +08003977 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003978 if (local->scan_dev == dev)
3979 return 0;
3980 return -EBUSY;
3981 }
3982
3983 if (local->ops->hw_scan) {
3984 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003985 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003986 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003987 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003988 local->scan_dev = dev;
3989 }
3990 return rc;
3991 }
3992
Zhu Yiece8edd2007-11-22 10:53:21 +08003993 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003994
Johannes Berg79010422007-09-18 17:29:21 -04003995 rcu_read_lock();
3996 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003997
3998 /* Don't stop the master interface, otherwise we can't transmit
3999 * probes! */
4000 if (sdata->dev == local->mdev)
4001 continue;
4002
Jiri Bencf0706e82007-05-05 11:45:53 -07004003 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01004004 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004005 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004006 ieee80211_send_nullfunc(local, sdata, 1);
4007 }
Johannes Berg79010422007-09-18 17:29:21 -04004008 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07004009
4010 if (ssid) {
4011 local->scan_ssid_len = ssid_len;
4012 memcpy(local->scan_ssid, ssid, ssid_len);
4013 } else
4014 local->scan_ssid_len = 0;
4015 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07004016 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004017 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07004018 local->scan_dev = dev;
4019
Johannes Berg4150c572007-09-17 01:29:23 -04004020 netif_tx_lock_bh(local->mdev);
4021 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4022 local->ops->configure_filter(local_to_hw(local),
4023 FIF_BCN_PRBRESP_PROMISC,
4024 &local->filter_flags,
4025 local->mdev->mc_count,
4026 local->mdev->mc_list);
4027 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07004028
4029 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4030 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4031 IEEE80211_CHANNEL_TIME);
4032
4033 return 0;
4034}
4035
4036
4037int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4038{
4039 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4040 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4041 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4042
Johannes Berg51fb61e2007-12-19 01:31:27 +01004043 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004044 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4045
Zhu Yiece8edd2007-11-22 10:53:21 +08004046 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004047 if (local->scan_dev == dev)
4048 return 0;
4049 return -EBUSY;
4050 }
4051
Helmut Schaaa0af5f12007-11-09 16:25:08 +01004052 ifsta->scan_ssid_len = ssid_len;
4053 if (ssid_len)
4054 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07004055 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4056 queue_work(local->hw.workqueue, &ifsta->work);
4057 return 0;
4058}
4059
4060static char *
4061ieee80211_sta_scan_result(struct net_device *dev,
4062 struct ieee80211_sta_bss *bss,
4063 char *current_ev, char *end_buf)
4064{
4065 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4066 struct iw_event iwe;
4067
4068 if (time_after(jiffies,
4069 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4070 return current_ev;
4071
Jiri Bencf0706e82007-05-05 11:45:53 -07004072 memset(&iwe, 0, sizeof(iwe));
4073 iwe.cmd = SIOCGIWAP;
4074 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4075 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4076 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4077 IW_EV_ADDR_LEN);
4078
4079 memset(&iwe, 0, sizeof(iwe));
4080 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01004081 if (bss_mesh_cfg(bss)) {
4082 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004083 iwe.u.data.flags = 1;
4084 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
Johannes Berg902acc72008-02-23 15:17:19 +01004085 bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004086 } else {
4087 iwe.u.data.length = bss->ssid_len;
4088 iwe.u.data.flags = 1;
4089 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4090 bss->ssid);
4091 }
Jiri Bencf0706e82007-05-05 11:45:53 -07004092
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08004093 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4094 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004095 memset(&iwe, 0, sizeof(iwe));
4096 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01004097 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004098 iwe.u.mode = IW_MODE_MESH;
4099 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004100 iwe.u.mode = IW_MODE_MASTER;
4101 else
4102 iwe.u.mode = IW_MODE_ADHOC;
4103 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4104 IW_EV_UINT_LEN);
4105 }
4106
4107 memset(&iwe, 0, sizeof(iwe));
4108 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01004109 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4110 iwe.u.freq.e = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07004111 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4112 IW_EV_FREQ_LEN);
4113
4114 memset(&iwe, 0, sizeof(iwe));
Tomas Winkler9381be02008-05-23 01:36:36 +03004115 iwe.cmd = SIOCGIWFREQ;
4116 iwe.u.freq.m = bss->freq;
4117 iwe.u.freq.e = 6;
4118 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4119 IW_EV_FREQ_LEN);
4120 memset(&iwe, 0, sizeof(iwe));
Jiri Bencf0706e82007-05-05 11:45:53 -07004121 iwe.cmd = IWEVQUAL;
4122 iwe.u.qual.qual = bss->signal;
4123 iwe.u.qual.level = bss->rssi;
4124 iwe.u.qual.noise = bss->noise;
4125 iwe.u.qual.updated = local->wstats_flags;
4126 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4127 IW_EV_QUAL_LEN);
4128
4129 memset(&iwe, 0, sizeof(iwe));
4130 iwe.cmd = SIOCGIWENCODE;
4131 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4132 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4133 else
4134 iwe.u.data.flags = IW_ENCODE_DISABLED;
4135 iwe.u.data.length = 0;
4136 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4137
4138 if (bss && bss->wpa_ie) {
4139 memset(&iwe, 0, sizeof(iwe));
4140 iwe.cmd = IWEVGENIE;
4141 iwe.u.data.length = bss->wpa_ie_len;
4142 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4143 bss->wpa_ie);
4144 }
4145
4146 if (bss && bss->rsn_ie) {
4147 memset(&iwe, 0, sizeof(iwe));
4148 iwe.cmd = IWEVGENIE;
4149 iwe.u.data.length = bss->rsn_ie_len;
4150 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4151 bss->rsn_ie);
4152 }
4153
4154 if (bss && bss->supp_rates_len > 0) {
4155 /* display all supported rates in readable format */
4156 char *p = current_ev + IW_EV_LCP_LEN;
4157 int i;
4158
4159 memset(&iwe, 0, sizeof(iwe));
4160 iwe.cmd = SIOCGIWRATE;
4161 /* Those two flags are ignored... */
4162 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4163
4164 for (i = 0; i < bss->supp_rates_len; i++) {
4165 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4166 0x7f) * 500000);
4167 p = iwe_stream_add_value(current_ev, p,
4168 end_buf, &iwe, IW_EV_PARAM_LEN);
4169 }
4170 current_ev = p;
4171 }
4172
4173 if (bss) {
4174 char *buf;
4175 buf = kmalloc(30, GFP_ATOMIC);
4176 if (buf) {
4177 memset(&iwe, 0, sizeof(iwe));
4178 iwe.cmd = IWEVCUSTOM;
4179 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4180 iwe.u.data.length = strlen(buf);
4181 current_ev = iwe_stream_add_point(current_ev, end_buf,
4182 &iwe, buf);
4183 kfree(buf);
4184 }
4185 }
4186
Johannes Berg902acc72008-02-23 15:17:19 +01004187 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004188 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04004189 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004190 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004191 if (buf) {
4192 memset(&iwe, 0, sizeof(iwe));
4193 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04004194 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004195 iwe.u.data.length = strlen(buf);
4196 current_ev = iwe_stream_add_point(current_ev, end_buf,
4197 &iwe, buf);
4198 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04004199 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4200 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004201 iwe.u.data.length = strlen(buf);
4202 current_ev = iwe_stream_add_point(current_ev, end_buf,
4203 &iwe, buf);
4204 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04004205 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4206 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004207 iwe.u.data.length = strlen(buf);
4208 current_ev = iwe_stream_add_point(current_ev, end_buf,
4209 &iwe, buf);
4210 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04004211 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4212 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004213 iwe.u.data.length = strlen(buf);
4214 current_ev = iwe_stream_add_point(current_ev, end_buf,
4215 &iwe, buf);
4216 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04004217 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4218 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004219 iwe.u.data.length = strlen(buf);
4220 current_ev = iwe_stream_add_point(current_ev, end_buf,
4221 &iwe, buf);
4222 kfree(buf);
4223 }
4224 }
4225
Jiri Bencf0706e82007-05-05 11:45:53 -07004226 return current_ev;
4227}
4228
4229
4230int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4231{
4232 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4233 char *current_ev = buf;
4234 char *end_buf = buf + len;
4235 struct ieee80211_sta_bss *bss;
4236
4237 spin_lock_bh(&local->sta_bss_lock);
4238 list_for_each_entry(bss, &local->sta_bss_list, list) {
4239 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4240 spin_unlock_bh(&local->sta_bss_lock);
4241 return -E2BIG;
4242 }
4243 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4244 end_buf);
4245 }
4246 spin_unlock_bh(&local->sta_bss_lock);
4247 return current_ev - buf;
4248}
4249
4250
4251int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4252{
4253 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4254 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4255 kfree(ifsta->extra_ie);
4256 if (len == 0) {
4257 ifsta->extra_ie = NULL;
4258 ifsta->extra_ie_len = 0;
4259 return 0;
4260 }
4261 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4262 if (!ifsta->extra_ie) {
4263 ifsta->extra_ie_len = 0;
4264 return -ENOMEM;
4265 }
4266 memcpy(ifsta->extra_ie, ie, len);
4267 ifsta->extra_ie_len = len;
4268 return 0;
4269}
4270
4271
4272struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4273 struct sk_buff *skb, u8 *bssid,
4274 u8 *addr)
4275{
4276 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4277 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004278 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004279 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07004280
4281 /* TODO: Could consider removing the least recently used entry and
4282 * allow new one to be added. */
4283 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4284 if (net_ratelimit()) {
4285 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004286 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004287 }
4288 return NULL;
4289 }
4290
Joe Perches0795af52007-10-03 17:59:30 -07004291 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004292 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07004293
Johannes Berg73651ee2008-02-25 16:27:47 +01004294 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4295 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004296 return NULL;
4297
Johannes Berg238814f2008-01-28 17:19:37 +01004298 sta->flags |= WLAN_STA_AUTHORIZED;
4299
Johannes Berg8318d782008-01-24 19:38:38 +01004300 sta->supp_rates[local->hw.conf.channel->band] =
4301 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004302
4303 rate_control_rate_init(sta, local);
4304
Johannes Berg93e5deb2008-04-01 15:21:00 +02004305 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004306 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004307
Johannes Bergd0709a62008-02-25 16:27:46 +01004308 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004309}
4310
4311
4312int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4313{
4314 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4315 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4316
4317 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4318 dev->name, reason);
4319
Johannes Berg51fb61e2007-12-19 01:31:27 +01004320 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4321 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004322 return -EINVAL;
4323
4324 ieee80211_send_deauth(dev, ifsta, reason);
4325 ieee80211_set_disassoc(dev, ifsta, 1);
4326 return 0;
4327}
4328
4329
4330int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4331{
4332 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4333 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4334
4335 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4336 dev->name, reason);
4337
Johannes Berg51fb61e2007-12-19 01:31:27 +01004338 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004339 return -EINVAL;
4340
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004341 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004342 return -1;
4343
4344 ieee80211_send_disassoc(dev, ifsta, reason);
4345 ieee80211_set_disassoc(dev, ifsta, 0);
4346 return 0;
4347}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004348
4349void ieee80211_notify_mac(struct ieee80211_hw *hw,
4350 enum ieee80211_notification_types notif_type)
4351{
4352 struct ieee80211_local *local = hw_to_local(hw);
4353 struct ieee80211_sub_if_data *sdata;
4354
4355 switch (notif_type) {
4356 case IEEE80211_NOTIFY_RE_ASSOC:
4357 rcu_read_lock();
4358 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4359
4360 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4361 ieee80211_sta_req_auth(sdata->dev,
4362 &sdata->u.sta);
4363 }
4364
4365 }
4366 rcu_read_unlock();
4367 break;
4368 }
4369}
4370EXPORT_SYMBOL(ieee80211_notify_mac);