blob: 64d710a88b864586f1d4f54fa2d9dc4b1bc40f9b [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);
Johannes Berg3e122be2008-07-09 14:40:34 +020081static void ieee80211_rx_bss_put(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -070082 struct ieee80211_sta_bss *bss);
83static int ieee80211_sta_find_ibss(struct net_device *dev,
84 struct ieee80211_if_sta *ifsta);
85static int ieee80211_sta_wep_configured(struct net_device *dev);
86static int ieee80211_sta_start_scan(struct net_device *dev,
87 u8 *ssid, size_t ssid_len);
88static int ieee80211_sta_config_auth(struct net_device *dev,
89 struct ieee80211_if_sta *ifsta);
Adrian Bunk712590d2008-04-21 11:47:51 +030090static void sta_rx_agg_session_timer_expired(unsigned long data);
Jiri Bencf0706e82007-05-05 11:45:53 -070091
92
Luis Carlos Coboee385852008-02-23 15:17:11 +010093void ieee802_11_parse_elems(u8 *start, size_t len,
94 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -070095{
96 size_t left = len;
97 u8 *pos = start;
Jiri Bencf0706e82007-05-05 11:45:53 -070098
99 memset(elems, 0, sizeof(*elems));
100
101 while (left >= 2) {
102 u8 id, elen;
103
104 id = *pos++;
105 elen = *pos++;
106 left -= 2;
107
John W. Linville67a4cce2007-10-12 16:40:37 -0400108 if (elen > left)
109 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700110
111 switch (id) {
112 case WLAN_EID_SSID:
113 elems->ssid = pos;
114 elems->ssid_len = elen;
115 break;
116 case WLAN_EID_SUPP_RATES:
117 elems->supp_rates = pos;
118 elems->supp_rates_len = elen;
119 break;
120 case WLAN_EID_FH_PARAMS:
121 elems->fh_params = pos;
122 elems->fh_params_len = elen;
123 break;
124 case WLAN_EID_DS_PARAMS:
125 elems->ds_params = pos;
126 elems->ds_params_len = elen;
127 break;
128 case WLAN_EID_CF_PARAMS:
129 elems->cf_params = pos;
130 elems->cf_params_len = elen;
131 break;
132 case WLAN_EID_TIM:
133 elems->tim = pos;
134 elems->tim_len = elen;
135 break;
136 case WLAN_EID_IBSS_PARAMS:
137 elems->ibss_params = pos;
138 elems->ibss_params_len = elen;
139 break;
140 case WLAN_EID_CHALLENGE:
141 elems->challenge = pos;
142 elems->challenge_len = elen;
143 break;
144 case WLAN_EID_WPA:
145 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
146 pos[2] == 0xf2) {
147 /* Microsoft OUI (00:50:F2) */
148 if (pos[3] == 1) {
149 /* OUI Type 1 - WPA IE */
150 elems->wpa = pos;
151 elems->wpa_len = elen;
152 } else if (elen >= 5 && pos[3] == 2) {
153 if (pos[4] == 0) {
154 elems->wmm_info = pos;
155 elems->wmm_info_len = elen;
156 } else if (pos[4] == 1) {
157 elems->wmm_param = pos;
158 elems->wmm_param_len = elen;
159 }
160 }
161 }
162 break;
163 case WLAN_EID_RSN:
164 elems->rsn = pos;
165 elems->rsn_len = elen;
166 break;
167 case WLAN_EID_ERP_INFO:
168 elems->erp_info = pos;
169 elems->erp_info_len = elen;
170 break;
171 case WLAN_EID_EXT_SUPP_RATES:
172 elems->ext_supp_rates = pos;
173 elems->ext_supp_rates_len = elen;
174 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200175 case WLAN_EID_HT_CAPABILITY:
176 elems->ht_cap_elem = pos;
177 elems->ht_cap_elem_len = elen;
178 break;
179 case WLAN_EID_HT_EXTRA_INFO:
180 elems->ht_info_elem = pos;
181 elems->ht_info_elem_len = elen;
182 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100183 case WLAN_EID_MESH_ID:
184 elems->mesh_id = pos;
185 elems->mesh_id_len = elen;
186 break;
187 case WLAN_EID_MESH_CONFIG:
188 elems->mesh_config = pos;
189 elems->mesh_config_len = elen;
190 break;
191 case WLAN_EID_PEER_LINK:
192 elems->peer_link = pos;
193 elems->peer_link_len = elen;
194 break;
195 case WLAN_EID_PREQ:
196 elems->preq = pos;
197 elems->preq_len = elen;
198 break;
199 case WLAN_EID_PREP:
200 elems->prep = pos;
201 elems->prep_len = elen;
202 break;
203 case WLAN_EID_PERR:
204 elems->perr = pos;
205 elems->perr_len = elen;
206 break;
Assaf Kraussf2df3852008-06-15 18:23:29 +0300207 case WLAN_EID_CHANNEL_SWITCH:
208 elems->ch_switch_elem = pos;
209 elems->ch_switch_elem_len = elen;
210 break;
211 case WLAN_EID_QUIET:
212 if (!elems->quiet_elem) {
213 elems->quiet_elem = pos;
214 elems->quiet_elem_len = elen;
215 }
216 elems->num_of_quiet_elem++;
217 break;
218 case WLAN_EID_COUNTRY:
219 elems->country_elem = pos;
220 elems->country_elem_len = elen;
221 break;
222 case WLAN_EID_PWR_CONSTRAINT:
223 elems->pwr_constr_elem = pos;
224 elems->pwr_constr_elem_len = elen;
225 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700226 default:
Jiri Bencf0706e82007-05-05 11:45:53 -0700227 break;
228 }
229
230 left -= elen;
231 pos += elen;
232 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700233}
234
235
Jiri Bencf0706e82007-05-05 11:45:53 -0700236static int ecw2cw(int ecw)
237{
Johannes Bergac2bf322008-02-14 00:30:35 +0100238 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700239}
240
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100241
242static void ieee80211_sta_def_wmm_params(struct net_device *dev,
243 struct ieee80211_sta_bss *bss,
244 int ibss)
245{
246 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
247 struct ieee80211_local *local = sdata->local;
248 int i, have_higher_than_11mbit = 0;
249
250
251 /* cf. IEEE 802.11 9.2.12 */
252 for (i = 0; i < bss->supp_rates_len; i++)
253 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
254 have_higher_than_11mbit = 1;
255
256 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
257 have_higher_than_11mbit)
258 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
259 else
260 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
261
262
263 if (local->ops->conf_tx) {
264 struct ieee80211_tx_queue_params qparam;
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100265
266 memset(&qparam, 0, sizeof(qparam));
267
268 qparam.aifs = 2;
269
270 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
271 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
272 qparam.cw_min = 31;
273 else
274 qparam.cw_min = 15;
275
276 qparam.cw_max = 1023;
277 qparam.txop = 0;
278
Johannes Berge100bb62008-04-30 18:51:21 +0200279 for (i = 0; i < local_to_hw(local)->queues; i++)
280 local->ops->conf_tx(local_to_hw(local), i, &qparam);
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100281 }
282}
283
Jiri Bencf0706e82007-05-05 11:45:53 -0700284static void ieee80211_sta_wmm_params(struct net_device *dev,
285 struct ieee80211_if_sta *ifsta,
286 u8 *wmm_param, size_t wmm_param_len)
287{
288 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
289 struct ieee80211_tx_queue_params params;
290 size_t left;
291 int count;
292 u8 *pos;
293
Johannes Berg3434fbd2008-05-03 00:59:37 +0200294 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
295 return;
296
297 if (!wmm_param)
298 return;
299
Jiri Bencf0706e82007-05-05 11:45:53 -0700300 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
301 return;
302 count = wmm_param[6] & 0x0f;
303 if (count == ifsta->wmm_last_param_set)
304 return;
305 ifsta->wmm_last_param_set = count;
306
307 pos = wmm_param + 8;
308 left = wmm_param_len - 8;
309
310 memset(&params, 0, sizeof(params));
311
312 if (!local->ops->conf_tx)
313 return;
314
315 local->wmm_acm = 0;
316 for (; left >= 4; left -= 4, pos += 4) {
317 int aci = (pos[0] >> 5) & 0x03;
318 int acm = (pos[0] >> 4) & 0x01;
319 int queue;
320
321 switch (aci) {
322 case 1:
Johannes Berge100bb62008-04-30 18:51:21 +0200323 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200324 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700325 local->wmm_acm |= BIT(0) | BIT(3);
Jiri Bencf0706e82007-05-05 11:45:53 -0700326 break;
327 case 2:
Johannes Berge100bb62008-04-30 18:51:21 +0200328 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200329 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700330 local->wmm_acm |= BIT(4) | BIT(5);
Jiri Bencf0706e82007-05-05 11:45:53 -0700331 break;
332 case 3:
Johannes Berge100bb62008-04-30 18:51:21 +0200333 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200334 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700335 local->wmm_acm |= BIT(6) | BIT(7);
Jiri Bencf0706e82007-05-05 11:45:53 -0700336 break;
337 case 0:
338 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200339 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200340 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700341 local->wmm_acm |= BIT(1) | BIT(2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700342 break;
343 }
344
345 params.aifs = pos[0] & 0x0f;
346 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
347 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100348 params.txop = pos[2] | (pos[3] << 8);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200349#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700350 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100351 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e82007-05-05 11:45:53 -0700352 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100353 params.cw_max, params.txop);
354#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700355 /* TODO: handle ACM (block TX, fallback to next lowest allowed
356 * AC for now) */
357 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
358 printk(KERN_DEBUG "%s: failed to set TX queue "
359 "parameters for queue %d\n", dev->name, queue);
360 }
361 }
362}
363
John W. Linville50c4afb2008-04-15 14:09:27 -0400364static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
365 bool use_protection,
366 bool use_short_preamble)
Daniel Drake56282212007-07-10 19:32:10 +0200367{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100368 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Tomas Winklerebd74482008-07-01 10:44:50 +0300369#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200370 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Joe Perches0795af52007-10-03 17:59:30 -0700371 DECLARE_MAC_BUF(mac);
Tomas Winklerebd74482008-07-01 10:44:50 +0300372#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100373 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200374
Johannes Berg471b3ef2007-12-28 14:32:58 +0100375 if (use_protection != bss_conf->use_cts_prot) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200376#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake56282212007-07-10 19:32:10 +0200377 if (net_ratelimit()) {
378 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700379 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100380 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200381 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700382 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200383 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200384#endif
Johannes Berg471b3ef2007-12-28 14:32:58 +0100385 bss_conf->use_cts_prot = use_protection;
386 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200387 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200388
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200389 if (use_short_preamble != bss_conf->use_short_preamble) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200390#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Daniel Drake7e9ed182007-07-27 15:43:24 +0200391 if (net_ratelimit()) {
392 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700393 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100394 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200395 use_short_preamble ? "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700396 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200397 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200398#endif
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200399 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100400 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200401 }
Daniel Draked9430a32007-07-27 15:43:24 +0200402
Johannes Berg471b3ef2007-12-28 14:32:58 +0100403 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200404}
405
John W. Linville50c4afb2008-04-15 14:09:27 -0400406static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
407 u8 erp_value)
408{
409 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
410 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
411
412 return ieee80211_handle_protect_preamb(sdata,
413 use_protection, use_short_preamble);
414}
415
416static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
417 struct ieee80211_sta_bss *bss)
418{
419 u32 changed = 0;
420
421 if (bss->has_erp_value)
422 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
423 else {
424 u16 capab = bss->capability;
425 changed |= ieee80211_handle_protect_preamb(sdata, false,
426 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
427 }
428
429 return changed;
430}
431
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200432int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
433 struct ieee80211_ht_info *ht_info)
434{
435
436 if (ht_info == NULL)
437 return -EINVAL;
438
439 memset(ht_info, 0, sizeof(*ht_info));
440
441 if (ht_cap_ie) {
442 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
443
444 ht_info->ht_supported = 1;
445 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
446 ht_info->ampdu_factor =
447 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
448 ht_info->ampdu_density =
449 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
450 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
451 } else
452 ht_info->ht_supported = 0;
453
454 return 0;
455}
456
457int ieee80211_ht_addt_info_ie_to_ht_bss_info(
458 struct ieee80211_ht_addt_info *ht_add_info_ie,
459 struct ieee80211_ht_bss_info *bss_info)
460{
461 if (bss_info == NULL)
462 return -EINVAL;
463
464 memset(bss_info, 0, sizeof(*bss_info));
465
466 if (ht_add_info_ie) {
467 u16 op_mode;
468 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
469
470 bss_info->primary_channel = ht_add_info_ie->control_chan;
471 bss_info->bss_cap = ht_add_info_ie->ht_param;
472 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
473 }
474
475 return 0;
476}
Daniel Drake56282212007-07-10 19:32:10 +0200477
Jiri Bencf0706e82007-05-05 11:45:53 -0700478static void ieee80211_sta_send_associnfo(struct net_device *dev,
479 struct ieee80211_if_sta *ifsta)
480{
481 char *buf;
482 size_t len;
483 int i;
484 union iwreq_data wrqu;
485
486 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
487 return;
488
489 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200490 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700491 if (!buf)
492 return;
493
494 len = sprintf(buf, "ASSOCINFO(");
495 if (ifsta->assocreq_ies) {
496 len += sprintf(buf + len, "ReqIEs=");
497 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
498 len += sprintf(buf + len, "%02x",
499 ifsta->assocreq_ies[i]);
500 }
501 }
502 if (ifsta->assocresp_ies) {
503 if (ifsta->assocreq_ies)
504 len += sprintf(buf + len, " ");
505 len += sprintf(buf + len, "RespIEs=");
506 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
507 len += sprintf(buf + len, "%02x",
508 ifsta->assocresp_ies[i]);
509 }
510 }
511 len += sprintf(buf + len, ")");
512
513 if (len > IW_CUSTOM_MAX) {
514 len = sprintf(buf, "ASSOCRESPIE=");
515 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
516 len += sprintf(buf + len, "%02x",
517 ifsta->assocresp_ies[i]);
518 }
519 }
520
521 memset(&wrqu, 0, sizeof(wrqu));
522 wrqu.data.length = len;
523 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
524
525 kfree(buf);
526}
527
528
529static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400530 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200531 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700532{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100533 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
534 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700535 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700536 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100537 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700538
Jiri Bencf0706e82007-05-05 11:45:53 -0700539 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200540 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400541
542 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
543
Johannes Berg51fb61e2007-12-19 01:31:27 +0100544 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700545 return;
Daniel Drake56282212007-07-10 19:32:10 +0200546
John W. Linville65c107a2007-10-05 14:23:27 -0400547 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Tomas Winkler38668c02008-03-28 16:33:32 -0700548 conf->channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400549 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200550 if (bss) {
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700551 /* set timing information */
552 sdata->bss_conf.beacon_int = bss->beacon_int;
553 sdata->bss_conf.timestamp = bss->timestamp;
554
John W. Linville50c4afb2008-04-15 14:09:27 -0400555 changed |= ieee80211_handle_bss_capability(sdata, bss);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700556
Johannes Berg3e122be2008-07-09 14:40:34 +0200557 ieee80211_rx_bss_put(local, bss);
Daniel Drake56282212007-07-10 19:32:10 +0200558 }
559
Tomas Winkler38668c02008-03-28 16:33:32 -0700560 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
561 changed |= BSS_CHANGED_HT;
562 sdata->bss_conf.assoc_ht = 1;
563 sdata->bss_conf.ht_conf = &conf->ht_conf;
564 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
565 }
566
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400567 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700568 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
569 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
570 ieee80211_sta_send_associnfo(dev, ifsta);
571 } else {
Guy Cohen8db93692008-07-03 19:56:13 +0300572 netif_carrier_off(dev);
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700573 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400574 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Tomas Winklerfc32f922008-07-03 01:27:13 +0300575 changed |= ieee80211_reset_erp_info(dev);
Tomas Winkler38668c02008-03-28 16:33:32 -0700576
577 sdata->bss_conf.assoc_ht = 0;
578 sdata->bss_conf.ht_conf = NULL;
579 sdata->bss_conf.ht_bss_conf = NULL;
580
Jiri Bencf0706e82007-05-05 11:45:53 -0700581 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
582 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700583 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200584 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100585
Tomas Winklerb2205252008-01-27 16:18:22 +0200586 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100587 ieee80211_bss_info_change_notify(sdata, changed);
Guy Cohen8db93692008-07-03 19:56:13 +0300588
589 if (assoc)
590 netif_carrier_on(dev);
591
Reinette Chatre41a7be42008-04-03 16:08:49 -0700592 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
593 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700594}
595
596static void ieee80211_set_disassoc(struct net_device *dev,
597 struct ieee80211_if_sta *ifsta, int deauth)
598{
599 if (deauth)
600 ifsta->auth_tries = 0;
601 ifsta->assoc_tries = 0;
602 ieee80211_set_associated(dev, ifsta, 0);
603}
604
Luis Carlos Coboee385852008-02-23 15:17:11 +0100605void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
606 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700607{
608 struct ieee80211_sub_if_data *sdata;
Johannes Berge039fa42008-05-15 12:55:29 +0200609 struct ieee80211_tx_info *info;
Jiri Bencf0706e82007-05-05 11:45:53 -0700610
611 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
612 skb->dev = sdata->local->mdev;
613 skb_set_mac_header(skb, 0);
614 skb_set_network_header(skb, 0);
615 skb_set_transport_header(skb, 0);
616
Johannes Berge039fa42008-05-15 12:55:29 +0200617 info = IEEE80211_SKB_CB(skb);
618 memset(info, 0, sizeof(struct ieee80211_tx_info));
619 info->control.ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400620 if (!encrypt)
Johannes Berge039fa42008-05-15 12:55:29 +0200621 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700622
623 dev_queue_xmit(skb);
624}
625
626
627static void ieee80211_send_auth(struct net_device *dev,
628 struct ieee80211_if_sta *ifsta,
629 int transaction, u8 *extra, size_t extra_len,
630 int encrypt)
631{
632 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
633 struct sk_buff *skb;
634 struct ieee80211_mgmt *mgmt;
635
636 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
637 sizeof(*mgmt) + 6 + extra_len);
638 if (!skb) {
639 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
640 "frame\n", dev->name);
641 return;
642 }
643 skb_reserve(skb, local->hw.extra_tx_headroom);
644
645 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
646 memset(mgmt, 0, 24 + 6);
647 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
648 IEEE80211_STYPE_AUTH);
649 if (encrypt)
650 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
651 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
652 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
653 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
654 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
655 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
656 ifsta->auth_transaction = transaction + 1;
657 mgmt->u.auth.status_code = cpu_to_le16(0);
658 if (extra)
659 memcpy(skb_put(skb, extra_len), extra, extra_len);
660
661 ieee80211_sta_tx(dev, skb, encrypt);
662}
663
664
665static void ieee80211_authenticate(struct net_device *dev,
666 struct ieee80211_if_sta *ifsta)
667{
Joe Perches0795af52007-10-03 17:59:30 -0700668 DECLARE_MAC_BUF(mac);
669
Jiri Bencf0706e82007-05-05 11:45:53 -0700670 ifsta->auth_tries++;
671 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700672 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700673 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700674 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700675 ifsta->state = IEEE80211_DISABLED;
676 return;
677 }
678
679 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700680 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
681 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700682
683 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
684
685 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
686}
687
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200688static int ieee80211_compatible_rates(struct ieee80211_sta_bss *bss,
689 struct ieee80211_supported_band *sband,
690 u64 *rates)
691{
692 int i, j, count;
693 *rates = 0;
694 count = 0;
695 for (i = 0; i < bss->supp_rates_len; i++) {
696 int rate = (bss->supp_rates[i] & 0x7F) * 5;
697
698 for (j = 0; j < sband->n_bitrates; j++)
699 if (sband->bitrates[j].bitrate == rate) {
700 *rates |= BIT(j);
701 count++;
702 break;
703 }
704 }
705
706 return count;
707}
Jiri Bencf0706e82007-05-05 11:45:53 -0700708
709static void ieee80211_send_assoc(struct net_device *dev,
710 struct ieee80211_if_sta *ifsta)
711{
712 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700713 struct sk_buff *skb;
714 struct ieee80211_mgmt *mgmt;
715 u8 *pos, *ies;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200716 int i, len, count, rates_len, supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700717 u16 capab;
718 struct ieee80211_sta_bss *bss;
719 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100720 struct ieee80211_supported_band *sband;
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200721 u64 rates = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700722
723 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
724 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
725 ifsta->ssid_len);
726 if (!skb) {
727 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
728 "frame\n", dev->name);
729 return;
730 }
731 skb_reserve(skb, local->hw.extra_tx_headroom);
732
Johannes Berg8318d782008-01-24 19:38:38 +0100733 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
734
Jiri Bencf0706e82007-05-05 11:45:53 -0700735 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100736
737 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
738 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
739 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
740 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
741 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700742 }
Johannes Berg8318d782008-01-24 19:38:38 +0100743
744 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
745 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400746 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700747 if (bss) {
748 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
749 capab |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +0200750 if (bss->wmm_ie)
Jiri Bencf0706e82007-05-05 11:45:53 -0700751 wmm = 1;
Helmut Schaa0d580a72008-05-20 09:56:37 +0200752
753 /* get all rates supported by the device and the AP as
754 * some APs don't like getting a superset of their rates
755 * in the association request (e.g. D-Link DAP 1353 in
756 * b-only mode) */
757 rates_len = ieee80211_compatible_rates(bss, sband, &rates);
758
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300759 if ((bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
760 (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
761 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
762
Johannes Berg3e122be2008-07-09 14:40:34 +0200763 ieee80211_rx_bss_put(local, bss);
Helmut Schaa0d580a72008-05-20 09:56:37 +0200764 } else {
765 rates = ~0;
766 rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700767 }
768
769 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
770 memset(mgmt, 0, 24);
771 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
772 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
773 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
774
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400775 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700776 skb_put(skb, 10);
777 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
778 IEEE80211_STYPE_REASSOC_REQ);
779 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
780 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
781 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
782 ETH_ALEN);
783 } else {
784 skb_put(skb, 4);
785 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
786 IEEE80211_STYPE_ASSOC_REQ);
787 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
788 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
789 }
790
791 /* SSID */
792 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
793 *pos++ = WLAN_EID_SSID;
794 *pos++ = ifsta->ssid_len;
795 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
796
Helmut Schaa0d580a72008-05-20 09:56:37 +0200797 /* add all rates which were marked to be used above */
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200798 supp_rates_len = rates_len;
799 if (supp_rates_len > 8)
800 supp_rates_len = 8;
Jiri Bencf0706e82007-05-05 11:45:53 -0700801
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200802 len = sband->n_bitrates;
803 pos = skb_put(skb, supp_rates_len + 2);
804 *pos++ = WLAN_EID_SUPP_RATES;
805 *pos++ = supp_rates_len;
806
807 count = 0;
808 for (i = 0; i < sband->n_bitrates; i++) {
809 if (BIT(i) & rates) {
Johannes Berg8318d782008-01-24 19:38:38 +0100810 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700811 *pos++ = (u8) (rate / 5);
Helmut Schaa36d16ae2008-05-08 13:34:07 +0200812 if (++count == 8)
813 break;
814 }
815 }
816
817 if (count == 8) {
818 pos = skb_put(skb, rates_len - count + 2);
819 *pos++ = WLAN_EID_EXT_SUPP_RATES;
820 *pos++ = rates_len - count;
821
822 for (i++; i < sband->n_bitrates; i++) {
823 if (BIT(i) & rates) {
824 int rate = sband->bitrates[i].bitrate;
825 *pos++ = (u8) (rate / 5);
826 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700827 }
828 }
829
Tomas Winkler06ff47b2008-06-18 17:53:44 +0300830 if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
831 /* 1. power capabilities */
832 pos = skb_put(skb, 4);
833 *pos++ = WLAN_EID_PWR_CAPABILITY;
834 *pos++ = 2;
835 *pos++ = 0; /* min tx power */
836 *pos++ = local->hw.conf.channel->max_power; /* max tx power */
837
838 /* 2. supported channels */
839 /* TODO: get this in reg domain format */
840 pos = skb_put(skb, 2 * sband->n_channels + 2);
841 *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
842 *pos++ = 2 * sband->n_channels;
843 for (i = 0; i < sband->n_channels; i++) {
844 *pos++ = ieee80211_frequency_to_channel(
845 sband->channels[i].center_freq);
846 *pos++ = 1; /* one channel in the subband*/
847 }
848 }
849
Jiri Bencf0706e82007-05-05 11:45:53 -0700850 if (ifsta->extra_ie) {
851 pos = skb_put(skb, ifsta->extra_ie_len);
852 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
853 }
854
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400855 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700856 pos = skb_put(skb, 9);
857 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
858 *pos++ = 7; /* len */
859 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
860 *pos++ = 0x50;
861 *pos++ = 0xf2;
862 *pos++ = 2; /* WME */
863 *pos++ = 0; /* WME info */
864 *pos++ = 1; /* WME ver */
865 *pos++ = 0;
866 }
Johannes Berg3434fbd2008-05-03 00:59:37 +0200867
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200868 /* wmm support is a must to HT */
Johannes Berg3434fbd2008-05-03 00:59:37 +0200869 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
Emmanuel Grumbach93061022008-05-29 16:35:23 +0800870 sband->ht_info.ht_supported && bss->ht_add_ie) {
871 struct ieee80211_ht_addt_info *ht_add_info =
872 (struct ieee80211_ht_addt_info *)bss->ht_add_ie;
873 u16 cap = sband->ht_info.cap;
874 __le16 tmp;
875 u32 flags = local->hw.conf.channel->flags;
876
877 switch (ht_add_info->ht_param & IEEE80211_HT_IE_CHA_SEC_OFFSET) {
878 case IEEE80211_HT_IE_CHA_SEC_ABOVE:
879 if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
880 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
881 cap &= ~IEEE80211_HT_CAP_SGI_40;
882 }
883 break;
884 case IEEE80211_HT_IE_CHA_SEC_BELOW:
885 if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
886 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH;
887 cap &= ~IEEE80211_HT_CAP_SGI_40;
888 }
889 break;
890 }
891
892 tmp = cpu_to_le16(cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200893 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
894 *pos++ = WLAN_EID_HT_CAPABILITY;
895 *pos++ = sizeof(struct ieee80211_ht_cap);
896 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
897 memcpy(pos, &tmp, sizeof(u16));
898 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100899 /* TODO: needs a define here for << 2 */
900 *pos++ = sband->ht_info.ampdu_factor |
901 (sband->ht_info.ampdu_density << 2);
902 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200903 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700904
905 kfree(ifsta->assocreq_ies);
906 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200907 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700908 if (ifsta->assocreq_ies)
909 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
910
911 ieee80211_sta_tx(dev, skb, 0);
912}
913
914
915static void ieee80211_send_deauth(struct net_device *dev,
916 struct ieee80211_if_sta *ifsta, u16 reason)
917{
918 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
919 struct sk_buff *skb;
920 struct ieee80211_mgmt *mgmt;
921
922 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
923 if (!skb) {
924 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
925 "frame\n", dev->name);
926 return;
927 }
928 skb_reserve(skb, local->hw.extra_tx_headroom);
929
930 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
931 memset(mgmt, 0, 24);
932 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
933 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
934 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
935 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
936 IEEE80211_STYPE_DEAUTH);
937 skb_put(skb, 2);
938 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
939
940 ieee80211_sta_tx(dev, skb, 0);
941}
942
943
944static void ieee80211_send_disassoc(struct net_device *dev,
945 struct ieee80211_if_sta *ifsta, u16 reason)
946{
947 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
948 struct sk_buff *skb;
949 struct ieee80211_mgmt *mgmt;
950
951 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
952 if (!skb) {
953 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
954 "frame\n", dev->name);
955 return;
956 }
957 skb_reserve(skb, local->hw.extra_tx_headroom);
958
959 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
960 memset(mgmt, 0, 24);
961 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
962 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
963 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
964 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
965 IEEE80211_STYPE_DISASSOC);
966 skb_put(skb, 2);
967 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
968
969 ieee80211_sta_tx(dev, skb, 0);
970}
971
972
973static int ieee80211_privacy_mismatch(struct net_device *dev,
974 struct ieee80211_if_sta *ifsta)
975{
John W. Linville65c107a2007-10-05 14:23:27 -0400976 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700977 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000978 int bss_privacy;
979 int wep_privacy;
980 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700981
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000982 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700983 return 0;
984
Johannes Berg8318d782008-01-24 19:38:38 +0100985 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
986 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400987 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700988 if (!bss)
989 return 0;
990
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000991 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
992 wep_privacy = !!ieee80211_sta_wep_configured(dev);
993 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700994
Johannes Berg3e122be2008-07-09 14:40:34 +0200995 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -0700996
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000997 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
998 return 0;
999
1000 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001001}
1002
1003
1004static void ieee80211_associate(struct net_device *dev,
1005 struct ieee80211_if_sta *ifsta)
1006{
Joe Perches0795af52007-10-03 17:59:30 -07001007 DECLARE_MAC_BUF(mac);
1008
Jiri Bencf0706e82007-05-05 11:45:53 -07001009 ifsta->assoc_tries++;
1010 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -07001011 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001012 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -07001013 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001014 ifsta->state = IEEE80211_DISABLED;
1015 return;
1016 }
1017
1018 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -07001019 printk(KERN_DEBUG "%s: associate with AP %s\n",
1020 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001021 if (ieee80211_privacy_mismatch(dev, ifsta)) {
1022 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
1023 "mixed-cell disabled - abort association\n", dev->name);
1024 ifsta->state = IEEE80211_DISABLED;
1025 return;
1026 }
1027
1028 ieee80211_send_assoc(dev, ifsta);
1029
1030 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
1031}
1032
1033
1034static void ieee80211_associated(struct net_device *dev,
1035 struct ieee80211_if_sta *ifsta)
1036{
1037 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1038 struct sta_info *sta;
1039 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -07001040 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001041
1042 /* TODO: start monitoring current AP signal quality and number of
1043 * missed beacons. Scan other channels every now and then and search
1044 * for better APs. */
1045 /* TODO: remove expired BSSes */
1046
1047 ifsta->state = IEEE80211_ASSOCIATED;
1048
Johannes Bergd0709a62008-02-25 16:27:46 +01001049 rcu_read_lock();
1050
Jiri Bencf0706e82007-05-05 11:45:53 -07001051 sta = sta_info_get(local, ifsta->bssid);
1052 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -07001053 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
1054 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001055 disassoc = 1;
1056 } else {
1057 disassoc = 0;
1058 if (time_after(jiffies,
1059 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001060 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001061 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -07001062 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -07001063 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -07001064 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001065 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +01001066 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001067 } else
Jiri Bencf0706e82007-05-05 11:45:53 -07001068 ieee80211_send_probe_req(dev, ifsta->bssid,
1069 local->scan_ssid,
1070 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001071 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001072 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001073 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001074 if (time_after(jiffies, ifsta->last_probe +
1075 IEEE80211_PROBE_INTERVAL)) {
1076 ifsta->last_probe = jiffies;
1077 ieee80211_send_probe_req(dev, ifsta->bssid,
1078 ifsta->ssid,
1079 ifsta->ssid_len);
1080 }
1081 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001082 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001083
1084 rcu_read_unlock();
1085
Johannes Berg3b967662008-04-08 17:56:52 +02001086 if (disassoc && sta)
Johannes Bergd0709a62008-02-25 16:27:46 +01001087 sta_info_destroy(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01001088
Jiri Bencf0706e82007-05-05 11:45:53 -07001089 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -05001090 ifsta->state = IEEE80211_DISABLED;
1091 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07001092 } else {
1093 mod_timer(&ifsta->timer, jiffies +
1094 IEEE80211_MONITORING_INTERVAL);
1095 }
1096}
1097
1098
1099static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
1100 u8 *ssid, size_t ssid_len)
1101{
1102 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01001103 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001104 struct sk_buff *skb;
1105 struct ieee80211_mgmt *mgmt;
1106 u8 *pos, *supp_rates, *esupp_rates = NULL;
1107 int i;
1108
1109 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
1110 if (!skb) {
1111 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
1112 "request\n", dev->name);
1113 return;
1114 }
1115 skb_reserve(skb, local->hw.extra_tx_headroom);
1116
1117 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1118 memset(mgmt, 0, 24);
1119 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1120 IEEE80211_STYPE_PROBE_REQ);
1121 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1122 if (dst) {
1123 memcpy(mgmt->da, dst, ETH_ALEN);
1124 memcpy(mgmt->bssid, dst, ETH_ALEN);
1125 } else {
1126 memset(mgmt->da, 0xff, ETH_ALEN);
1127 memset(mgmt->bssid, 0xff, ETH_ALEN);
1128 }
1129 pos = skb_put(skb, 2 + ssid_len);
1130 *pos++ = WLAN_EID_SSID;
1131 *pos++ = ssid_len;
1132 memcpy(pos, ssid, ssid_len);
1133
1134 supp_rates = skb_put(skb, 2);
1135 supp_rates[0] = WLAN_EID_SUPP_RATES;
1136 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001137 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1138
1139 for (i = 0; i < sband->n_bitrates; i++) {
1140 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07001141 if (esupp_rates) {
1142 pos = skb_put(skb, 1);
1143 esupp_rates[1]++;
1144 } else if (supp_rates[1] == 8) {
1145 esupp_rates = skb_put(skb, 3);
1146 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1147 esupp_rates[1] = 1;
1148 pos = &esupp_rates[2];
1149 } else {
1150 pos = skb_put(skb, 1);
1151 supp_rates[1]++;
1152 }
Johannes Berg8318d782008-01-24 19:38:38 +01001153 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -07001154 }
1155
1156 ieee80211_sta_tx(dev, skb, 0);
1157}
1158
1159
1160static int ieee80211_sta_wep_configured(struct net_device *dev)
1161{
1162 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1163 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001164 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -07001165 return 0;
1166 return 1;
1167}
1168
1169
1170static void ieee80211_auth_completed(struct net_device *dev,
1171 struct ieee80211_if_sta *ifsta)
1172{
1173 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001174 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001175 ieee80211_associate(dev, ifsta);
1176}
1177
1178
1179static void ieee80211_auth_challenge(struct net_device *dev,
1180 struct ieee80211_if_sta *ifsta,
1181 struct ieee80211_mgmt *mgmt,
1182 size_t len)
1183{
1184 u8 *pos;
1185 struct ieee802_11_elems elems;
1186
Jiri Bencf0706e82007-05-05 11:45:53 -07001187 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001188 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001189 if (!elems.challenge)
Jiri Bencf0706e82007-05-05 11:45:53 -07001190 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001191 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1192 elems.challenge_len + 2, 1);
1193}
1194
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001195static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1196 u8 dialog_token, u16 status, u16 policy,
1197 u16 buf_size, u16 timeout)
1198{
1199 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1200 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1201 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1202 struct sk_buff *skb;
1203 struct ieee80211_mgmt *mgmt;
1204 u16 capab;
1205
Ester Kummer3acea5b2008-04-17 16:05:14 -07001206 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1207
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001208 if (!skb) {
1209 printk(KERN_DEBUG "%s: failed to allocate buffer "
1210 "for addba resp frame\n", dev->name);
1211 return;
1212 }
1213
1214 skb_reserve(skb, local->hw.extra_tx_headroom);
1215 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1216 memset(mgmt, 0, 24);
1217 memcpy(mgmt->da, da, ETH_ALEN);
1218 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001219 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001220 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1221 else
1222 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1223 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1224 IEEE80211_STYPE_ACTION);
1225
1226 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1227 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1228 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1229 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1230
1231 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1232 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1233 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1234
1235 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1236 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1237 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1238
1239 ieee80211_sta_tx(dev, skb, 0);
1240
1241 return;
1242}
1243
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001244void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1245 u16 tid, u8 dialog_token, u16 start_seq_num,
1246 u16 agg_size, u16 timeout)
1247{
1248 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1249 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1250 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1251 struct sk_buff *skb;
1252 struct ieee80211_mgmt *mgmt;
1253 u16 capab;
1254
Ester Kummer3acea5b2008-04-17 16:05:14 -07001255 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001256
1257 if (!skb) {
1258 printk(KERN_ERR "%s: failed to allocate buffer "
1259 "for addba request frame\n", dev->name);
1260 return;
1261 }
1262 skb_reserve(skb, local->hw.extra_tx_headroom);
1263 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1264 memset(mgmt, 0, 24);
1265 memcpy(mgmt->da, da, ETH_ALEN);
1266 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1267 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1268 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1269 else
1270 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1271
1272 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1273 IEEE80211_STYPE_ACTION);
1274
1275 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1276
1277 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1278 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1279
1280 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1281 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1282 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1283 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1284
1285 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1286
1287 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1288 mgmt->u.action.u.addba_req.start_seq_num =
1289 cpu_to_le16(start_seq_num << 4);
1290
1291 ieee80211_sta_tx(dev, skb, 0);
1292}
1293
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001294static void ieee80211_sta_process_addba_request(struct net_device *dev,
1295 struct ieee80211_mgmt *mgmt,
1296 size_t len)
1297{
1298 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001299 struct ieee80211_hw *hw = &local->hw;
1300 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001301 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001302 struct tid_ampdu_rx *tid_agg_rx;
1303 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001304 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001305 int ret = -EOPNOTSUPP;
1306 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001307
Johannes Bergd0709a62008-02-25 16:27:46 +01001308 rcu_read_lock();
1309
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001310 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001311 if (!sta) {
1312 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001313 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001314 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001315
1316 /* extract session parameters from addba request frame */
1317 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1318 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001319 start_seq_num =
1320 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001321
1322 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1323 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1324 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1325 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1326
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001327 status = WLAN_STATUS_REQUEST_DECLINED;
1328
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001329 /* sanity check for incoming parameters:
1330 * check if configuration can support the BA policy
1331 * and if buffer size does not exceeds max value */
1332 if (((ba_policy != 1)
1333 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1334 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1335 status = WLAN_STATUS_INVALID_QOS_PARAM;
1336#ifdef CONFIG_MAC80211_HT_DEBUG
1337 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001338 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001339 "%s on tid %u. policy %d, buffer size %d\n",
1340 print_mac(mac, mgmt->sa), tid, ba_policy,
1341 buf_size);
1342#endif /* CONFIG_MAC80211_HT_DEBUG */
1343 goto end_no_lock;
1344 }
1345 /* determine default buffer size */
1346 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001347 struct ieee80211_supported_band *sband;
1348
1349 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001350 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001351 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001352 }
1353
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001354
1355 /* examine state machine */
Johannes Berg07346f812008-05-03 01:02:02 +02001356 spin_lock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001357
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001358 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001359#ifdef CONFIG_MAC80211_HT_DEBUG
1360 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001361 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001362 "%s on tid %u\n",
1363 print_mac(mac, mgmt->sa), tid);
1364#endif /* CONFIG_MAC80211_HT_DEBUG */
1365 goto end;
1366 }
1367
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001368 /* prepare A-MPDU MLME for Rx aggregation */
1369 sta->ampdu_mlme.tid_rx[tid] =
1370 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1371 if (!sta->ampdu_mlme.tid_rx[tid]) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001372#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001373 if (net_ratelimit())
1374 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1375 tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001376#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001377 goto end;
1378 }
1379 /* rx timer */
1380 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1381 sta_rx_agg_session_timer_expired;
1382 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1383 (unsigned long)&sta->timer_to_tid[tid];
1384 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1385
1386 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1387
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001388 /* prepare reordering buffer */
1389 tid_agg_rx->reorder_buf =
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301390 kmalloc(buf_size * sizeof(struct sk_buff *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001391 if (!tid_agg_rx->reorder_buf) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001392#ifdef CONFIG_MAC80211_HT_DEBUG
Johannes Berg03147df2008-02-26 00:39:28 +01001393 if (net_ratelimit())
1394 printk(KERN_ERR "can not allocate reordering buffer "
1395 "to tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001396#endif
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001397 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001398 goto end;
1399 }
1400 memset(tid_agg_rx->reorder_buf, 0,
Senthil Balasubramanianc97c23e2008-05-28 23:15:32 +05301401 buf_size * sizeof(struct sk_buff *));
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001402
1403 if (local->ops->ampdu_action)
1404 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001405 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001406#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001407 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001408#endif /* CONFIG_MAC80211_HT_DEBUG */
1409
1410 if (ret) {
1411 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001412 kfree(tid_agg_rx);
1413 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001414 goto end;
1415 }
1416
1417 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001418 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001419 tid_agg_rx->dialog_token = dialog_token;
1420 tid_agg_rx->ssn = start_seq_num;
1421 tid_agg_rx->head_seq_num = start_seq_num;
1422 tid_agg_rx->buf_size = buf_size;
1423 tid_agg_rx->timeout = timeout;
1424 tid_agg_rx->stored_mpdu_num = 0;
1425 status = WLAN_STATUS_SUCCESS;
1426end:
Johannes Berg07346f812008-05-03 01:02:02 +02001427 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001428
1429end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001430 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1431 dialog_token, status, 1, buf_size, timeout);
1432 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001433}
Jiri Bencf0706e82007-05-05 11:45:53 -07001434
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001435static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1436 struct ieee80211_mgmt *mgmt,
1437 size_t len)
1438{
1439 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1440 struct ieee80211_hw *hw = &local->hw;
1441 struct sta_info *sta;
1442 u16 capab;
1443 u16 tid;
1444 u8 *state;
1445
Johannes Bergd0709a62008-02-25 16:27:46 +01001446 rcu_read_lock();
1447
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001448 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001449 if (!sta) {
1450 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001451 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001452 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001453
1454 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1455 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1456
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001457 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001458
Johannes Berg07346f812008-05-03 01:02:02 +02001459 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001460
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001461 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001462 spin_unlock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001463 goto addba_resp_exit;
1464 }
1465
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001466 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001467 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Johannes Berg07346f812008-05-03 01:02:02 +02001468 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001469#ifdef CONFIG_MAC80211_HT_DEBUG
1470 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1471#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001472 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001473 }
1474
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001475 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001476#ifdef CONFIG_MAC80211_HT_DEBUG
1477 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1478#endif /* CONFIG_MAC80211_HT_DEBUG */
1479 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1480 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001481 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001482 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001483
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001484 if (*state == HT_AGG_STATE_OPERATIONAL)
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001485 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001486
Johannes Berg07346f812008-05-03 01:02:02 +02001487 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001488 } else {
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001489 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001490 /* this will allow the state check in stop_BA_session */
1491 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001492 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001493 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1494 WLAN_BACK_INITIATOR);
1495 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001496
1497addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001498 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001499}
1500
1501void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1502 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001503{
1504 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1505 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1506 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1507 struct sk_buff *skb;
1508 struct ieee80211_mgmt *mgmt;
1509 u16 params;
1510
Ester Kummer3acea5b2008-04-17 16:05:14 -07001511 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001512
1513 if (!skb) {
1514 printk(KERN_ERR "%s: failed to allocate buffer "
1515 "for delba frame\n", dev->name);
1516 return;
1517 }
1518
1519 skb_reserve(skb, local->hw.extra_tx_headroom);
1520 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1521 memset(mgmt, 0, 24);
1522 memcpy(mgmt->da, da, ETH_ALEN);
1523 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001524 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001525 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1526 else
1527 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1528 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1529 IEEE80211_STYPE_ACTION);
1530
1531 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1532
1533 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1534 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1535 params = (u16)(initiator << 11); /* bit 11 initiator */
1536 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1537
1538 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1539 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1540
1541 ieee80211_sta_tx(dev, skb, 0);
1542}
1543
Ron Rindjunsky429a3802008-07-01 14:16:03 +03001544void ieee80211_send_bar(struct net_device *dev, u8 *ra, u16 tid, u16 ssn)
1545{
1546 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1547 struct sk_buff *skb;
1548 struct ieee80211_bar *bar;
1549 u16 bar_control = 0;
1550
1551 skb = dev_alloc_skb(sizeof(*bar) + local->hw.extra_tx_headroom);
1552 if (!skb) {
1553 printk(KERN_ERR "%s: failed to allocate buffer for "
1554 "bar frame\n", dev->name);
1555 return;
1556 }
1557 skb_reserve(skb, local->hw.extra_tx_headroom);
1558 bar = (struct ieee80211_bar *)skb_put(skb, sizeof(*bar));
1559 memset(bar, 0, sizeof(*bar));
1560 bar->frame_control = IEEE80211_FC(IEEE80211_FTYPE_CTL,
1561 IEEE80211_STYPE_BACK_REQ);
1562 memcpy(bar->ra, ra, ETH_ALEN);
1563 memcpy(bar->ta, dev->dev_addr, ETH_ALEN);
1564 bar_control |= (u16)IEEE80211_BAR_CTRL_ACK_POLICY_NORMAL;
1565 bar_control |= (u16)IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA;
1566 bar_control |= (u16)(tid << 12);
1567 bar->control = cpu_to_le16(bar_control);
1568 bar->start_seq_num = cpu_to_le16(ssn);
1569
1570 ieee80211_sta_tx(dev, skb, 0);
1571}
1572
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001573void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1574 u16 initiator, u16 reason)
1575{
1576 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1577 struct ieee80211_hw *hw = &local->hw;
1578 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001579 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001580 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001581
Johannes Bergd0709a62008-02-25 16:27:46 +01001582 rcu_read_lock();
1583
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001584 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001585 if (!sta) {
1586 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001587 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001588 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001589
1590 /* check if TID is in operational state */
Johannes Berg07346f812008-05-03 01:02:02 +02001591 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001592 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001593 != HT_AGG_STATE_OPERATIONAL) {
Johannes Berg07346f812008-05-03 01:02:02 +02001594 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +01001595 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001596 return;
1597 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001598 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001599 HT_AGG_STATE_REQ_STOP_BA_MSK |
1600 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Johannes Berg07346f812008-05-03 01:02:02 +02001601 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001602
1603 /* stop HW Rx aggregation. ampdu_action existence
1604 * already verified in session init so we add the BUG_ON */
1605 BUG_ON(!local->ops->ampdu_action);
1606
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001607#ifdef CONFIG_MAC80211_HT_DEBUG
1608 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1609 print_mac(mac, ra), tid);
1610#endif /* CONFIG_MAC80211_HT_DEBUG */
1611
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001612 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001613 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001614 if (ret)
1615 printk(KERN_DEBUG "HW problem - can not stop rx "
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001616 "aggregation for tid %d\n", tid);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001617
1618 /* shutdown timer has not expired */
1619 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001620 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001621
1622 /* check if this is a self generated aggregation halt */
1623 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1624 ieee80211_send_delba(dev, ra, tid, 0, reason);
1625
1626 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001627 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1628 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001629 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001630 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1631 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1632 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001633 }
1634 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001635 /* free resources */
1636 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1637 kfree(sta->ampdu_mlme.tid_rx[tid]);
1638 sta->ampdu_mlme.tid_rx[tid] = NULL;
1639 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001640
Johannes Bergd0709a62008-02-25 16:27:46 +01001641 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001642}
1643
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001644
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001645static void ieee80211_sta_process_delba(struct net_device *dev,
1646 struct ieee80211_mgmt *mgmt, size_t len)
1647{
1648 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1649 struct sta_info *sta;
1650 u16 tid, params;
1651 u16 initiator;
1652 DECLARE_MAC_BUF(mac);
1653
Johannes Bergd0709a62008-02-25 16:27:46 +01001654 rcu_read_lock();
1655
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001656 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001657 if (!sta) {
1658 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001659 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001660 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001661
1662 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1663 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1664 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1665
1666#ifdef CONFIG_MAC80211_HT_DEBUG
1667 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001668 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1669 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001670 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001671 mgmt->u.action.u.delba.reason_code);
1672#endif /* CONFIG_MAC80211_HT_DEBUG */
1673
1674 if (initiator == WLAN_BACK_INITIATOR)
1675 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1676 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001677 else { /* WLAN_BACK_RECIPIENT */
Johannes Berg07346f812008-05-03 01:02:02 +02001678 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001679 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001680 HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001681 spin_unlock_bh(&sta->lock);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001682 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1683 WLAN_BACK_RECIPIENT);
1684 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001685 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001686}
1687
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001688/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001689 * After sending add Block Ack request we activated a timer until
1690 * add Block Ack response will arrive from the recipient.
1691 * If this timer expires sta_addba_resp_timer_expired will be executed.
1692 */
1693void sta_addba_resp_timer_expired(unsigned long data)
1694{
1695 /* not an elegant detour, but there is no choice as the timer passes
1696 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001697 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001698 * array gives the sta through container_of */
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301699 u16 tid = *(u8 *)data;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001700 struct sta_info *temp_sta = container_of((void *)data,
1701 struct sta_info, timer_to_tid[tid]);
1702
1703 struct ieee80211_local *local = temp_sta->local;
1704 struct ieee80211_hw *hw = &local->hw;
1705 struct sta_info *sta;
1706 u8 *state;
1707
Johannes Bergd0709a62008-02-25 16:27:46 +01001708 rcu_read_lock();
1709
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001710 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001711 if (!sta) {
1712 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001713 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001714 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001715
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001716 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001717 /* check if the TID waits for addBA response */
Johannes Berg07346f812008-05-03 01:02:02 +02001718 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001719 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001720 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001721 *state = HT_AGG_STATE_IDLE;
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001722#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001723 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1724 "expecting addBA response there", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001725#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001726 goto timer_expired_exit;
1727 }
1728
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001729#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001730 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001731#endif
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001732
1733 /* go through the state check in stop_BA_session */
1734 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001735 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001736 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1737 WLAN_BACK_INITIATOR);
1738
1739timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001740 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001741}
1742
1743/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001744 * After accepting the AddBA Request we activated a timer,
1745 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001746 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1747 */
Adrian Bunk712590d2008-04-21 11:47:51 +03001748static void sta_rx_agg_session_timer_expired(unsigned long data)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001749{
1750 /* not an elegant detour, but there is no choice as the timer passes
Senthil Balasubramanian70d251b2008-05-28 20:08:12 +05301751 * only one argument, and various sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001752 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001753 * array gives the sta through container_of */
1754 u8 *ptid = (u8 *)data;
1755 u8 *timer_to_id = ptid - *ptid;
1756 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1757 timer_to_tid[0]);
1758
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001759#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001760 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001761#endif
Johannes Bergd0709a62008-02-25 16:27:46 +01001762 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1763 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001764 WLAN_REASON_QSTA_TIMEOUT);
1765}
1766
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001767void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1768{
1769 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1770 int i;
1771
1772 for (i = 0; i < STA_TID_NUM; i++) {
1773 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1774 WLAN_BACK_INITIATOR);
1775 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1776 WLAN_BACK_RECIPIENT,
1777 WLAN_REASON_QSTA_LEAVE_QBSS);
1778 }
1779}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001780
Assaf Kraussb6623482008-06-16 16:09:49 +03001781static void ieee80211_send_refuse_measurement_request(struct net_device *dev,
1782 struct ieee80211_msrment_ie *request_ie,
1783 const u8 *da, const u8 *bssid,
1784 u8 dialog_token)
1785{
1786 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1787 struct sk_buff *skb;
1788 struct ieee80211_mgmt *msr_report;
1789
1790 skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
1791 sizeof(struct ieee80211_msrment_ie));
1792
1793 if (!skb) {
1794 printk(KERN_ERR "%s: failed to allocate buffer for "
1795 "measurement report frame\n", dev->name);
1796 return;
1797 }
1798
1799 skb_reserve(skb, local->hw.extra_tx_headroom);
1800 msr_report = (struct ieee80211_mgmt *)skb_put(skb, 24);
1801 memset(msr_report, 0, 24);
1802 memcpy(msr_report->da, da, ETH_ALEN);
1803 memcpy(msr_report->sa, dev->dev_addr, ETH_ALEN);
1804 memcpy(msr_report->bssid, bssid, ETH_ALEN);
1805 msr_report->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1806 IEEE80211_STYPE_ACTION);
1807
1808 skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
1809 msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
1810 msr_report->u.action.u.measurement.action_code =
1811 WLAN_ACTION_SPCT_MSR_RPRT;
1812 msr_report->u.action.u.measurement.dialog_token = dialog_token;
1813
1814 msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
1815 msr_report->u.action.u.measurement.length =
1816 sizeof(struct ieee80211_msrment_ie);
1817
1818 memset(&msr_report->u.action.u.measurement.msr_elem, 0,
1819 sizeof(struct ieee80211_msrment_ie));
1820 msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
1821 msr_report->u.action.u.measurement.msr_elem.mode |=
1822 IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
1823 msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
1824
1825 ieee80211_sta_tx(dev, skb, 0);
1826}
1827
1828static void ieee80211_sta_process_measurement_req(struct net_device *dev,
1829 struct ieee80211_mgmt *mgmt,
1830 size_t len)
1831{
1832 /*
1833 * Ignoring measurement request is spec violation.
1834 * Mandatory measurements must be reported optional
1835 * measurements might be refused or reported incapable
1836 * For now just refuse
1837 * TODO: Answer basic measurement as unmeasured
1838 */
1839 ieee80211_send_refuse_measurement_request(dev,
1840 &mgmt->u.action.u.measurement.msr_elem,
1841 mgmt->sa, mgmt->bssid,
1842 mgmt->u.action.u.measurement.dialog_token);
1843}
1844
1845
Jiri Bencf0706e82007-05-05 11:45:53 -07001846static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1847 struct ieee80211_if_sta *ifsta,
1848 struct ieee80211_mgmt *mgmt,
1849 size_t len)
1850{
1851 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1852 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001853 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001854
1855 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001856 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07001857 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001858
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001859 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07001860 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001861
Johannes Berg51fb61e2007-12-19 01:31:27 +01001862 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001863 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001864 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001865
Johannes Berg51fb61e2007-12-19 01:31:27 +01001866 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001867 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07001868 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001869
1870 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1871 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1872 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1873
Johannes Berg51fb61e2007-12-19 01:31:27 +01001874 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg135a2112008-06-16 20:55:29 +02001875 /*
1876 * IEEE 802.11 standard does not require authentication in IBSS
Jiri Bencf0706e82007-05-05 11:45:53 -07001877 * networks and most implementations do not seem to use it.
1878 * However, try to reply to authentication attempts if someone
1879 * has actually implemented this.
Johannes Berg135a2112008-06-16 20:55:29 +02001880 */
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001881 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1)
Jiri Bencf0706e82007-05-05 11:45:53 -07001882 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001883 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1884 }
1885
1886 if (auth_alg != ifsta->auth_alg ||
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001887 auth_transaction != ifsta->auth_transaction)
Jiri Bencf0706e82007-05-05 11:45:53 -07001888 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001889
1890 if (status_code != WLAN_STATUS_SUCCESS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001891 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1892 u8 algs[3];
1893 const int num_algs = ARRAY_SIZE(algs);
1894 int i, pos;
1895 algs[0] = algs[1] = algs[2] = 0xff;
1896 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1897 algs[0] = WLAN_AUTH_OPEN;
1898 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1899 algs[1] = WLAN_AUTH_SHARED_KEY;
1900 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1901 algs[2] = WLAN_AUTH_LEAP;
1902 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1903 pos = 0;
1904 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1905 pos = 1;
1906 else
1907 pos = 2;
1908 for (i = 0; i < num_algs; i++) {
1909 pos++;
1910 if (pos >= num_algs)
1911 pos = 0;
1912 if (algs[pos] == ifsta->auth_alg ||
1913 algs[pos] == 0xff)
1914 continue;
1915 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1916 !ieee80211_sta_wep_configured(dev))
1917 continue;
1918 ifsta->auth_alg = algs[pos];
Jiri Bencf0706e82007-05-05 11:45:53 -07001919 break;
1920 }
1921 }
1922 return;
1923 }
1924
1925 switch (ifsta->auth_alg) {
1926 case WLAN_AUTH_OPEN:
1927 case WLAN_AUTH_LEAP:
1928 ieee80211_auth_completed(dev, ifsta);
1929 break;
1930 case WLAN_AUTH_SHARED_KEY:
1931 if (ifsta->auth_transaction == 4)
1932 ieee80211_auth_completed(dev, ifsta);
1933 else
1934 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1935 break;
1936 }
1937}
1938
1939
1940static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1941 struct ieee80211_if_sta *ifsta,
1942 struct ieee80211_mgmt *mgmt,
1943 size_t len)
1944{
1945 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001946 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001947
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001948 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07001949 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001950
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001951 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07001952 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001953
1954 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1955
Johannes Berg988c0f72008-04-17 19:21:22 +02001956 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001957 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001958
1959 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1960 ifsta->state == IEEE80211_ASSOCIATE ||
1961 ifsta->state == IEEE80211_ASSOCIATED) {
1962 ifsta->state = IEEE80211_AUTHENTICATE;
1963 mod_timer(&ifsta->timer, jiffies +
1964 IEEE80211_RETRY_AUTH_INTERVAL);
1965 }
1966
1967 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001968 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001969}
1970
1971
1972static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1973 struct ieee80211_if_sta *ifsta,
1974 struct ieee80211_mgmt *mgmt,
1975 size_t len)
1976{
1977 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001978 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001979
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001980 if (len < 24 + 2)
Jiri Bencf0706e82007-05-05 11:45:53 -07001981 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001982
Johannes Bergf4ea83d2008-06-30 15:10:46 +02001983 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN))
Jiri Bencf0706e82007-05-05 11:45:53 -07001984 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001985
1986 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1987
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001988 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001989 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1990
1991 if (ifsta->state == IEEE80211_ASSOCIATED) {
1992 ifsta->state = IEEE80211_ASSOCIATE;
1993 mod_timer(&ifsta->timer, jiffies +
1994 IEEE80211_RETRY_AUTH_INTERVAL);
1995 }
1996
1997 ieee80211_set_disassoc(dev, ifsta, 0);
1998}
1999
2000
Johannes Berg471b3ef2007-12-28 14:32:58 +01002001static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07002002 struct ieee80211_if_sta *ifsta,
2003 struct ieee80211_mgmt *mgmt,
2004 size_t len,
2005 int reassoc)
2006{
Johannes Berg471b3ef2007-12-28 14:32:58 +01002007 struct ieee80211_local *local = sdata->local;
2008 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01002009 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07002010 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01002011 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07002012 u16 capab_info, status_code, aid;
2013 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002014 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07002015 u8 *pos;
2016 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07002017 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01002018 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07002019
2020 /* AssocResp and ReassocResp have identical structure, so process both
2021 * of them in this function. */
2022
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002023 if (ifsta->state != IEEE80211_ASSOCIATE)
Jiri Bencf0706e82007-05-05 11:45:53 -07002024 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002025
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002026 if (len < 24 + 6)
Jiri Bencf0706e82007-05-05 11:45:53 -07002027 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002028
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002029 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0)
Jiri Bencf0706e82007-05-05 11:45:53 -07002030 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07002031
2032 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
2033 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
2034 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07002035
Joe Perches0795af52007-10-03 17:59:30 -07002036 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07002037 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07002038 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02002039 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07002040
2041 if (status_code != WLAN_STATUS_SUCCESS) {
2042 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
2043 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02002044 /* if this was a reassociation, ensure we try a "full"
2045 * association next time. This works around some broken APs
2046 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002047 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07002048 return;
2049 }
2050
Johannes Berg1dd84aa2007-10-10 12:03:41 +02002051 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
2052 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
2053 "set\n", dev->name, aid);
2054 aid &= ~(BIT(15) | BIT(14));
2055
Jiri Bencf0706e82007-05-05 11:45:53 -07002056 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04002057 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002058
2059 if (!elems.supp_rates) {
2060 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
2061 dev->name);
2062 return;
2063 }
2064
2065 printk(KERN_DEBUG "%s: associated\n", dev->name);
2066 ifsta->aid = aid;
2067 ifsta->ap_capab = capab_info;
2068
2069 kfree(ifsta->assocresp_ies);
2070 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002071 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002072 if (ifsta->assocresp_ies)
2073 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
2074
Johannes Bergd0709a62008-02-25 16:27:46 +01002075 rcu_read_lock();
2076
Jiri Bencf0706e82007-05-05 11:45:53 -07002077 /* Add STA entry for the AP */
2078 sta = sta_info_get(local, ifsta->bssid);
2079 if (!sta) {
2080 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01002081 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01002082
Johannes Berg73651ee2008-02-25 16:27:47 +01002083 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
2084 if (!sta) {
2085 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
2086 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01002087 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002088 return;
2089 }
John W. Linville65c107a2007-10-05 14:23:27 -04002090 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01002091 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002092 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002093 if (bss) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002094 sta->last_signal = bss->signal;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002095 sta->last_qual = bss->qual;
Jiri Bencf0706e82007-05-05 11:45:53 -07002096 sta->last_noise = bss->noise;
Johannes Berg3e122be2008-07-09 14:40:34 +02002097 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002098 }
Johannes Berg73651ee2008-02-25 16:27:47 +01002099
2100 err = sta_info_insert(sta);
2101 if (err) {
2102 printk(KERN_DEBUG "%s: failed to insert STA entry for"
2103 " the AP (error %d)\n", dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01002104 rcu_read_unlock();
2105 return;
2106 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002107 }
2108
Johannes Berg73651ee2008-02-25 16:27:47 +01002109 /*
2110 * FIXME: Do we really need to update the sta_info's information here?
2111 * We already know about the AP (we found it in our list) so it
2112 * should already be filled with the right info, no?
2113 * As is stands, all this is racy because typically we assume
2114 * the information that is filled in here (except flags) doesn't
2115 * change while a STA structure is alive. As such, it should move
2116 * to between the sta_info_alloc() and sta_info_insert() above.
2117 */
2118
Johannes Berg07346f812008-05-03 01:02:02 +02002119 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
2120 WLAN_STA_AUTHORIZED);
Jiri Bencf0706e82007-05-05 11:45:53 -07002121
2122 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002123 basic_rates = 0;
2124 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2125
Jiri Bencf0706e82007-05-05 11:45:53 -07002126 for (i = 0; i < elems.supp_rates_len; i++) {
2127 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002128
2129 if (rate > 110)
2130 have_higher_than_11mbit = true;
2131
2132 for (j = 0; j < sband->n_bitrates; j++) {
2133 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002134 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002135 if (elems.supp_rates[i] & 0x80)
2136 basic_rates |= BIT(j);
2137 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002138 }
Johannes Berg8318d782008-01-24 19:38:38 +01002139
Jiri Bencf0706e82007-05-05 11:45:53 -07002140 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2141 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002142
2143 if (rate > 110)
2144 have_higher_than_11mbit = true;
2145
2146 for (j = 0; j < sband->n_bitrates; j++) {
2147 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002148 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002149 if (elems.ext_supp_rates[i] & 0x80)
2150 basic_rates |= BIT(j);
2151 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002152 }
Johannes Berg8318d782008-01-24 19:38:38 +01002153
2154 sta->supp_rates[local->hw.conf.channel->band] = rates;
2155 sdata->basic_rates = basic_rates;
2156
2157 /* cf. IEEE 802.11 9.2.12 */
2158 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2159 have_higher_than_11mbit)
2160 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2161 else
2162 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002163
Johannes Berg3434fbd2008-05-03 00:59:37 +02002164 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2165 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002166 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002167 ieee80211_ht_cap_ie_to_ht_info(
2168 (struct ieee80211_ht_cap *)
2169 elems.ht_cap_elem, &sta->ht_info);
2170 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2171 (struct ieee80211_ht_addt_info *)
2172 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002173 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002174 }
2175
Jiri Bencf0706e82007-05-05 11:45:53 -07002176 rate_control_rate_init(sta, local);
2177
Johannes Berg3434fbd2008-05-03 00:59:37 +02002178 if (elems.wmm_param) {
Johannes Berg07346f812008-05-03 01:02:02 +02002179 set_sta_flags(sta, WLAN_STA_WME);
Johannes Berge5f98f22008-03-05 20:39:31 +01002180 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002181 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2182 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002183 } else
2184 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002185
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002186 /* set AID and assoc capability,
2187 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002188 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002189 bss_conf->assoc_capability = capab_info;
Johannes Berg8318d782008-01-24 19:38:38 +01002190 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002191
Jiri Bencf0706e82007-05-05 11:45:53 -07002192 ieee80211_associated(dev, ifsta);
2193}
2194
2195
2196/* Caller must hold local->sta_bss_lock */
2197static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2198 struct ieee80211_sta_bss *bss)
2199{
2200 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002201 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002202
2203 if (bss_mesh_cfg(bss))
2204 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2205 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002206 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002207 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002208
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002209 bss->hnext = local->sta_bss_hash[hash_idx];
2210 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002211}
2212
2213
2214/* Caller must hold local->sta_bss_lock */
Johannes Berg3e122be2008-07-09 14:40:34 +02002215static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -07002216 struct ieee80211_sta_bss *bss)
2217{
Jiri Bencf0706e82007-05-05 11:45:53 -07002218 struct ieee80211_sta_bss *b, *prev = NULL;
2219 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2220 while (b) {
2221 if (b == bss) {
2222 if (!prev)
2223 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2224 bss->hnext;
2225 else
2226 prev->hnext = bss->hnext;
2227 break;
2228 }
2229 prev = b;
2230 b = b->hnext;
2231 }
2232}
2233
2234
2235static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002236ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002237 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002238{
2239 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2240 struct ieee80211_sta_bss *bss;
2241
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002242 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002243 if (!bss)
2244 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002245 atomic_inc(&bss->users);
2246 atomic_inc(&bss->users);
2247 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002248 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002249 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2250 memcpy(bss->ssid, ssid, ssid_len);
2251 bss->ssid_len = ssid_len;
2252 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002253
2254 spin_lock_bh(&local->sta_bss_lock);
2255 /* TODO: order by RSSI? */
2256 list_add_tail(&bss->list, &local->sta_bss_list);
2257 __ieee80211_rx_bss_hash_add(dev, bss);
2258 spin_unlock_bh(&local->sta_bss_lock);
2259 return bss;
2260}
2261
Jiri Bencf0706e82007-05-05 11:45:53 -07002262static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002263ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002264 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002265{
2266 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2267 struct ieee80211_sta_bss *bss;
2268
2269 spin_lock_bh(&local->sta_bss_lock);
2270 bss = local->sta_bss_hash[STA_HASH(bssid)];
2271 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002272 if (!bss_mesh_cfg(bss) &&
2273 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002274 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002275 bss->ssid_len == ssid_len &&
2276 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002277 atomic_inc(&bss->users);
2278 break;
2279 }
2280 bss = bss->hnext;
2281 }
2282 spin_unlock_bh(&local->sta_bss_lock);
2283 return bss;
2284}
2285
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002286#ifdef CONFIG_MAC80211_MESH
2287static struct ieee80211_sta_bss *
2288ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2289 u8 *mesh_cfg, int freq)
2290{
2291 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2292 struct ieee80211_sta_bss *bss;
2293
2294 spin_lock_bh(&local->sta_bss_lock);
2295 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2296 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002297 if (bss_mesh_cfg(bss) &&
2298 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002299 bss->freq == freq &&
2300 mesh_id_len == bss->mesh_id_len &&
2301 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2302 mesh_id_len))) {
2303 atomic_inc(&bss->users);
2304 break;
2305 }
2306 bss = bss->hnext;
2307 }
2308 spin_unlock_bh(&local->sta_bss_lock);
2309 return bss;
2310}
2311
2312static struct ieee80211_sta_bss *
2313ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002314 u8 *mesh_cfg, int mesh_config_len, int freq)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002315{
2316 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2317 struct ieee80211_sta_bss *bss;
2318
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002319 if (mesh_config_len != MESH_CFG_LEN)
2320 return NULL;
2321
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002322 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2323 if (!bss)
2324 return NULL;
2325
John W. Linville24736702008-04-08 14:15:46 -04002326 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002327 if (!bss->mesh_cfg) {
2328 kfree(bss);
2329 return NULL;
2330 }
2331
2332 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2333 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2334 if (!bss->mesh_id) {
2335 kfree(bss->mesh_cfg);
2336 kfree(bss);
2337 return NULL;
2338 }
2339 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2340 }
2341
2342 atomic_inc(&bss->users);
2343 atomic_inc(&bss->users);
John W. Linville24736702008-04-08 14:15:46 -04002344 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002345 bss->mesh_id_len = mesh_id_len;
2346 bss->freq = freq;
2347 spin_lock_bh(&local->sta_bss_lock);
2348 /* TODO: order by RSSI? */
2349 list_add_tail(&bss->list, &local->sta_bss_list);
2350 __ieee80211_rx_bss_hash_add(dev, bss);
2351 spin_unlock_bh(&local->sta_bss_lock);
2352 return bss;
2353}
2354#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002355
2356static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2357{
2358 kfree(bss->wpa_ie);
2359 kfree(bss->rsn_ie);
2360 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002361 kfree(bss->ht_ie);
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002362 kfree(bss->ht_add_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002363 kfree(bss_mesh_id(bss));
2364 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002365 kfree(bss);
2366}
2367
2368
Johannes Berg3e122be2008-07-09 14:40:34 +02002369static void ieee80211_rx_bss_put(struct ieee80211_local *local,
Jiri Bencf0706e82007-05-05 11:45:53 -07002370 struct ieee80211_sta_bss *bss)
2371{
Pavel Emelyanov1ebebea2008-04-23 11:47:15 +04002372 local_bh_disable();
2373 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2374 local_bh_enable();
2375 return;
2376 }
2377
Johannes Berg3e122be2008-07-09 14:40:34 +02002378 __ieee80211_rx_bss_hash_del(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002379 list_del(&bss->list);
2380 spin_unlock_bh(&local->sta_bss_lock);
2381 ieee80211_rx_bss_free(bss);
2382}
2383
2384
Johannes Berg3e122be2008-07-09 14:40:34 +02002385void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
Jiri Bencf0706e82007-05-05 11:45:53 -07002386{
Jiri Bencf0706e82007-05-05 11:45:53 -07002387 spin_lock_init(&local->sta_bss_lock);
2388 INIT_LIST_HEAD(&local->sta_bss_list);
2389}
2390
2391
Johannes Berg3e122be2008-07-09 14:40:34 +02002392void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
Jiri Bencf0706e82007-05-05 11:45:53 -07002393{
Jiri Bencf0706e82007-05-05 11:45:53 -07002394 struct ieee80211_sta_bss *bss, *tmp;
2395
2396 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
Johannes Berg3e122be2008-07-09 14:40:34 +02002397 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002398}
2399
2400
Bruno Randolfa6072682008-02-18 11:21:15 +09002401static int ieee80211_sta_join_ibss(struct net_device *dev,
2402 struct ieee80211_if_sta *ifsta,
2403 struct ieee80211_sta_bss *bss)
2404{
2405 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2406 int res, rates, i, j;
2407 struct sk_buff *skb;
2408 struct ieee80211_mgmt *mgmt;
Johannes Berge039fa42008-05-15 12:55:29 +02002409 struct ieee80211_tx_info *control;
Bruno Randolfa6072682008-02-18 11:21:15 +09002410 struct rate_selection ratesel;
2411 u8 *pos;
2412 struct ieee80211_sub_if_data *sdata;
2413 struct ieee80211_supported_band *sband;
Dan Williams507b06d2008-06-03 23:39:55 -04002414 union iwreq_data wrqu;
Bruno Randolfa6072682008-02-18 11:21:15 +09002415
2416 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2417
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002418 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2419
Bruno Randolfa6072682008-02-18 11:21:15 +09002420 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002421 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002422
2423 if (local->ops->reset_tsf) {
2424 /* Reset own TSF to allow time synchronization work. */
2425 local->ops->reset_tsf(local_to_hw(local));
2426 }
2427 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2428 res = ieee80211_if_config(dev);
2429 if (res)
2430 return res;
2431
2432 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2433
Bruno Randolfa6072682008-02-18 11:21:15 +09002434 sdata->drop_unencrypted = bss->capability &
2435 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2436
Assaf Kraussbe038b32008-06-05 19:55:21 +03002437 res = ieee80211_set_freq(dev, bss->freq);
Bruno Randolfa6072682008-02-18 11:21:15 +09002438
Assaf Kraussbe038b32008-06-05 19:55:21 +03002439 if (res)
2440 return res;
Bruno Randolfa6072682008-02-18 11:21:15 +09002441
2442 /* Set beacon template */
2443 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2444 do {
2445 if (!skb)
2446 break;
2447
2448 skb_reserve(skb, local->hw.extra_tx_headroom);
2449
2450 mgmt = (struct ieee80211_mgmt *)
2451 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2452 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2453 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2454 IEEE80211_STYPE_BEACON);
2455 memset(mgmt->da, 0xff, ETH_ALEN);
2456 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2457 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2458 mgmt->u.beacon.beacon_int =
2459 cpu_to_le16(local->hw.conf.beacon_int);
Assaf Krauss4faeb862008-06-30 17:23:16 +08002460 mgmt->u.beacon.timestamp = cpu_to_le64(bss->timestamp);
Bruno Randolfa6072682008-02-18 11:21:15 +09002461 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2462
2463 pos = skb_put(skb, 2 + ifsta->ssid_len);
2464 *pos++ = WLAN_EID_SSID;
2465 *pos++ = ifsta->ssid_len;
2466 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2467
2468 rates = bss->supp_rates_len;
2469 if (rates > 8)
2470 rates = 8;
2471 pos = skb_put(skb, 2 + rates);
2472 *pos++ = WLAN_EID_SUPP_RATES;
2473 *pos++ = rates;
2474 memcpy(pos, bss->supp_rates, rates);
2475
2476 if (bss->band == IEEE80211_BAND_2GHZ) {
2477 pos = skb_put(skb, 2 + 1);
2478 *pos++ = WLAN_EID_DS_PARAMS;
2479 *pos++ = 1;
2480 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2481 }
2482
2483 pos = skb_put(skb, 2 + 2);
2484 *pos++ = WLAN_EID_IBSS_PARAMS;
2485 *pos++ = 2;
2486 /* FIX: set ATIM window based on scan results */
2487 *pos++ = 0;
2488 *pos++ = 0;
2489
2490 if (bss->supp_rates_len > 8) {
2491 rates = bss->supp_rates_len - 8;
2492 pos = skb_put(skb, 2 + rates);
2493 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2494 *pos++ = rates;
2495 memcpy(pos, &bss->supp_rates[8], rates);
2496 }
2497
Johannes Berge039fa42008-05-15 12:55:29 +02002498 control = IEEE80211_SKB_CB(skb);
2499
Bruno Randolfa6072682008-02-18 11:21:15 +09002500 rate_control_get_rate(dev, sband, skb, &ratesel);
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002501 if (ratesel.rate_idx < 0) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002502 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2503 "for IBSS beacon\n", dev->name);
2504 break;
2505 }
Johannes Berge039fa42008-05-15 12:55:29 +02002506 control->control.vif = &sdata->vif;
2507 control->tx_rate_idx = ratesel.rate_idx;
Bruno Randolfa6072682008-02-18 11:21:15 +09002508 if (sdata->bss_conf.use_short_preamble &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002509 sband->bitrates[ratesel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
Johannes Berge039fa42008-05-15 12:55:29 +02002510 control->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
2511 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2512 control->flags |= IEEE80211_TX_CTL_NO_ACK;
Ivo van Doornb2898a22008-07-01 10:45:13 +02002513 control->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
Johannes Berge039fa42008-05-15 12:55:29 +02002514 control->control.retry_limit = 1;
Bruno Randolfa6072682008-02-18 11:21:15 +09002515
2516 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2517 if (ifsta->probe_resp) {
2518 mgmt = (struct ieee80211_mgmt *)
2519 ifsta->probe_resp->data;
2520 mgmt->frame_control =
2521 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2522 IEEE80211_STYPE_PROBE_RESP);
2523 } else {
2524 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2525 "template for IBSS\n", dev->name);
2526 }
2527
2528 if (local->ops->beacon_update &&
Johannes Berge039fa42008-05-15 12:55:29 +02002529 local->ops->beacon_update(local_to_hw(local), skb) == 0) {
Bruno Randolfa6072682008-02-18 11:21:15 +09002530 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2531 "template\n", dev->name);
2532 skb = NULL;
2533 }
2534
2535 rates = 0;
2536 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2537 for (i = 0; i < bss->supp_rates_len; i++) {
2538 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2539 for (j = 0; j < sband->n_bitrates; j++)
2540 if (sband->bitrates[j].bitrate == bitrate)
2541 rates |= BIT(j);
2542 }
2543 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Vladimir Koutnye2839d82008-03-18 21:14:07 +01002544
2545 ieee80211_sta_def_wmm_params(dev, bss, 1);
Bruno Randolfa6072682008-02-18 11:21:15 +09002546 } while (0);
2547
2548 if (skb) {
2549 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2550 "template\n", dev->name);
2551 dev_kfree_skb(skb);
2552 }
2553
2554 ifsta->state = IEEE80211_IBSS_JOINED;
2555 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2556
Dan Williams507b06d2008-06-03 23:39:55 -04002557 memset(&wrqu, 0, sizeof(wrqu));
2558 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2559 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Bruno Randolfa6072682008-02-18 11:21:15 +09002560
2561 return res;
2562}
2563
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002564u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2565 struct ieee802_11_elems *elems,
2566 enum ieee80211_band band)
2567{
2568 struct ieee80211_supported_band *sband;
2569 struct ieee80211_rate *bitrates;
2570 size_t num_rates;
2571 u64 supp_rates;
2572 int i, j;
2573 sband = local->hw.wiphy->bands[band];
2574
2575 if (!sband) {
2576 WARN_ON(1);
2577 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2578 }
2579
2580 bitrates = sband->bitrates;
2581 num_rates = sband->n_bitrates;
2582 supp_rates = 0;
2583 for (i = 0; i < elems->supp_rates_len +
2584 elems->ext_supp_rates_len; i++) {
2585 u8 rate = 0;
2586 int own_rate;
2587 if (i < elems->supp_rates_len)
2588 rate = elems->supp_rates[i];
2589 else if (elems->ext_supp_rates)
2590 rate = elems->ext_supp_rates
2591 [i - elems->supp_rates_len];
2592 own_rate = 5 * (rate & 0x7f);
2593 for (j = 0; j < num_rates; j++)
2594 if (bitrates[j].bitrate == own_rate)
2595 supp_rates |= BIT(j);
2596 }
2597 return supp_rates;
2598}
2599
Bruno Randolfa6072682008-02-18 11:21:15 +09002600
Jiri Bencf0706e82007-05-05 11:45:53 -07002601static void ieee80211_rx_bss_info(struct net_device *dev,
2602 struct ieee80211_mgmt *mgmt,
2603 size_t len,
2604 struct ieee80211_rx_status *rx_status,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002605 struct ieee802_11_elems *elems,
Jiri Bencf0706e82007-05-05 11:45:53 -07002606 int beacon)
2607{
2608 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +01002609 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002610 struct ieee80211_sta_bss *bss;
2611 struct sta_info *sta;
2612 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002613 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002614 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002615 DECLARE_MAC_BUF(mac);
2616 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002617
2618 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2619 return; /* ignore ProbeResp to foreign address */
2620
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002621 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
Jiri Bencf0706e82007-05-05 11:45:53 -07002622
Ester Kummerae6a44e2008-06-27 18:54:48 +03002623 if (ieee80211_vif_is_mesh(&sdata->vif) && elems->mesh_id &&
2624 elems->mesh_config && mesh_matches_local(elems, dev)) {
2625 u64 rates = ieee80211_sta_get_rates(local, elems,
Johannes Berg902acc72008-02-23 15:17:19 +01002626 rx_status->band);
2627
2628 mesh_neighbour_update(mgmt->sa, rates, dev,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002629 mesh_peer_accepts_plinks(elems, dev));
Johannes Berg902acc72008-02-23 15:17:19 +01002630 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002631
Johannes Bergd0709a62008-02-25 16:27:46 +01002632 rcu_read_lock();
2633
Ester Kummerae6a44e2008-06-27 18:54:48 +03002634 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems->supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002635 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2636 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002637 u64 prev_rates;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002638 u64 supp_rates = ieee80211_sta_get_rates(local, elems,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002639 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002640
Johannes Berg8318d782008-01-24 19:38:38 +01002641 prev_rates = sta->supp_rates[rx_status->band];
2642 sta->supp_rates[rx_status->band] &= supp_rates;
2643 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002644 /* No matching rates - this should not really happen.
2645 * Make sure that at least one rate is marked
2646 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002647 sta->supp_rates[rx_status->band] =
2648 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002649 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002650 }
2651
Johannes Bergd0709a62008-02-25 16:27:46 +01002652 rcu_read_unlock();
2653
Ester Kummerae6a44e2008-06-27 18:54:48 +03002654 if (elems->ds_params && elems->ds_params_len == 1)
2655 freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002656 else
Johannes Berg8318d782008-01-24 19:38:38 +01002657 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002658
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002659 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2660
2661 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2662 return;
2663
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002664#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002665 if (elems->mesh_config)
2666 bss = ieee80211_rx_mesh_bss_get(dev, elems->mesh_id,
2667 elems->mesh_id_len, elems->mesh_config, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002668 else
2669#endif
2670 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002671 elems->ssid, elems->ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002672 if (!bss) {
2673#ifdef CONFIG_MAC80211_MESH
Ester Kummerae6a44e2008-06-27 18:54:48 +03002674 if (elems->mesh_config)
2675 bss = ieee80211_rx_mesh_bss_add(dev, elems->mesh_id,
2676 elems->mesh_id_len, elems->mesh_config,
2677 elems->mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002678 else
2679#endif
2680 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
Ester Kummerae6a44e2008-06-27 18:54:48 +03002681 elems->ssid, elems->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002682 if (!bss)
2683 return;
2684 } else {
2685#if 0
2686 /* TODO: order by RSSI? */
2687 spin_lock_bh(&local->sta_bss_lock);
2688 list_move_tail(&bss->list, &local->sta_bss_list);
2689 spin_unlock_bh(&local->sta_bss_lock);
2690#endif
2691 }
2692
Daniel Drake56282212007-07-10 19:32:10 +02002693 /* save the ERP value so that it is available at association time */
Ester Kummerae6a44e2008-06-27 18:54:48 +03002694 if (elems->erp_info && elems->erp_info_len >= 1) {
2695 bss->erp_value = elems->erp_info[0];
Daniel Drake56282212007-07-10 19:32:10 +02002696 bss->has_erp_value = 1;
2697 }
2698
Ester Kummerae6a44e2008-06-27 18:54:48 +03002699 if (elems->ht_cap_elem &&
2700 (!bss->ht_ie || bss->ht_ie_len != elems->ht_cap_elem_len ||
2701 memcmp(bss->ht_ie, elems->ht_cap_elem, elems->ht_cap_elem_len))) {
Johannes Berg30b89b02008-04-16 17:43:20 +02002702 kfree(bss->ht_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002703 bss->ht_ie = kmalloc(elems->ht_cap_elem_len + 2, GFP_ATOMIC);
Johannes Berg30b89b02008-04-16 17:43:20 +02002704 if (bss->ht_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002705 memcpy(bss->ht_ie, elems->ht_cap_elem - 2,
2706 elems->ht_cap_elem_len + 2);
2707 bss->ht_ie_len = elems->ht_cap_elem_len + 2;
Johannes Berg30b89b02008-04-16 17:43:20 +02002708 } else
2709 bss->ht_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002710 } else if (!elems->ht_cap_elem && bss->ht_ie) {
Johannes Berg30b89b02008-04-16 17:43:20 +02002711 kfree(bss->ht_ie);
2712 bss->ht_ie = NULL;
2713 bss->ht_ie_len = 0;
2714 }
2715
Ester Kummerae6a44e2008-06-27 18:54:48 +03002716 if (elems->ht_info_elem &&
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002717 (!bss->ht_add_ie ||
Ester Kummerae6a44e2008-06-27 18:54:48 +03002718 bss->ht_add_ie_len != elems->ht_info_elem_len ||
2719 memcmp(bss->ht_add_ie, elems->ht_info_elem,
2720 elems->ht_info_elem_len))) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002721 kfree(bss->ht_add_ie);
2722 bss->ht_add_ie =
Ester Kummerae6a44e2008-06-27 18:54:48 +03002723 kmalloc(elems->ht_info_elem_len + 2, GFP_ATOMIC);
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002724 if (bss->ht_add_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002725 memcpy(bss->ht_add_ie, elems->ht_info_elem - 2,
2726 elems->ht_info_elem_len + 2);
2727 bss->ht_add_ie_len = elems->ht_info_elem_len + 2;
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002728 } else
2729 bss->ht_add_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002730 } else if (!elems->ht_info_elem && bss->ht_add_ie) {
Emmanuel Grumbach93061022008-05-29 16:35:23 +08002731 kfree(bss->ht_add_ie);
2732 bss->ht_add_ie = NULL;
2733 bss->ht_add_ie_len = 0;
2734 }
2735
Jiri Bencf0706e82007-05-05 11:45:53 -07002736 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2737 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002738
2739 bss->supp_rates_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002740 if (elems->supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002741 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002742 if (clen > elems->supp_rates_len)
2743 clen = elems->supp_rates_len;
2744 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
Jiri Bencf0706e82007-05-05 11:45:53 -07002745 clen);
2746 bss->supp_rates_len += clen;
2747 }
Ester Kummerae6a44e2008-06-27 18:54:48 +03002748 if (elems->ext_supp_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002749 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002750 if (clen > elems->ext_supp_rates_len)
2751 clen = elems->ext_supp_rates_len;
Jiri Bencf0706e82007-05-05 11:45:53 -07002752 memcpy(&bss->supp_rates[bss->supp_rates_len],
Ester Kummerae6a44e2008-06-27 18:54:48 +03002753 elems->ext_supp_rates, clen);
Jiri Bencf0706e82007-05-05 11:45:53 -07002754 bss->supp_rates_len += clen;
2755 }
2756
Johannes Berg30b89b02008-04-16 17:43:20 +02002757 bss->band = rx_status->band;
2758
2759 bss->timestamp = beacon_timestamp;
2760 bss->last_update = jiffies;
Johannes Berg30b89b02008-04-16 17:43:20 +02002761 bss->signal = rx_status->signal;
2762 bss->noise = rx_status->noise;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002763 bss->qual = rx_status->qual;
Johannes Berg30b89b02008-04-16 17:43:20 +02002764 if (!beacon && !bss->probe_resp)
2765 bss->probe_resp = true;
2766
2767 /*
2768 * In STA mode, the remaining parameters should not be overridden
2769 * by beacons because they're not necessarily accurate there.
2770 */
2771 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2772 bss->probe_resp && beacon) {
Johannes Berg3e122be2008-07-09 14:40:34 +02002773 ieee80211_rx_bss_put(local, bss);
Johannes Berg30b89b02008-04-16 17:43:20 +02002774 return;
2775 }
2776
Ester Kummerae6a44e2008-06-27 18:54:48 +03002777 if (elems->wpa &&
2778 (!bss->wpa_ie || bss->wpa_ie_len != elems->wpa_len ||
2779 memcmp(bss->wpa_ie, elems->wpa, elems->wpa_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002780 kfree(bss->wpa_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002781 bss->wpa_ie = kmalloc(elems->wpa_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002782 if (bss->wpa_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002783 memcpy(bss->wpa_ie, elems->wpa - 2, elems->wpa_len + 2);
2784 bss->wpa_ie_len = elems->wpa_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002785 } else
2786 bss->wpa_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002787 } else if (!elems->wpa && bss->wpa_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002788 kfree(bss->wpa_ie);
2789 bss->wpa_ie = NULL;
2790 bss->wpa_ie_len = 0;
2791 }
2792
Ester Kummerae6a44e2008-06-27 18:54:48 +03002793 if (elems->rsn &&
2794 (!bss->rsn_ie || bss->rsn_ie_len != elems->rsn_len ||
2795 memcmp(bss->rsn_ie, elems->rsn, elems->rsn_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002796 kfree(bss->rsn_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002797 bss->rsn_ie = kmalloc(elems->rsn_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002798 if (bss->rsn_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002799 memcpy(bss->rsn_ie, elems->rsn - 2, elems->rsn_len + 2);
2800 bss->rsn_ie_len = elems->rsn_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002801 } else
2802 bss->rsn_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002803 } else if (!elems->rsn && bss->rsn_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002804 kfree(bss->rsn_ie);
2805 bss->rsn_ie = NULL;
2806 bss->rsn_ie_len = 0;
2807 }
2808
Johannes Berg30b89b02008-04-16 17:43:20 +02002809 /*
2810 * Cf.
2811 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2812 *
2813 * quoting:
2814 *
2815 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2816 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2817 * Alliance (September 1, 2004) is incorporated by reference herein.
2818 * The inclusion of the WMM Parameters in probe responses and
2819 * association responses is mandatory for WMM enabled networks. The
2820 * inclusion of the WMM Parameters in beacons, however, is optional.
2821 */
2822
Ester Kummerae6a44e2008-06-27 18:54:48 +03002823 if (elems->wmm_param &&
2824 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_param_len ||
2825 memcmp(bss->wmm_ie, elems->wmm_param, elems->wmm_param_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002826 kfree(bss->wmm_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002827 bss->wmm_ie = kmalloc(elems->wmm_param_len + 2, GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002828 if (bss->wmm_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002829 memcpy(bss->wmm_ie, elems->wmm_param - 2,
2830 elems->wmm_param_len + 2);
2831 bss->wmm_ie_len = elems->wmm_param_len + 2;
Jiri Bencf0706e82007-05-05 11:45:53 -07002832 } else
2833 bss->wmm_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002834 } else if (elems->wmm_info &&
2835 (!bss->wmm_ie || bss->wmm_ie_len != elems->wmm_info_len ||
2836 memcmp(bss->wmm_ie, elems->wmm_info,
2837 elems->wmm_info_len))) {
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002838 /* As for certain AP's Fifth bit is not set in WMM IE in
2839 * beacon frames.So while parsing the beacon frame the
2840 * wmm_info structure is used instead of wmm_param.
2841 * wmm_info structure was never used to set bss->wmm_ie.
2842 * This code fixes this problem by copying the WME
2843 * information from wmm_info to bss->wmm_ie and enabling
2844 * n-band association.
2845 */
2846 kfree(bss->wmm_ie);
Ester Kummerae6a44e2008-06-27 18:54:48 +03002847 bss->wmm_ie = kmalloc(elems->wmm_info_len + 2, GFP_ATOMIC);
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002848 if (bss->wmm_ie) {
Ester Kummerae6a44e2008-06-27 18:54:48 +03002849 memcpy(bss->wmm_ie, elems->wmm_info - 2,
2850 elems->wmm_info_len + 2);
2851 bss->wmm_ie_len = elems->wmm_info_len + 2;
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002852 } else
2853 bss->wmm_ie_len = 0;
Ester Kummerae6a44e2008-06-27 18:54:48 +03002854 } else if (!elems->wmm_param && !elems->wmm_info && bss->wmm_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002855 kfree(bss->wmm_ie);
2856 bss->wmm_ie = NULL;
2857 bss->wmm_ie_len = 0;
2858 }
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002859
2860 /* check if we need to merge IBSS */
2861 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2862 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002863 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002864 bss->freq == local->oper_channel->center_freq &&
Ester Kummerae6a44e2008-06-27 18:54:48 +03002865 elems->ssid_len == sdata->u.sta.ssid_len &&
2866 memcmp(elems->ssid, sdata->u.sta.ssid,
2867 sdata->u.sta.ssid_len) == 0) {
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002868 if (rx_status->flag & RX_FLAG_TSFT) {
2869 /* in order for correct IBSS merging we need mactime
2870 *
2871 * since mactime is defined as the time the first data
2872 * symbol of the frame hits the PHY, and the timestamp
2873 * of the beacon is defined as "the time that the data
2874 * symbol containing the first bit of the timestamp is
2875 * transmitted to the PHY plus the transmitting STA’s
2876 * delays through its local PHY from the MAC-PHY
2877 * interface to its interface with the WM"
2878 * (802.11 11.1.2) - equals the time this bit arrives at
2879 * the receiver - we have to take into account the
2880 * offset between the two.
2881 * e.g: at 1 MBit that means mactime is 192 usec earlier
2882 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2883 */
2884 int rate = local->hw.wiphy->bands[rx_status->band]->
2885 bitrates[rx_status->rate_idx].bitrate;
2886 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2887 } else if (local && local->ops && local->ops->get_tsf)
2888 /* second best option: get current TSF */
2889 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2890 else
2891 /* can't merge without knowing the TSF */
2892 rx_timestamp = -1LLU;
2893#ifdef CONFIG_MAC80211_IBSS_DEBUG
2894 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2895 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2896 print_mac(mac, mgmt->sa),
2897 print_mac(mac2, mgmt->bssid),
2898 (unsigned long long)rx_timestamp,
2899 (unsigned long long)beacon_timestamp,
2900 (unsigned long long)(rx_timestamp - beacon_timestamp),
2901 jiffies);
2902#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2903 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002904#ifndef CONFIG_MAC80211_IBSS_DEBUG
Johannes Bergf4ea83d2008-06-30 15:10:46 +02002905 printk(KERN_DEBUG "%s: beacon TSF higher than "
2906 "local TSF - IBSS merge with BSSID %s\n",
2907 dev->name, print_mac(mac, mgmt->bssid));
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002908#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002909 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2910 ieee80211_ibss_add_sta(dev, NULL,
Vladimir Koutny87291c02008-06-13 16:50:44 +02002911 mgmt->bssid, mgmt->sa,
2912 BIT(rx_status->rate_idx));
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002913 }
2914 }
2915
Johannes Berg3e122be2008-07-09 14:40:34 +02002916 ieee80211_rx_bss_put(local, bss);
Jiri Bencf0706e82007-05-05 11:45:53 -07002917}
2918
2919
2920static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2921 struct ieee80211_mgmt *mgmt,
2922 size_t len,
2923 struct ieee80211_rx_status *rx_status)
2924{
Ester Kummerae6a44e2008-06-27 18:54:48 +03002925 size_t baselen;
2926 struct ieee802_11_elems elems;
2927
2928 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
2929 if (baselen > len)
2930 return;
2931
2932 ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
2933 &elems);
2934
2935 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002936}
2937
2938
2939static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2940 struct ieee80211_mgmt *mgmt,
2941 size_t len,
2942 struct ieee80211_rx_status *rx_status)
2943{
Jiri Bencf0706e82007-05-05 11:45:53 -07002944 struct ieee80211_sub_if_data *sdata;
2945 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002946 size_t baselen;
2947 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002948 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2949 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002950 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002951
Ester Kummerae6a44e2008-06-27 18:54:48 +03002952 /* Process beacon from the current BSS */
2953 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2954 if (baselen > len)
2955 return;
2956
2957 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
2958
2959 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, &elems, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002960
2961 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002962 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002963 return;
2964 ifsta = &sdata->u.sta;
2965
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002966 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002967 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2968 return;
2969
Johannes Berg3434fbd2008-05-03 00:59:37 +02002970 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2971 elems.wmm_param_len);
Reinette Chatred18ef292008-04-09 16:56:15 -07002972
2973 /* Do not send changes to driver if we are scanning. This removes
2974 * requirement that driver's bss_info_changed function needs to be
2975 * atomic. */
2976 if (local->sta_sw_scanning || local->sta_hw_scanning)
2977 return;
2978
Daniel Drake56282212007-07-10 19:32:10 +02002979 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002980 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
John W. Linville50c4afb2008-04-15 14:09:27 -04002981 else {
2982 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2983 changed |= ieee80211_handle_protect_preamb(sdata, false,
2984 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2985 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002986
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002987 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07002988 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002989 struct ieee80211_ht_bss_info bss_info;
2990
2991 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2992 (struct ieee80211_ht_addt_info *)
2993 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002994 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2995 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002996 }
2997
Johannes Berg471b3ef2007-12-28 14:32:58 +01002998 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002999}
3000
3001
3002static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
3003 struct ieee80211_if_sta *ifsta,
3004 struct ieee80211_mgmt *mgmt,
3005 size_t len,
3006 struct ieee80211_rx_status *rx_status)
3007{
3008 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3009 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3010 int tx_last_beacon;
3011 struct sk_buff *skb;
3012 struct ieee80211_mgmt *resp;
3013 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07003014 DECLARE_MAC_BUF(mac);
3015#ifdef CONFIG_MAC80211_IBSS_DEBUG
3016 DECLARE_MAC_BUF(mac2);
3017 DECLARE_MAC_BUF(mac3);
3018#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003019
Johannes Berg51fb61e2007-12-19 01:31:27 +01003020 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003021 ifsta->state != IEEE80211_IBSS_JOINED ||
3022 len < 24 + 2 || !ifsta->probe_resp)
3023 return;
3024
3025 if (local->ops->tx_last_beacon)
3026 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
3027 else
3028 tx_last_beacon = 1;
3029
3030#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003031 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
3032 "%s (tx_last_beacon=%d)\n",
3033 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
3034 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07003035#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3036
3037 if (!tx_last_beacon)
3038 return;
3039
3040 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
3041 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
3042 return;
3043
3044 end = ((u8 *) mgmt) + len;
3045 pos = mgmt->u.probe_req.variable;
3046 if (pos[0] != WLAN_EID_SSID ||
3047 pos + 2 + pos[1] > end) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003048#ifdef CONFIG_MAC80211_IBSS_DEBUG
3049 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
3050 "from %s\n",
3051 dev->name, print_mac(mac, mgmt->sa));
3052#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003053 return;
3054 }
3055 if (pos[1] != 0 &&
3056 (pos[1] != ifsta->ssid_len ||
3057 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
3058 /* Ignore ProbeReq for foreign SSID */
3059 return;
3060 }
3061
3062 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02003063 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003064 if (!skb)
3065 return;
3066
3067 resp = (struct ieee80211_mgmt *) skb->data;
3068 memcpy(resp->da, mgmt->sa, ETH_ALEN);
3069#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003070 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
3071 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07003072#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3073 ieee80211_sta_tx(dev, skb, 0);
3074}
3075
Johannes Berg4e20cb22007-12-19 01:31:24 +01003076static void ieee80211_rx_mgmt_action(struct net_device *dev,
3077 struct ieee80211_if_sta *ifsta,
3078 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003079 size_t len,
3080 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003081{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003082 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Assaf Kraussb6623482008-06-16 16:09:49 +03003083 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003084
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003085 if (len < IEEE80211_MIN_ACTION_SIZE)
3086 return;
3087
3088 switch (mgmt->u.action.category) {
Assaf Kraussb6623482008-06-16 16:09:49 +03003089 case WLAN_CATEGORY_SPECTRUM_MGMT:
3090 if (local->hw.conf.channel->band != IEEE80211_BAND_5GHZ)
3091 break;
3092 switch (mgmt->u.action.u.chan_switch.action_code) {
3093 case WLAN_ACTION_SPCT_MSR_REQ:
3094 if (len < (IEEE80211_MIN_ACTION_SIZE +
3095 sizeof(mgmt->u.action.u.measurement)))
3096 break;
3097 ieee80211_sta_process_measurement_req(dev, mgmt, len);
3098 break;
3099 }
3100 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003101 case WLAN_CATEGORY_BACK:
3102 switch (mgmt->u.action.u.addba_req.action_code) {
3103 case WLAN_ACTION_ADDBA_REQ:
3104 if (len < (IEEE80211_MIN_ACTION_SIZE +
3105 sizeof(mgmt->u.action.u.addba_req)))
3106 break;
3107 ieee80211_sta_process_addba_request(dev, mgmt, len);
3108 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02003109 case WLAN_ACTION_ADDBA_RESP:
3110 if (len < (IEEE80211_MIN_ACTION_SIZE +
3111 sizeof(mgmt->u.action.u.addba_resp)))
3112 break;
3113 ieee80211_sta_process_addba_resp(dev, mgmt, len);
3114 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02003115 case WLAN_ACTION_DELBA:
3116 if (len < (IEEE80211_MIN_ACTION_SIZE +
3117 sizeof(mgmt->u.action.u.delba)))
3118 break;
3119 ieee80211_sta_process_delba(dev, mgmt, len);
3120 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003121 }
3122 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003123 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003124 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003125 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
3126 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003127 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01003128 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003129 mesh_rx_path_sel_frame(dev, mgmt, len);
3130 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003131 }
3132}
Jiri Bencf0706e82007-05-05 11:45:53 -07003133
3134void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
3135 struct ieee80211_rx_status *rx_status)
3136{
3137 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3138 struct ieee80211_sub_if_data *sdata;
3139 struct ieee80211_if_sta *ifsta;
3140 struct ieee80211_mgmt *mgmt;
3141 u16 fc;
3142
3143 if (skb->len < 24)
3144 goto fail;
3145
3146 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3147 ifsta = &sdata->u.sta;
3148
3149 mgmt = (struct ieee80211_mgmt *) skb->data;
3150 fc = le16_to_cpu(mgmt->frame_control);
3151
3152 switch (fc & IEEE80211_FCTL_STYPE) {
3153 case IEEE80211_STYPE_PROBE_REQ:
3154 case IEEE80211_STYPE_PROBE_RESP:
3155 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003156 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07003157 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3158 case IEEE80211_STYPE_AUTH:
3159 case IEEE80211_STYPE_ASSOC_RESP:
3160 case IEEE80211_STYPE_REASSOC_RESP:
3161 case IEEE80211_STYPE_DEAUTH:
3162 case IEEE80211_STYPE_DISASSOC:
3163 skb_queue_tail(&ifsta->skb_queue, skb);
3164 queue_work(local->hw.workqueue, &ifsta->work);
3165 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003166 }
3167
3168 fail:
3169 kfree_skb(skb);
3170}
3171
3172
3173static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3174 struct sk_buff *skb)
3175{
3176 struct ieee80211_rx_status *rx_status;
3177 struct ieee80211_sub_if_data *sdata;
3178 struct ieee80211_if_sta *ifsta;
3179 struct ieee80211_mgmt *mgmt;
3180 u16 fc;
3181
3182 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3183 ifsta = &sdata->u.sta;
3184
3185 rx_status = (struct ieee80211_rx_status *) skb->cb;
3186 mgmt = (struct ieee80211_mgmt *) skb->data;
3187 fc = le16_to_cpu(mgmt->frame_control);
3188
3189 switch (fc & IEEE80211_FCTL_STYPE) {
3190 case IEEE80211_STYPE_PROBE_REQ:
3191 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3192 rx_status);
3193 break;
3194 case IEEE80211_STYPE_PROBE_RESP:
3195 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3196 break;
3197 case IEEE80211_STYPE_BEACON:
3198 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3199 break;
3200 case IEEE80211_STYPE_AUTH:
3201 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3202 break;
3203 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003204 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003205 break;
3206 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003207 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003208 break;
3209 case IEEE80211_STYPE_DEAUTH:
3210 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3211 break;
3212 case IEEE80211_STYPE_DISASSOC:
3213 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3214 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003215 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003216 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003217 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07003218 }
3219
3220 kfree_skb(skb);
3221}
3222
3223
Johannes Berg9ae54c82008-01-31 19:48:20 +01003224ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003225ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3226 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003227{
3228 struct ieee80211_mgmt *mgmt;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003229 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003230
Zhu Yiece8edd2007-11-22 10:53:21 +08003231 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003232 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003233
3234 mgmt = (struct ieee80211_mgmt *) skb->data;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003235 fc = mgmt->frame_control;
Jiri Bencf0706e82007-05-05 11:45:53 -07003236
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003237 if (ieee80211_is_ctl(fc))
Johannes Berg9ae54c82008-01-31 19:48:20 +01003238 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003239
3240 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003241 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003242
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003243 if (ieee80211_is_probe_resp(fc)) {
3244 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3245 dev_kfree_skb(skb);
3246 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003247 }
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003248
3249 if (ieee80211_is_beacon(fc)) {
3250 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3251 dev_kfree_skb(skb);
3252 return RX_QUEUED;
3253 }
3254
Johannes Berg9ae54c82008-01-31 19:48:20 +01003255 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003256}
3257
3258
3259static int ieee80211_sta_active_ibss(struct net_device *dev)
3260{
3261 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3262 int active = 0;
3263 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003264 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003265
Johannes Bergd0709a62008-02-25 16:27:46 +01003266 rcu_read_lock();
3267
3268 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3269 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003270 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3271 jiffies)) {
3272 active++;
3273 break;
3274 }
3275 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003276
3277 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003278
3279 return active;
3280}
3281
3282
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003283static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003284{
3285 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3286 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003287 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003288 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003289 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003290
Johannes Bergd0709a62008-02-25 16:27:46 +01003291 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003292 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003293 if (time_after(jiffies, sta->last_rx + exp_time)) {
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003294#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003295 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3296 dev->name, print_mac(mac, sta->addr));
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003297#endif
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003298 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003299 if (sta)
3300 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003301 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003302 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003303
Johannes Bergd0709a62008-02-25 16:27:46 +01003304 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3305 sta_info_destroy(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003306}
3307
3308
3309static void ieee80211_sta_merge_ibss(struct net_device *dev,
3310 struct ieee80211_if_sta *ifsta)
3311{
3312 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3313
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003314 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07003315 if (ieee80211_sta_active_ibss(dev))
3316 return;
3317
3318 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3319 "IBSS networks with same SSID (merge)\n", dev->name);
3320 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3321}
3322
3323
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003324#ifdef CONFIG_MAC80211_MESH
3325static void ieee80211_mesh_housekeeping(struct net_device *dev,
3326 struct ieee80211_if_sta *ifsta)
3327{
3328 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3329 bool free_plinks;
3330
3331 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3332 mesh_path_expire(dev);
3333
3334 free_plinks = mesh_plink_availables(sdata);
3335 if (free_plinks != sdata->u.sta.accepting_plinks)
3336 ieee80211_if_config_beacon(dev);
3337
3338 mod_timer(&ifsta->timer, jiffies +
3339 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3340}
3341
3342
3343void ieee80211_start_mesh(struct net_device *dev)
3344{
3345 struct ieee80211_if_sta *ifsta;
3346 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3347 ifsta = &sdata->u.sta;
3348 ifsta->state = IEEE80211_MESH_UP;
3349 ieee80211_sta_timer((unsigned long)sdata);
3350}
3351#endif
3352
3353
Jiri Bencf0706e82007-05-05 11:45:53 -07003354void ieee80211_sta_timer(unsigned long data)
3355{
3356 struct ieee80211_sub_if_data *sdata =
3357 (struct ieee80211_sub_if_data *) data;
3358 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3359 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3360
3361 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3362 queue_work(local->hw.workqueue, &ifsta->work);
3363}
3364
Jiri Bencf0706e82007-05-05 11:45:53 -07003365void ieee80211_sta_work(struct work_struct *work)
3366{
3367 struct ieee80211_sub_if_data *sdata =
3368 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3369 struct net_device *dev = sdata->dev;
3370 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3371 struct ieee80211_if_sta *ifsta;
3372 struct sk_buff *skb;
3373
3374 if (!netif_running(dev))
3375 return;
3376
Zhu Yiece8edd2007-11-22 10:53:21 +08003377 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003378 return;
3379
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003380 if (WARN_ON(sdata->vif.type != IEEE80211_IF_TYPE_STA &&
3381 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3382 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT))
Jiri Bencf0706e82007-05-05 11:45:53 -07003383 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07003384 ifsta = &sdata->u.sta;
3385
3386 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3387 ieee80211_sta_rx_queued_mgmt(dev, skb);
3388
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003389#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003390 if (ifsta->preq_queue_len &&
3391 time_after(jiffies,
3392 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003393 mesh_path_start_discovery(dev);
3394#endif
3395
Jiri Bencf0706e82007-05-05 11:45:53 -07003396 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3397 ifsta->state != IEEE80211_ASSOCIATE &&
3398 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003399 if (ifsta->scan_ssid_len)
3400 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3401 else
3402 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003403 return;
3404 }
3405
3406 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3407 if (ieee80211_sta_config_auth(dev, ifsta))
3408 return;
3409 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3410 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3411 return;
3412
3413 switch (ifsta->state) {
3414 case IEEE80211_DISABLED:
3415 break;
3416 case IEEE80211_AUTHENTICATE:
3417 ieee80211_authenticate(dev, ifsta);
3418 break;
3419 case IEEE80211_ASSOCIATE:
3420 ieee80211_associate(dev, ifsta);
3421 break;
3422 case IEEE80211_ASSOCIATED:
3423 ieee80211_associated(dev, ifsta);
3424 break;
3425 case IEEE80211_IBSS_SEARCH:
3426 ieee80211_sta_find_ibss(dev, ifsta);
3427 break;
3428 case IEEE80211_IBSS_JOINED:
3429 ieee80211_sta_merge_ibss(dev, ifsta);
3430 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003431#ifdef CONFIG_MAC80211_MESH
3432 case IEEE80211_MESH_UP:
3433 ieee80211_mesh_housekeeping(dev, ifsta);
3434 break;
3435#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003436 default:
Johannes Bergf4ea83d2008-06-30 15:10:46 +02003437 WARN_ON(1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003438 break;
3439 }
3440
3441 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3442 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3443 "mixed-cell disabled - disassociate\n", dev->name);
3444
3445 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3446 ieee80211_set_disassoc(dev, ifsta, 0);
3447 }
3448}
3449
3450
3451static void ieee80211_sta_reset_auth(struct net_device *dev,
3452 struct ieee80211_if_sta *ifsta)
3453{
3454 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3455
3456 if (local->ops->reset_tsf) {
3457 /* Reset own TSF to allow time synchronization work. */
3458 local->ops->reset_tsf(local_to_hw(local));
3459 }
3460
3461 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3462
3463
3464 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3465 ifsta->auth_alg = WLAN_AUTH_OPEN;
3466 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3467 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3468 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3469 ifsta->auth_alg = WLAN_AUTH_LEAP;
3470 else
3471 ifsta->auth_alg = WLAN_AUTH_OPEN;
Jiri Bencf0706e82007-05-05 11:45:53 -07003472 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003473 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3474 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003475 netif_carrier_off(dev);
3476}
3477
3478
3479void ieee80211_sta_req_auth(struct net_device *dev,
3480 struct ieee80211_if_sta *ifsta)
3481{
3482 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3483 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3484
Johannes Berg51fb61e2007-12-19 01:31:27 +01003485 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003486 return;
3487
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003488 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3489 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3490 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3491 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003492 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3493 queue_work(local->hw.workqueue, &ifsta->work);
3494 }
3495}
3496
3497static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3498 const char *ssid, int ssid_len)
3499{
3500 int tmp, hidden_ssid;
3501
Michael Wu48225702007-10-19 17:14:36 -04003502 if (ssid_len == ifsta->ssid_len &&
3503 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003504 return 1;
3505
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003506 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003507 return 0;
3508
3509 hidden_ssid = 1;
3510 tmp = ssid_len;
3511 while (tmp--) {
3512 if (ssid[tmp] != '\0') {
3513 hidden_ssid = 0;
3514 break;
3515 }
3516 }
3517
3518 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3519 return 1;
3520
3521 if (ssid_len == 1 && ssid[0] == ' ')
3522 return 1;
3523
3524 return 0;
3525}
3526
3527static int ieee80211_sta_config_auth(struct net_device *dev,
3528 struct ieee80211_if_sta *ifsta)
3529{
3530 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3531 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3532 struct ieee80211_sta_bss *bss, *selected = NULL;
3533 int top_rssi = 0, freq;
3534
Jiri Bencf0706e82007-05-05 11:45:53 -07003535 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003536 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003537 list_for_each_entry(bss, &local->sta_bss_list, list) {
3538 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3539 continue;
3540
Abhijeet Kolekar34a961f2008-05-09 09:35:41 -07003541 if ((ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3542 IEEE80211_STA_AUTO_BSSID_SEL |
3543 IEEE80211_STA_AUTO_CHANNEL_SEL)) &&
3544 (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3545 !!sdata->default_key))
Jiri Bencf0706e82007-05-05 11:45:53 -07003546 continue;
3547
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003548 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3549 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003550 continue;
3551
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003552 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003553 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3554 continue;
3555
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003556 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003557 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3558 continue;
3559
Bruno Randolf566bfe52008-05-08 19:15:40 +02003560 if (!selected || top_rssi < bss->signal) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003561 selected = bss;
Bruno Randolf566bfe52008-05-08 19:15:40 +02003562 top_rssi = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07003563 }
3564 }
3565 if (selected)
3566 atomic_inc(&selected->users);
3567 spin_unlock_bh(&local->sta_bss_lock);
3568
3569 if (selected) {
Assaf Kraussbe038b32008-06-05 19:55:21 +03003570 ieee80211_set_freq(dev, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003571 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003572 ieee80211_sta_set_ssid(dev, selected->ssid,
3573 selected->ssid_len);
3574 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003575 ieee80211_sta_def_wmm_params(dev, selected, 0);
Johannes Berg3e122be2008-07-09 14:40:34 +02003576 ieee80211_rx_bss_put(local, selected);
Jiri Bencf0706e82007-05-05 11:45:53 -07003577 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003578 ieee80211_sta_reset_auth(dev, ifsta);
3579 return 0;
3580 } else {
3581 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003582 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003583 ieee80211_sta_start_scan(dev, NULL, 0);
3584 else
3585 ieee80211_sta_start_scan(dev, ifsta->ssid,
3586 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003587 ifsta->state = IEEE80211_AUTHENTICATE;
3588 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3589 } else
3590 ifsta->state = IEEE80211_DISABLED;
3591 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003592 return -1;
3593}
3594
Jiri Bencf0706e82007-05-05 11:45:53 -07003595
3596static int ieee80211_sta_create_ibss(struct net_device *dev,
3597 struct ieee80211_if_sta *ifsta)
3598{
3599 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3600 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003601 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003602 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003603 u8 bssid[ETH_ALEN], *pos;
3604 int i;
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003605 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003606 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003607
3608#if 0
3609 /* Easier testing, use fixed BSSID. */
3610 memset(bssid, 0xfe, ETH_ALEN);
3611#else
3612 /* Generate random, not broadcast, locally administered BSSID. Mix in
3613 * own MAC address to make sure that devices that do not have proper
3614 * random number generator get different BSSID. */
3615 get_random_bytes(bssid, ETH_ALEN);
3616 for (i = 0; i < ETH_ALEN; i++)
3617 bssid[i] ^= dev->dev_addr[i];
3618 bssid[0] &= ~0x01;
3619 bssid[0] |= 0x02;
3620#endif
3621
Joe Perches0795af52007-10-03 17:59:30 -07003622 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3623 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003624
Johannes Berg8318d782008-01-24 19:38:38 +01003625 bss = ieee80211_rx_bss_add(dev, bssid,
3626 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003627 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003628 if (!bss)
3629 return -ENOMEM;
3630
Johannes Berg8318d782008-01-24 19:38:38 +01003631 bss->band = local->hw.conf.channel->band;
3632 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003633
3634 if (local->hw.conf.beacon_int == 0)
Tomas Winklerdc0ae302008-06-12 22:38:37 +03003635 local->hw.conf.beacon_int = 100;
Jiri Bencf0706e82007-05-05 11:45:53 -07003636 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003637 bss->last_update = jiffies;
3638 bss->capability = WLAN_CAPABILITY_IBSS;
Johannes Berg988c0f72008-04-17 19:21:22 +02003639
3640 if (sdata->default_key)
Jiri Bencf0706e82007-05-05 11:45:53 -07003641 bss->capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +02003642 else
Jiri Bencf0706e82007-05-05 11:45:53 -07003643 sdata->drop_unencrypted = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02003644
Johannes Berg8318d782008-01-24 19:38:38 +01003645 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003646 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003647 for (i = 0; i < sband->n_bitrates; i++) {
3648 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003649 *pos++ = (u8) (rate / 5);
3650 }
3651
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003652 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
Johannes Berg3e122be2008-07-09 14:40:34 +02003653 ieee80211_rx_bss_put(local, bss);
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003654 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003655}
3656
3657
3658static int ieee80211_sta_find_ibss(struct net_device *dev,
3659 struct ieee80211_if_sta *ifsta)
3660{
3661 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3662 struct ieee80211_sta_bss *bss;
3663 int found = 0;
3664 u8 bssid[ETH_ALEN];
3665 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003666 DECLARE_MAC_BUF(mac);
3667 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003668
3669 if (ifsta->ssid_len == 0)
3670 return -EINVAL;
3671
3672 active_ibss = ieee80211_sta_active_ibss(dev);
3673#ifdef CONFIG_MAC80211_IBSS_DEBUG
3674 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3675 dev->name, active_ibss);
3676#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3677 spin_lock_bh(&local->sta_bss_lock);
3678 list_for_each_entry(bss, &local->sta_bss_list, list) {
3679 if (ifsta->ssid_len != bss->ssid_len ||
3680 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3681 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3682 continue;
3683#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003684 printk(KERN_DEBUG " bssid=%s found\n",
3685 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003686#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3687 memcpy(bssid, bss->bssid, ETH_ALEN);
3688 found = 1;
3689 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3690 break;
3691 }
3692 spin_unlock_bh(&local->sta_bss_lock);
3693
3694#ifdef CONFIG_MAC80211_IBSS_DEBUG
Vladimir Koutny6e438292008-07-07 14:23:01 +02003695 if (found)
3696 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3697 "%s\n", print_mac(mac, bssid),
3698 print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003699#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3700 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003701 (bss = ieee80211_rx_bss_get(dev, bssid,
3702 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003703 ifsta->ssid, ifsta->ssid_len))) {
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003704 int ret;
Joe Perches0795af52007-10-03 17:59:30 -07003705 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003706 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003707 dev->name, print_mac(mac, bssid));
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003708 ret = ieee80211_sta_join_ibss(dev, ifsta, bss);
Johannes Berg3e122be2008-07-09 14:40:34 +02003709 ieee80211_rx_bss_put(local, bss);
Tomas Winkler167ad6f2008-05-21 18:17:05 +03003710 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07003711 }
3712#ifdef CONFIG_MAC80211_IBSS_DEBUG
3713 printk(KERN_DEBUG " did not try to join ibss\n");
3714#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3715
3716 /* Selected IBSS not found in current scan results - try to scan */
3717 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3718 !ieee80211_sta_active_ibss(dev)) {
3719 mod_timer(&ifsta->timer, jiffies +
3720 IEEE80211_IBSS_MERGE_INTERVAL);
3721 } else if (time_after(jiffies, local->last_scan_completed +
3722 IEEE80211_SCAN_INTERVAL)) {
3723 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3724 "join\n", dev->name);
3725 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3726 ifsta->ssid_len);
3727 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3728 int interval = IEEE80211_SCAN_INTERVAL;
3729
3730 if (time_after(jiffies, ifsta->ibss_join_req +
3731 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003732 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003733 (!(local->oper_channel->flags &
3734 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003735 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003736 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003737 printk(KERN_DEBUG "%s: IBSS not allowed on"
3738 " %d MHz\n", dev->name,
3739 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003740 }
3741
3742 /* No IBSS found - decrease scan interval and continue
3743 * scanning. */
3744 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3745 }
3746
3747 ifsta->state = IEEE80211_IBSS_SEARCH;
3748 mod_timer(&ifsta->timer, jiffies + interval);
3749 return 0;
3750 }
3751
3752 return 0;
3753}
3754
3755
3756int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3757{
Johannes Berg8318d782008-01-24 19:38:38 +01003758 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003759 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07003760
3761 if (len > IEEE80211_MAX_SSID_LEN)
3762 return -EINVAL;
3763
Jiri Bencf0706e82007-05-05 11:45:53 -07003764 ifsta = &sdata->u.sta;
3765
3766 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003767 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003768 memcpy(ifsta->ssid, ssid, len);
3769 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3770 ifsta->ssid_len = len;
3771
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003772 if (len)
3773 ifsta->flags |= IEEE80211_STA_SSID_SET;
3774 else
3775 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003776 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003777 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003778 ifsta->ibss_join_req = jiffies;
3779 ifsta->state = IEEE80211_IBSS_SEARCH;
3780 return ieee80211_sta_find_ibss(dev, ifsta);
3781 }
3782 return 0;
3783}
3784
3785
3786int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3787{
3788 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3789 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3790 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3791 *len = ifsta->ssid_len;
3792 return 0;
3793}
3794
3795
3796int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3797{
3798 struct ieee80211_sub_if_data *sdata;
3799 struct ieee80211_if_sta *ifsta;
3800 int res;
3801
3802 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3803 ifsta = &sdata->u.sta;
3804
3805 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3806 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3807 res = ieee80211_if_config(dev);
3808 if (res) {
3809 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3810 "the low-level driver\n", dev->name);
3811 return res;
3812 }
3813 }
3814
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003815 if (is_valid_ether_addr(bssid))
3816 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003817 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003818 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3819
Jiri Bencf0706e82007-05-05 11:45:53 -07003820 return 0;
3821}
3822
3823
3824static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3825 struct ieee80211_sub_if_data *sdata,
3826 int powersave)
3827{
3828 struct sk_buff *skb;
3829 struct ieee80211_hdr *nullfunc;
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003830 __le16 fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003831
3832 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3833 if (!skb) {
3834 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3835 "frame\n", sdata->dev->name);
3836 return;
3837 }
3838 skb_reserve(skb, local->hw.extra_tx_headroom);
3839
3840 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3841 memset(nullfunc, 0, 24);
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003842 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3843 IEEE80211_FCTL_TODS);
Jiri Bencf0706e82007-05-05 11:45:53 -07003844 if (powersave)
Harvey Harrison4e3996f2008-06-22 16:45:32 -07003845 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
3846 nullfunc->frame_control = fc;
Jiri Bencf0706e82007-05-05 11:45:53 -07003847 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3848 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3849 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3850
3851 ieee80211_sta_tx(sdata->dev, skb, 0);
3852}
3853
3854
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003855static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3856{
3857 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3858 ieee80211_vif_is_mesh(&sdata->vif))
3859 ieee80211_sta_timer((unsigned long)sdata);
3860}
3861
Jiri Bencf0706e82007-05-05 11:45:53 -07003862void ieee80211_scan_completed(struct ieee80211_hw *hw)
3863{
3864 struct ieee80211_local *local = hw_to_local(hw);
3865 struct net_device *dev = local->scan_dev;
3866 struct ieee80211_sub_if_data *sdata;
3867 union iwreq_data wrqu;
3868
3869 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003870 memset(&wrqu, 0, sizeof(wrqu));
3871 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003872
Zhu Yiece8edd2007-11-22 10:53:21 +08003873 if (local->sta_hw_scanning) {
3874 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003875 if (ieee80211_hw_config(local))
3876 printk(KERN_DEBUG "%s: failed to restore operational "
3877 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003878 /* Restart STA timer for HW scan case */
3879 rcu_read_lock();
3880 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3881 ieee80211_restart_sta_timer(sdata);
3882 rcu_read_unlock();
3883
Zhu Yiece8edd2007-11-22 10:53:21 +08003884 goto done;
3885 }
3886
3887 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003888 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003889 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003890 "channel after scan\n", dev->name);
3891
Johannes Berg4150c572007-09-17 01:29:23 -04003892
3893 netif_tx_lock_bh(local->mdev);
3894 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3895 local->ops->configure_filter(local_to_hw(local),
3896 FIF_BCN_PRBRESP_PROMISC,
3897 &local->filter_flags,
3898 local->mdev->mc_count,
3899 local->mdev->mc_list);
3900
3901 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003902
Johannes Berg79010422007-09-18 17:29:21 -04003903 rcu_read_lock();
3904 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003905 /* Tell AP we're back */
3906 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3907 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3908 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003909
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003910 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003911
Jiri Bencf0706e82007-05-05 11:45:53 -07003912 netif_wake_queue(sdata->dev);
3913 }
Johannes Berg79010422007-09-18 17:29:21 -04003914 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003915
Zhu Yiece8edd2007-11-22 10:53:21 +08003916done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003917 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003918 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003919 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003920 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003921 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3922 !ieee80211_sta_active_ibss(dev)))
3923 ieee80211_sta_find_ibss(dev, ifsta);
3924 }
3925}
3926EXPORT_SYMBOL(ieee80211_scan_completed);
3927
3928void ieee80211_sta_scan_work(struct work_struct *work)
3929{
3930 struct ieee80211_local *local =
3931 container_of(work, struct ieee80211_local, scan_work.work);
3932 struct net_device *dev = local->scan_dev;
3933 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003934 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003935 struct ieee80211_channel *chan;
3936 int skip;
3937 unsigned long next_delay = 0;
3938
Zhu Yiece8edd2007-11-22 10:53:21 +08003939 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003940 return;
3941
3942 switch (local->scan_state) {
3943 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003944 /*
3945 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3946 * after we successfully scanned the last channel of the last
3947 * band (and the last band is supported by the hw)
3948 */
Johannes Berg8318d782008-01-24 19:38:38 +01003949 if (local->scan_band < IEEE80211_NUM_BANDS)
3950 sband = local->hw.wiphy->bands[local->scan_band];
3951 else
3952 sband = NULL;
3953
Johannes Berg69d464d2008-01-28 17:08:56 +01003954 /*
3955 * If we are at an unsupported band and have more bands
3956 * left to scan, advance to the next supported one.
3957 */
3958 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003959 local->scan_band++;
3960 sband = local->hw.wiphy->bands[local->scan_band];
3961 local->scan_channel_idx = 0;
3962 }
3963
Johannes Berg69d464d2008-01-28 17:08:56 +01003964 /* if no more bands/channels left, complete scan */
3965 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003966 ieee80211_scan_completed(local_to_hw(local));
3967 return;
3968 }
Johannes Berg8318d782008-01-24 19:38:38 +01003969 skip = 0;
3970 chan = &sband->channels[local->scan_channel_idx];
3971
3972 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003973 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003974 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003975 skip = 1;
3976
3977 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003978 local->scan_channel = chan;
3979 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003980 printk(KERN_DEBUG "%s: failed to set freq to "
3981 "%d MHz for scan\n", dev->name,
3982 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003983 skip = 1;
3984 }
3985 }
3986
Johannes Berg69d464d2008-01-28 17:08:56 +01003987 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003988 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003989 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003990 /*
3991 * scan_band may end up == IEEE80211_NUM_BANDS, but
3992 * we'll catch that case above and complete the scan
3993 * if that is the case.
3994 */
Johannes Berg8318d782008-01-24 19:38:38 +01003995 local->scan_band++;
3996 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003997 }
3998
3999 if (skip)
4000 break;
4001
4002 next_delay = IEEE80211_PROBE_DELAY +
4003 usecs_to_jiffies(local->hw.channel_change_time);
4004 local->scan_state = SCAN_SEND_PROBE;
4005 break;
4006 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01004007 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07004008 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01004009
4010 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
4011 break;
4012 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
4013 local->scan_ssid_len);
4014 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07004015 break;
4016 }
4017
Zhu Yiece8edd2007-11-22 10:53:21 +08004018 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07004019 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4020 next_delay);
4021}
4022
4023
4024static int ieee80211_sta_start_scan(struct net_device *dev,
4025 u8 *ssid, size_t ssid_len)
4026{
4027 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4028 struct ieee80211_sub_if_data *sdata;
4029
4030 if (ssid_len > IEEE80211_MAX_SSID_LEN)
4031 return -EINVAL;
4032
4033 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
4034 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
4035 * BSSID: MACAddress
4036 * SSID
4037 * ScanType: ACTIVE, PASSIVE
4038 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
4039 * a Probe frame during active scanning
4040 * ChannelList
4041 * MinChannelTime (>= ProbeDelay), in TU
4042 * MaxChannelTime: (>= MinChannelTime), in TU
4043 */
4044
4045 /* MLME-SCAN.confirm
4046 * BSSDescriptionSet
4047 * ResultCode: SUCCESS, INVALID_PARAMETERS
4048 */
4049
Zhu Yiece8edd2007-11-22 10:53:21 +08004050 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004051 if (local->scan_dev == dev)
4052 return 0;
4053 return -EBUSY;
4054 }
4055
4056 if (local->ops->hw_scan) {
4057 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08004058 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07004059 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08004060 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07004061 local->scan_dev = dev;
4062 }
4063 return rc;
4064 }
4065
Zhu Yiece8edd2007-11-22 10:53:21 +08004066 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07004067
Johannes Berg79010422007-09-18 17:29:21 -04004068 rcu_read_lock();
4069 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004070 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01004071 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004072 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004073 ieee80211_send_nullfunc(local, sdata, 1);
4074 }
Johannes Berg79010422007-09-18 17:29:21 -04004075 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07004076
4077 if (ssid) {
4078 local->scan_ssid_len = ssid_len;
4079 memcpy(local->scan_ssid, ssid, ssid_len);
4080 } else
4081 local->scan_ssid_len = 0;
4082 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07004083 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01004084 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07004085 local->scan_dev = dev;
4086
Johannes Berg4150c572007-09-17 01:29:23 -04004087 netif_tx_lock_bh(local->mdev);
4088 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
4089 local->ops->configure_filter(local_to_hw(local),
4090 FIF_BCN_PRBRESP_PROMISC,
4091 &local->filter_flags,
4092 local->mdev->mc_count,
4093 local->mdev->mc_list);
4094 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07004095
4096 /* TODO: start scan as soon as all nullfunc frames are ACKed */
4097 queue_delayed_work(local->hw.workqueue, &local->scan_work,
4098 IEEE80211_CHANNEL_TIME);
4099
4100 return 0;
4101}
4102
4103
4104int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
4105{
4106 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4107 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4108 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4109
Johannes Berg51fb61e2007-12-19 01:31:27 +01004110 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004111 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
4112
Zhu Yiece8edd2007-11-22 10:53:21 +08004113 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004114 if (local->scan_dev == dev)
4115 return 0;
4116 return -EBUSY;
4117 }
4118
Helmut Schaaa0af5f12007-11-09 16:25:08 +01004119 ifsta->scan_ssid_len = ssid_len;
4120 if (ssid_len)
4121 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07004122 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
4123 queue_work(local->hw.workqueue, &ifsta->work);
4124 return 0;
4125}
4126
4127static char *
4128ieee80211_sta_scan_result(struct net_device *dev,
David S. Millerccc58052008-06-16 18:50:49 -07004129 struct iw_request_info *info,
Jiri Bencf0706e82007-05-05 11:45:53 -07004130 struct ieee80211_sta_bss *bss,
4131 char *current_ev, char *end_buf)
4132{
4133 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4134 struct iw_event iwe;
4135
4136 if (time_after(jiffies,
4137 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4138 return current_ev;
4139
Jiri Bencf0706e82007-05-05 11:45:53 -07004140 memset(&iwe, 0, sizeof(iwe));
4141 iwe.cmd = SIOCGIWAP;
4142 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4143 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -07004144 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004145 IW_EV_ADDR_LEN);
4146
4147 memset(&iwe, 0, sizeof(iwe));
4148 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01004149 if (bss_mesh_cfg(bss)) {
4150 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004151 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004152 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4153 &iwe, bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004154 } else {
4155 iwe.u.data.length = bss->ssid_len;
4156 iwe.u.data.flags = 1;
David S. Millerccc58052008-06-16 18:50:49 -07004157 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4158 &iwe, bss->ssid);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004159 }
Jiri Bencf0706e82007-05-05 11:45:53 -07004160
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08004161 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4162 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004163 memset(&iwe, 0, sizeof(iwe));
4164 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01004165 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004166 iwe.u.mode = IW_MODE_MESH;
4167 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004168 iwe.u.mode = IW_MODE_MASTER;
4169 else
4170 iwe.u.mode = IW_MODE_ADHOC;
David S. Millerccc58052008-06-16 18:50:49 -07004171 current_ev = iwe_stream_add_event(info, current_ev, end_buf,
4172 &iwe, IW_EV_UINT_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -07004173 }
4174
4175 memset(&iwe, 0, sizeof(iwe));
4176 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01004177 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4178 iwe.u.freq.e = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004179 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004180 IW_EV_FREQ_LEN);
4181
4182 memset(&iwe, 0, sizeof(iwe));
Tomas Winkler9381be02008-05-23 01:36:36 +03004183 iwe.cmd = SIOCGIWFREQ;
4184 iwe.u.freq.m = bss->freq;
4185 iwe.u.freq.e = 6;
David S. Millerccc58052008-06-16 18:50:49 -07004186 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Tomas Winkler9381be02008-05-23 01:36:36 +03004187 IW_EV_FREQ_LEN);
4188 memset(&iwe, 0, sizeof(iwe));
Jiri Bencf0706e82007-05-05 11:45:53 -07004189 iwe.cmd = IWEVQUAL;
Bruno Randolf566bfe52008-05-08 19:15:40 +02004190 iwe.u.qual.qual = bss->qual;
4191 iwe.u.qual.level = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07004192 iwe.u.qual.noise = bss->noise;
4193 iwe.u.qual.updated = local->wstats_flags;
David S. Millerccc58052008-06-16 18:50:49 -07004194 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
Jiri Bencf0706e82007-05-05 11:45:53 -07004195 IW_EV_QUAL_LEN);
4196
4197 memset(&iwe, 0, sizeof(iwe));
4198 iwe.cmd = SIOCGIWENCODE;
4199 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4200 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4201 else
4202 iwe.u.data.flags = IW_ENCODE_DISABLED;
4203 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07004204 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4205 &iwe, "");
Jiri Bencf0706e82007-05-05 11:45:53 -07004206
4207 if (bss && bss->wpa_ie) {
4208 memset(&iwe, 0, sizeof(iwe));
4209 iwe.cmd = IWEVGENIE;
4210 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004211 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4212 &iwe, bss->wpa_ie);
Jiri Bencf0706e82007-05-05 11:45:53 -07004213 }
4214
4215 if (bss && bss->rsn_ie) {
4216 memset(&iwe, 0, sizeof(iwe));
4217 iwe.cmd = IWEVGENIE;
4218 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004219 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4220 &iwe, bss->rsn_ie);
Jiri Bencf0706e82007-05-05 11:45:53 -07004221 }
4222
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004223 if (bss && bss->ht_ie) {
4224 memset(&iwe, 0, sizeof(iwe));
4225 iwe.cmd = IWEVGENIE;
4226 iwe.u.data.length = bss->ht_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07004227 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
4228 &iwe, bss->ht_ie);
Emmanuel Grumbache6231572008-05-27 20:00:11 +03004229 }
4230
Jiri Bencf0706e82007-05-05 11:45:53 -07004231 if (bss && bss->supp_rates_len > 0) {
4232 /* display all supported rates in readable format */
David S. Millerccc58052008-06-16 18:50:49 -07004233 char *p = current_ev + iwe_stream_lcp_len(info);
Jiri Bencf0706e82007-05-05 11:45:53 -07004234 int i;
4235
4236 memset(&iwe, 0, sizeof(iwe));
4237 iwe.cmd = SIOCGIWRATE;
4238 /* Those two flags are ignored... */
4239 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4240
4241 for (i = 0; i < bss->supp_rates_len; i++) {
4242 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4243 0x7f) * 500000);
David S. Millerccc58052008-06-16 18:50:49 -07004244 p = iwe_stream_add_value(info, current_ev, p,
Jiri Bencf0706e82007-05-05 11:45:53 -07004245 end_buf, &iwe, IW_EV_PARAM_LEN);
4246 }
4247 current_ev = p;
4248 }
4249
4250 if (bss) {
4251 char *buf;
4252 buf = kmalloc(30, GFP_ATOMIC);
4253 if (buf) {
4254 memset(&iwe, 0, sizeof(iwe));
4255 iwe.cmd = IWEVCUSTOM;
4256 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4257 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004258 current_ev = iwe_stream_add_point(info, current_ev,
4259 end_buf,
Jiri Bencf0706e82007-05-05 11:45:53 -07004260 &iwe, buf);
Emmanuel Grumbachbf998f62008-06-24 13:38:00 +03004261 memset(&iwe, 0, sizeof(iwe));
4262 iwe.cmd = IWEVCUSTOM;
4263 sprintf(buf, " Last beacon: %dms ago",
4264 jiffies_to_msecs(jiffies - bss->last_update));
4265 iwe.u.data.length = strlen(buf);
4266 current_ev = iwe_stream_add_point(info, current_ev,
4267 end_buf, &iwe, buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004268 kfree(buf);
4269 }
4270 }
4271
Johannes Berg902acc72008-02-23 15:17:19 +01004272 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004273 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04004274 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004275 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004276 if (buf) {
4277 memset(&iwe, 0, sizeof(iwe));
4278 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04004279 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004280 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004281 current_ev = iwe_stream_add_point(info, current_ev,
4282 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004283 &iwe, buf);
4284 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04004285 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4286 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004287 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004288 current_ev = iwe_stream_add_point(info, current_ev,
4289 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004290 &iwe, buf);
4291 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04004292 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4293 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004294 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004295 current_ev = iwe_stream_add_point(info, current_ev,
4296 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004297 &iwe, buf);
4298 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04004299 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4300 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004301 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004302 current_ev = iwe_stream_add_point(info, current_ev,
4303 end_buf,
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004304 &iwe, buf);
4305 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04004306 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4307 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004308 iwe.u.data.length = strlen(buf);
David S. Millerccc58052008-06-16 18:50:49 -07004309 current_ev = iwe_stream_add_point(info, current_ev,
4310 end_buf,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004311 &iwe, buf);
4312 kfree(buf);
4313 }
4314 }
4315
Jiri Bencf0706e82007-05-05 11:45:53 -07004316 return current_ev;
4317}
4318
4319
David S. Millerccc58052008-06-16 18:50:49 -07004320int ieee80211_sta_scan_results(struct net_device *dev,
4321 struct iw_request_info *info,
4322 char *buf, size_t len)
Jiri Bencf0706e82007-05-05 11:45:53 -07004323{
4324 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4325 char *current_ev = buf;
4326 char *end_buf = buf + len;
4327 struct ieee80211_sta_bss *bss;
4328
4329 spin_lock_bh(&local->sta_bss_lock);
4330 list_for_each_entry(bss, &local->sta_bss_list, list) {
4331 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4332 spin_unlock_bh(&local->sta_bss_lock);
4333 return -E2BIG;
4334 }
David S. Millerccc58052008-06-16 18:50:49 -07004335 current_ev = ieee80211_sta_scan_result(dev, info, bss,
4336 current_ev, end_buf);
Jiri Bencf0706e82007-05-05 11:45:53 -07004337 }
4338 spin_unlock_bh(&local->sta_bss_lock);
4339 return current_ev - buf;
4340}
4341
4342
4343int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4344{
4345 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4346 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg988c0f72008-04-17 19:21:22 +02004347
Jiri Bencf0706e82007-05-05 11:45:53 -07004348 kfree(ifsta->extra_ie);
4349 if (len == 0) {
4350 ifsta->extra_ie = NULL;
4351 ifsta->extra_ie_len = 0;
4352 return 0;
4353 }
4354 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4355 if (!ifsta->extra_ie) {
4356 ifsta->extra_ie_len = 0;
4357 return -ENOMEM;
4358 }
4359 memcpy(ifsta->extra_ie, ie, len);
4360 ifsta->extra_ie_len = len;
4361 return 0;
4362}
4363
4364
Johannes Berg988c0f72008-04-17 19:21:22 +02004365struct sta_info *ieee80211_ibss_add_sta(struct net_device *dev,
4366 struct sk_buff *skb, u8 *bssid,
Vladimir Koutny87291c02008-06-13 16:50:44 +02004367 u8 *addr, u64 supp_rates)
Jiri Bencf0706e82007-05-05 11:45:53 -07004368{
4369 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4370 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004371 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004372 DECLARE_MAC_BUF(mac);
Vladimir Koutny87291c02008-06-13 16:50:44 +02004373 int band = local->hw.conf.channel->band;
Jiri Bencf0706e82007-05-05 11:45:53 -07004374
4375 /* TODO: Could consider removing the least recently used entry and
4376 * allow new one to be added. */
4377 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4378 if (net_ratelimit()) {
4379 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004380 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004381 }
4382 return NULL;
4383 }
4384
Vladimir Koutny87291c02008-06-13 16:50:44 +02004385 if (!ieee80211_bssid_match(bssid, sdata->u.sta.bssid))
4386 return NULL;
4387
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004388#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07004389 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004390 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004391#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07004392
Johannes Berg73651ee2008-02-25 16:27:47 +01004393 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4394 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004395 return NULL;
4396
Johannes Berg07346f812008-05-03 01:02:02 +02004397 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
Johannes Berg238814f2008-01-28 17:19:37 +01004398
Vladimir Koutny87291c02008-06-13 16:50:44 +02004399 if (supp_rates)
4400 sta->supp_rates[band] = supp_rates;
4401 else
4402 sta->supp_rates[band] = sdata->u.sta.supp_rates_bits[band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004403
4404 rate_control_rate_init(sta, local);
4405
Johannes Berg93e5deb2008-04-01 15:21:00 +02004406 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004407 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004408
Johannes Bergd0709a62008-02-25 16:27:46 +01004409 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004410}
4411
4412
4413int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4414{
4415 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4416 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4417
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004418 printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07004419 dev->name, reason);
4420
Johannes Berg51fb61e2007-12-19 01:31:27 +01004421 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4422 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004423 return -EINVAL;
4424
4425 ieee80211_send_deauth(dev, ifsta, reason);
4426 ieee80211_set_disassoc(dev, ifsta, 1);
4427 return 0;
4428}
4429
4430
4431int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4432{
4433 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4434 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4435
Johannes Bergf4ea83d2008-06-30 15:10:46 +02004436 printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07004437 dev->name, reason);
4438
Johannes Berg51fb61e2007-12-19 01:31:27 +01004439 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004440 return -EINVAL;
4441
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004442 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004443 return -1;
4444
4445 ieee80211_send_disassoc(dev, ifsta, reason);
4446 ieee80211_set_disassoc(dev, ifsta, 0);
4447 return 0;
4448}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004449
4450void ieee80211_notify_mac(struct ieee80211_hw *hw,
4451 enum ieee80211_notification_types notif_type)
4452{
4453 struct ieee80211_local *local = hw_to_local(hw);
4454 struct ieee80211_sub_if_data *sdata;
4455
4456 switch (notif_type) {
4457 case IEEE80211_NOTIFY_RE_ASSOC:
4458 rcu_read_lock();
4459 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg3e122be2008-07-09 14:40:34 +02004460 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
4461 continue;
Mohamed Abbas84363e62008-04-04 16:59:58 -07004462
Johannes Berg3e122be2008-07-09 14:40:34 +02004463 ieee80211_sta_req_auth(sdata->dev, &sdata->u.sta);
Mohamed Abbas84363e62008-04-04 16:59:58 -07004464 }
4465 rcu_read_unlock();
4466 break;
4467 }
4468}
4469EXPORT_SYMBOL(ieee80211_notify_mac);