blob: 2a3f8a8e9a436fa050def9f13aca16ba56011467 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
Jiri Bencf0706e82007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010027#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
33#include "ieee80211_rate.h"
Michael Buesch47f0c502007-09-27 15:10:44 +020034#include "ieee80211_led.h"
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010035#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
37#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40#define IEEE80211_ASSOC_MAX_TRIES 3
41#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010042#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070043#define IEEE80211_PROBE_INTERVAL (60 * HZ)
44#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45#define IEEE80211_SCAN_INTERVAL (2 * HZ)
46#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47#define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
48
49#define IEEE80211_PROBE_DELAY (HZ / 33)
50#define IEEE80211_CHANNEL_TIME (HZ / 33)
51#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010055#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070056
57#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
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
353
Johannes Berg471b3ef2007-12-28 14:32:58 +0100354static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
355 u8 erp_value)
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;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100359 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200360 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
Joe Perches0795af52007-10-03 17:59:30 -0700361 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100362 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200363
Johannes Berg471b3ef2007-12-28 14:32:58 +0100364 if (use_protection != bss_conf->use_cts_prot) {
Daniel Drake56282212007-07-10 19:32:10 +0200365 if (net_ratelimit()) {
366 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700367 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100368 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200369 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700370 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200371 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100372 bss_conf->use_cts_prot = use_protection;
373 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200374 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200375
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200376 if (use_short_preamble != bss_conf->use_short_preamble) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200377 if (net_ratelimit()) {
378 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700379 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100380 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200381 use_short_preamble ? "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700382 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200383 }
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200384 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100385 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200386 }
Daniel Draked9430a32007-07-27 15:43:24 +0200387
Johannes Berg471b3ef2007-12-28 14:32:58 +0100388 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200389}
390
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200391int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
392 struct ieee80211_ht_info *ht_info)
393{
394
395 if (ht_info == NULL)
396 return -EINVAL;
397
398 memset(ht_info, 0, sizeof(*ht_info));
399
400 if (ht_cap_ie) {
401 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
402
403 ht_info->ht_supported = 1;
404 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
405 ht_info->ampdu_factor =
406 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
407 ht_info->ampdu_density =
408 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
409 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
410 } else
411 ht_info->ht_supported = 0;
412
413 return 0;
414}
415
416int ieee80211_ht_addt_info_ie_to_ht_bss_info(
417 struct ieee80211_ht_addt_info *ht_add_info_ie,
418 struct ieee80211_ht_bss_info *bss_info)
419{
420 if (bss_info == NULL)
421 return -EINVAL;
422
423 memset(bss_info, 0, sizeof(*bss_info));
424
425 if (ht_add_info_ie) {
426 u16 op_mode;
427 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
428
429 bss_info->primary_channel = ht_add_info_ie->control_chan;
430 bss_info->bss_cap = ht_add_info_ie->ht_param;
431 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
432 }
433
434 return 0;
435}
Daniel Drake56282212007-07-10 19:32:10 +0200436
Jiri Bencf0706e82007-05-05 11:45:53 -0700437static void ieee80211_sta_send_associnfo(struct net_device *dev,
438 struct ieee80211_if_sta *ifsta)
439{
440 char *buf;
441 size_t len;
442 int i;
443 union iwreq_data wrqu;
444
445 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
446 return;
447
448 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200449 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700450 if (!buf)
451 return;
452
453 len = sprintf(buf, "ASSOCINFO(");
454 if (ifsta->assocreq_ies) {
455 len += sprintf(buf + len, "ReqIEs=");
456 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
457 len += sprintf(buf + len, "%02x",
458 ifsta->assocreq_ies[i]);
459 }
460 }
461 if (ifsta->assocresp_ies) {
462 if (ifsta->assocreq_ies)
463 len += sprintf(buf + len, " ");
464 len += sprintf(buf + len, "RespIEs=");
465 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
466 len += sprintf(buf + len, "%02x",
467 ifsta->assocresp_ies[i]);
468 }
469 }
470 len += sprintf(buf + len, ")");
471
472 if (len > IW_CUSTOM_MAX) {
473 len = sprintf(buf, "ASSOCRESPIE=");
474 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
475 len += sprintf(buf + len, "%02x",
476 ifsta->assocresp_ies[i]);
477 }
478 }
479
480 memset(&wrqu, 0, sizeof(wrqu));
481 wrqu.data.length = len;
482 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
483
484 kfree(buf);
485}
486
487
488static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400489 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200490 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700491{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100492 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
493 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700494 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700495 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100496 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700497
Jiri Bencf0706e82007-05-05 11:45:53 -0700498 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200499 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400500
501 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
502
Johannes Berg51fb61e2007-12-19 01:31:27 +0100503 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700504 return;
Daniel Drake56282212007-07-10 19:32:10 +0200505
John W. Linville65c107a2007-10-05 14:23:27 -0400506 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Tomas Winkler38668c02008-03-28 16:33:32 -0700507 conf->channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400508 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200509 if (bss) {
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700510 /* set timing information */
511 sdata->bss_conf.beacon_int = bss->beacon_int;
512 sdata->bss_conf.timestamp = bss->timestamp;
513
Daniel Drake56282212007-07-10 19:32:10 +0200514 if (bss->has_erp_value)
Johannes Berg471b3ef2007-12-28 14:32:58 +0100515 changed |= ieee80211_handle_erp_ie(
516 sdata, bss->erp_value);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700517
Daniel Drake56282212007-07-10 19:32:10 +0200518 ieee80211_rx_bss_put(dev, bss);
519 }
520
Tomas Winkler38668c02008-03-28 16:33:32 -0700521 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
522 changed |= BSS_CHANGED_HT;
523 sdata->bss_conf.assoc_ht = 1;
524 sdata->bss_conf.ht_conf = &conf->ht_conf;
525 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
526 }
527
Jiri Bencf0706e82007-05-05 11:45:53 -0700528 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400529 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700530 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
531 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
532 ieee80211_sta_send_associnfo(dev, ifsta);
533 } else {
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700534 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400535 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700536 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200537 ieee80211_reset_erp_info(dev);
Tomas Winkler38668c02008-03-28 16:33:32 -0700538
539 sdata->bss_conf.assoc_ht = 0;
540 sdata->bss_conf.ht_conf = NULL;
541 sdata->bss_conf.ht_bss_conf = NULL;
542
Jiri Bencf0706e82007-05-05 11:45:53 -0700543 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
544 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700545 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200546 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100547
Tomas Winklerb2205252008-01-27 16:18:22 +0200548 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100549 ieee80211_bss_info_change_notify(sdata, changed);
Reinette Chatre41a7be42008-04-03 16:08:49 -0700550 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
551 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700552}
553
554static void ieee80211_set_disassoc(struct net_device *dev,
555 struct ieee80211_if_sta *ifsta, int deauth)
556{
557 if (deauth)
558 ifsta->auth_tries = 0;
559 ifsta->assoc_tries = 0;
560 ieee80211_set_associated(dev, ifsta, 0);
561}
562
Luis Carlos Coboee385852008-02-23 15:17:11 +0100563void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
564 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700565{
566 struct ieee80211_sub_if_data *sdata;
567 struct ieee80211_tx_packet_data *pkt_data;
568
569 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
570 skb->dev = sdata->local->mdev;
571 skb_set_mac_header(skb, 0);
572 skb_set_network_header(skb, 0);
573 skb_set_transport_header(skb, 0);
574
575 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
576 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
577 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400578 if (!encrypt)
579 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700580
581 dev_queue_xmit(skb);
582}
583
584
585static void ieee80211_send_auth(struct net_device *dev,
586 struct ieee80211_if_sta *ifsta,
587 int transaction, u8 *extra, size_t extra_len,
588 int encrypt)
589{
590 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
591 struct sk_buff *skb;
592 struct ieee80211_mgmt *mgmt;
593
594 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
595 sizeof(*mgmt) + 6 + extra_len);
596 if (!skb) {
597 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
598 "frame\n", dev->name);
599 return;
600 }
601 skb_reserve(skb, local->hw.extra_tx_headroom);
602
603 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
604 memset(mgmt, 0, 24 + 6);
605 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
606 IEEE80211_STYPE_AUTH);
607 if (encrypt)
608 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
609 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
610 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
611 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
612 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
613 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
614 ifsta->auth_transaction = transaction + 1;
615 mgmt->u.auth.status_code = cpu_to_le16(0);
616 if (extra)
617 memcpy(skb_put(skb, extra_len), extra, extra_len);
618
619 ieee80211_sta_tx(dev, skb, encrypt);
620}
621
622
623static void ieee80211_authenticate(struct net_device *dev,
624 struct ieee80211_if_sta *ifsta)
625{
Joe Perches0795af52007-10-03 17:59:30 -0700626 DECLARE_MAC_BUF(mac);
627
Jiri Bencf0706e82007-05-05 11:45:53 -0700628 ifsta->auth_tries++;
629 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700630 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700631 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700632 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700633 ifsta->state = IEEE80211_DISABLED;
634 return;
635 }
636
637 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700638 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
639 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700640
641 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
642
643 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
644}
645
646
647static void ieee80211_send_assoc(struct net_device *dev,
648 struct ieee80211_if_sta *ifsta)
649{
650 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700651 struct sk_buff *skb;
652 struct ieee80211_mgmt *mgmt;
653 u8 *pos, *ies;
654 int i, len;
655 u16 capab;
656 struct ieee80211_sta_bss *bss;
657 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100658 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700659
660 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
661 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
662 ifsta->ssid_len);
663 if (!skb) {
664 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
665 "frame\n", dev->name);
666 return;
667 }
668 skb_reserve(skb, local->hw.extra_tx_headroom);
669
Johannes Berg8318d782008-01-24 19:38:38 +0100670 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
671
Jiri Bencf0706e82007-05-05 11:45:53 -0700672 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100673
674 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
675 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
676 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
677 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
678 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700679 }
Johannes Berg8318d782008-01-24 19:38:38 +0100680
681 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
682 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400683 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700684 if (bss) {
685 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
686 capab |= WLAN_CAPABILITY_PRIVACY;
687 if (bss->wmm_ie) {
688 wmm = 1;
689 }
690 ieee80211_rx_bss_put(dev, bss);
691 }
692
693 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
694 memset(mgmt, 0, 24);
695 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
696 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
697 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
698
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400699 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700700 skb_put(skb, 10);
701 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
702 IEEE80211_STYPE_REASSOC_REQ);
703 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
704 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
705 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
706 ETH_ALEN);
707 } else {
708 skb_put(skb, 4);
709 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
710 IEEE80211_STYPE_ASSOC_REQ);
711 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
712 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
713 }
714
715 /* SSID */
716 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
717 *pos++ = WLAN_EID_SSID;
718 *pos++ = ifsta->ssid_len;
719 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
720
Johannes Berg8318d782008-01-24 19:38:38 +0100721 len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700722 if (len > 8)
723 len = 8;
724 pos = skb_put(skb, len + 2);
725 *pos++ = WLAN_EID_SUPP_RATES;
726 *pos++ = len;
727 for (i = 0; i < len; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100728 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700729 *pos++ = (u8) (rate / 5);
730 }
731
Johannes Berg8318d782008-01-24 19:38:38 +0100732 if (sband->n_bitrates > len) {
733 pos = skb_put(skb, sband->n_bitrates - len + 2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700734 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Johannes Berg8318d782008-01-24 19:38:38 +0100735 *pos++ = sband->n_bitrates - len;
736 for (i = len; i < sband->n_bitrates; i++) {
737 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700738 *pos++ = (u8) (rate / 5);
739 }
740 }
741
742 if (ifsta->extra_ie) {
743 pos = skb_put(skb, ifsta->extra_ie_len);
744 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
745 }
746
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400747 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700748 pos = skb_put(skb, 9);
749 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
750 *pos++ = 7; /* len */
751 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
752 *pos++ = 0x50;
753 *pos++ = 0xf2;
754 *pos++ = 2; /* WME */
755 *pos++ = 0; /* WME info */
756 *pos++ = 1; /* WME ver */
757 *pos++ = 0;
758 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200759 /* wmm support is a must to HT */
Johannes Berg8318d782008-01-24 19:38:38 +0100760 if (wmm && sband->ht_info.ht_supported) {
761 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200762 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
763 *pos++ = WLAN_EID_HT_CAPABILITY;
764 *pos++ = sizeof(struct ieee80211_ht_cap);
765 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
766 memcpy(pos, &tmp, sizeof(u16));
767 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100768 /* TODO: needs a define here for << 2 */
769 *pos++ = sband->ht_info.ampdu_factor |
770 (sband->ht_info.ampdu_density << 2);
771 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200772 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700773
774 kfree(ifsta->assocreq_ies);
775 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200776 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700777 if (ifsta->assocreq_ies)
778 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
779
780 ieee80211_sta_tx(dev, skb, 0);
781}
782
783
784static void ieee80211_send_deauth(struct net_device *dev,
785 struct ieee80211_if_sta *ifsta, u16 reason)
786{
787 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
788 struct sk_buff *skb;
789 struct ieee80211_mgmt *mgmt;
790
791 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
792 if (!skb) {
793 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
794 "frame\n", dev->name);
795 return;
796 }
797 skb_reserve(skb, local->hw.extra_tx_headroom);
798
799 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
800 memset(mgmt, 0, 24);
801 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
802 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
803 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
804 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
805 IEEE80211_STYPE_DEAUTH);
806 skb_put(skb, 2);
807 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
808
809 ieee80211_sta_tx(dev, skb, 0);
810}
811
812
813static void ieee80211_send_disassoc(struct net_device *dev,
814 struct ieee80211_if_sta *ifsta, u16 reason)
815{
816 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
817 struct sk_buff *skb;
818 struct ieee80211_mgmt *mgmt;
819
820 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
821 if (!skb) {
822 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
823 "frame\n", dev->name);
824 return;
825 }
826 skb_reserve(skb, local->hw.extra_tx_headroom);
827
828 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
829 memset(mgmt, 0, 24);
830 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
831 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
832 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
833 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
834 IEEE80211_STYPE_DISASSOC);
835 skb_put(skb, 2);
836 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
837
838 ieee80211_sta_tx(dev, skb, 0);
839}
840
841
842static int ieee80211_privacy_mismatch(struct net_device *dev,
843 struct ieee80211_if_sta *ifsta)
844{
John W. Linville65c107a2007-10-05 14:23:27 -0400845 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700846 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000847 int bss_privacy;
848 int wep_privacy;
849 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700850
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000851 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700852 return 0;
853
Johannes Berg8318d782008-01-24 19:38:38 +0100854 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
855 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400856 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700857 if (!bss)
858 return 0;
859
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000860 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
861 wep_privacy = !!ieee80211_sta_wep_configured(dev);
862 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700863
864 ieee80211_rx_bss_put(dev, bss);
865
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000866 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
867 return 0;
868
869 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700870}
871
872
873static void ieee80211_associate(struct net_device *dev,
874 struct ieee80211_if_sta *ifsta)
875{
Joe Perches0795af52007-10-03 17:59:30 -0700876 DECLARE_MAC_BUF(mac);
877
Jiri Bencf0706e82007-05-05 11:45:53 -0700878 ifsta->assoc_tries++;
879 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700880 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700881 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700882 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700883 ifsta->state = IEEE80211_DISABLED;
884 return;
885 }
886
887 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700888 printk(KERN_DEBUG "%s: associate with AP %s\n",
889 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700890 if (ieee80211_privacy_mismatch(dev, ifsta)) {
891 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
892 "mixed-cell disabled - abort association\n", dev->name);
893 ifsta->state = IEEE80211_DISABLED;
894 return;
895 }
896
897 ieee80211_send_assoc(dev, ifsta);
898
899 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
900}
901
902
903static void ieee80211_associated(struct net_device *dev,
904 struct ieee80211_if_sta *ifsta)
905{
906 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
907 struct sta_info *sta;
908 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700909 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700910
911 /* TODO: start monitoring current AP signal quality and number of
912 * missed beacons. Scan other channels every now and then and search
913 * for better APs. */
914 /* TODO: remove expired BSSes */
915
916 ifsta->state = IEEE80211_ASSOCIATED;
917
Johannes Bergd0709a62008-02-25 16:27:46 +0100918 rcu_read_lock();
919
Jiri Bencf0706e82007-05-05 11:45:53 -0700920 sta = sta_info_get(local, ifsta->bssid);
921 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700922 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
923 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700924 disassoc = 1;
925 } else {
926 disassoc = 0;
927 if (time_after(jiffies,
928 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400929 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700930 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700931 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -0700932 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700933 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700934 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +0100935 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400936 } else
Jiri Bencf0706e82007-05-05 11:45:53 -0700937 ieee80211_send_probe_req(dev, ifsta->bssid,
938 local->scan_ssid,
939 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400940 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700941 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400942 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700943 if (time_after(jiffies, ifsta->last_probe +
944 IEEE80211_PROBE_INTERVAL)) {
945 ifsta->last_probe = jiffies;
946 ieee80211_send_probe_req(dev, ifsta->bssid,
947 ifsta->ssid,
948 ifsta->ssid_len);
949 }
950 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700951 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100952
953 rcu_read_unlock();
954
955 if (disassoc && sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100956 rtnl_lock();
957 sta_info_destroy(sta);
958 rtnl_unlock();
959 }
960
Jiri Bencf0706e82007-05-05 11:45:53 -0700961 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -0500962 ifsta->state = IEEE80211_DISABLED;
963 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700964 } else {
965 mod_timer(&ifsta->timer, jiffies +
966 IEEE80211_MONITORING_INTERVAL);
967 }
968}
969
970
971static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
972 u8 *ssid, size_t ssid_len)
973{
974 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +0100975 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700976 struct sk_buff *skb;
977 struct ieee80211_mgmt *mgmt;
978 u8 *pos, *supp_rates, *esupp_rates = NULL;
979 int i;
980
981 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
982 if (!skb) {
983 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
984 "request\n", dev->name);
985 return;
986 }
987 skb_reserve(skb, local->hw.extra_tx_headroom);
988
989 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
990 memset(mgmt, 0, 24);
991 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
992 IEEE80211_STYPE_PROBE_REQ);
993 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
994 if (dst) {
995 memcpy(mgmt->da, dst, ETH_ALEN);
996 memcpy(mgmt->bssid, dst, ETH_ALEN);
997 } else {
998 memset(mgmt->da, 0xff, ETH_ALEN);
999 memset(mgmt->bssid, 0xff, ETH_ALEN);
1000 }
1001 pos = skb_put(skb, 2 + ssid_len);
1002 *pos++ = WLAN_EID_SSID;
1003 *pos++ = ssid_len;
1004 memcpy(pos, ssid, ssid_len);
1005
1006 supp_rates = skb_put(skb, 2);
1007 supp_rates[0] = WLAN_EID_SUPP_RATES;
1008 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001009 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1010
1011 for (i = 0; i < sband->n_bitrates; i++) {
1012 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07001013 if (esupp_rates) {
1014 pos = skb_put(skb, 1);
1015 esupp_rates[1]++;
1016 } else if (supp_rates[1] == 8) {
1017 esupp_rates = skb_put(skb, 3);
1018 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1019 esupp_rates[1] = 1;
1020 pos = &esupp_rates[2];
1021 } else {
1022 pos = skb_put(skb, 1);
1023 supp_rates[1]++;
1024 }
Johannes Berg8318d782008-01-24 19:38:38 +01001025 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -07001026 }
1027
1028 ieee80211_sta_tx(dev, skb, 0);
1029}
1030
1031
1032static int ieee80211_sta_wep_configured(struct net_device *dev)
1033{
1034 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1035 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001036 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -07001037 return 0;
1038 return 1;
1039}
1040
1041
1042static void ieee80211_auth_completed(struct net_device *dev,
1043 struct ieee80211_if_sta *ifsta)
1044{
1045 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001046 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001047 ieee80211_associate(dev, ifsta);
1048}
1049
1050
1051static void ieee80211_auth_challenge(struct net_device *dev,
1052 struct ieee80211_if_sta *ifsta,
1053 struct ieee80211_mgmt *mgmt,
1054 size_t len)
1055{
1056 u8 *pos;
1057 struct ieee802_11_elems elems;
1058
1059 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1060 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001061 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001062 if (!elems.challenge) {
1063 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1064 "frame\n", dev->name);
1065 return;
1066 }
1067 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1068 elems.challenge_len + 2, 1);
1069}
1070
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001071static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1072 u8 dialog_token, u16 status, u16 policy,
1073 u16 buf_size, u16 timeout)
1074{
1075 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1076 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1077 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1078 struct sk_buff *skb;
1079 struct ieee80211_mgmt *mgmt;
1080 u16 capab;
1081
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001082 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1083 sizeof(mgmt->u.action.u.addba_resp));
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001084 if (!skb) {
1085 printk(KERN_DEBUG "%s: failed to allocate buffer "
1086 "for addba resp frame\n", dev->name);
1087 return;
1088 }
1089
1090 skb_reserve(skb, local->hw.extra_tx_headroom);
1091 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1092 memset(mgmt, 0, 24);
1093 memcpy(mgmt->da, da, ETH_ALEN);
1094 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001095 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001096 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1097 else
1098 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1099 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1100 IEEE80211_STYPE_ACTION);
1101
1102 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1103 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1104 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1105 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1106
1107 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1108 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1109 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1110
1111 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1112 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1113 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1114
1115 ieee80211_sta_tx(dev, skb, 0);
1116
1117 return;
1118}
1119
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001120void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1121 u16 tid, u8 dialog_token, u16 start_seq_num,
1122 u16 agg_size, u16 timeout)
1123{
1124 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1125 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1126 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1127 struct sk_buff *skb;
1128 struct ieee80211_mgmt *mgmt;
1129 u16 capab;
1130
1131 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1132 sizeof(mgmt->u.action.u.addba_req));
1133
1134
1135 if (!skb) {
1136 printk(KERN_ERR "%s: failed to allocate buffer "
1137 "for addba request frame\n", dev->name);
1138 return;
1139 }
1140 skb_reserve(skb, local->hw.extra_tx_headroom);
1141 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1142 memset(mgmt, 0, 24);
1143 memcpy(mgmt->da, da, ETH_ALEN);
1144 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1145 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1146 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1147 else
1148 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1149
1150 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1151 IEEE80211_STYPE_ACTION);
1152
1153 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1154
1155 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1156 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1157
1158 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1159 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1160 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1161 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1162
1163 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1164
1165 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1166 mgmt->u.action.u.addba_req.start_seq_num =
1167 cpu_to_le16(start_seq_num << 4);
1168
1169 ieee80211_sta_tx(dev, skb, 0);
1170}
1171
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001172static void ieee80211_sta_process_addba_request(struct net_device *dev,
1173 struct ieee80211_mgmt *mgmt,
1174 size_t len)
1175{
1176 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001177 struct ieee80211_hw *hw = &local->hw;
1178 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001179 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001180 struct tid_ampdu_rx *tid_agg_rx;
1181 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001182 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001183 int ret = -EOPNOTSUPP;
1184 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001185
Johannes Bergd0709a62008-02-25 16:27:46 +01001186 rcu_read_lock();
1187
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001188 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001189 if (!sta) {
1190 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001191 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001192 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001193
1194 /* extract session parameters from addba request frame */
1195 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1196 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001197 start_seq_num =
1198 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001199
1200 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1201 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1202 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1203 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1204
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001205 status = WLAN_STATUS_REQUEST_DECLINED;
1206
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001207 /* sanity check for incoming parameters:
1208 * check if configuration can support the BA policy
1209 * and if buffer size does not exceeds max value */
1210 if (((ba_policy != 1)
1211 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1212 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1213 status = WLAN_STATUS_INVALID_QOS_PARAM;
1214#ifdef CONFIG_MAC80211_HT_DEBUG
1215 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001216 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001217 "%s on tid %u. policy %d, buffer size %d\n",
1218 print_mac(mac, mgmt->sa), tid, ba_policy,
1219 buf_size);
1220#endif /* CONFIG_MAC80211_HT_DEBUG */
1221 goto end_no_lock;
1222 }
1223 /* determine default buffer size */
1224 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001225 struct ieee80211_supported_band *sband;
1226
1227 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001228 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001229 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001230 }
1231
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001232
1233 /* examine state machine */
1234 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1235
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001236 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001237#ifdef CONFIG_MAC80211_HT_DEBUG
1238 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001239 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001240 "%s on tid %u\n",
1241 print_mac(mac, mgmt->sa), tid);
1242#endif /* CONFIG_MAC80211_HT_DEBUG */
1243 goto end;
1244 }
1245
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001246 /* prepare A-MPDU MLME for Rx aggregation */
1247 sta->ampdu_mlme.tid_rx[tid] =
1248 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1249 if (!sta->ampdu_mlme.tid_rx[tid]) {
1250 if (net_ratelimit())
1251 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1252 tid);
1253 goto end;
1254 }
1255 /* rx timer */
1256 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1257 sta_rx_agg_session_timer_expired;
1258 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1259 (unsigned long)&sta->timer_to_tid[tid];
1260 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1261
1262 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1263
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001264 /* prepare reordering buffer */
1265 tid_agg_rx->reorder_buf =
1266 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001267 if (!tid_agg_rx->reorder_buf) {
1268 if (net_ratelimit())
1269 printk(KERN_ERR "can not allocate reordering buffer "
1270 "to tid %d\n", tid);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001271 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001272 goto end;
1273 }
1274 memset(tid_agg_rx->reorder_buf, 0,
1275 buf_size * sizeof(struct sk_buf *));
1276
1277 if (local->ops->ampdu_action)
1278 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001279 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001280#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001281 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001282#endif /* CONFIG_MAC80211_HT_DEBUG */
1283
1284 if (ret) {
1285 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001286 kfree(tid_agg_rx);
1287 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001288 goto end;
1289 }
1290
1291 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001292 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001293 tid_agg_rx->dialog_token = dialog_token;
1294 tid_agg_rx->ssn = start_seq_num;
1295 tid_agg_rx->head_seq_num = start_seq_num;
1296 tid_agg_rx->buf_size = buf_size;
1297 tid_agg_rx->timeout = timeout;
1298 tid_agg_rx->stored_mpdu_num = 0;
1299 status = WLAN_STATUS_SUCCESS;
1300end:
1301 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1302
1303end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001304 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1305 dialog_token, status, 1, buf_size, timeout);
1306 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001307}
Jiri Bencf0706e82007-05-05 11:45:53 -07001308
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001309static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1310 struct ieee80211_mgmt *mgmt,
1311 size_t len)
1312{
1313 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1314 struct ieee80211_hw *hw = &local->hw;
1315 struct sta_info *sta;
1316 u16 capab;
1317 u16 tid;
1318 u8 *state;
1319
Johannes Bergd0709a62008-02-25 16:27:46 +01001320 rcu_read_lock();
1321
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001322 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001323 if (!sta) {
1324 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001325 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001326 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001327
1328 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1329 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1330
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001331 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001332
1333 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1334
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001335 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1336 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1337 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1338 "%d\n", *state);
1339 goto addba_resp_exit;
1340 }
1341
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001342 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001343 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001344 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1345#ifdef CONFIG_MAC80211_HT_DEBUG
1346 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1347#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001348 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001349 }
1350
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001351 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001352#ifdef CONFIG_MAC80211_HT_DEBUG
1353 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1354#endif /* CONFIG_MAC80211_HT_DEBUG */
1355 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1356 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001357 if (*state & HT_ADDBA_RECEIVED_MSK)
1358 printk(KERN_DEBUG "double addBA response\n");
1359
1360 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001361 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001362
1363 if (*state == HT_AGG_STATE_OPERATIONAL) {
1364 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1365 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1366 }
1367
1368 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1369 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1370 } else {
1371 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1372
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001373 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001374 /* this will allow the state check in stop_BA_session */
1375 *state = HT_AGG_STATE_OPERATIONAL;
1376 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1377 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1378 WLAN_BACK_INITIATOR);
1379 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001380
1381addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001382 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001383}
1384
1385void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1386 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001387{
1388 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1389 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1390 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1391 struct sk_buff *skb;
1392 struct ieee80211_mgmt *mgmt;
1393 u16 params;
1394
1395 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1396 sizeof(mgmt->u.action.u.delba));
1397
1398 if (!skb) {
1399 printk(KERN_ERR "%s: failed to allocate buffer "
1400 "for delba frame\n", dev->name);
1401 return;
1402 }
1403
1404 skb_reserve(skb, local->hw.extra_tx_headroom);
1405 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1406 memset(mgmt, 0, 24);
1407 memcpy(mgmt->da, da, ETH_ALEN);
1408 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001409 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001410 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1411 else
1412 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1413 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1414 IEEE80211_STYPE_ACTION);
1415
1416 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1417
1418 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1419 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1420 params = (u16)(initiator << 11); /* bit 11 initiator */
1421 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1422
1423 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1424 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1425
1426 ieee80211_sta_tx(dev, skb, 0);
1427}
1428
1429void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1430 u16 initiator, u16 reason)
1431{
1432 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1433 struct ieee80211_hw *hw = &local->hw;
1434 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001435 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001436 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001437
Johannes Bergd0709a62008-02-25 16:27:46 +01001438 rcu_read_lock();
1439
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001440 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001441 if (!sta) {
1442 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001443 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001444 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001445
1446 /* check if TID is in operational state */
1447 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001448 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001449 != HT_AGG_STATE_OPERATIONAL) {
1450 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Johannes Bergd0709a62008-02-25 16:27:46 +01001451 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001452 return;
1453 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001454 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001455 HT_AGG_STATE_REQ_STOP_BA_MSK |
1456 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001457 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001458
1459 /* stop HW Rx aggregation. ampdu_action existence
1460 * already verified in session init so we add the BUG_ON */
1461 BUG_ON(!local->ops->ampdu_action);
1462
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001463#ifdef CONFIG_MAC80211_HT_DEBUG
1464 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1465 print_mac(mac, ra), tid);
1466#endif /* CONFIG_MAC80211_HT_DEBUG */
1467
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001468 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001469 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001470 if (ret)
1471 printk(KERN_DEBUG "HW problem - can not stop rx "
1472 "aggergation for tid %d\n", tid);
1473
1474 /* shutdown timer has not expired */
1475 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001476 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001477
1478 /* check if this is a self generated aggregation halt */
1479 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1480 ieee80211_send_delba(dev, ra, tid, 0, reason);
1481
1482 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001483 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1484 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001485 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001486 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1487 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1488 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001489 }
1490 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001491 /* free resources */
1492 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1493 kfree(sta->ampdu_mlme.tid_rx[tid]);
1494 sta->ampdu_mlme.tid_rx[tid] = NULL;
1495 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001496
Johannes Bergd0709a62008-02-25 16:27:46 +01001497 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001498}
1499
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001500
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001501static void ieee80211_sta_process_delba(struct net_device *dev,
1502 struct ieee80211_mgmt *mgmt, size_t len)
1503{
1504 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1505 struct sta_info *sta;
1506 u16 tid, params;
1507 u16 initiator;
1508 DECLARE_MAC_BUF(mac);
1509
Johannes Bergd0709a62008-02-25 16:27:46 +01001510 rcu_read_lock();
1511
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001512 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001513 if (!sta) {
1514 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001515 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001516 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001517
1518 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1519 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1520 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1521
1522#ifdef CONFIG_MAC80211_HT_DEBUG
1523 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001524 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1525 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001526 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001527 mgmt->u.action.u.delba.reason_code);
1528#endif /* CONFIG_MAC80211_HT_DEBUG */
1529
1530 if (initiator == WLAN_BACK_INITIATOR)
1531 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1532 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001533 else { /* WLAN_BACK_RECIPIENT */
1534 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001535 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001536 HT_AGG_STATE_OPERATIONAL;
1537 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1538 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1539 WLAN_BACK_RECIPIENT);
1540 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001541 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001542}
1543
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001544/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001545 * After sending add Block Ack request we activated a timer until
1546 * add Block Ack response will arrive from the recipient.
1547 * If this timer expires sta_addba_resp_timer_expired will be executed.
1548 */
1549void sta_addba_resp_timer_expired(unsigned long data)
1550{
1551 /* not an elegant detour, but there is no choice as the timer passes
1552 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001553 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001554 * array gives the sta through container_of */
1555 u16 tid = *(int *)data;
1556 struct sta_info *temp_sta = container_of((void *)data,
1557 struct sta_info, timer_to_tid[tid]);
1558
1559 struct ieee80211_local *local = temp_sta->local;
1560 struct ieee80211_hw *hw = &local->hw;
1561 struct sta_info *sta;
1562 u8 *state;
1563
Johannes Bergd0709a62008-02-25 16:27:46 +01001564 rcu_read_lock();
1565
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001566 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001567 if (!sta) {
1568 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001569 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001570 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001571
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001572 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001573 /* check if the TID waits for addBA response */
1574 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1575 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1576 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1577 *state = HT_AGG_STATE_IDLE;
1578 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1579 "expecting addBA response there", tid);
1580 goto timer_expired_exit;
1581 }
1582
1583 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1584
1585 /* go through the state check in stop_BA_session */
1586 *state = HT_AGG_STATE_OPERATIONAL;
1587 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1588 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1589 WLAN_BACK_INITIATOR);
1590
1591timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001592 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001593}
1594
1595/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001596 * After accepting the AddBA Request we activated a timer,
1597 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001598 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1599 */
1600void sta_rx_agg_session_timer_expired(unsigned long data)
1601{
1602 /* not an elegant detour, but there is no choice as the timer passes
1603 * only one argument, and verious sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001604 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001605 * array gives the sta through container_of */
1606 u8 *ptid = (u8 *)data;
1607 u8 *timer_to_id = ptid - *ptid;
1608 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1609 timer_to_tid[0]);
1610
1611 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergd0709a62008-02-25 16:27:46 +01001612 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1613 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001614 WLAN_REASON_QSTA_TIMEOUT);
1615}
1616
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001617void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1618{
1619 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1620 int i;
1621
1622 for (i = 0; i < STA_TID_NUM; i++) {
1623 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1624 WLAN_BACK_INITIATOR);
1625 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1626 WLAN_BACK_RECIPIENT,
1627 WLAN_REASON_QSTA_LEAVE_QBSS);
1628 }
1629}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001630
Jiri Bencf0706e82007-05-05 11:45:53 -07001631static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1632 struct ieee80211_if_sta *ifsta,
1633 struct ieee80211_mgmt *mgmt,
1634 size_t len)
1635{
1636 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1637 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001638 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001639
1640 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Berg51fb61e2007-12-19 01:31:27 +01001641 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001642 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001643 "%s, but not in authenticate state - ignored\n",
1644 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001645 return;
1646 }
1647
1648 if (len < 24 + 6) {
1649 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001650 "received from %s - ignored\n",
1651 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001652 return;
1653 }
1654
Johannes Berg51fb61e2007-12-19 01:31:27 +01001655 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001656 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1657 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001658 "unknown AP (SA=%s BSSID=%s) - "
1659 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1660 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001661 return;
1662 }
1663
Johannes Berg51fb61e2007-12-19 01:31:27 +01001664 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001665 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1666 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001667 "unknown BSSID (SA=%s BSSID=%s) - "
1668 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1669 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001670 return;
1671 }
1672
1673 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1674 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1675 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1676
Joe Perches0795af52007-10-03 17:59:30 -07001677 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e82007-05-05 11:45:53 -07001678 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001679 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e82007-05-05 11:45:53 -07001680 auth_transaction, status_code);
1681
Johannes Berg51fb61e2007-12-19 01:31:27 +01001682 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001683 /* IEEE 802.11 standard does not require authentication in IBSS
1684 * networks and most implementations do not seem to use it.
1685 * However, try to reply to authentication attempts if someone
1686 * has actually implemented this.
1687 * TODO: Could implement shared key authentication. */
1688 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1689 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1690 "frame (alg=%d transaction=%d)\n",
1691 dev->name, auth_alg, auth_transaction);
1692 return;
1693 }
1694 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1695 }
1696
1697 if (auth_alg != ifsta->auth_alg ||
1698 auth_transaction != ifsta->auth_transaction) {
1699 printk(KERN_DEBUG "%s: unexpected authentication frame "
1700 "(alg=%d transaction=%d)\n",
1701 dev->name, auth_alg, auth_transaction);
1702 return;
1703 }
1704
1705 if (status_code != WLAN_STATUS_SUCCESS) {
1706 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1707 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1708 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1709 u8 algs[3];
1710 const int num_algs = ARRAY_SIZE(algs);
1711 int i, pos;
1712 algs[0] = algs[1] = algs[2] = 0xff;
1713 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1714 algs[0] = WLAN_AUTH_OPEN;
1715 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1716 algs[1] = WLAN_AUTH_SHARED_KEY;
1717 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1718 algs[2] = WLAN_AUTH_LEAP;
1719 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1720 pos = 0;
1721 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1722 pos = 1;
1723 else
1724 pos = 2;
1725 for (i = 0; i < num_algs; i++) {
1726 pos++;
1727 if (pos >= num_algs)
1728 pos = 0;
1729 if (algs[pos] == ifsta->auth_alg ||
1730 algs[pos] == 0xff)
1731 continue;
1732 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1733 !ieee80211_sta_wep_configured(dev))
1734 continue;
1735 ifsta->auth_alg = algs[pos];
1736 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1737 "next try\n",
1738 dev->name, ifsta->auth_alg);
1739 break;
1740 }
1741 }
1742 return;
1743 }
1744
1745 switch (ifsta->auth_alg) {
1746 case WLAN_AUTH_OPEN:
1747 case WLAN_AUTH_LEAP:
1748 ieee80211_auth_completed(dev, ifsta);
1749 break;
1750 case WLAN_AUTH_SHARED_KEY:
1751 if (ifsta->auth_transaction == 4)
1752 ieee80211_auth_completed(dev, ifsta);
1753 else
1754 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1755 break;
1756 }
1757}
1758
1759
1760static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1761 struct ieee80211_if_sta *ifsta,
1762 struct ieee80211_mgmt *mgmt,
1763 size_t len)
1764{
1765 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001766 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001767
1768 if (len < 24 + 2) {
1769 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001770 "received from %s - ignored\n",
1771 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001772 return;
1773 }
1774
1775 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1776 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001777 "unknown AP (SA=%s BSSID=%s) - "
1778 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1779 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001780 return;
1781 }
1782
1783 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1784
Joe Perches0795af52007-10-03 17:59:30 -07001785 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001786 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001787 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001788
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001789 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001790 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1791 }
1792
1793 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1794 ifsta->state == IEEE80211_ASSOCIATE ||
1795 ifsta->state == IEEE80211_ASSOCIATED) {
1796 ifsta->state = IEEE80211_AUTHENTICATE;
1797 mod_timer(&ifsta->timer, jiffies +
1798 IEEE80211_RETRY_AUTH_INTERVAL);
1799 }
1800
1801 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001802 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001803}
1804
1805
1806static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1807 struct ieee80211_if_sta *ifsta,
1808 struct ieee80211_mgmt *mgmt,
1809 size_t len)
1810{
1811 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001812 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001813
1814 if (len < 24 + 2) {
1815 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001816 "received from %s - ignored\n",
1817 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001818 return;
1819 }
1820
1821 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1822 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001823 "unknown AP (SA=%s BSSID=%s) - "
1824 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1825 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001826 return;
1827 }
1828
1829 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1830
Joe Perches0795af52007-10-03 17:59:30 -07001831 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001832 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001833 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001834
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001835 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001836 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1837
1838 if (ifsta->state == IEEE80211_ASSOCIATED) {
1839 ifsta->state = IEEE80211_ASSOCIATE;
1840 mod_timer(&ifsta->timer, jiffies +
1841 IEEE80211_RETRY_AUTH_INTERVAL);
1842 }
1843
1844 ieee80211_set_disassoc(dev, ifsta, 0);
1845}
1846
1847
Johannes Berg471b3ef2007-12-28 14:32:58 +01001848static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001849 struct ieee80211_if_sta *ifsta,
1850 struct ieee80211_mgmt *mgmt,
1851 size_t len,
1852 int reassoc)
1853{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001854 struct ieee80211_local *local = sdata->local;
1855 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01001856 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001857 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001858 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001859 u16 capab_info, status_code, aid;
1860 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001861 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001862 u8 *pos;
1863 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001864 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001865 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001866
1867 /* AssocResp and ReassocResp have identical structure, so process both
1868 * of them in this function. */
1869
1870 if (ifsta->state != IEEE80211_ASSOCIATE) {
1871 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001872 "%s, but not in associate state - ignored\n",
1873 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001874 return;
1875 }
1876
1877 if (len < 24 + 6) {
1878 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001879 "received from %s - ignored\n",
1880 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001881 return;
1882 }
1883
1884 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1885 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001886 "unknown AP (SA=%s BSSID=%s) - "
1887 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1888 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001889 return;
1890 }
1891
1892 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1893 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1894 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001895
Joe Perches0795af52007-10-03 17:59:30 -07001896 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001897 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001898 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001899 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001900
1901 if (status_code != WLAN_STATUS_SUCCESS) {
1902 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1903 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001904 /* if this was a reassociation, ensure we try a "full"
1905 * association next time. This works around some broken APs
1906 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001907 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07001908 return;
1909 }
1910
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001911 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1912 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1913 "set\n", dev->name, aid);
1914 aid &= ~(BIT(15) | BIT(14));
1915
Jiri Bencf0706e82007-05-05 11:45:53 -07001916 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001917 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001918
1919 if (!elems.supp_rates) {
1920 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1921 dev->name);
1922 return;
1923 }
1924
1925 printk(KERN_DEBUG "%s: associated\n", dev->name);
1926 ifsta->aid = aid;
1927 ifsta->ap_capab = capab_info;
1928
1929 kfree(ifsta->assocresp_ies);
1930 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001931 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001932 if (ifsta->assocresp_ies)
1933 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1934
Johannes Bergd0709a62008-02-25 16:27:46 +01001935 rcu_read_lock();
1936
Jiri Bencf0706e82007-05-05 11:45:53 -07001937 /* Add STA entry for the AP */
1938 sta = sta_info_get(local, ifsta->bssid);
1939 if (!sta) {
1940 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01001941 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01001942
Johannes Berg73651ee2008-02-25 16:27:47 +01001943 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1944 if (!sta) {
1945 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1946 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01001947 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001948 return;
1949 }
John W. Linville65c107a2007-10-05 14:23:27 -04001950 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001951 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001952 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001953 if (bss) {
1954 sta->last_rssi = bss->rssi;
1955 sta->last_signal = bss->signal;
1956 sta->last_noise = bss->noise;
1957 ieee80211_rx_bss_put(dev, bss);
1958 }
Johannes Berg73651ee2008-02-25 16:27:47 +01001959
1960 err = sta_info_insert(sta);
1961 if (err) {
1962 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1963 " the AP (error %d)\n", dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01001964 rcu_read_unlock();
1965 return;
1966 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001967 }
1968
Johannes Berg73651ee2008-02-25 16:27:47 +01001969 /*
1970 * FIXME: Do we really need to update the sta_info's information here?
1971 * We already know about the AP (we found it in our list) so it
1972 * should already be filled with the right info, no?
1973 * As is stands, all this is racy because typically we assume
1974 * the information that is filled in here (except flags) doesn't
1975 * change while a STA structure is alive. As such, it should move
1976 * to between the sta_info_alloc() and sta_info_insert() above.
1977 */
1978
Johannes Berg238814f2008-01-28 17:19:37 +01001979 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1980 WLAN_STA_AUTHORIZED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001981
1982 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001983 basic_rates = 0;
1984 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1985
Jiri Bencf0706e82007-05-05 11:45:53 -07001986 for (i = 0; i < elems.supp_rates_len; i++) {
1987 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001988
1989 if (rate > 110)
1990 have_higher_than_11mbit = true;
1991
1992 for (j = 0; j < sband->n_bitrates; j++) {
1993 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001994 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001995 if (elems.supp_rates[i] & 0x80)
1996 basic_rates |= BIT(j);
1997 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001998 }
Johannes Berg8318d782008-01-24 19:38:38 +01001999
Jiri Bencf0706e82007-05-05 11:45:53 -07002000 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2001 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002002
2003 if (rate > 110)
2004 have_higher_than_11mbit = true;
2005
2006 for (j = 0; j < sband->n_bitrates; j++) {
2007 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002008 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002009 if (elems.ext_supp_rates[i] & 0x80)
2010 basic_rates |= BIT(j);
2011 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002012 }
Johannes Berg8318d782008-01-24 19:38:38 +01002013
2014 sta->supp_rates[local->hw.conf.channel->band] = rates;
2015 sdata->basic_rates = basic_rates;
2016
2017 /* cf. IEEE 802.11 9.2.12 */
2018 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2019 have_higher_than_11mbit)
2020 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2021 else
2022 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002023
Tomas Winkler38668c02008-03-28 16:33:32 -07002024 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002025 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002026 ieee80211_ht_cap_ie_to_ht_info(
2027 (struct ieee80211_ht_cap *)
2028 elems.ht_cap_elem, &sta->ht_info);
2029 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2030 (struct ieee80211_ht_addt_info *)
2031 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002032 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002033 }
2034
Jiri Bencf0706e82007-05-05 11:45:53 -07002035 rate_control_rate_init(sta, local);
2036
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002037 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002038 sta->flags |= WLAN_STA_WME;
Johannes Berge5f98f22008-03-05 20:39:31 +01002039 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002040 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2041 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002042 } else
2043 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002044
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002045 /* set AID and assoc capability,
2046 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002047 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002048 bss_conf->assoc_capability = capab_info;
Johannes Berg8318d782008-01-24 19:38:38 +01002049 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002050
Jiri Bencf0706e82007-05-05 11:45:53 -07002051 ieee80211_associated(dev, ifsta);
2052}
2053
2054
2055/* Caller must hold local->sta_bss_lock */
2056static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2057 struct ieee80211_sta_bss *bss)
2058{
2059 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002060 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002061
2062 if (bss_mesh_cfg(bss))
2063 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2064 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002065 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002066 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002067
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002068 bss->hnext = local->sta_bss_hash[hash_idx];
2069 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002070}
2071
2072
2073/* Caller must hold local->sta_bss_lock */
2074static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2075 struct ieee80211_sta_bss *bss)
2076{
2077 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2078 struct ieee80211_sta_bss *b, *prev = NULL;
2079 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2080 while (b) {
2081 if (b == bss) {
2082 if (!prev)
2083 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2084 bss->hnext;
2085 else
2086 prev->hnext = bss->hnext;
2087 break;
2088 }
2089 prev = b;
2090 b = b->hnext;
2091 }
2092}
2093
2094
2095static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002096ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002097 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002098{
2099 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2100 struct ieee80211_sta_bss *bss;
2101
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002102 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002103 if (!bss)
2104 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002105 atomic_inc(&bss->users);
2106 atomic_inc(&bss->users);
2107 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002108 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002109 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2110 memcpy(bss->ssid, ssid, ssid_len);
2111 bss->ssid_len = ssid_len;
2112 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002113
2114 spin_lock_bh(&local->sta_bss_lock);
2115 /* TODO: order by RSSI? */
2116 list_add_tail(&bss->list, &local->sta_bss_list);
2117 __ieee80211_rx_bss_hash_add(dev, bss);
2118 spin_unlock_bh(&local->sta_bss_lock);
2119 return bss;
2120}
2121
Jiri Bencf0706e82007-05-05 11:45:53 -07002122static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002123ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002124 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002125{
2126 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2127 struct ieee80211_sta_bss *bss;
2128
2129 spin_lock_bh(&local->sta_bss_lock);
2130 bss = local->sta_bss_hash[STA_HASH(bssid)];
2131 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002132 if (!bss_mesh_cfg(bss) &&
2133 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002134 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002135 bss->ssid_len == ssid_len &&
2136 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002137 atomic_inc(&bss->users);
2138 break;
2139 }
2140 bss = bss->hnext;
2141 }
2142 spin_unlock_bh(&local->sta_bss_lock);
2143 return bss;
2144}
2145
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002146#ifdef CONFIG_MAC80211_MESH
2147static struct ieee80211_sta_bss *
2148ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2149 u8 *mesh_cfg, int freq)
2150{
2151 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2152 struct ieee80211_sta_bss *bss;
2153
2154 spin_lock_bh(&local->sta_bss_lock);
2155 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2156 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002157 if (bss_mesh_cfg(bss) &&
2158 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002159 bss->freq == freq &&
2160 mesh_id_len == bss->mesh_id_len &&
2161 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2162 mesh_id_len))) {
2163 atomic_inc(&bss->users);
2164 break;
2165 }
2166 bss = bss->hnext;
2167 }
2168 spin_unlock_bh(&local->sta_bss_lock);
2169 return bss;
2170}
2171
2172static struct ieee80211_sta_bss *
2173ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002174 u8 *mesh_cfg, int mesh_config_len, int freq)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002175{
2176 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2177 struct ieee80211_sta_bss *bss;
2178
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002179 if (mesh_config_len != MESH_CFG_LEN)
2180 return NULL;
2181
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002182 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2183 if (!bss)
2184 return NULL;
2185
John W. Linville24736702008-04-08 14:15:46 -04002186 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002187 if (!bss->mesh_cfg) {
2188 kfree(bss);
2189 return NULL;
2190 }
2191
2192 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2193 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2194 if (!bss->mesh_id) {
2195 kfree(bss->mesh_cfg);
2196 kfree(bss);
2197 return NULL;
2198 }
2199 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2200 }
2201
2202 atomic_inc(&bss->users);
2203 atomic_inc(&bss->users);
John W. Linville24736702008-04-08 14:15:46 -04002204 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002205 bss->mesh_id_len = mesh_id_len;
2206 bss->freq = freq;
2207 spin_lock_bh(&local->sta_bss_lock);
2208 /* TODO: order by RSSI? */
2209 list_add_tail(&bss->list, &local->sta_bss_list);
2210 __ieee80211_rx_bss_hash_add(dev, bss);
2211 spin_unlock_bh(&local->sta_bss_lock);
2212 return bss;
2213}
2214#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002215
2216static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2217{
2218 kfree(bss->wpa_ie);
2219 kfree(bss->rsn_ie);
2220 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002221 kfree(bss->ht_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002222 kfree(bss_mesh_id(bss));
2223 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002224 kfree(bss);
2225}
2226
2227
2228static void ieee80211_rx_bss_put(struct net_device *dev,
2229 struct ieee80211_sta_bss *bss)
2230{
2231 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2232 if (!atomic_dec_and_test(&bss->users))
2233 return;
2234
2235 spin_lock_bh(&local->sta_bss_lock);
2236 __ieee80211_rx_bss_hash_del(dev, bss);
2237 list_del(&bss->list);
2238 spin_unlock_bh(&local->sta_bss_lock);
2239 ieee80211_rx_bss_free(bss);
2240}
2241
2242
2243void ieee80211_rx_bss_list_init(struct net_device *dev)
2244{
2245 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2246 spin_lock_init(&local->sta_bss_lock);
2247 INIT_LIST_HEAD(&local->sta_bss_list);
2248}
2249
2250
2251void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2252{
2253 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2254 struct ieee80211_sta_bss *bss, *tmp;
2255
2256 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2257 ieee80211_rx_bss_put(dev, bss);
2258}
2259
2260
Bruno Randolfa6072682008-02-18 11:21:15 +09002261static int ieee80211_sta_join_ibss(struct net_device *dev,
2262 struct ieee80211_if_sta *ifsta,
2263 struct ieee80211_sta_bss *bss)
2264{
2265 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2266 int res, rates, i, j;
2267 struct sk_buff *skb;
2268 struct ieee80211_mgmt *mgmt;
2269 struct ieee80211_tx_control control;
2270 struct rate_selection ratesel;
2271 u8 *pos;
2272 struct ieee80211_sub_if_data *sdata;
2273 struct ieee80211_supported_band *sband;
2274
2275 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2276
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002277 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2278
Bruno Randolfa6072682008-02-18 11:21:15 +09002279 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002280 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002281
2282 if (local->ops->reset_tsf) {
2283 /* Reset own TSF to allow time synchronization work. */
2284 local->ops->reset_tsf(local_to_hw(local));
2285 }
2286 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2287 res = ieee80211_if_config(dev);
2288 if (res)
2289 return res;
2290
2291 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2292
Bruno Randolfa6072682008-02-18 11:21:15 +09002293 sdata->drop_unencrypted = bss->capability &
2294 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2295
2296 res = ieee80211_set_freq(local, bss->freq);
2297
2298 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2299 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2300 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2301 return -1;
2302 }
2303
2304 /* Set beacon template */
2305 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2306 do {
2307 if (!skb)
2308 break;
2309
2310 skb_reserve(skb, local->hw.extra_tx_headroom);
2311
2312 mgmt = (struct ieee80211_mgmt *)
2313 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2314 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2315 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2316 IEEE80211_STYPE_BEACON);
2317 memset(mgmt->da, 0xff, ETH_ALEN);
2318 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2319 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2320 mgmt->u.beacon.beacon_int =
2321 cpu_to_le16(local->hw.conf.beacon_int);
2322 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2323
2324 pos = skb_put(skb, 2 + ifsta->ssid_len);
2325 *pos++ = WLAN_EID_SSID;
2326 *pos++ = ifsta->ssid_len;
2327 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2328
2329 rates = bss->supp_rates_len;
2330 if (rates > 8)
2331 rates = 8;
2332 pos = skb_put(skb, 2 + rates);
2333 *pos++ = WLAN_EID_SUPP_RATES;
2334 *pos++ = rates;
2335 memcpy(pos, bss->supp_rates, rates);
2336
2337 if (bss->band == IEEE80211_BAND_2GHZ) {
2338 pos = skb_put(skb, 2 + 1);
2339 *pos++ = WLAN_EID_DS_PARAMS;
2340 *pos++ = 1;
2341 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2342 }
2343
2344 pos = skb_put(skb, 2 + 2);
2345 *pos++ = WLAN_EID_IBSS_PARAMS;
2346 *pos++ = 2;
2347 /* FIX: set ATIM window based on scan results */
2348 *pos++ = 0;
2349 *pos++ = 0;
2350
2351 if (bss->supp_rates_len > 8) {
2352 rates = bss->supp_rates_len - 8;
2353 pos = skb_put(skb, 2 + rates);
2354 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2355 *pos++ = rates;
2356 memcpy(pos, &bss->supp_rates[8], rates);
2357 }
2358
2359 memset(&control, 0, sizeof(control));
2360 rate_control_get_rate(dev, sband, skb, &ratesel);
2361 if (!ratesel.rate) {
2362 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2363 "for IBSS beacon\n", dev->name);
2364 break;
2365 }
2366 control.vif = &sdata->vif;
2367 control.tx_rate = ratesel.rate;
2368 if (sdata->bss_conf.use_short_preamble &&
2369 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2370 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2371 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2372 control.flags |= IEEE80211_TXCTL_NO_ACK;
2373 control.retry_limit = 1;
2374
2375 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2376 if (ifsta->probe_resp) {
2377 mgmt = (struct ieee80211_mgmt *)
2378 ifsta->probe_resp->data;
2379 mgmt->frame_control =
2380 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2381 IEEE80211_STYPE_PROBE_RESP);
2382 } else {
2383 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2384 "template for IBSS\n", dev->name);
2385 }
2386
2387 if (local->ops->beacon_update &&
2388 local->ops->beacon_update(local_to_hw(local),
2389 skb, &control) == 0) {
2390 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2391 "template\n", dev->name);
2392 skb = NULL;
2393 }
2394
2395 rates = 0;
2396 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2397 for (i = 0; i < bss->supp_rates_len; i++) {
2398 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2399 for (j = 0; j < sband->n_bitrates; j++)
2400 if (sband->bitrates[j].bitrate == bitrate)
2401 rates |= BIT(j);
2402 }
2403 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Vladimir Koutnye2839d82008-03-18 21:14:07 +01002404
2405 ieee80211_sta_def_wmm_params(dev, bss, 1);
Bruno Randolfa6072682008-02-18 11:21:15 +09002406 } while (0);
2407
2408 if (skb) {
2409 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2410 "template\n", dev->name);
2411 dev_kfree_skb(skb);
2412 }
2413
2414 ifsta->state = IEEE80211_IBSS_JOINED;
2415 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2416
2417 ieee80211_rx_bss_put(dev, bss);
2418
2419 return res;
2420}
2421
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002422u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2423 struct ieee802_11_elems *elems,
2424 enum ieee80211_band band)
2425{
2426 struct ieee80211_supported_band *sband;
2427 struct ieee80211_rate *bitrates;
2428 size_t num_rates;
2429 u64 supp_rates;
2430 int i, j;
2431 sband = local->hw.wiphy->bands[band];
2432
2433 if (!sband) {
2434 WARN_ON(1);
2435 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2436 }
2437
2438 bitrates = sband->bitrates;
2439 num_rates = sband->n_bitrates;
2440 supp_rates = 0;
2441 for (i = 0; i < elems->supp_rates_len +
2442 elems->ext_supp_rates_len; i++) {
2443 u8 rate = 0;
2444 int own_rate;
2445 if (i < elems->supp_rates_len)
2446 rate = elems->supp_rates[i];
2447 else if (elems->ext_supp_rates)
2448 rate = elems->ext_supp_rates
2449 [i - elems->supp_rates_len];
2450 own_rate = 5 * (rate & 0x7f);
2451 for (j = 0; j < num_rates; j++)
2452 if (bitrates[j].bitrate == own_rate)
2453 supp_rates |= BIT(j);
2454 }
2455 return supp_rates;
2456}
2457
Bruno Randolfa6072682008-02-18 11:21:15 +09002458
Jiri Bencf0706e82007-05-05 11:45:53 -07002459static void ieee80211_rx_bss_info(struct net_device *dev,
2460 struct ieee80211_mgmt *mgmt,
2461 size_t len,
2462 struct ieee80211_rx_status *rx_status,
2463 int beacon)
2464{
2465 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2466 struct ieee802_11_elems elems;
2467 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002468 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002469 struct ieee80211_sta_bss *bss;
2470 struct sta_info *sta;
2471 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002472 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002473 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002474 DECLARE_MAC_BUF(mac);
2475 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002476
2477 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2478 return; /* ignore ProbeResp to foreign address */
2479
2480#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002481 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07002482 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002483 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002484#endif
2485
2486 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2487 if (baselen > len)
2488 return;
2489
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002490 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
John W. Linville67a4cce2007-10-12 16:40:37 -04002491 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002492
Johannes Berg902acc72008-02-23 15:17:19 +01002493 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2494 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2495 u64 rates = ieee80211_sta_get_rates(local, &elems,
2496 rx_status->band);
2497
2498 mesh_neighbour_update(mgmt->sa, rates, dev,
2499 mesh_peer_accepts_plinks(&elems, dev));
2500 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002501
Johannes Bergd0709a62008-02-25 16:27:46 +01002502 rcu_read_lock();
2503
Johannes Berg51fb61e2007-12-19 01:31:27 +01002504 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002505 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2506 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002507 u64 prev_rates;
2508 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2509 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002510
Johannes Berg8318d782008-01-24 19:38:38 +01002511 prev_rates = sta->supp_rates[rx_status->band];
2512 sta->supp_rates[rx_status->band] &= supp_rates;
2513 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002514 /* No matching rates - this should not really happen.
2515 * Make sure that at least one rate is marked
2516 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002517 sta->supp_rates[rx_status->band] =
2518 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002519 }
Johannes Berg8318d782008-01-24 19:38:38 +01002520 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002521 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002522 "%s based on beacon info (0x%llx & 0x%llx -> "
2523 "0x%llx)\n",
2524 dev->name, print_mac(mac, sta->addr),
2525 (unsigned long long) prev_rates,
2526 (unsigned long long) supp_rates,
2527 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002528 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002529 }
2530
Johannes Bergd0709a62008-02-25 16:27:46 +01002531 rcu_read_unlock();
2532
Jiri Bencf0706e82007-05-05 11:45:53 -07002533 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002534 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002535 else
Johannes Berg8318d782008-01-24 19:38:38 +01002536 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002537
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002538 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2539
2540 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2541 return;
2542
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002543#ifdef CONFIG_MAC80211_MESH
2544 if (elems.mesh_config)
2545 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2546 elems.mesh_id_len, elems.mesh_config, freq);
2547 else
2548#endif
2549 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002550 elems.ssid, elems.ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002551 if (!bss) {
2552#ifdef CONFIG_MAC80211_MESH
2553 if (elems.mesh_config)
2554 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002555 elems.mesh_id_len, elems.mesh_config,
2556 elems.mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002557 else
2558#endif
2559 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2560 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002561 if (!bss)
2562 return;
2563 } else {
2564#if 0
2565 /* TODO: order by RSSI? */
2566 spin_lock_bh(&local->sta_bss_lock);
2567 list_move_tail(&bss->list, &local->sta_bss_list);
2568 spin_unlock_bh(&local->sta_bss_lock);
2569#endif
2570 }
2571
Johannes Berg8318d782008-01-24 19:38:38 +01002572 bss->band = rx_status->band;
2573
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002574 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2575 bss->probe_resp && beacon) {
2576 /* STA mode:
2577 * Do not allow beacon to override data from Probe Response. */
Jiri Bencf0706e82007-05-05 11:45:53 -07002578 ieee80211_rx_bss_put(dev, bss);
2579 return;
2580 }
2581
Daniel Drake56282212007-07-10 19:32:10 +02002582 /* save the ERP value so that it is available at association time */
2583 if (elems.erp_info && elems.erp_info_len >= 1) {
2584 bss->erp_value = elems.erp_info[0];
2585 bss->has_erp_value = 1;
2586 }
2587
Jiri Bencf0706e82007-05-05 11:45:53 -07002588 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2589 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002590
2591 bss->supp_rates_len = 0;
2592 if (elems.supp_rates) {
2593 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2594 if (clen > elems.supp_rates_len)
2595 clen = elems.supp_rates_len;
2596 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2597 clen);
2598 bss->supp_rates_len += clen;
2599 }
2600 if (elems.ext_supp_rates) {
2601 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2602 if (clen > elems.ext_supp_rates_len)
2603 clen = elems.ext_supp_rates_len;
2604 memcpy(&bss->supp_rates[bss->supp_rates_len],
2605 elems.ext_supp_rates, clen);
2606 bss->supp_rates_len += clen;
2607 }
2608
2609 if (elems.wpa &&
2610 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2611 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2612 kfree(bss->wpa_ie);
2613 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2614 if (bss->wpa_ie) {
2615 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2616 bss->wpa_ie_len = elems.wpa_len + 2;
2617 } else
2618 bss->wpa_ie_len = 0;
2619 } else if (!elems.wpa && bss->wpa_ie) {
2620 kfree(bss->wpa_ie);
2621 bss->wpa_ie = NULL;
2622 bss->wpa_ie_len = 0;
2623 }
2624
2625 if (elems.rsn &&
2626 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2627 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2628 kfree(bss->rsn_ie);
2629 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2630 if (bss->rsn_ie) {
2631 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2632 bss->rsn_ie_len = elems.rsn_len + 2;
2633 } else
2634 bss->rsn_ie_len = 0;
2635 } else if (!elems.rsn && bss->rsn_ie) {
2636 kfree(bss->rsn_ie);
2637 bss->rsn_ie = NULL;
2638 bss->rsn_ie_len = 0;
2639 }
2640
2641 if (elems.wmm_param &&
2642 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2643 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2644 kfree(bss->wmm_ie);
2645 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2646 if (bss->wmm_ie) {
2647 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2648 elems.wmm_param_len + 2);
2649 bss->wmm_ie_len = elems.wmm_param_len + 2;
2650 } else
2651 bss->wmm_ie_len = 0;
2652 } else if (!elems.wmm_param && bss->wmm_ie) {
2653 kfree(bss->wmm_ie);
2654 bss->wmm_ie = NULL;
2655 bss->wmm_ie_len = 0;
2656 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002657 if (elems.ht_cap_elem &&
2658 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2659 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2660 kfree(bss->ht_ie);
2661 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2662 if (bss->ht_ie) {
2663 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2664 elems.ht_cap_elem_len + 2);
2665 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2666 } else
2667 bss->ht_ie_len = 0;
2668 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2669 kfree(bss->ht_ie);
2670 bss->ht_ie = NULL;
2671 bss->ht_ie_len = 0;
2672 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002673
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002674 bss->timestamp = beacon_timestamp;
Jiri Bencf0706e82007-05-05 11:45:53 -07002675 bss->last_update = jiffies;
2676 bss->rssi = rx_status->ssi;
2677 bss->signal = rx_status->signal;
2678 bss->noise = rx_status->noise;
2679 if (!beacon)
2680 bss->probe_resp++;
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002681
2682 /* check if we need to merge IBSS */
2683 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2684 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002685 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002686 bss->freq == local->oper_channel->center_freq &&
2687 elems.ssid_len == sdata->u.sta.ssid_len &&
2688 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2689 if (rx_status->flag & RX_FLAG_TSFT) {
2690 /* in order for correct IBSS merging we need mactime
2691 *
2692 * since mactime is defined as the time the first data
2693 * symbol of the frame hits the PHY, and the timestamp
2694 * of the beacon is defined as "the time that the data
2695 * symbol containing the first bit of the timestamp is
2696 * transmitted to the PHY plus the transmitting STA’s
2697 * delays through its local PHY from the MAC-PHY
2698 * interface to its interface with the WM"
2699 * (802.11 11.1.2) - equals the time this bit arrives at
2700 * the receiver - we have to take into account the
2701 * offset between the two.
2702 * e.g: at 1 MBit that means mactime is 192 usec earlier
2703 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2704 */
2705 int rate = local->hw.wiphy->bands[rx_status->band]->
2706 bitrates[rx_status->rate_idx].bitrate;
2707 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2708 } else if (local && local->ops && local->ops->get_tsf)
2709 /* second best option: get current TSF */
2710 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2711 else
2712 /* can't merge without knowing the TSF */
2713 rx_timestamp = -1LLU;
2714#ifdef CONFIG_MAC80211_IBSS_DEBUG
2715 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2716 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2717 print_mac(mac, mgmt->sa),
2718 print_mac(mac2, mgmt->bssid),
2719 (unsigned long long)rx_timestamp,
2720 (unsigned long long)beacon_timestamp,
2721 (unsigned long long)(rx_timestamp - beacon_timestamp),
2722 jiffies);
2723#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2724 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002725#ifndef CONFIG_MAC80211_IBSS_DEBUG
Pavel Roskind97cf012008-02-21 11:33:58 -05002726 if (net_ratelimit())
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002727#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002728 printk(KERN_DEBUG "%s: beacon TSF higher than "
2729 "local TSF - IBSS merge with BSSID %s\n",
2730 dev->name, print_mac(mac, mgmt->bssid));
2731 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2732 ieee80211_ibss_add_sta(dev, NULL,
2733 mgmt->bssid, mgmt->sa);
2734 }
2735 }
2736
Jiri Bencf0706e82007-05-05 11:45:53 -07002737 ieee80211_rx_bss_put(dev, bss);
2738}
2739
2740
2741static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2742 struct ieee80211_mgmt *mgmt,
2743 size_t len,
2744 struct ieee80211_rx_status *rx_status)
2745{
2746 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2747}
2748
2749
2750static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2751 struct ieee80211_mgmt *mgmt,
2752 size_t len,
2753 struct ieee80211_rx_status *rx_status)
2754{
Jiri Bencf0706e82007-05-05 11:45:53 -07002755 struct ieee80211_sub_if_data *sdata;
2756 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002757 size_t baselen;
2758 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002759 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2760 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002761 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002762
2763 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2764
2765 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002766 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002767 return;
2768 ifsta = &sdata->u.sta;
2769
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002770 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002771 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2772 return;
2773
2774 /* Process beacon from the current BSS */
2775 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2776 if (baselen > len)
2777 return;
2778
John W. Linville67a4cce2007-10-12 16:40:37 -04002779 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002780
Daniel Drake56282212007-07-10 19:32:10 +02002781 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002782 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002783
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002784 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07002785 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002786 struct ieee80211_ht_bss_info bss_info;
2787
2788 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2789 (struct ieee80211_ht_addt_info *)
2790 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002791 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2792 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002793 }
2794
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002795 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002796 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2797 elems.wmm_param_len);
2798 }
Johannes Berg471b3ef2007-12-28 14:32:58 +01002799
2800 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002801}
2802
2803
2804static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2805 struct ieee80211_if_sta *ifsta,
2806 struct ieee80211_mgmt *mgmt,
2807 size_t len,
2808 struct ieee80211_rx_status *rx_status)
2809{
2810 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2811 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2812 int tx_last_beacon;
2813 struct sk_buff *skb;
2814 struct ieee80211_mgmt *resp;
2815 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002816 DECLARE_MAC_BUF(mac);
2817#ifdef CONFIG_MAC80211_IBSS_DEBUG
2818 DECLARE_MAC_BUF(mac2);
2819 DECLARE_MAC_BUF(mac3);
2820#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002821
Johannes Berg51fb61e2007-12-19 01:31:27 +01002822 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002823 ifsta->state != IEEE80211_IBSS_JOINED ||
2824 len < 24 + 2 || !ifsta->probe_resp)
2825 return;
2826
2827 if (local->ops->tx_last_beacon)
2828 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2829 else
2830 tx_last_beacon = 1;
2831
2832#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002833 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2834 "%s (tx_last_beacon=%d)\n",
2835 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2836 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002837#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2838
2839 if (!tx_last_beacon)
2840 return;
2841
2842 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2843 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2844 return;
2845
2846 end = ((u8 *) mgmt) + len;
2847 pos = mgmt->u.probe_req.variable;
2848 if (pos[0] != WLAN_EID_SSID ||
2849 pos + 2 + pos[1] > end) {
2850 if (net_ratelimit()) {
2851 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002852 "from %s\n",
2853 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002854 }
2855 return;
2856 }
2857 if (pos[1] != 0 &&
2858 (pos[1] != ifsta->ssid_len ||
2859 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2860 /* Ignore ProbeReq for foreign SSID */
2861 return;
2862 }
2863
2864 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002865 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002866 if (!skb)
2867 return;
2868
2869 resp = (struct ieee80211_mgmt *) skb->data;
2870 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2871#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002872 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2873 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002874#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2875 ieee80211_sta_tx(dev, skb, 0);
2876}
2877
Johannes Berg4e20cb22007-12-19 01:31:24 +01002878static void ieee80211_rx_mgmt_action(struct net_device *dev,
2879 struct ieee80211_if_sta *ifsta,
2880 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002881 size_t len,
2882 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002883{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002884 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002885
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002886 if (len < IEEE80211_MIN_ACTION_SIZE)
2887 return;
2888
2889 switch (mgmt->u.action.category) {
2890 case WLAN_CATEGORY_BACK:
2891 switch (mgmt->u.action.u.addba_req.action_code) {
2892 case WLAN_ACTION_ADDBA_REQ:
2893 if (len < (IEEE80211_MIN_ACTION_SIZE +
2894 sizeof(mgmt->u.action.u.addba_req)))
2895 break;
2896 ieee80211_sta_process_addba_request(dev, mgmt, len);
2897 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002898 case WLAN_ACTION_ADDBA_RESP:
2899 if (len < (IEEE80211_MIN_ACTION_SIZE +
2900 sizeof(mgmt->u.action.u.addba_resp)))
2901 break;
2902 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2903 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002904 case WLAN_ACTION_DELBA:
2905 if (len < (IEEE80211_MIN_ACTION_SIZE +
2906 sizeof(mgmt->u.action.u.delba)))
2907 break;
2908 ieee80211_sta_process_delba(dev, mgmt, len);
2909 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002910 default:
2911 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002912 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002913 dev->name);
2914 break;
2915 }
2916 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002917 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002918 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002919 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
2920 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002921 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002922 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002923 mesh_rx_path_sel_frame(dev, mgmt, len);
2924 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002925 default:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002926 if (net_ratelimit())
2927 printk(KERN_DEBUG "%s: Rx unknown action frame - "
2928 "category=%d\n", dev->name, mgmt->u.action.category);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002929 break;
2930 }
2931}
Jiri Bencf0706e82007-05-05 11:45:53 -07002932
2933void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2934 struct ieee80211_rx_status *rx_status)
2935{
2936 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2937 struct ieee80211_sub_if_data *sdata;
2938 struct ieee80211_if_sta *ifsta;
2939 struct ieee80211_mgmt *mgmt;
2940 u16 fc;
2941
2942 if (skb->len < 24)
2943 goto fail;
2944
2945 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2946 ifsta = &sdata->u.sta;
2947
2948 mgmt = (struct ieee80211_mgmt *) skb->data;
2949 fc = le16_to_cpu(mgmt->frame_control);
2950
2951 switch (fc & IEEE80211_FCTL_STYPE) {
2952 case IEEE80211_STYPE_PROBE_REQ:
2953 case IEEE80211_STYPE_PROBE_RESP:
2954 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002955 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07002956 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2957 case IEEE80211_STYPE_AUTH:
2958 case IEEE80211_STYPE_ASSOC_RESP:
2959 case IEEE80211_STYPE_REASSOC_RESP:
2960 case IEEE80211_STYPE_DEAUTH:
2961 case IEEE80211_STYPE_DISASSOC:
2962 skb_queue_tail(&ifsta->skb_queue, skb);
2963 queue_work(local->hw.workqueue, &ifsta->work);
2964 return;
2965 default:
2966 printk(KERN_DEBUG "%s: received unknown management frame - "
2967 "stype=%d\n", dev->name,
2968 (fc & IEEE80211_FCTL_STYPE) >> 4);
2969 break;
2970 }
2971
2972 fail:
2973 kfree_skb(skb);
2974}
2975
2976
2977static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2978 struct sk_buff *skb)
2979{
2980 struct ieee80211_rx_status *rx_status;
2981 struct ieee80211_sub_if_data *sdata;
2982 struct ieee80211_if_sta *ifsta;
2983 struct ieee80211_mgmt *mgmt;
2984 u16 fc;
2985
2986 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2987 ifsta = &sdata->u.sta;
2988
2989 rx_status = (struct ieee80211_rx_status *) skb->cb;
2990 mgmt = (struct ieee80211_mgmt *) skb->data;
2991 fc = le16_to_cpu(mgmt->frame_control);
2992
2993 switch (fc & IEEE80211_FCTL_STYPE) {
2994 case IEEE80211_STYPE_PROBE_REQ:
2995 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2996 rx_status);
2997 break;
2998 case IEEE80211_STYPE_PROBE_RESP:
2999 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3000 break;
3001 case IEEE80211_STYPE_BEACON:
3002 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3003 break;
3004 case IEEE80211_STYPE_AUTH:
3005 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3006 break;
3007 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003008 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003009 break;
3010 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003011 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003012 break;
3013 case IEEE80211_STYPE_DEAUTH:
3014 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3015 break;
3016 case IEEE80211_STYPE_DISASSOC:
3017 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3018 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003019 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003020 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003021 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07003022 }
3023
3024 kfree_skb(skb);
3025}
3026
3027
Johannes Berg9ae54c82008-01-31 19:48:20 +01003028ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003029ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3030 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003031{
3032 struct ieee80211_mgmt *mgmt;
3033 u16 fc;
3034
Zhu Yiece8edd2007-11-22 10:53:21 +08003035 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003036 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003037
3038 mgmt = (struct ieee80211_mgmt *) skb->data;
3039 fc = le16_to_cpu(mgmt->frame_control);
3040
Zhu Yiece8edd2007-11-22 10:53:21 +08003041 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01003042 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003043
3044 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003045 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003046
Jiri Bencf0706e82007-05-05 11:45:53 -07003047 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3048 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3049 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3050 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003051 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003052 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003053 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3054 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3055 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003056 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003057 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003058 }
3059 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01003060 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003061}
3062
3063
3064static int ieee80211_sta_active_ibss(struct net_device *dev)
3065{
3066 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3067 int active = 0;
3068 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003069 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003070
Johannes Bergd0709a62008-02-25 16:27:46 +01003071 rcu_read_lock();
3072
3073 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3074 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003075 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3076 jiffies)) {
3077 active++;
3078 break;
3079 }
3080 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003081
3082 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003083
3084 return active;
3085}
3086
3087
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003088static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003089{
3090 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3091 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003092 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003093 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003094 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003095
Johannes Bergd0709a62008-02-25 16:27:46 +01003096 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003097 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003098 if (time_after(jiffies, sta->last_rx + exp_time)) {
Joe Perches0795af52007-10-03 17:59:30 -07003099 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3100 dev->name, print_mac(mac, sta->addr));
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003101 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003102 if (sta)
3103 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003104 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003105 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003106
Johannes Bergd0709a62008-02-25 16:27:46 +01003107 synchronize_rcu();
3108
3109 rtnl_lock();
3110 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3111 sta_info_destroy(sta);
3112 rtnl_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003113}
3114
3115
3116static void ieee80211_sta_merge_ibss(struct net_device *dev,
3117 struct ieee80211_if_sta *ifsta)
3118{
3119 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3120
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003121 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07003122 if (ieee80211_sta_active_ibss(dev))
3123 return;
3124
3125 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3126 "IBSS networks with same SSID (merge)\n", dev->name);
3127 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3128}
3129
3130
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003131#ifdef CONFIG_MAC80211_MESH
3132static void ieee80211_mesh_housekeeping(struct net_device *dev,
3133 struct ieee80211_if_sta *ifsta)
3134{
3135 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3136 bool free_plinks;
3137
3138 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3139 mesh_path_expire(dev);
3140
3141 free_plinks = mesh_plink_availables(sdata);
3142 if (free_plinks != sdata->u.sta.accepting_plinks)
3143 ieee80211_if_config_beacon(dev);
3144
3145 mod_timer(&ifsta->timer, jiffies +
3146 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3147}
3148
3149
3150void ieee80211_start_mesh(struct net_device *dev)
3151{
3152 struct ieee80211_if_sta *ifsta;
3153 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3154 ifsta = &sdata->u.sta;
3155 ifsta->state = IEEE80211_MESH_UP;
3156 ieee80211_sta_timer((unsigned long)sdata);
3157}
3158#endif
3159
3160
Jiri Bencf0706e82007-05-05 11:45:53 -07003161void ieee80211_sta_timer(unsigned long data)
3162{
3163 struct ieee80211_sub_if_data *sdata =
3164 (struct ieee80211_sub_if_data *) data;
3165 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3166 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3167
3168 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3169 queue_work(local->hw.workqueue, &ifsta->work);
3170}
3171
Jiri Bencf0706e82007-05-05 11:45:53 -07003172void ieee80211_sta_work(struct work_struct *work)
3173{
3174 struct ieee80211_sub_if_data *sdata =
3175 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3176 struct net_device *dev = sdata->dev;
3177 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3178 struct ieee80211_if_sta *ifsta;
3179 struct sk_buff *skb;
3180
3181 if (!netif_running(dev))
3182 return;
3183
Zhu Yiece8edd2007-11-22 10:53:21 +08003184 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003185 return;
3186
Johannes Berg51fb61e2007-12-19 01:31:27 +01003187 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003188 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3189 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003190 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01003191 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e82007-05-05 11:45:53 -07003192 return;
3193 }
3194 ifsta = &sdata->u.sta;
3195
3196 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3197 ieee80211_sta_rx_queued_mgmt(dev, skb);
3198
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003199#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003200 if (ifsta->preq_queue_len &&
3201 time_after(jiffies,
3202 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003203 mesh_path_start_discovery(dev);
3204#endif
3205
Jiri Bencf0706e82007-05-05 11:45:53 -07003206 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3207 ifsta->state != IEEE80211_ASSOCIATE &&
3208 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003209 if (ifsta->scan_ssid_len)
3210 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3211 else
3212 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003213 return;
3214 }
3215
3216 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3217 if (ieee80211_sta_config_auth(dev, ifsta))
3218 return;
3219 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3220 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3221 return;
3222
3223 switch (ifsta->state) {
3224 case IEEE80211_DISABLED:
3225 break;
3226 case IEEE80211_AUTHENTICATE:
3227 ieee80211_authenticate(dev, ifsta);
3228 break;
3229 case IEEE80211_ASSOCIATE:
3230 ieee80211_associate(dev, ifsta);
3231 break;
3232 case IEEE80211_ASSOCIATED:
3233 ieee80211_associated(dev, ifsta);
3234 break;
3235 case IEEE80211_IBSS_SEARCH:
3236 ieee80211_sta_find_ibss(dev, ifsta);
3237 break;
3238 case IEEE80211_IBSS_JOINED:
3239 ieee80211_sta_merge_ibss(dev, ifsta);
3240 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003241#ifdef CONFIG_MAC80211_MESH
3242 case IEEE80211_MESH_UP:
3243 ieee80211_mesh_housekeeping(dev, ifsta);
3244 break;
3245#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003246 default:
3247 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3248 ifsta->state);
3249 break;
3250 }
3251
3252 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3253 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3254 "mixed-cell disabled - disassociate\n", dev->name);
3255
3256 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3257 ieee80211_set_disassoc(dev, ifsta, 0);
3258 }
3259}
3260
3261
3262static void ieee80211_sta_reset_auth(struct net_device *dev,
3263 struct ieee80211_if_sta *ifsta)
3264{
3265 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3266
3267 if (local->ops->reset_tsf) {
3268 /* Reset own TSF to allow time synchronization work. */
3269 local->ops->reset_tsf(local_to_hw(local));
3270 }
3271
3272 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3273
3274
3275 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3276 ifsta->auth_alg = WLAN_AUTH_OPEN;
3277 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3278 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3279 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3280 ifsta->auth_alg = WLAN_AUTH_LEAP;
3281 else
3282 ifsta->auth_alg = WLAN_AUTH_OPEN;
3283 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3284 ifsta->auth_alg);
3285 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003286 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3287 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003288 netif_carrier_off(dev);
3289}
3290
3291
3292void ieee80211_sta_req_auth(struct net_device *dev,
3293 struct ieee80211_if_sta *ifsta)
3294{
3295 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3296 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3297
Johannes Berg51fb61e2007-12-19 01:31:27 +01003298 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003299 return;
3300
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003301 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3302 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3303 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3304 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003305 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3306 queue_work(local->hw.workqueue, &ifsta->work);
3307 }
3308}
3309
3310static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3311 const char *ssid, int ssid_len)
3312{
3313 int tmp, hidden_ssid;
3314
Michael Wu48225702007-10-19 17:14:36 -04003315 if (ssid_len == ifsta->ssid_len &&
3316 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003317 return 1;
3318
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003319 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003320 return 0;
3321
3322 hidden_ssid = 1;
3323 tmp = ssid_len;
3324 while (tmp--) {
3325 if (ssid[tmp] != '\0') {
3326 hidden_ssid = 0;
3327 break;
3328 }
3329 }
3330
3331 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3332 return 1;
3333
3334 if (ssid_len == 1 && ssid[0] == ' ')
3335 return 1;
3336
3337 return 0;
3338}
3339
3340static int ieee80211_sta_config_auth(struct net_device *dev,
3341 struct ieee80211_if_sta *ifsta)
3342{
3343 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3344 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3345 struct ieee80211_sta_bss *bss, *selected = NULL;
3346 int top_rssi = 0, freq;
3347
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003348 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3349 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003350 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003351 ieee80211_sta_reset_auth(dev, ifsta);
3352 return 0;
3353 }
3354
3355 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003356 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003357 list_for_each_entry(bss, &local->sta_bss_list, list) {
3358 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3359 continue;
3360
3361 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3362 !!sdata->default_key)
3363 continue;
3364
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003365 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3366 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003367 continue;
3368
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003369 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003370 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3371 continue;
3372
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003373 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003374 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3375 continue;
3376
3377 if (!selected || top_rssi < bss->rssi) {
3378 selected = bss;
3379 top_rssi = bss->rssi;
3380 }
3381 }
3382 if (selected)
3383 atomic_inc(&selected->users);
3384 spin_unlock_bh(&local->sta_bss_lock);
3385
3386 if (selected) {
Johannes Berg8318d782008-01-24 19:38:38 +01003387 ieee80211_set_freq(local, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003388 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003389 ieee80211_sta_set_ssid(dev, selected->ssid,
3390 selected->ssid_len);
3391 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003392 ieee80211_sta_def_wmm_params(dev, selected, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003393 ieee80211_rx_bss_put(dev, selected);
3394 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003395 ieee80211_sta_reset_auth(dev, ifsta);
3396 return 0;
3397 } else {
3398 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003399 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003400 ieee80211_sta_start_scan(dev, NULL, 0);
3401 else
3402 ieee80211_sta_start_scan(dev, ifsta->ssid,
3403 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003404 ifsta->state = IEEE80211_AUTHENTICATE;
3405 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3406 } else
3407 ifsta->state = IEEE80211_DISABLED;
3408 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003409 return -1;
3410}
3411
Jiri Bencf0706e82007-05-05 11:45:53 -07003412
3413static int ieee80211_sta_create_ibss(struct net_device *dev,
3414 struct ieee80211_if_sta *ifsta)
3415{
3416 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3417 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003418 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003419 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003420 u8 bssid[ETH_ALEN], *pos;
3421 int i;
Joe Perches0795af52007-10-03 17:59:30 -07003422 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003423
3424#if 0
3425 /* Easier testing, use fixed BSSID. */
3426 memset(bssid, 0xfe, ETH_ALEN);
3427#else
3428 /* Generate random, not broadcast, locally administered BSSID. Mix in
3429 * own MAC address to make sure that devices that do not have proper
3430 * random number generator get different BSSID. */
3431 get_random_bytes(bssid, ETH_ALEN);
3432 for (i = 0; i < ETH_ALEN; i++)
3433 bssid[i] ^= dev->dev_addr[i];
3434 bssid[0] &= ~0x01;
3435 bssid[0] |= 0x02;
3436#endif
3437
Joe Perches0795af52007-10-03 17:59:30 -07003438 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3439 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003440
Johannes Berg8318d782008-01-24 19:38:38 +01003441 bss = ieee80211_rx_bss_add(dev, bssid,
3442 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003443 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003444 if (!bss)
3445 return -ENOMEM;
3446
Johannes Berg8318d782008-01-24 19:38:38 +01003447 bss->band = local->hw.conf.channel->band;
3448 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003449
3450 if (local->hw.conf.beacon_int == 0)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003451 local->hw.conf.beacon_int = 10000;
Jiri Bencf0706e82007-05-05 11:45:53 -07003452 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003453 bss->last_update = jiffies;
3454 bss->capability = WLAN_CAPABILITY_IBSS;
3455 if (sdata->default_key) {
3456 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3457 } else
3458 sdata->drop_unencrypted = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003459 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003460 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003461 for (i = 0; i < sband->n_bitrates; i++) {
3462 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003463 *pos++ = (u8) (rate / 5);
3464 }
3465
3466 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3467}
3468
3469
3470static int ieee80211_sta_find_ibss(struct net_device *dev,
3471 struct ieee80211_if_sta *ifsta)
3472{
3473 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3474 struct ieee80211_sta_bss *bss;
3475 int found = 0;
3476 u8 bssid[ETH_ALEN];
3477 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003478 DECLARE_MAC_BUF(mac);
3479 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003480
3481 if (ifsta->ssid_len == 0)
3482 return -EINVAL;
3483
3484 active_ibss = ieee80211_sta_active_ibss(dev);
3485#ifdef CONFIG_MAC80211_IBSS_DEBUG
3486 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3487 dev->name, active_ibss);
3488#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3489 spin_lock_bh(&local->sta_bss_lock);
3490 list_for_each_entry(bss, &local->sta_bss_list, list) {
3491 if (ifsta->ssid_len != bss->ssid_len ||
3492 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3493 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3494 continue;
3495#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003496 printk(KERN_DEBUG " bssid=%s found\n",
3497 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003498#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3499 memcpy(bssid, bss->bssid, ETH_ALEN);
3500 found = 1;
3501 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3502 break;
3503 }
3504 spin_unlock_bh(&local->sta_bss_lock);
3505
3506#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003507 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3508 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003509#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3510 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003511 (bss = ieee80211_rx_bss_get(dev, bssid,
3512 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003513 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07003514 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003515 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003516 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003517 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3518 }
3519#ifdef CONFIG_MAC80211_IBSS_DEBUG
3520 printk(KERN_DEBUG " did not try to join ibss\n");
3521#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3522
3523 /* Selected IBSS not found in current scan results - try to scan */
3524 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3525 !ieee80211_sta_active_ibss(dev)) {
3526 mod_timer(&ifsta->timer, jiffies +
3527 IEEE80211_IBSS_MERGE_INTERVAL);
3528 } else if (time_after(jiffies, local->last_scan_completed +
3529 IEEE80211_SCAN_INTERVAL)) {
3530 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3531 "join\n", dev->name);
3532 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3533 ifsta->ssid_len);
3534 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3535 int interval = IEEE80211_SCAN_INTERVAL;
3536
3537 if (time_after(jiffies, ifsta->ibss_join_req +
3538 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003539 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003540 (!(local->oper_channel->flags &
3541 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003542 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003543 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003544 printk(KERN_DEBUG "%s: IBSS not allowed on"
3545 " %d MHz\n", dev->name,
3546 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003547 }
3548
3549 /* No IBSS found - decrease scan interval and continue
3550 * scanning. */
3551 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3552 }
3553
3554 ifsta->state = IEEE80211_IBSS_SEARCH;
3555 mod_timer(&ifsta->timer, jiffies + interval);
3556 return 0;
3557 }
3558
3559 return 0;
3560}
3561
3562
3563int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3564{
Johannes Berg8318d782008-01-24 19:38:38 +01003565 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003566 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07003567
3568 if (len > IEEE80211_MAX_SSID_LEN)
3569 return -EINVAL;
3570
Jiri Bencf0706e82007-05-05 11:45:53 -07003571 ifsta = &sdata->u.sta;
3572
3573 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003574 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003575 memcpy(ifsta->ssid, ssid, len);
3576 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3577 ifsta->ssid_len = len;
3578
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003579 if (len)
3580 ifsta->flags |= IEEE80211_STA_SSID_SET;
3581 else
3582 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003583 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003584 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003585 ifsta->ibss_join_req = jiffies;
3586 ifsta->state = IEEE80211_IBSS_SEARCH;
3587 return ieee80211_sta_find_ibss(dev, ifsta);
3588 }
3589 return 0;
3590}
3591
3592
3593int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3594{
3595 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3596 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3597 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3598 *len = ifsta->ssid_len;
3599 return 0;
3600}
3601
3602
3603int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3604{
3605 struct ieee80211_sub_if_data *sdata;
3606 struct ieee80211_if_sta *ifsta;
3607 int res;
3608
3609 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3610 ifsta = &sdata->u.sta;
3611
3612 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3613 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3614 res = ieee80211_if_config(dev);
3615 if (res) {
3616 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3617 "the low-level driver\n", dev->name);
3618 return res;
3619 }
3620 }
3621
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003622 if (is_valid_ether_addr(bssid))
3623 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003624 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003625 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3626
Jiri Bencf0706e82007-05-05 11:45:53 -07003627 return 0;
3628}
3629
3630
3631static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3632 struct ieee80211_sub_if_data *sdata,
3633 int powersave)
3634{
3635 struct sk_buff *skb;
3636 struct ieee80211_hdr *nullfunc;
3637 u16 fc;
3638
3639 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3640 if (!skb) {
3641 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3642 "frame\n", sdata->dev->name);
3643 return;
3644 }
3645 skb_reserve(skb, local->hw.extra_tx_headroom);
3646
3647 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3648 memset(nullfunc, 0, 24);
3649 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3650 IEEE80211_FCTL_TODS;
3651 if (powersave)
3652 fc |= IEEE80211_FCTL_PM;
3653 nullfunc->frame_control = cpu_to_le16(fc);
3654 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3655 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3656 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3657
3658 ieee80211_sta_tx(sdata->dev, skb, 0);
3659}
3660
3661
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003662static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3663{
3664 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3665 ieee80211_vif_is_mesh(&sdata->vif))
3666 ieee80211_sta_timer((unsigned long)sdata);
3667}
3668
Jiri Bencf0706e82007-05-05 11:45:53 -07003669void ieee80211_scan_completed(struct ieee80211_hw *hw)
3670{
3671 struct ieee80211_local *local = hw_to_local(hw);
3672 struct net_device *dev = local->scan_dev;
3673 struct ieee80211_sub_if_data *sdata;
3674 union iwreq_data wrqu;
3675
3676 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003677 memset(&wrqu, 0, sizeof(wrqu));
3678 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003679
Zhu Yiece8edd2007-11-22 10:53:21 +08003680 if (local->sta_hw_scanning) {
3681 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003682 if (ieee80211_hw_config(local))
3683 printk(KERN_DEBUG "%s: failed to restore operational "
3684 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003685 /* Restart STA timer for HW scan case */
3686 rcu_read_lock();
3687 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3688 ieee80211_restart_sta_timer(sdata);
3689 rcu_read_unlock();
3690
Zhu Yiece8edd2007-11-22 10:53:21 +08003691 goto done;
3692 }
3693
3694 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003695 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003696 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003697 "channel after scan\n", dev->name);
3698
Johannes Berg4150c572007-09-17 01:29:23 -04003699
3700 netif_tx_lock_bh(local->mdev);
3701 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3702 local->ops->configure_filter(local_to_hw(local),
3703 FIF_BCN_PRBRESP_PROMISC,
3704 &local->filter_flags,
3705 local->mdev->mc_count,
3706 local->mdev->mc_list);
3707
3708 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003709
Johannes Berg79010422007-09-18 17:29:21 -04003710 rcu_read_lock();
3711 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003712
3713 /* No need to wake the master device. */
3714 if (sdata->dev == local->mdev)
3715 continue;
3716
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003717 /* Tell AP we're back */
3718 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3719 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3720 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003721
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003722 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003723
Jiri Bencf0706e82007-05-05 11:45:53 -07003724 netif_wake_queue(sdata->dev);
3725 }
Johannes Berg79010422007-09-18 17:29:21 -04003726 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003727
Zhu Yiece8edd2007-11-22 10:53:21 +08003728done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003729 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003730 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003731 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003732 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003733 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3734 !ieee80211_sta_active_ibss(dev)))
3735 ieee80211_sta_find_ibss(dev, ifsta);
3736 }
3737}
3738EXPORT_SYMBOL(ieee80211_scan_completed);
3739
3740void ieee80211_sta_scan_work(struct work_struct *work)
3741{
3742 struct ieee80211_local *local =
3743 container_of(work, struct ieee80211_local, scan_work.work);
3744 struct net_device *dev = local->scan_dev;
3745 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003746 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003747 struct ieee80211_channel *chan;
3748 int skip;
3749 unsigned long next_delay = 0;
3750
Zhu Yiece8edd2007-11-22 10:53:21 +08003751 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003752 return;
3753
3754 switch (local->scan_state) {
3755 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003756 /*
3757 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3758 * after we successfully scanned the last channel of the last
3759 * band (and the last band is supported by the hw)
3760 */
Johannes Berg8318d782008-01-24 19:38:38 +01003761 if (local->scan_band < IEEE80211_NUM_BANDS)
3762 sband = local->hw.wiphy->bands[local->scan_band];
3763 else
3764 sband = NULL;
3765
Johannes Berg69d464d2008-01-28 17:08:56 +01003766 /*
3767 * If we are at an unsupported band and have more bands
3768 * left to scan, advance to the next supported one.
3769 */
3770 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003771 local->scan_band++;
3772 sband = local->hw.wiphy->bands[local->scan_band];
3773 local->scan_channel_idx = 0;
3774 }
3775
Johannes Berg69d464d2008-01-28 17:08:56 +01003776 /* if no more bands/channels left, complete scan */
3777 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003778 ieee80211_scan_completed(local_to_hw(local));
3779 return;
3780 }
Johannes Berg8318d782008-01-24 19:38:38 +01003781 skip = 0;
3782 chan = &sband->channels[local->scan_channel_idx];
3783
3784 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003785 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003786 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003787 skip = 1;
3788
3789 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003790 local->scan_channel = chan;
3791 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003792 printk(KERN_DEBUG "%s: failed to set freq to "
3793 "%d MHz for scan\n", dev->name,
3794 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003795 skip = 1;
3796 }
3797 }
3798
Johannes Berg69d464d2008-01-28 17:08:56 +01003799 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003800 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003801 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003802 /*
3803 * scan_band may end up == IEEE80211_NUM_BANDS, but
3804 * we'll catch that case above and complete the scan
3805 * if that is the case.
3806 */
Johannes Berg8318d782008-01-24 19:38:38 +01003807 local->scan_band++;
3808 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003809 }
3810
3811 if (skip)
3812 break;
3813
3814 next_delay = IEEE80211_PROBE_DELAY +
3815 usecs_to_jiffies(local->hw.channel_change_time);
3816 local->scan_state = SCAN_SEND_PROBE;
3817 break;
3818 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003819 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003820 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003821
3822 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3823 break;
3824 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3825 local->scan_ssid_len);
3826 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003827 break;
3828 }
3829
Zhu Yiece8edd2007-11-22 10:53:21 +08003830 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003831 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3832 next_delay);
3833}
3834
3835
3836static int ieee80211_sta_start_scan(struct net_device *dev,
3837 u8 *ssid, size_t ssid_len)
3838{
3839 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3840 struct ieee80211_sub_if_data *sdata;
3841
3842 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3843 return -EINVAL;
3844
3845 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3846 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3847 * BSSID: MACAddress
3848 * SSID
3849 * ScanType: ACTIVE, PASSIVE
3850 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3851 * a Probe frame during active scanning
3852 * ChannelList
3853 * MinChannelTime (>= ProbeDelay), in TU
3854 * MaxChannelTime: (>= MinChannelTime), in TU
3855 */
3856
3857 /* MLME-SCAN.confirm
3858 * BSSDescriptionSet
3859 * ResultCode: SUCCESS, INVALID_PARAMETERS
3860 */
3861
Zhu Yiece8edd2007-11-22 10:53:21 +08003862 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003863 if (local->scan_dev == dev)
3864 return 0;
3865 return -EBUSY;
3866 }
3867
3868 if (local->ops->hw_scan) {
3869 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003870 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003871 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003872 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003873 local->scan_dev = dev;
3874 }
3875 return rc;
3876 }
3877
Zhu Yiece8edd2007-11-22 10:53:21 +08003878 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003879
Johannes Berg79010422007-09-18 17:29:21 -04003880 rcu_read_lock();
3881 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003882
3883 /* Don't stop the master interface, otherwise we can't transmit
3884 * probes! */
3885 if (sdata->dev == local->mdev)
3886 continue;
3887
Jiri Bencf0706e82007-05-05 11:45:53 -07003888 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003889 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003890 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003891 ieee80211_send_nullfunc(local, sdata, 1);
3892 }
Johannes Berg79010422007-09-18 17:29:21 -04003893 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003894
3895 if (ssid) {
3896 local->scan_ssid_len = ssid_len;
3897 memcpy(local->scan_ssid, ssid, ssid_len);
3898 } else
3899 local->scan_ssid_len = 0;
3900 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07003901 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003902 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07003903 local->scan_dev = dev;
3904
Johannes Berg4150c572007-09-17 01:29:23 -04003905 netif_tx_lock_bh(local->mdev);
3906 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3907 local->ops->configure_filter(local_to_hw(local),
3908 FIF_BCN_PRBRESP_PROMISC,
3909 &local->filter_flags,
3910 local->mdev->mc_count,
3911 local->mdev->mc_list);
3912 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003913
3914 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3915 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3916 IEEE80211_CHANNEL_TIME);
3917
3918 return 0;
3919}
3920
3921
3922int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3923{
3924 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3925 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3926 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3927
Johannes Berg51fb61e2007-12-19 01:31:27 +01003928 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003929 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3930
Zhu Yiece8edd2007-11-22 10:53:21 +08003931 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003932 if (local->scan_dev == dev)
3933 return 0;
3934 return -EBUSY;
3935 }
3936
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003937 ifsta->scan_ssid_len = ssid_len;
3938 if (ssid_len)
3939 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003940 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3941 queue_work(local->hw.workqueue, &ifsta->work);
3942 return 0;
3943}
3944
3945static char *
3946ieee80211_sta_scan_result(struct net_device *dev,
3947 struct ieee80211_sta_bss *bss,
3948 char *current_ev, char *end_buf)
3949{
3950 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3951 struct iw_event iwe;
3952
3953 if (time_after(jiffies,
3954 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3955 return current_ev;
3956
Jiri Bencf0706e82007-05-05 11:45:53 -07003957 memset(&iwe, 0, sizeof(iwe));
3958 iwe.cmd = SIOCGIWAP;
3959 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3960 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3961 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3962 IW_EV_ADDR_LEN);
3963
3964 memset(&iwe, 0, sizeof(iwe));
3965 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01003966 if (bss_mesh_cfg(bss)) {
3967 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003968 iwe.u.data.flags = 1;
3969 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
Johannes Berg902acc72008-02-23 15:17:19 +01003970 bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003971 } else {
3972 iwe.u.data.length = bss->ssid_len;
3973 iwe.u.data.flags = 1;
3974 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3975 bss->ssid);
3976 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003977
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08003978 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
3979 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003980 memset(&iwe, 0, sizeof(iwe));
3981 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01003982 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003983 iwe.u.mode = IW_MODE_MESH;
3984 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07003985 iwe.u.mode = IW_MODE_MASTER;
3986 else
3987 iwe.u.mode = IW_MODE_ADHOC;
3988 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3989 IW_EV_UINT_LEN);
3990 }
3991
3992 memset(&iwe, 0, sizeof(iwe));
3993 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01003994 iwe.u.freq.m = bss->freq;
3995 iwe.u.freq.e = 6;
Jiri Bencf0706e82007-05-05 11:45:53 -07003996 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3997 IW_EV_FREQ_LEN);
Johannes Berg8318d782008-01-24 19:38:38 +01003998
3999 memset(&iwe, 0, sizeof(iwe));
4000 iwe.cmd = SIOCGIWFREQ;
4001 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4002 iwe.u.freq.e = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07004003 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4004 IW_EV_FREQ_LEN);
4005
4006 memset(&iwe, 0, sizeof(iwe));
4007 iwe.cmd = IWEVQUAL;
4008 iwe.u.qual.qual = bss->signal;
4009 iwe.u.qual.level = bss->rssi;
4010 iwe.u.qual.noise = bss->noise;
4011 iwe.u.qual.updated = local->wstats_flags;
4012 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4013 IW_EV_QUAL_LEN);
4014
4015 memset(&iwe, 0, sizeof(iwe));
4016 iwe.cmd = SIOCGIWENCODE;
4017 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4018 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4019 else
4020 iwe.u.data.flags = IW_ENCODE_DISABLED;
4021 iwe.u.data.length = 0;
4022 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4023
4024 if (bss && bss->wpa_ie) {
4025 memset(&iwe, 0, sizeof(iwe));
4026 iwe.cmd = IWEVGENIE;
4027 iwe.u.data.length = bss->wpa_ie_len;
4028 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4029 bss->wpa_ie);
4030 }
4031
4032 if (bss && bss->rsn_ie) {
4033 memset(&iwe, 0, sizeof(iwe));
4034 iwe.cmd = IWEVGENIE;
4035 iwe.u.data.length = bss->rsn_ie_len;
4036 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4037 bss->rsn_ie);
4038 }
4039
4040 if (bss && bss->supp_rates_len > 0) {
4041 /* display all supported rates in readable format */
4042 char *p = current_ev + IW_EV_LCP_LEN;
4043 int i;
4044
4045 memset(&iwe, 0, sizeof(iwe));
4046 iwe.cmd = SIOCGIWRATE;
4047 /* Those two flags are ignored... */
4048 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4049
4050 for (i = 0; i < bss->supp_rates_len; i++) {
4051 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4052 0x7f) * 500000);
4053 p = iwe_stream_add_value(current_ev, p,
4054 end_buf, &iwe, IW_EV_PARAM_LEN);
4055 }
4056 current_ev = p;
4057 }
4058
4059 if (bss) {
4060 char *buf;
4061 buf = kmalloc(30, GFP_ATOMIC);
4062 if (buf) {
4063 memset(&iwe, 0, sizeof(iwe));
4064 iwe.cmd = IWEVCUSTOM;
4065 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4066 iwe.u.data.length = strlen(buf);
4067 current_ev = iwe_stream_add_point(current_ev, end_buf,
4068 &iwe, buf);
4069 kfree(buf);
4070 }
4071 }
4072
Johannes Berg902acc72008-02-23 15:17:19 +01004073 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004074 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04004075 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004076 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004077 if (buf) {
4078 memset(&iwe, 0, sizeof(iwe));
4079 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04004080 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004081 iwe.u.data.length = strlen(buf);
4082 current_ev = iwe_stream_add_point(current_ev, end_buf,
4083 &iwe, buf);
4084 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04004085 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4086 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004087 iwe.u.data.length = strlen(buf);
4088 current_ev = iwe_stream_add_point(current_ev, end_buf,
4089 &iwe, buf);
4090 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04004091 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4092 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004093 iwe.u.data.length = strlen(buf);
4094 current_ev = iwe_stream_add_point(current_ev, end_buf,
4095 &iwe, buf);
4096 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04004097 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4098 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004099 iwe.u.data.length = strlen(buf);
4100 current_ev = iwe_stream_add_point(current_ev, end_buf,
4101 &iwe, buf);
4102 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04004103 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4104 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004105 iwe.u.data.length = strlen(buf);
4106 current_ev = iwe_stream_add_point(current_ev, end_buf,
4107 &iwe, buf);
4108 kfree(buf);
4109 }
4110 }
4111
Jiri Bencf0706e82007-05-05 11:45:53 -07004112 return current_ev;
4113}
4114
4115
4116int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4117{
4118 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4119 char *current_ev = buf;
4120 char *end_buf = buf + len;
4121 struct ieee80211_sta_bss *bss;
4122
4123 spin_lock_bh(&local->sta_bss_lock);
4124 list_for_each_entry(bss, &local->sta_bss_list, list) {
4125 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4126 spin_unlock_bh(&local->sta_bss_lock);
4127 return -E2BIG;
4128 }
4129 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4130 end_buf);
4131 }
4132 spin_unlock_bh(&local->sta_bss_lock);
4133 return current_ev - buf;
4134}
4135
4136
4137int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4138{
4139 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4140 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4141 kfree(ifsta->extra_ie);
4142 if (len == 0) {
4143 ifsta->extra_ie = NULL;
4144 ifsta->extra_ie_len = 0;
4145 return 0;
4146 }
4147 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4148 if (!ifsta->extra_ie) {
4149 ifsta->extra_ie_len = 0;
4150 return -ENOMEM;
4151 }
4152 memcpy(ifsta->extra_ie, ie, len);
4153 ifsta->extra_ie_len = len;
4154 return 0;
4155}
4156
4157
4158struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4159 struct sk_buff *skb, u8 *bssid,
4160 u8 *addr)
4161{
4162 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4163 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004164 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004165 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07004166
4167 /* TODO: Could consider removing the least recently used entry and
4168 * allow new one to be added. */
4169 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4170 if (net_ratelimit()) {
4171 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004172 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004173 }
4174 return NULL;
4175 }
4176
Joe Perches0795af52007-10-03 17:59:30 -07004177 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004178 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07004179
Johannes Berg73651ee2008-02-25 16:27:47 +01004180 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4181 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004182 return NULL;
4183
Johannes Berg238814f2008-01-28 17:19:37 +01004184 sta->flags |= WLAN_STA_AUTHORIZED;
4185
Johannes Berg8318d782008-01-24 19:38:38 +01004186 sta->supp_rates[local->hw.conf.channel->band] =
4187 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004188
4189 rate_control_rate_init(sta, local);
4190
Johannes Berg93e5deb2008-04-01 15:21:00 +02004191 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004192 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004193
Johannes Bergd0709a62008-02-25 16:27:46 +01004194 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004195}
4196
4197
4198int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4199{
4200 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4201 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4202
4203 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4204 dev->name, reason);
4205
Johannes Berg51fb61e2007-12-19 01:31:27 +01004206 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4207 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004208 return -EINVAL;
4209
4210 ieee80211_send_deauth(dev, ifsta, reason);
4211 ieee80211_set_disassoc(dev, ifsta, 1);
4212 return 0;
4213}
4214
4215
4216int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4217{
4218 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4219 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4220
4221 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4222 dev->name, reason);
4223
Johannes Berg51fb61e2007-12-19 01:31:27 +01004224 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004225 return -EINVAL;
4226
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004227 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004228 return -1;
4229
4230 ieee80211_send_disassoc(dev, ifsta, reason);
4231 ieee80211_set_disassoc(dev, ifsta, 0);
4232 return 0;
4233}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004234
4235void ieee80211_notify_mac(struct ieee80211_hw *hw,
4236 enum ieee80211_notification_types notif_type)
4237{
4238 struct ieee80211_local *local = hw_to_local(hw);
4239 struct ieee80211_sub_if_data *sdata;
4240
4241 switch (notif_type) {
4242 case IEEE80211_NOTIFY_RE_ASSOC:
4243 rcu_read_lock();
4244 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4245
4246 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4247 ieee80211_sta_req_auth(sdata->dev,
4248 &sdata->u.sta);
4249 }
4250
4251 }
4252 rcu_read_unlock();
4253 break;
4254 }
4255}
4256EXPORT_SYMBOL(ieee80211_notify_mac);