blob: a3e96eb59eb0ddfea54cef2f6d941a77dd6f0ed5 [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"
33#include "ieee80211_rate.h"
Michael Buesch47f0c502007-09-27 15:10:44 +020034#include "ieee80211_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)
47#define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
48
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
60#define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
61
62#define ERP_INFO_USE_PROTECTION BIT(1)
63
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020064/* mgmt header + 1 byte action code */
65#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
66
67#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
68#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
69#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020070#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
71#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020072
Ron Rindjunsky07db2182007-12-25 17:00:33 +020073/* next values represent the buffer size for A-MPDU frame.
74 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
75#define IEEE80211_MIN_AMPDU_BUF 0x8
76#define IEEE80211_MAX_AMPDU_BUF 0x40
77
Jiri Bencf0706e82007-05-05 11:45:53 -070078static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
79 u8 *ssid, size_t ssid_len);
80static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +010081ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -040082 u8 *ssid, u8 ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070083static void ieee80211_rx_bss_put(struct net_device *dev,
84 struct ieee80211_sta_bss *bss);
85static int ieee80211_sta_find_ibss(struct net_device *dev,
86 struct ieee80211_if_sta *ifsta);
87static int ieee80211_sta_wep_configured(struct net_device *dev);
88static int ieee80211_sta_start_scan(struct net_device *dev,
89 u8 *ssid, size_t ssid_len);
90static int ieee80211_sta_config_auth(struct net_device *dev,
91 struct ieee80211_if_sta *ifsta);
92
93
Luis Carlos Coboee385852008-02-23 15:17:11 +010094void ieee802_11_parse_elems(u8 *start, size_t len,
95 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -070096{
97 size_t left = len;
98 u8 *pos = start;
Jiri Bencf0706e82007-05-05 11:45:53 -070099
100 memset(elems, 0, sizeof(*elems));
101
102 while (left >= 2) {
103 u8 id, elen;
104
105 id = *pos++;
106 elen = *pos++;
107 left -= 2;
108
John W. Linville67a4cce2007-10-12 16:40:37 -0400109 if (elen > left)
110 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700111
112 switch (id) {
113 case WLAN_EID_SSID:
114 elems->ssid = pos;
115 elems->ssid_len = elen;
116 break;
117 case WLAN_EID_SUPP_RATES:
118 elems->supp_rates = pos;
119 elems->supp_rates_len = elen;
120 break;
121 case WLAN_EID_FH_PARAMS:
122 elems->fh_params = pos;
123 elems->fh_params_len = elen;
124 break;
125 case WLAN_EID_DS_PARAMS:
126 elems->ds_params = pos;
127 elems->ds_params_len = elen;
128 break;
129 case WLAN_EID_CF_PARAMS:
130 elems->cf_params = pos;
131 elems->cf_params_len = elen;
132 break;
133 case WLAN_EID_TIM:
134 elems->tim = pos;
135 elems->tim_len = elen;
136 break;
137 case WLAN_EID_IBSS_PARAMS:
138 elems->ibss_params = pos;
139 elems->ibss_params_len = elen;
140 break;
141 case WLAN_EID_CHALLENGE:
142 elems->challenge = pos;
143 elems->challenge_len = elen;
144 break;
145 case WLAN_EID_WPA:
146 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
147 pos[2] == 0xf2) {
148 /* Microsoft OUI (00:50:F2) */
149 if (pos[3] == 1) {
150 /* OUI Type 1 - WPA IE */
151 elems->wpa = pos;
152 elems->wpa_len = elen;
153 } else if (elen >= 5 && pos[3] == 2) {
154 if (pos[4] == 0) {
155 elems->wmm_info = pos;
156 elems->wmm_info_len = elen;
157 } else if (pos[4] == 1) {
158 elems->wmm_param = pos;
159 elems->wmm_param_len = elen;
160 }
161 }
162 }
163 break;
164 case WLAN_EID_RSN:
165 elems->rsn = pos;
166 elems->rsn_len = elen;
167 break;
168 case WLAN_EID_ERP_INFO:
169 elems->erp_info = pos;
170 elems->erp_info_len = elen;
171 break;
172 case WLAN_EID_EXT_SUPP_RATES:
173 elems->ext_supp_rates = pos;
174 elems->ext_supp_rates_len = elen;
175 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200176 case WLAN_EID_HT_CAPABILITY:
177 elems->ht_cap_elem = pos;
178 elems->ht_cap_elem_len = elen;
179 break;
180 case WLAN_EID_HT_EXTRA_INFO:
181 elems->ht_info_elem = pos;
182 elems->ht_info_elem_len = elen;
183 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100184 case WLAN_EID_MESH_ID:
185 elems->mesh_id = pos;
186 elems->mesh_id_len = elen;
187 break;
188 case WLAN_EID_MESH_CONFIG:
189 elems->mesh_config = pos;
190 elems->mesh_config_len = elen;
191 break;
192 case WLAN_EID_PEER_LINK:
193 elems->peer_link = pos;
194 elems->peer_link_len = elen;
195 break;
196 case WLAN_EID_PREQ:
197 elems->preq = pos;
198 elems->preq_len = elen;
199 break;
200 case WLAN_EID_PREP:
201 elems->prep = pos;
202 elems->prep_len = elen;
203 break;
204 case WLAN_EID_PERR:
205 elems->perr = pos;
206 elems->perr_len = elen;
207 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700208 default:
Jiri Bencf0706e82007-05-05 11:45:53 -0700209 break;
210 }
211
212 left -= elen;
213 pos += elen;
214 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700215}
216
217
Jiri Bencf0706e82007-05-05 11:45:53 -0700218static int ecw2cw(int ecw)
219{
Johannes Bergac2bf322008-02-14 00:30:35 +0100220 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700221}
222
Jiri Bencf0706e82007-05-05 11:45:53 -0700223static void ieee80211_sta_wmm_params(struct net_device *dev,
224 struct ieee80211_if_sta *ifsta,
225 u8 *wmm_param, size_t wmm_param_len)
226{
227 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
228 struct ieee80211_tx_queue_params params;
229 size_t left;
230 int count;
231 u8 *pos;
232
233 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
234 return;
235 count = wmm_param[6] & 0x0f;
236 if (count == ifsta->wmm_last_param_set)
237 return;
238 ifsta->wmm_last_param_set = count;
239
240 pos = wmm_param + 8;
241 left = wmm_param_len - 8;
242
243 memset(&params, 0, sizeof(params));
244
245 if (!local->ops->conf_tx)
246 return;
247
248 local->wmm_acm = 0;
249 for (; left >= 4; left -= 4, pos += 4) {
250 int aci = (pos[0] >> 5) & 0x03;
251 int acm = (pos[0] >> 4) & 0x01;
252 int queue;
253
254 switch (aci) {
255 case 1:
256 queue = IEEE80211_TX_QUEUE_DATA3;
257 if (acm) {
258 local->wmm_acm |= BIT(0) | BIT(3);
259 }
260 break;
261 case 2:
262 queue = IEEE80211_TX_QUEUE_DATA1;
263 if (acm) {
264 local->wmm_acm |= BIT(4) | BIT(5);
265 }
266 break;
267 case 3:
268 queue = IEEE80211_TX_QUEUE_DATA0;
269 if (acm) {
270 local->wmm_acm |= BIT(6) | BIT(7);
271 }
272 break;
273 case 0:
274 default:
275 queue = IEEE80211_TX_QUEUE_DATA2;
276 if (acm) {
277 local->wmm_acm |= BIT(1) | BIT(2);
278 }
279 break;
280 }
281
282 params.aifs = pos[0] & 0x0f;
283 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
284 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100285 params.txop = pos[2] | (pos[3] << 8);
286#ifdef CONFIG_MAC80211_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700287 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100288 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e82007-05-05 11:45:53 -0700289 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100290 params.cw_max, params.txop);
291#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700292 /* TODO: handle ACM (block TX, fallback to next lowest allowed
293 * AC for now) */
294 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
295 printk(KERN_DEBUG "%s: failed to set TX queue "
296 "parameters for queue %d\n", dev->name, queue);
297 }
298 }
299}
300
301
Johannes Berg471b3ef2007-12-28 14:32:58 +0100302static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
303 u8 erp_value)
Daniel Drake56282212007-07-10 19:32:10 +0200304{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100305 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Daniel Drake56282212007-07-10 19:32:10 +0200306 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100307 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
308 bool preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
Joe Perches0795af52007-10-03 17:59:30 -0700309 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100310 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200311
Johannes Berg471b3ef2007-12-28 14:32:58 +0100312 if (use_protection != bss_conf->use_cts_prot) {
Daniel Drake56282212007-07-10 19:32:10 +0200313 if (net_ratelimit()) {
314 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700315 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100316 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200317 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700318 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200319 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100320 bss_conf->use_cts_prot = use_protection;
321 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200322 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200323
Johannes Berg471b3ef2007-12-28 14:32:58 +0100324 if (preamble_mode != bss_conf->use_short_preamble) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200325 if (net_ratelimit()) {
326 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700327 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100328 sdata->dev->name,
Daniel Drake7e9ed182007-07-27 15:43:24 +0200329 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
330 "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700331 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200332 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100333 bss_conf->use_short_preamble = preamble_mode;
334 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200335 }
Daniel Draked9430a32007-07-27 15:43:24 +0200336
Johannes Berg471b3ef2007-12-28 14:32:58 +0100337 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200338}
339
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200340int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
341 struct ieee80211_ht_info *ht_info)
342{
343
344 if (ht_info == NULL)
345 return -EINVAL;
346
347 memset(ht_info, 0, sizeof(*ht_info));
348
349 if (ht_cap_ie) {
350 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
351
352 ht_info->ht_supported = 1;
353 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
354 ht_info->ampdu_factor =
355 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
356 ht_info->ampdu_density =
357 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
358 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
359 } else
360 ht_info->ht_supported = 0;
361
362 return 0;
363}
364
365int ieee80211_ht_addt_info_ie_to_ht_bss_info(
366 struct ieee80211_ht_addt_info *ht_add_info_ie,
367 struct ieee80211_ht_bss_info *bss_info)
368{
369 if (bss_info == NULL)
370 return -EINVAL;
371
372 memset(bss_info, 0, sizeof(*bss_info));
373
374 if (ht_add_info_ie) {
375 u16 op_mode;
376 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
377
378 bss_info->primary_channel = ht_add_info_ie->control_chan;
379 bss_info->bss_cap = ht_add_info_ie->ht_param;
380 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
381 }
382
383 return 0;
384}
Daniel Drake56282212007-07-10 19:32:10 +0200385
Jiri Bencf0706e82007-05-05 11:45:53 -0700386static void ieee80211_sta_send_associnfo(struct net_device *dev,
387 struct ieee80211_if_sta *ifsta)
388{
389 char *buf;
390 size_t len;
391 int i;
392 union iwreq_data wrqu;
393
394 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
395 return;
396
397 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200398 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700399 if (!buf)
400 return;
401
402 len = sprintf(buf, "ASSOCINFO(");
403 if (ifsta->assocreq_ies) {
404 len += sprintf(buf + len, "ReqIEs=");
405 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
406 len += sprintf(buf + len, "%02x",
407 ifsta->assocreq_ies[i]);
408 }
409 }
410 if (ifsta->assocresp_ies) {
411 if (ifsta->assocreq_ies)
412 len += sprintf(buf + len, " ");
413 len += sprintf(buf + len, "RespIEs=");
414 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
415 len += sprintf(buf + len, "%02x",
416 ifsta->assocresp_ies[i]);
417 }
418 }
419 len += sprintf(buf + len, ")");
420
421 if (len > IW_CUSTOM_MAX) {
422 len = sprintf(buf, "ASSOCRESPIE=");
423 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
424 len += sprintf(buf + len, "%02x",
425 ifsta->assocresp_ies[i]);
426 }
427 }
428
429 memset(&wrqu, 0, sizeof(wrqu));
430 wrqu.data.length = len;
431 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
432
433 kfree(buf);
434}
435
436
437static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400438 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200439 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700440{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100441 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
442 struct ieee80211_local *local = sdata->local;
Jiri Bencf0706e82007-05-05 11:45:53 -0700443 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100444 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700445
Jiri Bencf0706e82007-05-05 11:45:53 -0700446 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200447 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400448
449 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
450
Johannes Berg51fb61e2007-12-19 01:31:27 +0100451 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700452 return;
Daniel Drake56282212007-07-10 19:32:10 +0200453
John W. Linville65c107a2007-10-05 14:23:27 -0400454 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +0100455 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400456 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200457 if (bss) {
458 if (bss->has_erp_value)
Johannes Berg471b3ef2007-12-28 14:32:58 +0100459 changed |= ieee80211_handle_erp_ie(
460 sdata, bss->erp_value);
Daniel Drake56282212007-07-10 19:32:10 +0200461 ieee80211_rx_bss_put(dev, bss);
462 }
463
Jiri Bencf0706e82007-05-05 11:45:53 -0700464 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400465 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700466 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
467 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
468 ieee80211_sta_send_associnfo(dev, ifsta);
469 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400470 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
471
Jiri Bencf0706e82007-05-05 11:45:53 -0700472 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200473 ieee80211_reset_erp_info(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700474 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
475 }
476 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
477 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
478 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200479 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100480
Tomas Winklerb2205252008-01-27 16:18:22 +0200481 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100482 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -0700483}
484
485static void ieee80211_set_disassoc(struct net_device *dev,
486 struct ieee80211_if_sta *ifsta, int deauth)
487{
488 if (deauth)
489 ifsta->auth_tries = 0;
490 ifsta->assoc_tries = 0;
491 ieee80211_set_associated(dev, ifsta, 0);
492}
493
Luis Carlos Coboee385852008-02-23 15:17:11 +0100494void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
495 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700496{
497 struct ieee80211_sub_if_data *sdata;
498 struct ieee80211_tx_packet_data *pkt_data;
499
500 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
501 skb->dev = sdata->local->mdev;
502 skb_set_mac_header(skb, 0);
503 skb_set_network_header(skb, 0);
504 skb_set_transport_header(skb, 0);
505
506 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
507 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
508 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400509 if (!encrypt)
510 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700511
512 dev_queue_xmit(skb);
513}
514
515
516static void ieee80211_send_auth(struct net_device *dev,
517 struct ieee80211_if_sta *ifsta,
518 int transaction, u8 *extra, size_t extra_len,
519 int encrypt)
520{
521 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
522 struct sk_buff *skb;
523 struct ieee80211_mgmt *mgmt;
524
525 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
526 sizeof(*mgmt) + 6 + extra_len);
527 if (!skb) {
528 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
529 "frame\n", dev->name);
530 return;
531 }
532 skb_reserve(skb, local->hw.extra_tx_headroom);
533
534 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
535 memset(mgmt, 0, 24 + 6);
536 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
537 IEEE80211_STYPE_AUTH);
538 if (encrypt)
539 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
540 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
541 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
542 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
543 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
544 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
545 ifsta->auth_transaction = transaction + 1;
546 mgmt->u.auth.status_code = cpu_to_le16(0);
547 if (extra)
548 memcpy(skb_put(skb, extra_len), extra, extra_len);
549
550 ieee80211_sta_tx(dev, skb, encrypt);
551}
552
553
554static void ieee80211_authenticate(struct net_device *dev,
555 struct ieee80211_if_sta *ifsta)
556{
Joe Perches0795af52007-10-03 17:59:30 -0700557 DECLARE_MAC_BUF(mac);
558
Jiri Bencf0706e82007-05-05 11:45:53 -0700559 ifsta->auth_tries++;
560 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700561 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700562 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700563 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700564 ifsta->state = IEEE80211_DISABLED;
565 return;
566 }
567
568 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700569 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
570 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700571
572 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
573
574 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
575}
576
577
578static void ieee80211_send_assoc(struct net_device *dev,
579 struct ieee80211_if_sta *ifsta)
580{
581 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700582 struct sk_buff *skb;
583 struct ieee80211_mgmt *mgmt;
584 u8 *pos, *ies;
585 int i, len;
586 u16 capab;
587 struct ieee80211_sta_bss *bss;
588 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100589 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700590
591 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
592 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
593 ifsta->ssid_len);
594 if (!skb) {
595 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
596 "frame\n", dev->name);
597 return;
598 }
599 skb_reserve(skb, local->hw.extra_tx_headroom);
600
Johannes Berg8318d782008-01-24 19:38:38 +0100601 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
602
Jiri Bencf0706e82007-05-05 11:45:53 -0700603 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100604
605 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
606 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
607 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
608 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
609 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700610 }
Johannes Berg8318d782008-01-24 19:38:38 +0100611
612 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
613 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400614 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700615 if (bss) {
616 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
617 capab |= WLAN_CAPABILITY_PRIVACY;
618 if (bss->wmm_ie) {
619 wmm = 1;
620 }
621 ieee80211_rx_bss_put(dev, bss);
622 }
623
624 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
625 memset(mgmt, 0, 24);
626 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
627 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
628 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
629
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400630 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700631 skb_put(skb, 10);
632 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
633 IEEE80211_STYPE_REASSOC_REQ);
634 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
635 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
636 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
637 ETH_ALEN);
638 } else {
639 skb_put(skb, 4);
640 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
641 IEEE80211_STYPE_ASSOC_REQ);
642 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
643 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
644 }
645
646 /* SSID */
647 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
648 *pos++ = WLAN_EID_SSID;
649 *pos++ = ifsta->ssid_len;
650 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
651
Johannes Berg8318d782008-01-24 19:38:38 +0100652 len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700653 if (len > 8)
654 len = 8;
655 pos = skb_put(skb, len + 2);
656 *pos++ = WLAN_EID_SUPP_RATES;
657 *pos++ = len;
658 for (i = 0; i < len; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100659 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700660 *pos++ = (u8) (rate / 5);
661 }
662
Johannes Berg8318d782008-01-24 19:38:38 +0100663 if (sband->n_bitrates > len) {
664 pos = skb_put(skb, sband->n_bitrates - len + 2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700665 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Johannes Berg8318d782008-01-24 19:38:38 +0100666 *pos++ = sband->n_bitrates - len;
667 for (i = len; i < sband->n_bitrates; i++) {
668 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700669 *pos++ = (u8) (rate / 5);
670 }
671 }
672
673 if (ifsta->extra_ie) {
674 pos = skb_put(skb, ifsta->extra_ie_len);
675 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
676 }
677
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400678 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700679 pos = skb_put(skb, 9);
680 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
681 *pos++ = 7; /* len */
682 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
683 *pos++ = 0x50;
684 *pos++ = 0xf2;
685 *pos++ = 2; /* WME */
686 *pos++ = 0; /* WME info */
687 *pos++ = 1; /* WME ver */
688 *pos++ = 0;
689 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200690 /* wmm support is a must to HT */
Johannes Berg8318d782008-01-24 19:38:38 +0100691 if (wmm && sband->ht_info.ht_supported) {
692 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200693 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
694 *pos++ = WLAN_EID_HT_CAPABILITY;
695 *pos++ = sizeof(struct ieee80211_ht_cap);
696 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
697 memcpy(pos, &tmp, sizeof(u16));
698 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100699 /* TODO: needs a define here for << 2 */
700 *pos++ = sband->ht_info.ampdu_factor |
701 (sband->ht_info.ampdu_density << 2);
702 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200703 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700704
705 kfree(ifsta->assocreq_ies);
706 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200707 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700708 if (ifsta->assocreq_ies)
709 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
710
711 ieee80211_sta_tx(dev, skb, 0);
712}
713
714
715static void ieee80211_send_deauth(struct net_device *dev,
716 struct ieee80211_if_sta *ifsta, u16 reason)
717{
718 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
719 struct sk_buff *skb;
720 struct ieee80211_mgmt *mgmt;
721
722 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
723 if (!skb) {
724 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
725 "frame\n", dev->name);
726 return;
727 }
728 skb_reserve(skb, local->hw.extra_tx_headroom);
729
730 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
731 memset(mgmt, 0, 24);
732 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
733 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
734 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
735 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
736 IEEE80211_STYPE_DEAUTH);
737 skb_put(skb, 2);
738 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
739
740 ieee80211_sta_tx(dev, skb, 0);
741}
742
743
744static void ieee80211_send_disassoc(struct net_device *dev,
745 struct ieee80211_if_sta *ifsta, u16 reason)
746{
747 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
748 struct sk_buff *skb;
749 struct ieee80211_mgmt *mgmt;
750
751 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
752 if (!skb) {
753 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
754 "frame\n", dev->name);
755 return;
756 }
757 skb_reserve(skb, local->hw.extra_tx_headroom);
758
759 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
760 memset(mgmt, 0, 24);
761 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
762 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
763 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
764 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
765 IEEE80211_STYPE_DISASSOC);
766 skb_put(skb, 2);
767 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
768
769 ieee80211_sta_tx(dev, skb, 0);
770}
771
772
773static int ieee80211_privacy_mismatch(struct net_device *dev,
774 struct ieee80211_if_sta *ifsta)
775{
John W. Linville65c107a2007-10-05 14:23:27 -0400776 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700777 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000778 int bss_privacy;
779 int wep_privacy;
780 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700781
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000782 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700783 return 0;
784
Johannes Berg8318d782008-01-24 19:38:38 +0100785 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
786 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400787 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700788 if (!bss)
789 return 0;
790
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000791 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
792 wep_privacy = !!ieee80211_sta_wep_configured(dev);
793 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700794
795 ieee80211_rx_bss_put(dev, bss);
796
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000797 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
798 return 0;
799
800 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700801}
802
803
804static void ieee80211_associate(struct net_device *dev,
805 struct ieee80211_if_sta *ifsta)
806{
Joe Perches0795af52007-10-03 17:59:30 -0700807 DECLARE_MAC_BUF(mac);
808
Jiri Bencf0706e82007-05-05 11:45:53 -0700809 ifsta->assoc_tries++;
810 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700811 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700812 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700813 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700814 ifsta->state = IEEE80211_DISABLED;
815 return;
816 }
817
818 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700819 printk(KERN_DEBUG "%s: associate with AP %s\n",
820 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700821 if (ieee80211_privacy_mismatch(dev, ifsta)) {
822 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
823 "mixed-cell disabled - abort association\n", dev->name);
824 ifsta->state = IEEE80211_DISABLED;
825 return;
826 }
827
828 ieee80211_send_assoc(dev, ifsta);
829
830 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
831}
832
833
834static void ieee80211_associated(struct net_device *dev,
835 struct ieee80211_if_sta *ifsta)
836{
837 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
838 struct sta_info *sta;
839 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700840 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700841
842 /* TODO: start monitoring current AP signal quality and number of
843 * missed beacons. Scan other channels every now and then and search
844 * for better APs. */
845 /* TODO: remove expired BSSes */
846
847 ifsta->state = IEEE80211_ASSOCIATED;
848
Johannes Bergd0709a62008-02-25 16:27:46 +0100849 rcu_read_lock();
850
Jiri Bencf0706e82007-05-05 11:45:53 -0700851 sta = sta_info_get(local, ifsta->bssid);
852 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700853 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
854 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700855 disassoc = 1;
856 } else {
857 disassoc = 0;
858 if (time_after(jiffies,
859 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400860 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700861 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700862 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -0700863 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700864 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700865 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +0100866 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400867 } else
Jiri Bencf0706e82007-05-05 11:45:53 -0700868 ieee80211_send_probe_req(dev, ifsta->bssid,
869 local->scan_ssid,
870 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400871 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700872 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400873 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700874 if (time_after(jiffies, ifsta->last_probe +
875 IEEE80211_PROBE_INTERVAL)) {
876 ifsta->last_probe = jiffies;
877 ieee80211_send_probe_req(dev, ifsta->bssid,
878 ifsta->ssid,
879 ifsta->ssid_len);
880 }
881 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700882 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100883
884 rcu_read_unlock();
885
886 if (disassoc && sta) {
887 synchronize_rcu();
888 rtnl_lock();
889 sta_info_destroy(sta);
890 rtnl_unlock();
891 }
892
Jiri Bencf0706e82007-05-05 11:45:53 -0700893 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -0500894 ifsta->state = IEEE80211_DISABLED;
895 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700896 } else {
897 mod_timer(&ifsta->timer, jiffies +
898 IEEE80211_MONITORING_INTERVAL);
899 }
900}
901
902
903static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
904 u8 *ssid, size_t ssid_len)
905{
906 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +0100907 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700908 struct sk_buff *skb;
909 struct ieee80211_mgmt *mgmt;
910 u8 *pos, *supp_rates, *esupp_rates = NULL;
911 int i;
912
913 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
914 if (!skb) {
915 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
916 "request\n", dev->name);
917 return;
918 }
919 skb_reserve(skb, local->hw.extra_tx_headroom);
920
921 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
922 memset(mgmt, 0, 24);
923 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
924 IEEE80211_STYPE_PROBE_REQ);
925 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
926 if (dst) {
927 memcpy(mgmt->da, dst, ETH_ALEN);
928 memcpy(mgmt->bssid, dst, ETH_ALEN);
929 } else {
930 memset(mgmt->da, 0xff, ETH_ALEN);
931 memset(mgmt->bssid, 0xff, ETH_ALEN);
932 }
933 pos = skb_put(skb, 2 + ssid_len);
934 *pos++ = WLAN_EID_SSID;
935 *pos++ = ssid_len;
936 memcpy(pos, ssid, ssid_len);
937
938 supp_rates = skb_put(skb, 2);
939 supp_rates[0] = WLAN_EID_SUPP_RATES;
940 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100941 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
942
943 for (i = 0; i < sband->n_bitrates; i++) {
944 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -0700945 if (esupp_rates) {
946 pos = skb_put(skb, 1);
947 esupp_rates[1]++;
948 } else if (supp_rates[1] == 8) {
949 esupp_rates = skb_put(skb, 3);
950 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
951 esupp_rates[1] = 1;
952 pos = &esupp_rates[2];
953 } else {
954 pos = skb_put(skb, 1);
955 supp_rates[1]++;
956 }
Johannes Berg8318d782008-01-24 19:38:38 +0100957 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -0700958 }
959
960 ieee80211_sta_tx(dev, skb, 0);
961}
962
963
964static int ieee80211_sta_wep_configured(struct net_device *dev)
965{
966 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
967 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -0400968 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -0700969 return 0;
970 return 1;
971}
972
973
974static void ieee80211_auth_completed(struct net_device *dev,
975 struct ieee80211_if_sta *ifsta)
976{
977 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400978 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700979 ieee80211_associate(dev, ifsta);
980}
981
982
983static void ieee80211_auth_challenge(struct net_device *dev,
984 struct ieee80211_if_sta *ifsta,
985 struct ieee80211_mgmt *mgmt,
986 size_t len)
987{
988 u8 *pos;
989 struct ieee802_11_elems elems;
990
991 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
992 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -0400993 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -0700994 if (!elems.challenge) {
995 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
996 "frame\n", dev->name);
997 return;
998 }
999 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1000 elems.challenge_len + 2, 1);
1001}
1002
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001003static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1004 u8 dialog_token, u16 status, u16 policy,
1005 u16 buf_size, u16 timeout)
1006{
1007 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1008 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1009 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1010 struct sk_buff *skb;
1011 struct ieee80211_mgmt *mgmt;
1012 u16 capab;
1013
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001014 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1015 sizeof(mgmt->u.action.u.addba_resp));
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001016 if (!skb) {
1017 printk(KERN_DEBUG "%s: failed to allocate buffer "
1018 "for addba resp frame\n", dev->name);
1019 return;
1020 }
1021
1022 skb_reserve(skb, local->hw.extra_tx_headroom);
1023 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1024 memset(mgmt, 0, 24);
1025 memcpy(mgmt->da, da, ETH_ALEN);
1026 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001027 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001028 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1029 else
1030 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1031 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1032 IEEE80211_STYPE_ACTION);
1033
1034 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1035 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1036 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1037 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1038
1039 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1040 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1041 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1042
1043 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1044 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1045 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1046
1047 ieee80211_sta_tx(dev, skb, 0);
1048
1049 return;
1050}
1051
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001052void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1053 u16 tid, u8 dialog_token, u16 start_seq_num,
1054 u16 agg_size, u16 timeout)
1055{
1056 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1057 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1058 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1059 struct sk_buff *skb;
1060 struct ieee80211_mgmt *mgmt;
1061 u16 capab;
1062
1063 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1064 sizeof(mgmt->u.action.u.addba_req));
1065
1066
1067 if (!skb) {
1068 printk(KERN_ERR "%s: failed to allocate buffer "
1069 "for addba request frame\n", dev->name);
1070 return;
1071 }
1072 skb_reserve(skb, local->hw.extra_tx_headroom);
1073 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1074 memset(mgmt, 0, 24);
1075 memcpy(mgmt->da, da, ETH_ALEN);
1076 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1077 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1078 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1079 else
1080 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1081
1082 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1083 IEEE80211_STYPE_ACTION);
1084
1085 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1086
1087 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1088 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1089
1090 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1091 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1092 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1093 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1094
1095 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1096
1097 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1098 mgmt->u.action.u.addba_req.start_seq_num =
1099 cpu_to_le16(start_seq_num << 4);
1100
1101 ieee80211_sta_tx(dev, skb, 0);
1102}
1103
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001104static void ieee80211_sta_process_addba_request(struct net_device *dev,
1105 struct ieee80211_mgmt *mgmt,
1106 size_t len)
1107{
1108 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001109 struct ieee80211_hw *hw = &local->hw;
1110 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001111 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001112 struct tid_ampdu_rx *tid_agg_rx;
1113 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001114 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001115 int ret = -EOPNOTSUPP;
1116 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001117
Johannes Bergd0709a62008-02-25 16:27:46 +01001118 rcu_read_lock();
1119
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001120 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001121 if (!sta) {
1122 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001123 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001124 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001125
1126 /* extract session parameters from addba request frame */
1127 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1128 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001129 start_seq_num =
1130 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001131
1132 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1133 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1134 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1135 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1136
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001137 status = WLAN_STATUS_REQUEST_DECLINED;
1138
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001139 /* sanity check for incoming parameters:
1140 * check if configuration can support the BA policy
1141 * and if buffer size does not exceeds max value */
1142 if (((ba_policy != 1)
1143 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1144 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1145 status = WLAN_STATUS_INVALID_QOS_PARAM;
1146#ifdef CONFIG_MAC80211_HT_DEBUG
1147 if (net_ratelimit())
1148 printk(KERN_DEBUG "Block Ack Req with bad params from "
1149 "%s on tid %u. policy %d, buffer size %d\n",
1150 print_mac(mac, mgmt->sa), tid, ba_policy,
1151 buf_size);
1152#endif /* CONFIG_MAC80211_HT_DEBUG */
1153 goto end_no_lock;
1154 }
1155 /* determine default buffer size */
1156 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001157 struct ieee80211_supported_band *sband;
1158
1159 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001160 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001161 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001162 }
1163
1164 tid_agg_rx = &sta->ampdu_mlme.tid_rx[tid];
1165
1166 /* examine state machine */
1167 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1168
1169 if (tid_agg_rx->state != HT_AGG_STATE_IDLE) {
1170#ifdef CONFIG_MAC80211_HT_DEBUG
1171 if (net_ratelimit())
1172 printk(KERN_DEBUG "unexpected Block Ack Req from "
1173 "%s on tid %u\n",
1174 print_mac(mac, mgmt->sa), tid);
1175#endif /* CONFIG_MAC80211_HT_DEBUG */
1176 goto end;
1177 }
1178
1179 /* prepare reordering buffer */
1180 tid_agg_rx->reorder_buf =
1181 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001182 if (!tid_agg_rx->reorder_buf) {
1183 if (net_ratelimit())
1184 printk(KERN_ERR "can not allocate reordering buffer "
1185 "to tid %d\n", tid);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001186 goto end;
1187 }
1188 memset(tid_agg_rx->reorder_buf, 0,
1189 buf_size * sizeof(struct sk_buf *));
1190
1191 if (local->ops->ampdu_action)
1192 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001193 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001194#ifdef CONFIG_MAC80211_HT_DEBUG
1195 printk(KERN_DEBUG "Rx A-MPDU on tid %d result %d", tid, ret);
1196#endif /* CONFIG_MAC80211_HT_DEBUG */
1197
1198 if (ret) {
1199 kfree(tid_agg_rx->reorder_buf);
1200 goto end;
1201 }
1202
1203 /* change state and send addba resp */
1204 tid_agg_rx->state = HT_AGG_STATE_OPERATIONAL;
1205 tid_agg_rx->dialog_token = dialog_token;
1206 tid_agg_rx->ssn = start_seq_num;
1207 tid_agg_rx->head_seq_num = start_seq_num;
1208 tid_agg_rx->buf_size = buf_size;
1209 tid_agg_rx->timeout = timeout;
1210 tid_agg_rx->stored_mpdu_num = 0;
1211 status = WLAN_STATUS_SUCCESS;
1212end:
1213 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1214
1215end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001216 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1217 dialog_token, status, 1, buf_size, timeout);
1218 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001219}
Jiri Bencf0706e82007-05-05 11:45:53 -07001220
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001221static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1222 struct ieee80211_mgmt *mgmt,
1223 size_t len)
1224{
1225 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1226 struct ieee80211_hw *hw = &local->hw;
1227 struct sta_info *sta;
1228 u16 capab;
1229 u16 tid;
1230 u8 *state;
1231
Johannes Bergd0709a62008-02-25 16:27:46 +01001232 rcu_read_lock();
1233
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001234 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001235 if (!sta) {
1236 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001237 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001238 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001239
1240 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1241 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1242
1243 state = &sta->ampdu_mlme.tid_tx[tid].state;
1244
1245 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1246
1247 if (mgmt->u.action.u.addba_resp.dialog_token !=
1248 sta->ampdu_mlme.tid_tx[tid].dialog_token) {
1249 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1250#ifdef CONFIG_MAC80211_HT_DEBUG
1251 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1252#endif /* CONFIG_MAC80211_HT_DEBUG */
Johannes Bergd0709a62008-02-25 16:27:46 +01001253 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001254 return;
1255 }
1256
1257 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid].addba_resp_timer);
1258#ifdef CONFIG_MAC80211_HT_DEBUG
1259 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1260#endif /* CONFIG_MAC80211_HT_DEBUG */
1261 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1262 == WLAN_STATUS_SUCCESS) {
1263 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1264 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1265 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1266 "%d\n", *state);
Johannes Bergd0709a62008-02-25 16:27:46 +01001267 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001268 return;
1269 }
1270
1271 if (*state & HT_ADDBA_RECEIVED_MSK)
1272 printk(KERN_DEBUG "double addBA response\n");
1273
1274 *state |= HT_ADDBA_RECEIVED_MSK;
1275 sta->ampdu_mlme.tid_tx[tid].addba_req_num = 0;
1276
1277 if (*state == HT_AGG_STATE_OPERATIONAL) {
1278 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1279 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1280 }
1281
1282 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1283 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1284 } else {
1285 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1286
1287 sta->ampdu_mlme.tid_tx[tid].addba_req_num++;
1288 /* this will allow the state check in stop_BA_session */
1289 *state = HT_AGG_STATE_OPERATIONAL;
1290 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1291 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1292 WLAN_BACK_INITIATOR);
1293 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001294 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001295}
1296
1297void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1298 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001299{
1300 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1301 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1302 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1303 struct sk_buff *skb;
1304 struct ieee80211_mgmt *mgmt;
1305 u16 params;
1306
1307 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom + 1 +
1308 sizeof(mgmt->u.action.u.delba));
1309
1310 if (!skb) {
1311 printk(KERN_ERR "%s: failed to allocate buffer "
1312 "for delba frame\n", dev->name);
1313 return;
1314 }
1315
1316 skb_reserve(skb, local->hw.extra_tx_headroom);
1317 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1318 memset(mgmt, 0, 24);
1319 memcpy(mgmt->da, da, ETH_ALEN);
1320 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001321 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001322 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1323 else
1324 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1325 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1326 IEEE80211_STYPE_ACTION);
1327
1328 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1329
1330 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1331 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1332 params = (u16)(initiator << 11); /* bit 11 initiator */
1333 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1334
1335 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1336 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1337
1338 ieee80211_sta_tx(dev, skb, 0);
1339}
1340
1341void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1342 u16 initiator, u16 reason)
1343{
1344 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1345 struct ieee80211_hw *hw = &local->hw;
1346 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001347 int ret, i;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001348
Johannes Bergd0709a62008-02-25 16:27:46 +01001349 rcu_read_lock();
1350
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001351 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001352 if (!sta) {
1353 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001354 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001355 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001356
1357 /* check if TID is in operational state */
1358 spin_lock_bh(&sta->ampdu_mlme.ampdu_rx);
1359 if (sta->ampdu_mlme.tid_rx[tid].state
1360 != HT_AGG_STATE_OPERATIONAL) {
1361 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
Johannes Bergd0709a62008-02-25 16:27:46 +01001362 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001363 return;
1364 }
1365 sta->ampdu_mlme.tid_rx[tid].state =
1366 HT_AGG_STATE_REQ_STOP_BA_MSK |
1367 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
1368 spin_unlock_bh(&sta->ampdu_mlme.ampdu_rx);
1369
1370 /* stop HW Rx aggregation. ampdu_action existence
1371 * already verified in session init so we add the BUG_ON */
1372 BUG_ON(!local->ops->ampdu_action);
1373
1374 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001375 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001376 if (ret)
1377 printk(KERN_DEBUG "HW problem - can not stop rx "
1378 "aggergation for tid %d\n", tid);
1379
1380 /* shutdown timer has not expired */
1381 if (initiator != WLAN_BACK_TIMER)
1382 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid].
1383 session_timer);
1384
1385 /* check if this is a self generated aggregation halt */
1386 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1387 ieee80211_send_delba(dev, ra, tid, 0, reason);
1388
1389 /* free the reordering buffer */
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001390 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid].buf_size; i++) {
1391 if (sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]) {
1392 /* release the reordered frames */
1393 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid].reorder_buf[i]);
1394 sta->ampdu_mlme.tid_rx[tid].stored_mpdu_num--;
1395 sta->ampdu_mlme.tid_rx[tid].reorder_buf[i] = NULL;
1396 }
1397 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001398 kfree(sta->ampdu_mlme.tid_rx[tid].reorder_buf);
1399
1400 sta->ampdu_mlme.tid_rx[tid].state = HT_AGG_STATE_IDLE;
Johannes Bergd0709a62008-02-25 16:27:46 +01001401 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001402}
1403
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001404
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001405static void ieee80211_sta_process_delba(struct net_device *dev,
1406 struct ieee80211_mgmt *mgmt, size_t len)
1407{
1408 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1409 struct sta_info *sta;
1410 u16 tid, params;
1411 u16 initiator;
1412 DECLARE_MAC_BUF(mac);
1413
Johannes Bergd0709a62008-02-25 16:27:46 +01001414 rcu_read_lock();
1415
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001416 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001417 if (!sta) {
1418 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001419 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001420 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001421
1422 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1423 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1424 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1425
1426#ifdef CONFIG_MAC80211_HT_DEBUG
1427 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001428 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1429 print_mac(mac, mgmt->sa),
1430 initiator ? "recipient" : "initiator", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001431 mgmt->u.action.u.delba.reason_code);
1432#endif /* CONFIG_MAC80211_HT_DEBUG */
1433
1434 if (initiator == WLAN_BACK_INITIATOR)
1435 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1436 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001437 else { /* WLAN_BACK_RECIPIENT */
1438 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1439 sta->ampdu_mlme.tid_tx[tid].state =
1440 HT_AGG_STATE_OPERATIONAL;
1441 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1442 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1443 WLAN_BACK_RECIPIENT);
1444 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001445 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001446}
1447
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001448/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001449 * After sending add Block Ack request we activated a timer until
1450 * add Block Ack response will arrive from the recipient.
1451 * If this timer expires sta_addba_resp_timer_expired will be executed.
1452 */
1453void sta_addba_resp_timer_expired(unsigned long data)
1454{
1455 /* not an elegant detour, but there is no choice as the timer passes
1456 * only one argument, and both sta_info and TID are needed, so init
1457 * flow in sta_info_add gives the TID as data, while the timer_to_id
1458 * array gives the sta through container_of */
1459 u16 tid = *(int *)data;
1460 struct sta_info *temp_sta = container_of((void *)data,
1461 struct sta_info, timer_to_tid[tid]);
1462
1463 struct ieee80211_local *local = temp_sta->local;
1464 struct ieee80211_hw *hw = &local->hw;
1465 struct sta_info *sta;
1466 u8 *state;
1467
Johannes Bergd0709a62008-02-25 16:27:46 +01001468 rcu_read_lock();
1469
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001470 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001471 if (!sta) {
1472 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001473 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001474 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001475
1476 state = &sta->ampdu_mlme.tid_tx[tid].state;
1477 /* check if the TID waits for addBA response */
1478 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
1479 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
1480 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1481 *state = HT_AGG_STATE_IDLE;
1482 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1483 "expecting addBA response there", tid);
1484 goto timer_expired_exit;
1485 }
1486
1487 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1488
1489 /* go through the state check in stop_BA_session */
1490 *state = HT_AGG_STATE_OPERATIONAL;
1491 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
1492 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1493 WLAN_BACK_INITIATOR);
1494
1495timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001496 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001497}
1498
1499/*
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001500 * After receiving Block Ack Request (BAR) we activated a
1501 * timer after each frame arrives from the originator.
1502 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1503 */
1504void sta_rx_agg_session_timer_expired(unsigned long data)
1505{
1506 /* not an elegant detour, but there is no choice as the timer passes
1507 * only one argument, and verious sta_info are needed here, so init
1508 * flow in sta_info_add gives the TID as data, while the timer_to_id
1509 * array gives the sta through container_of */
1510 u8 *ptid = (u8 *)data;
1511 u8 *timer_to_id = ptid - *ptid;
1512 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1513 timer_to_tid[0]);
1514
1515 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergd0709a62008-02-25 16:27:46 +01001516 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1517 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001518 WLAN_REASON_QSTA_TIMEOUT);
1519}
1520
1521
Jiri Bencf0706e82007-05-05 11:45:53 -07001522static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1523 struct ieee80211_if_sta *ifsta,
1524 struct ieee80211_mgmt *mgmt,
1525 size_t len)
1526{
1527 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1528 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001529 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001530
1531 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Berg51fb61e2007-12-19 01:31:27 +01001532 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001533 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001534 "%s, but not in authenticate state - ignored\n",
1535 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001536 return;
1537 }
1538
1539 if (len < 24 + 6) {
1540 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001541 "received from %s - ignored\n",
1542 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001543 return;
1544 }
1545
Johannes Berg51fb61e2007-12-19 01:31:27 +01001546 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001547 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1548 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001549 "unknown AP (SA=%s BSSID=%s) - "
1550 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1551 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001552 return;
1553 }
1554
Johannes Berg51fb61e2007-12-19 01:31:27 +01001555 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001556 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1557 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001558 "unknown BSSID (SA=%s BSSID=%s) - "
1559 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1560 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001561 return;
1562 }
1563
1564 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1565 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1566 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1567
Joe Perches0795af52007-10-03 17:59:30 -07001568 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e82007-05-05 11:45:53 -07001569 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001570 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e82007-05-05 11:45:53 -07001571 auth_transaction, status_code);
1572
Johannes Berg51fb61e2007-12-19 01:31:27 +01001573 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001574 /* IEEE 802.11 standard does not require authentication in IBSS
1575 * networks and most implementations do not seem to use it.
1576 * However, try to reply to authentication attempts if someone
1577 * has actually implemented this.
1578 * TODO: Could implement shared key authentication. */
1579 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1580 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1581 "frame (alg=%d transaction=%d)\n",
1582 dev->name, auth_alg, auth_transaction);
1583 return;
1584 }
1585 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1586 }
1587
1588 if (auth_alg != ifsta->auth_alg ||
1589 auth_transaction != ifsta->auth_transaction) {
1590 printk(KERN_DEBUG "%s: unexpected authentication frame "
1591 "(alg=%d transaction=%d)\n",
1592 dev->name, auth_alg, auth_transaction);
1593 return;
1594 }
1595
1596 if (status_code != WLAN_STATUS_SUCCESS) {
1597 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1598 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1599 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1600 u8 algs[3];
1601 const int num_algs = ARRAY_SIZE(algs);
1602 int i, pos;
1603 algs[0] = algs[1] = algs[2] = 0xff;
1604 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1605 algs[0] = WLAN_AUTH_OPEN;
1606 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1607 algs[1] = WLAN_AUTH_SHARED_KEY;
1608 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1609 algs[2] = WLAN_AUTH_LEAP;
1610 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1611 pos = 0;
1612 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1613 pos = 1;
1614 else
1615 pos = 2;
1616 for (i = 0; i < num_algs; i++) {
1617 pos++;
1618 if (pos >= num_algs)
1619 pos = 0;
1620 if (algs[pos] == ifsta->auth_alg ||
1621 algs[pos] == 0xff)
1622 continue;
1623 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1624 !ieee80211_sta_wep_configured(dev))
1625 continue;
1626 ifsta->auth_alg = algs[pos];
1627 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1628 "next try\n",
1629 dev->name, ifsta->auth_alg);
1630 break;
1631 }
1632 }
1633 return;
1634 }
1635
1636 switch (ifsta->auth_alg) {
1637 case WLAN_AUTH_OPEN:
1638 case WLAN_AUTH_LEAP:
1639 ieee80211_auth_completed(dev, ifsta);
1640 break;
1641 case WLAN_AUTH_SHARED_KEY:
1642 if (ifsta->auth_transaction == 4)
1643 ieee80211_auth_completed(dev, ifsta);
1644 else
1645 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1646 break;
1647 }
1648}
1649
1650
1651static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1652 struct ieee80211_if_sta *ifsta,
1653 struct ieee80211_mgmt *mgmt,
1654 size_t len)
1655{
1656 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001657 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001658
1659 if (len < 24 + 2) {
1660 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001661 "received from %s - ignored\n",
1662 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001663 return;
1664 }
1665
1666 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1667 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001668 "unknown AP (SA=%s BSSID=%s) - "
1669 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1670 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001671 return;
1672 }
1673
1674 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1675
Joe Perches0795af52007-10-03 17:59:30 -07001676 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001677 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001678 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001679
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001680 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001681 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1682 }
1683
1684 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1685 ifsta->state == IEEE80211_ASSOCIATE ||
1686 ifsta->state == IEEE80211_ASSOCIATED) {
1687 ifsta->state = IEEE80211_AUTHENTICATE;
1688 mod_timer(&ifsta->timer, jiffies +
1689 IEEE80211_RETRY_AUTH_INTERVAL);
1690 }
1691
1692 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001693 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001694}
1695
1696
1697static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1698 struct ieee80211_if_sta *ifsta,
1699 struct ieee80211_mgmt *mgmt,
1700 size_t len)
1701{
1702 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001703 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001704
1705 if (len < 24 + 2) {
1706 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001707 "received from %s - ignored\n",
1708 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001709 return;
1710 }
1711
1712 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1713 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001714 "unknown AP (SA=%s BSSID=%s) - "
1715 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1716 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001717 return;
1718 }
1719
1720 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1721
Joe Perches0795af52007-10-03 17:59:30 -07001722 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001723 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001724 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001725
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001726 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001727 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1728
1729 if (ifsta->state == IEEE80211_ASSOCIATED) {
1730 ifsta->state = IEEE80211_ASSOCIATE;
1731 mod_timer(&ifsta->timer, jiffies +
1732 IEEE80211_RETRY_AUTH_INTERVAL);
1733 }
1734
1735 ieee80211_set_disassoc(dev, ifsta, 0);
1736}
1737
1738
Johannes Berg471b3ef2007-12-28 14:32:58 +01001739static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001740 struct ieee80211_if_sta *ifsta,
1741 struct ieee80211_mgmt *mgmt,
1742 size_t len,
1743 int reassoc)
1744{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001745 struct ieee80211_local *local = sdata->local;
1746 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01001747 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001748 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001749 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001750 u16 capab_info, status_code, aid;
1751 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001752 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001753 u8 *pos;
1754 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001755 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001756 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001757
1758 /* AssocResp and ReassocResp have identical structure, so process both
1759 * of them in this function. */
1760
1761 if (ifsta->state != IEEE80211_ASSOCIATE) {
1762 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001763 "%s, but not in associate state - ignored\n",
1764 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001765 return;
1766 }
1767
1768 if (len < 24 + 6) {
1769 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001770 "received from %s - ignored\n",
1771 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001772 return;
1773 }
1774
1775 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1776 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001777 "unknown AP (SA=%s BSSID=%s) - "
1778 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1779 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001780 return;
1781 }
1782
1783 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1784 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1785 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001786
Joe Perches0795af52007-10-03 17:59:30 -07001787 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001788 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001789 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001790 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001791
1792 if (status_code != WLAN_STATUS_SUCCESS) {
1793 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1794 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001795 /* if this was a reassociation, ensure we try a "full"
1796 * association next time. This works around some broken APs
1797 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001798 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07001799 return;
1800 }
1801
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001802 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1803 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1804 "set\n", dev->name, aid);
1805 aid &= ~(BIT(15) | BIT(14));
1806
Jiri Bencf0706e82007-05-05 11:45:53 -07001807 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001808 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001809
1810 if (!elems.supp_rates) {
1811 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1812 dev->name);
1813 return;
1814 }
1815
1816 printk(KERN_DEBUG "%s: associated\n", dev->name);
1817 ifsta->aid = aid;
1818 ifsta->ap_capab = capab_info;
1819
1820 kfree(ifsta->assocresp_ies);
1821 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001822 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001823 if (ifsta->assocresp_ies)
1824 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1825
Johannes Bergd0709a62008-02-25 16:27:46 +01001826 rcu_read_lock();
1827
Jiri Bencf0706e82007-05-05 11:45:53 -07001828 /* Add STA entry for the AP */
1829 sta = sta_info_get(local, ifsta->bssid);
1830 if (!sta) {
1831 struct ieee80211_sta_bss *bss;
Johannes Bergd0709a62008-02-25 16:27:46 +01001832
1833 sta = sta_info_add(sdata, ifsta->bssid);
Johannes Berg43ba7e92008-02-21 14:09:30 +01001834 if (IS_ERR(sta)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001835 printk(KERN_DEBUG "%s: failed to add STA entry for the"
Johannes Berg43ba7e92008-02-21 14:09:30 +01001836 " AP (error %ld)\n", dev->name, PTR_ERR(sta));
Johannes Bergd0709a62008-02-25 16:27:46 +01001837 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001838 return;
1839 }
John W. Linville65c107a2007-10-05 14:23:27 -04001840 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001841 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001842 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001843 if (bss) {
1844 sta->last_rssi = bss->rssi;
1845 sta->last_signal = bss->signal;
1846 sta->last_noise = bss->noise;
1847 ieee80211_rx_bss_put(dev, bss);
1848 }
1849 }
1850
Johannes Berg238814f2008-01-28 17:19:37 +01001851 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1852 WLAN_STA_AUTHORIZED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001853
1854 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001855 basic_rates = 0;
1856 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1857
Jiri Bencf0706e82007-05-05 11:45:53 -07001858 for (i = 0; i < elems.supp_rates_len; i++) {
1859 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001860
1861 if (rate > 110)
1862 have_higher_than_11mbit = true;
1863
1864 for (j = 0; j < sband->n_bitrates; j++) {
1865 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001866 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001867 if (elems.supp_rates[i] & 0x80)
1868 basic_rates |= BIT(j);
1869 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001870 }
Johannes Berg8318d782008-01-24 19:38:38 +01001871
Jiri Bencf0706e82007-05-05 11:45:53 -07001872 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1873 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001874
1875 if (rate > 110)
1876 have_higher_than_11mbit = true;
1877
1878 for (j = 0; j < sband->n_bitrates; j++) {
1879 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07001880 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01001881 if (elems.ext_supp_rates[i] & 0x80)
1882 basic_rates |= BIT(j);
1883 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001884 }
Johannes Berg8318d782008-01-24 19:38:38 +01001885
1886 sta->supp_rates[local->hw.conf.channel->band] = rates;
1887 sdata->basic_rates = basic_rates;
1888
1889 /* cf. IEEE 802.11 9.2.12 */
1890 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
1891 have_higher_than_11mbit)
1892 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1893 else
1894 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07001895
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001896 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
1897 local->ops->conf_ht) {
1898 struct ieee80211_ht_bss_info bss_info;
1899
1900 ieee80211_ht_cap_ie_to_ht_info(
1901 (struct ieee80211_ht_cap *)
1902 elems.ht_cap_elem, &sta->ht_info);
1903 ieee80211_ht_addt_info_ie_to_ht_bss_info(
1904 (struct ieee80211_ht_addt_info *)
1905 elems.ht_info_elem, &bss_info);
1906 ieee80211_hw_config_ht(local, 1, &sta->ht_info, &bss_info);
1907 }
1908
Jiri Bencf0706e82007-05-05 11:45:53 -07001909 rate_control_rate_init(sta, local);
1910
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001911 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001912 sta->flags |= WLAN_STA_WME;
1913 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1914 elems.wmm_param_len);
1915 }
1916
Johannes Berg8318d782008-01-24 19:38:38 +01001917 /* set AID, ieee80211_set_associated() will tell the driver */
1918 bss_conf->aid = aid;
1919 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07001920
Johannes Bergd0709a62008-02-25 16:27:46 +01001921 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001922
1923 ieee80211_associated(dev, ifsta);
1924}
1925
1926
1927/* Caller must hold local->sta_bss_lock */
1928static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1929 struct ieee80211_sta_bss *bss)
1930{
1931 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01001932 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01001933
1934 if (bss_mesh_cfg(bss))
1935 hash_idx = mesh_id_hash(bss_mesh_id(bss),
1936 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01001937 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01001938 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01001939
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01001940 bss->hnext = local->sta_bss_hash[hash_idx];
1941 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07001942}
1943
1944
1945/* Caller must hold local->sta_bss_lock */
1946static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1947 struct ieee80211_sta_bss *bss)
1948{
1949 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1950 struct ieee80211_sta_bss *b, *prev = NULL;
1951 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1952 while (b) {
1953 if (b == bss) {
1954 if (!prev)
1955 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1956 bss->hnext;
1957 else
1958 prev->hnext = bss->hnext;
1959 break;
1960 }
1961 prev = b;
1962 b = b->hnext;
1963 }
1964}
1965
1966
1967static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01001968ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001969 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001970{
1971 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1972 struct ieee80211_sta_bss *bss;
1973
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001974 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07001975 if (!bss)
1976 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001977 atomic_inc(&bss->users);
1978 atomic_inc(&bss->users);
1979 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01001980 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04001981 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
1982 memcpy(bss->ssid, ssid, ssid_len);
1983 bss->ssid_len = ssid_len;
1984 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001985
1986 spin_lock_bh(&local->sta_bss_lock);
1987 /* TODO: order by RSSI? */
1988 list_add_tail(&bss->list, &local->sta_bss_list);
1989 __ieee80211_rx_bss_hash_add(dev, bss);
1990 spin_unlock_bh(&local->sta_bss_lock);
1991 return bss;
1992}
1993
Jiri Bencf0706e82007-05-05 11:45:53 -07001994static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01001995ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001996 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07001997{
1998 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1999 struct ieee80211_sta_bss *bss;
2000
2001 spin_lock_bh(&local->sta_bss_lock);
2002 bss = local->sta_bss_hash[STA_HASH(bssid)];
2003 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002004 if (!bss_mesh_cfg(bss) &&
2005 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002006 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002007 bss->ssid_len == ssid_len &&
2008 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002009 atomic_inc(&bss->users);
2010 break;
2011 }
2012 bss = bss->hnext;
2013 }
2014 spin_unlock_bh(&local->sta_bss_lock);
2015 return bss;
2016}
2017
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002018#ifdef CONFIG_MAC80211_MESH
2019static struct ieee80211_sta_bss *
2020ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2021 u8 *mesh_cfg, int freq)
2022{
2023 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2024 struct ieee80211_sta_bss *bss;
2025
2026 spin_lock_bh(&local->sta_bss_lock);
2027 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2028 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002029 if (bss_mesh_cfg(bss) &&
2030 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002031 bss->freq == freq &&
2032 mesh_id_len == bss->mesh_id_len &&
2033 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2034 mesh_id_len))) {
2035 atomic_inc(&bss->users);
2036 break;
2037 }
2038 bss = bss->hnext;
2039 }
2040 spin_unlock_bh(&local->sta_bss_lock);
2041 return bss;
2042}
2043
2044static struct ieee80211_sta_bss *
2045ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2046 u8 *mesh_cfg, int freq)
2047{
2048 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2049 struct ieee80211_sta_bss *bss;
2050
2051 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2052 if (!bss)
2053 return NULL;
2054
2055 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
2056 if (!bss->mesh_cfg) {
2057 kfree(bss);
2058 return NULL;
2059 }
2060
2061 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2062 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2063 if (!bss->mesh_id) {
2064 kfree(bss->mesh_cfg);
2065 kfree(bss);
2066 return NULL;
2067 }
2068 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2069 }
2070
2071 atomic_inc(&bss->users);
2072 atomic_inc(&bss->users);
2073 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
2074 bss->mesh_id_len = mesh_id_len;
2075 bss->freq = freq;
2076 spin_lock_bh(&local->sta_bss_lock);
2077 /* TODO: order by RSSI? */
2078 list_add_tail(&bss->list, &local->sta_bss_list);
2079 __ieee80211_rx_bss_hash_add(dev, bss);
2080 spin_unlock_bh(&local->sta_bss_lock);
2081 return bss;
2082}
2083#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002084
2085static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2086{
2087 kfree(bss->wpa_ie);
2088 kfree(bss->rsn_ie);
2089 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002090 kfree(bss->ht_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002091 kfree(bss_mesh_id(bss));
2092 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002093 kfree(bss);
2094}
2095
2096
2097static void ieee80211_rx_bss_put(struct net_device *dev,
2098 struct ieee80211_sta_bss *bss)
2099{
2100 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2101 if (!atomic_dec_and_test(&bss->users))
2102 return;
2103
2104 spin_lock_bh(&local->sta_bss_lock);
2105 __ieee80211_rx_bss_hash_del(dev, bss);
2106 list_del(&bss->list);
2107 spin_unlock_bh(&local->sta_bss_lock);
2108 ieee80211_rx_bss_free(bss);
2109}
2110
2111
2112void ieee80211_rx_bss_list_init(struct net_device *dev)
2113{
2114 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2115 spin_lock_init(&local->sta_bss_lock);
2116 INIT_LIST_HEAD(&local->sta_bss_list);
2117}
2118
2119
2120void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2121{
2122 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2123 struct ieee80211_sta_bss *bss, *tmp;
2124
2125 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2126 ieee80211_rx_bss_put(dev, bss);
2127}
2128
2129
Bruno Randolfa6072682008-02-18 11:21:15 +09002130static int ieee80211_sta_join_ibss(struct net_device *dev,
2131 struct ieee80211_if_sta *ifsta,
2132 struct ieee80211_sta_bss *bss)
2133{
2134 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2135 int res, rates, i, j;
2136 struct sk_buff *skb;
2137 struct ieee80211_mgmt *mgmt;
2138 struct ieee80211_tx_control control;
2139 struct rate_selection ratesel;
2140 u8 *pos;
2141 struct ieee80211_sub_if_data *sdata;
2142 struct ieee80211_supported_band *sband;
2143
2144 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2145
2146 /* Remove possible STA entries from other IBSS networks. */
2147 sta_info_flush(local, NULL);
2148
2149 if (local->ops->reset_tsf) {
2150 /* Reset own TSF to allow time synchronization work. */
2151 local->ops->reset_tsf(local_to_hw(local));
2152 }
2153 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2154 res = ieee80211_if_config(dev);
2155 if (res)
2156 return res;
2157
2158 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2159
2160 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2161 sdata->drop_unencrypted = bss->capability &
2162 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2163
2164 res = ieee80211_set_freq(local, bss->freq);
2165
2166 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2167 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2168 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2169 return -1;
2170 }
2171
2172 /* Set beacon template */
2173 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2174 do {
2175 if (!skb)
2176 break;
2177
2178 skb_reserve(skb, local->hw.extra_tx_headroom);
2179
2180 mgmt = (struct ieee80211_mgmt *)
2181 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2182 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2183 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2184 IEEE80211_STYPE_BEACON);
2185 memset(mgmt->da, 0xff, ETH_ALEN);
2186 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2187 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2188 mgmt->u.beacon.beacon_int =
2189 cpu_to_le16(local->hw.conf.beacon_int);
2190 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2191
2192 pos = skb_put(skb, 2 + ifsta->ssid_len);
2193 *pos++ = WLAN_EID_SSID;
2194 *pos++ = ifsta->ssid_len;
2195 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2196
2197 rates = bss->supp_rates_len;
2198 if (rates > 8)
2199 rates = 8;
2200 pos = skb_put(skb, 2 + rates);
2201 *pos++ = WLAN_EID_SUPP_RATES;
2202 *pos++ = rates;
2203 memcpy(pos, bss->supp_rates, rates);
2204
2205 if (bss->band == IEEE80211_BAND_2GHZ) {
2206 pos = skb_put(skb, 2 + 1);
2207 *pos++ = WLAN_EID_DS_PARAMS;
2208 *pos++ = 1;
2209 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2210 }
2211
2212 pos = skb_put(skb, 2 + 2);
2213 *pos++ = WLAN_EID_IBSS_PARAMS;
2214 *pos++ = 2;
2215 /* FIX: set ATIM window based on scan results */
2216 *pos++ = 0;
2217 *pos++ = 0;
2218
2219 if (bss->supp_rates_len > 8) {
2220 rates = bss->supp_rates_len - 8;
2221 pos = skb_put(skb, 2 + rates);
2222 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2223 *pos++ = rates;
2224 memcpy(pos, &bss->supp_rates[8], rates);
2225 }
2226
2227 memset(&control, 0, sizeof(control));
2228 rate_control_get_rate(dev, sband, skb, &ratesel);
2229 if (!ratesel.rate) {
2230 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2231 "for IBSS beacon\n", dev->name);
2232 break;
2233 }
2234 control.vif = &sdata->vif;
2235 control.tx_rate = ratesel.rate;
2236 if (sdata->bss_conf.use_short_preamble &&
2237 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2238 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2239 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2240 control.flags |= IEEE80211_TXCTL_NO_ACK;
2241 control.retry_limit = 1;
2242
2243 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2244 if (ifsta->probe_resp) {
2245 mgmt = (struct ieee80211_mgmt *)
2246 ifsta->probe_resp->data;
2247 mgmt->frame_control =
2248 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2249 IEEE80211_STYPE_PROBE_RESP);
2250 } else {
2251 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2252 "template for IBSS\n", dev->name);
2253 }
2254
2255 if (local->ops->beacon_update &&
2256 local->ops->beacon_update(local_to_hw(local),
2257 skb, &control) == 0) {
2258 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2259 "template\n", dev->name);
2260 skb = NULL;
2261 }
2262
2263 rates = 0;
2264 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2265 for (i = 0; i < bss->supp_rates_len; i++) {
2266 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2267 for (j = 0; j < sband->n_bitrates; j++)
2268 if (sband->bitrates[j].bitrate == bitrate)
2269 rates |= BIT(j);
2270 }
2271 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
2272 } while (0);
2273
2274 if (skb) {
2275 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2276 "template\n", dev->name);
2277 dev_kfree_skb(skb);
2278 }
2279
2280 ifsta->state = IEEE80211_IBSS_JOINED;
2281 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2282
2283 ieee80211_rx_bss_put(dev, bss);
2284
2285 return res;
2286}
2287
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002288u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2289 struct ieee802_11_elems *elems,
2290 enum ieee80211_band band)
2291{
2292 struct ieee80211_supported_band *sband;
2293 struct ieee80211_rate *bitrates;
2294 size_t num_rates;
2295 u64 supp_rates;
2296 int i, j;
2297 sband = local->hw.wiphy->bands[band];
2298
2299 if (!sband) {
2300 WARN_ON(1);
2301 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2302 }
2303
2304 bitrates = sband->bitrates;
2305 num_rates = sband->n_bitrates;
2306 supp_rates = 0;
2307 for (i = 0; i < elems->supp_rates_len +
2308 elems->ext_supp_rates_len; i++) {
2309 u8 rate = 0;
2310 int own_rate;
2311 if (i < elems->supp_rates_len)
2312 rate = elems->supp_rates[i];
2313 else if (elems->ext_supp_rates)
2314 rate = elems->ext_supp_rates
2315 [i - elems->supp_rates_len];
2316 own_rate = 5 * (rate & 0x7f);
2317 for (j = 0; j < num_rates; j++)
2318 if (bitrates[j].bitrate == own_rate)
2319 supp_rates |= BIT(j);
2320 }
2321 return supp_rates;
2322}
2323
Bruno Randolfa6072682008-02-18 11:21:15 +09002324
Jiri Bencf0706e82007-05-05 11:45:53 -07002325static void ieee80211_rx_bss_info(struct net_device *dev,
2326 struct ieee80211_mgmt *mgmt,
2327 size_t len,
2328 struct ieee80211_rx_status *rx_status,
2329 int beacon)
2330{
2331 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2332 struct ieee802_11_elems elems;
2333 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002334 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002335 struct ieee80211_sta_bss *bss;
2336 struct sta_info *sta;
2337 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002338 u64 beacon_timestamp, rx_timestamp;
Joe Perches0795af52007-10-03 17:59:30 -07002339 DECLARE_MAC_BUF(mac);
2340 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002341
2342 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2343 return; /* ignore ProbeResp to foreign address */
2344
2345#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002346 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07002347 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002348 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002349#endif
2350
2351 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2352 if (baselen > len)
2353 return;
2354
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002355 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
John W. Linville67a4cce2007-10-12 16:40:37 -04002356 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002357
Johannes Berg902acc72008-02-23 15:17:19 +01002358 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2359 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2360 u64 rates = ieee80211_sta_get_rates(local, &elems,
2361 rx_status->band);
2362
2363 mesh_neighbour_update(mgmt->sa, rates, dev,
2364 mesh_peer_accepts_plinks(&elems, dev));
2365 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002366
Johannes Bergd0709a62008-02-25 16:27:46 +01002367 rcu_read_lock();
2368
Johannes Berg51fb61e2007-12-19 01:31:27 +01002369 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002370 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2371 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002372 u64 prev_rates;
2373 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2374 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002375
Johannes Berg8318d782008-01-24 19:38:38 +01002376 prev_rates = sta->supp_rates[rx_status->band];
2377 sta->supp_rates[rx_status->band] &= supp_rates;
2378 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002379 /* No matching rates - this should not really happen.
2380 * Make sure that at least one rate is marked
2381 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002382 sta->supp_rates[rx_status->band] =
2383 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002384 }
Johannes Berg8318d782008-01-24 19:38:38 +01002385 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002386 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002387 "%s based on beacon info (0x%llx & 0x%llx -> "
2388 "0x%llx)\n",
2389 dev->name, print_mac(mac, sta->addr),
2390 (unsigned long long) prev_rates,
2391 (unsigned long long) supp_rates,
2392 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002393 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002394 }
2395
Johannes Bergd0709a62008-02-25 16:27:46 +01002396 rcu_read_unlock();
2397
Jiri Bencf0706e82007-05-05 11:45:53 -07002398 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002399 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002400 else
Johannes Berg8318d782008-01-24 19:38:38 +01002401 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002402
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002403#ifdef CONFIG_MAC80211_MESH
2404 if (elems.mesh_config)
2405 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2406 elems.mesh_id_len, elems.mesh_config, freq);
2407 else
2408#endif
2409 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002410 elems.ssid, elems.ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002411 if (!bss) {
2412#ifdef CONFIG_MAC80211_MESH
2413 if (elems.mesh_config)
2414 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
2415 elems.mesh_id_len, elems.mesh_config, freq);
2416 else
2417#endif
2418 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2419 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002420 if (!bss)
2421 return;
2422 } else {
2423#if 0
2424 /* TODO: order by RSSI? */
2425 spin_lock_bh(&local->sta_bss_lock);
2426 list_move_tail(&bss->list, &local->sta_bss_list);
2427 spin_unlock_bh(&local->sta_bss_lock);
2428#endif
2429 }
2430
Johannes Berg8318d782008-01-24 19:38:38 +01002431 bss->band = rx_status->band;
2432
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002433 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2434 bss->probe_resp && beacon) {
2435 /* STA mode:
2436 * Do not allow beacon to override data from Probe Response. */
Jiri Bencf0706e82007-05-05 11:45:53 -07002437 ieee80211_rx_bss_put(dev, bss);
2438 return;
2439 }
2440
Daniel Drake56282212007-07-10 19:32:10 +02002441 /* save the ERP value so that it is available at association time */
2442 if (elems.erp_info && elems.erp_info_len >= 1) {
2443 bss->erp_value = elems.erp_info[0];
2444 bss->has_erp_value = 1;
2445 }
2446
Jiri Bencf0706e82007-05-05 11:45:53 -07002447 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2448 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002449
2450 bss->supp_rates_len = 0;
2451 if (elems.supp_rates) {
2452 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2453 if (clen > elems.supp_rates_len)
2454 clen = elems.supp_rates_len;
2455 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2456 clen);
2457 bss->supp_rates_len += clen;
2458 }
2459 if (elems.ext_supp_rates) {
2460 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2461 if (clen > elems.ext_supp_rates_len)
2462 clen = elems.ext_supp_rates_len;
2463 memcpy(&bss->supp_rates[bss->supp_rates_len],
2464 elems.ext_supp_rates, clen);
2465 bss->supp_rates_len += clen;
2466 }
2467
2468 if (elems.wpa &&
2469 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2470 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2471 kfree(bss->wpa_ie);
2472 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2473 if (bss->wpa_ie) {
2474 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2475 bss->wpa_ie_len = elems.wpa_len + 2;
2476 } else
2477 bss->wpa_ie_len = 0;
2478 } else if (!elems.wpa && bss->wpa_ie) {
2479 kfree(bss->wpa_ie);
2480 bss->wpa_ie = NULL;
2481 bss->wpa_ie_len = 0;
2482 }
2483
2484 if (elems.rsn &&
2485 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2486 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2487 kfree(bss->rsn_ie);
2488 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2489 if (bss->rsn_ie) {
2490 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2491 bss->rsn_ie_len = elems.rsn_len + 2;
2492 } else
2493 bss->rsn_ie_len = 0;
2494 } else if (!elems.rsn && bss->rsn_ie) {
2495 kfree(bss->rsn_ie);
2496 bss->rsn_ie = NULL;
2497 bss->rsn_ie_len = 0;
2498 }
2499
2500 if (elems.wmm_param &&
2501 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2502 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2503 kfree(bss->wmm_ie);
2504 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2505 if (bss->wmm_ie) {
2506 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2507 elems.wmm_param_len + 2);
2508 bss->wmm_ie_len = elems.wmm_param_len + 2;
2509 } else
2510 bss->wmm_ie_len = 0;
2511 } else if (!elems.wmm_param && bss->wmm_ie) {
2512 kfree(bss->wmm_ie);
2513 bss->wmm_ie = NULL;
2514 bss->wmm_ie_len = 0;
2515 }
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002516 if (elems.ht_cap_elem &&
2517 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2518 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2519 kfree(bss->ht_ie);
2520 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2521 if (bss->ht_ie) {
2522 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2523 elems.ht_cap_elem_len + 2);
2524 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2525 } else
2526 bss->ht_ie_len = 0;
2527 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2528 kfree(bss->ht_ie);
2529 bss->ht_ie = NULL;
2530 bss->ht_ie_len = 0;
2531 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002532
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002533 bss->timestamp = beacon_timestamp;
Jiri Bencf0706e82007-05-05 11:45:53 -07002534 bss->last_update = jiffies;
2535 bss->rssi = rx_status->ssi;
2536 bss->signal = rx_status->signal;
2537 bss->noise = rx_status->noise;
2538 if (!beacon)
2539 bss->probe_resp++;
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002540
2541 /* check if we need to merge IBSS */
2542 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2543 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002544 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002545 bss->freq == local->oper_channel->center_freq &&
2546 elems.ssid_len == sdata->u.sta.ssid_len &&
2547 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2548 if (rx_status->flag & RX_FLAG_TSFT) {
2549 /* in order for correct IBSS merging we need mactime
2550 *
2551 * since mactime is defined as the time the first data
2552 * symbol of the frame hits the PHY, and the timestamp
2553 * of the beacon is defined as "the time that the data
2554 * symbol containing the first bit of the timestamp is
2555 * transmitted to the PHY plus the transmitting STA’s
2556 * delays through its local PHY from the MAC-PHY
2557 * interface to its interface with the WM"
2558 * (802.11 11.1.2) - equals the time this bit arrives at
2559 * the receiver - we have to take into account the
2560 * offset between the two.
2561 * e.g: at 1 MBit that means mactime is 192 usec earlier
2562 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2563 */
2564 int rate = local->hw.wiphy->bands[rx_status->band]->
2565 bitrates[rx_status->rate_idx].bitrate;
2566 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2567 } else if (local && local->ops && local->ops->get_tsf)
2568 /* second best option: get current TSF */
2569 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2570 else
2571 /* can't merge without knowing the TSF */
2572 rx_timestamp = -1LLU;
2573#ifdef CONFIG_MAC80211_IBSS_DEBUG
2574 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2575 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2576 print_mac(mac, mgmt->sa),
2577 print_mac(mac2, mgmt->bssid),
2578 (unsigned long long)rx_timestamp,
2579 (unsigned long long)beacon_timestamp,
2580 (unsigned long long)(rx_timestamp - beacon_timestamp),
2581 jiffies);
2582#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2583 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002584#ifndef CONFIG_MAC80211_IBSS_DEBUG
Pavel Roskind97cf012008-02-21 11:33:58 -05002585 if (net_ratelimit())
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002586#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002587 printk(KERN_DEBUG "%s: beacon TSF higher than "
2588 "local TSF - IBSS merge with BSSID %s\n",
2589 dev->name, print_mac(mac, mgmt->bssid));
2590 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
Johannes Bergd0709a62008-02-25 16:27:46 +01002591 rcu_read_lock();
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002592 ieee80211_ibss_add_sta(dev, NULL,
2593 mgmt->bssid, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01002594 rcu_read_unlock();
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002595 }
2596 }
2597
Jiri Bencf0706e82007-05-05 11:45:53 -07002598 ieee80211_rx_bss_put(dev, bss);
2599}
2600
2601
2602static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2603 struct ieee80211_mgmt *mgmt,
2604 size_t len,
2605 struct ieee80211_rx_status *rx_status)
2606{
2607 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2608}
2609
2610
2611static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2612 struct ieee80211_mgmt *mgmt,
2613 size_t len,
2614 struct ieee80211_rx_status *rx_status)
2615{
Jiri Bencf0706e82007-05-05 11:45:53 -07002616 struct ieee80211_sub_if_data *sdata;
2617 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002618 size_t baselen;
2619 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002620 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2621 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002622 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002623
2624 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2625
2626 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002627 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002628 return;
2629 ifsta = &sdata->u.sta;
2630
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002631 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002632 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2633 return;
2634
2635 /* Process beacon from the current BSS */
2636 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2637 if (baselen > len)
2638 return;
2639
John W. Linville67a4cce2007-10-12 16:40:37 -04002640 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002641
Daniel Drake56282212007-07-10 19:32:10 +02002642 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002643 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002644
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002645 if (elems.ht_cap_elem && elems.ht_info_elem &&
2646 elems.wmm_param && local->ops->conf_ht &&
2647 conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
2648 struct ieee80211_ht_bss_info bss_info;
2649
2650 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2651 (struct ieee80211_ht_addt_info *)
2652 elems.ht_info_elem, &bss_info);
2653 /* check if AP changed bss inforamation */
2654 if ((conf->ht_bss_conf.primary_channel !=
2655 bss_info.primary_channel) ||
2656 (conf->ht_bss_conf.bss_cap != bss_info.bss_cap) ||
2657 (conf->ht_bss_conf.bss_op_mode != bss_info.bss_op_mode))
2658 ieee80211_hw_config_ht(local, 1, &conf->ht_conf,
2659 &bss_info);
2660 }
2661
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002662 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002663 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2664 elems.wmm_param_len);
2665 }
Johannes Berg471b3ef2007-12-28 14:32:58 +01002666
2667 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002668}
2669
2670
2671static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2672 struct ieee80211_if_sta *ifsta,
2673 struct ieee80211_mgmt *mgmt,
2674 size_t len,
2675 struct ieee80211_rx_status *rx_status)
2676{
2677 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2678 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2679 int tx_last_beacon;
2680 struct sk_buff *skb;
2681 struct ieee80211_mgmt *resp;
2682 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002683 DECLARE_MAC_BUF(mac);
2684#ifdef CONFIG_MAC80211_IBSS_DEBUG
2685 DECLARE_MAC_BUF(mac2);
2686 DECLARE_MAC_BUF(mac3);
2687#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002688
Johannes Berg51fb61e2007-12-19 01:31:27 +01002689 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002690 ifsta->state != IEEE80211_IBSS_JOINED ||
2691 len < 24 + 2 || !ifsta->probe_resp)
2692 return;
2693
2694 if (local->ops->tx_last_beacon)
2695 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2696 else
2697 tx_last_beacon = 1;
2698
2699#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002700 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2701 "%s (tx_last_beacon=%d)\n",
2702 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2703 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002704#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2705
2706 if (!tx_last_beacon)
2707 return;
2708
2709 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2710 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2711 return;
2712
2713 end = ((u8 *) mgmt) + len;
2714 pos = mgmt->u.probe_req.variable;
2715 if (pos[0] != WLAN_EID_SSID ||
2716 pos + 2 + pos[1] > end) {
2717 if (net_ratelimit()) {
2718 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002719 "from %s\n",
2720 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002721 }
2722 return;
2723 }
2724 if (pos[1] != 0 &&
2725 (pos[1] != ifsta->ssid_len ||
2726 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2727 /* Ignore ProbeReq for foreign SSID */
2728 return;
2729 }
2730
2731 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002732 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002733 if (!skb)
2734 return;
2735
2736 resp = (struct ieee80211_mgmt *) skb->data;
2737 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2738#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002739 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2740 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002741#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2742 ieee80211_sta_tx(dev, skb, 0);
2743}
2744
Johannes Berg4e20cb22007-12-19 01:31:24 +01002745static void ieee80211_rx_mgmt_action(struct net_device *dev,
2746 struct ieee80211_if_sta *ifsta,
2747 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002748 size_t len,
2749 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002750{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002751 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002752
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002753 if (len < IEEE80211_MIN_ACTION_SIZE)
2754 return;
2755
2756 switch (mgmt->u.action.category) {
2757 case WLAN_CATEGORY_BACK:
2758 switch (mgmt->u.action.u.addba_req.action_code) {
2759 case WLAN_ACTION_ADDBA_REQ:
2760 if (len < (IEEE80211_MIN_ACTION_SIZE +
2761 sizeof(mgmt->u.action.u.addba_req)))
2762 break;
2763 ieee80211_sta_process_addba_request(dev, mgmt, len);
2764 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002765 case WLAN_ACTION_ADDBA_RESP:
2766 if (len < (IEEE80211_MIN_ACTION_SIZE +
2767 sizeof(mgmt->u.action.u.addba_resp)))
2768 break;
2769 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2770 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002771 case WLAN_ACTION_DELBA:
2772 if (len < (IEEE80211_MIN_ACTION_SIZE +
2773 sizeof(mgmt->u.action.u.delba)))
2774 break;
2775 ieee80211_sta_process_delba(dev, mgmt, len);
2776 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002777 default:
2778 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002779 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002780 dev->name);
2781 break;
2782 }
2783 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002784 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002785 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002786 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
2787 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002788 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002789 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002790 mesh_rx_path_sel_frame(dev, mgmt, len);
2791 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002792 default:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002793 if (net_ratelimit())
2794 printk(KERN_DEBUG "%s: Rx unknown action frame - "
2795 "category=%d\n", dev->name, mgmt->u.action.category);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002796 break;
2797 }
2798}
Jiri Bencf0706e82007-05-05 11:45:53 -07002799
2800void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2801 struct ieee80211_rx_status *rx_status)
2802{
2803 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2804 struct ieee80211_sub_if_data *sdata;
2805 struct ieee80211_if_sta *ifsta;
2806 struct ieee80211_mgmt *mgmt;
2807 u16 fc;
2808
2809 if (skb->len < 24)
2810 goto fail;
2811
2812 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2813 ifsta = &sdata->u.sta;
2814
2815 mgmt = (struct ieee80211_mgmt *) skb->data;
2816 fc = le16_to_cpu(mgmt->frame_control);
2817
2818 switch (fc & IEEE80211_FCTL_STYPE) {
2819 case IEEE80211_STYPE_PROBE_REQ:
2820 case IEEE80211_STYPE_PROBE_RESP:
2821 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002822 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07002823 memcpy(skb->cb, rx_status, sizeof(*rx_status));
2824 case IEEE80211_STYPE_AUTH:
2825 case IEEE80211_STYPE_ASSOC_RESP:
2826 case IEEE80211_STYPE_REASSOC_RESP:
2827 case IEEE80211_STYPE_DEAUTH:
2828 case IEEE80211_STYPE_DISASSOC:
2829 skb_queue_tail(&ifsta->skb_queue, skb);
2830 queue_work(local->hw.workqueue, &ifsta->work);
2831 return;
2832 default:
2833 printk(KERN_DEBUG "%s: received unknown management frame - "
2834 "stype=%d\n", dev->name,
2835 (fc & IEEE80211_FCTL_STYPE) >> 4);
2836 break;
2837 }
2838
2839 fail:
2840 kfree_skb(skb);
2841}
2842
2843
2844static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
2845 struct sk_buff *skb)
2846{
2847 struct ieee80211_rx_status *rx_status;
2848 struct ieee80211_sub_if_data *sdata;
2849 struct ieee80211_if_sta *ifsta;
2850 struct ieee80211_mgmt *mgmt;
2851 u16 fc;
2852
2853 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2854 ifsta = &sdata->u.sta;
2855
2856 rx_status = (struct ieee80211_rx_status *) skb->cb;
2857 mgmt = (struct ieee80211_mgmt *) skb->data;
2858 fc = le16_to_cpu(mgmt->frame_control);
2859
2860 switch (fc & IEEE80211_FCTL_STYPE) {
2861 case IEEE80211_STYPE_PROBE_REQ:
2862 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
2863 rx_status);
2864 break;
2865 case IEEE80211_STYPE_PROBE_RESP:
2866 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
2867 break;
2868 case IEEE80211_STYPE_BEACON:
2869 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
2870 break;
2871 case IEEE80211_STYPE_AUTH:
2872 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
2873 break;
2874 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002875 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07002876 break;
2877 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01002878 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002879 break;
2880 case IEEE80211_STYPE_DEAUTH:
2881 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
2882 break;
2883 case IEEE80211_STYPE_DISASSOC:
2884 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
2885 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002886 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002887 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002888 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07002889 }
2890
2891 kfree_skb(skb);
2892}
2893
2894
Johannes Berg9ae54c82008-01-31 19:48:20 +01002895ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08002896ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
2897 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07002898{
2899 struct ieee80211_mgmt *mgmt;
2900 u16 fc;
2901
Zhu Yiece8edd2007-11-22 10:53:21 +08002902 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002903 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002904
2905 mgmt = (struct ieee80211_mgmt *) skb->data;
2906 fc = le16_to_cpu(mgmt->frame_control);
2907
Zhu Yiece8edd2007-11-22 10:53:21 +08002908 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01002909 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08002910
2911 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01002912 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08002913
Jiri Bencf0706e82007-05-05 11:45:53 -07002914 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2915 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
2916 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
2917 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002918 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002919 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07002920 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
2921 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
2922 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08002923 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002924 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07002925 }
2926 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01002927 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002928}
2929
2930
2931static int ieee80211_sta_active_ibss(struct net_device *dev)
2932{
2933 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2934 int active = 0;
2935 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01002936 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07002937
Johannes Bergd0709a62008-02-25 16:27:46 +01002938 rcu_read_lock();
2939
2940 list_for_each_entry_rcu(sta, &local->sta_list, list) {
2941 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002942 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
2943 jiffies)) {
2944 active++;
2945 break;
2946 }
2947 }
Johannes Bergd0709a62008-02-25 16:27:46 +01002948
2949 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002950
2951 return active;
2952}
2953
2954
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002955static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07002956{
2957 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2958 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02002959 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07002960 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01002961 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07002962
Johannes Bergd0709a62008-02-25 16:27:46 +01002963 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07002964 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002965 if (time_after(jiffies, sta->last_rx + exp_time)) {
Joe Perches0795af52007-10-03 17:59:30 -07002966 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
2967 dev->name, print_mac(mac, sta->addr));
Johannes Bergd0709a62008-02-25 16:27:46 +01002968 sta_info_unlink(&sta);
2969 if (sta)
2970 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07002971 }
Johannes Bergd0709a62008-02-25 16:27:46 +01002972 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02002973
Johannes Bergd0709a62008-02-25 16:27:46 +01002974 synchronize_rcu();
2975
2976 rtnl_lock();
2977 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
2978 sta_info_destroy(sta);
2979 rtnl_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002980}
2981
2982
2983static void ieee80211_sta_merge_ibss(struct net_device *dev,
2984 struct ieee80211_if_sta *ifsta)
2985{
2986 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2987
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002988 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07002989 if (ieee80211_sta_active_ibss(dev))
2990 return;
2991
2992 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
2993 "IBSS networks with same SSID (merge)\n", dev->name);
2994 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
2995}
2996
2997
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002998#ifdef CONFIG_MAC80211_MESH
2999static void ieee80211_mesh_housekeeping(struct net_device *dev,
3000 struct ieee80211_if_sta *ifsta)
3001{
3002 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3003 bool free_plinks;
3004
3005 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3006 mesh_path_expire(dev);
3007
3008 free_plinks = mesh_plink_availables(sdata);
3009 if (free_plinks != sdata->u.sta.accepting_plinks)
3010 ieee80211_if_config_beacon(dev);
3011
3012 mod_timer(&ifsta->timer, jiffies +
3013 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3014}
3015
3016
3017void ieee80211_start_mesh(struct net_device *dev)
3018{
3019 struct ieee80211_if_sta *ifsta;
3020 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3021 ifsta = &sdata->u.sta;
3022 ifsta->state = IEEE80211_MESH_UP;
3023 ieee80211_sta_timer((unsigned long)sdata);
3024}
3025#endif
3026
3027
Jiri Bencf0706e82007-05-05 11:45:53 -07003028void ieee80211_sta_timer(unsigned long data)
3029{
3030 struct ieee80211_sub_if_data *sdata =
3031 (struct ieee80211_sub_if_data *) data;
3032 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3033 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3034
3035 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3036 queue_work(local->hw.workqueue, &ifsta->work);
3037}
3038
Jiri Bencf0706e82007-05-05 11:45:53 -07003039void ieee80211_sta_work(struct work_struct *work)
3040{
3041 struct ieee80211_sub_if_data *sdata =
3042 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3043 struct net_device *dev = sdata->dev;
3044 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3045 struct ieee80211_if_sta *ifsta;
3046 struct sk_buff *skb;
3047
3048 if (!netif_running(dev))
3049 return;
3050
Zhu Yiece8edd2007-11-22 10:53:21 +08003051 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003052 return;
3053
Johannes Berg51fb61e2007-12-19 01:31:27 +01003054 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003055 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3056 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003057 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01003058 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e82007-05-05 11:45:53 -07003059 return;
3060 }
3061 ifsta = &sdata->u.sta;
3062
3063 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3064 ieee80211_sta_rx_queued_mgmt(dev, skb);
3065
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003066#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003067 if (ifsta->preq_queue_len &&
3068 time_after(jiffies,
3069 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003070 mesh_path_start_discovery(dev);
3071#endif
3072
Jiri Bencf0706e82007-05-05 11:45:53 -07003073 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3074 ifsta->state != IEEE80211_ASSOCIATE &&
3075 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003076 if (ifsta->scan_ssid_len)
3077 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3078 else
3079 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003080 return;
3081 }
3082
3083 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3084 if (ieee80211_sta_config_auth(dev, ifsta))
3085 return;
3086 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3087 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3088 return;
3089
3090 switch (ifsta->state) {
3091 case IEEE80211_DISABLED:
3092 break;
3093 case IEEE80211_AUTHENTICATE:
3094 ieee80211_authenticate(dev, ifsta);
3095 break;
3096 case IEEE80211_ASSOCIATE:
3097 ieee80211_associate(dev, ifsta);
3098 break;
3099 case IEEE80211_ASSOCIATED:
3100 ieee80211_associated(dev, ifsta);
3101 break;
3102 case IEEE80211_IBSS_SEARCH:
3103 ieee80211_sta_find_ibss(dev, ifsta);
3104 break;
3105 case IEEE80211_IBSS_JOINED:
3106 ieee80211_sta_merge_ibss(dev, ifsta);
3107 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003108#ifdef CONFIG_MAC80211_MESH
3109 case IEEE80211_MESH_UP:
3110 ieee80211_mesh_housekeeping(dev, ifsta);
3111 break;
3112#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003113 default:
3114 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3115 ifsta->state);
3116 break;
3117 }
3118
3119 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3120 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3121 "mixed-cell disabled - disassociate\n", dev->name);
3122
3123 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3124 ieee80211_set_disassoc(dev, ifsta, 0);
3125 }
3126}
3127
3128
3129static void ieee80211_sta_reset_auth(struct net_device *dev,
3130 struct ieee80211_if_sta *ifsta)
3131{
3132 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3133
3134 if (local->ops->reset_tsf) {
3135 /* Reset own TSF to allow time synchronization work. */
3136 local->ops->reset_tsf(local_to_hw(local));
3137 }
3138
3139 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3140
3141
3142 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3143 ifsta->auth_alg = WLAN_AUTH_OPEN;
3144 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3145 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3146 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3147 ifsta->auth_alg = WLAN_AUTH_LEAP;
3148 else
3149 ifsta->auth_alg = WLAN_AUTH_OPEN;
3150 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3151 ifsta->auth_alg);
3152 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003153 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3154 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003155 netif_carrier_off(dev);
3156}
3157
3158
3159void ieee80211_sta_req_auth(struct net_device *dev,
3160 struct ieee80211_if_sta *ifsta)
3161{
3162 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3163 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3164
Johannes Berg51fb61e2007-12-19 01:31:27 +01003165 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003166 return;
3167
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003168 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3169 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3170 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3171 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003172 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3173 queue_work(local->hw.workqueue, &ifsta->work);
3174 }
3175}
3176
3177static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3178 const char *ssid, int ssid_len)
3179{
3180 int tmp, hidden_ssid;
3181
Michael Wu48225702007-10-19 17:14:36 -04003182 if (ssid_len == ifsta->ssid_len &&
3183 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003184 return 1;
3185
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003186 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003187 return 0;
3188
3189 hidden_ssid = 1;
3190 tmp = ssid_len;
3191 while (tmp--) {
3192 if (ssid[tmp] != '\0') {
3193 hidden_ssid = 0;
3194 break;
3195 }
3196 }
3197
3198 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3199 return 1;
3200
3201 if (ssid_len == 1 && ssid[0] == ' ')
3202 return 1;
3203
3204 return 0;
3205}
3206
3207static int ieee80211_sta_config_auth(struct net_device *dev,
3208 struct ieee80211_if_sta *ifsta)
3209{
3210 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3211 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3212 struct ieee80211_sta_bss *bss, *selected = NULL;
3213 int top_rssi = 0, freq;
3214
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003215 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3216 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003217 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003218 ieee80211_sta_reset_auth(dev, ifsta);
3219 return 0;
3220 }
3221
3222 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003223 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003224 list_for_each_entry(bss, &local->sta_bss_list, list) {
3225 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3226 continue;
3227
3228 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3229 !!sdata->default_key)
3230 continue;
3231
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003232 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3233 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003234 continue;
3235
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003236 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003237 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3238 continue;
3239
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003240 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003241 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3242 continue;
3243
3244 if (!selected || top_rssi < bss->rssi) {
3245 selected = bss;
3246 top_rssi = bss->rssi;
3247 }
3248 }
3249 if (selected)
3250 atomic_inc(&selected->users);
3251 spin_unlock_bh(&local->sta_bss_lock);
3252
3253 if (selected) {
Johannes Berg8318d782008-01-24 19:38:38 +01003254 ieee80211_set_freq(local, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003255 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003256 ieee80211_sta_set_ssid(dev, selected->ssid,
3257 selected->ssid_len);
3258 ieee80211_sta_set_bssid(dev, selected->bssid);
3259 ieee80211_rx_bss_put(dev, selected);
3260 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003261 ieee80211_sta_reset_auth(dev, ifsta);
3262 return 0;
3263 } else {
3264 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003265 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003266 ieee80211_sta_start_scan(dev, NULL, 0);
3267 else
3268 ieee80211_sta_start_scan(dev, ifsta->ssid,
3269 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003270 ifsta->state = IEEE80211_AUTHENTICATE;
3271 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3272 } else
3273 ifsta->state = IEEE80211_DISABLED;
3274 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003275 return -1;
3276}
3277
Jiri Bencf0706e82007-05-05 11:45:53 -07003278
3279static int ieee80211_sta_create_ibss(struct net_device *dev,
3280 struct ieee80211_if_sta *ifsta)
3281{
3282 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3283 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003284 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003285 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003286 u8 bssid[ETH_ALEN], *pos;
3287 int i;
Joe Perches0795af52007-10-03 17:59:30 -07003288 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003289
3290#if 0
3291 /* Easier testing, use fixed BSSID. */
3292 memset(bssid, 0xfe, ETH_ALEN);
3293#else
3294 /* Generate random, not broadcast, locally administered BSSID. Mix in
3295 * own MAC address to make sure that devices that do not have proper
3296 * random number generator get different BSSID. */
3297 get_random_bytes(bssid, ETH_ALEN);
3298 for (i = 0; i < ETH_ALEN; i++)
3299 bssid[i] ^= dev->dev_addr[i];
3300 bssid[0] &= ~0x01;
3301 bssid[0] |= 0x02;
3302#endif
3303
Joe Perches0795af52007-10-03 17:59:30 -07003304 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3305 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003306
Johannes Berg8318d782008-01-24 19:38:38 +01003307 bss = ieee80211_rx_bss_add(dev, bssid,
3308 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003309 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003310 if (!bss)
3311 return -ENOMEM;
3312
Johannes Berg8318d782008-01-24 19:38:38 +01003313 bss->band = local->hw.conf.channel->band;
3314 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003315
3316 if (local->hw.conf.beacon_int == 0)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003317 local->hw.conf.beacon_int = 10000;
Jiri Bencf0706e82007-05-05 11:45:53 -07003318 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003319 bss->last_update = jiffies;
3320 bss->capability = WLAN_CAPABILITY_IBSS;
3321 if (sdata->default_key) {
3322 bss->capability |= WLAN_CAPABILITY_PRIVACY;
3323 } else
3324 sdata->drop_unencrypted = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003325 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003326 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003327 for (i = 0; i < sband->n_bitrates; i++) {
3328 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003329 *pos++ = (u8) (rate / 5);
3330 }
3331
3332 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3333}
3334
3335
3336static int ieee80211_sta_find_ibss(struct net_device *dev,
3337 struct ieee80211_if_sta *ifsta)
3338{
3339 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3340 struct ieee80211_sta_bss *bss;
3341 int found = 0;
3342 u8 bssid[ETH_ALEN];
3343 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003344 DECLARE_MAC_BUF(mac);
3345 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003346
3347 if (ifsta->ssid_len == 0)
3348 return -EINVAL;
3349
3350 active_ibss = ieee80211_sta_active_ibss(dev);
3351#ifdef CONFIG_MAC80211_IBSS_DEBUG
3352 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3353 dev->name, active_ibss);
3354#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3355 spin_lock_bh(&local->sta_bss_lock);
3356 list_for_each_entry(bss, &local->sta_bss_list, list) {
3357 if (ifsta->ssid_len != bss->ssid_len ||
3358 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3359 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3360 continue;
3361#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003362 printk(KERN_DEBUG " bssid=%s found\n",
3363 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003364#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3365 memcpy(bssid, bss->bssid, ETH_ALEN);
3366 found = 1;
3367 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3368 break;
3369 }
3370 spin_unlock_bh(&local->sta_bss_lock);
3371
3372#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003373 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3374 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003375#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3376 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003377 (bss = ieee80211_rx_bss_get(dev, bssid,
3378 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003379 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07003380 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003381 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003382 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003383 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3384 }
3385#ifdef CONFIG_MAC80211_IBSS_DEBUG
3386 printk(KERN_DEBUG " did not try to join ibss\n");
3387#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3388
3389 /* Selected IBSS not found in current scan results - try to scan */
3390 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3391 !ieee80211_sta_active_ibss(dev)) {
3392 mod_timer(&ifsta->timer, jiffies +
3393 IEEE80211_IBSS_MERGE_INTERVAL);
3394 } else if (time_after(jiffies, local->last_scan_completed +
3395 IEEE80211_SCAN_INTERVAL)) {
3396 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3397 "join\n", dev->name);
3398 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3399 ifsta->ssid_len);
3400 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3401 int interval = IEEE80211_SCAN_INTERVAL;
3402
3403 if (time_after(jiffies, ifsta->ibss_join_req +
3404 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003405 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003406 (!(local->oper_channel->flags &
3407 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003408 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003409 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003410 printk(KERN_DEBUG "%s: IBSS not allowed on"
3411 " %d MHz\n", dev->name,
3412 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003413 }
3414
3415 /* No IBSS found - decrease scan interval and continue
3416 * scanning. */
3417 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3418 }
3419
3420 ifsta->state = IEEE80211_IBSS_SEARCH;
3421 mod_timer(&ifsta->timer, jiffies + interval);
3422 return 0;
3423 }
3424
3425 return 0;
3426}
3427
3428
3429int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3430{
Johannes Berg8318d782008-01-24 19:38:38 +01003431 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003432 struct ieee80211_if_sta *ifsta;
3433 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3434
3435 if (len > IEEE80211_MAX_SSID_LEN)
3436 return -EINVAL;
3437
3438 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
3439 * not defined. */
3440 if (local->ops->conf_tx) {
3441 struct ieee80211_tx_queue_params qparam;
3442 int i;
3443
3444 memset(&qparam, 0, sizeof(qparam));
Johannes Berg8318d782008-01-24 19:38:38 +01003445
Jiri Bencf0706e82007-05-05 11:45:53 -07003446 qparam.aifs = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01003447
3448 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
3449 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
3450 qparam.cw_min = 31;
3451 else
3452 qparam.cw_min = 15;
3453
Jiri Bencf0706e82007-05-05 11:45:53 -07003454 qparam.cw_max = 1023;
Johannes Berg3330d7b2008-02-10 16:49:38 +01003455 qparam.txop = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003456
Jiri Bencf0706e82007-05-05 11:45:53 -07003457 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
Jiri Bencf0706e82007-05-05 11:45:53 -07003458 local->ops->conf_tx(local_to_hw(local),
3459 i + IEEE80211_TX_QUEUE_DATA0,
3460 &qparam);
Johannes Berg8318d782008-01-24 19:38:38 +01003461
Jiri Bencf0706e82007-05-05 11:45:53 -07003462 /* IBSS uses different parameters for Beacon sending */
3463 qparam.cw_min++;
3464 qparam.cw_min *= 2;
3465 qparam.cw_min--;
3466 local->ops->conf_tx(local_to_hw(local),
3467 IEEE80211_TX_QUEUE_BEACON, &qparam);
3468 }
3469
Jiri Bencf0706e82007-05-05 11:45:53 -07003470 ifsta = &sdata->u.sta;
3471
3472 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003473 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003474 memcpy(ifsta->ssid, ssid, len);
3475 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3476 ifsta->ssid_len = len;
3477
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003478 if (len)
3479 ifsta->flags |= IEEE80211_STA_SSID_SET;
3480 else
3481 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003482 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003483 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003484 ifsta->ibss_join_req = jiffies;
3485 ifsta->state = IEEE80211_IBSS_SEARCH;
3486 return ieee80211_sta_find_ibss(dev, ifsta);
3487 }
3488 return 0;
3489}
3490
3491
3492int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3493{
3494 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3495 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3496 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3497 *len = ifsta->ssid_len;
3498 return 0;
3499}
3500
3501
3502int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3503{
3504 struct ieee80211_sub_if_data *sdata;
3505 struct ieee80211_if_sta *ifsta;
3506 int res;
3507
3508 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3509 ifsta = &sdata->u.sta;
3510
3511 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3512 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3513 res = ieee80211_if_config(dev);
3514 if (res) {
3515 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3516 "the low-level driver\n", dev->name);
3517 return res;
3518 }
3519 }
3520
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003521 if (is_valid_ether_addr(bssid))
3522 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003523 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003524 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3525
Jiri Bencf0706e82007-05-05 11:45:53 -07003526 return 0;
3527}
3528
3529
3530static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3531 struct ieee80211_sub_if_data *sdata,
3532 int powersave)
3533{
3534 struct sk_buff *skb;
3535 struct ieee80211_hdr *nullfunc;
3536 u16 fc;
3537
3538 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3539 if (!skb) {
3540 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3541 "frame\n", sdata->dev->name);
3542 return;
3543 }
3544 skb_reserve(skb, local->hw.extra_tx_headroom);
3545
3546 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3547 memset(nullfunc, 0, 24);
3548 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3549 IEEE80211_FCTL_TODS;
3550 if (powersave)
3551 fc |= IEEE80211_FCTL_PM;
3552 nullfunc->frame_control = cpu_to_le16(fc);
3553 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3554 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3555 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3556
3557 ieee80211_sta_tx(sdata->dev, skb, 0);
3558}
3559
3560
3561void ieee80211_scan_completed(struct ieee80211_hw *hw)
3562{
3563 struct ieee80211_local *local = hw_to_local(hw);
3564 struct net_device *dev = local->scan_dev;
3565 struct ieee80211_sub_if_data *sdata;
3566 union iwreq_data wrqu;
3567
3568 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003569 memset(&wrqu, 0, sizeof(wrqu));
3570 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003571
Zhu Yiece8edd2007-11-22 10:53:21 +08003572 if (local->sta_hw_scanning) {
3573 local->sta_hw_scanning = 0;
3574 goto done;
3575 }
3576
3577 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003578 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003579 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003580 "channel after scan\n", dev->name);
3581
Johannes Berg4150c572007-09-17 01:29:23 -04003582
3583 netif_tx_lock_bh(local->mdev);
3584 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3585 local->ops->configure_filter(local_to_hw(local),
3586 FIF_BCN_PRBRESP_PROMISC,
3587 &local->filter_flags,
3588 local->mdev->mc_count,
3589 local->mdev->mc_list);
3590
3591 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003592
Johannes Berg79010422007-09-18 17:29:21 -04003593 rcu_read_lock();
3594 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003595
3596 /* No need to wake the master device. */
3597 if (sdata->dev == local->mdev)
3598 continue;
3599
Johannes Berg51fb61e2007-12-19 01:31:27 +01003600 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003601 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07003602 ieee80211_send_nullfunc(local, sdata, 0);
3603 ieee80211_sta_timer((unsigned long)sdata);
3604 }
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003605
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003606 if (sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT)
3607 ieee80211_sta_timer((unsigned long)sdata);
3608
Jiri Bencf0706e82007-05-05 11:45:53 -07003609 netif_wake_queue(sdata->dev);
3610 }
Johannes Berg79010422007-09-18 17:29:21 -04003611 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003612
Zhu Yiece8edd2007-11-22 10:53:21 +08003613done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003614 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003615 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003616 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003617 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003618 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3619 !ieee80211_sta_active_ibss(dev)))
3620 ieee80211_sta_find_ibss(dev, ifsta);
3621 }
3622}
3623EXPORT_SYMBOL(ieee80211_scan_completed);
3624
3625void ieee80211_sta_scan_work(struct work_struct *work)
3626{
3627 struct ieee80211_local *local =
3628 container_of(work, struct ieee80211_local, scan_work.work);
3629 struct net_device *dev = local->scan_dev;
3630 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003631 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003632 struct ieee80211_channel *chan;
3633 int skip;
3634 unsigned long next_delay = 0;
3635
Zhu Yiece8edd2007-11-22 10:53:21 +08003636 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003637 return;
3638
3639 switch (local->scan_state) {
3640 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003641 /*
3642 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3643 * after we successfully scanned the last channel of the last
3644 * band (and the last band is supported by the hw)
3645 */
Johannes Berg8318d782008-01-24 19:38:38 +01003646 if (local->scan_band < IEEE80211_NUM_BANDS)
3647 sband = local->hw.wiphy->bands[local->scan_band];
3648 else
3649 sband = NULL;
3650
Johannes Berg69d464d2008-01-28 17:08:56 +01003651 /*
3652 * If we are at an unsupported band and have more bands
3653 * left to scan, advance to the next supported one.
3654 */
3655 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003656 local->scan_band++;
3657 sband = local->hw.wiphy->bands[local->scan_band];
3658 local->scan_channel_idx = 0;
3659 }
3660
Johannes Berg69d464d2008-01-28 17:08:56 +01003661 /* if no more bands/channels left, complete scan */
3662 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003663 ieee80211_scan_completed(local_to_hw(local));
3664 return;
3665 }
Johannes Berg8318d782008-01-24 19:38:38 +01003666 skip = 0;
3667 chan = &sband->channels[local->scan_channel_idx];
3668
3669 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003670 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003671 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003672 skip = 1;
3673
3674 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003675 local->scan_channel = chan;
3676 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003677 printk(KERN_DEBUG "%s: failed to set freq to "
3678 "%d MHz for scan\n", dev->name,
3679 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003680 skip = 1;
3681 }
3682 }
3683
Johannes Berg69d464d2008-01-28 17:08:56 +01003684 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003685 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003686 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003687 /*
3688 * scan_band may end up == IEEE80211_NUM_BANDS, but
3689 * we'll catch that case above and complete the scan
3690 * if that is the case.
3691 */
Johannes Berg8318d782008-01-24 19:38:38 +01003692 local->scan_band++;
3693 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003694 }
3695
3696 if (skip)
3697 break;
3698
3699 next_delay = IEEE80211_PROBE_DELAY +
3700 usecs_to_jiffies(local->hw.channel_change_time);
3701 local->scan_state = SCAN_SEND_PROBE;
3702 break;
3703 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003704 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003705 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003706
3707 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3708 break;
3709 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3710 local->scan_ssid_len);
3711 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003712 break;
3713 }
3714
Zhu Yiece8edd2007-11-22 10:53:21 +08003715 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003716 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3717 next_delay);
3718}
3719
3720
3721static int ieee80211_sta_start_scan(struct net_device *dev,
3722 u8 *ssid, size_t ssid_len)
3723{
3724 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3725 struct ieee80211_sub_if_data *sdata;
3726
3727 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3728 return -EINVAL;
3729
3730 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3731 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3732 * BSSID: MACAddress
3733 * SSID
3734 * ScanType: ACTIVE, PASSIVE
3735 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3736 * a Probe frame during active scanning
3737 * ChannelList
3738 * MinChannelTime (>= ProbeDelay), in TU
3739 * MaxChannelTime: (>= MinChannelTime), in TU
3740 */
3741
3742 /* MLME-SCAN.confirm
3743 * BSSDescriptionSet
3744 * ResultCode: SUCCESS, INVALID_PARAMETERS
3745 */
3746
Zhu Yiece8edd2007-11-22 10:53:21 +08003747 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003748 if (local->scan_dev == dev)
3749 return 0;
3750 return -EBUSY;
3751 }
3752
3753 if (local->ops->hw_scan) {
3754 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003755 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003756 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003757 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003758 local->scan_dev = dev;
3759 }
3760 return rc;
3761 }
3762
Zhu Yiece8edd2007-11-22 10:53:21 +08003763 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003764
Johannes Berg79010422007-09-18 17:29:21 -04003765 rcu_read_lock();
3766 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003767
3768 /* Don't stop the master interface, otherwise we can't transmit
3769 * probes! */
3770 if (sdata->dev == local->mdev)
3771 continue;
3772
Jiri Bencf0706e82007-05-05 11:45:53 -07003773 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003774 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003775 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003776 ieee80211_send_nullfunc(local, sdata, 1);
3777 }
Johannes Berg79010422007-09-18 17:29:21 -04003778 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003779
3780 if (ssid) {
3781 local->scan_ssid_len = ssid_len;
3782 memcpy(local->scan_ssid, ssid, ssid_len);
3783 } else
3784 local->scan_ssid_len = 0;
3785 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07003786 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003787 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07003788 local->scan_dev = dev;
3789
Johannes Berg4150c572007-09-17 01:29:23 -04003790 netif_tx_lock_bh(local->mdev);
3791 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3792 local->ops->configure_filter(local_to_hw(local),
3793 FIF_BCN_PRBRESP_PROMISC,
3794 &local->filter_flags,
3795 local->mdev->mc_count,
3796 local->mdev->mc_list);
3797 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003798
3799 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3800 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3801 IEEE80211_CHANNEL_TIME);
3802
3803 return 0;
3804}
3805
3806
3807int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3808{
3809 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3810 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3811 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3812
Johannes Berg51fb61e2007-12-19 01:31:27 +01003813 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003814 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3815
Zhu Yiece8edd2007-11-22 10:53:21 +08003816 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003817 if (local->scan_dev == dev)
3818 return 0;
3819 return -EBUSY;
3820 }
3821
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003822 ifsta->scan_ssid_len = ssid_len;
3823 if (ssid_len)
3824 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003825 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3826 queue_work(local->hw.workqueue, &ifsta->work);
3827 return 0;
3828}
3829
3830static char *
3831ieee80211_sta_scan_result(struct net_device *dev,
3832 struct ieee80211_sta_bss *bss,
3833 char *current_ev, char *end_buf)
3834{
3835 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3836 struct iw_event iwe;
3837
3838 if (time_after(jiffies,
3839 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
3840 return current_ev;
3841
Jiri Bencf0706e82007-05-05 11:45:53 -07003842 memset(&iwe, 0, sizeof(iwe));
3843 iwe.cmd = SIOCGIWAP;
3844 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
3845 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
3846 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3847 IW_EV_ADDR_LEN);
3848
3849 memset(&iwe, 0, sizeof(iwe));
3850 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01003851 if (bss_mesh_cfg(bss)) {
3852 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003853 iwe.u.data.flags = 1;
3854 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
Johannes Berg902acc72008-02-23 15:17:19 +01003855 bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003856 } else {
3857 iwe.u.data.length = bss->ssid_len;
3858 iwe.u.data.flags = 1;
3859 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3860 bss->ssid);
3861 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003862
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003863 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS
Johannes Berg902acc72008-02-23 15:17:19 +01003864 || bss_mesh_cfg(bss))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003865 memset(&iwe, 0, sizeof(iwe));
3866 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01003867 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003868 iwe.u.mode = IW_MODE_MESH;
3869 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07003870 iwe.u.mode = IW_MODE_MASTER;
3871 else
3872 iwe.u.mode = IW_MODE_ADHOC;
3873 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3874 IW_EV_UINT_LEN);
3875 }
3876
3877 memset(&iwe, 0, sizeof(iwe));
3878 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01003879 iwe.u.freq.m = bss->freq;
3880 iwe.u.freq.e = 6;
Jiri Bencf0706e82007-05-05 11:45:53 -07003881 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3882 IW_EV_FREQ_LEN);
Johannes Berg8318d782008-01-24 19:38:38 +01003883
3884 memset(&iwe, 0, sizeof(iwe));
3885 iwe.cmd = SIOCGIWFREQ;
3886 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
3887 iwe.u.freq.e = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003888 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3889 IW_EV_FREQ_LEN);
3890
3891 memset(&iwe, 0, sizeof(iwe));
3892 iwe.cmd = IWEVQUAL;
3893 iwe.u.qual.qual = bss->signal;
3894 iwe.u.qual.level = bss->rssi;
3895 iwe.u.qual.noise = bss->noise;
3896 iwe.u.qual.updated = local->wstats_flags;
3897 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
3898 IW_EV_QUAL_LEN);
3899
3900 memset(&iwe, 0, sizeof(iwe));
3901 iwe.cmd = SIOCGIWENCODE;
3902 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
3903 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
3904 else
3905 iwe.u.data.flags = IW_ENCODE_DISABLED;
3906 iwe.u.data.length = 0;
3907 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
3908
3909 if (bss && bss->wpa_ie) {
3910 memset(&iwe, 0, sizeof(iwe));
3911 iwe.cmd = IWEVGENIE;
3912 iwe.u.data.length = bss->wpa_ie_len;
3913 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3914 bss->wpa_ie);
3915 }
3916
3917 if (bss && bss->rsn_ie) {
3918 memset(&iwe, 0, sizeof(iwe));
3919 iwe.cmd = IWEVGENIE;
3920 iwe.u.data.length = bss->rsn_ie_len;
3921 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3922 bss->rsn_ie);
3923 }
3924
3925 if (bss && bss->supp_rates_len > 0) {
3926 /* display all supported rates in readable format */
3927 char *p = current_ev + IW_EV_LCP_LEN;
3928 int i;
3929
3930 memset(&iwe, 0, sizeof(iwe));
3931 iwe.cmd = SIOCGIWRATE;
3932 /* Those two flags are ignored... */
3933 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
3934
3935 for (i = 0; i < bss->supp_rates_len; i++) {
3936 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3937 0x7f) * 500000);
3938 p = iwe_stream_add_value(current_ev, p,
3939 end_buf, &iwe, IW_EV_PARAM_LEN);
3940 }
3941 current_ev = p;
3942 }
3943
3944 if (bss) {
3945 char *buf;
3946 buf = kmalloc(30, GFP_ATOMIC);
3947 if (buf) {
3948 memset(&iwe, 0, sizeof(iwe));
3949 iwe.cmd = IWEVCUSTOM;
3950 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3951 iwe.u.data.length = strlen(buf);
3952 current_ev = iwe_stream_add_point(current_ev, end_buf,
3953 &iwe, buf);
3954 kfree(buf);
3955 }
3956 }
3957
Johannes Berg902acc72008-02-23 15:17:19 +01003958 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003959 char *buf;
Johannes Berg902acc72008-02-23 15:17:19 +01003960 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003961 buf = kmalloc(200, GFP_ATOMIC);
3962 if (buf) {
3963 memset(&iwe, 0, sizeof(iwe));
3964 iwe.cmd = IWEVCUSTOM;
3965 sprintf(buf, "Mesh network (version %d)\n"
3966 "\t\t\tPath Selection Protocol ID: 0x%02X%02X%02X%02X\n"
3967 "\t\t\tPath Selection Metric ID: 0x%02X%02X%02X%02X\n"
3968 "\t\t\tCongestion Control Mode ID: 0x%02X%02X%02X%02X\n"
3969 "\t\t\tChannel Precedence: 0x%02X%02X%02X%02X",
3970 cfg[0], cfg[1], cfg[2], cfg[3], cfg[4], cfg[5], cfg[6],
3971 cfg[7], cfg[8], cfg[9], cfg[10], cfg[11], cfg[12],
3972 cfg[13], cfg[14], cfg[15], cfg[16]);
3973 iwe.u.data.length = strlen(buf);
3974 current_ev = iwe_stream_add_point(current_ev, end_buf,
3975 &iwe, buf);
3976 kfree(buf);
3977 }
3978 }
3979
Jiri Bencf0706e82007-05-05 11:45:53 -07003980 return current_ev;
3981}
3982
3983
3984int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3985{
3986 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3987 char *current_ev = buf;
3988 char *end_buf = buf + len;
3989 struct ieee80211_sta_bss *bss;
3990
3991 spin_lock_bh(&local->sta_bss_lock);
3992 list_for_each_entry(bss, &local->sta_bss_list, list) {
3993 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3994 spin_unlock_bh(&local->sta_bss_lock);
3995 return -E2BIG;
3996 }
3997 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3998 end_buf);
3999 }
4000 spin_unlock_bh(&local->sta_bss_lock);
4001 return current_ev - buf;
4002}
4003
4004
4005int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4006{
4007 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4008 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4009 kfree(ifsta->extra_ie);
4010 if (len == 0) {
4011 ifsta->extra_ie = NULL;
4012 ifsta->extra_ie_len = 0;
4013 return 0;
4014 }
4015 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4016 if (!ifsta->extra_ie) {
4017 ifsta->extra_ie_len = 0;
4018 return -ENOMEM;
4019 }
4020 memcpy(ifsta->extra_ie, ie, len);
4021 ifsta->extra_ie_len = len;
4022 return 0;
4023}
4024
4025
Johannes Bergd0709a62008-02-25 16:27:46 +01004026/* must be called under RCU read lock */
Jiri Bencf0706e82007-05-05 11:45:53 -07004027struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
4028 struct sk_buff *skb, u8 *bssid,
4029 u8 *addr)
4030{
4031 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4032 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004033 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004034 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07004035
4036 /* TODO: Could consider removing the least recently used entry and
4037 * allow new one to be added. */
4038 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4039 if (net_ratelimit()) {
4040 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004041 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004042 }
4043 return NULL;
4044 }
4045
Joe Perches0795af52007-10-03 17:59:30 -07004046 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004047 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07004048
Johannes Bergd0709a62008-02-25 16:27:46 +01004049 sta = sta_info_add(sdata, addr);
Johannes Berg43ba7e92008-02-21 14:09:30 +01004050 if (IS_ERR(sta))
Jiri Bencf0706e82007-05-05 11:45:53 -07004051 return NULL;
4052
Johannes Berg238814f2008-01-28 17:19:37 +01004053 sta->flags |= WLAN_STA_AUTHORIZED;
4054
Johannes Berg8318d782008-01-24 19:38:38 +01004055 sta->supp_rates[local->hw.conf.channel->band] =
4056 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004057
4058 rate_control_rate_init(sta, local);
4059
Johannes Bergd0709a62008-02-25 16:27:46 +01004060 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004061}
4062
4063
4064int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4065{
4066 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4067 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4068
4069 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4070 dev->name, reason);
4071
Johannes Berg51fb61e2007-12-19 01:31:27 +01004072 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4073 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004074 return -EINVAL;
4075
4076 ieee80211_send_deauth(dev, ifsta, reason);
4077 ieee80211_set_disassoc(dev, ifsta, 1);
4078 return 0;
4079}
4080
4081
4082int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4083{
4084 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4085 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4086
4087 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4088 dev->name, reason);
4089
Johannes Berg51fb61e2007-12-19 01:31:27 +01004090 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004091 return -EINVAL;
4092
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004093 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004094 return -1;
4095
4096 ieee80211_send_disassoc(dev, ifsta, reason);
4097 ieee80211_set_disassoc(dev, ifsta, 0);
4098 return 0;
4099}