blob: e1d11c9b6729e0145866ab9df120ada75601fda1 [file] [log] [blame]
Jiri Bencf0706e822007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
Jiri Bencf0706e822007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e822007-05-05 11:45:53 -070020#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010027#include <linux/rtnetlink.h>
Jiri Bencf0706e822007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -070056
57#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
Jiri Bencf0706e822007-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 Bencf0706e822007-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);
Johannes Berg3e122be2008-07-09 14:40:34 +020081static void ieee80211_rx_bss_put(struct ieee80211_local *local,
Jiri Bencf0706e822007-05-05 11:45:53 -070082 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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -070095{
96 size_t left = len;
97 u8 *pos = start;
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700226 default:
Jiri Bencf0706e822007-05-05 11:45:53 -0700227 break;
228 }
229
230 left -= elen;
231 pos += elen;
232 }
Jiri Bencf0706e822007-05-05 11:45:53 -0700233}
234
235
Jiri Bencf0706e822007-05-05 11:45:53 -0700236static int ecw2cw(int ecw)
237{
Johannes Bergac2bf322008-02-14 00:30:35 +0100238 return (1 << ecw) - 1;
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700325 local->wmm_acm |= BIT(0) | BIT(3);
Jiri Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700330 local->wmm_acm |= BIT(4) | BIT(5);
Jiri Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700335 local->wmm_acm |= BIT(6) | BIT(7);
Jiri Bencf0706e822007-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 Bencf0706e822007-05-05 11:45:53 -0700341 local->wmm_acm |= BIT(1) | BIT(2);
Jiri Bencf0706e822007-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 Bergf434b2d2008-07-10 11:22:31 +0200348 params.txop = get_unaligned_le16(pos + 2);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200349#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Jiri Bencf0706e822007-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 Bencf0706e822007-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 Bencf0706e822007-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;
Tomas Winklerebd74482008-07-01 10:44:50 +0300369#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200370 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Joe Perches0795af52007-10-03 17:59:30 -0700371 DECLARE_MAC_BUF(mac);
Tomas Winklerebd74482008-07-01 10:44:50 +0300372#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100373 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200374
Johannes Berg471b3ef2007-12-28 14:32:58 +0100375 if (use_protection != bss_conf->use_cts_prot) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200376#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200377 if (net_ratelimit()) {
378 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700379 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100380 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200381 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700382 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200383 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200384#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100385 bss_conf->use_cts_prot = use_protection;
386 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200387 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200388
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200389 if (use_short_preamble != bss_conf->use_short_preamble) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200390#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake7e9ed182007-07-27 15:43:24 +0200391 if (net_ratelimit()) {
392 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700393 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100394 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200395 use_short_preamble ? "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700396 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200397 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200398#endif
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200399 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100400 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200401 }
Daniel Draked9430a32007-07-27 15:43:24 +0200402
Johannes Berg471b3ef2007-12-28 14:32:58 +0100403 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200404}
405
John W. Linville50c4afb2008-04-15 14:09:27 -0400406static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
407 u8 erp_value)
408{
409 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
410 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
411
412 return ieee80211_handle_protect_preamb(sdata,
413 use_protection, use_short_preamble);
414}
415
416static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
417 struct ieee80211_sta_bss *bss)
418{
419 u32 changed = 0;
420
421 if (bss->has_erp_value)
422 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
423 else {
424 u16 capab = bss->capability;
425 changed |= ieee80211_handle_protect_preamb(sdata, false,
426 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
427 }
428
429 return changed;
430}
431
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200432int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
433 struct ieee80211_ht_info *ht_info)
434{
435
436 if (ht_info == NULL)
437 return -EINVAL;
438
439 memset(ht_info, 0, sizeof(*ht_info));
440
441 if (ht_cap_ie) {
442 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
443
444 ht_info->ht_supported = 1;
445 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
446 ht_info->ampdu_factor =
447 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
448 ht_info->ampdu_density =
449 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
450 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
451 } else
452 ht_info->ht_supported = 0;
453
454 return 0;
455}
456
457int ieee80211_ht_addt_info_ie_to_ht_bss_info(
458 struct ieee80211_ht_addt_info *ht_add_info_ie,
459 struct ieee80211_ht_bss_info *bss_info)
460{
461 if (bss_info == NULL)
462 return -EINVAL;
463
464 memset(bss_info, 0, sizeof(*bss_info));
465
466 if (ht_add_info_ie) {
467 u16 op_mode;
468 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
469
470 bss_info->primary_channel = ht_add_info_ie->control_chan;
471 bss_info->bss_cap = ht_add_info_ie->ht_param;
472 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
473 }
474
475 return 0;
476}
Daniel Drake56282212007-07-10 19:32:10 +0200477
Jiri Bencf0706e822007-05-05 11:45:53 -0700478static void ieee80211_sta_send_associnfo(struct net_device *dev,
479 struct ieee80211_if_sta *ifsta)
480{
481 char *buf;
482 size_t len;
483 int i;
484 union iwreq_data wrqu;
485
486 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
487 return;
488
489 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200490 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -0700491 if (!buf)
492 return;
493
494 len = sprintf(buf, "ASSOCINFO(");
495 if (ifsta->assocreq_ies) {
496 len += sprintf(buf + len, "ReqIEs=");
497 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
498 len += sprintf(buf + len, "%02x",
499 ifsta->assocreq_ies[i]);
500 }
501 }
502 if (ifsta->assocresp_ies) {
503 if (ifsta->assocreq_ies)
504 len += sprintf(buf + len, " ");
505 len += sprintf(buf + len, "RespIEs=");
506 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
507 len += sprintf(buf + len, "%02x",
508 ifsta->assocresp_ies[i]);
509 }
510 }
511 len += sprintf(buf + len, ")");
512
513 if (len > IW_CUSTOM_MAX) {
514 len = sprintf(buf, "ASSOCRESPIE=");
515 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
516 len += sprintf(buf + len, "%02x",
517 ifsta->assocresp_ies[i]);
518 }
519 }
520
521 memset(&wrqu, 0, sizeof(wrqu));
522 wrqu.data.length = len;
523 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
524
525 kfree(buf);
526}
527
528
529static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400530 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200531 bool assoc)
Jiri Bencf0706e822007-05-05 11:45:53 -0700532{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100533 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
534 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700535 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Jiri Bencf0706e822007-05-05 11:45:53 -0700536 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100537 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e822007-05-05 11:45:53 -0700538
Jiri Bencf0706e822007-05-05 11:45:53 -0700539 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200540 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400541
542 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
543
Johannes Berg51fb61e2007-12-19 01:31:27 +0100544 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -0700545 return;
Daniel Drake56282212007-07-10 19:32:10 +0200546
John W. Linville65c107a2007-10-05 14:23:27 -0400547 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Tomas Winkler38668c02008-03-28 16:33:32 -0700548 conf->channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400549 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200550 if (bss) {
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700551 /* set timing information */
552 sdata->bss_conf.beacon_int = bss->beacon_int;
553 sdata->bss_conf.timestamp = bss->timestamp;
Emmanuel Grumbach98f7dfd2008-07-18 13:52:59 +0800554 sdata->bss_conf.dtim_period = bss->dtim_period;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700555
John W. Linville50c4afb2008-04-15 14:09:27 -0400556 changed |= ieee80211_handle_bss_capability(sdata, bss);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700557
Johannes Berg3e122be2008-07-09 14:40:34 +0200558 ieee80211_rx_bss_put(local, bss);
Daniel Drake56282212007-07-10 19:32:10 +0200559 }
560
Tomas Winkler38668c02008-03-28 16:33:32 -0700561 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
562 changed |= BSS_CHANGED_HT;
563 sdata->bss_conf.assoc_ht = 1;
564 sdata->bss_conf.ht_conf = &conf->ht_conf;
565 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
566 }
567
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400568 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -0700569 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
570 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
571 ieee80211_sta_send_associnfo(dev, ifsta);
572 } else {
Guy Cohen8db93692008-07-03 19:56:13 +0300573 netif_carrier_off(dev);
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700574 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400575 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Tomas Winklerfc32f922008-07-03 01:27:13 +0300576 changed |= ieee80211_reset_erp_info(dev);
Tomas Winkler38668c02008-03-28 16:33:32 -0700577
578 sdata->bss_conf.assoc_ht = 0;
579 sdata->bss_conf.ht_conf = NULL;
580 sdata->bss_conf.ht_bss_conf = NULL;
581
Jiri Bencf0706e822007-05-05 11:45:53 -0700582 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
583 }
Jiri Bencf0706e822007-05-05 11:45:53 -0700584 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200585 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100586
Tomas Winklerb2205252008-01-27 16:18:22 +0200587 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100588 ieee80211_bss_info_change_notify(sdata, changed);
Guy Cohen8db93692008-07-03 19:56:13 +0300589
590 if (assoc)
591 netif_carrier_on(dev);
592
Reinette Chatre41a7be42008-04-03 16:08:49 -0700593 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
594 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Jiri Bencf0706e822007-05-05 11:45:53 -0700595}
596
597static void ieee80211_set_disassoc(struct net_device *dev,
598 struct ieee80211_if_sta *ifsta, int deauth)
599{
600 if (deauth)
601 ifsta->auth_tries = 0;
602 ifsta->assoc_tries = 0;
603 ieee80211_set_associated(dev, ifsta, 0);
604}
605
Luis Carlos Coboee385852008-02-23 15:17:11 +0100606void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
607 int encrypt)
Jiri Bencf0706e822007-05-05 11:45:53 -0700608{
609 struct ieee80211_sub_if_data *sdata;
Jiri Bencf0706e822007-05-05 11:45:53 -0700610
611 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
612 skb->dev = sdata->local->mdev;
613 skb_set_mac_header(skb, 0);
614 skb_set_network_header(skb, 0);
615 skb_set_transport_header(skb, 0);
616
Johannes Bergd0f09802008-07-29 11:32:07 +0200617 skb->iif = sdata->dev->ifindex;
618 skb->do_not_encrypt = !encrypt;
Jiri Bencf0706e822007-05-05 11:45:53 -0700619
620 dev_queue_xmit(skb);
621}
622
623
624static void ieee80211_send_auth(struct net_device *dev,
625 struct ieee80211_if_sta *ifsta,
626 int transaction, u8 *extra, size_t extra_len,
627 int encrypt)
628{
629 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
630 struct sk_buff *skb;
631 struct ieee80211_mgmt *mgmt;
632
633 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
634 sizeof(*mgmt) + 6 + extra_len);
635 if (!skb) {
636 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
637 "frame\n", dev->name);
638 return;
639 }
640 skb_reserve(skb, local->hw.extra_tx_headroom);
641
642 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
643 memset(mgmt, 0, 24 + 6);
644 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
645 IEEE80211_STYPE_AUTH);
646 if (encrypt)
647 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
648 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
649 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
650 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
651 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
652 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
653 ifsta->auth_transaction = transaction + 1;
654 mgmt->u.auth.status_code = cpu_to_le16(0);
655 if (extra)
656 memcpy(skb_put(skb, extra_len), extra, extra_len);
657
658 ieee80211_sta_tx(dev, skb, encrypt);
659}
660
661
662static void ieee80211_authenticate(struct net_device *dev,
663 struct ieee80211_if_sta *ifsta)
664{
Joe Perches0795af52007-10-03 17:59:30 -0700665 DECLARE_MAC_BUF(mac);
666
Jiri Bencf0706e822007-05-05 11:45:53 -0700667 ifsta->auth_tries++;
668 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700669 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e822007-05-05 11:45:53 -0700670 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700671 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700672 ifsta->state = IEEE80211_DISABLED;
673 return;
674 }
675
676 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700677 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
678 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700679
680 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
681
682 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
683}
684
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200685static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
686 struct ieee80211_supported_band *sband,
687 u64 *rates)
688{
689 int i, j, count;
690 *rates = 0;
691 count = 0;
692 for (i = 0; i < bss->supp_rates_len; i++) {
693 int rate = (bss->supp_rates[i] & 0x7F) * 5;
694
695 for (j = 0; j < sband->n_bitrates; j++)
696 if (sband->bitrates[j].bitrate == rate) {
697 *rates |= BIT(j);
698 count++;
699 break;
700 }
701 }
702
703 return count;
704}
Jiri Bencf0706e822007-05-05 11:45:53 -0700705
706static void ieee80211_send_assoc(struct net_device *dev,
707 struct ieee80211_if_sta *ifsta)
708{
709 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e822007-05-05 11:45:53 -0700710 struct sk_buff *skb;
711 struct ieee80211_mgmt *mgmt;
712 u8 *pos, *ies;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200713 int i, len, count, rates_len, supp_rates_len;
Jiri Bencf0706e822007-05-05 11:45:53 -0700714 u16 capab;
715 struct ieee80211_sta_bss *bss;
716 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100717 struct ieee80211_supported_band *sband;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200718 u64 rates = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -0700719
720 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
721 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
722 ifsta->ssid_len);
723 if (!skb) {
724 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
725 "frame\n", dev->name);
726 return;
727 }
728 skb_reserve(skb, local->hw.extra_tx_headroom);
729
Johannes Berg8318d782008-01-24 19:38:38 +0100730 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
731
Jiri Bencf0706e822007-05-05 11:45:53 -0700732 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100733
734 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
735 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
736 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
737 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
738 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e822007-05-05 11:45:53 -0700739 }
Johannes Berg8318d782008-01-24 19:38:38 +0100740
741 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
742 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400743 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -0700744 if (bss) {
745 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
746 capab |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +0200747 if (bss->wmm_ie)
Jiri Bencf0706e822007-05-05 11:45:53 -0700748 wmm = 1;
Helmut Schaa0d580a72008-05-20 09:56:37 +0200749
750 /* get all rates supported by the device and the AP as
751 * some APs don't like getting a superset of their rates
752 * in the association request (e.g. D-Link DAP 1353 in
753 * b-only mode) */
754 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
755
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300756 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
757 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
758 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
759
Johannes Berg3e122be2008-07-09 14:40:34 +0200760 ieee80211_rx_bss_put(local, bss);
Helmut Schaa0d580a72008-05-20 09:56:37 +0200761 } else {
762 rates = ~0;
763 rates_len = sband->n_bitrates;
Jiri Bencf0706e822007-05-05 11:45:53 -0700764 }
765
766 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
767 memset(mgmt, 0, 24);
768 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
769 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
770 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
771
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400772 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700773 skb_put(skb, 10);
774 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
775 IEEE80211_STYPE_REASSOC_REQ);
776 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
Tomas Winklerea95bba2008-07-18 13:53:00 +0800777 mgmt->u.reassoc_req.listen_interval =
778 cpu_to_le16(local->hw.conf.listen_interval);
Jiri Bencf0706e822007-05-05 11:45:53 -0700779 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
780 ETH_ALEN);
781 } else {
782 skb_put(skb, 4);
783 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
784 IEEE80211_STYPE_ASSOC_REQ);
785 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
Tomas Winklerea95bba2008-07-18 13:53:00 +0800786 mgmt->u.reassoc_req.listen_interval =
787 cpu_to_le16(local->hw.conf.listen_interval);
Jiri Bencf0706e822007-05-05 11:45:53 -0700788 }
789
790 /* SSID */
791 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
792 *pos++ = WLAN_EID_SSID;
793 *pos++ = ifsta->ssid_len;
794 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
795
Helmut Schaa0d580a72008-05-20 09:56:37 +0200796 /* add all rates which were marked to be used above */
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200797 supp_rates_len = rates_len;
798 if (supp_rates_len > 8)
799 supp_rates_len = 8;
Jiri Bencf0706e822007-05-05 11:45:53 -0700800
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200801 len = sband->n_bitrates;
802 pos = skb_put(skb, supp_rates_len + 2);
803 *pos++ = WLAN_EID_SUPP_RATES;
804 *pos++ = supp_rates_len;
805
806 count = 0;
807 for (i = 0; i < sband->n_bitrates; i++) {
808 if (BIT(i) & rates) {
Johannes Berg8318d782008-01-24 19:38:38 +0100809 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e822007-05-05 11:45:53 -0700810 *pos++ = (u8) (rate / 5);
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200811 if (++count == 8)
812 break;
813 }
814 }
815
816 if (count == 8) {
817 pos = skb_put(skb, rates_len - count + 2);
818 *pos++ = WLAN_EID_EXT_SUPP_RATES;
819 *pos++ = rates_len - count;
820
821 for (i++; i < sband->n_bitrates; i++) {
822 if (BIT(i) & rates) {
823 int rate = sband->bitrates[i].bitrate;
824 *pos++ = (u8) (rate / 5);
825 }
Jiri Bencf0706e822007-05-05 11:45:53 -0700826 }
827 }
828
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300829 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
830 /* 1. power capabilities */
831 pos = skb_put(skb, 4);
832 *pos++ = WLAN_EID_PWR_CAPABILITY;
833 *pos++ = 2;
834 *pos++ = 0; /* min tx power */
835 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
836
837 /* 2. supported channels */
838 /* TODO: get this in reg domain format */
839 pos = skb_put(skb, 2 * sband->n_channels + 2);
840 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
841 *pos++ = 2 * sband->n_channels;
842 for (i = 0; i < sband->n_channels; i++) {
843 *pos++ = ieee80211_frequency_to_channel(
844 sband->channels[i].center_freq);
845 *pos++ = 1; /* one channel in the subband*/
846 }
847 }
848
Jiri Bencf0706e822007-05-05 11:45:53 -0700849 if (ifsta->extra_ie) {
850 pos = skb_put(skb, ifsta->extra_ie_len);
851 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
852 }
853
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400854 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700855 pos = skb_put(skb, 9);
856 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
857 *pos++ = 7; /* len */
858 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
859 *pos++ = 0x50;
860 *pos++ = 0xf2;
861 *pos++ = 2; /* WME */
862 *pos++ = 0; /* WME info */
863 *pos++ = 1; /* WME ver */
864 *pos++ = 0;
865 }
Johannes Berg3434fbd2008-05-03 00:59:37 +0200866
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200867 /* wmm support is a must to HT */
Johannes Berg3434fbd2008-05-03 00:59:37 +0200868 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
Emmanuel Grumbach93061022008-05-29 16:35:23 +0800869 sband->ht_info.ht_supported && bss->ht_add_ie) {
870 struct ieee80211_ht_addt_info *ht_add_info =
871 (struct ieee80211_ht_addt_info *)bss->ht_add_ie;
872 u16 cap = sband->ht_info.cap;
873 __le16 tmp;
874 u32 flags = local->hw.conf.channel->flags;
875
876 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
877 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
878 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
879 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
880 cap &= ~IEEE80211_HT_CAP_SGI_40;
881 }
882 break;
883 case IEEE80211_HT_IE_CHA_SEC_BELOW:
884 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
885 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
886 cap &= ~IEEE80211_HT_CAP_SGI_40;
887 }
888 break;
889 }
890
891 tmp = cpu_to_le16(cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200892 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
893 *pos++ = WLAN_EID_HT_CAPABILITY;
894 *pos++ = sizeof(struct ieee80211_ht_cap);
895 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
896 memcpy(pos, &tmp, sizeof(u16));
897 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100898 /* TODO: needs a define here for << 2 */
899 *pos++ = sband->ht_info.ampdu_factor |
900 (sband->ht_info.ampdu_density << 2);
901 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200902 }
Jiri Bencf0706e822007-05-05 11:45:53 -0700903
904 kfree(ifsta->assocreq_ies);
905 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200906 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -0700907 if (ifsta->assocreq_ies)
908 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
909
910 ieee80211_sta_tx(dev, skb, 0);
911}
912
913
914static void ieee80211_send_deauth(struct net_device *dev,
915 struct ieee80211_if_sta *ifsta, u16 reason)
916{
917 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
918 struct sk_buff *skb;
919 struct ieee80211_mgmt *mgmt;
920
921 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
922 if (!skb) {
923 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
924 "frame\n", dev->name);
925 return;
926 }
927 skb_reserve(skb, local->hw.extra_tx_headroom);
928
929 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
930 memset(mgmt, 0, 24);
931 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
932 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
933 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
934 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
935 IEEE80211_STYPE_DEAUTH);
936 skb_put(skb, 2);
937 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
938
939 ieee80211_sta_tx(dev, skb, 0);
940}
941
942
943static void ieee80211_send_disassoc(struct net_device *dev,
944 struct ieee80211_if_sta *ifsta, u16 reason)
945{
946 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
947 struct sk_buff *skb;
948 struct ieee80211_mgmt *mgmt;
949
950 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
951 if (!skb) {
952 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
953 "frame\n", dev->name);
954 return;
955 }
956 skb_reserve(skb, local->hw.extra_tx_headroom);
957
958 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
959 memset(mgmt, 0, 24);
960 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
961 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
962 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
963 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
964 IEEE80211_STYPE_DISASSOC);
965 skb_put(skb, 2);
966 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
967
968 ieee80211_sta_tx(dev, skb, 0);
969}
970
971
972static int ieee80211_privacy_mismatch(struct net_device *dev,
973 struct ieee80211_if_sta *ifsta)
974{
John W. Linville65c107a2007-10-05 14:23:27 -0400975 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e822007-05-05 11:45:53 -0700976 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000977 int bss_privacy;
978 int wep_privacy;
979 int privacy_invoked;
Jiri Bencf0706e822007-05-05 11:45:53 -0700980
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000981 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e822007-05-05 11:45:53 -0700982 return 0;
983
Johannes Berg8318d782008-01-24 19:38:38 +0100984 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
985 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400986 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -0700987 if (!bss)
988 return 0;
989
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000990 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
991 wep_privacy = !!ieee80211_sta_wep_configured(dev);
992 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e822007-05-05 11:45:53 -0700993
Johannes Berg3e122be2008-07-09 14:40:34 +0200994 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e822007-05-05 11:45:53 -0700995
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000996 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
997 return 0;
998
999 return 1;
Jiri Bencf0706e822007-05-05 11:45:53 -07001000}
1001
1002
1003static void ieee80211_associate(struct net_device *dev,
1004 struct ieee80211_if_sta *ifsta)
1005{
Joe Perches0795af52007-10-03 17:59:30 -07001006 DECLARE_MAC_BUF(mac);
1007
Jiri Bencf0706e822007-05-05 11:45:53 -07001008 ifsta->assoc_tries++;
1009 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -07001010 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07001011 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -07001012 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001013 ifsta->state = IEEE80211_DISABLED;
1014 return;
1015 }
1016
1017 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -07001018 printk(KERN_DEBUG "%s: associate with AP %s\n",
1019 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001020 if (ieee80211_privacy_mismatch(dev, ifsta)) {
1021 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
1022 "mixed-cell disabled - abort association\n", dev->name);
1023 ifsta->state = IEEE80211_DISABLED;
1024 return;
1025 }
1026
1027 ieee80211_send_assoc(dev, ifsta);
1028
1029 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1030}
1031
1032
1033static void ieee80211_associated(struct net_device *dev,
1034 struct ieee80211_if_sta *ifsta)
1035{
1036 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1037 struct sta_info *sta;
1038 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -07001039 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001040
1041 /* TODO: start monitoring current AP signal quality and number of
1042 * missed beacons. Scan other channels every now and then and search
1043 * for better APs. */
1044 /* TODO: remove expired BSSes */
1045
1046 ifsta->state = IEEE80211_ASSOCIATED;
1047
Johannes Bergd0709a62008-02-25 16:27:46 +01001048 rcu_read_lock();
1049
Jiri Bencf0706e822007-05-05 11:45:53 -07001050 sta = sta_info_get(local, ifsta->bssid);
1051 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -07001052 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1053 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001054 disassoc = 1;
1055 } else {
1056 disassoc = 0;
1057 if (time_after(jiffies,
1058 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001059 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001060 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -07001061 "current AP %s - assume out of "
Jiri Bencf0706e822007-05-05 11:45:53 -07001062 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -07001063 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001064 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +01001065 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001066 } else
Jiri Bencf0706e822007-05-05 11:45:53 -07001067 ieee80211_send_probe_req(dev, ifsta->bssid,
1068 local->scan_ssid,
1069 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001070 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e822007-05-05 11:45:53 -07001071 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001072 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e822007-05-05 11:45:53 -07001073 if (time_after(jiffies, ifsta->last_probe +
1074 IEEE80211_PROBE_INTERVAL)) {
1075 ifsta->last_probe = jiffies;
1076 ieee80211_send_probe_req(dev, ifsta->bssid,
1077 ifsta->ssid,
1078 ifsta->ssid_len);
1079 }
1080 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001081 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001082
1083 rcu_read_unlock();
1084
Johannes Berg3b967662008-04-08 17:56:52 +02001085 if (disassoc && sta)
Johannes Bergd0709a62008-02-25 16:27:46 +01001086 sta_info_destroy(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01001087
Jiri Bencf0706e822007-05-05 11:45:53 -07001088 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -05001089 ifsta->state = IEEE80211_DISABLED;
1090 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -07001091 } else {
1092 mod_timer(&ifsta->timer, jiffies +
1093 IEEE80211_MONITORING_INTERVAL);
1094 }
1095}
1096
1097
1098static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1099 u8 *ssid, size_t ssid_len)
1100{
1101 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01001102 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -07001103 struct sk_buff *skb;
1104 struct ieee80211_mgmt *mgmt;
1105 u8 *pos, *supp_rates, *esupp_rates = NULL;
1106 int i;
1107
1108 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1109 if (!skb) {
1110 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1111 "request\n", dev->name);
1112 return;
1113 }
1114 skb_reserve(skb, local->hw.extra_tx_headroom);
1115
1116 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1117 memset(mgmt, 0, 24);
1118 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1119 IEEE80211_STYPE_PROBE_REQ);
1120 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1121 if (dst) {
1122 memcpy(mgmt->da, dst, ETH_ALEN);
1123 memcpy(mgmt->bssid, dst, ETH_ALEN);
1124 } else {
1125 memset(mgmt->da, 0xff, ETH_ALEN);
1126 memset(mgmt->bssid, 0xff, ETH_ALEN);
1127 }
1128 pos = skb_put(skb, 2 + ssid_len);
1129 *pos++ = WLAN_EID_SSID;
1130 *pos++ = ssid_len;
1131 memcpy(pos, ssid, ssid_len);
1132
1133 supp_rates = skb_put(skb, 2);
1134 supp_rates[0] = WLAN_EID_SUPP_RATES;
1135 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001136 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1137
1138 for (i = 0; i < sband->n_bitrates; i++) {
1139 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e822007-05-05 11:45:53 -07001140 if (esupp_rates) {
1141 pos = skb_put(skb, 1);
1142 esupp_rates[1]++;
1143 } else if (supp_rates[1] == 8) {
1144 esupp_rates = skb_put(skb, 3);
1145 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1146 esupp_rates[1] = 1;
1147 pos = &esupp_rates[2];
1148 } else {
1149 pos = skb_put(skb, 1);
1150 supp_rates[1]++;
1151 }
Johannes Berg8318d782008-01-24 19:38:38 +01001152 *pos = rate->bitrate / 5;
Jiri Bencf0706e822007-05-05 11:45:53 -07001153 }
1154
1155 ieee80211_sta_tx(dev, skb, 0);
1156}
1157
1158
1159static int ieee80211_sta_wep_configured(struct net_device *dev)
1160{
1161 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1162 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001163 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e822007-05-05 11:45:53 -07001164 return 0;
1165 return 1;
1166}
1167
1168
1169static void ieee80211_auth_completed(struct net_device *dev,
1170 struct ieee80211_if_sta *ifsta)
1171{
1172 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001173 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e822007-05-05 11:45:53 -07001174 ieee80211_associate(dev, ifsta);
1175}
1176
1177
1178static void ieee80211_auth_challenge(struct net_device *dev,
1179 struct ieee80211_if_sta *ifsta,
1180 struct ieee80211_mgmt *mgmt,
1181 size_t len)
1182{
1183 u8 *pos;
1184 struct ieee802_11_elems elems;
1185
Jiri Bencf0706e822007-05-05 11:45:53 -07001186 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001187 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001188 if (!elems.challenge)
Jiri Bencf0706e822007-05-05 11:45:53 -07001189 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001190 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1191 elems.challenge_len + 2, 1);
1192}
1193
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001194static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1195 u8 dialog_token, u16 status, u16 policy,
1196 u16 buf_size, u16 timeout)
1197{
1198 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1199 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1200 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1201 struct sk_buff *skb;
1202 struct ieee80211_mgmt *mgmt;
1203 u16 capab;
1204
Ester Kummer3acea5b2008-04-17 16:05:14 -07001205 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1206
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001207 if (!skb) {
1208 printk(KERN_DEBUG "%s: failed to allocate buffer "
1209 "for addba resp frame\n", dev->name);
1210 return;
1211 }
1212
1213 skb_reserve(skb, local->hw.extra_tx_headroom);
1214 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1215 memset(mgmt, 0, 24);
1216 memcpy(mgmt->da, da, ETH_ALEN);
1217 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001218 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001219 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1220 else
1221 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1222 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1223 IEEE80211_STYPE_ACTION);
1224
1225 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1226 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1227 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1228 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1229
1230 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1231 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1232 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1233
1234 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1235 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1236 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1237
1238 ieee80211_sta_tx(dev, skb, 0);
1239
1240 return;
1241}
1242
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001243void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1244 u16 tid, u8 dialog_token, u16 start_seq_num,
1245 u16 agg_size, u16 timeout)
1246{
1247 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1248 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1249 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1250 struct sk_buff *skb;
1251 struct ieee80211_mgmt *mgmt;
1252 u16 capab;
1253
Ester Kummer3acea5b2008-04-17 16:05:14 -07001254 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001255
1256 if (!skb) {
1257 printk(KERN_ERR "%s: failed to allocate buffer "
1258 "for addba request frame\n", dev->name);
1259 return;
1260 }
1261 skb_reserve(skb, local->hw.extra_tx_headroom);
1262 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1263 memset(mgmt, 0, 24);
1264 memcpy(mgmt->da, da, ETH_ALEN);
1265 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1266 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1267 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1268 else
1269 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1270
1271 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1272 IEEE80211_STYPE_ACTION);
1273
1274 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1275
1276 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1277 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1278
1279 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1280 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1281 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1282 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1283
1284 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1285
1286 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1287 mgmt->u.action.u.addba_req.start_seq_num =
1288 cpu_to_le16(start_seq_num << 4);
1289
1290 ieee80211_sta_tx(dev, skb, 0);
1291}
1292
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001293static void ieee80211_sta_process_addba_request(struct net_device *dev,
1294 struct ieee80211_mgmt *mgmt,
1295 size_t len)
1296{
1297 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001298 struct ieee80211_hw *hw = &local->hw;
1299 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001300 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001301 struct tid_ampdu_rx *tid_agg_rx;
1302 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001303 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001304 int ret = -EOPNOTSUPP;
1305 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001306
Johannes Bergd0709a62008-02-25 16:27:46 +01001307 rcu_read_lock();
1308
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001309 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001310 if (!sta) {
1311 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001312 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001313 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001314
1315 /* extract session parameters from addba request frame */
1316 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1317 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001318 start_seq_num =
1319 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001320
1321 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1322 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1323 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1324 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1325
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001326 status = WLAN_STATUS_REQUEST_DECLINED;
1327
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001328 /* sanity check for incoming parameters:
1329 * check if configuration can support the BA policy
1330 * and if buffer size does not exceeds max value */
1331 if (((ba_policy != 1)
1332 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1333 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1334 status = WLAN_STATUS_INVALID_QOS_PARAM;
1335#ifdef CONFIG_MAC80211_HT_DEBUG
1336 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001337 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001338 "%s on tid %u. policy %d, buffer size %d\n",
1339 print_mac(mac, mgmt->sa), tid, ba_policy,
1340 buf_size);
1341#endif /* CONFIG_MAC80211_HT_DEBUG */
1342 goto end_no_lock;
1343 }
1344 /* determine default buffer size */
1345 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001346 struct ieee80211_supported_band *sband;
1347
1348 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001349 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001350 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001351 }
1352
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001353
1354 /* examine state machine */
Johannes Berg07346f812008-05-03 01:02:02 +02001355 spin_lock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001356
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001357 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001358#ifdef CONFIG_MAC80211_HT_DEBUG
1359 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001360 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001361 "%s on tid %u\n",
1362 print_mac(mac, mgmt->sa), tid);
1363#endif /* CONFIG_MAC80211_HT_DEBUG */
1364 goto end;
1365 }
1366
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001367 /* prepare A-MPDU MLME for Rx aggregation */
1368 sta->ampdu_mlme.tid_rx[tid] =
1369 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1370 if (!sta->ampdu_mlme.tid_rx[tid]) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001371#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001372 if (net_ratelimit())
1373 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1374 tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001375#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001376 goto end;
1377 }
1378 /* rx timer */
1379 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1380 sta_rx_agg_session_timer_expired;
1381 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1382 (unsigned long)&sta->timer_to_tid[tid];
1383 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1384
1385 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1386
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001387 /* prepare reordering buffer */
1388 tid_agg_rx->reorder_buf =
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301389 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001390 if (!tid_agg_rx->reorder_buf) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001391#ifdef CONFIG_MAC80211_HT_DEBUG
Johannes Berg03147df2008-02-26 00:39:28 +01001392 if (net_ratelimit())
1393 printk(KERN_ERR "can not allocate reordering buffer "
1394 "to tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001395#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001396 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001397 goto end;
1398 }
1399 memset(tid_agg_rx->reorder_buf, 0,
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301400 buf_size * sizeof(struct sk_buff *));
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001401
1402 if (local->ops->ampdu_action)
1403 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001404 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001405#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001406 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001407#endif /* CONFIG_MAC80211_HT_DEBUG */
1408
1409 if (ret) {
1410 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001411 kfree(tid_agg_rx);
1412 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001413 goto end;
1414 }
1415
1416 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001417 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001418 tid_agg_rx->dialog_token = dialog_token;
1419 tid_agg_rx->ssn = start_seq_num;
1420 tid_agg_rx->head_seq_num = start_seq_num;
1421 tid_agg_rx->buf_size = buf_size;
1422 tid_agg_rx->timeout = timeout;
1423 tid_agg_rx->stored_mpdu_num = 0;
1424 status = WLAN_STATUS_SUCCESS;
1425end:
Johannes Berg07346f812008-05-03 01:02:02 +02001426 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001427
1428end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001429 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1430 dialog_token, status, 1, buf_size, timeout);
1431 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001432}
Jiri Bencf0706e822007-05-05 11:45:53 -07001433
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001434static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1435 struct ieee80211_mgmt *mgmt,
1436 size_t len)
1437{
1438 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1439 struct ieee80211_hw *hw = &local->hw;
1440 struct sta_info *sta;
1441 u16 capab;
1442 u16 tid;
1443 u8 *state;
1444
Johannes Bergd0709a62008-02-25 16:27:46 +01001445 rcu_read_lock();
1446
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001447 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001448 if (!sta) {
1449 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001450 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001451 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001452
1453 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1454 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1455
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001456 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001457
Johannes Berg07346f812008-05-03 01:02:02 +02001458 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001459
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001460 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001461 spin_unlock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001462 goto addba_resp_exit;
1463 }
1464
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001465 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001466 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Johannes Berg07346f812008-05-03 01:02:02 +02001467 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001468#ifdef CONFIG_MAC80211_HT_DEBUG
1469 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1470#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001471 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001472 }
1473
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001474 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001475#ifdef CONFIG_MAC80211_HT_DEBUG
1476 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1477#endif /* CONFIG_MAC80211_HT_DEBUG */
1478 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1479 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001480 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001481 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001482
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001483 if (*state == HT_AGG_STATE_OPERATIONAL)
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001484 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001485
Johannes Berg07346f812008-05-03 01:02:02 +02001486 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001487 } else {
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001488 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001489 /* this will allow the state check in stop_BA_session */
1490 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001491 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001492 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1493 WLAN_BACK_INITIATOR);
1494 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001495
1496addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001497 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001498}
1499
1500void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1501 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001502{
1503 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1504 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1505 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1506 struct sk_buff *skb;
1507 struct ieee80211_mgmt *mgmt;
1508 u16 params;
1509
Ester Kummer3acea5b2008-04-17 16:05:14 -07001510 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001511
1512 if (!skb) {
1513 printk(KERN_ERR "%s: failed to allocate buffer "
1514 "for delba frame\n", dev->name);
1515 return;
1516 }
1517
1518 skb_reserve(skb, local->hw.extra_tx_headroom);
1519 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1520 memset(mgmt, 0, 24);
1521 memcpy(mgmt->da, da, ETH_ALEN);
1522 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001523 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001524 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1525 else
1526 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1527 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1528 IEEE80211_STYPE_ACTION);
1529
1530 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1531
1532 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1533 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1534 params = (u16)(initiator << 11); /* bit 11 initiator */
1535 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1536
1537 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1538 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1539
1540 ieee80211_sta_tx(dev, skb, 0);
1541}
1542
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001543void ieee80211_send_bar(struct net_device *dev, u8 *ra, u16 tid, u16 ssn)
1544{
1545 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1546 struct sk_buff *skb;
1547 struct ieee80211_bar *bar;
1548 u16 bar_control = 0;
1549
1550 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
1551 if (!skb) {
1552 printk(KERN_ERR "%s: failed to allocate buffer for "
1553 "bar frame\n", dev->name);
1554 return;
1555 }
1556 skb_reserve(skb, local->hw.extra_tx_headroom);
1557 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
1558 memset(bar, 0, sizeof(*bar));
1559 bar->frame_control = IEEE80211_FC(IEEE80211_FTYPE_CTL,
1560 IEEE80211_STYPE_BACK_REQ);
1561 memcpy(bar->ra, ra, ETH_ALEN);
1562 memcpy(bar->ta, dev->dev_addr, ETH_ALEN);
1563 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
1564 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
1565 bar_control |= (u16)(tid << 12);
1566 bar->control = cpu_to_le16(bar_control);
1567 bar->start_seq_num = cpu_to_le16(ssn);
1568
1569 ieee80211_sta_tx(dev, skb, 0);
1570}
1571
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001572void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1573 u16 initiator, u16 reason)
1574{
1575 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1576 struct ieee80211_hw *hw = &local->hw;
1577 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001578 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001579 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001580
Johannes Bergd0709a62008-02-25 16:27:46 +01001581 rcu_read_lock();
1582
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001583 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001584 if (!sta) {
1585 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001586 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001587 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001588
1589 /* check if TID is in operational state */
Johannes Berg07346f812008-05-03 01:02:02 +02001590 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001591 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001592 != HT_AGG_STATE_OPERATIONAL) {
Johannes Berg07346f812008-05-03 01:02:02 +02001593 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +01001594 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001595 return;
1596 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001597 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001598 HT_AGG_STATE_REQ_STOP_BA_MSK |
1599 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Johannes Berg07346f812008-05-03 01:02:02 +02001600 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001601
1602 /* stop HW Rx aggregation. ampdu_action existence
1603 * already verified in session init so we add the BUG_ON */
1604 BUG_ON(!local->ops->ampdu_action);
1605
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001606#ifdef CONFIG_MAC80211_HT_DEBUG
1607 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1608 print_mac(mac, ra), tid);
1609#endif /* CONFIG_MAC80211_HT_DEBUG */
1610
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001611 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001612 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001613 if (ret)
1614 printk(KERN_DEBUG "HW problem - can not stop rx "
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001615 "aggregation for tid %d\n", tid);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001616
1617 /* shutdown timer has not expired */
1618 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001619 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001620
1621 /* check if this is a self generated aggregation halt */
1622 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1623 ieee80211_send_delba(dev, ra, tid, 0, reason);
1624
1625 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001626 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1627 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001628 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001629 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1630 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1631 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001632 }
1633 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001634 /* free resources */
1635 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1636 kfree(sta->ampdu_mlme.tid_rx[tid]);
1637 sta->ampdu_mlme.tid_rx[tid] = NULL;
1638 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001639
Johannes Bergd0709a62008-02-25 16:27:46 +01001640 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001641}
1642
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001643
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001644static void ieee80211_sta_process_delba(struct net_device *dev,
1645 struct ieee80211_mgmt *mgmt, size_t len)
1646{
1647 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1648 struct sta_info *sta;
1649 u16 tid, params;
1650 u16 initiator;
1651 DECLARE_MAC_BUF(mac);
1652
Johannes Bergd0709a62008-02-25 16:27:46 +01001653 rcu_read_lock();
1654
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001655 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001656 if (!sta) {
1657 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001658 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001659 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001660
1661 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1662 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1663 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1664
1665#ifdef CONFIG_MAC80211_HT_DEBUG
1666 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001667 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1668 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001669 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001670 mgmt->u.action.u.delba.reason_code);
1671#endif /* CONFIG_MAC80211_HT_DEBUG */
1672
1673 if (initiator == WLAN_BACK_INITIATOR)
1674 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1675 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001676 else { /* WLAN_BACK_RECIPIENT */
Johannes Berg07346f812008-05-03 01:02:02 +02001677 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001678 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001679 HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001680 spin_unlock_bh(&sta->lock);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001681 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1682 WLAN_BACK_RECIPIENT);
1683 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001684 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001685}
1686
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001687/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001688 * After sending add Block Ack request we activated a timer until
1689 * add Block Ack response will arrive from the recipient.
1690 * If this timer expires sta_addba_resp_timer_expired will be executed.
1691 */
1692void sta_addba_resp_timer_expired(unsigned long data)
1693{
1694 /* not an elegant detour, but there is no choice as the timer passes
1695 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001696 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001697 * array gives the sta through container_of */
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301698 u16 tid = *(u8 *)data;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001699 struct sta_info *temp_sta = container_of((void *)data,
1700 struct sta_info, timer_to_tid[tid]);
1701
1702 struct ieee80211_local *local = temp_sta->local;
1703 struct ieee80211_hw *hw = &local->hw;
1704 struct sta_info *sta;
1705 u8 *state;
1706
Johannes Bergd0709a62008-02-25 16:27:46 +01001707 rcu_read_lock();
1708
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001709 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001710 if (!sta) {
1711 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001712 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001713 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001714
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001715 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001716 /* check if the TID waits for addBA response */
Johannes Berg07346f812008-05-03 01:02:02 +02001717 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001718 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001719 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001720 *state = HT_AGG_STATE_IDLE;
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001721#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001722 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1723 "expecting addBA response there", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001724#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001725 goto timer_expired_exit;
1726 }
1727
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001728#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001729 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001730#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001731
1732 /* go through the state check in stop_BA_session */
1733 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001734 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001735 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1736 WLAN_BACK_INITIATOR);
1737
1738timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001739 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001740}
1741
1742/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001743 * After accepting the AddBA Request we activated a timer,
1744 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001745 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1746 */
Adrian Bunk712590d2008-04-21 11:47:51 +03001747static void sta_rx_agg_session_timer_expired(unsigned long data)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001748{
1749 /* not an elegant detour, but there is no choice as the timer passes
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301750 * only one argument, and various sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001751 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001752 * array gives the sta through container_of */
1753 u8 *ptid = (u8 *)data;
1754 u8 *timer_to_id = ptid - *ptid;
1755 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1756 timer_to_tid[0]);
1757
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001758#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001759 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001760#endif
Johannes Bergd0709a62008-02-25 16:27:46 +01001761 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1762 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001763 WLAN_REASON_QSTA_TIMEOUT);
1764}
1765
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001766void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1767{
1768 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1769 int i;
1770
1771 for (i = 0; i < STA_TID_NUM; i++) {
1772 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1773 WLAN_BACK_INITIATOR);
1774 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1775 WLAN_BACK_RECIPIENT,
1776 WLAN_REASON_QSTA_LEAVE_QBSS);
1777 }
1778}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001779
Assaf Kraussb6623482008-06-16 16:09:49 +03001780static void ieee80211_send_refuse_measurement_request(struct net_device *dev,
1781 struct ieee80211_msrment_ie *request_ie,
1782 const u8 *da, const u8 *bssid,
1783 u8 dialog_token)
1784{
1785 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1786 struct sk_buff *skb;
1787 struct ieee80211_mgmt *msr_report;
1788
1789 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1790 sizeof(struct ieee80211_msrment_ie));
1791
1792 if (!skb) {
1793 printk(KERN_ERR "%s: failed to allocate buffer for "
1794 "measurement report frame\n", dev->name);
1795 return;
1796 }
1797
1798 skb_reserve(skb, local->hw.extra_tx_headroom);
1799 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1800 memset(msr_report, 0, 24);
1801 memcpy(msr_report->da, da, ETH_ALEN);
1802 memcpy(msr_report->sa, dev->dev_addr, ETH_ALEN);
1803 memcpy(msr_report->bssid, bssid, ETH_ALEN);
1804 msr_report->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1805 IEEE80211_STYPE_ACTION);
1806
1807 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1808 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1809 msr_report->u.action.u.measurement.action_code =
1810 WLAN_ACTION_SPCT_MSR_RPRT;
1811 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1812
1813 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1814 msr_report->u.action.u.measurement.length =
1815 sizeof(struct ieee80211_msrment_ie);
1816
1817 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1818 sizeof(struct ieee80211_msrment_ie));
1819 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1820 msr_report->u.action.u.measurement.msr_elem.mode |=
1821 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1822 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1823
1824 ieee80211_sta_tx(dev, skb, 0);
1825}
1826
1827static void ieee80211_sta_process_measurement_req(struct net_device *dev,
1828 struct ieee80211_mgmt *mgmt,
1829 size_t len)
1830{
1831 /*
1832 * Ignoring measurement request is spec violation.
1833 * Mandatory measurements must be reported optional
1834 * measurements might be refused or reported incapable
1835 * For now just refuse
1836 * TODO: Answer basic measurement as unmeasured
1837 */
1838 ieee80211_send_refuse_measurement_request(dev,
1839 &mgmt->u.action.u.measurement.msr_elem,
1840 mgmt->sa, mgmt->bssid,
1841 mgmt->u.action.u.measurement.dialog_token);
1842}
1843
1844
Jiri Bencf0706e822007-05-05 11:45:53 -07001845static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1846 struct ieee80211_if_sta *ifsta,
1847 struct ieee80211_mgmt *mgmt,
1848 size_t len)
1849{
1850 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1851 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001852 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001853
1854 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001855 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e822007-05-05 11:45:53 -07001856 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001857
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001858 if (len < 24 + 6)
Jiri Bencf0706e822007-05-05 11:45:53 -07001859 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001860
Johannes Berg51fb61e2007-12-19 01:31:27 +01001861 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001862 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e822007-05-05 11:45:53 -07001863 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001864
Johannes Berg51fb61e2007-12-19 01:31:27 +01001865 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001866 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e822007-05-05 11:45:53 -07001867 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001868
1869 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1870 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1871 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1872
Johannes Berg51fb61e2007-12-19 01:31:27 +01001873 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg135a2112008-06-16 20:55:29 +02001874 /*
1875 * IEEE 802.11 standard does not require authentication in IBSS
Jiri Bencf0706e822007-05-05 11:45:53 -07001876 * networks and most implementations do not seem to use it.
1877 * However, try to reply to authentication attempts if someone
1878 * has actually implemented this.
Johannes Berg135a2112008-06-16 20:55:29 +02001879 */
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001880 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
Jiri Bencf0706e822007-05-05 11:45:53 -07001881 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001882 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1883 }
1884
1885 if (auth_alg != ifsta->auth_alg ||
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001886 auth_transaction != ifsta->auth_transaction)
Jiri Bencf0706e822007-05-05 11:45:53 -07001887 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001888
1889 if (status_code != WLAN_STATUS_SUCCESS) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001890 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1891 u8 algs[3];
1892 const int num_algs = ARRAY_SIZE(algs);
1893 int i, pos;
1894 algs[0] = algs[1] = algs[2] = 0xff;
1895 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1896 algs[0] = WLAN_AUTH_OPEN;
1897 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1898 algs[1] = WLAN_AUTH_SHARED_KEY;
1899 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1900 algs[2] = WLAN_AUTH_LEAP;
1901 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1902 pos = 0;
1903 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1904 pos = 1;
1905 else
1906 pos = 2;
1907 for (i = 0; i < num_algs; i++) {
1908 pos++;
1909 if (pos >= num_algs)
1910 pos = 0;
1911 if (algs[pos] == ifsta->auth_alg ||
1912 algs[pos] == 0xff)
1913 continue;
1914 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1915 !ieee80211_sta_wep_configured(dev))
1916 continue;
1917 ifsta->auth_alg = algs[pos];
Jiri Bencf0706e822007-05-05 11:45:53 -07001918 break;
1919 }
1920 }
1921 return;
1922 }
1923
1924 switch (ifsta->auth_alg) {
1925 case WLAN_AUTH_OPEN:
1926 case WLAN_AUTH_LEAP:
1927 ieee80211_auth_completed(dev, ifsta);
1928 break;
1929 case WLAN_AUTH_SHARED_KEY:
1930 if (ifsta->auth_transaction == 4)
1931 ieee80211_auth_completed(dev, ifsta);
1932 else
1933 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1934 break;
1935 }
1936}
1937
1938
1939static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1940 struct ieee80211_if_sta *ifsta,
1941 struct ieee80211_mgmt *mgmt,
1942 size_t len)
1943{
1944 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001945 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001946
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001947 if (len < 24 + 2)
Jiri Bencf0706e822007-05-05 11:45:53 -07001948 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001949
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001950 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e822007-05-05 11:45:53 -07001951 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001952
1953 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1954
Johannes Berg988c0f72008-04-17 19:21:22 +02001955 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
Jiri Bencf0706e822007-05-05 11:45:53 -07001956 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
Jiri Bencf0706e822007-05-05 11:45:53 -07001957
1958 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1959 ifsta->state == IEEE80211_ASSOCIATE ||
1960 ifsta->state == IEEE80211_ASSOCIATED) {
1961 ifsta->state = IEEE80211_AUTHENTICATE;
1962 mod_timer(&ifsta->timer, jiffies +
1963 IEEE80211_RETRY_AUTH_INTERVAL);
1964 }
1965
1966 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001967 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e822007-05-05 11:45:53 -07001968}
1969
1970
1971static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1972 struct ieee80211_if_sta *ifsta,
1973 struct ieee80211_mgmt *mgmt,
1974 size_t len)
1975{
1976 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001977 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001978
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001979 if (len < 24 + 2)
Jiri Bencf0706e822007-05-05 11:45:53 -07001980 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001981
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001982 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e822007-05-05 11:45:53 -07001983 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07001984
1985 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1986
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001987 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e822007-05-05 11:45:53 -07001988 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1989
1990 if (ifsta->state == IEEE80211_ASSOCIATED) {
1991 ifsta->state = IEEE80211_ASSOCIATE;
1992 mod_timer(&ifsta->timer, jiffies +
1993 IEEE80211_RETRY_AUTH_INTERVAL);
1994 }
1995
1996 ieee80211_set_disassoc(dev, ifsta, 0);
1997}
1998
1999
Johannes Berg471b3ef2007-12-28 14:32:58 +01002000static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e822007-05-05 11:45:53 -07002001 struct ieee80211_if_sta *ifsta,
2002 struct ieee80211_mgmt *mgmt,
2003 size_t len,
2004 int reassoc)
2005{
Johannes Berg471b3ef2007-12-28 14:32:58 +01002006 struct ieee80211_local *local = sdata->local;
2007 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01002008 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -07002009 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01002010 u64 rates, basic_rates;
Jiri Bencf0706e822007-05-05 11:45:53 -07002011 u16 capab_info, status_code, aid;
2012 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002013 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e822007-05-05 11:45:53 -07002014 u8 *pos;
2015 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07002016 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01002017 bool have_higher_than_11mbit = false;
Jiri Bencf0706e822007-05-05 11:45:53 -07002018
2019 /* AssocResp and ReassocResp have identical structure, so process both
2020 * of them in this function. */
2021
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002022 if (ifsta->state != IEEE80211_ASSOCIATE)
Jiri Bencf0706e822007-05-05 11:45:53 -07002023 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07002024
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002025 if (len < 24 + 6)
Jiri Bencf0706e822007-05-05 11:45:53 -07002026 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07002027
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002028 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e822007-05-05 11:45:53 -07002029 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07002030
2031 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2032 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2033 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e822007-05-05 11:45:53 -07002034
Joe Perches0795af52007-10-03 17:59:30 -07002035 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e822007-05-05 11:45:53 -07002036 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07002037 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02002038 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e822007-05-05 11:45:53 -07002039
2040 if (status_code != WLAN_STATUS_SUCCESS) {
2041 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
2042 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02002043 /* if this was a reassociation, ensure we try a "full"
2044 * association next time. This works around some broken APs
2045 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002046 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07002047 return;
2048 }
2049
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002050 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2051 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
2052 "set\n", dev->name, aid);
2053 aid &= ~(BIT(15) | BIT(14));
2054
Jiri Bencf0706e822007-05-05 11:45:53 -07002055 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04002056 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e822007-05-05 11:45:53 -07002057
2058 if (!elems.supp_rates) {
2059 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
2060 dev->name);
2061 return;
2062 }
2063
2064 printk(KERN_DEBUG "%s: associated\n", dev->name);
2065 ifsta->aid = aid;
2066 ifsta->ap_capab = capab_info;
2067
2068 kfree(ifsta->assocresp_ies);
2069 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002070 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07002071 if (ifsta->assocresp_ies)
2072 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2073
Johannes Bergd0709a62008-02-25 16:27:46 +01002074 rcu_read_lock();
2075
Jiri Bencf0706e822007-05-05 11:45:53 -07002076 /* Add STA entry for the AP */
2077 sta = sta_info_get(local, ifsta->bssid);
2078 if (!sta) {
2079 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01002080 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01002081
Johannes Berg73651ee2008-02-25 16:27:47 +01002082 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2083 if (!sta) {
2084 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2085 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01002086 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07002087 return;
2088 }
John W. Linville65c107a2007-10-05 14:23:27 -04002089 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01002090 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002091 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07002092 if (bss) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002093 sta->last_signal = bss->signal;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002094 sta->last_qual = bss->qual;
Jiri Bencf0706e822007-05-05 11:45:53 -07002095 sta->last_noise = bss->noise;
Johannes Berg3e122be2008-07-09 14:40:34 +02002096 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e822007-05-05 11:45:53 -07002097 }
Johannes Berg73651ee2008-02-25 16:27:47 +01002098
2099 err = sta_info_insert(sta);
2100 if (err) {
2101 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2102 " the AP (error %d)\n", dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01002103 rcu_read_unlock();
2104 return;
2105 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002106 }
2107
Johannes Berg73651ee2008-02-25 16:27:47 +01002108 /*
2109 * FIXME: Do we really need to update the sta_info's information here?
2110 * We already know about the AP (we found it in our list) so it
2111 * should already be filled with the right info, no?
2112 * As is stands, all this is racy because typically we assume
2113 * the information that is filled in here (except flags) doesn't
2114 * change while a STA structure is alive. As such, it should move
2115 * to between the sta_info_alloc() and sta_info_insert() above.
2116 */
2117
Johannes Berg07346f812008-05-03 01:02:02 +02002118 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2119 WLAN_STA_AUTHORIZED);
Jiri Bencf0706e822007-05-05 11:45:53 -07002120
2121 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002122 basic_rates = 0;
2123 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2124
Jiri Bencf0706e822007-05-05 11:45:53 -07002125 for (i = 0; i < elems.supp_rates_len; i++) {
2126 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002127
2128 if (rate > 110)
2129 have_higher_than_11mbit = true;
2130
2131 for (j = 0; j < sband->n_bitrates; j++) {
2132 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e822007-05-05 11:45:53 -07002133 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002134 if (elems.supp_rates[i] & 0x80)
2135 basic_rates |= BIT(j);
2136 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002137 }
Johannes Berg8318d782008-01-24 19:38:38 +01002138
Jiri Bencf0706e822007-05-05 11:45:53 -07002139 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2140 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002141
2142 if (rate > 110)
2143 have_higher_than_11mbit = true;
2144
2145 for (j = 0; j < sband->n_bitrates; j++) {
2146 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e822007-05-05 11:45:53 -07002147 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002148 if (elems.ext_supp_rates[i] & 0x80)
2149 basic_rates |= BIT(j);
2150 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002151 }
Johannes Berg8318d782008-01-24 19:38:38 +01002152
2153 sta->supp_rates[local->hw.conf.channel->band] = rates;
2154 sdata->basic_rates = basic_rates;
2155
2156 /* cf. IEEE 802.11 9.2.12 */
2157 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2158 have_higher_than_11mbit)
2159 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2160 else
2161 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e822007-05-05 11:45:53 -07002162
Johannes Berg3434fbd2008-05-03 00:59:37 +02002163 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2164 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002165 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002166 ieee80211_ht_cap_ie_to_ht_info(
2167 (struct ieee80211_ht_cap *)
2168 elems.ht_cap_elem, &sta->ht_info);
2169 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2170 (struct ieee80211_ht_addt_info *)
2171 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002172 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002173 }
2174
Jiri Bencf0706e822007-05-05 11:45:53 -07002175 rate_control_rate_init(sta, local);
2176
Johannes Berg3434fbd2008-05-03 00:59:37 +02002177 if (elems.wmm_param) {
Johannes Berg07346f812008-05-03 01:02:02 +02002178 set_sta_flags(sta, WLAN_STA_WME);
Johannes Berge5f98f22008-03-05 20:39:31 +01002179 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07002180 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2181 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002182 } else
2183 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07002184
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002185 /* set AID and assoc capability,
2186 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002187 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002188 bss_conf->assoc_capability = capab_info;
Johannes Berg8318d782008-01-24 19:38:38 +01002189 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e822007-05-05 11:45:53 -07002190
Jiri Bencf0706e822007-05-05 11:45:53 -07002191 ieee80211_associated(dev, ifsta);
2192}
2193
2194
2195/* Caller must hold local->sta_bss_lock */
2196static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2197 struct ieee80211_sta_bss *bss)
2198{
2199 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002200 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002201
2202 if (bss_mesh_cfg(bss))
2203 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2204 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002205 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002206 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002207
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002208 bss->hnext = local->sta_bss_hash[hash_idx];
2209 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e822007-05-05 11:45:53 -07002210}
2211
2212
2213/* Caller must hold local->sta_bss_lock */
Johannes Berg3e122be2008-07-09 14:40:34 +02002214static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
Jiri Bencf0706e822007-05-05 11:45:53 -07002215 struct ieee80211_sta_bss *bss)
2216{
Jiri Bencf0706e822007-05-05 11:45:53 -07002217 struct ieee80211_sta_bss *b, *prev = NULL;
2218 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2219 while (b) {
2220 if (b == bss) {
2221 if (!prev)
2222 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2223 bss->hnext;
2224 else
2225 prev->hnext = bss->hnext;
2226 break;
2227 }
2228 prev = b;
2229 b = b->hnext;
2230 }
2231}
2232
2233
2234static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002235ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002236 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e822007-05-05 11:45:53 -07002237{
2238 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2239 struct ieee80211_sta_bss *bss;
2240
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002241 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e822007-05-05 11:45:53 -07002242 if (!bss)
2243 return NULL;
Jiri Bencf0706e822007-05-05 11:45:53 -07002244 atomic_inc(&bss->users);
2245 atomic_inc(&bss->users);
2246 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002247 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002248 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2249 memcpy(bss->ssid, ssid, ssid_len);
2250 bss->ssid_len = ssid_len;
2251 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002252
2253 spin_lock_bh(&local->sta_bss_lock);
2254 /* TODO: order by RSSI? */
2255 list_add_tail(&bss->list, &local->sta_bss_list);
2256 __ieee80211_rx_bss_hash_add(dev, bss);
2257 spin_unlock_bh(&local->sta_bss_lock);
2258 return bss;
2259}
2260
Jiri Bencf0706e822007-05-05 11:45:53 -07002261static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002262ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002263 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e822007-05-05 11:45:53 -07002264{
2265 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2266 struct ieee80211_sta_bss *bss;
2267
2268 spin_lock_bh(&local->sta_bss_lock);
2269 bss = local->sta_bss_hash[STA_HASH(bssid)];
2270 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002271 if (!bss_mesh_cfg(bss) &&
2272 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002273 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002274 bss->ssid_len == ssid_len &&
2275 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002276 atomic_inc(&bss->users);
2277 break;
2278 }
2279 bss = bss->hnext;
2280 }
2281 spin_unlock_bh(&local->sta_bss_lock);
2282 return bss;
2283}
2284
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002285#ifdef CONFIG_MAC80211_MESH
2286static struct ieee80211_sta_bss *
2287ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2288 u8 *mesh_cfg, int freq)
2289{
2290 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2291 struct ieee80211_sta_bss *bss;
2292
2293 spin_lock_bh(&local->sta_bss_lock);
2294 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2295 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002296 if (bss_mesh_cfg(bss) &&
2297 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002298 bss->freq == freq &&
2299 mesh_id_len == bss->mesh_id_len &&
2300 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2301 mesh_id_len))) {
2302 atomic_inc(&bss->users);
2303 break;
2304 }
2305 bss = bss->hnext;
2306 }
2307 spin_unlock_bh(&local->sta_bss_lock);
2308 return bss;
2309}
2310
2311static struct ieee80211_sta_bss *
2312ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002313 u8 *mesh_cfg, int mesh_config_len, int freq)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002314{
2315 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2316 struct ieee80211_sta_bss *bss;
2317
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002318 if (mesh_config_len != MESH_CFG_LEN)
2319 return NULL;
2320
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002321 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2322 if (!bss)
2323 return NULL;
2324
John W. Linville24736702008-04-08 14:15:46 -04002325 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002326 if (!bss->mesh_cfg) {
2327 kfree(bss);
2328 return NULL;
2329 }
2330
2331 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2332 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2333 if (!bss->mesh_id) {
2334 kfree(bss->mesh_cfg);
2335 kfree(bss);
2336 return NULL;
2337 }
2338 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2339 }
2340
2341 atomic_inc(&bss->users);
2342 atomic_inc(&bss->users);
John W. Linville24736702008-04-08 14:15:46 -04002343 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002344 bss->mesh_id_len = mesh_id_len;
2345 bss->freq = freq;
2346 spin_lock_bh(&local->sta_bss_lock);
2347 /* TODO: order by RSSI? */
2348 list_add_tail(&bss->list, &local->sta_bss_list);
2349 __ieee80211_rx_bss_hash_add(dev, bss);
2350 spin_unlock_bh(&local->sta_bss_lock);
2351 return bss;
2352}
2353#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07002354
2355static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2356{
2357 kfree(bss->wpa_ie);
2358 kfree(bss->rsn_ie);
2359 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002360 kfree(bss->ht_ie);
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002361 kfree(bss->ht_add_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002362 kfree(bss_mesh_id(bss));
2363 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e822007-05-05 11:45:53 -07002364 kfree(bss);
2365}
2366
2367
Johannes Berg3e122be2008-07-09 14:40:34 +02002368static void ieee80211_rx_bss_put(struct ieee80211_local *local,
Jiri Bencf0706e822007-05-05 11:45:53 -07002369 struct ieee80211_sta_bss *bss)
2370{
Pavel Emelyanov1ebebea2008-04-23 11:47:15 +04002371 local_bh_disable();
2372 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2373 local_bh_enable();
2374 return;
2375 }
2376
Johannes Berg3e122be2008-07-09 14:40:34 +02002377 __ieee80211_rx_bss_hash_del(local, bss);
Jiri Bencf0706e822007-05-05 11:45:53 -07002378 list_del(&bss->list);
2379 spin_unlock_bh(&local->sta_bss_lock);
2380 ieee80211_rx_bss_free(bss);
2381}
2382
2383
Johannes Berg3e122be2008-07-09 14:40:34 +02002384void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
Jiri Bencf0706e822007-05-05 11:45:53 -07002385{
Jiri Bencf0706e822007-05-05 11:45:53 -07002386 spin_lock_init(&local->sta_bss_lock);
2387 INIT_LIST_HEAD(&local->sta_bss_list);
2388}
2389
2390
Johannes Berg3e122be2008-07-09 14:40:34 +02002391void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
Jiri Bencf0706e822007-05-05 11:45:53 -07002392{
Jiri Bencf0706e822007-05-05 11:45:53 -07002393 struct ieee80211_sta_bss *bss, *tmp;
2394
2395 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
Johannes Berg3e122be2008-07-09 14:40:34 +02002396 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e822007-05-05 11:45:53 -07002397}
2398
2399
Bruno Randolfa6072682008-02-18 11:21:15 +09002400static int ieee80211_sta_join_ibss(struct net_device *dev,
2401 struct ieee80211_if_sta *ifsta,
2402 struct ieee80211_sta_bss *bss)
2403{
2404 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2405 int res, rates, i, j;
2406 struct sk_buff *skb;
2407 struct ieee80211_mgmt *mgmt;
Bruno Randolfa6072682008-02-18 11:21:15 +09002408 u8 *pos;
2409 struct ieee80211_sub_if_data *sdata;
2410 struct ieee80211_supported_band *sband;
Dan Williams507b06d2008-06-03 23:39:55 -04002411 union iwreq_data wrqu;
Bruno Randolfa6072682008-02-18 11:21:15 +09002412
2413 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2414
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002415 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2416
Bruno Randolfa6072682008-02-18 11:21:15 +09002417 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002418 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002419
2420 if (local->ops->reset_tsf) {
2421 /* Reset own TSF to allow time synchronization work. */
2422 local->ops->reset_tsf(local_to_hw(local));
2423 }
2424 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
Johannes Berg9d139c82008-07-09 14:40:37 +02002425 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
Bruno Randolfa6072682008-02-18 11:21:15 +09002426 if (res)
2427 return res;
2428
2429 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2430
Bruno Randolfa6072682008-02-18 11:21:15 +09002431 sdata->drop_unencrypted = bss->capability &
2432 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2433
Assaf Kraussbe038b32008-06-05 19:55:21 +03002434 res = ieee80211_set_freq(dev, bss->freq);
Bruno Randolfa6072682008-02-18 11:21:15 +09002435
Assaf Kraussbe038b32008-06-05 19:55:21 +03002436 if (res)
2437 return res;
Bruno Randolfa6072682008-02-18 11:21:15 +09002438
Johannes Berg9d139c82008-07-09 14:40:37 +02002439 /* Build IBSS probe response */
Bruno Randolfa6072682008-02-18 11:21:15 +09002440 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
Johannes Berg9d139c82008-07-09 14:40:37 +02002441 if (skb) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002442 skb_reserve(skb, local->hw.extra_tx_headroom);
2443
2444 mgmt = (struct ieee80211_mgmt *)
2445 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2446 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2447 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
Johannes Berg9d139c82008-07-09 14:40:37 +02002448 IEEE80211_STYPE_PROBE_RESP);
Bruno Randolfa6072682008-02-18 11:21:15 +09002449 memset(mgmt->da, 0xff, ETH_ALEN);
2450 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2451 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2452 mgmt->u.beacon.beacon_int =
2453 cpu_to_le16(local->hw.conf.beacon_int);
Assaf Krauss4faeb862008-06-30 17:23:16 +08002454 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
Bruno Randolfa6072682008-02-18 11:21:15 +09002455 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2456
2457 pos = skb_put(skb, 2 + ifsta->ssid_len);
2458 *pos++ = WLAN_EID_SSID;
2459 *pos++ = ifsta->ssid_len;
2460 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2461
2462 rates = bss->supp_rates_len;
2463 if (rates > 8)
2464 rates = 8;
2465 pos = skb_put(skb, 2 + rates);
2466 *pos++ = WLAN_EID_SUPP_RATES;
2467 *pos++ = rates;
2468 memcpy(pos, bss->supp_rates, rates);
2469
2470 if (bss->band == IEEE80211_BAND_2GHZ) {
2471 pos = skb_put(skb, 2 + 1);
2472 *pos++ = WLAN_EID_DS_PARAMS;
2473 *pos++ = 1;
2474 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2475 }
2476
2477 pos = skb_put(skb, 2 + 2);
2478 *pos++ = WLAN_EID_IBSS_PARAMS;
2479 *pos++ = 2;
2480 /* FIX: set ATIM window based on scan results */
2481 *pos++ = 0;
2482 *pos++ = 0;
2483
2484 if (bss->supp_rates_len > 8) {
2485 rates = bss->supp_rates_len - 8;
2486 pos = skb_put(skb, 2 + rates);
2487 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2488 *pos++ = rates;
2489 memcpy(pos, &bss->supp_rates[8], rates);
2490 }
2491
Johannes Berg9d139c82008-07-09 14:40:37 +02002492 ifsta->probe_resp = skb;
Johannes Berge039fa42008-05-15 12:55:29 +02002493
Johannes Berg9d139c82008-07-09 14:40:37 +02002494 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
Bruno Randolfa6072682008-02-18 11:21:15 +09002495 }
2496
Johannes Berg9d139c82008-07-09 14:40:37 +02002497 rates = 0;
2498 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2499 for (i = 0; i < bss->supp_rates_len; i++) {
2500 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2501 for (j = 0; j < sband->n_bitrates; j++)
2502 if (sband->bitrates[j].bitrate == bitrate)
2503 rates |= BIT(j);
2504 }
2505 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2506
2507 ieee80211_sta_def_wmm_params(dev, bss, 1);
2508
Bruno Randolfa6072682008-02-18 11:21:15 +09002509 ifsta->state = IEEE80211_IBSS_JOINED;
2510 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2511
Dan Williams507b06d2008-06-03 23:39:55 -04002512 memset(&wrqu, 0, sizeof(wrqu));
2513 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2514 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Bruno Randolfa6072682008-02-18 11:21:15 +09002515
2516 return res;
2517}
2518
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002519u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2520 struct ieee802_11_elems *elems,
2521 enum ieee80211_band band)
2522{
2523 struct ieee80211_supported_band *sband;
2524 struct ieee80211_rate *bitrates;
2525 size_t num_rates;
2526 u64 supp_rates;
2527 int i, j;
2528 sband = local->hw.wiphy->bands[band];
2529
2530 if (!sband) {
2531 WARN_ON(1);
2532 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2533 }
2534
2535 bitrates = sband->bitrates;
2536 num_rates = sband->n_bitrates;
2537 supp_rates = 0;
2538 for (i = 0; i < elems->supp_rates_len +
2539 elems->ext_supp_rates_len; i++) {
2540 u8 rate = 0;
2541 int own_rate;
2542 if (i < elems->supp_rates_len)
2543 rate = elems->supp_rates[i];
2544 else if (elems->ext_supp_rates)
2545 rate = elems->ext_supp_rates
2546 [i - elems->supp_rates_len];
2547 own_rate = 5 * (rate & 0x7f);
2548 for (j = 0; j < num_rates; j++)
2549 if (bitrates[j].bitrate == own_rate)
2550 supp_rates |= BIT(j);
2551 }
2552 return supp_rates;
2553}
2554
Bruno Randolfa6072682008-02-18 11:21:15 +09002555
Jiri Bencf0706e822007-05-05 11:45:53 -07002556static void ieee80211_rx_bss_info(struct net_device *dev,
2557 struct ieee80211_mgmt *mgmt,
2558 size_t len,
2559 struct ieee80211_rx_status *rx_status,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002560 struct ieee802_11_elems *elems,
Jiri Bencf0706e822007-05-05 11:45:53 -07002561 int beacon)
2562{
2563 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01002564 int freq, clen;
Jiri Bencf0706e822007-05-05 11:45:53 -07002565 struct ieee80211_sta_bss *bss;
2566 struct sta_info *sta;
2567 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002568 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002569 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002570 DECLARE_MAC_BUF(mac);
2571 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e822007-05-05 11:45:53 -07002572
2573 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2574 return; /* ignore ProbeResp to foreign address */
2575
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002576 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
Jiri Bencf0706e822007-05-05 11:45:53 -07002577
Ester Kummerae6a44e2008-06-27 18:54:48 +03002578 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
2579 elems->mesh_config && mesh_matches_local(elems, dev)) {
2580 u64 rates = ieee80211_sta_get_rates(local, elems,
Johannes Berg902acc72008-02-23 15:17:19 +01002581 rx_status->band);
2582
2583 mesh_neighbour_update(mgmt->sa, rates, dev,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002584 mesh_peer_accepts_plinks(elems, dev));
Johannes Berg902acc72008-02-23 15:17:19 +01002585 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002586
Johannes Bergd0709a62008-02-25 16:27:46 +01002587 rcu_read_lock();
2588
Ester Kummerae6a44e2008-06-27 18:54:48 +03002589 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
Jiri Bencf0706e822007-05-05 11:45:53 -07002590 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2591 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002592 u64 prev_rates;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002593 u64 supp_rates = ieee80211_sta_get_rates(local, elems,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002594 rx_status->band);
Jiri Bencf0706e822007-05-05 11:45:53 -07002595
Johannes Berg8318d782008-01-24 19:38:38 +01002596 prev_rates = sta->supp_rates[rx_status->band];
2597 sta->supp_rates[rx_status->band] &= supp_rates;
2598 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002599 /* No matching rates - this should not really happen.
2600 * Make sure that at least one rate is marked
2601 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002602 sta->supp_rates[rx_status->band] =
2603 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e822007-05-05 11:45:53 -07002604 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002605 }
2606
Johannes Bergd0709a62008-02-25 16:27:46 +01002607 rcu_read_unlock();
2608
Ester Kummerae6a44e2008-06-27 18:54:48 +03002609 if (elems->ds_params && elems->ds_params_len == 1)
2610 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
Jiri Bencf0706e822007-05-05 11:45:53 -07002611 else
Johannes Berg8318d782008-01-24 19:38:38 +01002612 freq = rx_status->freq;
Jiri Bencf0706e822007-05-05 11:45:53 -07002613
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002614 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2615
2616 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2617 return;
2618
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002619#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002620 if (elems->mesh_config)
2621 bss = ieee80211_rx_mesh_bss_get(dev, elems->mesh_id,
2622 elems->mesh_id_len, elems->mesh_config, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002623 else
2624#endif
2625 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002626 elems->ssid, elems->ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002627 if (!bss) {
2628#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002629 if (elems->mesh_config)
2630 bss = ieee80211_rx_mesh_bss_add(dev, elems->mesh_id,
2631 elems->mesh_id_len, elems->mesh_config,
2632 elems->mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002633 else
2634#endif
2635 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002636 elems->ssid, elems->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07002637 if (!bss)
2638 return;
2639 } else {
2640#if 0
2641 /* TODO: order by RSSI? */
2642 spin_lock_bh(&local->sta_bss_lock);
2643 list_move_tail(&bss->list, &local->sta_bss_list);
2644 spin_unlock_bh(&local->sta_bss_lock);
2645#endif
2646 }
2647
Daniel Drake56282212007-07-10 19:32:10 +02002648 /* save the ERP value so that it is available at association time */
Ester Kummerae6a44e2008-06-27 18:54:48 +03002649 if (elems->erp_info && elems->erp_info_len >= 1) {
2650 bss->erp_value = elems->erp_info[0];
Daniel Drake56282212007-07-10 19:32:10 +02002651 bss->has_erp_value = 1;
2652 }
2653
Ester Kummerae6a44e2008-06-27 18:54:48 +03002654 if (elems->ht_cap_elem &&
2655 (!bss->ht_ie || bss->ht_ie_len != elems->ht_cap_elem_len ||
2656 memcmp(bss->ht_ie, elems->ht_cap_elem, elems->ht_cap_elem_len))) {
Johannes Berg30b89b02008-04-16 17:43:20 +02002657 kfree(bss->ht_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002658 bss->ht_ie = kmalloc(elems->ht_cap_elem_len + 2, GFP_ATOMIC);
Johannes Berg30b89b02008-04-16 17:43:20 +02002659 if (bss->ht_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002660 memcpy(bss->ht_ie, elems->ht_cap_elem - 2,
2661 elems->ht_cap_elem_len + 2);
2662 bss->ht_ie_len = elems->ht_cap_elem_len + 2;
Johannes Berg30b89b02008-04-16 17:43:20 +02002663 } else
2664 bss->ht_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002665 } else if (!elems->ht_cap_elem && bss->ht_ie) {
Johannes Berg30b89b02008-04-16 17:43:20 +02002666 kfree(bss->ht_ie);
2667 bss->ht_ie = NULL;
2668 bss->ht_ie_len = 0;
2669 }
2670
Ester Kummerae6a44e2008-06-27 18:54:48 +03002671 if (elems->ht_info_elem &&
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002672 (!bss->ht_add_ie ||
Ester Kummerae6a44e2008-06-27 18:54:48 +03002673 bss->ht_add_ie_len != elems->ht_info_elem_len ||
2674 memcmp(bss->ht_add_ie, elems->ht_info_elem,
2675 elems->ht_info_elem_len))) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002676 kfree(bss->ht_add_ie);
2677 bss->ht_add_ie =
Ester Kummerae6a44e2008-06-27 18:54:48 +03002678 kmalloc(elems->ht_info_elem_len + 2, GFP_ATOMIC);
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002679 if (bss->ht_add_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002680 memcpy(bss->ht_add_ie, elems->ht_info_elem - 2,
2681 elems->ht_info_elem_len + 2);
2682 bss->ht_add_ie_len = elems->ht_info_elem_len + 2;
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002683 } else
2684 bss->ht_add_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002685 } else if (!elems->ht_info_elem && bss->ht_add_ie) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002686 kfree(bss->ht_add_ie);
2687 bss->ht_add_ie = NULL;
2688 bss->ht_add_ie_len = 0;
2689 }
2690
Jiri Bencf0706e822007-05-05 11:45:53 -07002691 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2692 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e822007-05-05 11:45:53 -07002693
Emmanuel Grumbach98f7dfd2008-07-18 13:52:59 +08002694 if (elems->tim) {
2695 struct ieee80211_tim_ie *tim_ie =
2696 (struct ieee80211_tim_ie *)elems->tim;
2697 bss->dtim_period = tim_ie->dtim_period;
2698 }
2699
2700 /* set default value for buggy APs */
2701 if (!elems->tim || bss->dtim_period == 0)
2702 bss->dtim_period = 1;
2703
Jiri Bencf0706e822007-05-05 11:45:53 -07002704 bss->supp_rates_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002705 if (elems->supp_rates) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002706 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002707 if (clen > elems->supp_rates_len)
2708 clen = elems->supp_rates_len;
2709 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
Jiri Bencf0706e822007-05-05 11:45:53 -07002710 clen);
2711 bss->supp_rates_len += clen;
2712 }
Ester Kummerae6a44e2008-06-27 18:54:48 +03002713 if (elems->ext_supp_rates) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002714 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002715 if (clen > elems->ext_supp_rates_len)
2716 clen = elems->ext_supp_rates_len;
Jiri Bencf0706e822007-05-05 11:45:53 -07002717 memcpy(&bss->supp_rates[bss->supp_rates_len],
Ester Kummerae6a44e2008-06-27 18:54:48 +03002718 elems->ext_supp_rates, clen);
Jiri Bencf0706e822007-05-05 11:45:53 -07002719 bss->supp_rates_len += clen;
2720 }
2721
Johannes Berg30b89b02008-04-16 17:43:20 +02002722 bss->band = rx_status->band;
2723
2724 bss->timestamp = beacon_timestamp;
2725 bss->last_update = jiffies;
Johannes Berg30b89b02008-04-16 17:43:20 +02002726 bss->signal = rx_status->signal;
2727 bss->noise = rx_status->noise;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002728 bss->qual = rx_status->qual;
Johannes Berg30b89b02008-04-16 17:43:20 +02002729 if (!beacon && !bss->probe_resp)
2730 bss->probe_resp = true;
2731
2732 /*
2733 * In STA mode, the remaining parameters should not be overridden
2734 * by beacons because they're not necessarily accurate there.
2735 */
2736 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2737 bss->probe_resp && beacon) {
Johannes Berg3e122be2008-07-09 14:40:34 +02002738 ieee80211_rx_bss_put(local, bss);
Johannes Berg30b89b02008-04-16 17:43:20 +02002739 return;
2740 }
2741
Ester Kummerae6a44e2008-06-27 18:54:48 +03002742 if (elems->wpa &&
2743 (!bss->wpa_ie || bss->wpa_ie_len != elems->wpa_len ||
2744 memcmp(bss->wpa_ie, elems->wpa, elems->wpa_len))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002745 kfree(bss->wpa_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002746 bss->wpa_ie = kmalloc(elems->wpa_len + 2, GFP_ATOMIC);
Jiri Bencf0706e822007-05-05 11:45:53 -07002747 if (bss->wpa_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002748 memcpy(bss->wpa_ie, elems->wpa - 2, elems->wpa_len + 2);
2749 bss->wpa_ie_len = elems->wpa_len + 2;
Jiri Bencf0706e822007-05-05 11:45:53 -07002750 } else
2751 bss->wpa_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002752 } else if (!elems->wpa && bss->wpa_ie) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002753 kfree(bss->wpa_ie);
2754 bss->wpa_ie = NULL;
2755 bss->wpa_ie_len = 0;
2756 }
2757
Ester Kummerae6a44e2008-06-27 18:54:48 +03002758 if (elems->rsn &&
2759 (!bss->rsn_ie || bss->rsn_ie_len != elems->rsn_len ||
2760 memcmp(bss->rsn_ie, elems->rsn, elems->rsn_len))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002761 kfree(bss->rsn_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002762 bss->rsn_ie = kmalloc(elems->rsn_len + 2, GFP_ATOMIC);
Jiri Bencf0706e822007-05-05 11:45:53 -07002763 if (bss->rsn_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002764 memcpy(bss->rsn_ie, elems->rsn - 2, elems->rsn_len + 2);
2765 bss->rsn_ie_len = elems->rsn_len + 2;
Jiri Bencf0706e822007-05-05 11:45:53 -07002766 } else
2767 bss->rsn_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002768 } else if (!elems->rsn && bss->rsn_ie) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002769 kfree(bss->rsn_ie);
2770 bss->rsn_ie = NULL;
2771 bss->rsn_ie_len = 0;
2772 }
2773
Johannes Berg30b89b02008-04-16 17:43:20 +02002774 /*
2775 * Cf.
2776 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2777 *
2778 * quoting:
2779 *
2780 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2781 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2782 * Alliance (September 1, 2004) is incorporated by reference herein.
2783 * The inclusion of the WMM Parameters in probe responses and
2784 * association responses is mandatory for WMM enabled networks. The
2785 * inclusion of the WMM Parameters in beacons, however, is optional.
2786 */
2787
Ester Kummerae6a44e2008-06-27 18:54:48 +03002788 if (elems->wmm_param &&
2789 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_param_len ||
2790 memcmp(bss->wmm_ie, elems->wmm_param, elems->wmm_param_len))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002791 kfree(bss->wmm_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002792 bss->wmm_ie = kmalloc(elems->wmm_param_len + 2, GFP_ATOMIC);
Jiri Bencf0706e822007-05-05 11:45:53 -07002793 if (bss->wmm_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002794 memcpy(bss->wmm_ie, elems->wmm_param - 2,
2795 elems->wmm_param_len + 2);
2796 bss->wmm_ie_len = elems->wmm_param_len + 2;
Jiri Bencf0706e822007-05-05 11:45:53 -07002797 } else
2798 bss->wmm_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002799 } else if (elems->wmm_info &&
2800 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_info_len ||
2801 memcmp(bss->wmm_ie, elems->wmm_info,
2802 elems->wmm_info_len))) {
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002803 /* As for certain AP's Fifth bit is not set in WMM IE in
2804 * beacon frames.So while parsing the beacon frame the
2805 * wmm_info structure is used instead of wmm_param.
2806 * wmm_info structure was never used to set bss->wmm_ie.
2807 * This code fixes this problem by copying the WME
2808 * information from wmm_info to bss->wmm_ie and enabling
2809 * n-band association.
2810 */
2811 kfree(bss->wmm_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002812 bss->wmm_ie = kmalloc(elems->wmm_info_len + 2, GFP_ATOMIC);
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002813 if (bss->wmm_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002814 memcpy(bss->wmm_ie, elems->wmm_info - 2,
2815 elems->wmm_info_len + 2);
2816 bss->wmm_ie_len = elems->wmm_info_len + 2;
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002817 } else
2818 bss->wmm_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002819 } else if (!elems->wmm_param && !elems->wmm_info && bss->wmm_ie) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002820 kfree(bss->wmm_ie);
2821 bss->wmm_ie = NULL;
2822 bss->wmm_ie_len = 0;
2823 }
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002824
2825 /* check if we need to merge IBSS */
2826 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2827 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002828 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002829 bss->freq == local->oper_channel->center_freq &&
Ester Kummerae6a44e2008-06-27 18:54:48 +03002830 elems->ssid_len == sdata->u.sta.ssid_len &&
2831 memcmp(elems->ssid, sdata->u.sta.ssid,
2832 sdata->u.sta.ssid_len) == 0) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002833 if (rx_status->flag & RX_FLAG_TSFT) {
2834 /* in order for correct IBSS merging we need mactime
2835 *
2836 * since mactime is defined as the time the first data
2837 * symbol of the frame hits the PHY, and the timestamp
2838 * of the beacon is defined as "the time that the data
2839 * symbol containing the first bit of the timestamp is
2840 * transmitted to the PHY plus the transmitting STA’s
2841 * delays through its local PHY from the MAC-PHY
2842 * interface to its interface with the WM"
2843 * (802.11 11.1.2) - equals the time this bit arrives at
2844 * the receiver - we have to take into account the
2845 * offset between the two.
2846 * e.g: at 1 MBit that means mactime is 192 usec earlier
2847 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2848 */
2849 int rate = local->hw.wiphy->bands[rx_status->band]->
2850 bitrates[rx_status->rate_idx].bitrate;
2851 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2852 } else if (local && local->ops && local->ops->get_tsf)
2853 /* second best option: get current TSF */
2854 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2855 else
2856 /* can't merge without knowing the TSF */
2857 rx_timestamp = -1LLU;
2858#ifdef CONFIG_MAC80211_IBSS_DEBUG
2859 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2860 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2861 print_mac(mac, mgmt->sa),
2862 print_mac(mac2, mgmt->bssid),
2863 (unsigned long long)rx_timestamp,
2864 (unsigned long long)beacon_timestamp,
2865 (unsigned long long)(rx_timestamp - beacon_timestamp),
2866 jiffies);
2867#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2868 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002869#ifndef CONFIG_MAC80211_IBSS_DEBUG
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002870 printk(KERN_DEBUG "%s: beacon TSF higher than "
2871 "local TSF - IBSS merge with BSSID %s\n",
2872 dev->name, print_mac(mac, mgmt->bssid));
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002873#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002874 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2875 ieee80211_ibss_add_sta(dev, NULL,
Vladimir Koutny87291c02008-06-13 16:50:44 +02002876 mgmt->bssid, mgmt->sa,
2877 BIT(rx_status->rate_idx));
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002878 }
2879 }
2880
Johannes Berg3e122be2008-07-09 14:40:34 +02002881 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e822007-05-05 11:45:53 -07002882}
2883
2884
2885static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2886 struct ieee80211_mgmt *mgmt,
2887 size_t len,
2888 struct ieee80211_rx_status *rx_status)
2889{
Ester Kummerae6a44e2008-06-27 18:54:48 +03002890 size_t baselen;
2891 struct ieee802_11_elems elems;
2892
2893 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2894 if (baselen > len)
2895 return;
2896
2897 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2898 &elems);
2899
2900 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -07002901}
2902
2903
2904static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2905 struct ieee80211_mgmt *mgmt,
2906 size_t len,
2907 struct ieee80211_rx_status *rx_status)
2908{
Jiri Bencf0706e822007-05-05 11:45:53 -07002909 struct ieee80211_sub_if_data *sdata;
2910 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e822007-05-05 11:45:53 -07002911 size_t baselen;
2912 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002913 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2914 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002915 u32 changed = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07002916
Ester Kummerae6a44e2008-06-27 18:54:48 +03002917 /* Process beacon from the current BSS */
2918 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2919 if (baselen > len)
2920 return;
2921
2922 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2923
2924 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 1);
Jiri Bencf0706e822007-05-05 11:45:53 -07002925
2926 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002927 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07002928 return;
2929 ifsta = &sdata->u.sta;
2930
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002931 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e822007-05-05 11:45:53 -07002932 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2933 return;
2934
Johannes Berg3434fbd2008-05-03 00:59:37 +02002935 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2936 elems.wmm_param_len);
Reinette Chatred18ef292008-04-09 16:56:15 -07002937
2938 /* Do not send changes to driver if we are scanning. This removes
2939 * requirement that driver's bss_info_changed function needs to be
2940 * atomic. */
2941 if (local->sta_sw_scanning || local->sta_hw_scanning)
2942 return;
2943
Daniel Drake56282212007-07-10 19:32:10 +02002944 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002945 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
John W. Linville50c4afb2008-04-15 14:09:27 -04002946 else {
2947 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2948 changed |= ieee80211_handle_protect_preamb(sdata, false,
2949 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2950 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002951
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002952 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07002953 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002954 struct ieee80211_ht_bss_info bss_info;
2955
2956 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2957 (struct ieee80211_ht_addt_info *)
2958 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002959 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2960 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002961 }
2962
Johannes Berg471b3ef2007-12-28 14:32:58 +01002963 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e822007-05-05 11:45:53 -07002964}
2965
2966
2967static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2968 struct ieee80211_if_sta *ifsta,
2969 struct ieee80211_mgmt *mgmt,
2970 size_t len,
2971 struct ieee80211_rx_status *rx_status)
2972{
2973 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2974 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2975 int tx_last_beacon;
2976 struct sk_buff *skb;
2977 struct ieee80211_mgmt *resp;
2978 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002979 DECLARE_MAC_BUF(mac);
2980#ifdef CONFIG_MAC80211_IBSS_DEBUG
2981 DECLARE_MAC_BUF(mac2);
2982 DECLARE_MAC_BUF(mac3);
2983#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07002984
Johannes Berg51fb61e2007-12-19 01:31:27 +01002985 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e822007-05-05 11:45:53 -07002986 ifsta->state != IEEE80211_IBSS_JOINED ||
2987 len < 24 + 2 || !ifsta->probe_resp)
2988 return;
2989
2990 if (local->ops->tx_last_beacon)
2991 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2992 else
2993 tx_last_beacon = 1;
2994
2995#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002996 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2997 "%s (tx_last_beacon=%d)\n",
2998 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2999 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e822007-05-05 11:45:53 -07003000#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3001
3002 if (!tx_last_beacon)
3003 return;
3004
3005 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
3006 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
3007 return;
3008
3009 end = ((u8 *) mgmt) + len;
3010 pos = mgmt->u.probe_req.variable;
3011 if (pos[0] != WLAN_EID_SSID ||
3012 pos + 2 + pos[1] > end) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003013#ifdef CONFIG_MAC80211_IBSS_DEBUG
3014 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
3015 "from %s\n",
3016 dev->name, print_mac(mac, mgmt->sa));
3017#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07003018 return;
3019 }
3020 if (pos[1] != 0 &&
3021 (pos[1] != ifsta->ssid_len ||
3022 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
3023 /* Ignore ProbeReq for foreign SSID */
3024 return;
3025 }
3026
3027 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02003028 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07003029 if (!skb)
3030 return;
3031
3032 resp = (struct ieee80211_mgmt *) skb->data;
3033 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3034#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003035 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
3036 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e822007-05-05 11:45:53 -07003037#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3038 ieee80211_sta_tx(dev, skb, 0);
3039}
3040
Johannes Berg4e20cb22007-12-19 01:31:24 +01003041static void ieee80211_rx_mgmt_action(struct net_device *dev,
3042 struct ieee80211_if_sta *ifsta,
3043 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003044 size_t len,
3045 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003046{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003047 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Assaf Kraussb6623482008-06-16 16:09:49 +03003048 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003049
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003050 if (len < IEEE80211_MIN_ACTION_SIZE)
3051 return;
3052
3053 switch (mgmt->u.action.category) {
Assaf Kraussb6623482008-06-16 16:09:49 +03003054 case WLAN_CATEGORY_SPECTRUM_MGMT:
3055 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
3056 break;
3057 switch (mgmt->u.action.u.chan_switch.action_code) {
3058 case WLAN_ACTION_SPCT_MSR_REQ:
3059 if (len < (IEEE80211_MIN_ACTION_SIZE +
3060 sizeof(mgmt->u.action.u.measurement)))
3061 break;
3062 ieee80211_sta_process_measurement_req(dev, mgmt, len);
3063 break;
3064 }
3065 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003066 case WLAN_CATEGORY_BACK:
3067 switch (mgmt->u.action.u.addba_req.action_code) {
3068 case WLAN_ACTION_ADDBA_REQ:
3069 if (len < (IEEE80211_MIN_ACTION_SIZE +
3070 sizeof(mgmt->u.action.u.addba_req)))
3071 break;
3072 ieee80211_sta_process_addba_request(dev, mgmt, len);
3073 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02003074 case WLAN_ACTION_ADDBA_RESP:
3075 if (len < (IEEE80211_MIN_ACTION_SIZE +
3076 sizeof(mgmt->u.action.u.addba_resp)))
3077 break;
3078 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3079 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02003080 case WLAN_ACTION_DELBA:
3081 if (len < (IEEE80211_MIN_ACTION_SIZE +
3082 sizeof(mgmt->u.action.u.delba)))
3083 break;
3084 ieee80211_sta_process_delba(dev, mgmt, len);
3085 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003086 }
3087 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003088 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003089 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003090 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3091 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003092 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003093 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003094 mesh_rx_path_sel_frame(dev, mgmt, len);
3095 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003096 }
3097}
Jiri Bencf0706e822007-05-05 11:45:53 -07003098
3099void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3100 struct ieee80211_rx_status *rx_status)
3101{
3102 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3103 struct ieee80211_sub_if_data *sdata;
3104 struct ieee80211_if_sta *ifsta;
3105 struct ieee80211_mgmt *mgmt;
3106 u16 fc;
3107
3108 if (skb->len < 24)
3109 goto fail;
3110
3111 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3112 ifsta = &sdata->u.sta;
3113
3114 mgmt = (struct ieee80211_mgmt *) skb->data;
3115 fc = le16_to_cpu(mgmt->frame_control);
3116
3117 switch (fc & IEEE80211_FCTL_STYPE) {
3118 case IEEE80211_STYPE_PROBE_REQ:
3119 case IEEE80211_STYPE_PROBE_RESP:
3120 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003121 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e822007-05-05 11:45:53 -07003122 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3123 case IEEE80211_STYPE_AUTH:
3124 case IEEE80211_STYPE_ASSOC_RESP:
3125 case IEEE80211_STYPE_REASSOC_RESP:
3126 case IEEE80211_STYPE_DEAUTH:
3127 case IEEE80211_STYPE_DISASSOC:
3128 skb_queue_tail(&ifsta->skb_queue, skb);
3129 queue_work(local->hw.workqueue, &ifsta->work);
3130 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07003131 }
3132
3133 fail:
3134 kfree_skb(skb);
3135}
3136
3137
3138static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3139 struct sk_buff *skb)
3140{
3141 struct ieee80211_rx_status *rx_status;
3142 struct ieee80211_sub_if_data *sdata;
3143 struct ieee80211_if_sta *ifsta;
3144 struct ieee80211_mgmt *mgmt;
3145 u16 fc;
3146
3147 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3148 ifsta = &sdata->u.sta;
3149
3150 rx_status = (struct ieee80211_rx_status *) skb->cb;
3151 mgmt = (struct ieee80211_mgmt *) skb->data;
3152 fc = le16_to_cpu(mgmt->frame_control);
3153
3154 switch (fc & IEEE80211_FCTL_STYPE) {
3155 case IEEE80211_STYPE_PROBE_REQ:
3156 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3157 rx_status);
3158 break;
3159 case IEEE80211_STYPE_PROBE_RESP:
3160 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3161 break;
3162 case IEEE80211_STYPE_BEACON:
3163 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3164 break;
3165 case IEEE80211_STYPE_AUTH:
3166 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3167 break;
3168 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003169 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -07003170 break;
3171 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003172 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e822007-05-05 11:45:53 -07003173 break;
3174 case IEEE80211_STYPE_DEAUTH:
3175 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3176 break;
3177 case IEEE80211_STYPE_DISASSOC:
3178 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3179 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003180 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003181 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003182 break;
Jiri Bencf0706e822007-05-05 11:45:53 -07003183 }
3184
3185 kfree_skb(skb);
3186}
3187
3188
Johannes Berg9ae54c82008-01-31 19:48:20 +01003189ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003190ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3191 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e822007-05-05 11:45:53 -07003192{
3193 struct ieee80211_mgmt *mgmt;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003194 __le16 fc;
Jiri Bencf0706e822007-05-05 11:45:53 -07003195
Zhu Yiece8edd2007-11-22 10:53:21 +08003196 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003197 return RX_DROP_UNUSABLE;
Jiri Bencf0706e822007-05-05 11:45:53 -07003198
3199 mgmt = (struct ieee80211_mgmt *) skb->data;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003200 fc = mgmt->frame_control;
Jiri Bencf0706e822007-05-05 11:45:53 -07003201
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003202 if (ieee80211_is_ctl(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +01003203 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003204
3205 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003206 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003207
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003208 if (ieee80211_is_probe_resp(fc)) {
3209 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3210 dev_kfree_skb(skb);
3211 return RX_QUEUED;
Jiri Bencf0706e822007-05-05 11:45:53 -07003212 }
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003213
3214 if (ieee80211_is_beacon(fc)) {
3215 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3216 dev_kfree_skb(skb);
3217 return RX_QUEUED;
3218 }
3219
Johannes Berg9ae54c82008-01-31 19:48:20 +01003220 return RX_CONTINUE;
Jiri Bencf0706e822007-05-05 11:45:53 -07003221}
3222
3223
3224static int ieee80211_sta_active_ibss(struct net_device *dev)
3225{
3226 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3227 int active = 0;
3228 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003229 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003230
Johannes Bergd0709a62008-02-25 16:27:46 +01003231 rcu_read_lock();
3232
3233 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3234 if (sta->sdata == sdata &&
Jiri Bencf0706e822007-05-05 11:45:53 -07003235 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3236 jiffies)) {
3237 active++;
3238 break;
3239 }
3240 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003241
3242 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07003243
3244 return active;
3245}
3246
3247
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003248static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e822007-05-05 11:45:53 -07003249{
3250 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3251 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003252 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003253 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003254 unsigned long flags;
Jiri Bencf0706e822007-05-05 11:45:53 -07003255
Johannes Bergd0709a62008-02-25 16:27:46 +01003256 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e822007-05-05 11:45:53 -07003257 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003258 if (time_after(jiffies, sta->last_rx + exp_time)) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003259#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003260 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3261 dev->name, print_mac(mac, sta->addr));
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003262#endif
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003263 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003264 if (sta)
3265 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e822007-05-05 11:45:53 -07003266 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003267 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003268
Johannes Bergd0709a62008-02-25 16:27:46 +01003269 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3270 sta_info_destroy(sta);
Jiri Bencf0706e822007-05-05 11:45:53 -07003271}
3272
3273
3274static void ieee80211_sta_merge_ibss(struct net_device *dev,
3275 struct ieee80211_if_sta *ifsta)
3276{
3277 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3278
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003279 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e822007-05-05 11:45:53 -07003280 if (ieee80211_sta_active_ibss(dev))
3281 return;
3282
3283 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3284 "IBSS networks with same SSID (merge)\n", dev->name);
3285 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3286}
3287
3288
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003289#ifdef CONFIG_MAC80211_MESH
3290static void ieee80211_mesh_housekeeping(struct net_device *dev,
3291 struct ieee80211_if_sta *ifsta)
3292{
3293 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3294 bool free_plinks;
3295
3296 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3297 mesh_path_expire(dev);
3298
3299 free_plinks = mesh_plink_availables(sdata);
3300 if (free_plinks != sdata->u.sta.accepting_plinks)
Johannes Berg9d139c82008-07-09 14:40:37 +02003301 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003302
3303 mod_timer(&ifsta->timer, jiffies +
3304 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3305}
3306
3307
3308void ieee80211_start_mesh(struct net_device *dev)
3309{
3310 struct ieee80211_if_sta *ifsta;
3311 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3312 ifsta = &sdata->u.sta;
3313 ifsta->state = IEEE80211_MESH_UP;
3314 ieee80211_sta_timer((unsigned long)sdata);
Luis Carlos Cobo56a6d132008-07-29 19:59:31 +02003315 ieee80211_if_config(sdata, IEEE80211_IFCC_BEACON);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003316}
3317#endif
3318
3319
Jiri Bencf0706e822007-05-05 11:45:53 -07003320void ieee80211_sta_timer(unsigned long data)
3321{
3322 struct ieee80211_sub_if_data *sdata =
3323 (struct ieee80211_sub_if_data *) data;
3324 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3325 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3326
3327 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3328 queue_work(local->hw.workqueue, &ifsta->work);
3329}
3330
Jiri Bencf0706e822007-05-05 11:45:53 -07003331void ieee80211_sta_work(struct work_struct *work)
3332{
3333 struct ieee80211_sub_if_data *sdata =
3334 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3335 struct net_device *dev = sdata->dev;
3336 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3337 struct ieee80211_if_sta *ifsta;
3338 struct sk_buff *skb;
3339
3340 if (!netif_running(dev))
3341 return;
3342
Zhu Yiece8edd2007-11-22 10:53:21 +08003343 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07003344 return;
3345
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003346 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3347 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3348 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
Jiri Bencf0706e822007-05-05 11:45:53 -07003349 return;
Jiri Bencf0706e822007-05-05 11:45:53 -07003350 ifsta = &sdata->u.sta;
3351
3352 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3353 ieee80211_sta_rx_queued_mgmt(dev, skb);
3354
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003355#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003356 if (ifsta->preq_queue_len &&
3357 time_after(jiffies,
3358 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003359 mesh_path_start_discovery(dev);
3360#endif
3361
Jiri Bencf0706e822007-05-05 11:45:53 -07003362 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3363 ifsta->state != IEEE80211_ASSOCIATE &&
3364 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003365 if (ifsta->scan_ssid_len)
3366 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3367 else
3368 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e822007-05-05 11:45:53 -07003369 return;
3370 }
3371
3372 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3373 if (ieee80211_sta_config_auth(dev, ifsta))
3374 return;
3375 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3376 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3377 return;
3378
3379 switch (ifsta->state) {
3380 case IEEE80211_DISABLED:
3381 break;
3382 case IEEE80211_AUTHENTICATE:
3383 ieee80211_authenticate(dev, ifsta);
3384 break;
3385 case IEEE80211_ASSOCIATE:
3386 ieee80211_associate(dev, ifsta);
3387 break;
3388 case IEEE80211_ASSOCIATED:
3389 ieee80211_associated(dev, ifsta);
3390 break;
3391 case IEEE80211_IBSS_SEARCH:
3392 ieee80211_sta_find_ibss(dev, ifsta);
3393 break;
3394 case IEEE80211_IBSS_JOINED:
3395 ieee80211_sta_merge_ibss(dev, ifsta);
3396 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003397#ifdef CONFIG_MAC80211_MESH
3398 case IEEE80211_MESH_UP:
3399 ieee80211_mesh_housekeeping(dev, ifsta);
3400 break;
3401#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07003402 default:
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003403 WARN_ON(1);
Jiri Bencf0706e822007-05-05 11:45:53 -07003404 break;
3405 }
3406
3407 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3408 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3409 "mixed-cell disabled - disassociate\n", dev->name);
3410
3411 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3412 ieee80211_set_disassoc(dev, ifsta, 0);
3413 }
3414}
3415
3416
3417static void ieee80211_sta_reset_auth(struct net_device *dev,
3418 struct ieee80211_if_sta *ifsta)
3419{
3420 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3421
3422 if (local->ops->reset_tsf) {
3423 /* Reset own TSF to allow time synchronization work. */
3424 local->ops->reset_tsf(local_to_hw(local));
3425 }
3426
3427 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3428
3429
3430 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3431 ifsta->auth_alg = WLAN_AUTH_OPEN;
3432 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3433 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3434 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3435 ifsta->auth_alg = WLAN_AUTH_LEAP;
3436 else
3437 ifsta->auth_alg = WLAN_AUTH_OPEN;
Jiri Bencf0706e822007-05-05 11:45:53 -07003438 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003439 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3440 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07003441 netif_carrier_off(dev);
3442}
3443
3444
3445void ieee80211_sta_req_auth(struct net_device *dev,
3446 struct ieee80211_if_sta *ifsta)
3447{
3448 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3449 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3450
Johannes Berg51fb61e2007-12-19 01:31:27 +01003451 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07003452 return;
3453
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003454 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3455 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3456 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3457 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003458 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3459 queue_work(local->hw.workqueue, &ifsta->work);
3460 }
3461}
3462
3463static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3464 const char *ssid, int ssid_len)
3465{
3466 int tmp, hidden_ssid;
3467
Michael Wu48225702007-10-19 17:14:36 -04003468 if (ssid_len == ifsta->ssid_len &&
3469 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e822007-05-05 11:45:53 -07003470 return 1;
3471
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003472 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e822007-05-05 11:45:53 -07003473 return 0;
3474
3475 hidden_ssid = 1;
3476 tmp = ssid_len;
3477 while (tmp--) {
3478 if (ssid[tmp] != '\0') {
3479 hidden_ssid = 0;
3480 break;
3481 }
3482 }
3483
3484 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3485 return 1;
3486
3487 if (ssid_len == 1 && ssid[0] == ' ')
3488 return 1;
3489
3490 return 0;
3491}
3492
3493static int ieee80211_sta_config_auth(struct net_device *dev,
3494 struct ieee80211_if_sta *ifsta)
3495{
3496 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3497 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3498 struct ieee80211_sta_bss *bss, *selected = NULL;
3499 int top_rssi = 0, freq;
3500
Jiri Bencf0706e822007-05-05 11:45:53 -07003501 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003502 freq = local->oper_channel->center_freq;
Jiri Bencf0706e822007-05-05 11:45:53 -07003503 list_for_each_entry(bss, &local->sta_bss_list, list) {
3504 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3505 continue;
3506
Abhijeet Kolekar34a961f2008-05-09 09:35:41 -07003507 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3508 IEEE80211_STA_AUTO_BSSID_SEL |
3509 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3510 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3511 !!sdata->default_key))
Jiri Bencf0706e822007-05-05 11:45:53 -07003512 continue;
3513
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003514 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3515 bss->freq != freq)
Jiri Bencf0706e822007-05-05 11:45:53 -07003516 continue;
3517
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003518 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07003519 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3520 continue;
3521
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003522 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07003523 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3524 continue;
3525
Bruno Randolf566bfe52008-05-08 19:15:40 +02003526 if (!selected || top_rssi < bss->signal) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003527 selected = bss;
Bruno Randolf566bfe52008-05-08 19:15:40 +02003528 top_rssi = bss->signal;
Jiri Bencf0706e822007-05-05 11:45:53 -07003529 }
3530 }
3531 if (selected)
3532 atomic_inc(&selected->users);
3533 spin_unlock_bh(&local->sta_bss_lock);
3534
3535 if (selected) {
Assaf Kraussbe038b32008-06-05 19:55:21 +03003536 ieee80211_set_freq(dev, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003537 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e822007-05-05 11:45:53 -07003538 ieee80211_sta_set_ssid(dev, selected->ssid,
3539 selected->ssid_len);
3540 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003541 ieee80211_sta_def_wmm_params(dev, selected, 0);
Johannes Berg3e122be2008-07-09 14:40:34 +02003542 ieee80211_rx_bss_put(local, selected);
Jiri Bencf0706e822007-05-05 11:45:53 -07003543 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e822007-05-05 11:45:53 -07003544 ieee80211_sta_reset_auth(dev, ifsta);
3545 return 0;
3546 } else {
3547 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003548 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003549 ieee80211_sta_start_scan(dev, NULL, 0);
3550 else
3551 ieee80211_sta_start_scan(dev, ifsta->ssid,
3552 ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003553 ifsta->state = IEEE80211_AUTHENTICATE;
3554 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3555 } else
3556 ifsta->state = IEEE80211_DISABLED;
3557 }
Jiri Bencf0706e822007-05-05 11:45:53 -07003558 return -1;
3559}
3560
Jiri Bencf0706e822007-05-05 11:45:53 -07003561
3562static int ieee80211_sta_create_ibss(struct net_device *dev,
3563 struct ieee80211_if_sta *ifsta)
3564{
3565 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3566 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003567 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003568 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -07003569 u8 bssid[ETH_ALEN], *pos;
3570 int i;
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003571 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003572 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07003573
3574#if 0
3575 /* Easier testing, use fixed BSSID. */
3576 memset(bssid, 0xfe, ETH_ALEN);
3577#else
3578 /* Generate random, not broadcast, locally administered BSSID. Mix in
3579 * own MAC address to make sure that devices that do not have proper
3580 * random number generator get different BSSID. */
3581 get_random_bytes(bssid, ETH_ALEN);
3582 for (i = 0; i < ETH_ALEN; i++)
3583 bssid[i] ^= dev->dev_addr[i];
3584 bssid[0] &= ~0x01;
3585 bssid[0] |= 0x02;
3586#endif
3587
Joe Perches0795af52007-10-03 17:59:30 -07003588 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3589 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003590
Johannes Berg8318d782008-01-24 19:38:38 +01003591 bss = ieee80211_rx_bss_add(dev, bssid,
3592 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003593 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07003594 if (!bss)
3595 return -ENOMEM;
3596
Johannes Berg8318d782008-01-24 19:38:38 +01003597 bss->band = local->hw.conf.channel->band;
3598 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e822007-05-05 11:45:53 -07003599
3600 if (local->hw.conf.beacon_int == 0)
Tomas Winklerdc0ae302008-06-12 22:38:37 +03003601 local->hw.conf.beacon_int = 100;
Jiri Bencf0706e822007-05-05 11:45:53 -07003602 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e822007-05-05 11:45:53 -07003603 bss->last_update = jiffies;
3604 bss->capability = WLAN_CAPABILITY_IBSS;
Johannes Berg988c0f72008-04-17 19:21:22 +02003605
3606 if (sdata->default_key)
Jiri Bencf0706e822007-05-05 11:45:53 -07003607 bss->capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +02003608 else
Jiri Bencf0706e822007-05-05 11:45:53 -07003609 sdata->drop_unencrypted = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02003610
Johannes Berg8318d782008-01-24 19:38:38 +01003611 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e822007-05-05 11:45:53 -07003612 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003613 for (i = 0; i < sband->n_bitrates; i++) {
3614 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e822007-05-05 11:45:53 -07003615 *pos++ = (u8) (rate / 5);
3616 }
3617
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003618 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
Johannes Berg3e122be2008-07-09 14:40:34 +02003619 ieee80211_rx_bss_put(local, bss);
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003620 return ret;
Jiri Bencf0706e822007-05-05 11:45:53 -07003621}
3622
3623
3624static int ieee80211_sta_find_ibss(struct net_device *dev,
3625 struct ieee80211_if_sta *ifsta)
3626{
3627 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3628 struct ieee80211_sta_bss *bss;
3629 int found = 0;
3630 u8 bssid[ETH_ALEN];
3631 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003632 DECLARE_MAC_BUF(mac);
3633 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e822007-05-05 11:45:53 -07003634
3635 if (ifsta->ssid_len == 0)
3636 return -EINVAL;
3637
3638 active_ibss = ieee80211_sta_active_ibss(dev);
3639#ifdef CONFIG_MAC80211_IBSS_DEBUG
3640 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3641 dev->name, active_ibss);
3642#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3643 spin_lock_bh(&local->sta_bss_lock);
3644 list_for_each_entry(bss, &local->sta_bss_list, list) {
3645 if (ifsta->ssid_len != bss->ssid_len ||
3646 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3647 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3648 continue;
3649#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003650 printk(KERN_DEBUG " bssid=%s found\n",
3651 print_mac(mac, bss->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003652#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3653 memcpy(bssid, bss->bssid, ETH_ALEN);
3654 found = 1;
3655 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3656 break;
3657 }
3658 spin_unlock_bh(&local->sta_bss_lock);
3659
3660#ifdef CONFIG_MAC80211_IBSS_DEBUG
Vladimir Koutny6e438292008-07-07 14:23:01 +02003661 if (found)
3662 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3663 "%s\n", print_mac(mac, bssid),
3664 print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07003665#endif /* CONFIG_MAC80211_IBSS_DEBUG */
Daniel Drake80693ce2008-07-19 23:31:17 +01003666
3667 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003668 int ret;
Daniel Drake80693ce2008-07-19 23:31:17 +01003669 int search_freq;
3670
3671 if (ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
3672 search_freq = bss->freq;
3673 else
3674 search_freq = local->hw.conf.channel->center_freq;
3675
3676 bss = ieee80211_rx_bss_get(dev, bssid, search_freq,
3677 ifsta->ssid, ifsta->ssid_len);
3678 if (!bss)
3679 goto dont_join;
3680
Joe Perches0795af52007-10-03 17:59:30 -07003681 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07003682 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003683 dev->name, print_mac(mac, bssid));
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003684 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
Johannes Berg3e122be2008-07-09 14:40:34 +02003685 ieee80211_rx_bss_put(local, bss);
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003686 return ret;
Jiri Bencf0706e822007-05-05 11:45:53 -07003687 }
Daniel Drake80693ce2008-07-19 23:31:17 +01003688
3689dont_join:
Jiri Bencf0706e822007-05-05 11:45:53 -07003690#ifdef CONFIG_MAC80211_IBSS_DEBUG
3691 printk(KERN_DEBUG " did not try to join ibss\n");
3692#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3693
3694 /* Selected IBSS not found in current scan results - try to scan */
3695 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3696 !ieee80211_sta_active_ibss(dev)) {
3697 mod_timer(&ifsta->timer, jiffies +
3698 IEEE80211_IBSS_MERGE_INTERVAL);
3699 } else if (time_after(jiffies, local->last_scan_completed +
3700 IEEE80211_SCAN_INTERVAL)) {
3701 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3702 "join\n", dev->name);
3703 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3704 ifsta->ssid_len);
3705 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3706 int interval = IEEE80211_SCAN_INTERVAL;
3707
3708 if (time_after(jiffies, ifsta->ibss_join_req +
3709 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003710 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003711 (!(local->oper_channel->flags &
3712 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e822007-05-05 11:45:53 -07003713 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003714 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003715 printk(KERN_DEBUG "%s: IBSS not allowed on"
3716 " %d MHz\n", dev->name,
3717 local->hw.conf.channel->center_freq);
Jiri Bencf0706e822007-05-05 11:45:53 -07003718 }
3719
3720 /* No IBSS found - decrease scan interval and continue
3721 * scanning. */
3722 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3723 }
3724
3725 ifsta->state = IEEE80211_IBSS_SEARCH;
3726 mod_timer(&ifsta->timer, jiffies + interval);
3727 return 0;
3728 }
3729
3730 return 0;
3731}
3732
3733
3734int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3735{
Johannes Berg8318d782008-01-24 19:38:38 +01003736 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003737 struct ieee80211_if_sta *ifsta;
Johannes Berg9d139c82008-07-09 14:40:37 +02003738 int res;
Jiri Bencf0706e822007-05-05 11:45:53 -07003739
3740 if (len > IEEE80211_MAX_SSID_LEN)
3741 return -EINVAL;
3742
Jiri Bencf0706e822007-05-05 11:45:53 -07003743 ifsta = &sdata->u.sta;
3744
Johannes Berg9d139c82008-07-09 14:40:37 +02003745 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0) {
3746 memset(ifsta->ssid, 0, sizeof(ifsta->ssid));
3747 memcpy(ifsta->ssid, ssid, len);
3748 ifsta->ssid_len = len;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003749 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Johannes Berg9d139c82008-07-09 14:40:37 +02003750
3751 res = 0;
3752 /*
3753 * Hack! MLME code needs to be cleaned up to have different
3754 * entry points for configuration and internal selection change
3755 */
3756 if (netif_running(sdata->dev))
3757 res = ieee80211_if_config(sdata, IEEE80211_IFCC_SSID);
3758 if (res) {
3759 printk(KERN_DEBUG "%s: Failed to config new SSID to "
3760 "the low-level driver\n", dev->name);
3761 return res;
3762 }
3763 }
Jiri Bencf0706e822007-05-05 11:45:53 -07003764
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003765 if (len)
3766 ifsta->flags |= IEEE80211_STA_SSID_SET;
3767 else
3768 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg9d139c82008-07-09 14:40:37 +02003769
Johannes Berg51fb61e2007-12-19 01:31:27 +01003770 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003771 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003772 ifsta->ibss_join_req = jiffies;
3773 ifsta->state = IEEE80211_IBSS_SEARCH;
3774 return ieee80211_sta_find_ibss(dev, ifsta);
3775 }
Johannes Berg9d139c82008-07-09 14:40:37 +02003776
Jiri Bencf0706e822007-05-05 11:45:53 -07003777 return 0;
3778}
3779
3780
3781int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3782{
3783 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3784 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3785 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3786 *len = ifsta->ssid_len;
3787 return 0;
3788}
3789
3790
3791int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3792{
3793 struct ieee80211_sub_if_data *sdata;
3794 struct ieee80211_if_sta *ifsta;
3795 int res;
3796
3797 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3798 ifsta = &sdata->u.sta;
3799
3800 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3801 memcpy(ifsta->bssid, bssid, ETH_ALEN);
Johannes Berg9d139c82008-07-09 14:40:37 +02003802 res = 0;
3803 /*
3804 * Hack! See also ieee80211_sta_set_ssid.
3805 */
3806 if (netif_running(sdata->dev))
3807 res = ieee80211_if_config(sdata, IEEE80211_IFCC_BSSID);
Jiri Bencf0706e822007-05-05 11:45:53 -07003808 if (res) {
3809 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3810 "the low-level driver\n", dev->name);
3811 return res;
3812 }
3813 }
3814
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003815 if (is_valid_ether_addr(bssid))
3816 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07003817 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003818 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3819
Jiri Bencf0706e822007-05-05 11:45:53 -07003820 return 0;
3821}
3822
3823
3824static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3825 struct ieee80211_sub_if_data *sdata,
3826 int powersave)
3827{
3828 struct sk_buff *skb;
3829 struct ieee80211_hdr *nullfunc;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003830 __le16 fc;
Jiri Bencf0706e822007-05-05 11:45:53 -07003831
3832 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3833 if (!skb) {
3834 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3835 "frame\n", sdata->dev->name);
3836 return;
3837 }
3838 skb_reserve(skb, local->hw.extra_tx_headroom);
3839
3840 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3841 memset(nullfunc, 0, 24);
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003842 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3843 IEEE80211_FCTL_TODS);
Jiri Bencf0706e822007-05-05 11:45:53 -07003844 if (powersave)
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003845 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3846 nullfunc->frame_control = fc;
Jiri Bencf0706e822007-05-05 11:45:53 -07003847 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3848 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3849 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3850
3851 ieee80211_sta_tx(sdata->dev, skb, 0);
3852}
3853
3854
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003855static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3856{
3857 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3858 ieee80211_vif_is_mesh(&sdata->vif))
3859 ieee80211_sta_timer((unsigned long)sdata);
3860}
3861
Jiri Bencf0706e822007-05-05 11:45:53 -07003862void ieee80211_scan_completed(struct ieee80211_hw *hw)
3863{
3864 struct ieee80211_local *local = hw_to_local(hw);
3865 struct net_device *dev = local->scan_dev;
3866 struct ieee80211_sub_if_data *sdata;
3867 union iwreq_data wrqu;
3868
3869 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003870 memset(&wrqu, 0, sizeof(wrqu));
3871 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e822007-05-05 11:45:53 -07003872
Zhu Yiece8edd2007-11-22 10:53:21 +08003873 if (local->sta_hw_scanning) {
3874 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003875 if (ieee80211_hw_config(local))
3876 printk(KERN_DEBUG "%s: failed to restore operational "
3877 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003878 /* Restart STA timer for HW scan case */
3879 rcu_read_lock();
3880 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3881 ieee80211_restart_sta_timer(sdata);
3882 rcu_read_unlock();
3883
Zhu Yiece8edd2007-11-22 10:53:21 +08003884 goto done;
3885 }
3886
3887 local->sta_sw_scanning = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07003888 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003889 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e822007-05-05 11:45:53 -07003890 "channel after scan\n", dev->name);
3891
Johannes Berg4150c572007-09-17 01:29:23 -04003892
3893 netif_tx_lock_bh(local->mdev);
David S. Millere308a5d2008-07-15 00:13:44 -07003894 netif_addr_lock(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -04003895 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3896 local->ops->configure_filter(local_to_hw(local),
3897 FIF_BCN_PRBRESP_PROMISC,
3898 &local->filter_flags,
3899 local->mdev->mc_count,
3900 local->mdev->mc_list);
3901
David S. Millere308a5d2008-07-15 00:13:44 -07003902 netif_addr_unlock(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -04003903 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e822007-05-05 11:45:53 -07003904
Johannes Berg79010422007-09-18 17:29:21 -04003905 rcu_read_lock();
3906 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003907 /* Tell AP we're back */
3908 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3909 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3910 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cb2007-06-08 15:31:13 +02003911
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003912 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003913
Jiri Bencf0706e822007-05-05 11:45:53 -07003914 netif_wake_queue(sdata->dev);
3915 }
Johannes Berg79010422007-09-18 17:29:21 -04003916 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07003917
Zhu Yiece8edd2007-11-22 10:53:21 +08003918done:
Jiri Bencf0706e822007-05-05 11:45:53 -07003919 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003920 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003921 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003922 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Tomas Winkler4c43e0d2008-08-04 16:00:39 +08003923 (!(ifsta->state == IEEE80211_IBSS_JOINED) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07003924 !ieee80211_sta_active_ibss(dev)))
3925 ieee80211_sta_find_ibss(dev, ifsta);
3926 }
3927}
3928EXPORT_SYMBOL(ieee80211_scan_completed);
3929
3930void ieee80211_sta_scan_work(struct work_struct *work)
3931{
3932 struct ieee80211_local *local =
3933 container_of(work, struct ieee80211_local, scan_work.work);
3934 struct net_device *dev = local->scan_dev;
3935 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003936 struct ieee80211_supported_band *sband;
Jiri Bencf0706e822007-05-05 11:45:53 -07003937 struct ieee80211_channel *chan;
3938 int skip;
3939 unsigned long next_delay = 0;
3940
Zhu Yiece8edd2007-11-22 10:53:21 +08003941 if (!local->sta_sw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07003942 return;
3943
3944 switch (local->scan_state) {
3945 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003946 /*
3947 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3948 * after we successfully scanned the last channel of the last
3949 * band (and the last band is supported by the hw)
3950 */
Johannes Berg8318d782008-01-24 19:38:38 +01003951 if (local->scan_band < IEEE80211_NUM_BANDS)
3952 sband = local->hw.wiphy->bands[local->scan_band];
3953 else
3954 sband = NULL;
3955
Johannes Berg69d464d2008-01-28 17:08:56 +01003956 /*
3957 * If we are at an unsupported band and have more bands
3958 * left to scan, advance to the next supported one.
3959 */
3960 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003961 local->scan_band++;
3962 sband = local->hw.wiphy->bands[local->scan_band];
3963 local->scan_channel_idx = 0;
3964 }
3965
Johannes Berg69d464d2008-01-28 17:08:56 +01003966 /* if no more bands/channels left, complete scan */
3967 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003968 ieee80211_scan_completed(local_to_hw(local));
3969 return;
3970 }
Johannes Berg8318d782008-01-24 19:38:38 +01003971 skip = 0;
3972 chan = &sband->channels[local->scan_channel_idx];
3973
3974 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003975 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003976 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e822007-05-05 11:45:53 -07003977 skip = 1;
3978
3979 if (!skip) {
Jiri Bencf0706e822007-05-05 11:45:53 -07003980 local->scan_channel = chan;
3981 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003982 printk(KERN_DEBUG "%s: failed to set freq to "
3983 "%d MHz for scan\n", dev->name,
3984 chan->center_freq);
Jiri Bencf0706e822007-05-05 11:45:53 -07003985 skip = 1;
3986 }
3987 }
3988
Johannes Berg69d464d2008-01-28 17:08:56 +01003989 /* advance state machine to next channel/band */
Jiri Bencf0706e822007-05-05 11:45:53 -07003990 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003991 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003992 /*
3993 * scan_band may end up == IEEE80211_NUM_BANDS, but
3994 * we'll catch that case above and complete the scan
3995 * if that is the case.
3996 */
Johannes Berg8318d782008-01-24 19:38:38 +01003997 local->scan_band++;
3998 local->scan_channel_idx = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07003999 }
4000
4001 if (skip)
4002 break;
4003
4004 next_delay = IEEE80211_PROBE_DELAY +
4005 usecs_to_jiffies(local->hw.channel_change_time);
4006 local->scan_state = SCAN_SEND_PROBE;
4007 break;
4008 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01004009 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e822007-05-05 11:45:53 -07004010 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01004011
4012 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
4013 break;
4014 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
4015 local->scan_ssid_len);
4016 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e822007-05-05 11:45:53 -07004017 break;
4018 }
4019
Zhu Yiece8edd2007-11-22 10:53:21 +08004020 if (local->sta_sw_scanning)
Jiri Bencf0706e822007-05-05 11:45:53 -07004021 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4022 next_delay);
4023}
4024
4025
4026static int ieee80211_sta_start_scan(struct net_device *dev,
4027 u8 *ssid, size_t ssid_len)
4028{
4029 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4030 struct ieee80211_sub_if_data *sdata;
4031
4032 if (ssid_len > IEEE80211_MAX_SSID_LEN)
4033 return -EINVAL;
4034
4035 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
4036 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
4037 * BSSID: MACAddress
4038 * SSID
4039 * ScanType: ACTIVE, PASSIVE
4040 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
4041 * a Probe frame during active scanning
4042 * ChannelList
4043 * MinChannelTime (>= ProbeDelay), in TU
4044 * MaxChannelTime: (>= MinChannelTime), in TU
4045 */
4046
4047 /* MLME-SCAN.confirm
4048 * BSSDescriptionSet
4049 * ResultCode: SUCCESS, INVALID_PARAMETERS
4050 */
4051
Zhu Yiece8edd2007-11-22 10:53:21 +08004052 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e822007-05-05 11:45:53 -07004053 if (local->scan_dev == dev)
4054 return 0;
4055 return -EBUSY;
4056 }
4057
4058 if (local->ops->hw_scan) {
4059 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08004060 ssid, ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07004061 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08004062 local->sta_hw_scanning = 1;
Jiri Bencf0706e822007-05-05 11:45:53 -07004063 local->scan_dev = dev;
4064 }
4065 return rc;
4066 }
4067
Zhu Yiece8edd2007-11-22 10:53:21 +08004068 local->sta_sw_scanning = 1;
Jiri Bencf0706e822007-05-05 11:45:53 -07004069
Johannes Berg79010422007-09-18 17:29:21 -04004070 rcu_read_lock();
4071 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Jiri Bencf0706e822007-05-05 11:45:53 -07004072 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01004073 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004074 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e822007-05-05 11:45:53 -07004075 ieee80211_send_nullfunc(local, sdata, 1);
4076 }
Johannes Berg79010422007-09-18 17:29:21 -04004077 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07004078
4079 if (ssid) {
4080 local->scan_ssid_len = ssid_len;
4081 memcpy(local->scan_ssid, ssid, ssid_len);
4082 } else
4083 local->scan_ssid_len = 0;
4084 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e822007-05-05 11:45:53 -07004085 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004086 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e822007-05-05 11:45:53 -07004087 local->scan_dev = dev;
4088
David S. Millerb9e40852008-07-15 00:15:08 -07004089 netif_addr_lock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -04004090 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4091 local->ops->configure_filter(local_to_hw(local),
4092 FIF_BCN_PRBRESP_PROMISC,
4093 &local->filter_flags,
4094 local->mdev->mc_count,
4095 local->mdev->mc_list);
David S. Millerb9e40852008-07-15 00:15:08 -07004096 netif_addr_unlock_bh(local->mdev);
Jiri Bencf0706e822007-05-05 11:45:53 -07004097
4098 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4099 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4100 IEEE80211_CHANNEL_TIME);
4101
4102 return 0;
4103}
4104
4105
4106int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4107{
4108 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4109 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4110 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4111
Johannes Berg51fb61e2007-12-19 01:31:27 +01004112 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07004113 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4114
Zhu Yiece8edd2007-11-22 10:53:21 +08004115 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e822007-05-05 11:45:53 -07004116 if (local->scan_dev == dev)
4117 return 0;
4118 return -EBUSY;
4119 }
4120
Helmut Schaaa0af5f12007-11-09 16:25:08 +01004121 ifsta->scan_ssid_len = ssid_len;
4122 if (ssid_len)
4123 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07004124 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4125 queue_work(local->hw.workqueue, &ifsta->work);
4126 return 0;
4127}
4128
4129static char *
4130ieee80211_sta_scan_result(struct net_device *dev,
David S. Millerccc58052008-06-16 18:50:49 -07004131 struct iw_request_info *info,
Jiri Bencf0706e822007-05-05 11:45:53 -07004132 struct ieee80211_sta_bss *bss,
4133 char *current_ev, char *end_buf)
4134{
4135 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4136 struct iw_event iwe;
4137
4138 if (time_after(jiffies,
4139 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4140 return current_ev;
4141
Jiri Bencf0706e822007-05-05 11:45:53 -07004142 memset(&iwe, 0, sizeof(iwe));
4143 iwe.cmd = SIOCGIWAP;
4144 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4145 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07004146 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e822007-05-05 11:45:53 -07004147 IW_EV_ADDR_LEN);
4148
4149 memset(&iwe, 0, sizeof(iwe));
4150 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01004151 if (bss_mesh_cfg(bss)) {
4152 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004153 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004154 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4155 &iwe, bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004156 } else {
4157 iwe.u.data.length = bss->ssid_len;
4158 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004159 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4160 &iwe, bss->ssid);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004161 }
Jiri Bencf0706e822007-05-05 11:45:53 -07004162
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08004163 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4164 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07004165 memset(&iwe, 0, sizeof(iwe));
4166 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01004167 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004168 iwe.u.mode = IW_MODE_MESH;
4169 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e822007-05-05 11:45:53 -07004170 iwe.u.mode = IW_MODE_MASTER;
4171 else
4172 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07004173 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4174 &iwe, IW_EV_UINT_LEN);
Jiri Bencf0706e822007-05-05 11:45:53 -07004175 }
4176
4177 memset(&iwe, 0, sizeof(iwe));
4178 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01004179 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4180 iwe.u.freq.e = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004181 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e822007-05-05 11:45:53 -07004182 IW_EV_FREQ_LEN);
4183
4184 memset(&iwe, 0, sizeof(iwe));
Tomas Winkler9381be02008-05-23 01:36:36 +03004185 iwe.cmd = SIOCGIWFREQ;
4186 iwe.u.freq.m = bss->freq;
4187 iwe.u.freq.e = 6;
David S. Millerccc58052008-06-16 18:50:49 -07004188 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Tomas Winkler9381be02008-05-23 01:36:36 +03004189 IW_EV_FREQ_LEN);
4190 memset(&iwe, 0, sizeof(iwe));
Jiri Bencf0706e822007-05-05 11:45:53 -07004191 iwe.cmd = IWEVQUAL;
Bruno Randolf566bfe52008-05-08 19:15:40 +02004192 iwe.u.qual.qual = bss->qual;
4193 iwe.u.qual.level = bss->signal;
Jiri Bencf0706e822007-05-05 11:45:53 -07004194 iwe.u.qual.noise = bss->noise;
4195 iwe.u.qual.updated = local->wstats_flags;
David S. Millerccc58052008-06-16 18:50:49 -07004196 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e822007-05-05 11:45:53 -07004197 IW_EV_QUAL_LEN);
4198
4199 memset(&iwe, 0, sizeof(iwe));
4200 iwe.cmd = SIOCGIWENCODE;
4201 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4202 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4203 else
4204 iwe.u.data.flags = IW_ENCODE_DISABLED;
4205 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004206 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4207 &iwe, "");
Jiri Bencf0706e822007-05-05 11:45:53 -07004208
4209 if (bss && bss->wpa_ie) {
4210 memset(&iwe, 0, sizeof(iwe));
4211 iwe.cmd = IWEVGENIE;
4212 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004213 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4214 &iwe, bss->wpa_ie);
Jiri Bencf0706e822007-05-05 11:45:53 -07004215 }
4216
4217 if (bss && bss->rsn_ie) {
4218 memset(&iwe, 0, sizeof(iwe));
4219 iwe.cmd = IWEVGENIE;
4220 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004221 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4222 &iwe, bss->rsn_ie);
Jiri Bencf0706e822007-05-05 11:45:53 -07004223 }
4224
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004225 if (bss && bss->ht_ie) {
4226 memset(&iwe, 0, sizeof(iwe));
4227 iwe.cmd = IWEVGENIE;
4228 iwe.u.data.length = bss->ht_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004229 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4230 &iwe, bss->ht_ie);
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004231 }
4232
Jiri Bencf0706e822007-05-05 11:45:53 -07004233 if (bss && bss->supp_rates_len > 0) {
4234 /* display all supported rates in readable format */
David S. Millerccc58052008-06-16 18:50:49 -07004235 char *p = current_ev + iwe_stream_lcp_len(info);
Jiri Bencf0706e822007-05-05 11:45:53 -07004236 int i;
4237
4238 memset(&iwe, 0, sizeof(iwe));
4239 iwe.cmd = SIOCGIWRATE;
4240 /* Those two flags are ignored... */
4241 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4242
4243 for (i = 0; i < bss->supp_rates_len; i++) {
4244 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4245 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07004246 p = iwe_stream_add_value(info, current_ev, p,
Jiri Bencf0706e822007-05-05 11:45:53 -07004247 end_buf, &iwe, IW_EV_PARAM_LEN);
4248 }
4249 current_ev = p;
4250 }
4251
4252 if (bss) {
4253 char *buf;
4254 buf = kmalloc(30, GFP_ATOMIC);
4255 if (buf) {
4256 memset(&iwe, 0, sizeof(iwe));
4257 iwe.cmd = IWEVCUSTOM;
4258 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4259 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004260 current_ev = iwe_stream_add_point(info, current_ev,
4261 end_buf,
Jiri Bencf0706e822007-05-05 11:45:53 -07004262 &iwe, buf);
Emmanuel Grumbachbf998f62008-06-24 13:38:00 +03004263 memset(&iwe, 0, sizeof(iwe));
4264 iwe.cmd = IWEVCUSTOM;
4265 sprintf(buf, " Last beacon: %dms ago",
4266 jiffies_to_msecs(jiffies - bss->last_update));
4267 iwe.u.data.length = strlen(buf);
4268 current_ev = iwe_stream_add_point(info, current_ev,
4269 end_buf, &iwe, buf);
Jiri Bencf0706e822007-05-05 11:45:53 -07004270 kfree(buf);
4271 }
4272 }
4273
Johannes Berg902acc72008-02-23 15:17:19 +01004274 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004275 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04004276 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004277 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004278 if (buf) {
4279 memset(&iwe, 0, sizeof(iwe));
4280 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04004281 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004282 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004283 current_ev = iwe_stream_add_point(info, current_ev,
4284 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004285 &iwe, buf);
4286 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04004287 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4288 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004289 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004290 current_ev = iwe_stream_add_point(info, current_ev,
4291 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004292 &iwe, buf);
4293 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04004294 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4295 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004296 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004297 current_ev = iwe_stream_add_point(info, current_ev,
4298 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004299 &iwe, buf);
4300 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04004301 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4302 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004303 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004304 current_ev = iwe_stream_add_point(info, current_ev,
4305 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004306 &iwe, buf);
4307 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04004308 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4309 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004310 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004311 current_ev = iwe_stream_add_point(info, current_ev,
4312 end_buf,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004313 &iwe, buf);
4314 kfree(buf);
4315 }
4316 }
4317
Jiri Bencf0706e822007-05-05 11:45:53 -07004318 return current_ev;
4319}
4320
4321
David S. Millerccc58052008-06-16 18:50:49 -07004322int ieee80211_sta_scan_results(struct net_device *dev,
4323 struct iw_request_info *info,
4324 char *buf, size_t len)
Jiri Bencf0706e822007-05-05 11:45:53 -07004325{
4326 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4327 char *current_ev = buf;
4328 char *end_buf = buf + len;
4329 struct ieee80211_sta_bss *bss;
4330
4331 spin_lock_bh(&local->sta_bss_lock);
4332 list_for_each_entry(bss, &local->sta_bss_list, list) {
4333 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4334 spin_unlock_bh(&local->sta_bss_lock);
4335 return -E2BIG;
4336 }
David S. Millerccc58052008-06-16 18:50:49 -07004337 current_ev = ieee80211_sta_scan_result(dev, info, bss,
4338 current_ev, end_buf);
Jiri Bencf0706e822007-05-05 11:45:53 -07004339 }
4340 spin_unlock_bh(&local->sta_bss_lock);
4341 return current_ev - buf;
4342}
4343
4344
4345int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4346{
4347 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4348 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg988c0f72008-04-17 19:21:22 +02004349
Jiri Bencf0706e822007-05-05 11:45:53 -07004350 kfree(ifsta->extra_ie);
4351 if (len == 0) {
4352 ifsta->extra_ie = NULL;
4353 ifsta->extra_ie_len = 0;
4354 return 0;
4355 }
4356 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4357 if (!ifsta->extra_ie) {
4358 ifsta->extra_ie_len = 0;
4359 return -ENOMEM;
4360 }
4361 memcpy(ifsta->extra_ie, ie, len);
4362 ifsta->extra_ie_len = len;
4363 return 0;
4364}
4365
4366
Johannes Berg988c0f72008-04-17 19:21:22 +02004367struct sta_info *ieee80211_ibss_add_sta(struct net_device *dev,
4368 struct sk_buff *skb, u8 *bssid,
Vladimir Koutny87291c02008-06-13 16:50:44 +02004369 u8 *addr, u64 supp_rates)
Jiri Bencf0706e822007-05-05 11:45:53 -07004370{
4371 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4372 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004373 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004374 DECLARE_MAC_BUF(mac);
Vladimir Koutny87291c02008-06-13 16:50:44 +02004375 int band = local->hw.conf.channel->band;
Jiri Bencf0706e822007-05-05 11:45:53 -07004376
4377 /* TODO: Could consider removing the least recently used entry and
4378 * allow new one to be added. */
4379 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4380 if (net_ratelimit()) {
4381 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004382 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e822007-05-05 11:45:53 -07004383 }
4384 return NULL;
4385 }
4386
Emmanuel Grumbach1e188632008-07-10 17:54:14 +03004387 if (compare_ether_addr(bssid, sdata->u.sta.bssid))
Vladimir Koutny87291c02008-06-13 16:50:44 +02004388 return NULL;
4389
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004390#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07004391 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004392 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004393#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07004394
Johannes Berg73651ee2008-02-25 16:27:47 +01004395 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4396 if (!sta)
Jiri Bencf0706e822007-05-05 11:45:53 -07004397 return NULL;
4398
Johannes Berg07346f812008-05-03 01:02:02 +02004399 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
Johannes Berg238814f2008-01-28 17:19:37 +01004400
Vladimir Koutny87291c02008-06-13 16:50:44 +02004401 if (supp_rates)
4402 sta->supp_rates[band] = supp_rates;
4403 else
4404 sta->supp_rates[band] = sdata->u.sta.supp_rates_bits[band];
Jiri Bencf0706e822007-05-05 11:45:53 -07004405
4406 rate_control_rate_init(sta, local);
4407
Johannes Berg93e5deb2008-04-01 15:21:00 +02004408 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004409 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004410
Johannes Bergd0709a62008-02-25 16:27:46 +01004411 return sta;
Jiri Bencf0706e822007-05-05 11:45:53 -07004412}
4413
4414
4415int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4416{
4417 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4418 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4419
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004420 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
Jiri Bencf0706e822007-05-05 11:45:53 -07004421 dev->name, reason);
4422
Johannes Berg51fb61e2007-12-19 01:31:27 +01004423 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4424 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e822007-05-05 11:45:53 -07004425 return -EINVAL;
4426
4427 ieee80211_send_deauth(dev, ifsta, reason);
4428 ieee80211_set_disassoc(dev, ifsta, 1);
4429 return 0;
4430}
4431
4432
4433int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4434{
4435 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4436 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4437
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004438 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
Jiri Bencf0706e822007-05-05 11:45:53 -07004439 dev->name, reason);
4440
Johannes Berg51fb61e2007-12-19 01:31:27 +01004441 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e822007-05-05 11:45:53 -07004442 return -EINVAL;
4443
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004444 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e822007-05-05 11:45:53 -07004445 return -1;
4446
4447 ieee80211_send_disassoc(dev, ifsta, reason);
4448 ieee80211_set_disassoc(dev, ifsta, 0);
4449 return 0;
4450}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004451
4452void ieee80211_notify_mac(struct ieee80211_hw *hw,
4453 enum ieee80211_notification_types notif_type)
4454{
4455 struct ieee80211_local *local = hw_to_local(hw);
4456 struct ieee80211_sub_if_data *sdata;
4457
4458 switch (notif_type) {
4459 case IEEE80211_NOTIFY_RE_ASSOC:
4460 rcu_read_lock();
4461 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg3e122be2008-07-09 14:40:34 +02004462 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4463 continue;
Mohamed Abbas84363e62008-04-04 16:59:58 -07004464
Johannes Berg3e122be2008-07-09 14:40:34 +02004465 ieee80211_sta_req_auth(sdata->dev, &sdata->u.sta);
Mohamed Abbas84363e62008-04-04 16:59:58 -07004466 }
4467 rcu_read_unlock();
4468 break;
4469 }
4470}
4471EXPORT_SYMBOL(ieee80211_notify_mac);