blob: b03f1f3ef2ed5bc4d61358a58093b05399dd143e [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
Jiri Bencf0706e82007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010027#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040033#include "rate.h"
34#include "led.h"
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010035#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +030037#define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
Jiri Bencf0706e82007-05-05 11:45:53 -070038#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
39#define IEEE80211_AUTH_MAX_TRIES 3
40#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
41#define IEEE80211_ASSOC_MAX_TRIES 3
42#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010043#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070044#define IEEE80211_PROBE_INTERVAL (60 * HZ)
45#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
46#define IEEE80211_SCAN_INTERVAL (2 * HZ)
47#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
Dan Williams872ba532008-06-04 13:59:34 -040048#define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070049
50#define IEEE80211_PROBE_DELAY (HZ / 33)
51#define IEEE80211_CHANNEL_TIME (HZ / 33)
52#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
53#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
54#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
55#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010056#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070057
58#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
59
60
Jiri Bencf0706e82007-05-05 11:45:53 -070061#define ERP_INFO_USE_PROTECTION BIT(1)
62
Johannes Berg9c80d3d2008-09-08 15:41:59 +020063/* mgmt header + 1 byte category code */
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020064#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
65
66#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
67#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
68#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020069#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
70#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020071
Ron Rindjunsky07db2182007-12-25 17:00:33 +020072/* next values represent the buffer size for A-MPDU frame.
73 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
74#define IEEE80211_MIN_AMPDU_BUF 0x8
75#define IEEE80211_MAX_AMPDU_BUF 0x40
76
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120077static void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
Jiri Bencf0706e82007-05-05 11:45:53 -070078 u8 *ssid, size_t ssid_len);
79static struct ieee80211_sta_bss *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120080ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -040081 u8 *ssid, u8 ssid_len);
Johannes Berg3e122be2008-07-09 14:40:34 +020082static void ieee80211_rx_bss_put(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -070083 struct ieee80211_sta_bss *bss);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120084static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -070085 struct ieee80211_if_sta *ifsta);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120086static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata);
87static int ieee80211_sta_start_scan(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -070088 u8 *ssid, size_t ssid_len);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +120089static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -070090 struct ieee80211_if_sta *ifsta);
Adrian Bunk712590d2008-04-21 11:47:51 +030091static void sta_rx_agg_session_timer_expired(unsigned long data);
Jiri Bencf0706e82007-05-05 11:45:53 -070092
93
Jouni Malinen43ac2ca2008-08-15 22:21:27 +030094static u8 * ieee80211_bss_get_ie(struct ieee80211_sta_bss *bss, u8 ie)
95{
96 u8 *end, *pos;
97
98 pos = bss->ies;
99 if (pos == NULL)
100 return NULL;
101 end = pos + bss->ies_len;
102
103 while (pos + 1 < end) {
104 if (pos + 2 + pos[1] > end)
105 break;
106 if (pos[0] == ie)
107 return pos;
108 pos += 2 + pos[1];
109 }
110
111 return NULL;
112}
113
114
Jiri Bencf0706e82007-05-05 11:45:53 -0700115static int ecw2cw(int ecw)
116{
Johannes Bergac2bf322008-02-14 00:30:35 +0100117 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700118}
119
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100120
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200121static void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100122 struct ieee80211_sta_bss *bss,
123 int ibss)
124{
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100125 struct ieee80211_local *local = sdata->local;
126 int i, have_higher_than_11mbit = 0;
127
128
129 /* cf. IEEE 802.11 9.2.12 */
130 for (i = 0; i < bss->supp_rates_len; i++)
131 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
132 have_higher_than_11mbit = 1;
133
134 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
135 have_higher_than_11mbit)
136 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
137 else
138 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
139
140
141 if (local->ops->conf_tx) {
142 struct ieee80211_tx_queue_params qparam;
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100143
144 memset(&qparam, 0, sizeof(qparam));
145
146 qparam.aifs = 2;
147
148 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
149 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
150 qparam.cw_min = 31;
151 else
152 qparam.cw_min = 15;
153
154 qparam.cw_max = 1023;
155 qparam.txop = 0;
156
Johannes Berge100bb62008-04-30 18:51:21 +0200157 for (i = 0; i < local_to_hw(local)->queues; i++)
158 local->ops->conf_tx(local_to_hw(local), i, &qparam);
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100159 }
160}
161
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200162static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -0700163 struct ieee80211_if_sta *ifsta,
164 u8 *wmm_param, size_t wmm_param_len)
165{
Jiri Bencf0706e82007-05-05 11:45:53 -0700166 struct ieee80211_tx_queue_params params;
167 size_t left;
168 int count;
169 u8 *pos;
170
Johannes Berg3434fbd2008-05-03 00:59:37 +0200171 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
172 return;
173
174 if (!wmm_param)
175 return;
176
Jiri Bencf0706e82007-05-05 11:45:53 -0700177 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
178 return;
179 count = wmm_param[6] & 0x0f;
180 if (count == ifsta->wmm_last_param_set)
181 return;
182 ifsta->wmm_last_param_set = count;
183
184 pos = wmm_param + 8;
185 left = wmm_param_len - 8;
186
187 memset(&params, 0, sizeof(params));
188
189 if (!local->ops->conf_tx)
190 return;
191
192 local->wmm_acm = 0;
193 for (; left >= 4; left -= 4, pos += 4) {
194 int aci = (pos[0] >> 5) & 0x03;
195 int acm = (pos[0] >> 4) & 0x01;
196 int queue;
197
198 switch (aci) {
199 case 1:
Johannes Berge100bb62008-04-30 18:51:21 +0200200 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200201 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700202 local->wmm_acm |= BIT(0) | BIT(3);
Jiri Bencf0706e82007-05-05 11:45:53 -0700203 break;
204 case 2:
Johannes Berge100bb62008-04-30 18:51:21 +0200205 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200206 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700207 local->wmm_acm |= BIT(4) | BIT(5);
Jiri Bencf0706e82007-05-05 11:45:53 -0700208 break;
209 case 3:
Johannes Berge100bb62008-04-30 18:51:21 +0200210 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200211 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700212 local->wmm_acm |= BIT(6) | BIT(7);
Jiri Bencf0706e82007-05-05 11:45:53 -0700213 break;
214 case 0:
215 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200216 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200217 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700218 local->wmm_acm |= BIT(1) | BIT(2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700219 break;
220 }
221
222 params.aifs = pos[0] & 0x0f;
223 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
224 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Bergf434b2d2008-07-10 11:22:31 +0200225 params.txop = get_unaligned_le16(pos + 2);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200226#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700227 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100228 "cWmin=%d cWmax=%d txop=%d\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200229 local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100230 params.cw_max, params.txop);
231#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700232 /* TODO: handle ACM (block TX, fallback to next lowest allowed
233 * AC for now) */
234 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
235 printk(KERN_DEBUG "%s: failed to set TX queue "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200236 "parameters for queue %d\n", local->mdev->name, queue);
Jiri Bencf0706e82007-05-05 11:45:53 -0700237 }
238 }
239}
240
John W. Linville50c4afb2008-04-15 14:09:27 -0400241static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
242 bool use_protection,
243 bool use_short_preamble)
Daniel Drake56282212007-07-10 19:32:10 +0200244{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100245 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Tomas Winklerebd74482008-07-01 10:44:50 +0300246#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200247 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Joe Perches0795af52007-10-03 17:59:30 -0700248 DECLARE_MAC_BUF(mac);
Tomas Winklerebd74482008-07-01 10:44:50 +0300249#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100250 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200251
Johannes Berg471b3ef2007-12-28 14:32:58 +0100252 if (use_protection != bss_conf->use_cts_prot) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200253#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200254 if (net_ratelimit()) {
255 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700256 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100257 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200258 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700259 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200260 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200261#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100262 bss_conf->use_cts_prot = use_protection;
263 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200264 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200265
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200266 if (use_short_preamble != bss_conf->use_short_preamble) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200267#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake7e9ed182007-07-27 15:43:24 +0200268 if (net_ratelimit()) {
269 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700270 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100271 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200272 use_short_preamble ? "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700273 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200274 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200275#endif
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200276 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100277 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200278 }
Daniel Draked9430a32007-07-27 15:43:24 +0200279
Johannes Berg471b3ef2007-12-28 14:32:58 +0100280 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200281}
282
John W. Linville50c4afb2008-04-15 14:09:27 -0400283static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
284 u8 erp_value)
285{
286 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
287 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
288
289 return ieee80211_handle_protect_preamb(sdata,
290 use_protection, use_short_preamble);
291}
292
293static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
294 struct ieee80211_sta_bss *bss)
295{
296 u32 changed = 0;
297
298 if (bss->has_erp_value)
299 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
300 else {
301 u16 capab = bss->capability;
302 changed |= ieee80211_handle_protect_preamb(sdata, false,
303 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
304 }
305
306 return changed;
307}
308
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200309int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
310 struct ieee80211_ht_info *ht_info)
311{
312
313 if (ht_info == NULL)
314 return -EINVAL;
315
316 memset(ht_info, 0, sizeof(*ht_info));
317
318 if (ht_cap_ie) {
319 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
320
321 ht_info->ht_supported = 1;
322 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
323 ht_info->ampdu_factor =
324 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
325 ht_info->ampdu_density =
326 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
327 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
328 } else
329 ht_info->ht_supported = 0;
330
331 return 0;
332}
333
334int ieee80211_ht_addt_info_ie_to_ht_bss_info(
335 struct ieee80211_ht_addt_info *ht_add_info_ie,
336 struct ieee80211_ht_bss_info *bss_info)
337{
338 if (bss_info == NULL)
339 return -EINVAL;
340
341 memset(bss_info, 0, sizeof(*bss_info));
342
343 if (ht_add_info_ie) {
344 u16 op_mode;
345 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
346
347 bss_info->primary_channel = ht_add_info_ie->control_chan;
348 bss_info->bss_cap = ht_add_info_ie->ht_param;
349 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
350 }
351
352 return 0;
353}
Daniel Drake56282212007-07-10 19:32:10 +0200354
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200355static void ieee80211_sta_send_associnfo(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700356 struct ieee80211_if_sta *ifsta)
357{
Jiri Bencf0706e82007-05-05 11:45:53 -0700358 union iwreq_data wrqu;
359
Jiri Bencf0706e82007-05-05 11:45:53 -0700360 if (ifsta->assocreq_ies) {
Jouni Malinen087d8332008-08-19 10:54:32 +0300361 memset(&wrqu, 0, sizeof(wrqu));
362 wrqu.data.length = ifsta->assocreq_ies_len;
David S. Millerb171e192008-08-29 23:06:00 -0700363 wireless_send_event(sdata->dev, IWEVASSOCREQIE, &wrqu,
Jouni Malinen087d8332008-08-19 10:54:32 +0300364 ifsta->assocreq_ies);
Jiri Bencf0706e82007-05-05 11:45:53 -0700365 }
366 if (ifsta->assocresp_ies) {
Jouni Malinen087d8332008-08-19 10:54:32 +0300367 memset(&wrqu, 0, sizeof(wrqu));
368 wrqu.data.length = ifsta->assocresp_ies_len;
David S. Millerb171e192008-08-29 23:06:00 -0700369 wireless_send_event(sdata->dev, IWEVASSOCRESPIE, &wrqu,
Jouni Malinen087d8332008-08-19 10:54:32 +0300370 ifsta->assocresp_ies);
Jiri Bencf0706e82007-05-05 11:45:53 -0700371 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700372}
373
374
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200375static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400376 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200377 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700378{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100379 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700380 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700381 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100382 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700383
Jiri Bencf0706e82007-05-05 11:45:53 -0700384 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200385 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400386
387 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
388
Johannes Berg51fb61e2007-12-19 01:31:27 +0100389 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700390 return;
Daniel Drake56282212007-07-10 19:32:10 +0200391
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200392 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
Tomas Winkler38668c02008-03-28 16:33:32 -0700393 conf->channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400394 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200395 if (bss) {
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700396 /* set timing information */
397 sdata->bss_conf.beacon_int = bss->beacon_int;
398 sdata->bss_conf.timestamp = bss->timestamp;
Emmanuel Grumbach98f7dfd2008-07-18 13:52:59 +0800399 sdata->bss_conf.dtim_period = bss->dtim_period;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700400
John W. Linville50c4afb2008-04-15 14:09:27 -0400401 changed |= ieee80211_handle_bss_capability(sdata, bss);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700402
Johannes Berg3e122be2008-07-09 14:40:34 +0200403 ieee80211_rx_bss_put(local, bss);
Daniel Drake56282212007-07-10 19:32:10 +0200404 }
405
Tomas Winkler38668c02008-03-28 16:33:32 -0700406 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
407 changed |= BSS_CHANGED_HT;
408 sdata->bss_conf.assoc_ht = 1;
409 sdata->bss_conf.ht_conf = &conf->ht_conf;
410 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
411 }
412
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400413 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700414 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
415 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200416 ieee80211_sta_send_associnfo(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700417 } else {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200418 netif_carrier_off(sdata->dev);
419 ieee80211_sta_tear_down_BA_sessions(sdata, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400420 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200421 changed |= ieee80211_reset_erp_info(sdata);
Tomas Winkler38668c02008-03-28 16:33:32 -0700422
423 sdata->bss_conf.assoc_ht = 0;
424 sdata->bss_conf.ht_conf = NULL;
425 sdata->bss_conf.ht_bss_conf = NULL;
426
Jiri Bencf0706e82007-05-05 11:45:53 -0700427 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
428 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700429 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200430 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100431
Tomas Winklerb2205252008-01-27 16:18:22 +0200432 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100433 ieee80211_bss_info_change_notify(sdata, changed);
Guy Cohen8db93692008-07-03 19:56:13 +0300434
435 if (assoc)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200436 netif_carrier_on(sdata->dev);
Guy Cohen8db93692008-07-03 19:56:13 +0300437
Reinette Chatre41a7be42008-04-03 16:08:49 -0700438 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200439 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700440}
441
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200442static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700443 struct ieee80211_if_sta *ifsta, int deauth)
444{
Ron Rindjunsky9859b812008-08-09 03:02:19 +0300445 if (deauth) {
446 ifsta->direct_probe_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700447 ifsta->auth_tries = 0;
Ron Rindjunsky9859b812008-08-09 03:02:19 +0300448 }
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +0300449 ifsta->assoc_scan_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700450 ifsta->assoc_tries = 0;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200451 ieee80211_set_associated(sdata, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700452}
453
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200454void ieee80211_sta_tx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
Luis Carlos Coboee385852008-02-23 15:17:11 +0100455 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700456{
Jiri Bencf0706e82007-05-05 11:45:53 -0700457 skb->dev = sdata->local->mdev;
458 skb_set_mac_header(skb, 0);
459 skb_set_network_header(skb, 0);
460 skb_set_transport_header(skb, 0);
461
Johannes Bergd0f09802008-07-29 11:32:07 +0200462 skb->iif = sdata->dev->ifindex;
463 skb->do_not_encrypt = !encrypt;
Jiri Bencf0706e82007-05-05 11:45:53 -0700464
465 dev_queue_xmit(skb);
466}
467
468
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200469static void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700470 struct ieee80211_if_sta *ifsta,
471 int transaction, u8 *extra, size_t extra_len,
472 int encrypt)
473{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200474 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700475 struct sk_buff *skb;
476 struct ieee80211_mgmt *mgmt;
477
478 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
479 sizeof(*mgmt) + 6 + extra_len);
480 if (!skb) {
481 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200482 "frame\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -0700483 return;
484 }
485 skb_reserve(skb, local->hw.extra_tx_headroom);
486
487 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
488 memset(mgmt, 0, 24 + 6);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700489 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
490 IEEE80211_STYPE_AUTH);
Jiri Bencf0706e82007-05-05 11:45:53 -0700491 if (encrypt)
492 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
493 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200494 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700495 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
496 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
497 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
498 ifsta->auth_transaction = transaction + 1;
499 mgmt->u.auth.status_code = cpu_to_le16(0);
500 if (extra)
501 memcpy(skb_put(skb, extra_len), extra, extra_len);
502
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200503 ieee80211_sta_tx(sdata, skb, encrypt);
Jiri Bencf0706e82007-05-05 11:45:53 -0700504}
505
Ron Rindjunsky9859b812008-08-09 03:02:19 +0300506static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata,
507 struct ieee80211_if_sta *ifsta)
508{
509 DECLARE_MAC_BUF(mac);
510
511 ifsta->direct_probe_tries++;
512 if (ifsta->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
513 printk(KERN_DEBUG "%s: direct probe to AP %s timed out\n",
514 sdata->dev->name, print_mac(mac, ifsta->bssid));
515 ifsta->state = IEEE80211_STA_MLME_DISABLED;
516 return;
517 }
518
519 printk(KERN_DEBUG "%s: direct probe to AP %s try %d\n",
520 sdata->dev->name, print_mac(mac, ifsta->bssid),
521 ifsta->direct_probe_tries);
522
523 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
524
525 set_bit(IEEE80211_STA_REQ_DIRECT_PROBE, &ifsta->request);
526
527 /* Direct probe is sent to broadcast address as some APs
528 * will not answer to direct packet in unassociated state.
529 */
530 ieee80211_send_probe_req(sdata, NULL,
531 ifsta->ssid, ifsta->ssid_len);
532
533 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
534}
535
Jiri Bencf0706e82007-05-05 11:45:53 -0700536
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200537static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700538 struct ieee80211_if_sta *ifsta)
539{
Joe Perches0795af52007-10-03 17:59:30 -0700540 DECLARE_MAC_BUF(mac);
541
Jiri Bencf0706e82007-05-05 11:45:53 -0700542 ifsta->auth_tries++;
543 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700544 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700545 " timed out\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200546 sdata->dev->name, print_mac(mac, ifsta->bssid));
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300547 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700548 return;
549 }
550
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300551 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700552 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200553 sdata->dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700554
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200555 ieee80211_send_auth(sdata, ifsta, 1, NULL, 0, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700556
557 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
558}
559
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200560static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
561 struct ieee80211_supported_band *sband,
562 u64 *rates)
563{
564 int i, j, count;
565 *rates = 0;
566 count = 0;
567 for (i = 0; i < bss->supp_rates_len; i++) {
568 int rate = (bss->supp_rates[i] & 0x7F) * 5;
569
570 for (j = 0; j < sband->n_bitrates; j++)
571 if (sband->bitrates[j].bitrate == rate) {
572 *rates |= BIT(j);
573 count++;
574 break;
575 }
576 }
577
578 return count;
579}
Jiri Bencf0706e82007-05-05 11:45:53 -0700580
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200581static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700582 struct ieee80211_if_sta *ifsta)
583{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200584 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700585 struct sk_buff *skb;
586 struct ieee80211_mgmt *mgmt;
Jouni Malinen43ac2ca2008-08-15 22:21:27 +0300587 u8 *pos, *ies, *ht_add_ie;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200588 int i, len, count, rates_len, supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700589 u16 capab;
590 struct ieee80211_sta_bss *bss;
591 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100592 struct ieee80211_supported_band *sband;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200593 u64 rates = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700594
595 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
596 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
597 ifsta->ssid_len);
598 if (!skb) {
599 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200600 "frame\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -0700601 return;
602 }
603 skb_reserve(skb, local->hw.extra_tx_headroom);
604
Johannes Berg8318d782008-01-24 19:38:38 +0100605 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
606
Jiri Bencf0706e82007-05-05 11:45:53 -0700607 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100608
609 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
610 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
611 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
612 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
613 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700614 }
Johannes Berg8318d782008-01-24 19:38:38 +0100615
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200616 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +0100617 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400618 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700619 if (bss) {
620 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
621 capab |= WLAN_CAPABILITY_PRIVACY;
Jouni Malinen43ac2ca2008-08-15 22:21:27 +0300622 if (bss->wmm_used)
Jiri Bencf0706e82007-05-05 11:45:53 -0700623 wmm = 1;
Helmut Schaa0d580a72008-05-20 09:56:37 +0200624
625 /* get all rates supported by the device and the AP as
626 * some APs don't like getting a superset of their rates
627 * in the association request (e.g. D-Link DAP 1353 in
628 * b-only mode) */
629 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
630
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300631 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
632 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
633 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
634
Johannes Berg3e122be2008-07-09 14:40:34 +0200635 ieee80211_rx_bss_put(local, bss);
Helmut Schaa0d580a72008-05-20 09:56:37 +0200636 } else {
637 rates = ~0;
638 rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700639 }
640
641 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
642 memset(mgmt, 0, 24);
643 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200644 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700645 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
646
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400647 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700648 skb_put(skb, 10);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700649 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
650 IEEE80211_STYPE_REASSOC_REQ);
Jiri Bencf0706e82007-05-05 11:45:53 -0700651 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
Tomas Winklerea95bba2008-07-18 13:53:00 +0800652 mgmt->u.reassoc_req.listen_interval =
653 cpu_to_le16(local->hw.conf.listen_interval);
Jiri Bencf0706e82007-05-05 11:45:53 -0700654 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
655 ETH_ALEN);
656 } else {
657 skb_put(skb, 4);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700658 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
659 IEEE80211_STYPE_ASSOC_REQ);
Jiri Bencf0706e82007-05-05 11:45:53 -0700660 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
Tomas Winklerea95bba2008-07-18 13:53:00 +0800661 mgmt->u.reassoc_req.listen_interval =
662 cpu_to_le16(local->hw.conf.listen_interval);
Jiri Bencf0706e82007-05-05 11:45:53 -0700663 }
664
665 /* SSID */
666 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
667 *pos++ = WLAN_EID_SSID;
668 *pos++ = ifsta->ssid_len;
669 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
670
Helmut Schaa0d580a72008-05-20 09:56:37 +0200671 /* add all rates which were marked to be used above */
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200672 supp_rates_len = rates_len;
673 if (supp_rates_len > 8)
674 supp_rates_len = 8;
Jiri Bencf0706e82007-05-05 11:45:53 -0700675
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200676 len = sband->n_bitrates;
677 pos = skb_put(skb, supp_rates_len + 2);
678 *pos++ = WLAN_EID_SUPP_RATES;
679 *pos++ = supp_rates_len;
680
681 count = 0;
682 for (i = 0; i < sband->n_bitrates; i++) {
683 if (BIT(i) & rates) {
Johannes Berg8318d782008-01-24 19:38:38 +0100684 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700685 *pos++ = (u8) (rate / 5);
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200686 if (++count == 8)
687 break;
688 }
689 }
690
Jan-Espen Pettersen8ab65b02008-08-25 20:29:22 +0200691 if (rates_len > count) {
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200692 pos = skb_put(skb, rates_len - count + 2);
693 *pos++ = WLAN_EID_EXT_SUPP_RATES;
694 *pos++ = rates_len - count;
695
696 for (i++; i < sband->n_bitrates; i++) {
697 if (BIT(i) & rates) {
698 int rate = sband->bitrates[i].bitrate;
699 *pos++ = (u8) (rate / 5);
700 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700701 }
702 }
703
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300704 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
705 /* 1. power capabilities */
706 pos = skb_put(skb, 4);
707 *pos++ = WLAN_EID_PWR_CAPABILITY;
708 *pos++ = 2;
709 *pos++ = 0; /* min tx power */
710 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
711
712 /* 2. supported channels */
713 /* TODO: get this in reg domain format */
714 pos = skb_put(skb, 2 * sband->n_channels + 2);
715 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
716 *pos++ = 2 * sband->n_channels;
717 for (i = 0; i < sband->n_channels; i++) {
718 *pos++ = ieee80211_frequency_to_channel(
719 sband->channels[i].center_freq);
720 *pos++ = 1; /* one channel in the subband*/
721 }
722 }
723
Jiri Bencf0706e82007-05-05 11:45:53 -0700724 if (ifsta->extra_ie) {
725 pos = skb_put(skb, ifsta->extra_ie_len);
726 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
727 }
728
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400729 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700730 pos = skb_put(skb, 9);
731 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
732 *pos++ = 7; /* len */
733 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
734 *pos++ = 0x50;
735 *pos++ = 0xf2;
736 *pos++ = 2; /* WME */
737 *pos++ = 0; /* WME info */
738 *pos++ = 1; /* WME ver */
739 *pos++ = 0;
740 }
Johannes Berg3434fbd2008-05-03 00:59:37 +0200741
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200742 /* wmm support is a must to HT */
Johannes Berg3434fbd2008-05-03 00:59:37 +0200743 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
Jouni Malinen43ac2ca2008-08-15 22:21:27 +0300744 sband->ht_info.ht_supported &&
745 (ht_add_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_EXTRA_INFO))) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +0800746 struct ieee80211_ht_addt_info *ht_add_info =
Jouni Malinen43ac2ca2008-08-15 22:21:27 +0300747 (struct ieee80211_ht_addt_info *)ht_add_ie;
Emmanuel Grumbach93061022008-05-29 16:35:23 +0800748 u16 cap = sband->ht_info.cap;
749 __le16 tmp;
750 u32 flags = local->hw.conf.channel->flags;
751
752 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
753 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
754 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
755 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
756 cap &= ~IEEE80211_HT_CAP_SGI_40;
757 }
758 break;
759 case IEEE80211_HT_IE_CHA_SEC_BELOW:
760 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
761 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
762 cap &= ~IEEE80211_HT_CAP_SGI_40;
763 }
764 break;
765 }
766
767 tmp = cpu_to_le16(cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200768 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
769 *pos++ = WLAN_EID_HT_CAPABILITY;
770 *pos++ = sizeof(struct ieee80211_ht_cap);
771 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
772 memcpy(pos, &tmp, sizeof(u16));
773 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100774 /* TODO: needs a define here for << 2 */
775 *pos++ = sband->ht_info.ampdu_factor |
776 (sband->ht_info.ampdu_density << 2);
777 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200778 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700779
780 kfree(ifsta->assocreq_ies);
781 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200782 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700783 if (ifsta->assocreq_ies)
784 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
785
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200786 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700787}
788
789
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200790static void ieee80211_send_deauth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700791 struct ieee80211_if_sta *ifsta, u16 reason)
792{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200793 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700794 struct sk_buff *skb;
795 struct ieee80211_mgmt *mgmt;
796
797 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
798 if (!skb) {
799 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200800 "frame\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -0700801 return;
802 }
803 skb_reserve(skb, local->hw.extra_tx_headroom);
804
805 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
806 memset(mgmt, 0, 24);
807 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200808 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700809 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700810 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
811 IEEE80211_STYPE_DEAUTH);
Jiri Bencf0706e82007-05-05 11:45:53 -0700812 skb_put(skb, 2);
813 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
814
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200815 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700816}
817
818
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200819static void ieee80211_send_disassoc(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700820 struct ieee80211_if_sta *ifsta, u16 reason)
821{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200822 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700823 struct sk_buff *skb;
824 struct ieee80211_mgmt *mgmt;
825
826 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
827 if (!skb) {
828 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200829 "frame\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -0700830 return;
831 }
832 skb_reserve(skb, local->hw.extra_tx_headroom);
833
834 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
835 memset(mgmt, 0, 24);
836 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200837 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700838 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700839 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
840 IEEE80211_STYPE_DISASSOC);
Jiri Bencf0706e82007-05-05 11:45:53 -0700841 skb_put(skb, 2);
842 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
843
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200844 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700845}
846
847
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200848static int ieee80211_privacy_mismatch(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700849 struct ieee80211_if_sta *ifsta)
850{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200851 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700852 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000853 int bss_privacy;
854 int wep_privacy;
855 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700856
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000857 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700858 return 0;
859
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200860 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +0100861 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400862 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700863 if (!bss)
864 return 0;
865
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000866 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200867 wep_privacy = !!ieee80211_sta_wep_configured(sdata);
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000868 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700869
Johannes Berg3e122be2008-07-09 14:40:34 +0200870 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -0700871
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000872 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
873 return 0;
874
875 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700876}
877
878
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200879static void ieee80211_associate(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700880 struct ieee80211_if_sta *ifsta)
881{
Joe Perches0795af52007-10-03 17:59:30 -0700882 DECLARE_MAC_BUF(mac);
883
Jiri Bencf0706e82007-05-05 11:45:53 -0700884 ifsta->assoc_tries++;
885 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700886 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700887 " timed out\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200888 sdata->dev->name, print_mac(mac, ifsta->bssid));
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300889 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700890 return;
891 }
892
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300893 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700894 printk(KERN_DEBUG "%s: associate with AP %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200895 sdata->dev->name, print_mac(mac, ifsta->bssid));
896 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700897 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200898 "mixed-cell disabled - abort association\n", sdata->dev->name);
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300899 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700900 return;
901 }
902
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200903 ieee80211_send_assoc(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700904
905 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
906}
907
908
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200909static void ieee80211_associated(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -0700910 struct ieee80211_if_sta *ifsta)
911{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200912 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700913 struct sta_info *sta;
914 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700915 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700916
917 /* TODO: start monitoring current AP signal quality and number of
918 * missed beacons. Scan other channels every now and then and search
919 * for better APs. */
920 /* TODO: remove expired BSSes */
921
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300922 ifsta->state = IEEE80211_STA_MLME_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700923
Johannes Bergd0709a62008-02-25 16:27:46 +0100924 rcu_read_lock();
925
Jiri Bencf0706e82007-05-05 11:45:53 -0700926 sta = sta_info_get(local, ifsta->bssid);
927 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700928 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200929 sdata->dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700930 disassoc = 1;
931 } else {
932 disassoc = 0;
933 if (time_after(jiffies,
934 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400935 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700936 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700937 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -0700938 "range\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200939 sdata->dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700940 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +0100941 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400942 } else
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200943 ieee80211_send_probe_req(sdata, ifsta->bssid,
Jiri Bencf0706e82007-05-05 11:45:53 -0700944 local->scan_ssid,
945 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400946 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700947 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400948 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700949 if (time_after(jiffies, ifsta->last_probe +
950 IEEE80211_PROBE_INTERVAL)) {
951 ifsta->last_probe = jiffies;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200952 ieee80211_send_probe_req(sdata, ifsta->bssid,
Jiri Bencf0706e82007-05-05 11:45:53 -0700953 ifsta->ssid,
954 ifsta->ssid_len);
955 }
956 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700957 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100958
959 rcu_read_unlock();
960
Johannes Berg3b967662008-04-08 17:56:52 +0200961 if (disassoc && sta)
Johannes Bergd0709a62008-02-25 16:27:46 +0100962 sta_info_destroy(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100963
Jiri Bencf0706e82007-05-05 11:45:53 -0700964 if (disassoc) {
Tomas Winkler48c2fc52008-08-06 14:22:01 +0300965 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200966 ieee80211_set_associated(sdata, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700967 } else {
968 mod_timer(&ifsta->timer, jiffies +
969 IEEE80211_MONITORING_INTERVAL);
970 }
971}
972
973
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200974static void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
Jiri Bencf0706e82007-05-05 11:45:53 -0700975 u8 *ssid, size_t ssid_len)
976{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200977 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +0100978 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700979 struct sk_buff *skb;
980 struct ieee80211_mgmt *mgmt;
981 u8 *pos, *supp_rates, *esupp_rates = NULL;
982 int i;
983
984 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
985 if (!skb) {
986 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200987 "request\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -0700988 return;
989 }
990 skb_reserve(skb, local->hw.extra_tx_headroom);
991
992 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
993 memset(mgmt, 0, 24);
Harvey Harrisone7827a72008-07-15 18:44:13 -0700994 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
995 IEEE80211_STYPE_PROBE_REQ);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +1200996 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Jiri Bencf0706e82007-05-05 11:45:53 -0700997 if (dst) {
998 memcpy(mgmt->da, dst, ETH_ALEN);
999 memcpy(mgmt->bssid, dst, ETH_ALEN);
1000 } else {
1001 memset(mgmt->da, 0xff, ETH_ALEN);
1002 memset(mgmt->bssid, 0xff, ETH_ALEN);
1003 }
1004 pos = skb_put(skb, 2 + ssid_len);
1005 *pos++ = WLAN_EID_SSID;
1006 *pos++ = ssid_len;
1007 memcpy(pos, ssid, ssid_len);
1008
1009 supp_rates = skb_put(skb, 2);
1010 supp_rates[0] = WLAN_EID_SUPP_RATES;
1011 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001012 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1013
1014 for (i = 0; i < sband->n_bitrates; i++) {
1015 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07001016 if (esupp_rates) {
1017 pos = skb_put(skb, 1);
1018 esupp_rates[1]++;
1019 } else if (supp_rates[1] == 8) {
1020 esupp_rates = skb_put(skb, 3);
1021 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1022 esupp_rates[1] = 1;
1023 pos = &esupp_rates[2];
1024 } else {
1025 pos = skb_put(skb, 1);
1026 supp_rates[1]++;
1027 }
Johannes Berg8318d782008-01-24 19:38:38 +01001028 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -07001029 }
1030
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001031 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001032}
1033
1034
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001035static int ieee80211_sta_wep_configured(struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -07001036{
Jiri Bencf0706e82007-05-05 11:45:53 -07001037 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001038 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -07001039 return 0;
1040 return 1;
1041}
1042
1043
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001044static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001045 struct ieee80211_if_sta *ifsta)
1046{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001047 printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001048 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001049 ieee80211_associate(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001050}
1051
1052
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001053static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001054 struct ieee80211_if_sta *ifsta,
1055 struct ieee80211_mgmt *mgmt,
1056 size_t len)
1057{
1058 u8 *pos;
1059 struct ieee802_11_elems elems;
1060
Jiri Bencf0706e82007-05-05 11:45:53 -07001061 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001062 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001063 if (!elems.challenge)
Jiri Bencf0706e82007-05-05 11:45:53 -07001064 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001065 ieee80211_send_auth(sdata, ifsta, 3, elems.challenge - 2,
Jiri Bencf0706e82007-05-05 11:45:53 -07001066 elems.challenge_len + 2, 1);
1067}
1068
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001069static void ieee80211_send_addba_resp(struct ieee80211_sub_if_data *sdata, u8 *da, u16 tid,
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001070 u8 dialog_token, u16 status, u16 policy,
1071 u16 buf_size, u16 timeout)
1072{
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001073 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001074 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001075 struct sk_buff *skb;
1076 struct ieee80211_mgmt *mgmt;
1077 u16 capab;
1078
Ester Kummer3acea5b2008-04-17 16:05:14 -07001079 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1080
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001081 if (!skb) {
1082 printk(KERN_DEBUG "%s: failed to allocate buffer "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001083 "for addba resp frame\n", sdata->dev->name);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001084 return;
1085 }
1086
1087 skb_reserve(skb, local->hw.extra_tx_headroom);
1088 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1089 memset(mgmt, 0, 24);
1090 memcpy(mgmt->da, da, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001091 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001092 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001093 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001094 else
1095 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
Harvey Harrisone7827a72008-07-15 18:44:13 -07001096 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1097 IEEE80211_STYPE_ACTION);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001098
1099 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1100 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1101 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1102 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1103
1104 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1105 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1106 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1107
1108 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1109 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1110 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1111
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001112 ieee80211_sta_tx(sdata, skb, 0);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001113
1114 return;
1115}
1116
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001117void ieee80211_send_addba_request(struct ieee80211_sub_if_data *sdata, const u8 *da,
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001118 u16 tid, u8 dialog_token, u16 start_seq_num,
1119 u16 agg_size, u16 timeout)
1120{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001121 struct ieee80211_local *local = sdata->local;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001122 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1123 struct sk_buff *skb;
1124 struct ieee80211_mgmt *mgmt;
1125 u16 capab;
1126
Ester Kummer3acea5b2008-04-17 16:05:14 -07001127 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001128
1129 if (!skb) {
1130 printk(KERN_ERR "%s: failed to allocate buffer "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001131 "for addba request frame\n", sdata->dev->name);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001132 return;
1133 }
1134 skb_reserve(skb, local->hw.extra_tx_headroom);
1135 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1136 memset(mgmt, 0, 24);
1137 memcpy(mgmt->da, da, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001138 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001139 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001140 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001141 else
1142 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1143
Harvey Harrisone7827a72008-07-15 18:44:13 -07001144 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1145 IEEE80211_STYPE_ACTION);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001146
1147 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1148
1149 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1150 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1151
1152 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1153 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1154 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1155 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1156
1157 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1158
1159 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1160 mgmt->u.action.u.addba_req.start_seq_num =
1161 cpu_to_le16(start_seq_num << 4);
1162
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001163 ieee80211_sta_tx(sdata, skb, 0);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001164}
1165
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001166static void ieee80211_sta_process_addba_request(struct ieee80211_local *local,
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001167 struct ieee80211_mgmt *mgmt,
1168 size_t len)
1169{
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001170 struct ieee80211_hw *hw = &local->hw;
1171 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001172 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001173 struct tid_ampdu_rx *tid_agg_rx;
1174 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001175 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001176 int ret = -EOPNOTSUPP;
1177 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001178
Johannes Bergd0709a62008-02-25 16:27:46 +01001179 rcu_read_lock();
1180
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001181 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001182 if (!sta) {
1183 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001184 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001185 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001186
1187 /* extract session parameters from addba request frame */
1188 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1189 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001190 start_seq_num =
1191 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001192
1193 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1194 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1195 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1196 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1197
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001198 status = WLAN_STATUS_REQUEST_DECLINED;
1199
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001200 /* sanity check for incoming parameters:
1201 * check if configuration can support the BA policy
1202 * and if buffer size does not exceeds max value */
1203 if (((ba_policy != 1)
1204 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1205 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1206 status = WLAN_STATUS_INVALID_QOS_PARAM;
1207#ifdef CONFIG_MAC80211_HT_DEBUG
1208 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001209 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001210 "%s on tid %u. policy %d, buffer size %d\n",
1211 print_mac(mac, mgmt->sa), tid, ba_policy,
1212 buf_size);
1213#endif /* CONFIG_MAC80211_HT_DEBUG */
1214 goto end_no_lock;
1215 }
1216 /* determine default buffer size */
1217 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001218 struct ieee80211_supported_band *sband;
1219
1220 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001221 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001222 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001223 }
1224
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001225
1226 /* examine state machine */
Johannes Berg07346f812008-05-03 01:02:02 +02001227 spin_lock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001228
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001229 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001230#ifdef CONFIG_MAC80211_HT_DEBUG
1231 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001232 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001233 "%s on tid %u\n",
1234 print_mac(mac, mgmt->sa), tid);
1235#endif /* CONFIG_MAC80211_HT_DEBUG */
1236 goto end;
1237 }
1238
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001239 /* prepare A-MPDU MLME for Rx aggregation */
1240 sta->ampdu_mlme.tid_rx[tid] =
1241 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1242 if (!sta->ampdu_mlme.tid_rx[tid]) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001243#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001244 if (net_ratelimit())
1245 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1246 tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001247#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001248 goto end;
1249 }
1250 /* rx timer */
1251 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1252 sta_rx_agg_session_timer_expired;
1253 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1254 (unsigned long)&sta->timer_to_tid[tid];
1255 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1256
1257 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1258
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001259 /* prepare reordering buffer */
1260 tid_agg_rx->reorder_buf =
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301261 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001262 if (!tid_agg_rx->reorder_buf) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001263#ifdef CONFIG_MAC80211_HT_DEBUG
Johannes Berg03147df2008-02-26 00:39:28 +01001264 if (net_ratelimit())
1265 printk(KERN_ERR "can not allocate reordering buffer "
1266 "to tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001267#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001268 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001269 goto end;
1270 }
1271 memset(tid_agg_rx->reorder_buf, 0,
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301272 buf_size * sizeof(struct sk_buff *));
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001273
1274 if (local->ops->ampdu_action)
1275 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001276 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001277#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001278 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001279#endif /* CONFIG_MAC80211_HT_DEBUG */
1280
1281 if (ret) {
1282 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001283 kfree(tid_agg_rx);
1284 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001285 goto end;
1286 }
1287
1288 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001289 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001290 tid_agg_rx->dialog_token = dialog_token;
1291 tid_agg_rx->ssn = start_seq_num;
1292 tid_agg_rx->head_seq_num = start_seq_num;
1293 tid_agg_rx->buf_size = buf_size;
1294 tid_agg_rx->timeout = timeout;
1295 tid_agg_rx->stored_mpdu_num = 0;
1296 status = WLAN_STATUS_SUCCESS;
1297end:
Johannes Berg07346f812008-05-03 01:02:02 +02001298 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001299
1300end_no_lock:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001301 ieee80211_send_addba_resp(sta->sdata, sta->addr, tid,
Johannes Bergd0709a62008-02-25 16:27:46 +01001302 dialog_token, status, 1, buf_size, timeout);
1303 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001304}
Jiri Bencf0706e82007-05-05 11:45:53 -07001305
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001306static void ieee80211_sta_process_addba_resp(struct ieee80211_local *local,
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001307 struct ieee80211_mgmt *mgmt,
1308 size_t len)
1309{
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001310 struct ieee80211_hw *hw = &local->hw;
1311 struct sta_info *sta;
1312 u16 capab;
1313 u16 tid;
1314 u8 *state;
1315
Johannes Bergd0709a62008-02-25 16:27:46 +01001316 rcu_read_lock();
1317
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001318 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001319 if (!sta) {
1320 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001321 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001322 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001323
1324 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1325 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1326
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001327 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001328
Johannes Berg07346f812008-05-03 01:02:02 +02001329 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001330
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001331 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001332 spin_unlock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001333 goto addba_resp_exit;
1334 }
1335
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001336 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001337 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Johannes Berg07346f812008-05-03 01:02:02 +02001338 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001339#ifdef CONFIG_MAC80211_HT_DEBUG
1340 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1341#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001342 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001343 }
1344
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001345 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001346#ifdef CONFIG_MAC80211_HT_DEBUG
1347 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1348#endif /* CONFIG_MAC80211_HT_DEBUG */
1349 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1350 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001351 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001352 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001353
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001354 if (*state == HT_AGG_STATE_OPERATIONAL)
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001355 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001356
Johannes Berg07346f812008-05-03 01:02:02 +02001357 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001358 } else {
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001359 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001360 /* this will allow the state check in stop_BA_session */
1361 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001362 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001363 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1364 WLAN_BACK_INITIATOR);
1365 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001366
1367addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001368 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001369}
1370
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001371void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata, const u8 *da, u16 tid,
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001372 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001373{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001374 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001375 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1376 struct sk_buff *skb;
1377 struct ieee80211_mgmt *mgmt;
1378 u16 params;
1379
Ester Kummer3acea5b2008-04-17 16:05:14 -07001380 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001381
1382 if (!skb) {
1383 printk(KERN_ERR "%s: failed to allocate buffer "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001384 "for delba frame\n", sdata->dev->name);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001385 return;
1386 }
1387
1388 skb_reserve(skb, local->hw.extra_tx_headroom);
1389 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1390 memset(mgmt, 0, 24);
1391 memcpy(mgmt->da, da, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001392 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001393 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001394 memcpy(mgmt->bssid, sdata->dev->dev_addr, ETH_ALEN);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001395 else
1396 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
Harvey Harrisone7827a72008-07-15 18:44:13 -07001397 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1398 IEEE80211_STYPE_ACTION);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001399
1400 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1401
1402 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1403 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1404 params = (u16)(initiator << 11); /* bit 11 initiator */
1405 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1406
1407 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1408 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1409
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001410 ieee80211_sta_tx(sdata, skb, 0);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001411}
1412
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001413void ieee80211_send_bar(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid, u16 ssn)
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001414{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001415 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001416 struct sk_buff *skb;
1417 struct ieee80211_bar *bar;
1418 u16 bar_control = 0;
1419
1420 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
1421 if (!skb) {
1422 printk(KERN_ERR "%s: failed to allocate buffer for "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001423 "bar frame\n", sdata->dev->name);
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001424 return;
1425 }
1426 skb_reserve(skb, local->hw.extra_tx_headroom);
1427 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
1428 memset(bar, 0, sizeof(*bar));
Harvey Harrisone7827a72008-07-15 18:44:13 -07001429 bar->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
1430 IEEE80211_STYPE_BACK_REQ);
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001431 memcpy(bar->ra, ra, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001432 memcpy(bar->ta, sdata->dev->dev_addr, ETH_ALEN);
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001433 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
1434 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
1435 bar_control |= (u16)(tid << 12);
1436 bar->control = cpu_to_le16(bar_control);
1437 bar->start_seq_num = cpu_to_le16(ssn);
1438
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001439 ieee80211_sta_tx(sdata, skb, 0);
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001440}
1441
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001442void ieee80211_sta_stop_rx_ba_session(struct ieee80211_sub_if_data *sdata, u8 *ra, u16 tid,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001443 u16 initiator, u16 reason)
1444{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001445 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001446 struct ieee80211_hw *hw = &local->hw;
1447 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001448 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001449 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001450
Johannes Bergd0709a62008-02-25 16:27:46 +01001451 rcu_read_lock();
1452
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001453 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001454 if (!sta) {
1455 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001456 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001457 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001458
1459 /* check if TID is in operational state */
Johannes Berg07346f812008-05-03 01:02:02 +02001460 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001461 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001462 != HT_AGG_STATE_OPERATIONAL) {
Johannes Berg07346f812008-05-03 01:02:02 +02001463 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +01001464 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001465 return;
1466 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001467 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001468 HT_AGG_STATE_REQ_STOP_BA_MSK |
1469 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Johannes Berg07346f812008-05-03 01:02:02 +02001470 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001471
1472 /* stop HW Rx aggregation. ampdu_action existence
1473 * already verified in session init so we add the BUG_ON */
1474 BUG_ON(!local->ops->ampdu_action);
1475
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001476#ifdef CONFIG_MAC80211_HT_DEBUG
1477 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1478 print_mac(mac, ra), tid);
1479#endif /* CONFIG_MAC80211_HT_DEBUG */
1480
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001481 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001482 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001483 if (ret)
1484 printk(KERN_DEBUG "HW problem - can not stop rx "
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001485 "aggregation for tid %d\n", tid);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001486
1487 /* shutdown timer has not expired */
1488 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001489 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001490
1491 /* check if this is a self generated aggregation halt */
1492 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001493 ieee80211_send_delba(sdata, ra, tid, 0, reason);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001494
1495 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001496 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1497 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001498 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001499 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1500 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1501 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001502 }
1503 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001504 /* free resources */
1505 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1506 kfree(sta->ampdu_mlme.tid_rx[tid]);
1507 sta->ampdu_mlme.tid_rx[tid] = NULL;
1508 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001509
Johannes Bergd0709a62008-02-25 16:27:46 +01001510 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001511}
1512
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001513
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001514static void ieee80211_sta_process_delba(struct ieee80211_sub_if_data *sdata,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001515 struct ieee80211_mgmt *mgmt, size_t len)
1516{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001517 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001518 struct sta_info *sta;
1519 u16 tid, params;
1520 u16 initiator;
1521 DECLARE_MAC_BUF(mac);
1522
Johannes Bergd0709a62008-02-25 16:27:46 +01001523 rcu_read_lock();
1524
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001525 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001526 if (!sta) {
1527 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001528 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001529 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001530
1531 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1532 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1533 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1534
1535#ifdef CONFIG_MAC80211_HT_DEBUG
1536 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001537 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1538 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001539 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001540 mgmt->u.action.u.delba.reason_code);
1541#endif /* CONFIG_MAC80211_HT_DEBUG */
1542
1543 if (initiator == WLAN_BACK_INITIATOR)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001544 ieee80211_sta_stop_rx_ba_session(sdata, sta->addr, tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001545 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001546 else { /* WLAN_BACK_RECIPIENT */
Johannes Berg07346f812008-05-03 01:02:02 +02001547 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001548 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001549 HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001550 spin_unlock_bh(&sta->lock);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001551 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1552 WLAN_BACK_RECIPIENT);
1553 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001554 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001555}
1556
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001557/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001558 * After sending add Block Ack request we activated a timer until
1559 * add Block Ack response will arrive from the recipient.
1560 * If this timer expires sta_addba_resp_timer_expired will be executed.
1561 */
1562void sta_addba_resp_timer_expired(unsigned long data)
1563{
1564 /* not an elegant detour, but there is no choice as the timer passes
1565 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001566 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001567 * array gives the sta through container_of */
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301568 u16 tid = *(u8 *)data;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001569 struct sta_info *temp_sta = container_of((void *)data,
1570 struct sta_info, timer_to_tid[tid]);
1571
1572 struct ieee80211_local *local = temp_sta->local;
1573 struct ieee80211_hw *hw = &local->hw;
1574 struct sta_info *sta;
1575 u8 *state;
1576
Johannes Bergd0709a62008-02-25 16:27:46 +01001577 rcu_read_lock();
1578
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001579 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001580 if (!sta) {
1581 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001582 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001583 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001584
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001585 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001586 /* check if the TID waits for addBA response */
Johannes Berg07346f812008-05-03 01:02:02 +02001587 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001588 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001589 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001590 *state = HT_AGG_STATE_IDLE;
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001591#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001592 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1593 "expecting addBA response there", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001594#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001595 goto timer_expired_exit;
1596 }
1597
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001598#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001599 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001600#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001601
1602 /* go through the state check in stop_BA_session */
1603 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001604 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001605 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1606 WLAN_BACK_INITIATOR);
1607
1608timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001609 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001610}
1611
1612/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001613 * After accepting the AddBA Request we activated a timer,
1614 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001615 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1616 */
Adrian Bunk712590d2008-04-21 11:47:51 +03001617static void sta_rx_agg_session_timer_expired(unsigned long data)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001618{
1619 /* not an elegant detour, but there is no choice as the timer passes
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301620 * only one argument, and various sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001621 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001622 * array gives the sta through container_of */
1623 u8 *ptid = (u8 *)data;
1624 u8 *timer_to_id = ptid - *ptid;
1625 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1626 timer_to_tid[0]);
1627
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001628#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001629 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001630#endif
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001631 ieee80211_sta_stop_rx_ba_session(sta->sdata, sta->addr,
Johannes Bergd0709a62008-02-25 16:27:46 +01001632 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001633 WLAN_REASON_QSTA_TIMEOUT);
1634}
1635
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001636void ieee80211_sta_tear_down_BA_sessions(struct ieee80211_sub_if_data *sdata, u8 *addr)
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001637{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001638 struct ieee80211_local *local = sdata->local;
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001639 int i;
1640
1641 for (i = 0; i < STA_TID_NUM; i++) {
1642 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1643 WLAN_BACK_INITIATOR);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001644 ieee80211_sta_stop_rx_ba_session(sdata, addr, i,
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001645 WLAN_BACK_RECIPIENT,
1646 WLAN_REASON_QSTA_LEAVE_QBSS);
1647 }
1648}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001649
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001650static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
Assaf Kraussb6623482008-06-16 16:09:49 +03001651 struct ieee80211_msrment_ie *request_ie,
1652 const u8 *da, const u8 *bssid,
1653 u8 dialog_token)
1654{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001655 struct ieee80211_local *local = sdata->local;
Assaf Kraussb6623482008-06-16 16:09:49 +03001656 struct sk_buff *skb;
1657 struct ieee80211_mgmt *msr_report;
1658
1659 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1660 sizeof(struct ieee80211_msrment_ie));
1661
1662 if (!skb) {
1663 printk(KERN_ERR "%s: failed to allocate buffer for "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001664 "measurement report frame\n", sdata->dev->name);
Assaf Kraussb6623482008-06-16 16:09:49 +03001665 return;
1666 }
1667
1668 skb_reserve(skb, local->hw.extra_tx_headroom);
1669 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1670 memset(msr_report, 0, 24);
1671 memcpy(msr_report->da, da, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001672 memcpy(msr_report->sa, sdata->dev->dev_addr, ETH_ALEN);
Assaf Kraussb6623482008-06-16 16:09:49 +03001673 memcpy(msr_report->bssid, bssid, ETH_ALEN);
Harvey Harrisone7827a72008-07-15 18:44:13 -07001674 msr_report->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
Assaf Kraussb6623482008-06-16 16:09:49 +03001675 IEEE80211_STYPE_ACTION);
1676
1677 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1678 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1679 msr_report->u.action.u.measurement.action_code =
1680 WLAN_ACTION_SPCT_MSR_RPRT;
1681 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1682
1683 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1684 msr_report->u.action.u.measurement.length =
1685 sizeof(struct ieee80211_msrment_ie);
1686
1687 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1688 sizeof(struct ieee80211_msrment_ie));
1689 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1690 msr_report->u.action.u.measurement.msr_elem.mode |=
1691 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1692 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1693
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001694 ieee80211_sta_tx(sdata, skb, 0);
Assaf Kraussb6623482008-06-16 16:09:49 +03001695}
1696
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001697static void ieee80211_sta_process_measurement_req(struct ieee80211_sub_if_data *sdata,
Assaf Kraussb6623482008-06-16 16:09:49 +03001698 struct ieee80211_mgmt *mgmt,
1699 size_t len)
1700{
1701 /*
1702 * Ignoring measurement request is spec violation.
1703 * Mandatory measurements must be reported optional
1704 * measurements might be refused or reported incapable
1705 * For now just refuse
1706 * TODO: Answer basic measurement as unmeasured
1707 */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001708 ieee80211_send_refuse_measurement_request(sdata,
Assaf Kraussb6623482008-06-16 16:09:49 +03001709 &mgmt->u.action.u.measurement.msr_elem,
1710 mgmt->sa, mgmt->bssid,
1711 mgmt->u.action.u.measurement.dialog_token);
1712}
1713
1714
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001715static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001716 struct ieee80211_if_sta *ifsta,
1717 struct ieee80211_mgmt *mgmt,
1718 size_t len)
1719{
Jiri Bencf0706e82007-05-05 11:45:53 -07001720 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001721 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001722
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001723 if (ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001724 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07001725 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001726
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001727 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07001728 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001729
Johannes Berg51fb61e2007-12-19 01:31:27 +01001730 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001731 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001732 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001733
Johannes Berg51fb61e2007-12-19 01:31:27 +01001734 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001735 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001736 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001737
1738 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1739 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1740 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1741
Johannes Berg51fb61e2007-12-19 01:31:27 +01001742 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg135a2112008-06-16 20:55:29 +02001743 /*
1744 * IEEE 802.11 standard does not require authentication in IBSS
Jiri Bencf0706e82007-05-05 11:45:53 -07001745 * networks and most implementations do not seem to use it.
1746 * However, try to reply to authentication attempts if someone
1747 * has actually implemented this.
Johannes Berg135a2112008-06-16 20:55:29 +02001748 */
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001749 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
Jiri Bencf0706e82007-05-05 11:45:53 -07001750 return;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001751 ieee80211_send_auth(sdata, ifsta, 2, NULL, 0, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001752 }
1753
1754 if (auth_alg != ifsta->auth_alg ||
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001755 auth_transaction != ifsta->auth_transaction)
Jiri Bencf0706e82007-05-05 11:45:53 -07001756 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001757
1758 if (status_code != WLAN_STATUS_SUCCESS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001759 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1760 u8 algs[3];
1761 const int num_algs = ARRAY_SIZE(algs);
1762 int i, pos;
1763 algs[0] = algs[1] = algs[2] = 0xff;
1764 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1765 algs[0] = WLAN_AUTH_OPEN;
1766 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1767 algs[1] = WLAN_AUTH_SHARED_KEY;
1768 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1769 algs[2] = WLAN_AUTH_LEAP;
1770 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1771 pos = 0;
1772 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1773 pos = 1;
1774 else
1775 pos = 2;
1776 for (i = 0; i < num_algs; i++) {
1777 pos++;
1778 if (pos >= num_algs)
1779 pos = 0;
1780 if (algs[pos] == ifsta->auth_alg ||
1781 algs[pos] == 0xff)
1782 continue;
1783 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001784 !ieee80211_sta_wep_configured(sdata))
Jiri Bencf0706e82007-05-05 11:45:53 -07001785 continue;
1786 ifsta->auth_alg = algs[pos];
Jiri Bencf0706e82007-05-05 11:45:53 -07001787 break;
1788 }
1789 }
1790 return;
1791 }
1792
1793 switch (ifsta->auth_alg) {
1794 case WLAN_AUTH_OPEN:
1795 case WLAN_AUTH_LEAP:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001796 ieee80211_auth_completed(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001797 break;
1798 case WLAN_AUTH_SHARED_KEY:
1799 if (ifsta->auth_transaction == 4)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001800 ieee80211_auth_completed(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07001801 else
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001802 ieee80211_auth_challenge(sdata, ifsta, mgmt, len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001803 break;
1804 }
1805}
1806
1807
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001808static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001809 struct ieee80211_if_sta *ifsta,
1810 struct ieee80211_mgmt *mgmt,
1811 size_t len)
1812{
1813 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001814 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001815
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001816 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07001817 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001818
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001819 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07001820 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001821
1822 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1823
Johannes Berg988c0f72008-04-17 19:21:22 +02001824 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001825 printk(KERN_DEBUG "%s: deauthenticated\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001826
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001827 if (ifsta->state == IEEE80211_STA_MLME_AUTHENTICATE ||
1828 ifsta->state == IEEE80211_STA_MLME_ASSOCIATE ||
1829 ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
Ron Rindjunsky9859b812008-08-09 03:02:19 +03001830 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001831 mod_timer(&ifsta->timer, jiffies +
1832 IEEE80211_RETRY_AUTH_INTERVAL);
1833 }
1834
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001835 ieee80211_set_disassoc(sdata, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001836 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001837}
1838
1839
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001840static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001841 struct ieee80211_if_sta *ifsta,
1842 struct ieee80211_mgmt *mgmt,
1843 size_t len)
1844{
1845 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001846 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001847
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001848 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07001849 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001850
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001851 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07001852 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001853
1854 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1855
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001856 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001857 printk(KERN_DEBUG "%s: disassociated\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001858
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001859 if (ifsta->state == IEEE80211_STA_MLME_ASSOCIATED) {
1860 ifsta->state = IEEE80211_STA_MLME_ASSOCIATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001861 mod_timer(&ifsta->timer, jiffies +
1862 IEEE80211_RETRY_AUTH_INTERVAL);
1863 }
1864
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001865 ieee80211_set_disassoc(sdata, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001866}
1867
1868
Johannes Berg471b3ef2007-12-28 14:32:58 +01001869static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001870 struct ieee80211_if_sta *ifsta,
1871 struct ieee80211_mgmt *mgmt,
1872 size_t len,
1873 int reassoc)
1874{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001875 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01001876 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001877 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001878 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001879 u16 capab_info, status_code, aid;
1880 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001881 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001882 u8 *pos;
1883 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001884 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001885 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001886
1887 /* AssocResp and ReassocResp have identical structure, so process both
1888 * of them in this function. */
1889
Tomas Winkler48c2fc52008-08-06 14:22:01 +03001890 if (ifsta->state != IEEE80211_STA_MLME_ASSOCIATE)
Jiri Bencf0706e82007-05-05 11:45:53 -07001891 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001892
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001893 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07001894 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001895
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001896 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001897 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001898
1899 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1900 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1901 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001902
Joe Perches0795af52007-10-03 17:59:30 -07001903 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001904 "status=%d aid=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001905 sdata->dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001906 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001907
1908 if (status_code != WLAN_STATUS_SUCCESS) {
1909 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001910 sdata->dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001911 /* if this was a reassociation, ensure we try a "full"
1912 * association next time. This works around some broken APs
1913 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001914 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07001915 return;
1916 }
1917
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001918 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1919 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001920 "set\n", sdata->dev->name, aid);
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001921 aid &= ~(BIT(15) | BIT(14));
1922
Jiri Bencf0706e82007-05-05 11:45:53 -07001923 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001924 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001925
1926 if (!elems.supp_rates) {
1927 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001928 sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001929 return;
1930 }
1931
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001932 printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001933 ifsta->aid = aid;
1934 ifsta->ap_capab = capab_info;
1935
1936 kfree(ifsta->assocresp_ies);
1937 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001938 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001939 if (ifsta->assocresp_ies)
1940 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1941
Johannes Bergd0709a62008-02-25 16:27:46 +01001942 rcu_read_lock();
1943
Jiri Bencf0706e82007-05-05 11:45:53 -07001944 /* Add STA entry for the AP */
1945 sta = sta_info_get(local, ifsta->bssid);
1946 if (!sta) {
1947 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01001948 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01001949
Johannes Berg73651ee2008-02-25 16:27:47 +01001950 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1951 if (!sta) {
1952 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001953 " the AP\n", sdata->dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01001954 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001955 return;
1956 }
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001957 bss = ieee80211_rx_bss_get(local, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001958 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001959 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001960 if (bss) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001961 sta->last_signal = bss->signal;
Bruno Randolf566bfe52008-05-08 19:15:40 +02001962 sta->last_qual = bss->qual;
Jiri Bencf0706e82007-05-05 11:45:53 -07001963 sta->last_noise = bss->noise;
Johannes Berg3e122be2008-07-09 14:40:34 +02001964 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07001965 }
Johannes Berg73651ee2008-02-25 16:27:47 +01001966
1967 err = sta_info_insert(sta);
1968 if (err) {
1969 printk(KERN_DEBUG "%s: failed to insert STA entry for"
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12001970 " the AP (error %d)\n", sdata->dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01001971 rcu_read_unlock();
1972 return;
1973 }
Ron Rindjunskya61dae12008-08-10 00:54:34 +03001974 /* update new sta with its last rx activity */
1975 sta->last_rx = jiffies;
Jiri Bencf0706e82007-05-05 11:45:53 -07001976 }
1977
Johannes Berg73651ee2008-02-25 16:27:47 +01001978 /*
1979 * FIXME: Do we really need to update the sta_info's information here?
1980 * We already know about the AP (we found it in our list) so it
1981 * should already be filled with the right info, no?
1982 * As is stands, all this is racy because typically we assume
1983 * the information that is filled in here (except flags) doesn't
1984 * change while a STA structure is alive. As such, it should move
1985 * to between the sta_info_alloc() and sta_info_insert() above.
1986 */
1987
Johannes Berg07346f812008-05-03 01:02:02 +02001988 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1989 WLAN_STA_AUTHORIZED);
Jiri Bencf0706e82007-05-05 11:45:53 -07001990
1991 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001992 basic_rates = 0;
1993 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1994
Jiri Bencf0706e82007-05-05 11:45:53 -07001995 for (i = 0; i < elems.supp_rates_len; i++) {
1996 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001997
1998 if (rate > 110)
1999 have_higher_than_11mbit = true;
2000
2001 for (j = 0; j < sband->n_bitrates; j++) {
2002 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002003 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002004 if (elems.supp_rates[i] & 0x80)
2005 basic_rates |= BIT(j);
2006 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002007 }
Johannes Berg8318d782008-01-24 19:38:38 +01002008
Jiri Bencf0706e82007-05-05 11:45:53 -07002009 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2010 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002011
2012 if (rate > 110)
2013 have_higher_than_11mbit = true;
2014
2015 for (j = 0; j < sband->n_bitrates; j++) {
2016 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002017 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002018 if (elems.ext_supp_rates[i] & 0x80)
2019 basic_rates |= BIT(j);
2020 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002021 }
Johannes Berg8318d782008-01-24 19:38:38 +01002022
2023 sta->supp_rates[local->hw.conf.channel->band] = rates;
2024 sdata->basic_rates = basic_rates;
2025
2026 /* cf. IEEE 802.11 9.2.12 */
2027 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2028 have_higher_than_11mbit)
2029 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2030 else
2031 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002032
Johannes Berg3434fbd2008-05-03 00:59:37 +02002033 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2034 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002035 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002036 ieee80211_ht_cap_ie_to_ht_info(
2037 (struct ieee80211_ht_cap *)
2038 elems.ht_cap_elem, &sta->ht_info);
2039 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2040 (struct ieee80211_ht_addt_info *)
2041 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002042 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002043 }
2044
Jiri Bencf0706e82007-05-05 11:45:53 -07002045 rate_control_rate_init(sta, local);
2046
Johannes Berg3434fbd2008-05-03 00:59:37 +02002047 if (elems.wmm_param) {
Johannes Berg07346f812008-05-03 01:02:02 +02002048 set_sta_flags(sta, WLAN_STA_WME);
Johannes Berge5f98f22008-03-05 20:39:31 +01002049 rcu_read_unlock();
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002050 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
Jiri Bencf0706e82007-05-05 11:45:53 -07002051 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002052 } else
2053 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002054
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002055 /* set AID and assoc capability,
2056 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002057 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002058 bss_conf->assoc_capability = capab_info;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002059 ieee80211_set_associated(sdata, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002060
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002061 ieee80211_associated(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07002062}
2063
2064
2065/* Caller must hold local->sta_bss_lock */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002066static void __ieee80211_rx_bss_hash_add(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -07002067 struct ieee80211_sta_bss *bss)
2068{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002069 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002070
2071 if (bss_mesh_cfg(bss))
2072 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2073 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002074 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002075 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002076
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002077 bss->hnext = local->sta_bss_hash[hash_idx];
2078 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002079}
2080
2081
2082/* Caller must hold local->sta_bss_lock */
Johannes Berg3e122be2008-07-09 14:40:34 +02002083static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -07002084 struct ieee80211_sta_bss *bss)
2085{
Jiri Bencf0706e82007-05-05 11:45:53 -07002086 struct ieee80211_sta_bss *b, *prev = NULL;
2087 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2088 while (b) {
2089 if (b == bss) {
2090 if (!prev)
2091 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2092 bss->hnext;
2093 else
2094 prev->hnext = bss->hnext;
2095 break;
2096 }
2097 prev = b;
2098 b = b->hnext;
2099 }
2100}
2101
2102
2103static struct ieee80211_sta_bss *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002104ieee80211_rx_bss_add(struct ieee80211_sub_if_data *sdata, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002105 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002106{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002107 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002108 struct ieee80211_sta_bss *bss;
2109
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002110 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002111 if (!bss)
2112 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002113 atomic_inc(&bss->users);
2114 atomic_inc(&bss->users);
2115 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002116 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002117 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2118 memcpy(bss->ssid, ssid, ssid_len);
2119 bss->ssid_len = ssid_len;
2120 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002121
2122 spin_lock_bh(&local->sta_bss_lock);
2123 /* TODO: order by RSSI? */
2124 list_add_tail(&bss->list, &local->sta_bss_list);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002125 __ieee80211_rx_bss_hash_add(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002126 spin_unlock_bh(&local->sta_bss_lock);
2127 return bss;
2128}
2129
Jiri Bencf0706e82007-05-05 11:45:53 -07002130static struct ieee80211_sta_bss *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002131ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002132 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002133{
Jiri Bencf0706e82007-05-05 11:45:53 -07002134 struct ieee80211_sta_bss *bss;
2135
2136 spin_lock_bh(&local->sta_bss_lock);
2137 bss = local->sta_bss_hash[STA_HASH(bssid)];
2138 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002139 if (!bss_mesh_cfg(bss) &&
2140 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002141 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002142 bss->ssid_len == ssid_len &&
2143 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002144 atomic_inc(&bss->users);
2145 break;
2146 }
2147 bss = bss->hnext;
2148 }
2149 spin_unlock_bh(&local->sta_bss_lock);
2150 return bss;
2151}
2152
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002153#ifdef CONFIG_MAC80211_MESH
2154static struct ieee80211_sta_bss *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002155ieee80211_rx_mesh_bss_get(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002156 u8 *mesh_cfg, int freq)
2157{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002158 struct ieee80211_sta_bss *bss;
2159
2160 spin_lock_bh(&local->sta_bss_lock);
2161 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2162 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002163 if (bss_mesh_cfg(bss) &&
2164 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002165 bss->freq == freq &&
2166 mesh_id_len == bss->mesh_id_len &&
2167 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2168 mesh_id_len))) {
2169 atomic_inc(&bss->users);
2170 break;
2171 }
2172 bss = bss->hnext;
2173 }
2174 spin_unlock_bh(&local->sta_bss_lock);
2175 return bss;
2176}
2177
2178static struct ieee80211_sta_bss *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002179ieee80211_rx_mesh_bss_add(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002180 u8 *mesh_cfg, int mesh_config_len, int freq)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002181{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002182 struct ieee80211_sta_bss *bss;
2183
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002184 if (mesh_config_len != MESH_CFG_LEN)
2185 return NULL;
2186
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002187 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2188 if (!bss)
2189 return NULL;
2190
John W. Linville24736702008-04-08 14:15:46 -04002191 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002192 if (!bss->mesh_cfg) {
2193 kfree(bss);
2194 return NULL;
2195 }
2196
2197 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2198 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2199 if (!bss->mesh_id) {
2200 kfree(bss->mesh_cfg);
2201 kfree(bss);
2202 return NULL;
2203 }
2204 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2205 }
2206
2207 atomic_inc(&bss->users);
2208 atomic_inc(&bss->users);
John W. Linville24736702008-04-08 14:15:46 -04002209 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002210 bss->mesh_id_len = mesh_id_len;
2211 bss->freq = freq;
2212 spin_lock_bh(&local->sta_bss_lock);
2213 /* TODO: order by RSSI? */
2214 list_add_tail(&bss->list, &local->sta_bss_list);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002215 __ieee80211_rx_bss_hash_add(local, bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002216 spin_unlock_bh(&local->sta_bss_lock);
2217 return bss;
2218}
2219#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002220
2221static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2222{
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03002223 kfree(bss->ies);
Johannes Berg902acc72008-02-23 15:17:19 +01002224 kfree(bss_mesh_id(bss));
2225 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002226 kfree(bss);
2227}
2228
2229
Johannes Berg3e122be2008-07-09 14:40:34 +02002230static void ieee80211_rx_bss_put(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -07002231 struct ieee80211_sta_bss *bss)
2232{
Pavel Emelyanov1ebebea2008-04-23 11:47:15 +04002233 local_bh_disable();
2234 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2235 local_bh_enable();
2236 return;
2237 }
2238
Johannes Berg3e122be2008-07-09 14:40:34 +02002239 __ieee80211_rx_bss_hash_del(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002240 list_del(&bss->list);
2241 spin_unlock_bh(&local->sta_bss_lock);
2242 ieee80211_rx_bss_free(bss);
2243}
2244
2245
Johannes Berg3e122be2008-07-09 14:40:34 +02002246void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
Jiri Bencf0706e82007-05-05 11:45:53 -07002247{
Jiri Bencf0706e82007-05-05 11:45:53 -07002248 spin_lock_init(&local->sta_bss_lock);
2249 INIT_LIST_HEAD(&local->sta_bss_list);
2250}
2251
2252
Johannes Berg3e122be2008-07-09 14:40:34 +02002253void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
Jiri Bencf0706e82007-05-05 11:45:53 -07002254{
Jiri Bencf0706e82007-05-05 11:45:53 -07002255 struct ieee80211_sta_bss *bss, *tmp;
2256
2257 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
Johannes Berg3e122be2008-07-09 14:40:34 +02002258 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002259}
2260
2261
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002262static int ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
Bruno Randolfa6072682008-02-18 11:21:15 +09002263 struct ieee80211_if_sta *ifsta,
2264 struct ieee80211_sta_bss *bss)
2265{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002266 struct ieee80211_local *local = sdata->local;
Bruno Randolfa6072682008-02-18 11:21:15 +09002267 int res, rates, i, j;
2268 struct sk_buff *skb;
2269 struct ieee80211_mgmt *mgmt;
Bruno Randolfa6072682008-02-18 11:21:15 +09002270 u8 *pos;
Bruno Randolfa6072682008-02-18 11:21:15 +09002271 struct ieee80211_supported_band *sband;
Dan Williams507b06d2008-06-03 23:39:55 -04002272 union iwreq_data wrqu;
Bruno Randolfa6072682008-02-18 11:21:15 +09002273
2274 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2275
2276 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002277 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002278
2279 if (local->ops->reset_tsf) {
2280 /* Reset own TSF to allow time synchronization work. */
2281 local->ops->reset_tsf(local_to_hw(local));
2282 }
2283 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
Johannes Berg9d139c82008-07-09 14:40:37 +02002284 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
Bruno Randolfa6072682008-02-18 11:21:15 +09002285 if (res)
2286 return res;
2287
2288 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2289
Bruno Randolfa6072682008-02-18 11:21:15 +09002290 sdata->drop_unencrypted = bss->capability &
2291 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2292
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002293 res = ieee80211_set_freq(sdata, bss->freq);
Bruno Randolfa6072682008-02-18 11:21:15 +09002294
Assaf Kraussbe038b32008-06-05 19:55:21 +03002295 if (res)
2296 return res;
Bruno Randolfa6072682008-02-18 11:21:15 +09002297
Johannes Berg9d139c82008-07-09 14:40:37 +02002298 /* Build IBSS probe response */
Bruno Randolfa6072682008-02-18 11:21:15 +09002299 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
Johannes Berg9d139c82008-07-09 14:40:37 +02002300 if (skb) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002301 skb_reserve(skb, local->hw.extra_tx_headroom);
2302
2303 mgmt = (struct ieee80211_mgmt *)
2304 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2305 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
Harvey Harrisone7827a72008-07-15 18:44:13 -07002306 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2307 IEEE80211_STYPE_PROBE_RESP);
Bruno Randolfa6072682008-02-18 11:21:15 +09002308 memset(mgmt->da, 0xff, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002309 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
Bruno Randolfa6072682008-02-18 11:21:15 +09002310 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2311 mgmt->u.beacon.beacon_int =
2312 cpu_to_le16(local->hw.conf.beacon_int);
Assaf Krauss4faeb862008-06-30 17:23:16 +08002313 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
Bruno Randolfa6072682008-02-18 11:21:15 +09002314 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2315
2316 pos = skb_put(skb, 2 + ifsta->ssid_len);
2317 *pos++ = WLAN_EID_SSID;
2318 *pos++ = ifsta->ssid_len;
2319 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2320
2321 rates = bss->supp_rates_len;
2322 if (rates > 8)
2323 rates = 8;
2324 pos = skb_put(skb, 2 + rates);
2325 *pos++ = WLAN_EID_SUPP_RATES;
2326 *pos++ = rates;
2327 memcpy(pos, bss->supp_rates, rates);
2328
2329 if (bss->band == IEEE80211_BAND_2GHZ) {
2330 pos = skb_put(skb, 2 + 1);
2331 *pos++ = WLAN_EID_DS_PARAMS;
2332 *pos++ = 1;
2333 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2334 }
2335
2336 pos = skb_put(skb, 2 + 2);
2337 *pos++ = WLAN_EID_IBSS_PARAMS;
2338 *pos++ = 2;
2339 /* FIX: set ATIM window based on scan results */
2340 *pos++ = 0;
2341 *pos++ = 0;
2342
2343 if (bss->supp_rates_len > 8) {
2344 rates = bss->supp_rates_len - 8;
2345 pos = skb_put(skb, 2 + rates);
2346 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2347 *pos++ = rates;
2348 memcpy(pos, &bss->supp_rates[8], rates);
2349 }
2350
Johannes Berg9d139c82008-07-09 14:40:37 +02002351 ifsta->probe_resp = skb;
Johannes Berge039fa42008-05-15 12:55:29 +02002352
Johannes Berg9d139c82008-07-09 14:40:37 +02002353 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
Bruno Randolfa6072682008-02-18 11:21:15 +09002354 }
2355
Johannes Berg9d139c82008-07-09 14:40:37 +02002356 rates = 0;
2357 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2358 for (i = 0; i < bss->supp_rates_len; i++) {
2359 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2360 for (j = 0; j < sband->n_bitrates; j++)
2361 if (sband->bitrates[j].bitrate == bitrate)
2362 rates |= BIT(j);
2363 }
2364 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2365
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002366 ieee80211_sta_def_wmm_params(sdata, bss, 1);
Johannes Berg9d139c82008-07-09 14:40:37 +02002367
Tomas Winkler48c2fc52008-08-06 14:22:01 +03002368 ifsta->state = IEEE80211_STA_MLME_IBSS_JOINED;
Bruno Randolfa6072682008-02-18 11:21:15 +09002369 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2370
Dan Williams507b06d2008-06-03 23:39:55 -04002371 memset(&wrqu, 0, sizeof(wrqu));
2372 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002373 wireless_send_event(sdata->dev, SIOCGIWAP, &wrqu, NULL);
Bruno Randolfa6072682008-02-18 11:21:15 +09002374
2375 return res;
2376}
2377
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002378u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2379 struct ieee802_11_elems *elems,
2380 enum ieee80211_band band)
2381{
2382 struct ieee80211_supported_band *sband;
2383 struct ieee80211_rate *bitrates;
2384 size_t num_rates;
2385 u64 supp_rates;
2386 int i, j;
2387 sband = local->hw.wiphy->bands[band];
2388
2389 if (!sband) {
2390 WARN_ON(1);
2391 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2392 }
2393
2394 bitrates = sband->bitrates;
2395 num_rates = sband->n_bitrates;
2396 supp_rates = 0;
2397 for (i = 0; i < elems->supp_rates_len +
2398 elems->ext_supp_rates_len; i++) {
2399 u8 rate = 0;
2400 int own_rate;
2401 if (i < elems->supp_rates_len)
2402 rate = elems->supp_rates[i];
2403 else if (elems->ext_supp_rates)
2404 rate = elems->ext_supp_rates
2405 [i - elems->supp_rates_len];
2406 own_rate = 5 * (rate & 0x7f);
2407 for (j = 0; j < num_rates; j++)
2408 if (bitrates[j].bitrate == own_rate)
2409 supp_rates |= BIT(j);
2410 }
2411 return supp_rates;
2412}
2413
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002414static u64 ieee80211_sta_get_mandatory_rates(struct ieee80211_local *local,
2415 enum ieee80211_band band)
2416{
2417 struct ieee80211_supported_band *sband;
2418 struct ieee80211_rate *bitrates;
2419 u64 mandatory_rates;
2420 enum ieee80211_rate_flags mandatory_flag;
2421 int i;
2422
2423 sband = local->hw.wiphy->bands[band];
2424 if (!sband) {
2425 WARN_ON(1);
2426 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2427 }
2428
2429 if (band == IEEE80211_BAND_2GHZ)
2430 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
2431 else
2432 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
2433
2434 bitrates = sband->bitrates;
2435 mandatory_rates = 0;
2436 for (i = 0; i < sband->n_bitrates; i++)
2437 if (bitrates[i].flags & mandatory_flag)
2438 mandatory_rates |= BIT(i);
2439 return mandatory_rates;
2440}
Bruno Randolfa6072682008-02-18 11:21:15 +09002441
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002442static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002443 struct ieee80211_mgmt *mgmt,
2444 size_t len,
2445 struct ieee80211_rx_status *rx_status,
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002446 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -07002447{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002448 struct ieee80211_local *local = sdata->local;
Johannes Berg8318d782008-01-24 19:38:38 +01002449 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002450 struct ieee80211_sta_bss *bss;
2451 struct sta_info *sta;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002452 struct ieee80211_channel *channel;
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002453 u64 beacon_timestamp, rx_timestamp;
2454 u64 supp_rates = 0;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002455 bool beacon = ieee80211_is_beacon(mgmt->frame_control);
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002456 enum ieee80211_band band = rx_status->band;
Joe Perches0795af52007-10-03 17:59:30 -07002457 DECLARE_MAC_BUF(mac);
2458 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002459
Johannes Berg5bda6172008-09-08 11:05:10 +02002460 if (elems->ds_params && elems->ds_params_len == 1)
2461 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
2462 else
2463 freq = rx_status->freq;
2464
2465 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2466
2467 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2468 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002469
Ester Kummerae6a44e2008-06-27 18:54:48 +03002470 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002471 elems->mesh_config && mesh_matches_local(elems, sdata)) {
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002472 supp_rates = ieee80211_sta_get_rates(local, elems, band);
Johannes Berg902acc72008-02-23 15:17:19 +01002473
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002474 mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002475 mesh_peer_accepts_plinks(elems));
Johannes Berg902acc72008-02-23 15:17:19 +01002476 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002477
Ester Kummerae6a44e2008-06-27 18:54:48 +03002478 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002479 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002480 supp_rates = ieee80211_sta_get_rates(local, elems, band);
2481
Johannes Berg69e6c012008-09-08 11:05:08 +02002482 rcu_read_lock();
2483
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002484 sta = sta_info_get(local, mgmt->sa);
2485 if (sta) {
2486 u64 prev_rates;
2487
2488 prev_rates = sta->supp_rates[band];
2489 /* make sure mandatory rates are always added */
2490 sta->supp_rates[band] = supp_rates |
2491 ieee80211_sta_get_mandatory_rates(local, band);
2492
2493#ifdef CONFIG_MAC80211_IBSS_DEBUG
2494 if (sta->supp_rates[band] != prev_rates)
2495 printk(KERN_DEBUG "%s: updated supp_rates set "
2496 "for %s based on beacon info (0x%llx | "
2497 "0x%llx -> 0x%llx)\n",
2498 sdata->dev->name, print_mac(mac, sta->addr),
2499 (unsigned long long) prev_rates,
2500 (unsigned long long) supp_rates,
2501 (unsigned long long) sta->supp_rates[band]);
2502#endif
2503 } else {
2504 ieee80211_ibss_add_sta(sdata, NULL, mgmt->bssid,
2505 mgmt->sa, supp_rates);
Jiri Bencf0706e82007-05-05 11:45:53 -07002506 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002507
Johannes Berg69e6c012008-09-08 11:05:08 +02002508 rcu_read_unlock();
2509 }
Johannes Bergd0709a62008-02-25 16:27:46 +01002510
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002511#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002512 if (elems->mesh_config)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002513 bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002514 elems->mesh_id_len, elems->mesh_config, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002515 else
2516#endif
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002517 bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002518 elems->ssid, elems->ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002519 if (!bss) {
2520#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002521 if (elems->mesh_config)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002522 bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002523 elems->mesh_id_len, elems->mesh_config,
2524 elems->mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002525 else
2526#endif
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002527 bss = ieee80211_rx_bss_add(sdata, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002528 elems->ssid, elems->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002529 if (!bss)
2530 return;
2531 } else {
2532#if 0
2533 /* TODO: order by RSSI? */
2534 spin_lock_bh(&local->sta_bss_lock);
2535 list_move_tail(&bss->list, &local->sta_bss_list);
2536 spin_unlock_bh(&local->sta_bss_lock);
2537#endif
2538 }
2539
Daniel Drake56282212007-07-10 19:32:10 +02002540 /* save the ERP value so that it is available at association time */
Ester Kummerae6a44e2008-06-27 18:54:48 +03002541 if (elems->erp_info && elems->erp_info_len >= 1) {
2542 bss->erp_value = elems->erp_info[0];
Daniel Drake56282212007-07-10 19:32:10 +02002543 bss->has_erp_value = 1;
2544 }
2545
Jiri Bencf0706e82007-05-05 11:45:53 -07002546 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2547 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002548
Emmanuel Grumbach98f7dfd2008-07-18 13:52:59 +08002549 if (elems->tim) {
2550 struct ieee80211_tim_ie *tim_ie =
2551 (struct ieee80211_tim_ie *)elems->tim;
2552 bss->dtim_period = tim_ie->dtim_period;
2553 }
2554
2555 /* set default value for buggy APs */
2556 if (!elems->tim || bss->dtim_period == 0)
2557 bss->dtim_period = 1;
2558
Jiri Bencf0706e82007-05-05 11:45:53 -07002559 bss->supp_rates_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002560 if (elems->supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002561 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002562 if (clen > elems->supp_rates_len)
2563 clen = elems->supp_rates_len;
2564 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
Jiri Bencf0706e82007-05-05 11:45:53 -07002565 clen);
2566 bss->supp_rates_len += clen;
2567 }
Ester Kummerae6a44e2008-06-27 18:54:48 +03002568 if (elems->ext_supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002569 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002570 if (clen > elems->ext_supp_rates_len)
2571 clen = elems->ext_supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07002572 memcpy(&bss->supp_rates[bss->supp_rates_len],
Ester Kummerae6a44e2008-06-27 18:54:48 +03002573 elems->ext_supp_rates, clen);
Jiri Bencf0706e82007-05-05 11:45:53 -07002574 bss->supp_rates_len += clen;
2575 }
2576
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002577 bss->band = band;
Johannes Berg30b89b02008-04-16 17:43:20 +02002578
Johannes Berg5bda6172008-09-08 11:05:10 +02002579 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
2580
Johannes Berg30b89b02008-04-16 17:43:20 +02002581 bss->timestamp = beacon_timestamp;
2582 bss->last_update = jiffies;
Johannes Berg30b89b02008-04-16 17:43:20 +02002583 bss->signal = rx_status->signal;
2584 bss->noise = rx_status->noise;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002585 bss->qual = rx_status->qual;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002586 if (!beacon)
2587 bss->last_probe_resp = jiffies;
Johannes Berg30b89b02008-04-16 17:43:20 +02002588 /*
2589 * In STA mode, the remaining parameters should not be overridden
2590 * by beacons because they're not necessarily accurate there.
2591 */
2592 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002593 bss->last_probe_resp && beacon) {
Johannes Berg3e122be2008-07-09 14:40:34 +02002594 ieee80211_rx_bss_put(local, bss);
Johannes Berg30b89b02008-04-16 17:43:20 +02002595 return;
2596 }
2597
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03002598 if (bss->ies == NULL || bss->ies_len < elems->total_len) {
2599 kfree(bss->ies);
2600 bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002601 }
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03002602 if (bss->ies) {
2603 memcpy(bss->ies, elems->ie_start, elems->total_len);
2604 bss->ies_len = elems->total_len;
2605 } else
2606 bss->ies_len = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002607
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03002608 bss->wmm_used = elems->wmm_param || elems->wmm_info;
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002609
2610 /* check if we need to merge IBSS */
2611 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2612 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002613 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002614 bss->freq == local->oper_channel->center_freq &&
Ester Kummerae6a44e2008-06-27 18:54:48 +03002615 elems->ssid_len == sdata->u.sta.ssid_len &&
2616 memcmp(elems->ssid, sdata->u.sta.ssid,
2617 sdata->u.sta.ssid_len) == 0) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002618 if (rx_status->flag & RX_FLAG_TSFT) {
2619 /* in order for correct IBSS merging we need mactime
2620 *
2621 * since mactime is defined as the time the first data
2622 * symbol of the frame hits the PHY, and the timestamp
2623 * of the beacon is defined as "the time that the data
2624 * symbol containing the first bit of the timestamp is
2625 * transmitted to the PHY plus the transmitting STA’s
2626 * delays through its local PHY from the MAC-PHY
2627 * interface to its interface with the WM"
2628 * (802.11 11.1.2) - equals the time this bit arrives at
2629 * the receiver - we have to take into account the
2630 * offset between the two.
2631 * e.g: at 1 MBit that means mactime is 192 usec earlier
2632 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2633 */
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002634 int rate = local->hw.wiphy->bands[band]->
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002635 bitrates[rx_status->rate_idx].bitrate;
2636 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2637 } else if (local && local->ops && local->ops->get_tsf)
2638 /* second best option: get current TSF */
2639 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2640 else
2641 /* can't merge without knowing the TSF */
2642 rx_timestamp = -1LLU;
2643#ifdef CONFIG_MAC80211_IBSS_DEBUG
2644 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2645 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2646 print_mac(mac, mgmt->sa),
2647 print_mac(mac2, mgmt->bssid),
2648 (unsigned long long)rx_timestamp,
2649 (unsigned long long)beacon_timestamp,
2650 (unsigned long long)(rx_timestamp - beacon_timestamp),
2651 jiffies);
2652#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2653 if (beacon_timestamp > rx_timestamp) {
John W. Linville576fdea2008-08-26 20:33:34 -04002654#ifdef CONFIG_MAC80211_IBSS_DEBUG
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002655 printk(KERN_DEBUG "%s: beacon TSF higher than "
2656 "local TSF - IBSS merge with BSSID %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002657 sdata->dev->name, print_mac(mac, mgmt->bssid));
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002658#endif
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002659 ieee80211_sta_join_ibss(sdata, &sdata->u.sta, bss);
2660 ieee80211_ibss_add_sta(sdata, NULL,
Vladimir Koutny87291c02008-06-13 16:50:44 +02002661 mgmt->bssid, mgmt->sa,
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03002662 supp_rates);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002663 }
2664 }
2665
Johannes Berg3e122be2008-07-09 14:40:34 +02002666 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002667}
2668
2669
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002670static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002671 struct ieee80211_mgmt *mgmt,
2672 size_t len,
2673 struct ieee80211_rx_status *rx_status)
2674{
Ester Kummerae6a44e2008-06-27 18:54:48 +03002675 size_t baselen;
2676 struct ieee802_11_elems elems;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002677 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002678
Tomas Winkler8e7cdbb2008-08-03 14:32:01 +03002679 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
2680 return; /* ignore ProbeResp to foreign address */
2681
Ester Kummerae6a44e2008-06-27 18:54:48 +03002682 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2683 if (baselen > len)
2684 return;
2685
2686 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2687 &elems);
2688
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002689 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
2690
2691 /* direct probe may be part of the association flow */
2692 if (test_and_clear_bit(IEEE80211_STA_REQ_DIRECT_PROBE,
2693 &ifsta->request)) {
2694 printk(KERN_DEBUG "%s direct probe responded\n",
2695 sdata->dev->name);
2696 ieee80211_authenticate(sdata, ifsta);
2697 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002698}
2699
2700
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002701static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002702 struct ieee80211_mgmt *mgmt,
2703 size_t len,
2704 struct ieee80211_rx_status *rx_status)
2705{
Jiri Bencf0706e82007-05-05 11:45:53 -07002706 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002707 size_t baselen;
2708 struct ieee802_11_elems elems;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002709 struct ieee80211_local *local = sdata->local;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002710 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002711 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002712
Ester Kummerae6a44e2008-06-27 18:54:48 +03002713 /* Process beacon from the current BSS */
2714 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2715 if (baselen > len)
2716 return;
2717
2718 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2719
Ron Rindjunsky9859b812008-08-09 03:02:19 +03002720 ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002721
Johannes Berg51fb61e2007-12-19 01:31:27 +01002722 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002723 return;
2724 ifsta = &sdata->u.sta;
2725
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002726 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002727 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2728 return;
2729
Reinette Chatred18ef292008-04-09 16:56:15 -07002730 /* Do not send changes to driver if we are scanning. This removes
Johannes Bergfe3fa822008-09-08 11:05:09 +02002731 * requirement that a driver's bss_info_changed/conf_tx functions
2732 * need to be atomic.
2733 * This is really ugly code, we should rewrite scanning and make
2734 * all this more understandable for humans.
2735 */
Reinette Chatred18ef292008-04-09 16:56:15 -07002736 if (local->sta_sw_scanning || local->sta_hw_scanning)
2737 return;
2738
Johannes Bergfe3fa822008-09-08 11:05:09 +02002739 ieee80211_sta_wmm_params(local, ifsta, elems.wmm_param,
2740 elems.wmm_param_len);
2741
Daniel Drake56282212007-07-10 19:32:10 +02002742 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002743 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
John W. Linville50c4afb2008-04-15 14:09:27 -04002744 else {
2745 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2746 changed |= ieee80211_handle_protect_preamb(sdata, false,
2747 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2748 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002749
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002750 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07002751 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002752 struct ieee80211_ht_bss_info bss_info;
2753
2754 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2755 (struct ieee80211_ht_addt_info *)
2756 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002757 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2758 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002759 }
2760
Johannes Berg471b3ef2007-12-28 14:32:58 +01002761 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002762}
2763
2764
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002765static void ieee80211_rx_mgmt_probe_req(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002766 struct ieee80211_if_sta *ifsta,
2767 struct ieee80211_mgmt *mgmt,
2768 size_t len,
2769 struct ieee80211_rx_status *rx_status)
2770{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002771 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002772 int tx_last_beacon;
2773 struct sk_buff *skb;
2774 struct ieee80211_mgmt *resp;
2775 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002776 DECLARE_MAC_BUF(mac);
2777#ifdef CONFIG_MAC80211_IBSS_DEBUG
2778 DECLARE_MAC_BUF(mac2);
2779 DECLARE_MAC_BUF(mac3);
2780#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002781
Johannes Berg51fb61e2007-12-19 01:31:27 +01002782 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Tomas Winkler48c2fc52008-08-06 14:22:01 +03002783 ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002784 len < 24 + 2 || !ifsta->probe_resp)
2785 return;
2786
2787 if (local->ops->tx_last_beacon)
2788 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2789 else
2790 tx_last_beacon = 1;
2791
2792#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002793 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2794 "%s (tx_last_beacon=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002795 sdata->dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
Joe Perches0795af52007-10-03 17:59:30 -07002796 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002797#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2798
2799 if (!tx_last_beacon)
2800 return;
2801
2802 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2803 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2804 return;
2805
2806 end = ((u8 *) mgmt) + len;
2807 pos = mgmt->u.probe_req.variable;
2808 if (pos[0] != WLAN_EID_SSID ||
2809 pos + 2 + pos[1] > end) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002810#ifdef CONFIG_MAC80211_IBSS_DEBUG
2811 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
2812 "from %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002813 sdata->dev->name, print_mac(mac, mgmt->sa));
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002814#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002815 return;
2816 }
2817 if (pos[1] != 0 &&
2818 (pos[1] != ifsta->ssid_len ||
2819 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2820 /* Ignore ProbeReq for foreign SSID */
2821 return;
2822 }
2823
2824 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002825 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002826 if (!skb)
2827 return;
2828
2829 resp = (struct ieee80211_mgmt *) skb->data;
2830 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2831#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002832 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002833 sdata->dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002834#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002835 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002836}
2837
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002838static void ieee80211_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
Johannes Berg4e20cb22007-12-19 01:31:24 +01002839 struct ieee80211_if_sta *ifsta,
2840 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002841 size_t len,
2842 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002843{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002844 struct ieee80211_local *local = sdata->local;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002845
Johannes Berg9c80d3d2008-09-08 15:41:59 +02002846 /* all categories we currently handle have action_code */
2847 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002848 return;
2849
2850 switch (mgmt->u.action.category) {
Assaf Kraussb6623482008-06-16 16:09:49 +03002851 case WLAN_CATEGORY_SPECTRUM_MGMT:
2852 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
2853 break;
Johannes Berg5fd12d42008-09-08 16:31:48 +02002854 switch (mgmt->u.action.u.measurement.action_code) {
Assaf Kraussb6623482008-06-16 16:09:49 +03002855 case WLAN_ACTION_SPCT_MSR_REQ:
2856 if (len < (IEEE80211_MIN_ACTION_SIZE +
2857 sizeof(mgmt->u.action.u.measurement)))
2858 break;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002859 ieee80211_sta_process_measurement_req(sdata, mgmt, len);
Assaf Kraussb6623482008-06-16 16:09:49 +03002860 break;
2861 }
2862 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002863 case WLAN_CATEGORY_BACK:
2864 switch (mgmt->u.action.u.addba_req.action_code) {
2865 case WLAN_ACTION_ADDBA_REQ:
2866 if (len < (IEEE80211_MIN_ACTION_SIZE +
2867 sizeof(mgmt->u.action.u.addba_req)))
2868 break;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002869 ieee80211_sta_process_addba_request(local, mgmt, len);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002870 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002871 case WLAN_ACTION_ADDBA_RESP:
2872 if (len < (IEEE80211_MIN_ACTION_SIZE +
2873 sizeof(mgmt->u.action.u.addba_resp)))
2874 break;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002875 ieee80211_sta_process_addba_resp(local, mgmt, len);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002876 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002877 case WLAN_ACTION_DELBA:
2878 if (len < (IEEE80211_MIN_ACTION_SIZE +
2879 sizeof(mgmt->u.action.u.delba)))
2880 break;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002881 ieee80211_sta_process_delba(sdata, mgmt, len);
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002882 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002883 }
2884 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002885 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002886 if (ieee80211_vif_is_mesh(&sdata->vif))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002887 mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002888 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002889 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002890 if (ieee80211_vif_is_mesh(&sdata->vif))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002891 mesh_rx_path_sel_frame(sdata, mgmt, len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002892 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002893 }
2894}
Jiri Bencf0706e82007-05-05 11:45:53 -07002895
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002896void ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
Jiri Bencf0706e82007-05-05 11:45:53 -07002897 struct ieee80211_rx_status *rx_status)
2898{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002899 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07002900 struct ieee80211_if_sta *ifsta;
2901 struct ieee80211_mgmt *mgmt;
2902 u16 fc;
2903
2904 if (skb->len < 24)
2905 goto fail;
2906
Jiri Bencf0706e82007-05-05 11:45:53 -07002907 ifsta = &sdata->u.sta;
2908
2909 mgmt = (struct ieee80211_mgmt *) skb->data;
2910 fc = le16_to_cpu(mgmt->frame_control);
2911
2912 switch (fc & IEEE80211_FCTL_STYPE) {
2913 case IEEE80211_STYPE_PROBE_REQ:
2914 case IEEE80211_STYPE_PROBE_RESP:
2915 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002916 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07002917 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2918 case IEEE80211_STYPE_AUTH:
2919 case IEEE80211_STYPE_ASSOC_RESP:
2920 case IEEE80211_STYPE_REASSOC_RESP:
2921 case IEEE80211_STYPE_DEAUTH:
2922 case IEEE80211_STYPE_DISASSOC:
2923 skb_queue_tail(&ifsta->skb_queue, skb);
2924 queue_work(local->hw.workqueue, &ifsta->work);
2925 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002926 }
2927
2928 fail:
2929 kfree_skb(skb);
2930}
2931
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002932static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002933 struct sk_buff *skb)
2934{
2935 struct ieee80211_rx_status *rx_status;
Jiri Bencf0706e82007-05-05 11:45:53 -07002936 struct ieee80211_if_sta *ifsta;
2937 struct ieee80211_mgmt *mgmt;
2938 u16 fc;
2939
Jiri Bencf0706e82007-05-05 11:45:53 -07002940 ifsta = &sdata->u.sta;
2941
2942 rx_status = (struct ieee80211_rx_status *) skb->cb;
2943 mgmt = (struct ieee80211_mgmt *) skb->data;
2944 fc = le16_to_cpu(mgmt->frame_control);
2945
2946 switch (fc & IEEE80211_FCTL_STYPE) {
2947 case IEEE80211_STYPE_PROBE_REQ:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002948 ieee80211_rx_mgmt_probe_req(sdata, ifsta, mgmt, skb->len,
Jiri Bencf0706e82007-05-05 11:45:53 -07002949 rx_status);
2950 break;
2951 case IEEE80211_STYPE_PROBE_RESP:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002952 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
Jiri Bencf0706e82007-05-05 11:45:53 -07002953 break;
2954 case IEEE80211_STYPE_BEACON:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002955 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Jiri Bencf0706e82007-05-05 11:45:53 -07002956 break;
2957 case IEEE80211_STYPE_AUTH:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002958 ieee80211_rx_mgmt_auth(sdata, ifsta, mgmt, skb->len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002959 break;
2960 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002961 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002962 break;
2963 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002964 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002965 break;
2966 case IEEE80211_STYPE_DEAUTH:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002967 ieee80211_rx_mgmt_deauth(sdata, ifsta, mgmt, skb->len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002968 break;
2969 case IEEE80211_STYPE_DISASSOC:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002970 ieee80211_rx_mgmt_disassoc(sdata, ifsta, mgmt, skb->len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002971 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002972 case IEEE80211_STYPE_ACTION:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002973 ieee80211_rx_mgmt_action(sdata, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002974 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07002975 }
2976
2977 kfree_skb(skb);
2978}
2979
2980
Johannes Berg9ae54c82008-01-31 19:48:20 +01002981ieee80211_rx_result
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002982ieee80211_sta_rx_scan(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
Zhu Yiece8edd2007-11-22 10:53:21 +08002983 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002984{
2985 struct ieee80211_mgmt *mgmt;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07002986 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07002987
Zhu Yiece8edd2007-11-22 10:53:21 +08002988 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002989 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002990
2991 mgmt = (struct ieee80211_mgmt *) skb->data;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07002992 fc = mgmt->frame_control;
Jiri Bencf0706e82007-05-05 11:45:53 -07002993
Harvey Harrison4e3996f2008-06-22 16:45:32 -07002994 if (ieee80211_is_ctl(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +01002995 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08002996
2997 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002998 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08002999
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003000 if (ieee80211_is_probe_resp(fc)) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003001 ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len, rx_status);
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003002 dev_kfree_skb(skb);
3003 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003004 }
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003005
3006 if (ieee80211_is_beacon(fc)) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003007 ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003008 dev_kfree_skb(skb);
3009 return RX_QUEUED;
3010 }
3011
Johannes Berg9ae54c82008-01-31 19:48:20 +01003012 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003013}
3014
3015
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003016static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
Jiri Bencf0706e82007-05-05 11:45:53 -07003017{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003018 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003019 int active = 0;
3020 struct sta_info *sta;
3021
Johannes Bergd0709a62008-02-25 16:27:46 +01003022 rcu_read_lock();
3023
3024 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3025 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003026 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3027 jiffies)) {
3028 active++;
3029 break;
3030 }
3031 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003032
3033 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003034
3035 return active;
3036}
3037
3038
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003039static void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003040{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003041 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003042 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003043 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003044 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003045 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003046
Johannes Bergd0709a62008-02-25 16:27:46 +01003047 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003048 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003049 if (time_after(jiffies, sta->last_rx + exp_time)) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003050#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003051 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003052 sdata->dev->name, print_mac(mac, sta->addr));
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003053#endif
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003054 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003055 if (sta)
3056 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003057 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003058 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003059
Johannes Bergd0709a62008-02-25 16:27:46 +01003060 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3061 sta_info_destroy(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003062}
3063
3064
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003065static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003066 struct ieee80211_if_sta *ifsta)
3067{
3068 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3069
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003070 ieee80211_sta_expire(sdata, IEEE80211_IBSS_INACTIVITY_LIMIT);
3071 if (ieee80211_sta_active_ibss(sdata))
Jiri Bencf0706e82007-05-05 11:45:53 -07003072 return;
3073
3074 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003075 "IBSS networks with same SSID (merge)\n", sdata->dev->name);
3076 ieee80211_sta_req_scan(sdata, ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003077}
3078
3079
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003080#ifdef CONFIG_MAC80211_MESH
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003081static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003082 struct ieee80211_if_sta *ifsta)
3083{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003084 bool free_plinks;
3085
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003086 ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3087 mesh_path_expire(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003088
3089 free_plinks = mesh_plink_availables(sdata);
3090 if (free_plinks != sdata->u.sta.accepting_plinks)
Johannes Berg9d139c82008-07-09 14:40:37 +02003091 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003092
3093 mod_timer(&ifsta->timer, jiffies +
3094 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3095}
3096
3097
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003098void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003099{
3100 struct ieee80211_if_sta *ifsta;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003101 ifsta = &sdata->u.sta;
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003102 ifsta->state = IEEE80211_STA_MLME_MESH_UP;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003103 ieee80211_sta_timer((unsigned long)sdata);
Luis Carlos Cobo56a6d132008-07-29 19:59:31 +02003104 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003105}
3106#endif
3107
3108
Jiri Bencf0706e82007-05-05 11:45:53 -07003109void ieee80211_sta_timer(unsigned long data)
3110{
3111 struct ieee80211_sub_if_data *sdata =
3112 (struct ieee80211_sub_if_data *) data;
3113 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003114 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003115
3116 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3117 queue_work(local->hw.workqueue, &ifsta->work);
3118}
3119
Jiri Bencf0706e82007-05-05 11:45:53 -07003120void ieee80211_sta_work(struct work_struct *work)
3121{
3122 struct ieee80211_sub_if_data *sdata =
3123 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003124 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003125 struct ieee80211_if_sta *ifsta;
3126 struct sk_buff *skb;
3127
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003128 if (!netif_running(sdata->dev))
Jiri Bencf0706e82007-05-05 11:45:53 -07003129 return;
3130
Zhu Yiece8edd2007-11-22 10:53:21 +08003131 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003132 return;
3133
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003134 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3135 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3136 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
Jiri Bencf0706e82007-05-05 11:45:53 -07003137 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003138 ifsta = &sdata->u.sta;
3139
3140 while ((skb = skb_dequeue(&ifsta->skb_queue)))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003141 ieee80211_sta_rx_queued_mgmt(sdata, skb);
Jiri Bencf0706e82007-05-05 11:45:53 -07003142
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003143#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003144 if (ifsta->preq_queue_len &&
3145 time_after(jiffies,
3146 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003147 mesh_path_start_discovery(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003148#endif
3149
Ron Rindjunsky9859b812008-08-09 03:02:19 +03003150 if (ifsta->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
3151 ifsta->state != IEEE80211_STA_MLME_AUTHENTICATE &&
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003152 ifsta->state != IEEE80211_STA_MLME_ASSOCIATE &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003153 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003154 if (ifsta->scan_ssid_len)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003155 ieee80211_sta_start_scan(sdata, ifsta->scan_ssid, ifsta->scan_ssid_len);
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003156 else
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003157 ieee80211_sta_start_scan(sdata, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003158 return;
3159 }
3160
3161 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003162 if (ieee80211_sta_config_auth(sdata, ifsta))
Jiri Bencf0706e82007-05-05 11:45:53 -07003163 return;
3164 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3165 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3166 return;
3167
3168 switch (ifsta->state) {
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003169 case IEEE80211_STA_MLME_DISABLED:
Jiri Bencf0706e82007-05-05 11:45:53 -07003170 break;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03003171 case IEEE80211_STA_MLME_DIRECT_PROBE:
3172 ieee80211_direct_probe(sdata, ifsta);
3173 break;
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003174 case IEEE80211_STA_MLME_AUTHENTICATE:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003175 ieee80211_authenticate(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003176 break;
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003177 case IEEE80211_STA_MLME_ASSOCIATE:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003178 ieee80211_associate(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003179 break;
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003180 case IEEE80211_STA_MLME_ASSOCIATED:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003181 ieee80211_associated(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003182 break;
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003183 case IEEE80211_STA_MLME_IBSS_SEARCH:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003184 ieee80211_sta_find_ibss(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003185 break;
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003186 case IEEE80211_STA_MLME_IBSS_JOINED:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003187 ieee80211_sta_merge_ibss(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003188 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003189#ifdef CONFIG_MAC80211_MESH
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003190 case IEEE80211_STA_MLME_MESH_UP:
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003191 ieee80211_mesh_housekeeping(sdata, ifsta);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003192 break;
3193#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003194 default:
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003195 WARN_ON(1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003196 break;
3197 }
3198
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003199 if (ieee80211_privacy_mismatch(sdata, ifsta)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003200 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003201 "mixed-cell disabled - disassociate\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07003202
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003203 ieee80211_send_disassoc(sdata, ifsta, WLAN_REASON_UNSPECIFIED);
3204 ieee80211_set_disassoc(sdata, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003205 }
3206}
3207
3208
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003209static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003210 struct ieee80211_if_sta *ifsta)
3211{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003212 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003213
3214 if (local->ops->reset_tsf) {
3215 /* Reset own TSF to allow time synchronization work. */
3216 local->ops->reset_tsf(local_to_hw(local));
3217 }
3218
3219 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3220
3221
3222 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3223 ifsta->auth_alg = WLAN_AUTH_OPEN;
3224 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3225 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3226 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3227 ifsta->auth_alg = WLAN_AUTH_LEAP;
3228 else
3229 ifsta->auth_alg = WLAN_AUTH_OPEN;
Jiri Bencf0706e82007-05-05 11:45:53 -07003230 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003231 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +03003232 ifsta->assoc_scan_tries = 0;
Ron Rindjunsky9859b812008-08-09 03:02:19 +03003233 ifsta->direct_probe_tries = 0;
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +03003234 ifsta->auth_tries = 0;
3235 ifsta->assoc_tries = 0;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003236 netif_carrier_off(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003237}
3238
3239
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003240void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003241 struct ieee80211_if_sta *ifsta)
3242{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003243 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003244
Johannes Berg51fb61e2007-12-19 01:31:27 +01003245 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003246 return;
3247
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003248 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3249 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3250 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3251 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003252 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3253 queue_work(local->hw.workqueue, &ifsta->work);
3254 }
3255}
3256
3257static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3258 const char *ssid, int ssid_len)
3259{
3260 int tmp, hidden_ssid;
3261
Michael Wu48225702007-10-19 17:14:36 -04003262 if (ssid_len == ifsta->ssid_len &&
3263 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003264 return 1;
3265
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003266 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003267 return 0;
3268
3269 hidden_ssid = 1;
3270 tmp = ssid_len;
3271 while (tmp--) {
3272 if (ssid[tmp] != '\0') {
3273 hidden_ssid = 0;
3274 break;
3275 }
3276 }
3277
3278 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3279 return 1;
3280
3281 if (ssid_len == 1 && ssid[0] == ' ')
3282 return 1;
3283
3284 return 0;
3285}
3286
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003287static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003288 struct ieee80211_if_sta *ifsta)
3289{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003290 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003291 struct ieee80211_sta_bss *bss, *selected = NULL;
3292 int top_rssi = 0, freq;
3293
Jiri Bencf0706e82007-05-05 11:45:53 -07003294 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003295 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003296 list_for_each_entry(bss, &local->sta_bss_list, list) {
3297 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3298 continue;
3299
Abhijeet Kolekar34a961f2008-05-09 09:35:41 -07003300 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3301 IEEE80211_STA_AUTO_BSSID_SEL |
3302 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3303 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3304 !!sdata->default_key))
Jiri Bencf0706e82007-05-05 11:45:53 -07003305 continue;
3306
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003307 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3308 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003309 continue;
3310
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003311 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003312 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3313 continue;
3314
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003315 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003316 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3317 continue;
3318
Bruno Randolf566bfe52008-05-08 19:15:40 +02003319 if (!selected || top_rssi < bss->signal) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003320 selected = bss;
Bruno Randolf566bfe52008-05-08 19:15:40 +02003321 top_rssi = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07003322 }
3323 }
3324 if (selected)
3325 atomic_inc(&selected->users);
3326 spin_unlock_bh(&local->sta_bss_lock);
3327
3328 if (selected) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003329 ieee80211_set_freq(sdata, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003330 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003331 ieee80211_sta_set_ssid(sdata, selected->ssid,
Jiri Bencf0706e82007-05-05 11:45:53 -07003332 selected->ssid_len);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003333 ieee80211_sta_set_bssid(sdata, selected->bssid);
3334 ieee80211_sta_def_wmm_params(sdata, selected, 0);
Ron Rindjunsky9859b812008-08-09 03:02:19 +03003335
3336 /* Send out direct probe if no probe resp was received or
3337 * the one we have is outdated
3338 */
3339 if (!selected->last_probe_resp ||
3340 time_after(jiffies, selected->last_probe_resp
3341 + IEEE80211_SCAN_RESULT_EXPIRE))
3342 ifsta->state = IEEE80211_STA_MLME_DIRECT_PROBE;
3343 else
3344 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
3345
Johannes Berg3e122be2008-07-09 14:40:34 +02003346 ieee80211_rx_bss_put(local, selected);
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003347 ieee80211_sta_reset_auth(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003348 return 0;
3349 } else {
Ron Rindjunsky6042a3e2008-08-08 01:50:46 +03003350 if (ifsta->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
3351 ifsta->assoc_scan_tries++;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003352 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003353 ieee80211_sta_start_scan(sdata, NULL, 0);
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003354 else
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003355 ieee80211_sta_start_scan(sdata, ifsta->ssid,
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003356 ifsta->ssid_len);
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003357 ifsta->state = IEEE80211_STA_MLME_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003358 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3359 } else
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003360 ifsta->state = IEEE80211_STA_MLME_DISABLED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003361 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003362 return -1;
3363}
3364
Jiri Bencf0706e82007-05-05 11:45:53 -07003365
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003366static int ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003367 struct ieee80211_if_sta *ifsta)
3368{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003369 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003370 struct ieee80211_sta_bss *bss;
Johannes Berg8318d782008-01-24 19:38:38 +01003371 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003372 u8 bssid[ETH_ALEN], *pos;
3373 int i;
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003374 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003375 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003376
3377#if 0
3378 /* Easier testing, use fixed BSSID. */
3379 memset(bssid, 0xfe, ETH_ALEN);
3380#else
3381 /* Generate random, not broadcast, locally administered BSSID. Mix in
3382 * own MAC address to make sure that devices that do not have proper
3383 * random number generator get different BSSID. */
3384 get_random_bytes(bssid, ETH_ALEN);
3385 for (i = 0; i < ETH_ALEN; i++)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003386 bssid[i] ^= sdata->dev->dev_addr[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07003387 bssid[0] &= ~0x01;
3388 bssid[0] |= 0x02;
3389#endif
3390
Joe Perches0795af52007-10-03 17:59:30 -07003391 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003392 sdata->dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003393
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003394 bss = ieee80211_rx_bss_add(sdata, bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01003395 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003396 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003397 if (!bss)
3398 return -ENOMEM;
3399
Johannes Berg8318d782008-01-24 19:38:38 +01003400 bss->band = local->hw.conf.channel->band;
3401 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003402
3403 if (local->hw.conf.beacon_int == 0)
Tomas Winklerdc0ae302008-06-12 22:38:37 +03003404 local->hw.conf.beacon_int = 100;
Jiri Bencf0706e82007-05-05 11:45:53 -07003405 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003406 bss->last_update = jiffies;
3407 bss->capability = WLAN_CAPABILITY_IBSS;
Johannes Berg988c0f72008-04-17 19:21:22 +02003408
3409 if (sdata->default_key)
Jiri Bencf0706e82007-05-05 11:45:53 -07003410 bss->capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +02003411 else
Jiri Bencf0706e82007-05-05 11:45:53 -07003412 sdata->drop_unencrypted = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02003413
Johannes Berg8318d782008-01-24 19:38:38 +01003414 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003415 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003416 for (i = 0; i < sband->n_bitrates; i++) {
3417 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003418 *pos++ = (u8) (rate / 5);
3419 }
3420
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003421 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
Johannes Berg3e122be2008-07-09 14:40:34 +02003422 ieee80211_rx_bss_put(local, bss);
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003423 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003424}
3425
3426
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003427static int ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003428 struct ieee80211_if_sta *ifsta)
3429{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003430 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003431 struct ieee80211_sta_bss *bss;
3432 int found = 0;
3433 u8 bssid[ETH_ALEN];
3434 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003435 DECLARE_MAC_BUF(mac);
3436 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003437
3438 if (ifsta->ssid_len == 0)
3439 return -EINVAL;
3440
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003441 active_ibss = ieee80211_sta_active_ibss(sdata);
Jiri Bencf0706e82007-05-05 11:45:53 -07003442#ifdef CONFIG_MAC80211_IBSS_DEBUG
3443 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003444 sdata->dev->name, active_ibss);
Jiri Bencf0706e82007-05-05 11:45:53 -07003445#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3446 spin_lock_bh(&local->sta_bss_lock);
3447 list_for_each_entry(bss, &local->sta_bss_list, list) {
3448 if (ifsta->ssid_len != bss->ssid_len ||
3449 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3450 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3451 continue;
3452#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003453 printk(KERN_DEBUG " bssid=%s found\n",
3454 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003455#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3456 memcpy(bssid, bss->bssid, ETH_ALEN);
3457 found = 1;
3458 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3459 break;
3460 }
3461 spin_unlock_bh(&local->sta_bss_lock);
3462
3463#ifdef CONFIG_MAC80211_IBSS_DEBUG
Vladimir Koutny6e438292008-07-07 14:23:01 +02003464 if (found)
3465 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3466 "%s\n", print_mac(mac, bssid),
3467 print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003468#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Daniel Drake80693ce2008-07-19 23:31:17 +01003469
3470 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003471 int ret;
Daniel Drake80693ce2008-07-19 23:31:17 +01003472 int search_freq;
3473
3474 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
3475 search_freq = bss->freq;
3476 else
3477 search_freq = local->hw.conf.channel->center_freq;
3478
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003479 bss = ieee80211_rx_bss_get(local, bssid, search_freq,
Daniel Drake80693ce2008-07-19 23:31:17 +01003480 ifsta->ssid, ifsta->ssid_len);
3481 if (!bss)
3482 goto dont_join;
3483
Joe Perches0795af52007-10-03 17:59:30 -07003484 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003485 " based on configured SSID\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003486 sdata->dev->name, print_mac(mac, bssid));
3487 ret = ieee80211_sta_join_ibss(sdata, ifsta, bss);
Johannes Berg3e122be2008-07-09 14:40:34 +02003488 ieee80211_rx_bss_put(local, bss);
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003489 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003490 }
Daniel Drake80693ce2008-07-19 23:31:17 +01003491
3492dont_join:
Jiri Bencf0706e82007-05-05 11:45:53 -07003493#ifdef CONFIG_MAC80211_IBSS_DEBUG
3494 printk(KERN_DEBUG " did not try to join ibss\n");
3495#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3496
3497 /* Selected IBSS not found in current scan results - try to scan */
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003498 if (ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED &&
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003499 !ieee80211_sta_active_ibss(sdata)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003500 mod_timer(&ifsta->timer, jiffies +
3501 IEEE80211_IBSS_MERGE_INTERVAL);
3502 } else if (time_after(jiffies, local->last_scan_completed +
3503 IEEE80211_SCAN_INTERVAL)) {
3504 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003505 "join\n", sdata->dev->name);
3506 return ieee80211_sta_req_scan(sdata, ifsta->ssid,
Jiri Bencf0706e82007-05-05 11:45:53 -07003507 ifsta->ssid_len);
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003508 } else if (ifsta->state != IEEE80211_STA_MLME_IBSS_JOINED) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003509 int interval = IEEE80211_SCAN_INTERVAL;
3510
3511 if (time_after(jiffies, ifsta->ibss_join_req +
3512 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003513 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003514 (!(local->oper_channel->flags &
3515 IEEE80211_CHAN_NO_IBSS)))
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003516 return ieee80211_sta_create_ibss(sdata, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003517 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003518 printk(KERN_DEBUG "%s: IBSS not allowed on"
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003519 " %d MHz\n", sdata->dev->name,
Johannes Berg8318d782008-01-24 19:38:38 +01003520 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003521 }
3522
3523 /* No IBSS found - decrease scan interval and continue
3524 * scanning. */
3525 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3526 }
3527
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003528 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
Jiri Bencf0706e82007-05-05 11:45:53 -07003529 mod_timer(&ifsta->timer, jiffies + interval);
3530 return 0;
3531 }
3532
3533 return 0;
3534}
3535
3536
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003537int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003538{
Jiri Bencf0706e82007-05-05 11:45:53 -07003539 struct ieee80211_if_sta *ifsta;
Johannes Berg9d139c82008-07-09 14:40:37 +02003540 int res;
Jiri Bencf0706e82007-05-05 11:45:53 -07003541
3542 if (len > IEEE80211_MAX_SSID_LEN)
3543 return -EINVAL;
3544
Jiri Bencf0706e82007-05-05 11:45:53 -07003545 ifsta = &sdata->u.sta;
3546
Johannes Berg9d139c82008-07-09 14:40:37 +02003547 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
3548 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
3549 memcpy(ifsta->ssid, ssid, len);
3550 ifsta->ssid_len = len;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003551 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Johannes Berg9d139c82008-07-09 14:40:37 +02003552
3553 res = 0;
3554 /*
3555 * Hack! MLME code needs to be cleaned up to have different
3556 * entry points for configuration and internal selection change
3557 */
3558 if (netif_running(sdata->dev))
3559 res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
3560 if (res) {
3561 printk(KERN_DEBUG "%s: Failed to config new SSID to "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003562 "the low-level driver\n", sdata->dev->name);
Johannes Berg9d139c82008-07-09 14:40:37 +02003563 return res;
3564 }
3565 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003566
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003567 if (len)
3568 ifsta->flags |= IEEE80211_STA_SSID_SET;
3569 else
3570 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg9d139c82008-07-09 14:40:37 +02003571
Johannes Berg51fb61e2007-12-19 01:31:27 +01003572 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003573 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003574 ifsta->ibss_join_req = jiffies;
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003575 ifsta->state = IEEE80211_STA_MLME_IBSS_SEARCH;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003576 return ieee80211_sta_find_ibss(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003577 }
Johannes Berg9d139c82008-07-09 14:40:37 +02003578
Jiri Bencf0706e82007-05-05 11:45:53 -07003579 return 0;
3580}
3581
3582
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003583int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003584{
Jiri Bencf0706e82007-05-05 11:45:53 -07003585 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3586 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3587 *len = ifsta->ssid_len;
3588 return 0;
3589}
3590
3591
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003592int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
Jiri Bencf0706e82007-05-05 11:45:53 -07003593{
Jiri Bencf0706e82007-05-05 11:45:53 -07003594 struct ieee80211_if_sta *ifsta;
3595 int res;
3596
Jiri Bencf0706e82007-05-05 11:45:53 -07003597 ifsta = &sdata->u.sta;
3598
3599 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3600 memcpy(ifsta->bssid, bssid, ETH_ALEN);
Johannes Berg9d139c82008-07-09 14:40:37 +02003601 res = 0;
3602 /*
3603 * Hack! See also ieee80211_sta_set_ssid.
3604 */
3605 if (netif_running(sdata->dev))
3606 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
Jiri Bencf0706e82007-05-05 11:45:53 -07003607 if (res) {
3608 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003609 "the low-level driver\n", sdata->dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07003610 return res;
3611 }
3612 }
3613
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003614 if (is_valid_ether_addr(bssid))
3615 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003616 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003617 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3618
Jiri Bencf0706e82007-05-05 11:45:53 -07003619 return 0;
3620}
3621
3622
3623static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3624 struct ieee80211_sub_if_data *sdata,
3625 int powersave)
3626{
3627 struct sk_buff *skb;
3628 struct ieee80211_hdr *nullfunc;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003629 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003630
3631 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3632 if (!skb) {
3633 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3634 "frame\n", sdata->dev->name);
3635 return;
3636 }
3637 skb_reserve(skb, local->hw.extra_tx_headroom);
3638
3639 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3640 memset(nullfunc, 0, 24);
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003641 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3642 IEEE80211_FCTL_TODS);
Jiri Bencf0706e82007-05-05 11:45:53 -07003643 if (powersave)
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003644 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3645 nullfunc->frame_control = fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003646 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3647 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3648 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3649
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003650 ieee80211_sta_tx(sdata, skb, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003651}
3652
3653
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003654static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3655{
3656 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3657 ieee80211_vif_is_mesh(&sdata->vif))
3658 ieee80211_sta_timer((unsigned long)sdata);
3659}
3660
Jiri Bencf0706e82007-05-05 11:45:53 -07003661void ieee80211_scan_completed(struct ieee80211_hw *hw)
3662{
3663 struct ieee80211_local *local = hw_to_local(hw);
3664 struct net_device *dev = local->scan_dev;
3665 struct ieee80211_sub_if_data *sdata;
3666 union iwreq_data wrqu;
3667
3668 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003669 memset(&wrqu, 0, sizeof(wrqu));
3670 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003671
Zhu Yiece8edd2007-11-22 10:53:21 +08003672 if (local->sta_hw_scanning) {
3673 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003674 if (ieee80211_hw_config(local))
3675 printk(KERN_DEBUG "%s: failed to restore operational "
3676 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003677 /* Restart STA timer for HW scan case */
3678 rcu_read_lock();
3679 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3680 ieee80211_restart_sta_timer(sdata);
3681 rcu_read_unlock();
3682
Zhu Yiece8edd2007-11-22 10:53:21 +08003683 goto done;
3684 }
3685
3686 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003687 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003688 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003689 "channel after scan\n", dev->name);
3690
Johannes Berg4150c572007-09-17 01:29:23 -04003691
3692 netif_tx_lock_bh(local->mdev);
David S. Millere308a5d2008-07-15 00:13:44 -07003693 netif_addr_lock(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -04003694 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3695 local->ops->configure_filter(local_to_hw(local),
3696 FIF_BCN_PRBRESP_PROMISC,
3697 &local->filter_flags,
3698 local->mdev->mc_count,
3699 local->mdev->mc_list);
3700
David S. Millere308a5d2008-07-15 00:13:44 -07003701 netif_addr_unlock(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -04003702 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003703
Johannes Berg79010422007-09-18 17:29:21 -04003704 rcu_read_lock();
3705 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003706 /* Tell AP we're back */
3707 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3708 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3709 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003710
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003711 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003712
Jiri Bencf0706e82007-05-05 11:45:53 -07003713 netif_wake_queue(sdata->dev);
3714 }
Johannes Berg79010422007-09-18 17:29:21 -04003715 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003716
Zhu Yiece8edd2007-11-22 10:53:21 +08003717done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003718 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003719 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003720 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003721 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Tomas Winkler48c2fc52008-08-06 14:22:01 +03003722 (!(ifsta->state == IEEE80211_STA_MLME_IBSS_JOINED) &&
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003723 !ieee80211_sta_active_ibss(sdata)))
3724 ieee80211_sta_find_ibss(sdata, ifsta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003725 }
3726}
3727EXPORT_SYMBOL(ieee80211_scan_completed);
3728
3729void ieee80211_sta_scan_work(struct work_struct *work)
3730{
3731 struct ieee80211_local *local =
3732 container_of(work, struct ieee80211_local, scan_work.work);
3733 struct net_device *dev = local->scan_dev;
3734 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003735 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003736 struct ieee80211_channel *chan;
3737 int skip;
3738 unsigned long next_delay = 0;
3739
Zhu Yiece8edd2007-11-22 10:53:21 +08003740 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003741 return;
3742
3743 switch (local->scan_state) {
3744 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003745 /*
3746 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3747 * after we successfully scanned the last channel of the last
3748 * band (and the last band is supported by the hw)
3749 */
Johannes Berg8318d782008-01-24 19:38:38 +01003750 if (local->scan_band < IEEE80211_NUM_BANDS)
3751 sband = local->hw.wiphy->bands[local->scan_band];
3752 else
3753 sband = NULL;
3754
Johannes Berg69d464d2008-01-28 17:08:56 +01003755 /*
3756 * If we are at an unsupported band and have more bands
3757 * left to scan, advance to the next supported one.
3758 */
3759 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003760 local->scan_band++;
3761 sband = local->hw.wiphy->bands[local->scan_band];
3762 local->scan_channel_idx = 0;
3763 }
3764
Johannes Berg69d464d2008-01-28 17:08:56 +01003765 /* if no more bands/channels left, complete scan */
3766 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003767 ieee80211_scan_completed(local_to_hw(local));
3768 return;
3769 }
Johannes Berg8318d782008-01-24 19:38:38 +01003770 skip = 0;
3771 chan = &sband->channels[local->scan_channel_idx];
3772
3773 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003774 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003775 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003776 skip = 1;
3777
3778 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003779 local->scan_channel = chan;
3780 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003781 printk(KERN_DEBUG "%s: failed to set freq to "
3782 "%d MHz for scan\n", dev->name,
3783 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003784 skip = 1;
3785 }
3786 }
3787
Johannes Berg69d464d2008-01-28 17:08:56 +01003788 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003789 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003790 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003791 /*
3792 * scan_band may end up == IEEE80211_NUM_BANDS, but
3793 * we'll catch that case above and complete the scan
3794 * if that is the case.
3795 */
Johannes Berg8318d782008-01-24 19:38:38 +01003796 local->scan_band++;
3797 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003798 }
3799
3800 if (skip)
3801 break;
3802
3803 next_delay = IEEE80211_PROBE_DELAY +
3804 usecs_to_jiffies(local->hw.channel_change_time);
3805 local->scan_state = SCAN_SEND_PROBE;
3806 break;
3807 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003808 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003809 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003810
3811 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3812 break;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003813 ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
Johannes Berg8318d782008-01-24 19:38:38 +01003814 local->scan_ssid_len);
3815 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003816 break;
3817 }
3818
Zhu Yiece8edd2007-11-22 10:53:21 +08003819 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003820 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3821 next_delay);
3822}
3823
3824
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003825static int ieee80211_sta_start_scan(struct ieee80211_sub_if_data *scan_sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07003826 u8 *ssid, size_t ssid_len)
3827{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003828 struct ieee80211_local *local = scan_sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003829 struct ieee80211_sub_if_data *sdata;
3830
3831 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3832 return -EINVAL;
3833
3834 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3835 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3836 * BSSID: MACAddress
3837 * SSID
3838 * ScanType: ACTIVE, PASSIVE
3839 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3840 * a Probe frame during active scanning
3841 * ChannelList
3842 * MinChannelTime (>= ProbeDelay), in TU
3843 * MaxChannelTime: (>= MinChannelTime), in TU
3844 */
3845
3846 /* MLME-SCAN.confirm
3847 * BSSDescriptionSet
3848 * ResultCode: SUCCESS, INVALID_PARAMETERS
3849 */
3850
Zhu Yiece8edd2007-11-22 10:53:21 +08003851 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003852 if (local->scan_dev == scan_sdata->dev)
Jiri Bencf0706e82007-05-05 11:45:53 -07003853 return 0;
3854 return -EBUSY;
3855 }
3856
3857 if (local->ops->hw_scan) {
3858 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003859 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003860 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003861 local->sta_hw_scanning = 1;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003862 local->scan_dev = scan_sdata->dev;
Jiri Bencf0706e82007-05-05 11:45:53 -07003863 }
3864 return rc;
3865 }
3866
Zhu Yiece8edd2007-11-22 10:53:21 +08003867 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003868
Johannes Berg79010422007-09-18 17:29:21 -04003869 rcu_read_lock();
3870 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003871 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003872 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003873 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003874 ieee80211_send_nullfunc(local, sdata, 1);
3875 }
Johannes Berg79010422007-09-18 17:29:21 -04003876 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003877
3878 if (ssid) {
3879 local->scan_ssid_len = ssid_len;
3880 memcpy(local->scan_ssid, ssid, ssid_len);
3881 } else
3882 local->scan_ssid_len = 0;
3883 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07003884 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003885 local->scan_band = IEEE80211_BAND_2GHZ;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003886 local->scan_dev = scan_sdata->dev;
Jiri Bencf0706e82007-05-05 11:45:53 -07003887
David S. Millerb9e40852008-07-15 00:15:08 -07003888 netif_addr_lock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -04003889 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3890 local->ops->configure_filter(local_to_hw(local),
3891 FIF_BCN_PRBRESP_PROMISC,
3892 &local->filter_flags,
3893 local->mdev->mc_count,
3894 local->mdev->mc_list);
David S. Millerb9e40852008-07-15 00:15:08 -07003895 netif_addr_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003896
3897 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3898 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3899 IEEE80211_CHANNEL_TIME);
3900
3901 return 0;
3902}
3903
3904
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003905int ieee80211_sta_req_scan(struct ieee80211_sub_if_data *sdata, u8 *ssid, size_t ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07003906{
Jiri Bencf0706e82007-05-05 11:45:53 -07003907 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003908 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07003909
Johannes Berg51fb61e2007-12-19 01:31:27 +01003910 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003911 return ieee80211_sta_start_scan(sdata, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003912
Zhu Yiece8edd2007-11-22 10:53:21 +08003913 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003914 if (local->scan_dev == sdata->dev)
Jiri Bencf0706e82007-05-05 11:45:53 -07003915 return 0;
3916 return -EBUSY;
3917 }
3918
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003919 ifsta->scan_ssid_len = ssid_len;
3920 if (ssid_len)
3921 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003922 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3923 queue_work(local->hw.workqueue, &ifsta->work);
3924 return 0;
3925}
3926
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03003927
3928static void ieee80211_sta_add_scan_ies(struct iw_request_info *info,
3929 struct ieee80211_sta_bss *bss,
3930 char **current_ev, char *end_buf)
3931{
3932 u8 *pos, *end, *next;
3933 struct iw_event iwe;
3934
3935 if (bss == NULL || bss->ies == NULL)
3936 return;
3937
3938 /*
3939 * If needed, fragment the IEs buffer (at IE boundaries) into short
3940 * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
3941 */
3942 pos = bss->ies;
3943 end = pos + bss->ies_len;
3944
3945 while (end - pos > IW_GENERIC_IE_MAX) {
3946 next = pos + 2 + pos[1];
3947 while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
3948 next = next + 2 + next[1];
3949
3950 memset(&iwe, 0, sizeof(iwe));
3951 iwe.cmd = IWEVGENIE;
3952 iwe.u.data.length = next - pos;
3953 *current_ev = iwe_stream_add_point(info, *current_ev,
3954 end_buf, &iwe, pos);
3955
3956 pos = next;
3957 }
3958
3959 if (end > pos) {
3960 memset(&iwe, 0, sizeof(iwe));
3961 iwe.cmd = IWEVGENIE;
3962 iwe.u.data.length = end - pos;
3963 *current_ev = iwe_stream_add_point(info, *current_ev,
3964 end_buf, &iwe, pos);
3965 }
3966}
3967
3968
Jiri Bencf0706e82007-05-05 11:45:53 -07003969static char *
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12003970ieee80211_sta_scan_result(struct ieee80211_local *local,
David S. Millerccc58052008-06-16 18:50:49 -07003971 struct iw_request_info *info,
Jiri Bencf0706e82007-05-05 11:45:53 -07003972 struct ieee80211_sta_bss *bss,
3973 char *current_ev, char *end_buf)
3974{
Jiri Bencf0706e82007-05-05 11:45:53 -07003975 struct iw_event iwe;
3976
3977 if (time_after(jiffies,
3978 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3979 return current_ev;
3980
Jiri Bencf0706e82007-05-05 11:45:53 -07003981 memset(&iwe, 0, sizeof(iwe));
3982 iwe.cmd = SIOCGIWAP;
3983 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3984 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07003985 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07003986 IW_EV_ADDR_LEN);
3987
3988 memset(&iwe, 0, sizeof(iwe));
3989 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01003990 if (bss_mesh_cfg(bss)) {
3991 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003992 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07003993 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3994 &iwe, bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003995 } else {
3996 iwe.u.data.length = bss->ssid_len;
3997 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07003998 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
3999 &iwe, bss->ssid);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004000 }
Jiri Bencf0706e82007-05-05 11:45:53 -07004001
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08004002 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4003 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004004 memset(&iwe, 0, sizeof(iwe));
4005 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01004006 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004007 iwe.u.mode = IW_MODE_MESH;
4008 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004009 iwe.u.mode = IW_MODE_MASTER;
4010 else
4011 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07004012 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4013 &iwe, IW_EV_UINT_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -07004014 }
4015
4016 memset(&iwe, 0, sizeof(iwe));
4017 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01004018 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4019 iwe.u.freq.e = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004020 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004021 IW_EV_FREQ_LEN);
4022
4023 memset(&iwe, 0, sizeof(iwe));
Tomas Winkler9381be02008-05-23 01:36:36 +03004024 iwe.cmd = SIOCGIWFREQ;
4025 iwe.u.freq.m = bss->freq;
4026 iwe.u.freq.e = 6;
David S. Millerccc58052008-06-16 18:50:49 -07004027 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Tomas Winkler9381be02008-05-23 01:36:36 +03004028 IW_EV_FREQ_LEN);
4029 memset(&iwe, 0, sizeof(iwe));
Jiri Bencf0706e82007-05-05 11:45:53 -07004030 iwe.cmd = IWEVQUAL;
Bruno Randolf566bfe52008-05-08 19:15:40 +02004031 iwe.u.qual.qual = bss->qual;
4032 iwe.u.qual.level = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07004033 iwe.u.qual.noise = bss->noise;
4034 iwe.u.qual.updated = local->wstats_flags;
David S. Millerccc58052008-06-16 18:50:49 -07004035 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004036 IW_EV_QUAL_LEN);
4037
4038 memset(&iwe, 0, sizeof(iwe));
4039 iwe.cmd = SIOCGIWENCODE;
4040 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4041 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4042 else
4043 iwe.u.data.flags = IW_ENCODE_DISABLED;
4044 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004045 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4046 &iwe, "");
Jiri Bencf0706e82007-05-05 11:45:53 -07004047
Jouni Malinen43ac2ca2008-08-15 22:21:27 +03004048 ieee80211_sta_add_scan_ies(info, bss, &current_ev, end_buf);
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004049
Jiri Bencf0706e82007-05-05 11:45:53 -07004050 if (bss && bss->supp_rates_len > 0) {
4051 /* display all supported rates in readable format */
David S. Millerccc58052008-06-16 18:50:49 -07004052 char *p = current_ev + iwe_stream_lcp_len(info);
Jiri Bencf0706e82007-05-05 11:45:53 -07004053 int i;
4054
4055 memset(&iwe, 0, sizeof(iwe));
4056 iwe.cmd = SIOCGIWRATE;
4057 /* Those two flags are ignored... */
4058 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4059
4060 for (i = 0; i < bss->supp_rates_len; i++) {
4061 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4062 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07004063 p = iwe_stream_add_value(info, current_ev, p,
Jiri Bencf0706e82007-05-05 11:45:53 -07004064 end_buf, &iwe, IW_EV_PARAM_LEN);
4065 }
4066 current_ev = p;
4067 }
4068
4069 if (bss) {
4070 char *buf;
4071 buf = kmalloc(30, GFP_ATOMIC);
4072 if (buf) {
4073 memset(&iwe, 0, sizeof(iwe));
4074 iwe.cmd = IWEVCUSTOM;
4075 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4076 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004077 current_ev = iwe_stream_add_point(info, current_ev,
4078 end_buf,
Jiri Bencf0706e82007-05-05 11:45:53 -07004079 &iwe, buf);
Emmanuel Grumbachbf998f62008-06-24 13:38:00 +03004080 memset(&iwe, 0, sizeof(iwe));
4081 iwe.cmd = IWEVCUSTOM;
4082 sprintf(buf, " Last beacon: %dms ago",
4083 jiffies_to_msecs(jiffies - bss->last_update));
4084 iwe.u.data.length = strlen(buf);
4085 current_ev = iwe_stream_add_point(info, current_ev,
4086 end_buf, &iwe, buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004087 kfree(buf);
4088 }
4089 }
4090
Johannes Berg902acc72008-02-23 15:17:19 +01004091 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004092 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04004093 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004094 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004095 if (buf) {
4096 memset(&iwe, 0, sizeof(iwe));
4097 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04004098 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004099 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004100 current_ev = iwe_stream_add_point(info, current_ev,
4101 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004102 &iwe, buf);
4103 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04004104 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4105 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004106 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004107 current_ev = iwe_stream_add_point(info, current_ev,
4108 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004109 &iwe, buf);
4110 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04004111 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4112 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004113 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004114 current_ev = iwe_stream_add_point(info, current_ev,
4115 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004116 &iwe, buf);
4117 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04004118 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4119 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004120 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004121 current_ev = iwe_stream_add_point(info, current_ev,
4122 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004123 &iwe, buf);
4124 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04004125 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4126 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004127 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004128 current_ev = iwe_stream_add_point(info, current_ev,
4129 end_buf,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004130 &iwe, buf);
4131 kfree(buf);
4132 }
4133 }
4134
Jiri Bencf0706e82007-05-05 11:45:53 -07004135 return current_ev;
4136}
4137
4138
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004139int ieee80211_sta_scan_results(struct ieee80211_local *local,
David S. Millerccc58052008-06-16 18:50:49 -07004140 struct iw_request_info *info,
4141 char *buf, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07004142{
Jiri Bencf0706e82007-05-05 11:45:53 -07004143 char *current_ev = buf;
4144 char *end_buf = buf + len;
4145 struct ieee80211_sta_bss *bss;
4146
4147 spin_lock_bh(&local->sta_bss_lock);
4148 list_for_each_entry(bss, &local->sta_bss_list, list) {
4149 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4150 spin_unlock_bh(&local->sta_bss_lock);
4151 return -E2BIG;
4152 }
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004153 current_ev = ieee80211_sta_scan_result(local, info, bss,
David S. Millerccc58052008-06-16 18:50:49 -07004154 current_ev, end_buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004155 }
4156 spin_unlock_bh(&local->sta_bss_lock);
4157 return current_ev - buf;
4158}
4159
4160
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004161int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata, char *ie, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07004162{
Jiri Bencf0706e82007-05-05 11:45:53 -07004163 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg988c0f72008-04-17 19:21:22 +02004164
Jiri Bencf0706e82007-05-05 11:45:53 -07004165 kfree(ifsta->extra_ie);
4166 if (len == 0) {
4167 ifsta->extra_ie = NULL;
4168 ifsta->extra_ie_len = 0;
4169 return 0;
4170 }
4171 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4172 if (!ifsta->extra_ie) {
4173 ifsta->extra_ie_len = 0;
4174 return -ENOMEM;
4175 }
4176 memcpy(ifsta->extra_ie, ie, len);
4177 ifsta->extra_ie_len = len;
4178 return 0;
4179}
4180
4181
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004182struct sta_info *ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata,
Johannes Berg988c0f72008-04-17 19:21:22 +02004183 struct sk_buff *skb, u8 *bssid,
Vladimir Koutny87291c02008-06-13 16:50:44 +02004184 u8 *addr, u64 supp_rates)
Jiri Bencf0706e82007-05-05 11:45:53 -07004185{
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004186 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -07004187 struct sta_info *sta;
Joe Perches0795af52007-10-03 17:59:30 -07004188 DECLARE_MAC_BUF(mac);
Vladimir Koutny87291c02008-06-13 16:50:44 +02004189 int band = local->hw.conf.channel->band;
Jiri Bencf0706e82007-05-05 11:45:53 -07004190
4191 /* TODO: Could consider removing the least recently used entry and
4192 * allow new one to be added. */
4193 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4194 if (net_ratelimit()) {
4195 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004196 "entry %s\n", sdata->dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004197 }
4198 return NULL;
4199 }
4200
Emmanuel Grumbach1e188632008-07-10 17:54:14 +03004201 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
Vladimir Koutny87291c02008-06-13 16:50:44 +02004202 return NULL;
4203
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004204#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07004205 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004206 wiphy_name(local->hw.wiphy), print_mac(mac, addr), sdata->dev->name);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004207#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07004208
Johannes Berg73651ee2008-02-25 16:27:47 +01004209 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4210 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004211 return NULL;
4212
Johannes Berg07346f812008-05-03 01:02:02 +02004213 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
Johannes Berg238814f2008-01-28 17:19:37 +01004214
Emmanuel Grumbach8e1535d2008-09-03 23:42:20 +03004215 /* make sure mandatory rates are always added */
4216 sta->supp_rates[band] = supp_rates |
4217 ieee80211_sta_get_mandatory_rates(local, band);
Jiri Bencf0706e82007-05-05 11:45:53 -07004218
4219 rate_control_rate_init(sta, local);
4220
Johannes Berg93e5deb2008-04-01 15:21:00 +02004221 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004222 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004223
Johannes Bergd0709a62008-02-25 16:27:46 +01004224 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004225}
4226
4227
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004228int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
Jiri Bencf0706e82007-05-05 11:45:53 -07004229{
Jiri Bencf0706e82007-05-05 11:45:53 -07004230 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4231
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004232 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004233 sdata->dev->name, reason);
Jiri Bencf0706e82007-05-05 11:45:53 -07004234
Johannes Berg51fb61e2007-12-19 01:31:27 +01004235 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4236 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004237 return -EINVAL;
4238
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004239 ieee80211_send_deauth(sdata, ifsta, reason);
4240 ieee80211_set_disassoc(sdata, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07004241 return 0;
4242}
4243
4244
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004245int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
Jiri Bencf0706e82007-05-05 11:45:53 -07004246{
Jiri Bencf0706e82007-05-05 11:45:53 -07004247 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4248
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004249 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004250 sdata->dev->name, reason);
Jiri Bencf0706e82007-05-05 11:45:53 -07004251
Johannes Berg51fb61e2007-12-19 01:31:27 +01004252 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004253 return -EINVAL;
4254
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004255 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004256 return -1;
4257
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004258 ieee80211_send_disassoc(sdata, ifsta, reason);
4259 ieee80211_set_disassoc(sdata, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07004260 return 0;
4261}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004262
4263void ieee80211_notify_mac(struct ieee80211_hw *hw,
4264 enum ieee80211_notification_types notif_type)
4265{
4266 struct ieee80211_local *local = hw_to_local(hw);
4267 struct ieee80211_sub_if_data *sdata;
4268
4269 switch (notif_type) {
4270 case IEEE80211_NOTIFY_RE_ASSOC:
4271 rcu_read_lock();
4272 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg3e122be2008-07-09 14:40:34 +02004273 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4274 continue;
Mohamed Abbas84363e62008-04-04 16:59:58 -07004275
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12004276 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
Mohamed Abbas84363e62008-04-04 16:59:58 -07004277 }
4278 rcu_read_unlock();
4279 break;
4280 }
4281}
4282EXPORT_SYMBOL(ieee80211_notify_mac);