blob: 0a310d09ab076fee5c7a6ca74f788ea12188be06 [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);
349#ifdef CONFIG_MAC80211_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700350 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100351 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e82007-05-05 11:45:53 -0700352 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100353 params.cw_max, params.txop);
354#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700355 /* TODO: handle ACM (block TX, fallback to next lowest allowed
356 * AC for now) */
357 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
358 printk(KERN_DEBUG "%s: failed to set TX queue "
359 "parameters for queue %d\n", dev->name, queue);
360 }
361 }
362}
363
John W. Linville50c4afb2008-04-15 14:09:27 -0400364static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
365 bool use_protection,
366 bool use_short_preamble)
Daniel Drake56282212007-07-10 19:32:10 +0200367{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100368 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Daniel Drake56282212007-07-10 19:32:10 +0200369 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Joe Perches0795af52007-10-03 17:59:30 -0700370 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100371 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200372
Johannes Berg471b3ef2007-12-28 14:32:58 +0100373 if (use_protection != bss_conf->use_cts_prot) {
Daniel Drake56282212007-07-10 19:32:10 +0200374 if (net_ratelimit()) {
375 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700376 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100377 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200378 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700379 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200380 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100381 bss_conf->use_cts_prot = use_protection;
382 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200383 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200384
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200385 if (use_short_preamble != bss_conf->use_short_preamble) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200386 if (net_ratelimit()) {
387 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700388 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100389 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200390 use_short_preamble ? "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700391 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200392 }
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200393 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100394 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200395 }
Daniel Draked9430a32007-07-27 15:43:24 +0200396
Johannes Berg471b3ef2007-12-28 14:32:58 +0100397 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200398}
399
John W. Linville50c4afb2008-04-15 14:09:27 -0400400static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
401 u8 erp_value)
402{
403 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
404 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
405
406 return ieee80211_handle_protect_preamb(sdata,
407 use_protection, use_short_preamble);
408}
409
410static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
411 struct ieee80211_sta_bss *bss)
412{
413 u32 changed = 0;
414
415 if (bss->has_erp_value)
416 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
417 else {
418 u16 capab = bss->capability;
419 changed |= ieee80211_handle_protect_preamb(sdata, false,
420 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
421 }
422
423 return changed;
424}
425
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200426int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
427 struct ieee80211_ht_info *ht_info)
428{
429
430 if (ht_info == NULL)
431 return -EINVAL;
432
433 memset(ht_info, 0, sizeof(*ht_info));
434
435 if (ht_cap_ie) {
436 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
437
438 ht_info->ht_supported = 1;
439 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
440 ht_info->ampdu_factor =
441 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
442 ht_info->ampdu_density =
443 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
444 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
445 } else
446 ht_info->ht_supported = 0;
447
448 return 0;
449}
450
451int ieee80211_ht_addt_info_ie_to_ht_bss_info(
452 struct ieee80211_ht_addt_info *ht_add_info_ie,
453 struct ieee80211_ht_bss_info *bss_info)
454{
455 if (bss_info == NULL)
456 return -EINVAL;
457
458 memset(bss_info, 0, sizeof(*bss_info));
459
460 if (ht_add_info_ie) {
461 u16 op_mode;
462 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
463
464 bss_info->primary_channel = ht_add_info_ie->control_chan;
465 bss_info->bss_cap = ht_add_info_ie->ht_param;
466 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
467 }
468
469 return 0;
470}
Daniel Drake56282212007-07-10 19:32:10 +0200471
Jiri Bencf0706e82007-05-05 11:45:53 -0700472static void ieee80211_sta_send_associnfo(struct net_device *dev,
473 struct ieee80211_if_sta *ifsta)
474{
475 char *buf;
476 size_t len;
477 int i;
478 union iwreq_data wrqu;
479
480 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
481 return;
482
483 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200484 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700485 if (!buf)
486 return;
487
488 len = sprintf(buf, "ASSOCINFO(");
489 if (ifsta->assocreq_ies) {
490 len += sprintf(buf + len, "ReqIEs=");
491 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
492 len += sprintf(buf + len, "%02x",
493 ifsta->assocreq_ies[i]);
494 }
495 }
496 if (ifsta->assocresp_ies) {
497 if (ifsta->assocreq_ies)
498 len += sprintf(buf + len, " ");
499 len += sprintf(buf + len, "RespIEs=");
500 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
501 len += sprintf(buf + len, "%02x",
502 ifsta->assocresp_ies[i]);
503 }
504 }
505 len += sprintf(buf + len, ")");
506
507 if (len > IW_CUSTOM_MAX) {
508 len = sprintf(buf, "ASSOCRESPIE=");
509 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
510 len += sprintf(buf + len, "%02x",
511 ifsta->assocresp_ies[i]);
512 }
513 }
514
515 memset(&wrqu, 0, sizeof(wrqu));
516 wrqu.data.length = len;
517 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
518
519 kfree(buf);
520}
521
522
523static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400524 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200525 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700526{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100527 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
528 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700529 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700530 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100531 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700532
Jiri Bencf0706e82007-05-05 11:45:53 -0700533 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200534 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400535
536 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
537
Johannes Berg51fb61e2007-12-19 01:31:27 +0100538 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700539 return;
Daniel Drake56282212007-07-10 19:32:10 +0200540
John W. Linville65c107a2007-10-05 14:23:27 -0400541 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Tomas Winkler38668c02008-03-28 16:33:32 -0700542 conf->channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400543 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200544 if (bss) {
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700545 /* set timing information */
546 sdata->bss_conf.beacon_int = bss->beacon_int;
547 sdata->bss_conf.timestamp = bss->timestamp;
548
John W. Linville50c4afb2008-04-15 14:09:27 -0400549 changed |= ieee80211_handle_bss_capability(sdata, bss);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700550
Daniel Drake56282212007-07-10 19:32:10 +0200551 ieee80211_rx_bss_put(dev, bss);
552 }
553
Tomas Winkler38668c02008-03-28 16:33:32 -0700554 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
555 changed |= BSS_CHANGED_HT;
556 sdata->bss_conf.assoc_ht = 1;
557 sdata->bss_conf.ht_conf = &conf->ht_conf;
558 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
559 }
560
Jiri Bencf0706e82007-05-05 11:45:53 -0700561 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400562 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700563 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
564 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
565 ieee80211_sta_send_associnfo(dev, ifsta);
566 } else {
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700567 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400568 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700569 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200570 ieee80211_reset_erp_info(dev);
Tomas Winkler38668c02008-03-28 16:33:32 -0700571
572 sdata->bss_conf.assoc_ht = 0;
573 sdata->bss_conf.ht_conf = NULL;
574 sdata->bss_conf.ht_bss_conf = NULL;
575
Jiri Bencf0706e82007-05-05 11:45:53 -0700576 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
577 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700578 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200579 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100580
Tomas Winklerb2205252008-01-27 16:18:22 +0200581 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100582 ieee80211_bss_info_change_notify(sdata, changed);
Reinette Chatre41a7be42008-04-03 16:08:49 -0700583 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
584 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700585}
586
587static void ieee80211_set_disassoc(struct net_device *dev,
588 struct ieee80211_if_sta *ifsta, int deauth)
589{
590 if (deauth)
591 ifsta->auth_tries = 0;
592 ifsta->assoc_tries = 0;
593 ieee80211_set_associated(dev, ifsta, 0);
594}
595
Luis Carlos Coboee385852008-02-23 15:17:11 +0100596void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
597 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700598{
599 struct ieee80211_sub_if_data *sdata;
Johannes Berge039fa42008-05-15 12:55:29 +0200600 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700601
602 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
603 skb->dev = sdata->local->mdev;
604 skb_set_mac_header(skb, 0);
605 skb_set_network_header(skb, 0);
606 skb_set_transport_header(skb, 0);
607
Johannes Berge039fa42008-05-15 12:55:29 +0200608 info = IEEE80211_SKB_CB(skb);
609 memset(info, 0, sizeof(struct ieee80211_tx_info));
610 info->control.ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400611 if (!encrypt)
Johannes Berge039fa42008-05-15 12:55:29 +0200612 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700613
614 dev_queue_xmit(skb);
615}
616
617
618static void ieee80211_send_auth(struct net_device *dev,
619 struct ieee80211_if_sta *ifsta,
620 int transaction, u8 *extra, size_t extra_len,
621 int encrypt)
622{
623 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
624 struct sk_buff *skb;
625 struct ieee80211_mgmt *mgmt;
626
627 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
628 sizeof(*mgmt) + 6 + extra_len);
629 if (!skb) {
630 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
631 "frame\n", dev->name);
632 return;
633 }
634 skb_reserve(skb, local->hw.extra_tx_headroom);
635
636 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
637 memset(mgmt, 0, 24 + 6);
638 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
639 IEEE80211_STYPE_AUTH);
640 if (encrypt)
641 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
642 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
643 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
644 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
645 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
646 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
647 ifsta->auth_transaction = transaction + 1;
648 mgmt->u.auth.status_code = cpu_to_le16(0);
649 if (extra)
650 memcpy(skb_put(skb, extra_len), extra, extra_len);
651
652 ieee80211_sta_tx(dev, skb, encrypt);
653}
654
655
656static void ieee80211_authenticate(struct net_device *dev,
657 struct ieee80211_if_sta *ifsta)
658{
Joe Perches0795af52007-10-03 17:59:30 -0700659 DECLARE_MAC_BUF(mac);
660
Jiri Bencf0706e82007-05-05 11:45:53 -0700661 ifsta->auth_tries++;
662 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700663 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700664 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700665 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700666 ifsta->state = IEEE80211_DISABLED;
667 return;
668 }
669
670 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700671 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
672 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700673
674 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
675
676 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
677}
678
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200679static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
680 struct ieee80211_supported_band *sband,
681 u64 *rates)
682{
683 int i, j, count;
684 *rates = 0;
685 count = 0;
686 for (i = 0; i < bss->supp_rates_len; i++) {
687 int rate = (bss->supp_rates[i] & 0x7F) * 5;
688
689 for (j = 0; j < sband->n_bitrates; j++)
690 if (sband->bitrates[j].bitrate == rate) {
691 *rates |= BIT(j);
692 count++;
693 break;
694 }
695 }
696
697 return count;
698}
Jiri Bencf0706e82007-05-05 11:45:53 -0700699
700static void ieee80211_send_assoc(struct net_device *dev,
701 struct ieee80211_if_sta *ifsta)
702{
703 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700704 struct sk_buff *skb;
705 struct ieee80211_mgmt *mgmt;
706 u8 *pos, *ies;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200707 int i, len, count, rates_len, supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700708 u16 capab;
709 struct ieee80211_sta_bss *bss;
710 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100711 struct ieee80211_supported_band *sband;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200712 u64 rates = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700713
714 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
715 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
716 ifsta->ssid_len);
717 if (!skb) {
718 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
719 "frame\n", dev->name);
720 return;
721 }
722 skb_reserve(skb, local->hw.extra_tx_headroom);
723
Johannes Berg8318d782008-01-24 19:38:38 +0100724 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
725
Jiri Bencf0706e82007-05-05 11:45:53 -0700726 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100727
728 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
729 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
730 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
731 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
732 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700733 }
Johannes Berg8318d782008-01-24 19:38:38 +0100734
735 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
736 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400737 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700738 if (bss) {
739 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
740 capab |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +0200741 if (bss->wmm_ie)
Jiri Bencf0706e82007-05-05 11:45:53 -0700742 wmm = 1;
Helmut Schaa0d580a72008-05-20 09:56:37 +0200743
744 /* get all rates supported by the device and the AP as
745 * some APs don't like getting a superset of their rates
746 * in the association request (e.g. D-Link DAP 1353 in
747 * b-only mode) */
748 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
749
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300750 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
751 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
752 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
753
Jiri Bencf0706e82007-05-05 11:45:53 -0700754 ieee80211_rx_bss_put(dev, bss);
Helmut Schaa0d580a72008-05-20 09:56:37 +0200755 } else {
756 rates = ~0;
757 rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700758 }
759
760 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
761 memset(mgmt, 0, 24);
762 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
763 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
764 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
765
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400766 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700767 skb_put(skb, 10);
768 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
769 IEEE80211_STYPE_REASSOC_REQ);
770 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
771 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
772 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
773 ETH_ALEN);
774 } else {
775 skb_put(skb, 4);
776 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
777 IEEE80211_STYPE_ASSOC_REQ);
778 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
779 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
780 }
781
782 /* SSID */
783 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
784 *pos++ = WLAN_EID_SSID;
785 *pos++ = ifsta->ssid_len;
786 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
787
Helmut Schaa0d580a72008-05-20 09:56:37 +0200788 /* add all rates which were marked to be used above */
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200789 supp_rates_len = rates_len;
790 if (supp_rates_len > 8)
791 supp_rates_len = 8;
Jiri Bencf0706e82007-05-05 11:45:53 -0700792
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200793 len = sband->n_bitrates;
794 pos = skb_put(skb, supp_rates_len + 2);
795 *pos++ = WLAN_EID_SUPP_RATES;
796 *pos++ = supp_rates_len;
797
798 count = 0;
799 for (i = 0; i < sband->n_bitrates; i++) {
800 if (BIT(i) & rates) {
Johannes Berg8318d782008-01-24 19:38:38 +0100801 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700802 *pos++ = (u8) (rate / 5);
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200803 if (++count == 8)
804 break;
805 }
806 }
807
808 if (count == 8) {
809 pos = skb_put(skb, rates_len - count + 2);
810 *pos++ = WLAN_EID_EXT_SUPP_RATES;
811 *pos++ = rates_len - count;
812
813 for (i++; i < sband->n_bitrates; i++) {
814 if (BIT(i) & rates) {
815 int rate = sband->bitrates[i].bitrate;
816 *pos++ = (u8) (rate / 5);
817 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700818 }
819 }
820
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300821 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
822 /* 1. power capabilities */
823 pos = skb_put(skb, 4);
824 *pos++ = WLAN_EID_PWR_CAPABILITY;
825 *pos++ = 2;
826 *pos++ = 0; /* min tx power */
827 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
828
829 /* 2. supported channels */
830 /* TODO: get this in reg domain format */
831 pos = skb_put(skb, 2 * sband->n_channels + 2);
832 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
833 *pos++ = 2 * sband->n_channels;
834 for (i = 0; i < sband->n_channels; i++) {
835 *pos++ = ieee80211_frequency_to_channel(
836 sband->channels[i].center_freq);
837 *pos++ = 1; /* one channel in the subband*/
838 }
839 }
840
Jiri Bencf0706e82007-05-05 11:45:53 -0700841 if (ifsta->extra_ie) {
842 pos = skb_put(skb, ifsta->extra_ie_len);
843 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
844 }
845
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400846 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700847 pos = skb_put(skb, 9);
848 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
849 *pos++ = 7; /* len */
850 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
851 *pos++ = 0x50;
852 *pos++ = 0xf2;
853 *pos++ = 2; /* WME */
854 *pos++ = 0; /* WME info */
855 *pos++ = 1; /* WME ver */
856 *pos++ = 0;
857 }
Johannes Berg3434fbd2008-05-03 00:59:37 +0200858
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200859 /* wmm support is a must to HT */
Johannes Berg3434fbd2008-05-03 00:59:37 +0200860 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
Emmanuel Grumbach93061022008-05-29 16:35:23 +0800861 sband->ht_info.ht_supported && bss->ht_add_ie) {
862 struct ieee80211_ht_addt_info *ht_add_info =
863 (struct ieee80211_ht_addt_info *)bss->ht_add_ie;
864 u16 cap = sband->ht_info.cap;
865 __le16 tmp;
866 u32 flags = local->hw.conf.channel->flags;
867
868 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
869 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
870 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
871 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
872 cap &= ~IEEE80211_HT_CAP_SGI_40;
873 }
874 break;
875 case IEEE80211_HT_IE_CHA_SEC_BELOW:
876 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
877 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
878 cap &= ~IEEE80211_HT_CAP_SGI_40;
879 }
880 break;
881 }
882
883 tmp = cpu_to_le16(cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200884 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
885 *pos++ = WLAN_EID_HT_CAPABILITY;
886 *pos++ = sizeof(struct ieee80211_ht_cap);
887 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
888 memcpy(pos, &tmp, sizeof(u16));
889 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100890 /* TODO: needs a define here for << 2 */
891 *pos++ = sband->ht_info.ampdu_factor |
892 (sband->ht_info.ampdu_density << 2);
893 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200894 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700895
896 kfree(ifsta->assocreq_ies);
897 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200898 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700899 if (ifsta->assocreq_ies)
900 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
901
902 ieee80211_sta_tx(dev, skb, 0);
903}
904
905
906static void ieee80211_send_deauth(struct net_device *dev,
907 struct ieee80211_if_sta *ifsta, u16 reason)
908{
909 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
910 struct sk_buff *skb;
911 struct ieee80211_mgmt *mgmt;
912
913 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
914 if (!skb) {
915 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
916 "frame\n", dev->name);
917 return;
918 }
919 skb_reserve(skb, local->hw.extra_tx_headroom);
920
921 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
922 memset(mgmt, 0, 24);
923 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
924 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
925 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
926 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
927 IEEE80211_STYPE_DEAUTH);
928 skb_put(skb, 2);
929 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
930
931 ieee80211_sta_tx(dev, skb, 0);
932}
933
934
935static void ieee80211_send_disassoc(struct net_device *dev,
936 struct ieee80211_if_sta *ifsta, u16 reason)
937{
938 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
939 struct sk_buff *skb;
940 struct ieee80211_mgmt *mgmt;
941
942 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
943 if (!skb) {
944 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
945 "frame\n", dev->name);
946 return;
947 }
948 skb_reserve(skb, local->hw.extra_tx_headroom);
949
950 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
951 memset(mgmt, 0, 24);
952 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
953 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
954 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
955 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
956 IEEE80211_STYPE_DISASSOC);
957 skb_put(skb, 2);
958 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
959
960 ieee80211_sta_tx(dev, skb, 0);
961}
962
963
964static int ieee80211_privacy_mismatch(struct net_device *dev,
965 struct ieee80211_if_sta *ifsta)
966{
John W. Linville65c107a2007-10-05 14:23:27 -0400967 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700968 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000969 int bss_privacy;
970 int wep_privacy;
971 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700972
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000973 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700974 return 0;
975
Johannes Berg8318d782008-01-24 19:38:38 +0100976 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
977 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400978 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700979 if (!bss)
980 return 0;
981
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000982 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
983 wep_privacy = !!ieee80211_sta_wep_configured(dev);
984 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700985
986 ieee80211_rx_bss_put(dev, bss);
987
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000988 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
989 return 0;
990
991 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700992}
993
994
995static void ieee80211_associate(struct net_device *dev,
996 struct ieee80211_if_sta *ifsta)
997{
Joe Perches0795af52007-10-03 17:59:30 -0700998 DECLARE_MAC_BUF(mac);
999
Jiri Bencf0706e82007-05-05 11:45:53 -07001000 ifsta->assoc_tries++;
1001 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -07001002 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001003 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -07001004 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001005 ifsta->state = IEEE80211_DISABLED;
1006 return;
1007 }
1008
1009 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -07001010 printk(KERN_DEBUG "%s: associate with AP %s\n",
1011 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001012 if (ieee80211_privacy_mismatch(dev, ifsta)) {
1013 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
1014 "mixed-cell disabled - abort association\n", dev->name);
1015 ifsta->state = IEEE80211_DISABLED;
1016 return;
1017 }
1018
1019 ieee80211_send_assoc(dev, ifsta);
1020
1021 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1022}
1023
1024
1025static void ieee80211_associated(struct net_device *dev,
1026 struct ieee80211_if_sta *ifsta)
1027{
1028 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1029 struct sta_info *sta;
1030 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -07001031 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001032
1033 /* TODO: start monitoring current AP signal quality and number of
1034 * missed beacons. Scan other channels every now and then and search
1035 * for better APs. */
1036 /* TODO: remove expired BSSes */
1037
1038 ifsta->state = IEEE80211_ASSOCIATED;
1039
Johannes Bergd0709a62008-02-25 16:27:46 +01001040 rcu_read_lock();
1041
Jiri Bencf0706e82007-05-05 11:45:53 -07001042 sta = sta_info_get(local, ifsta->bssid);
1043 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -07001044 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1045 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001046 disassoc = 1;
1047 } else {
1048 disassoc = 0;
1049 if (time_after(jiffies,
1050 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001051 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001052 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -07001053 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -07001054 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -07001055 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001056 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +01001057 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001058 } else
Jiri Bencf0706e82007-05-05 11:45:53 -07001059 ieee80211_send_probe_req(dev, ifsta->bssid,
1060 local->scan_ssid,
1061 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001062 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001063 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001064 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001065 if (time_after(jiffies, ifsta->last_probe +
1066 IEEE80211_PROBE_INTERVAL)) {
1067 ifsta->last_probe = jiffies;
1068 ieee80211_send_probe_req(dev, ifsta->bssid,
1069 ifsta->ssid,
1070 ifsta->ssid_len);
1071 }
1072 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001073 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001074
1075 rcu_read_unlock();
1076
Johannes Berg3b967662008-04-08 17:56:52 +02001077 if (disassoc && sta)
Johannes Bergd0709a62008-02-25 16:27:46 +01001078 sta_info_destroy(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01001079
Jiri Bencf0706e82007-05-05 11:45:53 -07001080 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -05001081 ifsta->state = IEEE80211_DISABLED;
1082 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001083 } else {
1084 mod_timer(&ifsta->timer, jiffies +
1085 IEEE80211_MONITORING_INTERVAL);
1086 }
1087}
1088
1089
1090static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1091 u8 *ssid, size_t ssid_len)
1092{
1093 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01001094 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001095 struct sk_buff *skb;
1096 struct ieee80211_mgmt *mgmt;
1097 u8 *pos, *supp_rates, *esupp_rates = NULL;
1098 int i;
1099
1100 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1101 if (!skb) {
1102 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1103 "request\n", dev->name);
1104 return;
1105 }
1106 skb_reserve(skb, local->hw.extra_tx_headroom);
1107
1108 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1109 memset(mgmt, 0, 24);
1110 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1111 IEEE80211_STYPE_PROBE_REQ);
1112 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1113 if (dst) {
1114 memcpy(mgmt->da, dst, ETH_ALEN);
1115 memcpy(mgmt->bssid, dst, ETH_ALEN);
1116 } else {
1117 memset(mgmt->da, 0xff, ETH_ALEN);
1118 memset(mgmt->bssid, 0xff, ETH_ALEN);
1119 }
1120 pos = skb_put(skb, 2 + ssid_len);
1121 *pos++ = WLAN_EID_SSID;
1122 *pos++ = ssid_len;
1123 memcpy(pos, ssid, ssid_len);
1124
1125 supp_rates = skb_put(skb, 2);
1126 supp_rates[0] = WLAN_EID_SUPP_RATES;
1127 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001128 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1129
1130 for (i = 0; i < sband->n_bitrates; i++) {
1131 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07001132 if (esupp_rates) {
1133 pos = skb_put(skb, 1);
1134 esupp_rates[1]++;
1135 } else if (supp_rates[1] == 8) {
1136 esupp_rates = skb_put(skb, 3);
1137 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1138 esupp_rates[1] = 1;
1139 pos = &esupp_rates[2];
1140 } else {
1141 pos = skb_put(skb, 1);
1142 supp_rates[1]++;
1143 }
Johannes Berg8318d782008-01-24 19:38:38 +01001144 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -07001145 }
1146
1147 ieee80211_sta_tx(dev, skb, 0);
1148}
1149
1150
1151static int ieee80211_sta_wep_configured(struct net_device *dev)
1152{
1153 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1154 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001155 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -07001156 return 0;
1157 return 1;
1158}
1159
1160
1161static void ieee80211_auth_completed(struct net_device *dev,
1162 struct ieee80211_if_sta *ifsta)
1163{
1164 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001165 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001166 ieee80211_associate(dev, ifsta);
1167}
1168
1169
1170static void ieee80211_auth_challenge(struct net_device *dev,
1171 struct ieee80211_if_sta *ifsta,
1172 struct ieee80211_mgmt *mgmt,
1173 size_t len)
1174{
1175 u8 *pos;
1176 struct ieee802_11_elems elems;
1177
1178 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1179 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001180 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001181 if (!elems.challenge) {
1182 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1183 "frame\n", dev->name);
1184 return;
1185 }
1186 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1187 elems.challenge_len + 2, 1);
1188}
1189
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001190static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1191 u8 dialog_token, u16 status, u16 policy,
1192 u16 buf_size, u16 timeout)
1193{
1194 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1195 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1196 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1197 struct sk_buff *skb;
1198 struct ieee80211_mgmt *mgmt;
1199 u16 capab;
1200
Ester Kummer3acea5b2008-04-17 16:05:14 -07001201 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1202
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001203 if (!skb) {
1204 printk(KERN_DEBUG "%s: failed to allocate buffer "
1205 "for addba resp frame\n", dev->name);
1206 return;
1207 }
1208
1209 skb_reserve(skb, local->hw.extra_tx_headroom);
1210 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1211 memset(mgmt, 0, 24);
1212 memcpy(mgmt->da, da, ETH_ALEN);
1213 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001214 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001215 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1216 else
1217 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1218 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1219 IEEE80211_STYPE_ACTION);
1220
1221 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1222 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1223 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1224 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1225
1226 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1227 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1228 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1229
1230 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1231 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1232 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1233
1234 ieee80211_sta_tx(dev, skb, 0);
1235
1236 return;
1237}
1238
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001239void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1240 u16 tid, u8 dialog_token, u16 start_seq_num,
1241 u16 agg_size, u16 timeout)
1242{
1243 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1244 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1245 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1246 struct sk_buff *skb;
1247 struct ieee80211_mgmt *mgmt;
1248 u16 capab;
1249
Ester Kummer3acea5b2008-04-17 16:05:14 -07001250 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001251
1252 if (!skb) {
1253 printk(KERN_ERR "%s: failed to allocate buffer "
1254 "for addba request frame\n", dev->name);
1255 return;
1256 }
1257 skb_reserve(skb, local->hw.extra_tx_headroom);
1258 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1259 memset(mgmt, 0, 24);
1260 memcpy(mgmt->da, da, ETH_ALEN);
1261 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1262 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1263 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1264 else
1265 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1266
1267 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1268 IEEE80211_STYPE_ACTION);
1269
1270 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1271
1272 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1273 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1274
1275 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1276 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1277 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1278 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1279
1280 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1281
1282 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1283 mgmt->u.action.u.addba_req.start_seq_num =
1284 cpu_to_le16(start_seq_num << 4);
1285
1286 ieee80211_sta_tx(dev, skb, 0);
1287}
1288
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001289static void ieee80211_sta_process_addba_request(struct net_device *dev,
1290 struct ieee80211_mgmt *mgmt,
1291 size_t len)
1292{
1293 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001294 struct ieee80211_hw *hw = &local->hw;
1295 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001296 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001297 struct tid_ampdu_rx *tid_agg_rx;
1298 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001299 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001300 int ret = -EOPNOTSUPP;
1301 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001302
Johannes Bergd0709a62008-02-25 16:27:46 +01001303 rcu_read_lock();
1304
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001305 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001306 if (!sta) {
1307 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001308 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001309 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001310
1311 /* extract session parameters from addba request frame */
1312 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1313 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001314 start_seq_num =
1315 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001316
1317 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1318 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1319 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1320 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1321
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001322 status = WLAN_STATUS_REQUEST_DECLINED;
1323
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001324 /* sanity check for incoming parameters:
1325 * check if configuration can support the BA policy
1326 * and if buffer size does not exceeds max value */
1327 if (((ba_policy != 1)
1328 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1329 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1330 status = WLAN_STATUS_INVALID_QOS_PARAM;
1331#ifdef CONFIG_MAC80211_HT_DEBUG
1332 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001333 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001334 "%s on tid %u. policy %d, buffer size %d\n",
1335 print_mac(mac, mgmt->sa), tid, ba_policy,
1336 buf_size);
1337#endif /* CONFIG_MAC80211_HT_DEBUG */
1338 goto end_no_lock;
1339 }
1340 /* determine default buffer size */
1341 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001342 struct ieee80211_supported_band *sband;
1343
1344 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001345 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001346 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001347 }
1348
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001349
1350 /* examine state machine */
Johannes Berg07346f812008-05-03 01:02:02 +02001351 spin_lock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001352
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001353 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001354#ifdef CONFIG_MAC80211_HT_DEBUG
1355 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001356 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001357 "%s on tid %u\n",
1358 print_mac(mac, mgmt->sa), tid);
1359#endif /* CONFIG_MAC80211_HT_DEBUG */
1360 goto end;
1361 }
1362
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001363 /* prepare A-MPDU MLME for Rx aggregation */
1364 sta->ampdu_mlme.tid_rx[tid] =
1365 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1366 if (!sta->ampdu_mlme.tid_rx[tid]) {
1367 if (net_ratelimit())
1368 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1369 tid);
1370 goto end;
1371 }
1372 /* rx timer */
1373 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1374 sta_rx_agg_session_timer_expired;
1375 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1376 (unsigned long)&sta->timer_to_tid[tid];
1377 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1378
1379 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1380
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001381 /* prepare reordering buffer */
1382 tid_agg_rx->reorder_buf =
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301383 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001384 if (!tid_agg_rx->reorder_buf) {
1385 if (net_ratelimit())
1386 printk(KERN_ERR "can not allocate reordering buffer "
1387 "to tid %d\n", tid);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001388 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001389 goto end;
1390 }
1391 memset(tid_agg_rx->reorder_buf, 0,
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301392 buf_size * sizeof(struct sk_buff *));
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001393
1394 if (local->ops->ampdu_action)
1395 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001396 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001397#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001398 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001399#endif /* CONFIG_MAC80211_HT_DEBUG */
1400
1401 if (ret) {
1402 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001403 kfree(tid_agg_rx);
1404 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001405 goto end;
1406 }
1407
1408 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001409 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001410 tid_agg_rx->dialog_token = dialog_token;
1411 tid_agg_rx->ssn = start_seq_num;
1412 tid_agg_rx->head_seq_num = start_seq_num;
1413 tid_agg_rx->buf_size = buf_size;
1414 tid_agg_rx->timeout = timeout;
1415 tid_agg_rx->stored_mpdu_num = 0;
1416 status = WLAN_STATUS_SUCCESS;
1417end:
Johannes Berg07346f812008-05-03 01:02:02 +02001418 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001419
1420end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001421 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1422 dialog_token, status, 1, buf_size, timeout);
1423 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001424}
Jiri Bencf0706e82007-05-05 11:45:53 -07001425
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001426static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1427 struct ieee80211_mgmt *mgmt,
1428 size_t len)
1429{
1430 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1431 struct ieee80211_hw *hw = &local->hw;
1432 struct sta_info *sta;
1433 u16 capab;
1434 u16 tid;
1435 u8 *state;
1436
Johannes Bergd0709a62008-02-25 16:27:46 +01001437 rcu_read_lock();
1438
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001439 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001440 if (!sta) {
1441 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001442 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001443 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001444
1445 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1446 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1447
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001448 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001449
Johannes Berg07346f812008-05-03 01:02:02 +02001450 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001451
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001452 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001453 spin_unlock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001454 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1455 "%d\n", *state);
1456 goto addba_resp_exit;
1457 }
1458
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001459 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001460 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Johannes Berg07346f812008-05-03 01:02:02 +02001461 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001462#ifdef CONFIG_MAC80211_HT_DEBUG
1463 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1464#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001465 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001466 }
1467
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001468 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001469#ifdef CONFIG_MAC80211_HT_DEBUG
1470 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1471#endif /* CONFIG_MAC80211_HT_DEBUG */
1472 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1473 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001474 if (*state & HT_ADDBA_RECEIVED_MSK)
1475 printk(KERN_DEBUG "double addBA response\n");
1476
1477 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001478 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001479
1480 if (*state == HT_AGG_STATE_OPERATIONAL) {
1481 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1482 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1483 }
1484
Johannes Berg07346f812008-05-03 01:02:02 +02001485 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001486 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1487 } else {
1488 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1489
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001490 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001491 /* this will allow the state check in stop_BA_session */
1492 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001493 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001494 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1495 WLAN_BACK_INITIATOR);
1496 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001497
1498addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001499 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001500}
1501
1502void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1503 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001504{
1505 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1506 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1507 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1508 struct sk_buff *skb;
1509 struct ieee80211_mgmt *mgmt;
1510 u16 params;
1511
Ester Kummer3acea5b2008-04-17 16:05:14 -07001512 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001513
1514 if (!skb) {
1515 printk(KERN_ERR "%s: failed to allocate buffer "
1516 "for delba frame\n", dev->name);
1517 return;
1518 }
1519
1520 skb_reserve(skb, local->hw.extra_tx_headroom);
1521 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1522 memset(mgmt, 0, 24);
1523 memcpy(mgmt->da, da, ETH_ALEN);
1524 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001525 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001526 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1527 else
1528 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1529 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1530 IEEE80211_STYPE_ACTION);
1531
1532 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1533
1534 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1535 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1536 params = (u16)(initiator << 11); /* bit 11 initiator */
1537 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1538
1539 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1540 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1541
1542 ieee80211_sta_tx(dev, skb, 0);
1543}
1544
1545void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1546 u16 initiator, u16 reason)
1547{
1548 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1549 struct ieee80211_hw *hw = &local->hw;
1550 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001551 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001552 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001553
Johannes Bergd0709a62008-02-25 16:27:46 +01001554 rcu_read_lock();
1555
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001556 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001557 if (!sta) {
1558 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001559 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001560 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001561
1562 /* check if TID is in operational state */
Johannes Berg07346f812008-05-03 01:02:02 +02001563 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001564 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001565 != HT_AGG_STATE_OPERATIONAL) {
Johannes Berg07346f812008-05-03 01:02:02 +02001566 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +01001567 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001568 return;
1569 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001570 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001571 HT_AGG_STATE_REQ_STOP_BA_MSK |
1572 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Johannes Berg07346f812008-05-03 01:02:02 +02001573 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001574
1575 /* stop HW Rx aggregation. ampdu_action existence
1576 * already verified in session init so we add the BUG_ON */
1577 BUG_ON(!local->ops->ampdu_action);
1578
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001579#ifdef CONFIG_MAC80211_HT_DEBUG
1580 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1581 print_mac(mac, ra), tid);
1582#endif /* CONFIG_MAC80211_HT_DEBUG */
1583
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001584 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001585 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001586 if (ret)
1587 printk(KERN_DEBUG "HW problem - can not stop rx "
1588 "aggergation for tid %d\n", tid);
1589
1590 /* shutdown timer has not expired */
1591 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001592 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001593
1594 /* check if this is a self generated aggregation halt */
1595 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1596 ieee80211_send_delba(dev, ra, tid, 0, reason);
1597
1598 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001599 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1600 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001601 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001602 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1603 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1604 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001605 }
1606 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001607 /* free resources */
1608 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1609 kfree(sta->ampdu_mlme.tid_rx[tid]);
1610 sta->ampdu_mlme.tid_rx[tid] = NULL;
1611 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001612
Johannes Bergd0709a62008-02-25 16:27:46 +01001613 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001614}
1615
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001616
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001617static void ieee80211_sta_process_delba(struct net_device *dev,
1618 struct ieee80211_mgmt *mgmt, size_t len)
1619{
1620 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1621 struct sta_info *sta;
1622 u16 tid, params;
1623 u16 initiator;
1624 DECLARE_MAC_BUF(mac);
1625
Johannes Bergd0709a62008-02-25 16:27:46 +01001626 rcu_read_lock();
1627
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001628 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001629 if (!sta) {
1630 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001631 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001632 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001633
1634 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1635 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1636 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1637
1638#ifdef CONFIG_MAC80211_HT_DEBUG
1639 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001640 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1641 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001642 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001643 mgmt->u.action.u.delba.reason_code);
1644#endif /* CONFIG_MAC80211_HT_DEBUG */
1645
1646 if (initiator == WLAN_BACK_INITIATOR)
1647 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1648 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001649 else { /* WLAN_BACK_RECIPIENT */
Johannes Berg07346f812008-05-03 01:02:02 +02001650 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001651 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001652 HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001653 spin_unlock_bh(&sta->lock);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001654 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1655 WLAN_BACK_RECIPIENT);
1656 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001657 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001658}
1659
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001660/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001661 * After sending add Block Ack request we activated a timer until
1662 * add Block Ack response will arrive from the recipient.
1663 * If this timer expires sta_addba_resp_timer_expired will be executed.
1664 */
1665void sta_addba_resp_timer_expired(unsigned long data)
1666{
1667 /* not an elegant detour, but there is no choice as the timer passes
1668 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001669 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001670 * array gives the sta through container_of */
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301671 u16 tid = *(u8 *)data;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001672 struct sta_info *temp_sta = container_of((void *)data,
1673 struct sta_info, timer_to_tid[tid]);
1674
1675 struct ieee80211_local *local = temp_sta->local;
1676 struct ieee80211_hw *hw = &local->hw;
1677 struct sta_info *sta;
1678 u8 *state;
1679
Johannes Bergd0709a62008-02-25 16:27:46 +01001680 rcu_read_lock();
1681
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001682 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001683 if (!sta) {
1684 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001685 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001686 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001687
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001688 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001689 /* check if the TID waits for addBA response */
Johannes Berg07346f812008-05-03 01:02:02 +02001690 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001691 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001692 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001693 *state = HT_AGG_STATE_IDLE;
1694 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1695 "expecting addBA response there", tid);
1696 goto timer_expired_exit;
1697 }
1698
1699 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1700
1701 /* go through the state check in stop_BA_session */
1702 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001703 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001704 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1705 WLAN_BACK_INITIATOR);
1706
1707timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001708 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001709}
1710
1711/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001712 * After accepting the AddBA Request we activated a timer,
1713 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001714 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1715 */
Adrian Bunk712590d2008-04-21 11:47:51 +03001716static void sta_rx_agg_session_timer_expired(unsigned long data)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001717{
1718 /* not an elegant detour, but there is no choice as the timer passes
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301719 * only one argument, and various sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001720 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001721 * array gives the sta through container_of */
1722 u8 *ptid = (u8 *)data;
1723 u8 *timer_to_id = ptid - *ptid;
1724 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1725 timer_to_tid[0]);
1726
1727 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergd0709a62008-02-25 16:27:46 +01001728 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1729 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001730 WLAN_REASON_QSTA_TIMEOUT);
1731}
1732
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001733void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1734{
1735 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1736 int i;
1737
1738 for (i = 0; i < STA_TID_NUM; i++) {
1739 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1740 WLAN_BACK_INITIATOR);
1741 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1742 WLAN_BACK_RECIPIENT,
1743 WLAN_REASON_QSTA_LEAVE_QBSS);
1744 }
1745}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001746
Assaf Kraussb6623482008-06-16 16:09:49 +03001747static void ieee80211_send_refuse_measurement_request(struct net_device *dev,
1748 struct ieee80211_msrment_ie *request_ie,
1749 const u8 *da, const u8 *bssid,
1750 u8 dialog_token)
1751{
1752 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1753 struct sk_buff *skb;
1754 struct ieee80211_mgmt *msr_report;
1755
1756 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1757 sizeof(struct ieee80211_msrment_ie));
1758
1759 if (!skb) {
1760 printk(KERN_ERR "%s: failed to allocate buffer for "
1761 "measurement report frame\n", dev->name);
1762 return;
1763 }
1764
1765 skb_reserve(skb, local->hw.extra_tx_headroom);
1766 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1767 memset(msr_report, 0, 24);
1768 memcpy(msr_report->da, da, ETH_ALEN);
1769 memcpy(msr_report->sa, dev->dev_addr, ETH_ALEN);
1770 memcpy(msr_report->bssid, bssid, ETH_ALEN);
1771 msr_report->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1772 IEEE80211_STYPE_ACTION);
1773
1774 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1775 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1776 msr_report->u.action.u.measurement.action_code =
1777 WLAN_ACTION_SPCT_MSR_RPRT;
1778 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1779
1780 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1781 msr_report->u.action.u.measurement.length =
1782 sizeof(struct ieee80211_msrment_ie);
1783
1784 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1785 sizeof(struct ieee80211_msrment_ie));
1786 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1787 msr_report->u.action.u.measurement.msr_elem.mode |=
1788 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1789 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1790
1791 ieee80211_sta_tx(dev, skb, 0);
1792}
1793
1794static void ieee80211_sta_process_measurement_req(struct net_device *dev,
1795 struct ieee80211_mgmt *mgmt,
1796 size_t len)
1797{
1798 /*
1799 * Ignoring measurement request is spec violation.
1800 * Mandatory measurements must be reported optional
1801 * measurements might be refused or reported incapable
1802 * For now just refuse
1803 * TODO: Answer basic measurement as unmeasured
1804 */
1805 ieee80211_send_refuse_measurement_request(dev,
1806 &mgmt->u.action.u.measurement.msr_elem,
1807 mgmt->sa, mgmt->bssid,
1808 mgmt->u.action.u.measurement.dialog_token);
1809}
1810
1811
Jiri Bencf0706e82007-05-05 11:45:53 -07001812static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1813 struct ieee80211_if_sta *ifsta,
1814 struct ieee80211_mgmt *mgmt,
1815 size_t len)
1816{
1817 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1818 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001819 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001820
1821 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Berg51fb61e2007-12-19 01:31:27 +01001822 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001823 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001824 "%s, but not in authenticate state - ignored\n",
1825 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001826 return;
1827 }
1828
1829 if (len < 24 + 6) {
1830 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001831 "received from %s - ignored\n",
1832 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001833 return;
1834 }
1835
Johannes Berg51fb61e2007-12-19 01:31:27 +01001836 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001837 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1838 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001839 "unknown AP (SA=%s BSSID=%s) - "
1840 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1841 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001842 return;
1843 }
1844
Johannes Berg51fb61e2007-12-19 01:31:27 +01001845 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001846 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1847 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001848 "unknown BSSID (SA=%s BSSID=%s) - "
1849 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1850 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001851 return;
1852 }
1853
1854 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1855 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1856 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1857
Joe Perches0795af52007-10-03 17:59:30 -07001858 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e82007-05-05 11:45:53 -07001859 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001860 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e82007-05-05 11:45:53 -07001861 auth_transaction, status_code);
1862
Johannes Berg51fb61e2007-12-19 01:31:27 +01001863 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg135a2112008-06-16 20:55:29 +02001864 /*
1865 * IEEE 802.11 standard does not require authentication in IBSS
Jiri Bencf0706e82007-05-05 11:45:53 -07001866 * networks and most implementations do not seem to use it.
1867 * However, try to reply to authentication attempts if someone
1868 * has actually implemented this.
Johannes Berg135a2112008-06-16 20:55:29 +02001869 */
Jiri Bencf0706e82007-05-05 11:45:53 -07001870 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1871 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1872 "frame (alg=%d transaction=%d)\n",
1873 dev->name, auth_alg, auth_transaction);
1874 return;
1875 }
1876 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1877 }
1878
1879 if (auth_alg != ifsta->auth_alg ||
1880 auth_transaction != ifsta->auth_transaction) {
1881 printk(KERN_DEBUG "%s: unexpected authentication frame "
1882 "(alg=%d transaction=%d)\n",
1883 dev->name, auth_alg, auth_transaction);
1884 return;
1885 }
1886
1887 if (status_code != WLAN_STATUS_SUCCESS) {
1888 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1889 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1890 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1891 u8 algs[3];
1892 const int num_algs = ARRAY_SIZE(algs);
1893 int i, pos;
1894 algs[0] = algs[1] = algs[2] = 0xff;
1895 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1896 algs[0] = WLAN_AUTH_OPEN;
1897 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1898 algs[1] = WLAN_AUTH_SHARED_KEY;
1899 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1900 algs[2] = WLAN_AUTH_LEAP;
1901 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1902 pos = 0;
1903 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1904 pos = 1;
1905 else
1906 pos = 2;
1907 for (i = 0; i < num_algs; i++) {
1908 pos++;
1909 if (pos >= num_algs)
1910 pos = 0;
1911 if (algs[pos] == ifsta->auth_alg ||
1912 algs[pos] == 0xff)
1913 continue;
1914 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1915 !ieee80211_sta_wep_configured(dev))
1916 continue;
1917 ifsta->auth_alg = algs[pos];
1918 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1919 "next try\n",
1920 dev->name, ifsta->auth_alg);
1921 break;
1922 }
1923 }
1924 return;
1925 }
1926
1927 switch (ifsta->auth_alg) {
1928 case WLAN_AUTH_OPEN:
1929 case WLAN_AUTH_LEAP:
1930 ieee80211_auth_completed(dev, ifsta);
1931 break;
1932 case WLAN_AUTH_SHARED_KEY:
1933 if (ifsta->auth_transaction == 4)
1934 ieee80211_auth_completed(dev, ifsta);
1935 else
1936 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1937 break;
1938 }
1939}
1940
1941
1942static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1943 struct ieee80211_if_sta *ifsta,
1944 struct ieee80211_mgmt *mgmt,
1945 size_t len)
1946{
1947 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001948 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001949
1950 if (len < 24 + 2) {
1951 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001952 "received from %s - ignored\n",
1953 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001954 return;
1955 }
1956
1957 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1958 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001959 "unknown AP (SA=%s BSSID=%s) - "
1960 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1961 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001962 return;
1963 }
1964
1965 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1966
Joe Perches0795af52007-10-03 17:59:30 -07001967 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001968 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001969 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001970
Johannes Berg988c0f72008-04-17 19:21:22 +02001971 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001972 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001973
1974 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1975 ifsta->state == IEEE80211_ASSOCIATE ||
1976 ifsta->state == IEEE80211_ASSOCIATED) {
1977 ifsta->state = IEEE80211_AUTHENTICATE;
1978 mod_timer(&ifsta->timer, jiffies +
1979 IEEE80211_RETRY_AUTH_INTERVAL);
1980 }
1981
1982 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001983 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001984}
1985
1986
1987static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1988 struct ieee80211_if_sta *ifsta,
1989 struct ieee80211_mgmt *mgmt,
1990 size_t len)
1991{
1992 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001993 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001994
1995 if (len < 24 + 2) {
1996 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001997 "received from %s - ignored\n",
1998 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001999 return;
2000 }
2001
2002 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
2003 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07002004 "unknown AP (SA=%s BSSID=%s) - "
2005 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
2006 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07002007 return;
2008 }
2009
2010 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
2011
Joe Perches0795af52007-10-03 17:59:30 -07002012 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07002013 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07002014 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07002015
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002016 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07002017 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
2018
2019 if (ifsta->state == IEEE80211_ASSOCIATED) {
2020 ifsta->state = IEEE80211_ASSOCIATE;
2021 mod_timer(&ifsta->timer, jiffies +
2022 IEEE80211_RETRY_AUTH_INTERVAL);
2023 }
2024
2025 ieee80211_set_disassoc(dev, ifsta, 0);
2026}
2027
2028
Johannes Berg471b3ef2007-12-28 14:32:58 +01002029static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002030 struct ieee80211_if_sta *ifsta,
2031 struct ieee80211_mgmt *mgmt,
2032 size_t len,
2033 int reassoc)
2034{
Johannes Berg471b3ef2007-12-28 14:32:58 +01002035 struct ieee80211_local *local = sdata->local;
2036 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01002037 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002038 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01002039 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002040 u16 capab_info, status_code, aid;
2041 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002042 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002043 u8 *pos;
2044 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07002045 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01002046 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002047
2048 /* AssocResp and ReassocResp have identical structure, so process both
2049 * of them in this function. */
2050
2051 if (ifsta->state != IEEE80211_ASSOCIATE) {
2052 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07002053 "%s, but not in associate state - ignored\n",
2054 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002055 return;
2056 }
2057
2058 if (len < 24 + 6) {
2059 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07002060 "received from %s - ignored\n",
2061 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002062 return;
2063 }
2064
2065 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
2066 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07002067 "unknown AP (SA=%s BSSID=%s) - "
2068 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
2069 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07002070 return;
2071 }
2072
2073 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2074 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2075 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07002076
Joe Perches0795af52007-10-03 17:59:30 -07002077 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07002078 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07002079 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02002080 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07002081
2082 if (status_code != WLAN_STATUS_SUCCESS) {
2083 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
2084 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02002085 /* if this was a reassociation, ensure we try a "full"
2086 * association next time. This works around some broken APs
2087 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002088 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07002089 return;
2090 }
2091
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002092 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2093 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
2094 "set\n", dev->name, aid);
2095 aid &= ~(BIT(15) | BIT(14));
2096
Jiri Bencf0706e82007-05-05 11:45:53 -07002097 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04002098 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002099
2100 if (!elems.supp_rates) {
2101 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
2102 dev->name);
2103 return;
2104 }
2105
2106 printk(KERN_DEBUG "%s: associated\n", dev->name);
2107 ifsta->aid = aid;
2108 ifsta->ap_capab = capab_info;
2109
2110 kfree(ifsta->assocresp_ies);
2111 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002112 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002113 if (ifsta->assocresp_ies)
2114 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2115
Johannes Bergd0709a62008-02-25 16:27:46 +01002116 rcu_read_lock();
2117
Jiri Bencf0706e82007-05-05 11:45:53 -07002118 /* Add STA entry for the AP */
2119 sta = sta_info_get(local, ifsta->bssid);
2120 if (!sta) {
2121 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01002122 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01002123
Johannes Berg73651ee2008-02-25 16:27:47 +01002124 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2125 if (!sta) {
2126 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2127 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01002128 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002129 return;
2130 }
John W. Linville65c107a2007-10-05 14:23:27 -04002131 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01002132 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002133 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002134 if (bss) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002135 sta->last_signal = bss->signal;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002136 sta->last_qual = bss->qual;
Jiri Bencf0706e82007-05-05 11:45:53 -07002137 sta->last_noise = bss->noise;
2138 ieee80211_rx_bss_put(dev, bss);
2139 }
Johannes Berg73651ee2008-02-25 16:27:47 +01002140
2141 err = sta_info_insert(sta);
2142 if (err) {
2143 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2144 " the AP (error %d)\n", dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01002145 rcu_read_unlock();
2146 return;
2147 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002148 }
2149
Johannes Berg73651ee2008-02-25 16:27:47 +01002150 /*
2151 * FIXME: Do we really need to update the sta_info's information here?
2152 * We already know about the AP (we found it in our list) so it
2153 * should already be filled with the right info, no?
2154 * As is stands, all this is racy because typically we assume
2155 * the information that is filled in here (except flags) doesn't
2156 * change while a STA structure is alive. As such, it should move
2157 * to between the sta_info_alloc() and sta_info_insert() above.
2158 */
2159
Johannes Berg07346f812008-05-03 01:02:02 +02002160 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2161 WLAN_STA_AUTHORIZED);
Jiri Bencf0706e82007-05-05 11:45:53 -07002162
2163 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002164 basic_rates = 0;
2165 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2166
Jiri Bencf0706e82007-05-05 11:45:53 -07002167 for (i = 0; i < elems.supp_rates_len; i++) {
2168 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002169
2170 if (rate > 110)
2171 have_higher_than_11mbit = true;
2172
2173 for (j = 0; j < sband->n_bitrates; j++) {
2174 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002175 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002176 if (elems.supp_rates[i] & 0x80)
2177 basic_rates |= BIT(j);
2178 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002179 }
Johannes Berg8318d782008-01-24 19:38:38 +01002180
Jiri Bencf0706e82007-05-05 11:45:53 -07002181 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2182 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002183
2184 if (rate > 110)
2185 have_higher_than_11mbit = true;
2186
2187 for (j = 0; j < sband->n_bitrates; j++) {
2188 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002189 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002190 if (elems.ext_supp_rates[i] & 0x80)
2191 basic_rates |= BIT(j);
2192 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002193 }
Johannes Berg8318d782008-01-24 19:38:38 +01002194
2195 sta->supp_rates[local->hw.conf.channel->band] = rates;
2196 sdata->basic_rates = basic_rates;
2197
2198 /* cf. IEEE 802.11 9.2.12 */
2199 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2200 have_higher_than_11mbit)
2201 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2202 else
2203 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002204
Johannes Berg3434fbd2008-05-03 00:59:37 +02002205 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2206 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002207 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002208 ieee80211_ht_cap_ie_to_ht_info(
2209 (struct ieee80211_ht_cap *)
2210 elems.ht_cap_elem, &sta->ht_info);
2211 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2212 (struct ieee80211_ht_addt_info *)
2213 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002214 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002215 }
2216
Jiri Bencf0706e82007-05-05 11:45:53 -07002217 rate_control_rate_init(sta, local);
2218
Johannes Berg3434fbd2008-05-03 00:59:37 +02002219 if (elems.wmm_param) {
Johannes Berg07346f812008-05-03 01:02:02 +02002220 set_sta_flags(sta, WLAN_STA_WME);
Johannes Berge5f98f22008-03-05 20:39:31 +01002221 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002222 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2223 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002224 } else
2225 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002226
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002227 /* set AID and assoc capability,
2228 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002229 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002230 bss_conf->assoc_capability = capab_info;
Johannes Berg8318d782008-01-24 19:38:38 +01002231 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002232
Jiri Bencf0706e82007-05-05 11:45:53 -07002233 ieee80211_associated(dev, ifsta);
2234}
2235
2236
2237/* Caller must hold local->sta_bss_lock */
2238static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2239 struct ieee80211_sta_bss *bss)
2240{
2241 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002242 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002243
2244 if (bss_mesh_cfg(bss))
2245 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2246 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002247 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002248 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002249
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002250 bss->hnext = local->sta_bss_hash[hash_idx];
2251 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002252}
2253
2254
2255/* Caller must hold local->sta_bss_lock */
2256static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2257 struct ieee80211_sta_bss *bss)
2258{
2259 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2260 struct ieee80211_sta_bss *b, *prev = NULL;
2261 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2262 while (b) {
2263 if (b == bss) {
2264 if (!prev)
2265 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2266 bss->hnext;
2267 else
2268 prev->hnext = bss->hnext;
2269 break;
2270 }
2271 prev = b;
2272 b = b->hnext;
2273 }
2274}
2275
2276
2277static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002278ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002279 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002280{
2281 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2282 struct ieee80211_sta_bss *bss;
2283
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002284 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002285 if (!bss)
2286 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002287 atomic_inc(&bss->users);
2288 atomic_inc(&bss->users);
2289 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002290 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002291 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2292 memcpy(bss->ssid, ssid, ssid_len);
2293 bss->ssid_len = ssid_len;
2294 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002295
2296 spin_lock_bh(&local->sta_bss_lock);
2297 /* TODO: order by RSSI? */
2298 list_add_tail(&bss->list, &local->sta_bss_list);
2299 __ieee80211_rx_bss_hash_add(dev, bss);
2300 spin_unlock_bh(&local->sta_bss_lock);
2301 return bss;
2302}
2303
Jiri Bencf0706e82007-05-05 11:45:53 -07002304static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002305ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002306 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002307{
2308 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2309 struct ieee80211_sta_bss *bss;
2310
2311 spin_lock_bh(&local->sta_bss_lock);
2312 bss = local->sta_bss_hash[STA_HASH(bssid)];
2313 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002314 if (!bss_mesh_cfg(bss) &&
2315 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002316 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002317 bss->ssid_len == ssid_len &&
2318 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002319 atomic_inc(&bss->users);
2320 break;
2321 }
2322 bss = bss->hnext;
2323 }
2324 spin_unlock_bh(&local->sta_bss_lock);
2325 return bss;
2326}
2327
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002328#ifdef CONFIG_MAC80211_MESH
2329static struct ieee80211_sta_bss *
2330ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2331 u8 *mesh_cfg, int freq)
2332{
2333 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2334 struct ieee80211_sta_bss *bss;
2335
2336 spin_lock_bh(&local->sta_bss_lock);
2337 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2338 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002339 if (bss_mesh_cfg(bss) &&
2340 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002341 bss->freq == freq &&
2342 mesh_id_len == bss->mesh_id_len &&
2343 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2344 mesh_id_len))) {
2345 atomic_inc(&bss->users);
2346 break;
2347 }
2348 bss = bss->hnext;
2349 }
2350 spin_unlock_bh(&local->sta_bss_lock);
2351 return bss;
2352}
2353
2354static struct ieee80211_sta_bss *
2355ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002356 u8 *mesh_cfg, int mesh_config_len, int freq)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002357{
2358 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2359 struct ieee80211_sta_bss *bss;
2360
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002361 if (mesh_config_len != MESH_CFG_LEN)
2362 return NULL;
2363
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002364 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2365 if (!bss)
2366 return NULL;
2367
John W. Linville24736702008-04-08 14:15:46 -04002368 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002369 if (!bss->mesh_cfg) {
2370 kfree(bss);
2371 return NULL;
2372 }
2373
2374 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2375 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2376 if (!bss->mesh_id) {
2377 kfree(bss->mesh_cfg);
2378 kfree(bss);
2379 return NULL;
2380 }
2381 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2382 }
2383
2384 atomic_inc(&bss->users);
2385 atomic_inc(&bss->users);
John W. Linville24736702008-04-08 14:15:46 -04002386 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002387 bss->mesh_id_len = mesh_id_len;
2388 bss->freq = freq;
2389 spin_lock_bh(&local->sta_bss_lock);
2390 /* TODO: order by RSSI? */
2391 list_add_tail(&bss->list, &local->sta_bss_list);
2392 __ieee80211_rx_bss_hash_add(dev, bss);
2393 spin_unlock_bh(&local->sta_bss_lock);
2394 return bss;
2395}
2396#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002397
2398static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2399{
2400 kfree(bss->wpa_ie);
2401 kfree(bss->rsn_ie);
2402 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002403 kfree(bss->ht_ie);
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002404 kfree(bss->ht_add_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002405 kfree(bss_mesh_id(bss));
2406 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002407 kfree(bss);
2408}
2409
2410
2411static void ieee80211_rx_bss_put(struct net_device *dev,
2412 struct ieee80211_sta_bss *bss)
2413{
2414 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -07002415
Pavel Emelyanov1ebebea2008-04-23 11:47:15 +04002416 local_bh_disable();
2417 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2418 local_bh_enable();
2419 return;
2420 }
2421
Jiri Bencf0706e82007-05-05 11:45:53 -07002422 __ieee80211_rx_bss_hash_del(dev, bss);
2423 list_del(&bss->list);
2424 spin_unlock_bh(&local->sta_bss_lock);
2425 ieee80211_rx_bss_free(bss);
2426}
2427
2428
2429void ieee80211_rx_bss_list_init(struct net_device *dev)
2430{
2431 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2432 spin_lock_init(&local->sta_bss_lock);
2433 INIT_LIST_HEAD(&local->sta_bss_list);
2434}
2435
2436
2437void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2438{
2439 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2440 struct ieee80211_sta_bss *bss, *tmp;
2441
2442 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2443 ieee80211_rx_bss_put(dev, bss);
2444}
2445
2446
Bruno Randolfa6072682008-02-18 11:21:15 +09002447static int ieee80211_sta_join_ibss(struct net_device *dev,
2448 struct ieee80211_if_sta *ifsta,
2449 struct ieee80211_sta_bss *bss)
2450{
2451 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2452 int res, rates, i, j;
2453 struct sk_buff *skb;
2454 struct ieee80211_mgmt *mgmt;
Johannes Berge039fa42008-05-15 12:55:29 +02002455 struct ieee80211_tx_info *control;
Bruno Randolfa6072682008-02-18 11:21:15 +09002456 struct rate_selection ratesel;
2457 u8 *pos;
2458 struct ieee80211_sub_if_data *sdata;
2459 struct ieee80211_supported_band *sband;
Dan Williams507b06d2008-06-03 23:39:55 -04002460 union iwreq_data wrqu;
Bruno Randolfa6072682008-02-18 11:21:15 +09002461
2462 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2463
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002464 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2465
Bruno Randolfa6072682008-02-18 11:21:15 +09002466 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002467 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002468
2469 if (local->ops->reset_tsf) {
2470 /* Reset own TSF to allow time synchronization work. */
2471 local->ops->reset_tsf(local_to_hw(local));
2472 }
2473 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2474 res = ieee80211_if_config(dev);
2475 if (res)
2476 return res;
2477
2478 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2479
Bruno Randolfa6072682008-02-18 11:21:15 +09002480 sdata->drop_unencrypted = bss->capability &
2481 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2482
Assaf Kraussbe038b32008-06-05 19:55:21 +03002483 res = ieee80211_set_freq(dev, bss->freq);
Bruno Randolfa6072682008-02-18 11:21:15 +09002484
Assaf Kraussbe038b32008-06-05 19:55:21 +03002485 if (res)
2486 return res;
Bruno Randolfa6072682008-02-18 11:21:15 +09002487
2488 /* Set beacon template */
2489 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2490 do {
2491 if (!skb)
2492 break;
2493
2494 skb_reserve(skb, local->hw.extra_tx_headroom);
2495
2496 mgmt = (struct ieee80211_mgmt *)
2497 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2498 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2499 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2500 IEEE80211_STYPE_BEACON);
2501 memset(mgmt->da, 0xff, ETH_ALEN);
2502 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2503 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2504 mgmt->u.beacon.beacon_int =
2505 cpu_to_le16(local->hw.conf.beacon_int);
Assaf Krauss4faeb862008-06-30 17:23:16 +08002506 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
Bruno Randolfa6072682008-02-18 11:21:15 +09002507 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2508
2509 pos = skb_put(skb, 2 + ifsta->ssid_len);
2510 *pos++ = WLAN_EID_SSID;
2511 *pos++ = ifsta->ssid_len;
2512 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2513
2514 rates = bss->supp_rates_len;
2515 if (rates > 8)
2516 rates = 8;
2517 pos = skb_put(skb, 2 + rates);
2518 *pos++ = WLAN_EID_SUPP_RATES;
2519 *pos++ = rates;
2520 memcpy(pos, bss->supp_rates, rates);
2521
2522 if (bss->band == IEEE80211_BAND_2GHZ) {
2523 pos = skb_put(skb, 2 + 1);
2524 *pos++ = WLAN_EID_DS_PARAMS;
2525 *pos++ = 1;
2526 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2527 }
2528
2529 pos = skb_put(skb, 2 + 2);
2530 *pos++ = WLAN_EID_IBSS_PARAMS;
2531 *pos++ = 2;
2532 /* FIX: set ATIM window based on scan results */
2533 *pos++ = 0;
2534 *pos++ = 0;
2535
2536 if (bss->supp_rates_len > 8) {
2537 rates = bss->supp_rates_len - 8;
2538 pos = skb_put(skb, 2 + rates);
2539 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2540 *pos++ = rates;
2541 memcpy(pos, &bss->supp_rates[8], rates);
2542 }
2543
Johannes Berge039fa42008-05-15 12:55:29 +02002544 control = IEEE80211_SKB_CB(skb);
2545
Bruno Randolfa6072682008-02-18 11:21:15 +09002546 rate_control_get_rate(dev, sband, skb, &ratesel);
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002547 if (ratesel.rate_idx < 0) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002548 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2549 "for IBSS beacon\n", dev->name);
2550 break;
2551 }
Johannes Berge039fa42008-05-15 12:55:29 +02002552 control->control.vif = &sdata->vif;
2553 control->tx_rate_idx = ratesel.rate_idx;
Bruno Randolfa6072682008-02-18 11:21:15 +09002554 if (sdata->bss_conf.use_short_preamble &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002555 sband->bitrates[ratesel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
Johannes Berge039fa42008-05-15 12:55:29 +02002556 control->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
2557 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2558 control->flags |= IEEE80211_TX_CTL_NO_ACK;
2559 control->control.retry_limit = 1;
Bruno Randolfa6072682008-02-18 11:21:15 +09002560
2561 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2562 if (ifsta->probe_resp) {
2563 mgmt = (struct ieee80211_mgmt *)
2564 ifsta->probe_resp->data;
2565 mgmt->frame_control =
2566 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2567 IEEE80211_STYPE_PROBE_RESP);
2568 } else {
2569 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2570 "template for IBSS\n", dev->name);
2571 }
2572
2573 if (local->ops->beacon_update &&
Johannes Berge039fa42008-05-15 12:55:29 +02002574 local->ops->beacon_update(local_to_hw(local), skb) == 0) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002575 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2576 "template\n", dev->name);
2577 skb = NULL;
2578 }
2579
2580 rates = 0;
2581 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2582 for (i = 0; i < bss->supp_rates_len; i++) {
2583 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2584 for (j = 0; j < sband->n_bitrates; j++)
2585 if (sband->bitrates[j].bitrate == bitrate)
2586 rates |= BIT(j);
2587 }
2588 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Vladimir Koutnye2839d82008-03-18 21:14:07 +01002589
2590 ieee80211_sta_def_wmm_params(dev, bss, 1);
Bruno Randolfa6072682008-02-18 11:21:15 +09002591 } while (0);
2592
2593 if (skb) {
2594 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2595 "template\n", dev->name);
2596 dev_kfree_skb(skb);
2597 }
2598
2599 ifsta->state = IEEE80211_IBSS_JOINED;
2600 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2601
Dan Williams507b06d2008-06-03 23:39:55 -04002602 memset(&wrqu, 0, sizeof(wrqu));
2603 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2604 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Bruno Randolfa6072682008-02-18 11:21:15 +09002605
2606 return res;
2607}
2608
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002609u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2610 struct ieee802_11_elems *elems,
2611 enum ieee80211_band band)
2612{
2613 struct ieee80211_supported_band *sband;
2614 struct ieee80211_rate *bitrates;
2615 size_t num_rates;
2616 u64 supp_rates;
2617 int i, j;
2618 sband = local->hw.wiphy->bands[band];
2619
2620 if (!sband) {
2621 WARN_ON(1);
2622 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2623 }
2624
2625 bitrates = sband->bitrates;
2626 num_rates = sband->n_bitrates;
2627 supp_rates = 0;
2628 for (i = 0; i < elems->supp_rates_len +
2629 elems->ext_supp_rates_len; i++) {
2630 u8 rate = 0;
2631 int own_rate;
2632 if (i < elems->supp_rates_len)
2633 rate = elems->supp_rates[i];
2634 else if (elems->ext_supp_rates)
2635 rate = elems->ext_supp_rates
2636 [i - elems->supp_rates_len];
2637 own_rate = 5 * (rate & 0x7f);
2638 for (j = 0; j < num_rates; j++)
2639 if (bitrates[j].bitrate == own_rate)
2640 supp_rates |= BIT(j);
2641 }
2642 return supp_rates;
2643}
2644
Bruno Randolfa6072682008-02-18 11:21:15 +09002645
Jiri Bencf0706e82007-05-05 11:45:53 -07002646static void ieee80211_rx_bss_info(struct net_device *dev,
2647 struct ieee80211_mgmt *mgmt,
2648 size_t len,
2649 struct ieee80211_rx_status *rx_status,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002650 struct ieee802_11_elems *elems,
Jiri Bencf0706e82007-05-05 11:45:53 -07002651 int beacon)
2652{
2653 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01002654 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002655 struct ieee80211_sta_bss *bss;
2656 struct sta_info *sta;
2657 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002658 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002659 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002660 DECLARE_MAC_BUF(mac);
2661 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002662
2663 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2664 return; /* ignore ProbeResp to foreign address */
2665
2666#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002667 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07002668 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002669 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002670#endif
2671
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002672 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
Jiri Bencf0706e82007-05-05 11:45:53 -07002673
Ester Kummerae6a44e2008-06-27 18:54:48 +03002674 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
2675 elems->mesh_config && mesh_matches_local(elems, dev)) {
2676 u64 rates = ieee80211_sta_get_rates(local, elems,
Johannes Berg902acc72008-02-23 15:17:19 +01002677 rx_status->band);
2678
2679 mesh_neighbour_update(mgmt->sa, rates, dev,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002680 mesh_peer_accepts_plinks(elems, dev));
Johannes Berg902acc72008-02-23 15:17:19 +01002681 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002682
Johannes Bergd0709a62008-02-25 16:27:46 +01002683 rcu_read_lock();
2684
Ester Kummerae6a44e2008-06-27 18:54:48 +03002685 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002686 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2687 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002688 u64 prev_rates;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002689 u64 supp_rates = ieee80211_sta_get_rates(local, elems,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002690 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002691
Johannes Berg8318d782008-01-24 19:38:38 +01002692 prev_rates = sta->supp_rates[rx_status->band];
2693 sta->supp_rates[rx_status->band] &= supp_rates;
2694 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002695 /* No matching rates - this should not really happen.
2696 * Make sure that at least one rate is marked
2697 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002698 sta->supp_rates[rx_status->band] =
2699 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002700 }
Johannes Berg8318d782008-01-24 19:38:38 +01002701 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002702 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002703 "%s based on beacon info (0x%llx & 0x%llx -> "
2704 "0x%llx)\n",
2705 dev->name, print_mac(mac, sta->addr),
2706 (unsigned long long) prev_rates,
2707 (unsigned long long) supp_rates,
2708 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002709 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002710 }
2711
Johannes Bergd0709a62008-02-25 16:27:46 +01002712 rcu_read_unlock();
2713
Ester Kummerae6a44e2008-06-27 18:54:48 +03002714 if (elems->ds_params && elems->ds_params_len == 1)
2715 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002716 else
Johannes Berg8318d782008-01-24 19:38:38 +01002717 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002718
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002719 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2720
2721 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2722 return;
2723
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002724#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002725 if (elems->mesh_config)
2726 bss = ieee80211_rx_mesh_bss_get(dev, elems->mesh_id,
2727 elems->mesh_id_len, elems->mesh_config, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002728 else
2729#endif
2730 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002731 elems->ssid, elems->ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002732 if (!bss) {
2733#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002734 if (elems->mesh_config)
2735 bss = ieee80211_rx_mesh_bss_add(dev, elems->mesh_id,
2736 elems->mesh_id_len, elems->mesh_config,
2737 elems->mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002738 else
2739#endif
2740 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002741 elems->ssid, elems->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002742 if (!bss)
2743 return;
2744 } else {
2745#if 0
2746 /* TODO: order by RSSI? */
2747 spin_lock_bh(&local->sta_bss_lock);
2748 list_move_tail(&bss->list, &local->sta_bss_list);
2749 spin_unlock_bh(&local->sta_bss_lock);
2750#endif
2751 }
2752
Daniel Drake56282212007-07-10 19:32:10 +02002753 /* save the ERP value so that it is available at association time */
Ester Kummerae6a44e2008-06-27 18:54:48 +03002754 if (elems->erp_info && elems->erp_info_len >= 1) {
2755 bss->erp_value = elems->erp_info[0];
Daniel Drake56282212007-07-10 19:32:10 +02002756 bss->has_erp_value = 1;
2757 }
2758
Ester Kummerae6a44e2008-06-27 18:54:48 +03002759 if (elems->ht_cap_elem &&
2760 (!bss->ht_ie || bss->ht_ie_len != elems->ht_cap_elem_len ||
2761 memcmp(bss->ht_ie, elems->ht_cap_elem, elems->ht_cap_elem_len))) {
Johannes Berg30b89b02008-04-16 17:43:20 +02002762 kfree(bss->ht_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002763 bss->ht_ie = kmalloc(elems->ht_cap_elem_len + 2, GFP_ATOMIC);
Johannes Berg30b89b02008-04-16 17:43:20 +02002764 if (bss->ht_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002765 memcpy(bss->ht_ie, elems->ht_cap_elem - 2,
2766 elems->ht_cap_elem_len + 2);
2767 bss->ht_ie_len = elems->ht_cap_elem_len + 2;
Johannes Berg30b89b02008-04-16 17:43:20 +02002768 } else
2769 bss->ht_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002770 } else if (!elems->ht_cap_elem && bss->ht_ie) {
Johannes Berg30b89b02008-04-16 17:43:20 +02002771 kfree(bss->ht_ie);
2772 bss->ht_ie = NULL;
2773 bss->ht_ie_len = 0;
2774 }
2775
Ester Kummerae6a44e2008-06-27 18:54:48 +03002776 if (elems->ht_info_elem &&
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002777 (!bss->ht_add_ie ||
Ester Kummerae6a44e2008-06-27 18:54:48 +03002778 bss->ht_add_ie_len != elems->ht_info_elem_len ||
2779 memcmp(bss->ht_add_ie, elems->ht_info_elem,
2780 elems->ht_info_elem_len))) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002781 kfree(bss->ht_add_ie);
2782 bss->ht_add_ie =
Ester Kummerae6a44e2008-06-27 18:54:48 +03002783 kmalloc(elems->ht_info_elem_len + 2, GFP_ATOMIC);
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002784 if (bss->ht_add_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002785 memcpy(bss->ht_add_ie, elems->ht_info_elem - 2,
2786 elems->ht_info_elem_len + 2);
2787 bss->ht_add_ie_len = elems->ht_info_elem_len + 2;
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002788 } else
2789 bss->ht_add_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002790 } else if (!elems->ht_info_elem && bss->ht_add_ie) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002791 kfree(bss->ht_add_ie);
2792 bss->ht_add_ie = NULL;
2793 bss->ht_add_ie_len = 0;
2794 }
2795
Jiri Bencf0706e82007-05-05 11:45:53 -07002796 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2797 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002798
2799 bss->supp_rates_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002800 if (elems->supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002801 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002802 if (clen > elems->supp_rates_len)
2803 clen = elems->supp_rates_len;
2804 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
Jiri Bencf0706e82007-05-05 11:45:53 -07002805 clen);
2806 bss->supp_rates_len += clen;
2807 }
Ester Kummerae6a44e2008-06-27 18:54:48 +03002808 if (elems->ext_supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002809 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002810 if (clen > elems->ext_supp_rates_len)
2811 clen = elems->ext_supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07002812 memcpy(&bss->supp_rates[bss->supp_rates_len],
Ester Kummerae6a44e2008-06-27 18:54:48 +03002813 elems->ext_supp_rates, clen);
Jiri Bencf0706e82007-05-05 11:45:53 -07002814 bss->supp_rates_len += clen;
2815 }
2816
Johannes Berg30b89b02008-04-16 17:43:20 +02002817 bss->band = rx_status->band;
2818
2819 bss->timestamp = beacon_timestamp;
2820 bss->last_update = jiffies;
Johannes Berg30b89b02008-04-16 17:43:20 +02002821 bss->signal = rx_status->signal;
2822 bss->noise = rx_status->noise;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002823 bss->qual = rx_status->qual;
Johannes Berg30b89b02008-04-16 17:43:20 +02002824 if (!beacon && !bss->probe_resp)
2825 bss->probe_resp = true;
2826
2827 /*
2828 * In STA mode, the remaining parameters should not be overridden
2829 * by beacons because they're not necessarily accurate there.
2830 */
2831 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2832 bss->probe_resp && beacon) {
2833 ieee80211_rx_bss_put(dev, bss);
2834 return;
2835 }
2836
Ester Kummerae6a44e2008-06-27 18:54:48 +03002837 if (elems->wpa &&
2838 (!bss->wpa_ie || bss->wpa_ie_len != elems->wpa_len ||
2839 memcmp(bss->wpa_ie, elems->wpa, elems->wpa_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002840 kfree(bss->wpa_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002841 bss->wpa_ie = kmalloc(elems->wpa_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002842 if (bss->wpa_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002843 memcpy(bss->wpa_ie, elems->wpa - 2, elems->wpa_len + 2);
2844 bss->wpa_ie_len = elems->wpa_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002845 } else
2846 bss->wpa_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002847 } else if (!elems->wpa && bss->wpa_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002848 kfree(bss->wpa_ie);
2849 bss->wpa_ie = NULL;
2850 bss->wpa_ie_len = 0;
2851 }
2852
Ester Kummerae6a44e2008-06-27 18:54:48 +03002853 if (elems->rsn &&
2854 (!bss->rsn_ie || bss->rsn_ie_len != elems->rsn_len ||
2855 memcmp(bss->rsn_ie, elems->rsn, elems->rsn_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002856 kfree(bss->rsn_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002857 bss->rsn_ie = kmalloc(elems->rsn_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002858 if (bss->rsn_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002859 memcpy(bss->rsn_ie, elems->rsn - 2, elems->rsn_len + 2);
2860 bss->rsn_ie_len = elems->rsn_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002861 } else
2862 bss->rsn_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002863 } else if (!elems->rsn && bss->rsn_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002864 kfree(bss->rsn_ie);
2865 bss->rsn_ie = NULL;
2866 bss->rsn_ie_len = 0;
2867 }
2868
Johannes Berg30b89b02008-04-16 17:43:20 +02002869 /*
2870 * Cf.
2871 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2872 *
2873 * quoting:
2874 *
2875 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2876 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2877 * Alliance (September 1, 2004) is incorporated by reference herein.
2878 * The inclusion of the WMM Parameters in probe responses and
2879 * association responses is mandatory for WMM enabled networks. The
2880 * inclusion of the WMM Parameters in beacons, however, is optional.
2881 */
2882
Ester Kummerae6a44e2008-06-27 18:54:48 +03002883 if (elems->wmm_param &&
2884 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_param_len ||
2885 memcmp(bss->wmm_ie, elems->wmm_param, elems->wmm_param_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002886 kfree(bss->wmm_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002887 bss->wmm_ie = kmalloc(elems->wmm_param_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002888 if (bss->wmm_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002889 memcpy(bss->wmm_ie, elems->wmm_param - 2,
2890 elems->wmm_param_len + 2);
2891 bss->wmm_ie_len = elems->wmm_param_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002892 } else
2893 bss->wmm_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002894 } else if (elems->wmm_info &&
2895 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_info_len ||
2896 memcmp(bss->wmm_ie, elems->wmm_info,
2897 elems->wmm_info_len))) {
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002898 /* As for certain AP's Fifth bit is not set in WMM IE in
2899 * beacon frames.So while parsing the beacon frame the
2900 * wmm_info structure is used instead of wmm_param.
2901 * wmm_info structure was never used to set bss->wmm_ie.
2902 * This code fixes this problem by copying the WME
2903 * information from wmm_info to bss->wmm_ie and enabling
2904 * n-band association.
2905 */
2906 kfree(bss->wmm_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002907 bss->wmm_ie = kmalloc(elems->wmm_info_len + 2, GFP_ATOMIC);
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002908 if (bss->wmm_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002909 memcpy(bss->wmm_ie, elems->wmm_info - 2,
2910 elems->wmm_info_len + 2);
2911 bss->wmm_ie_len = elems->wmm_info_len + 2;
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002912 } else
2913 bss->wmm_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002914 } else if (!elems->wmm_param && !elems->wmm_info && bss->wmm_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002915 kfree(bss->wmm_ie);
2916 bss->wmm_ie = NULL;
2917 bss->wmm_ie_len = 0;
2918 }
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002919
2920 /* check if we need to merge IBSS */
2921 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2922 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002923 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002924 bss->freq == local->oper_channel->center_freq &&
Ester Kummerae6a44e2008-06-27 18:54:48 +03002925 elems->ssid_len == sdata->u.sta.ssid_len &&
2926 memcmp(elems->ssid, sdata->u.sta.ssid,
2927 sdata->u.sta.ssid_len) == 0) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002928 if (rx_status->flag & RX_FLAG_TSFT) {
2929 /* in order for correct IBSS merging we need mactime
2930 *
2931 * since mactime is defined as the time the first data
2932 * symbol of the frame hits the PHY, and the timestamp
2933 * of the beacon is defined as "the time that the data
2934 * symbol containing the first bit of the timestamp is
2935 * transmitted to the PHY plus the transmitting STA’s
2936 * delays through its local PHY from the MAC-PHY
2937 * interface to its interface with the WM"
2938 * (802.11 11.1.2) - equals the time this bit arrives at
2939 * the receiver - we have to take into account the
2940 * offset between the two.
2941 * e.g: at 1 MBit that means mactime is 192 usec earlier
2942 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2943 */
2944 int rate = local->hw.wiphy->bands[rx_status->band]->
2945 bitrates[rx_status->rate_idx].bitrate;
2946 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2947 } else if (local && local->ops && local->ops->get_tsf)
2948 /* second best option: get current TSF */
2949 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2950 else
2951 /* can't merge without knowing the TSF */
2952 rx_timestamp = -1LLU;
2953#ifdef CONFIG_MAC80211_IBSS_DEBUG
2954 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2955 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2956 print_mac(mac, mgmt->sa),
2957 print_mac(mac2, mgmt->bssid),
2958 (unsigned long long)rx_timestamp,
2959 (unsigned long long)beacon_timestamp,
2960 (unsigned long long)(rx_timestamp - beacon_timestamp),
2961 jiffies);
2962#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2963 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002964#ifndef CONFIG_MAC80211_IBSS_DEBUG
Pavel Roskind97cf012008-02-21 11:33:58 -05002965 if (net_ratelimit())
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002966#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002967 printk(KERN_DEBUG "%s: beacon TSF higher than "
2968 "local TSF - IBSS merge with BSSID %s\n",
2969 dev->name, print_mac(mac, mgmt->bssid));
2970 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2971 ieee80211_ibss_add_sta(dev, NULL,
Vladimir Koutny87291c02008-06-13 16:50:44 +02002972 mgmt->bssid, mgmt->sa,
2973 BIT(rx_status->rate_idx));
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002974 }
2975 }
2976
Jiri Bencf0706e82007-05-05 11:45:53 -07002977 ieee80211_rx_bss_put(dev, bss);
2978}
2979
2980
2981static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2982 struct ieee80211_mgmt *mgmt,
2983 size_t len,
2984 struct ieee80211_rx_status *rx_status)
2985{
Ester Kummerae6a44e2008-06-27 18:54:48 +03002986 size_t baselen;
2987 struct ieee802_11_elems elems;
2988
2989 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2990 if (baselen > len)
2991 return;
2992
2993 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2994 &elems);
2995
2996 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002997}
2998
2999
3000static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
3001 struct ieee80211_mgmt *mgmt,
3002 size_t len,
3003 struct ieee80211_rx_status *rx_status)
3004{
Jiri Bencf0706e82007-05-05 11:45:53 -07003005 struct ieee80211_sub_if_data *sdata;
3006 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07003007 size_t baselen;
3008 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003009 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3010 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01003011 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003012
Ester Kummerae6a44e2008-06-27 18:54:48 +03003013 /* Process beacon from the current BSS */
3014 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
3015 if (baselen > len)
3016 return;
3017
3018 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
3019
3020 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003021
3022 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003023 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003024 return;
3025 ifsta = &sdata->u.sta;
3026
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003027 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003028 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
3029 return;
3030
Johannes Berg3434fbd2008-05-03 00:59:37 +02003031 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
3032 elems.wmm_param_len);
Reinette Chatred18ef292008-04-09 16:56:15 -07003033
3034 /* Do not send changes to driver if we are scanning. This removes
3035 * requirement that driver's bss_info_changed function needs to be
3036 * atomic. */
3037 if (local->sta_sw_scanning || local->sta_hw_scanning)
3038 return;
3039
Daniel Drake56282212007-07-10 19:32:10 +02003040 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01003041 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
John W. Linville50c4afb2008-04-15 14:09:27 -04003042 else {
3043 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
3044 changed |= ieee80211_handle_protect_preamb(sdata, false,
3045 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
3046 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003047
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003048 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07003049 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003050 struct ieee80211_ht_bss_info bss_info;
3051
3052 ieee80211_ht_addt_info_ie_to_ht_bss_info(
3053 (struct ieee80211_ht_addt_info *)
3054 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07003055 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
3056 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02003057 }
3058
Johannes Berg471b3ef2007-12-28 14:32:58 +01003059 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07003060}
3061
3062
3063static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
3064 struct ieee80211_if_sta *ifsta,
3065 struct ieee80211_mgmt *mgmt,
3066 size_t len,
3067 struct ieee80211_rx_status *rx_status)
3068{
3069 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3070 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3071 int tx_last_beacon;
3072 struct sk_buff *skb;
3073 struct ieee80211_mgmt *resp;
3074 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07003075 DECLARE_MAC_BUF(mac);
3076#ifdef CONFIG_MAC80211_IBSS_DEBUG
3077 DECLARE_MAC_BUF(mac2);
3078 DECLARE_MAC_BUF(mac3);
3079#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003080
Johannes Berg51fb61e2007-12-19 01:31:27 +01003081 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003082 ifsta->state != IEEE80211_IBSS_JOINED ||
3083 len < 24 + 2 || !ifsta->probe_resp)
3084 return;
3085
3086 if (local->ops->tx_last_beacon)
3087 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
3088 else
3089 tx_last_beacon = 1;
3090
3091#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003092 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
3093 "%s (tx_last_beacon=%d)\n",
3094 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
3095 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07003096#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3097
3098 if (!tx_last_beacon)
3099 return;
3100
3101 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
3102 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
3103 return;
3104
3105 end = ((u8 *) mgmt) + len;
3106 pos = mgmt->u.probe_req.variable;
3107 if (pos[0] != WLAN_EID_SSID ||
3108 pos + 2 + pos[1] > end) {
3109 if (net_ratelimit()) {
3110 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07003111 "from %s\n",
3112 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07003113 }
3114 return;
3115 }
3116 if (pos[1] != 0 &&
3117 (pos[1] != ifsta->ssid_len ||
3118 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
3119 /* Ignore ProbeReq for foreign SSID */
3120 return;
3121 }
3122
3123 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02003124 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003125 if (!skb)
3126 return;
3127
3128 resp = (struct ieee80211_mgmt *) skb->data;
3129 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3130#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003131 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
3132 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07003133#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3134 ieee80211_sta_tx(dev, skb, 0);
3135}
3136
Johannes Berg4e20cb22007-12-19 01:31:24 +01003137static void ieee80211_rx_mgmt_action(struct net_device *dev,
3138 struct ieee80211_if_sta *ifsta,
3139 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003140 size_t len,
3141 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003142{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003143 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Assaf Kraussb6623482008-06-16 16:09:49 +03003144 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003145
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003146 if (len < IEEE80211_MIN_ACTION_SIZE)
3147 return;
3148
3149 switch (mgmt->u.action.category) {
Assaf Kraussb6623482008-06-16 16:09:49 +03003150 case WLAN_CATEGORY_SPECTRUM_MGMT:
3151 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
3152 break;
3153 switch (mgmt->u.action.u.chan_switch.action_code) {
3154 case WLAN_ACTION_SPCT_MSR_REQ:
3155 if (len < (IEEE80211_MIN_ACTION_SIZE +
3156 sizeof(mgmt->u.action.u.measurement)))
3157 break;
3158 ieee80211_sta_process_measurement_req(dev, mgmt, len);
3159 break;
3160 }
3161 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003162 case WLAN_CATEGORY_BACK:
3163 switch (mgmt->u.action.u.addba_req.action_code) {
3164 case WLAN_ACTION_ADDBA_REQ:
3165 if (len < (IEEE80211_MIN_ACTION_SIZE +
3166 sizeof(mgmt->u.action.u.addba_req)))
3167 break;
3168 ieee80211_sta_process_addba_request(dev, mgmt, len);
3169 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02003170 case WLAN_ACTION_ADDBA_RESP:
3171 if (len < (IEEE80211_MIN_ACTION_SIZE +
3172 sizeof(mgmt->u.action.u.addba_resp)))
3173 break;
3174 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3175 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02003176 case WLAN_ACTION_DELBA:
3177 if (len < (IEEE80211_MIN_ACTION_SIZE +
3178 sizeof(mgmt->u.action.u.delba)))
3179 break;
3180 ieee80211_sta_process_delba(dev, mgmt, len);
3181 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003182 default:
3183 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02003184 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003185 dev->name);
3186 break;
3187 }
3188 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003189 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003190 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003191 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3192 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003193 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003194 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003195 mesh_rx_path_sel_frame(dev, mgmt, len);
3196 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003197 default:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003198 if (net_ratelimit())
3199 printk(KERN_DEBUG "%s: Rx unknown action frame - "
3200 "category=%d\n", dev->name, mgmt->u.action.category);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003201 break;
3202 }
3203}
Jiri Bencf0706e82007-05-05 11:45:53 -07003204
3205void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3206 struct ieee80211_rx_status *rx_status)
3207{
3208 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3209 struct ieee80211_sub_if_data *sdata;
3210 struct ieee80211_if_sta *ifsta;
3211 struct ieee80211_mgmt *mgmt;
3212 u16 fc;
3213
3214 if (skb->len < 24)
3215 goto fail;
3216
3217 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3218 ifsta = &sdata->u.sta;
3219
3220 mgmt = (struct ieee80211_mgmt *) skb->data;
3221 fc = le16_to_cpu(mgmt->frame_control);
3222
3223 switch (fc & IEEE80211_FCTL_STYPE) {
3224 case IEEE80211_STYPE_PROBE_REQ:
3225 case IEEE80211_STYPE_PROBE_RESP:
3226 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003227 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07003228 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3229 case IEEE80211_STYPE_AUTH:
3230 case IEEE80211_STYPE_ASSOC_RESP:
3231 case IEEE80211_STYPE_REASSOC_RESP:
3232 case IEEE80211_STYPE_DEAUTH:
3233 case IEEE80211_STYPE_DISASSOC:
3234 skb_queue_tail(&ifsta->skb_queue, skb);
3235 queue_work(local->hw.workqueue, &ifsta->work);
3236 return;
3237 default:
3238 printk(KERN_DEBUG "%s: received unknown management frame - "
3239 "stype=%d\n", dev->name,
3240 (fc & IEEE80211_FCTL_STYPE) >> 4);
3241 break;
3242 }
3243
3244 fail:
3245 kfree_skb(skb);
3246}
3247
3248
3249static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3250 struct sk_buff *skb)
3251{
3252 struct ieee80211_rx_status *rx_status;
3253 struct ieee80211_sub_if_data *sdata;
3254 struct ieee80211_if_sta *ifsta;
3255 struct ieee80211_mgmt *mgmt;
3256 u16 fc;
3257
3258 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3259 ifsta = &sdata->u.sta;
3260
3261 rx_status = (struct ieee80211_rx_status *) skb->cb;
3262 mgmt = (struct ieee80211_mgmt *) skb->data;
3263 fc = le16_to_cpu(mgmt->frame_control);
3264
3265 switch (fc & IEEE80211_FCTL_STYPE) {
3266 case IEEE80211_STYPE_PROBE_REQ:
3267 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3268 rx_status);
3269 break;
3270 case IEEE80211_STYPE_PROBE_RESP:
3271 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3272 break;
3273 case IEEE80211_STYPE_BEACON:
3274 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3275 break;
3276 case IEEE80211_STYPE_AUTH:
3277 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3278 break;
3279 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003280 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003281 break;
3282 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003283 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003284 break;
3285 case IEEE80211_STYPE_DEAUTH:
3286 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3287 break;
3288 case IEEE80211_STYPE_DISASSOC:
3289 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3290 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003291 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003292 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003293 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07003294 }
3295
3296 kfree_skb(skb);
3297}
3298
3299
Johannes Berg9ae54c82008-01-31 19:48:20 +01003300ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003301ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3302 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003303{
3304 struct ieee80211_mgmt *mgmt;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003305 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003306
Zhu Yiece8edd2007-11-22 10:53:21 +08003307 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003308 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003309
3310 mgmt = (struct ieee80211_mgmt *) skb->data;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003311 fc = mgmt->frame_control;
Jiri Bencf0706e82007-05-05 11:45:53 -07003312
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003313 if (ieee80211_is_ctl(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +01003314 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003315
3316 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003317 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003318
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003319 if (ieee80211_is_probe_resp(fc)) {
3320 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3321 dev_kfree_skb(skb);
3322 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003323 }
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003324
3325 if (ieee80211_is_beacon(fc)) {
3326 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3327 dev_kfree_skb(skb);
3328 return RX_QUEUED;
3329 }
3330
Johannes Berg9ae54c82008-01-31 19:48:20 +01003331 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003332}
3333
3334
3335static int ieee80211_sta_active_ibss(struct net_device *dev)
3336{
3337 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3338 int active = 0;
3339 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003340 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003341
Johannes Bergd0709a62008-02-25 16:27:46 +01003342 rcu_read_lock();
3343
3344 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3345 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003346 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3347 jiffies)) {
3348 active++;
3349 break;
3350 }
3351 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003352
3353 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003354
3355 return active;
3356}
3357
3358
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003359static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003360{
3361 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3362 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003363 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003364 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003365 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003366
Johannes Bergd0709a62008-02-25 16:27:46 +01003367 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003368 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003369 if (time_after(jiffies, sta->last_rx + exp_time)) {
Joe Perches0795af52007-10-03 17:59:30 -07003370 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3371 dev->name, print_mac(mac, sta->addr));
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003372 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003373 if (sta)
3374 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003375 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003376 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003377
Johannes Bergd0709a62008-02-25 16:27:46 +01003378 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3379 sta_info_destroy(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003380}
3381
3382
3383static void ieee80211_sta_merge_ibss(struct net_device *dev,
3384 struct ieee80211_if_sta *ifsta)
3385{
3386 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3387
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003388 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07003389 if (ieee80211_sta_active_ibss(dev))
3390 return;
3391
3392 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3393 "IBSS networks with same SSID (merge)\n", dev->name);
3394 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3395}
3396
3397
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003398#ifdef CONFIG_MAC80211_MESH
3399static void ieee80211_mesh_housekeeping(struct net_device *dev,
3400 struct ieee80211_if_sta *ifsta)
3401{
3402 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3403 bool free_plinks;
3404
3405 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3406 mesh_path_expire(dev);
3407
3408 free_plinks = mesh_plink_availables(sdata);
3409 if (free_plinks != sdata->u.sta.accepting_plinks)
3410 ieee80211_if_config_beacon(dev);
3411
3412 mod_timer(&ifsta->timer, jiffies +
3413 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3414}
3415
3416
3417void ieee80211_start_mesh(struct net_device *dev)
3418{
3419 struct ieee80211_if_sta *ifsta;
3420 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3421 ifsta = &sdata->u.sta;
3422 ifsta->state = IEEE80211_MESH_UP;
3423 ieee80211_sta_timer((unsigned long)sdata);
3424}
3425#endif
3426
3427
Jiri Bencf0706e82007-05-05 11:45:53 -07003428void ieee80211_sta_timer(unsigned long data)
3429{
3430 struct ieee80211_sub_if_data *sdata =
3431 (struct ieee80211_sub_if_data *) data;
3432 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3433 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3434
3435 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3436 queue_work(local->hw.workqueue, &ifsta->work);
3437}
3438
Jiri Bencf0706e82007-05-05 11:45:53 -07003439void ieee80211_sta_work(struct work_struct *work)
3440{
3441 struct ieee80211_sub_if_data *sdata =
3442 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3443 struct net_device *dev = sdata->dev;
3444 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3445 struct ieee80211_if_sta *ifsta;
3446 struct sk_buff *skb;
3447
3448 if (!netif_running(dev))
3449 return;
3450
Zhu Yiece8edd2007-11-22 10:53:21 +08003451 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003452 return;
3453
Johannes Berg51fb61e2007-12-19 01:31:27 +01003454 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003455 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3456 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003457 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01003458 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e82007-05-05 11:45:53 -07003459 return;
3460 }
3461 ifsta = &sdata->u.sta;
3462
3463 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3464 ieee80211_sta_rx_queued_mgmt(dev, skb);
3465
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003466#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003467 if (ifsta->preq_queue_len &&
3468 time_after(jiffies,
3469 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003470 mesh_path_start_discovery(dev);
3471#endif
3472
Jiri Bencf0706e82007-05-05 11:45:53 -07003473 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3474 ifsta->state != IEEE80211_ASSOCIATE &&
3475 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003476 if (ifsta->scan_ssid_len)
3477 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3478 else
3479 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003480 return;
3481 }
3482
3483 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3484 if (ieee80211_sta_config_auth(dev, ifsta))
3485 return;
3486 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3487 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3488 return;
3489
3490 switch (ifsta->state) {
3491 case IEEE80211_DISABLED:
3492 break;
3493 case IEEE80211_AUTHENTICATE:
3494 ieee80211_authenticate(dev, ifsta);
3495 break;
3496 case IEEE80211_ASSOCIATE:
3497 ieee80211_associate(dev, ifsta);
3498 break;
3499 case IEEE80211_ASSOCIATED:
3500 ieee80211_associated(dev, ifsta);
3501 break;
3502 case IEEE80211_IBSS_SEARCH:
3503 ieee80211_sta_find_ibss(dev, ifsta);
3504 break;
3505 case IEEE80211_IBSS_JOINED:
3506 ieee80211_sta_merge_ibss(dev, ifsta);
3507 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003508#ifdef CONFIG_MAC80211_MESH
3509 case IEEE80211_MESH_UP:
3510 ieee80211_mesh_housekeeping(dev, ifsta);
3511 break;
3512#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003513 default:
3514 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3515 ifsta->state);
3516 break;
3517 }
3518
3519 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3520 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3521 "mixed-cell disabled - disassociate\n", dev->name);
3522
3523 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3524 ieee80211_set_disassoc(dev, ifsta, 0);
3525 }
3526}
3527
3528
3529static void ieee80211_sta_reset_auth(struct net_device *dev,
3530 struct ieee80211_if_sta *ifsta)
3531{
3532 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3533
3534 if (local->ops->reset_tsf) {
3535 /* Reset own TSF to allow time synchronization work. */
3536 local->ops->reset_tsf(local_to_hw(local));
3537 }
3538
3539 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3540
3541
3542 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3543 ifsta->auth_alg = WLAN_AUTH_OPEN;
3544 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3545 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3546 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3547 ifsta->auth_alg = WLAN_AUTH_LEAP;
3548 else
3549 ifsta->auth_alg = WLAN_AUTH_OPEN;
3550 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3551 ifsta->auth_alg);
3552 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003553 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3554 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003555 netif_carrier_off(dev);
3556}
3557
3558
3559void ieee80211_sta_req_auth(struct net_device *dev,
3560 struct ieee80211_if_sta *ifsta)
3561{
3562 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3563 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3564
Johannes Berg51fb61e2007-12-19 01:31:27 +01003565 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003566 return;
3567
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003568 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3569 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3570 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3571 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003572 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3573 queue_work(local->hw.workqueue, &ifsta->work);
3574 }
3575}
3576
3577static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3578 const char *ssid, int ssid_len)
3579{
3580 int tmp, hidden_ssid;
3581
Michael Wu48225702007-10-19 17:14:36 -04003582 if (ssid_len == ifsta->ssid_len &&
3583 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003584 return 1;
3585
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003586 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003587 return 0;
3588
3589 hidden_ssid = 1;
3590 tmp = ssid_len;
3591 while (tmp--) {
3592 if (ssid[tmp] != '\0') {
3593 hidden_ssid = 0;
3594 break;
3595 }
3596 }
3597
3598 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3599 return 1;
3600
3601 if (ssid_len == 1 && ssid[0] == ' ')
3602 return 1;
3603
3604 return 0;
3605}
3606
3607static int ieee80211_sta_config_auth(struct net_device *dev,
3608 struct ieee80211_if_sta *ifsta)
3609{
3610 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3611 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3612 struct ieee80211_sta_bss *bss, *selected = NULL;
3613 int top_rssi = 0, freq;
3614
Jiri Bencf0706e82007-05-05 11:45:53 -07003615 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003616 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003617 list_for_each_entry(bss, &local->sta_bss_list, list) {
3618 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3619 continue;
3620
Abhijeet Kolekar34a961f2008-05-09 09:35:41 -07003621 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3622 IEEE80211_STA_AUTO_BSSID_SEL |
3623 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3624 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3625 !!sdata->default_key))
Jiri Bencf0706e82007-05-05 11:45:53 -07003626 continue;
3627
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003628 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3629 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003630 continue;
3631
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003632 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003633 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3634 continue;
3635
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003636 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003637 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3638 continue;
3639
Bruno Randolf566bfe52008-05-08 19:15:40 +02003640 if (!selected || top_rssi < bss->signal) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003641 selected = bss;
Bruno Randolf566bfe52008-05-08 19:15:40 +02003642 top_rssi = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07003643 }
3644 }
3645 if (selected)
3646 atomic_inc(&selected->users);
3647 spin_unlock_bh(&local->sta_bss_lock);
3648
3649 if (selected) {
Assaf Kraussbe038b32008-06-05 19:55:21 +03003650 ieee80211_set_freq(dev, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003651 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003652 ieee80211_sta_set_ssid(dev, selected->ssid,
3653 selected->ssid_len);
3654 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003655 ieee80211_sta_def_wmm_params(dev, selected, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003656 ieee80211_rx_bss_put(dev, selected);
3657 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003658 ieee80211_sta_reset_auth(dev, ifsta);
3659 return 0;
3660 } else {
3661 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003662 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003663 ieee80211_sta_start_scan(dev, NULL, 0);
3664 else
3665 ieee80211_sta_start_scan(dev, ifsta->ssid,
3666 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003667 ifsta->state = IEEE80211_AUTHENTICATE;
3668 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3669 } else
3670 ifsta->state = IEEE80211_DISABLED;
3671 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003672 return -1;
3673}
3674
Jiri Bencf0706e82007-05-05 11:45:53 -07003675
3676static int ieee80211_sta_create_ibss(struct net_device *dev,
3677 struct ieee80211_if_sta *ifsta)
3678{
3679 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3680 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003681 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003682 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003683 u8 bssid[ETH_ALEN], *pos;
3684 int i;
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003685 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003686 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003687
3688#if 0
3689 /* Easier testing, use fixed BSSID. */
3690 memset(bssid, 0xfe, ETH_ALEN);
3691#else
3692 /* Generate random, not broadcast, locally administered BSSID. Mix in
3693 * own MAC address to make sure that devices that do not have proper
3694 * random number generator get different BSSID. */
3695 get_random_bytes(bssid, ETH_ALEN);
3696 for (i = 0; i < ETH_ALEN; i++)
3697 bssid[i] ^= dev->dev_addr[i];
3698 bssid[0] &= ~0x01;
3699 bssid[0] |= 0x02;
3700#endif
3701
Joe Perches0795af52007-10-03 17:59:30 -07003702 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3703 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003704
Johannes Berg8318d782008-01-24 19:38:38 +01003705 bss = ieee80211_rx_bss_add(dev, bssid,
3706 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003707 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003708 if (!bss)
3709 return -ENOMEM;
3710
Johannes Berg8318d782008-01-24 19:38:38 +01003711 bss->band = local->hw.conf.channel->band;
3712 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003713
3714 if (local->hw.conf.beacon_int == 0)
Tomas Winklerdc0ae302008-06-12 22:38:37 +03003715 local->hw.conf.beacon_int = 100;
Jiri Bencf0706e82007-05-05 11:45:53 -07003716 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003717 bss->last_update = jiffies;
3718 bss->capability = WLAN_CAPABILITY_IBSS;
Johannes Berg988c0f72008-04-17 19:21:22 +02003719
3720 if (sdata->default_key)
Jiri Bencf0706e82007-05-05 11:45:53 -07003721 bss->capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +02003722 else
Jiri Bencf0706e82007-05-05 11:45:53 -07003723 sdata->drop_unencrypted = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02003724
Johannes Berg8318d782008-01-24 19:38:38 +01003725 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003726 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003727 for (i = 0; i < sband->n_bitrates; i++) {
3728 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003729 *pos++ = (u8) (rate / 5);
3730 }
3731
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003732 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3733 ieee80211_rx_bss_put(dev, bss);
3734 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003735}
3736
3737
3738static int ieee80211_sta_find_ibss(struct net_device *dev,
3739 struct ieee80211_if_sta *ifsta)
3740{
3741 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3742 struct ieee80211_sta_bss *bss;
3743 int found = 0;
3744 u8 bssid[ETH_ALEN];
3745 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003746 DECLARE_MAC_BUF(mac);
3747 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003748
3749 if (ifsta->ssid_len == 0)
3750 return -EINVAL;
3751
3752 active_ibss = ieee80211_sta_active_ibss(dev);
3753#ifdef CONFIG_MAC80211_IBSS_DEBUG
3754 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3755 dev->name, active_ibss);
3756#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3757 spin_lock_bh(&local->sta_bss_lock);
3758 list_for_each_entry(bss, &local->sta_bss_list, list) {
3759 if (ifsta->ssid_len != bss->ssid_len ||
3760 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3761 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3762 continue;
3763#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003764 printk(KERN_DEBUG " bssid=%s found\n",
3765 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003766#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3767 memcpy(bssid, bss->bssid, ETH_ALEN);
3768 found = 1;
3769 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3770 break;
3771 }
3772 spin_unlock_bh(&local->sta_bss_lock);
3773
3774#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003775 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3776 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003777#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3778 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003779 (bss = ieee80211_rx_bss_get(dev, bssid,
3780 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003781 ifsta->ssid, ifsta->ssid_len))) {
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003782 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003783 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003784 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003785 dev->name, print_mac(mac, bssid));
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003786 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
3787 ieee80211_rx_bss_put(dev, bss);
3788 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003789 }
3790#ifdef CONFIG_MAC80211_IBSS_DEBUG
3791 printk(KERN_DEBUG " did not try to join ibss\n");
3792#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3793
3794 /* Selected IBSS not found in current scan results - try to scan */
3795 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3796 !ieee80211_sta_active_ibss(dev)) {
3797 mod_timer(&ifsta->timer, jiffies +
3798 IEEE80211_IBSS_MERGE_INTERVAL);
3799 } else if (time_after(jiffies, local->last_scan_completed +
3800 IEEE80211_SCAN_INTERVAL)) {
3801 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3802 "join\n", dev->name);
3803 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3804 ifsta->ssid_len);
3805 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3806 int interval = IEEE80211_SCAN_INTERVAL;
3807
3808 if (time_after(jiffies, ifsta->ibss_join_req +
3809 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003810 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003811 (!(local->oper_channel->flags &
3812 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003813 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003814 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003815 printk(KERN_DEBUG "%s: IBSS not allowed on"
3816 " %d MHz\n", dev->name,
3817 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003818 }
3819
3820 /* No IBSS found - decrease scan interval and continue
3821 * scanning. */
3822 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3823 }
3824
3825 ifsta->state = IEEE80211_IBSS_SEARCH;
3826 mod_timer(&ifsta->timer, jiffies + interval);
3827 return 0;
3828 }
3829
3830 return 0;
3831}
3832
3833
3834int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3835{
Johannes Berg8318d782008-01-24 19:38:38 +01003836 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003837 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07003838
3839 if (len > IEEE80211_MAX_SSID_LEN)
3840 return -EINVAL;
3841
Jiri Bencf0706e82007-05-05 11:45:53 -07003842 ifsta = &sdata->u.sta;
3843
3844 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003845 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003846 memcpy(ifsta->ssid, ssid, len);
3847 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3848 ifsta->ssid_len = len;
3849
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003850 if (len)
3851 ifsta->flags |= IEEE80211_STA_SSID_SET;
3852 else
3853 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003854 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003855 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003856 ifsta->ibss_join_req = jiffies;
3857 ifsta->state = IEEE80211_IBSS_SEARCH;
3858 return ieee80211_sta_find_ibss(dev, ifsta);
3859 }
3860 return 0;
3861}
3862
3863
3864int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3865{
3866 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3867 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3868 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3869 *len = ifsta->ssid_len;
3870 return 0;
3871}
3872
3873
3874int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3875{
3876 struct ieee80211_sub_if_data *sdata;
3877 struct ieee80211_if_sta *ifsta;
3878 int res;
3879
3880 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3881 ifsta = &sdata->u.sta;
3882
3883 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3884 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3885 res = ieee80211_if_config(dev);
3886 if (res) {
3887 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3888 "the low-level driver\n", dev->name);
3889 return res;
3890 }
3891 }
3892
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003893 if (is_valid_ether_addr(bssid))
3894 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003895 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003896 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3897
Jiri Bencf0706e82007-05-05 11:45:53 -07003898 return 0;
3899}
3900
3901
3902static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3903 struct ieee80211_sub_if_data *sdata,
3904 int powersave)
3905{
3906 struct sk_buff *skb;
3907 struct ieee80211_hdr *nullfunc;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003908 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003909
3910 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3911 if (!skb) {
3912 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3913 "frame\n", sdata->dev->name);
3914 return;
3915 }
3916 skb_reserve(skb, local->hw.extra_tx_headroom);
3917
3918 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3919 memset(nullfunc, 0, 24);
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003920 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3921 IEEE80211_FCTL_TODS);
Jiri Bencf0706e82007-05-05 11:45:53 -07003922 if (powersave)
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003923 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3924 nullfunc->frame_control = fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003925 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3926 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3927 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3928
3929 ieee80211_sta_tx(sdata->dev, skb, 0);
3930}
3931
3932
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003933static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3934{
3935 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3936 ieee80211_vif_is_mesh(&sdata->vif))
3937 ieee80211_sta_timer((unsigned long)sdata);
3938}
3939
Jiri Bencf0706e82007-05-05 11:45:53 -07003940void ieee80211_scan_completed(struct ieee80211_hw *hw)
3941{
3942 struct ieee80211_local *local = hw_to_local(hw);
3943 struct net_device *dev = local->scan_dev;
3944 struct ieee80211_sub_if_data *sdata;
3945 union iwreq_data wrqu;
3946
3947 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003948 memset(&wrqu, 0, sizeof(wrqu));
3949 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003950
Zhu Yiece8edd2007-11-22 10:53:21 +08003951 if (local->sta_hw_scanning) {
3952 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003953 if (ieee80211_hw_config(local))
3954 printk(KERN_DEBUG "%s: failed to restore operational "
3955 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003956 /* Restart STA timer for HW scan case */
3957 rcu_read_lock();
3958 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3959 ieee80211_restart_sta_timer(sdata);
3960 rcu_read_unlock();
3961
Zhu Yiece8edd2007-11-22 10:53:21 +08003962 goto done;
3963 }
3964
3965 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003966 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003967 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003968 "channel after scan\n", dev->name);
3969
Johannes Berg4150c572007-09-17 01:29:23 -04003970
3971 netif_tx_lock_bh(local->mdev);
3972 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3973 local->ops->configure_filter(local_to_hw(local),
3974 FIF_BCN_PRBRESP_PROMISC,
3975 &local->filter_flags,
3976 local->mdev->mc_count,
3977 local->mdev->mc_list);
3978
3979 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003980
Johannes Berg79010422007-09-18 17:29:21 -04003981 rcu_read_lock();
3982 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003983
3984 /* No need to wake the master device. */
3985 if (sdata->dev == local->mdev)
3986 continue;
3987
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003988 /* Tell AP we're back */
3989 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3990 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3991 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003992
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003993 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003994
Jiri Bencf0706e82007-05-05 11:45:53 -07003995 netif_wake_queue(sdata->dev);
3996 }
Johannes Berg79010422007-09-18 17:29:21 -04003997 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003998
Zhu Yiece8edd2007-11-22 10:53:21 +08003999done:
Jiri Bencf0706e82007-05-05 11:45:53 -07004000 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01004001 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004002 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004003 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07004004 (!ifsta->state == IEEE80211_IBSS_JOINED &&
4005 !ieee80211_sta_active_ibss(dev)))
4006 ieee80211_sta_find_ibss(dev, ifsta);
4007 }
4008}
4009EXPORT_SYMBOL(ieee80211_scan_completed);
4010
4011void ieee80211_sta_scan_work(struct work_struct *work)
4012{
4013 struct ieee80211_local *local =
4014 container_of(work, struct ieee80211_local, scan_work.work);
4015 struct net_device *dev = local->scan_dev;
4016 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01004017 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07004018 struct ieee80211_channel *chan;
4019 int skip;
4020 unsigned long next_delay = 0;
4021
Zhu Yiece8edd2007-11-22 10:53:21 +08004022 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07004023 return;
4024
4025 switch (local->scan_state) {
4026 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01004027 /*
4028 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
4029 * after we successfully scanned the last channel of the last
4030 * band (and the last band is supported by the hw)
4031 */
Johannes Berg8318d782008-01-24 19:38:38 +01004032 if (local->scan_band < IEEE80211_NUM_BANDS)
4033 sband = local->hw.wiphy->bands[local->scan_band];
4034 else
4035 sband = NULL;
4036
Johannes Berg69d464d2008-01-28 17:08:56 +01004037 /*
4038 * If we are at an unsupported band and have more bands
4039 * left to scan, advance to the next supported one.
4040 */
4041 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01004042 local->scan_band++;
4043 sband = local->hw.wiphy->bands[local->scan_band];
4044 local->scan_channel_idx = 0;
4045 }
4046
Johannes Berg69d464d2008-01-28 17:08:56 +01004047 /* if no more bands/channels left, complete scan */
4048 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004049 ieee80211_scan_completed(local_to_hw(local));
4050 return;
4051 }
Johannes Berg8318d782008-01-24 19:38:38 +01004052 skip = 0;
4053 chan = &sband->channels[local->scan_channel_idx];
4054
4055 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01004056 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01004057 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07004058 skip = 1;
4059
4060 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004061 local->scan_channel = chan;
4062 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004063 printk(KERN_DEBUG "%s: failed to set freq to "
4064 "%d MHz for scan\n", dev->name,
4065 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07004066 skip = 1;
4067 }
4068 }
4069
Johannes Berg69d464d2008-01-28 17:08:56 +01004070 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07004071 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01004072 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01004073 /*
4074 * scan_band may end up == IEEE80211_NUM_BANDS, but
4075 * we'll catch that case above and complete the scan
4076 * if that is the case.
4077 */
Johannes Berg8318d782008-01-24 19:38:38 +01004078 local->scan_band++;
4079 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07004080 }
4081
4082 if (skip)
4083 break;
4084
4085 next_delay = IEEE80211_PROBE_DELAY +
4086 usecs_to_jiffies(local->hw.channel_change_time);
4087 local->scan_state = SCAN_SEND_PROBE;
4088 break;
4089 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01004090 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07004091 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01004092
4093 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
4094 break;
4095 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
4096 local->scan_ssid_len);
4097 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07004098 break;
4099 }
4100
Zhu Yiece8edd2007-11-22 10:53:21 +08004101 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07004102 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4103 next_delay);
4104}
4105
4106
4107static int ieee80211_sta_start_scan(struct net_device *dev,
4108 u8 *ssid, size_t ssid_len)
4109{
4110 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4111 struct ieee80211_sub_if_data *sdata;
4112
4113 if (ssid_len > IEEE80211_MAX_SSID_LEN)
4114 return -EINVAL;
4115
4116 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
4117 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
4118 * BSSID: MACAddress
4119 * SSID
4120 * ScanType: ACTIVE, PASSIVE
4121 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
4122 * a Probe frame during active scanning
4123 * ChannelList
4124 * MinChannelTime (>= ProbeDelay), in TU
4125 * MaxChannelTime: (>= MinChannelTime), in TU
4126 */
4127
4128 /* MLME-SCAN.confirm
4129 * BSSDescriptionSet
4130 * ResultCode: SUCCESS, INVALID_PARAMETERS
4131 */
4132
Zhu Yiece8edd2007-11-22 10:53:21 +08004133 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004134 if (local->scan_dev == dev)
4135 return 0;
4136 return -EBUSY;
4137 }
4138
4139 if (local->ops->hw_scan) {
4140 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08004141 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07004142 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08004143 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07004144 local->scan_dev = dev;
4145 }
4146 return rc;
4147 }
4148
Zhu Yiece8edd2007-11-22 10:53:21 +08004149 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07004150
Johannes Berg79010422007-09-18 17:29:21 -04004151 rcu_read_lock();
4152 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02004153
4154 /* Don't stop the master interface, otherwise we can't transmit
4155 * probes! */
4156 if (sdata->dev == local->mdev)
4157 continue;
4158
Jiri Bencf0706e82007-05-05 11:45:53 -07004159 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01004160 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004161 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004162 ieee80211_send_nullfunc(local, sdata, 1);
4163 }
Johannes Berg79010422007-09-18 17:29:21 -04004164 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07004165
4166 if (ssid) {
4167 local->scan_ssid_len = ssid_len;
4168 memcpy(local->scan_ssid, ssid, ssid_len);
4169 } else
4170 local->scan_ssid_len = 0;
4171 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07004172 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004173 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07004174 local->scan_dev = dev;
4175
Johannes Berg4150c572007-09-17 01:29:23 -04004176 netif_tx_lock_bh(local->mdev);
4177 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4178 local->ops->configure_filter(local_to_hw(local),
4179 FIF_BCN_PRBRESP_PROMISC,
4180 &local->filter_flags,
4181 local->mdev->mc_count,
4182 local->mdev->mc_list);
4183 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07004184
4185 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4186 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4187 IEEE80211_CHANNEL_TIME);
4188
4189 return 0;
4190}
4191
4192
4193int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4194{
4195 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4196 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4197 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4198
Johannes Berg51fb61e2007-12-19 01:31:27 +01004199 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004200 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4201
Zhu Yiece8edd2007-11-22 10:53:21 +08004202 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004203 if (local->scan_dev == dev)
4204 return 0;
4205 return -EBUSY;
4206 }
4207
Helmut Schaaa0af5f12007-11-09 16:25:08 +01004208 ifsta->scan_ssid_len = ssid_len;
4209 if (ssid_len)
4210 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07004211 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4212 queue_work(local->hw.workqueue, &ifsta->work);
4213 return 0;
4214}
4215
4216static char *
4217ieee80211_sta_scan_result(struct net_device *dev,
David S. Millerccc58052008-06-16 18:50:49 -07004218 struct iw_request_info *info,
Jiri Bencf0706e82007-05-05 11:45:53 -07004219 struct ieee80211_sta_bss *bss,
4220 char *current_ev, char *end_buf)
4221{
4222 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4223 struct iw_event iwe;
4224
4225 if (time_after(jiffies,
4226 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4227 return current_ev;
4228
Jiri Bencf0706e82007-05-05 11:45:53 -07004229 memset(&iwe, 0, sizeof(iwe));
4230 iwe.cmd = SIOCGIWAP;
4231 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4232 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07004233 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004234 IW_EV_ADDR_LEN);
4235
4236 memset(&iwe, 0, sizeof(iwe));
4237 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01004238 if (bss_mesh_cfg(bss)) {
4239 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004240 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004241 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4242 &iwe, bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004243 } else {
4244 iwe.u.data.length = bss->ssid_len;
4245 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004246 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4247 &iwe, bss->ssid);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004248 }
Jiri Bencf0706e82007-05-05 11:45:53 -07004249
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08004250 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4251 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004252 memset(&iwe, 0, sizeof(iwe));
4253 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01004254 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004255 iwe.u.mode = IW_MODE_MESH;
4256 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004257 iwe.u.mode = IW_MODE_MASTER;
4258 else
4259 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07004260 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4261 &iwe, IW_EV_UINT_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -07004262 }
4263
4264 memset(&iwe, 0, sizeof(iwe));
4265 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01004266 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4267 iwe.u.freq.e = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004268 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004269 IW_EV_FREQ_LEN);
4270
4271 memset(&iwe, 0, sizeof(iwe));
Tomas Winkler9381be02008-05-23 01:36:36 +03004272 iwe.cmd = SIOCGIWFREQ;
4273 iwe.u.freq.m = bss->freq;
4274 iwe.u.freq.e = 6;
David S. Millerccc58052008-06-16 18:50:49 -07004275 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Tomas Winkler9381be02008-05-23 01:36:36 +03004276 IW_EV_FREQ_LEN);
4277 memset(&iwe, 0, sizeof(iwe));
Jiri Bencf0706e82007-05-05 11:45:53 -07004278 iwe.cmd = IWEVQUAL;
Bruno Randolf566bfe52008-05-08 19:15:40 +02004279 iwe.u.qual.qual = bss->qual;
4280 iwe.u.qual.level = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07004281 iwe.u.qual.noise = bss->noise;
4282 iwe.u.qual.updated = local->wstats_flags;
David S. Millerccc58052008-06-16 18:50:49 -07004283 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004284 IW_EV_QUAL_LEN);
4285
4286 memset(&iwe, 0, sizeof(iwe));
4287 iwe.cmd = SIOCGIWENCODE;
4288 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4289 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4290 else
4291 iwe.u.data.flags = IW_ENCODE_DISABLED;
4292 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004293 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4294 &iwe, "");
Jiri Bencf0706e82007-05-05 11:45:53 -07004295
4296 if (bss && bss->wpa_ie) {
4297 memset(&iwe, 0, sizeof(iwe));
4298 iwe.cmd = IWEVGENIE;
4299 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004300 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4301 &iwe, bss->wpa_ie);
Jiri Bencf0706e82007-05-05 11:45:53 -07004302 }
4303
4304 if (bss && bss->rsn_ie) {
4305 memset(&iwe, 0, sizeof(iwe));
4306 iwe.cmd = IWEVGENIE;
4307 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004308 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4309 &iwe, bss->rsn_ie);
Jiri Bencf0706e82007-05-05 11:45:53 -07004310 }
4311
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004312 if (bss && bss->ht_ie) {
4313 memset(&iwe, 0, sizeof(iwe));
4314 iwe.cmd = IWEVGENIE;
4315 iwe.u.data.length = bss->ht_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004316 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4317 &iwe, bss->ht_ie);
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004318 }
4319
Jiri Bencf0706e82007-05-05 11:45:53 -07004320 if (bss && bss->supp_rates_len > 0) {
4321 /* display all supported rates in readable format */
David S. Millerccc58052008-06-16 18:50:49 -07004322 char *p = current_ev + iwe_stream_lcp_len(info);
Jiri Bencf0706e82007-05-05 11:45:53 -07004323 int i;
4324
4325 memset(&iwe, 0, sizeof(iwe));
4326 iwe.cmd = SIOCGIWRATE;
4327 /* Those two flags are ignored... */
4328 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4329
4330 for (i = 0; i < bss->supp_rates_len; i++) {
4331 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4332 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07004333 p = iwe_stream_add_value(info, current_ev, p,
Jiri Bencf0706e82007-05-05 11:45:53 -07004334 end_buf, &iwe, IW_EV_PARAM_LEN);
4335 }
4336 current_ev = p;
4337 }
4338
4339 if (bss) {
4340 char *buf;
4341 buf = kmalloc(30, GFP_ATOMIC);
4342 if (buf) {
4343 memset(&iwe, 0, sizeof(iwe));
4344 iwe.cmd = IWEVCUSTOM;
4345 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4346 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004347 current_ev = iwe_stream_add_point(info, current_ev,
4348 end_buf,
Jiri Bencf0706e82007-05-05 11:45:53 -07004349 &iwe, buf);
Emmanuel Grumbachbf998f62008-06-24 13:38:00 +03004350 memset(&iwe, 0, sizeof(iwe));
4351 iwe.cmd = IWEVCUSTOM;
4352 sprintf(buf, " Last beacon: %dms ago",
4353 jiffies_to_msecs(jiffies - bss->last_update));
4354 iwe.u.data.length = strlen(buf);
4355 current_ev = iwe_stream_add_point(info, current_ev,
4356 end_buf, &iwe, buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004357 kfree(buf);
4358 }
4359 }
4360
Johannes Berg902acc72008-02-23 15:17:19 +01004361 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004362 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04004363 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004364 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004365 if (buf) {
4366 memset(&iwe, 0, sizeof(iwe));
4367 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04004368 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004369 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004370 current_ev = iwe_stream_add_point(info, current_ev,
4371 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004372 &iwe, buf);
4373 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04004374 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4375 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004376 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004377 current_ev = iwe_stream_add_point(info, current_ev,
4378 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004379 &iwe, buf);
4380 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04004381 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4382 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004383 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004384 current_ev = iwe_stream_add_point(info, current_ev,
4385 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004386 &iwe, buf);
4387 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04004388 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4389 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004390 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004391 current_ev = iwe_stream_add_point(info, current_ev,
4392 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004393 &iwe, buf);
4394 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04004395 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4396 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004397 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004398 current_ev = iwe_stream_add_point(info, current_ev,
4399 end_buf,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004400 &iwe, buf);
4401 kfree(buf);
4402 }
4403 }
4404
Jiri Bencf0706e82007-05-05 11:45:53 -07004405 return current_ev;
4406}
4407
4408
David S. Millerccc58052008-06-16 18:50:49 -07004409int ieee80211_sta_scan_results(struct net_device *dev,
4410 struct iw_request_info *info,
4411 char *buf, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07004412{
4413 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4414 char *current_ev = buf;
4415 char *end_buf = buf + len;
4416 struct ieee80211_sta_bss *bss;
4417
4418 spin_lock_bh(&local->sta_bss_lock);
4419 list_for_each_entry(bss, &local->sta_bss_list, list) {
4420 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4421 spin_unlock_bh(&local->sta_bss_lock);
4422 return -E2BIG;
4423 }
David S. Millerccc58052008-06-16 18:50:49 -07004424 current_ev = ieee80211_sta_scan_result(dev, info, bss,
4425 current_ev, end_buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004426 }
4427 spin_unlock_bh(&local->sta_bss_lock);
4428 return current_ev - buf;
4429}
4430
4431
4432int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4433{
4434 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4435 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg988c0f72008-04-17 19:21:22 +02004436
Jiri Bencf0706e82007-05-05 11:45:53 -07004437 kfree(ifsta->extra_ie);
4438 if (len == 0) {
4439 ifsta->extra_ie = NULL;
4440 ifsta->extra_ie_len = 0;
4441 return 0;
4442 }
4443 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4444 if (!ifsta->extra_ie) {
4445 ifsta->extra_ie_len = 0;
4446 return -ENOMEM;
4447 }
4448 memcpy(ifsta->extra_ie, ie, len);
4449 ifsta->extra_ie_len = len;
4450 return 0;
4451}
4452
4453
Johannes Berg988c0f72008-04-17 19:21:22 +02004454struct sta_info *ieee80211_ibss_add_sta(struct net_device *dev,
4455 struct sk_buff *skb, u8 *bssid,
Vladimir Koutny87291c02008-06-13 16:50:44 +02004456 u8 *addr, u64 supp_rates)
Jiri Bencf0706e82007-05-05 11:45:53 -07004457{
4458 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4459 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004460 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004461 DECLARE_MAC_BUF(mac);
Vladimir Koutny87291c02008-06-13 16:50:44 +02004462 int band = local->hw.conf.channel->band;
Jiri Bencf0706e82007-05-05 11:45:53 -07004463
4464 /* TODO: Could consider removing the least recently used entry and
4465 * allow new one to be added. */
4466 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4467 if (net_ratelimit()) {
4468 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004469 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004470 }
4471 return NULL;
4472 }
4473
Vladimir Koutny87291c02008-06-13 16:50:44 +02004474 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid))
4475 return NULL;
4476
Joe Perches0795af52007-10-03 17:59:30 -07004477 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004478 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07004479
Johannes Berg73651ee2008-02-25 16:27:47 +01004480 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4481 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004482 return NULL;
4483
Johannes Berg07346f812008-05-03 01:02:02 +02004484 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
Johannes Berg238814f2008-01-28 17:19:37 +01004485
Vladimir Koutny87291c02008-06-13 16:50:44 +02004486 if (supp_rates)
4487 sta->supp_rates[band] = supp_rates;
4488 else
4489 sta->supp_rates[band] = sdata->u.sta.supp_rates_bits[band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004490
4491 rate_control_rate_init(sta, local);
4492
Johannes Berg93e5deb2008-04-01 15:21:00 +02004493 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004494 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004495
Johannes Bergd0709a62008-02-25 16:27:46 +01004496 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004497}
4498
4499
4500int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4501{
4502 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4503 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4504
4505 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4506 dev->name, reason);
4507
Johannes Berg51fb61e2007-12-19 01:31:27 +01004508 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4509 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004510 return -EINVAL;
4511
4512 ieee80211_send_deauth(dev, ifsta, reason);
4513 ieee80211_set_disassoc(dev, ifsta, 1);
4514 return 0;
4515}
4516
4517
4518int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4519{
4520 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4521 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4522
4523 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4524 dev->name, reason);
4525
Johannes Berg51fb61e2007-12-19 01:31:27 +01004526 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004527 return -EINVAL;
4528
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004529 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004530 return -1;
4531
4532 ieee80211_send_disassoc(dev, ifsta, reason);
4533 ieee80211_set_disassoc(dev, ifsta, 0);
4534 return 0;
4535}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004536
4537void ieee80211_notify_mac(struct ieee80211_hw *hw,
4538 enum ieee80211_notification_types notif_type)
4539{
4540 struct ieee80211_local *local = hw_to_local(hw);
4541 struct ieee80211_sub_if_data *sdata;
4542
4543 switch (notif_type) {
4544 case IEEE80211_NOTIFY_RE_ASSOC:
4545 rcu_read_lock();
4546 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4547
4548 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4549 ieee80211_sta_req_auth(sdata->dev,
4550 &sdata->u.sta);
4551 }
4552
4553 }
4554 rcu_read_unlock();
4555 break;
4556 }
4557}
4558EXPORT_SYMBOL(ieee80211_notify_mac);