blob: 2a927089f4db62ce98dd58c687f6e797c4bb7f43 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
Jiri Bencf0706e82007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010027#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040033#include "rate.h"
34#include "led.h"
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010035#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
37#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40#define IEEE80211_ASSOC_MAX_TRIES 3
41#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010042#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070043#define IEEE80211_PROBE_INTERVAL (60 * HZ)
44#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45#define IEEE80211_SCAN_INTERVAL (2 * HZ)
46#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
Dan Williams872ba532008-06-04 13:59:34 -040047#define IEEE80211_IBSS_JOIN_TIMEOUT (7 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070048
49#define IEEE80211_PROBE_DELAY (HZ / 33)
50#define IEEE80211_CHANNEL_TIME (HZ / 33)
51#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
52#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
53#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
54#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010055#define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070056
57#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
58
59
Jiri Bencf0706e82007-05-05 11:45:53 -070060#define ERP_INFO_USE_PROTECTION BIT(1)
61
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020062/* mgmt header + 1 byte action code */
63#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
64
65#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
66#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
67#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020068#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
69#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020070
Ron Rindjunsky07db2182007-12-25 17:00:33 +020071/* next values represent the buffer size for A-MPDU frame.
72 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
73#define IEEE80211_MIN_AMPDU_BUF 0x8
74#define IEEE80211_MAX_AMPDU_BUF 0x40
75
Jiri Bencf0706e82007-05-05 11:45:53 -070076static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
77 u8 *ssid, size_t ssid_len);
78static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +010079ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -040080 u8 *ssid, u8 ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070081static void ieee80211_rx_bss_put(struct net_device *dev,
82 struct ieee80211_sta_bss *bss);
83static int ieee80211_sta_find_ibss(struct net_device *dev,
84 struct ieee80211_if_sta *ifsta);
85static int ieee80211_sta_wep_configured(struct net_device *dev);
86static int ieee80211_sta_start_scan(struct net_device *dev,
87 u8 *ssid, size_t ssid_len);
88static int ieee80211_sta_config_auth(struct net_device *dev,
89 struct ieee80211_if_sta *ifsta);
Adrian Bunk712590d2008-04-21 11:47:51 +030090static void sta_rx_agg_session_timer_expired(unsigned long data);
Jiri Bencf0706e82007-05-05 11:45:53 -070091
92
Luis Carlos Coboee385852008-02-23 15:17:11 +010093void ieee802_11_parse_elems(u8 *start, size_t len,
94 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -070095{
96 size_t left = len;
97 u8 *pos = start;
Jiri Bencf0706e82007-05-05 11:45:53 -070098
99 memset(elems, 0, sizeof(*elems));
100
101 while (left >= 2) {
102 u8 id, elen;
103
104 id = *pos++;
105 elen = *pos++;
106 left -= 2;
107
John W. Linville67a4cce2007-10-12 16:40:37 -0400108 if (elen > left)
109 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700110
111 switch (id) {
112 case WLAN_EID_SSID:
113 elems->ssid = pos;
114 elems->ssid_len = elen;
115 break;
116 case WLAN_EID_SUPP_RATES:
117 elems->supp_rates = pos;
118 elems->supp_rates_len = elen;
119 break;
120 case WLAN_EID_FH_PARAMS:
121 elems->fh_params = pos;
122 elems->fh_params_len = elen;
123 break;
124 case WLAN_EID_DS_PARAMS:
125 elems->ds_params = pos;
126 elems->ds_params_len = elen;
127 break;
128 case WLAN_EID_CF_PARAMS:
129 elems->cf_params = pos;
130 elems->cf_params_len = elen;
131 break;
132 case WLAN_EID_TIM:
133 elems->tim = pos;
134 elems->tim_len = elen;
135 break;
136 case WLAN_EID_IBSS_PARAMS:
137 elems->ibss_params = pos;
138 elems->ibss_params_len = elen;
139 break;
140 case WLAN_EID_CHALLENGE:
141 elems->challenge = pos;
142 elems->challenge_len = elen;
143 break;
144 case WLAN_EID_WPA:
145 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
146 pos[2] == 0xf2) {
147 /* Microsoft OUI (00:50:F2) */
148 if (pos[3] == 1) {
149 /* OUI Type 1 - WPA IE */
150 elems->wpa = pos;
151 elems->wpa_len = elen;
152 } else if (elen >= 5 && pos[3] == 2) {
153 if (pos[4] == 0) {
154 elems->wmm_info = pos;
155 elems->wmm_info_len = elen;
156 } else if (pos[4] == 1) {
157 elems->wmm_param = pos;
158 elems->wmm_param_len = elen;
159 }
160 }
161 }
162 break;
163 case WLAN_EID_RSN:
164 elems->rsn = pos;
165 elems->rsn_len = elen;
166 break;
167 case WLAN_EID_ERP_INFO:
168 elems->erp_info = pos;
169 elems->erp_info_len = elen;
170 break;
171 case WLAN_EID_EXT_SUPP_RATES:
172 elems->ext_supp_rates = pos;
173 elems->ext_supp_rates_len = elen;
174 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200175 case WLAN_EID_HT_CAPABILITY:
176 elems->ht_cap_elem = pos;
177 elems->ht_cap_elem_len = elen;
178 break;
179 case WLAN_EID_HT_EXTRA_INFO:
180 elems->ht_info_elem = pos;
181 elems->ht_info_elem_len = elen;
182 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100183 case WLAN_EID_MESH_ID:
184 elems->mesh_id = pos;
185 elems->mesh_id_len = elen;
186 break;
187 case WLAN_EID_MESH_CONFIG:
188 elems->mesh_config = pos;
189 elems->mesh_config_len = elen;
190 break;
191 case WLAN_EID_PEER_LINK:
192 elems->peer_link = pos;
193 elems->peer_link_len = elen;
194 break;
195 case WLAN_EID_PREQ:
196 elems->preq = pos;
197 elems->preq_len = elen;
198 break;
199 case WLAN_EID_PREP:
200 elems->prep = pos;
201 elems->prep_len = elen;
202 break;
203 case WLAN_EID_PERR:
204 elems->perr = pos;
205 elems->perr_len = elen;
206 break;
Assaf Kraussf2df3852008-06-15 18:23:29 +0300207 case WLAN_EID_CHANNEL_SWITCH:
208 elems->ch_switch_elem = pos;
209 elems->ch_switch_elem_len = elen;
210 break;
211 case WLAN_EID_QUIET:
212 if (!elems->quiet_elem) {
213 elems->quiet_elem = pos;
214 elems->quiet_elem_len = elen;
215 }
216 elems->num_of_quiet_elem++;
217 break;
218 case WLAN_EID_COUNTRY:
219 elems->country_elem = pos;
220 elems->country_elem_len = elen;
221 break;
222 case WLAN_EID_PWR_CONSTRAINT:
223 elems->pwr_constr_elem = pos;
224 elems->pwr_constr_elem_len = elen;
225 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700226 default:
Jiri Bencf0706e82007-05-05 11:45:53 -0700227 break;
228 }
229
230 left -= elen;
231 pos += elen;
232 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700233}
234
235
Jiri Bencf0706e82007-05-05 11:45:53 -0700236static int ecw2cw(int ecw)
237{
Johannes Bergac2bf322008-02-14 00:30:35 +0100238 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700239}
240
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100241
242static void ieee80211_sta_def_wmm_params(struct net_device *dev,
243 struct ieee80211_sta_bss *bss,
244 int ibss)
245{
246 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
247 struct ieee80211_local *local = sdata->local;
248 int i, have_higher_than_11mbit = 0;
249
250
251 /* cf. IEEE 802.11 9.2.12 */
252 for (i = 0; i < bss->supp_rates_len; i++)
253 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
254 have_higher_than_11mbit = 1;
255
256 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
257 have_higher_than_11mbit)
258 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
259 else
260 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
261
262
263 if (local->ops->conf_tx) {
264 struct ieee80211_tx_queue_params qparam;
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100265
266 memset(&qparam, 0, sizeof(qparam));
267
268 qparam.aifs = 2;
269
270 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
271 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
272 qparam.cw_min = 31;
273 else
274 qparam.cw_min = 15;
275
276 qparam.cw_max = 1023;
277 qparam.txop = 0;
278
Johannes Berge100bb62008-04-30 18:51:21 +0200279 for (i = 0; i < local_to_hw(local)->queues; i++)
280 local->ops->conf_tx(local_to_hw(local), i, &qparam);
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100281 }
282}
283
Jiri Bencf0706e82007-05-05 11:45:53 -0700284static void ieee80211_sta_wmm_params(struct net_device *dev,
285 struct ieee80211_if_sta *ifsta,
286 u8 *wmm_param, size_t wmm_param_len)
287{
288 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
289 struct ieee80211_tx_queue_params params;
290 size_t left;
291 int count;
292 u8 *pos;
293
Johannes Berg3434fbd2008-05-03 00:59:37 +0200294 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
295 return;
296
297 if (!wmm_param)
298 return;
299
Jiri Bencf0706e82007-05-05 11:45:53 -0700300 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
301 return;
302 count = wmm_param[6] & 0x0f;
303 if (count == ifsta->wmm_last_param_set)
304 return;
305 ifsta->wmm_last_param_set = count;
306
307 pos = wmm_param + 8;
308 left = wmm_param_len - 8;
309
310 memset(&params, 0, sizeof(params));
311
312 if (!local->ops->conf_tx)
313 return;
314
315 local->wmm_acm = 0;
316 for (; left >= 4; left -= 4, pos += 4) {
317 int aci = (pos[0] >> 5) & 0x03;
318 int acm = (pos[0] >> 4) & 0x01;
319 int queue;
320
321 switch (aci) {
322 case 1:
Johannes Berge100bb62008-04-30 18:51:21 +0200323 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200324 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700325 local->wmm_acm |= BIT(0) | BIT(3);
Jiri Bencf0706e82007-05-05 11:45:53 -0700326 break;
327 case 2:
Johannes Berge100bb62008-04-30 18:51:21 +0200328 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200329 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700330 local->wmm_acm |= BIT(4) | BIT(5);
Jiri Bencf0706e82007-05-05 11:45:53 -0700331 break;
332 case 3:
Johannes Berge100bb62008-04-30 18:51:21 +0200333 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200334 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700335 local->wmm_acm |= BIT(6) | BIT(7);
Jiri Bencf0706e82007-05-05 11:45:53 -0700336 break;
337 case 0:
338 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200339 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200340 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700341 local->wmm_acm |= BIT(1) | BIT(2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700342 break;
343 }
344
345 params.aifs = pos[0] & 0x0f;
346 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
347 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100348 params.txop = pos[2] | (pos[3] << 8);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200349#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700350 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100351 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e82007-05-05 11:45:53 -0700352 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100353 params.cw_max, params.txop);
354#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700355 /* TODO: handle ACM (block TX, fallback to next lowest allowed
356 * AC for now) */
357 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
358 printk(KERN_DEBUG "%s: failed to set TX queue "
359 "parameters for queue %d\n", dev->name, queue);
360 }
361 }
362}
363
John W. Linville50c4afb2008-04-15 14:09:27 -0400364static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
365 bool use_protection,
366 bool use_short_preamble)
Daniel Drake56282212007-07-10 19:32:10 +0200367{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100368 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-05-05 11:45:53 -0700536 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100537 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700538
Jiri Bencf0706e82007-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 Bencf0706e82007-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;
554
John W. Linville50c4afb2008-04-15 14:09:27 -0400555 changed |= ieee80211_handle_bss_capability(sdata, bss);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700556
Daniel Drake56282212007-07-10 19:32:10 +0200557 ieee80211_rx_bss_put(dev, bss);
558 }
559
Tomas Winkler38668c02008-03-28 16:33:32 -0700560 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
561 changed |= BSS_CHANGED_HT;
562 sdata->bss_conf.assoc_ht = 1;
563 sdata->bss_conf.ht_conf = &conf->ht_conf;
564 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
565 }
566
Jiri Bencf0706e82007-05-05 11:45:53 -0700567 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400568 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-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 {
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700573 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400574 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700575 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200576 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 Bencf0706e82007-05-05 11:45:53 -0700582 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
583 }
Jiri Bencf0706e82007-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);
Reinette Chatre41a7be42008-04-03 16:08:49 -0700589 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
590 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700591}
592
593static void ieee80211_set_disassoc(struct net_device *dev,
594 struct ieee80211_if_sta *ifsta, int deauth)
595{
596 if (deauth)
597 ifsta->auth_tries = 0;
598 ifsta->assoc_tries = 0;
599 ieee80211_set_associated(dev, ifsta, 0);
600}
601
Luis Carlos Coboee385852008-02-23 15:17:11 +0100602void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
603 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700604{
605 struct ieee80211_sub_if_data *sdata;
Johannes Berge039fa42008-05-15 12:55:29 +0200606 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700607
608 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
609 skb->dev = sdata->local->mdev;
610 skb_set_mac_header(skb, 0);
611 skb_set_network_header(skb, 0);
612 skb_set_transport_header(skb, 0);
613
Johannes Berge039fa42008-05-15 12:55:29 +0200614 info = IEEE80211_SKB_CB(skb);
615 memset(info, 0, sizeof(struct ieee80211_tx_info));
616 info->control.ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400617 if (!encrypt)
Johannes Berge039fa42008-05-15 12:55:29 +0200618 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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
Jiri Bencf0706e82007-05-05 11:45:53 -0700760 ieee80211_rx_bss_put(dev, bss);
Helmut Schaa0d580a72008-05-20 09:56:37 +0200761 } else {
762 rates = ~0;
763 rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-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 Bencf0706e82007-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);
777 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
778 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
779 ETH_ALEN);
780 } else {
781 skb_put(skb, 4);
782 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
783 IEEE80211_STYPE_ASSOC_REQ);
784 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
785 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
786 }
787
788 /* SSID */
789 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
790 *pos++ = WLAN_EID_SSID;
791 *pos++ = ifsta->ssid_len;
792 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
793
Helmut Schaa0d580a72008-05-20 09:56:37 +0200794 /* add all rates which were marked to be used above */
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200795 supp_rates_len = rates_len;
796 if (supp_rates_len > 8)
797 supp_rates_len = 8;
Jiri Bencf0706e82007-05-05 11:45:53 -0700798
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200799 len = sband->n_bitrates;
800 pos = skb_put(skb, supp_rates_len + 2);
801 *pos++ = WLAN_EID_SUPP_RATES;
802 *pos++ = supp_rates_len;
803
804 count = 0;
805 for (i = 0; i < sband->n_bitrates; i++) {
806 if (BIT(i) & rates) {
Johannes Berg8318d782008-01-24 19:38:38 +0100807 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700808 *pos++ = (u8) (rate / 5);
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200809 if (++count == 8)
810 break;
811 }
812 }
813
814 if (count == 8) {
815 pos = skb_put(skb, rates_len - count + 2);
816 *pos++ = WLAN_EID_EXT_SUPP_RATES;
817 *pos++ = rates_len - count;
818
819 for (i++; i < sband->n_bitrates; i++) {
820 if (BIT(i) & rates) {
821 int rate = sband->bitrates[i].bitrate;
822 *pos++ = (u8) (rate / 5);
823 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700824 }
825 }
826
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300827 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
828 /* 1. power capabilities */
829 pos = skb_put(skb, 4);
830 *pos++ = WLAN_EID_PWR_CAPABILITY;
831 *pos++ = 2;
832 *pos++ = 0; /* min tx power */
833 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
834
835 /* 2. supported channels */
836 /* TODO: get this in reg domain format */
837 pos = skb_put(skb, 2 * sband->n_channels + 2);
838 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
839 *pos++ = 2 * sband->n_channels;
840 for (i = 0; i < sband->n_channels; i++) {
841 *pos++ = ieee80211_frequency_to_channel(
842 sband->channels[i].center_freq);
843 *pos++ = 1; /* one channel in the subband*/
844 }
845 }
846
Jiri Bencf0706e82007-05-05 11:45:53 -0700847 if (ifsta->extra_ie) {
848 pos = skb_put(skb, ifsta->extra_ie_len);
849 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
850 }
851
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400852 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700853 pos = skb_put(skb, 9);
854 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
855 *pos++ = 7; /* len */
856 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
857 *pos++ = 0x50;
858 *pos++ = 0xf2;
859 *pos++ = 2; /* WME */
860 *pos++ = 0; /* WME info */
861 *pos++ = 1; /* WME ver */
862 *pos++ = 0;
863 }
Johannes Berg3434fbd2008-05-03 00:59:37 +0200864
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200865 /* wmm support is a must to HT */
Johannes Berg3434fbd2008-05-03 00:59:37 +0200866 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
Emmanuel Grumbach93061022008-05-29 16:35:23 +0800867 sband->ht_info.ht_supported && bss->ht_add_ie) {
868 struct ieee80211_ht_addt_info *ht_add_info =
869 (struct ieee80211_ht_addt_info *)bss->ht_add_ie;
870 u16 cap = sband->ht_info.cap;
871 __le16 tmp;
872 u32 flags = local->hw.conf.channel->flags;
873
874 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
875 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
876 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
877 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
878 cap &= ~IEEE80211_HT_CAP_SGI_40;
879 }
880 break;
881 case IEEE80211_HT_IE_CHA_SEC_BELOW:
882 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
883 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
884 cap &= ~IEEE80211_HT_CAP_SGI_40;
885 }
886 break;
887 }
888
889 tmp = cpu_to_le16(cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200890 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
891 *pos++ = WLAN_EID_HT_CAPABILITY;
892 *pos++ = sizeof(struct ieee80211_ht_cap);
893 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
894 memcpy(pos, &tmp, sizeof(u16));
895 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100896 /* TODO: needs a define here for << 2 */
897 *pos++ = sband->ht_info.ampdu_factor |
898 (sband->ht_info.ampdu_density << 2);
899 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200900 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700901
902 kfree(ifsta->assocreq_ies);
903 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200904 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700905 if (ifsta->assocreq_ies)
906 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
907
908 ieee80211_sta_tx(dev, skb, 0);
909}
910
911
912static void ieee80211_send_deauth(struct net_device *dev,
913 struct ieee80211_if_sta *ifsta, u16 reason)
914{
915 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
916 struct sk_buff *skb;
917 struct ieee80211_mgmt *mgmt;
918
919 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
920 if (!skb) {
921 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
922 "frame\n", dev->name);
923 return;
924 }
925 skb_reserve(skb, local->hw.extra_tx_headroom);
926
927 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
928 memset(mgmt, 0, 24);
929 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
930 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
931 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
932 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
933 IEEE80211_STYPE_DEAUTH);
934 skb_put(skb, 2);
935 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
936
937 ieee80211_sta_tx(dev, skb, 0);
938}
939
940
941static void ieee80211_send_disassoc(struct net_device *dev,
942 struct ieee80211_if_sta *ifsta, u16 reason)
943{
944 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
945 struct sk_buff *skb;
946 struct ieee80211_mgmt *mgmt;
947
948 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
949 if (!skb) {
950 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
951 "frame\n", dev->name);
952 return;
953 }
954 skb_reserve(skb, local->hw.extra_tx_headroom);
955
956 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
957 memset(mgmt, 0, 24);
958 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
959 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
960 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
961 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
962 IEEE80211_STYPE_DISASSOC);
963 skb_put(skb, 2);
964 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
965
966 ieee80211_sta_tx(dev, skb, 0);
967}
968
969
970static int ieee80211_privacy_mismatch(struct net_device *dev,
971 struct ieee80211_if_sta *ifsta)
972{
John W. Linville65c107a2007-10-05 14:23:27 -0400973 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700974 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000975 int bss_privacy;
976 int wep_privacy;
977 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700978
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000979 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700980 return 0;
981
Johannes Berg8318d782008-01-24 19:38:38 +0100982 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
983 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400984 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700985 if (!bss)
986 return 0;
987
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000988 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
989 wep_privacy = !!ieee80211_sta_wep_configured(dev);
990 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700991
992 ieee80211_rx_bss_put(dev, bss);
993
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000994 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
995 return 0;
996
997 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700998}
999
1000
1001static void ieee80211_associate(struct net_device *dev,
1002 struct ieee80211_if_sta *ifsta)
1003{
Joe Perches0795af52007-10-03 17:59:30 -07001004 DECLARE_MAC_BUF(mac);
1005
Jiri Bencf0706e82007-05-05 11:45:53 -07001006 ifsta->assoc_tries++;
1007 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -07001008 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001009 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -07001010 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001011 ifsta->state = IEEE80211_DISABLED;
1012 return;
1013 }
1014
1015 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -07001016 printk(KERN_DEBUG "%s: associate with AP %s\n",
1017 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001018 if (ieee80211_privacy_mismatch(dev, ifsta)) {
1019 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
1020 "mixed-cell disabled - abort association\n", dev->name);
1021 ifsta->state = IEEE80211_DISABLED;
1022 return;
1023 }
1024
1025 ieee80211_send_assoc(dev, ifsta);
1026
1027 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1028}
1029
1030
1031static void ieee80211_associated(struct net_device *dev,
1032 struct ieee80211_if_sta *ifsta)
1033{
1034 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1035 struct sta_info *sta;
1036 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -07001037 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001038
1039 /* TODO: start monitoring current AP signal quality and number of
1040 * missed beacons. Scan other channels every now and then and search
1041 * for better APs. */
1042 /* TODO: remove expired BSSes */
1043
1044 ifsta->state = IEEE80211_ASSOCIATED;
1045
Johannes Bergd0709a62008-02-25 16:27:46 +01001046 rcu_read_lock();
1047
Jiri Bencf0706e82007-05-05 11:45:53 -07001048 sta = sta_info_get(local, ifsta->bssid);
1049 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -07001050 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1051 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001052 disassoc = 1;
1053 } else {
1054 disassoc = 0;
1055 if (time_after(jiffies,
1056 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001057 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001058 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -07001059 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -07001060 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -07001061 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001062 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +01001063 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001064 } else
Jiri Bencf0706e82007-05-05 11:45:53 -07001065 ieee80211_send_probe_req(dev, ifsta->bssid,
1066 local->scan_ssid,
1067 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001068 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001069 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001070 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001071 if (time_after(jiffies, ifsta->last_probe +
1072 IEEE80211_PROBE_INTERVAL)) {
1073 ifsta->last_probe = jiffies;
1074 ieee80211_send_probe_req(dev, ifsta->bssid,
1075 ifsta->ssid,
1076 ifsta->ssid_len);
1077 }
1078 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001079 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001080
1081 rcu_read_unlock();
1082
Johannes Berg3b967662008-04-08 17:56:52 +02001083 if (disassoc && sta)
Johannes Bergd0709a62008-02-25 16:27:46 +01001084 sta_info_destroy(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01001085
Jiri Bencf0706e82007-05-05 11:45:53 -07001086 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -05001087 ifsta->state = IEEE80211_DISABLED;
1088 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001089 } else {
1090 mod_timer(&ifsta->timer, jiffies +
1091 IEEE80211_MONITORING_INTERVAL);
1092 }
1093}
1094
1095
1096static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1097 u8 *ssid, size_t ssid_len)
1098{
1099 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01001100 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001101 struct sk_buff *skb;
1102 struct ieee80211_mgmt *mgmt;
1103 u8 *pos, *supp_rates, *esupp_rates = NULL;
1104 int i;
1105
1106 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1107 if (!skb) {
1108 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1109 "request\n", dev->name);
1110 return;
1111 }
1112 skb_reserve(skb, local->hw.extra_tx_headroom);
1113
1114 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1115 memset(mgmt, 0, 24);
1116 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1117 IEEE80211_STYPE_PROBE_REQ);
1118 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1119 if (dst) {
1120 memcpy(mgmt->da, dst, ETH_ALEN);
1121 memcpy(mgmt->bssid, dst, ETH_ALEN);
1122 } else {
1123 memset(mgmt->da, 0xff, ETH_ALEN);
1124 memset(mgmt->bssid, 0xff, ETH_ALEN);
1125 }
1126 pos = skb_put(skb, 2 + ssid_len);
1127 *pos++ = WLAN_EID_SSID;
1128 *pos++ = ssid_len;
1129 memcpy(pos, ssid, ssid_len);
1130
1131 supp_rates = skb_put(skb, 2);
1132 supp_rates[0] = WLAN_EID_SUPP_RATES;
1133 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001134 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1135
1136 for (i = 0; i < sband->n_bitrates; i++) {
1137 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07001138 if (esupp_rates) {
1139 pos = skb_put(skb, 1);
1140 esupp_rates[1]++;
1141 } else if (supp_rates[1] == 8) {
1142 esupp_rates = skb_put(skb, 3);
1143 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1144 esupp_rates[1] = 1;
1145 pos = &esupp_rates[2];
1146 } else {
1147 pos = skb_put(skb, 1);
1148 supp_rates[1]++;
1149 }
Johannes Berg8318d782008-01-24 19:38:38 +01001150 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -07001151 }
1152
1153 ieee80211_sta_tx(dev, skb, 0);
1154}
1155
1156
1157static int ieee80211_sta_wep_configured(struct net_device *dev)
1158{
1159 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1160 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001161 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -07001162 return 0;
1163 return 1;
1164}
1165
1166
1167static void ieee80211_auth_completed(struct net_device *dev,
1168 struct ieee80211_if_sta *ifsta)
1169{
1170 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001171 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001172 ieee80211_associate(dev, ifsta);
1173}
1174
1175
1176static void ieee80211_auth_challenge(struct net_device *dev,
1177 struct ieee80211_if_sta *ifsta,
1178 struct ieee80211_mgmt *mgmt,
1179 size_t len)
1180{
1181 u8 *pos;
1182 struct ieee802_11_elems elems;
1183
Jiri Bencf0706e82007-05-05 11:45:53 -07001184 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001185 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001186 if (!elems.challenge)
Jiri Bencf0706e82007-05-05 11:45:53 -07001187 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001188 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1189 elems.challenge_len + 2, 1);
1190}
1191
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001192static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1193 u8 dialog_token, u16 status, u16 policy,
1194 u16 buf_size, u16 timeout)
1195{
1196 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1197 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1198 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1199 struct sk_buff *skb;
1200 struct ieee80211_mgmt *mgmt;
1201 u16 capab;
1202
Ester Kummer3acea5b2008-04-17 16:05:14 -07001203 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1204
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001205 if (!skb) {
1206 printk(KERN_DEBUG "%s: failed to allocate buffer "
1207 "for addba resp frame\n", dev->name);
1208 return;
1209 }
1210
1211 skb_reserve(skb, local->hw.extra_tx_headroom);
1212 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1213 memset(mgmt, 0, 24);
1214 memcpy(mgmt->da, da, ETH_ALEN);
1215 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001216 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001217 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1218 else
1219 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1220 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1221 IEEE80211_STYPE_ACTION);
1222
1223 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1224 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1225 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1226 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1227
1228 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1229 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1230 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1231
1232 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1233 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1234 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1235
1236 ieee80211_sta_tx(dev, skb, 0);
1237
1238 return;
1239}
1240
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001241void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1242 u16 tid, u8 dialog_token, u16 start_seq_num,
1243 u16 agg_size, u16 timeout)
1244{
1245 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1246 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1247 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1248 struct sk_buff *skb;
1249 struct ieee80211_mgmt *mgmt;
1250 u16 capab;
1251
Ester Kummer3acea5b2008-04-17 16:05:14 -07001252 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001253
1254 if (!skb) {
1255 printk(KERN_ERR "%s: failed to allocate buffer "
1256 "for addba request frame\n", dev->name);
1257 return;
1258 }
1259 skb_reserve(skb, local->hw.extra_tx_headroom);
1260 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1261 memset(mgmt, 0, 24);
1262 memcpy(mgmt->da, da, ETH_ALEN);
1263 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1264 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1265 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1266 else
1267 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1268
1269 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1270 IEEE80211_STYPE_ACTION);
1271
1272 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1273
1274 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1275 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1276
1277 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1278 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1279 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1280 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1281
1282 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1283
1284 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1285 mgmt->u.action.u.addba_req.start_seq_num =
1286 cpu_to_le16(start_seq_num << 4);
1287
1288 ieee80211_sta_tx(dev, skb, 0);
1289}
1290
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001291static void ieee80211_sta_process_addba_request(struct net_device *dev,
1292 struct ieee80211_mgmt *mgmt,
1293 size_t len)
1294{
1295 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001296 struct ieee80211_hw *hw = &local->hw;
1297 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001298 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001299 struct tid_ampdu_rx *tid_agg_rx;
1300 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001301 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001302 int ret = -EOPNOTSUPP;
1303 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001304
Johannes Bergd0709a62008-02-25 16:27:46 +01001305 rcu_read_lock();
1306
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001307 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001308 if (!sta) {
1309 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001310 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001311 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001312
1313 /* extract session parameters from addba request frame */
1314 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1315 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001316 start_seq_num =
1317 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001318
1319 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1320 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1321 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1322 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1323
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001324 status = WLAN_STATUS_REQUEST_DECLINED;
1325
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001326 /* sanity check for incoming parameters:
1327 * check if configuration can support the BA policy
1328 * and if buffer size does not exceeds max value */
1329 if (((ba_policy != 1)
1330 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1331 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1332 status = WLAN_STATUS_INVALID_QOS_PARAM;
1333#ifdef CONFIG_MAC80211_HT_DEBUG
1334 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001335 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001336 "%s on tid %u. policy %d, buffer size %d\n",
1337 print_mac(mac, mgmt->sa), tid, ba_policy,
1338 buf_size);
1339#endif /* CONFIG_MAC80211_HT_DEBUG */
1340 goto end_no_lock;
1341 }
1342 /* determine default buffer size */
1343 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001344 struct ieee80211_supported_band *sband;
1345
1346 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001347 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001348 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001349 }
1350
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001351
1352 /* examine state machine */
Johannes Berg07346f812008-05-03 01:02:02 +02001353 spin_lock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001354
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001355 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001356#ifdef CONFIG_MAC80211_HT_DEBUG
1357 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001358 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001359 "%s on tid %u\n",
1360 print_mac(mac, mgmt->sa), tid);
1361#endif /* CONFIG_MAC80211_HT_DEBUG */
1362 goto end;
1363 }
1364
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001365 /* prepare A-MPDU MLME for Rx aggregation */
1366 sta->ampdu_mlme.tid_rx[tid] =
1367 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1368 if (!sta->ampdu_mlme.tid_rx[tid]) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001369#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001370 if (net_ratelimit())
1371 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1372 tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001373#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001374 goto end;
1375 }
1376 /* rx timer */
1377 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1378 sta_rx_agg_session_timer_expired;
1379 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1380 (unsigned long)&sta->timer_to_tid[tid];
1381 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1382
1383 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1384
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001385 /* prepare reordering buffer */
1386 tid_agg_rx->reorder_buf =
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301387 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001388 if (!tid_agg_rx->reorder_buf) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001389#ifdef CONFIG_MAC80211_HT_DEBUG
Johannes Berg03147df2008-02-26 00:39:28 +01001390 if (net_ratelimit())
1391 printk(KERN_ERR "can not allocate reordering buffer "
1392 "to tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001393#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001394 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001395 goto end;
1396 }
1397 memset(tid_agg_rx->reorder_buf, 0,
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301398 buf_size * sizeof(struct sk_buff *));
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001399
1400 if (local->ops->ampdu_action)
1401 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001402 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001403#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001404 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001405#endif /* CONFIG_MAC80211_HT_DEBUG */
1406
1407 if (ret) {
1408 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001409 kfree(tid_agg_rx);
1410 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001411 goto end;
1412 }
1413
1414 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001415 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001416 tid_agg_rx->dialog_token = dialog_token;
1417 tid_agg_rx->ssn = start_seq_num;
1418 tid_agg_rx->head_seq_num = start_seq_num;
1419 tid_agg_rx->buf_size = buf_size;
1420 tid_agg_rx->timeout = timeout;
1421 tid_agg_rx->stored_mpdu_num = 0;
1422 status = WLAN_STATUS_SUCCESS;
1423end:
Johannes Berg07346f812008-05-03 01:02:02 +02001424 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001425
1426end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001427 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1428 dialog_token, status, 1, buf_size, timeout);
1429 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001430}
Jiri Bencf0706e82007-05-05 11:45:53 -07001431
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001432static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1433 struct ieee80211_mgmt *mgmt,
1434 size_t len)
1435{
1436 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1437 struct ieee80211_hw *hw = &local->hw;
1438 struct sta_info *sta;
1439 u16 capab;
1440 u16 tid;
1441 u8 *state;
1442
Johannes Bergd0709a62008-02-25 16:27:46 +01001443 rcu_read_lock();
1444
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001445 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001446 if (!sta) {
1447 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001448 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001449 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001450
1451 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1452 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1453
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001454 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001455
Johannes Berg07346f812008-05-03 01:02:02 +02001456 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001457
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001458 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001459 spin_unlock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001460 goto addba_resp_exit;
1461 }
1462
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001463 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001464 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Johannes Berg07346f812008-05-03 01:02:02 +02001465 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001466#ifdef CONFIG_MAC80211_HT_DEBUG
1467 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1468#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001469 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001470 }
1471
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001472 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001473#ifdef CONFIG_MAC80211_HT_DEBUG
1474 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1475#endif /* CONFIG_MAC80211_HT_DEBUG */
1476 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1477 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001478 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001479 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001480
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001481 if (*state == HT_AGG_STATE_OPERATIONAL)
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001482 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001483
Johannes Berg07346f812008-05-03 01:02:02 +02001484 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001485 } else {
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001486 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001487 /* this will allow the state check in stop_BA_session */
1488 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001489 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001490 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1491 WLAN_BACK_INITIATOR);
1492 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001493
1494addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001495 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001496}
1497
1498void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1499 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001500{
1501 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1502 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1503 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1504 struct sk_buff *skb;
1505 struct ieee80211_mgmt *mgmt;
1506 u16 params;
1507
Ester Kummer3acea5b2008-04-17 16:05:14 -07001508 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001509
1510 if (!skb) {
1511 printk(KERN_ERR "%s: failed to allocate buffer "
1512 "for delba frame\n", dev->name);
1513 return;
1514 }
1515
1516 skb_reserve(skb, local->hw.extra_tx_headroom);
1517 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1518 memset(mgmt, 0, 24);
1519 memcpy(mgmt->da, da, ETH_ALEN);
1520 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001521 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001522 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1523 else
1524 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1525 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1526 IEEE80211_STYPE_ACTION);
1527
1528 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1529
1530 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1531 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1532 params = (u16)(initiator << 11); /* bit 11 initiator */
1533 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1534
1535 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1536 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1537
1538 ieee80211_sta_tx(dev, skb, 0);
1539}
1540
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001541void ieee80211_send_bar(struct net_device *dev, u8 *ra, u16 tid, u16 ssn)
1542{
1543 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1544 struct sk_buff *skb;
1545 struct ieee80211_bar *bar;
1546 u16 bar_control = 0;
1547
1548 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
1549 if (!skb) {
1550 printk(KERN_ERR "%s: failed to allocate buffer for "
1551 "bar frame\n", dev->name);
1552 return;
1553 }
1554 skb_reserve(skb, local->hw.extra_tx_headroom);
1555 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
1556 memset(bar, 0, sizeof(*bar));
1557 bar->frame_control = IEEE80211_FC(IEEE80211_FTYPE_CTL,
1558 IEEE80211_STYPE_BACK_REQ);
1559 memcpy(bar->ra, ra, ETH_ALEN);
1560 memcpy(bar->ta, dev->dev_addr, ETH_ALEN);
1561 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
1562 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
1563 bar_control |= (u16)(tid << 12);
1564 bar->control = cpu_to_le16(bar_control);
1565 bar->start_seq_num = cpu_to_le16(ssn);
1566
1567 ieee80211_sta_tx(dev, skb, 0);
1568}
1569
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001570void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1571 u16 initiator, u16 reason)
1572{
1573 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1574 struct ieee80211_hw *hw = &local->hw;
1575 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001576 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001577 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001578
Johannes Bergd0709a62008-02-25 16:27:46 +01001579 rcu_read_lock();
1580
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001581 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001582 if (!sta) {
1583 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001584 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001585 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001586
1587 /* check if TID is in operational state */
Johannes Berg07346f812008-05-03 01:02:02 +02001588 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001589 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001590 != HT_AGG_STATE_OPERATIONAL) {
Johannes Berg07346f812008-05-03 01:02:02 +02001591 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +01001592 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001593 return;
1594 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001595 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001596 HT_AGG_STATE_REQ_STOP_BA_MSK |
1597 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Johannes Berg07346f812008-05-03 01:02:02 +02001598 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001599
1600 /* stop HW Rx aggregation. ampdu_action existence
1601 * already verified in session init so we add the BUG_ON */
1602 BUG_ON(!local->ops->ampdu_action);
1603
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001604#ifdef CONFIG_MAC80211_HT_DEBUG
1605 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1606 print_mac(mac, ra), tid);
1607#endif /* CONFIG_MAC80211_HT_DEBUG */
1608
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001609 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001610 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001611 if (ret)
1612 printk(KERN_DEBUG "HW problem - can not stop rx "
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001613 "aggregation for tid %d\n", tid);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001614
1615 /* shutdown timer has not expired */
1616 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001617 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001618
1619 /* check if this is a self generated aggregation halt */
1620 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1621 ieee80211_send_delba(dev, ra, tid, 0, reason);
1622
1623 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001624 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1625 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001626 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001627 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1628 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1629 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001630 }
1631 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001632 /* free resources */
1633 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1634 kfree(sta->ampdu_mlme.tid_rx[tid]);
1635 sta->ampdu_mlme.tid_rx[tid] = NULL;
1636 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001637
Johannes Bergd0709a62008-02-25 16:27:46 +01001638 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001639}
1640
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001641
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001642static void ieee80211_sta_process_delba(struct net_device *dev,
1643 struct ieee80211_mgmt *mgmt, size_t len)
1644{
1645 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1646 struct sta_info *sta;
1647 u16 tid, params;
1648 u16 initiator;
1649 DECLARE_MAC_BUF(mac);
1650
Johannes Bergd0709a62008-02-25 16:27:46 +01001651 rcu_read_lock();
1652
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001653 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001654 if (!sta) {
1655 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001656 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001657 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001658
1659 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1660 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1661 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1662
1663#ifdef CONFIG_MAC80211_HT_DEBUG
1664 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001665 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1666 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001667 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001668 mgmt->u.action.u.delba.reason_code);
1669#endif /* CONFIG_MAC80211_HT_DEBUG */
1670
1671 if (initiator == WLAN_BACK_INITIATOR)
1672 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1673 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001674 else { /* WLAN_BACK_RECIPIENT */
Johannes Berg07346f812008-05-03 01:02:02 +02001675 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001676 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001677 HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001678 spin_unlock_bh(&sta->lock);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001679 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1680 WLAN_BACK_RECIPIENT);
1681 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001682 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001683}
1684
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001685/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001686 * After sending add Block Ack request we activated a timer until
1687 * add Block Ack response will arrive from the recipient.
1688 * If this timer expires sta_addba_resp_timer_expired will be executed.
1689 */
1690void sta_addba_resp_timer_expired(unsigned long data)
1691{
1692 /* not an elegant detour, but there is no choice as the timer passes
1693 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001694 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001695 * array gives the sta through container_of */
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301696 u16 tid = *(u8 *)data;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001697 struct sta_info *temp_sta = container_of((void *)data,
1698 struct sta_info, timer_to_tid[tid]);
1699
1700 struct ieee80211_local *local = temp_sta->local;
1701 struct ieee80211_hw *hw = &local->hw;
1702 struct sta_info *sta;
1703 u8 *state;
1704
Johannes Bergd0709a62008-02-25 16:27:46 +01001705 rcu_read_lock();
1706
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001707 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001708 if (!sta) {
1709 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001710 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001711 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001712
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001713 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001714 /* check if the TID waits for addBA response */
Johannes Berg07346f812008-05-03 01:02:02 +02001715 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001716 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001717 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001718 *state = HT_AGG_STATE_IDLE;
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001719#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001720 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1721 "expecting addBA response there", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001722#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001723 goto timer_expired_exit;
1724 }
1725
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001726#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001727 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001728#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001729
1730 /* go through the state check in stop_BA_session */
1731 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001732 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001733 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1734 WLAN_BACK_INITIATOR);
1735
1736timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001737 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001738}
1739
1740/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001741 * After accepting the AddBA Request we activated a timer,
1742 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001743 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1744 */
Adrian Bunk712590d2008-04-21 11:47:51 +03001745static void sta_rx_agg_session_timer_expired(unsigned long data)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001746{
1747 /* not an elegant detour, but there is no choice as the timer passes
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301748 * only one argument, and various sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001749 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001750 * array gives the sta through container_of */
1751 u8 *ptid = (u8 *)data;
1752 u8 *timer_to_id = ptid - *ptid;
1753 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1754 timer_to_tid[0]);
1755
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001756#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001757 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001758#endif
Johannes Bergd0709a62008-02-25 16:27:46 +01001759 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1760 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001761 WLAN_REASON_QSTA_TIMEOUT);
1762}
1763
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001764void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1765{
1766 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1767 int i;
1768
1769 for (i = 0; i < STA_TID_NUM; i++) {
1770 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1771 WLAN_BACK_INITIATOR);
1772 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1773 WLAN_BACK_RECIPIENT,
1774 WLAN_REASON_QSTA_LEAVE_QBSS);
1775 }
1776}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001777
Assaf Kraussb6623482008-06-16 16:09:49 +03001778static void ieee80211_send_refuse_measurement_request(struct net_device *dev,
1779 struct ieee80211_msrment_ie *request_ie,
1780 const u8 *da, const u8 *bssid,
1781 u8 dialog_token)
1782{
1783 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1784 struct sk_buff *skb;
1785 struct ieee80211_mgmt *msr_report;
1786
1787 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1788 sizeof(struct ieee80211_msrment_ie));
1789
1790 if (!skb) {
1791 printk(KERN_ERR "%s: failed to allocate buffer for "
1792 "measurement report frame\n", dev->name);
1793 return;
1794 }
1795
1796 skb_reserve(skb, local->hw.extra_tx_headroom);
1797 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1798 memset(msr_report, 0, 24);
1799 memcpy(msr_report->da, da, ETH_ALEN);
1800 memcpy(msr_report->sa, dev->dev_addr, ETH_ALEN);
1801 memcpy(msr_report->bssid, bssid, ETH_ALEN);
1802 msr_report->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1803 IEEE80211_STYPE_ACTION);
1804
1805 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1806 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1807 msr_report->u.action.u.measurement.action_code =
1808 WLAN_ACTION_SPCT_MSR_RPRT;
1809 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1810
1811 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1812 msr_report->u.action.u.measurement.length =
1813 sizeof(struct ieee80211_msrment_ie);
1814
1815 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1816 sizeof(struct ieee80211_msrment_ie));
1817 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1818 msr_report->u.action.u.measurement.msr_elem.mode |=
1819 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1820 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1821
1822 ieee80211_sta_tx(dev, skb, 0);
1823}
1824
1825static void ieee80211_sta_process_measurement_req(struct net_device *dev,
1826 struct ieee80211_mgmt *mgmt,
1827 size_t len)
1828{
1829 /*
1830 * Ignoring measurement request is spec violation.
1831 * Mandatory measurements must be reported optional
1832 * measurements might be refused or reported incapable
1833 * For now just refuse
1834 * TODO: Answer basic measurement as unmeasured
1835 */
1836 ieee80211_send_refuse_measurement_request(dev,
1837 &mgmt->u.action.u.measurement.msr_elem,
1838 mgmt->sa, mgmt->bssid,
1839 mgmt->u.action.u.measurement.dialog_token);
1840}
1841
1842
Jiri Bencf0706e82007-05-05 11:45:53 -07001843static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1844 struct ieee80211_if_sta *ifsta,
1845 struct ieee80211_mgmt *mgmt,
1846 size_t len)
1847{
1848 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1849 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001850 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001851
1852 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001853 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07001854 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001855
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001856 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07001857 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001858
Johannes Berg51fb61e2007-12-19 01:31:27 +01001859 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001860 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001861 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001862
Johannes Berg51fb61e2007-12-19 01:31:27 +01001863 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001864 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001865 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001866
1867 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1868 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1869 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1870
Johannes Berg51fb61e2007-12-19 01:31:27 +01001871 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg135a2112008-06-16 20:55:29 +02001872 /*
1873 * IEEE 802.11 standard does not require authentication in IBSS
Jiri Bencf0706e82007-05-05 11:45:53 -07001874 * networks and most implementations do not seem to use it.
1875 * However, try to reply to authentication attempts if someone
1876 * has actually implemented this.
Johannes Berg135a2112008-06-16 20:55:29 +02001877 */
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001878 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
Jiri Bencf0706e82007-05-05 11:45:53 -07001879 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001880 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1881 }
1882
1883 if (auth_alg != ifsta->auth_alg ||
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001884 auth_transaction != ifsta->auth_transaction)
Jiri Bencf0706e82007-05-05 11:45:53 -07001885 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001886
1887 if (status_code != WLAN_STATUS_SUCCESS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001888 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1889 u8 algs[3];
1890 const int num_algs = ARRAY_SIZE(algs);
1891 int i, pos;
1892 algs[0] = algs[1] = algs[2] = 0xff;
1893 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1894 algs[0] = WLAN_AUTH_OPEN;
1895 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1896 algs[1] = WLAN_AUTH_SHARED_KEY;
1897 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1898 algs[2] = WLAN_AUTH_LEAP;
1899 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1900 pos = 0;
1901 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1902 pos = 1;
1903 else
1904 pos = 2;
1905 for (i = 0; i < num_algs; i++) {
1906 pos++;
1907 if (pos >= num_algs)
1908 pos = 0;
1909 if (algs[pos] == ifsta->auth_alg ||
1910 algs[pos] == 0xff)
1911 continue;
1912 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1913 !ieee80211_sta_wep_configured(dev))
1914 continue;
1915 ifsta->auth_alg = algs[pos];
Jiri Bencf0706e82007-05-05 11:45:53 -07001916 break;
1917 }
1918 }
1919 return;
1920 }
1921
1922 switch (ifsta->auth_alg) {
1923 case WLAN_AUTH_OPEN:
1924 case WLAN_AUTH_LEAP:
1925 ieee80211_auth_completed(dev, ifsta);
1926 break;
1927 case WLAN_AUTH_SHARED_KEY:
1928 if (ifsta->auth_transaction == 4)
1929 ieee80211_auth_completed(dev, ifsta);
1930 else
1931 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1932 break;
1933 }
1934}
1935
1936
1937static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1938 struct ieee80211_if_sta *ifsta,
1939 struct ieee80211_mgmt *mgmt,
1940 size_t len)
1941{
1942 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001943 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001944
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001945 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07001946 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001947
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001948 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07001949 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001950
1951 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1952
Johannes Berg988c0f72008-04-17 19:21:22 +02001953 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001954 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001955
1956 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1957 ifsta->state == IEEE80211_ASSOCIATE ||
1958 ifsta->state == IEEE80211_ASSOCIATED) {
1959 ifsta->state = IEEE80211_AUTHENTICATE;
1960 mod_timer(&ifsta->timer, jiffies +
1961 IEEE80211_RETRY_AUTH_INTERVAL);
1962 }
1963
1964 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001965 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001966}
1967
1968
1969static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1970 struct ieee80211_if_sta *ifsta,
1971 struct ieee80211_mgmt *mgmt,
1972 size_t len)
1973{
1974 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001975 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001976
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001977 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07001978 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001979
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001980 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07001981 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001982
1983 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1984
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001985 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001986 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1987
1988 if (ifsta->state == IEEE80211_ASSOCIATED) {
1989 ifsta->state = IEEE80211_ASSOCIATE;
1990 mod_timer(&ifsta->timer, jiffies +
1991 IEEE80211_RETRY_AUTH_INTERVAL);
1992 }
1993
1994 ieee80211_set_disassoc(dev, ifsta, 0);
1995}
1996
1997
Johannes Berg471b3ef2007-12-28 14:32:58 +01001998static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001999 struct ieee80211_if_sta *ifsta,
2000 struct ieee80211_mgmt *mgmt,
2001 size_t len,
2002 int reassoc)
2003{
Johannes Berg471b3ef2007-12-28 14:32:58 +01002004 struct ieee80211_local *local = sdata->local;
2005 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01002006 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002007 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01002008 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002009 u16 capab_info, status_code, aid;
2010 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002011 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002012 u8 *pos;
2013 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07002014 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01002015 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002016
2017 /* AssocResp and ReassocResp have identical structure, so process both
2018 * of them in this function. */
2019
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002020 if (ifsta->state != IEEE80211_ASSOCIATE)
Jiri Bencf0706e82007-05-05 11:45:53 -07002021 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002022
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002023 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07002024 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002025
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002026 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07002027 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002028
2029 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2030 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2031 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07002032
Joe Perches0795af52007-10-03 17:59:30 -07002033 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07002034 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07002035 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02002036 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07002037
2038 if (status_code != WLAN_STATUS_SUCCESS) {
2039 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
2040 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02002041 /* if this was a reassociation, ensure we try a "full"
2042 * association next time. This works around some broken APs
2043 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002044 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07002045 return;
2046 }
2047
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002048 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2049 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
2050 "set\n", dev->name, aid);
2051 aid &= ~(BIT(15) | BIT(14));
2052
Jiri Bencf0706e82007-05-05 11:45:53 -07002053 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04002054 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002055
2056 if (!elems.supp_rates) {
2057 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
2058 dev->name);
2059 return;
2060 }
2061
2062 printk(KERN_DEBUG "%s: associated\n", dev->name);
2063 ifsta->aid = aid;
2064 ifsta->ap_capab = capab_info;
2065
2066 kfree(ifsta->assocresp_ies);
2067 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002068 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002069 if (ifsta->assocresp_ies)
2070 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2071
Johannes Bergd0709a62008-02-25 16:27:46 +01002072 rcu_read_lock();
2073
Jiri Bencf0706e82007-05-05 11:45:53 -07002074 /* Add STA entry for the AP */
2075 sta = sta_info_get(local, ifsta->bssid);
2076 if (!sta) {
2077 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01002078 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01002079
Johannes Berg73651ee2008-02-25 16:27:47 +01002080 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2081 if (!sta) {
2082 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2083 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01002084 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002085 return;
2086 }
John W. Linville65c107a2007-10-05 14:23:27 -04002087 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01002088 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002089 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002090 if (bss) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002091 sta->last_signal = bss->signal;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002092 sta->last_qual = bss->qual;
Jiri Bencf0706e82007-05-05 11:45:53 -07002093 sta->last_noise = bss->noise;
2094 ieee80211_rx_bss_put(dev, bss);
2095 }
Johannes Berg73651ee2008-02-25 16:27:47 +01002096
2097 err = sta_info_insert(sta);
2098 if (err) {
2099 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2100 " the AP (error %d)\n", dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01002101 rcu_read_unlock();
2102 return;
2103 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002104 }
2105
Johannes Berg73651ee2008-02-25 16:27:47 +01002106 /*
2107 * FIXME: Do we really need to update the sta_info's information here?
2108 * We already know about the AP (we found it in our list) so it
2109 * should already be filled with the right info, no?
2110 * As is stands, all this is racy because typically we assume
2111 * the information that is filled in here (except flags) doesn't
2112 * change while a STA structure is alive. As such, it should move
2113 * to between the sta_info_alloc() and sta_info_insert() above.
2114 */
2115
Johannes Berg07346f812008-05-03 01:02:02 +02002116 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2117 WLAN_STA_AUTHORIZED);
Jiri Bencf0706e82007-05-05 11:45:53 -07002118
2119 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002120 basic_rates = 0;
2121 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2122
Jiri Bencf0706e82007-05-05 11:45:53 -07002123 for (i = 0; i < elems.supp_rates_len; i++) {
2124 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002125
2126 if (rate > 110)
2127 have_higher_than_11mbit = true;
2128
2129 for (j = 0; j < sband->n_bitrates; j++) {
2130 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002131 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002132 if (elems.supp_rates[i] & 0x80)
2133 basic_rates |= BIT(j);
2134 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002135 }
Johannes Berg8318d782008-01-24 19:38:38 +01002136
Jiri Bencf0706e82007-05-05 11:45:53 -07002137 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2138 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002139
2140 if (rate > 110)
2141 have_higher_than_11mbit = true;
2142
2143 for (j = 0; j < sband->n_bitrates; j++) {
2144 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002145 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002146 if (elems.ext_supp_rates[i] & 0x80)
2147 basic_rates |= BIT(j);
2148 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002149 }
Johannes Berg8318d782008-01-24 19:38:38 +01002150
2151 sta->supp_rates[local->hw.conf.channel->band] = rates;
2152 sdata->basic_rates = basic_rates;
2153
2154 /* cf. IEEE 802.11 9.2.12 */
2155 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2156 have_higher_than_11mbit)
2157 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2158 else
2159 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002160
Johannes Berg3434fbd2008-05-03 00:59:37 +02002161 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2162 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002163 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002164 ieee80211_ht_cap_ie_to_ht_info(
2165 (struct ieee80211_ht_cap *)
2166 elems.ht_cap_elem, &sta->ht_info);
2167 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2168 (struct ieee80211_ht_addt_info *)
2169 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002170 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002171 }
2172
Jiri Bencf0706e82007-05-05 11:45:53 -07002173 rate_control_rate_init(sta, local);
2174
Johannes Berg3434fbd2008-05-03 00:59:37 +02002175 if (elems.wmm_param) {
Johannes Berg07346f812008-05-03 01:02:02 +02002176 set_sta_flags(sta, WLAN_STA_WME);
Johannes Berge5f98f22008-03-05 20:39:31 +01002177 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002178 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2179 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002180 } else
2181 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002182
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002183 /* set AID and assoc capability,
2184 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002185 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002186 bss_conf->assoc_capability = capab_info;
Johannes Berg8318d782008-01-24 19:38:38 +01002187 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002188
Jiri Bencf0706e82007-05-05 11:45:53 -07002189 ieee80211_associated(dev, ifsta);
2190}
2191
2192
2193/* Caller must hold local->sta_bss_lock */
2194static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2195 struct ieee80211_sta_bss *bss)
2196{
2197 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002198 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002199
2200 if (bss_mesh_cfg(bss))
2201 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2202 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002203 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002204 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002205
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002206 bss->hnext = local->sta_bss_hash[hash_idx];
2207 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002208}
2209
2210
2211/* Caller must hold local->sta_bss_lock */
2212static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2213 struct ieee80211_sta_bss *bss)
2214{
2215 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2216 struct ieee80211_sta_bss *b, *prev = NULL;
2217 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2218 while (b) {
2219 if (b == bss) {
2220 if (!prev)
2221 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2222 bss->hnext;
2223 else
2224 prev->hnext = bss->hnext;
2225 break;
2226 }
2227 prev = b;
2228 b = b->hnext;
2229 }
2230}
2231
2232
2233static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002234ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002235 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002236{
2237 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2238 struct ieee80211_sta_bss *bss;
2239
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002240 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002241 if (!bss)
2242 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002243 atomic_inc(&bss->users);
2244 atomic_inc(&bss->users);
2245 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002246 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002247 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2248 memcpy(bss->ssid, ssid, ssid_len);
2249 bss->ssid_len = ssid_len;
2250 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002251
2252 spin_lock_bh(&local->sta_bss_lock);
2253 /* TODO: order by RSSI? */
2254 list_add_tail(&bss->list, &local->sta_bss_list);
2255 __ieee80211_rx_bss_hash_add(dev, bss);
2256 spin_unlock_bh(&local->sta_bss_lock);
2257 return bss;
2258}
2259
Jiri Bencf0706e82007-05-05 11:45:53 -07002260static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002261ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002262 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002263{
2264 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2265 struct ieee80211_sta_bss *bss;
2266
2267 spin_lock_bh(&local->sta_bss_lock);
2268 bss = local->sta_bss_hash[STA_HASH(bssid)];
2269 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002270 if (!bss_mesh_cfg(bss) &&
2271 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002272 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002273 bss->ssid_len == ssid_len &&
2274 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002275 atomic_inc(&bss->users);
2276 break;
2277 }
2278 bss = bss->hnext;
2279 }
2280 spin_unlock_bh(&local->sta_bss_lock);
2281 return bss;
2282}
2283
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002284#ifdef CONFIG_MAC80211_MESH
2285static struct ieee80211_sta_bss *
2286ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2287 u8 *mesh_cfg, int freq)
2288{
2289 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2290 struct ieee80211_sta_bss *bss;
2291
2292 spin_lock_bh(&local->sta_bss_lock);
2293 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2294 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002295 if (bss_mesh_cfg(bss) &&
2296 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002297 bss->freq == freq &&
2298 mesh_id_len == bss->mesh_id_len &&
2299 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2300 mesh_id_len))) {
2301 atomic_inc(&bss->users);
2302 break;
2303 }
2304 bss = bss->hnext;
2305 }
2306 spin_unlock_bh(&local->sta_bss_lock);
2307 return bss;
2308}
2309
2310static struct ieee80211_sta_bss *
2311ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002312 u8 *mesh_cfg, int mesh_config_len, int freq)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002313{
2314 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2315 struct ieee80211_sta_bss *bss;
2316
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002317 if (mesh_config_len != MESH_CFG_LEN)
2318 return NULL;
2319
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002320 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2321 if (!bss)
2322 return NULL;
2323
John W. Linville24736702008-04-08 14:15:46 -04002324 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002325 if (!bss->mesh_cfg) {
2326 kfree(bss);
2327 return NULL;
2328 }
2329
2330 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2331 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2332 if (!bss->mesh_id) {
2333 kfree(bss->mesh_cfg);
2334 kfree(bss);
2335 return NULL;
2336 }
2337 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2338 }
2339
2340 atomic_inc(&bss->users);
2341 atomic_inc(&bss->users);
John W. Linville24736702008-04-08 14:15:46 -04002342 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002343 bss->mesh_id_len = mesh_id_len;
2344 bss->freq = freq;
2345 spin_lock_bh(&local->sta_bss_lock);
2346 /* TODO: order by RSSI? */
2347 list_add_tail(&bss->list, &local->sta_bss_list);
2348 __ieee80211_rx_bss_hash_add(dev, bss);
2349 spin_unlock_bh(&local->sta_bss_lock);
2350 return bss;
2351}
2352#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002353
2354static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2355{
2356 kfree(bss->wpa_ie);
2357 kfree(bss->rsn_ie);
2358 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002359 kfree(bss->ht_ie);
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002360 kfree(bss->ht_add_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002361 kfree(bss_mesh_id(bss));
2362 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002363 kfree(bss);
2364}
2365
2366
2367static void ieee80211_rx_bss_put(struct net_device *dev,
2368 struct ieee80211_sta_bss *bss)
2369{
2370 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -07002371
Pavel Emelyanov1ebebea2008-04-23 11:47:15 +04002372 local_bh_disable();
2373 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2374 local_bh_enable();
2375 return;
2376 }
2377
Jiri Bencf0706e82007-05-05 11:45:53 -07002378 __ieee80211_rx_bss_hash_del(dev, bss);
2379 list_del(&bss->list);
2380 spin_unlock_bh(&local->sta_bss_lock);
2381 ieee80211_rx_bss_free(bss);
2382}
2383
2384
2385void ieee80211_rx_bss_list_init(struct net_device *dev)
2386{
2387 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2388 spin_lock_init(&local->sta_bss_lock);
2389 INIT_LIST_HEAD(&local->sta_bss_list);
2390}
2391
2392
2393void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2394{
2395 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2396 struct ieee80211_sta_bss *bss, *tmp;
2397
2398 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2399 ieee80211_rx_bss_put(dev, bss);
2400}
2401
2402
Bruno Randolfa6072682008-02-18 11:21:15 +09002403static int ieee80211_sta_join_ibss(struct net_device *dev,
2404 struct ieee80211_if_sta *ifsta,
2405 struct ieee80211_sta_bss *bss)
2406{
2407 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2408 int res, rates, i, j;
2409 struct sk_buff *skb;
2410 struct ieee80211_mgmt *mgmt;
Johannes Berge039fa42008-05-15 12:55:29 +02002411 struct ieee80211_tx_info *control;
Bruno Randolfa6072682008-02-18 11:21:15 +09002412 struct rate_selection ratesel;
2413 u8 *pos;
2414 struct ieee80211_sub_if_data *sdata;
2415 struct ieee80211_supported_band *sband;
Dan Williams507b06d2008-06-03 23:39:55 -04002416 union iwreq_data wrqu;
Bruno Randolfa6072682008-02-18 11:21:15 +09002417
2418 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2419
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002420 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2421
Bruno Randolfa6072682008-02-18 11:21:15 +09002422 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002423 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002424
2425 if (local->ops->reset_tsf) {
2426 /* Reset own TSF to allow time synchronization work. */
2427 local->ops->reset_tsf(local_to_hw(local));
2428 }
2429 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2430 res = ieee80211_if_config(dev);
2431 if (res)
2432 return res;
2433
2434 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2435
Bruno Randolfa6072682008-02-18 11:21:15 +09002436 sdata->drop_unencrypted = bss->capability &
2437 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2438
Assaf Kraussbe038b32008-06-05 19:55:21 +03002439 res = ieee80211_set_freq(dev, bss->freq);
Bruno Randolfa6072682008-02-18 11:21:15 +09002440
Assaf Kraussbe038b32008-06-05 19:55:21 +03002441 if (res)
2442 return res;
Bruno Randolfa6072682008-02-18 11:21:15 +09002443
2444 /* Set beacon template */
2445 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2446 do {
2447 if (!skb)
2448 break;
2449
2450 skb_reserve(skb, local->hw.extra_tx_headroom);
2451
2452 mgmt = (struct ieee80211_mgmt *)
2453 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2454 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2455 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2456 IEEE80211_STYPE_BEACON);
2457 memset(mgmt->da, 0xff, ETH_ALEN);
2458 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2459 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2460 mgmt->u.beacon.beacon_int =
2461 cpu_to_le16(local->hw.conf.beacon_int);
Assaf Krauss4faeb862008-06-30 17:23:16 +08002462 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
Bruno Randolfa6072682008-02-18 11:21:15 +09002463 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2464
2465 pos = skb_put(skb, 2 + ifsta->ssid_len);
2466 *pos++ = WLAN_EID_SSID;
2467 *pos++ = ifsta->ssid_len;
2468 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2469
2470 rates = bss->supp_rates_len;
2471 if (rates > 8)
2472 rates = 8;
2473 pos = skb_put(skb, 2 + rates);
2474 *pos++ = WLAN_EID_SUPP_RATES;
2475 *pos++ = rates;
2476 memcpy(pos, bss->supp_rates, rates);
2477
2478 if (bss->band == IEEE80211_BAND_2GHZ) {
2479 pos = skb_put(skb, 2 + 1);
2480 *pos++ = WLAN_EID_DS_PARAMS;
2481 *pos++ = 1;
2482 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2483 }
2484
2485 pos = skb_put(skb, 2 + 2);
2486 *pos++ = WLAN_EID_IBSS_PARAMS;
2487 *pos++ = 2;
2488 /* FIX: set ATIM window based on scan results */
2489 *pos++ = 0;
2490 *pos++ = 0;
2491
2492 if (bss->supp_rates_len > 8) {
2493 rates = bss->supp_rates_len - 8;
2494 pos = skb_put(skb, 2 + rates);
2495 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2496 *pos++ = rates;
2497 memcpy(pos, &bss->supp_rates[8], rates);
2498 }
2499
Johannes Berge039fa42008-05-15 12:55:29 +02002500 control = IEEE80211_SKB_CB(skb);
2501
Bruno Randolfa6072682008-02-18 11:21:15 +09002502 rate_control_get_rate(dev, sband, skb, &ratesel);
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002503 if (ratesel.rate_idx < 0) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002504 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2505 "for IBSS beacon\n", dev->name);
2506 break;
2507 }
Johannes Berge039fa42008-05-15 12:55:29 +02002508 control->control.vif = &sdata->vif;
2509 control->tx_rate_idx = ratesel.rate_idx;
Bruno Randolfa6072682008-02-18 11:21:15 +09002510 if (sdata->bss_conf.use_short_preamble &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002511 sband->bitrates[ratesel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
Johannes Berge039fa42008-05-15 12:55:29 +02002512 control->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
2513 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2514 control->flags |= IEEE80211_TX_CTL_NO_ACK;
Ivo van Doornb2898a22008-07-01 10:45:13 +02002515 control->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
Johannes Berge039fa42008-05-15 12:55:29 +02002516 control->control.retry_limit = 1;
Bruno Randolfa6072682008-02-18 11:21:15 +09002517
2518 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2519 if (ifsta->probe_resp) {
2520 mgmt = (struct ieee80211_mgmt *)
2521 ifsta->probe_resp->data;
2522 mgmt->frame_control =
2523 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2524 IEEE80211_STYPE_PROBE_RESP);
2525 } else {
2526 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2527 "template for IBSS\n", dev->name);
2528 }
2529
2530 if (local->ops->beacon_update &&
Johannes Berge039fa42008-05-15 12:55:29 +02002531 local->ops->beacon_update(local_to_hw(local), skb) == 0) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002532 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2533 "template\n", dev->name);
2534 skb = NULL;
2535 }
2536
2537 rates = 0;
2538 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2539 for (i = 0; i < bss->supp_rates_len; i++) {
2540 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2541 for (j = 0; j < sband->n_bitrates; j++)
2542 if (sband->bitrates[j].bitrate == bitrate)
2543 rates |= BIT(j);
2544 }
2545 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Vladimir Koutnye2839d82008-03-18 21:14:07 +01002546
2547 ieee80211_sta_def_wmm_params(dev, bss, 1);
Bruno Randolfa6072682008-02-18 11:21:15 +09002548 } while (0);
2549
2550 if (skb) {
2551 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2552 "template\n", dev->name);
2553 dev_kfree_skb(skb);
2554 }
2555
2556 ifsta->state = IEEE80211_IBSS_JOINED;
2557 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2558
Dan Williams507b06d2008-06-03 23:39:55 -04002559 memset(&wrqu, 0, sizeof(wrqu));
2560 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2561 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Bruno Randolfa6072682008-02-18 11:21:15 +09002562
2563 return res;
2564}
2565
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002566u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2567 struct ieee802_11_elems *elems,
2568 enum ieee80211_band band)
2569{
2570 struct ieee80211_supported_band *sband;
2571 struct ieee80211_rate *bitrates;
2572 size_t num_rates;
2573 u64 supp_rates;
2574 int i, j;
2575 sband = local->hw.wiphy->bands[band];
2576
2577 if (!sband) {
2578 WARN_ON(1);
2579 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2580 }
2581
2582 bitrates = sband->bitrates;
2583 num_rates = sband->n_bitrates;
2584 supp_rates = 0;
2585 for (i = 0; i < elems->supp_rates_len +
2586 elems->ext_supp_rates_len; i++) {
2587 u8 rate = 0;
2588 int own_rate;
2589 if (i < elems->supp_rates_len)
2590 rate = elems->supp_rates[i];
2591 else if (elems->ext_supp_rates)
2592 rate = elems->ext_supp_rates
2593 [i - elems->supp_rates_len];
2594 own_rate = 5 * (rate & 0x7f);
2595 for (j = 0; j < num_rates; j++)
2596 if (bitrates[j].bitrate == own_rate)
2597 supp_rates |= BIT(j);
2598 }
2599 return supp_rates;
2600}
2601
Bruno Randolfa6072682008-02-18 11:21:15 +09002602
Jiri Bencf0706e82007-05-05 11:45:53 -07002603static void ieee80211_rx_bss_info(struct net_device *dev,
2604 struct ieee80211_mgmt *mgmt,
2605 size_t len,
2606 struct ieee80211_rx_status *rx_status,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002607 struct ieee802_11_elems *elems,
Jiri Bencf0706e82007-05-05 11:45:53 -07002608 int beacon)
2609{
2610 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01002611 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002612 struct ieee80211_sta_bss *bss;
2613 struct sta_info *sta;
2614 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002615 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002616 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002617 DECLARE_MAC_BUF(mac);
2618 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002619
2620 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2621 return; /* ignore ProbeResp to foreign address */
2622
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002623 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
Jiri Bencf0706e82007-05-05 11:45:53 -07002624
Ester Kummerae6a44e2008-06-27 18:54:48 +03002625 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
2626 elems->mesh_config && mesh_matches_local(elems, dev)) {
2627 u64 rates = ieee80211_sta_get_rates(local, elems,
Johannes Berg902acc72008-02-23 15:17:19 +01002628 rx_status->band);
2629
2630 mesh_neighbour_update(mgmt->sa, rates, dev,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002631 mesh_peer_accepts_plinks(elems, dev));
Johannes Berg902acc72008-02-23 15:17:19 +01002632 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002633
Johannes Bergd0709a62008-02-25 16:27:46 +01002634 rcu_read_lock();
2635
Ester Kummerae6a44e2008-06-27 18:54:48 +03002636 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002637 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2638 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002639 u64 prev_rates;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002640 u64 supp_rates = ieee80211_sta_get_rates(local, elems,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002641 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002642
Johannes Berg8318d782008-01-24 19:38:38 +01002643 prev_rates = sta->supp_rates[rx_status->band];
2644 sta->supp_rates[rx_status->band] &= supp_rates;
2645 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002646 /* No matching rates - this should not really happen.
2647 * Make sure that at least one rate is marked
2648 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002649 sta->supp_rates[rx_status->band] =
2650 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002651 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002652 }
2653
Johannes Bergd0709a62008-02-25 16:27:46 +01002654 rcu_read_unlock();
2655
Ester Kummerae6a44e2008-06-27 18:54:48 +03002656 if (elems->ds_params && elems->ds_params_len == 1)
2657 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002658 else
Johannes Berg8318d782008-01-24 19:38:38 +01002659 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002660
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002661 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2662
2663 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2664 return;
2665
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002666#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002667 if (elems->mesh_config)
2668 bss = ieee80211_rx_mesh_bss_get(dev, elems->mesh_id,
2669 elems->mesh_id_len, elems->mesh_config, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002670 else
2671#endif
2672 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002673 elems->ssid, elems->ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002674 if (!bss) {
2675#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002676 if (elems->mesh_config)
2677 bss = ieee80211_rx_mesh_bss_add(dev, elems->mesh_id,
2678 elems->mesh_id_len, elems->mesh_config,
2679 elems->mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002680 else
2681#endif
2682 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002683 elems->ssid, elems->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002684 if (!bss)
2685 return;
2686 } else {
2687#if 0
2688 /* TODO: order by RSSI? */
2689 spin_lock_bh(&local->sta_bss_lock);
2690 list_move_tail(&bss->list, &local->sta_bss_list);
2691 spin_unlock_bh(&local->sta_bss_lock);
2692#endif
2693 }
2694
Daniel Drake56282212007-07-10 19:32:10 +02002695 /* save the ERP value so that it is available at association time */
Ester Kummerae6a44e2008-06-27 18:54:48 +03002696 if (elems->erp_info && elems->erp_info_len >= 1) {
2697 bss->erp_value = elems->erp_info[0];
Daniel Drake56282212007-07-10 19:32:10 +02002698 bss->has_erp_value = 1;
2699 }
2700
Ester Kummerae6a44e2008-06-27 18:54:48 +03002701 if (elems->ht_cap_elem &&
2702 (!bss->ht_ie || bss->ht_ie_len != elems->ht_cap_elem_len ||
2703 memcmp(bss->ht_ie, elems->ht_cap_elem, elems->ht_cap_elem_len))) {
Johannes Berg30b89b02008-04-16 17:43:20 +02002704 kfree(bss->ht_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002705 bss->ht_ie = kmalloc(elems->ht_cap_elem_len + 2, GFP_ATOMIC);
Johannes Berg30b89b02008-04-16 17:43:20 +02002706 if (bss->ht_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002707 memcpy(bss->ht_ie, elems->ht_cap_elem - 2,
2708 elems->ht_cap_elem_len + 2);
2709 bss->ht_ie_len = elems->ht_cap_elem_len + 2;
Johannes Berg30b89b02008-04-16 17:43:20 +02002710 } else
2711 bss->ht_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002712 } else if (!elems->ht_cap_elem && bss->ht_ie) {
Johannes Berg30b89b02008-04-16 17:43:20 +02002713 kfree(bss->ht_ie);
2714 bss->ht_ie = NULL;
2715 bss->ht_ie_len = 0;
2716 }
2717
Ester Kummerae6a44e2008-06-27 18:54:48 +03002718 if (elems->ht_info_elem &&
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002719 (!bss->ht_add_ie ||
Ester Kummerae6a44e2008-06-27 18:54:48 +03002720 bss->ht_add_ie_len != elems->ht_info_elem_len ||
2721 memcmp(bss->ht_add_ie, elems->ht_info_elem,
2722 elems->ht_info_elem_len))) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002723 kfree(bss->ht_add_ie);
2724 bss->ht_add_ie =
Ester Kummerae6a44e2008-06-27 18:54:48 +03002725 kmalloc(elems->ht_info_elem_len + 2, GFP_ATOMIC);
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002726 if (bss->ht_add_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002727 memcpy(bss->ht_add_ie, elems->ht_info_elem - 2,
2728 elems->ht_info_elem_len + 2);
2729 bss->ht_add_ie_len = elems->ht_info_elem_len + 2;
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002730 } else
2731 bss->ht_add_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002732 } else if (!elems->ht_info_elem && bss->ht_add_ie) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002733 kfree(bss->ht_add_ie);
2734 bss->ht_add_ie = NULL;
2735 bss->ht_add_ie_len = 0;
2736 }
2737
Jiri Bencf0706e82007-05-05 11:45:53 -07002738 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2739 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002740
2741 bss->supp_rates_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002742 if (elems->supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002743 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002744 if (clen > elems->supp_rates_len)
2745 clen = elems->supp_rates_len;
2746 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
Jiri Bencf0706e82007-05-05 11:45:53 -07002747 clen);
2748 bss->supp_rates_len += clen;
2749 }
Ester Kummerae6a44e2008-06-27 18:54:48 +03002750 if (elems->ext_supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002751 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002752 if (clen > elems->ext_supp_rates_len)
2753 clen = elems->ext_supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07002754 memcpy(&bss->supp_rates[bss->supp_rates_len],
Ester Kummerae6a44e2008-06-27 18:54:48 +03002755 elems->ext_supp_rates, clen);
Jiri Bencf0706e82007-05-05 11:45:53 -07002756 bss->supp_rates_len += clen;
2757 }
2758
Johannes Berg30b89b02008-04-16 17:43:20 +02002759 bss->band = rx_status->band;
2760
2761 bss->timestamp = beacon_timestamp;
2762 bss->last_update = jiffies;
Johannes Berg30b89b02008-04-16 17:43:20 +02002763 bss->signal = rx_status->signal;
2764 bss->noise = rx_status->noise;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002765 bss->qual = rx_status->qual;
Johannes Berg30b89b02008-04-16 17:43:20 +02002766 if (!beacon && !bss->probe_resp)
2767 bss->probe_resp = true;
2768
2769 /*
2770 * In STA mode, the remaining parameters should not be overridden
2771 * by beacons because they're not necessarily accurate there.
2772 */
2773 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2774 bss->probe_resp && beacon) {
2775 ieee80211_rx_bss_put(dev, bss);
2776 return;
2777 }
2778
Ester Kummerae6a44e2008-06-27 18:54:48 +03002779 if (elems->wpa &&
2780 (!bss->wpa_ie || bss->wpa_ie_len != elems->wpa_len ||
2781 memcmp(bss->wpa_ie, elems->wpa, elems->wpa_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002782 kfree(bss->wpa_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002783 bss->wpa_ie = kmalloc(elems->wpa_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002784 if (bss->wpa_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002785 memcpy(bss->wpa_ie, elems->wpa - 2, elems->wpa_len + 2);
2786 bss->wpa_ie_len = elems->wpa_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002787 } else
2788 bss->wpa_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002789 } else if (!elems->wpa && bss->wpa_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002790 kfree(bss->wpa_ie);
2791 bss->wpa_ie = NULL;
2792 bss->wpa_ie_len = 0;
2793 }
2794
Ester Kummerae6a44e2008-06-27 18:54:48 +03002795 if (elems->rsn &&
2796 (!bss->rsn_ie || bss->rsn_ie_len != elems->rsn_len ||
2797 memcmp(bss->rsn_ie, elems->rsn, elems->rsn_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002798 kfree(bss->rsn_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002799 bss->rsn_ie = kmalloc(elems->rsn_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002800 if (bss->rsn_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002801 memcpy(bss->rsn_ie, elems->rsn - 2, elems->rsn_len + 2);
2802 bss->rsn_ie_len = elems->rsn_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002803 } else
2804 bss->rsn_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002805 } else if (!elems->rsn && bss->rsn_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002806 kfree(bss->rsn_ie);
2807 bss->rsn_ie = NULL;
2808 bss->rsn_ie_len = 0;
2809 }
2810
Johannes Berg30b89b02008-04-16 17:43:20 +02002811 /*
2812 * Cf.
2813 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2814 *
2815 * quoting:
2816 *
2817 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2818 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2819 * Alliance (September 1, 2004) is incorporated by reference herein.
2820 * The inclusion of the WMM Parameters in probe responses and
2821 * association responses is mandatory for WMM enabled networks. The
2822 * inclusion of the WMM Parameters in beacons, however, is optional.
2823 */
2824
Ester Kummerae6a44e2008-06-27 18:54:48 +03002825 if (elems->wmm_param &&
2826 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_param_len ||
2827 memcmp(bss->wmm_ie, elems->wmm_param, elems->wmm_param_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002828 kfree(bss->wmm_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002829 bss->wmm_ie = kmalloc(elems->wmm_param_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002830 if (bss->wmm_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002831 memcpy(bss->wmm_ie, elems->wmm_param - 2,
2832 elems->wmm_param_len + 2);
2833 bss->wmm_ie_len = elems->wmm_param_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002834 } else
2835 bss->wmm_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002836 } else if (elems->wmm_info &&
2837 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_info_len ||
2838 memcmp(bss->wmm_ie, elems->wmm_info,
2839 elems->wmm_info_len))) {
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002840 /* As for certain AP's Fifth bit is not set in WMM IE in
2841 * beacon frames.So while parsing the beacon frame the
2842 * wmm_info structure is used instead of wmm_param.
2843 * wmm_info structure was never used to set bss->wmm_ie.
2844 * This code fixes this problem by copying the WME
2845 * information from wmm_info to bss->wmm_ie and enabling
2846 * n-band association.
2847 */
2848 kfree(bss->wmm_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002849 bss->wmm_ie = kmalloc(elems->wmm_info_len + 2, GFP_ATOMIC);
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002850 if (bss->wmm_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002851 memcpy(bss->wmm_ie, elems->wmm_info - 2,
2852 elems->wmm_info_len + 2);
2853 bss->wmm_ie_len = elems->wmm_info_len + 2;
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002854 } else
2855 bss->wmm_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002856 } else if (!elems->wmm_param && !elems->wmm_info && bss->wmm_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002857 kfree(bss->wmm_ie);
2858 bss->wmm_ie = NULL;
2859 bss->wmm_ie_len = 0;
2860 }
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002861
2862 /* check if we need to merge IBSS */
2863 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2864 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002865 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002866 bss->freq == local->oper_channel->center_freq &&
Ester Kummerae6a44e2008-06-27 18:54:48 +03002867 elems->ssid_len == sdata->u.sta.ssid_len &&
2868 memcmp(elems->ssid, sdata->u.sta.ssid,
2869 sdata->u.sta.ssid_len) == 0) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002870 if (rx_status->flag & RX_FLAG_TSFT) {
2871 /* in order for correct IBSS merging we need mactime
2872 *
2873 * since mactime is defined as the time the first data
2874 * symbol of the frame hits the PHY, and the timestamp
2875 * of the beacon is defined as "the time that the data
2876 * symbol containing the first bit of the timestamp is
2877 * transmitted to the PHY plus the transmitting STA’s
2878 * delays through its local PHY from the MAC-PHY
2879 * interface to its interface with the WM"
2880 * (802.11 11.1.2) - equals the time this bit arrives at
2881 * the receiver - we have to take into account the
2882 * offset between the two.
2883 * e.g: at 1 MBit that means mactime is 192 usec earlier
2884 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2885 */
2886 int rate = local->hw.wiphy->bands[rx_status->band]->
2887 bitrates[rx_status->rate_idx].bitrate;
2888 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2889 } else if (local && local->ops && local->ops->get_tsf)
2890 /* second best option: get current TSF */
2891 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2892 else
2893 /* can't merge without knowing the TSF */
2894 rx_timestamp = -1LLU;
2895#ifdef CONFIG_MAC80211_IBSS_DEBUG
2896 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2897 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2898 print_mac(mac, mgmt->sa),
2899 print_mac(mac2, mgmt->bssid),
2900 (unsigned long long)rx_timestamp,
2901 (unsigned long long)beacon_timestamp,
2902 (unsigned long long)(rx_timestamp - beacon_timestamp),
2903 jiffies);
2904#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2905 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002906#ifndef CONFIG_MAC80211_IBSS_DEBUG
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002907 printk(KERN_DEBUG "%s: beacon TSF higher than "
2908 "local TSF - IBSS merge with BSSID %s\n",
2909 dev->name, print_mac(mac, mgmt->bssid));
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002910#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002911 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2912 ieee80211_ibss_add_sta(dev, NULL,
Vladimir Koutny87291c02008-06-13 16:50:44 +02002913 mgmt->bssid, mgmt->sa,
2914 BIT(rx_status->rate_idx));
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002915 }
2916 }
2917
Jiri Bencf0706e82007-05-05 11:45:53 -07002918 ieee80211_rx_bss_put(dev, bss);
2919}
2920
2921
2922static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2923 struct ieee80211_mgmt *mgmt,
2924 size_t len,
2925 struct ieee80211_rx_status *rx_status)
2926{
Ester Kummerae6a44e2008-06-27 18:54:48 +03002927 size_t baselen;
2928 struct ieee802_11_elems elems;
2929
2930 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2931 if (baselen > len)
2932 return;
2933
2934 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2935 &elems);
2936
2937 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002938}
2939
2940
2941static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2942 struct ieee80211_mgmt *mgmt,
2943 size_t len,
2944 struct ieee80211_rx_status *rx_status)
2945{
Jiri Bencf0706e82007-05-05 11:45:53 -07002946 struct ieee80211_sub_if_data *sdata;
2947 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002948 size_t baselen;
2949 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002950 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2951 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002952 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002953
Ester Kummerae6a44e2008-06-27 18:54:48 +03002954 /* Process beacon from the current BSS */
2955 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2956 if (baselen > len)
2957 return;
2958
2959 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2960
2961 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002962
2963 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002964 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002965 return;
2966 ifsta = &sdata->u.sta;
2967
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002968 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002969 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2970 return;
2971
Johannes Berg3434fbd2008-05-03 00:59:37 +02002972 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2973 elems.wmm_param_len);
Reinette Chatred18ef292008-04-09 16:56:15 -07002974
2975 /* Do not send changes to driver if we are scanning. This removes
2976 * requirement that driver's bss_info_changed function needs to be
2977 * atomic. */
2978 if (local->sta_sw_scanning || local->sta_hw_scanning)
2979 return;
2980
Daniel Drake56282212007-07-10 19:32:10 +02002981 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002982 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
John W. Linville50c4afb2008-04-15 14:09:27 -04002983 else {
2984 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2985 changed |= ieee80211_handle_protect_preamb(sdata, false,
2986 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2987 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002988
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002989 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07002990 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002991 struct ieee80211_ht_bss_info bss_info;
2992
2993 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2994 (struct ieee80211_ht_addt_info *)
2995 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002996 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2997 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002998 }
2999
Johannes Berg471b3ef2007-12-28 14:32:58 +01003000 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003001}
3002
3003
3004static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
3005 struct ieee80211_if_sta *ifsta,
3006 struct ieee80211_mgmt *mgmt,
3007 size_t len,
3008 struct ieee80211_rx_status *rx_status)
3009{
3010 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3011 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3012 int tx_last_beacon;
3013 struct sk_buff *skb;
3014 struct ieee80211_mgmt *resp;
3015 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07003016 DECLARE_MAC_BUF(mac);
3017#ifdef CONFIG_MAC80211_IBSS_DEBUG
3018 DECLARE_MAC_BUF(mac2);
3019 DECLARE_MAC_BUF(mac3);
3020#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003021
Johannes Berg51fb61e2007-12-19 01:31:27 +01003022 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003023 ifsta->state != IEEE80211_IBSS_JOINED ||
3024 len < 24 + 2 || !ifsta->probe_resp)
3025 return;
3026
3027 if (local->ops->tx_last_beacon)
3028 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
3029 else
3030 tx_last_beacon = 1;
3031
3032#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003033 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
3034 "%s (tx_last_beacon=%d)\n",
3035 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
3036 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07003037#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3038
3039 if (!tx_last_beacon)
3040 return;
3041
3042 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
3043 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
3044 return;
3045
3046 end = ((u8 *) mgmt) + len;
3047 pos = mgmt->u.probe_req.variable;
3048 if (pos[0] != WLAN_EID_SSID ||
3049 pos + 2 + pos[1] > end) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003050#ifdef CONFIG_MAC80211_IBSS_DEBUG
3051 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
3052 "from %s\n",
3053 dev->name, print_mac(mac, mgmt->sa));
3054#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003055 return;
3056 }
3057 if (pos[1] != 0 &&
3058 (pos[1] != ifsta->ssid_len ||
3059 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
3060 /* Ignore ProbeReq for foreign SSID */
3061 return;
3062 }
3063
3064 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02003065 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003066 if (!skb)
3067 return;
3068
3069 resp = (struct ieee80211_mgmt *) skb->data;
3070 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3071#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003072 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
3073 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07003074#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3075 ieee80211_sta_tx(dev, skb, 0);
3076}
3077
Johannes Berg4e20cb22007-12-19 01:31:24 +01003078static void ieee80211_rx_mgmt_action(struct net_device *dev,
3079 struct ieee80211_if_sta *ifsta,
3080 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003081 size_t len,
3082 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003083{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003084 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Assaf Kraussb6623482008-06-16 16:09:49 +03003085 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003086
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003087 if (len < IEEE80211_MIN_ACTION_SIZE)
3088 return;
3089
3090 switch (mgmt->u.action.category) {
Assaf Kraussb6623482008-06-16 16:09:49 +03003091 case WLAN_CATEGORY_SPECTRUM_MGMT:
3092 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
3093 break;
3094 switch (mgmt->u.action.u.chan_switch.action_code) {
3095 case WLAN_ACTION_SPCT_MSR_REQ:
3096 if (len < (IEEE80211_MIN_ACTION_SIZE +
3097 sizeof(mgmt->u.action.u.measurement)))
3098 break;
3099 ieee80211_sta_process_measurement_req(dev, mgmt, len);
3100 break;
3101 }
3102 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003103 case WLAN_CATEGORY_BACK:
3104 switch (mgmt->u.action.u.addba_req.action_code) {
3105 case WLAN_ACTION_ADDBA_REQ:
3106 if (len < (IEEE80211_MIN_ACTION_SIZE +
3107 sizeof(mgmt->u.action.u.addba_req)))
3108 break;
3109 ieee80211_sta_process_addba_request(dev, mgmt, len);
3110 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02003111 case WLAN_ACTION_ADDBA_RESP:
3112 if (len < (IEEE80211_MIN_ACTION_SIZE +
3113 sizeof(mgmt->u.action.u.addba_resp)))
3114 break;
3115 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3116 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02003117 case WLAN_ACTION_DELBA:
3118 if (len < (IEEE80211_MIN_ACTION_SIZE +
3119 sizeof(mgmt->u.action.u.delba)))
3120 break;
3121 ieee80211_sta_process_delba(dev, mgmt, len);
3122 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003123 }
3124 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003125 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003126 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003127 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3128 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003129 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003130 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003131 mesh_rx_path_sel_frame(dev, mgmt, len);
3132 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003133 }
3134}
Jiri Bencf0706e82007-05-05 11:45:53 -07003135
3136void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3137 struct ieee80211_rx_status *rx_status)
3138{
3139 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3140 struct ieee80211_sub_if_data *sdata;
3141 struct ieee80211_if_sta *ifsta;
3142 struct ieee80211_mgmt *mgmt;
3143 u16 fc;
3144
3145 if (skb->len < 24)
3146 goto fail;
3147
3148 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3149 ifsta = &sdata->u.sta;
3150
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 case IEEE80211_STYPE_PROBE_RESP:
3157 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003158 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07003159 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3160 case IEEE80211_STYPE_AUTH:
3161 case IEEE80211_STYPE_ASSOC_RESP:
3162 case IEEE80211_STYPE_REASSOC_RESP:
3163 case IEEE80211_STYPE_DEAUTH:
3164 case IEEE80211_STYPE_DISASSOC:
3165 skb_queue_tail(&ifsta->skb_queue, skb);
3166 queue_work(local->hw.workqueue, &ifsta->work);
3167 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003168 }
3169
3170 fail:
3171 kfree_skb(skb);
3172}
3173
3174
3175static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3176 struct sk_buff *skb)
3177{
3178 struct ieee80211_rx_status *rx_status;
3179 struct ieee80211_sub_if_data *sdata;
3180 struct ieee80211_if_sta *ifsta;
3181 struct ieee80211_mgmt *mgmt;
3182 u16 fc;
3183
3184 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3185 ifsta = &sdata->u.sta;
3186
3187 rx_status = (struct ieee80211_rx_status *) skb->cb;
3188 mgmt = (struct ieee80211_mgmt *) skb->data;
3189 fc = le16_to_cpu(mgmt->frame_control);
3190
3191 switch (fc & IEEE80211_FCTL_STYPE) {
3192 case IEEE80211_STYPE_PROBE_REQ:
3193 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3194 rx_status);
3195 break;
3196 case IEEE80211_STYPE_PROBE_RESP:
3197 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3198 break;
3199 case IEEE80211_STYPE_BEACON:
3200 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3201 break;
3202 case IEEE80211_STYPE_AUTH:
3203 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3204 break;
3205 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003206 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003207 break;
3208 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003209 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003210 break;
3211 case IEEE80211_STYPE_DEAUTH:
3212 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3213 break;
3214 case IEEE80211_STYPE_DISASSOC:
3215 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3216 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003217 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003218 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003219 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07003220 }
3221
3222 kfree_skb(skb);
3223}
3224
3225
Johannes Berg9ae54c82008-01-31 19:48:20 +01003226ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003227ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3228 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003229{
3230 struct ieee80211_mgmt *mgmt;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003231 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003232
Zhu Yiece8edd2007-11-22 10:53:21 +08003233 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003234 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003235
3236 mgmt = (struct ieee80211_mgmt *) skb->data;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003237 fc = mgmt->frame_control;
Jiri Bencf0706e82007-05-05 11:45:53 -07003238
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003239 if (ieee80211_is_ctl(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +01003240 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003241
3242 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003243 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003244
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003245 if (ieee80211_is_probe_resp(fc)) {
3246 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3247 dev_kfree_skb(skb);
3248 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003249 }
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003250
3251 if (ieee80211_is_beacon(fc)) {
3252 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3253 dev_kfree_skb(skb);
3254 return RX_QUEUED;
3255 }
3256
Johannes Berg9ae54c82008-01-31 19:48:20 +01003257 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003258}
3259
3260
3261static int ieee80211_sta_active_ibss(struct net_device *dev)
3262{
3263 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3264 int active = 0;
3265 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003266 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003267
Johannes Bergd0709a62008-02-25 16:27:46 +01003268 rcu_read_lock();
3269
3270 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3271 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003272 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3273 jiffies)) {
3274 active++;
3275 break;
3276 }
3277 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003278
3279 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003280
3281 return active;
3282}
3283
3284
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003285static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003286{
3287 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3288 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003289 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003290 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003291 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003292
Johannes Bergd0709a62008-02-25 16:27:46 +01003293 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003294 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003295 if (time_after(jiffies, sta->last_rx + exp_time)) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003296#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003297 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3298 dev->name, print_mac(mac, sta->addr));
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003299#endif
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003300 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003301 if (sta)
3302 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003303 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003304 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003305
Johannes Bergd0709a62008-02-25 16:27:46 +01003306 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3307 sta_info_destroy(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003308}
3309
3310
3311static void ieee80211_sta_merge_ibss(struct net_device *dev,
3312 struct ieee80211_if_sta *ifsta)
3313{
3314 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3315
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003316 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07003317 if (ieee80211_sta_active_ibss(dev))
3318 return;
3319
3320 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3321 "IBSS networks with same SSID (merge)\n", dev->name);
3322 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3323}
3324
3325
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003326#ifdef CONFIG_MAC80211_MESH
3327static void ieee80211_mesh_housekeeping(struct net_device *dev,
3328 struct ieee80211_if_sta *ifsta)
3329{
3330 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3331 bool free_plinks;
3332
3333 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3334 mesh_path_expire(dev);
3335
3336 free_plinks = mesh_plink_availables(sdata);
3337 if (free_plinks != sdata->u.sta.accepting_plinks)
3338 ieee80211_if_config_beacon(dev);
3339
3340 mod_timer(&ifsta->timer, jiffies +
3341 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3342}
3343
3344
3345void ieee80211_start_mesh(struct net_device *dev)
3346{
3347 struct ieee80211_if_sta *ifsta;
3348 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3349 ifsta = &sdata->u.sta;
3350 ifsta->state = IEEE80211_MESH_UP;
3351 ieee80211_sta_timer((unsigned long)sdata);
3352}
3353#endif
3354
3355
Jiri Bencf0706e82007-05-05 11:45:53 -07003356void ieee80211_sta_timer(unsigned long data)
3357{
3358 struct ieee80211_sub_if_data *sdata =
3359 (struct ieee80211_sub_if_data *) data;
3360 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3361 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3362
3363 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3364 queue_work(local->hw.workqueue, &ifsta->work);
3365}
3366
Jiri Bencf0706e82007-05-05 11:45:53 -07003367void ieee80211_sta_work(struct work_struct *work)
3368{
3369 struct ieee80211_sub_if_data *sdata =
3370 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3371 struct net_device *dev = sdata->dev;
3372 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3373 struct ieee80211_if_sta *ifsta;
3374 struct sk_buff *skb;
3375
3376 if (!netif_running(dev))
3377 return;
3378
Zhu Yiece8edd2007-11-22 10:53:21 +08003379 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003380 return;
3381
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003382 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3383 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3384 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
Jiri Bencf0706e82007-05-05 11:45:53 -07003385 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003386 ifsta = &sdata->u.sta;
3387
3388 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3389 ieee80211_sta_rx_queued_mgmt(dev, skb);
3390
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003391#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003392 if (ifsta->preq_queue_len &&
3393 time_after(jiffies,
3394 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003395 mesh_path_start_discovery(dev);
3396#endif
3397
Jiri Bencf0706e82007-05-05 11:45:53 -07003398 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3399 ifsta->state != IEEE80211_ASSOCIATE &&
3400 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003401 if (ifsta->scan_ssid_len)
3402 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3403 else
3404 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003405 return;
3406 }
3407
3408 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3409 if (ieee80211_sta_config_auth(dev, ifsta))
3410 return;
3411 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3412 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3413 return;
3414
3415 switch (ifsta->state) {
3416 case IEEE80211_DISABLED:
3417 break;
3418 case IEEE80211_AUTHENTICATE:
3419 ieee80211_authenticate(dev, ifsta);
3420 break;
3421 case IEEE80211_ASSOCIATE:
3422 ieee80211_associate(dev, ifsta);
3423 break;
3424 case IEEE80211_ASSOCIATED:
3425 ieee80211_associated(dev, ifsta);
3426 break;
3427 case IEEE80211_IBSS_SEARCH:
3428 ieee80211_sta_find_ibss(dev, ifsta);
3429 break;
3430 case IEEE80211_IBSS_JOINED:
3431 ieee80211_sta_merge_ibss(dev, ifsta);
3432 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003433#ifdef CONFIG_MAC80211_MESH
3434 case IEEE80211_MESH_UP:
3435 ieee80211_mesh_housekeeping(dev, ifsta);
3436 break;
3437#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003438 default:
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003439 WARN_ON(1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003440 break;
3441 }
3442
3443 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3444 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3445 "mixed-cell disabled - disassociate\n", dev->name);
3446
3447 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3448 ieee80211_set_disassoc(dev, ifsta, 0);
3449 }
3450}
3451
3452
3453static void ieee80211_sta_reset_auth(struct net_device *dev,
3454 struct ieee80211_if_sta *ifsta)
3455{
3456 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3457
3458 if (local->ops->reset_tsf) {
3459 /* Reset own TSF to allow time synchronization work. */
3460 local->ops->reset_tsf(local_to_hw(local));
3461 }
3462
3463 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3464
3465
3466 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3467 ifsta->auth_alg = WLAN_AUTH_OPEN;
3468 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3469 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3470 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3471 ifsta->auth_alg = WLAN_AUTH_LEAP;
3472 else
3473 ifsta->auth_alg = WLAN_AUTH_OPEN;
Jiri Bencf0706e82007-05-05 11:45:53 -07003474 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003475 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3476 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003477 netif_carrier_off(dev);
3478}
3479
3480
3481void ieee80211_sta_req_auth(struct net_device *dev,
3482 struct ieee80211_if_sta *ifsta)
3483{
3484 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3485 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3486
Johannes Berg51fb61e2007-12-19 01:31:27 +01003487 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003488 return;
3489
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003490 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3491 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3492 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3493 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003494 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3495 queue_work(local->hw.workqueue, &ifsta->work);
3496 }
3497}
3498
3499static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3500 const char *ssid, int ssid_len)
3501{
3502 int tmp, hidden_ssid;
3503
Michael Wu48225702007-10-19 17:14:36 -04003504 if (ssid_len == ifsta->ssid_len &&
3505 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003506 return 1;
3507
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003508 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003509 return 0;
3510
3511 hidden_ssid = 1;
3512 tmp = ssid_len;
3513 while (tmp--) {
3514 if (ssid[tmp] != '\0') {
3515 hidden_ssid = 0;
3516 break;
3517 }
3518 }
3519
3520 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3521 return 1;
3522
3523 if (ssid_len == 1 && ssid[0] == ' ')
3524 return 1;
3525
3526 return 0;
3527}
3528
3529static int ieee80211_sta_config_auth(struct net_device *dev,
3530 struct ieee80211_if_sta *ifsta)
3531{
3532 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3533 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3534 struct ieee80211_sta_bss *bss, *selected = NULL;
3535 int top_rssi = 0, freq;
3536
Jiri Bencf0706e82007-05-05 11:45:53 -07003537 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003538 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003539 list_for_each_entry(bss, &local->sta_bss_list, list) {
3540 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3541 continue;
3542
Abhijeet Kolekar34a961f2008-05-09 09:35:41 -07003543 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3544 IEEE80211_STA_AUTO_BSSID_SEL |
3545 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3546 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3547 !!sdata->default_key))
Jiri Bencf0706e82007-05-05 11:45:53 -07003548 continue;
3549
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003550 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3551 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003552 continue;
3553
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003554 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003555 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3556 continue;
3557
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003558 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003559 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3560 continue;
3561
Bruno Randolf566bfe52008-05-08 19:15:40 +02003562 if (!selected || top_rssi < bss->signal) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003563 selected = bss;
Bruno Randolf566bfe52008-05-08 19:15:40 +02003564 top_rssi = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07003565 }
3566 }
3567 if (selected)
3568 atomic_inc(&selected->users);
3569 spin_unlock_bh(&local->sta_bss_lock);
3570
3571 if (selected) {
Assaf Kraussbe038b32008-06-05 19:55:21 +03003572 ieee80211_set_freq(dev, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003573 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003574 ieee80211_sta_set_ssid(dev, selected->ssid,
3575 selected->ssid_len);
3576 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003577 ieee80211_sta_def_wmm_params(dev, selected, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003578 ieee80211_rx_bss_put(dev, selected);
3579 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003580 ieee80211_sta_reset_auth(dev, ifsta);
3581 return 0;
3582 } else {
3583 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003584 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003585 ieee80211_sta_start_scan(dev, NULL, 0);
3586 else
3587 ieee80211_sta_start_scan(dev, ifsta->ssid,
3588 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003589 ifsta->state = IEEE80211_AUTHENTICATE;
3590 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3591 } else
3592 ifsta->state = IEEE80211_DISABLED;
3593 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003594 return -1;
3595}
3596
Jiri Bencf0706e82007-05-05 11:45:53 -07003597
3598static int ieee80211_sta_create_ibss(struct net_device *dev,
3599 struct ieee80211_if_sta *ifsta)
3600{
3601 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3602 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003603 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003604 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003605 u8 bssid[ETH_ALEN], *pos;
3606 int i;
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003607 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003608 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003609
3610#if 0
3611 /* Easier testing, use fixed BSSID. */
3612 memset(bssid, 0xfe, ETH_ALEN);
3613#else
3614 /* Generate random, not broadcast, locally administered BSSID. Mix in
3615 * own MAC address to make sure that devices that do not have proper
3616 * random number generator get different BSSID. */
3617 get_random_bytes(bssid, ETH_ALEN);
3618 for (i = 0; i < ETH_ALEN; i++)
3619 bssid[i] ^= dev->dev_addr[i];
3620 bssid[0] &= ~0x01;
3621 bssid[0] |= 0x02;
3622#endif
3623
Joe Perches0795af52007-10-03 17:59:30 -07003624 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3625 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003626
Johannes Berg8318d782008-01-24 19:38:38 +01003627 bss = ieee80211_rx_bss_add(dev, bssid,
3628 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003629 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003630 if (!bss)
3631 return -ENOMEM;
3632
Johannes Berg8318d782008-01-24 19:38:38 +01003633 bss->band = local->hw.conf.channel->band;
3634 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003635
3636 if (local->hw.conf.beacon_int == 0)
Tomas Winklerdc0ae302008-06-12 22:38:37 +03003637 local->hw.conf.beacon_int = 100;
Jiri Bencf0706e82007-05-05 11:45:53 -07003638 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003639 bss->last_update = jiffies;
3640 bss->capability = WLAN_CAPABILITY_IBSS;
Johannes Berg988c0f72008-04-17 19:21:22 +02003641
3642 if (sdata->default_key)
Jiri Bencf0706e82007-05-05 11:45:53 -07003643 bss->capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +02003644 else
Jiri Bencf0706e82007-05-05 11:45:53 -07003645 sdata->drop_unencrypted = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02003646
Johannes Berg8318d782008-01-24 19:38:38 +01003647 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003648 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003649 for (i = 0; i < sband->n_bitrates; i++) {
3650 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003651 *pos++ = (u8) (rate / 5);
3652 }
3653
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003654 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3655 ieee80211_rx_bss_put(dev, bss);
3656 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003657}
3658
3659
3660static int ieee80211_sta_find_ibss(struct net_device *dev,
3661 struct ieee80211_if_sta *ifsta)
3662{
3663 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3664 struct ieee80211_sta_bss *bss;
3665 int found = 0;
3666 u8 bssid[ETH_ALEN];
3667 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003668 DECLARE_MAC_BUF(mac);
3669 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003670
3671 if (ifsta->ssid_len == 0)
3672 return -EINVAL;
3673
3674 active_ibss = ieee80211_sta_active_ibss(dev);
3675#ifdef CONFIG_MAC80211_IBSS_DEBUG
3676 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3677 dev->name, active_ibss);
3678#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3679 spin_lock_bh(&local->sta_bss_lock);
3680 list_for_each_entry(bss, &local->sta_bss_list, list) {
3681 if (ifsta->ssid_len != bss->ssid_len ||
3682 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3683 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3684 continue;
3685#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003686 printk(KERN_DEBUG " bssid=%s found\n",
3687 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003688#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3689 memcpy(bssid, bss->bssid, ETH_ALEN);
3690 found = 1;
3691 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3692 break;
3693 }
3694 spin_unlock_bh(&local->sta_bss_lock);
3695
3696#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003697 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3698 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003699#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3700 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003701 (bss = ieee80211_rx_bss_get(dev, bssid,
3702 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003703 ifsta->ssid, ifsta->ssid_len))) {
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003704 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003705 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003706 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003707 dev->name, print_mac(mac, bssid));
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003708 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3709 ieee80211_rx_bss_put(dev, bss);
3710 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003711 }
3712#ifdef CONFIG_MAC80211_IBSS_DEBUG
3713 printk(KERN_DEBUG " did not try to join ibss\n");
3714#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3715
3716 /* Selected IBSS not found in current scan results - try to scan */
3717 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3718 !ieee80211_sta_active_ibss(dev)) {
3719 mod_timer(&ifsta->timer, jiffies +
3720 IEEE80211_IBSS_MERGE_INTERVAL);
3721 } else if (time_after(jiffies, local->last_scan_completed +
3722 IEEE80211_SCAN_INTERVAL)) {
3723 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3724 "join\n", dev->name);
3725 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3726 ifsta->ssid_len);
3727 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3728 int interval = IEEE80211_SCAN_INTERVAL;
3729
3730 if (time_after(jiffies, ifsta->ibss_join_req +
3731 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003732 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003733 (!(local->oper_channel->flags &
3734 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003735 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003736 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003737 printk(KERN_DEBUG "%s: IBSS not allowed on"
3738 " %d MHz\n", dev->name,
3739 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003740 }
3741
3742 /* No IBSS found - decrease scan interval and continue
3743 * scanning. */
3744 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3745 }
3746
3747 ifsta->state = IEEE80211_IBSS_SEARCH;
3748 mod_timer(&ifsta->timer, jiffies + interval);
3749 return 0;
3750 }
3751
3752 return 0;
3753}
3754
3755
3756int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3757{
Johannes Berg8318d782008-01-24 19:38:38 +01003758 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003759 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07003760
3761 if (len > IEEE80211_MAX_SSID_LEN)
3762 return -EINVAL;
3763
Jiri Bencf0706e82007-05-05 11:45:53 -07003764 ifsta = &sdata->u.sta;
3765
3766 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003767 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003768 memcpy(ifsta->ssid, ssid, len);
3769 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3770 ifsta->ssid_len = len;
3771
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003772 if (len)
3773 ifsta->flags |= IEEE80211_STA_SSID_SET;
3774 else
3775 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003776 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003777 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003778 ifsta->ibss_join_req = jiffies;
3779 ifsta->state = IEEE80211_IBSS_SEARCH;
3780 return ieee80211_sta_find_ibss(dev, ifsta);
3781 }
3782 return 0;
3783}
3784
3785
3786int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3787{
3788 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3789 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3790 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3791 *len = ifsta->ssid_len;
3792 return 0;
3793}
3794
3795
3796int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3797{
3798 struct ieee80211_sub_if_data *sdata;
3799 struct ieee80211_if_sta *ifsta;
3800 int res;
3801
3802 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3803 ifsta = &sdata->u.sta;
3804
3805 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3806 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3807 res = ieee80211_if_config(dev);
3808 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 Bencf0706e82007-05-05 11:45:53 -07003817 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003818 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3819
Jiri Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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 Bencf0706e82007-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);
3894 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3895 local->ops->configure_filter(local_to_hw(local),
3896 FIF_BCN_PRBRESP_PROMISC,
3897 &local->filter_flags,
3898 local->mdev->mc_count,
3899 local->mdev->mc_list);
3900
3901 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003902
Johannes Berg79010422007-09-18 17:29:21 -04003903 rcu_read_lock();
3904 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003905
3906 /* No need to wake the master device. */
3907 if (sdata->dev == local->mdev)
3908 continue;
3909
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003910 /* Tell AP we're back */
3911 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3912 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3913 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003914
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003915 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003916
Jiri Bencf0706e82007-05-05 11:45:53 -07003917 netif_wake_queue(sdata->dev);
3918 }
Johannes Berg79010422007-09-18 17:29:21 -04003919 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003920
Zhu Yiece8edd2007-11-22 10:53:21 +08003921done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003922 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003923 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003924 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003925 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003926 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3927 !ieee80211_sta_active_ibss(dev)))
3928 ieee80211_sta_find_ibss(dev, ifsta);
3929 }
3930}
3931EXPORT_SYMBOL(ieee80211_scan_completed);
3932
3933void ieee80211_sta_scan_work(struct work_struct *work)
3934{
3935 struct ieee80211_local *local =
3936 container_of(work, struct ieee80211_local, scan_work.work);
3937 struct net_device *dev = local->scan_dev;
3938 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003939 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003940 struct ieee80211_channel *chan;
3941 int skip;
3942 unsigned long next_delay = 0;
3943
Zhu Yiece8edd2007-11-22 10:53:21 +08003944 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003945 return;
3946
3947 switch (local->scan_state) {
3948 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003949 /*
3950 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3951 * after we successfully scanned the last channel of the last
3952 * band (and the last band is supported by the hw)
3953 */
Johannes Berg8318d782008-01-24 19:38:38 +01003954 if (local->scan_band < IEEE80211_NUM_BANDS)
3955 sband = local->hw.wiphy->bands[local->scan_band];
3956 else
3957 sband = NULL;
3958
Johannes Berg69d464d2008-01-28 17:08:56 +01003959 /*
3960 * If we are at an unsupported band and have more bands
3961 * left to scan, advance to the next supported one.
3962 */
3963 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003964 local->scan_band++;
3965 sband = local->hw.wiphy->bands[local->scan_band];
3966 local->scan_channel_idx = 0;
3967 }
3968
Johannes Berg69d464d2008-01-28 17:08:56 +01003969 /* if no more bands/channels left, complete scan */
3970 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003971 ieee80211_scan_completed(local_to_hw(local));
3972 return;
3973 }
Johannes Berg8318d782008-01-24 19:38:38 +01003974 skip = 0;
3975 chan = &sband->channels[local->scan_channel_idx];
3976
3977 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003978 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003979 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003980 skip = 1;
3981
3982 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003983 local->scan_channel = chan;
3984 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003985 printk(KERN_DEBUG "%s: failed to set freq to "
3986 "%d MHz for scan\n", dev->name,
3987 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003988 skip = 1;
3989 }
3990 }
3991
Johannes Berg69d464d2008-01-28 17:08:56 +01003992 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003993 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003994 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003995 /*
3996 * scan_band may end up == IEEE80211_NUM_BANDS, but
3997 * we'll catch that case above and complete the scan
3998 * if that is the case.
3999 */
Johannes Berg8318d782008-01-24 19:38:38 +01004000 local->scan_band++;
4001 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07004002 }
4003
4004 if (skip)
4005 break;
4006
4007 next_delay = IEEE80211_PROBE_DELAY +
4008 usecs_to_jiffies(local->hw.channel_change_time);
4009 local->scan_state = SCAN_SEND_PROBE;
4010 break;
4011 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01004012 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07004013 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01004014
4015 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
4016 break;
4017 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
4018 local->scan_ssid_len);
4019 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07004020 break;
4021 }
4022
Zhu Yiece8edd2007-11-22 10:53:21 +08004023 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07004024 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4025 next_delay);
4026}
4027
4028
4029static int ieee80211_sta_start_scan(struct net_device *dev,
4030 u8 *ssid, size_t ssid_len)
4031{
4032 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4033 struct ieee80211_sub_if_data *sdata;
4034
4035 if (ssid_len > IEEE80211_MAX_SSID_LEN)
4036 return -EINVAL;
4037
4038 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
4039 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
4040 * BSSID: MACAddress
4041 * SSID
4042 * ScanType: ACTIVE, PASSIVE
4043 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
4044 * a Probe frame during active scanning
4045 * ChannelList
4046 * MinChannelTime (>= ProbeDelay), in TU
4047 * MaxChannelTime: (>= MinChannelTime), in TU
4048 */
4049
4050 /* MLME-SCAN.confirm
4051 * BSSDescriptionSet
4052 * ResultCode: SUCCESS, INVALID_PARAMETERS
4053 */
4054
Zhu Yiece8edd2007-11-22 10:53:21 +08004055 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004056 if (local->scan_dev == dev)
4057 return 0;
4058 return -EBUSY;
4059 }
4060
4061 if (local->ops->hw_scan) {
4062 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08004063 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07004064 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08004065 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07004066 local->scan_dev = dev;
4067 }
4068 return rc;
4069 }
4070
Zhu Yiece8edd2007-11-22 10:53:21 +08004071 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07004072
Johannes Berg79010422007-09-18 17:29:21 -04004073 rcu_read_lock();
4074 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02004075
4076 /* Don't stop the master interface, otherwise we can't transmit
4077 * probes! */
4078 if (sdata->dev == local->mdev)
4079 continue;
4080
Jiri Bencf0706e82007-05-05 11:45:53 -07004081 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01004082 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004083 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004084 ieee80211_send_nullfunc(local, sdata, 1);
4085 }
Johannes Berg79010422007-09-18 17:29:21 -04004086 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07004087
4088 if (ssid) {
4089 local->scan_ssid_len = ssid_len;
4090 memcpy(local->scan_ssid, ssid, ssid_len);
4091 } else
4092 local->scan_ssid_len = 0;
4093 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07004094 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004095 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07004096 local->scan_dev = dev;
4097
Johannes Berg4150c572007-09-17 01:29:23 -04004098 netif_tx_lock_bh(local->mdev);
4099 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4100 local->ops->configure_filter(local_to_hw(local),
4101 FIF_BCN_PRBRESP_PROMISC,
4102 &local->filter_flags,
4103 local->mdev->mc_count,
4104 local->mdev->mc_list);
4105 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07004106
4107 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4108 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4109 IEEE80211_CHANNEL_TIME);
4110
4111 return 0;
4112}
4113
4114
4115int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4116{
4117 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4118 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4119 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4120
Johannes Berg51fb61e2007-12-19 01:31:27 +01004121 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004122 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4123
Zhu Yiece8edd2007-11-22 10:53:21 +08004124 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004125 if (local->scan_dev == dev)
4126 return 0;
4127 return -EBUSY;
4128 }
4129
Helmut Schaaa0af5f12007-11-09 16:25:08 +01004130 ifsta->scan_ssid_len = ssid_len;
4131 if (ssid_len)
4132 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07004133 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4134 queue_work(local->hw.workqueue, &ifsta->work);
4135 return 0;
4136}
4137
4138static char *
4139ieee80211_sta_scan_result(struct net_device *dev,
David S. Millerccc58052008-06-16 18:50:49 -07004140 struct iw_request_info *info,
Jiri Bencf0706e82007-05-05 11:45:53 -07004141 struct ieee80211_sta_bss *bss,
4142 char *current_ev, char *end_buf)
4143{
4144 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4145 struct iw_event iwe;
4146
4147 if (time_after(jiffies,
4148 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4149 return current_ev;
4150
Jiri Bencf0706e82007-05-05 11:45:53 -07004151 memset(&iwe, 0, sizeof(iwe));
4152 iwe.cmd = SIOCGIWAP;
4153 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4154 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07004155 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004156 IW_EV_ADDR_LEN);
4157
4158 memset(&iwe, 0, sizeof(iwe));
4159 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01004160 if (bss_mesh_cfg(bss)) {
4161 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004162 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004163 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4164 &iwe, bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004165 } else {
4166 iwe.u.data.length = bss->ssid_len;
4167 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004168 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4169 &iwe, bss->ssid);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004170 }
Jiri Bencf0706e82007-05-05 11:45:53 -07004171
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08004172 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4173 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004174 memset(&iwe, 0, sizeof(iwe));
4175 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01004176 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004177 iwe.u.mode = IW_MODE_MESH;
4178 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004179 iwe.u.mode = IW_MODE_MASTER;
4180 else
4181 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07004182 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4183 &iwe, IW_EV_UINT_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -07004184 }
4185
4186 memset(&iwe, 0, sizeof(iwe));
4187 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01004188 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4189 iwe.u.freq.e = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004190 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004191 IW_EV_FREQ_LEN);
4192
4193 memset(&iwe, 0, sizeof(iwe));
Tomas Winkler9381be02008-05-23 01:36:36 +03004194 iwe.cmd = SIOCGIWFREQ;
4195 iwe.u.freq.m = bss->freq;
4196 iwe.u.freq.e = 6;
David S. Millerccc58052008-06-16 18:50:49 -07004197 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Tomas Winkler9381be02008-05-23 01:36:36 +03004198 IW_EV_FREQ_LEN);
4199 memset(&iwe, 0, sizeof(iwe));
Jiri Bencf0706e82007-05-05 11:45:53 -07004200 iwe.cmd = IWEVQUAL;
Bruno Randolf566bfe52008-05-08 19:15:40 +02004201 iwe.u.qual.qual = bss->qual;
4202 iwe.u.qual.level = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07004203 iwe.u.qual.noise = bss->noise;
4204 iwe.u.qual.updated = local->wstats_flags;
David S. Millerccc58052008-06-16 18:50:49 -07004205 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004206 IW_EV_QUAL_LEN);
4207
4208 memset(&iwe, 0, sizeof(iwe));
4209 iwe.cmd = SIOCGIWENCODE;
4210 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4211 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4212 else
4213 iwe.u.data.flags = IW_ENCODE_DISABLED;
4214 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004215 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4216 &iwe, "");
Jiri Bencf0706e82007-05-05 11:45:53 -07004217
4218 if (bss && bss->wpa_ie) {
4219 memset(&iwe, 0, sizeof(iwe));
4220 iwe.cmd = IWEVGENIE;
4221 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004222 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4223 &iwe, bss->wpa_ie);
Jiri Bencf0706e82007-05-05 11:45:53 -07004224 }
4225
4226 if (bss && bss->rsn_ie) {
4227 memset(&iwe, 0, sizeof(iwe));
4228 iwe.cmd = IWEVGENIE;
4229 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004230 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4231 &iwe, bss->rsn_ie);
Jiri Bencf0706e82007-05-05 11:45:53 -07004232 }
4233
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004234 if (bss && bss->ht_ie) {
4235 memset(&iwe, 0, sizeof(iwe));
4236 iwe.cmd = IWEVGENIE;
4237 iwe.u.data.length = bss->ht_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004238 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4239 &iwe, bss->ht_ie);
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004240 }
4241
Jiri Bencf0706e82007-05-05 11:45:53 -07004242 if (bss && bss->supp_rates_len > 0) {
4243 /* display all supported rates in readable format */
David S. Millerccc58052008-06-16 18:50:49 -07004244 char *p = current_ev + iwe_stream_lcp_len(info);
Jiri Bencf0706e82007-05-05 11:45:53 -07004245 int i;
4246
4247 memset(&iwe, 0, sizeof(iwe));
4248 iwe.cmd = SIOCGIWRATE;
4249 /* Those two flags are ignored... */
4250 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4251
4252 for (i = 0; i < bss->supp_rates_len; i++) {
4253 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4254 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07004255 p = iwe_stream_add_value(info, current_ev, p,
Jiri Bencf0706e82007-05-05 11:45:53 -07004256 end_buf, &iwe, IW_EV_PARAM_LEN);
4257 }
4258 current_ev = p;
4259 }
4260
4261 if (bss) {
4262 char *buf;
4263 buf = kmalloc(30, GFP_ATOMIC);
4264 if (buf) {
4265 memset(&iwe, 0, sizeof(iwe));
4266 iwe.cmd = IWEVCUSTOM;
4267 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4268 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004269 current_ev = iwe_stream_add_point(info, current_ev,
4270 end_buf,
Jiri Bencf0706e82007-05-05 11:45:53 -07004271 &iwe, buf);
Emmanuel Grumbachbf998f62008-06-24 13:38:00 +03004272 memset(&iwe, 0, sizeof(iwe));
4273 iwe.cmd = IWEVCUSTOM;
4274 sprintf(buf, " Last beacon: %dms ago",
4275 jiffies_to_msecs(jiffies - bss->last_update));
4276 iwe.u.data.length = strlen(buf);
4277 current_ev = iwe_stream_add_point(info, current_ev,
4278 end_buf, &iwe, buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004279 kfree(buf);
4280 }
4281 }
4282
Johannes Berg902acc72008-02-23 15:17:19 +01004283 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004284 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04004285 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004286 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004287 if (buf) {
4288 memset(&iwe, 0, sizeof(iwe));
4289 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04004290 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004291 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004292 current_ev = iwe_stream_add_point(info, current_ev,
4293 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004294 &iwe, buf);
4295 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04004296 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4297 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004298 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004299 current_ev = iwe_stream_add_point(info, current_ev,
4300 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004301 &iwe, buf);
4302 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04004303 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4304 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004305 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004306 current_ev = iwe_stream_add_point(info, current_ev,
4307 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004308 &iwe, buf);
4309 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04004310 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4311 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004312 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004313 current_ev = iwe_stream_add_point(info, current_ev,
4314 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004315 &iwe, buf);
4316 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04004317 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4318 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004319 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004320 current_ev = iwe_stream_add_point(info, current_ev,
4321 end_buf,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004322 &iwe, buf);
4323 kfree(buf);
4324 }
4325 }
4326
Jiri Bencf0706e82007-05-05 11:45:53 -07004327 return current_ev;
4328}
4329
4330
David S. Millerccc58052008-06-16 18:50:49 -07004331int ieee80211_sta_scan_results(struct net_device *dev,
4332 struct iw_request_info *info,
4333 char *buf, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07004334{
4335 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4336 char *current_ev = buf;
4337 char *end_buf = buf + len;
4338 struct ieee80211_sta_bss *bss;
4339
4340 spin_lock_bh(&local->sta_bss_lock);
4341 list_for_each_entry(bss, &local->sta_bss_list, list) {
4342 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4343 spin_unlock_bh(&local->sta_bss_lock);
4344 return -E2BIG;
4345 }
David S. Millerccc58052008-06-16 18:50:49 -07004346 current_ev = ieee80211_sta_scan_result(dev, info, bss,
4347 current_ev, end_buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004348 }
4349 spin_unlock_bh(&local->sta_bss_lock);
4350 return current_ev - buf;
4351}
4352
4353
4354int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4355{
4356 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4357 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg988c0f72008-04-17 19:21:22 +02004358
Jiri Bencf0706e82007-05-05 11:45:53 -07004359 kfree(ifsta->extra_ie);
4360 if (len == 0) {
4361 ifsta->extra_ie = NULL;
4362 ifsta->extra_ie_len = 0;
4363 return 0;
4364 }
4365 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4366 if (!ifsta->extra_ie) {
4367 ifsta->extra_ie_len = 0;
4368 return -ENOMEM;
4369 }
4370 memcpy(ifsta->extra_ie, ie, len);
4371 ifsta->extra_ie_len = len;
4372 return 0;
4373}
4374
4375
Johannes Berg988c0f72008-04-17 19:21:22 +02004376struct sta_info *ieee80211_ibss_add_sta(struct net_device *dev,
4377 struct sk_buff *skb, u8 *bssid,
Vladimir Koutny87291c02008-06-13 16:50:44 +02004378 u8 *addr, u64 supp_rates)
Jiri Bencf0706e82007-05-05 11:45:53 -07004379{
4380 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4381 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004382 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004383 DECLARE_MAC_BUF(mac);
Vladimir Koutny87291c02008-06-13 16:50:44 +02004384 int band = local->hw.conf.channel->band;
Jiri Bencf0706e82007-05-05 11:45:53 -07004385
4386 /* TODO: Could consider removing the least recently used entry and
4387 * allow new one to be added. */
4388 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4389 if (net_ratelimit()) {
4390 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004391 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004392 }
4393 return NULL;
4394 }
4395
Vladimir Koutny87291c02008-06-13 16:50:44 +02004396 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid))
4397 return NULL;
4398
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004399#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07004400 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004401 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004402#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07004403
Johannes Berg73651ee2008-02-25 16:27:47 +01004404 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4405 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004406 return NULL;
4407
Johannes Berg07346f812008-05-03 01:02:02 +02004408 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
Johannes Berg238814f2008-01-28 17:19:37 +01004409
Vladimir Koutny87291c02008-06-13 16:50:44 +02004410 if (supp_rates)
4411 sta->supp_rates[band] = supp_rates;
4412 else
4413 sta->supp_rates[band] = sdata->u.sta.supp_rates_bits[band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004414
4415 rate_control_rate_init(sta, local);
4416
Johannes Berg93e5deb2008-04-01 15:21:00 +02004417 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004418 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004419
Johannes Bergd0709a62008-02-25 16:27:46 +01004420 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004421}
4422
4423
4424int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4425{
4426 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4427 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4428
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004429 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07004430 dev->name, reason);
4431
Johannes Berg51fb61e2007-12-19 01:31:27 +01004432 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4433 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004434 return -EINVAL;
4435
4436 ieee80211_send_deauth(dev, ifsta, reason);
4437 ieee80211_set_disassoc(dev, ifsta, 1);
4438 return 0;
4439}
4440
4441
4442int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4443{
4444 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4445 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4446
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004447 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07004448 dev->name, reason);
4449
Johannes Berg51fb61e2007-12-19 01:31:27 +01004450 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004451 return -EINVAL;
4452
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004453 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004454 return -1;
4455
4456 ieee80211_send_disassoc(dev, ifsta, reason);
4457 ieee80211_set_disassoc(dev, ifsta, 0);
4458 return 0;
4459}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004460
4461void ieee80211_notify_mac(struct ieee80211_hw *hw,
4462 enum ieee80211_notification_types notif_type)
4463{
4464 struct ieee80211_local *local = hw_to_local(hw);
4465 struct ieee80211_sub_if_data *sdata;
4466
4467 switch (notif_type) {
4468 case IEEE80211_NOTIFY_RE_ASSOC:
4469 rcu_read_lock();
4470 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4471
4472 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4473 ieee80211_sta_req_auth(sdata->dev,
4474 &sdata->u.sta);
4475 }
4476
4477 }
4478 rcu_read_unlock();
4479 break;
4480 }
4481}
4482EXPORT_SYMBOL(ieee80211_notify_mac);