blob: 63b391d0125159d0339b8d775e3255b1c22c7dac [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
Jiri Bencf0706e82007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010027#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040033#include "rate.h"
34#include "led.h"
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010035#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
37#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40#define IEEE80211_ASSOC_MAX_TRIES 3
41#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010042#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070043#define IEEE80211_PROBE_INTERVAL (60 * HZ)
44#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45#define IEEE80211_SCAN_INTERVAL (2 * HZ)
46#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
47#define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
48
49#define IEEE80211_PROBE_DELAY (HZ / 33)
50#define IEEE80211_CHANNEL_TIME (HZ / 33)
51#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010055#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070056
57#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
Jiri Bencf0706e82007-05-05 11:45:53 -070060#define ERP_INFO_USE_PROTECTION BIT(1)
61
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020062/* mgmt header + 1 byte action code */
63#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
64
65#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
66#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
67#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020068#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
69#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020070
Ron Rindjunsky07db2182007-12-25 17:00:33 +020071/* next values represent the buffer size for A-MPDU frame.
72 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
73#define IEEE80211_MIN_AMPDU_BUF 0x8
74#define IEEE80211_MAX_AMPDU_BUF 0x40
75
Jiri Bencf0706e82007-05-05 11:45:53 -070076static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
77 u8 *ssid, size_t ssid_len);
78static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +010079ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -040080 u8 *ssid, u8 ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070081static void ieee80211_rx_bss_put(struct net_device *dev,
82 struct ieee80211_sta_bss *bss);
83static int ieee80211_sta_find_ibss(struct net_device *dev,
84 struct ieee80211_if_sta *ifsta);
85static int ieee80211_sta_wep_configured(struct net_device *dev);
86static int ieee80211_sta_start_scan(struct net_device *dev,
87 u8 *ssid, size_t ssid_len);
88static int ieee80211_sta_config_auth(struct net_device *dev,
89 struct ieee80211_if_sta *ifsta);
90
91
Luis Carlos Coboee385852008-02-23 15:17:11 +010092void ieee802_11_parse_elems(u8 *start, size_t len,
93 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -070094{
95 size_t left = len;
96 u8 *pos = start;
Jiri Bencf0706e82007-05-05 11:45:53 -070097
98 memset(elems, 0, sizeof(*elems));
99
100 while (left >= 2) {
101 u8 id, elen;
102
103 id = *pos++;
104 elen = *pos++;
105 left -= 2;
106
John W. Linville67a4cce2007-10-12 16:40:37 -0400107 if (elen > left)
108 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700109
110 switch (id) {
111 case WLAN_EID_SSID:
112 elems->ssid = pos;
113 elems->ssid_len = elen;
114 break;
115 case WLAN_EID_SUPP_RATES:
116 elems->supp_rates = pos;
117 elems->supp_rates_len = elen;
118 break;
119 case WLAN_EID_FH_PARAMS:
120 elems->fh_params = pos;
121 elems->fh_params_len = elen;
122 break;
123 case WLAN_EID_DS_PARAMS:
124 elems->ds_params = pos;
125 elems->ds_params_len = elen;
126 break;
127 case WLAN_EID_CF_PARAMS:
128 elems->cf_params = pos;
129 elems->cf_params_len = elen;
130 break;
131 case WLAN_EID_TIM:
132 elems->tim = pos;
133 elems->tim_len = elen;
134 break;
135 case WLAN_EID_IBSS_PARAMS:
136 elems->ibss_params = pos;
137 elems->ibss_params_len = elen;
138 break;
139 case WLAN_EID_CHALLENGE:
140 elems->challenge = pos;
141 elems->challenge_len = elen;
142 break;
143 case WLAN_EID_WPA:
144 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
145 pos[2] == 0xf2) {
146 /* Microsoft OUI (00:50:F2) */
147 if (pos[3] == 1) {
148 /* OUI Type 1 - WPA IE */
149 elems->wpa = pos;
150 elems->wpa_len = elen;
151 } else if (elen >= 5 && pos[3] == 2) {
152 if (pos[4] == 0) {
153 elems->wmm_info = pos;
154 elems->wmm_info_len = elen;
155 } else if (pos[4] == 1) {
156 elems->wmm_param = pos;
157 elems->wmm_param_len = elen;
158 }
159 }
160 }
161 break;
162 case WLAN_EID_RSN:
163 elems->rsn = pos;
164 elems->rsn_len = elen;
165 break;
166 case WLAN_EID_ERP_INFO:
167 elems->erp_info = pos;
168 elems->erp_info_len = elen;
169 break;
170 case WLAN_EID_EXT_SUPP_RATES:
171 elems->ext_supp_rates = pos;
172 elems->ext_supp_rates_len = elen;
173 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200174 case WLAN_EID_HT_CAPABILITY:
175 elems->ht_cap_elem = pos;
176 elems->ht_cap_elem_len = elen;
177 break;
178 case WLAN_EID_HT_EXTRA_INFO:
179 elems->ht_info_elem = pos;
180 elems->ht_info_elem_len = elen;
181 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100182 case WLAN_EID_MESH_ID:
183 elems->mesh_id = pos;
184 elems->mesh_id_len = elen;
185 break;
186 case WLAN_EID_MESH_CONFIG:
187 elems->mesh_config = pos;
188 elems->mesh_config_len = elen;
189 break;
190 case WLAN_EID_PEER_LINK:
191 elems->peer_link = pos;
192 elems->peer_link_len = elen;
193 break;
194 case WLAN_EID_PREQ:
195 elems->preq = pos;
196 elems->preq_len = elen;
197 break;
198 case WLAN_EID_PREP:
199 elems->prep = pos;
200 elems->prep_len = elen;
201 break;
202 case WLAN_EID_PERR:
203 elems->perr = pos;
204 elems->perr_len = elen;
205 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700206 default:
Jiri Bencf0706e82007-05-05 11:45:53 -0700207 break;
208 }
209
210 left -= elen;
211 pos += elen;
212 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700213}
214
215
Jiri Bencf0706e82007-05-05 11:45:53 -0700216static int ecw2cw(int ecw)
217{
Johannes Bergac2bf322008-02-14 00:30:35 +0100218 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700219}
220
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100221
222static void ieee80211_sta_def_wmm_params(struct net_device *dev,
223 struct ieee80211_sta_bss *bss,
224 int ibss)
225{
226 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
227 struct ieee80211_local *local = sdata->local;
228 int i, have_higher_than_11mbit = 0;
229
230
231 /* cf. IEEE 802.11 9.2.12 */
232 for (i = 0; i < bss->supp_rates_len; i++)
233 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
234 have_higher_than_11mbit = 1;
235
236 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
237 have_higher_than_11mbit)
238 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
239 else
240 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
241
242
243 if (local->ops->conf_tx) {
244 struct ieee80211_tx_queue_params qparam;
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100245
246 memset(&qparam, 0, sizeof(qparam));
247
248 qparam.aifs = 2;
249
250 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
251 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
252 qparam.cw_min = 31;
253 else
254 qparam.cw_min = 15;
255
256 qparam.cw_max = 1023;
257 qparam.txop = 0;
258
259 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
260 local->ops->conf_tx(local_to_hw(local),
261 i + IEEE80211_TX_QUEUE_DATA0,
262 &qparam);
263
264 if (ibss) {
265 /* IBSS uses different parameters for Beacon sending */
266 qparam.cw_min++;
267 qparam.cw_min *= 2;
268 qparam.cw_min--;
269 local->ops->conf_tx(local_to_hw(local),
270 IEEE80211_TX_QUEUE_BEACON, &qparam);
271 }
272 }
273}
274
Jiri Bencf0706e82007-05-05 11:45:53 -0700275static void ieee80211_sta_wmm_params(struct net_device *dev,
276 struct ieee80211_if_sta *ifsta,
277 u8 *wmm_param, size_t wmm_param_len)
278{
279 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
280 struct ieee80211_tx_queue_params params;
281 size_t left;
282 int count;
283 u8 *pos;
284
285 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
286 return;
287 count = wmm_param[6] & 0x0f;
288 if (count == ifsta->wmm_last_param_set)
289 return;
290 ifsta->wmm_last_param_set = count;
291
292 pos = wmm_param + 8;
293 left = wmm_param_len - 8;
294
295 memset(&params, 0, sizeof(params));
296
297 if (!local->ops->conf_tx)
298 return;
299
300 local->wmm_acm = 0;
301 for (; left >= 4; left -= 4, pos += 4) {
302 int aci = (pos[0] >> 5) & 0x03;
303 int acm = (pos[0] >> 4) & 0x01;
304 int queue;
305
306 switch (aci) {
307 case 1:
308 queue = IEEE80211_TX_QUEUE_DATA3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200309 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700310 local->wmm_acm |= BIT(0) | BIT(3);
Jiri Bencf0706e82007-05-05 11:45:53 -0700311 break;
312 case 2:
313 queue = IEEE80211_TX_QUEUE_DATA1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200314 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700315 local->wmm_acm |= BIT(4) | BIT(5);
Jiri Bencf0706e82007-05-05 11:45:53 -0700316 break;
317 case 3:
318 queue = IEEE80211_TX_QUEUE_DATA0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200319 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700320 local->wmm_acm |= BIT(6) | BIT(7);
Jiri Bencf0706e82007-05-05 11:45:53 -0700321 break;
322 case 0:
323 default:
324 queue = IEEE80211_TX_QUEUE_DATA2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200325 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700326 local->wmm_acm |= BIT(1) | BIT(2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700327 break;
328 }
329
330 params.aifs = pos[0] & 0x0f;
331 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
332 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100333 params.txop = pos[2] | (pos[3] << 8);
334#ifdef CONFIG_MAC80211_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700335 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100336 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e82007-05-05 11:45:53 -0700337 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100338 params.cw_max, params.txop);
339#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700340 /* TODO: handle ACM (block TX, fallback to next lowest allowed
341 * AC for now) */
342 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
343 printk(KERN_DEBUG "%s: failed to set TX queue "
344 "parameters for queue %d\n", dev->name, queue);
345 }
346 }
347}
348
John W. Linville50c4afb2008-04-15 14:09:27 -0400349static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
350 bool use_protection,
351 bool use_short_preamble)
Daniel Drake56282212007-07-10 19:32:10 +0200352{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100353 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Daniel Drake56282212007-07-10 19:32:10 +0200354 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Joe Perches0795af52007-10-03 17:59:30 -0700355 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100356 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200357
Johannes Berg471b3ef2007-12-28 14:32:58 +0100358 if (use_protection != bss_conf->use_cts_prot) {
Daniel Drake56282212007-07-10 19:32:10 +0200359 if (net_ratelimit()) {
360 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700361 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100362 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200363 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700364 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200365 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100366 bss_conf->use_cts_prot = use_protection;
367 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200368 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200369
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200370 if (use_short_preamble != bss_conf->use_short_preamble) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200371 if (net_ratelimit()) {
372 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700373 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100374 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200375 use_short_preamble ? "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700376 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200377 }
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200378 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100379 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200380 }
Daniel Draked9430a32007-07-27 15:43:24 +0200381
Johannes Berg471b3ef2007-12-28 14:32:58 +0100382 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200383}
384
John W. Linville50c4afb2008-04-15 14:09:27 -0400385static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
386 u8 erp_value)
387{
388 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
389 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
390
391 return ieee80211_handle_protect_preamb(sdata,
392 use_protection, use_short_preamble);
393}
394
395static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
396 struct ieee80211_sta_bss *bss)
397{
398 u32 changed = 0;
399
400 if (bss->has_erp_value)
401 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
402 else {
403 u16 capab = bss->capability;
404 changed |= ieee80211_handle_protect_preamb(sdata, false,
405 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
406 }
407
408 return changed;
409}
410
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200411int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
412 struct ieee80211_ht_info *ht_info)
413{
414
415 if (ht_info == NULL)
416 return -EINVAL;
417
418 memset(ht_info, 0, sizeof(*ht_info));
419
420 if (ht_cap_ie) {
421 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
422
423 ht_info->ht_supported = 1;
424 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
425 ht_info->ampdu_factor =
426 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
427 ht_info->ampdu_density =
428 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
429 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
430 } else
431 ht_info->ht_supported = 0;
432
433 return 0;
434}
435
436int ieee80211_ht_addt_info_ie_to_ht_bss_info(
437 struct ieee80211_ht_addt_info *ht_add_info_ie,
438 struct ieee80211_ht_bss_info *bss_info)
439{
440 if (bss_info == NULL)
441 return -EINVAL;
442
443 memset(bss_info, 0, sizeof(*bss_info));
444
445 if (ht_add_info_ie) {
446 u16 op_mode;
447 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
448
449 bss_info->primary_channel = ht_add_info_ie->control_chan;
450 bss_info->bss_cap = ht_add_info_ie->ht_param;
451 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
452 }
453
454 return 0;
455}
Daniel Drake56282212007-07-10 19:32:10 +0200456
Jiri Bencf0706e82007-05-05 11:45:53 -0700457static void ieee80211_sta_send_associnfo(struct net_device *dev,
458 struct ieee80211_if_sta *ifsta)
459{
460 char *buf;
461 size_t len;
462 int i;
463 union iwreq_data wrqu;
464
465 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
466 return;
467
468 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200469 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700470 if (!buf)
471 return;
472
473 len = sprintf(buf, "ASSOCINFO(");
474 if (ifsta->assocreq_ies) {
475 len += sprintf(buf + len, "ReqIEs=");
476 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
477 len += sprintf(buf + len, "%02x",
478 ifsta->assocreq_ies[i]);
479 }
480 }
481 if (ifsta->assocresp_ies) {
482 if (ifsta->assocreq_ies)
483 len += sprintf(buf + len, " ");
484 len += sprintf(buf + len, "RespIEs=");
485 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
486 len += sprintf(buf + len, "%02x",
487 ifsta->assocresp_ies[i]);
488 }
489 }
490 len += sprintf(buf + len, ")");
491
492 if (len > IW_CUSTOM_MAX) {
493 len = sprintf(buf, "ASSOCRESPIE=");
494 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
495 len += sprintf(buf + len, "%02x",
496 ifsta->assocresp_ies[i]);
497 }
498 }
499
500 memset(&wrqu, 0, sizeof(wrqu));
501 wrqu.data.length = len;
502 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
503
504 kfree(buf);
505}
506
507
508static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400509 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200510 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700511{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100512 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
513 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700514 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700515 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100516 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700517
Jiri Bencf0706e82007-05-05 11:45:53 -0700518 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200519 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400520
521 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
522
Johannes Berg51fb61e2007-12-19 01:31:27 +0100523 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700524 return;
Daniel Drake56282212007-07-10 19:32:10 +0200525
John W. Linville65c107a2007-10-05 14:23:27 -0400526 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Tomas Winkler38668c02008-03-28 16:33:32 -0700527 conf->channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400528 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200529 if (bss) {
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700530 /* set timing information */
531 sdata->bss_conf.beacon_int = bss->beacon_int;
532 sdata->bss_conf.timestamp = bss->timestamp;
533
John W. Linville50c4afb2008-04-15 14:09:27 -0400534 changed |= ieee80211_handle_bss_capability(sdata, bss);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700535
Daniel Drake56282212007-07-10 19:32:10 +0200536 ieee80211_rx_bss_put(dev, bss);
537 }
538
Tomas Winkler38668c02008-03-28 16:33:32 -0700539 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
540 changed |= BSS_CHANGED_HT;
541 sdata->bss_conf.assoc_ht = 1;
542 sdata->bss_conf.ht_conf = &conf->ht_conf;
543 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
544 }
545
Jiri Bencf0706e82007-05-05 11:45:53 -0700546 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400547 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700548 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
549 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
550 ieee80211_sta_send_associnfo(dev, ifsta);
551 } else {
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700552 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400553 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700554 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200555 ieee80211_reset_erp_info(dev);
Tomas Winkler38668c02008-03-28 16:33:32 -0700556
557 sdata->bss_conf.assoc_ht = 0;
558 sdata->bss_conf.ht_conf = NULL;
559 sdata->bss_conf.ht_bss_conf = NULL;
560
Jiri Bencf0706e82007-05-05 11:45:53 -0700561 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
562 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700563 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200564 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100565
Tomas Winklerb2205252008-01-27 16:18:22 +0200566 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100567 ieee80211_bss_info_change_notify(sdata, changed);
Reinette Chatre41a7be42008-04-03 16:08:49 -0700568 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
569 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700570}
571
572static void ieee80211_set_disassoc(struct net_device *dev,
573 struct ieee80211_if_sta *ifsta, int deauth)
574{
575 if (deauth)
576 ifsta->auth_tries = 0;
577 ifsta->assoc_tries = 0;
578 ieee80211_set_associated(dev, ifsta, 0);
579}
580
Luis Carlos Coboee385852008-02-23 15:17:11 +0100581void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
582 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700583{
584 struct ieee80211_sub_if_data *sdata;
585 struct ieee80211_tx_packet_data *pkt_data;
586
587 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
588 skb->dev = sdata->local->mdev;
589 skb_set_mac_header(skb, 0);
590 skb_set_network_header(skb, 0);
591 skb_set_transport_header(skb, 0);
592
593 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
594 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
595 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400596 if (!encrypt)
597 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700598
599 dev_queue_xmit(skb);
600}
601
602
603static void ieee80211_send_auth(struct net_device *dev,
604 struct ieee80211_if_sta *ifsta,
605 int transaction, u8 *extra, size_t extra_len,
606 int encrypt)
607{
608 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
609 struct sk_buff *skb;
610 struct ieee80211_mgmt *mgmt;
611
612 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
613 sizeof(*mgmt) + 6 + extra_len);
614 if (!skb) {
615 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
616 "frame\n", dev->name);
617 return;
618 }
619 skb_reserve(skb, local->hw.extra_tx_headroom);
620
621 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
622 memset(mgmt, 0, 24 + 6);
623 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
624 IEEE80211_STYPE_AUTH);
625 if (encrypt)
626 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
627 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
628 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
629 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
630 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
631 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
632 ifsta->auth_transaction = transaction + 1;
633 mgmt->u.auth.status_code = cpu_to_le16(0);
634 if (extra)
635 memcpy(skb_put(skb, extra_len), extra, extra_len);
636
637 ieee80211_sta_tx(dev, skb, encrypt);
638}
639
640
641static void ieee80211_authenticate(struct net_device *dev,
642 struct ieee80211_if_sta *ifsta)
643{
Joe Perches0795af52007-10-03 17:59:30 -0700644 DECLARE_MAC_BUF(mac);
645
Jiri Bencf0706e82007-05-05 11:45:53 -0700646 ifsta->auth_tries++;
647 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700648 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700649 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700650 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700651 ifsta->state = IEEE80211_DISABLED;
652 return;
653 }
654
655 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700656 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
657 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700658
659 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
660
661 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
662}
663
664
665static void ieee80211_send_assoc(struct net_device *dev,
666 struct ieee80211_if_sta *ifsta)
667{
668 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700669 struct sk_buff *skb;
670 struct ieee80211_mgmt *mgmt;
671 u8 *pos, *ies;
672 int i, len;
673 u16 capab;
674 struct ieee80211_sta_bss *bss;
675 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100676 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700677
678 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
679 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
680 ifsta->ssid_len);
681 if (!skb) {
682 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
683 "frame\n", dev->name);
684 return;
685 }
686 skb_reserve(skb, local->hw.extra_tx_headroom);
687
Johannes Berg8318d782008-01-24 19:38:38 +0100688 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
689
Jiri Bencf0706e82007-05-05 11:45:53 -0700690 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100691
692 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
693 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
694 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
695 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
696 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700697 }
Johannes Berg8318d782008-01-24 19:38:38 +0100698
699 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
700 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400701 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700702 if (bss) {
703 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
704 capab |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +0200705 if (bss->wmm_ie)
Jiri Bencf0706e82007-05-05 11:45:53 -0700706 wmm = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700707 ieee80211_rx_bss_put(dev, bss);
708 }
709
710 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
711 memset(mgmt, 0, 24);
712 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
713 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
714 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
715
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400716 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700717 skb_put(skb, 10);
718 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
719 IEEE80211_STYPE_REASSOC_REQ);
720 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
721 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
722 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
723 ETH_ALEN);
724 } else {
725 skb_put(skb, 4);
726 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
727 IEEE80211_STYPE_ASSOC_REQ);
728 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
729 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
730 }
731
732 /* SSID */
733 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
734 *pos++ = WLAN_EID_SSID;
735 *pos++ = ifsta->ssid_len;
736 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
737
Johannes Berg8318d782008-01-24 19:38:38 +0100738 len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700739 if (len > 8)
740 len = 8;
741 pos = skb_put(skb, len + 2);
742 *pos++ = WLAN_EID_SUPP_RATES;
743 *pos++ = len;
744 for (i = 0; i < len; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100745 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700746 *pos++ = (u8) (rate / 5);
747 }
748
Johannes Berg8318d782008-01-24 19:38:38 +0100749 if (sband->n_bitrates > len) {
750 pos = skb_put(skb, sband->n_bitrates - len + 2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700751 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Johannes Berg8318d782008-01-24 19:38:38 +0100752 *pos++ = sband->n_bitrates - len;
753 for (i = len; i < sband->n_bitrates; i++) {
754 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700755 *pos++ = (u8) (rate / 5);
756 }
757 }
758
759 if (ifsta->extra_ie) {
760 pos = skb_put(skb, ifsta->extra_ie_len);
761 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
762 }
763
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400764 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700765 pos = skb_put(skb, 9);
766 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
767 *pos++ = 7; /* len */
768 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
769 *pos++ = 0x50;
770 *pos++ = 0xf2;
771 *pos++ = 2; /* WME */
772 *pos++ = 0; /* WME info */
773 *pos++ = 1; /* WME ver */
774 *pos++ = 0;
775 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200776 /* wmm support is a must to HT */
Johannes Berg8318d782008-01-24 19:38:38 +0100777 if (wmm && sband->ht_info.ht_supported) {
778 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200779 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
780 *pos++ = WLAN_EID_HT_CAPABILITY;
781 *pos++ = sizeof(struct ieee80211_ht_cap);
782 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
783 memcpy(pos, &tmp, sizeof(u16));
784 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100785 /* TODO: needs a define here for << 2 */
786 *pos++ = sband->ht_info.ampdu_factor |
787 (sband->ht_info.ampdu_density << 2);
788 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200789 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700790
791 kfree(ifsta->assocreq_ies);
792 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200793 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700794 if (ifsta->assocreq_ies)
795 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
796
797 ieee80211_sta_tx(dev, skb, 0);
798}
799
800
801static void ieee80211_send_deauth(struct net_device *dev,
802 struct ieee80211_if_sta *ifsta, u16 reason)
803{
804 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
805 struct sk_buff *skb;
806 struct ieee80211_mgmt *mgmt;
807
808 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
809 if (!skb) {
810 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
811 "frame\n", dev->name);
812 return;
813 }
814 skb_reserve(skb, local->hw.extra_tx_headroom);
815
816 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
817 memset(mgmt, 0, 24);
818 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
819 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
820 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
821 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
822 IEEE80211_STYPE_DEAUTH);
823 skb_put(skb, 2);
824 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
825
826 ieee80211_sta_tx(dev, skb, 0);
827}
828
829
830static void ieee80211_send_disassoc(struct net_device *dev,
831 struct ieee80211_if_sta *ifsta, u16 reason)
832{
833 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
834 struct sk_buff *skb;
835 struct ieee80211_mgmt *mgmt;
836
837 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
838 if (!skb) {
839 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
840 "frame\n", dev->name);
841 return;
842 }
843 skb_reserve(skb, local->hw.extra_tx_headroom);
844
845 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
846 memset(mgmt, 0, 24);
847 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
848 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
849 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
850 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
851 IEEE80211_STYPE_DISASSOC);
852 skb_put(skb, 2);
853 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
854
855 ieee80211_sta_tx(dev, skb, 0);
856}
857
858
859static int ieee80211_privacy_mismatch(struct net_device *dev,
860 struct ieee80211_if_sta *ifsta)
861{
John W. Linville65c107a2007-10-05 14:23:27 -0400862 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700863 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000864 int bss_privacy;
865 int wep_privacy;
866 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700867
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000868 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700869 return 0;
870
Johannes Berg8318d782008-01-24 19:38:38 +0100871 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
872 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400873 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700874 if (!bss)
875 return 0;
876
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000877 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
878 wep_privacy = !!ieee80211_sta_wep_configured(dev);
879 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700880
881 ieee80211_rx_bss_put(dev, bss);
882
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000883 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
884 return 0;
885
886 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700887}
888
889
890static void ieee80211_associate(struct net_device *dev,
891 struct ieee80211_if_sta *ifsta)
892{
Joe Perches0795af52007-10-03 17:59:30 -0700893 DECLARE_MAC_BUF(mac);
894
Jiri Bencf0706e82007-05-05 11:45:53 -0700895 ifsta->assoc_tries++;
896 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700897 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700898 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700899 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700900 ifsta->state = IEEE80211_DISABLED;
901 return;
902 }
903
904 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700905 printk(KERN_DEBUG "%s: associate with AP %s\n",
906 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700907 if (ieee80211_privacy_mismatch(dev, ifsta)) {
908 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
909 "mixed-cell disabled - abort association\n", dev->name);
910 ifsta->state = IEEE80211_DISABLED;
911 return;
912 }
913
914 ieee80211_send_assoc(dev, ifsta);
915
916 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
917}
918
919
920static void ieee80211_associated(struct net_device *dev,
921 struct ieee80211_if_sta *ifsta)
922{
923 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
924 struct sta_info *sta;
925 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700926 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700927
928 /* TODO: start monitoring current AP signal quality and number of
929 * missed beacons. Scan other channels every now and then and search
930 * for better APs. */
931 /* TODO: remove expired BSSes */
932
933 ifsta->state = IEEE80211_ASSOCIATED;
934
Johannes Bergd0709a62008-02-25 16:27:46 +0100935 rcu_read_lock();
936
Jiri Bencf0706e82007-05-05 11:45:53 -0700937 sta = sta_info_get(local, ifsta->bssid);
938 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700939 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
940 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700941 disassoc = 1;
942 } else {
943 disassoc = 0;
944 if (time_after(jiffies,
945 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400946 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700947 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700948 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -0700949 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700950 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700951 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +0100952 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400953 } else
Jiri Bencf0706e82007-05-05 11:45:53 -0700954 ieee80211_send_probe_req(dev, ifsta->bssid,
955 local->scan_ssid,
956 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400957 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700958 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400959 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700960 if (time_after(jiffies, ifsta->last_probe +
961 IEEE80211_PROBE_INTERVAL)) {
962 ifsta->last_probe = jiffies;
963 ieee80211_send_probe_req(dev, ifsta->bssid,
964 ifsta->ssid,
965 ifsta->ssid_len);
966 }
967 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700968 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100969
970 rcu_read_unlock();
971
Johannes Berg3b967662008-04-08 17:56:52 +0200972 if (disassoc && sta)
Johannes Bergd0709a62008-02-25 16:27:46 +0100973 sta_info_destroy(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100974
Jiri Bencf0706e82007-05-05 11:45:53 -0700975 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -0500976 ifsta->state = IEEE80211_DISABLED;
977 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700978 } else {
979 mod_timer(&ifsta->timer, jiffies +
980 IEEE80211_MONITORING_INTERVAL);
981 }
982}
983
984
985static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
986 u8 *ssid, size_t ssid_len)
987{
988 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +0100989 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700990 struct sk_buff *skb;
991 struct ieee80211_mgmt *mgmt;
992 u8 *pos, *supp_rates, *esupp_rates = NULL;
993 int i;
994
995 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
996 if (!skb) {
997 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
998 "request\n", dev->name);
999 return;
1000 }
1001 skb_reserve(skb, local->hw.extra_tx_headroom);
1002
1003 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1004 memset(mgmt, 0, 24);
1005 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1006 IEEE80211_STYPE_PROBE_REQ);
1007 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1008 if (dst) {
1009 memcpy(mgmt->da, dst, ETH_ALEN);
1010 memcpy(mgmt->bssid, dst, ETH_ALEN);
1011 } else {
1012 memset(mgmt->da, 0xff, ETH_ALEN);
1013 memset(mgmt->bssid, 0xff, ETH_ALEN);
1014 }
1015 pos = skb_put(skb, 2 + ssid_len);
1016 *pos++ = WLAN_EID_SSID;
1017 *pos++ = ssid_len;
1018 memcpy(pos, ssid, ssid_len);
1019
1020 supp_rates = skb_put(skb, 2);
1021 supp_rates[0] = WLAN_EID_SUPP_RATES;
1022 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001023 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1024
1025 for (i = 0; i < sband->n_bitrates; i++) {
1026 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07001027 if (esupp_rates) {
1028 pos = skb_put(skb, 1);
1029 esupp_rates[1]++;
1030 } else if (supp_rates[1] == 8) {
1031 esupp_rates = skb_put(skb, 3);
1032 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1033 esupp_rates[1] = 1;
1034 pos = &esupp_rates[2];
1035 } else {
1036 pos = skb_put(skb, 1);
1037 supp_rates[1]++;
1038 }
Johannes Berg8318d782008-01-24 19:38:38 +01001039 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -07001040 }
1041
1042 ieee80211_sta_tx(dev, skb, 0);
1043}
1044
1045
1046static int ieee80211_sta_wep_configured(struct net_device *dev)
1047{
1048 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1049 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001050 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -07001051 return 0;
1052 return 1;
1053}
1054
1055
1056static void ieee80211_auth_completed(struct net_device *dev,
1057 struct ieee80211_if_sta *ifsta)
1058{
1059 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001060 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001061 ieee80211_associate(dev, ifsta);
1062}
1063
1064
1065static void ieee80211_auth_challenge(struct net_device *dev,
1066 struct ieee80211_if_sta *ifsta,
1067 struct ieee80211_mgmt *mgmt,
1068 size_t len)
1069{
1070 u8 *pos;
1071 struct ieee802_11_elems elems;
1072
1073 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1074 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001075 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001076 if (!elems.challenge) {
1077 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1078 "frame\n", dev->name);
1079 return;
1080 }
1081 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1082 elems.challenge_len + 2, 1);
1083}
1084
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001085static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1086 u8 dialog_token, u16 status, u16 policy,
1087 u16 buf_size, u16 timeout)
1088{
1089 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1090 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1091 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1092 struct sk_buff *skb;
1093 struct ieee80211_mgmt *mgmt;
1094 u16 capab;
1095
Ester Kummer3acea5b2008-04-17 16:05:14 -07001096 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1097
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001098 if (!skb) {
1099 printk(KERN_DEBUG "%s: failed to allocate buffer "
1100 "for addba resp frame\n", dev->name);
1101 return;
1102 }
1103
1104 skb_reserve(skb, local->hw.extra_tx_headroom);
1105 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1106 memset(mgmt, 0, 24);
1107 memcpy(mgmt->da, da, ETH_ALEN);
1108 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001109 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001110 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1111 else
1112 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1113 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1114 IEEE80211_STYPE_ACTION);
1115
1116 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1117 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1118 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1119 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1120
1121 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1122 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1123 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1124
1125 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1126 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1127 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1128
1129 ieee80211_sta_tx(dev, skb, 0);
1130
1131 return;
1132}
1133
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001134void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1135 u16 tid, u8 dialog_token, u16 start_seq_num,
1136 u16 agg_size, u16 timeout)
1137{
1138 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1139 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1140 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1141 struct sk_buff *skb;
1142 struct ieee80211_mgmt *mgmt;
1143 u16 capab;
1144
Ester Kummer3acea5b2008-04-17 16:05:14 -07001145 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001146
1147 if (!skb) {
1148 printk(KERN_ERR "%s: failed to allocate buffer "
1149 "for addba request frame\n", dev->name);
1150 return;
1151 }
1152 skb_reserve(skb, local->hw.extra_tx_headroom);
1153 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1154 memset(mgmt, 0, 24);
1155 memcpy(mgmt->da, da, ETH_ALEN);
1156 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1157 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1158 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1159 else
1160 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1161
1162 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1163 IEEE80211_STYPE_ACTION);
1164
1165 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1166
1167 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1168 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1169
1170 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1171 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1172 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1173 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1174
1175 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1176
1177 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1178 mgmt->u.action.u.addba_req.start_seq_num =
1179 cpu_to_le16(start_seq_num << 4);
1180
1181 ieee80211_sta_tx(dev, skb, 0);
1182}
1183
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001184static void ieee80211_sta_process_addba_request(struct net_device *dev,
1185 struct ieee80211_mgmt *mgmt,
1186 size_t len)
1187{
1188 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001189 struct ieee80211_hw *hw = &local->hw;
1190 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001191 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001192 struct tid_ampdu_rx *tid_agg_rx;
1193 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001194 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001195 int ret = -EOPNOTSUPP;
1196 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001197
Johannes Bergd0709a62008-02-25 16:27:46 +01001198 rcu_read_lock();
1199
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001200 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001201 if (!sta) {
1202 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001203 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001204 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001205
1206 /* extract session parameters from addba request frame */
1207 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1208 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001209 start_seq_num =
1210 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001211
1212 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1213 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1214 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1215 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1216
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001217 status = WLAN_STATUS_REQUEST_DECLINED;
1218
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001219 /* sanity check for incoming parameters:
1220 * check if configuration can support the BA policy
1221 * and if buffer size does not exceeds max value */
1222 if (((ba_policy != 1)
1223 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1224 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1225 status = WLAN_STATUS_INVALID_QOS_PARAM;
1226#ifdef CONFIG_MAC80211_HT_DEBUG
1227 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001228 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001229 "%s on tid %u. policy %d, buffer size %d\n",
1230 print_mac(mac, mgmt->sa), tid, ba_policy,
1231 buf_size);
1232#endif /* CONFIG_MAC80211_HT_DEBUG */
1233 goto end_no_lock;
1234 }
1235 /* determine default buffer size */
1236 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001237 struct ieee80211_supported_band *sband;
1238
1239 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001240 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001241 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001242 }
1243
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001244
1245 /* examine state machine */
1246 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1247
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001248 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001249#ifdef CONFIG_MAC80211_HT_DEBUG
1250 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001251 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001252 "%s on tid %u\n",
1253 print_mac(mac, mgmt->sa), tid);
1254#endif /* CONFIG_MAC80211_HT_DEBUG */
1255 goto end;
1256 }
1257
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001258 /* prepare A-MPDU MLME for Rx aggregation */
1259 sta->ampdu_mlme.tid_rx[tid] =
1260 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1261 if (!sta->ampdu_mlme.tid_rx[tid]) {
1262 if (net_ratelimit())
1263 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1264 tid);
1265 goto end;
1266 }
1267 /* rx timer */
1268 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1269 sta_rx_agg_session_timer_expired;
1270 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1271 (unsigned long)&sta->timer_to_tid[tid];
1272 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1273
1274 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1275
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001276 /* prepare reordering buffer */
1277 tid_agg_rx->reorder_buf =
1278 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001279 if (!tid_agg_rx->reorder_buf) {
1280 if (net_ratelimit())
1281 printk(KERN_ERR "can not allocate reordering buffer "
1282 "to tid %d\n", tid);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001283 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001284 goto end;
1285 }
1286 memset(tid_agg_rx->reorder_buf, 0,
1287 buf_size * sizeof(struct sk_buf *));
1288
1289 if (local->ops->ampdu_action)
1290 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001291 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001292#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001293 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001294#endif /* CONFIG_MAC80211_HT_DEBUG */
1295
1296 if (ret) {
1297 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001298 kfree(tid_agg_rx);
1299 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001300 goto end;
1301 }
1302
1303 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001304 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001305 tid_agg_rx->dialog_token = dialog_token;
1306 tid_agg_rx->ssn = start_seq_num;
1307 tid_agg_rx->head_seq_num = start_seq_num;
1308 tid_agg_rx->buf_size = buf_size;
1309 tid_agg_rx->timeout = timeout;
1310 tid_agg_rx->stored_mpdu_num = 0;
1311 status = WLAN_STATUS_SUCCESS;
1312end:
1313 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1314
1315end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001316 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1317 dialog_token, status, 1, buf_size, timeout);
1318 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001319}
Jiri Bencf0706e82007-05-05 11:45:53 -07001320
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001321static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1322 struct ieee80211_mgmt *mgmt,
1323 size_t len)
1324{
1325 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1326 struct ieee80211_hw *hw = &local->hw;
1327 struct sta_info *sta;
1328 u16 capab;
1329 u16 tid;
1330 u8 *state;
1331
Johannes Bergd0709a62008-02-25 16:27:46 +01001332 rcu_read_lock();
1333
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001334 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001335 if (!sta) {
1336 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001337 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001338 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001339
1340 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1341 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1342
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001343 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001344
1345 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1346
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001347 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1348 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1349 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1350 "%d\n", *state);
1351 goto addba_resp_exit;
1352 }
1353
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001354 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001355 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001356 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1357#ifdef CONFIG_MAC80211_HT_DEBUG
1358 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1359#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001360 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001361 }
1362
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001363 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001364#ifdef CONFIG_MAC80211_HT_DEBUG
1365 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1366#endif /* CONFIG_MAC80211_HT_DEBUG */
1367 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1368 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001369 if (*state & HT_ADDBA_RECEIVED_MSK)
1370 printk(KERN_DEBUG "double addBA response\n");
1371
1372 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001373 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001374
1375 if (*state == HT_AGG_STATE_OPERATIONAL) {
1376 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1377 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1378 }
1379
1380 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1381 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1382 } else {
1383 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1384
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001385 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001386 /* this will allow the state check in stop_BA_session */
1387 *state = HT_AGG_STATE_OPERATIONAL;
1388 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1389 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1390 WLAN_BACK_INITIATOR);
1391 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001392
1393addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001394 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001395}
1396
1397void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1398 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001399{
1400 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1401 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1402 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1403 struct sk_buff *skb;
1404 struct ieee80211_mgmt *mgmt;
1405 u16 params;
1406
Ester Kummer3acea5b2008-04-17 16:05:14 -07001407 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001408
1409 if (!skb) {
1410 printk(KERN_ERR "%s: failed to allocate buffer "
1411 "for delba frame\n", dev->name);
1412 return;
1413 }
1414
1415 skb_reserve(skb, local->hw.extra_tx_headroom);
1416 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1417 memset(mgmt, 0, 24);
1418 memcpy(mgmt->da, da, ETH_ALEN);
1419 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001420 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001421 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1422 else
1423 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1424 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1425 IEEE80211_STYPE_ACTION);
1426
1427 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1428
1429 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1430 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1431 params = (u16)(initiator << 11); /* bit 11 initiator */
1432 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1433
1434 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1435 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1436
1437 ieee80211_sta_tx(dev, skb, 0);
1438}
1439
1440void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1441 u16 initiator, u16 reason)
1442{
1443 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1444 struct ieee80211_hw *hw = &local->hw;
1445 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001446 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001447 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001448
Johannes Bergd0709a62008-02-25 16:27:46 +01001449 rcu_read_lock();
1450
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001451 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001452 if (!sta) {
1453 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001454 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001455 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001456
1457 /* check if TID is in operational state */
1458 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001459 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001460 != HT_AGG_STATE_OPERATIONAL) {
1461 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Johannes Bergd0709a62008-02-25 16:27:46 +01001462 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001463 return;
1464 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001465 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001466 HT_AGG_STATE_REQ_STOP_BA_MSK |
1467 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001468 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001469
1470 /* stop HW Rx aggregation. ampdu_action existence
1471 * already verified in session init so we add the BUG_ON */
1472 BUG_ON(!local->ops->ampdu_action);
1473
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001474#ifdef CONFIG_MAC80211_HT_DEBUG
1475 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1476 print_mac(mac, ra), tid);
1477#endif /* CONFIG_MAC80211_HT_DEBUG */
1478
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001479 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001480 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001481 if (ret)
1482 printk(KERN_DEBUG "HW problem - can not stop rx "
1483 "aggergation for tid %d\n", tid);
1484
1485 /* shutdown timer has not expired */
1486 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001487 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001488
1489 /* check if this is a self generated aggregation halt */
1490 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1491 ieee80211_send_delba(dev, ra, tid, 0, reason);
1492
1493 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001494 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1495 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001496 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001497 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1498 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1499 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001500 }
1501 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001502 /* free resources */
1503 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1504 kfree(sta->ampdu_mlme.tid_rx[tid]);
1505 sta->ampdu_mlme.tid_rx[tid] = NULL;
1506 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001507
Johannes Bergd0709a62008-02-25 16:27:46 +01001508 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001509}
1510
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001511
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001512static void ieee80211_sta_process_delba(struct net_device *dev,
1513 struct ieee80211_mgmt *mgmt, size_t len)
1514{
1515 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1516 struct sta_info *sta;
1517 u16 tid, params;
1518 u16 initiator;
1519 DECLARE_MAC_BUF(mac);
1520
Johannes Bergd0709a62008-02-25 16:27:46 +01001521 rcu_read_lock();
1522
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001523 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001524 if (!sta) {
1525 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001526 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001527 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001528
1529 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1530 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1531 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1532
1533#ifdef CONFIG_MAC80211_HT_DEBUG
1534 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001535 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1536 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001537 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001538 mgmt->u.action.u.delba.reason_code);
1539#endif /* CONFIG_MAC80211_HT_DEBUG */
1540
1541 if (initiator == WLAN_BACK_INITIATOR)
1542 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1543 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001544 else { /* WLAN_BACK_RECIPIENT */
1545 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001546 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001547 HT_AGG_STATE_OPERATIONAL;
1548 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1549 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1550 WLAN_BACK_RECIPIENT);
1551 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001552 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001553}
1554
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001555/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001556 * After sending add Block Ack request we activated a timer until
1557 * add Block Ack response will arrive from the recipient.
1558 * If this timer expires sta_addba_resp_timer_expired will be executed.
1559 */
1560void sta_addba_resp_timer_expired(unsigned long data)
1561{
1562 /* not an elegant detour, but there is no choice as the timer passes
1563 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001564 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001565 * array gives the sta through container_of */
1566 u16 tid = *(int *)data;
1567 struct sta_info *temp_sta = container_of((void *)data,
1568 struct sta_info, timer_to_tid[tid]);
1569
1570 struct ieee80211_local *local = temp_sta->local;
1571 struct ieee80211_hw *hw = &local->hw;
1572 struct sta_info *sta;
1573 u8 *state;
1574
Johannes Bergd0709a62008-02-25 16:27:46 +01001575 rcu_read_lock();
1576
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001577 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001578 if (!sta) {
1579 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001580 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001581 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001582
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001583 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001584 /* check if the TID waits for addBA response */
1585 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1586 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1587 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1588 *state = HT_AGG_STATE_IDLE;
1589 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1590 "expecting addBA response there", tid);
1591 goto timer_expired_exit;
1592 }
1593
1594 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1595
1596 /* go through the state check in stop_BA_session */
1597 *state = HT_AGG_STATE_OPERATIONAL;
1598 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1599 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1600 WLAN_BACK_INITIATOR);
1601
1602timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001603 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001604}
1605
1606/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001607 * After accepting the AddBA Request we activated a timer,
1608 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001609 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1610 */
1611void sta_rx_agg_session_timer_expired(unsigned long data)
1612{
1613 /* not an elegant detour, but there is no choice as the timer passes
1614 * only one argument, and verious sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001615 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001616 * array gives the sta through container_of */
1617 u8 *ptid = (u8 *)data;
1618 u8 *timer_to_id = ptid - *ptid;
1619 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1620 timer_to_tid[0]);
1621
1622 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergd0709a62008-02-25 16:27:46 +01001623 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1624 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001625 WLAN_REASON_QSTA_TIMEOUT);
1626}
1627
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001628void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1629{
1630 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1631 int i;
1632
1633 for (i = 0; i < STA_TID_NUM; i++) {
1634 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1635 WLAN_BACK_INITIATOR);
1636 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1637 WLAN_BACK_RECIPIENT,
1638 WLAN_REASON_QSTA_LEAVE_QBSS);
1639 }
1640}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001641
Jiri Bencf0706e82007-05-05 11:45:53 -07001642static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1643 struct ieee80211_if_sta *ifsta,
1644 struct ieee80211_mgmt *mgmt,
1645 size_t len)
1646{
1647 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1648 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001649 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001650
1651 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Berg51fb61e2007-12-19 01:31:27 +01001652 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001653 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001654 "%s, but not in authenticate state - ignored\n",
1655 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001656 return;
1657 }
1658
1659 if (len < 24 + 6) {
1660 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001661 "received from %s - ignored\n",
1662 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001663 return;
1664 }
1665
Johannes Berg51fb61e2007-12-19 01:31:27 +01001666 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001667 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1668 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001669 "unknown AP (SA=%s BSSID=%s) - "
1670 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1671 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001672 return;
1673 }
1674
Johannes Berg51fb61e2007-12-19 01:31:27 +01001675 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001676 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1677 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001678 "unknown BSSID (SA=%s BSSID=%s) - "
1679 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1680 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001681 return;
1682 }
1683
1684 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1685 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1686 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1687
Joe Perches0795af52007-10-03 17:59:30 -07001688 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e82007-05-05 11:45:53 -07001689 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001690 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e82007-05-05 11:45:53 -07001691 auth_transaction, status_code);
1692
Johannes Berg51fb61e2007-12-19 01:31:27 +01001693 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001694 /* IEEE 802.11 standard does not require authentication in IBSS
1695 * networks and most implementations do not seem to use it.
1696 * However, try to reply to authentication attempts if someone
1697 * has actually implemented this.
1698 * TODO: Could implement shared key authentication. */
1699 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1700 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1701 "frame (alg=%d transaction=%d)\n",
1702 dev->name, auth_alg, auth_transaction);
1703 return;
1704 }
1705 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1706 }
1707
1708 if (auth_alg != ifsta->auth_alg ||
1709 auth_transaction != ifsta->auth_transaction) {
1710 printk(KERN_DEBUG "%s: unexpected authentication frame "
1711 "(alg=%d transaction=%d)\n",
1712 dev->name, auth_alg, auth_transaction);
1713 return;
1714 }
1715
1716 if (status_code != WLAN_STATUS_SUCCESS) {
1717 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1718 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1719 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1720 u8 algs[3];
1721 const int num_algs = ARRAY_SIZE(algs);
1722 int i, pos;
1723 algs[0] = algs[1] = algs[2] = 0xff;
1724 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1725 algs[0] = WLAN_AUTH_OPEN;
1726 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1727 algs[1] = WLAN_AUTH_SHARED_KEY;
1728 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1729 algs[2] = WLAN_AUTH_LEAP;
1730 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1731 pos = 0;
1732 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1733 pos = 1;
1734 else
1735 pos = 2;
1736 for (i = 0; i < num_algs; i++) {
1737 pos++;
1738 if (pos >= num_algs)
1739 pos = 0;
1740 if (algs[pos] == ifsta->auth_alg ||
1741 algs[pos] == 0xff)
1742 continue;
1743 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1744 !ieee80211_sta_wep_configured(dev))
1745 continue;
1746 ifsta->auth_alg = algs[pos];
1747 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1748 "next try\n",
1749 dev->name, ifsta->auth_alg);
1750 break;
1751 }
1752 }
1753 return;
1754 }
1755
1756 switch (ifsta->auth_alg) {
1757 case WLAN_AUTH_OPEN:
1758 case WLAN_AUTH_LEAP:
1759 ieee80211_auth_completed(dev, ifsta);
1760 break;
1761 case WLAN_AUTH_SHARED_KEY:
1762 if (ifsta->auth_transaction == 4)
1763 ieee80211_auth_completed(dev, ifsta);
1764 else
1765 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1766 break;
1767 }
1768}
1769
1770
1771static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1772 struct ieee80211_if_sta *ifsta,
1773 struct ieee80211_mgmt *mgmt,
1774 size_t len)
1775{
1776 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001777 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001778
1779 if (len < 24 + 2) {
1780 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001781 "received from %s - ignored\n",
1782 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001783 return;
1784 }
1785
1786 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1787 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001788 "unknown AP (SA=%s BSSID=%s) - "
1789 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1790 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001791 return;
1792 }
1793
1794 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1795
Joe Perches0795af52007-10-03 17:59:30 -07001796 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001797 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001798 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001799
Johannes Berg988c0f72008-04-17 19:21:22 +02001800 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001801 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001802
1803 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1804 ifsta->state == IEEE80211_ASSOCIATE ||
1805 ifsta->state == IEEE80211_ASSOCIATED) {
1806 ifsta->state = IEEE80211_AUTHENTICATE;
1807 mod_timer(&ifsta->timer, jiffies +
1808 IEEE80211_RETRY_AUTH_INTERVAL);
1809 }
1810
1811 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001812 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001813}
1814
1815
1816static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1817 struct ieee80211_if_sta *ifsta,
1818 struct ieee80211_mgmt *mgmt,
1819 size_t len)
1820{
1821 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001822 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001823
1824 if (len < 24 + 2) {
1825 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001826 "received from %s - ignored\n",
1827 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001828 return;
1829 }
1830
1831 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1832 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001833 "unknown AP (SA=%s BSSID=%s) - "
1834 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1835 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001836 return;
1837 }
1838
1839 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1840
Joe Perches0795af52007-10-03 17:59:30 -07001841 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001842 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001843 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001844
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001845 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001846 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1847
1848 if (ifsta->state == IEEE80211_ASSOCIATED) {
1849 ifsta->state = IEEE80211_ASSOCIATE;
1850 mod_timer(&ifsta->timer, jiffies +
1851 IEEE80211_RETRY_AUTH_INTERVAL);
1852 }
1853
1854 ieee80211_set_disassoc(dev, ifsta, 0);
1855}
1856
1857
Johannes Berg471b3ef2007-12-28 14:32:58 +01001858static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001859 struct ieee80211_if_sta *ifsta,
1860 struct ieee80211_mgmt *mgmt,
1861 size_t len,
1862 int reassoc)
1863{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001864 struct ieee80211_local *local = sdata->local;
1865 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01001866 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001867 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001868 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001869 u16 capab_info, status_code, aid;
1870 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001871 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001872 u8 *pos;
1873 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001874 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001875 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001876
1877 /* AssocResp and ReassocResp have identical structure, so process both
1878 * of them in this function. */
1879
1880 if (ifsta->state != IEEE80211_ASSOCIATE) {
1881 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001882 "%s, but not in associate state - ignored\n",
1883 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001884 return;
1885 }
1886
1887 if (len < 24 + 6) {
1888 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001889 "received from %s - ignored\n",
1890 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001891 return;
1892 }
1893
1894 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1895 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001896 "unknown AP (SA=%s BSSID=%s) - "
1897 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1898 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001899 return;
1900 }
1901
1902 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1903 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1904 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001905
Joe Perches0795af52007-10-03 17:59:30 -07001906 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001907 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001908 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001909 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001910
1911 if (status_code != WLAN_STATUS_SUCCESS) {
1912 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1913 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001914 /* if this was a reassociation, ensure we try a "full"
1915 * association next time. This works around some broken APs
1916 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001917 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07001918 return;
1919 }
1920
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001921 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1922 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1923 "set\n", dev->name, aid);
1924 aid &= ~(BIT(15) | BIT(14));
1925
Jiri Bencf0706e82007-05-05 11:45:53 -07001926 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001927 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001928
1929 if (!elems.supp_rates) {
1930 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1931 dev->name);
1932 return;
1933 }
1934
1935 printk(KERN_DEBUG "%s: associated\n", dev->name);
1936 ifsta->aid = aid;
1937 ifsta->ap_capab = capab_info;
1938
1939 kfree(ifsta->assocresp_ies);
1940 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001941 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001942 if (ifsta->assocresp_ies)
1943 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1944
Johannes Bergd0709a62008-02-25 16:27:46 +01001945 rcu_read_lock();
1946
Jiri Bencf0706e82007-05-05 11:45:53 -07001947 /* Add STA entry for the AP */
1948 sta = sta_info_get(local, ifsta->bssid);
1949 if (!sta) {
1950 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01001951 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01001952
Johannes Berg73651ee2008-02-25 16:27:47 +01001953 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1954 if (!sta) {
1955 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1956 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01001957 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001958 return;
1959 }
John W. Linville65c107a2007-10-05 14:23:27 -04001960 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001961 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001962 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001963 if (bss) {
1964 sta->last_rssi = bss->rssi;
1965 sta->last_signal = bss->signal;
1966 sta->last_noise = bss->noise;
1967 ieee80211_rx_bss_put(dev, bss);
1968 }
Johannes Berg73651ee2008-02-25 16:27:47 +01001969
1970 err = sta_info_insert(sta);
1971 if (err) {
1972 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1973 " the AP (error %d)\n", dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01001974 rcu_read_unlock();
1975 return;
1976 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001977 }
1978
Johannes Berg73651ee2008-02-25 16:27:47 +01001979 /*
1980 * FIXME: Do we really need to update the sta_info's information here?
1981 * We already know about the AP (we found it in our list) so it
1982 * should already be filled with the right info, no?
1983 * As is stands, all this is racy because typically we assume
1984 * the information that is filled in here (except flags) doesn't
1985 * change while a STA structure is alive. As such, it should move
1986 * to between the sta_info_alloc() and sta_info_insert() above.
1987 */
1988
Johannes Berg238814f2008-01-28 17:19:37 +01001989 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1990 WLAN_STA_AUTHORIZED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001991
1992 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001993 basic_rates = 0;
1994 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1995
Jiri Bencf0706e82007-05-05 11:45:53 -07001996 for (i = 0; i < elems.supp_rates_len; i++) {
1997 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001998
1999 if (rate > 110)
2000 have_higher_than_11mbit = true;
2001
2002 for (j = 0; j < sband->n_bitrates; j++) {
2003 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002004 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002005 if (elems.supp_rates[i] & 0x80)
2006 basic_rates |= BIT(j);
2007 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002008 }
Johannes Berg8318d782008-01-24 19:38:38 +01002009
Jiri Bencf0706e82007-05-05 11:45:53 -07002010 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2011 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002012
2013 if (rate > 110)
2014 have_higher_than_11mbit = true;
2015
2016 for (j = 0; j < sband->n_bitrates; j++) {
2017 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002018 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002019 if (elems.ext_supp_rates[i] & 0x80)
2020 basic_rates |= BIT(j);
2021 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002022 }
Johannes Berg8318d782008-01-24 19:38:38 +01002023
2024 sta->supp_rates[local->hw.conf.channel->band] = rates;
2025 sdata->basic_rates = basic_rates;
2026
2027 /* cf. IEEE 802.11 9.2.12 */
2028 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2029 have_higher_than_11mbit)
2030 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2031 else
2032 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002033
Tomas Winkler38668c02008-03-28 16:33:32 -07002034 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002035 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002036 ieee80211_ht_cap_ie_to_ht_info(
2037 (struct ieee80211_ht_cap *)
2038 elems.ht_cap_elem, &sta->ht_info);
2039 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2040 (struct ieee80211_ht_addt_info *)
2041 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002042 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002043 }
2044
Jiri Bencf0706e82007-05-05 11:45:53 -07002045 rate_control_rate_init(sta, local);
2046
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002047 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002048 sta->flags |= WLAN_STA_WME;
Johannes Berge5f98f22008-03-05 20:39:31 +01002049 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002050 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2051 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002052 } else
2053 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002054
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002055 /* set AID and assoc capability,
2056 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002057 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002058 bss_conf->assoc_capability = capab_info;
Johannes Berg8318d782008-01-24 19:38:38 +01002059 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002060
Jiri Bencf0706e82007-05-05 11:45:53 -07002061 ieee80211_associated(dev, ifsta);
2062}
2063
2064
2065/* Caller must hold local->sta_bss_lock */
2066static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2067 struct ieee80211_sta_bss *bss)
2068{
2069 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002070 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002071
2072 if (bss_mesh_cfg(bss))
2073 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2074 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002075 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002076 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002077
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002078 bss->hnext = local->sta_bss_hash[hash_idx];
2079 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002080}
2081
2082
2083/* Caller must hold local->sta_bss_lock */
2084static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2085 struct ieee80211_sta_bss *bss)
2086{
2087 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2088 struct ieee80211_sta_bss *b, *prev = NULL;
2089 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2090 while (b) {
2091 if (b == bss) {
2092 if (!prev)
2093 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2094 bss->hnext;
2095 else
2096 prev->hnext = bss->hnext;
2097 break;
2098 }
2099 prev = b;
2100 b = b->hnext;
2101 }
2102}
2103
2104
2105static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002106ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002107 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002108{
2109 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2110 struct ieee80211_sta_bss *bss;
2111
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002112 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002113 if (!bss)
2114 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002115 atomic_inc(&bss->users);
2116 atomic_inc(&bss->users);
2117 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002118 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002119 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2120 memcpy(bss->ssid, ssid, ssid_len);
2121 bss->ssid_len = ssid_len;
2122 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002123
2124 spin_lock_bh(&local->sta_bss_lock);
2125 /* TODO: order by RSSI? */
2126 list_add_tail(&bss->list, &local->sta_bss_list);
2127 __ieee80211_rx_bss_hash_add(dev, bss);
2128 spin_unlock_bh(&local->sta_bss_lock);
2129 return bss;
2130}
2131
Jiri Bencf0706e82007-05-05 11:45:53 -07002132static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002133ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002134 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002135{
2136 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2137 struct ieee80211_sta_bss *bss;
2138
2139 spin_lock_bh(&local->sta_bss_lock);
2140 bss = local->sta_bss_hash[STA_HASH(bssid)];
2141 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002142 if (!bss_mesh_cfg(bss) &&
2143 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002144 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002145 bss->ssid_len == ssid_len &&
2146 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002147 atomic_inc(&bss->users);
2148 break;
2149 }
2150 bss = bss->hnext;
2151 }
2152 spin_unlock_bh(&local->sta_bss_lock);
2153 return bss;
2154}
2155
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002156#ifdef CONFIG_MAC80211_MESH
2157static struct ieee80211_sta_bss *
2158ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2159 u8 *mesh_cfg, int freq)
2160{
2161 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2162 struct ieee80211_sta_bss *bss;
2163
2164 spin_lock_bh(&local->sta_bss_lock);
2165 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2166 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002167 if (bss_mesh_cfg(bss) &&
2168 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002169 bss->freq == freq &&
2170 mesh_id_len == bss->mesh_id_len &&
2171 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2172 mesh_id_len))) {
2173 atomic_inc(&bss->users);
2174 break;
2175 }
2176 bss = bss->hnext;
2177 }
2178 spin_unlock_bh(&local->sta_bss_lock);
2179 return bss;
2180}
2181
2182static struct ieee80211_sta_bss *
2183ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002184 u8 *mesh_cfg, int mesh_config_len, int freq)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002185{
2186 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2187 struct ieee80211_sta_bss *bss;
2188
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002189 if (mesh_config_len != MESH_CFG_LEN)
2190 return NULL;
2191
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002192 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2193 if (!bss)
2194 return NULL;
2195
John W. Linville24736702008-04-08 14:15:46 -04002196 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002197 if (!bss->mesh_cfg) {
2198 kfree(bss);
2199 return NULL;
2200 }
2201
2202 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2203 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2204 if (!bss->mesh_id) {
2205 kfree(bss->mesh_cfg);
2206 kfree(bss);
2207 return NULL;
2208 }
2209 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2210 }
2211
2212 atomic_inc(&bss->users);
2213 atomic_inc(&bss->users);
John W. Linville24736702008-04-08 14:15:46 -04002214 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002215 bss->mesh_id_len = mesh_id_len;
2216 bss->freq = freq;
2217 spin_lock_bh(&local->sta_bss_lock);
2218 /* TODO: order by RSSI? */
2219 list_add_tail(&bss->list, &local->sta_bss_list);
2220 __ieee80211_rx_bss_hash_add(dev, bss);
2221 spin_unlock_bh(&local->sta_bss_lock);
2222 return bss;
2223}
2224#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002225
2226static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2227{
2228 kfree(bss->wpa_ie);
2229 kfree(bss->rsn_ie);
2230 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002231 kfree(bss->ht_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002232 kfree(bss_mesh_id(bss));
2233 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002234 kfree(bss);
2235}
2236
2237
2238static void ieee80211_rx_bss_put(struct net_device *dev,
2239 struct ieee80211_sta_bss *bss)
2240{
2241 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -07002242
Pavel Emelyanov1ebebea2008-04-23 11:47:15 +04002243 local_bh_disable();
2244 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2245 local_bh_enable();
2246 return;
2247 }
2248
Jiri Bencf0706e82007-05-05 11:45:53 -07002249 __ieee80211_rx_bss_hash_del(dev, bss);
2250 list_del(&bss->list);
2251 spin_unlock_bh(&local->sta_bss_lock);
2252 ieee80211_rx_bss_free(bss);
2253}
2254
2255
2256void ieee80211_rx_bss_list_init(struct net_device *dev)
2257{
2258 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2259 spin_lock_init(&local->sta_bss_lock);
2260 INIT_LIST_HEAD(&local->sta_bss_list);
2261}
2262
2263
2264void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2265{
2266 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2267 struct ieee80211_sta_bss *bss, *tmp;
2268
2269 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2270 ieee80211_rx_bss_put(dev, bss);
2271}
2272
2273
Bruno Randolfa6072682008-02-18 11:21:15 +09002274static int ieee80211_sta_join_ibss(struct net_device *dev,
2275 struct ieee80211_if_sta *ifsta,
2276 struct ieee80211_sta_bss *bss)
2277{
2278 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2279 int res, rates, i, j;
2280 struct sk_buff *skb;
2281 struct ieee80211_mgmt *mgmt;
2282 struct ieee80211_tx_control control;
2283 struct rate_selection ratesel;
2284 u8 *pos;
2285 struct ieee80211_sub_if_data *sdata;
2286 struct ieee80211_supported_band *sband;
2287
2288 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2289
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002290 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2291
Bruno Randolfa6072682008-02-18 11:21:15 +09002292 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002293 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002294
2295 if (local->ops->reset_tsf) {
2296 /* Reset own TSF to allow time synchronization work. */
2297 local->ops->reset_tsf(local_to_hw(local));
2298 }
2299 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2300 res = ieee80211_if_config(dev);
2301 if (res)
2302 return res;
2303
2304 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2305
Bruno Randolfa6072682008-02-18 11:21:15 +09002306 sdata->drop_unencrypted = bss->capability &
2307 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2308
2309 res = ieee80211_set_freq(local, bss->freq);
2310
2311 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2312 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2313 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2314 return -1;
2315 }
2316
2317 /* Set beacon template */
2318 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2319 do {
2320 if (!skb)
2321 break;
2322
2323 skb_reserve(skb, local->hw.extra_tx_headroom);
2324
2325 mgmt = (struct ieee80211_mgmt *)
2326 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2327 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2328 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2329 IEEE80211_STYPE_BEACON);
2330 memset(mgmt->da, 0xff, ETH_ALEN);
2331 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2332 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2333 mgmt->u.beacon.beacon_int =
2334 cpu_to_le16(local->hw.conf.beacon_int);
2335 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2336
2337 pos = skb_put(skb, 2 + ifsta->ssid_len);
2338 *pos++ = WLAN_EID_SSID;
2339 *pos++ = ifsta->ssid_len;
2340 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2341
2342 rates = bss->supp_rates_len;
2343 if (rates > 8)
2344 rates = 8;
2345 pos = skb_put(skb, 2 + rates);
2346 *pos++ = WLAN_EID_SUPP_RATES;
2347 *pos++ = rates;
2348 memcpy(pos, bss->supp_rates, rates);
2349
2350 if (bss->band == IEEE80211_BAND_2GHZ) {
2351 pos = skb_put(skb, 2 + 1);
2352 *pos++ = WLAN_EID_DS_PARAMS;
2353 *pos++ = 1;
2354 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2355 }
2356
2357 pos = skb_put(skb, 2 + 2);
2358 *pos++ = WLAN_EID_IBSS_PARAMS;
2359 *pos++ = 2;
2360 /* FIX: set ATIM window based on scan results */
2361 *pos++ = 0;
2362 *pos++ = 0;
2363
2364 if (bss->supp_rates_len > 8) {
2365 rates = bss->supp_rates_len - 8;
2366 pos = skb_put(skb, 2 + rates);
2367 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2368 *pos++ = rates;
2369 memcpy(pos, &bss->supp_rates[8], rates);
2370 }
2371
2372 memset(&control, 0, sizeof(control));
2373 rate_control_get_rate(dev, sband, skb, &ratesel);
2374 if (!ratesel.rate) {
2375 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2376 "for IBSS beacon\n", dev->name);
2377 break;
2378 }
2379 control.vif = &sdata->vif;
2380 control.tx_rate = ratesel.rate;
2381 if (sdata->bss_conf.use_short_preamble &&
2382 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2383 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2384 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2385 control.flags |= IEEE80211_TXCTL_NO_ACK;
2386 control.retry_limit = 1;
2387
2388 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2389 if (ifsta->probe_resp) {
2390 mgmt = (struct ieee80211_mgmt *)
2391 ifsta->probe_resp->data;
2392 mgmt->frame_control =
2393 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2394 IEEE80211_STYPE_PROBE_RESP);
2395 } else {
2396 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2397 "template for IBSS\n", dev->name);
2398 }
2399
2400 if (local->ops->beacon_update &&
2401 local->ops->beacon_update(local_to_hw(local),
2402 skb, &control) == 0) {
2403 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2404 "template\n", dev->name);
2405 skb = NULL;
2406 }
2407
2408 rates = 0;
2409 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2410 for (i = 0; i < bss->supp_rates_len; i++) {
2411 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2412 for (j = 0; j < sband->n_bitrates; j++)
2413 if (sband->bitrates[j].bitrate == bitrate)
2414 rates |= BIT(j);
2415 }
2416 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Vladimir Koutnye2839d82008-03-18 21:14:07 +01002417
2418 ieee80211_sta_def_wmm_params(dev, bss, 1);
Bruno Randolfa6072682008-02-18 11:21:15 +09002419 } while (0);
2420
2421 if (skb) {
2422 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2423 "template\n", dev->name);
2424 dev_kfree_skb(skb);
2425 }
2426
2427 ifsta->state = IEEE80211_IBSS_JOINED;
2428 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2429
2430 ieee80211_rx_bss_put(dev, bss);
2431
2432 return res;
2433}
2434
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002435u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2436 struct ieee802_11_elems *elems,
2437 enum ieee80211_band band)
2438{
2439 struct ieee80211_supported_band *sband;
2440 struct ieee80211_rate *bitrates;
2441 size_t num_rates;
2442 u64 supp_rates;
2443 int i, j;
2444 sband = local->hw.wiphy->bands[band];
2445
2446 if (!sband) {
2447 WARN_ON(1);
2448 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2449 }
2450
2451 bitrates = sband->bitrates;
2452 num_rates = sband->n_bitrates;
2453 supp_rates = 0;
2454 for (i = 0; i < elems->supp_rates_len +
2455 elems->ext_supp_rates_len; i++) {
2456 u8 rate = 0;
2457 int own_rate;
2458 if (i < elems->supp_rates_len)
2459 rate = elems->supp_rates[i];
2460 else if (elems->ext_supp_rates)
2461 rate = elems->ext_supp_rates
2462 [i - elems->supp_rates_len];
2463 own_rate = 5 * (rate & 0x7f);
2464 for (j = 0; j < num_rates; j++)
2465 if (bitrates[j].bitrate == own_rate)
2466 supp_rates |= BIT(j);
2467 }
2468 return supp_rates;
2469}
2470
Bruno Randolfa6072682008-02-18 11:21:15 +09002471
Jiri Bencf0706e82007-05-05 11:45:53 -07002472static void ieee80211_rx_bss_info(struct net_device *dev,
2473 struct ieee80211_mgmt *mgmt,
2474 size_t len,
2475 struct ieee80211_rx_status *rx_status,
2476 int beacon)
2477{
2478 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2479 struct ieee802_11_elems elems;
2480 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002481 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002482 struct ieee80211_sta_bss *bss;
2483 struct sta_info *sta;
2484 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002485 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002486 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002487 DECLARE_MAC_BUF(mac);
2488 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002489
2490 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2491 return; /* ignore ProbeResp to foreign address */
2492
2493#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002494 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07002495 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002496 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002497#endif
2498
2499 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2500 if (baselen > len)
2501 return;
2502
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002503 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
John W. Linville67a4cce2007-10-12 16:40:37 -04002504 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002505
Johannes Berg902acc72008-02-23 15:17:19 +01002506 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2507 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2508 u64 rates = ieee80211_sta_get_rates(local, &elems,
2509 rx_status->band);
2510
2511 mesh_neighbour_update(mgmt->sa, rates, dev,
2512 mesh_peer_accepts_plinks(&elems, dev));
2513 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002514
Johannes Bergd0709a62008-02-25 16:27:46 +01002515 rcu_read_lock();
2516
Johannes Berg51fb61e2007-12-19 01:31:27 +01002517 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002518 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2519 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002520 u64 prev_rates;
2521 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2522 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002523
Johannes Berg8318d782008-01-24 19:38:38 +01002524 prev_rates = sta->supp_rates[rx_status->band];
2525 sta->supp_rates[rx_status->band] &= supp_rates;
2526 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002527 /* No matching rates - this should not really happen.
2528 * Make sure that at least one rate is marked
2529 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002530 sta->supp_rates[rx_status->band] =
2531 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002532 }
Johannes Berg8318d782008-01-24 19:38:38 +01002533 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002534 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002535 "%s based on beacon info (0x%llx & 0x%llx -> "
2536 "0x%llx)\n",
2537 dev->name, print_mac(mac, sta->addr),
2538 (unsigned long long) prev_rates,
2539 (unsigned long long) supp_rates,
2540 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002541 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002542 }
2543
Johannes Bergd0709a62008-02-25 16:27:46 +01002544 rcu_read_unlock();
2545
Jiri Bencf0706e82007-05-05 11:45:53 -07002546 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002547 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002548 else
Johannes Berg8318d782008-01-24 19:38:38 +01002549 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002550
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002551 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2552
2553 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2554 return;
2555
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002556#ifdef CONFIG_MAC80211_MESH
2557 if (elems.mesh_config)
2558 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2559 elems.mesh_id_len, elems.mesh_config, freq);
2560 else
2561#endif
2562 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002563 elems.ssid, elems.ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002564 if (!bss) {
2565#ifdef CONFIG_MAC80211_MESH
2566 if (elems.mesh_config)
2567 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002568 elems.mesh_id_len, elems.mesh_config,
2569 elems.mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002570 else
2571#endif
2572 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2573 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002574 if (!bss)
2575 return;
2576 } else {
2577#if 0
2578 /* TODO: order by RSSI? */
2579 spin_lock_bh(&local->sta_bss_lock);
2580 list_move_tail(&bss->list, &local->sta_bss_list);
2581 spin_unlock_bh(&local->sta_bss_lock);
2582#endif
2583 }
2584
Daniel Drake56282212007-07-10 19:32:10 +02002585 /* save the ERP value so that it is available at association time */
2586 if (elems.erp_info && elems.erp_info_len >= 1) {
2587 bss->erp_value = elems.erp_info[0];
2588 bss->has_erp_value = 1;
2589 }
2590
Johannes Berg30b89b02008-04-16 17:43:20 +02002591 if (elems.ht_cap_elem &&
2592 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2593 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2594 kfree(bss->ht_ie);
2595 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2596 if (bss->ht_ie) {
2597 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2598 elems.ht_cap_elem_len + 2);
2599 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2600 } else
2601 bss->ht_ie_len = 0;
2602 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2603 kfree(bss->ht_ie);
2604 bss->ht_ie = NULL;
2605 bss->ht_ie_len = 0;
2606 }
2607
Jiri Bencf0706e82007-05-05 11:45:53 -07002608 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2609 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002610
2611 bss->supp_rates_len = 0;
2612 if (elems.supp_rates) {
2613 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2614 if (clen > elems.supp_rates_len)
2615 clen = elems.supp_rates_len;
2616 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2617 clen);
2618 bss->supp_rates_len += clen;
2619 }
2620 if (elems.ext_supp_rates) {
2621 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2622 if (clen > elems.ext_supp_rates_len)
2623 clen = elems.ext_supp_rates_len;
2624 memcpy(&bss->supp_rates[bss->supp_rates_len],
2625 elems.ext_supp_rates, clen);
2626 bss->supp_rates_len += clen;
2627 }
2628
Johannes Berg30b89b02008-04-16 17:43:20 +02002629 bss->band = rx_status->band;
2630
2631 bss->timestamp = beacon_timestamp;
2632 bss->last_update = jiffies;
2633 bss->rssi = rx_status->ssi;
2634 bss->signal = rx_status->signal;
2635 bss->noise = rx_status->noise;
2636 if (!beacon && !bss->probe_resp)
2637 bss->probe_resp = true;
2638
2639 /*
2640 * In STA mode, the remaining parameters should not be overridden
2641 * by beacons because they're not necessarily accurate there.
2642 */
2643 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2644 bss->probe_resp && beacon) {
2645 ieee80211_rx_bss_put(dev, bss);
2646 return;
2647 }
2648
Jiri Bencf0706e82007-05-05 11:45:53 -07002649 if (elems.wpa &&
2650 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2651 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2652 kfree(bss->wpa_ie);
2653 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2654 if (bss->wpa_ie) {
2655 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2656 bss->wpa_ie_len = elems.wpa_len + 2;
2657 } else
2658 bss->wpa_ie_len = 0;
2659 } else if (!elems.wpa && bss->wpa_ie) {
2660 kfree(bss->wpa_ie);
2661 bss->wpa_ie = NULL;
2662 bss->wpa_ie_len = 0;
2663 }
2664
2665 if (elems.rsn &&
2666 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2667 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2668 kfree(bss->rsn_ie);
2669 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2670 if (bss->rsn_ie) {
2671 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2672 bss->rsn_ie_len = elems.rsn_len + 2;
2673 } else
2674 bss->rsn_ie_len = 0;
2675 } else if (!elems.rsn && bss->rsn_ie) {
2676 kfree(bss->rsn_ie);
2677 bss->rsn_ie = NULL;
2678 bss->rsn_ie_len = 0;
2679 }
2680
Johannes Berg30b89b02008-04-16 17:43:20 +02002681 /*
2682 * Cf.
2683 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2684 *
2685 * quoting:
2686 *
2687 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2688 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2689 * Alliance (September 1, 2004) is incorporated by reference herein.
2690 * The inclusion of the WMM Parameters in probe responses and
2691 * association responses is mandatory for WMM enabled networks. The
2692 * inclusion of the WMM Parameters in beacons, however, is optional.
2693 */
2694
Jiri Bencf0706e82007-05-05 11:45:53 -07002695 if (elems.wmm_param &&
2696 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2697 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2698 kfree(bss->wmm_ie);
2699 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2700 if (bss->wmm_ie) {
2701 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2702 elems.wmm_param_len + 2);
2703 bss->wmm_ie_len = elems.wmm_param_len + 2;
2704 } else
2705 bss->wmm_ie_len = 0;
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002706 } else if (elems.wmm_info &&
2707 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_info_len ||
2708 memcmp(bss->wmm_ie, elems.wmm_info, elems.wmm_info_len))) {
2709 /* As for certain AP's Fifth bit is not set in WMM IE in
2710 * beacon frames.So while parsing the beacon frame the
2711 * wmm_info structure is used instead of wmm_param.
2712 * wmm_info structure was never used to set bss->wmm_ie.
2713 * This code fixes this problem by copying the WME
2714 * information from wmm_info to bss->wmm_ie and enabling
2715 * n-band association.
2716 */
2717 kfree(bss->wmm_ie);
2718 bss->wmm_ie = kmalloc(elems.wmm_info_len + 2, GFP_ATOMIC);
2719 if (bss->wmm_ie) {
2720 memcpy(bss->wmm_ie, elems.wmm_info - 2,
2721 elems.wmm_info_len + 2);
2722 bss->wmm_ie_len = elems.wmm_info_len + 2;
2723 } else
2724 bss->wmm_ie_len = 0;
2725 } else if (!elems.wmm_param && !elems.wmm_info && bss->wmm_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002726 kfree(bss->wmm_ie);
2727 bss->wmm_ie = NULL;
2728 bss->wmm_ie_len = 0;
2729 }
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002730
2731 /* check if we need to merge IBSS */
2732 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2733 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002734 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002735 bss->freq == local->oper_channel->center_freq &&
2736 elems.ssid_len == sdata->u.sta.ssid_len &&
2737 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2738 if (rx_status->flag & RX_FLAG_TSFT) {
2739 /* in order for correct IBSS merging we need mactime
2740 *
2741 * since mactime is defined as the time the first data
2742 * symbol of the frame hits the PHY, and the timestamp
2743 * of the beacon is defined as "the time that the data
2744 * symbol containing the first bit of the timestamp is
2745 * transmitted to the PHY plus the transmitting STA’s
2746 * delays through its local PHY from the MAC-PHY
2747 * interface to its interface with the WM"
2748 * (802.11 11.1.2) - equals the time this bit arrives at
2749 * the receiver - we have to take into account the
2750 * offset between the two.
2751 * e.g: at 1 MBit that means mactime is 192 usec earlier
2752 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2753 */
2754 int rate = local->hw.wiphy->bands[rx_status->band]->
2755 bitrates[rx_status->rate_idx].bitrate;
2756 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2757 } else if (local && local->ops && local->ops->get_tsf)
2758 /* second best option: get current TSF */
2759 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2760 else
2761 /* can't merge without knowing the TSF */
2762 rx_timestamp = -1LLU;
2763#ifdef CONFIG_MAC80211_IBSS_DEBUG
2764 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2765 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2766 print_mac(mac, mgmt->sa),
2767 print_mac(mac2, mgmt->bssid),
2768 (unsigned long long)rx_timestamp,
2769 (unsigned long long)beacon_timestamp,
2770 (unsigned long long)(rx_timestamp - beacon_timestamp),
2771 jiffies);
2772#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2773 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002774#ifndef CONFIG_MAC80211_IBSS_DEBUG
Pavel Roskind97cf012008-02-21 11:33:58 -05002775 if (net_ratelimit())
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002776#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002777 printk(KERN_DEBUG "%s: beacon TSF higher than "
2778 "local TSF - IBSS merge with BSSID %s\n",
2779 dev->name, print_mac(mac, mgmt->bssid));
2780 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2781 ieee80211_ibss_add_sta(dev, NULL,
2782 mgmt->bssid, mgmt->sa);
2783 }
2784 }
2785
Jiri Bencf0706e82007-05-05 11:45:53 -07002786 ieee80211_rx_bss_put(dev, bss);
2787}
2788
2789
2790static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2791 struct ieee80211_mgmt *mgmt,
2792 size_t len,
2793 struct ieee80211_rx_status *rx_status)
2794{
2795 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2796}
2797
2798
2799static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2800 struct ieee80211_mgmt *mgmt,
2801 size_t len,
2802 struct ieee80211_rx_status *rx_status)
2803{
Jiri Bencf0706e82007-05-05 11:45:53 -07002804 struct ieee80211_sub_if_data *sdata;
2805 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002806 size_t baselen;
2807 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002808 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2809 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002810 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002811
2812 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2813
2814 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002815 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002816 return;
2817 ifsta = &sdata->u.sta;
2818
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002819 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002820 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2821 return;
2822
2823 /* Process beacon from the current BSS */
2824 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2825 if (baselen > len)
2826 return;
2827
John W. Linville67a4cce2007-10-12 16:40:37 -04002828 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002829
Reinette Chatred18ef292008-04-09 16:56:15 -07002830 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
2831 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2832 elems.wmm_param_len);
2833 }
2834
2835 /* Do not send changes to driver if we are scanning. This removes
2836 * requirement that driver's bss_info_changed function needs to be
2837 * atomic. */
2838 if (local->sta_sw_scanning || local->sta_hw_scanning)
2839 return;
2840
Daniel Drake56282212007-07-10 19:32:10 +02002841 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002842 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
John W. Linville50c4afb2008-04-15 14:09:27 -04002843 else {
2844 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2845 changed |= ieee80211_handle_protect_preamb(sdata, false,
2846 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2847 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002848
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002849 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07002850 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002851 struct ieee80211_ht_bss_info bss_info;
2852
2853 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2854 (struct ieee80211_ht_addt_info *)
2855 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002856 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2857 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002858 }
2859
Johannes Berg471b3ef2007-12-28 14:32:58 +01002860 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002861}
2862
2863
2864static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2865 struct ieee80211_if_sta *ifsta,
2866 struct ieee80211_mgmt *mgmt,
2867 size_t len,
2868 struct ieee80211_rx_status *rx_status)
2869{
2870 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2871 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2872 int tx_last_beacon;
2873 struct sk_buff *skb;
2874 struct ieee80211_mgmt *resp;
2875 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002876 DECLARE_MAC_BUF(mac);
2877#ifdef CONFIG_MAC80211_IBSS_DEBUG
2878 DECLARE_MAC_BUF(mac2);
2879 DECLARE_MAC_BUF(mac3);
2880#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002881
Johannes Berg51fb61e2007-12-19 01:31:27 +01002882 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002883 ifsta->state != IEEE80211_IBSS_JOINED ||
2884 len < 24 + 2 || !ifsta->probe_resp)
2885 return;
2886
2887 if (local->ops->tx_last_beacon)
2888 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2889 else
2890 tx_last_beacon = 1;
2891
2892#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002893 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2894 "%s (tx_last_beacon=%d)\n",
2895 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2896 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002897#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2898
2899 if (!tx_last_beacon)
2900 return;
2901
2902 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2903 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2904 return;
2905
2906 end = ((u8 *) mgmt) + len;
2907 pos = mgmt->u.probe_req.variable;
2908 if (pos[0] != WLAN_EID_SSID ||
2909 pos + 2 + pos[1] > end) {
2910 if (net_ratelimit()) {
2911 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002912 "from %s\n",
2913 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002914 }
2915 return;
2916 }
2917 if (pos[1] != 0 &&
2918 (pos[1] != ifsta->ssid_len ||
2919 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2920 /* Ignore ProbeReq for foreign SSID */
2921 return;
2922 }
2923
2924 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002925 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002926 if (!skb)
2927 return;
2928
2929 resp = (struct ieee80211_mgmt *) skb->data;
2930 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2931#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002932 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2933 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002934#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2935 ieee80211_sta_tx(dev, skb, 0);
2936}
2937
Johannes Berg4e20cb22007-12-19 01:31:24 +01002938static void ieee80211_rx_mgmt_action(struct net_device *dev,
2939 struct ieee80211_if_sta *ifsta,
2940 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002941 size_t len,
2942 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002943{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002944 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002945
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002946 if (len < IEEE80211_MIN_ACTION_SIZE)
2947 return;
2948
2949 switch (mgmt->u.action.category) {
2950 case WLAN_CATEGORY_BACK:
2951 switch (mgmt->u.action.u.addba_req.action_code) {
2952 case WLAN_ACTION_ADDBA_REQ:
2953 if (len < (IEEE80211_MIN_ACTION_SIZE +
2954 sizeof(mgmt->u.action.u.addba_req)))
2955 break;
2956 ieee80211_sta_process_addba_request(dev, mgmt, len);
2957 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002958 case WLAN_ACTION_ADDBA_RESP:
2959 if (len < (IEEE80211_MIN_ACTION_SIZE +
2960 sizeof(mgmt->u.action.u.addba_resp)))
2961 break;
2962 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2963 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002964 case WLAN_ACTION_DELBA:
2965 if (len < (IEEE80211_MIN_ACTION_SIZE +
2966 sizeof(mgmt->u.action.u.delba)))
2967 break;
2968 ieee80211_sta_process_delba(dev, mgmt, len);
2969 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002970 default:
2971 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002972 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002973 dev->name);
2974 break;
2975 }
2976 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002977 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002978 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002979 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
2980 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002981 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002982 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002983 mesh_rx_path_sel_frame(dev, mgmt, len);
2984 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002985 default:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002986 if (net_ratelimit())
2987 printk(KERN_DEBUG "%s: Rx unknown action frame - "
2988 "category=%d\n", dev->name, mgmt->u.action.category);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002989 break;
2990 }
2991}
Jiri Bencf0706e82007-05-05 11:45:53 -07002992
2993void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2994 struct ieee80211_rx_status *rx_status)
2995{
2996 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2997 struct ieee80211_sub_if_data *sdata;
2998 struct ieee80211_if_sta *ifsta;
2999 struct ieee80211_mgmt *mgmt;
3000 u16 fc;
3001
3002 if (skb->len < 24)
3003 goto fail;
3004
3005 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3006 ifsta = &sdata->u.sta;
3007
3008 mgmt = (struct ieee80211_mgmt *) skb->data;
3009 fc = le16_to_cpu(mgmt->frame_control);
3010
3011 switch (fc & IEEE80211_FCTL_STYPE) {
3012 case IEEE80211_STYPE_PROBE_REQ:
3013 case IEEE80211_STYPE_PROBE_RESP:
3014 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003015 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07003016 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3017 case IEEE80211_STYPE_AUTH:
3018 case IEEE80211_STYPE_ASSOC_RESP:
3019 case IEEE80211_STYPE_REASSOC_RESP:
3020 case IEEE80211_STYPE_DEAUTH:
3021 case IEEE80211_STYPE_DISASSOC:
3022 skb_queue_tail(&ifsta->skb_queue, skb);
3023 queue_work(local->hw.workqueue, &ifsta->work);
3024 return;
3025 default:
3026 printk(KERN_DEBUG "%s: received unknown management frame - "
3027 "stype=%d\n", dev->name,
3028 (fc & IEEE80211_FCTL_STYPE) >> 4);
3029 break;
3030 }
3031
3032 fail:
3033 kfree_skb(skb);
3034}
3035
3036
3037static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3038 struct sk_buff *skb)
3039{
3040 struct ieee80211_rx_status *rx_status;
3041 struct ieee80211_sub_if_data *sdata;
3042 struct ieee80211_if_sta *ifsta;
3043 struct ieee80211_mgmt *mgmt;
3044 u16 fc;
3045
3046 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3047 ifsta = &sdata->u.sta;
3048
3049 rx_status = (struct ieee80211_rx_status *) skb->cb;
3050 mgmt = (struct ieee80211_mgmt *) skb->data;
3051 fc = le16_to_cpu(mgmt->frame_control);
3052
3053 switch (fc & IEEE80211_FCTL_STYPE) {
3054 case IEEE80211_STYPE_PROBE_REQ:
3055 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3056 rx_status);
3057 break;
3058 case IEEE80211_STYPE_PROBE_RESP:
3059 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3060 break;
3061 case IEEE80211_STYPE_BEACON:
3062 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3063 break;
3064 case IEEE80211_STYPE_AUTH:
3065 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3066 break;
3067 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003068 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003069 break;
3070 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003071 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003072 break;
3073 case IEEE80211_STYPE_DEAUTH:
3074 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3075 break;
3076 case IEEE80211_STYPE_DISASSOC:
3077 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3078 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003079 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003080 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003081 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07003082 }
3083
3084 kfree_skb(skb);
3085}
3086
3087
Johannes Berg9ae54c82008-01-31 19:48:20 +01003088ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003089ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3090 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003091{
3092 struct ieee80211_mgmt *mgmt;
3093 u16 fc;
3094
Zhu Yiece8edd2007-11-22 10:53:21 +08003095 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003096 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003097
3098 mgmt = (struct ieee80211_mgmt *) skb->data;
3099 fc = le16_to_cpu(mgmt->frame_control);
3100
Zhu Yiece8edd2007-11-22 10:53:21 +08003101 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01003102 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003103
3104 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003105 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003106
Jiri Bencf0706e82007-05-05 11:45:53 -07003107 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3108 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3109 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3110 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003111 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003112 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003113 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3114 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3115 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003116 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003117 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003118 }
3119 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01003120 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003121}
3122
3123
3124static int ieee80211_sta_active_ibss(struct net_device *dev)
3125{
3126 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3127 int active = 0;
3128 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003129 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003130
Johannes Bergd0709a62008-02-25 16:27:46 +01003131 rcu_read_lock();
3132
3133 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3134 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003135 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3136 jiffies)) {
3137 active++;
3138 break;
3139 }
3140 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003141
3142 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003143
3144 return active;
3145}
3146
3147
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003148static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003149{
3150 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3151 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003152 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003153 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003154 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003155
Johannes Bergd0709a62008-02-25 16:27:46 +01003156 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003157 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003158 if (time_after(jiffies, sta->last_rx + exp_time)) {
Joe Perches0795af52007-10-03 17:59:30 -07003159 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3160 dev->name, print_mac(mac, sta->addr));
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003161 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003162 if (sta)
3163 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003164 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003165 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003166
Johannes Bergd0709a62008-02-25 16:27:46 +01003167 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3168 sta_info_destroy(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003169}
3170
3171
3172static void ieee80211_sta_merge_ibss(struct net_device *dev,
3173 struct ieee80211_if_sta *ifsta)
3174{
3175 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3176
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003177 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07003178 if (ieee80211_sta_active_ibss(dev))
3179 return;
3180
3181 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3182 "IBSS networks with same SSID (merge)\n", dev->name);
3183 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3184}
3185
3186
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003187#ifdef CONFIG_MAC80211_MESH
3188static void ieee80211_mesh_housekeeping(struct net_device *dev,
3189 struct ieee80211_if_sta *ifsta)
3190{
3191 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3192 bool free_plinks;
3193
3194 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3195 mesh_path_expire(dev);
3196
3197 free_plinks = mesh_plink_availables(sdata);
3198 if (free_plinks != sdata->u.sta.accepting_plinks)
3199 ieee80211_if_config_beacon(dev);
3200
3201 mod_timer(&ifsta->timer, jiffies +
3202 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3203}
3204
3205
3206void ieee80211_start_mesh(struct net_device *dev)
3207{
3208 struct ieee80211_if_sta *ifsta;
3209 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3210 ifsta = &sdata->u.sta;
3211 ifsta->state = IEEE80211_MESH_UP;
3212 ieee80211_sta_timer((unsigned long)sdata);
3213}
3214#endif
3215
3216
Jiri Bencf0706e82007-05-05 11:45:53 -07003217void ieee80211_sta_timer(unsigned long data)
3218{
3219 struct ieee80211_sub_if_data *sdata =
3220 (struct ieee80211_sub_if_data *) data;
3221 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3222 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3223
3224 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3225 queue_work(local->hw.workqueue, &ifsta->work);
3226}
3227
Jiri Bencf0706e82007-05-05 11:45:53 -07003228void ieee80211_sta_work(struct work_struct *work)
3229{
3230 struct ieee80211_sub_if_data *sdata =
3231 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3232 struct net_device *dev = sdata->dev;
3233 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3234 struct ieee80211_if_sta *ifsta;
3235 struct sk_buff *skb;
3236
3237 if (!netif_running(dev))
3238 return;
3239
Zhu Yiece8edd2007-11-22 10:53:21 +08003240 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003241 return;
3242
Johannes Berg51fb61e2007-12-19 01:31:27 +01003243 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003244 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3245 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003246 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01003247 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e82007-05-05 11:45:53 -07003248 return;
3249 }
3250 ifsta = &sdata->u.sta;
3251
3252 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3253 ieee80211_sta_rx_queued_mgmt(dev, skb);
3254
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003255#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003256 if (ifsta->preq_queue_len &&
3257 time_after(jiffies,
3258 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003259 mesh_path_start_discovery(dev);
3260#endif
3261
Jiri Bencf0706e82007-05-05 11:45:53 -07003262 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3263 ifsta->state != IEEE80211_ASSOCIATE &&
3264 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003265 if (ifsta->scan_ssid_len)
3266 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3267 else
3268 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003269 return;
3270 }
3271
3272 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3273 if (ieee80211_sta_config_auth(dev, ifsta))
3274 return;
3275 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3276 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3277 return;
3278
3279 switch (ifsta->state) {
3280 case IEEE80211_DISABLED:
3281 break;
3282 case IEEE80211_AUTHENTICATE:
3283 ieee80211_authenticate(dev, ifsta);
3284 break;
3285 case IEEE80211_ASSOCIATE:
3286 ieee80211_associate(dev, ifsta);
3287 break;
3288 case IEEE80211_ASSOCIATED:
3289 ieee80211_associated(dev, ifsta);
3290 break;
3291 case IEEE80211_IBSS_SEARCH:
3292 ieee80211_sta_find_ibss(dev, ifsta);
3293 break;
3294 case IEEE80211_IBSS_JOINED:
3295 ieee80211_sta_merge_ibss(dev, ifsta);
3296 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003297#ifdef CONFIG_MAC80211_MESH
3298 case IEEE80211_MESH_UP:
3299 ieee80211_mesh_housekeeping(dev, ifsta);
3300 break;
3301#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003302 default:
3303 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3304 ifsta->state);
3305 break;
3306 }
3307
3308 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3309 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3310 "mixed-cell disabled - disassociate\n", dev->name);
3311
3312 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3313 ieee80211_set_disassoc(dev, ifsta, 0);
3314 }
3315}
3316
3317
3318static void ieee80211_sta_reset_auth(struct net_device *dev,
3319 struct ieee80211_if_sta *ifsta)
3320{
3321 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3322
3323 if (local->ops->reset_tsf) {
3324 /* Reset own TSF to allow time synchronization work. */
3325 local->ops->reset_tsf(local_to_hw(local));
3326 }
3327
3328 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3329
3330
3331 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3332 ifsta->auth_alg = WLAN_AUTH_OPEN;
3333 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3334 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3335 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3336 ifsta->auth_alg = WLAN_AUTH_LEAP;
3337 else
3338 ifsta->auth_alg = WLAN_AUTH_OPEN;
3339 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3340 ifsta->auth_alg);
3341 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003342 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3343 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003344 netif_carrier_off(dev);
3345}
3346
3347
3348void ieee80211_sta_req_auth(struct net_device *dev,
3349 struct ieee80211_if_sta *ifsta)
3350{
3351 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3352 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3353
Johannes Berg51fb61e2007-12-19 01:31:27 +01003354 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003355 return;
3356
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003357 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3358 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3359 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3360 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003361 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3362 queue_work(local->hw.workqueue, &ifsta->work);
3363 }
3364}
3365
3366static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3367 const char *ssid, int ssid_len)
3368{
3369 int tmp, hidden_ssid;
3370
Michael Wu48225702007-10-19 17:14:36 -04003371 if (ssid_len == ifsta->ssid_len &&
3372 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003373 return 1;
3374
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003375 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003376 return 0;
3377
3378 hidden_ssid = 1;
3379 tmp = ssid_len;
3380 while (tmp--) {
3381 if (ssid[tmp] != '\0') {
3382 hidden_ssid = 0;
3383 break;
3384 }
3385 }
3386
3387 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3388 return 1;
3389
3390 if (ssid_len == 1 && ssid[0] == ' ')
3391 return 1;
3392
3393 return 0;
3394}
3395
3396static int ieee80211_sta_config_auth(struct net_device *dev,
3397 struct ieee80211_if_sta *ifsta)
3398{
3399 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3400 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3401 struct ieee80211_sta_bss *bss, *selected = NULL;
3402 int top_rssi = 0, freq;
3403
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003404 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3405 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003406 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003407 ieee80211_sta_reset_auth(dev, ifsta);
3408 return 0;
3409 }
3410
3411 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003412 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003413 list_for_each_entry(bss, &local->sta_bss_list, list) {
3414 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3415 continue;
3416
3417 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3418 !!sdata->default_key)
3419 continue;
3420
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003421 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3422 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003423 continue;
3424
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003425 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003426 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3427 continue;
3428
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003429 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003430 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3431 continue;
3432
3433 if (!selected || top_rssi < bss->rssi) {
3434 selected = bss;
3435 top_rssi = bss->rssi;
3436 }
3437 }
3438 if (selected)
3439 atomic_inc(&selected->users);
3440 spin_unlock_bh(&local->sta_bss_lock);
3441
3442 if (selected) {
Johannes Berg8318d782008-01-24 19:38:38 +01003443 ieee80211_set_freq(local, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003444 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003445 ieee80211_sta_set_ssid(dev, selected->ssid,
3446 selected->ssid_len);
3447 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003448 ieee80211_sta_def_wmm_params(dev, selected, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003449 ieee80211_rx_bss_put(dev, selected);
3450 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003451 ieee80211_sta_reset_auth(dev, ifsta);
3452 return 0;
3453 } else {
3454 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003455 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003456 ieee80211_sta_start_scan(dev, NULL, 0);
3457 else
3458 ieee80211_sta_start_scan(dev, ifsta->ssid,
3459 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003460 ifsta->state = IEEE80211_AUTHENTICATE;
3461 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3462 } else
3463 ifsta->state = IEEE80211_DISABLED;
3464 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003465 return -1;
3466}
3467
Jiri Bencf0706e82007-05-05 11:45:53 -07003468
3469static int ieee80211_sta_create_ibss(struct net_device *dev,
3470 struct ieee80211_if_sta *ifsta)
3471{
3472 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3473 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003474 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003475 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003476 u8 bssid[ETH_ALEN], *pos;
3477 int i;
Joe Perches0795af52007-10-03 17:59:30 -07003478 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003479
3480#if 0
3481 /* Easier testing, use fixed BSSID. */
3482 memset(bssid, 0xfe, ETH_ALEN);
3483#else
3484 /* Generate random, not broadcast, locally administered BSSID. Mix in
3485 * own MAC address to make sure that devices that do not have proper
3486 * random number generator get different BSSID. */
3487 get_random_bytes(bssid, ETH_ALEN);
3488 for (i = 0; i < ETH_ALEN; i++)
3489 bssid[i] ^= dev->dev_addr[i];
3490 bssid[0] &= ~0x01;
3491 bssid[0] |= 0x02;
3492#endif
3493
Joe Perches0795af52007-10-03 17:59:30 -07003494 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3495 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003496
Johannes Berg8318d782008-01-24 19:38:38 +01003497 bss = ieee80211_rx_bss_add(dev, bssid,
3498 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003499 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003500 if (!bss)
3501 return -ENOMEM;
3502
Johannes Berg8318d782008-01-24 19:38:38 +01003503 bss->band = local->hw.conf.channel->band;
3504 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003505
3506 if (local->hw.conf.beacon_int == 0)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003507 local->hw.conf.beacon_int = 10000;
Jiri Bencf0706e82007-05-05 11:45:53 -07003508 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003509 bss->last_update = jiffies;
3510 bss->capability = WLAN_CAPABILITY_IBSS;
Johannes Berg988c0f72008-04-17 19:21:22 +02003511
3512 if (sdata->default_key)
Jiri Bencf0706e82007-05-05 11:45:53 -07003513 bss->capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +02003514 else
Jiri Bencf0706e82007-05-05 11:45:53 -07003515 sdata->drop_unencrypted = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02003516
Johannes Berg8318d782008-01-24 19:38:38 +01003517 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003518 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003519 for (i = 0; i < sband->n_bitrates; i++) {
3520 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003521 *pos++ = (u8) (rate / 5);
3522 }
3523
3524 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3525}
3526
3527
3528static int ieee80211_sta_find_ibss(struct net_device *dev,
3529 struct ieee80211_if_sta *ifsta)
3530{
3531 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3532 struct ieee80211_sta_bss *bss;
3533 int found = 0;
3534 u8 bssid[ETH_ALEN];
3535 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003536 DECLARE_MAC_BUF(mac);
3537 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003538
3539 if (ifsta->ssid_len == 0)
3540 return -EINVAL;
3541
3542 active_ibss = ieee80211_sta_active_ibss(dev);
3543#ifdef CONFIG_MAC80211_IBSS_DEBUG
3544 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3545 dev->name, active_ibss);
3546#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3547 spin_lock_bh(&local->sta_bss_lock);
3548 list_for_each_entry(bss, &local->sta_bss_list, list) {
3549 if (ifsta->ssid_len != bss->ssid_len ||
3550 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3551 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3552 continue;
3553#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003554 printk(KERN_DEBUG " bssid=%s found\n",
3555 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003556#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3557 memcpy(bssid, bss->bssid, ETH_ALEN);
3558 found = 1;
3559 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3560 break;
3561 }
3562 spin_unlock_bh(&local->sta_bss_lock);
3563
3564#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003565 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3566 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003567#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3568 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003569 (bss = ieee80211_rx_bss_get(dev, bssid,
3570 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003571 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07003572 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003573 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003574 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003575 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3576 }
3577#ifdef CONFIG_MAC80211_IBSS_DEBUG
3578 printk(KERN_DEBUG " did not try to join ibss\n");
3579#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3580
3581 /* Selected IBSS not found in current scan results - try to scan */
3582 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3583 !ieee80211_sta_active_ibss(dev)) {
3584 mod_timer(&ifsta->timer, jiffies +
3585 IEEE80211_IBSS_MERGE_INTERVAL);
3586 } else if (time_after(jiffies, local->last_scan_completed +
3587 IEEE80211_SCAN_INTERVAL)) {
3588 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3589 "join\n", dev->name);
3590 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3591 ifsta->ssid_len);
3592 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3593 int interval = IEEE80211_SCAN_INTERVAL;
3594
3595 if (time_after(jiffies, ifsta->ibss_join_req +
3596 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003597 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003598 (!(local->oper_channel->flags &
3599 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003600 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003601 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003602 printk(KERN_DEBUG "%s: IBSS not allowed on"
3603 " %d MHz\n", dev->name,
3604 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003605 }
3606
3607 /* No IBSS found - decrease scan interval and continue
3608 * scanning. */
3609 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3610 }
3611
3612 ifsta->state = IEEE80211_IBSS_SEARCH;
3613 mod_timer(&ifsta->timer, jiffies + interval);
3614 return 0;
3615 }
3616
3617 return 0;
3618}
3619
3620
3621int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3622{
Johannes Berg8318d782008-01-24 19:38:38 +01003623 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003624 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07003625
3626 if (len > IEEE80211_MAX_SSID_LEN)
3627 return -EINVAL;
3628
Jiri Bencf0706e82007-05-05 11:45:53 -07003629 ifsta = &sdata->u.sta;
3630
3631 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003632 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003633 memcpy(ifsta->ssid, ssid, len);
3634 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3635 ifsta->ssid_len = len;
3636
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003637 if (len)
3638 ifsta->flags |= IEEE80211_STA_SSID_SET;
3639 else
3640 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003641 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003642 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003643 ifsta->ibss_join_req = jiffies;
3644 ifsta->state = IEEE80211_IBSS_SEARCH;
3645 return ieee80211_sta_find_ibss(dev, ifsta);
3646 }
3647 return 0;
3648}
3649
3650
3651int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3652{
3653 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3654 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3655 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3656 *len = ifsta->ssid_len;
3657 return 0;
3658}
3659
3660
3661int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3662{
3663 struct ieee80211_sub_if_data *sdata;
3664 struct ieee80211_if_sta *ifsta;
3665 int res;
3666
3667 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3668 ifsta = &sdata->u.sta;
3669
3670 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3671 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3672 res = ieee80211_if_config(dev);
3673 if (res) {
3674 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3675 "the low-level driver\n", dev->name);
3676 return res;
3677 }
3678 }
3679
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003680 if (is_valid_ether_addr(bssid))
3681 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003682 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003683 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3684
Jiri Bencf0706e82007-05-05 11:45:53 -07003685 return 0;
3686}
3687
3688
3689static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3690 struct ieee80211_sub_if_data *sdata,
3691 int powersave)
3692{
3693 struct sk_buff *skb;
3694 struct ieee80211_hdr *nullfunc;
3695 u16 fc;
3696
3697 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3698 if (!skb) {
3699 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3700 "frame\n", sdata->dev->name);
3701 return;
3702 }
3703 skb_reserve(skb, local->hw.extra_tx_headroom);
3704
3705 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3706 memset(nullfunc, 0, 24);
3707 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3708 IEEE80211_FCTL_TODS;
3709 if (powersave)
3710 fc |= IEEE80211_FCTL_PM;
3711 nullfunc->frame_control = cpu_to_le16(fc);
3712 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3713 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3714 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3715
3716 ieee80211_sta_tx(sdata->dev, skb, 0);
3717}
3718
3719
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003720static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3721{
3722 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3723 ieee80211_vif_is_mesh(&sdata->vif))
3724 ieee80211_sta_timer((unsigned long)sdata);
3725}
3726
Jiri Bencf0706e82007-05-05 11:45:53 -07003727void ieee80211_scan_completed(struct ieee80211_hw *hw)
3728{
3729 struct ieee80211_local *local = hw_to_local(hw);
3730 struct net_device *dev = local->scan_dev;
3731 struct ieee80211_sub_if_data *sdata;
3732 union iwreq_data wrqu;
3733
3734 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003735 memset(&wrqu, 0, sizeof(wrqu));
3736 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003737
Zhu Yiece8edd2007-11-22 10:53:21 +08003738 if (local->sta_hw_scanning) {
3739 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003740 if (ieee80211_hw_config(local))
3741 printk(KERN_DEBUG "%s: failed to restore operational "
3742 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003743 /* Restart STA timer for HW scan case */
3744 rcu_read_lock();
3745 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3746 ieee80211_restart_sta_timer(sdata);
3747 rcu_read_unlock();
3748
Zhu Yiece8edd2007-11-22 10:53:21 +08003749 goto done;
3750 }
3751
3752 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003753 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003754 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003755 "channel after scan\n", dev->name);
3756
Johannes Berg4150c572007-09-17 01:29:23 -04003757
3758 netif_tx_lock_bh(local->mdev);
3759 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3760 local->ops->configure_filter(local_to_hw(local),
3761 FIF_BCN_PRBRESP_PROMISC,
3762 &local->filter_flags,
3763 local->mdev->mc_count,
3764 local->mdev->mc_list);
3765
3766 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003767
Johannes Berg79010422007-09-18 17:29:21 -04003768 rcu_read_lock();
3769 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003770
3771 /* No need to wake the master device. */
3772 if (sdata->dev == local->mdev)
3773 continue;
3774
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003775 /* Tell AP we're back */
3776 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3777 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3778 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003779
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003780 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003781
Jiri Bencf0706e82007-05-05 11:45:53 -07003782 netif_wake_queue(sdata->dev);
3783 }
Johannes Berg79010422007-09-18 17:29:21 -04003784 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003785
Zhu Yiece8edd2007-11-22 10:53:21 +08003786done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003787 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003788 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003789 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003790 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003791 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3792 !ieee80211_sta_active_ibss(dev)))
3793 ieee80211_sta_find_ibss(dev, ifsta);
3794 }
3795}
3796EXPORT_SYMBOL(ieee80211_scan_completed);
3797
3798void ieee80211_sta_scan_work(struct work_struct *work)
3799{
3800 struct ieee80211_local *local =
3801 container_of(work, struct ieee80211_local, scan_work.work);
3802 struct net_device *dev = local->scan_dev;
3803 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003804 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003805 struct ieee80211_channel *chan;
3806 int skip;
3807 unsigned long next_delay = 0;
3808
Zhu Yiece8edd2007-11-22 10:53:21 +08003809 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003810 return;
3811
3812 switch (local->scan_state) {
3813 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003814 /*
3815 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3816 * after we successfully scanned the last channel of the last
3817 * band (and the last band is supported by the hw)
3818 */
Johannes Berg8318d782008-01-24 19:38:38 +01003819 if (local->scan_band < IEEE80211_NUM_BANDS)
3820 sband = local->hw.wiphy->bands[local->scan_band];
3821 else
3822 sband = NULL;
3823
Johannes Berg69d464d2008-01-28 17:08:56 +01003824 /*
3825 * If we are at an unsupported band and have more bands
3826 * left to scan, advance to the next supported one.
3827 */
3828 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003829 local->scan_band++;
3830 sband = local->hw.wiphy->bands[local->scan_band];
3831 local->scan_channel_idx = 0;
3832 }
3833
Johannes Berg69d464d2008-01-28 17:08:56 +01003834 /* if no more bands/channels left, complete scan */
3835 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003836 ieee80211_scan_completed(local_to_hw(local));
3837 return;
3838 }
Johannes Berg8318d782008-01-24 19:38:38 +01003839 skip = 0;
3840 chan = &sband->channels[local->scan_channel_idx];
3841
3842 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003843 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003844 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003845 skip = 1;
3846
3847 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003848 local->scan_channel = chan;
3849 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003850 printk(KERN_DEBUG "%s: failed to set freq to "
3851 "%d MHz for scan\n", dev->name,
3852 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003853 skip = 1;
3854 }
3855 }
3856
Johannes Berg69d464d2008-01-28 17:08:56 +01003857 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003858 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003859 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003860 /*
3861 * scan_band may end up == IEEE80211_NUM_BANDS, but
3862 * we'll catch that case above and complete the scan
3863 * if that is the case.
3864 */
Johannes Berg8318d782008-01-24 19:38:38 +01003865 local->scan_band++;
3866 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003867 }
3868
3869 if (skip)
3870 break;
3871
3872 next_delay = IEEE80211_PROBE_DELAY +
3873 usecs_to_jiffies(local->hw.channel_change_time);
3874 local->scan_state = SCAN_SEND_PROBE;
3875 break;
3876 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003877 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003878 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003879
3880 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3881 break;
3882 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3883 local->scan_ssid_len);
3884 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003885 break;
3886 }
3887
Zhu Yiece8edd2007-11-22 10:53:21 +08003888 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003889 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3890 next_delay);
3891}
3892
3893
3894static int ieee80211_sta_start_scan(struct net_device *dev,
3895 u8 *ssid, size_t ssid_len)
3896{
3897 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3898 struct ieee80211_sub_if_data *sdata;
3899
3900 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3901 return -EINVAL;
3902
3903 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3904 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3905 * BSSID: MACAddress
3906 * SSID
3907 * ScanType: ACTIVE, PASSIVE
3908 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3909 * a Probe frame during active scanning
3910 * ChannelList
3911 * MinChannelTime (>= ProbeDelay), in TU
3912 * MaxChannelTime: (>= MinChannelTime), in TU
3913 */
3914
3915 /* MLME-SCAN.confirm
3916 * BSSDescriptionSet
3917 * ResultCode: SUCCESS, INVALID_PARAMETERS
3918 */
3919
Zhu Yiece8edd2007-11-22 10:53:21 +08003920 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003921 if (local->scan_dev == dev)
3922 return 0;
3923 return -EBUSY;
3924 }
3925
3926 if (local->ops->hw_scan) {
3927 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003928 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003929 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003930 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003931 local->scan_dev = dev;
3932 }
3933 return rc;
3934 }
3935
Zhu Yiece8edd2007-11-22 10:53:21 +08003936 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003937
Johannes Berg79010422007-09-18 17:29:21 -04003938 rcu_read_lock();
3939 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003940
3941 /* Don't stop the master interface, otherwise we can't transmit
3942 * probes! */
3943 if (sdata->dev == local->mdev)
3944 continue;
3945
Jiri Bencf0706e82007-05-05 11:45:53 -07003946 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003947 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003948 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003949 ieee80211_send_nullfunc(local, sdata, 1);
3950 }
Johannes Berg79010422007-09-18 17:29:21 -04003951 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003952
3953 if (ssid) {
3954 local->scan_ssid_len = ssid_len;
3955 memcpy(local->scan_ssid, ssid, ssid_len);
3956 } else
3957 local->scan_ssid_len = 0;
3958 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07003959 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003960 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07003961 local->scan_dev = dev;
3962
Johannes Berg4150c572007-09-17 01:29:23 -04003963 netif_tx_lock_bh(local->mdev);
3964 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3965 local->ops->configure_filter(local_to_hw(local),
3966 FIF_BCN_PRBRESP_PROMISC,
3967 &local->filter_flags,
3968 local->mdev->mc_count,
3969 local->mdev->mc_list);
3970 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003971
3972 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3973 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3974 IEEE80211_CHANNEL_TIME);
3975
3976 return 0;
3977}
3978
3979
3980int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3981{
3982 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3983 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3984 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3985
Johannes Berg51fb61e2007-12-19 01:31:27 +01003986 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003987 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3988
Zhu Yiece8edd2007-11-22 10:53:21 +08003989 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003990 if (local->scan_dev == dev)
3991 return 0;
3992 return -EBUSY;
3993 }
3994
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003995 ifsta->scan_ssid_len = ssid_len;
3996 if (ssid_len)
3997 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003998 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3999 queue_work(local->hw.workqueue, &ifsta->work);
4000 return 0;
4001}
4002
4003static char *
4004ieee80211_sta_scan_result(struct net_device *dev,
4005 struct ieee80211_sta_bss *bss,
4006 char *current_ev, char *end_buf)
4007{
4008 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4009 struct iw_event iwe;
4010
4011 if (time_after(jiffies,
4012 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4013 return current_ev;
4014
Jiri Bencf0706e82007-05-05 11:45:53 -07004015 memset(&iwe, 0, sizeof(iwe));
4016 iwe.cmd = SIOCGIWAP;
4017 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4018 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4019 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4020 IW_EV_ADDR_LEN);
4021
4022 memset(&iwe, 0, sizeof(iwe));
4023 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01004024 if (bss_mesh_cfg(bss)) {
4025 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004026 iwe.u.data.flags = 1;
4027 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
Johannes Berg902acc72008-02-23 15:17:19 +01004028 bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004029 } else {
4030 iwe.u.data.length = bss->ssid_len;
4031 iwe.u.data.flags = 1;
4032 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4033 bss->ssid);
4034 }
Jiri Bencf0706e82007-05-05 11:45:53 -07004035
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08004036 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4037 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004038 memset(&iwe, 0, sizeof(iwe));
4039 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01004040 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004041 iwe.u.mode = IW_MODE_MESH;
4042 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004043 iwe.u.mode = IW_MODE_MASTER;
4044 else
4045 iwe.u.mode = IW_MODE_ADHOC;
4046 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4047 IW_EV_UINT_LEN);
4048 }
4049
4050 memset(&iwe, 0, sizeof(iwe));
4051 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01004052 iwe.u.freq.m = bss->freq;
4053 iwe.u.freq.e = 6;
Jiri Bencf0706e82007-05-05 11:45:53 -07004054 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4055 IW_EV_FREQ_LEN);
Johannes Berg8318d782008-01-24 19:38:38 +01004056
4057 memset(&iwe, 0, sizeof(iwe));
4058 iwe.cmd = SIOCGIWFREQ;
4059 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4060 iwe.u.freq.e = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07004061 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4062 IW_EV_FREQ_LEN);
4063
4064 memset(&iwe, 0, sizeof(iwe));
4065 iwe.cmd = IWEVQUAL;
4066 iwe.u.qual.qual = bss->signal;
4067 iwe.u.qual.level = bss->rssi;
4068 iwe.u.qual.noise = bss->noise;
4069 iwe.u.qual.updated = local->wstats_flags;
4070 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4071 IW_EV_QUAL_LEN);
4072
4073 memset(&iwe, 0, sizeof(iwe));
4074 iwe.cmd = SIOCGIWENCODE;
4075 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4076 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4077 else
4078 iwe.u.data.flags = IW_ENCODE_DISABLED;
4079 iwe.u.data.length = 0;
4080 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4081
4082 if (bss && bss->wpa_ie) {
4083 memset(&iwe, 0, sizeof(iwe));
4084 iwe.cmd = IWEVGENIE;
4085 iwe.u.data.length = bss->wpa_ie_len;
4086 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4087 bss->wpa_ie);
4088 }
4089
4090 if (bss && bss->rsn_ie) {
4091 memset(&iwe, 0, sizeof(iwe));
4092 iwe.cmd = IWEVGENIE;
4093 iwe.u.data.length = bss->rsn_ie_len;
4094 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4095 bss->rsn_ie);
4096 }
4097
4098 if (bss && bss->supp_rates_len > 0) {
4099 /* display all supported rates in readable format */
4100 char *p = current_ev + IW_EV_LCP_LEN;
4101 int i;
4102
4103 memset(&iwe, 0, sizeof(iwe));
4104 iwe.cmd = SIOCGIWRATE;
4105 /* Those two flags are ignored... */
4106 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4107
4108 for (i = 0; i < bss->supp_rates_len; i++) {
4109 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4110 0x7f) * 500000);
4111 p = iwe_stream_add_value(current_ev, p,
4112 end_buf, &iwe, IW_EV_PARAM_LEN);
4113 }
4114 current_ev = p;
4115 }
4116
4117 if (bss) {
4118 char *buf;
4119 buf = kmalloc(30, GFP_ATOMIC);
4120 if (buf) {
4121 memset(&iwe, 0, sizeof(iwe));
4122 iwe.cmd = IWEVCUSTOM;
4123 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4124 iwe.u.data.length = strlen(buf);
4125 current_ev = iwe_stream_add_point(current_ev, end_buf,
4126 &iwe, buf);
4127 kfree(buf);
4128 }
4129 }
4130
Johannes Berg902acc72008-02-23 15:17:19 +01004131 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004132 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04004133 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004134 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004135 if (buf) {
4136 memset(&iwe, 0, sizeof(iwe));
4137 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04004138 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004139 iwe.u.data.length = strlen(buf);
4140 current_ev = iwe_stream_add_point(current_ev, end_buf,
4141 &iwe, buf);
4142 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04004143 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4144 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004145 iwe.u.data.length = strlen(buf);
4146 current_ev = iwe_stream_add_point(current_ev, end_buf,
4147 &iwe, buf);
4148 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04004149 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4150 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004151 iwe.u.data.length = strlen(buf);
4152 current_ev = iwe_stream_add_point(current_ev, end_buf,
4153 &iwe, buf);
4154 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04004155 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4156 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004157 iwe.u.data.length = strlen(buf);
4158 current_ev = iwe_stream_add_point(current_ev, end_buf,
4159 &iwe, buf);
4160 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04004161 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4162 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004163 iwe.u.data.length = strlen(buf);
4164 current_ev = iwe_stream_add_point(current_ev, end_buf,
4165 &iwe, buf);
4166 kfree(buf);
4167 }
4168 }
4169
Jiri Bencf0706e82007-05-05 11:45:53 -07004170 return current_ev;
4171}
4172
4173
4174int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4175{
4176 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4177 char *current_ev = buf;
4178 char *end_buf = buf + len;
4179 struct ieee80211_sta_bss *bss;
4180
4181 spin_lock_bh(&local->sta_bss_lock);
4182 list_for_each_entry(bss, &local->sta_bss_list, list) {
4183 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4184 spin_unlock_bh(&local->sta_bss_lock);
4185 return -E2BIG;
4186 }
4187 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4188 end_buf);
4189 }
4190 spin_unlock_bh(&local->sta_bss_lock);
4191 return current_ev - buf;
4192}
4193
4194
4195int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4196{
4197 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4198 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg988c0f72008-04-17 19:21:22 +02004199
Jiri Bencf0706e82007-05-05 11:45:53 -07004200 kfree(ifsta->extra_ie);
4201 if (len == 0) {
4202 ifsta->extra_ie = NULL;
4203 ifsta->extra_ie_len = 0;
4204 return 0;
4205 }
4206 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4207 if (!ifsta->extra_ie) {
4208 ifsta->extra_ie_len = 0;
4209 return -ENOMEM;
4210 }
4211 memcpy(ifsta->extra_ie, ie, len);
4212 ifsta->extra_ie_len = len;
4213 return 0;
4214}
4215
4216
Johannes Berg988c0f72008-04-17 19:21:22 +02004217struct sta_info *ieee80211_ibss_add_sta(struct net_device *dev,
4218 struct sk_buff *skb, u8 *bssid,
4219 u8 *addr)
Jiri Bencf0706e82007-05-05 11:45:53 -07004220{
4221 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4222 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004223 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004224 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07004225
4226 /* TODO: Could consider removing the least recently used entry and
4227 * allow new one to be added. */
4228 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4229 if (net_ratelimit()) {
4230 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004231 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004232 }
4233 return NULL;
4234 }
4235
Joe Perches0795af52007-10-03 17:59:30 -07004236 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004237 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07004238
Johannes Berg73651ee2008-02-25 16:27:47 +01004239 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4240 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004241 return NULL;
4242
Johannes Berg238814f2008-01-28 17:19:37 +01004243 sta->flags |= WLAN_STA_AUTHORIZED;
4244
Johannes Berg8318d782008-01-24 19:38:38 +01004245 sta->supp_rates[local->hw.conf.channel->band] =
4246 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004247
4248 rate_control_rate_init(sta, local);
4249
Johannes Berg93e5deb2008-04-01 15:21:00 +02004250 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004251 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004252
Johannes Bergd0709a62008-02-25 16:27:46 +01004253 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004254}
4255
4256
4257int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4258{
4259 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4260 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4261
4262 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4263 dev->name, reason);
4264
Johannes Berg51fb61e2007-12-19 01:31:27 +01004265 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4266 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004267 return -EINVAL;
4268
4269 ieee80211_send_deauth(dev, ifsta, reason);
4270 ieee80211_set_disassoc(dev, ifsta, 1);
4271 return 0;
4272}
4273
4274
4275int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4276{
4277 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4278 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4279
4280 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4281 dev->name, reason);
4282
Johannes Berg51fb61e2007-12-19 01:31:27 +01004283 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004284 return -EINVAL;
4285
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004286 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004287 return -1;
4288
4289 ieee80211_send_disassoc(dev, ifsta, reason);
4290 ieee80211_set_disassoc(dev, ifsta, 0);
4291 return 0;
4292}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004293
4294void ieee80211_notify_mac(struct ieee80211_hw *hw,
4295 enum ieee80211_notification_types notif_type)
4296{
4297 struct ieee80211_local *local = hw_to_local(hw);
4298 struct ieee80211_sub_if_data *sdata;
4299
4300 switch (notif_type) {
4301 case IEEE80211_NOTIFY_RE_ASSOC:
4302 rcu_read_lock();
4303 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4304
4305 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4306 ieee80211_sta_req_auth(sdata->dev,
4307 &sdata->u.sta);
4308 }
4309
4310 }
4311 rcu_read_unlock();
4312 break;
4313 }
4314}
4315EXPORT_SYMBOL(ieee80211_notify_mac);