blob: b4b498ae60f2f7230321eed4d5d6d22bf9ab095a [file] [log] [blame]
Jiri Bencf0706e822007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
Jiri Bencf0706e822007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e822007-05-05 11:45:53 -070020#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
Jiri Bencf0706e822007-05-05 11:45:53 -070027#include <net/iw_handler.h>
28#include <asm/types.h>
Jiri Bencf0706e822007-05-05 11:45:53 -070029
30#include <net/mac80211.h>
31#include "ieee80211_i.h"
32#include "ieee80211_rate.h"
Michael Buesch47f0c502007-09-27 15:10:44 +020033#include "ieee80211_led.h"
Jiri Bencf0706e822007-05-05 11:45:53 -070034
35#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
36#define IEEE80211_AUTH_MAX_TRIES 3
37#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
38#define IEEE80211_ASSOC_MAX_TRIES 3
39#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
40#define IEEE80211_PROBE_INTERVAL (60 * HZ)
41#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
42#define IEEE80211_SCAN_INTERVAL (2 * HZ)
43#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
44#define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
45
46#define IEEE80211_PROBE_DELAY (HZ / 33)
47#define IEEE80211_CHANNEL_TIME (HZ / 33)
48#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
49#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
50#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
51#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
52
53#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
54
55
56#define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
57
58#define ERP_INFO_USE_PROTECTION BIT(1)
59
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020060/* mgmt header + 1 byte action code */
61#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
62
63#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
64#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
65#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020066#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
67#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020068
Ron Rindjunsky07db2182007-12-25 17:00:33 +020069/* next values represent the buffer size for A-MPDU frame.
70 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
71#define IEEE80211_MIN_AMPDU_BUF 0x8
72#define IEEE80211_MAX_AMPDU_BUF 0x40
73
Jiri Bencf0706e822007-05-05 11:45:53 -070074static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
75 u8 *ssid, size_t ssid_len);
76static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +010077ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -040078 u8 *ssid, u8 ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -070079static void ieee80211_rx_bss_put(struct net_device *dev,
80 struct ieee80211_sta_bss *bss);
81static int ieee80211_sta_find_ibss(struct net_device *dev,
82 struct ieee80211_if_sta *ifsta);
83static int ieee80211_sta_wep_configured(struct net_device *dev);
84static int ieee80211_sta_start_scan(struct net_device *dev,
85 u8 *ssid, size_t ssid_len);
86static int ieee80211_sta_config_auth(struct net_device *dev,
87 struct ieee80211_if_sta *ifsta);
88
89
Luis Carlos Coboee385852008-02-23 15:17:11 +010090void ieee802_11_parse_elems(u8 *start, size_t len,
91 struct ieee802_11_elems *elems)
Jiri Bencf0706e822007-05-05 11:45:53 -070092{
93 size_t left = len;
94 u8 *pos = start;
Jiri Bencf0706e822007-05-05 11:45:53 -070095
96 memset(elems, 0, sizeof(*elems));
97
98 while (left >= 2) {
99 u8 id, elen;
100
101 id = *pos++;
102 elen = *pos++;
103 left -= 2;
104
John W. Linville67a4cce2007-10-12 16:40:37 -0400105 if (elen > left)
106 return;
Jiri Bencf0706e822007-05-05 11:45:53 -0700107
108 switch (id) {
109 case WLAN_EID_SSID:
110 elems->ssid = pos;
111 elems->ssid_len = elen;
112 break;
113 case WLAN_EID_SUPP_RATES:
114 elems->supp_rates = pos;
115 elems->supp_rates_len = elen;
116 break;
117 case WLAN_EID_FH_PARAMS:
118 elems->fh_params = pos;
119 elems->fh_params_len = elen;
120 break;
121 case WLAN_EID_DS_PARAMS:
122 elems->ds_params = pos;
123 elems->ds_params_len = elen;
124 break;
125 case WLAN_EID_CF_PARAMS:
126 elems->cf_params = pos;
127 elems->cf_params_len = elen;
128 break;
129 case WLAN_EID_TIM:
130 elems->tim = pos;
131 elems->tim_len = elen;
132 break;
133 case WLAN_EID_IBSS_PARAMS:
134 elems->ibss_params = pos;
135 elems->ibss_params_len = elen;
136 break;
137 case WLAN_EID_CHALLENGE:
138 elems->challenge = pos;
139 elems->challenge_len = elen;
140 break;
141 case WLAN_EID_WPA:
142 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
143 pos[2] == 0xf2) {
144 /* Microsoft OUI (00:50:F2) */
145 if (pos[3] == 1) {
146 /* OUI Type 1 - WPA IE */
147 elems->wpa = pos;
148 elems->wpa_len = elen;
149 } else if (elen >= 5 && pos[3] == 2) {
150 if (pos[4] == 0) {
151 elems->wmm_info = pos;
152 elems->wmm_info_len = elen;
153 } else if (pos[4] == 1) {
154 elems->wmm_param = pos;
155 elems->wmm_param_len = elen;
156 }
157 }
158 }
159 break;
160 case WLAN_EID_RSN:
161 elems->rsn = pos;
162 elems->rsn_len = elen;
163 break;
164 case WLAN_EID_ERP_INFO:
165 elems->erp_info = pos;
166 elems->erp_info_len = elen;
167 break;
168 case WLAN_EID_EXT_SUPP_RATES:
169 elems->ext_supp_rates = pos;
170 elems->ext_supp_rates_len = elen;
171 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200172 case WLAN_EID_HT_CAPABILITY:
173 elems->ht_cap_elem = pos;
174 elems->ht_cap_elem_len = elen;
175 break;
176 case WLAN_EID_HT_EXTRA_INFO:
177 elems->ht_info_elem = pos;
178 elems->ht_info_elem_len = elen;
179 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100180 case WLAN_EID_MESH_ID:
181 elems->mesh_id = pos;
182 elems->mesh_id_len = elen;
183 break;
184 case WLAN_EID_MESH_CONFIG:
185 elems->mesh_config = pos;
186 elems->mesh_config_len = elen;
187 break;
188 case WLAN_EID_PEER_LINK:
189 elems->peer_link = pos;
190 elems->peer_link_len = elen;
191 break;
192 case WLAN_EID_PREQ:
193 elems->preq = pos;
194 elems->preq_len = elen;
195 break;
196 case WLAN_EID_PREP:
197 elems->prep = pos;
198 elems->prep_len = elen;
199 break;
200 case WLAN_EID_PERR:
201 elems->perr = pos;
202 elems->perr_len = elen;
203 break;
Jiri Bencf0706e822007-05-05 11:45:53 -0700204 default:
Jiri Bencf0706e822007-05-05 11:45:53 -0700205 break;
206 }
207
208 left -= elen;
209 pos += elen;
210 }
Jiri Bencf0706e822007-05-05 11:45:53 -0700211}
212
213
Jiri Bencf0706e822007-05-05 11:45:53 -0700214static int ecw2cw(int ecw)
215{
Johannes Bergac2bf322008-02-14 00:30:35 +0100216 return (1 << ecw) - 1;
Jiri Bencf0706e822007-05-05 11:45:53 -0700217}
218
Jiri Bencf0706e822007-05-05 11:45:53 -0700219static void ieee80211_sta_wmm_params(struct net_device *dev,
220 struct ieee80211_if_sta *ifsta,
221 u8 *wmm_param, size_t wmm_param_len)
222{
223 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
224 struct ieee80211_tx_queue_params params;
225 size_t left;
226 int count;
227 u8 *pos;
228
229 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
230 return;
231 count = wmm_param[6] & 0x0f;
232 if (count == ifsta->wmm_last_param_set)
233 return;
234 ifsta->wmm_last_param_set = count;
235
236 pos = wmm_param + 8;
237 left = wmm_param_len - 8;
238
239 memset(&params, 0, sizeof(params));
240
241 if (!local->ops->conf_tx)
242 return;
243
244 local->wmm_acm = 0;
245 for (; left >= 4; left -= 4, pos += 4) {
246 int aci = (pos[0] >> 5) & 0x03;
247 int acm = (pos[0] >> 4) & 0x01;
248 int queue;
249
250 switch (aci) {
251 case 1:
252 queue = IEEE80211_TX_QUEUE_DATA3;
253 if (acm) {
254 local->wmm_acm |= BIT(0) | BIT(3);
255 }
256 break;
257 case 2:
258 queue = IEEE80211_TX_QUEUE_DATA1;
259 if (acm) {
260 local->wmm_acm |= BIT(4) | BIT(5);
261 }
262 break;
263 case 3:
264 queue = IEEE80211_TX_QUEUE_DATA0;
265 if (acm) {
266 local->wmm_acm |= BIT(6) | BIT(7);
267 }
268 break;
269 case 0:
270 default:
271 queue = IEEE80211_TX_QUEUE_DATA2;
272 if (acm) {
273 local->wmm_acm |= BIT(1) | BIT(2);
274 }
275 break;
276 }
277
278 params.aifs = pos[0] & 0x0f;
279 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
280 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100281 params.txop = pos[2] | (pos[3] << 8);
282#ifdef CONFIG_MAC80211_DEBUG
Jiri Bencf0706e822007-05-05 11:45:53 -0700283 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100284 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e822007-05-05 11:45:53 -0700285 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100286 params.cw_max, params.txop);
287#endif
Jiri Bencf0706e822007-05-05 11:45:53 -0700288 /* TODO: handle ACM (block TX, fallback to next lowest allowed
289 * AC for now) */
290 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
291 printk(KERN_DEBUG "%s: failed to set TX queue "
292 "parameters for queue %d\n", dev->name, queue);
293 }
294 }
295}
296
297
Johannes Berg471b3ef2007-12-28 14:32:58 +0100298static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
299 u8 erp_value)
Daniel Drake56282212007-07-10 19:32:10 +0200300{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100301 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Daniel Drake56282212007-07-10 19:32:10 +0200302 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100303 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
304 bool preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
Joe Perches0795af52007-10-03 17:59:30 -0700305 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100306 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200307
Johannes Berg471b3ef2007-12-28 14:32:58 +0100308 if (use_protection != bss_conf->use_cts_prot) {
Daniel Drake56282212007-07-10 19:32:10 +0200309 if (net_ratelimit()) {
310 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700311 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100312 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200313 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700314 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200315 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100316 bss_conf->use_cts_prot = use_protection;
317 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200318 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200319
Johannes Berg471b3ef2007-12-28 14:32:58 +0100320 if (preamble_mode != bss_conf->use_short_preamble) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200321 if (net_ratelimit()) {
322 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700323 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100324 sdata->dev->name,
Daniel Drake7e9ed182007-07-27 15:43:24 +0200325 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
326 "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700327 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200328 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100329 bss_conf->use_short_preamble = preamble_mode;
330 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200331 }
Daniel Draked9430a32007-07-27 15:43:24 +0200332
Johannes Berg471b3ef2007-12-28 14:32:58 +0100333 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200334}
335
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200336int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
337 struct ieee80211_ht_info *ht_info)
338{
339
340 if (ht_info == NULL)
341 return -EINVAL;
342
343 memset(ht_info, 0, sizeof(*ht_info));
344
345 if (ht_cap_ie) {
346 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
347
348 ht_info->ht_supported = 1;
349 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
350 ht_info->ampdu_factor =
351 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
352 ht_info->ampdu_density =
353 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
354 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
355 } else
356 ht_info->ht_supported = 0;
357
358 return 0;
359}
360
361int ieee80211_ht_addt_info_ie_to_ht_bss_info(
362 struct ieee80211_ht_addt_info *ht_add_info_ie,
363 struct ieee80211_ht_bss_info *bss_info)
364{
365 if (bss_info == NULL)
366 return -EINVAL;
367
368 memset(bss_info, 0, sizeof(*bss_info));
369
370 if (ht_add_info_ie) {
371 u16 op_mode;
372 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
373
374 bss_info->primary_channel = ht_add_info_ie->control_chan;
375 bss_info->bss_cap = ht_add_info_ie->ht_param;
376 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
377 }
378
379 return 0;
380}
Daniel Drake56282212007-07-10 19:32:10 +0200381
Jiri Bencf0706e822007-05-05 11:45:53 -0700382static void ieee80211_sta_send_associnfo(struct net_device *dev,
383 struct ieee80211_if_sta *ifsta)
384{
385 char *buf;
386 size_t len;
387 int i;
388 union iwreq_data wrqu;
389
390 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
391 return;
392
393 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200394 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -0700395 if (!buf)
396 return;
397
398 len = sprintf(buf, "ASSOCINFO(");
399 if (ifsta->assocreq_ies) {
400 len += sprintf(buf + len, "ReqIEs=");
401 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
402 len += sprintf(buf + len, "%02x",
403 ifsta->assocreq_ies[i]);
404 }
405 }
406 if (ifsta->assocresp_ies) {
407 if (ifsta->assocreq_ies)
408 len += sprintf(buf + len, " ");
409 len += sprintf(buf + len, "RespIEs=");
410 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
411 len += sprintf(buf + len, "%02x",
412 ifsta->assocresp_ies[i]);
413 }
414 }
415 len += sprintf(buf + len, ")");
416
417 if (len > IW_CUSTOM_MAX) {
418 len = sprintf(buf, "ASSOCRESPIE=");
419 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
420 len += sprintf(buf + len, "%02x",
421 ifsta->assocresp_ies[i]);
422 }
423 }
424
425 memset(&wrqu, 0, sizeof(wrqu));
426 wrqu.data.length = len;
427 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
428
429 kfree(buf);
430}
431
432
433static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400434 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200435 bool assoc)
Jiri Bencf0706e822007-05-05 11:45:53 -0700436{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100437 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
438 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e822007-05-05 11:45:53 -0700439 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100440 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e822007-05-05 11:45:53 -0700441
Jiri Bencf0706e822007-05-05 11:45:53 -0700442 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200443 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400444
445 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
446
Johannes Berg51fb61e2007-12-19 01:31:27 +0100447 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -0700448 return;
Daniel Drake56282212007-07-10 19:32:10 +0200449
John W. Linville65c107a2007-10-05 14:23:27 -0400450 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +0100451 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400452 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200453 if (bss) {
454 if (bss->has_erp_value)
Johannes Berg471b3ef2007-12-28 14:32:58 +0100455 changed |= ieee80211_handle_erp_ie(
456 sdata, bss->erp_value);
Daniel Drake56282212007-07-10 19:32:10 +0200457 ieee80211_rx_bss_put(dev, bss);
458 }
459
Jiri Bencf0706e822007-05-05 11:45:53 -0700460 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400461 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -0700462 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
463 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
464 ieee80211_sta_send_associnfo(dev, ifsta);
465 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400466 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
467
Jiri Bencf0706e822007-05-05 11:45:53 -0700468 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200469 ieee80211_reset_erp_info(dev);
Jiri Bencf0706e822007-05-05 11:45:53 -0700470 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
471 }
472 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
473 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
474 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200475 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100476
Tomas Winklerb2205252008-01-27 16:18:22 +0200477 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100478 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e822007-05-05 11:45:53 -0700479}
480
481static void ieee80211_set_disassoc(struct net_device *dev,
482 struct ieee80211_if_sta *ifsta, int deauth)
483{
484 if (deauth)
485 ifsta->auth_tries = 0;
486 ifsta->assoc_tries = 0;
487 ieee80211_set_associated(dev, ifsta, 0);
488}
489
Luis Carlos Coboee385852008-02-23 15:17:11 +0100490void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
491 int encrypt)
Jiri Bencf0706e822007-05-05 11:45:53 -0700492{
493 struct ieee80211_sub_if_data *sdata;
494 struct ieee80211_tx_packet_data *pkt_data;
495
496 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
497 skb->dev = sdata->local->mdev;
498 skb_set_mac_header(skb, 0);
499 skb_set_network_header(skb, 0);
500 skb_set_transport_header(skb, 0);
501
502 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
503 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
504 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400505 if (!encrypt)
506 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e822007-05-05 11:45:53 -0700507
508 dev_queue_xmit(skb);
509}
510
511
512static void ieee80211_send_auth(struct net_device *dev,
513 struct ieee80211_if_sta *ifsta,
514 int transaction, u8 *extra, size_t extra_len,
515 int encrypt)
516{
517 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
518 struct sk_buff *skb;
519 struct ieee80211_mgmt *mgmt;
520
521 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
522 sizeof(*mgmt) + 6 + extra_len);
523 if (!skb) {
524 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
525 "frame\n", dev->name);
526 return;
527 }
528 skb_reserve(skb, local->hw.extra_tx_headroom);
529
530 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
531 memset(mgmt, 0, 24 + 6);
532 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
533 IEEE80211_STYPE_AUTH);
534 if (encrypt)
535 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
536 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
537 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
538 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
539 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
540 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
541 ifsta->auth_transaction = transaction + 1;
542 mgmt->u.auth.status_code = cpu_to_le16(0);
543 if (extra)
544 memcpy(skb_put(skb, extra_len), extra, extra_len);
545
546 ieee80211_sta_tx(dev, skb, encrypt);
547}
548
549
550static void ieee80211_authenticate(struct net_device *dev,
551 struct ieee80211_if_sta *ifsta)
552{
Joe Perches0795af52007-10-03 17:59:30 -0700553 DECLARE_MAC_BUF(mac);
554
Jiri Bencf0706e822007-05-05 11:45:53 -0700555 ifsta->auth_tries++;
556 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700557 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e822007-05-05 11:45:53 -0700558 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700559 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700560 ifsta->state = IEEE80211_DISABLED;
561 return;
562 }
563
564 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700565 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
566 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700567
568 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
569
570 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
571}
572
573
574static void ieee80211_send_assoc(struct net_device *dev,
575 struct ieee80211_if_sta *ifsta)
576{
577 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e822007-05-05 11:45:53 -0700578 struct sk_buff *skb;
579 struct ieee80211_mgmt *mgmt;
580 u8 *pos, *ies;
581 int i, len;
582 u16 capab;
583 struct ieee80211_sta_bss *bss;
584 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100585 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -0700586
587 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
588 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
589 ifsta->ssid_len);
590 if (!skb) {
591 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
592 "frame\n", dev->name);
593 return;
594 }
595 skb_reserve(skb, local->hw.extra_tx_headroom);
596
Johannes Berg8318d782008-01-24 19:38:38 +0100597 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
598
Jiri Bencf0706e822007-05-05 11:45:53 -0700599 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100600
601 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
602 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
603 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
604 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
605 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700606 }
Johannes Berg8318d782008-01-24 19:38:38 +0100607
608 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
609 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400610 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -0700611 if (bss) {
612 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
613 capab |= WLAN_CAPABILITY_PRIVACY;
614 if (bss->wmm_ie) {
615 wmm = 1;
616 }
617 ieee80211_rx_bss_put(dev, bss);
618 }
619
620 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
621 memset(mgmt, 0, 24);
622 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
623 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
624 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
625
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400626 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700627 skb_put(skb, 10);
628 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
629 IEEE80211_STYPE_REASSOC_REQ);
630 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
631 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
632 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
633 ETH_ALEN);
634 } else {
635 skb_put(skb, 4);
636 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
637 IEEE80211_STYPE_ASSOC_REQ);
638 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
639 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
640 }
641
642 /* SSID */
643 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
644 *pos++ = WLAN_EID_SSID;
645 *pos++ = ifsta->ssid_len;
646 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
647
Johannes Berg8318d782008-01-24 19:38:38 +0100648 len = sband->n_bitrates;
Jiri Bencf0706e822007-05-05 11:45:53 -0700649 if (len > 8)
650 len = 8;
651 pos = skb_put(skb, len + 2);
652 *pos++ = WLAN_EID_SUPP_RATES;
653 *pos++ = len;
654 for (i = 0; i < len; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100655 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e822007-05-05 11:45:53 -0700656 *pos++ = (u8) (rate / 5);
657 }
658
Johannes Berg8318d782008-01-24 19:38:38 +0100659 if (sband->n_bitrates > len) {
660 pos = skb_put(skb, sband->n_bitrates - len + 2);
Jiri Bencf0706e822007-05-05 11:45:53 -0700661 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Johannes Berg8318d782008-01-24 19:38:38 +0100662 *pos++ = sband->n_bitrates - len;
663 for (i = len; i < sband->n_bitrates; i++) {
664 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e822007-05-05 11:45:53 -0700665 *pos++ = (u8) (rate / 5);
666 }
667 }
668
669 if (ifsta->extra_ie) {
670 pos = skb_put(skb, ifsta->extra_ie_len);
671 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
672 }
673
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400674 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700675 pos = skb_put(skb, 9);
676 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
677 *pos++ = 7; /* len */
678 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
679 *pos++ = 0x50;
680 *pos++ = 0xf2;
681 *pos++ = 2; /* WME */
682 *pos++ = 0; /* WME info */
683 *pos++ = 1; /* WME ver */
684 *pos++ = 0;
685 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200686 /* wmm support is a must to HT */
Johannes Berg8318d782008-01-24 19:38:38 +0100687 if (wmm && sband->ht_info.ht_supported) {
688 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200689 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
690 *pos++ = WLAN_EID_HT_CAPABILITY;
691 *pos++ = sizeof(struct ieee80211_ht_cap);
692 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
693 memcpy(pos, &tmp, sizeof(u16));
694 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100695 /* TODO: needs a define here for << 2 */
696 *pos++ = sband->ht_info.ampdu_factor |
697 (sband->ht_info.ampdu_density << 2);
698 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200699 }
Jiri Bencf0706e822007-05-05 11:45:53 -0700700
701 kfree(ifsta->assocreq_ies);
702 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200703 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -0700704 if (ifsta->assocreq_ies)
705 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
706
707 ieee80211_sta_tx(dev, skb, 0);
708}
709
710
711static void ieee80211_send_deauth(struct net_device *dev,
712 struct ieee80211_if_sta *ifsta, u16 reason)
713{
714 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
715 struct sk_buff *skb;
716 struct ieee80211_mgmt *mgmt;
717
718 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
719 if (!skb) {
720 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
721 "frame\n", dev->name);
722 return;
723 }
724 skb_reserve(skb, local->hw.extra_tx_headroom);
725
726 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
727 memset(mgmt, 0, 24);
728 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
729 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
730 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
731 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
732 IEEE80211_STYPE_DEAUTH);
733 skb_put(skb, 2);
734 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
735
736 ieee80211_sta_tx(dev, skb, 0);
737}
738
739
740static void ieee80211_send_disassoc(struct net_device *dev,
741 struct ieee80211_if_sta *ifsta, u16 reason)
742{
743 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
744 struct sk_buff *skb;
745 struct ieee80211_mgmt *mgmt;
746
747 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
748 if (!skb) {
749 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
750 "frame\n", dev->name);
751 return;
752 }
753 skb_reserve(skb, local->hw.extra_tx_headroom);
754
755 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
756 memset(mgmt, 0, 24);
757 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
758 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
759 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
760 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
761 IEEE80211_STYPE_DISASSOC);
762 skb_put(skb, 2);
763 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
764
765 ieee80211_sta_tx(dev, skb, 0);
766}
767
768
769static int ieee80211_privacy_mismatch(struct net_device *dev,
770 struct ieee80211_if_sta *ifsta)
771{
John W. Linville65c107a2007-10-05 14:23:27 -0400772 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e822007-05-05 11:45:53 -0700773 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000774 int bss_privacy;
775 int wep_privacy;
776 int privacy_invoked;
Jiri Bencf0706e822007-05-05 11:45:53 -0700777
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000778 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e822007-05-05 11:45:53 -0700779 return 0;
780
Johannes Berg8318d782008-01-24 19:38:38 +0100781 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
782 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400783 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -0700784 if (!bss)
785 return 0;
786
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000787 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
788 wep_privacy = !!ieee80211_sta_wep_configured(dev);
789 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e822007-05-05 11:45:53 -0700790
791 ieee80211_rx_bss_put(dev, bss);
792
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000793 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
794 return 0;
795
796 return 1;
Jiri Bencf0706e822007-05-05 11:45:53 -0700797}
798
799
800static void ieee80211_associate(struct net_device *dev,
801 struct ieee80211_if_sta *ifsta)
802{
Joe Perches0795af52007-10-03 17:59:30 -0700803 DECLARE_MAC_BUF(mac);
804
Jiri Bencf0706e822007-05-05 11:45:53 -0700805 ifsta->assoc_tries++;
806 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700807 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e822007-05-05 11:45:53 -0700808 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700809 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700810 ifsta->state = IEEE80211_DISABLED;
811 return;
812 }
813
814 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700815 printk(KERN_DEBUG "%s: associate with AP %s\n",
816 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700817 if (ieee80211_privacy_mismatch(dev, ifsta)) {
818 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
819 "mixed-cell disabled - abort association\n", dev->name);
820 ifsta->state = IEEE80211_DISABLED;
821 return;
822 }
823
824 ieee80211_send_assoc(dev, ifsta);
825
826 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
827}
828
829
830static void ieee80211_associated(struct net_device *dev,
831 struct ieee80211_if_sta *ifsta)
832{
833 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
834 struct sta_info *sta;
835 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700836 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -0700837
838 /* TODO: start monitoring current AP signal quality and number of
839 * missed beacons. Scan other channels every now and then and search
840 * for better APs. */
841 /* TODO: remove expired BSSes */
842
843 ifsta->state = IEEE80211_ASSOCIATED;
844
845 sta = sta_info_get(local, ifsta->bssid);
846 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700847 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
848 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700849 disassoc = 1;
850 } else {
851 disassoc = 0;
852 if (time_after(jiffies,
853 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400854 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700855 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700856 "current AP %s - assume out of "
Jiri Bencf0706e822007-05-05 11:45:53 -0700857 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700858 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700859 disassoc = 1;
Michael Wube8755e2007-07-27 15:43:23 +0200860 sta_info_free(sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400861 } else
Jiri Bencf0706e822007-05-05 11:45:53 -0700862 ieee80211_send_probe_req(dev, ifsta->bssid,
863 local->scan_ssid,
864 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400865 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e822007-05-05 11:45:53 -0700866 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400867 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e822007-05-05 11:45:53 -0700868 if (time_after(jiffies, ifsta->last_probe +
869 IEEE80211_PROBE_INTERVAL)) {
870 ifsta->last_probe = jiffies;
871 ieee80211_send_probe_req(dev, ifsta->bssid,
872 ifsta->ssid,
873 ifsta->ssid_len);
874 }
875 }
876 sta_info_put(sta);
877 }
878 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -0500879 ifsta->state = IEEE80211_DISABLED;
880 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -0700881 } else {
882 mod_timer(&ifsta->timer, jiffies +
883 IEEE80211_MONITORING_INTERVAL);
884 }
885}
886
887
888static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
889 u8 *ssid, size_t ssid_len)
890{
891 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +0100892 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -0700893 struct sk_buff *skb;
894 struct ieee80211_mgmt *mgmt;
895 u8 *pos, *supp_rates, *esupp_rates = NULL;
896 int i;
897
898 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
899 if (!skb) {
900 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
901 "request\n", dev->name);
902 return;
903 }
904 skb_reserve(skb, local->hw.extra_tx_headroom);
905
906 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
907 memset(mgmt, 0, 24);
908 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
909 IEEE80211_STYPE_PROBE_REQ);
910 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
911 if (dst) {
912 memcpy(mgmt->da, dst, ETH_ALEN);
913 memcpy(mgmt->bssid, dst, ETH_ALEN);
914 } else {
915 memset(mgmt->da, 0xff, ETH_ALEN);
916 memset(mgmt->bssid, 0xff, ETH_ALEN);
917 }
918 pos = skb_put(skb, 2 + ssid_len);
919 *pos++ = WLAN_EID_SSID;
920 *pos++ = ssid_len;
921 memcpy(pos, ssid, ssid_len);
922
923 supp_rates = skb_put(skb, 2);
924 supp_rates[0] = WLAN_EID_SUPP_RATES;
925 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100926 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
927
928 for (i = 0; i < sband->n_bitrates; i++) {
929 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e822007-05-05 11:45:53 -0700930 if (esupp_rates) {
931 pos = skb_put(skb, 1);
932 esupp_rates[1]++;
933 } else if (supp_rates[1] == 8) {
934 esupp_rates = skb_put(skb, 3);
935 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
936 esupp_rates[1] = 1;
937 pos = &esupp_rates[2];
938 } else {
939 pos = skb_put(skb, 1);
940 supp_rates[1]++;
941 }
Johannes Berg8318d782008-01-24 19:38:38 +0100942 *pos = rate->bitrate / 5;
Jiri Bencf0706e822007-05-05 11:45:53 -0700943 }
944
945 ieee80211_sta_tx(dev, skb, 0);
946}
947
948
949static int ieee80211_sta_wep_configured(struct net_device *dev)
950{
951 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
952 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -0400953 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e822007-05-05 11:45:53 -0700954 return 0;
955 return 1;
956}
957
958
959static void ieee80211_auth_completed(struct net_device *dev,
960 struct ieee80211_if_sta *ifsta)
961{
962 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400963 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e822007-05-05 11:45:53 -0700964 ieee80211_associate(dev, ifsta);
965}
966
967
968static void ieee80211_auth_challenge(struct net_device *dev,
969 struct ieee80211_if_sta *ifsta,
970 struct ieee80211_mgmt *mgmt,
971 size_t len)
972{
973 u8 *pos;
974 struct ieee802_11_elems elems;
975
976 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
977 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -0400978 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -0700979 if (!elems.challenge) {
980 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
981 "frame\n", dev->name);
982 return;
983 }
984 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
985 elems.challenge_len + 2, 1);
986}
987
Ron Rindjunsky9f985b02007-11-26 16:14:32 +0200988static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
989 u8 dialog_token, u16 status, u16 policy,
990 u16 buf_size, u16 timeout)
991{
992 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
993 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
994 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
995 struct sk_buff *skb;
996 struct ieee80211_mgmt *mgmt;
997 u16 capab;
998
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200999 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1000 sizeof(mgmt->u.action.u.addba_resp));
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001001 if (!skb) {
1002 printk(KERN_DEBUG "%s: failed to allocate buffer "
1003 "for addba resp frame\n", dev->name);
1004 return;
1005 }
1006
1007 skb_reserve(skb, local->hw.extra_tx_headroom);
1008 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1009 memset(mgmt, 0, 24);
1010 memcpy(mgmt->da, da, ETH_ALEN);
1011 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001012 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001013 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1014 else
1015 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1016 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1017 IEEE80211_STYPE_ACTION);
1018
1019 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1020 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1021 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1022 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1023
1024 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1025 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1026 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1027
1028 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1029 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1030 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1031
1032 ieee80211_sta_tx(dev, skb, 0);
1033
1034 return;
1035}
1036
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001037void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1038 u16 tid, u8 dialog_token, u16 start_seq_num,
1039 u16 agg_size, u16 timeout)
1040{
1041 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1042 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1043 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1044 struct sk_buff *skb;
1045 struct ieee80211_mgmt *mgmt;
1046 u16 capab;
1047
1048 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1049 sizeof(mgmt->u.action.u.addba_req));
1050
1051
1052 if (!skb) {
1053 printk(KERN_ERR "%s: failed to allocate buffer "
1054 "for addba request frame\n", dev->name);
1055 return;
1056 }
1057 skb_reserve(skb, local->hw.extra_tx_headroom);
1058 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1059 memset(mgmt, 0, 24);
1060 memcpy(mgmt->da, da, ETH_ALEN);
1061 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1062 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1063 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1064 else
1065 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1066
1067 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1068 IEEE80211_STYPE_ACTION);
1069
1070 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1071
1072 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1073 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1074
1075 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1076 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1077 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1078 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1079
1080 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1081
1082 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1083 mgmt->u.action.u.addba_req.start_seq_num =
1084 cpu_to_le16(start_seq_num << 4);
1085
1086 ieee80211_sta_tx(dev, skb, 0);
1087}
1088
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001089static void ieee80211_sta_process_addba_request(struct net_device *dev,
1090 struct ieee80211_mgmt *mgmt,
1091 size_t len)
1092{
1093 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001094 struct ieee80211_hw *hw = &local->hw;
1095 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001096 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001097 struct tid_ampdu_rx *tid_agg_rx;
1098 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001099 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001100 int ret = -EOPNOTSUPP;
1101 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001102
1103 sta = sta_info_get(local, mgmt->sa);
1104 if (!sta)
1105 return;
1106
1107 /* extract session parameters from addba request frame */
1108 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1109 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001110 start_seq_num =
1111 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001112
1113 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1114 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1115 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1116 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1117
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001118 status = WLAN_STATUS_REQUEST_DECLINED;
1119
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001120 /* sanity check for incoming parameters:
1121 * check if configuration can support the BA policy
1122 * and if buffer size does not exceeds max value */
1123 if (((ba_policy != 1)
1124 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1125 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1126 status = WLAN_STATUS_INVALID_QOS_PARAM;
1127#ifdef CONFIG_MAC80211_HT_DEBUG
1128 if (net_ratelimit())
1129 printk(KERN_DEBUG "Block Ack Req with bad params from "
1130 "%s on tid %u. policy %d, buffer size %d\n",
1131 print_mac(mac, mgmt->sa), tid, ba_policy,
1132 buf_size);
1133#endif /* CONFIG_MAC80211_HT_DEBUG */
1134 goto end_no_lock;
1135 }
1136 /* determine default buffer size */
1137 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001138 struct ieee80211_supported_band *sband;
1139
1140 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001141 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001142 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001143 }
1144
1145 tid_agg_rx = &sta->ampdu_mlme.tid_rx[tid];
1146
1147 /* examine state machine */
1148 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1149
1150 if (tid_agg_rx->state != HT_AGG_STATE_IDLE) {
1151#ifdef CONFIG_MAC80211_HT_DEBUG
1152 if (net_ratelimit())
1153 printk(KERN_DEBUG "unexpected Block Ack Req from "
1154 "%s on tid %u\n",
1155 print_mac(mac, mgmt->sa), tid);
1156#endif /* CONFIG_MAC80211_HT_DEBUG */
1157 goto end;
1158 }
1159
1160 /* prepare reordering buffer */
1161 tid_agg_rx->reorder_buf =
1162 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001163 if (!tid_agg_rx->reorder_buf) {
1164 if (net_ratelimit())
1165 printk(KERN_ERR "can not allocate reordering buffer "
1166 "to tid %d\n", tid);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001167 goto end;
1168 }
1169 memset(tid_agg_rx->reorder_buf, 0,
1170 buf_size * sizeof(struct sk_buf *));
1171
1172 if (local->ops->ampdu_action)
1173 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001174 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001175#ifdef CONFIG_MAC80211_HT_DEBUG
1176 printk(KERN_DEBUG "Rx A-MPDU on tid %d result %d", tid, ret);
1177#endif /* CONFIG_MAC80211_HT_DEBUG */
1178
1179 if (ret) {
1180 kfree(tid_agg_rx->reorder_buf);
1181 goto end;
1182 }
1183
1184 /* change state and send addba resp */
1185 tid_agg_rx->state = HT_AGG_STATE_OPERATIONAL;
1186 tid_agg_rx->dialog_token = dialog_token;
1187 tid_agg_rx->ssn = start_seq_num;
1188 tid_agg_rx->head_seq_num = start_seq_num;
1189 tid_agg_rx->buf_size = buf_size;
1190 tid_agg_rx->timeout = timeout;
1191 tid_agg_rx->stored_mpdu_num = 0;
1192 status = WLAN_STATUS_SUCCESS;
1193end:
1194 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1195
1196end_no_lock:
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001197 ieee80211_send_addba_resp(sta->dev, sta->addr, tid, dialog_token,
1198 status, 1, buf_size, timeout);
1199 sta_info_put(sta);
1200}
Jiri Bencf0706e822007-05-05 11:45:53 -07001201
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001202static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1203 struct ieee80211_mgmt *mgmt,
1204 size_t len)
1205{
1206 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1207 struct ieee80211_hw *hw = &local->hw;
1208 struct sta_info *sta;
1209 u16 capab;
1210 u16 tid;
1211 u8 *state;
1212
1213 sta = sta_info_get(local, mgmt->sa);
1214 if (!sta)
1215 return;
1216
1217 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1218 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1219
1220 state = &sta->ampdu_mlme.tid_tx[tid].state;
1221
1222 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1223
1224 if (mgmt->u.action.u.addba_resp.dialog_token !=
1225 sta->ampdu_mlme.tid_tx[tid].dialog_token) {
1226 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1227#ifdef CONFIG_MAC80211_HT_DEBUG
1228 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1229#endif /* CONFIG_MAC80211_HT_DEBUG */
1230 sta_info_put(sta);
1231 return;
1232 }
1233
1234 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid].addba_resp_timer);
1235#ifdef CONFIG_MAC80211_HT_DEBUG
1236 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1237#endif /* CONFIG_MAC80211_HT_DEBUG */
1238 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1239 == WLAN_STATUS_SUCCESS) {
1240 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1241 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1242 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1243 "%d\n", *state);
1244 sta_info_put(sta);
1245 return;
1246 }
1247
1248 if (*state & HT_ADDBA_RECEIVED_MSK)
1249 printk(KERN_DEBUG "double addBA response\n");
1250
1251 *state |= HT_ADDBA_RECEIVED_MSK;
1252 sta->ampdu_mlme.tid_tx[tid].addba_req_num = 0;
1253
1254 if (*state == HT_AGG_STATE_OPERATIONAL) {
1255 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1256 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1257 }
1258
1259 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1260 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1261 } else {
1262 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1263
1264 sta->ampdu_mlme.tid_tx[tid].addba_req_num++;
1265 /* this will allow the state check in stop_BA_session */
1266 *state = HT_AGG_STATE_OPERATIONAL;
1267 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1268 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1269 WLAN_BACK_INITIATOR);
1270 }
1271 sta_info_put(sta);
1272}
1273
1274void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1275 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001276{
1277 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1278 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1279 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1280 struct sk_buff *skb;
1281 struct ieee80211_mgmt *mgmt;
1282 u16 params;
1283
1284 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1285 sizeof(mgmt->u.action.u.delba));
1286
1287 if (!skb) {
1288 printk(KERN_ERR "%s: failed to allocate buffer "
1289 "for delba frame\n", dev->name);
1290 return;
1291 }
1292
1293 skb_reserve(skb, local->hw.extra_tx_headroom);
1294 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1295 memset(mgmt, 0, 24);
1296 memcpy(mgmt->da, da, ETH_ALEN);
1297 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001298 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001299 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1300 else
1301 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1302 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1303 IEEE80211_STYPE_ACTION);
1304
1305 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1306
1307 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1308 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1309 params = (u16)(initiator << 11); /* bit 11 initiator */
1310 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1311
1312 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1313 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1314
1315 ieee80211_sta_tx(dev, skb, 0);
1316}
1317
1318void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1319 u16 initiator, u16 reason)
1320{
1321 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1322 struct ieee80211_hw *hw = &local->hw;
1323 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001324 int ret, i;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001325
1326 sta = sta_info_get(local, ra);
1327 if (!sta)
1328 return;
1329
1330 /* check if TID is in operational state */
1331 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1332 if (sta->ampdu_mlme.tid_rx[tid].state
1333 != HT_AGG_STATE_OPERATIONAL) {
1334 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001335 sta_info_put(sta);
1336 return;
1337 }
1338 sta->ampdu_mlme.tid_rx[tid].state =
1339 HT_AGG_STATE_REQ_STOP_BA_MSK |
1340 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1341 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1342
1343 /* stop HW Rx aggregation. ampdu_action existence
1344 * already verified in session init so we add the BUG_ON */
1345 BUG_ON(!local->ops->ampdu_action);
1346
1347 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001348 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001349 if (ret)
1350 printk(KERN_DEBUG "HW problem - can not stop rx "
1351 "aggergation for tid %d\n", tid);
1352
1353 /* shutdown timer has not expired */
1354 if (initiator != WLAN_BACK_TIMER)
1355 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid].
1356 session_timer);
1357
1358 /* check if this is a self generated aggregation halt */
1359 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1360 ieee80211_send_delba(dev, ra, tid, 0, reason);
1361
1362 /* free the reordering buffer */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001363 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid].buf_size; i++) {
1364 if (sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]) {
1365 /* release the reordered frames */
1366 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]);
1367 sta->ampdu_mlme.tid_rx[tid].stored_mpdu_num--;
1368 sta->ampdu_mlme.tid_rx[tid].reorder_buf[i] = NULL;
1369 }
1370 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001371 kfree(sta->ampdu_mlme.tid_rx[tid].reorder_buf);
1372
1373 sta->ampdu_mlme.tid_rx[tid].state = HT_AGG_STATE_IDLE;
1374 sta_info_put(sta);
1375}
1376
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001377
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001378static void ieee80211_sta_process_delba(struct net_device *dev,
1379 struct ieee80211_mgmt *mgmt, size_t len)
1380{
1381 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1382 struct sta_info *sta;
1383 u16 tid, params;
1384 u16 initiator;
1385 DECLARE_MAC_BUF(mac);
1386
1387 sta = sta_info_get(local, mgmt->sa);
1388 if (!sta)
1389 return;
1390
1391 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1392 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1393 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1394
1395#ifdef CONFIG_MAC80211_HT_DEBUG
1396 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001397 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1398 print_mac(mac, mgmt->sa),
1399 initiator ? "recipient" : "initiator", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001400 mgmt->u.action.u.delba.reason_code);
1401#endif /* CONFIG_MAC80211_HT_DEBUG */
1402
1403 if (initiator == WLAN_BACK_INITIATOR)
1404 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1405 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001406 else { /* WLAN_BACK_RECIPIENT */
1407 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1408 sta->ampdu_mlme.tid_tx[tid].state =
1409 HT_AGG_STATE_OPERATIONAL;
1410 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1411 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1412 WLAN_BACK_RECIPIENT);
1413 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001414 sta_info_put(sta);
1415}
1416
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001417/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001418 * After sending add Block Ack request we activated a timer until
1419 * add Block Ack response will arrive from the recipient.
1420 * If this timer expires sta_addba_resp_timer_expired will be executed.
1421 */
1422void sta_addba_resp_timer_expired(unsigned long data)
1423{
1424 /* not an elegant detour, but there is no choice as the timer passes
1425 * only one argument, and both sta_info and TID are needed, so init
1426 * flow in sta_info_add gives the TID as data, while the timer_to_id
1427 * array gives the sta through container_of */
1428 u16 tid = *(int *)data;
1429 struct sta_info *temp_sta = container_of((void *)data,
1430 struct sta_info, timer_to_tid[tid]);
1431
1432 struct ieee80211_local *local = temp_sta->local;
1433 struct ieee80211_hw *hw = &local->hw;
1434 struct sta_info *sta;
1435 u8 *state;
1436
1437 sta = sta_info_get(local, temp_sta->addr);
1438 if (!sta)
1439 return;
1440
1441 state = &sta->ampdu_mlme.tid_tx[tid].state;
1442 /* check if the TID waits for addBA response */
1443 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1444 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1445 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1446 *state = HT_AGG_STATE_IDLE;
1447 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1448 "expecting addBA response there", tid);
1449 goto timer_expired_exit;
1450 }
1451
1452 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1453
1454 /* go through the state check in stop_BA_session */
1455 *state = HT_AGG_STATE_OPERATIONAL;
1456 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1457 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1458 WLAN_BACK_INITIATOR);
1459
1460timer_expired_exit:
1461 sta_info_put(sta);
1462}
1463
1464/*
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001465 * After receiving Block Ack Request (BAR) we activated a
1466 * timer after each frame arrives from the originator.
1467 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1468 */
1469void sta_rx_agg_session_timer_expired(unsigned long data)
1470{
1471 /* not an elegant detour, but there is no choice as the timer passes
1472 * only one argument, and verious sta_info are needed here, so init
1473 * flow in sta_info_add gives the TID as data, while the timer_to_id
1474 * array gives the sta through container_of */
1475 u8 *ptid = (u8 *)data;
1476 u8 *timer_to_id = ptid - *ptid;
1477 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1478 timer_to_tid[0]);
1479
1480 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
1481 ieee80211_sta_stop_rx_ba_session(sta->dev, sta->addr, (u16)*ptid,
1482 WLAN_BACK_TIMER,
1483 WLAN_REASON_QSTA_TIMEOUT);
1484}
1485
1486
Jiri Bencf0706e822007-05-05 11:45:53 -07001487static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1488 struct ieee80211_if_sta *ifsta,
1489 struct ieee80211_mgmt *mgmt,
1490 size_t len)
1491{
1492 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1493 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001494 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001495
1496 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Berg51fb61e2007-12-19 01:31:27 +01001497 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001498 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001499 "%s, but not in authenticate state - ignored\n",
1500 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001501 return;
1502 }
1503
1504 if (len < 24 + 6) {
1505 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001506 "received from %s - ignored\n",
1507 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001508 return;
1509 }
1510
Johannes Berg51fb61e2007-12-19 01:31:27 +01001511 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e822007-05-05 11:45:53 -07001512 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1513 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001514 "unknown AP (SA=%s BSSID=%s) - "
1515 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1516 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001517 return;
1518 }
1519
Johannes Berg51fb61e2007-12-19 01:31:27 +01001520 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e822007-05-05 11:45:53 -07001521 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1522 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001523 "unknown BSSID (SA=%s BSSID=%s) - "
1524 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1525 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001526 return;
1527 }
1528
1529 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1530 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1531 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1532
Joe Perches0795af52007-10-03 17:59:30 -07001533 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e822007-05-05 11:45:53 -07001534 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001535 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e822007-05-05 11:45:53 -07001536 auth_transaction, status_code);
1537
Johannes Berg51fb61e2007-12-19 01:31:27 +01001538 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001539 /* IEEE 802.11 standard does not require authentication in IBSS
1540 * networks and most implementations do not seem to use it.
1541 * However, try to reply to authentication attempts if someone
1542 * has actually implemented this.
1543 * TODO: Could implement shared key authentication. */
1544 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1545 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1546 "frame (alg=%d transaction=%d)\n",
1547 dev->name, auth_alg, auth_transaction);
1548 return;
1549 }
1550 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1551 }
1552
1553 if (auth_alg != ifsta->auth_alg ||
1554 auth_transaction != ifsta->auth_transaction) {
1555 printk(KERN_DEBUG "%s: unexpected authentication frame "
1556 "(alg=%d transaction=%d)\n",
1557 dev->name, auth_alg, auth_transaction);
1558 return;
1559 }
1560
1561 if (status_code != WLAN_STATUS_SUCCESS) {
1562 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1563 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1564 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1565 u8 algs[3];
1566 const int num_algs = ARRAY_SIZE(algs);
1567 int i, pos;
1568 algs[0] = algs[1] = algs[2] = 0xff;
1569 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1570 algs[0] = WLAN_AUTH_OPEN;
1571 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1572 algs[1] = WLAN_AUTH_SHARED_KEY;
1573 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1574 algs[2] = WLAN_AUTH_LEAP;
1575 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1576 pos = 0;
1577 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1578 pos = 1;
1579 else
1580 pos = 2;
1581 for (i = 0; i < num_algs; i++) {
1582 pos++;
1583 if (pos >= num_algs)
1584 pos = 0;
1585 if (algs[pos] == ifsta->auth_alg ||
1586 algs[pos] == 0xff)
1587 continue;
1588 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1589 !ieee80211_sta_wep_configured(dev))
1590 continue;
1591 ifsta->auth_alg = algs[pos];
1592 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1593 "next try\n",
1594 dev->name, ifsta->auth_alg);
1595 break;
1596 }
1597 }
1598 return;
1599 }
1600
1601 switch (ifsta->auth_alg) {
1602 case WLAN_AUTH_OPEN:
1603 case WLAN_AUTH_LEAP:
1604 ieee80211_auth_completed(dev, ifsta);
1605 break;
1606 case WLAN_AUTH_SHARED_KEY:
1607 if (ifsta->auth_transaction == 4)
1608 ieee80211_auth_completed(dev, ifsta);
1609 else
1610 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1611 break;
1612 }
1613}
1614
1615
1616static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1617 struct ieee80211_if_sta *ifsta,
1618 struct ieee80211_mgmt *mgmt,
1619 size_t len)
1620{
1621 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001622 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001623
1624 if (len < 24 + 2) {
1625 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001626 "received from %s - ignored\n",
1627 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001628 return;
1629 }
1630
1631 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1632 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001633 "unknown AP (SA=%s BSSID=%s) - "
1634 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1635 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001636 return;
1637 }
1638
1639 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1640
Joe Perches0795af52007-10-03 17:59:30 -07001641 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07001642 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001643 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e822007-05-05 11:45:53 -07001644
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001645 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001646 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1647 }
1648
1649 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1650 ifsta->state == IEEE80211_ASSOCIATE ||
1651 ifsta->state == IEEE80211_ASSOCIATED) {
1652 ifsta->state = IEEE80211_AUTHENTICATE;
1653 mod_timer(&ifsta->timer, jiffies +
1654 IEEE80211_RETRY_AUTH_INTERVAL);
1655 }
1656
1657 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001658 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e822007-05-05 11:45:53 -07001659}
1660
1661
1662static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1663 struct ieee80211_if_sta *ifsta,
1664 struct ieee80211_mgmt *mgmt,
1665 size_t len)
1666{
1667 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001668 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001669
1670 if (len < 24 + 2) {
1671 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001672 "received from %s - ignored\n",
1673 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001674 return;
1675 }
1676
1677 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1678 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001679 "unknown AP (SA=%s BSSID=%s) - "
1680 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1681 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001682 return;
1683 }
1684
1685 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1686
Joe Perches0795af52007-10-03 17:59:30 -07001687 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07001688 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001689 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e822007-05-05 11:45:53 -07001690
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001691 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e822007-05-05 11:45:53 -07001692 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1693
1694 if (ifsta->state == IEEE80211_ASSOCIATED) {
1695 ifsta->state = IEEE80211_ASSOCIATE;
1696 mod_timer(&ifsta->timer, jiffies +
1697 IEEE80211_RETRY_AUTH_INTERVAL);
1698 }
1699
1700 ieee80211_set_disassoc(dev, ifsta, 0);
1701}
1702
1703
Johannes Berg471b3ef2007-12-28 14:32:58 +01001704static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e822007-05-05 11:45:53 -07001705 struct ieee80211_if_sta *ifsta,
1706 struct ieee80211_mgmt *mgmt,
1707 size_t len,
1708 int reassoc)
1709{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001710 struct ieee80211_local *local = sdata->local;
1711 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01001712 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -07001713 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001714 u64 rates, basic_rates;
Jiri Bencf0706e822007-05-05 11:45:53 -07001715 u16 capab_info, status_code, aid;
1716 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001717 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e822007-05-05 11:45:53 -07001718 u8 *pos;
1719 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001720 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001721 bool have_higher_than_11mbit = false;
Jiri Bencf0706e822007-05-05 11:45:53 -07001722
1723 /* AssocResp and ReassocResp have identical structure, so process both
1724 * of them in this function. */
1725
1726 if (ifsta->state != IEEE80211_ASSOCIATE) {
1727 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001728 "%s, but not in associate state - ignored\n",
1729 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001730 return;
1731 }
1732
1733 if (len < 24 + 6) {
1734 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001735 "received from %s - ignored\n",
1736 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001737 return;
1738 }
1739
1740 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1741 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001742 "unknown AP (SA=%s BSSID=%s) - "
1743 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1744 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001745 return;
1746 }
1747
1748 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1749 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1750 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e822007-05-05 11:45:53 -07001751
Joe Perches0795af52007-10-03 17:59:30 -07001752 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e822007-05-05 11:45:53 -07001753 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001754 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001755 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e822007-05-05 11:45:53 -07001756
1757 if (status_code != WLAN_STATUS_SUCCESS) {
1758 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1759 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001760 /* if this was a reassociation, ensure we try a "full"
1761 * association next time. This works around some broken APs
1762 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001763 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07001764 return;
1765 }
1766
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001767 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1768 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1769 "set\n", dev->name, aid);
1770 aid &= ~(BIT(15) | BIT(14));
1771
Jiri Bencf0706e822007-05-05 11:45:53 -07001772 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001773 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -07001774
1775 if (!elems.supp_rates) {
1776 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1777 dev->name);
1778 return;
1779 }
1780
1781 printk(KERN_DEBUG "%s: associated\n", dev->name);
1782 ifsta->aid = aid;
1783 ifsta->ap_capab = capab_info;
1784
1785 kfree(ifsta->assocresp_ies);
1786 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001787 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07001788 if (ifsta->assocresp_ies)
1789 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1790
Jiri Bencf0706e822007-05-05 11:45:53 -07001791 /* Add STA entry for the AP */
1792 sta = sta_info_get(local, ifsta->bssid);
1793 if (!sta) {
1794 struct ieee80211_sta_bss *bss;
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001795 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
Johannes Berg43ba7e92008-02-21 14:09:30 +01001796 if (IS_ERR(sta)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001797 printk(KERN_DEBUG "%s: failed to add STA entry for the"
Johannes Berg43ba7e92008-02-21 14:09:30 +01001798 " AP (error %ld)\n", dev->name, PTR_ERR(sta));
Jiri Bencf0706e822007-05-05 11:45:53 -07001799 return;
1800 }
John W. Linville65c107a2007-10-05 14:23:27 -04001801 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001802 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001803 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07001804 if (bss) {
1805 sta->last_rssi = bss->rssi;
1806 sta->last_signal = bss->signal;
1807 sta->last_noise = bss->noise;
1808 ieee80211_rx_bss_put(dev, bss);
1809 }
1810 }
1811
1812 sta->dev = dev;
Johannes Berg238814f2008-01-28 17:19:37 +01001813 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1814 WLAN_STA_AUTHORIZED;
Jiri Bencf0706e822007-05-05 11:45:53 -07001815
1816 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001817 basic_rates = 0;
1818 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1819
Jiri Bencf0706e822007-05-05 11:45:53 -07001820 for (i = 0; i < elems.supp_rates_len; i++) {
1821 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001822
1823 if (rate > 110)
1824 have_higher_than_11mbit = true;
1825
1826 for (j = 0; j < sband->n_bitrates; j++) {
1827 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e822007-05-05 11:45:53 -07001828 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001829 if (elems.supp_rates[i] & 0x80)
1830 basic_rates |= BIT(j);
1831 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001832 }
Johannes Berg8318d782008-01-24 19:38:38 +01001833
Jiri Bencf0706e822007-05-05 11:45:53 -07001834 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1835 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001836
1837 if (rate > 110)
1838 have_higher_than_11mbit = true;
1839
1840 for (j = 0; j < sband->n_bitrates; j++) {
1841 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e822007-05-05 11:45:53 -07001842 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001843 if (elems.ext_supp_rates[i] & 0x80)
1844 basic_rates |= BIT(j);
1845 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001846 }
Johannes Berg8318d782008-01-24 19:38:38 +01001847
1848 sta->supp_rates[local->hw.conf.channel->band] = rates;
1849 sdata->basic_rates = basic_rates;
1850
1851 /* cf. IEEE 802.11 9.2.12 */
1852 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1853 have_higher_than_11mbit)
1854 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1855 else
1856 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e822007-05-05 11:45:53 -07001857
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001858 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1859 local->ops->conf_ht) {
1860 struct ieee80211_ht_bss_info bss_info;
1861
1862 ieee80211_ht_cap_ie_to_ht_info(
1863 (struct ieee80211_ht_cap *)
1864 elems.ht_cap_elem, &sta->ht_info);
1865 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1866 (struct ieee80211_ht_addt_info *)
1867 elems.ht_info_elem, &bss_info);
1868 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
1869 }
1870
Jiri Bencf0706e822007-05-05 11:45:53 -07001871 rate_control_rate_init(sta, local);
1872
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001873 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001874 sta->flags |= WLAN_STA_WME;
1875 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1876 elems.wmm_param_len);
1877 }
1878
Johannes Berg8318d782008-01-24 19:38:38 +01001879 /* set AID, ieee80211_set_associated() will tell the driver */
1880 bss_conf->aid = aid;
1881 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e822007-05-05 11:45:53 -07001882
1883 sta_info_put(sta);
1884
1885 ieee80211_associated(dev, ifsta);
1886}
1887
1888
1889/* Caller must hold local->sta_bss_lock */
1890static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1891 struct ieee80211_sta_bss *bss)
1892{
1893 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1894 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1895 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1896}
1897
1898
1899/* Caller must hold local->sta_bss_lock */
1900static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1901 struct ieee80211_sta_bss *bss)
1902{
1903 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1904 struct ieee80211_sta_bss *b, *prev = NULL;
1905 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1906 while (b) {
1907 if (b == bss) {
1908 if (!prev)
1909 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1910 bss->hnext;
1911 else
1912 prev->hnext = bss->hnext;
1913 break;
1914 }
1915 prev = b;
1916 b = b->hnext;
1917 }
1918}
1919
1920
1921static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01001922ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001923 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e822007-05-05 11:45:53 -07001924{
1925 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1926 struct ieee80211_sta_bss *bss;
1927
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001928 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e822007-05-05 11:45:53 -07001929 if (!bss)
1930 return NULL;
Jiri Bencf0706e822007-05-05 11:45:53 -07001931 atomic_inc(&bss->users);
1932 atomic_inc(&bss->users);
1933 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01001934 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04001935 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
1936 memcpy(bss->ssid, ssid, ssid_len);
1937 bss->ssid_len = ssid_len;
1938 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001939
1940 spin_lock_bh(&local->sta_bss_lock);
1941 /* TODO: order by RSSI? */
1942 list_add_tail(&bss->list, &local->sta_bss_list);
1943 __ieee80211_rx_bss_hash_add(dev, bss);
1944 spin_unlock_bh(&local->sta_bss_lock);
1945 return bss;
1946}
1947
1948
1949static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01001950ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001951 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e822007-05-05 11:45:53 -07001952{
1953 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1954 struct ieee80211_sta_bss *bss;
1955
1956 spin_lock_bh(&local->sta_bss_lock);
1957 bss = local->sta_bss_hash[STA_HASH(bssid)];
1958 while (bss) {
John W. Linvillecffdd302007-10-05 14:23:27 -04001959 if (!memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01001960 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04001961 bss->ssid_len == ssid_len &&
1962 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001963 atomic_inc(&bss->users);
1964 break;
1965 }
1966 bss = bss->hnext;
1967 }
1968 spin_unlock_bh(&local->sta_bss_lock);
1969 return bss;
1970}
1971
1972
1973static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1974{
1975 kfree(bss->wpa_ie);
1976 kfree(bss->rsn_ie);
1977 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02001978 kfree(bss->ht_ie);
Jiri Bencf0706e822007-05-05 11:45:53 -07001979 kfree(bss);
1980}
1981
1982
1983static void ieee80211_rx_bss_put(struct net_device *dev,
1984 struct ieee80211_sta_bss *bss)
1985{
1986 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1987 if (!atomic_dec_and_test(&bss->users))
1988 return;
1989
1990 spin_lock_bh(&local->sta_bss_lock);
1991 __ieee80211_rx_bss_hash_del(dev, bss);
1992 list_del(&bss->list);
1993 spin_unlock_bh(&local->sta_bss_lock);
1994 ieee80211_rx_bss_free(bss);
1995}
1996
1997
1998void ieee80211_rx_bss_list_init(struct net_device *dev)
1999{
2000 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2001 spin_lock_init(&local->sta_bss_lock);
2002 INIT_LIST_HEAD(&local->sta_bss_list);
2003}
2004
2005
2006void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2007{
2008 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2009 struct ieee80211_sta_bss *bss, *tmp;
2010
2011 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2012 ieee80211_rx_bss_put(dev, bss);
2013}
2014
2015
Bruno Randolfa6072682008-02-18 11:21:15 +09002016static int ieee80211_sta_join_ibss(struct net_device *dev,
2017 struct ieee80211_if_sta *ifsta,
2018 struct ieee80211_sta_bss *bss)
2019{
2020 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2021 int res, rates, i, j;
2022 struct sk_buff *skb;
2023 struct ieee80211_mgmt *mgmt;
2024 struct ieee80211_tx_control control;
2025 struct rate_selection ratesel;
2026 u8 *pos;
2027 struct ieee80211_sub_if_data *sdata;
2028 struct ieee80211_supported_band *sband;
2029
2030 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2031
2032 /* Remove possible STA entries from other IBSS networks. */
2033 sta_info_flush(local, NULL);
2034
2035 if (local->ops->reset_tsf) {
2036 /* Reset own TSF to allow time synchronization work. */
2037 local->ops->reset_tsf(local_to_hw(local));
2038 }
2039 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2040 res = ieee80211_if_config(dev);
2041 if (res)
2042 return res;
2043
2044 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2045
2046 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2047 sdata->drop_unencrypted = bss->capability &
2048 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2049
2050 res = ieee80211_set_freq(local, bss->freq);
2051
2052 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2053 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2054 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2055 return -1;
2056 }
2057
2058 /* Set beacon template */
2059 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2060 do {
2061 if (!skb)
2062 break;
2063
2064 skb_reserve(skb, local->hw.extra_tx_headroom);
2065
2066 mgmt = (struct ieee80211_mgmt *)
2067 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2068 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2069 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2070 IEEE80211_STYPE_BEACON);
2071 memset(mgmt->da, 0xff, ETH_ALEN);
2072 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2073 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2074 mgmt->u.beacon.beacon_int =
2075 cpu_to_le16(local->hw.conf.beacon_int);
2076 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2077
2078 pos = skb_put(skb, 2 + ifsta->ssid_len);
2079 *pos++ = WLAN_EID_SSID;
2080 *pos++ = ifsta->ssid_len;
2081 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2082
2083 rates = bss->supp_rates_len;
2084 if (rates > 8)
2085 rates = 8;
2086 pos = skb_put(skb, 2 + rates);
2087 *pos++ = WLAN_EID_SUPP_RATES;
2088 *pos++ = rates;
2089 memcpy(pos, bss->supp_rates, rates);
2090
2091 if (bss->band == IEEE80211_BAND_2GHZ) {
2092 pos = skb_put(skb, 2 + 1);
2093 *pos++ = WLAN_EID_DS_PARAMS;
2094 *pos++ = 1;
2095 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2096 }
2097
2098 pos = skb_put(skb, 2 + 2);
2099 *pos++ = WLAN_EID_IBSS_PARAMS;
2100 *pos++ = 2;
2101 /* FIX: set ATIM window based on scan results */
2102 *pos++ = 0;
2103 *pos++ = 0;
2104
2105 if (bss->supp_rates_len > 8) {
2106 rates = bss->supp_rates_len - 8;
2107 pos = skb_put(skb, 2 + rates);
2108 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2109 *pos++ = rates;
2110 memcpy(pos, &bss->supp_rates[8], rates);
2111 }
2112
2113 memset(&control, 0, sizeof(control));
2114 rate_control_get_rate(dev, sband, skb, &ratesel);
2115 if (!ratesel.rate) {
2116 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2117 "for IBSS beacon\n", dev->name);
2118 break;
2119 }
2120 control.vif = &sdata->vif;
2121 control.tx_rate = ratesel.rate;
2122 if (sdata->bss_conf.use_short_preamble &&
2123 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2124 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2125 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2126 control.flags |= IEEE80211_TXCTL_NO_ACK;
2127 control.retry_limit = 1;
2128
2129 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2130 if (ifsta->probe_resp) {
2131 mgmt = (struct ieee80211_mgmt *)
2132 ifsta->probe_resp->data;
2133 mgmt->frame_control =
2134 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2135 IEEE80211_STYPE_PROBE_RESP);
2136 } else {
2137 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2138 "template for IBSS\n", dev->name);
2139 }
2140
2141 if (local->ops->beacon_update &&
2142 local->ops->beacon_update(local_to_hw(local),
2143 skb, &control) == 0) {
2144 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2145 "template\n", dev->name);
2146 skb = NULL;
2147 }
2148
2149 rates = 0;
2150 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2151 for (i = 0; i < bss->supp_rates_len; i++) {
2152 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2153 for (j = 0; j < sband->n_bitrates; j++)
2154 if (sband->bitrates[j].bitrate == bitrate)
2155 rates |= BIT(j);
2156 }
2157 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2158 } while (0);
2159
2160 if (skb) {
2161 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2162 "template\n", dev->name);
2163 dev_kfree_skb(skb);
2164 }
2165
2166 ifsta->state = IEEE80211_IBSS_JOINED;
2167 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2168
2169 ieee80211_rx_bss_put(dev, bss);
2170
2171 return res;
2172}
2173
2174
Jiri Bencf0706e822007-05-05 11:45:53 -07002175static void ieee80211_rx_bss_info(struct net_device *dev,
2176 struct ieee80211_mgmt *mgmt,
2177 size_t len,
2178 struct ieee80211_rx_status *rx_status,
2179 int beacon)
2180{
2181 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2182 struct ieee802_11_elems elems;
2183 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002184 int freq, clen;
Jiri Bencf0706e822007-05-05 11:45:53 -07002185 struct ieee80211_sta_bss *bss;
2186 struct sta_info *sta;
2187 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002188 u64 beacon_timestamp, rx_timestamp;
Joe Perches0795af52007-10-03 17:59:30 -07002189 DECLARE_MAC_BUF(mac);
2190 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e822007-05-05 11:45:53 -07002191
2192 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2193 return; /* ignore ProbeResp to foreign address */
2194
2195#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002196 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e822007-05-05 11:45:53 -07002197 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002198 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e822007-05-05 11:45:53 -07002199#endif
2200
2201 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2202 if (baselen > len)
2203 return;
2204
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002205 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
John W. Linville67a4cce2007-10-12 16:40:37 -04002206 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -07002207
Johannes Berg51fb61e2007-12-19 01:31:27 +01002208 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e822007-05-05 11:45:53 -07002209 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2210 (sta = sta_info_get(local, mgmt->sa))) {
Johannes Berg8318d782008-01-24 19:38:38 +01002211 struct ieee80211_supported_band *sband;
2212 struct ieee80211_rate *bitrates;
Jiri Bencf0706e822007-05-05 11:45:53 -07002213 size_t num_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01002214 u64 supp_rates, prev_rates;
Jiri Bencf0706e822007-05-05 11:45:53 -07002215 int i, j;
2216
Johannes Berg8318d782008-01-24 19:38:38 +01002217 sband = local->hw.wiphy->bands[rx_status->band];
Zhu Yiece8edd2007-11-22 10:53:21 +08002218
Johannes Berg8318d782008-01-24 19:38:38 +01002219 if (!sband) {
2220 WARN_ON(1);
2221 sband = local->hw.wiphy->bands[
2222 local->hw.conf.channel->band];
Zhu Yiece8edd2007-11-22 10:53:21 +08002223 }
Johannes Berg8318d782008-01-24 19:38:38 +01002224
2225 bitrates = sband->bitrates;
2226 num_rates = sband->n_bitrates;
Jiri Bencf0706e822007-05-05 11:45:53 -07002227
2228 supp_rates = 0;
2229 for (i = 0; i < elems.supp_rates_len +
2230 elems.ext_supp_rates_len; i++) {
2231 u8 rate = 0;
2232 int own_rate;
2233 if (i < elems.supp_rates_len)
2234 rate = elems.supp_rates[i];
2235 else if (elems.ext_supp_rates)
2236 rate = elems.ext_supp_rates
2237 [i - elems.supp_rates_len];
2238 own_rate = 5 * (rate & 0x7f);
Jiri Bencf0706e822007-05-05 11:45:53 -07002239 for (j = 0; j < num_rates; j++)
Johannes Berg8318d782008-01-24 19:38:38 +01002240 if (bitrates[j].bitrate == own_rate)
Jiri Bencf0706e822007-05-05 11:45:53 -07002241 supp_rates |= BIT(j);
2242 }
2243
Johannes Berg8318d782008-01-24 19:38:38 +01002244 prev_rates = sta->supp_rates[rx_status->band];
2245 sta->supp_rates[rx_status->band] &= supp_rates;
2246 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002247 /* No matching rates - this should not really happen.
2248 * Make sure that at least one rate is marked
2249 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002250 sta->supp_rates[rx_status->band] =
2251 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e822007-05-05 11:45:53 -07002252 }
Johannes Berg8318d782008-01-24 19:38:38 +01002253 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002254 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002255 "%s based on beacon info (0x%llx & 0x%llx -> "
2256 "0x%llx)\n",
2257 dev->name, print_mac(mac, sta->addr),
2258 (unsigned long long) prev_rates,
2259 (unsigned long long) supp_rates,
2260 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e822007-05-05 11:45:53 -07002261 }
2262 sta_info_put(sta);
2263 }
2264
2265 if (!elems.ssid)
2266 return;
2267
2268 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002269 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e822007-05-05 11:45:53 -07002270 else
Johannes Berg8318d782008-01-24 19:38:38 +01002271 freq = rx_status->freq;
Jiri Bencf0706e822007-05-05 11:45:53 -07002272
Johannes Berg8318d782008-01-24 19:38:38 +01002273 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002274 elems.ssid, elems.ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07002275 if (!bss) {
Johannes Berg8318d782008-01-24 19:38:38 +01002276 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002277 elems.ssid, elems.ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07002278 if (!bss)
2279 return;
2280 } else {
2281#if 0
2282 /* TODO: order by RSSI? */
2283 spin_lock_bh(&local->sta_bss_lock);
2284 list_move_tail(&bss->list, &local->sta_bss_list);
2285 spin_unlock_bh(&local->sta_bss_lock);
2286#endif
2287 }
2288
Johannes Berg8318d782008-01-24 19:38:38 +01002289 bss->band = rx_status->band;
2290
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002291 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2292 bss->probe_resp && beacon) {
2293 /* STA mode:
2294 * Do not allow beacon to override data from Probe Response. */
Jiri Bencf0706e822007-05-05 11:45:53 -07002295 ieee80211_rx_bss_put(dev, bss);
2296 return;
2297 }
2298
Daniel Drake56282212007-07-10 19:32:10 +02002299 /* save the ERP value so that it is available at association time */
2300 if (elems.erp_info && elems.erp_info_len >= 1) {
2301 bss->erp_value = elems.erp_info[0];
2302 bss->has_erp_value = 1;
2303 }
2304
Jiri Bencf0706e822007-05-05 11:45:53 -07002305 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2306 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e822007-05-05 11:45:53 -07002307
2308 bss->supp_rates_len = 0;
2309 if (elems.supp_rates) {
2310 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2311 if (clen > elems.supp_rates_len)
2312 clen = elems.supp_rates_len;
2313 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2314 clen);
2315 bss->supp_rates_len += clen;
2316 }
2317 if (elems.ext_supp_rates) {
2318 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2319 if (clen > elems.ext_supp_rates_len)
2320 clen = elems.ext_supp_rates_len;
2321 memcpy(&bss->supp_rates[bss->supp_rates_len],
2322 elems.ext_supp_rates, clen);
2323 bss->supp_rates_len += clen;
2324 }
2325
2326 if (elems.wpa &&
2327 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2328 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2329 kfree(bss->wpa_ie);
2330 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2331 if (bss->wpa_ie) {
2332 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2333 bss->wpa_ie_len = elems.wpa_len + 2;
2334 } else
2335 bss->wpa_ie_len = 0;
2336 } else if (!elems.wpa && bss->wpa_ie) {
2337 kfree(bss->wpa_ie);
2338 bss->wpa_ie = NULL;
2339 bss->wpa_ie_len = 0;
2340 }
2341
2342 if (elems.rsn &&
2343 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2344 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2345 kfree(bss->rsn_ie);
2346 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2347 if (bss->rsn_ie) {
2348 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2349 bss->rsn_ie_len = elems.rsn_len + 2;
2350 } else
2351 bss->rsn_ie_len = 0;
2352 } else if (!elems.rsn && bss->rsn_ie) {
2353 kfree(bss->rsn_ie);
2354 bss->rsn_ie = NULL;
2355 bss->rsn_ie_len = 0;
2356 }
2357
2358 if (elems.wmm_param &&
2359 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2360 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2361 kfree(bss->wmm_ie);
2362 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2363 if (bss->wmm_ie) {
2364 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2365 elems.wmm_param_len + 2);
2366 bss->wmm_ie_len = elems.wmm_param_len + 2;
2367 } else
2368 bss->wmm_ie_len = 0;
2369 } else if (!elems.wmm_param && bss->wmm_ie) {
2370 kfree(bss->wmm_ie);
2371 bss->wmm_ie = NULL;
2372 bss->wmm_ie_len = 0;
2373 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002374 if (elems.ht_cap_elem &&
2375 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2376 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2377 kfree(bss->ht_ie);
2378 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2379 if (bss->ht_ie) {
2380 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2381 elems.ht_cap_elem_len + 2);
2382 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2383 } else
2384 bss->ht_ie_len = 0;
2385 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2386 kfree(bss->ht_ie);
2387 bss->ht_ie = NULL;
2388 bss->ht_ie_len = 0;
2389 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002390
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002391 bss->timestamp = beacon_timestamp;
Jiri Bencf0706e822007-05-05 11:45:53 -07002392 bss->last_update = jiffies;
2393 bss->rssi = rx_status->ssi;
2394 bss->signal = rx_status->signal;
2395 bss->noise = rx_status->noise;
2396 if (!beacon)
2397 bss->probe_resp++;
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002398
2399 /* check if we need to merge IBSS */
2400 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2401 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002402 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002403 bss->freq == local->oper_channel->center_freq &&
2404 elems.ssid_len == sdata->u.sta.ssid_len &&
2405 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2406 if (rx_status->flag & RX_FLAG_TSFT) {
2407 /* in order for correct IBSS merging we need mactime
2408 *
2409 * since mactime is defined as the time the first data
2410 * symbol of the frame hits the PHY, and the timestamp
2411 * of the beacon is defined as "the time that the data
2412 * symbol containing the first bit of the timestamp is
2413 * transmitted to the PHY plus the transmitting STA’s
2414 * delays through its local PHY from the MAC-PHY
2415 * interface to its interface with the WM"
2416 * (802.11 11.1.2) - equals the time this bit arrives at
2417 * the receiver - we have to take into account the
2418 * offset between the two.
2419 * e.g: at 1 MBit that means mactime is 192 usec earlier
2420 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2421 */
2422 int rate = local->hw.wiphy->bands[rx_status->band]->
2423 bitrates[rx_status->rate_idx].bitrate;
2424 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2425 } else if (local && local->ops && local->ops->get_tsf)
2426 /* second best option: get current TSF */
2427 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2428 else
2429 /* can't merge without knowing the TSF */
2430 rx_timestamp = -1LLU;
2431#ifdef CONFIG_MAC80211_IBSS_DEBUG
2432 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2433 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2434 print_mac(mac, mgmt->sa),
2435 print_mac(mac2, mgmt->bssid),
2436 (unsigned long long)rx_timestamp,
2437 (unsigned long long)beacon_timestamp,
2438 (unsigned long long)(rx_timestamp - beacon_timestamp),
2439 jiffies);
2440#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2441 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002442#ifndef CONFIG_MAC80211_IBSS_DEBUG
Pavel Roskind97cf012008-02-21 11:33:58 -05002443 if (net_ratelimit())
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002444#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002445 printk(KERN_DEBUG "%s: beacon TSF higher than "
2446 "local TSF - IBSS merge with BSSID %s\n",
2447 dev->name, print_mac(mac, mgmt->bssid));
2448 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2449 ieee80211_ibss_add_sta(dev, NULL,
2450 mgmt->bssid, mgmt->sa);
2451 }
2452 }
2453
Jiri Bencf0706e822007-05-05 11:45:53 -07002454 ieee80211_rx_bss_put(dev, bss);
2455}
2456
2457
2458static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2459 struct ieee80211_mgmt *mgmt,
2460 size_t len,
2461 struct ieee80211_rx_status *rx_status)
2462{
2463 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2464}
2465
2466
2467static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2468 struct ieee80211_mgmt *mgmt,
2469 size_t len,
2470 struct ieee80211_rx_status *rx_status)
2471{
Jiri Bencf0706e822007-05-05 11:45:53 -07002472 struct ieee80211_sub_if_data *sdata;
2473 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e822007-05-05 11:45:53 -07002474 size_t baselen;
2475 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002476 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2477 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002478 u32 changed = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07002479
2480 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2481
2482 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002483 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07002484 return;
2485 ifsta = &sdata->u.sta;
2486
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002487 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e822007-05-05 11:45:53 -07002488 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2489 return;
2490
2491 /* Process beacon from the current BSS */
2492 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2493 if (baselen > len)
2494 return;
2495
John W. Linville67a4cce2007-10-12 16:40:37 -04002496 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -07002497
Daniel Drake56282212007-07-10 19:32:10 +02002498 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002499 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
Jiri Bencf0706e822007-05-05 11:45:53 -07002500
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002501 if (elems.ht_cap_elem && elems.ht_info_elem &&
2502 elems.wmm_param && local->ops->conf_ht &&
2503 conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2504 struct ieee80211_ht_bss_info bss_info;
2505
2506 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2507 (struct ieee80211_ht_addt_info *)
2508 elems.ht_info_elem, &bss_info);
2509 /* check if AP changed bss inforamation */
2510 if ((conf->ht_bss_conf.primary_channel !=
2511 bss_info.primary_channel) ||
2512 (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
2513 (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
2514 ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
2515 &bss_info);
2516 }
2517
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002518 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002519 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2520 elems.wmm_param_len);
2521 }
Johannes Berg471b3ef2007-12-28 14:32:58 +01002522
2523 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e822007-05-05 11:45:53 -07002524}
2525
2526
2527static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2528 struct ieee80211_if_sta *ifsta,
2529 struct ieee80211_mgmt *mgmt,
2530 size_t len,
2531 struct ieee80211_rx_status *rx_status)
2532{
2533 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2534 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2535 int tx_last_beacon;
2536 struct sk_buff *skb;
2537 struct ieee80211_mgmt *resp;
2538 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002539 DECLARE_MAC_BUF(mac);
2540#ifdef CONFIG_MAC80211_IBSS_DEBUG
2541 DECLARE_MAC_BUF(mac2);
2542 DECLARE_MAC_BUF(mac3);
2543#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07002544
Johannes Berg51fb61e2007-12-19 01:31:27 +01002545 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e822007-05-05 11:45:53 -07002546 ifsta->state != IEEE80211_IBSS_JOINED ||
2547 len < 24 + 2 || !ifsta->probe_resp)
2548 return;
2549
2550 if (local->ops->tx_last_beacon)
2551 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2552 else
2553 tx_last_beacon = 1;
2554
2555#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002556 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2557 "%s (tx_last_beacon=%d)\n",
2558 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2559 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e822007-05-05 11:45:53 -07002560#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2561
2562 if (!tx_last_beacon)
2563 return;
2564
2565 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2566 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2567 return;
2568
2569 end = ((u8 *) mgmt) + len;
2570 pos = mgmt->u.probe_req.variable;
2571 if (pos[0] != WLAN_EID_SSID ||
2572 pos + 2 + pos[1] > end) {
2573 if (net_ratelimit()) {
2574 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002575 "from %s\n",
2576 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07002577 }
2578 return;
2579 }
2580 if (pos[1] != 0 &&
2581 (pos[1] != ifsta->ssid_len ||
2582 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2583 /* Ignore ProbeReq for foreign SSID */
2584 return;
2585 }
2586
2587 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002588 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07002589 if (!skb)
2590 return;
2591
2592 resp = (struct ieee80211_mgmt *) skb->data;
2593 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2594#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002595 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2596 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e822007-05-05 11:45:53 -07002597#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2598 ieee80211_sta_tx(dev, skb, 0);
2599}
2600
Johannes Berg4e20cb22007-12-19 01:31:24 +01002601static void ieee80211_rx_mgmt_action(struct net_device *dev,
2602 struct ieee80211_if_sta *ifsta,
2603 struct ieee80211_mgmt *mgmt,
2604 size_t len)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002605{
2606 if (len < IEEE80211_MIN_ACTION_SIZE)
2607 return;
2608
2609 switch (mgmt->u.action.category) {
2610 case WLAN_CATEGORY_BACK:
2611 switch (mgmt->u.action.u.addba_req.action_code) {
2612 case WLAN_ACTION_ADDBA_REQ:
2613 if (len < (IEEE80211_MIN_ACTION_SIZE +
2614 sizeof(mgmt->u.action.u.addba_req)))
2615 break;
2616 ieee80211_sta_process_addba_request(dev, mgmt, len);
2617 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002618 case WLAN_ACTION_ADDBA_RESP:
2619 if (len < (IEEE80211_MIN_ACTION_SIZE +
2620 sizeof(mgmt->u.action.u.addba_resp)))
2621 break;
2622 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2623 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002624 case WLAN_ACTION_DELBA:
2625 if (len < (IEEE80211_MIN_ACTION_SIZE +
2626 sizeof(mgmt->u.action.u.delba)))
2627 break;
2628 ieee80211_sta_process_delba(dev, mgmt, len);
2629 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002630 default:
2631 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002632 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002633 dev->name);
2634 break;
2635 }
2636 break;
2637 default:
2638 break;
2639 }
2640}
Jiri Bencf0706e822007-05-05 11:45:53 -07002641
2642void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2643 struct ieee80211_rx_status *rx_status)
2644{
2645 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2646 struct ieee80211_sub_if_data *sdata;
2647 struct ieee80211_if_sta *ifsta;
2648 struct ieee80211_mgmt *mgmt;
2649 u16 fc;
2650
2651 if (skb->len < 24)
2652 goto fail;
2653
2654 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2655 ifsta = &sdata->u.sta;
2656
2657 mgmt = (struct ieee80211_mgmt *) skb->data;
2658 fc = le16_to_cpu(mgmt->frame_control);
2659
2660 switch (fc & IEEE80211_FCTL_STYPE) {
2661 case IEEE80211_STYPE_PROBE_REQ:
2662 case IEEE80211_STYPE_PROBE_RESP:
2663 case IEEE80211_STYPE_BEACON:
2664 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2665 case IEEE80211_STYPE_AUTH:
2666 case IEEE80211_STYPE_ASSOC_RESP:
2667 case IEEE80211_STYPE_REASSOC_RESP:
2668 case IEEE80211_STYPE_DEAUTH:
2669 case IEEE80211_STYPE_DISASSOC:
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002670 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e822007-05-05 11:45:53 -07002671 skb_queue_tail(&ifsta->skb_queue, skb);
2672 queue_work(local->hw.workqueue, &ifsta->work);
2673 return;
2674 default:
2675 printk(KERN_DEBUG "%s: received unknown management frame - "
2676 "stype=%d\n", dev->name,
2677 (fc & IEEE80211_FCTL_STYPE) >> 4);
2678 break;
2679 }
2680
2681 fail:
2682 kfree_skb(skb);
2683}
2684
2685
2686static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2687 struct sk_buff *skb)
2688{
2689 struct ieee80211_rx_status *rx_status;
2690 struct ieee80211_sub_if_data *sdata;
2691 struct ieee80211_if_sta *ifsta;
2692 struct ieee80211_mgmt *mgmt;
2693 u16 fc;
2694
2695 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2696 ifsta = &sdata->u.sta;
2697
2698 rx_status = (struct ieee80211_rx_status *) skb->cb;
2699 mgmt = (struct ieee80211_mgmt *) skb->data;
2700 fc = le16_to_cpu(mgmt->frame_control);
2701
2702 switch (fc & IEEE80211_FCTL_STYPE) {
2703 case IEEE80211_STYPE_PROBE_REQ:
2704 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2705 rx_status);
2706 break;
2707 case IEEE80211_STYPE_PROBE_RESP:
2708 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2709 break;
2710 case IEEE80211_STYPE_BEACON:
2711 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2712 break;
2713 case IEEE80211_STYPE_AUTH:
2714 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2715 break;
2716 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002717 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -07002718 break;
2719 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002720 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e822007-05-05 11:45:53 -07002721 break;
2722 case IEEE80211_STYPE_DEAUTH:
2723 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2724 break;
2725 case IEEE80211_STYPE_DISASSOC:
2726 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
2727 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002728 case IEEE80211_STYPE_ACTION:
2729 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len);
2730 break;
Jiri Bencf0706e822007-05-05 11:45:53 -07002731 }
2732
2733 kfree_skb(skb);
2734}
2735
2736
Johannes Berg9ae54c82008-01-31 19:48:20 +01002737ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08002738ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
2739 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e822007-05-05 11:45:53 -07002740{
2741 struct ieee80211_mgmt *mgmt;
2742 u16 fc;
2743
Zhu Yiece8edd2007-11-22 10:53:21 +08002744 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002745 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -07002746
2747 mgmt = (struct ieee80211_mgmt *) skb->data;
2748 fc = le16_to_cpu(mgmt->frame_control);
2749
Zhu Yiece8edd2007-11-22 10:53:21 +08002750 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01002751 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08002752
2753 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002754 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08002755
Jiri Bencf0706e822007-05-05 11:45:53 -07002756 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2757 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
2758 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
2759 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002760 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002761 return RX_QUEUED;
Jiri Bencf0706e822007-05-05 11:45:53 -07002762 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
2763 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
2764 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002765 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002766 return RX_QUEUED;
Jiri Bencf0706e822007-05-05 11:45:53 -07002767 }
2768 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01002769 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -07002770}
2771
2772
2773static int ieee80211_sta_active_ibss(struct net_device *dev)
2774{
2775 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2776 int active = 0;
2777 struct sta_info *sta;
2778
Michael Wube8755e2007-07-27 15:43:23 +02002779 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e822007-05-05 11:45:53 -07002780 list_for_each_entry(sta, &local->sta_list, list) {
2781 if (sta->dev == dev &&
2782 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2783 jiffies)) {
2784 active++;
2785 break;
2786 }
2787 }
Michael Wube8755e2007-07-27 15:43:23 +02002788 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e822007-05-05 11:45:53 -07002789
2790 return active;
2791}
2792
2793
2794static void ieee80211_sta_expire(struct net_device *dev)
2795{
2796 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2797 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02002798 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07002799 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07002800
Michael Wube8755e2007-07-27 15:43:23 +02002801 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e822007-05-05 11:45:53 -07002802 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
2803 if (time_after(jiffies, sta->last_rx +
2804 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
Joe Perches0795af52007-10-03 17:59:30 -07002805 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
2806 dev->name, print_mac(mac, sta->addr));
Michael Wube8755e2007-07-27 15:43:23 +02002807 __sta_info_get(sta);
2808 sta_info_remove(sta);
2809 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e822007-05-05 11:45:53 -07002810 }
Michael Wube8755e2007-07-27 15:43:23 +02002811 write_unlock_bh(&local->sta_lock);
2812
2813 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
2814 sta_info_free(sta);
2815 sta_info_put(sta);
2816 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002817}
2818
2819
2820static void ieee80211_sta_merge_ibss(struct net_device *dev,
2821 struct ieee80211_if_sta *ifsta)
2822{
2823 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2824
2825 ieee80211_sta_expire(dev);
2826 if (ieee80211_sta_active_ibss(dev))
2827 return;
2828
2829 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
2830 "IBSS networks with same SSID (merge)\n", dev->name);
2831 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
2832}
2833
2834
2835void ieee80211_sta_timer(unsigned long data)
2836{
2837 struct ieee80211_sub_if_data *sdata =
2838 (struct ieee80211_sub_if_data *) data;
2839 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2840 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
2841
2842 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2843 queue_work(local->hw.workqueue, &ifsta->work);
2844}
2845
2846
2847void ieee80211_sta_work(struct work_struct *work)
2848{
2849 struct ieee80211_sub_if_data *sdata =
2850 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
2851 struct net_device *dev = sdata->dev;
2852 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2853 struct ieee80211_if_sta *ifsta;
2854 struct sk_buff *skb;
2855
2856 if (!netif_running(dev))
2857 return;
2858
Zhu Yiece8edd2007-11-22 10:53:21 +08002859 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07002860 return;
2861
Johannes Berg51fb61e2007-12-19 01:31:27 +01002862 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
2863 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002864 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01002865 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e822007-05-05 11:45:53 -07002866 return;
2867 }
2868 ifsta = &sdata->u.sta;
2869
2870 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2871 ieee80211_sta_rx_queued_mgmt(dev, skb);
2872
2873 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2874 ifsta->state != IEEE80211_ASSOCIATE &&
2875 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01002876 if (ifsta->scan_ssid_len)
2877 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
2878 else
2879 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -07002880 return;
2881 }
2882
2883 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2884 if (ieee80211_sta_config_auth(dev, ifsta))
2885 return;
2886 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2887 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2888 return;
2889
2890 switch (ifsta->state) {
2891 case IEEE80211_DISABLED:
2892 break;
2893 case IEEE80211_AUTHENTICATE:
2894 ieee80211_authenticate(dev, ifsta);
2895 break;
2896 case IEEE80211_ASSOCIATE:
2897 ieee80211_associate(dev, ifsta);
2898 break;
2899 case IEEE80211_ASSOCIATED:
2900 ieee80211_associated(dev, ifsta);
2901 break;
2902 case IEEE80211_IBSS_SEARCH:
2903 ieee80211_sta_find_ibss(dev, ifsta);
2904 break;
2905 case IEEE80211_IBSS_JOINED:
2906 ieee80211_sta_merge_ibss(dev, ifsta);
2907 break;
2908 default:
2909 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2910 ifsta->state);
2911 break;
2912 }
2913
2914 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2915 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2916 "mixed-cell disabled - disassociate\n", dev->name);
2917
2918 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2919 ieee80211_set_disassoc(dev, ifsta, 0);
2920 }
2921}
2922
2923
2924static void ieee80211_sta_reset_auth(struct net_device *dev,
2925 struct ieee80211_if_sta *ifsta)
2926{
2927 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2928
2929 if (local->ops->reset_tsf) {
2930 /* Reset own TSF to allow time synchronization work. */
2931 local->ops->reset_tsf(local_to_hw(local));
2932 }
2933
2934 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2935
2936
2937 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2938 ifsta->auth_alg = WLAN_AUTH_OPEN;
2939 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2940 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2941 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2942 ifsta->auth_alg = WLAN_AUTH_LEAP;
2943 else
2944 ifsta->auth_alg = WLAN_AUTH_OPEN;
2945 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2946 ifsta->auth_alg);
2947 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002948 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2949 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07002950 netif_carrier_off(dev);
2951}
2952
2953
2954void ieee80211_sta_req_auth(struct net_device *dev,
2955 struct ieee80211_if_sta *ifsta)
2956{
2957 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2958 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2959
Johannes Berg51fb61e2007-12-19 01:31:27 +01002960 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07002961 return;
2962
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002963 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2964 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2965 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2966 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002967 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2968 queue_work(local->hw.workqueue, &ifsta->work);
2969 }
2970}
2971
2972static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2973 const char *ssid, int ssid_len)
2974{
2975 int tmp, hidden_ssid;
2976
Michael Wu48225702007-10-19 17:14:36 -04002977 if (ssid_len == ifsta->ssid_len &&
2978 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e822007-05-05 11:45:53 -07002979 return 1;
2980
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002981 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e822007-05-05 11:45:53 -07002982 return 0;
2983
2984 hidden_ssid = 1;
2985 tmp = ssid_len;
2986 while (tmp--) {
2987 if (ssid[tmp] != '\0') {
2988 hidden_ssid = 0;
2989 break;
2990 }
2991 }
2992
2993 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2994 return 1;
2995
2996 if (ssid_len == 1 && ssid[0] == ' ')
2997 return 1;
2998
2999 return 0;
3000}
3001
3002static int ieee80211_sta_config_auth(struct net_device *dev,
3003 struct ieee80211_if_sta *ifsta)
3004{
3005 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3006 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3007 struct ieee80211_sta_bss *bss, *selected = NULL;
3008 int top_rssi = 0, freq;
3009
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003010 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3011 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003012 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e822007-05-05 11:45:53 -07003013 ieee80211_sta_reset_auth(dev, ifsta);
3014 return 0;
3015 }
3016
3017 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003018 freq = local->oper_channel->center_freq;
Jiri Bencf0706e822007-05-05 11:45:53 -07003019 list_for_each_entry(bss, &local->sta_bss_list, list) {
3020 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3021 continue;
3022
3023 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3024 !!sdata->default_key)
3025 continue;
3026
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003027 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3028 bss->freq != freq)
Jiri Bencf0706e822007-05-05 11:45:53 -07003029 continue;
3030
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003031 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07003032 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3033 continue;
3034
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003035 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07003036 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3037 continue;
3038
3039 if (!selected || top_rssi < bss->rssi) {
3040 selected = bss;
3041 top_rssi = bss->rssi;
3042 }
3043 }
3044 if (selected)
3045 atomic_inc(&selected->users);
3046 spin_unlock_bh(&local->sta_bss_lock);
3047
3048 if (selected) {
Johannes Berg8318d782008-01-24 19:38:38 +01003049 ieee80211_set_freq(local, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003050 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e822007-05-05 11:45:53 -07003051 ieee80211_sta_set_ssid(dev, selected->ssid,
3052 selected->ssid_len);
3053 ieee80211_sta_set_bssid(dev, selected->bssid);
3054 ieee80211_rx_bss_put(dev, selected);
3055 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e822007-05-05 11:45:53 -07003056 ieee80211_sta_reset_auth(dev, ifsta);
3057 return 0;
3058 } else {
3059 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003060 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003061 ieee80211_sta_start_scan(dev, NULL, 0);
3062 else
3063 ieee80211_sta_start_scan(dev, ifsta->ssid,
3064 ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003065 ifsta->state = IEEE80211_AUTHENTICATE;
3066 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3067 } else
3068 ifsta->state = IEEE80211_DISABLED;
3069 }
Jiri Bencf0706e822007-05-05 11:45:53 -07003070 return -1;
3071}
3072
Jiri Bencf0706e822007-05-05 11:45:53 -07003073
3074static int ieee80211_sta_create_ibss(struct net_device *dev,
3075 struct ieee80211_if_sta *ifsta)
3076{
3077 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3078 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003079 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003080 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -07003081 u8 bssid[ETH_ALEN], *pos;
3082 int i;
Joe Perches0795af52007-10-03 17:59:30 -07003083 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07003084
3085#if 0
3086 /* Easier testing, use fixed BSSID. */
3087 memset(bssid, 0xfe, ETH_ALEN);
3088#else
3089 /* Generate random, not broadcast, locally administered BSSID. Mix in
3090 * own MAC address to make sure that devices that do not have proper
3091 * random number generator get different BSSID. */
3092 get_random_bytes(bssid, ETH_ALEN);
3093 for (i = 0; i < ETH_ALEN; i++)
3094 bssid[i] ^= dev->dev_addr[i];
3095 bssid[0] &= ~0x01;
3096 bssid[0] |= 0x02;
3097#endif
3098
Joe Perches0795af52007-10-03 17:59:30 -07003099 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3100 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003101
Johannes Berg8318d782008-01-24 19:38:38 +01003102 bss = ieee80211_rx_bss_add(dev, bssid,
3103 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003104 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003105 if (!bss)
3106 return -ENOMEM;
3107
Johannes Berg8318d782008-01-24 19:38:38 +01003108 bss->band = local->hw.conf.channel->band;
3109 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e822007-05-05 11:45:53 -07003110
3111 if (local->hw.conf.beacon_int == 0)
3112 local->hw.conf.beacon_int = 100;
3113 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e822007-05-05 11:45:53 -07003114 bss->last_update = jiffies;
3115 bss->capability = WLAN_CAPABILITY_IBSS;
3116 if (sdata->default_key) {
3117 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3118 } else
3119 sdata->drop_unencrypted = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003120 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e822007-05-05 11:45:53 -07003121 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003122 for (i = 0; i < sband->n_bitrates; i++) {
3123 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e822007-05-05 11:45:53 -07003124 *pos++ = (u8) (rate / 5);
3125 }
3126
3127 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3128}
3129
3130
3131static int ieee80211_sta_find_ibss(struct net_device *dev,
3132 struct ieee80211_if_sta *ifsta)
3133{
3134 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3135 struct ieee80211_sta_bss *bss;
3136 int found = 0;
3137 u8 bssid[ETH_ALEN];
3138 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003139 DECLARE_MAC_BUF(mac);
3140 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e822007-05-05 11:45:53 -07003141
3142 if (ifsta->ssid_len == 0)
3143 return -EINVAL;
3144
3145 active_ibss = ieee80211_sta_active_ibss(dev);
3146#ifdef CONFIG_MAC80211_IBSS_DEBUG
3147 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3148 dev->name, active_ibss);
3149#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3150 spin_lock_bh(&local->sta_bss_lock);
3151 list_for_each_entry(bss, &local->sta_bss_list, list) {
3152 if (ifsta->ssid_len != bss->ssid_len ||
3153 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3154 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3155 continue;
3156#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003157 printk(KERN_DEBUG " bssid=%s found\n",
3158 print_mac(mac, bss->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003159#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3160 memcpy(bssid, bss->bssid, ETH_ALEN);
3161 found = 1;
3162 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3163 break;
3164 }
3165 spin_unlock_bh(&local->sta_bss_lock);
3166
3167#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003168 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3169 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003170#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3171 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003172 (bss = ieee80211_rx_bss_get(dev, bssid,
3173 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003174 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07003175 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07003176 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003177 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003178 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3179 }
3180#ifdef CONFIG_MAC80211_IBSS_DEBUG
3181 printk(KERN_DEBUG " did not try to join ibss\n");
3182#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3183
3184 /* Selected IBSS not found in current scan results - try to scan */
3185 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3186 !ieee80211_sta_active_ibss(dev)) {
3187 mod_timer(&ifsta->timer, jiffies +
3188 IEEE80211_IBSS_MERGE_INTERVAL);
3189 } else if (time_after(jiffies, local->last_scan_completed +
3190 IEEE80211_SCAN_INTERVAL)) {
3191 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3192 "join\n", dev->name);
3193 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3194 ifsta->ssid_len);
3195 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3196 int interval = IEEE80211_SCAN_INTERVAL;
3197
3198 if (time_after(jiffies, ifsta->ibss_join_req +
3199 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003200 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003201 (!(local->oper_channel->flags &
3202 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e822007-05-05 11:45:53 -07003203 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003204 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003205 printk(KERN_DEBUG "%s: IBSS not allowed on"
3206 " %d MHz\n", dev->name,
3207 local->hw.conf.channel->center_freq);
Jiri Bencf0706e822007-05-05 11:45:53 -07003208 }
3209
3210 /* No IBSS found - decrease scan interval and continue
3211 * scanning. */
3212 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3213 }
3214
3215 ifsta->state = IEEE80211_IBSS_SEARCH;
3216 mod_timer(&ifsta->timer, jiffies + interval);
3217 return 0;
3218 }
3219
3220 return 0;
3221}
3222
3223
3224int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3225{
Johannes Berg8318d782008-01-24 19:38:38 +01003226 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003227 struct ieee80211_if_sta *ifsta;
3228 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3229
3230 if (len > IEEE80211_MAX_SSID_LEN)
3231 return -EINVAL;
3232
3233 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
3234 * not defined. */
3235 if (local->ops->conf_tx) {
3236 struct ieee80211_tx_queue_params qparam;
3237 int i;
3238
3239 memset(&qparam, 0, sizeof(qparam));
Johannes Berg8318d782008-01-24 19:38:38 +01003240
Jiri Bencf0706e822007-05-05 11:45:53 -07003241 qparam.aifs = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01003242
3243 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
3244 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
3245 qparam.cw_min = 31;
3246 else
3247 qparam.cw_min = 15;
3248
Jiri Bencf0706e822007-05-05 11:45:53 -07003249 qparam.cw_max = 1023;
Johannes Berg3330d7b2008-02-10 16:49:38 +01003250 qparam.txop = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003251
Jiri Bencf0706e822007-05-05 11:45:53 -07003252 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
Jiri Bencf0706e822007-05-05 11:45:53 -07003253 local->ops->conf_tx(local_to_hw(local),
3254 i + IEEE80211_TX_QUEUE_DATA0,
3255 &qparam);
Johannes Berg8318d782008-01-24 19:38:38 +01003256
Jiri Bencf0706e822007-05-05 11:45:53 -07003257 /* IBSS uses different parameters for Beacon sending */
3258 qparam.cw_min++;
3259 qparam.cw_min *= 2;
3260 qparam.cw_min--;
3261 local->ops->conf_tx(local_to_hw(local),
3262 IEEE80211_TX_QUEUE_BEACON, &qparam);
3263 }
3264
Jiri Bencf0706e822007-05-05 11:45:53 -07003265 ifsta = &sdata->u.sta;
3266
3267 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003268 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07003269 memcpy(ifsta->ssid, ssid, len);
3270 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3271 ifsta->ssid_len = len;
3272
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003273 if (len)
3274 ifsta->flags |= IEEE80211_STA_SSID_SET;
3275 else
3276 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003277 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003278 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003279 ifsta->ibss_join_req = jiffies;
3280 ifsta->state = IEEE80211_IBSS_SEARCH;
3281 return ieee80211_sta_find_ibss(dev, ifsta);
3282 }
3283 return 0;
3284}
3285
3286
3287int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3288{
3289 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3290 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3291 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3292 *len = ifsta->ssid_len;
3293 return 0;
3294}
3295
3296
3297int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3298{
3299 struct ieee80211_sub_if_data *sdata;
3300 struct ieee80211_if_sta *ifsta;
3301 int res;
3302
3303 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3304 ifsta = &sdata->u.sta;
3305
3306 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3307 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3308 res = ieee80211_if_config(dev);
3309 if (res) {
3310 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3311 "the low-level driver\n", dev->name);
3312 return res;
3313 }
3314 }
3315
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003316 if (is_valid_ether_addr(bssid))
3317 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07003318 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003319 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3320
Jiri Bencf0706e822007-05-05 11:45:53 -07003321 return 0;
3322}
3323
3324
3325static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3326 struct ieee80211_sub_if_data *sdata,
3327 int powersave)
3328{
3329 struct sk_buff *skb;
3330 struct ieee80211_hdr *nullfunc;
3331 u16 fc;
3332
3333 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3334 if (!skb) {
3335 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3336 "frame\n", sdata->dev->name);
3337 return;
3338 }
3339 skb_reserve(skb, local->hw.extra_tx_headroom);
3340
3341 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3342 memset(nullfunc, 0, 24);
3343 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3344 IEEE80211_FCTL_TODS;
3345 if (powersave)
3346 fc |= IEEE80211_FCTL_PM;
3347 nullfunc->frame_control = cpu_to_le16(fc);
3348 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3349 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3350 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3351
3352 ieee80211_sta_tx(sdata->dev, skb, 0);
3353}
3354
3355
3356void ieee80211_scan_completed(struct ieee80211_hw *hw)
3357{
3358 struct ieee80211_local *local = hw_to_local(hw);
3359 struct net_device *dev = local->scan_dev;
3360 struct ieee80211_sub_if_data *sdata;
3361 union iwreq_data wrqu;
3362
3363 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003364 memset(&wrqu, 0, sizeof(wrqu));
3365 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e822007-05-05 11:45:53 -07003366
Zhu Yiece8edd2007-11-22 10:53:21 +08003367 if (local->sta_hw_scanning) {
3368 local->sta_hw_scanning = 0;
3369 goto done;
3370 }
3371
3372 local->sta_sw_scanning = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07003373 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003374 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e822007-05-05 11:45:53 -07003375 "channel after scan\n", dev->name);
3376
Johannes Berg4150c572007-09-17 01:29:23 -04003377
3378 netif_tx_lock_bh(local->mdev);
3379 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3380 local->ops->configure_filter(local_to_hw(local),
3381 FIF_BCN_PRBRESP_PROMISC,
3382 &local->filter_flags,
3383 local->mdev->mc_count,
3384 local->mdev->mc_list);
3385
3386 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003387
Johannes Berg79010422007-09-18 17:29:21 -04003388 rcu_read_lock();
3389 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cb2007-06-08 15:31:13 +02003390
3391 /* No need to wake the master device. */
3392 if (sdata->dev == local->mdev)
3393 continue;
3394
Johannes Berg51fb61e2007-12-19 01:31:27 +01003395 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003396 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e822007-05-05 11:45:53 -07003397 ieee80211_send_nullfunc(local, sdata, 0);
3398 ieee80211_sta_timer((unsigned long)sdata);
3399 }
Mattias Nissler14042cb2007-06-08 15:31:13 +02003400
Jiri Bencf0706e822007-05-05 11:45:53 -07003401 netif_wake_queue(sdata->dev);
3402 }
Johannes Berg79010422007-09-18 17:29:21 -04003403 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07003404
Zhu Yiece8edd2007-11-22 10:53:21 +08003405done:
Jiri Bencf0706e822007-05-05 11:45:53 -07003406 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003407 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003408 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003409 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e822007-05-05 11:45:53 -07003410 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3411 !ieee80211_sta_active_ibss(dev)))
3412 ieee80211_sta_find_ibss(dev, ifsta);
3413 }
3414}
3415EXPORT_SYMBOL(ieee80211_scan_completed);
3416
3417void ieee80211_sta_scan_work(struct work_struct *work)
3418{
3419 struct ieee80211_local *local =
3420 container_of(work, struct ieee80211_local, scan_work.work);
3421 struct net_device *dev = local->scan_dev;
3422 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003423 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -07003424 struct ieee80211_channel *chan;
3425 int skip;
3426 unsigned long next_delay = 0;
3427
Zhu Yiece8edd2007-11-22 10:53:21 +08003428 if (!local->sta_sw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07003429 return;
3430
3431 switch (local->scan_state) {
3432 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003433 /*
3434 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3435 * after we successfully scanned the last channel of the last
3436 * band (and the last band is supported by the hw)
3437 */
Johannes Berg8318d782008-01-24 19:38:38 +01003438 if (local->scan_band < IEEE80211_NUM_BANDS)
3439 sband = local->hw.wiphy->bands[local->scan_band];
3440 else
3441 sband = NULL;
3442
Johannes Berg69d464d2008-01-28 17:08:56 +01003443 /*
3444 * If we are at an unsupported band and have more bands
3445 * left to scan, advance to the next supported one.
3446 */
3447 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003448 local->scan_band++;
3449 sband = local->hw.wiphy->bands[local->scan_band];
3450 local->scan_channel_idx = 0;
3451 }
3452
Johannes Berg69d464d2008-01-28 17:08:56 +01003453 /* if no more bands/channels left, complete scan */
3454 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003455 ieee80211_scan_completed(local_to_hw(local));
3456 return;
3457 }
Johannes Berg8318d782008-01-24 19:38:38 +01003458 skip = 0;
3459 chan = &sband->channels[local->scan_channel_idx];
3460
3461 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003462 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003463 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e822007-05-05 11:45:53 -07003464 skip = 1;
3465
3466 if (!skip) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003467 local->scan_channel = chan;
3468 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003469 printk(KERN_DEBUG "%s: failed to set freq to "
3470 "%d MHz for scan\n", dev->name,
3471 chan->center_freq);
Jiri Bencf0706e822007-05-05 11:45:53 -07003472 skip = 1;
3473 }
3474 }
3475
Johannes Berg69d464d2008-01-28 17:08:56 +01003476 /* advance state machine to next channel/band */
Jiri Bencf0706e822007-05-05 11:45:53 -07003477 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003478 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003479 /*
3480 * scan_band may end up == IEEE80211_NUM_BANDS, but
3481 * we'll catch that case above and complete the scan
3482 * if that is the case.
3483 */
Johannes Berg8318d782008-01-24 19:38:38 +01003484 local->scan_band++;
3485 local->scan_channel_idx = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07003486 }
3487
3488 if (skip)
3489 break;
3490
3491 next_delay = IEEE80211_PROBE_DELAY +
3492 usecs_to_jiffies(local->hw.channel_change_time);
3493 local->scan_state = SCAN_SEND_PROBE;
3494 break;
3495 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003496 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e822007-05-05 11:45:53 -07003497 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003498
3499 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3500 break;
3501 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3502 local->scan_ssid_len);
3503 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e822007-05-05 11:45:53 -07003504 break;
3505 }
3506
Zhu Yiece8edd2007-11-22 10:53:21 +08003507 if (local->sta_sw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07003508 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3509 next_delay);
3510}
3511
3512
3513static int ieee80211_sta_start_scan(struct net_device *dev,
3514 u8 *ssid, size_t ssid_len)
3515{
3516 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3517 struct ieee80211_sub_if_data *sdata;
3518
3519 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3520 return -EINVAL;
3521
3522 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3523 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3524 * BSSID: MACAddress
3525 * SSID
3526 * ScanType: ACTIVE, PASSIVE
3527 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3528 * a Probe frame during active scanning
3529 * ChannelList
3530 * MinChannelTime (>= ProbeDelay), in TU
3531 * MaxChannelTime: (>= MinChannelTime), in TU
3532 */
3533
3534 /* MLME-SCAN.confirm
3535 * BSSDescriptionSet
3536 * ResultCode: SUCCESS, INVALID_PARAMETERS
3537 */
3538
Zhu Yiece8edd2007-11-22 10:53:21 +08003539 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003540 if (local->scan_dev == dev)
3541 return 0;
3542 return -EBUSY;
3543 }
3544
3545 if (local->ops->hw_scan) {
3546 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003547 ssid, ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003548 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003549 local->sta_hw_scanning = 1;
Jiri Bencf0706e822007-05-05 11:45:53 -07003550 local->scan_dev = dev;
3551 }
3552 return rc;
3553 }
3554
Zhu Yiece8edd2007-11-22 10:53:21 +08003555 local->sta_sw_scanning = 1;
Jiri Bencf0706e822007-05-05 11:45:53 -07003556
Johannes Berg79010422007-09-18 17:29:21 -04003557 rcu_read_lock();
3558 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cb2007-06-08 15:31:13 +02003559
3560 /* Don't stop the master interface, otherwise we can't transmit
3561 * probes! */
3562 if (sdata->dev == local->mdev)
3563 continue;
3564
Jiri Bencf0706e822007-05-05 11:45:53 -07003565 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003566 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003567 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e822007-05-05 11:45:53 -07003568 ieee80211_send_nullfunc(local, sdata, 1);
3569 }
Johannes Berg79010422007-09-18 17:29:21 -04003570 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07003571
3572 if (ssid) {
3573 local->scan_ssid_len = ssid_len;
3574 memcpy(local->scan_ssid, ssid, ssid_len);
3575 } else
3576 local->scan_ssid_len = 0;
3577 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e822007-05-05 11:45:53 -07003578 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003579 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e822007-05-05 11:45:53 -07003580 local->scan_dev = dev;
3581
Johannes Berg4150c572007-09-17 01:29:23 -04003582 netif_tx_lock_bh(local->mdev);
3583 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3584 local->ops->configure_filter(local_to_hw(local),
3585 FIF_BCN_PRBRESP_PROMISC,
3586 &local->filter_flags,
3587 local->mdev->mc_count,
3588 local->mdev->mc_list);
3589 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003590
3591 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3592 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3593 IEEE80211_CHANNEL_TIME);
3594
3595 return 0;
3596}
3597
3598
3599int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3600{
3601 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3602 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3603 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3604
Johannes Berg51fb61e2007-12-19 01:31:27 +01003605 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07003606 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3607
Zhu Yiece8edd2007-11-22 10:53:21 +08003608 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003609 if (local->scan_dev == dev)
3610 return 0;
3611 return -EBUSY;
3612 }
3613
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003614 ifsta->scan_ssid_len = ssid_len;
3615 if (ssid_len)
3616 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003617 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3618 queue_work(local->hw.workqueue, &ifsta->work);
3619 return 0;
3620}
3621
3622static char *
3623ieee80211_sta_scan_result(struct net_device *dev,
3624 struct ieee80211_sta_bss *bss,
3625 char *current_ev, char *end_buf)
3626{
3627 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3628 struct iw_event iwe;
3629
3630 if (time_after(jiffies,
3631 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3632 return current_ev;
3633
Jiri Bencf0706e822007-05-05 11:45:53 -07003634 memset(&iwe, 0, sizeof(iwe));
3635 iwe.cmd = SIOCGIWAP;
3636 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3637 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3638 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3639 IW_EV_ADDR_LEN);
3640
3641 memset(&iwe, 0, sizeof(iwe));
3642 iwe.cmd = SIOCGIWESSID;
3643 iwe.u.data.length = bss->ssid_len;
3644 iwe.u.data.flags = 1;
3645 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3646 bss->ssid);
3647
3648 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
3649 memset(&iwe, 0, sizeof(iwe));
3650 iwe.cmd = SIOCGIWMODE;
3651 if (bss->capability & WLAN_CAPABILITY_ESS)
3652 iwe.u.mode = IW_MODE_MASTER;
3653 else
3654 iwe.u.mode = IW_MODE_ADHOC;
3655 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3656 IW_EV_UINT_LEN);
3657 }
3658
3659 memset(&iwe, 0, sizeof(iwe));
3660 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01003661 iwe.u.freq.m = bss->freq;
3662 iwe.u.freq.e = 6;
Jiri Bencf0706e822007-05-05 11:45:53 -07003663 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3664 IW_EV_FREQ_LEN);
Johannes Berg8318d782008-01-24 19:38:38 +01003665
3666 memset(&iwe, 0, sizeof(iwe));
3667 iwe.cmd = SIOCGIWFREQ;
3668 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3669 iwe.u.freq.e = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07003670 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3671 IW_EV_FREQ_LEN);
3672
3673 memset(&iwe, 0, sizeof(iwe));
3674 iwe.cmd = IWEVQUAL;
3675 iwe.u.qual.qual = bss->signal;
3676 iwe.u.qual.level = bss->rssi;
3677 iwe.u.qual.noise = bss->noise;
3678 iwe.u.qual.updated = local->wstats_flags;
3679 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3680 IW_EV_QUAL_LEN);
3681
3682 memset(&iwe, 0, sizeof(iwe));
3683 iwe.cmd = SIOCGIWENCODE;
3684 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3685 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3686 else
3687 iwe.u.data.flags = IW_ENCODE_DISABLED;
3688 iwe.u.data.length = 0;
3689 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
3690
3691 if (bss && bss->wpa_ie) {
3692 memset(&iwe, 0, sizeof(iwe));
3693 iwe.cmd = IWEVGENIE;
3694 iwe.u.data.length = bss->wpa_ie_len;
3695 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3696 bss->wpa_ie);
3697 }
3698
3699 if (bss && bss->rsn_ie) {
3700 memset(&iwe, 0, sizeof(iwe));
3701 iwe.cmd = IWEVGENIE;
3702 iwe.u.data.length = bss->rsn_ie_len;
3703 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3704 bss->rsn_ie);
3705 }
3706
3707 if (bss && bss->supp_rates_len > 0) {
3708 /* display all supported rates in readable format */
3709 char *p = current_ev + IW_EV_LCP_LEN;
3710 int i;
3711
3712 memset(&iwe, 0, sizeof(iwe));
3713 iwe.cmd = SIOCGIWRATE;
3714 /* Those two flags are ignored... */
3715 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3716
3717 for (i = 0; i < bss->supp_rates_len; i++) {
3718 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3719 0x7f) * 500000);
3720 p = iwe_stream_add_value(current_ev, p,
3721 end_buf, &iwe, IW_EV_PARAM_LEN);
3722 }
3723 current_ev = p;
3724 }
3725
3726 if (bss) {
3727 char *buf;
3728 buf = kmalloc(30, GFP_ATOMIC);
3729 if (buf) {
3730 memset(&iwe, 0, sizeof(iwe));
3731 iwe.cmd = IWEVCUSTOM;
3732 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3733 iwe.u.data.length = strlen(buf);
3734 current_ev = iwe_stream_add_point(current_ev, end_buf,
3735 &iwe, buf);
3736 kfree(buf);
3737 }
3738 }
3739
Jiri Bencf0706e822007-05-05 11:45:53 -07003740 return current_ev;
3741}
3742
3743
3744int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3745{
3746 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3747 char *current_ev = buf;
3748 char *end_buf = buf + len;
3749 struct ieee80211_sta_bss *bss;
3750
3751 spin_lock_bh(&local->sta_bss_lock);
3752 list_for_each_entry(bss, &local->sta_bss_list, list) {
3753 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3754 spin_unlock_bh(&local->sta_bss_lock);
3755 return -E2BIG;
3756 }
3757 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3758 end_buf);
3759 }
3760 spin_unlock_bh(&local->sta_bss_lock);
3761 return current_ev - buf;
3762}
3763
3764
3765int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3766{
3767 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3768 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3769 kfree(ifsta->extra_ie);
3770 if (len == 0) {
3771 ifsta->extra_ie = NULL;
3772 ifsta->extra_ie_len = 0;
3773 return 0;
3774 }
3775 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3776 if (!ifsta->extra_ie) {
3777 ifsta->extra_ie_len = 0;
3778 return -ENOMEM;
3779 }
3780 memcpy(ifsta->extra_ie, ie, len);
3781 ifsta->extra_ie_len = len;
3782 return 0;
3783}
3784
3785
3786struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3787 struct sk_buff *skb, u8 *bssid,
3788 u8 *addr)
3789{
3790 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3791 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04003792 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07003793 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07003794
3795 /* TODO: Could consider removing the least recently used entry and
3796 * allow new one to be added. */
3797 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3798 if (net_ratelimit()) {
3799 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07003800 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e822007-05-05 11:45:53 -07003801 }
3802 return NULL;
3803 }
3804
Joe Perches0795af52007-10-03 17:59:30 -07003805 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04003806 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e822007-05-05 11:45:53 -07003807
3808 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
Johannes Berg43ba7e92008-02-21 14:09:30 +01003809 if (IS_ERR(sta))
Jiri Bencf0706e822007-05-05 11:45:53 -07003810 return NULL;
3811
Johannes Berg238814f2008-01-28 17:19:37 +01003812 sta->flags |= WLAN_STA_AUTHORIZED;
3813
Johannes Berg8318d782008-01-24 19:38:38 +01003814 sta->supp_rates[local->hw.conf.channel->band] =
3815 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e822007-05-05 11:45:53 -07003816
3817 rate_control_rate_init(sta, local);
3818
3819 return sta; /* caller will call sta_info_put() */
3820}
3821
3822
3823int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3824{
3825 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3826 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3827
3828 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3829 dev->name, reason);
3830
Johannes Berg51fb61e2007-12-19 01:31:27 +01003831 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3832 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e822007-05-05 11:45:53 -07003833 return -EINVAL;
3834
3835 ieee80211_send_deauth(dev, ifsta, reason);
3836 ieee80211_set_disassoc(dev, ifsta, 1);
3837 return 0;
3838}
3839
3840
3841int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3842{
3843 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3844 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3845
3846 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3847 dev->name, reason);
3848
Johannes Berg51fb61e2007-12-19 01:31:27 +01003849 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07003850 return -EINVAL;
3851
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003852 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e822007-05-05 11:45:53 -07003853 return -1;
3854
3855 ieee80211_send_disassoc(dev, ifsta, reason);
3856 ieee80211_set_disassoc(dev, ifsta, 0);
3857 return 0;
3858}