blob: 86abdf96390c9bd59dc693137c88aff748fde5c7 [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)
Dan Williams872ba532008-06-04 13:59:34 -040047#define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070048
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);
Adrian Bunk712590d2008-04-21 11:47:51 +030090static void sta_rx_agg_session_timer_expired(unsigned long data);
Jiri Bencf0706e82007-05-05 11:45:53 -070091
92
Luis Carlos Coboee385852008-02-23 15:17:11 +010093void ieee802_11_parse_elems(u8 *start, size_t len,
94 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -070095{
96 size_t left = len;
97 u8 *pos = start;
Jiri Bencf0706e82007-05-05 11:45:53 -070098
99 memset(elems, 0, sizeof(*elems));
100
101 while (left >= 2) {
102 u8 id, elen;
103
104 id = *pos++;
105 elen = *pos++;
106 left -= 2;
107
John W. Linville67a4cce2007-10-12 16:40:37 -0400108 if (elen > left)
109 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700110
111 switch (id) {
112 case WLAN_EID_SSID:
113 elems->ssid = pos;
114 elems->ssid_len = elen;
115 break;
116 case WLAN_EID_SUPP_RATES:
117 elems->supp_rates = pos;
118 elems->supp_rates_len = elen;
119 break;
120 case WLAN_EID_FH_PARAMS:
121 elems->fh_params = pos;
122 elems->fh_params_len = elen;
123 break;
124 case WLAN_EID_DS_PARAMS:
125 elems->ds_params = pos;
126 elems->ds_params_len = elen;
127 break;
128 case WLAN_EID_CF_PARAMS:
129 elems->cf_params = pos;
130 elems->cf_params_len = elen;
131 break;
132 case WLAN_EID_TIM:
133 elems->tim = pos;
134 elems->tim_len = elen;
135 break;
136 case WLAN_EID_IBSS_PARAMS:
137 elems->ibss_params = pos;
138 elems->ibss_params_len = elen;
139 break;
140 case WLAN_EID_CHALLENGE:
141 elems->challenge = pos;
142 elems->challenge_len = elen;
143 break;
144 case WLAN_EID_WPA:
145 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
146 pos[2] == 0xf2) {
147 /* Microsoft OUI (00:50:F2) */
148 if (pos[3] == 1) {
149 /* OUI Type 1 - WPA IE */
150 elems->wpa = pos;
151 elems->wpa_len = elen;
152 } else if (elen >= 5 && pos[3] == 2) {
153 if (pos[4] == 0) {
154 elems->wmm_info = pos;
155 elems->wmm_info_len = elen;
156 } else if (pos[4] == 1) {
157 elems->wmm_param = pos;
158 elems->wmm_param_len = elen;
159 }
160 }
161 }
162 break;
163 case WLAN_EID_RSN:
164 elems->rsn = pos;
165 elems->rsn_len = elen;
166 break;
167 case WLAN_EID_ERP_INFO:
168 elems->erp_info = pos;
169 elems->erp_info_len = elen;
170 break;
171 case WLAN_EID_EXT_SUPP_RATES:
172 elems->ext_supp_rates = pos;
173 elems->ext_supp_rates_len = elen;
174 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200175 case WLAN_EID_HT_CAPABILITY:
176 elems->ht_cap_elem = pos;
177 elems->ht_cap_elem_len = elen;
178 break;
179 case WLAN_EID_HT_EXTRA_INFO:
180 elems->ht_info_elem = pos;
181 elems->ht_info_elem_len = elen;
182 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100183 case WLAN_EID_MESH_ID:
184 elems->mesh_id = pos;
185 elems->mesh_id_len = elen;
186 break;
187 case WLAN_EID_MESH_CONFIG:
188 elems->mesh_config = pos;
189 elems->mesh_config_len = elen;
190 break;
191 case WLAN_EID_PEER_LINK:
192 elems->peer_link = pos;
193 elems->peer_link_len = elen;
194 break;
195 case WLAN_EID_PREQ:
196 elems->preq = pos;
197 elems->preq_len = elen;
198 break;
199 case WLAN_EID_PREP:
200 elems->prep = pos;
201 elems->prep_len = elen;
202 break;
203 case WLAN_EID_PERR:
204 elems->perr = pos;
205 elems->perr_len = elen;
206 break;
Assaf Kraussf2df3852008-06-15 18:23:29 +0300207 case WLAN_EID_CHANNEL_SWITCH:
208 elems->ch_switch_elem = pos;
209 elems->ch_switch_elem_len = elen;
210 break;
211 case WLAN_EID_QUIET:
212 if (!elems->quiet_elem) {
213 elems->quiet_elem = pos;
214 elems->quiet_elem_len = elen;
215 }
216 elems->num_of_quiet_elem++;
217 break;
218 case WLAN_EID_COUNTRY:
219 elems->country_elem = pos;
220 elems->country_elem_len = elen;
221 break;
222 case WLAN_EID_PWR_CONSTRAINT:
223 elems->pwr_constr_elem = pos;
224 elems->pwr_constr_elem_len = elen;
225 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700226 default:
Jiri Bencf0706e82007-05-05 11:45:53 -0700227 break;
228 }
229
230 left -= elen;
231 pos += elen;
232 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700233}
234
235
Jiri Bencf0706e82007-05-05 11:45:53 -0700236static int ecw2cw(int ecw)
237{
Johannes Bergac2bf322008-02-14 00:30:35 +0100238 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700239}
240
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100241
242static void ieee80211_sta_def_wmm_params(struct net_device *dev,
243 struct ieee80211_sta_bss *bss,
244 int ibss)
245{
246 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
247 struct ieee80211_local *local = sdata->local;
248 int i, have_higher_than_11mbit = 0;
249
250
251 /* cf. IEEE 802.11 9.2.12 */
252 for (i = 0; i < bss->supp_rates_len; i++)
253 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
254 have_higher_than_11mbit = 1;
255
256 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
257 have_higher_than_11mbit)
258 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
259 else
260 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
261
262
263 if (local->ops->conf_tx) {
264 struct ieee80211_tx_queue_params qparam;
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100265
266 memset(&qparam, 0, sizeof(qparam));
267
268 qparam.aifs = 2;
269
270 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
271 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
272 qparam.cw_min = 31;
273 else
274 qparam.cw_min = 15;
275
276 qparam.cw_max = 1023;
277 qparam.txop = 0;
278
Johannes Berge100bb62008-04-30 18:51:21 +0200279 for (i = 0; i < local_to_hw(local)->queues; i++)
280 local->ops->conf_tx(local_to_hw(local), i, &qparam);
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100281 }
282}
283
Jiri Bencf0706e82007-05-05 11:45:53 -0700284static void ieee80211_sta_wmm_params(struct net_device *dev,
285 struct ieee80211_if_sta *ifsta,
286 u8 *wmm_param, size_t wmm_param_len)
287{
288 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
289 struct ieee80211_tx_queue_params params;
290 size_t left;
291 int count;
292 u8 *pos;
293
Johannes Berg3434fbd2008-05-03 00:59:37 +0200294 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
295 return;
296
297 if (!wmm_param)
298 return;
299
Jiri Bencf0706e82007-05-05 11:45:53 -0700300 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
301 return;
302 count = wmm_param[6] & 0x0f;
303 if (count == ifsta->wmm_last_param_set)
304 return;
305 ifsta->wmm_last_param_set = count;
306
307 pos = wmm_param + 8;
308 left = wmm_param_len - 8;
309
310 memset(&params, 0, sizeof(params));
311
312 if (!local->ops->conf_tx)
313 return;
314
315 local->wmm_acm = 0;
316 for (; left >= 4; left -= 4, pos += 4) {
317 int aci = (pos[0] >> 5) & 0x03;
318 int acm = (pos[0] >> 4) & 0x01;
319 int queue;
320
321 switch (aci) {
322 case 1:
Johannes Berge100bb62008-04-30 18:51:21 +0200323 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200324 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700325 local->wmm_acm |= BIT(0) | BIT(3);
Jiri Bencf0706e82007-05-05 11:45:53 -0700326 break;
327 case 2:
Johannes Berge100bb62008-04-30 18:51:21 +0200328 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200329 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700330 local->wmm_acm |= BIT(4) | BIT(5);
Jiri Bencf0706e82007-05-05 11:45:53 -0700331 break;
332 case 3:
Johannes Berge100bb62008-04-30 18:51:21 +0200333 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200334 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700335 local->wmm_acm |= BIT(6) | BIT(7);
Jiri Bencf0706e82007-05-05 11:45:53 -0700336 break;
337 case 0:
338 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200339 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200340 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700341 local->wmm_acm |= BIT(1) | BIT(2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700342 break;
343 }
344
345 params.aifs = pos[0] & 0x0f;
346 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
347 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100348 params.txop = pos[2] | (pos[3] << 8);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200349#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700350 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100351 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e82007-05-05 11:45:53 -0700352 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100353 params.cw_max, params.txop);
354#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700355 /* TODO: handle ACM (block TX, fallback to next lowest allowed
356 * AC for now) */
357 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
358 printk(KERN_DEBUG "%s: failed to set TX queue "
359 "parameters for queue %d\n", dev->name, queue);
360 }
361 }
362}
363
John W. Linville50c4afb2008-04-15 14:09:27 -0400364static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
365 bool use_protection,
366 bool use_short_preamble)
Daniel Drake56282212007-07-10 19:32:10 +0200367{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100368 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Daniel Drake56282212007-07-10 19:32:10 +0200369 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Joe Perches0795af52007-10-03 17:59:30 -0700370 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100371 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200372
Johannes Berg471b3ef2007-12-28 14:32:58 +0100373 if (use_protection != bss_conf->use_cts_prot) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200374#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200375 if (net_ratelimit()) {
376 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700377 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100378 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200379 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700380 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200381 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200382#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100383 bss_conf->use_cts_prot = use_protection;
384 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200385 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200386
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200387 if (use_short_preamble != bss_conf->use_short_preamble) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200388#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake7e9ed182007-07-27 15:43:24 +0200389 if (net_ratelimit()) {
390 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700391 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100392 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200393 use_short_preamble ? "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700394 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200395 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200396#endif
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200397 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100398 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200399 }
Daniel Draked9430a32007-07-27 15:43:24 +0200400
Johannes Berg471b3ef2007-12-28 14:32:58 +0100401 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200402}
403
John W. Linville50c4afb2008-04-15 14:09:27 -0400404static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
405 u8 erp_value)
406{
407 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
408 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
409
410 return ieee80211_handle_protect_preamb(sdata,
411 use_protection, use_short_preamble);
412}
413
414static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
415 struct ieee80211_sta_bss *bss)
416{
417 u32 changed = 0;
418
419 if (bss->has_erp_value)
420 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
421 else {
422 u16 capab = bss->capability;
423 changed |= ieee80211_handle_protect_preamb(sdata, false,
424 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
425 }
426
427 return changed;
428}
429
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200430int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
431 struct ieee80211_ht_info *ht_info)
432{
433
434 if (ht_info == NULL)
435 return -EINVAL;
436
437 memset(ht_info, 0, sizeof(*ht_info));
438
439 if (ht_cap_ie) {
440 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
441
442 ht_info->ht_supported = 1;
443 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
444 ht_info->ampdu_factor =
445 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
446 ht_info->ampdu_density =
447 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
448 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
449 } else
450 ht_info->ht_supported = 0;
451
452 return 0;
453}
454
455int ieee80211_ht_addt_info_ie_to_ht_bss_info(
456 struct ieee80211_ht_addt_info *ht_add_info_ie,
457 struct ieee80211_ht_bss_info *bss_info)
458{
459 if (bss_info == NULL)
460 return -EINVAL;
461
462 memset(bss_info, 0, sizeof(*bss_info));
463
464 if (ht_add_info_ie) {
465 u16 op_mode;
466 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
467
468 bss_info->primary_channel = ht_add_info_ie->control_chan;
469 bss_info->bss_cap = ht_add_info_ie->ht_param;
470 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
471 }
472
473 return 0;
474}
Daniel Drake56282212007-07-10 19:32:10 +0200475
Jiri Bencf0706e82007-05-05 11:45:53 -0700476static void ieee80211_sta_send_associnfo(struct net_device *dev,
477 struct ieee80211_if_sta *ifsta)
478{
479 char *buf;
480 size_t len;
481 int i;
482 union iwreq_data wrqu;
483
484 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
485 return;
486
487 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200488 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700489 if (!buf)
490 return;
491
492 len = sprintf(buf, "ASSOCINFO(");
493 if (ifsta->assocreq_ies) {
494 len += sprintf(buf + len, "ReqIEs=");
495 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
496 len += sprintf(buf + len, "%02x",
497 ifsta->assocreq_ies[i]);
498 }
499 }
500 if (ifsta->assocresp_ies) {
501 if (ifsta->assocreq_ies)
502 len += sprintf(buf + len, " ");
503 len += sprintf(buf + len, "RespIEs=");
504 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
505 len += sprintf(buf + len, "%02x",
506 ifsta->assocresp_ies[i]);
507 }
508 }
509 len += sprintf(buf + len, ")");
510
511 if (len > IW_CUSTOM_MAX) {
512 len = sprintf(buf, "ASSOCRESPIE=");
513 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
514 len += sprintf(buf + len, "%02x",
515 ifsta->assocresp_ies[i]);
516 }
517 }
518
519 memset(&wrqu, 0, sizeof(wrqu));
520 wrqu.data.length = len;
521 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
522
523 kfree(buf);
524}
525
526
527static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400528 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200529 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700530{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100531 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
532 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700533 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700534 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100535 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700536
Jiri Bencf0706e82007-05-05 11:45:53 -0700537 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200538 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400539
540 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
541
Johannes Berg51fb61e2007-12-19 01:31:27 +0100542 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700543 return;
Daniel Drake56282212007-07-10 19:32:10 +0200544
John W. Linville65c107a2007-10-05 14:23:27 -0400545 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Tomas Winkler38668c02008-03-28 16:33:32 -0700546 conf->channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400547 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200548 if (bss) {
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700549 /* set timing information */
550 sdata->bss_conf.beacon_int = bss->beacon_int;
551 sdata->bss_conf.timestamp = bss->timestamp;
552
John W. Linville50c4afb2008-04-15 14:09:27 -0400553 changed |= ieee80211_handle_bss_capability(sdata, bss);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700554
Daniel Drake56282212007-07-10 19:32:10 +0200555 ieee80211_rx_bss_put(dev, bss);
556 }
557
Tomas Winkler38668c02008-03-28 16:33:32 -0700558 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
559 changed |= BSS_CHANGED_HT;
560 sdata->bss_conf.assoc_ht = 1;
561 sdata->bss_conf.ht_conf = &conf->ht_conf;
562 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
563 }
564
Jiri Bencf0706e82007-05-05 11:45:53 -0700565 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400566 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700567 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
568 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
569 ieee80211_sta_send_associnfo(dev, ifsta);
570 } else {
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700571 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400572 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700573 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200574 ieee80211_reset_erp_info(dev);
Tomas Winkler38668c02008-03-28 16:33:32 -0700575
576 sdata->bss_conf.assoc_ht = 0;
577 sdata->bss_conf.ht_conf = NULL;
578 sdata->bss_conf.ht_bss_conf = NULL;
579
Jiri Bencf0706e82007-05-05 11:45:53 -0700580 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
581 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700582 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200583 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100584
Tomas Winklerb2205252008-01-27 16:18:22 +0200585 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100586 ieee80211_bss_info_change_notify(sdata, changed);
Reinette Chatre41a7be42008-04-03 16:08:49 -0700587 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
588 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700589}
590
591static void ieee80211_set_disassoc(struct net_device *dev,
592 struct ieee80211_if_sta *ifsta, int deauth)
593{
594 if (deauth)
595 ifsta->auth_tries = 0;
596 ifsta->assoc_tries = 0;
597 ieee80211_set_associated(dev, ifsta, 0);
598}
599
Luis Carlos Coboee385852008-02-23 15:17:11 +0100600void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
601 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700602{
603 struct ieee80211_sub_if_data *sdata;
Johannes Berge039fa42008-05-15 12:55:29 +0200604 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700605
606 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
607 skb->dev = sdata->local->mdev;
608 skb_set_mac_header(skb, 0);
609 skb_set_network_header(skb, 0);
610 skb_set_transport_header(skb, 0);
611
Johannes Berge039fa42008-05-15 12:55:29 +0200612 info = IEEE80211_SKB_CB(skb);
613 memset(info, 0, sizeof(struct ieee80211_tx_info));
614 info->control.ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400615 if (!encrypt)
Johannes Berge039fa42008-05-15 12:55:29 +0200616 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700617
618 dev_queue_xmit(skb);
619}
620
621
622static void ieee80211_send_auth(struct net_device *dev,
623 struct ieee80211_if_sta *ifsta,
624 int transaction, u8 *extra, size_t extra_len,
625 int encrypt)
626{
627 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
628 struct sk_buff *skb;
629 struct ieee80211_mgmt *mgmt;
630
631 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
632 sizeof(*mgmt) + 6 + extra_len);
633 if (!skb) {
634 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
635 "frame\n", dev->name);
636 return;
637 }
638 skb_reserve(skb, local->hw.extra_tx_headroom);
639
640 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
641 memset(mgmt, 0, 24 + 6);
642 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
643 IEEE80211_STYPE_AUTH);
644 if (encrypt)
645 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
646 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
647 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
648 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
649 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
650 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
651 ifsta->auth_transaction = transaction + 1;
652 mgmt->u.auth.status_code = cpu_to_le16(0);
653 if (extra)
654 memcpy(skb_put(skb, extra_len), extra, extra_len);
655
656 ieee80211_sta_tx(dev, skb, encrypt);
657}
658
659
660static void ieee80211_authenticate(struct net_device *dev,
661 struct ieee80211_if_sta *ifsta)
662{
Joe Perches0795af52007-10-03 17:59:30 -0700663 DECLARE_MAC_BUF(mac);
664
Jiri Bencf0706e82007-05-05 11:45:53 -0700665 ifsta->auth_tries++;
666 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700667 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700668 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700669 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700670 ifsta->state = IEEE80211_DISABLED;
671 return;
672 }
673
674 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700675 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
676 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700677
678 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
679
680 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
681}
682
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200683static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
684 struct ieee80211_supported_band *sband,
685 u64 *rates)
686{
687 int i, j, count;
688 *rates = 0;
689 count = 0;
690 for (i = 0; i < bss->supp_rates_len; i++) {
691 int rate = (bss->supp_rates[i] & 0x7F) * 5;
692
693 for (j = 0; j < sband->n_bitrates; j++)
694 if (sband->bitrates[j].bitrate == rate) {
695 *rates |= BIT(j);
696 count++;
697 break;
698 }
699 }
700
701 return count;
702}
Jiri Bencf0706e82007-05-05 11:45:53 -0700703
704static void ieee80211_send_assoc(struct net_device *dev,
705 struct ieee80211_if_sta *ifsta)
706{
707 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700708 struct sk_buff *skb;
709 struct ieee80211_mgmt *mgmt;
710 u8 *pos, *ies;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200711 int i, len, count, rates_len, supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700712 u16 capab;
713 struct ieee80211_sta_bss *bss;
714 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100715 struct ieee80211_supported_band *sband;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200716 u64 rates = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700717
718 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
719 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
720 ifsta->ssid_len);
721 if (!skb) {
722 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
723 "frame\n", dev->name);
724 return;
725 }
726 skb_reserve(skb, local->hw.extra_tx_headroom);
727
Johannes Berg8318d782008-01-24 19:38:38 +0100728 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
729
Jiri Bencf0706e82007-05-05 11:45:53 -0700730 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100731
732 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
733 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
734 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
735 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
736 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700737 }
Johannes Berg8318d782008-01-24 19:38:38 +0100738
739 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
740 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400741 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700742 if (bss) {
743 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
744 capab |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +0200745 if (bss->wmm_ie)
Jiri Bencf0706e82007-05-05 11:45:53 -0700746 wmm = 1;
Helmut Schaa0d580a72008-05-20 09:56:37 +0200747
748 /* get all rates supported by the device and the AP as
749 * some APs don't like getting a superset of their rates
750 * in the association request (e.g. D-Link DAP 1353 in
751 * b-only mode) */
752 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
753
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300754 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
755 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
756 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
757
Jiri Bencf0706e82007-05-05 11:45:53 -0700758 ieee80211_rx_bss_put(dev, bss);
Helmut Schaa0d580a72008-05-20 09:56:37 +0200759 } else {
760 rates = ~0;
761 rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700762 }
763
764 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
765 memset(mgmt, 0, 24);
766 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
767 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
768 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
769
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400770 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700771 skb_put(skb, 10);
772 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
773 IEEE80211_STYPE_REASSOC_REQ);
774 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
775 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
776 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
777 ETH_ALEN);
778 } else {
779 skb_put(skb, 4);
780 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
781 IEEE80211_STYPE_ASSOC_REQ);
782 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
783 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
784 }
785
786 /* SSID */
787 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
788 *pos++ = WLAN_EID_SSID;
789 *pos++ = ifsta->ssid_len;
790 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
791
Helmut Schaa0d580a72008-05-20 09:56:37 +0200792 /* add all rates which were marked to be used above */
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200793 supp_rates_len = rates_len;
794 if (supp_rates_len > 8)
795 supp_rates_len = 8;
Jiri Bencf0706e82007-05-05 11:45:53 -0700796
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200797 len = sband->n_bitrates;
798 pos = skb_put(skb, supp_rates_len + 2);
799 *pos++ = WLAN_EID_SUPP_RATES;
800 *pos++ = supp_rates_len;
801
802 count = 0;
803 for (i = 0; i < sband->n_bitrates; i++) {
804 if (BIT(i) & rates) {
Johannes Berg8318d782008-01-24 19:38:38 +0100805 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700806 *pos++ = (u8) (rate / 5);
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200807 if (++count == 8)
808 break;
809 }
810 }
811
812 if (count == 8) {
813 pos = skb_put(skb, rates_len - count + 2);
814 *pos++ = WLAN_EID_EXT_SUPP_RATES;
815 *pos++ = rates_len - count;
816
817 for (i++; i < sband->n_bitrates; i++) {
818 if (BIT(i) & rates) {
819 int rate = sband->bitrates[i].bitrate;
820 *pos++ = (u8) (rate / 5);
821 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700822 }
823 }
824
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300825 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
826 /* 1. power capabilities */
827 pos = skb_put(skb, 4);
828 *pos++ = WLAN_EID_PWR_CAPABILITY;
829 *pos++ = 2;
830 *pos++ = 0; /* min tx power */
831 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
832
833 /* 2. supported channels */
834 /* TODO: get this in reg domain format */
835 pos = skb_put(skb, 2 * sband->n_channels + 2);
836 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
837 *pos++ = 2 * sband->n_channels;
838 for (i = 0; i < sband->n_channels; i++) {
839 *pos++ = ieee80211_frequency_to_channel(
840 sband->channels[i].center_freq);
841 *pos++ = 1; /* one channel in the subband*/
842 }
843 }
844
Jiri Bencf0706e82007-05-05 11:45:53 -0700845 if (ifsta->extra_ie) {
846 pos = skb_put(skb, ifsta->extra_ie_len);
847 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
848 }
849
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400850 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700851 pos = skb_put(skb, 9);
852 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
853 *pos++ = 7; /* len */
854 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
855 *pos++ = 0x50;
856 *pos++ = 0xf2;
857 *pos++ = 2; /* WME */
858 *pos++ = 0; /* WME info */
859 *pos++ = 1; /* WME ver */
860 *pos++ = 0;
861 }
Johannes Berg3434fbd2008-05-03 00:59:37 +0200862
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200863 /* wmm support is a must to HT */
Johannes Berg3434fbd2008-05-03 00:59:37 +0200864 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
Emmanuel Grumbach93061022008-05-29 16:35:23 +0800865 sband->ht_info.ht_supported && bss->ht_add_ie) {
866 struct ieee80211_ht_addt_info *ht_add_info =
867 (struct ieee80211_ht_addt_info *)bss->ht_add_ie;
868 u16 cap = sband->ht_info.cap;
869 __le16 tmp;
870 u32 flags = local->hw.conf.channel->flags;
871
872 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
873 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
874 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
875 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
876 cap &= ~IEEE80211_HT_CAP_SGI_40;
877 }
878 break;
879 case IEEE80211_HT_IE_CHA_SEC_BELOW:
880 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
881 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
882 cap &= ~IEEE80211_HT_CAP_SGI_40;
883 }
884 break;
885 }
886
887 tmp = cpu_to_le16(cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200888 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
889 *pos++ = WLAN_EID_HT_CAPABILITY;
890 *pos++ = sizeof(struct ieee80211_ht_cap);
891 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
892 memcpy(pos, &tmp, sizeof(u16));
893 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100894 /* TODO: needs a define here for << 2 */
895 *pos++ = sband->ht_info.ampdu_factor |
896 (sband->ht_info.ampdu_density << 2);
897 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200898 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700899
900 kfree(ifsta->assocreq_ies);
901 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200902 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700903 if (ifsta->assocreq_ies)
904 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
905
906 ieee80211_sta_tx(dev, skb, 0);
907}
908
909
910static void ieee80211_send_deauth(struct net_device *dev,
911 struct ieee80211_if_sta *ifsta, u16 reason)
912{
913 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
914 struct sk_buff *skb;
915 struct ieee80211_mgmt *mgmt;
916
917 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
918 if (!skb) {
919 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
920 "frame\n", dev->name);
921 return;
922 }
923 skb_reserve(skb, local->hw.extra_tx_headroom);
924
925 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
926 memset(mgmt, 0, 24);
927 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
928 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
929 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
930 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
931 IEEE80211_STYPE_DEAUTH);
932 skb_put(skb, 2);
933 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
934
935 ieee80211_sta_tx(dev, skb, 0);
936}
937
938
939static void ieee80211_send_disassoc(struct net_device *dev,
940 struct ieee80211_if_sta *ifsta, u16 reason)
941{
942 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
943 struct sk_buff *skb;
944 struct ieee80211_mgmt *mgmt;
945
946 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
947 if (!skb) {
948 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
949 "frame\n", dev->name);
950 return;
951 }
952 skb_reserve(skb, local->hw.extra_tx_headroom);
953
954 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
955 memset(mgmt, 0, 24);
956 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
957 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
958 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
959 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
960 IEEE80211_STYPE_DISASSOC);
961 skb_put(skb, 2);
962 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
963
964 ieee80211_sta_tx(dev, skb, 0);
965}
966
967
968static int ieee80211_privacy_mismatch(struct net_device *dev,
969 struct ieee80211_if_sta *ifsta)
970{
John W. Linville65c107a2007-10-05 14:23:27 -0400971 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700972 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000973 int bss_privacy;
974 int wep_privacy;
975 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700976
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000977 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700978 return 0;
979
Johannes Berg8318d782008-01-24 19:38:38 +0100980 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
981 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400982 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700983 if (!bss)
984 return 0;
985
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000986 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
987 wep_privacy = !!ieee80211_sta_wep_configured(dev);
988 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700989
990 ieee80211_rx_bss_put(dev, bss);
991
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000992 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
993 return 0;
994
995 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700996}
997
998
999static void ieee80211_associate(struct net_device *dev,
1000 struct ieee80211_if_sta *ifsta)
1001{
Joe Perches0795af52007-10-03 17:59:30 -07001002 DECLARE_MAC_BUF(mac);
1003
Jiri Bencf0706e82007-05-05 11:45:53 -07001004 ifsta->assoc_tries++;
1005 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -07001006 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001007 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -07001008 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001009 ifsta->state = IEEE80211_DISABLED;
1010 return;
1011 }
1012
1013 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -07001014 printk(KERN_DEBUG "%s: associate with AP %s\n",
1015 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001016 if (ieee80211_privacy_mismatch(dev, ifsta)) {
1017 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
1018 "mixed-cell disabled - abort association\n", dev->name);
1019 ifsta->state = IEEE80211_DISABLED;
1020 return;
1021 }
1022
1023 ieee80211_send_assoc(dev, ifsta);
1024
1025 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1026}
1027
1028
1029static void ieee80211_associated(struct net_device *dev,
1030 struct ieee80211_if_sta *ifsta)
1031{
1032 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1033 struct sta_info *sta;
1034 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -07001035 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001036
1037 /* TODO: start monitoring current AP signal quality and number of
1038 * missed beacons. Scan other channels every now and then and search
1039 * for better APs. */
1040 /* TODO: remove expired BSSes */
1041
1042 ifsta->state = IEEE80211_ASSOCIATED;
1043
Johannes Bergd0709a62008-02-25 16:27:46 +01001044 rcu_read_lock();
1045
Jiri Bencf0706e82007-05-05 11:45:53 -07001046 sta = sta_info_get(local, ifsta->bssid);
1047 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -07001048 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1049 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001050 disassoc = 1;
1051 } else {
1052 disassoc = 0;
1053 if (time_after(jiffies,
1054 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001055 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001056 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -07001057 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -07001058 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -07001059 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001060 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +01001061 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001062 } else
Jiri Bencf0706e82007-05-05 11:45:53 -07001063 ieee80211_send_probe_req(dev, ifsta->bssid,
1064 local->scan_ssid,
1065 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001066 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001067 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001068 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001069 if (time_after(jiffies, ifsta->last_probe +
1070 IEEE80211_PROBE_INTERVAL)) {
1071 ifsta->last_probe = jiffies;
1072 ieee80211_send_probe_req(dev, ifsta->bssid,
1073 ifsta->ssid,
1074 ifsta->ssid_len);
1075 }
1076 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001077 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001078
1079 rcu_read_unlock();
1080
Johannes Berg3b967662008-04-08 17:56:52 +02001081 if (disassoc && sta)
Johannes Bergd0709a62008-02-25 16:27:46 +01001082 sta_info_destroy(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01001083
Jiri Bencf0706e82007-05-05 11:45:53 -07001084 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -05001085 ifsta->state = IEEE80211_DISABLED;
1086 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001087 } else {
1088 mod_timer(&ifsta->timer, jiffies +
1089 IEEE80211_MONITORING_INTERVAL);
1090 }
1091}
1092
1093
1094static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1095 u8 *ssid, size_t ssid_len)
1096{
1097 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01001098 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001099 struct sk_buff *skb;
1100 struct ieee80211_mgmt *mgmt;
1101 u8 *pos, *supp_rates, *esupp_rates = NULL;
1102 int i;
1103
1104 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1105 if (!skb) {
1106 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1107 "request\n", dev->name);
1108 return;
1109 }
1110 skb_reserve(skb, local->hw.extra_tx_headroom);
1111
1112 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1113 memset(mgmt, 0, 24);
1114 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1115 IEEE80211_STYPE_PROBE_REQ);
1116 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1117 if (dst) {
1118 memcpy(mgmt->da, dst, ETH_ALEN);
1119 memcpy(mgmt->bssid, dst, ETH_ALEN);
1120 } else {
1121 memset(mgmt->da, 0xff, ETH_ALEN);
1122 memset(mgmt->bssid, 0xff, ETH_ALEN);
1123 }
1124 pos = skb_put(skb, 2 + ssid_len);
1125 *pos++ = WLAN_EID_SSID;
1126 *pos++ = ssid_len;
1127 memcpy(pos, ssid, ssid_len);
1128
1129 supp_rates = skb_put(skb, 2);
1130 supp_rates[0] = WLAN_EID_SUPP_RATES;
1131 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001132 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1133
1134 for (i = 0; i < sband->n_bitrates; i++) {
1135 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07001136 if (esupp_rates) {
1137 pos = skb_put(skb, 1);
1138 esupp_rates[1]++;
1139 } else if (supp_rates[1] == 8) {
1140 esupp_rates = skb_put(skb, 3);
1141 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1142 esupp_rates[1] = 1;
1143 pos = &esupp_rates[2];
1144 } else {
1145 pos = skb_put(skb, 1);
1146 supp_rates[1]++;
1147 }
Johannes Berg8318d782008-01-24 19:38:38 +01001148 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -07001149 }
1150
1151 ieee80211_sta_tx(dev, skb, 0);
1152}
1153
1154
1155static int ieee80211_sta_wep_configured(struct net_device *dev)
1156{
1157 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1158 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001159 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -07001160 return 0;
1161 return 1;
1162}
1163
1164
1165static void ieee80211_auth_completed(struct net_device *dev,
1166 struct ieee80211_if_sta *ifsta)
1167{
1168 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001169 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001170 ieee80211_associate(dev, ifsta);
1171}
1172
1173
1174static void ieee80211_auth_challenge(struct net_device *dev,
1175 struct ieee80211_if_sta *ifsta,
1176 struct ieee80211_mgmt *mgmt,
1177 size_t len)
1178{
1179 u8 *pos;
1180 struct ieee802_11_elems elems;
1181
Jiri Bencf0706e82007-05-05 11:45:53 -07001182 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001183 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001184 if (!elems.challenge)
Jiri Bencf0706e82007-05-05 11:45:53 -07001185 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001186 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1187 elems.challenge_len + 2, 1);
1188}
1189
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001190static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1191 u8 dialog_token, u16 status, u16 policy,
1192 u16 buf_size, u16 timeout)
1193{
1194 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1195 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1196 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1197 struct sk_buff *skb;
1198 struct ieee80211_mgmt *mgmt;
1199 u16 capab;
1200
Ester Kummer3acea5b2008-04-17 16:05:14 -07001201 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1202
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001203 if (!skb) {
1204 printk(KERN_DEBUG "%s: failed to allocate buffer "
1205 "for addba resp frame\n", dev->name);
1206 return;
1207 }
1208
1209 skb_reserve(skb, local->hw.extra_tx_headroom);
1210 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1211 memset(mgmt, 0, 24);
1212 memcpy(mgmt->da, da, ETH_ALEN);
1213 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001214 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001215 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1216 else
1217 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1218 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1219 IEEE80211_STYPE_ACTION);
1220
1221 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1222 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1223 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1224 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1225
1226 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1227 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1228 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1229
1230 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1231 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1232 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1233
1234 ieee80211_sta_tx(dev, skb, 0);
1235
1236 return;
1237}
1238
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001239void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1240 u16 tid, u8 dialog_token, u16 start_seq_num,
1241 u16 agg_size, u16 timeout)
1242{
1243 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1244 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1245 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1246 struct sk_buff *skb;
1247 struct ieee80211_mgmt *mgmt;
1248 u16 capab;
1249
Ester Kummer3acea5b2008-04-17 16:05:14 -07001250 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001251
1252 if (!skb) {
1253 printk(KERN_ERR "%s: failed to allocate buffer "
1254 "for addba request frame\n", dev->name);
1255 return;
1256 }
1257 skb_reserve(skb, local->hw.extra_tx_headroom);
1258 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1259 memset(mgmt, 0, 24);
1260 memcpy(mgmt->da, da, ETH_ALEN);
1261 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1262 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1263 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1264 else
1265 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1266
1267 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1268 IEEE80211_STYPE_ACTION);
1269
1270 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1271
1272 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1273 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1274
1275 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1276 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1277 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1278 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1279
1280 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1281
1282 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1283 mgmt->u.action.u.addba_req.start_seq_num =
1284 cpu_to_le16(start_seq_num << 4);
1285
1286 ieee80211_sta_tx(dev, skb, 0);
1287}
1288
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001289static void ieee80211_sta_process_addba_request(struct net_device *dev,
1290 struct ieee80211_mgmt *mgmt,
1291 size_t len)
1292{
1293 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001294 struct ieee80211_hw *hw = &local->hw;
1295 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001296 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001297 struct tid_ampdu_rx *tid_agg_rx;
1298 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001299 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001300 int ret = -EOPNOTSUPP;
1301 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001302
Johannes Bergd0709a62008-02-25 16:27:46 +01001303 rcu_read_lock();
1304
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001305 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001306 if (!sta) {
1307 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001308 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001309 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001310
1311 /* extract session parameters from addba request frame */
1312 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1313 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001314 start_seq_num =
1315 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001316
1317 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1318 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1319 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1320 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1321
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001322 status = WLAN_STATUS_REQUEST_DECLINED;
1323
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001324 /* sanity check for incoming parameters:
1325 * check if configuration can support the BA policy
1326 * and if buffer size does not exceeds max value */
1327 if (((ba_policy != 1)
1328 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1329 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1330 status = WLAN_STATUS_INVALID_QOS_PARAM;
1331#ifdef CONFIG_MAC80211_HT_DEBUG
1332 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001333 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001334 "%s on tid %u. policy %d, buffer size %d\n",
1335 print_mac(mac, mgmt->sa), tid, ba_policy,
1336 buf_size);
1337#endif /* CONFIG_MAC80211_HT_DEBUG */
1338 goto end_no_lock;
1339 }
1340 /* determine default buffer size */
1341 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001342 struct ieee80211_supported_band *sband;
1343
1344 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001345 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001346 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001347 }
1348
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001349
1350 /* examine state machine */
Johannes Berg07346f812008-05-03 01:02:02 +02001351 spin_lock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001352
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001353 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001354#ifdef CONFIG_MAC80211_HT_DEBUG
1355 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001356 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001357 "%s on tid %u\n",
1358 print_mac(mac, mgmt->sa), tid);
1359#endif /* CONFIG_MAC80211_HT_DEBUG */
1360 goto end;
1361 }
1362
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001363 /* prepare A-MPDU MLME for Rx aggregation */
1364 sta->ampdu_mlme.tid_rx[tid] =
1365 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1366 if (!sta->ampdu_mlme.tid_rx[tid]) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001367#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001368 if (net_ratelimit())
1369 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1370 tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001371#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001372 goto end;
1373 }
1374 /* rx timer */
1375 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1376 sta_rx_agg_session_timer_expired;
1377 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1378 (unsigned long)&sta->timer_to_tid[tid];
1379 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1380
1381 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1382
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001383 /* prepare reordering buffer */
1384 tid_agg_rx->reorder_buf =
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301385 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001386 if (!tid_agg_rx->reorder_buf) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001387#ifdef CONFIG_MAC80211_HT_DEBUG
Johannes Berg03147df2008-02-26 00:39:28 +01001388 if (net_ratelimit())
1389 printk(KERN_ERR "can not allocate reordering buffer "
1390 "to tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001391#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001392 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001393 goto end;
1394 }
1395 memset(tid_agg_rx->reorder_buf, 0,
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301396 buf_size * sizeof(struct sk_buff *));
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001397
1398 if (local->ops->ampdu_action)
1399 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001400 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001401#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001402 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001403#endif /* CONFIG_MAC80211_HT_DEBUG */
1404
1405 if (ret) {
1406 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001407 kfree(tid_agg_rx);
1408 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001409 goto end;
1410 }
1411
1412 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001413 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001414 tid_agg_rx->dialog_token = dialog_token;
1415 tid_agg_rx->ssn = start_seq_num;
1416 tid_agg_rx->head_seq_num = start_seq_num;
1417 tid_agg_rx->buf_size = buf_size;
1418 tid_agg_rx->timeout = timeout;
1419 tid_agg_rx->stored_mpdu_num = 0;
1420 status = WLAN_STATUS_SUCCESS;
1421end:
Johannes Berg07346f812008-05-03 01:02:02 +02001422 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001423
1424end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001425 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1426 dialog_token, status, 1, buf_size, timeout);
1427 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001428}
Jiri Bencf0706e82007-05-05 11:45:53 -07001429
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001430static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1431 struct ieee80211_mgmt *mgmt,
1432 size_t len)
1433{
1434 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1435 struct ieee80211_hw *hw = &local->hw;
1436 struct sta_info *sta;
1437 u16 capab;
1438 u16 tid;
1439 u8 *state;
1440
Johannes Bergd0709a62008-02-25 16:27:46 +01001441 rcu_read_lock();
1442
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001443 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001444 if (!sta) {
1445 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001446 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001447 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001448
1449 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1450 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1451
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001452 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001453
Johannes Berg07346f812008-05-03 01:02:02 +02001454 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001455
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001456 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001457 spin_unlock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001458 goto addba_resp_exit;
1459 }
1460
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001461 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001462 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Johannes Berg07346f812008-05-03 01:02:02 +02001463 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001464#ifdef CONFIG_MAC80211_HT_DEBUG
1465 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1466#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001467 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001468 }
1469
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001470 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001471#ifdef CONFIG_MAC80211_HT_DEBUG
1472 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1473#endif /* CONFIG_MAC80211_HT_DEBUG */
1474 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1475 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001476 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001477 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001478
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001479 if (*state == HT_AGG_STATE_OPERATIONAL)
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001480 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001481
Johannes Berg07346f812008-05-03 01:02:02 +02001482 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001483 } else {
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001484 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001485 /* this will allow the state check in stop_BA_session */
1486 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001487 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001488 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1489 WLAN_BACK_INITIATOR);
1490 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001491
1492addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001493 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001494}
1495
1496void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1497 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001498{
1499 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1500 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1501 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1502 struct sk_buff *skb;
1503 struct ieee80211_mgmt *mgmt;
1504 u16 params;
1505
Ester Kummer3acea5b2008-04-17 16:05:14 -07001506 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001507
1508 if (!skb) {
1509 printk(KERN_ERR "%s: failed to allocate buffer "
1510 "for delba frame\n", dev->name);
1511 return;
1512 }
1513
1514 skb_reserve(skb, local->hw.extra_tx_headroom);
1515 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1516 memset(mgmt, 0, 24);
1517 memcpy(mgmt->da, da, ETH_ALEN);
1518 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001519 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001520 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1521 else
1522 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1523 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1524 IEEE80211_STYPE_ACTION);
1525
1526 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1527
1528 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1529 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1530 params = (u16)(initiator << 11); /* bit 11 initiator */
1531 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1532
1533 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1534 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1535
1536 ieee80211_sta_tx(dev, skb, 0);
1537}
1538
1539void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1540 u16 initiator, u16 reason)
1541{
1542 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1543 struct ieee80211_hw *hw = &local->hw;
1544 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001545 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001546 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001547
Johannes Bergd0709a62008-02-25 16:27:46 +01001548 rcu_read_lock();
1549
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001550 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001551 if (!sta) {
1552 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001553 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001554 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001555
1556 /* check if TID is in operational state */
Johannes Berg07346f812008-05-03 01:02:02 +02001557 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001558 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001559 != HT_AGG_STATE_OPERATIONAL) {
Johannes Berg07346f812008-05-03 01:02:02 +02001560 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +01001561 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001562 return;
1563 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001564 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001565 HT_AGG_STATE_REQ_STOP_BA_MSK |
1566 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Johannes Berg07346f812008-05-03 01:02:02 +02001567 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001568
1569 /* stop HW Rx aggregation. ampdu_action existence
1570 * already verified in session init so we add the BUG_ON */
1571 BUG_ON(!local->ops->ampdu_action);
1572
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001573#ifdef CONFIG_MAC80211_HT_DEBUG
1574 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1575 print_mac(mac, ra), tid);
1576#endif /* CONFIG_MAC80211_HT_DEBUG */
1577
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001578 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001579 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001580 if (ret)
1581 printk(KERN_DEBUG "HW problem - can not stop rx "
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001582 "aggregation for tid %d\n", tid);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001583
1584 /* shutdown timer has not expired */
1585 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001586 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001587
1588 /* check if this is a self generated aggregation halt */
1589 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1590 ieee80211_send_delba(dev, ra, tid, 0, reason);
1591
1592 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001593 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1594 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001595 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001596 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1597 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1598 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001599 }
1600 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001601 /* free resources */
1602 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1603 kfree(sta->ampdu_mlme.tid_rx[tid]);
1604 sta->ampdu_mlme.tid_rx[tid] = NULL;
1605 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001606
Johannes Bergd0709a62008-02-25 16:27:46 +01001607 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001608}
1609
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001610
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001611static void ieee80211_sta_process_delba(struct net_device *dev,
1612 struct ieee80211_mgmt *mgmt, size_t len)
1613{
1614 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1615 struct sta_info *sta;
1616 u16 tid, params;
1617 u16 initiator;
1618 DECLARE_MAC_BUF(mac);
1619
Johannes Bergd0709a62008-02-25 16:27:46 +01001620 rcu_read_lock();
1621
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001622 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001623 if (!sta) {
1624 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001625 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001626 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001627
1628 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1629 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1630 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1631
1632#ifdef CONFIG_MAC80211_HT_DEBUG
1633 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001634 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1635 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001636 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001637 mgmt->u.action.u.delba.reason_code);
1638#endif /* CONFIG_MAC80211_HT_DEBUG */
1639
1640 if (initiator == WLAN_BACK_INITIATOR)
1641 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1642 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001643 else { /* WLAN_BACK_RECIPIENT */
Johannes Berg07346f812008-05-03 01:02:02 +02001644 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001645 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001646 HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001647 spin_unlock_bh(&sta->lock);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001648 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1649 WLAN_BACK_RECIPIENT);
1650 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001651 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001652}
1653
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001654/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001655 * After sending add Block Ack request we activated a timer until
1656 * add Block Ack response will arrive from the recipient.
1657 * If this timer expires sta_addba_resp_timer_expired will be executed.
1658 */
1659void sta_addba_resp_timer_expired(unsigned long data)
1660{
1661 /* not an elegant detour, but there is no choice as the timer passes
1662 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001663 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001664 * array gives the sta through container_of */
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301665 u16 tid = *(u8 *)data;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001666 struct sta_info *temp_sta = container_of((void *)data,
1667 struct sta_info, timer_to_tid[tid]);
1668
1669 struct ieee80211_local *local = temp_sta->local;
1670 struct ieee80211_hw *hw = &local->hw;
1671 struct sta_info *sta;
1672 u8 *state;
1673
Johannes Bergd0709a62008-02-25 16:27:46 +01001674 rcu_read_lock();
1675
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001676 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001677 if (!sta) {
1678 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001679 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001680 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001681
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001682 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001683 /* check if the TID waits for addBA response */
Johannes Berg07346f812008-05-03 01:02:02 +02001684 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001685 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001686 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001687 *state = HT_AGG_STATE_IDLE;
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001688#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001689 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1690 "expecting addBA response there", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001691#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001692 goto timer_expired_exit;
1693 }
1694
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001695#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001696 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001697#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001698
1699 /* go through the state check in stop_BA_session */
1700 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001701 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001702 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1703 WLAN_BACK_INITIATOR);
1704
1705timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001706 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001707}
1708
1709/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001710 * After accepting the AddBA Request we activated a timer,
1711 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001712 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1713 */
Adrian Bunk712590d2008-04-21 11:47:51 +03001714static void sta_rx_agg_session_timer_expired(unsigned long data)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001715{
1716 /* not an elegant detour, but there is no choice as the timer passes
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301717 * only one argument, and various sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001718 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001719 * array gives the sta through container_of */
1720 u8 *ptid = (u8 *)data;
1721 u8 *timer_to_id = ptid - *ptid;
1722 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1723 timer_to_tid[0]);
1724
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001725#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001726 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001727#endif
Johannes Bergd0709a62008-02-25 16:27:46 +01001728 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1729 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001730 WLAN_REASON_QSTA_TIMEOUT);
1731}
1732
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001733void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1734{
1735 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1736 int i;
1737
1738 for (i = 0; i < STA_TID_NUM; i++) {
1739 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1740 WLAN_BACK_INITIATOR);
1741 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1742 WLAN_BACK_RECIPIENT,
1743 WLAN_REASON_QSTA_LEAVE_QBSS);
1744 }
1745}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001746
Assaf Kraussb6623482008-06-16 16:09:49 +03001747static void ieee80211_send_refuse_measurement_request(struct net_device *dev,
1748 struct ieee80211_msrment_ie *request_ie,
1749 const u8 *da, const u8 *bssid,
1750 u8 dialog_token)
1751{
1752 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1753 struct sk_buff *skb;
1754 struct ieee80211_mgmt *msr_report;
1755
1756 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1757 sizeof(struct ieee80211_msrment_ie));
1758
1759 if (!skb) {
1760 printk(KERN_ERR "%s: failed to allocate buffer for "
1761 "measurement report frame\n", dev->name);
1762 return;
1763 }
1764
1765 skb_reserve(skb, local->hw.extra_tx_headroom);
1766 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1767 memset(msr_report, 0, 24);
1768 memcpy(msr_report->da, da, ETH_ALEN);
1769 memcpy(msr_report->sa, dev->dev_addr, ETH_ALEN);
1770 memcpy(msr_report->bssid, bssid, ETH_ALEN);
1771 msr_report->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1772 IEEE80211_STYPE_ACTION);
1773
1774 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1775 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1776 msr_report->u.action.u.measurement.action_code =
1777 WLAN_ACTION_SPCT_MSR_RPRT;
1778 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1779
1780 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1781 msr_report->u.action.u.measurement.length =
1782 sizeof(struct ieee80211_msrment_ie);
1783
1784 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1785 sizeof(struct ieee80211_msrment_ie));
1786 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1787 msr_report->u.action.u.measurement.msr_elem.mode |=
1788 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1789 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1790
1791 ieee80211_sta_tx(dev, skb, 0);
1792}
1793
1794static void ieee80211_sta_process_measurement_req(struct net_device *dev,
1795 struct ieee80211_mgmt *mgmt,
1796 size_t len)
1797{
1798 /*
1799 * Ignoring measurement request is spec violation.
1800 * Mandatory measurements must be reported optional
1801 * measurements might be refused or reported incapable
1802 * For now just refuse
1803 * TODO: Answer basic measurement as unmeasured
1804 */
1805 ieee80211_send_refuse_measurement_request(dev,
1806 &mgmt->u.action.u.measurement.msr_elem,
1807 mgmt->sa, mgmt->bssid,
1808 mgmt->u.action.u.measurement.dialog_token);
1809}
1810
1811
Jiri Bencf0706e82007-05-05 11:45:53 -07001812static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1813 struct ieee80211_if_sta *ifsta,
1814 struct ieee80211_mgmt *mgmt,
1815 size_t len)
1816{
1817 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1818 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001819 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001820
1821 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001822 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07001823 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001824
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001825 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07001826 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001827
Johannes Berg51fb61e2007-12-19 01:31:27 +01001828 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001829 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001830 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001831
Johannes Berg51fb61e2007-12-19 01:31:27 +01001832 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001833 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001834 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001835
1836 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1837 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1838 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1839
Johannes Berg51fb61e2007-12-19 01:31:27 +01001840 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg135a2112008-06-16 20:55:29 +02001841 /*
1842 * IEEE 802.11 standard does not require authentication in IBSS
Jiri Bencf0706e82007-05-05 11:45:53 -07001843 * networks and most implementations do not seem to use it.
1844 * However, try to reply to authentication attempts if someone
1845 * has actually implemented this.
Johannes Berg135a2112008-06-16 20:55:29 +02001846 */
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001847 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
Jiri Bencf0706e82007-05-05 11:45:53 -07001848 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001849 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1850 }
1851
1852 if (auth_alg != ifsta->auth_alg ||
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001853 auth_transaction != ifsta->auth_transaction)
Jiri Bencf0706e82007-05-05 11:45:53 -07001854 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001855
1856 if (status_code != WLAN_STATUS_SUCCESS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001857 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1858 u8 algs[3];
1859 const int num_algs = ARRAY_SIZE(algs);
1860 int i, pos;
1861 algs[0] = algs[1] = algs[2] = 0xff;
1862 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1863 algs[0] = WLAN_AUTH_OPEN;
1864 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1865 algs[1] = WLAN_AUTH_SHARED_KEY;
1866 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1867 algs[2] = WLAN_AUTH_LEAP;
1868 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1869 pos = 0;
1870 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1871 pos = 1;
1872 else
1873 pos = 2;
1874 for (i = 0; i < num_algs; i++) {
1875 pos++;
1876 if (pos >= num_algs)
1877 pos = 0;
1878 if (algs[pos] == ifsta->auth_alg ||
1879 algs[pos] == 0xff)
1880 continue;
1881 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1882 !ieee80211_sta_wep_configured(dev))
1883 continue;
1884 ifsta->auth_alg = algs[pos];
Jiri Bencf0706e82007-05-05 11:45:53 -07001885 break;
1886 }
1887 }
1888 return;
1889 }
1890
1891 switch (ifsta->auth_alg) {
1892 case WLAN_AUTH_OPEN:
1893 case WLAN_AUTH_LEAP:
1894 ieee80211_auth_completed(dev, ifsta);
1895 break;
1896 case WLAN_AUTH_SHARED_KEY:
1897 if (ifsta->auth_transaction == 4)
1898 ieee80211_auth_completed(dev, ifsta);
1899 else
1900 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1901 break;
1902 }
1903}
1904
1905
1906static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1907 struct ieee80211_if_sta *ifsta,
1908 struct ieee80211_mgmt *mgmt,
1909 size_t len)
1910{
1911 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001912 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001913
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001914 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07001915 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001916
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001917 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07001918 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001919
1920 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1921
Johannes Berg988c0f72008-04-17 19:21:22 +02001922 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001923 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001924
1925 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1926 ifsta->state == IEEE80211_ASSOCIATE ||
1927 ifsta->state == IEEE80211_ASSOCIATED) {
1928 ifsta->state = IEEE80211_AUTHENTICATE;
1929 mod_timer(&ifsta->timer, jiffies +
1930 IEEE80211_RETRY_AUTH_INTERVAL);
1931 }
1932
1933 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001934 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001935}
1936
1937
1938static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1939 struct ieee80211_if_sta *ifsta,
1940 struct ieee80211_mgmt *mgmt,
1941 size_t len)
1942{
1943 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001944 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001945
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001946 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07001947 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001948
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001949 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07001950 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001951
1952 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1953
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001954 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001955 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1956
1957 if (ifsta->state == IEEE80211_ASSOCIATED) {
1958 ifsta->state = IEEE80211_ASSOCIATE;
1959 mod_timer(&ifsta->timer, jiffies +
1960 IEEE80211_RETRY_AUTH_INTERVAL);
1961 }
1962
1963 ieee80211_set_disassoc(dev, ifsta, 0);
1964}
1965
1966
Johannes Berg471b3ef2007-12-28 14:32:58 +01001967static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001968 struct ieee80211_if_sta *ifsta,
1969 struct ieee80211_mgmt *mgmt,
1970 size_t len,
1971 int reassoc)
1972{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001973 struct ieee80211_local *local = sdata->local;
1974 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01001975 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001976 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001977 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001978 u16 capab_info, status_code, aid;
1979 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001980 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001981 u8 *pos;
1982 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001983 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001984 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001985
1986 /* AssocResp and ReassocResp have identical structure, so process both
1987 * of them in this function. */
1988
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001989 if (ifsta->state != IEEE80211_ASSOCIATE)
Jiri Bencf0706e82007-05-05 11:45:53 -07001990 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001991
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001992 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07001993 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001994
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001995 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001996 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001997
1998 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1999 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2000 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07002001
Joe Perches0795af52007-10-03 17:59:30 -07002002 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07002003 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07002004 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02002005 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07002006
2007 if (status_code != WLAN_STATUS_SUCCESS) {
2008 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
2009 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02002010 /* if this was a reassociation, ensure we try a "full"
2011 * association next time. This works around some broken APs
2012 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002013 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07002014 return;
2015 }
2016
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002017 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2018 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
2019 "set\n", dev->name, aid);
2020 aid &= ~(BIT(15) | BIT(14));
2021
Jiri Bencf0706e82007-05-05 11:45:53 -07002022 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04002023 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002024
2025 if (!elems.supp_rates) {
2026 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
2027 dev->name);
2028 return;
2029 }
2030
2031 printk(KERN_DEBUG "%s: associated\n", dev->name);
2032 ifsta->aid = aid;
2033 ifsta->ap_capab = capab_info;
2034
2035 kfree(ifsta->assocresp_ies);
2036 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002037 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002038 if (ifsta->assocresp_ies)
2039 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2040
Johannes Bergd0709a62008-02-25 16:27:46 +01002041 rcu_read_lock();
2042
Jiri Bencf0706e82007-05-05 11:45:53 -07002043 /* Add STA entry for the AP */
2044 sta = sta_info_get(local, ifsta->bssid);
2045 if (!sta) {
2046 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01002047 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01002048
Johannes Berg73651ee2008-02-25 16:27:47 +01002049 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2050 if (!sta) {
2051 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2052 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01002053 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002054 return;
2055 }
John W. Linville65c107a2007-10-05 14:23:27 -04002056 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01002057 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002058 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002059 if (bss) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002060 sta->last_signal = bss->signal;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002061 sta->last_qual = bss->qual;
Jiri Bencf0706e82007-05-05 11:45:53 -07002062 sta->last_noise = bss->noise;
2063 ieee80211_rx_bss_put(dev, bss);
2064 }
Johannes Berg73651ee2008-02-25 16:27:47 +01002065
2066 err = sta_info_insert(sta);
2067 if (err) {
2068 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2069 " the AP (error %d)\n", dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01002070 rcu_read_unlock();
2071 return;
2072 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002073 }
2074
Johannes Berg73651ee2008-02-25 16:27:47 +01002075 /*
2076 * FIXME: Do we really need to update the sta_info's information here?
2077 * We already know about the AP (we found it in our list) so it
2078 * should already be filled with the right info, no?
2079 * As is stands, all this is racy because typically we assume
2080 * the information that is filled in here (except flags) doesn't
2081 * change while a STA structure is alive. As such, it should move
2082 * to between the sta_info_alloc() and sta_info_insert() above.
2083 */
2084
Johannes Berg07346f812008-05-03 01:02:02 +02002085 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2086 WLAN_STA_AUTHORIZED);
Jiri Bencf0706e82007-05-05 11:45:53 -07002087
2088 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002089 basic_rates = 0;
2090 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2091
Jiri Bencf0706e82007-05-05 11:45:53 -07002092 for (i = 0; i < elems.supp_rates_len; i++) {
2093 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002094
2095 if (rate > 110)
2096 have_higher_than_11mbit = true;
2097
2098 for (j = 0; j < sband->n_bitrates; j++) {
2099 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002100 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002101 if (elems.supp_rates[i] & 0x80)
2102 basic_rates |= BIT(j);
2103 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002104 }
Johannes Berg8318d782008-01-24 19:38:38 +01002105
Jiri Bencf0706e82007-05-05 11:45:53 -07002106 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2107 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002108
2109 if (rate > 110)
2110 have_higher_than_11mbit = true;
2111
2112 for (j = 0; j < sband->n_bitrates; j++) {
2113 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002114 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002115 if (elems.ext_supp_rates[i] & 0x80)
2116 basic_rates |= BIT(j);
2117 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002118 }
Johannes Berg8318d782008-01-24 19:38:38 +01002119
2120 sta->supp_rates[local->hw.conf.channel->band] = rates;
2121 sdata->basic_rates = basic_rates;
2122
2123 /* cf. IEEE 802.11 9.2.12 */
2124 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2125 have_higher_than_11mbit)
2126 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2127 else
2128 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002129
Johannes Berg3434fbd2008-05-03 00:59:37 +02002130 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2131 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002132 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002133 ieee80211_ht_cap_ie_to_ht_info(
2134 (struct ieee80211_ht_cap *)
2135 elems.ht_cap_elem, &sta->ht_info);
2136 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2137 (struct ieee80211_ht_addt_info *)
2138 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002139 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002140 }
2141
Jiri Bencf0706e82007-05-05 11:45:53 -07002142 rate_control_rate_init(sta, local);
2143
Johannes Berg3434fbd2008-05-03 00:59:37 +02002144 if (elems.wmm_param) {
Johannes Berg07346f812008-05-03 01:02:02 +02002145 set_sta_flags(sta, WLAN_STA_WME);
Johannes Berge5f98f22008-03-05 20:39:31 +01002146 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002147 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2148 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002149 } else
2150 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002151
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002152 /* set AID and assoc capability,
2153 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002154 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002155 bss_conf->assoc_capability = capab_info;
Johannes Berg8318d782008-01-24 19:38:38 +01002156 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002157
Jiri Bencf0706e82007-05-05 11:45:53 -07002158 ieee80211_associated(dev, ifsta);
2159}
2160
2161
2162/* Caller must hold local->sta_bss_lock */
2163static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2164 struct ieee80211_sta_bss *bss)
2165{
2166 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002167 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002168
2169 if (bss_mesh_cfg(bss))
2170 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2171 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002172 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002173 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002174
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002175 bss->hnext = local->sta_bss_hash[hash_idx];
2176 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002177}
2178
2179
2180/* Caller must hold local->sta_bss_lock */
2181static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2182 struct ieee80211_sta_bss *bss)
2183{
2184 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2185 struct ieee80211_sta_bss *b, *prev = NULL;
2186 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2187 while (b) {
2188 if (b == bss) {
2189 if (!prev)
2190 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2191 bss->hnext;
2192 else
2193 prev->hnext = bss->hnext;
2194 break;
2195 }
2196 prev = b;
2197 b = b->hnext;
2198 }
2199}
2200
2201
2202static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002203ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002204 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002205{
2206 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2207 struct ieee80211_sta_bss *bss;
2208
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002209 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002210 if (!bss)
2211 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002212 atomic_inc(&bss->users);
2213 atomic_inc(&bss->users);
2214 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002215 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002216 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2217 memcpy(bss->ssid, ssid, ssid_len);
2218 bss->ssid_len = ssid_len;
2219 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002220
2221 spin_lock_bh(&local->sta_bss_lock);
2222 /* TODO: order by RSSI? */
2223 list_add_tail(&bss->list, &local->sta_bss_list);
2224 __ieee80211_rx_bss_hash_add(dev, bss);
2225 spin_unlock_bh(&local->sta_bss_lock);
2226 return bss;
2227}
2228
Jiri Bencf0706e82007-05-05 11:45:53 -07002229static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002230ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002231 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002232{
2233 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2234 struct ieee80211_sta_bss *bss;
2235
2236 spin_lock_bh(&local->sta_bss_lock);
2237 bss = local->sta_bss_hash[STA_HASH(bssid)];
2238 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002239 if (!bss_mesh_cfg(bss) &&
2240 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002241 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002242 bss->ssid_len == ssid_len &&
2243 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002244 atomic_inc(&bss->users);
2245 break;
2246 }
2247 bss = bss->hnext;
2248 }
2249 spin_unlock_bh(&local->sta_bss_lock);
2250 return bss;
2251}
2252
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002253#ifdef CONFIG_MAC80211_MESH
2254static struct ieee80211_sta_bss *
2255ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2256 u8 *mesh_cfg, int freq)
2257{
2258 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2259 struct ieee80211_sta_bss *bss;
2260
2261 spin_lock_bh(&local->sta_bss_lock);
2262 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2263 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002264 if (bss_mesh_cfg(bss) &&
2265 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002266 bss->freq == freq &&
2267 mesh_id_len == bss->mesh_id_len &&
2268 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2269 mesh_id_len))) {
2270 atomic_inc(&bss->users);
2271 break;
2272 }
2273 bss = bss->hnext;
2274 }
2275 spin_unlock_bh(&local->sta_bss_lock);
2276 return bss;
2277}
2278
2279static struct ieee80211_sta_bss *
2280ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002281 u8 *mesh_cfg, int mesh_config_len, int freq)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002282{
2283 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2284 struct ieee80211_sta_bss *bss;
2285
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002286 if (mesh_config_len != MESH_CFG_LEN)
2287 return NULL;
2288
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002289 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2290 if (!bss)
2291 return NULL;
2292
John W. Linville24736702008-04-08 14:15:46 -04002293 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002294 if (!bss->mesh_cfg) {
2295 kfree(bss);
2296 return NULL;
2297 }
2298
2299 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2300 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2301 if (!bss->mesh_id) {
2302 kfree(bss->mesh_cfg);
2303 kfree(bss);
2304 return NULL;
2305 }
2306 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2307 }
2308
2309 atomic_inc(&bss->users);
2310 atomic_inc(&bss->users);
John W. Linville24736702008-04-08 14:15:46 -04002311 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002312 bss->mesh_id_len = mesh_id_len;
2313 bss->freq = freq;
2314 spin_lock_bh(&local->sta_bss_lock);
2315 /* TODO: order by RSSI? */
2316 list_add_tail(&bss->list, &local->sta_bss_list);
2317 __ieee80211_rx_bss_hash_add(dev, bss);
2318 spin_unlock_bh(&local->sta_bss_lock);
2319 return bss;
2320}
2321#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002322
2323static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2324{
2325 kfree(bss->wpa_ie);
2326 kfree(bss->rsn_ie);
2327 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002328 kfree(bss->ht_ie);
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002329 kfree(bss->ht_add_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002330 kfree(bss_mesh_id(bss));
2331 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002332 kfree(bss);
2333}
2334
2335
2336static void ieee80211_rx_bss_put(struct net_device *dev,
2337 struct ieee80211_sta_bss *bss)
2338{
2339 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -07002340
Pavel Emelyanov1ebebea2008-04-23 11:47:15 +04002341 local_bh_disable();
2342 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2343 local_bh_enable();
2344 return;
2345 }
2346
Jiri Bencf0706e82007-05-05 11:45:53 -07002347 __ieee80211_rx_bss_hash_del(dev, bss);
2348 list_del(&bss->list);
2349 spin_unlock_bh(&local->sta_bss_lock);
2350 ieee80211_rx_bss_free(bss);
2351}
2352
2353
2354void ieee80211_rx_bss_list_init(struct net_device *dev)
2355{
2356 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2357 spin_lock_init(&local->sta_bss_lock);
2358 INIT_LIST_HEAD(&local->sta_bss_list);
2359}
2360
2361
2362void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2363{
2364 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2365 struct ieee80211_sta_bss *bss, *tmp;
2366
2367 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2368 ieee80211_rx_bss_put(dev, bss);
2369}
2370
2371
Bruno Randolfa6072682008-02-18 11:21:15 +09002372static int ieee80211_sta_join_ibss(struct net_device *dev,
2373 struct ieee80211_if_sta *ifsta,
2374 struct ieee80211_sta_bss *bss)
2375{
2376 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2377 int res, rates, i, j;
2378 struct sk_buff *skb;
2379 struct ieee80211_mgmt *mgmt;
Johannes Berge039fa42008-05-15 12:55:29 +02002380 struct ieee80211_tx_info *control;
Bruno Randolfa6072682008-02-18 11:21:15 +09002381 struct rate_selection ratesel;
2382 u8 *pos;
2383 struct ieee80211_sub_if_data *sdata;
2384 struct ieee80211_supported_band *sband;
Dan Williams507b06d2008-06-03 23:39:55 -04002385 union iwreq_data wrqu;
Bruno Randolfa6072682008-02-18 11:21:15 +09002386
2387 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2388
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002389 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2390
Bruno Randolfa6072682008-02-18 11:21:15 +09002391 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002392 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002393
2394 if (local->ops->reset_tsf) {
2395 /* Reset own TSF to allow time synchronization work. */
2396 local->ops->reset_tsf(local_to_hw(local));
2397 }
2398 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2399 res = ieee80211_if_config(dev);
2400 if (res)
2401 return res;
2402
2403 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2404
Bruno Randolfa6072682008-02-18 11:21:15 +09002405 sdata->drop_unencrypted = bss->capability &
2406 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2407
Assaf Kraussbe038b32008-06-05 19:55:21 +03002408 res = ieee80211_set_freq(dev, bss->freq);
Bruno Randolfa6072682008-02-18 11:21:15 +09002409
Assaf Kraussbe038b32008-06-05 19:55:21 +03002410 if (res)
2411 return res;
Bruno Randolfa6072682008-02-18 11:21:15 +09002412
2413 /* Set beacon template */
2414 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2415 do {
2416 if (!skb)
2417 break;
2418
2419 skb_reserve(skb, local->hw.extra_tx_headroom);
2420
2421 mgmt = (struct ieee80211_mgmt *)
2422 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2423 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2424 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2425 IEEE80211_STYPE_BEACON);
2426 memset(mgmt->da, 0xff, ETH_ALEN);
2427 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2428 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2429 mgmt->u.beacon.beacon_int =
2430 cpu_to_le16(local->hw.conf.beacon_int);
Assaf Krauss4faeb862008-06-30 17:23:16 +08002431 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
Bruno Randolfa6072682008-02-18 11:21:15 +09002432 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2433
2434 pos = skb_put(skb, 2 + ifsta->ssid_len);
2435 *pos++ = WLAN_EID_SSID;
2436 *pos++ = ifsta->ssid_len;
2437 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2438
2439 rates = bss->supp_rates_len;
2440 if (rates > 8)
2441 rates = 8;
2442 pos = skb_put(skb, 2 + rates);
2443 *pos++ = WLAN_EID_SUPP_RATES;
2444 *pos++ = rates;
2445 memcpy(pos, bss->supp_rates, rates);
2446
2447 if (bss->band == IEEE80211_BAND_2GHZ) {
2448 pos = skb_put(skb, 2 + 1);
2449 *pos++ = WLAN_EID_DS_PARAMS;
2450 *pos++ = 1;
2451 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2452 }
2453
2454 pos = skb_put(skb, 2 + 2);
2455 *pos++ = WLAN_EID_IBSS_PARAMS;
2456 *pos++ = 2;
2457 /* FIX: set ATIM window based on scan results */
2458 *pos++ = 0;
2459 *pos++ = 0;
2460
2461 if (bss->supp_rates_len > 8) {
2462 rates = bss->supp_rates_len - 8;
2463 pos = skb_put(skb, 2 + rates);
2464 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2465 *pos++ = rates;
2466 memcpy(pos, &bss->supp_rates[8], rates);
2467 }
2468
Johannes Berge039fa42008-05-15 12:55:29 +02002469 control = IEEE80211_SKB_CB(skb);
2470
Bruno Randolfa6072682008-02-18 11:21:15 +09002471 rate_control_get_rate(dev, sband, skb, &ratesel);
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002472 if (ratesel.rate_idx < 0) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002473 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2474 "for IBSS beacon\n", dev->name);
2475 break;
2476 }
Johannes Berge039fa42008-05-15 12:55:29 +02002477 control->control.vif = &sdata->vif;
2478 control->tx_rate_idx = ratesel.rate_idx;
Bruno Randolfa6072682008-02-18 11:21:15 +09002479 if (sdata->bss_conf.use_short_preamble &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002480 sband->bitrates[ratesel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
Johannes Berge039fa42008-05-15 12:55:29 +02002481 control->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
2482 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2483 control->flags |= IEEE80211_TX_CTL_NO_ACK;
Ivo van Doornb2898a22008-07-01 10:45:13 +02002484 control->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
Johannes Berge039fa42008-05-15 12:55:29 +02002485 control->control.retry_limit = 1;
Bruno Randolfa6072682008-02-18 11:21:15 +09002486
2487 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2488 if (ifsta->probe_resp) {
2489 mgmt = (struct ieee80211_mgmt *)
2490 ifsta->probe_resp->data;
2491 mgmt->frame_control =
2492 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2493 IEEE80211_STYPE_PROBE_RESP);
2494 } else {
2495 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2496 "template for IBSS\n", dev->name);
2497 }
2498
2499 if (local->ops->beacon_update &&
Johannes Berge039fa42008-05-15 12:55:29 +02002500 local->ops->beacon_update(local_to_hw(local), skb) == 0) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002501 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2502 "template\n", dev->name);
2503 skb = NULL;
2504 }
2505
2506 rates = 0;
2507 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2508 for (i = 0; i < bss->supp_rates_len; i++) {
2509 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2510 for (j = 0; j < sband->n_bitrates; j++)
2511 if (sband->bitrates[j].bitrate == bitrate)
2512 rates |= BIT(j);
2513 }
2514 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Vladimir Koutnye2839d82008-03-18 21:14:07 +01002515
2516 ieee80211_sta_def_wmm_params(dev, bss, 1);
Bruno Randolfa6072682008-02-18 11:21:15 +09002517 } while (0);
2518
2519 if (skb) {
2520 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2521 "template\n", dev->name);
2522 dev_kfree_skb(skb);
2523 }
2524
2525 ifsta->state = IEEE80211_IBSS_JOINED;
2526 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2527
Dan Williams507b06d2008-06-03 23:39:55 -04002528 memset(&wrqu, 0, sizeof(wrqu));
2529 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2530 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Bruno Randolfa6072682008-02-18 11:21:15 +09002531
2532 return res;
2533}
2534
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002535u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2536 struct ieee802_11_elems *elems,
2537 enum ieee80211_band band)
2538{
2539 struct ieee80211_supported_band *sband;
2540 struct ieee80211_rate *bitrates;
2541 size_t num_rates;
2542 u64 supp_rates;
2543 int i, j;
2544 sband = local->hw.wiphy->bands[band];
2545
2546 if (!sband) {
2547 WARN_ON(1);
2548 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2549 }
2550
2551 bitrates = sband->bitrates;
2552 num_rates = sband->n_bitrates;
2553 supp_rates = 0;
2554 for (i = 0; i < elems->supp_rates_len +
2555 elems->ext_supp_rates_len; i++) {
2556 u8 rate = 0;
2557 int own_rate;
2558 if (i < elems->supp_rates_len)
2559 rate = elems->supp_rates[i];
2560 else if (elems->ext_supp_rates)
2561 rate = elems->ext_supp_rates
2562 [i - elems->supp_rates_len];
2563 own_rate = 5 * (rate & 0x7f);
2564 for (j = 0; j < num_rates; j++)
2565 if (bitrates[j].bitrate == own_rate)
2566 supp_rates |= BIT(j);
2567 }
2568 return supp_rates;
2569}
2570
Bruno Randolfa6072682008-02-18 11:21:15 +09002571
Jiri Bencf0706e82007-05-05 11:45:53 -07002572static void ieee80211_rx_bss_info(struct net_device *dev,
2573 struct ieee80211_mgmt *mgmt,
2574 size_t len,
2575 struct ieee80211_rx_status *rx_status,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002576 struct ieee802_11_elems *elems,
Jiri Bencf0706e82007-05-05 11:45:53 -07002577 int beacon)
2578{
2579 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01002580 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002581 struct ieee80211_sta_bss *bss;
2582 struct sta_info *sta;
2583 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002584 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002585 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002586 DECLARE_MAC_BUF(mac);
2587 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002588
2589 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2590 return; /* ignore ProbeResp to foreign address */
2591
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002592 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
Jiri Bencf0706e82007-05-05 11:45:53 -07002593
Ester Kummerae6a44e2008-06-27 18:54:48 +03002594 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
2595 elems->mesh_config && mesh_matches_local(elems, dev)) {
2596 u64 rates = ieee80211_sta_get_rates(local, elems,
Johannes Berg902acc72008-02-23 15:17:19 +01002597 rx_status->band);
2598
2599 mesh_neighbour_update(mgmt->sa, rates, dev,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002600 mesh_peer_accepts_plinks(elems, dev));
Johannes Berg902acc72008-02-23 15:17:19 +01002601 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002602
Johannes Bergd0709a62008-02-25 16:27:46 +01002603 rcu_read_lock();
2604
Ester Kummerae6a44e2008-06-27 18:54:48 +03002605 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002606 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2607 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002608 u64 prev_rates;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002609 u64 supp_rates = ieee80211_sta_get_rates(local, elems,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002610 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002611
Johannes Berg8318d782008-01-24 19:38:38 +01002612 prev_rates = sta->supp_rates[rx_status->band];
2613 sta->supp_rates[rx_status->band] &= supp_rates;
2614 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002615 /* No matching rates - this should not really happen.
2616 * Make sure that at least one rate is marked
2617 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002618 sta->supp_rates[rx_status->band] =
2619 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002620 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002621 }
2622
Johannes Bergd0709a62008-02-25 16:27:46 +01002623 rcu_read_unlock();
2624
Ester Kummerae6a44e2008-06-27 18:54:48 +03002625 if (elems->ds_params && elems->ds_params_len == 1)
2626 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002627 else
Johannes Berg8318d782008-01-24 19:38:38 +01002628 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002629
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002630 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2631
2632 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2633 return;
2634
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002635#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002636 if (elems->mesh_config)
2637 bss = ieee80211_rx_mesh_bss_get(dev, elems->mesh_id,
2638 elems->mesh_id_len, elems->mesh_config, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002639 else
2640#endif
2641 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002642 elems->ssid, elems->ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002643 if (!bss) {
2644#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002645 if (elems->mesh_config)
2646 bss = ieee80211_rx_mesh_bss_add(dev, elems->mesh_id,
2647 elems->mesh_id_len, elems->mesh_config,
2648 elems->mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002649 else
2650#endif
2651 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002652 elems->ssid, elems->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002653 if (!bss)
2654 return;
2655 } else {
2656#if 0
2657 /* TODO: order by RSSI? */
2658 spin_lock_bh(&local->sta_bss_lock);
2659 list_move_tail(&bss->list, &local->sta_bss_list);
2660 spin_unlock_bh(&local->sta_bss_lock);
2661#endif
2662 }
2663
Daniel Drake56282212007-07-10 19:32:10 +02002664 /* save the ERP value so that it is available at association time */
Ester Kummerae6a44e2008-06-27 18:54:48 +03002665 if (elems->erp_info && elems->erp_info_len >= 1) {
2666 bss->erp_value = elems->erp_info[0];
Daniel Drake56282212007-07-10 19:32:10 +02002667 bss->has_erp_value = 1;
2668 }
2669
Ester Kummerae6a44e2008-06-27 18:54:48 +03002670 if (elems->ht_cap_elem &&
2671 (!bss->ht_ie || bss->ht_ie_len != elems->ht_cap_elem_len ||
2672 memcmp(bss->ht_ie, elems->ht_cap_elem, elems->ht_cap_elem_len))) {
Johannes Berg30b89b02008-04-16 17:43:20 +02002673 kfree(bss->ht_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002674 bss->ht_ie = kmalloc(elems->ht_cap_elem_len + 2, GFP_ATOMIC);
Johannes Berg30b89b02008-04-16 17:43:20 +02002675 if (bss->ht_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002676 memcpy(bss->ht_ie, elems->ht_cap_elem - 2,
2677 elems->ht_cap_elem_len + 2);
2678 bss->ht_ie_len = elems->ht_cap_elem_len + 2;
Johannes Berg30b89b02008-04-16 17:43:20 +02002679 } else
2680 bss->ht_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002681 } else if (!elems->ht_cap_elem && bss->ht_ie) {
Johannes Berg30b89b02008-04-16 17:43:20 +02002682 kfree(bss->ht_ie);
2683 bss->ht_ie = NULL;
2684 bss->ht_ie_len = 0;
2685 }
2686
Ester Kummerae6a44e2008-06-27 18:54:48 +03002687 if (elems->ht_info_elem &&
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002688 (!bss->ht_add_ie ||
Ester Kummerae6a44e2008-06-27 18:54:48 +03002689 bss->ht_add_ie_len != elems->ht_info_elem_len ||
2690 memcmp(bss->ht_add_ie, elems->ht_info_elem,
2691 elems->ht_info_elem_len))) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002692 kfree(bss->ht_add_ie);
2693 bss->ht_add_ie =
Ester Kummerae6a44e2008-06-27 18:54:48 +03002694 kmalloc(elems->ht_info_elem_len + 2, GFP_ATOMIC);
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002695 if (bss->ht_add_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002696 memcpy(bss->ht_add_ie, elems->ht_info_elem - 2,
2697 elems->ht_info_elem_len + 2);
2698 bss->ht_add_ie_len = elems->ht_info_elem_len + 2;
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002699 } else
2700 bss->ht_add_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002701 } else if (!elems->ht_info_elem && bss->ht_add_ie) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002702 kfree(bss->ht_add_ie);
2703 bss->ht_add_ie = NULL;
2704 bss->ht_add_ie_len = 0;
2705 }
2706
Jiri Bencf0706e82007-05-05 11:45:53 -07002707 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2708 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002709
2710 bss->supp_rates_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002711 if (elems->supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002712 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002713 if (clen > elems->supp_rates_len)
2714 clen = elems->supp_rates_len;
2715 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
Jiri Bencf0706e82007-05-05 11:45:53 -07002716 clen);
2717 bss->supp_rates_len += clen;
2718 }
Ester Kummerae6a44e2008-06-27 18:54:48 +03002719 if (elems->ext_supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002720 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002721 if (clen > elems->ext_supp_rates_len)
2722 clen = elems->ext_supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07002723 memcpy(&bss->supp_rates[bss->supp_rates_len],
Ester Kummerae6a44e2008-06-27 18:54:48 +03002724 elems->ext_supp_rates, clen);
Jiri Bencf0706e82007-05-05 11:45:53 -07002725 bss->supp_rates_len += clen;
2726 }
2727
Johannes Berg30b89b02008-04-16 17:43:20 +02002728 bss->band = rx_status->band;
2729
2730 bss->timestamp = beacon_timestamp;
2731 bss->last_update = jiffies;
Johannes Berg30b89b02008-04-16 17:43:20 +02002732 bss->signal = rx_status->signal;
2733 bss->noise = rx_status->noise;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002734 bss->qual = rx_status->qual;
Johannes Berg30b89b02008-04-16 17:43:20 +02002735 if (!beacon && !bss->probe_resp)
2736 bss->probe_resp = true;
2737
2738 /*
2739 * In STA mode, the remaining parameters should not be overridden
2740 * by beacons because they're not necessarily accurate there.
2741 */
2742 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2743 bss->probe_resp && beacon) {
2744 ieee80211_rx_bss_put(dev, bss);
2745 return;
2746 }
2747
Ester Kummerae6a44e2008-06-27 18:54:48 +03002748 if (elems->wpa &&
2749 (!bss->wpa_ie || bss->wpa_ie_len != elems->wpa_len ||
2750 memcmp(bss->wpa_ie, elems->wpa, elems->wpa_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002751 kfree(bss->wpa_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002752 bss->wpa_ie = kmalloc(elems->wpa_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002753 if (bss->wpa_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002754 memcpy(bss->wpa_ie, elems->wpa - 2, elems->wpa_len + 2);
2755 bss->wpa_ie_len = elems->wpa_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002756 } else
2757 bss->wpa_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002758 } else if (!elems->wpa && bss->wpa_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002759 kfree(bss->wpa_ie);
2760 bss->wpa_ie = NULL;
2761 bss->wpa_ie_len = 0;
2762 }
2763
Ester Kummerae6a44e2008-06-27 18:54:48 +03002764 if (elems->rsn &&
2765 (!bss->rsn_ie || bss->rsn_ie_len != elems->rsn_len ||
2766 memcmp(bss->rsn_ie, elems->rsn, elems->rsn_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002767 kfree(bss->rsn_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002768 bss->rsn_ie = kmalloc(elems->rsn_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002769 if (bss->rsn_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002770 memcpy(bss->rsn_ie, elems->rsn - 2, elems->rsn_len + 2);
2771 bss->rsn_ie_len = elems->rsn_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002772 } else
2773 bss->rsn_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002774 } else if (!elems->rsn && bss->rsn_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002775 kfree(bss->rsn_ie);
2776 bss->rsn_ie = NULL;
2777 bss->rsn_ie_len = 0;
2778 }
2779
Johannes Berg30b89b02008-04-16 17:43:20 +02002780 /*
2781 * Cf.
2782 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2783 *
2784 * quoting:
2785 *
2786 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2787 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2788 * Alliance (September 1, 2004) is incorporated by reference herein.
2789 * The inclusion of the WMM Parameters in probe responses and
2790 * association responses is mandatory for WMM enabled networks. The
2791 * inclusion of the WMM Parameters in beacons, however, is optional.
2792 */
2793
Ester Kummerae6a44e2008-06-27 18:54:48 +03002794 if (elems->wmm_param &&
2795 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_param_len ||
2796 memcmp(bss->wmm_ie, elems->wmm_param, elems->wmm_param_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002797 kfree(bss->wmm_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002798 bss->wmm_ie = kmalloc(elems->wmm_param_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002799 if (bss->wmm_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002800 memcpy(bss->wmm_ie, elems->wmm_param - 2,
2801 elems->wmm_param_len + 2);
2802 bss->wmm_ie_len = elems->wmm_param_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002803 } else
2804 bss->wmm_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002805 } else if (elems->wmm_info &&
2806 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_info_len ||
2807 memcmp(bss->wmm_ie, elems->wmm_info,
2808 elems->wmm_info_len))) {
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002809 /* As for certain AP's Fifth bit is not set in WMM IE in
2810 * beacon frames.So while parsing the beacon frame the
2811 * wmm_info structure is used instead of wmm_param.
2812 * wmm_info structure was never used to set bss->wmm_ie.
2813 * This code fixes this problem by copying the WME
2814 * information from wmm_info to bss->wmm_ie and enabling
2815 * n-band association.
2816 */
2817 kfree(bss->wmm_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002818 bss->wmm_ie = kmalloc(elems->wmm_info_len + 2, GFP_ATOMIC);
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002819 if (bss->wmm_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002820 memcpy(bss->wmm_ie, elems->wmm_info - 2,
2821 elems->wmm_info_len + 2);
2822 bss->wmm_ie_len = elems->wmm_info_len + 2;
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002823 } else
2824 bss->wmm_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002825 } else if (!elems->wmm_param && !elems->wmm_info && bss->wmm_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002826 kfree(bss->wmm_ie);
2827 bss->wmm_ie = NULL;
2828 bss->wmm_ie_len = 0;
2829 }
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002830
2831 /* check if we need to merge IBSS */
2832 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2833 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002834 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002835 bss->freq == local->oper_channel->center_freq &&
Ester Kummerae6a44e2008-06-27 18:54:48 +03002836 elems->ssid_len == sdata->u.sta.ssid_len &&
2837 memcmp(elems->ssid, sdata->u.sta.ssid,
2838 sdata->u.sta.ssid_len) == 0) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002839 if (rx_status->flag & RX_FLAG_TSFT) {
2840 /* in order for correct IBSS merging we need mactime
2841 *
2842 * since mactime is defined as the time the first data
2843 * symbol of the frame hits the PHY, and the timestamp
2844 * of the beacon is defined as "the time that the data
2845 * symbol containing the first bit of the timestamp is
2846 * transmitted to the PHY plus the transmitting STA’s
2847 * delays through its local PHY from the MAC-PHY
2848 * interface to its interface with the WM"
2849 * (802.11 11.1.2) - equals the time this bit arrives at
2850 * the receiver - we have to take into account the
2851 * offset between the two.
2852 * e.g: at 1 MBit that means mactime is 192 usec earlier
2853 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2854 */
2855 int rate = local->hw.wiphy->bands[rx_status->band]->
2856 bitrates[rx_status->rate_idx].bitrate;
2857 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2858 } else if (local && local->ops && local->ops->get_tsf)
2859 /* second best option: get current TSF */
2860 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2861 else
2862 /* can't merge without knowing the TSF */
2863 rx_timestamp = -1LLU;
2864#ifdef CONFIG_MAC80211_IBSS_DEBUG
2865 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2866 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2867 print_mac(mac, mgmt->sa),
2868 print_mac(mac2, mgmt->bssid),
2869 (unsigned long long)rx_timestamp,
2870 (unsigned long long)beacon_timestamp,
2871 (unsigned long long)(rx_timestamp - beacon_timestamp),
2872 jiffies);
2873#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2874 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002875#ifndef CONFIG_MAC80211_IBSS_DEBUG
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002876 printk(KERN_DEBUG "%s: beacon TSF higher than "
2877 "local TSF - IBSS merge with BSSID %s\n",
2878 dev->name, print_mac(mac, mgmt->bssid));
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002879#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002880 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2881 ieee80211_ibss_add_sta(dev, NULL,
Vladimir Koutny87291c02008-06-13 16:50:44 +02002882 mgmt->bssid, mgmt->sa,
2883 BIT(rx_status->rate_idx));
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002884 }
2885 }
2886
Jiri Bencf0706e82007-05-05 11:45:53 -07002887 ieee80211_rx_bss_put(dev, bss);
2888}
2889
2890
2891static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2892 struct ieee80211_mgmt *mgmt,
2893 size_t len,
2894 struct ieee80211_rx_status *rx_status)
2895{
Ester Kummerae6a44e2008-06-27 18:54:48 +03002896 size_t baselen;
2897 struct ieee802_11_elems elems;
2898
2899 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2900 if (baselen > len)
2901 return;
2902
2903 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2904 &elems);
2905
2906 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002907}
2908
2909
2910static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2911 struct ieee80211_mgmt *mgmt,
2912 size_t len,
2913 struct ieee80211_rx_status *rx_status)
2914{
Jiri Bencf0706e82007-05-05 11:45:53 -07002915 struct ieee80211_sub_if_data *sdata;
2916 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002917 size_t baselen;
2918 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002919 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2920 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002921 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002922
Ester Kummerae6a44e2008-06-27 18:54:48 +03002923 /* Process beacon from the current BSS */
2924 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2925 if (baselen > len)
2926 return;
2927
2928 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2929
2930 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002931
2932 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002933 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002934 return;
2935 ifsta = &sdata->u.sta;
2936
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002937 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002938 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2939 return;
2940
Johannes Berg3434fbd2008-05-03 00:59:37 +02002941 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2942 elems.wmm_param_len);
Reinette Chatred18ef292008-04-09 16:56:15 -07002943
2944 /* Do not send changes to driver if we are scanning. This removes
2945 * requirement that driver's bss_info_changed function needs to be
2946 * atomic. */
2947 if (local->sta_sw_scanning || local->sta_hw_scanning)
2948 return;
2949
Daniel Drake56282212007-07-10 19:32:10 +02002950 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002951 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
John W. Linville50c4afb2008-04-15 14:09:27 -04002952 else {
2953 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2954 changed |= ieee80211_handle_protect_preamb(sdata, false,
2955 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2956 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002957
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002958 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07002959 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002960 struct ieee80211_ht_bss_info bss_info;
2961
2962 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2963 (struct ieee80211_ht_addt_info *)
2964 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002965 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2966 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002967 }
2968
Johannes Berg471b3ef2007-12-28 14:32:58 +01002969 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002970}
2971
2972
2973static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2974 struct ieee80211_if_sta *ifsta,
2975 struct ieee80211_mgmt *mgmt,
2976 size_t len,
2977 struct ieee80211_rx_status *rx_status)
2978{
2979 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2980 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2981 int tx_last_beacon;
2982 struct sk_buff *skb;
2983 struct ieee80211_mgmt *resp;
2984 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002985 DECLARE_MAC_BUF(mac);
2986#ifdef CONFIG_MAC80211_IBSS_DEBUG
2987 DECLARE_MAC_BUF(mac2);
2988 DECLARE_MAC_BUF(mac3);
2989#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002990
Johannes Berg51fb61e2007-12-19 01:31:27 +01002991 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002992 ifsta->state != IEEE80211_IBSS_JOINED ||
2993 len < 24 + 2 || !ifsta->probe_resp)
2994 return;
2995
2996 if (local->ops->tx_last_beacon)
2997 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2998 else
2999 tx_last_beacon = 1;
3000
3001#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003002 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
3003 "%s (tx_last_beacon=%d)\n",
3004 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
3005 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07003006#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3007
3008 if (!tx_last_beacon)
3009 return;
3010
3011 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
3012 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
3013 return;
3014
3015 end = ((u8 *) mgmt) + len;
3016 pos = mgmt->u.probe_req.variable;
3017 if (pos[0] != WLAN_EID_SSID ||
3018 pos + 2 + pos[1] > end) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003019#ifdef CONFIG_MAC80211_IBSS_DEBUG
3020 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
3021 "from %s\n",
3022 dev->name, print_mac(mac, mgmt->sa));
3023#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003024 return;
3025 }
3026 if (pos[1] != 0 &&
3027 (pos[1] != ifsta->ssid_len ||
3028 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
3029 /* Ignore ProbeReq for foreign SSID */
3030 return;
3031 }
3032
3033 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02003034 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003035 if (!skb)
3036 return;
3037
3038 resp = (struct ieee80211_mgmt *) skb->data;
3039 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3040#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003041 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
3042 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07003043#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3044 ieee80211_sta_tx(dev, skb, 0);
3045}
3046
Johannes Berg4e20cb22007-12-19 01:31:24 +01003047static void ieee80211_rx_mgmt_action(struct net_device *dev,
3048 struct ieee80211_if_sta *ifsta,
3049 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003050 size_t len,
3051 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003052{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003053 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Assaf Kraussb6623482008-06-16 16:09:49 +03003054 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003055
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003056 if (len < IEEE80211_MIN_ACTION_SIZE)
3057 return;
3058
3059 switch (mgmt->u.action.category) {
Assaf Kraussb6623482008-06-16 16:09:49 +03003060 case WLAN_CATEGORY_SPECTRUM_MGMT:
3061 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
3062 break;
3063 switch (mgmt->u.action.u.chan_switch.action_code) {
3064 case WLAN_ACTION_SPCT_MSR_REQ:
3065 if (len < (IEEE80211_MIN_ACTION_SIZE +
3066 sizeof(mgmt->u.action.u.measurement)))
3067 break;
3068 ieee80211_sta_process_measurement_req(dev, mgmt, len);
3069 break;
3070 }
3071 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003072 case WLAN_CATEGORY_BACK:
3073 switch (mgmt->u.action.u.addba_req.action_code) {
3074 case WLAN_ACTION_ADDBA_REQ:
3075 if (len < (IEEE80211_MIN_ACTION_SIZE +
3076 sizeof(mgmt->u.action.u.addba_req)))
3077 break;
3078 ieee80211_sta_process_addba_request(dev, mgmt, len);
3079 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02003080 case WLAN_ACTION_ADDBA_RESP:
3081 if (len < (IEEE80211_MIN_ACTION_SIZE +
3082 sizeof(mgmt->u.action.u.addba_resp)))
3083 break;
3084 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3085 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02003086 case WLAN_ACTION_DELBA:
3087 if (len < (IEEE80211_MIN_ACTION_SIZE +
3088 sizeof(mgmt->u.action.u.delba)))
3089 break;
3090 ieee80211_sta_process_delba(dev, mgmt, len);
3091 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003092 }
3093 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003094 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003095 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003096 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3097 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003098 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003099 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003100 mesh_rx_path_sel_frame(dev, mgmt, len);
3101 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003102 }
3103}
Jiri Bencf0706e82007-05-05 11:45:53 -07003104
3105void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3106 struct ieee80211_rx_status *rx_status)
3107{
3108 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3109 struct ieee80211_sub_if_data *sdata;
3110 struct ieee80211_if_sta *ifsta;
3111 struct ieee80211_mgmt *mgmt;
3112 u16 fc;
3113
3114 if (skb->len < 24)
3115 goto fail;
3116
3117 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3118 ifsta = &sdata->u.sta;
3119
3120 mgmt = (struct ieee80211_mgmt *) skb->data;
3121 fc = le16_to_cpu(mgmt->frame_control);
3122
3123 switch (fc & IEEE80211_FCTL_STYPE) {
3124 case IEEE80211_STYPE_PROBE_REQ:
3125 case IEEE80211_STYPE_PROBE_RESP:
3126 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003127 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07003128 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3129 case IEEE80211_STYPE_AUTH:
3130 case IEEE80211_STYPE_ASSOC_RESP:
3131 case IEEE80211_STYPE_REASSOC_RESP:
3132 case IEEE80211_STYPE_DEAUTH:
3133 case IEEE80211_STYPE_DISASSOC:
3134 skb_queue_tail(&ifsta->skb_queue, skb);
3135 queue_work(local->hw.workqueue, &ifsta->work);
3136 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003137 }
3138
3139 fail:
3140 kfree_skb(skb);
3141}
3142
3143
3144static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3145 struct sk_buff *skb)
3146{
3147 struct ieee80211_rx_status *rx_status;
3148 struct ieee80211_sub_if_data *sdata;
3149 struct ieee80211_if_sta *ifsta;
3150 struct ieee80211_mgmt *mgmt;
3151 u16 fc;
3152
3153 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3154 ifsta = &sdata->u.sta;
3155
3156 rx_status = (struct ieee80211_rx_status *) skb->cb;
3157 mgmt = (struct ieee80211_mgmt *) skb->data;
3158 fc = le16_to_cpu(mgmt->frame_control);
3159
3160 switch (fc & IEEE80211_FCTL_STYPE) {
3161 case IEEE80211_STYPE_PROBE_REQ:
3162 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3163 rx_status);
3164 break;
3165 case IEEE80211_STYPE_PROBE_RESP:
3166 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3167 break;
3168 case IEEE80211_STYPE_BEACON:
3169 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3170 break;
3171 case IEEE80211_STYPE_AUTH:
3172 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3173 break;
3174 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003175 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003176 break;
3177 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003178 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003179 break;
3180 case IEEE80211_STYPE_DEAUTH:
3181 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3182 break;
3183 case IEEE80211_STYPE_DISASSOC:
3184 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3185 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003186 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003187 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003188 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07003189 }
3190
3191 kfree_skb(skb);
3192}
3193
3194
Johannes Berg9ae54c82008-01-31 19:48:20 +01003195ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003196ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3197 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003198{
3199 struct ieee80211_mgmt *mgmt;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003200 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003201
Zhu Yiece8edd2007-11-22 10:53:21 +08003202 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003203 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003204
3205 mgmt = (struct ieee80211_mgmt *) skb->data;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003206 fc = mgmt->frame_control;
Jiri Bencf0706e82007-05-05 11:45:53 -07003207
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003208 if (ieee80211_is_ctl(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +01003209 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003210
3211 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003212 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003213
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003214 if (ieee80211_is_probe_resp(fc)) {
3215 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3216 dev_kfree_skb(skb);
3217 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003218 }
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003219
3220 if (ieee80211_is_beacon(fc)) {
3221 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3222 dev_kfree_skb(skb);
3223 return RX_QUEUED;
3224 }
3225
Johannes Berg9ae54c82008-01-31 19:48:20 +01003226 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003227}
3228
3229
3230static int ieee80211_sta_active_ibss(struct net_device *dev)
3231{
3232 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3233 int active = 0;
3234 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003235 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003236
Johannes Bergd0709a62008-02-25 16:27:46 +01003237 rcu_read_lock();
3238
3239 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3240 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003241 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3242 jiffies)) {
3243 active++;
3244 break;
3245 }
3246 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003247
3248 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003249
3250 return active;
3251}
3252
3253
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003254static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003255{
3256 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3257 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003258 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003259 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003260 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003261
Johannes Bergd0709a62008-02-25 16:27:46 +01003262 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003263 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003264 if (time_after(jiffies, sta->last_rx + exp_time)) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003265#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003266 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3267 dev->name, print_mac(mac, sta->addr));
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003268#endif
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003269 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003270 if (sta)
3271 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003272 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003273 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003274
Johannes Bergd0709a62008-02-25 16:27:46 +01003275 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3276 sta_info_destroy(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003277}
3278
3279
3280static void ieee80211_sta_merge_ibss(struct net_device *dev,
3281 struct ieee80211_if_sta *ifsta)
3282{
3283 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3284
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003285 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07003286 if (ieee80211_sta_active_ibss(dev))
3287 return;
3288
3289 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3290 "IBSS networks with same SSID (merge)\n", dev->name);
3291 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3292}
3293
3294
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003295#ifdef CONFIG_MAC80211_MESH
3296static void ieee80211_mesh_housekeeping(struct net_device *dev,
3297 struct ieee80211_if_sta *ifsta)
3298{
3299 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3300 bool free_plinks;
3301
3302 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3303 mesh_path_expire(dev);
3304
3305 free_plinks = mesh_plink_availables(sdata);
3306 if (free_plinks != sdata->u.sta.accepting_plinks)
3307 ieee80211_if_config_beacon(dev);
3308
3309 mod_timer(&ifsta->timer, jiffies +
3310 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3311}
3312
3313
3314void ieee80211_start_mesh(struct net_device *dev)
3315{
3316 struct ieee80211_if_sta *ifsta;
3317 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3318 ifsta = &sdata->u.sta;
3319 ifsta->state = IEEE80211_MESH_UP;
3320 ieee80211_sta_timer((unsigned long)sdata);
3321}
3322#endif
3323
3324
Jiri Bencf0706e82007-05-05 11:45:53 -07003325void ieee80211_sta_timer(unsigned long data)
3326{
3327 struct ieee80211_sub_if_data *sdata =
3328 (struct ieee80211_sub_if_data *) data;
3329 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3330 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3331
3332 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3333 queue_work(local->hw.workqueue, &ifsta->work);
3334}
3335
Jiri Bencf0706e82007-05-05 11:45:53 -07003336void ieee80211_sta_work(struct work_struct *work)
3337{
3338 struct ieee80211_sub_if_data *sdata =
3339 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3340 struct net_device *dev = sdata->dev;
3341 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3342 struct ieee80211_if_sta *ifsta;
3343 struct sk_buff *skb;
3344
3345 if (!netif_running(dev))
3346 return;
3347
Zhu Yiece8edd2007-11-22 10:53:21 +08003348 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003349 return;
3350
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003351 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3352 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3353 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
Jiri Bencf0706e82007-05-05 11:45:53 -07003354 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003355 ifsta = &sdata->u.sta;
3356
3357 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3358 ieee80211_sta_rx_queued_mgmt(dev, skb);
3359
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003360#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003361 if (ifsta->preq_queue_len &&
3362 time_after(jiffies,
3363 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003364 mesh_path_start_discovery(dev);
3365#endif
3366
Jiri Bencf0706e82007-05-05 11:45:53 -07003367 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3368 ifsta->state != IEEE80211_ASSOCIATE &&
3369 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003370 if (ifsta->scan_ssid_len)
3371 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3372 else
3373 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003374 return;
3375 }
3376
3377 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3378 if (ieee80211_sta_config_auth(dev, ifsta))
3379 return;
3380 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3381 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3382 return;
3383
3384 switch (ifsta->state) {
3385 case IEEE80211_DISABLED:
3386 break;
3387 case IEEE80211_AUTHENTICATE:
3388 ieee80211_authenticate(dev, ifsta);
3389 break;
3390 case IEEE80211_ASSOCIATE:
3391 ieee80211_associate(dev, ifsta);
3392 break;
3393 case IEEE80211_ASSOCIATED:
3394 ieee80211_associated(dev, ifsta);
3395 break;
3396 case IEEE80211_IBSS_SEARCH:
3397 ieee80211_sta_find_ibss(dev, ifsta);
3398 break;
3399 case IEEE80211_IBSS_JOINED:
3400 ieee80211_sta_merge_ibss(dev, ifsta);
3401 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003402#ifdef CONFIG_MAC80211_MESH
3403 case IEEE80211_MESH_UP:
3404 ieee80211_mesh_housekeeping(dev, ifsta);
3405 break;
3406#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003407 default:
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003408 WARN_ON(1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003409 break;
3410 }
3411
3412 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3413 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3414 "mixed-cell disabled - disassociate\n", dev->name);
3415
3416 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3417 ieee80211_set_disassoc(dev, ifsta, 0);
3418 }
3419}
3420
3421
3422static void ieee80211_sta_reset_auth(struct net_device *dev,
3423 struct ieee80211_if_sta *ifsta)
3424{
3425 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3426
3427 if (local->ops->reset_tsf) {
3428 /* Reset own TSF to allow time synchronization work. */
3429 local->ops->reset_tsf(local_to_hw(local));
3430 }
3431
3432 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3433
3434
3435 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3436 ifsta->auth_alg = WLAN_AUTH_OPEN;
3437 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3438 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3439 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3440 ifsta->auth_alg = WLAN_AUTH_LEAP;
3441 else
3442 ifsta->auth_alg = WLAN_AUTH_OPEN;
Jiri Bencf0706e82007-05-05 11:45:53 -07003443 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003444 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3445 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003446 netif_carrier_off(dev);
3447}
3448
3449
3450void ieee80211_sta_req_auth(struct net_device *dev,
3451 struct ieee80211_if_sta *ifsta)
3452{
3453 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3454 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3455
Johannes Berg51fb61e2007-12-19 01:31:27 +01003456 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003457 return;
3458
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003459 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3460 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3461 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3462 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003463 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3464 queue_work(local->hw.workqueue, &ifsta->work);
3465 }
3466}
3467
3468static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3469 const char *ssid, int ssid_len)
3470{
3471 int tmp, hidden_ssid;
3472
Michael Wu48225702007-10-19 17:14:36 -04003473 if (ssid_len == ifsta->ssid_len &&
3474 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003475 return 1;
3476
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003477 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003478 return 0;
3479
3480 hidden_ssid = 1;
3481 tmp = ssid_len;
3482 while (tmp--) {
3483 if (ssid[tmp] != '\0') {
3484 hidden_ssid = 0;
3485 break;
3486 }
3487 }
3488
3489 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3490 return 1;
3491
3492 if (ssid_len == 1 && ssid[0] == ' ')
3493 return 1;
3494
3495 return 0;
3496}
3497
3498static int ieee80211_sta_config_auth(struct net_device *dev,
3499 struct ieee80211_if_sta *ifsta)
3500{
3501 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3502 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3503 struct ieee80211_sta_bss *bss, *selected = NULL;
3504 int top_rssi = 0, freq;
3505
Jiri Bencf0706e82007-05-05 11:45:53 -07003506 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003507 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003508 list_for_each_entry(bss, &local->sta_bss_list, list) {
3509 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3510 continue;
3511
Abhijeet Kolekar34a961f2008-05-09 09:35:41 -07003512 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3513 IEEE80211_STA_AUTO_BSSID_SEL |
3514 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3515 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3516 !!sdata->default_key))
Jiri Bencf0706e82007-05-05 11:45:53 -07003517 continue;
3518
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003519 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3520 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003521 continue;
3522
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003523 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003524 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3525 continue;
3526
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003527 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003528 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3529 continue;
3530
Bruno Randolf566bfe52008-05-08 19:15:40 +02003531 if (!selected || top_rssi < bss->signal) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003532 selected = bss;
Bruno Randolf566bfe52008-05-08 19:15:40 +02003533 top_rssi = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07003534 }
3535 }
3536 if (selected)
3537 atomic_inc(&selected->users);
3538 spin_unlock_bh(&local->sta_bss_lock);
3539
3540 if (selected) {
Assaf Kraussbe038b32008-06-05 19:55:21 +03003541 ieee80211_set_freq(dev, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003542 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003543 ieee80211_sta_set_ssid(dev, selected->ssid,
3544 selected->ssid_len);
3545 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003546 ieee80211_sta_def_wmm_params(dev, selected, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003547 ieee80211_rx_bss_put(dev, selected);
3548 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003549 ieee80211_sta_reset_auth(dev, ifsta);
3550 return 0;
3551 } else {
3552 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003553 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003554 ieee80211_sta_start_scan(dev, NULL, 0);
3555 else
3556 ieee80211_sta_start_scan(dev, ifsta->ssid,
3557 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003558 ifsta->state = IEEE80211_AUTHENTICATE;
3559 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3560 } else
3561 ifsta->state = IEEE80211_DISABLED;
3562 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003563 return -1;
3564}
3565
Jiri Bencf0706e82007-05-05 11:45:53 -07003566
3567static int ieee80211_sta_create_ibss(struct net_device *dev,
3568 struct ieee80211_if_sta *ifsta)
3569{
3570 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3571 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003572 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003573 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003574 u8 bssid[ETH_ALEN], *pos;
3575 int i;
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003576 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003577 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003578
3579#if 0
3580 /* Easier testing, use fixed BSSID. */
3581 memset(bssid, 0xfe, ETH_ALEN);
3582#else
3583 /* Generate random, not broadcast, locally administered BSSID. Mix in
3584 * own MAC address to make sure that devices that do not have proper
3585 * random number generator get different BSSID. */
3586 get_random_bytes(bssid, ETH_ALEN);
3587 for (i = 0; i < ETH_ALEN; i++)
3588 bssid[i] ^= dev->dev_addr[i];
3589 bssid[0] &= ~0x01;
3590 bssid[0] |= 0x02;
3591#endif
3592
Joe Perches0795af52007-10-03 17:59:30 -07003593 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3594 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003595
Johannes Berg8318d782008-01-24 19:38:38 +01003596 bss = ieee80211_rx_bss_add(dev, bssid,
3597 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003598 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003599 if (!bss)
3600 return -ENOMEM;
3601
Johannes Berg8318d782008-01-24 19:38:38 +01003602 bss->band = local->hw.conf.channel->band;
3603 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003604
3605 if (local->hw.conf.beacon_int == 0)
Tomas Winklerdc0ae302008-06-12 22:38:37 +03003606 local->hw.conf.beacon_int = 100;
Jiri Bencf0706e82007-05-05 11:45:53 -07003607 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003608 bss->last_update = jiffies;
3609 bss->capability = WLAN_CAPABILITY_IBSS;
Johannes Berg988c0f72008-04-17 19:21:22 +02003610
3611 if (sdata->default_key)
Jiri Bencf0706e82007-05-05 11:45:53 -07003612 bss->capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +02003613 else
Jiri Bencf0706e82007-05-05 11:45:53 -07003614 sdata->drop_unencrypted = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02003615
Johannes Berg8318d782008-01-24 19:38:38 +01003616 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003617 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003618 for (i = 0; i < sband->n_bitrates; i++) {
3619 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003620 *pos++ = (u8) (rate / 5);
3621 }
3622
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003623 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3624 ieee80211_rx_bss_put(dev, bss);
3625 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003626}
3627
3628
3629static int ieee80211_sta_find_ibss(struct net_device *dev,
3630 struct ieee80211_if_sta *ifsta)
3631{
3632 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3633 struct ieee80211_sta_bss *bss;
3634 int found = 0;
3635 u8 bssid[ETH_ALEN];
3636 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003637 DECLARE_MAC_BUF(mac);
3638 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003639
3640 if (ifsta->ssid_len == 0)
3641 return -EINVAL;
3642
3643 active_ibss = ieee80211_sta_active_ibss(dev);
3644#ifdef CONFIG_MAC80211_IBSS_DEBUG
3645 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3646 dev->name, active_ibss);
3647#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3648 spin_lock_bh(&local->sta_bss_lock);
3649 list_for_each_entry(bss, &local->sta_bss_list, list) {
3650 if (ifsta->ssid_len != bss->ssid_len ||
3651 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3652 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3653 continue;
3654#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003655 printk(KERN_DEBUG " bssid=%s found\n",
3656 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003657#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3658 memcpy(bssid, bss->bssid, ETH_ALEN);
3659 found = 1;
3660 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3661 break;
3662 }
3663 spin_unlock_bh(&local->sta_bss_lock);
3664
3665#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003666 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3667 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003668#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3669 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003670 (bss = ieee80211_rx_bss_get(dev, bssid,
3671 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003672 ifsta->ssid, ifsta->ssid_len))) {
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003673 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003674 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003675 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003676 dev->name, print_mac(mac, bssid));
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003677 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3678 ieee80211_rx_bss_put(dev, bss);
3679 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003680 }
3681#ifdef CONFIG_MAC80211_IBSS_DEBUG
3682 printk(KERN_DEBUG " did not try to join ibss\n");
3683#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3684
3685 /* Selected IBSS not found in current scan results - try to scan */
3686 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3687 !ieee80211_sta_active_ibss(dev)) {
3688 mod_timer(&ifsta->timer, jiffies +
3689 IEEE80211_IBSS_MERGE_INTERVAL);
3690 } else if (time_after(jiffies, local->last_scan_completed +
3691 IEEE80211_SCAN_INTERVAL)) {
3692 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3693 "join\n", dev->name);
3694 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3695 ifsta->ssid_len);
3696 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3697 int interval = IEEE80211_SCAN_INTERVAL;
3698
3699 if (time_after(jiffies, ifsta->ibss_join_req +
3700 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003701 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003702 (!(local->oper_channel->flags &
3703 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003704 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003705 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003706 printk(KERN_DEBUG "%s: IBSS not allowed on"
3707 " %d MHz\n", dev->name,
3708 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003709 }
3710
3711 /* No IBSS found - decrease scan interval and continue
3712 * scanning. */
3713 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3714 }
3715
3716 ifsta->state = IEEE80211_IBSS_SEARCH;
3717 mod_timer(&ifsta->timer, jiffies + interval);
3718 return 0;
3719 }
3720
3721 return 0;
3722}
3723
3724
3725int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3726{
Johannes Berg8318d782008-01-24 19:38:38 +01003727 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003728 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07003729
3730 if (len > IEEE80211_MAX_SSID_LEN)
3731 return -EINVAL;
3732
Jiri Bencf0706e82007-05-05 11:45:53 -07003733 ifsta = &sdata->u.sta;
3734
3735 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003736 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003737 memcpy(ifsta->ssid, ssid, len);
3738 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3739 ifsta->ssid_len = len;
3740
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003741 if (len)
3742 ifsta->flags |= IEEE80211_STA_SSID_SET;
3743 else
3744 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003745 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003746 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003747 ifsta->ibss_join_req = jiffies;
3748 ifsta->state = IEEE80211_IBSS_SEARCH;
3749 return ieee80211_sta_find_ibss(dev, ifsta);
3750 }
3751 return 0;
3752}
3753
3754
3755int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3756{
3757 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3758 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3759 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3760 *len = ifsta->ssid_len;
3761 return 0;
3762}
3763
3764
3765int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3766{
3767 struct ieee80211_sub_if_data *sdata;
3768 struct ieee80211_if_sta *ifsta;
3769 int res;
3770
3771 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3772 ifsta = &sdata->u.sta;
3773
3774 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3775 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3776 res = ieee80211_if_config(dev);
3777 if (res) {
3778 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3779 "the low-level driver\n", dev->name);
3780 return res;
3781 }
3782 }
3783
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003784 if (is_valid_ether_addr(bssid))
3785 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003786 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003787 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3788
Jiri Bencf0706e82007-05-05 11:45:53 -07003789 return 0;
3790}
3791
3792
3793static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3794 struct ieee80211_sub_if_data *sdata,
3795 int powersave)
3796{
3797 struct sk_buff *skb;
3798 struct ieee80211_hdr *nullfunc;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003799 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003800
3801 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3802 if (!skb) {
3803 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3804 "frame\n", sdata->dev->name);
3805 return;
3806 }
3807 skb_reserve(skb, local->hw.extra_tx_headroom);
3808
3809 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3810 memset(nullfunc, 0, 24);
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003811 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3812 IEEE80211_FCTL_TODS);
Jiri Bencf0706e82007-05-05 11:45:53 -07003813 if (powersave)
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003814 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3815 nullfunc->frame_control = fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003816 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3817 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3818 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3819
3820 ieee80211_sta_tx(sdata->dev, skb, 0);
3821}
3822
3823
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003824static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3825{
3826 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3827 ieee80211_vif_is_mesh(&sdata->vif))
3828 ieee80211_sta_timer((unsigned long)sdata);
3829}
3830
Jiri Bencf0706e82007-05-05 11:45:53 -07003831void ieee80211_scan_completed(struct ieee80211_hw *hw)
3832{
3833 struct ieee80211_local *local = hw_to_local(hw);
3834 struct net_device *dev = local->scan_dev;
3835 struct ieee80211_sub_if_data *sdata;
3836 union iwreq_data wrqu;
3837
3838 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003839 memset(&wrqu, 0, sizeof(wrqu));
3840 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003841
Zhu Yiece8edd2007-11-22 10:53:21 +08003842 if (local->sta_hw_scanning) {
3843 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003844 if (ieee80211_hw_config(local))
3845 printk(KERN_DEBUG "%s: failed to restore operational "
3846 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003847 /* Restart STA timer for HW scan case */
3848 rcu_read_lock();
3849 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3850 ieee80211_restart_sta_timer(sdata);
3851 rcu_read_unlock();
3852
Zhu Yiece8edd2007-11-22 10:53:21 +08003853 goto done;
3854 }
3855
3856 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003857 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003858 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003859 "channel after scan\n", dev->name);
3860
Johannes Berg4150c572007-09-17 01:29:23 -04003861
3862 netif_tx_lock_bh(local->mdev);
3863 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3864 local->ops->configure_filter(local_to_hw(local),
3865 FIF_BCN_PRBRESP_PROMISC,
3866 &local->filter_flags,
3867 local->mdev->mc_count,
3868 local->mdev->mc_list);
3869
3870 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003871
Johannes Berg79010422007-09-18 17:29:21 -04003872 rcu_read_lock();
3873 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003874
3875 /* No need to wake the master device. */
3876 if (sdata->dev == local->mdev)
3877 continue;
3878
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003879 /* Tell AP we're back */
3880 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3881 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3882 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003883
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003884 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003885
Jiri Bencf0706e82007-05-05 11:45:53 -07003886 netif_wake_queue(sdata->dev);
3887 }
Johannes Berg79010422007-09-18 17:29:21 -04003888 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003889
Zhu Yiece8edd2007-11-22 10:53:21 +08003890done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003891 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003892 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003893 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003894 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003895 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3896 !ieee80211_sta_active_ibss(dev)))
3897 ieee80211_sta_find_ibss(dev, ifsta);
3898 }
3899}
3900EXPORT_SYMBOL(ieee80211_scan_completed);
3901
3902void ieee80211_sta_scan_work(struct work_struct *work)
3903{
3904 struct ieee80211_local *local =
3905 container_of(work, struct ieee80211_local, scan_work.work);
3906 struct net_device *dev = local->scan_dev;
3907 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003908 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003909 struct ieee80211_channel *chan;
3910 int skip;
3911 unsigned long next_delay = 0;
3912
Zhu Yiece8edd2007-11-22 10:53:21 +08003913 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003914 return;
3915
3916 switch (local->scan_state) {
3917 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003918 /*
3919 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3920 * after we successfully scanned the last channel of the last
3921 * band (and the last band is supported by the hw)
3922 */
Johannes Berg8318d782008-01-24 19:38:38 +01003923 if (local->scan_band < IEEE80211_NUM_BANDS)
3924 sband = local->hw.wiphy->bands[local->scan_band];
3925 else
3926 sband = NULL;
3927
Johannes Berg69d464d2008-01-28 17:08:56 +01003928 /*
3929 * If we are at an unsupported band and have more bands
3930 * left to scan, advance to the next supported one.
3931 */
3932 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003933 local->scan_band++;
3934 sband = local->hw.wiphy->bands[local->scan_band];
3935 local->scan_channel_idx = 0;
3936 }
3937
Johannes Berg69d464d2008-01-28 17:08:56 +01003938 /* if no more bands/channels left, complete scan */
3939 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003940 ieee80211_scan_completed(local_to_hw(local));
3941 return;
3942 }
Johannes Berg8318d782008-01-24 19:38:38 +01003943 skip = 0;
3944 chan = &sband->channels[local->scan_channel_idx];
3945
3946 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003947 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003948 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003949 skip = 1;
3950
3951 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003952 local->scan_channel = chan;
3953 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003954 printk(KERN_DEBUG "%s: failed to set freq to "
3955 "%d MHz for scan\n", dev->name,
3956 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003957 skip = 1;
3958 }
3959 }
3960
Johannes Berg69d464d2008-01-28 17:08:56 +01003961 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003962 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003963 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003964 /*
3965 * scan_band may end up == IEEE80211_NUM_BANDS, but
3966 * we'll catch that case above and complete the scan
3967 * if that is the case.
3968 */
Johannes Berg8318d782008-01-24 19:38:38 +01003969 local->scan_band++;
3970 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003971 }
3972
3973 if (skip)
3974 break;
3975
3976 next_delay = IEEE80211_PROBE_DELAY +
3977 usecs_to_jiffies(local->hw.channel_change_time);
3978 local->scan_state = SCAN_SEND_PROBE;
3979 break;
3980 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003981 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003982 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003983
3984 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3985 break;
3986 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3987 local->scan_ssid_len);
3988 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003989 break;
3990 }
3991
Zhu Yiece8edd2007-11-22 10:53:21 +08003992 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003993 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3994 next_delay);
3995}
3996
3997
3998static int ieee80211_sta_start_scan(struct net_device *dev,
3999 u8 *ssid, size_t ssid_len)
4000{
4001 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4002 struct ieee80211_sub_if_data *sdata;
4003
4004 if (ssid_len > IEEE80211_MAX_SSID_LEN)
4005 return -EINVAL;
4006
4007 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
4008 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
4009 * BSSID: MACAddress
4010 * SSID
4011 * ScanType: ACTIVE, PASSIVE
4012 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
4013 * a Probe frame during active scanning
4014 * ChannelList
4015 * MinChannelTime (>= ProbeDelay), in TU
4016 * MaxChannelTime: (>= MinChannelTime), in TU
4017 */
4018
4019 /* MLME-SCAN.confirm
4020 * BSSDescriptionSet
4021 * ResultCode: SUCCESS, INVALID_PARAMETERS
4022 */
4023
Zhu Yiece8edd2007-11-22 10:53:21 +08004024 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004025 if (local->scan_dev == dev)
4026 return 0;
4027 return -EBUSY;
4028 }
4029
4030 if (local->ops->hw_scan) {
4031 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08004032 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07004033 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08004034 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07004035 local->scan_dev = dev;
4036 }
4037 return rc;
4038 }
4039
Zhu Yiece8edd2007-11-22 10:53:21 +08004040 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07004041
Johannes Berg79010422007-09-18 17:29:21 -04004042 rcu_read_lock();
4043 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02004044
4045 /* Don't stop the master interface, otherwise we can't transmit
4046 * probes! */
4047 if (sdata->dev == local->mdev)
4048 continue;
4049
Jiri Bencf0706e82007-05-05 11:45:53 -07004050 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01004051 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004052 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004053 ieee80211_send_nullfunc(local, sdata, 1);
4054 }
Johannes Berg79010422007-09-18 17:29:21 -04004055 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07004056
4057 if (ssid) {
4058 local->scan_ssid_len = ssid_len;
4059 memcpy(local->scan_ssid, ssid, ssid_len);
4060 } else
4061 local->scan_ssid_len = 0;
4062 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07004063 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004064 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07004065 local->scan_dev = dev;
4066
Johannes Berg4150c572007-09-17 01:29:23 -04004067 netif_tx_lock_bh(local->mdev);
4068 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4069 local->ops->configure_filter(local_to_hw(local),
4070 FIF_BCN_PRBRESP_PROMISC,
4071 &local->filter_flags,
4072 local->mdev->mc_count,
4073 local->mdev->mc_list);
4074 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07004075
4076 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4077 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4078 IEEE80211_CHANNEL_TIME);
4079
4080 return 0;
4081}
4082
4083
4084int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4085{
4086 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4087 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4088 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4089
Johannes Berg51fb61e2007-12-19 01:31:27 +01004090 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004091 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4092
Zhu Yiece8edd2007-11-22 10:53:21 +08004093 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004094 if (local->scan_dev == dev)
4095 return 0;
4096 return -EBUSY;
4097 }
4098
Helmut Schaaa0af5f12007-11-09 16:25:08 +01004099 ifsta->scan_ssid_len = ssid_len;
4100 if (ssid_len)
4101 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07004102 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4103 queue_work(local->hw.workqueue, &ifsta->work);
4104 return 0;
4105}
4106
4107static char *
4108ieee80211_sta_scan_result(struct net_device *dev,
David S. Millerccc58052008-06-16 18:50:49 -07004109 struct iw_request_info *info,
Jiri Bencf0706e82007-05-05 11:45:53 -07004110 struct ieee80211_sta_bss *bss,
4111 char *current_ev, char *end_buf)
4112{
4113 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4114 struct iw_event iwe;
4115
4116 if (time_after(jiffies,
4117 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4118 return current_ev;
4119
Jiri Bencf0706e82007-05-05 11:45:53 -07004120 memset(&iwe, 0, sizeof(iwe));
4121 iwe.cmd = SIOCGIWAP;
4122 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4123 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07004124 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004125 IW_EV_ADDR_LEN);
4126
4127 memset(&iwe, 0, sizeof(iwe));
4128 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01004129 if (bss_mesh_cfg(bss)) {
4130 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004131 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004132 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4133 &iwe, bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004134 } else {
4135 iwe.u.data.length = bss->ssid_len;
4136 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004137 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4138 &iwe, bss->ssid);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004139 }
Jiri Bencf0706e82007-05-05 11:45:53 -07004140
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08004141 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4142 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004143 memset(&iwe, 0, sizeof(iwe));
4144 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01004145 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004146 iwe.u.mode = IW_MODE_MESH;
4147 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004148 iwe.u.mode = IW_MODE_MASTER;
4149 else
4150 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07004151 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4152 &iwe, IW_EV_UINT_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -07004153 }
4154
4155 memset(&iwe, 0, sizeof(iwe));
4156 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01004157 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4158 iwe.u.freq.e = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004159 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004160 IW_EV_FREQ_LEN);
4161
4162 memset(&iwe, 0, sizeof(iwe));
Tomas Winkler9381be02008-05-23 01:36:36 +03004163 iwe.cmd = SIOCGIWFREQ;
4164 iwe.u.freq.m = bss->freq;
4165 iwe.u.freq.e = 6;
David S. Millerccc58052008-06-16 18:50:49 -07004166 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Tomas Winkler9381be02008-05-23 01:36:36 +03004167 IW_EV_FREQ_LEN);
4168 memset(&iwe, 0, sizeof(iwe));
Jiri Bencf0706e82007-05-05 11:45:53 -07004169 iwe.cmd = IWEVQUAL;
Bruno Randolf566bfe52008-05-08 19:15:40 +02004170 iwe.u.qual.qual = bss->qual;
4171 iwe.u.qual.level = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07004172 iwe.u.qual.noise = bss->noise;
4173 iwe.u.qual.updated = local->wstats_flags;
David S. Millerccc58052008-06-16 18:50:49 -07004174 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004175 IW_EV_QUAL_LEN);
4176
4177 memset(&iwe, 0, sizeof(iwe));
4178 iwe.cmd = SIOCGIWENCODE;
4179 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4180 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4181 else
4182 iwe.u.data.flags = IW_ENCODE_DISABLED;
4183 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004184 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4185 &iwe, "");
Jiri Bencf0706e82007-05-05 11:45:53 -07004186
4187 if (bss && bss->wpa_ie) {
4188 memset(&iwe, 0, sizeof(iwe));
4189 iwe.cmd = IWEVGENIE;
4190 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004191 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4192 &iwe, bss->wpa_ie);
Jiri Bencf0706e82007-05-05 11:45:53 -07004193 }
4194
4195 if (bss && bss->rsn_ie) {
4196 memset(&iwe, 0, sizeof(iwe));
4197 iwe.cmd = IWEVGENIE;
4198 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004199 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4200 &iwe, bss->rsn_ie);
Jiri Bencf0706e82007-05-05 11:45:53 -07004201 }
4202
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004203 if (bss && bss->ht_ie) {
4204 memset(&iwe, 0, sizeof(iwe));
4205 iwe.cmd = IWEVGENIE;
4206 iwe.u.data.length = bss->ht_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004207 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4208 &iwe, bss->ht_ie);
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004209 }
4210
Jiri Bencf0706e82007-05-05 11:45:53 -07004211 if (bss && bss->supp_rates_len > 0) {
4212 /* display all supported rates in readable format */
David S. Millerccc58052008-06-16 18:50:49 -07004213 char *p = current_ev + iwe_stream_lcp_len(info);
Jiri Bencf0706e82007-05-05 11:45:53 -07004214 int i;
4215
4216 memset(&iwe, 0, sizeof(iwe));
4217 iwe.cmd = SIOCGIWRATE;
4218 /* Those two flags are ignored... */
4219 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4220
4221 for (i = 0; i < bss->supp_rates_len; i++) {
4222 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4223 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07004224 p = iwe_stream_add_value(info, current_ev, p,
Jiri Bencf0706e82007-05-05 11:45:53 -07004225 end_buf, &iwe, IW_EV_PARAM_LEN);
4226 }
4227 current_ev = p;
4228 }
4229
4230 if (bss) {
4231 char *buf;
4232 buf = kmalloc(30, GFP_ATOMIC);
4233 if (buf) {
4234 memset(&iwe, 0, sizeof(iwe));
4235 iwe.cmd = IWEVCUSTOM;
4236 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4237 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004238 current_ev = iwe_stream_add_point(info, current_ev,
4239 end_buf,
Jiri Bencf0706e82007-05-05 11:45:53 -07004240 &iwe, buf);
Emmanuel Grumbachbf998f62008-06-24 13:38:00 +03004241 memset(&iwe, 0, sizeof(iwe));
4242 iwe.cmd = IWEVCUSTOM;
4243 sprintf(buf, " Last beacon: %dms ago",
4244 jiffies_to_msecs(jiffies - bss->last_update));
4245 iwe.u.data.length = strlen(buf);
4246 current_ev = iwe_stream_add_point(info, current_ev,
4247 end_buf, &iwe, buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004248 kfree(buf);
4249 }
4250 }
4251
Johannes Berg902acc72008-02-23 15:17:19 +01004252 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004253 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04004254 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004255 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004256 if (buf) {
4257 memset(&iwe, 0, sizeof(iwe));
4258 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04004259 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004260 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004261 current_ev = iwe_stream_add_point(info, current_ev,
4262 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004263 &iwe, buf);
4264 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04004265 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4266 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004267 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004268 current_ev = iwe_stream_add_point(info, current_ev,
4269 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004270 &iwe, buf);
4271 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04004272 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4273 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004274 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004275 current_ev = iwe_stream_add_point(info, current_ev,
4276 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004277 &iwe, buf);
4278 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04004279 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4280 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004281 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004282 current_ev = iwe_stream_add_point(info, current_ev,
4283 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004284 &iwe, buf);
4285 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04004286 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4287 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004288 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004289 current_ev = iwe_stream_add_point(info, current_ev,
4290 end_buf,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004291 &iwe, buf);
4292 kfree(buf);
4293 }
4294 }
4295
Jiri Bencf0706e82007-05-05 11:45:53 -07004296 return current_ev;
4297}
4298
4299
David S. Millerccc58052008-06-16 18:50:49 -07004300int ieee80211_sta_scan_results(struct net_device *dev,
4301 struct iw_request_info *info,
4302 char *buf, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07004303{
4304 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4305 char *current_ev = buf;
4306 char *end_buf = buf + len;
4307 struct ieee80211_sta_bss *bss;
4308
4309 spin_lock_bh(&local->sta_bss_lock);
4310 list_for_each_entry(bss, &local->sta_bss_list, list) {
4311 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4312 spin_unlock_bh(&local->sta_bss_lock);
4313 return -E2BIG;
4314 }
David S. Millerccc58052008-06-16 18:50:49 -07004315 current_ev = ieee80211_sta_scan_result(dev, info, bss,
4316 current_ev, end_buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004317 }
4318 spin_unlock_bh(&local->sta_bss_lock);
4319 return current_ev - buf;
4320}
4321
4322
4323int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4324{
4325 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4326 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg988c0f72008-04-17 19:21:22 +02004327
Jiri Bencf0706e82007-05-05 11:45:53 -07004328 kfree(ifsta->extra_ie);
4329 if (len == 0) {
4330 ifsta->extra_ie = NULL;
4331 ifsta->extra_ie_len = 0;
4332 return 0;
4333 }
4334 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4335 if (!ifsta->extra_ie) {
4336 ifsta->extra_ie_len = 0;
4337 return -ENOMEM;
4338 }
4339 memcpy(ifsta->extra_ie, ie, len);
4340 ifsta->extra_ie_len = len;
4341 return 0;
4342}
4343
4344
Johannes Berg988c0f72008-04-17 19:21:22 +02004345struct sta_info *ieee80211_ibss_add_sta(struct net_device *dev,
4346 struct sk_buff *skb, u8 *bssid,
Vladimir Koutny87291c02008-06-13 16:50:44 +02004347 u8 *addr, u64 supp_rates)
Jiri Bencf0706e82007-05-05 11:45:53 -07004348{
4349 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4350 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004351 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004352 DECLARE_MAC_BUF(mac);
Vladimir Koutny87291c02008-06-13 16:50:44 +02004353 int band = local->hw.conf.channel->band;
Jiri Bencf0706e82007-05-05 11:45:53 -07004354
4355 /* TODO: Could consider removing the least recently used entry and
4356 * allow new one to be added. */
4357 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4358 if (net_ratelimit()) {
4359 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004360 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004361 }
4362 return NULL;
4363 }
4364
Vladimir Koutny87291c02008-06-13 16:50:44 +02004365 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid))
4366 return NULL;
4367
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004368#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07004369 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004370 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004371#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07004372
Johannes Berg73651ee2008-02-25 16:27:47 +01004373 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4374 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004375 return NULL;
4376
Johannes Berg07346f812008-05-03 01:02:02 +02004377 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
Johannes Berg238814f2008-01-28 17:19:37 +01004378
Vladimir Koutny87291c02008-06-13 16:50:44 +02004379 if (supp_rates)
4380 sta->supp_rates[band] = supp_rates;
4381 else
4382 sta->supp_rates[band] = sdata->u.sta.supp_rates_bits[band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004383
4384 rate_control_rate_init(sta, local);
4385
Johannes Berg93e5deb2008-04-01 15:21:00 +02004386 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004387 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004388
Johannes Bergd0709a62008-02-25 16:27:46 +01004389 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004390}
4391
4392
4393int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4394{
4395 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4396 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4397
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004398 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07004399 dev->name, reason);
4400
Johannes Berg51fb61e2007-12-19 01:31:27 +01004401 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4402 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004403 return -EINVAL;
4404
4405 ieee80211_send_deauth(dev, ifsta, reason);
4406 ieee80211_set_disassoc(dev, ifsta, 1);
4407 return 0;
4408}
4409
4410
4411int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4412{
4413 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4414 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4415
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004416 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07004417 dev->name, reason);
4418
Johannes Berg51fb61e2007-12-19 01:31:27 +01004419 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004420 return -EINVAL;
4421
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004422 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004423 return -1;
4424
4425 ieee80211_send_disassoc(dev, ifsta, reason);
4426 ieee80211_set_disassoc(dev, ifsta, 0);
4427 return 0;
4428}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004429
4430void ieee80211_notify_mac(struct ieee80211_hw *hw,
4431 enum ieee80211_notification_types notif_type)
4432{
4433 struct ieee80211_local *local = hw_to_local(hw);
4434 struct ieee80211_sub_if_data *sdata;
4435
4436 switch (notif_type) {
4437 case IEEE80211_NOTIFY_RE_ASSOC:
4438 rcu_read_lock();
4439 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4440
4441 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4442 ieee80211_sta_req_auth(sdata->dev,
4443 &sdata->u.sta);
4444 }
4445
4446 }
4447 rcu_read_unlock();
4448 break;
4449 }
4450}
4451EXPORT_SYMBOL(ieee80211_notify_mac);