blob: bf130b6255abce53ad2ea9db4780c3876bb9c232 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
Jiri Bencf0706e82007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010027#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
33#include "ieee80211_rate.h"
Michael Buesch47f0c502007-09-27 15:10:44 +020034#include "ieee80211_led.h"
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010035#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
37#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40#define IEEE80211_ASSOC_MAX_TRIES 3
41#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010042#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070043#define IEEE80211_PROBE_INTERVAL (60 * HZ)
44#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45#define IEEE80211_SCAN_INTERVAL (2 * HZ)
46#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47#define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
48
49#define IEEE80211_PROBE_DELAY (HZ / 33)
50#define IEEE80211_CHANNEL_TIME (HZ / 33)
51#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010055#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070056
57#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
60#define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
61
62#define ERP_INFO_USE_PROTECTION BIT(1)
63
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020064/* mgmt header + 1 byte action code */
65#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
66
67#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
68#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
69#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020070#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
71#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020072
Ron Rindjunsky07db2182007-12-25 17:00:33 +020073/* next values represent the buffer size for A-MPDU frame.
74 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
75#define IEEE80211_MIN_AMPDU_BUF 0x8
76#define IEEE80211_MAX_AMPDU_BUF 0x40
77
Jiri Bencf0706e82007-05-05 11:45:53 -070078static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
79 u8 *ssid, size_t ssid_len);
80static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +010081ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -040082 u8 *ssid, u8 ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070083static void ieee80211_rx_bss_put(struct net_device *dev,
84 struct ieee80211_sta_bss *bss);
85static int ieee80211_sta_find_ibss(struct net_device *dev,
86 struct ieee80211_if_sta *ifsta);
87static int ieee80211_sta_wep_configured(struct net_device *dev);
88static int ieee80211_sta_start_scan(struct net_device *dev,
89 u8 *ssid, size_t ssid_len);
90static int ieee80211_sta_config_auth(struct net_device *dev,
91 struct ieee80211_if_sta *ifsta);
92
93
Luis Carlos Coboee385852008-02-23 15:17:11 +010094void ieee802_11_parse_elems(u8 *start, size_t len,
95 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -070096{
97 size_t left = len;
98 u8 *pos = start;
Jiri Bencf0706e82007-05-05 11:45:53 -070099
100 memset(elems, 0, sizeof(*elems));
101
102 while (left >= 2) {
103 u8 id, elen;
104
105 id = *pos++;
106 elen = *pos++;
107 left -= 2;
108
John W. Linville67a4cce2007-10-12 16:40:37 -0400109 if (elen > left)
110 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700111
112 switch (id) {
113 case WLAN_EID_SSID:
114 elems->ssid = pos;
115 elems->ssid_len = elen;
116 break;
117 case WLAN_EID_SUPP_RATES:
118 elems->supp_rates = pos;
119 elems->supp_rates_len = elen;
120 break;
121 case WLAN_EID_FH_PARAMS:
122 elems->fh_params = pos;
123 elems->fh_params_len = elen;
124 break;
125 case WLAN_EID_DS_PARAMS:
126 elems->ds_params = pos;
127 elems->ds_params_len = elen;
128 break;
129 case WLAN_EID_CF_PARAMS:
130 elems->cf_params = pos;
131 elems->cf_params_len = elen;
132 break;
133 case WLAN_EID_TIM:
134 elems->tim = pos;
135 elems->tim_len = elen;
136 break;
137 case WLAN_EID_IBSS_PARAMS:
138 elems->ibss_params = pos;
139 elems->ibss_params_len = elen;
140 break;
141 case WLAN_EID_CHALLENGE:
142 elems->challenge = pos;
143 elems->challenge_len = elen;
144 break;
145 case WLAN_EID_WPA:
146 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
147 pos[2] == 0xf2) {
148 /* Microsoft OUI (00:50:F2) */
149 if (pos[3] == 1) {
150 /* OUI Type 1 - WPA IE */
151 elems->wpa = pos;
152 elems->wpa_len = elen;
153 } else if (elen >= 5 && pos[3] == 2) {
154 if (pos[4] == 0) {
155 elems->wmm_info = pos;
156 elems->wmm_info_len = elen;
157 } else if (pos[4] == 1) {
158 elems->wmm_param = pos;
159 elems->wmm_param_len = elen;
160 }
161 }
162 }
163 break;
164 case WLAN_EID_RSN:
165 elems->rsn = pos;
166 elems->rsn_len = elen;
167 break;
168 case WLAN_EID_ERP_INFO:
169 elems->erp_info = pos;
170 elems->erp_info_len = elen;
171 break;
172 case WLAN_EID_EXT_SUPP_RATES:
173 elems->ext_supp_rates = pos;
174 elems->ext_supp_rates_len = elen;
175 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200176 case WLAN_EID_HT_CAPABILITY:
177 elems->ht_cap_elem = pos;
178 elems->ht_cap_elem_len = elen;
179 break;
180 case WLAN_EID_HT_EXTRA_INFO:
181 elems->ht_info_elem = pos;
182 elems->ht_info_elem_len = elen;
183 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100184 case WLAN_EID_MESH_ID:
185 elems->mesh_id = pos;
186 elems->mesh_id_len = elen;
187 break;
188 case WLAN_EID_MESH_CONFIG:
189 elems->mesh_config = pos;
190 elems->mesh_config_len = elen;
191 break;
192 case WLAN_EID_PEER_LINK:
193 elems->peer_link = pos;
194 elems->peer_link_len = elen;
195 break;
196 case WLAN_EID_PREQ:
197 elems->preq = pos;
198 elems->preq_len = elen;
199 break;
200 case WLAN_EID_PREP:
201 elems->prep = pos;
202 elems->prep_len = elen;
203 break;
204 case WLAN_EID_PERR:
205 elems->perr = pos;
206 elems->perr_len = elen;
207 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700208 default:
Jiri Bencf0706e82007-05-05 11:45:53 -0700209 break;
210 }
211
212 left -= elen;
213 pos += elen;
214 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700215}
216
217
Jiri Bencf0706e82007-05-05 11:45:53 -0700218static int ecw2cw(int ecw)
219{
Johannes Bergac2bf322008-02-14 00:30:35 +0100220 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700221}
222
Jiri Bencf0706e82007-05-05 11:45:53 -0700223static void ieee80211_sta_wmm_params(struct net_device *dev,
224 struct ieee80211_if_sta *ifsta,
225 u8 *wmm_param, size_t wmm_param_len)
226{
227 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
228 struct ieee80211_tx_queue_params params;
229 size_t left;
230 int count;
231 u8 *pos;
232
233 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
234 return;
235 count = wmm_param[6] & 0x0f;
236 if (count == ifsta->wmm_last_param_set)
237 return;
238 ifsta->wmm_last_param_set = count;
239
240 pos = wmm_param + 8;
241 left = wmm_param_len - 8;
242
243 memset(&params, 0, sizeof(params));
244
245 if (!local->ops->conf_tx)
246 return;
247
248 local->wmm_acm = 0;
249 for (; left >= 4; left -= 4, pos += 4) {
250 int aci = (pos[0] >> 5) & 0x03;
251 int acm = (pos[0] >> 4) & 0x01;
252 int queue;
253
254 switch (aci) {
255 case 1:
256 queue = IEEE80211_TX_QUEUE_DATA3;
257 if (acm) {
258 local->wmm_acm |= BIT(0) | BIT(3);
259 }
260 break;
261 case 2:
262 queue = IEEE80211_TX_QUEUE_DATA1;
263 if (acm) {
264 local->wmm_acm |= BIT(4) | BIT(5);
265 }
266 break;
267 case 3:
268 queue = IEEE80211_TX_QUEUE_DATA0;
269 if (acm) {
270 local->wmm_acm |= BIT(6) | BIT(7);
271 }
272 break;
273 case 0:
274 default:
275 queue = IEEE80211_TX_QUEUE_DATA2;
276 if (acm) {
277 local->wmm_acm |= BIT(1) | BIT(2);
278 }
279 break;
280 }
281
282 params.aifs = pos[0] & 0x0f;
283 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
284 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100285 params.txop = pos[2] | (pos[3] << 8);
286#ifdef CONFIG_MAC80211_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700287 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100288 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e82007-05-05 11:45:53 -0700289 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100290 params.cw_max, params.txop);
291#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700292 /* TODO: handle ACM (block TX, fallback to next lowest allowed
293 * AC for now) */
294 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
295 printk(KERN_DEBUG "%s: failed to set TX queue "
296 "parameters for queue %d\n", dev->name, queue);
297 }
298 }
299}
300
301
Johannes Berg471b3ef2007-12-28 14:32:58 +0100302static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
303 u8 erp_value)
Daniel Drake56282212007-07-10 19:32:10 +0200304{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100305 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Daniel Drake56282212007-07-10 19:32:10 +0200306 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100307 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
308 bool preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
Joe Perches0795af52007-10-03 17:59:30 -0700309 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100310 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200311
Johannes Berg471b3ef2007-12-28 14:32:58 +0100312 if (use_protection != bss_conf->use_cts_prot) {
Daniel Drake56282212007-07-10 19:32:10 +0200313 if (net_ratelimit()) {
314 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700315 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100316 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200317 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700318 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200319 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100320 bss_conf->use_cts_prot = use_protection;
321 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200322 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200323
Johannes Berg471b3ef2007-12-28 14:32:58 +0100324 if (preamble_mode != bss_conf->use_short_preamble) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200325 if (net_ratelimit()) {
326 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700327 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100328 sdata->dev->name,
Daniel Drake7e9ed182007-07-27 15:43:24 +0200329 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
330 "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700331 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200332 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100333 bss_conf->use_short_preamble = preamble_mode;
334 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200335 }
Daniel Draked9430a32007-07-27 15:43:24 +0200336
Johannes Berg471b3ef2007-12-28 14:32:58 +0100337 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200338}
339
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200340int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
341 struct ieee80211_ht_info *ht_info)
342{
343
344 if (ht_info == NULL)
345 return -EINVAL;
346
347 memset(ht_info, 0, sizeof(*ht_info));
348
349 if (ht_cap_ie) {
350 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
351
352 ht_info->ht_supported = 1;
353 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
354 ht_info->ampdu_factor =
355 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
356 ht_info->ampdu_density =
357 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
358 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
359 } else
360 ht_info->ht_supported = 0;
361
362 return 0;
363}
364
365int ieee80211_ht_addt_info_ie_to_ht_bss_info(
366 struct ieee80211_ht_addt_info *ht_add_info_ie,
367 struct ieee80211_ht_bss_info *bss_info)
368{
369 if (bss_info == NULL)
370 return -EINVAL;
371
372 memset(bss_info, 0, sizeof(*bss_info));
373
374 if (ht_add_info_ie) {
375 u16 op_mode;
376 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
377
378 bss_info->primary_channel = ht_add_info_ie->control_chan;
379 bss_info->bss_cap = ht_add_info_ie->ht_param;
380 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
381 }
382
383 return 0;
384}
Daniel Drake56282212007-07-10 19:32:10 +0200385
Jiri Bencf0706e82007-05-05 11:45:53 -0700386static void ieee80211_sta_send_associnfo(struct net_device *dev,
387 struct ieee80211_if_sta *ifsta)
388{
389 char *buf;
390 size_t len;
391 int i;
392 union iwreq_data wrqu;
393
394 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
395 return;
396
397 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200398 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700399 if (!buf)
400 return;
401
402 len = sprintf(buf, "ASSOCINFO(");
403 if (ifsta->assocreq_ies) {
404 len += sprintf(buf + len, "ReqIEs=");
405 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
406 len += sprintf(buf + len, "%02x",
407 ifsta->assocreq_ies[i]);
408 }
409 }
410 if (ifsta->assocresp_ies) {
411 if (ifsta->assocreq_ies)
412 len += sprintf(buf + len, " ");
413 len += sprintf(buf + len, "RespIEs=");
414 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
415 len += sprintf(buf + len, "%02x",
416 ifsta->assocresp_ies[i]);
417 }
418 }
419 len += sprintf(buf + len, ")");
420
421 if (len > IW_CUSTOM_MAX) {
422 len = sprintf(buf, "ASSOCRESPIE=");
423 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
424 len += sprintf(buf + len, "%02x",
425 ifsta->assocresp_ies[i]);
426 }
427 }
428
429 memset(&wrqu, 0, sizeof(wrqu));
430 wrqu.data.length = len;
431 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
432
433 kfree(buf);
434}
435
436
437static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400438 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200439 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700440{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100441 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
442 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700443 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100444 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700445
Jiri Bencf0706e82007-05-05 11:45:53 -0700446 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200447 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400448
449 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
450
Johannes Berg51fb61e2007-12-19 01:31:27 +0100451 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700452 return;
Daniel Drake56282212007-07-10 19:32:10 +0200453
John W. Linville65c107a2007-10-05 14:23:27 -0400454 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +0100455 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400456 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200457 if (bss) {
458 if (bss->has_erp_value)
Johannes Berg471b3ef2007-12-28 14:32:58 +0100459 changed |= ieee80211_handle_erp_ie(
460 sdata, bss->erp_value);
Daniel Drake56282212007-07-10 19:32:10 +0200461 ieee80211_rx_bss_put(dev, bss);
462 }
463
Jiri Bencf0706e82007-05-05 11:45:53 -0700464 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400465 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700466 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
467 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
468 ieee80211_sta_send_associnfo(dev, ifsta);
469 } else {
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700470 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400471 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700472 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200473 ieee80211_reset_erp_info(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700474 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
475 }
476 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
477 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
478 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200479 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100480
Tomas Winklerb2205252008-01-27 16:18:22 +0200481 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100482 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -0700483}
484
485static void ieee80211_set_disassoc(struct net_device *dev,
486 struct ieee80211_if_sta *ifsta, int deauth)
487{
488 if (deauth)
489 ifsta->auth_tries = 0;
490 ifsta->assoc_tries = 0;
491 ieee80211_set_associated(dev, ifsta, 0);
492}
493
Luis Carlos Coboee385852008-02-23 15:17:11 +0100494void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
495 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700496{
497 struct ieee80211_sub_if_data *sdata;
498 struct ieee80211_tx_packet_data *pkt_data;
499
500 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
501 skb->dev = sdata->local->mdev;
502 skb_set_mac_header(skb, 0);
503 skb_set_network_header(skb, 0);
504 skb_set_transport_header(skb, 0);
505
506 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
507 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
508 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400509 if (!encrypt)
510 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700511
512 dev_queue_xmit(skb);
513}
514
515
516static void ieee80211_send_auth(struct net_device *dev,
517 struct ieee80211_if_sta *ifsta,
518 int transaction, u8 *extra, size_t extra_len,
519 int encrypt)
520{
521 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
522 struct sk_buff *skb;
523 struct ieee80211_mgmt *mgmt;
524
525 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
526 sizeof(*mgmt) + 6 + extra_len);
527 if (!skb) {
528 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
529 "frame\n", dev->name);
530 return;
531 }
532 skb_reserve(skb, local->hw.extra_tx_headroom);
533
534 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
535 memset(mgmt, 0, 24 + 6);
536 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
537 IEEE80211_STYPE_AUTH);
538 if (encrypt)
539 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
540 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
541 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
542 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
543 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
544 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
545 ifsta->auth_transaction = transaction + 1;
546 mgmt->u.auth.status_code = cpu_to_le16(0);
547 if (extra)
548 memcpy(skb_put(skb, extra_len), extra, extra_len);
549
550 ieee80211_sta_tx(dev, skb, encrypt);
551}
552
553
554static void ieee80211_authenticate(struct net_device *dev,
555 struct ieee80211_if_sta *ifsta)
556{
Joe Perches0795af52007-10-03 17:59:30 -0700557 DECLARE_MAC_BUF(mac);
558
Jiri Bencf0706e82007-05-05 11:45:53 -0700559 ifsta->auth_tries++;
560 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700561 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700562 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700563 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700564 ifsta->state = IEEE80211_DISABLED;
565 return;
566 }
567
568 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700569 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
570 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700571
572 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
573
574 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
575}
576
577
578static void ieee80211_send_assoc(struct net_device *dev,
579 struct ieee80211_if_sta *ifsta)
580{
581 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700582 struct sk_buff *skb;
583 struct ieee80211_mgmt *mgmt;
584 u8 *pos, *ies;
585 int i, len;
586 u16 capab;
587 struct ieee80211_sta_bss *bss;
588 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100589 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700590
591 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
592 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
593 ifsta->ssid_len);
594 if (!skb) {
595 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
596 "frame\n", dev->name);
597 return;
598 }
599 skb_reserve(skb, local->hw.extra_tx_headroom);
600
Johannes Berg8318d782008-01-24 19:38:38 +0100601 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
602
Jiri Bencf0706e82007-05-05 11:45:53 -0700603 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100604
605 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
606 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
607 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
608 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
609 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700610 }
Johannes Berg8318d782008-01-24 19:38:38 +0100611
612 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
613 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400614 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700615 if (bss) {
616 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
617 capab |= WLAN_CAPABILITY_PRIVACY;
618 if (bss->wmm_ie) {
619 wmm = 1;
620 }
621 ieee80211_rx_bss_put(dev, bss);
622 }
623
624 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
625 memset(mgmt, 0, 24);
626 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
627 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
628 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
629
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400630 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700631 skb_put(skb, 10);
632 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
633 IEEE80211_STYPE_REASSOC_REQ);
634 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
635 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
636 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
637 ETH_ALEN);
638 } else {
639 skb_put(skb, 4);
640 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
641 IEEE80211_STYPE_ASSOC_REQ);
642 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
643 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
644 }
645
646 /* SSID */
647 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
648 *pos++ = WLAN_EID_SSID;
649 *pos++ = ifsta->ssid_len;
650 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
651
Johannes Berg8318d782008-01-24 19:38:38 +0100652 len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700653 if (len > 8)
654 len = 8;
655 pos = skb_put(skb, len + 2);
656 *pos++ = WLAN_EID_SUPP_RATES;
657 *pos++ = len;
658 for (i = 0; i < len; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100659 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700660 *pos++ = (u8) (rate / 5);
661 }
662
Johannes Berg8318d782008-01-24 19:38:38 +0100663 if (sband->n_bitrates > len) {
664 pos = skb_put(skb, sband->n_bitrates - len + 2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700665 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Johannes Berg8318d782008-01-24 19:38:38 +0100666 *pos++ = sband->n_bitrates - len;
667 for (i = len; i < sband->n_bitrates; i++) {
668 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700669 *pos++ = (u8) (rate / 5);
670 }
671 }
672
673 if (ifsta->extra_ie) {
674 pos = skb_put(skb, ifsta->extra_ie_len);
675 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
676 }
677
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400678 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700679 pos = skb_put(skb, 9);
680 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
681 *pos++ = 7; /* len */
682 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
683 *pos++ = 0x50;
684 *pos++ = 0xf2;
685 *pos++ = 2; /* WME */
686 *pos++ = 0; /* WME info */
687 *pos++ = 1; /* WME ver */
688 *pos++ = 0;
689 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200690 /* wmm support is a must to HT */
Johannes Berg8318d782008-01-24 19:38:38 +0100691 if (wmm && sband->ht_info.ht_supported) {
692 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200693 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
694 *pos++ = WLAN_EID_HT_CAPABILITY;
695 *pos++ = sizeof(struct ieee80211_ht_cap);
696 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
697 memcpy(pos, &tmp, sizeof(u16));
698 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100699 /* TODO: needs a define here for << 2 */
700 *pos++ = sband->ht_info.ampdu_factor |
701 (sband->ht_info.ampdu_density << 2);
702 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200703 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700704
705 kfree(ifsta->assocreq_ies);
706 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200707 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700708 if (ifsta->assocreq_ies)
709 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
710
711 ieee80211_sta_tx(dev, skb, 0);
712}
713
714
715static void ieee80211_send_deauth(struct net_device *dev,
716 struct ieee80211_if_sta *ifsta, u16 reason)
717{
718 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
719 struct sk_buff *skb;
720 struct ieee80211_mgmt *mgmt;
721
722 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
723 if (!skb) {
724 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
725 "frame\n", dev->name);
726 return;
727 }
728 skb_reserve(skb, local->hw.extra_tx_headroom);
729
730 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
731 memset(mgmt, 0, 24);
732 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
733 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
734 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
735 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
736 IEEE80211_STYPE_DEAUTH);
737 skb_put(skb, 2);
738 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
739
740 ieee80211_sta_tx(dev, skb, 0);
741}
742
743
744static void ieee80211_send_disassoc(struct net_device *dev,
745 struct ieee80211_if_sta *ifsta, u16 reason)
746{
747 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
748 struct sk_buff *skb;
749 struct ieee80211_mgmt *mgmt;
750
751 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
752 if (!skb) {
753 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
754 "frame\n", dev->name);
755 return;
756 }
757 skb_reserve(skb, local->hw.extra_tx_headroom);
758
759 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
760 memset(mgmt, 0, 24);
761 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
762 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
763 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
764 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
765 IEEE80211_STYPE_DISASSOC);
766 skb_put(skb, 2);
767 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
768
769 ieee80211_sta_tx(dev, skb, 0);
770}
771
772
773static int ieee80211_privacy_mismatch(struct net_device *dev,
774 struct ieee80211_if_sta *ifsta)
775{
John W. Linville65c107a2007-10-05 14:23:27 -0400776 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700777 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000778 int bss_privacy;
779 int wep_privacy;
780 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700781
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000782 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700783 return 0;
784
Johannes Berg8318d782008-01-24 19:38:38 +0100785 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
786 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400787 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700788 if (!bss)
789 return 0;
790
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000791 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
792 wep_privacy = !!ieee80211_sta_wep_configured(dev);
793 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700794
795 ieee80211_rx_bss_put(dev, bss);
796
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000797 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
798 return 0;
799
800 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700801}
802
803
804static void ieee80211_associate(struct net_device *dev,
805 struct ieee80211_if_sta *ifsta)
806{
Joe Perches0795af52007-10-03 17:59:30 -0700807 DECLARE_MAC_BUF(mac);
808
Jiri Bencf0706e82007-05-05 11:45:53 -0700809 ifsta->assoc_tries++;
810 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700811 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700812 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700813 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700814 ifsta->state = IEEE80211_DISABLED;
815 return;
816 }
817
818 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700819 printk(KERN_DEBUG "%s: associate with AP %s\n",
820 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700821 if (ieee80211_privacy_mismatch(dev, ifsta)) {
822 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
823 "mixed-cell disabled - abort association\n", dev->name);
824 ifsta->state = IEEE80211_DISABLED;
825 return;
826 }
827
828 ieee80211_send_assoc(dev, ifsta);
829
830 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
831}
832
833
834static void ieee80211_associated(struct net_device *dev,
835 struct ieee80211_if_sta *ifsta)
836{
837 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
838 struct sta_info *sta;
839 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700840 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700841
842 /* TODO: start monitoring current AP signal quality and number of
843 * missed beacons. Scan other channels every now and then and search
844 * for better APs. */
845 /* TODO: remove expired BSSes */
846
847 ifsta->state = IEEE80211_ASSOCIATED;
848
Johannes Bergd0709a62008-02-25 16:27:46 +0100849 rcu_read_lock();
850
Jiri Bencf0706e82007-05-05 11:45:53 -0700851 sta = sta_info_get(local, ifsta->bssid);
852 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700853 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
854 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700855 disassoc = 1;
856 } else {
857 disassoc = 0;
858 if (time_after(jiffies,
859 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400860 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700861 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700862 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -0700863 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700864 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700865 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +0100866 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400867 } else
Jiri Bencf0706e82007-05-05 11:45:53 -0700868 ieee80211_send_probe_req(dev, ifsta->bssid,
869 local->scan_ssid,
870 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400871 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700872 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400873 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700874 if (time_after(jiffies, ifsta->last_probe +
875 IEEE80211_PROBE_INTERVAL)) {
876 ifsta->last_probe = jiffies;
877 ieee80211_send_probe_req(dev, ifsta->bssid,
878 ifsta->ssid,
879 ifsta->ssid_len);
880 }
881 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700882 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100883
884 rcu_read_unlock();
885
886 if (disassoc && sta) {
887 synchronize_rcu();
888 rtnl_lock();
889 sta_info_destroy(sta);
890 rtnl_unlock();
891 }
892
Jiri Bencf0706e82007-05-05 11:45:53 -0700893 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -0500894 ifsta->state = IEEE80211_DISABLED;
895 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700896 } else {
897 mod_timer(&ifsta->timer, jiffies +
898 IEEE80211_MONITORING_INTERVAL);
899 }
900}
901
902
903static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
904 u8 *ssid, size_t ssid_len)
905{
906 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +0100907 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700908 struct sk_buff *skb;
909 struct ieee80211_mgmt *mgmt;
910 u8 *pos, *supp_rates, *esupp_rates = NULL;
911 int i;
912
913 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
914 if (!skb) {
915 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
916 "request\n", dev->name);
917 return;
918 }
919 skb_reserve(skb, local->hw.extra_tx_headroom);
920
921 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
922 memset(mgmt, 0, 24);
923 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
924 IEEE80211_STYPE_PROBE_REQ);
925 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
926 if (dst) {
927 memcpy(mgmt->da, dst, ETH_ALEN);
928 memcpy(mgmt->bssid, dst, ETH_ALEN);
929 } else {
930 memset(mgmt->da, 0xff, ETH_ALEN);
931 memset(mgmt->bssid, 0xff, ETH_ALEN);
932 }
933 pos = skb_put(skb, 2 + ssid_len);
934 *pos++ = WLAN_EID_SSID;
935 *pos++ = ssid_len;
936 memcpy(pos, ssid, ssid_len);
937
938 supp_rates = skb_put(skb, 2);
939 supp_rates[0] = WLAN_EID_SUPP_RATES;
940 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100941 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
942
943 for (i = 0; i < sband->n_bitrates; i++) {
944 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -0700945 if (esupp_rates) {
946 pos = skb_put(skb, 1);
947 esupp_rates[1]++;
948 } else if (supp_rates[1] == 8) {
949 esupp_rates = skb_put(skb, 3);
950 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
951 esupp_rates[1] = 1;
952 pos = &esupp_rates[2];
953 } else {
954 pos = skb_put(skb, 1);
955 supp_rates[1]++;
956 }
Johannes Berg8318d782008-01-24 19:38:38 +0100957 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -0700958 }
959
960 ieee80211_sta_tx(dev, skb, 0);
961}
962
963
964static int ieee80211_sta_wep_configured(struct net_device *dev)
965{
966 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
967 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -0400968 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -0700969 return 0;
970 return 1;
971}
972
973
974static void ieee80211_auth_completed(struct net_device *dev,
975 struct ieee80211_if_sta *ifsta)
976{
977 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400978 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700979 ieee80211_associate(dev, ifsta);
980}
981
982
983static void ieee80211_auth_challenge(struct net_device *dev,
984 struct ieee80211_if_sta *ifsta,
985 struct ieee80211_mgmt *mgmt,
986 size_t len)
987{
988 u8 *pos;
989 struct ieee802_11_elems elems;
990
991 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
992 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -0400993 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -0700994 if (!elems.challenge) {
995 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
996 "frame\n", dev->name);
997 return;
998 }
999 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1000 elems.challenge_len + 2, 1);
1001}
1002
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001003static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1004 u8 dialog_token, u16 status, u16 policy,
1005 u16 buf_size, u16 timeout)
1006{
1007 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1008 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1009 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1010 struct sk_buff *skb;
1011 struct ieee80211_mgmt *mgmt;
1012 u16 capab;
1013
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001014 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1015 sizeof(mgmt->u.action.u.addba_resp));
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001016 if (!skb) {
1017 printk(KERN_DEBUG "%s: failed to allocate buffer "
1018 "for addba resp frame\n", dev->name);
1019 return;
1020 }
1021
1022 skb_reserve(skb, local->hw.extra_tx_headroom);
1023 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1024 memset(mgmt, 0, 24);
1025 memcpy(mgmt->da, da, ETH_ALEN);
1026 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001027 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001028 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1029 else
1030 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1031 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1032 IEEE80211_STYPE_ACTION);
1033
1034 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1035 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1036 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1037 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1038
1039 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1040 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1041 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1042
1043 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1044 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1045 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1046
1047 ieee80211_sta_tx(dev, skb, 0);
1048
1049 return;
1050}
1051
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001052void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1053 u16 tid, u8 dialog_token, u16 start_seq_num,
1054 u16 agg_size, u16 timeout)
1055{
1056 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1057 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1058 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1059 struct sk_buff *skb;
1060 struct ieee80211_mgmt *mgmt;
1061 u16 capab;
1062
1063 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1064 sizeof(mgmt->u.action.u.addba_req));
1065
1066
1067 if (!skb) {
1068 printk(KERN_ERR "%s: failed to allocate buffer "
1069 "for addba request frame\n", dev->name);
1070 return;
1071 }
1072 skb_reserve(skb, local->hw.extra_tx_headroom);
1073 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1074 memset(mgmt, 0, 24);
1075 memcpy(mgmt->da, da, ETH_ALEN);
1076 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1077 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1078 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1079 else
1080 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1081
1082 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1083 IEEE80211_STYPE_ACTION);
1084
1085 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1086
1087 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1088 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1089
1090 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1091 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1092 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1093 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1094
1095 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1096
1097 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1098 mgmt->u.action.u.addba_req.start_seq_num =
1099 cpu_to_le16(start_seq_num << 4);
1100
1101 ieee80211_sta_tx(dev, skb, 0);
1102}
1103
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001104static void ieee80211_sta_process_addba_request(struct net_device *dev,
1105 struct ieee80211_mgmt *mgmt,
1106 size_t len)
1107{
1108 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001109 struct ieee80211_hw *hw = &local->hw;
1110 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001111 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001112 struct tid_ampdu_rx *tid_agg_rx;
1113 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001114 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001115 int ret = -EOPNOTSUPP;
1116 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001117
Johannes Bergd0709a62008-02-25 16:27:46 +01001118 rcu_read_lock();
1119
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001120 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001121 if (!sta) {
1122 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001123 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001124 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001125
1126 /* extract session parameters from addba request frame */
1127 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1128 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001129 start_seq_num =
1130 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001131
1132 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1133 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1134 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1135 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1136
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001137 status = WLAN_STATUS_REQUEST_DECLINED;
1138
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001139 /* sanity check for incoming parameters:
1140 * check if configuration can support the BA policy
1141 * and if buffer size does not exceeds max value */
1142 if (((ba_policy != 1)
1143 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1144 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1145 status = WLAN_STATUS_INVALID_QOS_PARAM;
1146#ifdef CONFIG_MAC80211_HT_DEBUG
1147 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001148 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001149 "%s on tid %u. policy %d, buffer size %d\n",
1150 print_mac(mac, mgmt->sa), tid, ba_policy,
1151 buf_size);
1152#endif /* CONFIG_MAC80211_HT_DEBUG */
1153 goto end_no_lock;
1154 }
1155 /* determine default buffer size */
1156 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001157 struct ieee80211_supported_band *sband;
1158
1159 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001160 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001161 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001162 }
1163
1164 tid_agg_rx = &sta->ampdu_mlme.tid_rx[tid];
1165
1166 /* examine state machine */
1167 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1168
1169 if (tid_agg_rx->state != HT_AGG_STATE_IDLE) {
1170#ifdef CONFIG_MAC80211_HT_DEBUG
1171 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001172 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001173 "%s on tid %u\n",
1174 print_mac(mac, mgmt->sa), tid);
1175#endif /* CONFIG_MAC80211_HT_DEBUG */
1176 goto end;
1177 }
1178
1179 /* prepare reordering buffer */
1180 tid_agg_rx->reorder_buf =
1181 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001182 if (!tid_agg_rx->reorder_buf) {
1183 if (net_ratelimit())
1184 printk(KERN_ERR "can not allocate reordering buffer "
1185 "to tid %d\n", tid);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001186 goto end;
1187 }
1188 memset(tid_agg_rx->reorder_buf, 0,
1189 buf_size * sizeof(struct sk_buf *));
1190
1191 if (local->ops->ampdu_action)
1192 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001193 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001194#ifdef CONFIG_MAC80211_HT_DEBUG
1195 printk(KERN_DEBUG "Rx A-MPDU on tid %d result %d", tid, ret);
1196#endif /* CONFIG_MAC80211_HT_DEBUG */
1197
1198 if (ret) {
1199 kfree(tid_agg_rx->reorder_buf);
1200 goto end;
1201 }
1202
1203 /* change state and send addba resp */
1204 tid_agg_rx->state = HT_AGG_STATE_OPERATIONAL;
1205 tid_agg_rx->dialog_token = dialog_token;
1206 tid_agg_rx->ssn = start_seq_num;
1207 tid_agg_rx->head_seq_num = start_seq_num;
1208 tid_agg_rx->buf_size = buf_size;
1209 tid_agg_rx->timeout = timeout;
1210 tid_agg_rx->stored_mpdu_num = 0;
1211 status = WLAN_STATUS_SUCCESS;
1212end:
1213 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1214
1215end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001216 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1217 dialog_token, status, 1, buf_size, timeout);
1218 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001219}
Jiri Bencf0706e82007-05-05 11:45:53 -07001220
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001221static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1222 struct ieee80211_mgmt *mgmt,
1223 size_t len)
1224{
1225 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1226 struct ieee80211_hw *hw = &local->hw;
1227 struct sta_info *sta;
1228 u16 capab;
1229 u16 tid;
1230 u8 *state;
1231
Johannes Bergd0709a62008-02-25 16:27:46 +01001232 rcu_read_lock();
1233
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001234 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001235 if (!sta) {
1236 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001237 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001238 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001239
1240 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1241 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1242
1243 state = &sta->ampdu_mlme.tid_tx[tid].state;
1244
1245 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1246
1247 if (mgmt->u.action.u.addba_resp.dialog_token !=
1248 sta->ampdu_mlme.tid_tx[tid].dialog_token) {
1249 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1250#ifdef CONFIG_MAC80211_HT_DEBUG
1251 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1252#endif /* CONFIG_MAC80211_HT_DEBUG */
Johannes Bergd0709a62008-02-25 16:27:46 +01001253 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001254 return;
1255 }
1256
1257 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid].addba_resp_timer);
1258#ifdef CONFIG_MAC80211_HT_DEBUG
1259 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1260#endif /* CONFIG_MAC80211_HT_DEBUG */
1261 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1262 == WLAN_STATUS_SUCCESS) {
1263 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1264 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1265 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1266 "%d\n", *state);
Johannes Bergd0709a62008-02-25 16:27:46 +01001267 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001268 return;
1269 }
1270
1271 if (*state & HT_ADDBA_RECEIVED_MSK)
1272 printk(KERN_DEBUG "double addBA response\n");
1273
1274 *state |= HT_ADDBA_RECEIVED_MSK;
1275 sta->ampdu_mlme.tid_tx[tid].addba_req_num = 0;
1276
1277 if (*state == HT_AGG_STATE_OPERATIONAL) {
1278 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1279 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1280 }
1281
1282 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1283 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1284 } else {
1285 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1286
1287 sta->ampdu_mlme.tid_tx[tid].addba_req_num++;
1288 /* this will allow the state check in stop_BA_session */
1289 *state = HT_AGG_STATE_OPERATIONAL;
1290 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1291 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1292 WLAN_BACK_INITIATOR);
1293 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001294 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001295}
1296
1297void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1298 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001299{
1300 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1301 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1302 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1303 struct sk_buff *skb;
1304 struct ieee80211_mgmt *mgmt;
1305 u16 params;
1306
1307 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1308 sizeof(mgmt->u.action.u.delba));
1309
1310 if (!skb) {
1311 printk(KERN_ERR "%s: failed to allocate buffer "
1312 "for delba frame\n", dev->name);
1313 return;
1314 }
1315
1316 skb_reserve(skb, local->hw.extra_tx_headroom);
1317 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1318 memset(mgmt, 0, 24);
1319 memcpy(mgmt->da, da, ETH_ALEN);
1320 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001321 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001322 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1323 else
1324 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1325 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1326 IEEE80211_STYPE_ACTION);
1327
1328 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1329
1330 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1331 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1332 params = (u16)(initiator << 11); /* bit 11 initiator */
1333 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1334
1335 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1336 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1337
1338 ieee80211_sta_tx(dev, skb, 0);
1339}
1340
1341void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1342 u16 initiator, u16 reason)
1343{
1344 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1345 struct ieee80211_hw *hw = &local->hw;
1346 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001347 int ret, i;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001348
Johannes Bergd0709a62008-02-25 16:27:46 +01001349 rcu_read_lock();
1350
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001351 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001352 if (!sta) {
1353 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001354 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001355 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001356
1357 /* check if TID is in operational state */
1358 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1359 if (sta->ampdu_mlme.tid_rx[tid].state
1360 != HT_AGG_STATE_OPERATIONAL) {
1361 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Johannes Bergd0709a62008-02-25 16:27:46 +01001362 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001363 return;
1364 }
1365 sta->ampdu_mlme.tid_rx[tid].state =
1366 HT_AGG_STATE_REQ_STOP_BA_MSK |
1367 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1368 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1369
1370 /* stop HW Rx aggregation. ampdu_action existence
1371 * already verified in session init so we add the BUG_ON */
1372 BUG_ON(!local->ops->ampdu_action);
1373
1374 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001375 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001376 if (ret)
1377 printk(KERN_DEBUG "HW problem - can not stop rx "
1378 "aggergation for tid %d\n", tid);
1379
1380 /* shutdown timer has not expired */
1381 if (initiator != WLAN_BACK_TIMER)
1382 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid].
1383 session_timer);
1384
1385 /* check if this is a self generated aggregation halt */
1386 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1387 ieee80211_send_delba(dev, ra, tid, 0, reason);
1388
1389 /* free the reordering buffer */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001390 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid].buf_size; i++) {
1391 if (sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]) {
1392 /* release the reordered frames */
1393 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]);
1394 sta->ampdu_mlme.tid_rx[tid].stored_mpdu_num--;
1395 sta->ampdu_mlme.tid_rx[tid].reorder_buf[i] = NULL;
1396 }
1397 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001398 kfree(sta->ampdu_mlme.tid_rx[tid].reorder_buf);
1399
1400 sta->ampdu_mlme.tid_rx[tid].state = HT_AGG_STATE_IDLE;
Johannes Bergd0709a62008-02-25 16:27:46 +01001401 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001402}
1403
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001404
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001405static void ieee80211_sta_process_delba(struct net_device *dev,
1406 struct ieee80211_mgmt *mgmt, size_t len)
1407{
1408 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1409 struct sta_info *sta;
1410 u16 tid, params;
1411 u16 initiator;
1412 DECLARE_MAC_BUF(mac);
1413
Johannes Bergd0709a62008-02-25 16:27:46 +01001414 rcu_read_lock();
1415
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001416 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001417 if (!sta) {
1418 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001419 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001420 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001421
1422 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1423 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1424 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1425
1426#ifdef CONFIG_MAC80211_HT_DEBUG
1427 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001428 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1429 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001430 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001431 mgmt->u.action.u.delba.reason_code);
1432#endif /* CONFIG_MAC80211_HT_DEBUG */
1433
1434 if (initiator == WLAN_BACK_INITIATOR)
1435 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1436 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001437 else { /* WLAN_BACK_RECIPIENT */
1438 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1439 sta->ampdu_mlme.tid_tx[tid].state =
1440 HT_AGG_STATE_OPERATIONAL;
1441 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1442 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1443 WLAN_BACK_RECIPIENT);
1444 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001445 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001446}
1447
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001448/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001449 * After sending add Block Ack request we activated a timer until
1450 * add Block Ack response will arrive from the recipient.
1451 * If this timer expires sta_addba_resp_timer_expired will be executed.
1452 */
1453void sta_addba_resp_timer_expired(unsigned long data)
1454{
1455 /* not an elegant detour, but there is no choice as the timer passes
1456 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001457 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001458 * array gives the sta through container_of */
1459 u16 tid = *(int *)data;
1460 struct sta_info *temp_sta = container_of((void *)data,
1461 struct sta_info, timer_to_tid[tid]);
1462
1463 struct ieee80211_local *local = temp_sta->local;
1464 struct ieee80211_hw *hw = &local->hw;
1465 struct sta_info *sta;
1466 u8 *state;
1467
Johannes Bergd0709a62008-02-25 16:27:46 +01001468 rcu_read_lock();
1469
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001470 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001471 if (!sta) {
1472 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001473 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001474 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001475
1476 state = &sta->ampdu_mlme.tid_tx[tid].state;
1477 /* check if the TID waits for addBA response */
1478 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1479 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1480 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1481 *state = HT_AGG_STATE_IDLE;
1482 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1483 "expecting addBA response there", tid);
1484 goto timer_expired_exit;
1485 }
1486
1487 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1488
1489 /* go through the state check in stop_BA_session */
1490 *state = HT_AGG_STATE_OPERATIONAL;
1491 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1492 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1493 WLAN_BACK_INITIATOR);
1494
1495timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001496 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001497}
1498
1499/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001500 * After accepting the AddBA Request we activated a timer,
1501 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001502 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1503 */
1504void sta_rx_agg_session_timer_expired(unsigned long data)
1505{
1506 /* not an elegant detour, but there is no choice as the timer passes
1507 * only one argument, and verious sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001508 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001509 * array gives the sta through container_of */
1510 u8 *ptid = (u8 *)data;
1511 u8 *timer_to_id = ptid - *ptid;
1512 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1513 timer_to_tid[0]);
1514
1515 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergd0709a62008-02-25 16:27:46 +01001516 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1517 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001518 WLAN_REASON_QSTA_TIMEOUT);
1519}
1520
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001521void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1522{
1523 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1524 int i;
1525
1526 for (i = 0; i < STA_TID_NUM; i++) {
1527 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1528 WLAN_BACK_INITIATOR);
1529 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1530 WLAN_BACK_RECIPIENT,
1531 WLAN_REASON_QSTA_LEAVE_QBSS);
1532 }
1533}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001534
Jiri Bencf0706e82007-05-05 11:45:53 -07001535static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1536 struct ieee80211_if_sta *ifsta,
1537 struct ieee80211_mgmt *mgmt,
1538 size_t len)
1539{
1540 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1541 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001542 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001543
1544 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Berg51fb61e2007-12-19 01:31:27 +01001545 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001546 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001547 "%s, but not in authenticate state - ignored\n",
1548 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001549 return;
1550 }
1551
1552 if (len < 24 + 6) {
1553 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001554 "received from %s - ignored\n",
1555 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001556 return;
1557 }
1558
Johannes Berg51fb61e2007-12-19 01:31:27 +01001559 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001560 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1561 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001562 "unknown AP (SA=%s BSSID=%s) - "
1563 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1564 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001565 return;
1566 }
1567
Johannes Berg51fb61e2007-12-19 01:31:27 +01001568 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001569 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1570 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001571 "unknown BSSID (SA=%s BSSID=%s) - "
1572 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1573 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001574 return;
1575 }
1576
1577 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1578 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1579 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1580
Joe Perches0795af52007-10-03 17:59:30 -07001581 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e82007-05-05 11:45:53 -07001582 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001583 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e82007-05-05 11:45:53 -07001584 auth_transaction, status_code);
1585
Johannes Berg51fb61e2007-12-19 01:31:27 +01001586 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001587 /* IEEE 802.11 standard does not require authentication in IBSS
1588 * networks and most implementations do not seem to use it.
1589 * However, try to reply to authentication attempts if someone
1590 * has actually implemented this.
1591 * TODO: Could implement shared key authentication. */
1592 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1593 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1594 "frame (alg=%d transaction=%d)\n",
1595 dev->name, auth_alg, auth_transaction);
1596 return;
1597 }
1598 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1599 }
1600
1601 if (auth_alg != ifsta->auth_alg ||
1602 auth_transaction != ifsta->auth_transaction) {
1603 printk(KERN_DEBUG "%s: unexpected authentication frame "
1604 "(alg=%d transaction=%d)\n",
1605 dev->name, auth_alg, auth_transaction);
1606 return;
1607 }
1608
1609 if (status_code != WLAN_STATUS_SUCCESS) {
1610 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1611 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1612 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1613 u8 algs[3];
1614 const int num_algs = ARRAY_SIZE(algs);
1615 int i, pos;
1616 algs[0] = algs[1] = algs[2] = 0xff;
1617 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1618 algs[0] = WLAN_AUTH_OPEN;
1619 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1620 algs[1] = WLAN_AUTH_SHARED_KEY;
1621 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1622 algs[2] = WLAN_AUTH_LEAP;
1623 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1624 pos = 0;
1625 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1626 pos = 1;
1627 else
1628 pos = 2;
1629 for (i = 0; i < num_algs; i++) {
1630 pos++;
1631 if (pos >= num_algs)
1632 pos = 0;
1633 if (algs[pos] == ifsta->auth_alg ||
1634 algs[pos] == 0xff)
1635 continue;
1636 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1637 !ieee80211_sta_wep_configured(dev))
1638 continue;
1639 ifsta->auth_alg = algs[pos];
1640 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1641 "next try\n",
1642 dev->name, ifsta->auth_alg);
1643 break;
1644 }
1645 }
1646 return;
1647 }
1648
1649 switch (ifsta->auth_alg) {
1650 case WLAN_AUTH_OPEN:
1651 case WLAN_AUTH_LEAP:
1652 ieee80211_auth_completed(dev, ifsta);
1653 break;
1654 case WLAN_AUTH_SHARED_KEY:
1655 if (ifsta->auth_transaction == 4)
1656 ieee80211_auth_completed(dev, ifsta);
1657 else
1658 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1659 break;
1660 }
1661}
1662
1663
1664static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1665 struct ieee80211_if_sta *ifsta,
1666 struct ieee80211_mgmt *mgmt,
1667 size_t len)
1668{
1669 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001670 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001671
1672 if (len < 24 + 2) {
1673 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001674 "received from %s - ignored\n",
1675 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001676 return;
1677 }
1678
1679 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1680 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001681 "unknown AP (SA=%s BSSID=%s) - "
1682 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1683 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001684 return;
1685 }
1686
1687 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1688
Joe Perches0795af52007-10-03 17:59:30 -07001689 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001690 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001691 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001692
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001693 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001694 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1695 }
1696
1697 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1698 ifsta->state == IEEE80211_ASSOCIATE ||
1699 ifsta->state == IEEE80211_ASSOCIATED) {
1700 ifsta->state = IEEE80211_AUTHENTICATE;
1701 mod_timer(&ifsta->timer, jiffies +
1702 IEEE80211_RETRY_AUTH_INTERVAL);
1703 }
1704
1705 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001706 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001707}
1708
1709
1710static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1711 struct ieee80211_if_sta *ifsta,
1712 struct ieee80211_mgmt *mgmt,
1713 size_t len)
1714{
1715 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001716 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001717
1718 if (len < 24 + 2) {
1719 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001720 "received from %s - ignored\n",
1721 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001722 return;
1723 }
1724
1725 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1726 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001727 "unknown AP (SA=%s BSSID=%s) - "
1728 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1729 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001730 return;
1731 }
1732
1733 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1734
Joe Perches0795af52007-10-03 17:59:30 -07001735 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001736 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001737 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001738
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001739 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001740 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1741
1742 if (ifsta->state == IEEE80211_ASSOCIATED) {
1743 ifsta->state = IEEE80211_ASSOCIATE;
1744 mod_timer(&ifsta->timer, jiffies +
1745 IEEE80211_RETRY_AUTH_INTERVAL);
1746 }
1747
1748 ieee80211_set_disassoc(dev, ifsta, 0);
1749}
1750
1751
Johannes Berg471b3ef2007-12-28 14:32:58 +01001752static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001753 struct ieee80211_if_sta *ifsta,
1754 struct ieee80211_mgmt *mgmt,
1755 size_t len,
1756 int reassoc)
1757{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001758 struct ieee80211_local *local = sdata->local;
1759 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01001760 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001761 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001762 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001763 u16 capab_info, status_code, aid;
1764 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001765 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001766 u8 *pos;
1767 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001768 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001769 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001770
1771 /* AssocResp and ReassocResp have identical structure, so process both
1772 * of them in this function. */
1773
1774 if (ifsta->state != IEEE80211_ASSOCIATE) {
1775 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001776 "%s, but not in associate state - ignored\n",
1777 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001778 return;
1779 }
1780
1781 if (len < 24 + 6) {
1782 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001783 "received from %s - ignored\n",
1784 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001785 return;
1786 }
1787
1788 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1789 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001790 "unknown AP (SA=%s BSSID=%s) - "
1791 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1792 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001793 return;
1794 }
1795
1796 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1797 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1798 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001799
Joe Perches0795af52007-10-03 17:59:30 -07001800 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001801 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001802 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001803 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001804
1805 if (status_code != WLAN_STATUS_SUCCESS) {
1806 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1807 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001808 /* if this was a reassociation, ensure we try a "full"
1809 * association next time. This works around some broken APs
1810 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001811 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07001812 return;
1813 }
1814
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001815 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1816 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1817 "set\n", dev->name, aid);
1818 aid &= ~(BIT(15) | BIT(14));
1819
Jiri Bencf0706e82007-05-05 11:45:53 -07001820 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001821 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001822
1823 if (!elems.supp_rates) {
1824 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1825 dev->name);
1826 return;
1827 }
1828
1829 printk(KERN_DEBUG "%s: associated\n", dev->name);
1830 ifsta->aid = aid;
1831 ifsta->ap_capab = capab_info;
1832
1833 kfree(ifsta->assocresp_ies);
1834 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001835 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001836 if (ifsta->assocresp_ies)
1837 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1838
Johannes Bergd0709a62008-02-25 16:27:46 +01001839 rcu_read_lock();
1840
Jiri Bencf0706e82007-05-05 11:45:53 -07001841 /* Add STA entry for the AP */
1842 sta = sta_info_get(local, ifsta->bssid);
1843 if (!sta) {
1844 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01001845 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01001846
Johannes Berg73651ee2008-02-25 16:27:47 +01001847 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1848 if (!sta) {
1849 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1850 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01001851 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001852 return;
1853 }
John W. Linville65c107a2007-10-05 14:23:27 -04001854 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001855 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001856 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001857 if (bss) {
1858 sta->last_rssi = bss->rssi;
1859 sta->last_signal = bss->signal;
1860 sta->last_noise = bss->noise;
1861 ieee80211_rx_bss_put(dev, bss);
1862 }
Johannes Berg73651ee2008-02-25 16:27:47 +01001863
1864 err = sta_info_insert(sta);
1865 if (err) {
1866 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1867 " the AP (error %d)\n", dev->name, err);
1868 sta_info_destroy(sta);
1869 rcu_read_unlock();
1870 return;
1871 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001872 }
1873
Johannes Berg73651ee2008-02-25 16:27:47 +01001874 /*
1875 * FIXME: Do we really need to update the sta_info's information here?
1876 * We already know about the AP (we found it in our list) so it
1877 * should already be filled with the right info, no?
1878 * As is stands, all this is racy because typically we assume
1879 * the information that is filled in here (except flags) doesn't
1880 * change while a STA structure is alive. As such, it should move
1881 * to between the sta_info_alloc() and sta_info_insert() above.
1882 */
1883
Johannes Berg238814f2008-01-28 17:19:37 +01001884 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1885 WLAN_STA_AUTHORIZED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001886
1887 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001888 basic_rates = 0;
1889 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1890
Jiri Bencf0706e82007-05-05 11:45:53 -07001891 for (i = 0; i < elems.supp_rates_len; i++) {
1892 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001893
1894 if (rate > 110)
1895 have_higher_than_11mbit = true;
1896
1897 for (j = 0; j < sband->n_bitrates; j++) {
1898 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001899 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001900 if (elems.supp_rates[i] & 0x80)
1901 basic_rates |= BIT(j);
1902 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001903 }
Johannes Berg8318d782008-01-24 19:38:38 +01001904
Jiri Bencf0706e82007-05-05 11:45:53 -07001905 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1906 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001907
1908 if (rate > 110)
1909 have_higher_than_11mbit = true;
1910
1911 for (j = 0; j < sband->n_bitrates; j++) {
1912 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001913 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001914 if (elems.ext_supp_rates[i] & 0x80)
1915 basic_rates |= BIT(j);
1916 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001917 }
Johannes Berg8318d782008-01-24 19:38:38 +01001918
1919 sta->supp_rates[local->hw.conf.channel->band] = rates;
1920 sdata->basic_rates = basic_rates;
1921
1922 /* cf. IEEE 802.11 9.2.12 */
1923 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1924 have_higher_than_11mbit)
1925 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1926 else
1927 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001928
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001929 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1930 local->ops->conf_ht) {
1931 struct ieee80211_ht_bss_info bss_info;
1932
1933 ieee80211_ht_cap_ie_to_ht_info(
1934 (struct ieee80211_ht_cap *)
1935 elems.ht_cap_elem, &sta->ht_info);
1936 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1937 (struct ieee80211_ht_addt_info *)
1938 elems.ht_info_elem, &bss_info);
1939 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
1940 }
1941
Jiri Bencf0706e82007-05-05 11:45:53 -07001942 rate_control_rate_init(sta, local);
1943
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001944 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001945 sta->flags |= WLAN_STA_WME;
Johannes Berge5f98f22008-03-05 20:39:31 +01001946 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001947 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1948 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01001949 } else
1950 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001951
Johannes Berg8318d782008-01-24 19:38:38 +01001952 /* set AID, ieee80211_set_associated() will tell the driver */
1953 bss_conf->aid = aid;
1954 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07001955
Jiri Bencf0706e82007-05-05 11:45:53 -07001956 ieee80211_associated(dev, ifsta);
1957}
1958
1959
1960/* Caller must hold local->sta_bss_lock */
1961static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1962 struct ieee80211_sta_bss *bss)
1963{
1964 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01001965 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01001966
1967 if (bss_mesh_cfg(bss))
1968 hash_idx = mesh_id_hash(bss_mesh_id(bss),
1969 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01001970 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01001971 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01001972
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01001973 bss->hnext = local->sta_bss_hash[hash_idx];
1974 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07001975}
1976
1977
1978/* Caller must hold local->sta_bss_lock */
1979static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1980 struct ieee80211_sta_bss *bss)
1981{
1982 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1983 struct ieee80211_sta_bss *b, *prev = NULL;
1984 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1985 while (b) {
1986 if (b == bss) {
1987 if (!prev)
1988 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1989 bss->hnext;
1990 else
1991 prev->hnext = bss->hnext;
1992 break;
1993 }
1994 prev = b;
1995 b = b->hnext;
1996 }
1997}
1998
1999
2000static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002001ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002002 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002003{
2004 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2005 struct ieee80211_sta_bss *bss;
2006
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002007 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002008 if (!bss)
2009 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002010 atomic_inc(&bss->users);
2011 atomic_inc(&bss->users);
2012 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002013 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002014 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2015 memcpy(bss->ssid, ssid, ssid_len);
2016 bss->ssid_len = ssid_len;
2017 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002018
2019 spin_lock_bh(&local->sta_bss_lock);
2020 /* TODO: order by RSSI? */
2021 list_add_tail(&bss->list, &local->sta_bss_list);
2022 __ieee80211_rx_bss_hash_add(dev, bss);
2023 spin_unlock_bh(&local->sta_bss_lock);
2024 return bss;
2025}
2026
Jiri Bencf0706e82007-05-05 11:45:53 -07002027static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002028ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002029 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002030{
2031 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2032 struct ieee80211_sta_bss *bss;
2033
2034 spin_lock_bh(&local->sta_bss_lock);
2035 bss = local->sta_bss_hash[STA_HASH(bssid)];
2036 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002037 if (!bss_mesh_cfg(bss) &&
2038 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002039 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002040 bss->ssid_len == ssid_len &&
2041 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002042 atomic_inc(&bss->users);
2043 break;
2044 }
2045 bss = bss->hnext;
2046 }
2047 spin_unlock_bh(&local->sta_bss_lock);
2048 return bss;
2049}
2050
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002051#ifdef CONFIG_MAC80211_MESH
2052static struct ieee80211_sta_bss *
2053ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2054 u8 *mesh_cfg, int freq)
2055{
2056 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2057 struct ieee80211_sta_bss *bss;
2058
2059 spin_lock_bh(&local->sta_bss_lock);
2060 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2061 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002062 if (bss_mesh_cfg(bss) &&
2063 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002064 bss->freq == freq &&
2065 mesh_id_len == bss->mesh_id_len &&
2066 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2067 mesh_id_len))) {
2068 atomic_inc(&bss->users);
2069 break;
2070 }
2071 bss = bss->hnext;
2072 }
2073 spin_unlock_bh(&local->sta_bss_lock);
2074 return bss;
2075}
2076
2077static struct ieee80211_sta_bss *
2078ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2079 u8 *mesh_cfg, int freq)
2080{
2081 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2082 struct ieee80211_sta_bss *bss;
2083
2084 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2085 if (!bss)
2086 return NULL;
2087
2088 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2089 if (!bss->mesh_cfg) {
2090 kfree(bss);
2091 return NULL;
2092 }
2093
2094 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2095 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2096 if (!bss->mesh_id) {
2097 kfree(bss->mesh_cfg);
2098 kfree(bss);
2099 return NULL;
2100 }
2101 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2102 }
2103
2104 atomic_inc(&bss->users);
2105 atomic_inc(&bss->users);
2106 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2107 bss->mesh_id_len = mesh_id_len;
2108 bss->freq = freq;
2109 spin_lock_bh(&local->sta_bss_lock);
2110 /* TODO: order by RSSI? */
2111 list_add_tail(&bss->list, &local->sta_bss_list);
2112 __ieee80211_rx_bss_hash_add(dev, bss);
2113 spin_unlock_bh(&local->sta_bss_lock);
2114 return bss;
2115}
2116#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002117
2118static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2119{
2120 kfree(bss->wpa_ie);
2121 kfree(bss->rsn_ie);
2122 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002123 kfree(bss->ht_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002124 kfree(bss_mesh_id(bss));
2125 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002126 kfree(bss);
2127}
2128
2129
2130static void ieee80211_rx_bss_put(struct net_device *dev,
2131 struct ieee80211_sta_bss *bss)
2132{
2133 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2134 if (!atomic_dec_and_test(&bss->users))
2135 return;
2136
2137 spin_lock_bh(&local->sta_bss_lock);
2138 __ieee80211_rx_bss_hash_del(dev, bss);
2139 list_del(&bss->list);
2140 spin_unlock_bh(&local->sta_bss_lock);
2141 ieee80211_rx_bss_free(bss);
2142}
2143
2144
2145void ieee80211_rx_bss_list_init(struct net_device *dev)
2146{
2147 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2148 spin_lock_init(&local->sta_bss_lock);
2149 INIT_LIST_HEAD(&local->sta_bss_list);
2150}
2151
2152
2153void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2154{
2155 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2156 struct ieee80211_sta_bss *bss, *tmp;
2157
2158 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2159 ieee80211_rx_bss_put(dev, bss);
2160}
2161
2162
Bruno Randolfa6072682008-02-18 11:21:15 +09002163static int ieee80211_sta_join_ibss(struct net_device *dev,
2164 struct ieee80211_if_sta *ifsta,
2165 struct ieee80211_sta_bss *bss)
2166{
2167 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2168 int res, rates, i, j;
2169 struct sk_buff *skb;
2170 struct ieee80211_mgmt *mgmt;
2171 struct ieee80211_tx_control control;
2172 struct rate_selection ratesel;
2173 u8 *pos;
2174 struct ieee80211_sub_if_data *sdata;
2175 struct ieee80211_supported_band *sband;
2176
2177 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2178
2179 /* Remove possible STA entries from other IBSS networks. */
2180 sta_info_flush(local, NULL);
2181
2182 if (local->ops->reset_tsf) {
2183 /* Reset own TSF to allow time synchronization work. */
2184 local->ops->reset_tsf(local_to_hw(local));
2185 }
2186 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2187 res = ieee80211_if_config(dev);
2188 if (res)
2189 return res;
2190
2191 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2192
2193 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2194 sdata->drop_unencrypted = bss->capability &
2195 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2196
2197 res = ieee80211_set_freq(local, bss->freq);
2198
2199 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2200 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2201 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2202 return -1;
2203 }
2204
2205 /* Set beacon template */
2206 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2207 do {
2208 if (!skb)
2209 break;
2210
2211 skb_reserve(skb, local->hw.extra_tx_headroom);
2212
2213 mgmt = (struct ieee80211_mgmt *)
2214 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2215 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2216 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2217 IEEE80211_STYPE_BEACON);
2218 memset(mgmt->da, 0xff, ETH_ALEN);
2219 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2220 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2221 mgmt->u.beacon.beacon_int =
2222 cpu_to_le16(local->hw.conf.beacon_int);
2223 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2224
2225 pos = skb_put(skb, 2 + ifsta->ssid_len);
2226 *pos++ = WLAN_EID_SSID;
2227 *pos++ = ifsta->ssid_len;
2228 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2229
2230 rates = bss->supp_rates_len;
2231 if (rates > 8)
2232 rates = 8;
2233 pos = skb_put(skb, 2 + rates);
2234 *pos++ = WLAN_EID_SUPP_RATES;
2235 *pos++ = rates;
2236 memcpy(pos, bss->supp_rates, rates);
2237
2238 if (bss->band == IEEE80211_BAND_2GHZ) {
2239 pos = skb_put(skb, 2 + 1);
2240 *pos++ = WLAN_EID_DS_PARAMS;
2241 *pos++ = 1;
2242 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2243 }
2244
2245 pos = skb_put(skb, 2 + 2);
2246 *pos++ = WLAN_EID_IBSS_PARAMS;
2247 *pos++ = 2;
2248 /* FIX: set ATIM window based on scan results */
2249 *pos++ = 0;
2250 *pos++ = 0;
2251
2252 if (bss->supp_rates_len > 8) {
2253 rates = bss->supp_rates_len - 8;
2254 pos = skb_put(skb, 2 + rates);
2255 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2256 *pos++ = rates;
2257 memcpy(pos, &bss->supp_rates[8], rates);
2258 }
2259
2260 memset(&control, 0, sizeof(control));
2261 rate_control_get_rate(dev, sband, skb, &ratesel);
2262 if (!ratesel.rate) {
2263 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2264 "for IBSS beacon\n", dev->name);
2265 break;
2266 }
2267 control.vif = &sdata->vif;
2268 control.tx_rate = ratesel.rate;
2269 if (sdata->bss_conf.use_short_preamble &&
2270 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2271 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2272 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2273 control.flags |= IEEE80211_TXCTL_NO_ACK;
2274 control.retry_limit = 1;
2275
2276 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2277 if (ifsta->probe_resp) {
2278 mgmt = (struct ieee80211_mgmt *)
2279 ifsta->probe_resp->data;
2280 mgmt->frame_control =
2281 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2282 IEEE80211_STYPE_PROBE_RESP);
2283 } else {
2284 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2285 "template for IBSS\n", dev->name);
2286 }
2287
2288 if (local->ops->beacon_update &&
2289 local->ops->beacon_update(local_to_hw(local),
2290 skb, &control) == 0) {
2291 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2292 "template\n", dev->name);
2293 skb = NULL;
2294 }
2295
2296 rates = 0;
2297 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2298 for (i = 0; i < bss->supp_rates_len; i++) {
2299 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2300 for (j = 0; j < sband->n_bitrates; j++)
2301 if (sband->bitrates[j].bitrate == bitrate)
2302 rates |= BIT(j);
2303 }
2304 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2305 } while (0);
2306
2307 if (skb) {
2308 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2309 "template\n", dev->name);
2310 dev_kfree_skb(skb);
2311 }
2312
2313 ifsta->state = IEEE80211_IBSS_JOINED;
2314 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2315
2316 ieee80211_rx_bss_put(dev, bss);
2317
2318 return res;
2319}
2320
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002321u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2322 struct ieee802_11_elems *elems,
2323 enum ieee80211_band band)
2324{
2325 struct ieee80211_supported_band *sband;
2326 struct ieee80211_rate *bitrates;
2327 size_t num_rates;
2328 u64 supp_rates;
2329 int i, j;
2330 sband = local->hw.wiphy->bands[band];
2331
2332 if (!sband) {
2333 WARN_ON(1);
2334 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2335 }
2336
2337 bitrates = sband->bitrates;
2338 num_rates = sband->n_bitrates;
2339 supp_rates = 0;
2340 for (i = 0; i < elems->supp_rates_len +
2341 elems->ext_supp_rates_len; i++) {
2342 u8 rate = 0;
2343 int own_rate;
2344 if (i < elems->supp_rates_len)
2345 rate = elems->supp_rates[i];
2346 else if (elems->ext_supp_rates)
2347 rate = elems->ext_supp_rates
2348 [i - elems->supp_rates_len];
2349 own_rate = 5 * (rate & 0x7f);
2350 for (j = 0; j < num_rates; j++)
2351 if (bitrates[j].bitrate == own_rate)
2352 supp_rates |= BIT(j);
2353 }
2354 return supp_rates;
2355}
2356
Bruno Randolfa6072682008-02-18 11:21:15 +09002357
Jiri Bencf0706e82007-05-05 11:45:53 -07002358static void ieee80211_rx_bss_info(struct net_device *dev,
2359 struct ieee80211_mgmt *mgmt,
2360 size_t len,
2361 struct ieee80211_rx_status *rx_status,
2362 int beacon)
2363{
2364 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2365 struct ieee802_11_elems elems;
2366 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002367 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002368 struct ieee80211_sta_bss *bss;
2369 struct sta_info *sta;
2370 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002371 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002372 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002373 DECLARE_MAC_BUF(mac);
2374 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002375
2376 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2377 return; /* ignore ProbeResp to foreign address */
2378
2379#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002380 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07002381 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002382 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002383#endif
2384
2385 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2386 if (baselen > len)
2387 return;
2388
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002389 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
John W. Linville67a4cce2007-10-12 16:40:37 -04002390 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002391
Johannes Berg902acc72008-02-23 15:17:19 +01002392 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2393 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2394 u64 rates = ieee80211_sta_get_rates(local, &elems,
2395 rx_status->band);
2396
2397 mesh_neighbour_update(mgmt->sa, rates, dev,
2398 mesh_peer_accepts_plinks(&elems, dev));
2399 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002400
Johannes Bergd0709a62008-02-25 16:27:46 +01002401 rcu_read_lock();
2402
Johannes Berg51fb61e2007-12-19 01:31:27 +01002403 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002404 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2405 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002406 u64 prev_rates;
2407 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2408 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002409
Johannes Berg8318d782008-01-24 19:38:38 +01002410 prev_rates = sta->supp_rates[rx_status->band];
2411 sta->supp_rates[rx_status->band] &= supp_rates;
2412 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002413 /* No matching rates - this should not really happen.
2414 * Make sure that at least one rate is marked
2415 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002416 sta->supp_rates[rx_status->band] =
2417 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002418 }
Johannes Berg8318d782008-01-24 19:38:38 +01002419 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002420 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002421 "%s based on beacon info (0x%llx & 0x%llx -> "
2422 "0x%llx)\n",
2423 dev->name, print_mac(mac, sta->addr),
2424 (unsigned long long) prev_rates,
2425 (unsigned long long) supp_rates,
2426 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002427 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002428 }
2429
Johannes Bergd0709a62008-02-25 16:27:46 +01002430 rcu_read_unlock();
2431
Jiri Bencf0706e82007-05-05 11:45:53 -07002432 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002433 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002434 else
Johannes Berg8318d782008-01-24 19:38:38 +01002435 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002436
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002437 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2438
2439 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2440 return;
2441
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002442#ifdef CONFIG_MAC80211_MESH
2443 if (elems.mesh_config)
2444 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2445 elems.mesh_id_len, elems.mesh_config, freq);
2446 else
2447#endif
2448 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002449 elems.ssid, elems.ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002450 if (!bss) {
2451#ifdef CONFIG_MAC80211_MESH
2452 if (elems.mesh_config)
2453 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
2454 elems.mesh_id_len, elems.mesh_config, freq);
2455 else
2456#endif
2457 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2458 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002459 if (!bss)
2460 return;
2461 } else {
2462#if 0
2463 /* TODO: order by RSSI? */
2464 spin_lock_bh(&local->sta_bss_lock);
2465 list_move_tail(&bss->list, &local->sta_bss_list);
2466 spin_unlock_bh(&local->sta_bss_lock);
2467#endif
2468 }
2469
Johannes Berg8318d782008-01-24 19:38:38 +01002470 bss->band = rx_status->band;
2471
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002472 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2473 bss->probe_resp && beacon) {
2474 /* STA mode:
2475 * Do not allow beacon to override data from Probe Response. */
Jiri Bencf0706e82007-05-05 11:45:53 -07002476 ieee80211_rx_bss_put(dev, bss);
2477 return;
2478 }
2479
Daniel Drake56282212007-07-10 19:32:10 +02002480 /* save the ERP value so that it is available at association time */
2481 if (elems.erp_info && elems.erp_info_len >= 1) {
2482 bss->erp_value = elems.erp_info[0];
2483 bss->has_erp_value = 1;
2484 }
2485
Jiri Bencf0706e82007-05-05 11:45:53 -07002486 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2487 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002488
2489 bss->supp_rates_len = 0;
2490 if (elems.supp_rates) {
2491 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2492 if (clen > elems.supp_rates_len)
2493 clen = elems.supp_rates_len;
2494 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2495 clen);
2496 bss->supp_rates_len += clen;
2497 }
2498 if (elems.ext_supp_rates) {
2499 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2500 if (clen > elems.ext_supp_rates_len)
2501 clen = elems.ext_supp_rates_len;
2502 memcpy(&bss->supp_rates[bss->supp_rates_len],
2503 elems.ext_supp_rates, clen);
2504 bss->supp_rates_len += clen;
2505 }
2506
2507 if (elems.wpa &&
2508 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2509 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2510 kfree(bss->wpa_ie);
2511 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2512 if (bss->wpa_ie) {
2513 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2514 bss->wpa_ie_len = elems.wpa_len + 2;
2515 } else
2516 bss->wpa_ie_len = 0;
2517 } else if (!elems.wpa && bss->wpa_ie) {
2518 kfree(bss->wpa_ie);
2519 bss->wpa_ie = NULL;
2520 bss->wpa_ie_len = 0;
2521 }
2522
2523 if (elems.rsn &&
2524 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2525 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2526 kfree(bss->rsn_ie);
2527 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2528 if (bss->rsn_ie) {
2529 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2530 bss->rsn_ie_len = elems.rsn_len + 2;
2531 } else
2532 bss->rsn_ie_len = 0;
2533 } else if (!elems.rsn && bss->rsn_ie) {
2534 kfree(bss->rsn_ie);
2535 bss->rsn_ie = NULL;
2536 bss->rsn_ie_len = 0;
2537 }
2538
2539 if (elems.wmm_param &&
2540 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2541 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2542 kfree(bss->wmm_ie);
2543 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2544 if (bss->wmm_ie) {
2545 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2546 elems.wmm_param_len + 2);
2547 bss->wmm_ie_len = elems.wmm_param_len + 2;
2548 } else
2549 bss->wmm_ie_len = 0;
2550 } else if (!elems.wmm_param && bss->wmm_ie) {
2551 kfree(bss->wmm_ie);
2552 bss->wmm_ie = NULL;
2553 bss->wmm_ie_len = 0;
2554 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002555 if (elems.ht_cap_elem &&
2556 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2557 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2558 kfree(bss->ht_ie);
2559 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2560 if (bss->ht_ie) {
2561 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2562 elems.ht_cap_elem_len + 2);
2563 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2564 } else
2565 bss->ht_ie_len = 0;
2566 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2567 kfree(bss->ht_ie);
2568 bss->ht_ie = NULL;
2569 bss->ht_ie_len = 0;
2570 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002571
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002572 bss->timestamp = beacon_timestamp;
Jiri Bencf0706e82007-05-05 11:45:53 -07002573 bss->last_update = jiffies;
2574 bss->rssi = rx_status->ssi;
2575 bss->signal = rx_status->signal;
2576 bss->noise = rx_status->noise;
2577 if (!beacon)
2578 bss->probe_resp++;
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002579
2580 /* check if we need to merge IBSS */
2581 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2582 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002583 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002584 bss->freq == local->oper_channel->center_freq &&
2585 elems.ssid_len == sdata->u.sta.ssid_len &&
2586 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2587 if (rx_status->flag & RX_FLAG_TSFT) {
2588 /* in order for correct IBSS merging we need mactime
2589 *
2590 * since mactime is defined as the time the first data
2591 * symbol of the frame hits the PHY, and the timestamp
2592 * of the beacon is defined as "the time that the data
2593 * symbol containing the first bit of the timestamp is
2594 * transmitted to the PHY plus the transmitting STA’s
2595 * delays through its local PHY from the MAC-PHY
2596 * interface to its interface with the WM"
2597 * (802.11 11.1.2) - equals the time this bit arrives at
2598 * the receiver - we have to take into account the
2599 * offset between the two.
2600 * e.g: at 1 MBit that means mactime is 192 usec earlier
2601 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2602 */
2603 int rate = local->hw.wiphy->bands[rx_status->band]->
2604 bitrates[rx_status->rate_idx].bitrate;
2605 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2606 } else if (local && local->ops && local->ops->get_tsf)
2607 /* second best option: get current TSF */
2608 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2609 else
2610 /* can't merge without knowing the TSF */
2611 rx_timestamp = -1LLU;
2612#ifdef CONFIG_MAC80211_IBSS_DEBUG
2613 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2614 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2615 print_mac(mac, mgmt->sa),
2616 print_mac(mac2, mgmt->bssid),
2617 (unsigned long long)rx_timestamp,
2618 (unsigned long long)beacon_timestamp,
2619 (unsigned long long)(rx_timestamp - beacon_timestamp),
2620 jiffies);
2621#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2622 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002623#ifndef CONFIG_MAC80211_IBSS_DEBUG
Pavel Roskind97cf012008-02-21 11:33:58 -05002624 if (net_ratelimit())
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002625#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002626 printk(KERN_DEBUG "%s: beacon TSF higher than "
2627 "local TSF - IBSS merge with BSSID %s\n",
2628 dev->name, print_mac(mac, mgmt->bssid));
2629 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2630 ieee80211_ibss_add_sta(dev, NULL,
2631 mgmt->bssid, mgmt->sa);
2632 }
2633 }
2634
Jiri Bencf0706e82007-05-05 11:45:53 -07002635 ieee80211_rx_bss_put(dev, bss);
2636}
2637
2638
2639static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2640 struct ieee80211_mgmt *mgmt,
2641 size_t len,
2642 struct ieee80211_rx_status *rx_status)
2643{
2644 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2645}
2646
2647
2648static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2649 struct ieee80211_mgmt *mgmt,
2650 size_t len,
2651 struct ieee80211_rx_status *rx_status)
2652{
Jiri Bencf0706e82007-05-05 11:45:53 -07002653 struct ieee80211_sub_if_data *sdata;
2654 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002655 size_t baselen;
2656 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002657 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2658 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002659 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002660
2661 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2662
2663 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002664 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002665 return;
2666 ifsta = &sdata->u.sta;
2667
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002668 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002669 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2670 return;
2671
2672 /* Process beacon from the current BSS */
2673 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2674 if (baselen > len)
2675 return;
2676
John W. Linville67a4cce2007-10-12 16:40:37 -04002677 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002678
Daniel Drake56282212007-07-10 19:32:10 +02002679 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002680 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002681
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002682 if (elems.ht_cap_elem && elems.ht_info_elem &&
2683 elems.wmm_param && local->ops->conf_ht &&
2684 conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2685 struct ieee80211_ht_bss_info bss_info;
2686
2687 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2688 (struct ieee80211_ht_addt_info *)
2689 elems.ht_info_elem, &bss_info);
2690 /* check if AP changed bss inforamation */
2691 if ((conf->ht_bss_conf.primary_channel !=
2692 bss_info.primary_channel) ||
2693 (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
2694 (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
2695 ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
2696 &bss_info);
2697 }
2698
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002699 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002700 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2701 elems.wmm_param_len);
2702 }
Johannes Berg471b3ef2007-12-28 14:32:58 +01002703
2704 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002705}
2706
2707
2708static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2709 struct ieee80211_if_sta *ifsta,
2710 struct ieee80211_mgmt *mgmt,
2711 size_t len,
2712 struct ieee80211_rx_status *rx_status)
2713{
2714 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2715 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2716 int tx_last_beacon;
2717 struct sk_buff *skb;
2718 struct ieee80211_mgmt *resp;
2719 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002720 DECLARE_MAC_BUF(mac);
2721#ifdef CONFIG_MAC80211_IBSS_DEBUG
2722 DECLARE_MAC_BUF(mac2);
2723 DECLARE_MAC_BUF(mac3);
2724#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002725
Johannes Berg51fb61e2007-12-19 01:31:27 +01002726 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002727 ifsta->state != IEEE80211_IBSS_JOINED ||
2728 len < 24 + 2 || !ifsta->probe_resp)
2729 return;
2730
2731 if (local->ops->tx_last_beacon)
2732 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2733 else
2734 tx_last_beacon = 1;
2735
2736#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002737 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2738 "%s (tx_last_beacon=%d)\n",
2739 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2740 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002741#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2742
2743 if (!tx_last_beacon)
2744 return;
2745
2746 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2747 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2748 return;
2749
2750 end = ((u8 *) mgmt) + len;
2751 pos = mgmt->u.probe_req.variable;
2752 if (pos[0] != WLAN_EID_SSID ||
2753 pos + 2 + pos[1] > end) {
2754 if (net_ratelimit()) {
2755 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002756 "from %s\n",
2757 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002758 }
2759 return;
2760 }
2761 if (pos[1] != 0 &&
2762 (pos[1] != ifsta->ssid_len ||
2763 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2764 /* Ignore ProbeReq for foreign SSID */
2765 return;
2766 }
2767
2768 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002769 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002770 if (!skb)
2771 return;
2772
2773 resp = (struct ieee80211_mgmt *) skb->data;
2774 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2775#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002776 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2777 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002778#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2779 ieee80211_sta_tx(dev, skb, 0);
2780}
2781
Johannes Berg4e20cb22007-12-19 01:31:24 +01002782static void ieee80211_rx_mgmt_action(struct net_device *dev,
2783 struct ieee80211_if_sta *ifsta,
2784 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002785 size_t len,
2786 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002787{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002788 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002789
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002790 if (len < IEEE80211_MIN_ACTION_SIZE)
2791 return;
2792
2793 switch (mgmt->u.action.category) {
2794 case WLAN_CATEGORY_BACK:
2795 switch (mgmt->u.action.u.addba_req.action_code) {
2796 case WLAN_ACTION_ADDBA_REQ:
2797 if (len < (IEEE80211_MIN_ACTION_SIZE +
2798 sizeof(mgmt->u.action.u.addba_req)))
2799 break;
2800 ieee80211_sta_process_addba_request(dev, mgmt, len);
2801 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002802 case WLAN_ACTION_ADDBA_RESP:
2803 if (len < (IEEE80211_MIN_ACTION_SIZE +
2804 sizeof(mgmt->u.action.u.addba_resp)))
2805 break;
2806 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2807 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002808 case WLAN_ACTION_DELBA:
2809 if (len < (IEEE80211_MIN_ACTION_SIZE +
2810 sizeof(mgmt->u.action.u.delba)))
2811 break;
2812 ieee80211_sta_process_delba(dev, mgmt, len);
2813 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002814 default:
2815 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002816 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002817 dev->name);
2818 break;
2819 }
2820 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002821 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002822 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002823 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
2824 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002825 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002826 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002827 mesh_rx_path_sel_frame(dev, mgmt, len);
2828 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002829 default:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002830 if (net_ratelimit())
2831 printk(KERN_DEBUG "%s: Rx unknown action frame - "
2832 "category=%d\n", dev->name, mgmt->u.action.category);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002833 break;
2834 }
2835}
Jiri Bencf0706e82007-05-05 11:45:53 -07002836
2837void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2838 struct ieee80211_rx_status *rx_status)
2839{
2840 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2841 struct ieee80211_sub_if_data *sdata;
2842 struct ieee80211_if_sta *ifsta;
2843 struct ieee80211_mgmt *mgmt;
2844 u16 fc;
2845
2846 if (skb->len < 24)
2847 goto fail;
2848
2849 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2850 ifsta = &sdata->u.sta;
2851
2852 mgmt = (struct ieee80211_mgmt *) skb->data;
2853 fc = le16_to_cpu(mgmt->frame_control);
2854
2855 switch (fc & IEEE80211_FCTL_STYPE) {
2856 case IEEE80211_STYPE_PROBE_REQ:
2857 case IEEE80211_STYPE_PROBE_RESP:
2858 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002859 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07002860 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2861 case IEEE80211_STYPE_AUTH:
2862 case IEEE80211_STYPE_ASSOC_RESP:
2863 case IEEE80211_STYPE_REASSOC_RESP:
2864 case IEEE80211_STYPE_DEAUTH:
2865 case IEEE80211_STYPE_DISASSOC:
2866 skb_queue_tail(&ifsta->skb_queue, skb);
2867 queue_work(local->hw.workqueue, &ifsta->work);
2868 return;
2869 default:
2870 printk(KERN_DEBUG "%s: received unknown management frame - "
2871 "stype=%d\n", dev->name,
2872 (fc & IEEE80211_FCTL_STYPE) >> 4);
2873 break;
2874 }
2875
2876 fail:
2877 kfree_skb(skb);
2878}
2879
2880
2881static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2882 struct sk_buff *skb)
2883{
2884 struct ieee80211_rx_status *rx_status;
2885 struct ieee80211_sub_if_data *sdata;
2886 struct ieee80211_if_sta *ifsta;
2887 struct ieee80211_mgmt *mgmt;
2888 u16 fc;
2889
2890 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2891 ifsta = &sdata->u.sta;
2892
2893 rx_status = (struct ieee80211_rx_status *) skb->cb;
2894 mgmt = (struct ieee80211_mgmt *) skb->data;
2895 fc = le16_to_cpu(mgmt->frame_control);
2896
2897 switch (fc & IEEE80211_FCTL_STYPE) {
2898 case IEEE80211_STYPE_PROBE_REQ:
2899 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2900 rx_status);
2901 break;
2902 case IEEE80211_STYPE_PROBE_RESP:
2903 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2904 break;
2905 case IEEE80211_STYPE_BEACON:
2906 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2907 break;
2908 case IEEE80211_STYPE_AUTH:
2909 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2910 break;
2911 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002912 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002913 break;
2914 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002915 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002916 break;
2917 case IEEE80211_STYPE_DEAUTH:
2918 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2919 break;
2920 case IEEE80211_STYPE_DISASSOC:
2921 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
2922 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002923 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002924 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002925 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07002926 }
2927
2928 kfree_skb(skb);
2929}
2930
2931
Johannes Berg9ae54c82008-01-31 19:48:20 +01002932ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08002933ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
2934 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002935{
2936 struct ieee80211_mgmt *mgmt;
2937 u16 fc;
2938
Zhu Yiece8edd2007-11-22 10:53:21 +08002939 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002940 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002941
2942 mgmt = (struct ieee80211_mgmt *) skb->data;
2943 fc = le16_to_cpu(mgmt->frame_control);
2944
Zhu Yiece8edd2007-11-22 10:53:21 +08002945 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01002946 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08002947
2948 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002949 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08002950
Jiri Bencf0706e82007-05-05 11:45:53 -07002951 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2952 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
2953 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
2954 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002955 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002956 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07002957 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
2958 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
2959 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002960 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002961 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07002962 }
2963 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01002964 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002965}
2966
2967
2968static int ieee80211_sta_active_ibss(struct net_device *dev)
2969{
2970 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2971 int active = 0;
2972 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01002973 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07002974
Johannes Bergd0709a62008-02-25 16:27:46 +01002975 rcu_read_lock();
2976
2977 list_for_each_entry_rcu(sta, &local->sta_list, list) {
2978 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002979 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2980 jiffies)) {
2981 active++;
2982 break;
2983 }
2984 }
Johannes Bergd0709a62008-02-25 16:27:46 +01002985
2986 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002987
2988 return active;
2989}
2990
2991
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002992static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07002993{
2994 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2995 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02002996 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07002997 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01002998 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07002999
Johannes Bergd0709a62008-02-25 16:27:46 +01003000 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003001 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003002 if (time_after(jiffies, sta->last_rx + exp_time)) {
Joe Perches0795af52007-10-03 17:59:30 -07003003 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3004 dev->name, print_mac(mac, sta->addr));
Johannes Bergd0709a62008-02-25 16:27:46 +01003005 sta_info_unlink(&sta);
3006 if (sta)
3007 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003008 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003009 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003010
Johannes Bergd0709a62008-02-25 16:27:46 +01003011 synchronize_rcu();
3012
3013 rtnl_lock();
3014 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3015 sta_info_destroy(sta);
3016 rtnl_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003017}
3018
3019
3020static void ieee80211_sta_merge_ibss(struct net_device *dev,
3021 struct ieee80211_if_sta *ifsta)
3022{
3023 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3024
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003025 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07003026 if (ieee80211_sta_active_ibss(dev))
3027 return;
3028
3029 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3030 "IBSS networks with same SSID (merge)\n", dev->name);
3031 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3032}
3033
3034
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003035#ifdef CONFIG_MAC80211_MESH
3036static void ieee80211_mesh_housekeeping(struct net_device *dev,
3037 struct ieee80211_if_sta *ifsta)
3038{
3039 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3040 bool free_plinks;
3041
3042 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3043 mesh_path_expire(dev);
3044
3045 free_plinks = mesh_plink_availables(sdata);
3046 if (free_plinks != sdata->u.sta.accepting_plinks)
3047 ieee80211_if_config_beacon(dev);
3048
3049 mod_timer(&ifsta->timer, jiffies +
3050 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3051}
3052
3053
3054void ieee80211_start_mesh(struct net_device *dev)
3055{
3056 struct ieee80211_if_sta *ifsta;
3057 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3058 ifsta = &sdata->u.sta;
3059 ifsta->state = IEEE80211_MESH_UP;
3060 ieee80211_sta_timer((unsigned long)sdata);
3061}
3062#endif
3063
3064
Jiri Bencf0706e82007-05-05 11:45:53 -07003065void ieee80211_sta_timer(unsigned long data)
3066{
3067 struct ieee80211_sub_if_data *sdata =
3068 (struct ieee80211_sub_if_data *) data;
3069 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3070 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3071
3072 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3073 queue_work(local->hw.workqueue, &ifsta->work);
3074}
3075
Jiri Bencf0706e82007-05-05 11:45:53 -07003076void ieee80211_sta_work(struct work_struct *work)
3077{
3078 struct ieee80211_sub_if_data *sdata =
3079 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3080 struct net_device *dev = sdata->dev;
3081 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3082 struct ieee80211_if_sta *ifsta;
3083 struct sk_buff *skb;
3084
3085 if (!netif_running(dev))
3086 return;
3087
Zhu Yiece8edd2007-11-22 10:53:21 +08003088 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003089 return;
3090
Johannes Berg51fb61e2007-12-19 01:31:27 +01003091 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003092 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3093 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003094 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01003095 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e82007-05-05 11:45:53 -07003096 return;
3097 }
3098 ifsta = &sdata->u.sta;
3099
3100 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3101 ieee80211_sta_rx_queued_mgmt(dev, skb);
3102
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003103#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003104 if (ifsta->preq_queue_len &&
3105 time_after(jiffies,
3106 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003107 mesh_path_start_discovery(dev);
3108#endif
3109
Jiri Bencf0706e82007-05-05 11:45:53 -07003110 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3111 ifsta->state != IEEE80211_ASSOCIATE &&
3112 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003113 if (ifsta->scan_ssid_len)
3114 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3115 else
3116 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003117 return;
3118 }
3119
3120 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3121 if (ieee80211_sta_config_auth(dev, ifsta))
3122 return;
3123 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3124 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3125 return;
3126
3127 switch (ifsta->state) {
3128 case IEEE80211_DISABLED:
3129 break;
3130 case IEEE80211_AUTHENTICATE:
3131 ieee80211_authenticate(dev, ifsta);
3132 break;
3133 case IEEE80211_ASSOCIATE:
3134 ieee80211_associate(dev, ifsta);
3135 break;
3136 case IEEE80211_ASSOCIATED:
3137 ieee80211_associated(dev, ifsta);
3138 break;
3139 case IEEE80211_IBSS_SEARCH:
3140 ieee80211_sta_find_ibss(dev, ifsta);
3141 break;
3142 case IEEE80211_IBSS_JOINED:
3143 ieee80211_sta_merge_ibss(dev, ifsta);
3144 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003145#ifdef CONFIG_MAC80211_MESH
3146 case IEEE80211_MESH_UP:
3147 ieee80211_mesh_housekeeping(dev, ifsta);
3148 break;
3149#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003150 default:
3151 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3152 ifsta->state);
3153 break;
3154 }
3155
3156 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3157 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3158 "mixed-cell disabled - disassociate\n", dev->name);
3159
3160 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3161 ieee80211_set_disassoc(dev, ifsta, 0);
3162 }
3163}
3164
3165
3166static void ieee80211_sta_reset_auth(struct net_device *dev,
3167 struct ieee80211_if_sta *ifsta)
3168{
3169 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3170
3171 if (local->ops->reset_tsf) {
3172 /* Reset own TSF to allow time synchronization work. */
3173 local->ops->reset_tsf(local_to_hw(local));
3174 }
3175
3176 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3177
3178
3179 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3180 ifsta->auth_alg = WLAN_AUTH_OPEN;
3181 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3182 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3183 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3184 ifsta->auth_alg = WLAN_AUTH_LEAP;
3185 else
3186 ifsta->auth_alg = WLAN_AUTH_OPEN;
3187 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3188 ifsta->auth_alg);
3189 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003190 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3191 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003192 netif_carrier_off(dev);
3193}
3194
3195
3196void ieee80211_sta_req_auth(struct net_device *dev,
3197 struct ieee80211_if_sta *ifsta)
3198{
3199 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3200 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3201
Johannes Berg51fb61e2007-12-19 01:31:27 +01003202 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003203 return;
3204
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003205 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3206 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3207 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3208 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003209 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3210 queue_work(local->hw.workqueue, &ifsta->work);
3211 }
3212}
3213
3214static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3215 const char *ssid, int ssid_len)
3216{
3217 int tmp, hidden_ssid;
3218
Michael Wu48225702007-10-19 17:14:36 -04003219 if (ssid_len == ifsta->ssid_len &&
3220 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003221 return 1;
3222
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003223 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003224 return 0;
3225
3226 hidden_ssid = 1;
3227 tmp = ssid_len;
3228 while (tmp--) {
3229 if (ssid[tmp] != '\0') {
3230 hidden_ssid = 0;
3231 break;
3232 }
3233 }
3234
3235 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3236 return 1;
3237
3238 if (ssid_len == 1 && ssid[0] == ' ')
3239 return 1;
3240
3241 return 0;
3242}
3243
3244static int ieee80211_sta_config_auth(struct net_device *dev,
3245 struct ieee80211_if_sta *ifsta)
3246{
3247 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3248 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3249 struct ieee80211_sta_bss *bss, *selected = NULL;
3250 int top_rssi = 0, freq;
3251
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003252 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3253 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003254 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003255 ieee80211_sta_reset_auth(dev, ifsta);
3256 return 0;
3257 }
3258
3259 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003260 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003261 list_for_each_entry(bss, &local->sta_bss_list, list) {
3262 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3263 continue;
3264
3265 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3266 !!sdata->default_key)
3267 continue;
3268
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003269 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3270 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003271 continue;
3272
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003273 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003274 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3275 continue;
3276
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003277 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003278 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3279 continue;
3280
3281 if (!selected || top_rssi < bss->rssi) {
3282 selected = bss;
3283 top_rssi = bss->rssi;
3284 }
3285 }
3286 if (selected)
3287 atomic_inc(&selected->users);
3288 spin_unlock_bh(&local->sta_bss_lock);
3289
3290 if (selected) {
Johannes Berg8318d782008-01-24 19:38:38 +01003291 ieee80211_set_freq(local, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003292 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003293 ieee80211_sta_set_ssid(dev, selected->ssid,
3294 selected->ssid_len);
3295 ieee80211_sta_set_bssid(dev, selected->bssid);
3296 ieee80211_rx_bss_put(dev, selected);
3297 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003298 ieee80211_sta_reset_auth(dev, ifsta);
3299 return 0;
3300 } else {
3301 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003302 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003303 ieee80211_sta_start_scan(dev, NULL, 0);
3304 else
3305 ieee80211_sta_start_scan(dev, ifsta->ssid,
3306 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003307 ifsta->state = IEEE80211_AUTHENTICATE;
3308 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3309 } else
3310 ifsta->state = IEEE80211_DISABLED;
3311 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003312 return -1;
3313}
3314
Jiri Bencf0706e82007-05-05 11:45:53 -07003315
3316static int ieee80211_sta_create_ibss(struct net_device *dev,
3317 struct ieee80211_if_sta *ifsta)
3318{
3319 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3320 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003321 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003322 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003323 u8 bssid[ETH_ALEN], *pos;
3324 int i;
Joe Perches0795af52007-10-03 17:59:30 -07003325 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003326
3327#if 0
3328 /* Easier testing, use fixed BSSID. */
3329 memset(bssid, 0xfe, ETH_ALEN);
3330#else
3331 /* Generate random, not broadcast, locally administered BSSID. Mix in
3332 * own MAC address to make sure that devices that do not have proper
3333 * random number generator get different BSSID. */
3334 get_random_bytes(bssid, ETH_ALEN);
3335 for (i = 0; i < ETH_ALEN; i++)
3336 bssid[i] ^= dev->dev_addr[i];
3337 bssid[0] &= ~0x01;
3338 bssid[0] |= 0x02;
3339#endif
3340
Joe Perches0795af52007-10-03 17:59:30 -07003341 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3342 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003343
Johannes Berg8318d782008-01-24 19:38:38 +01003344 bss = ieee80211_rx_bss_add(dev, bssid,
3345 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003346 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003347 if (!bss)
3348 return -ENOMEM;
3349
Johannes Berg8318d782008-01-24 19:38:38 +01003350 bss->band = local->hw.conf.channel->band;
3351 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003352
3353 if (local->hw.conf.beacon_int == 0)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003354 local->hw.conf.beacon_int = 10000;
Jiri Bencf0706e82007-05-05 11:45:53 -07003355 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003356 bss->last_update = jiffies;
3357 bss->capability = WLAN_CAPABILITY_IBSS;
3358 if (sdata->default_key) {
3359 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3360 } else
3361 sdata->drop_unencrypted = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003362 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003363 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003364 for (i = 0; i < sband->n_bitrates; i++) {
3365 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003366 *pos++ = (u8) (rate / 5);
3367 }
3368
3369 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3370}
3371
3372
3373static int ieee80211_sta_find_ibss(struct net_device *dev,
3374 struct ieee80211_if_sta *ifsta)
3375{
3376 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3377 struct ieee80211_sta_bss *bss;
3378 int found = 0;
3379 u8 bssid[ETH_ALEN];
3380 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003381 DECLARE_MAC_BUF(mac);
3382 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003383
3384 if (ifsta->ssid_len == 0)
3385 return -EINVAL;
3386
3387 active_ibss = ieee80211_sta_active_ibss(dev);
3388#ifdef CONFIG_MAC80211_IBSS_DEBUG
3389 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3390 dev->name, active_ibss);
3391#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3392 spin_lock_bh(&local->sta_bss_lock);
3393 list_for_each_entry(bss, &local->sta_bss_list, list) {
3394 if (ifsta->ssid_len != bss->ssid_len ||
3395 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3396 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3397 continue;
3398#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003399 printk(KERN_DEBUG " bssid=%s found\n",
3400 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003401#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3402 memcpy(bssid, bss->bssid, ETH_ALEN);
3403 found = 1;
3404 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3405 break;
3406 }
3407 spin_unlock_bh(&local->sta_bss_lock);
3408
3409#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003410 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3411 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003412#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3413 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003414 (bss = ieee80211_rx_bss_get(dev, bssid,
3415 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003416 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07003417 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003418 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003419 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003420 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3421 }
3422#ifdef CONFIG_MAC80211_IBSS_DEBUG
3423 printk(KERN_DEBUG " did not try to join ibss\n");
3424#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3425
3426 /* Selected IBSS not found in current scan results - try to scan */
3427 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3428 !ieee80211_sta_active_ibss(dev)) {
3429 mod_timer(&ifsta->timer, jiffies +
3430 IEEE80211_IBSS_MERGE_INTERVAL);
3431 } else if (time_after(jiffies, local->last_scan_completed +
3432 IEEE80211_SCAN_INTERVAL)) {
3433 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3434 "join\n", dev->name);
3435 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3436 ifsta->ssid_len);
3437 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3438 int interval = IEEE80211_SCAN_INTERVAL;
3439
3440 if (time_after(jiffies, ifsta->ibss_join_req +
3441 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003442 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003443 (!(local->oper_channel->flags &
3444 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003445 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003446 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003447 printk(KERN_DEBUG "%s: IBSS not allowed on"
3448 " %d MHz\n", dev->name,
3449 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003450 }
3451
3452 /* No IBSS found - decrease scan interval and continue
3453 * scanning. */
3454 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3455 }
3456
3457 ifsta->state = IEEE80211_IBSS_SEARCH;
3458 mod_timer(&ifsta->timer, jiffies + interval);
3459 return 0;
3460 }
3461
3462 return 0;
3463}
3464
3465
3466int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3467{
Johannes Berg8318d782008-01-24 19:38:38 +01003468 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003469 struct ieee80211_if_sta *ifsta;
3470 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3471
3472 if (len > IEEE80211_MAX_SSID_LEN)
3473 return -EINVAL;
3474
3475 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
3476 * not defined. */
3477 if (local->ops->conf_tx) {
3478 struct ieee80211_tx_queue_params qparam;
3479 int i;
3480
3481 memset(&qparam, 0, sizeof(qparam));
Johannes Berg8318d782008-01-24 19:38:38 +01003482
Jiri Bencf0706e82007-05-05 11:45:53 -07003483 qparam.aifs = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01003484
3485 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
3486 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
3487 qparam.cw_min = 31;
3488 else
3489 qparam.cw_min = 15;
3490
Jiri Bencf0706e82007-05-05 11:45:53 -07003491 qparam.cw_max = 1023;
Johannes Berg3330d7b2008-02-10 16:49:38 +01003492 qparam.txop = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003493
Jiri Bencf0706e82007-05-05 11:45:53 -07003494 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
Jiri Bencf0706e82007-05-05 11:45:53 -07003495 local->ops->conf_tx(local_to_hw(local),
3496 i + IEEE80211_TX_QUEUE_DATA0,
3497 &qparam);
Johannes Berg8318d782008-01-24 19:38:38 +01003498
Jiri Bencf0706e82007-05-05 11:45:53 -07003499 /* IBSS uses different parameters for Beacon sending */
3500 qparam.cw_min++;
3501 qparam.cw_min *= 2;
3502 qparam.cw_min--;
3503 local->ops->conf_tx(local_to_hw(local),
3504 IEEE80211_TX_QUEUE_BEACON, &qparam);
3505 }
3506
Jiri Bencf0706e82007-05-05 11:45:53 -07003507 ifsta = &sdata->u.sta;
3508
3509 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003510 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003511 memcpy(ifsta->ssid, ssid, len);
3512 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3513 ifsta->ssid_len = len;
3514
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003515 if (len)
3516 ifsta->flags |= IEEE80211_STA_SSID_SET;
3517 else
3518 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003519 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003520 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003521 ifsta->ibss_join_req = jiffies;
3522 ifsta->state = IEEE80211_IBSS_SEARCH;
3523 return ieee80211_sta_find_ibss(dev, ifsta);
3524 }
3525 return 0;
3526}
3527
3528
3529int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3530{
3531 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3532 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3533 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3534 *len = ifsta->ssid_len;
3535 return 0;
3536}
3537
3538
3539int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3540{
3541 struct ieee80211_sub_if_data *sdata;
3542 struct ieee80211_if_sta *ifsta;
3543 int res;
3544
3545 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3546 ifsta = &sdata->u.sta;
3547
3548 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3549 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3550 res = ieee80211_if_config(dev);
3551 if (res) {
3552 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3553 "the low-level driver\n", dev->name);
3554 return res;
3555 }
3556 }
3557
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003558 if (is_valid_ether_addr(bssid))
3559 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003560 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003561 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3562
Jiri Bencf0706e82007-05-05 11:45:53 -07003563 return 0;
3564}
3565
3566
3567static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3568 struct ieee80211_sub_if_data *sdata,
3569 int powersave)
3570{
3571 struct sk_buff *skb;
3572 struct ieee80211_hdr *nullfunc;
3573 u16 fc;
3574
3575 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3576 if (!skb) {
3577 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3578 "frame\n", sdata->dev->name);
3579 return;
3580 }
3581 skb_reserve(skb, local->hw.extra_tx_headroom);
3582
3583 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3584 memset(nullfunc, 0, 24);
3585 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3586 IEEE80211_FCTL_TODS;
3587 if (powersave)
3588 fc |= IEEE80211_FCTL_PM;
3589 nullfunc->frame_control = cpu_to_le16(fc);
3590 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3591 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3592 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3593
3594 ieee80211_sta_tx(sdata->dev, skb, 0);
3595}
3596
3597
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003598static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3599{
3600 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3601 ieee80211_vif_is_mesh(&sdata->vif))
3602 ieee80211_sta_timer((unsigned long)sdata);
3603}
3604
Jiri Bencf0706e82007-05-05 11:45:53 -07003605void ieee80211_scan_completed(struct ieee80211_hw *hw)
3606{
3607 struct ieee80211_local *local = hw_to_local(hw);
3608 struct net_device *dev = local->scan_dev;
3609 struct ieee80211_sub_if_data *sdata;
3610 union iwreq_data wrqu;
3611
3612 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003613 memset(&wrqu, 0, sizeof(wrqu));
3614 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003615
Zhu Yiece8edd2007-11-22 10:53:21 +08003616 if (local->sta_hw_scanning) {
3617 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003618 if (ieee80211_hw_config(local))
3619 printk(KERN_DEBUG "%s: failed to restore operational "
3620 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003621 /* Restart STA timer for HW scan case */
3622 rcu_read_lock();
3623 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3624 ieee80211_restart_sta_timer(sdata);
3625 rcu_read_unlock();
3626
Zhu Yiece8edd2007-11-22 10:53:21 +08003627 goto done;
3628 }
3629
3630 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003631 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003632 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003633 "channel after scan\n", dev->name);
3634
Johannes Berg4150c572007-09-17 01:29:23 -04003635
3636 netif_tx_lock_bh(local->mdev);
3637 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3638 local->ops->configure_filter(local_to_hw(local),
3639 FIF_BCN_PRBRESP_PROMISC,
3640 &local->filter_flags,
3641 local->mdev->mc_count,
3642 local->mdev->mc_list);
3643
3644 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003645
Johannes Berg79010422007-09-18 17:29:21 -04003646 rcu_read_lock();
3647 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003648
3649 /* No need to wake the master device. */
3650 if (sdata->dev == local->mdev)
3651 continue;
3652
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003653 /* Tell AP we're back */
3654 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3655 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3656 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003657
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003658 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003659
Jiri Bencf0706e82007-05-05 11:45:53 -07003660 netif_wake_queue(sdata->dev);
3661 }
Johannes Berg79010422007-09-18 17:29:21 -04003662 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003663
Zhu Yiece8edd2007-11-22 10:53:21 +08003664done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003665 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003666 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003667 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003668 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003669 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3670 !ieee80211_sta_active_ibss(dev)))
3671 ieee80211_sta_find_ibss(dev, ifsta);
3672 }
3673}
3674EXPORT_SYMBOL(ieee80211_scan_completed);
3675
3676void ieee80211_sta_scan_work(struct work_struct *work)
3677{
3678 struct ieee80211_local *local =
3679 container_of(work, struct ieee80211_local, scan_work.work);
3680 struct net_device *dev = local->scan_dev;
3681 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003682 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003683 struct ieee80211_channel *chan;
3684 int skip;
3685 unsigned long next_delay = 0;
3686
Zhu Yiece8edd2007-11-22 10:53:21 +08003687 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003688 return;
3689
3690 switch (local->scan_state) {
3691 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003692 /*
3693 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3694 * after we successfully scanned the last channel of the last
3695 * band (and the last band is supported by the hw)
3696 */
Johannes Berg8318d782008-01-24 19:38:38 +01003697 if (local->scan_band < IEEE80211_NUM_BANDS)
3698 sband = local->hw.wiphy->bands[local->scan_band];
3699 else
3700 sband = NULL;
3701
Johannes Berg69d464d2008-01-28 17:08:56 +01003702 /*
3703 * If we are at an unsupported band and have more bands
3704 * left to scan, advance to the next supported one.
3705 */
3706 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003707 local->scan_band++;
3708 sband = local->hw.wiphy->bands[local->scan_band];
3709 local->scan_channel_idx = 0;
3710 }
3711
Johannes Berg69d464d2008-01-28 17:08:56 +01003712 /* if no more bands/channels left, complete scan */
3713 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003714 ieee80211_scan_completed(local_to_hw(local));
3715 return;
3716 }
Johannes Berg8318d782008-01-24 19:38:38 +01003717 skip = 0;
3718 chan = &sband->channels[local->scan_channel_idx];
3719
3720 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003721 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003722 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003723 skip = 1;
3724
3725 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003726 local->scan_channel = chan;
3727 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003728 printk(KERN_DEBUG "%s: failed to set freq to "
3729 "%d MHz for scan\n", dev->name,
3730 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003731 skip = 1;
3732 }
3733 }
3734
Johannes Berg69d464d2008-01-28 17:08:56 +01003735 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003736 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003737 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003738 /*
3739 * scan_band may end up == IEEE80211_NUM_BANDS, but
3740 * we'll catch that case above and complete the scan
3741 * if that is the case.
3742 */
Johannes Berg8318d782008-01-24 19:38:38 +01003743 local->scan_band++;
3744 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003745 }
3746
3747 if (skip)
3748 break;
3749
3750 next_delay = IEEE80211_PROBE_DELAY +
3751 usecs_to_jiffies(local->hw.channel_change_time);
3752 local->scan_state = SCAN_SEND_PROBE;
3753 break;
3754 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003755 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003756 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003757
3758 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3759 break;
3760 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3761 local->scan_ssid_len);
3762 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003763 break;
3764 }
3765
Zhu Yiece8edd2007-11-22 10:53:21 +08003766 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003767 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3768 next_delay);
3769}
3770
3771
3772static int ieee80211_sta_start_scan(struct net_device *dev,
3773 u8 *ssid, size_t ssid_len)
3774{
3775 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3776 struct ieee80211_sub_if_data *sdata;
3777
3778 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3779 return -EINVAL;
3780
3781 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3782 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3783 * BSSID: MACAddress
3784 * SSID
3785 * ScanType: ACTIVE, PASSIVE
3786 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3787 * a Probe frame during active scanning
3788 * ChannelList
3789 * MinChannelTime (>= ProbeDelay), in TU
3790 * MaxChannelTime: (>= MinChannelTime), in TU
3791 */
3792
3793 /* MLME-SCAN.confirm
3794 * BSSDescriptionSet
3795 * ResultCode: SUCCESS, INVALID_PARAMETERS
3796 */
3797
Zhu Yiece8edd2007-11-22 10:53:21 +08003798 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003799 if (local->scan_dev == dev)
3800 return 0;
3801 return -EBUSY;
3802 }
3803
3804 if (local->ops->hw_scan) {
3805 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003806 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003807 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003808 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003809 local->scan_dev = dev;
3810 }
3811 return rc;
3812 }
3813
Zhu Yiece8edd2007-11-22 10:53:21 +08003814 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003815
Johannes Berg79010422007-09-18 17:29:21 -04003816 rcu_read_lock();
3817 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003818
3819 /* Don't stop the master interface, otherwise we can't transmit
3820 * probes! */
3821 if (sdata->dev == local->mdev)
3822 continue;
3823
Jiri Bencf0706e82007-05-05 11:45:53 -07003824 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003825 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003826 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003827 ieee80211_send_nullfunc(local, sdata, 1);
3828 }
Johannes Berg79010422007-09-18 17:29:21 -04003829 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003830
3831 if (ssid) {
3832 local->scan_ssid_len = ssid_len;
3833 memcpy(local->scan_ssid, ssid, ssid_len);
3834 } else
3835 local->scan_ssid_len = 0;
3836 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07003837 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003838 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07003839 local->scan_dev = dev;
3840
Johannes Berg4150c572007-09-17 01:29:23 -04003841 netif_tx_lock_bh(local->mdev);
3842 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3843 local->ops->configure_filter(local_to_hw(local),
3844 FIF_BCN_PRBRESP_PROMISC,
3845 &local->filter_flags,
3846 local->mdev->mc_count,
3847 local->mdev->mc_list);
3848 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003849
3850 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3851 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3852 IEEE80211_CHANNEL_TIME);
3853
3854 return 0;
3855}
3856
3857
3858int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3859{
3860 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3861 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3862 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3863
Johannes Berg51fb61e2007-12-19 01:31:27 +01003864 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003865 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3866
Zhu Yiece8edd2007-11-22 10:53:21 +08003867 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003868 if (local->scan_dev == dev)
3869 return 0;
3870 return -EBUSY;
3871 }
3872
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003873 ifsta->scan_ssid_len = ssid_len;
3874 if (ssid_len)
3875 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003876 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3877 queue_work(local->hw.workqueue, &ifsta->work);
3878 return 0;
3879}
3880
3881static char *
3882ieee80211_sta_scan_result(struct net_device *dev,
3883 struct ieee80211_sta_bss *bss,
3884 char *current_ev, char *end_buf)
3885{
3886 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3887 struct iw_event iwe;
3888
3889 if (time_after(jiffies,
3890 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3891 return current_ev;
3892
Jiri Bencf0706e82007-05-05 11:45:53 -07003893 memset(&iwe, 0, sizeof(iwe));
3894 iwe.cmd = SIOCGIWAP;
3895 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3896 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3897 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3898 IW_EV_ADDR_LEN);
3899
3900 memset(&iwe, 0, sizeof(iwe));
3901 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01003902 if (bss_mesh_cfg(bss)) {
3903 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003904 iwe.u.data.flags = 1;
3905 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
Johannes Berg902acc72008-02-23 15:17:19 +01003906 bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003907 } else {
3908 iwe.u.data.length = bss->ssid_len;
3909 iwe.u.data.flags = 1;
3910 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3911 bss->ssid);
3912 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003913
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08003914 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
3915 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003916 memset(&iwe, 0, sizeof(iwe));
3917 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01003918 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003919 iwe.u.mode = IW_MODE_MESH;
3920 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07003921 iwe.u.mode = IW_MODE_MASTER;
3922 else
3923 iwe.u.mode = IW_MODE_ADHOC;
3924 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3925 IW_EV_UINT_LEN);
3926 }
3927
3928 memset(&iwe, 0, sizeof(iwe));
3929 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01003930 iwe.u.freq.m = bss->freq;
3931 iwe.u.freq.e = 6;
Jiri Bencf0706e82007-05-05 11:45:53 -07003932 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3933 IW_EV_FREQ_LEN);
Johannes Berg8318d782008-01-24 19:38:38 +01003934
3935 memset(&iwe, 0, sizeof(iwe));
3936 iwe.cmd = SIOCGIWFREQ;
3937 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3938 iwe.u.freq.e = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003939 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3940 IW_EV_FREQ_LEN);
3941
3942 memset(&iwe, 0, sizeof(iwe));
3943 iwe.cmd = IWEVQUAL;
3944 iwe.u.qual.qual = bss->signal;
3945 iwe.u.qual.level = bss->rssi;
3946 iwe.u.qual.noise = bss->noise;
3947 iwe.u.qual.updated = local->wstats_flags;
3948 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3949 IW_EV_QUAL_LEN);
3950
3951 memset(&iwe, 0, sizeof(iwe));
3952 iwe.cmd = SIOCGIWENCODE;
3953 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3954 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3955 else
3956 iwe.u.data.flags = IW_ENCODE_DISABLED;
3957 iwe.u.data.length = 0;
3958 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
3959
3960 if (bss && bss->wpa_ie) {
3961 memset(&iwe, 0, sizeof(iwe));
3962 iwe.cmd = IWEVGENIE;
3963 iwe.u.data.length = bss->wpa_ie_len;
3964 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3965 bss->wpa_ie);
3966 }
3967
3968 if (bss && bss->rsn_ie) {
3969 memset(&iwe, 0, sizeof(iwe));
3970 iwe.cmd = IWEVGENIE;
3971 iwe.u.data.length = bss->rsn_ie_len;
3972 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3973 bss->rsn_ie);
3974 }
3975
3976 if (bss && bss->supp_rates_len > 0) {
3977 /* display all supported rates in readable format */
3978 char *p = current_ev + IW_EV_LCP_LEN;
3979 int i;
3980
3981 memset(&iwe, 0, sizeof(iwe));
3982 iwe.cmd = SIOCGIWRATE;
3983 /* Those two flags are ignored... */
3984 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3985
3986 for (i = 0; i < bss->supp_rates_len; i++) {
3987 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3988 0x7f) * 500000);
3989 p = iwe_stream_add_value(current_ev, p,
3990 end_buf, &iwe, IW_EV_PARAM_LEN);
3991 }
3992 current_ev = p;
3993 }
3994
3995 if (bss) {
3996 char *buf;
3997 buf = kmalloc(30, GFP_ATOMIC);
3998 if (buf) {
3999 memset(&iwe, 0, sizeof(iwe));
4000 iwe.cmd = IWEVCUSTOM;
4001 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4002 iwe.u.data.length = strlen(buf);
4003 current_ev = iwe_stream_add_point(current_ev, end_buf,
4004 &iwe, buf);
4005 kfree(buf);
4006 }
4007 }
4008
Johannes Berg902acc72008-02-23 15:17:19 +01004009 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004010 char *buf;
Johannes Berg902acc72008-02-23 15:17:19 +01004011 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004012 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004013 if (buf) {
4014 memset(&iwe, 0, sizeof(iwe));
4015 iwe.cmd = IWEVCUSTOM;
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004016 sprintf(buf, "Mesh network (version %d)", cfg[0]);
4017 iwe.u.data.length = strlen(buf);
4018 current_ev = iwe_stream_add_point(current_ev, end_buf,
4019 &iwe, buf);
4020 sprintf(buf, "Path Selection Protocol ID: "
4021 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4022 cfg[4]);
4023 iwe.u.data.length = strlen(buf);
4024 current_ev = iwe_stream_add_point(current_ev, end_buf,
4025 &iwe, buf);
4026 sprintf(buf, "Path Selection Metric ID: "
4027 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4028 cfg[8]);
4029 iwe.u.data.length = strlen(buf);
4030 current_ev = iwe_stream_add_point(current_ev, end_buf,
4031 &iwe, buf);
4032 sprintf(buf, "Congestion Control Mode ID: "
4033 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4034 cfg[11], cfg[12]);
4035 iwe.u.data.length = strlen(buf);
4036 current_ev = iwe_stream_add_point(current_ev, end_buf,
4037 &iwe, buf);
4038 sprintf(buf, "Channel Precedence: "
4039 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4040 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004041 iwe.u.data.length = strlen(buf);
4042 current_ev = iwe_stream_add_point(current_ev, end_buf,
4043 &iwe, buf);
4044 kfree(buf);
4045 }
4046 }
4047
Jiri Bencf0706e82007-05-05 11:45:53 -07004048 return current_ev;
4049}
4050
4051
4052int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4053{
4054 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4055 char *current_ev = buf;
4056 char *end_buf = buf + len;
4057 struct ieee80211_sta_bss *bss;
4058
4059 spin_lock_bh(&local->sta_bss_lock);
4060 list_for_each_entry(bss, &local->sta_bss_list, list) {
4061 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4062 spin_unlock_bh(&local->sta_bss_lock);
4063 return -E2BIG;
4064 }
4065 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4066 end_buf);
4067 }
4068 spin_unlock_bh(&local->sta_bss_lock);
4069 return current_ev - buf;
4070}
4071
4072
4073int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4074{
4075 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4076 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4077 kfree(ifsta->extra_ie);
4078 if (len == 0) {
4079 ifsta->extra_ie = NULL;
4080 ifsta->extra_ie_len = 0;
4081 return 0;
4082 }
4083 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4084 if (!ifsta->extra_ie) {
4085 ifsta->extra_ie_len = 0;
4086 return -ENOMEM;
4087 }
4088 memcpy(ifsta->extra_ie, ie, len);
4089 ifsta->extra_ie_len = len;
4090 return 0;
4091}
4092
4093
4094struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4095 struct sk_buff *skb, u8 *bssid,
4096 u8 *addr)
4097{
4098 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4099 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004100 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004101 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07004102
4103 /* TODO: Could consider removing the least recently used entry and
4104 * allow new one to be added. */
4105 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4106 if (net_ratelimit()) {
4107 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004108 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004109 }
4110 return NULL;
4111 }
4112
Joe Perches0795af52007-10-03 17:59:30 -07004113 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004114 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07004115
Johannes Berg73651ee2008-02-25 16:27:47 +01004116 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4117 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004118 return NULL;
4119
Johannes Berg238814f2008-01-28 17:19:37 +01004120 sta->flags |= WLAN_STA_AUTHORIZED;
4121
Johannes Berg8318d782008-01-24 19:38:38 +01004122 sta->supp_rates[local->hw.conf.channel->band] =
4123 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004124
4125 rate_control_rate_init(sta, local);
4126
Johannes Berg73651ee2008-02-25 16:27:47 +01004127 if (sta_info_insert(sta)) {
4128 sta_info_destroy(sta);
4129 return NULL;
4130 }
4131
Johannes Bergd0709a62008-02-25 16:27:46 +01004132 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004133}
4134
4135
4136int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4137{
4138 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4139 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4140
4141 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4142 dev->name, reason);
4143
Johannes Berg51fb61e2007-12-19 01:31:27 +01004144 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4145 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004146 return -EINVAL;
4147
4148 ieee80211_send_deauth(dev, ifsta, reason);
4149 ieee80211_set_disassoc(dev, ifsta, 1);
4150 return 0;
4151}
4152
4153
4154int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4155{
4156 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4157 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4158
4159 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4160 dev->name, reason);
4161
Johannes Berg51fb61e2007-12-19 01:31:27 +01004162 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004163 return -EINVAL;
4164
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004165 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004166 return -1;
4167
4168 ieee80211_send_disassoc(dev, ifsta, reason);
4169 ieee80211_set_disassoc(dev, ifsta, 0);
4170 return 0;
4171}