blob: dda30e98aa1e6d0af837be7f339d05f481190220 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
Jiri Bencf0706e82007-05-05 11:45:53 -070015 * order BSS list by RSSI(?) ("quality of AP")
16 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
17 * SSID)
18 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070019#include <linux/delay.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include <linux/if_ether.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_arp.h>
24#include <linux/wireless.h>
25#include <linux/random.h>
26#include <linux/etherdevice.h>
Johannes Bergd0709a62008-02-25 16:27:46 +010027#include <linux/rtnetlink.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040033#include "rate.h"
34#include "led.h"
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010035#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
37#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
38#define IEEE80211_AUTH_MAX_TRIES 3
39#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
40#define IEEE80211_ASSOC_MAX_TRIES 3
41#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +010042#define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
Jiri Bencf0706e82007-05-05 11:45:53 -070043#define IEEE80211_PROBE_INTERVAL (60 * HZ)
44#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
45#define IEEE80211_SCAN_INTERVAL (2 * HZ)
46#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
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
Jiri Bencf0706e82007-05-05 11:45:53 -070060#define ERP_INFO_USE_PROTECTION BIT(1)
61
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020062/* mgmt header + 1 byte action code */
63#define IEEE80211_MIN_ACTION_SIZE (24 + 1)
64
65#define IEEE80211_ADDBA_PARAM_POLICY_MASK 0x0002
66#define IEEE80211_ADDBA_PARAM_TID_MASK 0x003C
67#define IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK 0xFFA0
Ron Rindjunsky688b88a2007-12-25 17:00:37 +020068#define IEEE80211_DELBA_PARAM_TID_MASK 0xF000
69#define IEEE80211_DELBA_PARAM_INITIATOR_MASK 0x0800
Ron Rindjunsky9f985b02007-11-26 16:14:32 +020070
Ron Rindjunsky07db2182007-12-25 17:00:33 +020071/* next values represent the buffer size for A-MPDU frame.
72 * According to IEEE802.11n spec size varies from 8K to 64K (in powers of 2) */
73#define IEEE80211_MIN_AMPDU_BUF 0x8
74#define IEEE80211_MAX_AMPDU_BUF 0x40
75
Jiri Bencf0706e82007-05-05 11:45:53 -070076static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
77 u8 *ssid, size_t ssid_len);
78static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +010079ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -040080 u8 *ssid, u8 ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070081static void ieee80211_rx_bss_put(struct net_device *dev,
82 struct ieee80211_sta_bss *bss);
83static int ieee80211_sta_find_ibss(struct net_device *dev,
84 struct ieee80211_if_sta *ifsta);
85static int ieee80211_sta_wep_configured(struct net_device *dev);
86static int ieee80211_sta_start_scan(struct net_device *dev,
87 u8 *ssid, size_t ssid_len);
88static int ieee80211_sta_config_auth(struct net_device *dev,
89 struct ieee80211_if_sta *ifsta);
Adrian Bunk712590d2008-04-21 11:47:51 +030090static void sta_rx_agg_session_timer_expired(unsigned long data);
Jiri Bencf0706e82007-05-05 11:45:53 -070091
92
Luis Carlos Coboee385852008-02-23 15:17:11 +010093void ieee802_11_parse_elems(u8 *start, size_t len,
94 struct ieee802_11_elems *elems)
Jiri Bencf0706e82007-05-05 11:45:53 -070095{
96 size_t left = len;
97 u8 *pos = start;
Jiri Bencf0706e82007-05-05 11:45:53 -070098
99 memset(elems, 0, sizeof(*elems));
100
101 while (left >= 2) {
102 u8 id, elen;
103
104 id = *pos++;
105 elen = *pos++;
106 left -= 2;
107
John W. Linville67a4cce2007-10-12 16:40:37 -0400108 if (elen > left)
109 return;
Jiri Bencf0706e82007-05-05 11:45:53 -0700110
111 switch (id) {
112 case WLAN_EID_SSID:
113 elems->ssid = pos;
114 elems->ssid_len = elen;
115 break;
116 case WLAN_EID_SUPP_RATES:
117 elems->supp_rates = pos;
118 elems->supp_rates_len = elen;
119 break;
120 case WLAN_EID_FH_PARAMS:
121 elems->fh_params = pos;
122 elems->fh_params_len = elen;
123 break;
124 case WLAN_EID_DS_PARAMS:
125 elems->ds_params = pos;
126 elems->ds_params_len = elen;
127 break;
128 case WLAN_EID_CF_PARAMS:
129 elems->cf_params = pos;
130 elems->cf_params_len = elen;
131 break;
132 case WLAN_EID_TIM:
133 elems->tim = pos;
134 elems->tim_len = elen;
135 break;
136 case WLAN_EID_IBSS_PARAMS:
137 elems->ibss_params = pos;
138 elems->ibss_params_len = elen;
139 break;
140 case WLAN_EID_CHALLENGE:
141 elems->challenge = pos;
142 elems->challenge_len = elen;
143 break;
144 case WLAN_EID_WPA:
145 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
146 pos[2] == 0xf2) {
147 /* Microsoft OUI (00:50:F2) */
148 if (pos[3] == 1) {
149 /* OUI Type 1 - WPA IE */
150 elems->wpa = pos;
151 elems->wpa_len = elen;
152 } else if (elen >= 5 && pos[3] == 2) {
153 if (pos[4] == 0) {
154 elems->wmm_info = pos;
155 elems->wmm_info_len = elen;
156 } else if (pos[4] == 1) {
157 elems->wmm_param = pos;
158 elems->wmm_param_len = elen;
159 }
160 }
161 }
162 break;
163 case WLAN_EID_RSN:
164 elems->rsn = pos;
165 elems->rsn_len = elen;
166 break;
167 case WLAN_EID_ERP_INFO:
168 elems->erp_info = pos;
169 elems->erp_info_len = elen;
170 break;
171 case WLAN_EID_EXT_SUPP_RATES:
172 elems->ext_supp_rates = pos;
173 elems->ext_supp_rates_len = elen;
174 break;
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200175 case WLAN_EID_HT_CAPABILITY:
176 elems->ht_cap_elem = pos;
177 elems->ht_cap_elem_len = elen;
178 break;
179 case WLAN_EID_HT_EXTRA_INFO:
180 elems->ht_info_elem = pos;
181 elems->ht_info_elem_len = elen;
182 break;
Luis Carlos Coboee385852008-02-23 15:17:11 +0100183 case WLAN_EID_MESH_ID:
184 elems->mesh_id = pos;
185 elems->mesh_id_len = elen;
186 break;
187 case WLAN_EID_MESH_CONFIG:
188 elems->mesh_config = pos;
189 elems->mesh_config_len = elen;
190 break;
191 case WLAN_EID_PEER_LINK:
192 elems->peer_link = pos;
193 elems->peer_link_len = elen;
194 break;
195 case WLAN_EID_PREQ:
196 elems->preq = pos;
197 elems->preq_len = elen;
198 break;
199 case WLAN_EID_PREP:
200 elems->prep = pos;
201 elems->prep_len = elen;
202 break;
203 case WLAN_EID_PERR:
204 elems->perr = pos;
205 elems->perr_len = elen;
206 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700207 default:
Jiri Bencf0706e82007-05-05 11:45:53 -0700208 break;
209 }
210
211 left -= elen;
212 pos += elen;
213 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700214}
215
216
Jiri Bencf0706e82007-05-05 11:45:53 -0700217static int ecw2cw(int ecw)
218{
Johannes Bergac2bf322008-02-14 00:30:35 +0100219 return (1 << ecw) - 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700220}
221
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100222
223static void ieee80211_sta_def_wmm_params(struct net_device *dev,
224 struct ieee80211_sta_bss *bss,
225 int ibss)
226{
227 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
228 struct ieee80211_local *local = sdata->local;
229 int i, have_higher_than_11mbit = 0;
230
231
232 /* cf. IEEE 802.11 9.2.12 */
233 for (i = 0; i < bss->supp_rates_len; i++)
234 if ((bss->supp_rates[i] & 0x7f) * 5 > 110)
235 have_higher_than_11mbit = 1;
236
237 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
238 have_higher_than_11mbit)
239 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
240 else
241 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
242
243
244 if (local->ops->conf_tx) {
245 struct ieee80211_tx_queue_params qparam;
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100246
247 memset(&qparam, 0, sizeof(qparam));
248
249 qparam.aifs = 2;
250
251 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
252 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE))
253 qparam.cw_min = 31;
254 else
255 qparam.cw_min = 15;
256
257 qparam.cw_max = 1023;
258 qparam.txop = 0;
259
Johannes Berge100bb62008-04-30 18:51:21 +0200260 for (i = 0; i < local_to_hw(local)->queues; i++)
261 local->ops->conf_tx(local_to_hw(local), i, &qparam);
Vladimir Koutnye2839d82008-03-18 21:14:07 +0100262 }
263}
264
Jiri Bencf0706e82007-05-05 11:45:53 -0700265static void ieee80211_sta_wmm_params(struct net_device *dev,
266 struct ieee80211_if_sta *ifsta,
267 u8 *wmm_param, size_t wmm_param_len)
268{
269 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
270 struct ieee80211_tx_queue_params params;
271 size_t left;
272 int count;
273 u8 *pos;
274
Johannes Berg3434fbd2008-05-03 00:59:37 +0200275 if (!(ifsta->flags & IEEE80211_STA_WMM_ENABLED))
276 return;
277
278 if (!wmm_param)
279 return;
280
Jiri Bencf0706e82007-05-05 11:45:53 -0700281 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
282 return;
283 count = wmm_param[6] & 0x0f;
284 if (count == ifsta->wmm_last_param_set)
285 return;
286 ifsta->wmm_last_param_set = count;
287
288 pos = wmm_param + 8;
289 left = wmm_param_len - 8;
290
291 memset(&params, 0, sizeof(params));
292
293 if (!local->ops->conf_tx)
294 return;
295
296 local->wmm_acm = 0;
297 for (; left >= 4; left -= 4, pos += 4) {
298 int aci = (pos[0] >> 5) & 0x03;
299 int acm = (pos[0] >> 4) & 0x01;
300 int queue;
301
302 switch (aci) {
303 case 1:
Johannes Berge100bb62008-04-30 18:51:21 +0200304 queue = 3;
Johannes Berg988c0f72008-04-17 19:21:22 +0200305 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700306 local->wmm_acm |= BIT(0) | BIT(3);
Jiri Bencf0706e82007-05-05 11:45:53 -0700307 break;
308 case 2:
Johannes Berge100bb62008-04-30 18:51:21 +0200309 queue = 1;
Johannes Berg988c0f72008-04-17 19:21:22 +0200310 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700311 local->wmm_acm |= BIT(4) | BIT(5);
Jiri Bencf0706e82007-05-05 11:45:53 -0700312 break;
313 case 3:
Johannes Berge100bb62008-04-30 18:51:21 +0200314 queue = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +0200315 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700316 local->wmm_acm |= BIT(6) | BIT(7);
Jiri Bencf0706e82007-05-05 11:45:53 -0700317 break;
318 case 0:
319 default:
Johannes Berge100bb62008-04-30 18:51:21 +0200320 queue = 2;
Johannes Berg988c0f72008-04-17 19:21:22 +0200321 if (acm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700322 local->wmm_acm |= BIT(1) | BIT(2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700323 break;
324 }
325
326 params.aifs = pos[0] & 0x0f;
327 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
328 params.cw_min = ecw2cw(pos[1] & 0x0f);
Johannes Berg3330d7b2008-02-10 16:49:38 +0100329 params.txop = pos[2] | (pos[3] << 8);
330#ifdef CONFIG_MAC80211_DEBUG
Jiri Bencf0706e82007-05-05 11:45:53 -0700331 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
Johannes Berg3330d7b2008-02-10 16:49:38 +0100332 "cWmin=%d cWmax=%d txop=%d\n",
Jiri Bencf0706e82007-05-05 11:45:53 -0700333 dev->name, queue, aci, acm, params.aifs, params.cw_min,
Johannes Berg3330d7b2008-02-10 16:49:38 +0100334 params.cw_max, params.txop);
335#endif
Jiri Bencf0706e82007-05-05 11:45:53 -0700336 /* TODO: handle ACM (block TX, fallback to next lowest allowed
337 * AC for now) */
338 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
339 printk(KERN_DEBUG "%s: failed to set TX queue "
340 "parameters for queue %d\n", dev->name, queue);
341 }
342 }
343}
344
John W. Linville50c4afb2008-04-15 14:09:27 -0400345static u32 ieee80211_handle_protect_preamb(struct ieee80211_sub_if_data *sdata,
346 bool use_protection,
347 bool use_short_preamble)
Daniel Drake56282212007-07-10 19:32:10 +0200348{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100349 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Daniel Drake56282212007-07-10 19:32:10 +0200350 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Joe Perches0795af52007-10-03 17:59:30 -0700351 DECLARE_MAC_BUF(mac);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100352 u32 changed = 0;
Daniel Drake56282212007-07-10 19:32:10 +0200353
Johannes Berg471b3ef2007-12-28 14:32:58 +0100354 if (use_protection != bss_conf->use_cts_prot) {
Daniel Drake56282212007-07-10 19:32:10 +0200355 if (net_ratelimit()) {
356 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700357 "%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100358 sdata->dev->name,
Daniel Drake56282212007-07-10 19:32:10 +0200359 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700360 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200361 }
Johannes Berg471b3ef2007-12-28 14:32:58 +0100362 bss_conf->use_cts_prot = use_protection;
363 changed |= BSS_CHANGED_ERP_CTS_PROT;
Daniel Drake56282212007-07-10 19:32:10 +0200364 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200365
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200366 if (use_short_preamble != bss_conf->use_short_preamble) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200367 if (net_ratelimit()) {
368 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700369 " (BSSID=%s)\n",
Johannes Berg471b3ef2007-12-28 14:32:58 +0100370 sdata->dev->name,
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200371 use_short_preamble ? "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700372 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200373 }
Vladimir Koutnyd43c7b32008-03-31 17:05:03 +0200374 bss_conf->use_short_preamble = use_short_preamble;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100375 changed |= BSS_CHANGED_ERP_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200376 }
Daniel Draked9430a32007-07-27 15:43:24 +0200377
Johannes Berg471b3ef2007-12-28 14:32:58 +0100378 return changed;
Daniel Drake56282212007-07-10 19:32:10 +0200379}
380
John W. Linville50c4afb2008-04-15 14:09:27 -0400381static u32 ieee80211_handle_erp_ie(struct ieee80211_sub_if_data *sdata,
382 u8 erp_value)
383{
384 bool use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
385 bool use_short_preamble = (erp_value & WLAN_ERP_BARKER_PREAMBLE) == 0;
386
387 return ieee80211_handle_protect_preamb(sdata,
388 use_protection, use_short_preamble);
389}
390
391static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
392 struct ieee80211_sta_bss *bss)
393{
394 u32 changed = 0;
395
396 if (bss->has_erp_value)
397 changed |= ieee80211_handle_erp_ie(sdata, bss->erp_value);
398 else {
399 u16 capab = bss->capability;
400 changed |= ieee80211_handle_protect_preamb(sdata, false,
401 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
402 }
403
404 return changed;
405}
406
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200407int ieee80211_ht_cap_ie_to_ht_info(struct ieee80211_ht_cap *ht_cap_ie,
408 struct ieee80211_ht_info *ht_info)
409{
410
411 if (ht_info == NULL)
412 return -EINVAL;
413
414 memset(ht_info, 0, sizeof(*ht_info));
415
416 if (ht_cap_ie) {
417 u8 ampdu_info = ht_cap_ie->ampdu_params_info;
418
419 ht_info->ht_supported = 1;
420 ht_info->cap = le16_to_cpu(ht_cap_ie->cap_info);
421 ht_info->ampdu_factor =
422 ampdu_info & IEEE80211_HT_CAP_AMPDU_FACTOR;
423 ht_info->ampdu_density =
424 (ampdu_info & IEEE80211_HT_CAP_AMPDU_DENSITY) >> 2;
425 memcpy(ht_info->supp_mcs_set, ht_cap_ie->supp_mcs_set, 16);
426 } else
427 ht_info->ht_supported = 0;
428
429 return 0;
430}
431
432int ieee80211_ht_addt_info_ie_to_ht_bss_info(
433 struct ieee80211_ht_addt_info *ht_add_info_ie,
434 struct ieee80211_ht_bss_info *bss_info)
435{
436 if (bss_info == NULL)
437 return -EINVAL;
438
439 memset(bss_info, 0, sizeof(*bss_info));
440
441 if (ht_add_info_ie) {
442 u16 op_mode;
443 op_mode = le16_to_cpu(ht_add_info_ie->operation_mode);
444
445 bss_info->primary_channel = ht_add_info_ie->control_chan;
446 bss_info->bss_cap = ht_add_info_ie->ht_param;
447 bss_info->bss_op_mode = (u8)(op_mode & 0xff);
448 }
449
450 return 0;
451}
Daniel Drake56282212007-07-10 19:32:10 +0200452
Jiri Bencf0706e82007-05-05 11:45:53 -0700453static void ieee80211_sta_send_associnfo(struct net_device *dev,
454 struct ieee80211_if_sta *ifsta)
455{
456 char *buf;
457 size_t len;
458 int i;
459 union iwreq_data wrqu;
460
461 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
462 return;
463
464 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200465 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700466 if (!buf)
467 return;
468
469 len = sprintf(buf, "ASSOCINFO(");
470 if (ifsta->assocreq_ies) {
471 len += sprintf(buf + len, "ReqIEs=");
472 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
473 len += sprintf(buf + len, "%02x",
474 ifsta->assocreq_ies[i]);
475 }
476 }
477 if (ifsta->assocresp_ies) {
478 if (ifsta->assocreq_ies)
479 len += sprintf(buf + len, " ");
480 len += sprintf(buf + len, "RespIEs=");
481 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
482 len += sprintf(buf + len, "%02x",
483 ifsta->assocresp_ies[i]);
484 }
485 }
486 len += sprintf(buf + len, ")");
487
488 if (len > IW_CUSTOM_MAX) {
489 len = sprintf(buf, "ASSOCRESPIE=");
490 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
491 len += sprintf(buf + len, "%02x",
492 ifsta->assocresp_ies[i]);
493 }
494 }
495
496 memset(&wrqu, 0, sizeof(wrqu));
497 wrqu.data.length = len;
498 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
499
500 kfree(buf);
501}
502
503
504static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400505 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200506 bool assoc)
Jiri Bencf0706e82007-05-05 11:45:53 -0700507{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100508 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
509 struct ieee80211_local *local = sdata->local;
Tomas Winkler38668c02008-03-28 16:33:32 -0700510 struct ieee80211_conf *conf = &local_to_hw(local)->conf;
Jiri Bencf0706e82007-05-05 11:45:53 -0700511 union iwreq_data wrqu;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100512 u32 changed = BSS_CHANGED_ASSOC;
Jiri Bencf0706e82007-05-05 11:45:53 -0700513
Jiri Bencf0706e82007-05-05 11:45:53 -0700514 if (assoc) {
Daniel Drake56282212007-07-10 19:32:10 +0200515 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400516
517 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
518
Johannes Berg51fb61e2007-12-19 01:31:27 +0100519 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -0700520 return;
Daniel Drake56282212007-07-10 19:32:10 +0200521
John W. Linville65c107a2007-10-05 14:23:27 -0400522 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Tomas Winkler38668c02008-03-28 16:33:32 -0700523 conf->channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400524 ifsta->ssid, ifsta->ssid_len);
Daniel Drake56282212007-07-10 19:32:10 +0200525 if (bss) {
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700526 /* set timing information */
527 sdata->bss_conf.beacon_int = bss->beacon_int;
528 sdata->bss_conf.timestamp = bss->timestamp;
529
John W. Linville50c4afb2008-04-15 14:09:27 -0400530 changed |= ieee80211_handle_bss_capability(sdata, bss);
Tomas Winkler21c0cbe2008-03-28 16:33:34 -0700531
Daniel Drake56282212007-07-10 19:32:10 +0200532 ieee80211_rx_bss_put(dev, bss);
533 }
534
Tomas Winkler38668c02008-03-28 16:33:32 -0700535 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
536 changed |= BSS_CHANGED_HT;
537 sdata->bss_conf.assoc_ht = 1;
538 sdata->bss_conf.ht_conf = &conf->ht_conf;
539 sdata->bss_conf.ht_bss_conf = &conf->ht_bss_conf;
540 }
541
Jiri Bencf0706e82007-05-05 11:45:53 -0700542 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400543 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -0700544 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
545 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
546 ieee80211_sta_send_associnfo(dev, ifsta);
547 } else {
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700548 ieee80211_sta_tear_down_BA_sessions(dev, ifsta->bssid);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400549 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
Jiri Bencf0706e82007-05-05 11:45:53 -0700550 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200551 ieee80211_reset_erp_info(dev);
Tomas Winkler38668c02008-03-28 16:33:32 -0700552
553 sdata->bss_conf.assoc_ht = 0;
554 sdata->bss_conf.ht_conf = NULL;
555 sdata->bss_conf.ht_bss_conf = NULL;
556
Jiri Bencf0706e82007-05-05 11:45:53 -0700557 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
558 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700559 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200560 ieee80211_led_assoc(local, assoc);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100561
Tomas Winklerb2205252008-01-27 16:18:22 +0200562 sdata->bss_conf.assoc = assoc;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100563 ieee80211_bss_info_change_notify(sdata, changed);
Reinette Chatre41a7be42008-04-03 16:08:49 -0700564 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
565 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700566}
567
568static void ieee80211_set_disassoc(struct net_device *dev,
569 struct ieee80211_if_sta *ifsta, int deauth)
570{
571 if (deauth)
572 ifsta->auth_tries = 0;
573 ifsta->assoc_tries = 0;
574 ieee80211_set_associated(dev, ifsta, 0);
575}
576
Luis Carlos Coboee385852008-02-23 15:17:11 +0100577void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
578 int encrypt)
Jiri Bencf0706e82007-05-05 11:45:53 -0700579{
580 struct ieee80211_sub_if_data *sdata;
581 struct ieee80211_tx_packet_data *pkt_data;
582
583 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
584 skb->dev = sdata->local->mdev;
585 skb_set_mac_header(skb, 0);
586 skb_set_network_header(skb, 0);
587 skb_set_transport_header(skb, 0);
588
589 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
590 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
591 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400592 if (!encrypt)
593 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e82007-05-05 11:45:53 -0700594
595 dev_queue_xmit(skb);
596}
597
598
599static void ieee80211_send_auth(struct net_device *dev,
600 struct ieee80211_if_sta *ifsta,
601 int transaction, u8 *extra, size_t extra_len,
602 int encrypt)
603{
604 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
605 struct sk_buff *skb;
606 struct ieee80211_mgmt *mgmt;
607
608 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
609 sizeof(*mgmt) + 6 + extra_len);
610 if (!skb) {
611 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
612 "frame\n", dev->name);
613 return;
614 }
615 skb_reserve(skb, local->hw.extra_tx_headroom);
616
617 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
618 memset(mgmt, 0, 24 + 6);
619 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
620 IEEE80211_STYPE_AUTH);
621 if (encrypt)
622 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
623 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
624 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
625 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
626 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
627 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
628 ifsta->auth_transaction = transaction + 1;
629 mgmt->u.auth.status_code = cpu_to_le16(0);
630 if (extra)
631 memcpy(skb_put(skb, extra_len), extra, extra_len);
632
633 ieee80211_sta_tx(dev, skb, encrypt);
634}
635
636
637static void ieee80211_authenticate(struct net_device *dev,
638 struct ieee80211_if_sta *ifsta)
639{
Joe Perches0795af52007-10-03 17:59:30 -0700640 DECLARE_MAC_BUF(mac);
641
Jiri Bencf0706e82007-05-05 11:45:53 -0700642 ifsta->auth_tries++;
643 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700644 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700645 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700646 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700647 ifsta->state = IEEE80211_DISABLED;
648 return;
649 }
650
651 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700652 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
653 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700654
655 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
656
657 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
658}
659
660
661static void ieee80211_send_assoc(struct net_device *dev,
662 struct ieee80211_if_sta *ifsta)
663{
664 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700665 struct sk_buff *skb;
666 struct ieee80211_mgmt *mgmt;
667 u8 *pos, *ies;
668 int i, len;
669 u16 capab;
670 struct ieee80211_sta_bss *bss;
671 int wmm = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100672 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700673
674 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
675 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
676 ifsta->ssid_len);
677 if (!skb) {
678 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
679 "frame\n", dev->name);
680 return;
681 }
682 skb_reserve(skb, local->hw.extra_tx_headroom);
683
Johannes Berg8318d782008-01-24 19:38:38 +0100684 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
685
Jiri Bencf0706e82007-05-05 11:45:53 -0700686 capab = ifsta->capab;
Johannes Berg8318d782008-01-24 19:38:38 +0100687
688 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
689 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
690 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
691 if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
692 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700693 }
Johannes Berg8318d782008-01-24 19:38:38 +0100694
695 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
696 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400697 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700698 if (bss) {
699 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
700 capab |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +0200701 if (bss->wmm_ie)
Jiri Bencf0706e82007-05-05 11:45:53 -0700702 wmm = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700703 ieee80211_rx_bss_put(dev, bss);
704 }
705
706 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
707 memset(mgmt, 0, 24);
708 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
709 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
710 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
711
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400712 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700713 skb_put(skb, 10);
714 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
715 IEEE80211_STYPE_REASSOC_REQ);
716 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
717 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
718 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
719 ETH_ALEN);
720 } else {
721 skb_put(skb, 4);
722 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
723 IEEE80211_STYPE_ASSOC_REQ);
724 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
725 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
726 }
727
728 /* SSID */
729 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
730 *pos++ = WLAN_EID_SSID;
731 *pos++ = ifsta->ssid_len;
732 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
733
Johannes Berg8318d782008-01-24 19:38:38 +0100734 len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -0700735 if (len > 8)
736 len = 8;
737 pos = skb_put(skb, len + 2);
738 *pos++ = WLAN_EID_SUPP_RATES;
739 *pos++ = len;
740 for (i = 0; i < len; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +0100741 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700742 *pos++ = (u8) (rate / 5);
743 }
744
Johannes Berg8318d782008-01-24 19:38:38 +0100745 if (sband->n_bitrates > len) {
746 pos = skb_put(skb, sband->n_bitrates - len + 2);
Jiri Bencf0706e82007-05-05 11:45:53 -0700747 *pos++ = WLAN_EID_EXT_SUPP_RATES;
Johannes Berg8318d782008-01-24 19:38:38 +0100748 *pos++ = sband->n_bitrates - len;
749 for (i = len; i < sband->n_bitrates; i++) {
750 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -0700751 *pos++ = (u8) (rate / 5);
752 }
753 }
754
755 if (ifsta->extra_ie) {
756 pos = skb_put(skb, ifsta->extra_ie_len);
757 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
758 }
759
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400760 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700761 pos = skb_put(skb, 9);
762 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
763 *pos++ = 7; /* len */
764 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
765 *pos++ = 0x50;
766 *pos++ = 0xf2;
767 *pos++ = 2; /* WME */
768 *pos++ = 0; /* WME info */
769 *pos++ = 1; /* WME ver */
770 *pos++ = 0;
771 }
Johannes Berg3434fbd2008-05-03 00:59:37 +0200772
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200773 /* wmm support is a must to HT */
Johannes Berg3434fbd2008-05-03 00:59:37 +0200774 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED) &&
775 sband->ht_info.ht_supported) {
Johannes Berg8318d782008-01-24 19:38:38 +0100776 __le16 tmp = cpu_to_le16(sband->ht_info.cap);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200777 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
778 *pos++ = WLAN_EID_HT_CAPABILITY;
779 *pos++ = sizeof(struct ieee80211_ht_cap);
780 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
781 memcpy(pos, &tmp, sizeof(u16));
782 pos += sizeof(u16);
Johannes Berg8318d782008-01-24 19:38:38 +0100783 /* TODO: needs a define here for << 2 */
784 *pos++ = sband->ht_info.ampdu_factor |
785 (sband->ht_info.ampdu_density << 2);
786 memcpy(pos, sband->ht_info.supp_mcs_set, 16);
Ron Rindjunskyc7153502007-11-26 16:14:31 +0200787 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700788
789 kfree(ifsta->assocreq_ies);
790 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200791 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -0700792 if (ifsta->assocreq_ies)
793 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
794
795 ieee80211_sta_tx(dev, skb, 0);
796}
797
798
799static void ieee80211_send_deauth(struct net_device *dev,
800 struct ieee80211_if_sta *ifsta, u16 reason)
801{
802 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
803 struct sk_buff *skb;
804 struct ieee80211_mgmt *mgmt;
805
806 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
807 if (!skb) {
808 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
809 "frame\n", dev->name);
810 return;
811 }
812 skb_reserve(skb, local->hw.extra_tx_headroom);
813
814 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
815 memset(mgmt, 0, 24);
816 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
817 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
818 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
819 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
820 IEEE80211_STYPE_DEAUTH);
821 skb_put(skb, 2);
822 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
823
824 ieee80211_sta_tx(dev, skb, 0);
825}
826
827
828static void ieee80211_send_disassoc(struct net_device *dev,
829 struct ieee80211_if_sta *ifsta, u16 reason)
830{
831 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
832 struct sk_buff *skb;
833 struct ieee80211_mgmt *mgmt;
834
835 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
836 if (!skb) {
837 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
838 "frame\n", dev->name);
839 return;
840 }
841 skb_reserve(skb, local->hw.extra_tx_headroom);
842
843 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
844 memset(mgmt, 0, 24);
845 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
846 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
847 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
848 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
849 IEEE80211_STYPE_DISASSOC);
850 skb_put(skb, 2);
851 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
852
853 ieee80211_sta_tx(dev, skb, 0);
854}
855
856
857static int ieee80211_privacy_mismatch(struct net_device *dev,
858 struct ieee80211_if_sta *ifsta)
859{
John W. Linville65c107a2007-10-05 14:23:27 -0400860 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -0700861 struct ieee80211_sta_bss *bss;
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000862 int bss_privacy;
863 int wep_privacy;
864 int privacy_invoked;
Jiri Bencf0706e82007-05-05 11:45:53 -0700865
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000866 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL))
Jiri Bencf0706e82007-05-05 11:45:53 -0700867 return 0;
868
Johannes Berg8318d782008-01-24 19:38:38 +0100869 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
870 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -0400871 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -0700872 if (!bss)
873 return 0;
874
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000875 bss_privacy = !!(bss->capability & WLAN_CAPABILITY_PRIVACY);
876 wep_privacy = !!ieee80211_sta_wep_configured(dev);
877 privacy_invoked = !!(ifsta->flags & IEEE80211_STA_PRIVACY_INVOKED);
Jiri Bencf0706e82007-05-05 11:45:53 -0700878
879 ieee80211_rx_bss_put(dev, bss);
880
Johannes Berg5b98b1f2007-11-03 13:11:10 +0000881 if ((bss_privacy == wep_privacy) || (bss_privacy == privacy_invoked))
882 return 0;
883
884 return 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700885}
886
887
888static void ieee80211_associate(struct net_device *dev,
889 struct ieee80211_if_sta *ifsta)
890{
Joe Perches0795af52007-10-03 17:59:30 -0700891 DECLARE_MAC_BUF(mac);
892
Jiri Bencf0706e82007-05-05 11:45:53 -0700893 ifsta->assoc_tries++;
894 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700895 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e82007-05-05 11:45:53 -0700896 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700897 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700898 ifsta->state = IEEE80211_DISABLED;
899 return;
900 }
901
902 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700903 printk(KERN_DEBUG "%s: associate with AP %s\n",
904 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700905 if (ieee80211_privacy_mismatch(dev, ifsta)) {
906 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
907 "mixed-cell disabled - abort association\n", dev->name);
908 ifsta->state = IEEE80211_DISABLED;
909 return;
910 }
911
912 ieee80211_send_assoc(dev, ifsta);
913
914 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
915}
916
917
918static void ieee80211_associated(struct net_device *dev,
919 struct ieee80211_if_sta *ifsta)
920{
921 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
922 struct sta_info *sta;
923 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700924 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700925
926 /* TODO: start monitoring current AP signal quality and number of
927 * missed beacons. Scan other channels every now and then and search
928 * for better APs. */
929 /* TODO: remove expired BSSes */
930
931 ifsta->state = IEEE80211_ASSOCIATED;
932
Johannes Bergd0709a62008-02-25 16:27:46 +0100933 rcu_read_lock();
934
Jiri Bencf0706e82007-05-05 11:45:53 -0700935 sta = sta_info_get(local, ifsta->bssid);
936 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700937 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
938 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700939 disassoc = 1;
940 } else {
941 disassoc = 0;
942 if (time_after(jiffies,
943 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400944 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700945 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700946 "current AP %s - assume out of "
Jiri Bencf0706e82007-05-05 11:45:53 -0700947 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700948 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -0700949 disassoc = 1;
Johannes Bergd0709a62008-02-25 16:27:46 +0100950 sta_info_unlink(&sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400951 } else
Jiri Bencf0706e82007-05-05 11:45:53 -0700952 ieee80211_send_probe_req(dev, ifsta->bssid,
953 local->scan_ssid,
954 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400955 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700956 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400957 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e82007-05-05 11:45:53 -0700958 if (time_after(jiffies, ifsta->last_probe +
959 IEEE80211_PROBE_INTERVAL)) {
960 ifsta->last_probe = jiffies;
961 ieee80211_send_probe_req(dev, ifsta->bssid,
962 ifsta->ssid,
963 ifsta->ssid_len);
964 }
965 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700966 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100967
968 rcu_read_unlock();
969
Johannes Berg3b967662008-04-08 17:56:52 +0200970 if (disassoc && sta)
Johannes Bergd0709a62008-02-25 16:27:46 +0100971 sta_info_destroy(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +0100972
Jiri Bencf0706e82007-05-05 11:45:53 -0700973 if (disassoc) {
Michael Wu2d192d92007-11-10 00:15:25 -0500974 ifsta->state = IEEE80211_DISABLED;
975 ieee80211_set_associated(dev, ifsta, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -0700976 } else {
977 mod_timer(&ifsta->timer, jiffies +
978 IEEE80211_MONITORING_INTERVAL);
979 }
980}
981
982
983static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
984 u8 *ssid, size_t ssid_len)
985{
986 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg8318d782008-01-24 19:38:38 +0100987 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -0700988 struct sk_buff *skb;
989 struct ieee80211_mgmt *mgmt;
990 u8 *pos, *supp_rates, *esupp_rates = NULL;
991 int i;
992
993 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
994 if (!skb) {
995 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
996 "request\n", dev->name);
997 return;
998 }
999 skb_reserve(skb, local->hw.extra_tx_headroom);
1000
1001 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1002 memset(mgmt, 0, 24);
1003 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1004 IEEE80211_STYPE_PROBE_REQ);
1005 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1006 if (dst) {
1007 memcpy(mgmt->da, dst, ETH_ALEN);
1008 memcpy(mgmt->bssid, dst, ETH_ALEN);
1009 } else {
1010 memset(mgmt->da, 0xff, ETH_ALEN);
1011 memset(mgmt->bssid, 0xff, ETH_ALEN);
1012 }
1013 pos = skb_put(skb, 2 + ssid_len);
1014 *pos++ = WLAN_EID_SSID;
1015 *pos++ = ssid_len;
1016 memcpy(pos, ssid, ssid_len);
1017
1018 supp_rates = skb_put(skb, 2);
1019 supp_rates[0] = WLAN_EID_SUPP_RATES;
1020 supp_rates[1] = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001021 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1022
1023 for (i = 0; i < sband->n_bitrates; i++) {
1024 struct ieee80211_rate *rate = &sband->bitrates[i];
Jiri Bencf0706e82007-05-05 11:45:53 -07001025 if (esupp_rates) {
1026 pos = skb_put(skb, 1);
1027 esupp_rates[1]++;
1028 } else if (supp_rates[1] == 8) {
1029 esupp_rates = skb_put(skb, 3);
1030 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
1031 esupp_rates[1] = 1;
1032 pos = &esupp_rates[2];
1033 } else {
1034 pos = skb_put(skb, 1);
1035 supp_rates[1]++;
1036 }
Johannes Berg8318d782008-01-24 19:38:38 +01001037 *pos = rate->bitrate / 5;
Jiri Bencf0706e82007-05-05 11:45:53 -07001038 }
1039
1040 ieee80211_sta_tx(dev, skb, 0);
1041}
1042
1043
1044static int ieee80211_sta_wep_configured(struct net_device *dev)
1045{
1046 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1047 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -04001048 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e82007-05-05 11:45:53 -07001049 return 0;
1050 return 1;
1051}
1052
1053
1054static void ieee80211_auth_completed(struct net_device *dev,
1055 struct ieee80211_if_sta *ifsta)
1056{
1057 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001058 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001059 ieee80211_associate(dev, ifsta);
1060}
1061
1062
1063static void ieee80211_auth_challenge(struct net_device *dev,
1064 struct ieee80211_if_sta *ifsta,
1065 struct ieee80211_mgmt *mgmt,
1066 size_t len)
1067{
1068 u8 *pos;
1069 struct ieee802_11_elems elems;
1070
1071 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
1072 pos = mgmt->u.auth.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001073 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001074 if (!elems.challenge) {
1075 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
1076 "frame\n", dev->name);
1077 return;
1078 }
1079 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
1080 elems.challenge_len + 2, 1);
1081}
1082
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001083static void ieee80211_send_addba_resp(struct net_device *dev, u8 *da, u16 tid,
1084 u8 dialog_token, u16 status, u16 policy,
1085 u16 buf_size, u16 timeout)
1086{
1087 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1088 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1089 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1090 struct sk_buff *skb;
1091 struct ieee80211_mgmt *mgmt;
1092 u16 capab;
1093
Ester Kummer3acea5b2008-04-17 16:05:14 -07001094 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
1095
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001096 if (!skb) {
1097 printk(KERN_DEBUG "%s: failed to allocate buffer "
1098 "for addba resp frame\n", dev->name);
1099 return;
1100 }
1101
1102 skb_reserve(skb, local->hw.extra_tx_headroom);
1103 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1104 memset(mgmt, 0, 24);
1105 memcpy(mgmt->da, da, ETH_ALEN);
1106 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001107 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001108 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1109 else
1110 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1111 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1112 IEEE80211_STYPE_ACTION);
1113
1114 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_resp));
1115 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1116 mgmt->u.action.u.addba_resp.action_code = WLAN_ACTION_ADDBA_RESP;
1117 mgmt->u.action.u.addba_resp.dialog_token = dialog_token;
1118
1119 capab = (u16)(policy << 1); /* bit 1 aggregation policy */
1120 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1121 capab |= (u16)(buf_size << 6); /* bit 15:6 max size of aggregation */
1122
1123 mgmt->u.action.u.addba_resp.capab = cpu_to_le16(capab);
1124 mgmt->u.action.u.addba_resp.timeout = cpu_to_le16(timeout);
1125 mgmt->u.action.u.addba_resp.status = cpu_to_le16(status);
1126
1127 ieee80211_sta_tx(dev, skb, 0);
1128
1129 return;
1130}
1131
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001132void ieee80211_send_addba_request(struct net_device *dev, const u8 *da,
1133 u16 tid, u8 dialog_token, u16 start_seq_num,
1134 u16 agg_size, u16 timeout)
1135{
1136 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1137 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1138 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1139 struct sk_buff *skb;
1140 struct ieee80211_mgmt *mgmt;
1141 u16 capab;
1142
Ester Kummer3acea5b2008-04-17 16:05:14 -07001143 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001144
1145 if (!skb) {
1146 printk(KERN_ERR "%s: failed to allocate buffer "
1147 "for addba request frame\n", dev->name);
1148 return;
1149 }
1150 skb_reserve(skb, local->hw.extra_tx_headroom);
1151 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1152 memset(mgmt, 0, 24);
1153 memcpy(mgmt->da, da, ETH_ALEN);
1154 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1155 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
1156 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1157 else
1158 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1159
1160 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1161 IEEE80211_STYPE_ACTION);
1162
1163 skb_put(skb, 1 + sizeof(mgmt->u.action.u.addba_req));
1164
1165 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1166 mgmt->u.action.u.addba_req.action_code = WLAN_ACTION_ADDBA_REQ;
1167
1168 mgmt->u.action.u.addba_req.dialog_token = dialog_token;
1169 capab = (u16)(1 << 1); /* bit 1 aggregation policy */
1170 capab |= (u16)(tid << 2); /* bit 5:2 TID number */
1171 capab |= (u16)(agg_size << 6); /* bit 15:6 max size of aggergation */
1172
1173 mgmt->u.action.u.addba_req.capab = cpu_to_le16(capab);
1174
1175 mgmt->u.action.u.addba_req.timeout = cpu_to_le16(timeout);
1176 mgmt->u.action.u.addba_req.start_seq_num =
1177 cpu_to_le16(start_seq_num << 4);
1178
1179 ieee80211_sta_tx(dev, skb, 0);
1180}
1181
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001182static void ieee80211_sta_process_addba_request(struct net_device *dev,
1183 struct ieee80211_mgmt *mgmt,
1184 size_t len)
1185{
1186 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001187 struct ieee80211_hw *hw = &local->hw;
1188 struct ieee80211_conf *conf = &hw->conf;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001189 struct sta_info *sta;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001190 struct tid_ampdu_rx *tid_agg_rx;
1191 u16 capab, tid, timeout, ba_policy, buf_size, start_seq_num, status;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001192 u8 dialog_token;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001193 int ret = -EOPNOTSUPP;
1194 DECLARE_MAC_BUF(mac);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001195
Johannes Bergd0709a62008-02-25 16:27:46 +01001196 rcu_read_lock();
1197
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001198 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001199 if (!sta) {
1200 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001201 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001202 }
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001203
1204 /* extract session parameters from addba request frame */
1205 dialog_token = mgmt->u.action.u.addba_req.dialog_token;
1206 timeout = le16_to_cpu(mgmt->u.action.u.addba_req.timeout);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001207 start_seq_num =
1208 le16_to_cpu(mgmt->u.action.u.addba_req.start_seq_num) >> 4;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001209
1210 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1211 ba_policy = (capab & IEEE80211_ADDBA_PARAM_POLICY_MASK) >> 1;
1212 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1213 buf_size = (capab & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK) >> 6;
1214
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001215 status = WLAN_STATUS_REQUEST_DECLINED;
1216
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001217 /* sanity check for incoming parameters:
1218 * check if configuration can support the BA policy
1219 * and if buffer size does not exceeds max value */
1220 if (((ba_policy != 1)
1221 && (!(conf->ht_conf.cap & IEEE80211_HT_CAP_DELAY_BA)))
1222 || (buf_size > IEEE80211_MAX_AMPDU_BUF)) {
1223 status = WLAN_STATUS_INVALID_QOS_PARAM;
1224#ifdef CONFIG_MAC80211_HT_DEBUG
1225 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001226 printk(KERN_DEBUG "AddBA Req with bad params from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001227 "%s on tid %u. policy %d, buffer size %d\n",
1228 print_mac(mac, mgmt->sa), tid, ba_policy,
1229 buf_size);
1230#endif /* CONFIG_MAC80211_HT_DEBUG */
1231 goto end_no_lock;
1232 }
1233 /* determine default buffer size */
1234 if (buf_size == 0) {
Johannes Berg8318d782008-01-24 19:38:38 +01001235 struct ieee80211_supported_band *sband;
1236
1237 sband = local->hw.wiphy->bands[conf->channel->band];
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001238 buf_size = IEEE80211_MIN_AMPDU_BUF;
Johannes Berg8318d782008-01-24 19:38:38 +01001239 buf_size = buf_size << sband->ht_info.ampdu_factor;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001240 }
1241
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001242
1243 /* examine state machine */
Johannes Berg07346f812008-05-03 01:02:02 +02001244 spin_lock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001245
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001246 if (sta->ampdu_mlme.tid_state_rx[tid] != HT_AGG_STATE_IDLE) {
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001247#ifdef CONFIG_MAC80211_HT_DEBUG
1248 if (net_ratelimit())
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001249 printk(KERN_DEBUG "unexpected AddBA Req from "
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001250 "%s on tid %u\n",
1251 print_mac(mac, mgmt->sa), tid);
1252#endif /* CONFIG_MAC80211_HT_DEBUG */
1253 goto end;
1254 }
1255
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001256 /* prepare A-MPDU MLME for Rx aggregation */
1257 sta->ampdu_mlme.tid_rx[tid] =
1258 kmalloc(sizeof(struct tid_ampdu_rx), GFP_ATOMIC);
1259 if (!sta->ampdu_mlme.tid_rx[tid]) {
1260 if (net_ratelimit())
1261 printk(KERN_ERR "allocate rx mlme to tid %d failed\n",
1262 tid);
1263 goto end;
1264 }
1265 /* rx timer */
1266 sta->ampdu_mlme.tid_rx[tid]->session_timer.function =
1267 sta_rx_agg_session_timer_expired;
1268 sta->ampdu_mlme.tid_rx[tid]->session_timer.data =
1269 (unsigned long)&sta->timer_to_tid[tid];
1270 init_timer(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
1271
1272 tid_agg_rx = sta->ampdu_mlme.tid_rx[tid];
1273
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001274 /* prepare reordering buffer */
1275 tid_agg_rx->reorder_buf =
1276 kmalloc(buf_size * sizeof(struct sk_buf *), GFP_ATOMIC);
Johannes Berg03147df2008-02-26 00:39:28 +01001277 if (!tid_agg_rx->reorder_buf) {
1278 if (net_ratelimit())
1279 printk(KERN_ERR "can not allocate reordering buffer "
1280 "to tid %d\n", tid);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001281 kfree(sta->ampdu_mlme.tid_rx[tid]);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001282 goto end;
1283 }
1284 memset(tid_agg_rx->reorder_buf, 0,
1285 buf_size * sizeof(struct sk_buf *));
1286
1287 if (local->ops->ampdu_action)
1288 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_START,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001289 sta->addr, tid, &start_seq_num);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001290#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001291 printk(KERN_DEBUG "Rx A-MPDU request on tid %d result %d\n", tid, ret);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001292#endif /* CONFIG_MAC80211_HT_DEBUG */
1293
1294 if (ret) {
1295 kfree(tid_agg_rx->reorder_buf);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001296 kfree(tid_agg_rx);
1297 sta->ampdu_mlme.tid_rx[tid] = NULL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001298 goto end;
1299 }
1300
1301 /* change state and send addba resp */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001302 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_OPERATIONAL;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001303 tid_agg_rx->dialog_token = dialog_token;
1304 tid_agg_rx->ssn = start_seq_num;
1305 tid_agg_rx->head_seq_num = start_seq_num;
1306 tid_agg_rx->buf_size = buf_size;
1307 tid_agg_rx->timeout = timeout;
1308 tid_agg_rx->stored_mpdu_num = 0;
1309 status = WLAN_STATUS_SUCCESS;
1310end:
Johannes Berg07346f812008-05-03 01:02:02 +02001311 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001312
1313end_no_lock:
Johannes Bergd0709a62008-02-25 16:27:46 +01001314 ieee80211_send_addba_resp(sta->sdata->dev, sta->addr, tid,
1315 dialog_token, status, 1, buf_size, timeout);
1316 rcu_read_unlock();
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02001317}
Jiri Bencf0706e82007-05-05 11:45:53 -07001318
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001319static void ieee80211_sta_process_addba_resp(struct net_device *dev,
1320 struct ieee80211_mgmt *mgmt,
1321 size_t len)
1322{
1323 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1324 struct ieee80211_hw *hw = &local->hw;
1325 struct sta_info *sta;
1326 u16 capab;
1327 u16 tid;
1328 u8 *state;
1329
Johannes Bergd0709a62008-02-25 16:27:46 +01001330 rcu_read_lock();
1331
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001332 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001333 if (!sta) {
1334 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001335 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001336 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001337
1338 capab = le16_to_cpu(mgmt->u.action.u.addba_resp.capab);
1339 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1340
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001341 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001342
Johannes Berg07346f812008-05-03 01:02:02 +02001343 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001344
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001345 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001346 spin_unlock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001347 printk(KERN_DEBUG "state not HT_ADDBA_REQUESTED_MSK:"
1348 "%d\n", *state);
1349 goto addba_resp_exit;
1350 }
1351
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001352 if (mgmt->u.action.u.addba_resp.dialog_token !=
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001353 sta->ampdu_mlme.tid_tx[tid]->dialog_token) {
Johannes Berg07346f812008-05-03 01:02:02 +02001354 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001355#ifdef CONFIG_MAC80211_HT_DEBUG
1356 printk(KERN_DEBUG "wrong addBA response token, tid %d\n", tid);
1357#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001358 goto addba_resp_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001359 }
1360
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001361 del_timer_sync(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001362#ifdef CONFIG_MAC80211_HT_DEBUG
1363 printk(KERN_DEBUG "switched off addBA timer for tid %d \n", tid);
1364#endif /* CONFIG_MAC80211_HT_DEBUG */
1365 if (le16_to_cpu(mgmt->u.action.u.addba_resp.status)
1366 == WLAN_STATUS_SUCCESS) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001367 if (*state & HT_ADDBA_RECEIVED_MSK)
1368 printk(KERN_DEBUG "double addBA response\n");
1369
1370 *state |= HT_ADDBA_RECEIVED_MSK;
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001371 sta->ampdu_mlme.addba_req_num[tid] = 0;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001372
1373 if (*state == HT_AGG_STATE_OPERATIONAL) {
1374 printk(KERN_DEBUG "Aggregation on for tid %d \n", tid);
1375 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
1376 }
1377
Johannes Berg07346f812008-05-03 01:02:02 +02001378 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001379 printk(KERN_DEBUG "recipient accepted agg: tid %d \n", tid);
1380 } else {
1381 printk(KERN_DEBUG "recipient rejected agg: tid %d \n", tid);
1382
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001383 sta->ampdu_mlme.addba_req_num[tid]++;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001384 /* this will allow the state check in stop_BA_session */
1385 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001386 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001387 ieee80211_stop_tx_ba_session(hw, sta->addr, tid,
1388 WLAN_BACK_INITIATOR);
1389 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001390
1391addba_resp_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001392 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001393}
1394
1395void ieee80211_send_delba(struct net_device *dev, const u8 *da, u16 tid,
1396 u16 initiator, u16 reason_code)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001397{
1398 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1399 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1400 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1401 struct sk_buff *skb;
1402 struct ieee80211_mgmt *mgmt;
1403 u16 params;
1404
Ester Kummer3acea5b2008-04-17 16:05:14 -07001405 skb = dev_alloc_skb(sizeof(*mgmt) + local->hw.extra_tx_headroom);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001406
1407 if (!skb) {
1408 printk(KERN_ERR "%s: failed to allocate buffer "
1409 "for delba frame\n", dev->name);
1410 return;
1411 }
1412
1413 skb_reserve(skb, local->hw.extra_tx_headroom);
1414 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
1415 memset(mgmt, 0, 24);
1416 memcpy(mgmt->da, da, ETH_ALEN);
1417 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001418 if (sdata->vif.type == IEEE80211_IF_TYPE_AP)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001419 memcpy(mgmt->bssid, dev->dev_addr, ETH_ALEN);
1420 else
1421 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
1422 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1423 IEEE80211_STYPE_ACTION);
1424
1425 skb_put(skb, 1 + sizeof(mgmt->u.action.u.delba));
1426
1427 mgmt->u.action.category = WLAN_CATEGORY_BACK;
1428 mgmt->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
1429 params = (u16)(initiator << 11); /* bit 11 initiator */
1430 params |= (u16)(tid << 12); /* bit 15:12 TID number */
1431
1432 mgmt->u.action.u.delba.params = cpu_to_le16(params);
1433 mgmt->u.action.u.delba.reason_code = cpu_to_le16(reason_code);
1434
1435 ieee80211_sta_tx(dev, skb, 0);
1436}
1437
1438void ieee80211_sta_stop_rx_ba_session(struct net_device *dev, u8 *ra, u16 tid,
1439 u16 initiator, u16 reason)
1440{
1441 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1442 struct ieee80211_hw *hw = &local->hw;
1443 struct sta_info *sta;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001444 int ret, i;
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001445 DECLARE_MAC_BUF(mac);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001446
Johannes Bergd0709a62008-02-25 16:27:46 +01001447 rcu_read_lock();
1448
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001449 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +01001450 if (!sta) {
1451 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001452 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001453 }
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001454
1455 /* check if TID is in operational state */
Johannes Berg07346f812008-05-03 01:02:02 +02001456 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001457 if (sta->ampdu_mlme.tid_state_rx[tid]
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001458 != HT_AGG_STATE_OPERATIONAL) {
Johannes Berg07346f812008-05-03 01:02:02 +02001459 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +01001460 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001461 return;
1462 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001463 sta->ampdu_mlme.tid_state_rx[tid] =
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001464 HT_AGG_STATE_REQ_STOP_BA_MSK |
1465 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
Johannes Berg07346f812008-05-03 01:02:02 +02001466 spin_unlock_bh(&sta->lock);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001467
1468 /* stop HW Rx aggregation. ampdu_action existence
1469 * already verified in session init so we add the BUG_ON */
1470 BUG_ON(!local->ops->ampdu_action);
1471
Ron Rindjunsky513a1022008-04-07 10:16:56 -07001472#ifdef CONFIG_MAC80211_HT_DEBUG
1473 printk(KERN_DEBUG "Rx BA session stop requested for %s tid %u\n",
1474 print_mac(mac, ra), tid);
1475#endif /* CONFIG_MAC80211_HT_DEBUG */
1476
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001477 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_RX_STOP,
Ron Rindjunsky0df3ef42008-01-28 14:07:15 +02001478 ra, tid, NULL);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001479 if (ret)
1480 printk(KERN_DEBUG "HW problem - can not stop rx "
1481 "aggergation for tid %d\n", tid);
1482
1483 /* shutdown timer has not expired */
1484 if (initiator != WLAN_BACK_TIMER)
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001485 del_timer_sync(&sta->ampdu_mlme.tid_rx[tid]->session_timer);
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001486
1487 /* check if this is a self generated aggregation halt */
1488 if (initiator == WLAN_BACK_RECIPIENT || initiator == WLAN_BACK_TIMER)
1489 ieee80211_send_delba(dev, ra, tid, 0, reason);
1490
1491 /* free the reordering buffer */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001492 for (i = 0; i < sta->ampdu_mlme.tid_rx[tid]->buf_size; i++) {
1493 if (sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]) {
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001494 /* release the reordered frames */
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001495 dev_kfree_skb(sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i]);
1496 sta->ampdu_mlme.tid_rx[tid]->stored_mpdu_num--;
1497 sta->ampdu_mlme.tid_rx[tid]->reorder_buf[i] = NULL;
Ron Rindjunskyb5807812007-12-25 17:00:35 +02001498 }
1499 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001500 /* free resources */
1501 kfree(sta->ampdu_mlme.tid_rx[tid]->reorder_buf);
1502 kfree(sta->ampdu_mlme.tid_rx[tid]);
1503 sta->ampdu_mlme.tid_rx[tid] = NULL;
1504 sta->ampdu_mlme.tid_state_rx[tid] = HT_AGG_STATE_IDLE;
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001505
Johannes Bergd0709a62008-02-25 16:27:46 +01001506 rcu_read_unlock();
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001507}
1508
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001509
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001510static void ieee80211_sta_process_delba(struct net_device *dev,
1511 struct ieee80211_mgmt *mgmt, size_t len)
1512{
1513 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1514 struct sta_info *sta;
1515 u16 tid, params;
1516 u16 initiator;
1517 DECLARE_MAC_BUF(mac);
1518
Johannes Bergd0709a62008-02-25 16:27:46 +01001519 rcu_read_lock();
1520
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001521 sta = sta_info_get(local, mgmt->sa);
Johannes Bergd0709a62008-02-25 16:27:46 +01001522 if (!sta) {
1523 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001524 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001525 }
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001526
1527 params = le16_to_cpu(mgmt->u.action.u.delba.params);
1528 tid = (params & IEEE80211_DELBA_PARAM_TID_MASK) >> 12;
1529 initiator = (params & IEEE80211_DELBA_PARAM_INITIATOR_MASK) >> 11;
1530
1531#ifdef CONFIG_MAC80211_HT_DEBUG
1532 if (net_ratelimit())
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001533 printk(KERN_DEBUG "delba from %s (%s) tid %d reason code %d\n",
1534 print_mac(mac, mgmt->sa),
Ron Rindjunsky2e354ed2008-03-18 15:00:30 -07001535 initiator ? "initiator" : "recipient", tid,
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001536 mgmt->u.action.u.delba.reason_code);
1537#endif /* CONFIG_MAC80211_HT_DEBUG */
1538
1539 if (initiator == WLAN_BACK_INITIATOR)
1540 ieee80211_sta_stop_rx_ba_session(dev, sta->addr, tid,
1541 WLAN_BACK_INITIATOR, 0);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001542 else { /* WLAN_BACK_RECIPIENT */
Johannes Berg07346f812008-05-03 01:02:02 +02001543 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001544 sta->ampdu_mlme.tid_state_tx[tid] =
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001545 HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001546 spin_unlock_bh(&sta->lock);
Ron Rindjunskyd92684e2008-01-28 14:07:22 +02001547 ieee80211_stop_tx_ba_session(&local->hw, sta->addr, tid,
1548 WLAN_BACK_RECIPIENT);
1549 }
Johannes Bergd0709a62008-02-25 16:27:46 +01001550 rcu_read_unlock();
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02001551}
1552
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001553/*
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001554 * After sending add Block Ack request we activated a timer until
1555 * add Block Ack response will arrive from the recipient.
1556 * If this timer expires sta_addba_resp_timer_expired will be executed.
1557 */
1558void sta_addba_resp_timer_expired(unsigned long data)
1559{
1560 /* not an elegant detour, but there is no choice as the timer passes
1561 * only one argument, and both sta_info and TID are needed, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001562 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001563 * array gives the sta through container_of */
1564 u16 tid = *(int *)data;
1565 struct sta_info *temp_sta = container_of((void *)data,
1566 struct sta_info, timer_to_tid[tid]);
1567
1568 struct ieee80211_local *local = temp_sta->local;
1569 struct ieee80211_hw *hw = &local->hw;
1570 struct sta_info *sta;
1571 u8 *state;
1572
Johannes Bergd0709a62008-02-25 16:27:46 +01001573 rcu_read_lock();
1574
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001575 sta = sta_info_get(local, temp_sta->addr);
Johannes Bergd0709a62008-02-25 16:27:46 +01001576 if (!sta) {
1577 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001578 return;
Johannes Bergd0709a62008-02-25 16:27:46 +01001579 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001580
Ron Rindjunskycee24a32008-03-26 20:36:03 +02001581 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001582 /* check if the TID waits for addBA response */
Johannes Berg07346f812008-05-03 01:02:02 +02001583 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001584 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
Johannes Berg07346f812008-05-03 01:02:02 +02001585 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001586 *state = HT_AGG_STATE_IDLE;
1587 printk(KERN_DEBUG "timer expired on tid %d but we are not "
1588 "expecting addBA response there", tid);
1589 goto timer_expired_exit;
1590 }
1591
1592 printk(KERN_DEBUG "addBA response timer expired on tid %d\n", tid);
1593
1594 /* go through the state check in stop_BA_session */
1595 *state = HT_AGG_STATE_OPERATIONAL;
Johannes Berg07346f812008-05-03 01:02:02 +02001596 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001597 ieee80211_stop_tx_ba_session(hw, temp_sta->addr, tid,
1598 WLAN_BACK_INITIATOR);
1599
1600timer_expired_exit:
Johannes Bergd0709a62008-02-25 16:27:46 +01001601 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001602}
1603
1604/*
Ron Rindjunsky7b9d44c2008-03-18 15:00:31 -07001605 * After accepting the AddBA Request we activated a timer,
1606 * resetting it after each frame that arrives from the originator.
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001607 * if this timer expires ieee80211_sta_stop_rx_ba_session will be executed.
1608 */
Adrian Bunk712590d2008-04-21 11:47:51 +03001609static void sta_rx_agg_session_timer_expired(unsigned long data)
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001610{
1611 /* not an elegant detour, but there is no choice as the timer passes
1612 * only one argument, and verious sta_info are needed here, so init
Johannes Berg73651ee2008-02-25 16:27:47 +01001613 * flow in sta_info_create gives the TID as data, while the timer_to_id
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001614 * array gives the sta through container_of */
1615 u8 *ptid = (u8 *)data;
1616 u8 *timer_to_id = ptid - *ptid;
1617 struct sta_info *sta = container_of(timer_to_id, struct sta_info,
1618 timer_to_tid[0]);
1619
1620 printk(KERN_DEBUG "rx session timer expired on tid %d\n", (u16)*ptid);
Johannes Bergd0709a62008-02-25 16:27:46 +01001621 ieee80211_sta_stop_rx_ba_session(sta->sdata->dev, sta->addr,
1622 (u16)*ptid, WLAN_BACK_TIMER,
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001623 WLAN_REASON_QSTA_TIMEOUT);
1624}
1625
Ron Rindjunsky85249e52008-03-18 15:00:32 -07001626void ieee80211_sta_tear_down_BA_sessions(struct net_device *dev, u8 *addr)
1627{
1628 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1629 int i;
1630
1631 for (i = 0; i < STA_TID_NUM; i++) {
1632 ieee80211_stop_tx_ba_session(&local->hw, addr, i,
1633 WLAN_BACK_INITIATOR);
1634 ieee80211_sta_stop_rx_ba_session(dev, addr, i,
1635 WLAN_BACK_RECIPIENT,
1636 WLAN_REASON_QSTA_LEAVE_QBSS);
1637 }
1638}
Ron Rindjunsky07db2182007-12-25 17:00:33 +02001639
Jiri Bencf0706e82007-05-05 11:45:53 -07001640static void ieee80211_rx_mgmt_auth(struct net_device *dev,
1641 struct ieee80211_if_sta *ifsta,
1642 struct ieee80211_mgmt *mgmt,
1643 size_t len)
1644{
1645 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1646 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -07001647 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001648
1649 if (ifsta->state != IEEE80211_AUTHENTICATE &&
Johannes Berg51fb61e2007-12-19 01:31:27 +01001650 sdata->vif.type != IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001651 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001652 "%s, but not in authenticate state - ignored\n",
1653 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001654 return;
1655 }
1656
1657 if (len < 24 + 6) {
1658 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001659 "received from %s - ignored\n",
1660 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001661 return;
1662 }
1663
Johannes Berg51fb61e2007-12-19 01:31:27 +01001664 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001665 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1666 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001667 "unknown AP (SA=%s BSSID=%s) - "
1668 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1669 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001670 return;
1671 }
1672
Johannes Berg51fb61e2007-12-19 01:31:27 +01001673 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Jiri Bencf0706e82007-05-05 11:45:53 -07001674 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
1675 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001676 "unknown BSSID (SA=%s BSSID=%s) - "
1677 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1678 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001679 return;
1680 }
1681
1682 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
1683 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
1684 status_code = le16_to_cpu(mgmt->u.auth.status_code);
1685
Joe Perches0795af52007-10-03 17:59:30 -07001686 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e82007-05-05 11:45:53 -07001687 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001688 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e82007-05-05 11:45:53 -07001689 auth_transaction, status_code);
1690
Johannes Berg51fb61e2007-12-19 01:31:27 +01001691 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001692 /* IEEE 802.11 standard does not require authentication in IBSS
1693 * networks and most implementations do not seem to use it.
1694 * However, try to reply to authentication attempts if someone
1695 * has actually implemented this.
1696 * TODO: Could implement shared key authentication. */
1697 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1698 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1699 "frame (alg=%d transaction=%d)\n",
1700 dev->name, auth_alg, auth_transaction);
1701 return;
1702 }
1703 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1704 }
1705
1706 if (auth_alg != ifsta->auth_alg ||
1707 auth_transaction != ifsta->auth_transaction) {
1708 printk(KERN_DEBUG "%s: unexpected authentication frame "
1709 "(alg=%d transaction=%d)\n",
1710 dev->name, auth_alg, auth_transaction);
1711 return;
1712 }
1713
1714 if (status_code != WLAN_STATUS_SUCCESS) {
1715 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1716 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1717 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1718 u8 algs[3];
1719 const int num_algs = ARRAY_SIZE(algs);
1720 int i, pos;
1721 algs[0] = algs[1] = algs[2] = 0xff;
1722 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1723 algs[0] = WLAN_AUTH_OPEN;
1724 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1725 algs[1] = WLAN_AUTH_SHARED_KEY;
1726 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1727 algs[2] = WLAN_AUTH_LEAP;
1728 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1729 pos = 0;
1730 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1731 pos = 1;
1732 else
1733 pos = 2;
1734 for (i = 0; i < num_algs; i++) {
1735 pos++;
1736 if (pos >= num_algs)
1737 pos = 0;
1738 if (algs[pos] == ifsta->auth_alg ||
1739 algs[pos] == 0xff)
1740 continue;
1741 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1742 !ieee80211_sta_wep_configured(dev))
1743 continue;
1744 ifsta->auth_alg = algs[pos];
1745 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1746 "next try\n",
1747 dev->name, ifsta->auth_alg);
1748 break;
1749 }
1750 }
1751 return;
1752 }
1753
1754 switch (ifsta->auth_alg) {
1755 case WLAN_AUTH_OPEN:
1756 case WLAN_AUTH_LEAP:
1757 ieee80211_auth_completed(dev, ifsta);
1758 break;
1759 case WLAN_AUTH_SHARED_KEY:
1760 if (ifsta->auth_transaction == 4)
1761 ieee80211_auth_completed(dev, ifsta);
1762 else
1763 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1764 break;
1765 }
1766}
1767
1768
1769static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1770 struct ieee80211_if_sta *ifsta,
1771 struct ieee80211_mgmt *mgmt,
1772 size_t len)
1773{
1774 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001775 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001776
1777 if (len < 24 + 2) {
1778 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001779 "received from %s - ignored\n",
1780 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001781 return;
1782 }
1783
1784 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1785 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001786 "unknown AP (SA=%s BSSID=%s) - "
1787 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1788 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001789 return;
1790 }
1791
1792 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1793
Joe Perches0795af52007-10-03 17:59:30 -07001794 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001795 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001796 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001797
Johannes Berg988c0f72008-04-17 19:21:22 +02001798 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001799 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07001800
1801 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1802 ifsta->state == IEEE80211_ASSOCIATE ||
1803 ifsta->state == IEEE80211_ASSOCIATED) {
1804 ifsta->state = IEEE80211_AUTHENTICATE;
1805 mod_timer(&ifsta->timer, jiffies +
1806 IEEE80211_RETRY_AUTH_INTERVAL);
1807 }
1808
1809 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001810 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001811}
1812
1813
1814static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1815 struct ieee80211_if_sta *ifsta,
1816 struct ieee80211_mgmt *mgmt,
1817 size_t len)
1818{
1819 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001820 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07001821
1822 if (len < 24 + 2) {
1823 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001824 "received from %s - ignored\n",
1825 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001826 return;
1827 }
1828
1829 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1830 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001831 "unknown AP (SA=%s BSSID=%s) - "
1832 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1833 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001834 return;
1835 }
1836
1837 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1838
Joe Perches0795af52007-10-03 17:59:30 -07001839 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07001840 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001841 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e82007-05-05 11:45:53 -07001842
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001843 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e82007-05-05 11:45:53 -07001844 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1845
1846 if (ifsta->state == IEEE80211_ASSOCIATED) {
1847 ifsta->state = IEEE80211_ASSOCIATE;
1848 mod_timer(&ifsta->timer, jiffies +
1849 IEEE80211_RETRY_AUTH_INTERVAL);
1850 }
1851
1852 ieee80211_set_disassoc(dev, ifsta, 0);
1853}
1854
1855
Johannes Berg471b3ef2007-12-28 14:32:58 +01001856static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
Jiri Bencf0706e82007-05-05 11:45:53 -07001857 struct ieee80211_if_sta *ifsta,
1858 struct ieee80211_mgmt *mgmt,
1859 size_t len,
1860 int reassoc)
1861{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001862 struct ieee80211_local *local = sdata->local;
1863 struct net_device *dev = sdata->dev;
Johannes Berg8318d782008-01-24 19:38:38 +01001864 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07001865 struct sta_info *sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001866 u64 rates, basic_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -07001867 u16 capab_info, status_code, aid;
1868 struct ieee802_11_elems elems;
Johannes Berg471b3ef2007-12-28 14:32:58 +01001869 struct ieee80211_bss_conf *bss_conf = &sdata->bss_conf;
Jiri Bencf0706e82007-05-05 11:45:53 -07001870 u8 *pos;
1871 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001872 DECLARE_MAC_BUF(mac);
Johannes Berg8318d782008-01-24 19:38:38 +01001873 bool have_higher_than_11mbit = false;
Jiri Bencf0706e82007-05-05 11:45:53 -07001874
1875 /* AssocResp and ReassocResp have identical structure, so process both
1876 * of them in this function. */
1877
1878 if (ifsta->state != IEEE80211_ASSOCIATE) {
1879 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001880 "%s, but not in associate state - ignored\n",
1881 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001882 return;
1883 }
1884
1885 if (len < 24 + 6) {
1886 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001887 "received from %s - ignored\n",
1888 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07001889 return;
1890 }
1891
1892 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1893 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001894 "unknown AP (SA=%s BSSID=%s) - "
1895 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1896 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07001897 return;
1898 }
1899
1900 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1901 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1902 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e82007-05-05 11:45:53 -07001903
Joe Perches0795af52007-10-03 17:59:30 -07001904 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e82007-05-05 11:45:53 -07001905 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001906 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Bergddd68582007-10-22 14:51:37 +02001907 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
Jiri Bencf0706e82007-05-05 11:45:53 -07001908
1909 if (status_code != WLAN_STATUS_SUCCESS) {
1910 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1911 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001912 /* if this was a reassociation, ensure we try a "full"
1913 * association next time. This works around some broken APs
1914 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001915 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07001916 return;
1917 }
1918
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001919 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1920 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1921 "set\n", dev->name, aid);
1922 aid &= ~(BIT(15) | BIT(14));
1923
Jiri Bencf0706e82007-05-05 11:45:53 -07001924 pos = mgmt->u.assoc_resp.variable;
John W. Linville67a4cce2007-10-12 16:40:37 -04001925 ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07001926
1927 if (!elems.supp_rates) {
1928 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1929 dev->name);
1930 return;
1931 }
1932
1933 printk(KERN_DEBUG "%s: associated\n", dev->name);
1934 ifsta->aid = aid;
1935 ifsta->ap_capab = capab_info;
1936
1937 kfree(ifsta->assocresp_ies);
1938 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001939 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001940 if (ifsta->assocresp_ies)
1941 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1942
Johannes Bergd0709a62008-02-25 16:27:46 +01001943 rcu_read_lock();
1944
Jiri Bencf0706e82007-05-05 11:45:53 -07001945 /* Add STA entry for the AP */
1946 sta = sta_info_get(local, ifsta->bssid);
1947 if (!sta) {
1948 struct ieee80211_sta_bss *bss;
Johannes Berg73651ee2008-02-25 16:27:47 +01001949 int err;
Johannes Bergd0709a62008-02-25 16:27:46 +01001950
Johannes Berg73651ee2008-02-25 16:27:47 +01001951 sta = sta_info_alloc(sdata, ifsta->bssid, GFP_ATOMIC);
1952 if (!sta) {
1953 printk(KERN_DEBUG "%s: failed to alloc STA entry for"
1954 " the AP\n", dev->name);
Johannes Bergd0709a62008-02-25 16:27:46 +01001955 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001956 return;
1957 }
John W. Linville65c107a2007-10-05 14:23:27 -04001958 bss = ieee80211_rx_bss_get(dev, ifsta->bssid,
Johannes Berg8318d782008-01-24 19:38:38 +01001959 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04001960 ifsta->ssid, ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07001961 if (bss) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001962 sta->last_signal = bss->signal;
Bruno Randolf566bfe52008-05-08 19:15:40 +02001963 sta->last_qual = bss->qual;
Jiri Bencf0706e82007-05-05 11:45:53 -07001964 sta->last_noise = bss->noise;
1965 ieee80211_rx_bss_put(dev, bss);
1966 }
Johannes Berg73651ee2008-02-25 16:27:47 +01001967
1968 err = sta_info_insert(sta);
1969 if (err) {
1970 printk(KERN_DEBUG "%s: failed to insert STA entry for"
1971 " the AP (error %d)\n", dev->name, err);
Johannes Berg73651ee2008-02-25 16:27:47 +01001972 rcu_read_unlock();
1973 return;
1974 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001975 }
1976
Johannes Berg73651ee2008-02-25 16:27:47 +01001977 /*
1978 * FIXME: Do we really need to update the sta_info's information here?
1979 * We already know about the AP (we found it in our list) so it
1980 * should already be filled with the right info, no?
1981 * As is stands, all this is racy because typically we assume
1982 * the information that is filled in here (except flags) doesn't
1983 * change while a STA structure is alive. As such, it should move
1984 * to between the sta_info_alloc() and sta_info_insert() above.
1985 */
1986
Johannes Berg07346f812008-05-03 01:02:02 +02001987 set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP |
1988 WLAN_STA_AUTHORIZED);
Jiri Bencf0706e82007-05-05 11:45:53 -07001989
1990 rates = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001991 basic_rates = 0;
1992 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1993
Jiri Bencf0706e82007-05-05 11:45:53 -07001994 for (i = 0; i < elems.supp_rates_len; i++) {
1995 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01001996
1997 if (rate > 110)
1998 have_higher_than_11mbit = true;
1999
2000 for (j = 0; j < sband->n_bitrates; j++) {
2001 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002002 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002003 if (elems.supp_rates[i] & 0x80)
2004 basic_rates |= BIT(j);
2005 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002006 }
Johannes Berg8318d782008-01-24 19:38:38 +01002007
Jiri Bencf0706e82007-05-05 11:45:53 -07002008 for (i = 0; i < elems.ext_supp_rates_len; i++) {
2009 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +01002010
2011 if (rate > 110)
2012 have_higher_than_11mbit = true;
2013
2014 for (j = 0; j < sband->n_bitrates; j++) {
2015 if (sband->bitrates[j].bitrate == rate)
Jiri Bencf0706e82007-05-05 11:45:53 -07002016 rates |= BIT(j);
Johannes Berg8318d782008-01-24 19:38:38 +01002017 if (elems.ext_supp_rates[i] & 0x80)
2018 basic_rates |= BIT(j);
2019 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002020 }
Johannes Berg8318d782008-01-24 19:38:38 +01002021
2022 sta->supp_rates[local->hw.conf.channel->band] = rates;
2023 sdata->basic_rates = basic_rates;
2024
2025 /* cf. IEEE 802.11 9.2.12 */
2026 if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
2027 have_higher_than_11mbit)
2028 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
2029 else
2030 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002031
Johannes Berg3434fbd2008-05-03 00:59:37 +02002032 if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
2033 (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002034 struct ieee80211_ht_bss_info bss_info;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002035 ieee80211_ht_cap_ie_to_ht_info(
2036 (struct ieee80211_ht_cap *)
2037 elems.ht_cap_elem, &sta->ht_info);
2038 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2039 (struct ieee80211_ht_addt_info *)
2040 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002041 ieee80211_handle_ht(local, 1, &sta->ht_info, &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002042 }
2043
Jiri Bencf0706e82007-05-05 11:45:53 -07002044 rate_control_rate_init(sta, local);
2045
Johannes Berg3434fbd2008-05-03 00:59:37 +02002046 if (elems.wmm_param) {
Johannes Berg07346f812008-05-03 01:02:02 +02002047 set_sta_flags(sta, WLAN_STA_WME);
Johannes Berge5f98f22008-03-05 20:39:31 +01002048 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002049 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2050 elems.wmm_param_len);
Johannes Berge5f98f22008-03-05 20:39:31 +01002051 } else
2052 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07002053
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002054 /* set AID and assoc capability,
2055 * ieee80211_set_associated() will tell the driver */
Johannes Berg8318d782008-01-24 19:38:38 +01002056 bss_conf->aid = aid;
Tomas Winkler21c0cbe2008-03-28 16:33:34 -07002057 bss_conf->assoc_capability = capab_info;
Johannes Berg8318d782008-01-24 19:38:38 +01002058 ieee80211_set_associated(dev, ifsta, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07002059
Jiri Bencf0706e82007-05-05 11:45:53 -07002060 ieee80211_associated(dev, ifsta);
2061}
2062
2063
2064/* Caller must hold local->sta_bss_lock */
2065static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
2066 struct ieee80211_sta_bss *bss)
2067{
2068 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002069 u8 hash_idx;
Johannes Berg902acc72008-02-23 15:17:19 +01002070
2071 if (bss_mesh_cfg(bss))
2072 hash_idx = mesh_id_hash(bss_mesh_id(bss),
2073 bss_mesh_id_len(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002074 else
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002075 hash_idx = STA_HASH(bss->bssid);
Johannes Berg902acc72008-02-23 15:17:19 +01002076
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002077 bss->hnext = local->sta_bss_hash[hash_idx];
2078 local->sta_bss_hash[hash_idx] = bss;
Jiri Bencf0706e82007-05-05 11:45:53 -07002079}
2080
2081
2082/* Caller must hold local->sta_bss_lock */
2083static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
2084 struct ieee80211_sta_bss *bss)
2085{
2086 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2087 struct ieee80211_sta_bss *b, *prev = NULL;
2088 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
2089 while (b) {
2090 if (b == bss) {
2091 if (!prev)
2092 local->sta_bss_hash[STA_HASH(bss->bssid)] =
2093 bss->hnext;
2094 else
2095 prev->hnext = bss->hnext;
2096 break;
2097 }
2098 prev = b;
2099 b = b->hnext;
2100 }
2101}
2102
2103
2104static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002105ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002106 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002107{
2108 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2109 struct ieee80211_sta_bss *bss;
2110
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002111 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07002112 if (!bss)
2113 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07002114 atomic_inc(&bss->users);
2115 atomic_inc(&bss->users);
2116 memcpy(bss->bssid, bssid, ETH_ALEN);
Johannes Berg8318d782008-01-24 19:38:38 +01002117 bss->freq = freq;
John W. Linvillecffdd302007-10-05 14:23:27 -04002118 if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
2119 memcpy(bss->ssid, ssid, ssid_len);
2120 bss->ssid_len = ssid_len;
2121 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002122
2123 spin_lock_bh(&local->sta_bss_lock);
2124 /* TODO: order by RSSI? */
2125 list_add_tail(&bss->list, &local->sta_bss_list);
2126 __ieee80211_rx_bss_hash_add(dev, bss);
2127 spin_unlock_bh(&local->sta_bss_lock);
2128 return bss;
2129}
2130
Jiri Bencf0706e82007-05-05 11:45:53 -07002131static struct ieee80211_sta_bss *
Johannes Berg8318d782008-01-24 19:38:38 +01002132ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid, int freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002133 u8 *ssid, u8 ssid_len)
Jiri Bencf0706e82007-05-05 11:45:53 -07002134{
2135 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2136 struct ieee80211_sta_bss *bss;
2137
2138 spin_lock_bh(&local->sta_bss_lock);
2139 bss = local->sta_bss_hash[STA_HASH(bssid)];
2140 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002141 if (!bss_mesh_cfg(bss) &&
2142 !memcmp(bss->bssid, bssid, ETH_ALEN) &&
Johannes Berg8318d782008-01-24 19:38:38 +01002143 bss->freq == freq &&
John W. Linvillecffdd302007-10-05 14:23:27 -04002144 bss->ssid_len == ssid_len &&
2145 (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002146 atomic_inc(&bss->users);
2147 break;
2148 }
2149 bss = bss->hnext;
2150 }
2151 spin_unlock_bh(&local->sta_bss_lock);
2152 return bss;
2153}
2154
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002155#ifdef CONFIG_MAC80211_MESH
2156static struct ieee80211_sta_bss *
2157ieee80211_rx_mesh_bss_get(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
2158 u8 *mesh_cfg, int freq)
2159{
2160 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2161 struct ieee80211_sta_bss *bss;
2162
2163 spin_lock_bh(&local->sta_bss_lock);
2164 bss = local->sta_bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
2165 while (bss) {
Johannes Berg902acc72008-02-23 15:17:19 +01002166 if (bss_mesh_cfg(bss) &&
2167 !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002168 bss->freq == freq &&
2169 mesh_id_len == bss->mesh_id_len &&
2170 (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
2171 mesh_id_len))) {
2172 atomic_inc(&bss->users);
2173 break;
2174 }
2175 bss = bss->hnext;
2176 }
2177 spin_unlock_bh(&local->sta_bss_lock);
2178 return bss;
2179}
2180
2181static struct ieee80211_sta_bss *
2182ieee80211_rx_mesh_bss_add(struct net_device *dev, u8 *mesh_id, int mesh_id_len,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002183 u8 *mesh_cfg, int mesh_config_len, int freq)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002184{
2185 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2186 struct ieee80211_sta_bss *bss;
2187
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002188 if (mesh_config_len != MESH_CFG_LEN)
2189 return NULL;
2190
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002191 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
2192 if (!bss)
2193 return NULL;
2194
John W. Linville24736702008-04-08 14:15:46 -04002195 bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002196 if (!bss->mesh_cfg) {
2197 kfree(bss);
2198 return NULL;
2199 }
2200
2201 if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
2202 bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
2203 if (!bss->mesh_id) {
2204 kfree(bss->mesh_cfg);
2205 kfree(bss);
2206 return NULL;
2207 }
2208 memcpy(bss->mesh_id, mesh_id, mesh_id_len);
2209 }
2210
2211 atomic_inc(&bss->users);
2212 atomic_inc(&bss->users);
John W. Linville24736702008-04-08 14:15:46 -04002213 memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002214 bss->mesh_id_len = mesh_id_len;
2215 bss->freq = freq;
2216 spin_lock_bh(&local->sta_bss_lock);
2217 /* TODO: order by RSSI? */
2218 list_add_tail(&bss->list, &local->sta_bss_list);
2219 __ieee80211_rx_bss_hash_add(dev, bss);
2220 spin_unlock_bh(&local->sta_bss_lock);
2221 return bss;
2222}
2223#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002224
2225static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
2226{
2227 kfree(bss->wpa_ie);
2228 kfree(bss->rsn_ie);
2229 kfree(bss->wmm_ie);
Ron Rindjunskyc7153502007-11-26 16:14:31 +02002230 kfree(bss->ht_ie);
Johannes Berg902acc72008-02-23 15:17:19 +01002231 kfree(bss_mesh_id(bss));
2232 kfree(bss_mesh_cfg(bss));
Jiri Bencf0706e82007-05-05 11:45:53 -07002233 kfree(bss);
2234}
2235
2236
2237static void ieee80211_rx_bss_put(struct net_device *dev,
2238 struct ieee80211_sta_bss *bss)
2239{
2240 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e82007-05-05 11:45:53 -07002241
Pavel Emelyanov1ebebea2008-04-23 11:47:15 +04002242 local_bh_disable();
2243 if (!atomic_dec_and_lock(&bss->users, &local->sta_bss_lock)) {
2244 local_bh_enable();
2245 return;
2246 }
2247
Jiri Bencf0706e82007-05-05 11:45:53 -07002248 __ieee80211_rx_bss_hash_del(dev, bss);
2249 list_del(&bss->list);
2250 spin_unlock_bh(&local->sta_bss_lock);
2251 ieee80211_rx_bss_free(bss);
2252}
2253
2254
2255void ieee80211_rx_bss_list_init(struct net_device *dev)
2256{
2257 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2258 spin_lock_init(&local->sta_bss_lock);
2259 INIT_LIST_HEAD(&local->sta_bss_list);
2260}
2261
2262
2263void ieee80211_rx_bss_list_deinit(struct net_device *dev)
2264{
2265 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2266 struct ieee80211_sta_bss *bss, *tmp;
2267
2268 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
2269 ieee80211_rx_bss_put(dev, bss);
2270}
2271
2272
Bruno Randolfa6072682008-02-18 11:21:15 +09002273static int ieee80211_sta_join_ibss(struct net_device *dev,
2274 struct ieee80211_if_sta *ifsta,
2275 struct ieee80211_sta_bss *bss)
2276{
2277 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2278 int res, rates, i, j;
2279 struct sk_buff *skb;
2280 struct ieee80211_mgmt *mgmt;
2281 struct ieee80211_tx_control control;
2282 struct rate_selection ratesel;
2283 u8 *pos;
2284 struct ieee80211_sub_if_data *sdata;
2285 struct ieee80211_supported_band *sband;
2286
2287 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2288
Johannes Berg9dd6aed2008-03-31 19:22:59 +02002289 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2290
Bruno Randolfa6072682008-02-18 11:21:15 +09002291 /* Remove possible STA entries from other IBSS networks. */
Johannes Bergdc6676b2008-03-31 19:23:03 +02002292 sta_info_flush_delayed(sdata);
Bruno Randolfa6072682008-02-18 11:21:15 +09002293
2294 if (local->ops->reset_tsf) {
2295 /* Reset own TSF to allow time synchronization work. */
2296 local->ops->reset_tsf(local_to_hw(local));
2297 }
2298 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2299 res = ieee80211_if_config(dev);
2300 if (res)
2301 return res;
2302
2303 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2304
Bruno Randolfa6072682008-02-18 11:21:15 +09002305 sdata->drop_unencrypted = bss->capability &
2306 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2307
2308 res = ieee80211_set_freq(local, bss->freq);
2309
2310 if (local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS) {
2311 printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
2312 "%d MHz\n", dev->name, local->oper_channel->center_freq);
2313 return -1;
2314 }
2315
2316 /* Set beacon template */
2317 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2318 do {
2319 if (!skb)
2320 break;
2321
2322 skb_reserve(skb, local->hw.extra_tx_headroom);
2323
2324 mgmt = (struct ieee80211_mgmt *)
2325 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2326 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2327 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2328 IEEE80211_STYPE_BEACON);
2329 memset(mgmt->da, 0xff, ETH_ALEN);
2330 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2331 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2332 mgmt->u.beacon.beacon_int =
2333 cpu_to_le16(local->hw.conf.beacon_int);
2334 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2335
2336 pos = skb_put(skb, 2 + ifsta->ssid_len);
2337 *pos++ = WLAN_EID_SSID;
2338 *pos++ = ifsta->ssid_len;
2339 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2340
2341 rates = bss->supp_rates_len;
2342 if (rates > 8)
2343 rates = 8;
2344 pos = skb_put(skb, 2 + rates);
2345 *pos++ = WLAN_EID_SUPP_RATES;
2346 *pos++ = rates;
2347 memcpy(pos, bss->supp_rates, rates);
2348
2349 if (bss->band == IEEE80211_BAND_2GHZ) {
2350 pos = skb_put(skb, 2 + 1);
2351 *pos++ = WLAN_EID_DS_PARAMS;
2352 *pos++ = 1;
2353 *pos++ = ieee80211_frequency_to_channel(bss->freq);
2354 }
2355
2356 pos = skb_put(skb, 2 + 2);
2357 *pos++ = WLAN_EID_IBSS_PARAMS;
2358 *pos++ = 2;
2359 /* FIX: set ATIM window based on scan results */
2360 *pos++ = 0;
2361 *pos++ = 0;
2362
2363 if (bss->supp_rates_len > 8) {
2364 rates = bss->supp_rates_len - 8;
2365 pos = skb_put(skb, 2 + rates);
2366 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2367 *pos++ = rates;
2368 memcpy(pos, &bss->supp_rates[8], rates);
2369 }
2370
2371 memset(&control, 0, sizeof(control));
2372 rate_control_get_rate(dev, sband, skb, &ratesel);
2373 if (!ratesel.rate) {
2374 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2375 "for IBSS beacon\n", dev->name);
2376 break;
2377 }
2378 control.vif = &sdata->vif;
2379 control.tx_rate = ratesel.rate;
2380 if (sdata->bss_conf.use_short_preamble &&
2381 ratesel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
2382 control.flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
2383 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2384 control.flags |= IEEE80211_TXCTL_NO_ACK;
2385 control.retry_limit = 1;
2386
2387 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2388 if (ifsta->probe_resp) {
2389 mgmt = (struct ieee80211_mgmt *)
2390 ifsta->probe_resp->data;
2391 mgmt->frame_control =
2392 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2393 IEEE80211_STYPE_PROBE_RESP);
2394 } else {
2395 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2396 "template for IBSS\n", dev->name);
2397 }
2398
2399 if (local->ops->beacon_update &&
2400 local->ops->beacon_update(local_to_hw(local),
2401 skb, &control) == 0) {
2402 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2403 "template\n", dev->name);
2404 skb = NULL;
2405 }
2406
2407 rates = 0;
2408 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2409 for (i = 0; i < bss->supp_rates_len; i++) {
2410 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2411 for (j = 0; j < sband->n_bitrates; j++)
2412 if (sband->bitrates[j].bitrate == bitrate)
2413 rates |= BIT(j);
2414 }
2415 ifsta->supp_rates_bits[local->hw.conf.channel->band] = rates;
Vladimir Koutnye2839d82008-03-18 21:14:07 +01002416
2417 ieee80211_sta_def_wmm_params(dev, bss, 1);
Bruno Randolfa6072682008-02-18 11:21:15 +09002418 } while (0);
2419
2420 if (skb) {
2421 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2422 "template\n", dev->name);
2423 dev_kfree_skb(skb);
2424 }
2425
2426 ifsta->state = IEEE80211_IBSS_JOINED;
2427 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2428
2429 ieee80211_rx_bss_put(dev, bss);
2430
2431 return res;
2432}
2433
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002434u64 ieee80211_sta_get_rates(struct ieee80211_local *local,
2435 struct ieee802_11_elems *elems,
2436 enum ieee80211_band band)
2437{
2438 struct ieee80211_supported_band *sband;
2439 struct ieee80211_rate *bitrates;
2440 size_t num_rates;
2441 u64 supp_rates;
2442 int i, j;
2443 sband = local->hw.wiphy->bands[band];
2444
2445 if (!sband) {
2446 WARN_ON(1);
2447 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2448 }
2449
2450 bitrates = sband->bitrates;
2451 num_rates = sband->n_bitrates;
2452 supp_rates = 0;
2453 for (i = 0; i < elems->supp_rates_len +
2454 elems->ext_supp_rates_len; i++) {
2455 u8 rate = 0;
2456 int own_rate;
2457 if (i < elems->supp_rates_len)
2458 rate = elems->supp_rates[i];
2459 else if (elems->ext_supp_rates)
2460 rate = elems->ext_supp_rates
2461 [i - elems->supp_rates_len];
2462 own_rate = 5 * (rate & 0x7f);
2463 for (j = 0; j < num_rates; j++)
2464 if (bitrates[j].bitrate == own_rate)
2465 supp_rates |= BIT(j);
2466 }
2467 return supp_rates;
2468}
2469
Bruno Randolfa6072682008-02-18 11:21:15 +09002470
Jiri Bencf0706e82007-05-05 11:45:53 -07002471static void ieee80211_rx_bss_info(struct net_device *dev,
2472 struct ieee80211_mgmt *mgmt,
2473 size_t len,
2474 struct ieee80211_rx_status *rx_status,
2475 int beacon)
2476{
2477 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2478 struct ieee802_11_elems elems;
2479 size_t baselen;
Johannes Berg8318d782008-01-24 19:38:38 +01002480 int freq, clen;
Jiri Bencf0706e82007-05-05 11:45:53 -07002481 struct ieee80211_sta_bss *bss;
2482 struct sta_info *sta;
2483 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002484 u64 beacon_timestamp, rx_timestamp;
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002485 struct ieee80211_channel *channel;
Joe Perches0795af52007-10-03 17:59:30 -07002486 DECLARE_MAC_BUF(mac);
2487 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07002488
2489 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
2490 return; /* ignore ProbeResp to foreign address */
2491
2492#if 0
Joe Perches0795af52007-10-03 17:59:30 -07002493 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e82007-05-05 11:45:53 -07002494 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07002495 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002496#endif
2497
2498 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2499 if (baselen > len)
2500 return;
2501
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002502 beacon_timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
John W. Linville67a4cce2007-10-12 16:40:37 -04002503 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002504
Johannes Berg902acc72008-02-23 15:17:19 +01002505 if (ieee80211_vif_is_mesh(&sdata->vif) && elems.mesh_id &&
2506 elems.mesh_config && mesh_matches_local(&elems, dev)) {
2507 u64 rates = ieee80211_sta_get_rates(local, &elems,
2508 rx_status->band);
2509
2510 mesh_neighbour_update(mgmt->sa, rates, dev,
2511 mesh_peer_accepts_plinks(&elems, dev));
2512 }
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002513
Johannes Bergd0709a62008-02-25 16:27:46 +01002514 rcu_read_lock();
2515
Johannes Berg51fb61e2007-12-19 01:31:27 +01002516 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
Jiri Bencf0706e82007-05-05 11:45:53 -07002517 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
2518 (sta = sta_info_get(local, mgmt->sa))) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002519 u64 prev_rates;
2520 u64 supp_rates = ieee80211_sta_get_rates(local, &elems,
2521 rx_status->band);
Jiri Bencf0706e82007-05-05 11:45:53 -07002522
Johannes Berg8318d782008-01-24 19:38:38 +01002523 prev_rates = sta->supp_rates[rx_status->band];
2524 sta->supp_rates[rx_status->band] &= supp_rates;
2525 if (sta->supp_rates[rx_status->band] == 0) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002526 /* No matching rates - this should not really happen.
2527 * Make sure that at least one rate is marked
2528 * supported to avoid issues with TX rate ctrl. */
Johannes Berg8318d782008-01-24 19:38:38 +01002529 sta->supp_rates[rx_status->band] =
2530 sdata->u.sta.supp_rates_bits[rx_status->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07002531 }
Johannes Berg8318d782008-01-24 19:38:38 +01002532 if (sta->supp_rates[rx_status->band] != prev_rates) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002533 printk(KERN_DEBUG "%s: updated supp_rates set for "
Johannes Berg8318d782008-01-24 19:38:38 +01002534 "%s based on beacon info (0x%llx & 0x%llx -> "
2535 "0x%llx)\n",
2536 dev->name, print_mac(mac, sta->addr),
2537 (unsigned long long) prev_rates,
2538 (unsigned long long) supp_rates,
2539 (unsigned long long) sta->supp_rates[rx_status->band]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002540 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002541 }
2542
Johannes Bergd0709a62008-02-25 16:27:46 +01002543 rcu_read_unlock();
2544
Jiri Bencf0706e82007-05-05 11:45:53 -07002545 if (elems.ds_params && elems.ds_params_len == 1)
Johannes Berg8318d782008-01-24 19:38:38 +01002546 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07002547 else
Johannes Berg8318d782008-01-24 19:38:38 +01002548 freq = rx_status->freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07002549
Johannes Bergfab7d4a2008-03-16 18:42:44 +01002550 channel = ieee80211_get_channel(local->hw.wiphy, freq);
2551
2552 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
2553 return;
2554
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002555#ifdef CONFIG_MAC80211_MESH
2556 if (elems.mesh_config)
2557 bss = ieee80211_rx_mesh_bss_get(dev, elems.mesh_id,
2558 elems.mesh_id_len, elems.mesh_config, freq);
2559 else
2560#endif
2561 bss = ieee80211_rx_bss_get(dev, mgmt->bssid, freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04002562 elems.ssid, elems.ssid_len);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002563 if (!bss) {
2564#ifdef CONFIG_MAC80211_MESH
2565 if (elems.mesh_config)
2566 bss = ieee80211_rx_mesh_bss_add(dev, elems.mesh_id,
Luis Carlos Cobo05e5e882008-03-31 16:00:13 -07002567 elems.mesh_id_len, elems.mesh_config,
2568 elems.mesh_config_len, freq);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002569 else
2570#endif
2571 bss = ieee80211_rx_bss_add(dev, mgmt->bssid, freq,
2572 elems.ssid, elems.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002573 if (!bss)
2574 return;
2575 } else {
2576#if 0
2577 /* TODO: order by RSSI? */
2578 spin_lock_bh(&local->sta_bss_lock);
2579 list_move_tail(&bss->list, &local->sta_bss_list);
2580 spin_unlock_bh(&local->sta_bss_lock);
2581#endif
2582 }
2583
Daniel Drake56282212007-07-10 19:32:10 +02002584 /* save the ERP value so that it is available at association time */
2585 if (elems.erp_info && elems.erp_info_len >= 1) {
2586 bss->erp_value = elems.erp_info[0];
2587 bss->has_erp_value = 1;
2588 }
2589
Johannes Berg30b89b02008-04-16 17:43:20 +02002590 if (elems.ht_cap_elem &&
2591 (!bss->ht_ie || bss->ht_ie_len != elems.ht_cap_elem_len ||
2592 memcmp(bss->ht_ie, elems.ht_cap_elem, elems.ht_cap_elem_len))) {
2593 kfree(bss->ht_ie);
2594 bss->ht_ie = kmalloc(elems.ht_cap_elem_len + 2, GFP_ATOMIC);
2595 if (bss->ht_ie) {
2596 memcpy(bss->ht_ie, elems.ht_cap_elem - 2,
2597 elems.ht_cap_elem_len + 2);
2598 bss->ht_ie_len = elems.ht_cap_elem_len + 2;
2599 } else
2600 bss->ht_ie_len = 0;
2601 } else if (!elems.ht_cap_elem && bss->ht_ie) {
2602 kfree(bss->ht_ie);
2603 bss->ht_ie = NULL;
2604 bss->ht_ie_len = 0;
2605 }
2606
Jiri Bencf0706e82007-05-05 11:45:53 -07002607 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
2608 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
Jiri Bencf0706e82007-05-05 11:45:53 -07002609
2610 bss->supp_rates_len = 0;
2611 if (elems.supp_rates) {
2612 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2613 if (clen > elems.supp_rates_len)
2614 clen = elems.supp_rates_len;
2615 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
2616 clen);
2617 bss->supp_rates_len += clen;
2618 }
2619 if (elems.ext_supp_rates) {
2620 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
2621 if (clen > elems.ext_supp_rates_len)
2622 clen = elems.ext_supp_rates_len;
2623 memcpy(&bss->supp_rates[bss->supp_rates_len],
2624 elems.ext_supp_rates, clen);
2625 bss->supp_rates_len += clen;
2626 }
2627
Johannes Berg30b89b02008-04-16 17:43:20 +02002628 bss->band = rx_status->band;
2629
2630 bss->timestamp = beacon_timestamp;
2631 bss->last_update = jiffies;
Johannes Berg30b89b02008-04-16 17:43:20 +02002632 bss->signal = rx_status->signal;
2633 bss->noise = rx_status->noise;
Bruno Randolf566bfe52008-05-08 19:15:40 +02002634 bss->qual = rx_status->qual;
Johannes Berg30b89b02008-04-16 17:43:20 +02002635 if (!beacon && !bss->probe_resp)
2636 bss->probe_resp = true;
2637
2638 /*
2639 * In STA mode, the remaining parameters should not be overridden
2640 * by beacons because they're not necessarily accurate there.
2641 */
2642 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
2643 bss->probe_resp && beacon) {
2644 ieee80211_rx_bss_put(dev, bss);
2645 return;
2646 }
2647
Jiri Bencf0706e82007-05-05 11:45:53 -07002648 if (elems.wpa &&
2649 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
2650 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
2651 kfree(bss->wpa_ie);
2652 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
2653 if (bss->wpa_ie) {
2654 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
2655 bss->wpa_ie_len = elems.wpa_len + 2;
2656 } else
2657 bss->wpa_ie_len = 0;
2658 } else if (!elems.wpa && bss->wpa_ie) {
2659 kfree(bss->wpa_ie);
2660 bss->wpa_ie = NULL;
2661 bss->wpa_ie_len = 0;
2662 }
2663
2664 if (elems.rsn &&
2665 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
2666 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
2667 kfree(bss->rsn_ie);
2668 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
2669 if (bss->rsn_ie) {
2670 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
2671 bss->rsn_ie_len = elems.rsn_len + 2;
2672 } else
2673 bss->rsn_ie_len = 0;
2674 } else if (!elems.rsn && bss->rsn_ie) {
2675 kfree(bss->rsn_ie);
2676 bss->rsn_ie = NULL;
2677 bss->rsn_ie_len = 0;
2678 }
2679
Johannes Berg30b89b02008-04-16 17:43:20 +02002680 /*
2681 * Cf.
2682 * http://www.wipo.int/pctdb/en/wo.jsp?wo=2007047181&IA=WO2007047181&DISPLAY=DESC
2683 *
2684 * quoting:
2685 *
2686 * In particular, "Wi-Fi CERTIFIED for WMM - Support for Multimedia
2687 * Applications with Quality of Service in Wi-Fi Networks," Wi- Fi
2688 * Alliance (September 1, 2004) is incorporated by reference herein.
2689 * The inclusion of the WMM Parameters in probe responses and
2690 * association responses is mandatory for WMM enabled networks. The
2691 * inclusion of the WMM Parameters in beacons, however, is optional.
2692 */
2693
Jiri Bencf0706e82007-05-05 11:45:53 -07002694 if (elems.wmm_param &&
2695 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
2696 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
2697 kfree(bss->wmm_ie);
2698 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
2699 if (bss->wmm_ie) {
2700 memcpy(bss->wmm_ie, elems.wmm_param - 2,
2701 elems.wmm_param_len + 2);
2702 bss->wmm_ie_len = elems.wmm_param_len + 2;
2703 } else
2704 bss->wmm_ie_len = 0;
Abhijeet Kolekara46f0252008-04-16 14:02:04 -07002705 } else if (elems.wmm_info &&
2706 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_info_len ||
2707 memcmp(bss->wmm_ie, elems.wmm_info, elems.wmm_info_len))) {
2708 /* As for certain AP's Fifth bit is not set in WMM IE in
2709 * beacon frames.So while parsing the beacon frame the
2710 * wmm_info structure is used instead of wmm_param.
2711 * wmm_info structure was never used to set bss->wmm_ie.
2712 * This code fixes this problem by copying the WME
2713 * information from wmm_info to bss->wmm_ie and enabling
2714 * n-band association.
2715 */
2716 kfree(bss->wmm_ie);
2717 bss->wmm_ie = kmalloc(elems.wmm_info_len + 2, GFP_ATOMIC);
2718 if (bss->wmm_ie) {
2719 memcpy(bss->wmm_ie, elems.wmm_info - 2,
2720 elems.wmm_info_len + 2);
2721 bss->wmm_ie_len = elems.wmm_info_len + 2;
2722 } else
2723 bss->wmm_ie_len = 0;
2724 } else if (!elems.wmm_param && !elems.wmm_info && bss->wmm_ie) {
Jiri Bencf0706e82007-05-05 11:45:53 -07002725 kfree(bss->wmm_ie);
2726 bss->wmm_ie = NULL;
2727 bss->wmm_ie_len = 0;
2728 }
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002729
2730 /* check if we need to merge IBSS */
2731 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS && beacon &&
2732 !local->sta_sw_scanning && !local->sta_hw_scanning &&
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002733 bss->capability & WLAN_CAPABILITY_IBSS &&
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002734 bss->freq == local->oper_channel->center_freq &&
2735 elems.ssid_len == sdata->u.sta.ssid_len &&
2736 memcmp(elems.ssid, sdata->u.sta.ssid, sdata->u.sta.ssid_len) == 0) {
2737 if (rx_status->flag & RX_FLAG_TSFT) {
2738 /* in order for correct IBSS merging we need mactime
2739 *
2740 * since mactime is defined as the time the first data
2741 * symbol of the frame hits the PHY, and the timestamp
2742 * of the beacon is defined as "the time that the data
2743 * symbol containing the first bit of the timestamp is
2744 * transmitted to the PHY plus the transmitting STA’s
2745 * delays through its local PHY from the MAC-PHY
2746 * interface to its interface with the WM"
2747 * (802.11 11.1.2) - equals the time this bit arrives at
2748 * the receiver - we have to take into account the
2749 * offset between the two.
2750 * e.g: at 1 MBit that means mactime is 192 usec earlier
2751 * (=24 bytes * 8 usecs/byte) than the beacon timestamp.
2752 */
2753 int rate = local->hw.wiphy->bands[rx_status->band]->
2754 bitrates[rx_status->rate_idx].bitrate;
2755 rx_timestamp = rx_status->mactime + (24 * 8 * 10 / rate);
2756 } else if (local && local->ops && local->ops->get_tsf)
2757 /* second best option: get current TSF */
2758 rx_timestamp = local->ops->get_tsf(local_to_hw(local));
2759 else
2760 /* can't merge without knowing the TSF */
2761 rx_timestamp = -1LLU;
2762#ifdef CONFIG_MAC80211_IBSS_DEBUG
2763 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
2764 "%s TSF=0x%llx BCN=0x%llx diff=%lld @%lu\n",
2765 print_mac(mac, mgmt->sa),
2766 print_mac(mac2, mgmt->bssid),
2767 (unsigned long long)rx_timestamp,
2768 (unsigned long long)beacon_timestamp,
2769 (unsigned long long)(rx_timestamp - beacon_timestamp),
2770 jiffies);
2771#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2772 if (beacon_timestamp > rx_timestamp) {
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002773#ifndef CONFIG_MAC80211_IBSS_DEBUG
Pavel Roskind97cf012008-02-21 11:33:58 -05002774 if (net_ratelimit())
Johannes Bergfba4a1e2008-02-21 11:08:33 +01002775#endif
Bruno Randolf9d9bf772008-02-18 11:21:36 +09002776 printk(KERN_DEBUG "%s: beacon TSF higher than "
2777 "local TSF - IBSS merge with BSSID %s\n",
2778 dev->name, print_mac(mac, mgmt->bssid));
2779 ieee80211_sta_join_ibss(dev, &sdata->u.sta, bss);
2780 ieee80211_ibss_add_sta(dev, NULL,
2781 mgmt->bssid, mgmt->sa);
2782 }
2783 }
2784
Jiri Bencf0706e82007-05-05 11:45:53 -07002785 ieee80211_rx_bss_put(dev, bss);
2786}
2787
2788
2789static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
2790 struct ieee80211_mgmt *mgmt,
2791 size_t len,
2792 struct ieee80211_rx_status *rx_status)
2793{
2794 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
2795}
2796
2797
2798static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
2799 struct ieee80211_mgmt *mgmt,
2800 size_t len,
2801 struct ieee80211_rx_status *rx_status)
2802{
Jiri Bencf0706e82007-05-05 11:45:53 -07002803 struct ieee80211_sub_if_data *sdata;
2804 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07002805 size_t baselen;
2806 struct ieee802_11_elems elems;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002807 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2808 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg471b3ef2007-12-28 14:32:58 +01002809 u32 changed = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07002810
2811 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
2812
2813 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01002814 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07002815 return;
2816 ifsta = &sdata->u.sta;
2817
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002818 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002819 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
2820 return;
2821
2822 /* Process beacon from the current BSS */
2823 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
2824 if (baselen > len)
2825 return;
2826
John W. Linville67a4cce2007-10-12 16:40:37 -04002827 ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen, &elems);
Jiri Bencf0706e82007-05-05 11:45:53 -07002828
Johannes Berg3434fbd2008-05-03 00:59:37 +02002829 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
2830 elems.wmm_param_len);
Reinette Chatred18ef292008-04-09 16:56:15 -07002831
2832 /* Do not send changes to driver if we are scanning. This removes
2833 * requirement that driver's bss_info_changed function needs to be
2834 * atomic. */
2835 if (local->sta_sw_scanning || local->sta_hw_scanning)
2836 return;
2837
Daniel Drake56282212007-07-10 19:32:10 +02002838 if (elems.erp_info && elems.erp_info_len >= 1)
Johannes Berg471b3ef2007-12-28 14:32:58 +01002839 changed |= ieee80211_handle_erp_ie(sdata, elems.erp_info[0]);
John W. Linville50c4afb2008-04-15 14:09:27 -04002840 else {
2841 u16 capab = le16_to_cpu(mgmt->u.beacon.capab_info);
2842 changed |= ieee80211_handle_protect_preamb(sdata, false,
2843 (capab & WLAN_CAPABILITY_SHORT_PREAMBLE) != 0);
2844 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002845
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002846 if (elems.ht_cap_elem && elems.ht_info_elem &&
Tomas Winkler38668c02008-03-28 16:33:32 -07002847 elems.wmm_param && conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002848 struct ieee80211_ht_bss_info bss_info;
2849
2850 ieee80211_ht_addt_info_ie_to_ht_bss_info(
2851 (struct ieee80211_ht_addt_info *)
2852 elems.ht_info_elem, &bss_info);
Tomas Winkler38668c02008-03-28 16:33:32 -07002853 changed |= ieee80211_handle_ht(local, 1, &conf->ht_conf,
2854 &bss_info);
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02002855 }
2856
Johannes Berg471b3ef2007-12-28 14:32:58 +01002857 ieee80211_bss_info_change_notify(sdata, changed);
Jiri Bencf0706e82007-05-05 11:45:53 -07002858}
2859
2860
2861static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
2862 struct ieee80211_if_sta *ifsta,
2863 struct ieee80211_mgmt *mgmt,
2864 size_t len,
2865 struct ieee80211_rx_status *rx_status)
2866{
2867 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2868 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2869 int tx_last_beacon;
2870 struct sk_buff *skb;
2871 struct ieee80211_mgmt *resp;
2872 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07002873 DECLARE_MAC_BUF(mac);
2874#ifdef CONFIG_MAC80211_IBSS_DEBUG
2875 DECLARE_MAC_BUF(mac2);
2876 DECLARE_MAC_BUF(mac3);
2877#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07002878
Johannes Berg51fb61e2007-12-19 01:31:27 +01002879 if (sdata->vif.type != IEEE80211_IF_TYPE_IBSS ||
Jiri Bencf0706e82007-05-05 11:45:53 -07002880 ifsta->state != IEEE80211_IBSS_JOINED ||
2881 len < 24 + 2 || !ifsta->probe_resp)
2882 return;
2883
2884 if (local->ops->tx_last_beacon)
2885 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
2886 else
2887 tx_last_beacon = 1;
2888
2889#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002890 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
2891 "%s (tx_last_beacon=%d)\n",
2892 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
2893 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e82007-05-05 11:45:53 -07002894#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2895
2896 if (!tx_last_beacon)
2897 return;
2898
2899 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
2900 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
2901 return;
2902
2903 end = ((u8 *) mgmt) + len;
2904 pos = mgmt->u.probe_req.variable;
2905 if (pos[0] != WLAN_EID_SSID ||
2906 pos + 2 + pos[1] > end) {
2907 if (net_ratelimit()) {
2908 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07002909 "from %s\n",
2910 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e82007-05-05 11:45:53 -07002911 }
2912 return;
2913 }
2914 if (pos[1] != 0 &&
2915 (pos[1] != ifsta->ssid_len ||
2916 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
2917 /* Ignore ProbeReq for foreign SSID */
2918 return;
2919 }
2920
2921 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02002922 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e82007-05-05 11:45:53 -07002923 if (!skb)
2924 return;
2925
2926 resp = (struct ieee80211_mgmt *) skb->data;
2927 memcpy(resp->da, mgmt->sa, ETH_ALEN);
2928#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002929 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
2930 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e82007-05-05 11:45:53 -07002931#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2932 ieee80211_sta_tx(dev, skb, 0);
2933}
2934
Johannes Berg4e20cb22007-12-19 01:31:24 +01002935static void ieee80211_rx_mgmt_action(struct net_device *dev,
2936 struct ieee80211_if_sta *ifsta,
2937 struct ieee80211_mgmt *mgmt,
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002938 size_t len,
2939 struct ieee80211_rx_status *rx_status)
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002940{
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002941 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002942
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002943 if (len < IEEE80211_MIN_ACTION_SIZE)
2944 return;
2945
2946 switch (mgmt->u.action.category) {
2947 case WLAN_CATEGORY_BACK:
2948 switch (mgmt->u.action.u.addba_req.action_code) {
2949 case WLAN_ACTION_ADDBA_REQ:
2950 if (len < (IEEE80211_MIN_ACTION_SIZE +
2951 sizeof(mgmt->u.action.u.addba_req)))
2952 break;
2953 ieee80211_sta_process_addba_request(dev, mgmt, len);
2954 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02002955 case WLAN_ACTION_ADDBA_RESP:
2956 if (len < (IEEE80211_MIN_ACTION_SIZE +
2957 sizeof(mgmt->u.action.u.addba_resp)))
2958 break;
2959 ieee80211_sta_process_addba_resp(dev, mgmt, len);
2960 break;
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002961 case WLAN_ACTION_DELBA:
2962 if (len < (IEEE80211_MIN_ACTION_SIZE +
2963 sizeof(mgmt->u.action.u.delba)))
2964 break;
2965 ieee80211_sta_process_delba(dev, mgmt, len);
2966 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002967 default:
2968 if (net_ratelimit())
Ron Rindjunsky688b88a2007-12-25 17:00:37 +02002969 printk(KERN_DEBUG "%s: Rx unknown A-MPDU action\n",
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002970 dev->name);
2971 break;
2972 }
2973 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002974 case PLINK_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002975 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002976 mesh_rx_plink_frame(dev, mgmt, len, rx_status);
2977 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002978 case MESH_PATH_SEL_CATEGORY:
Johannes Berg902acc72008-02-23 15:17:19 +01002979 if (ieee80211_vif_is_mesh(&sdata->vif))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002980 mesh_rx_path_sel_frame(dev, mgmt, len);
2981 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002982 default:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01002983 if (net_ratelimit())
2984 printk(KERN_DEBUG "%s: Rx unknown action frame - "
2985 "category=%d\n", dev->name, mgmt->u.action.category);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02002986 break;
2987 }
2988}
Jiri Bencf0706e82007-05-05 11:45:53 -07002989
2990void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
2991 struct ieee80211_rx_status *rx_status)
2992{
2993 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2994 struct ieee80211_sub_if_data *sdata;
2995 struct ieee80211_if_sta *ifsta;
2996 struct ieee80211_mgmt *mgmt;
2997 u16 fc;
2998
2999 if (skb->len < 24)
3000 goto fail;
3001
3002 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3003 ifsta = &sdata->u.sta;
3004
3005 mgmt = (struct ieee80211_mgmt *) skb->data;
3006 fc = le16_to_cpu(mgmt->frame_control);
3007
3008 switch (fc & IEEE80211_FCTL_STYPE) {
3009 case IEEE80211_STYPE_PROBE_REQ:
3010 case IEEE80211_STYPE_PROBE_RESP:
3011 case IEEE80211_STYPE_BEACON:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003012 case IEEE80211_STYPE_ACTION:
Jiri Bencf0706e82007-05-05 11:45:53 -07003013 memcpy(skb->cb, rx_status, sizeof(*rx_status));
3014 case IEEE80211_STYPE_AUTH:
3015 case IEEE80211_STYPE_ASSOC_RESP:
3016 case IEEE80211_STYPE_REASSOC_RESP:
3017 case IEEE80211_STYPE_DEAUTH:
3018 case IEEE80211_STYPE_DISASSOC:
3019 skb_queue_tail(&ifsta->skb_queue, skb);
3020 queue_work(local->hw.workqueue, &ifsta->work);
3021 return;
3022 default:
3023 printk(KERN_DEBUG "%s: received unknown management frame - "
3024 "stype=%d\n", dev->name,
3025 (fc & IEEE80211_FCTL_STYPE) >> 4);
3026 break;
3027 }
3028
3029 fail:
3030 kfree_skb(skb);
3031}
3032
3033
3034static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
3035 struct sk_buff *skb)
3036{
3037 struct ieee80211_rx_status *rx_status;
3038 struct ieee80211_sub_if_data *sdata;
3039 struct ieee80211_if_sta *ifsta;
3040 struct ieee80211_mgmt *mgmt;
3041 u16 fc;
3042
3043 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3044 ifsta = &sdata->u.sta;
3045
3046 rx_status = (struct ieee80211_rx_status *) skb->cb;
3047 mgmt = (struct ieee80211_mgmt *) skb->data;
3048 fc = le16_to_cpu(mgmt->frame_control);
3049
3050 switch (fc & IEEE80211_FCTL_STYPE) {
3051 case IEEE80211_STYPE_PROBE_REQ:
3052 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
3053 rx_status);
3054 break;
3055 case IEEE80211_STYPE_PROBE_RESP:
3056 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
3057 break;
3058 case IEEE80211_STYPE_BEACON:
3059 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
3060 break;
3061 case IEEE80211_STYPE_AUTH:
3062 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
3063 break;
3064 case IEEE80211_STYPE_ASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003065 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003066 break;
3067 case IEEE80211_STYPE_REASSOC_RESP:
Johannes Berg471b3ef2007-12-28 14:32:58 +01003068 ieee80211_rx_mgmt_assoc_resp(sdata, ifsta, mgmt, skb->len, 1);
Jiri Bencf0706e82007-05-05 11:45:53 -07003069 break;
3070 case IEEE80211_STYPE_DEAUTH:
3071 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
3072 break;
3073 case IEEE80211_STYPE_DISASSOC:
3074 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
3075 break;
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003076 case IEEE80211_STYPE_ACTION:
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003077 ieee80211_rx_mgmt_action(dev, ifsta, mgmt, skb->len, rx_status);
Ron Rindjunsky9f985b02007-11-26 16:14:32 +02003078 break;
Jiri Bencf0706e82007-05-05 11:45:53 -07003079 }
3080
3081 kfree_skb(skb);
3082}
3083
3084
Johannes Berg9ae54c82008-01-31 19:48:20 +01003085ieee80211_rx_result
Zhu Yiece8edd2007-11-22 10:53:21 +08003086ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
3087 struct ieee80211_rx_status *rx_status)
Jiri Bencf0706e82007-05-05 11:45:53 -07003088{
3089 struct ieee80211_mgmt *mgmt;
3090 u16 fc;
3091
Zhu Yiece8edd2007-11-22 10:53:21 +08003092 if (skb->len < 2)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003093 return RX_DROP_UNUSABLE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003094
3095 mgmt = (struct ieee80211_mgmt *) skb->data;
3096 fc = le16_to_cpu(mgmt->frame_control);
3097
Zhu Yiece8edd2007-11-22 10:53:21 +08003098 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
Johannes Berg9ae54c82008-01-31 19:48:20 +01003099 return RX_CONTINUE;
Zhu Yiece8edd2007-11-22 10:53:21 +08003100
3101 if (skb->len < 24)
Johannes Berge4c26ad2008-01-31 19:48:21 +01003102 return RX_DROP_MONITOR;
Zhu Yiece8edd2007-11-22 10:53:21 +08003103
Jiri Bencf0706e82007-05-05 11:45:53 -07003104 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
3105 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
3106 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
3107 skb->len, rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003108 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003109 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003110 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
3111 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
3112 rx_status);
Zhu Yiece8edd2007-11-22 10:53:21 +08003113 dev_kfree_skb(skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +01003114 return RX_QUEUED;
Jiri Bencf0706e82007-05-05 11:45:53 -07003115 }
3116 }
Johannes Berg9ae54c82008-01-31 19:48:20 +01003117 return RX_CONTINUE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003118}
3119
3120
3121static int ieee80211_sta_active_ibss(struct net_device *dev)
3122{
3123 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3124 int active = 0;
3125 struct sta_info *sta;
Johannes Bergd0709a62008-02-25 16:27:46 +01003126 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003127
Johannes Bergd0709a62008-02-25 16:27:46 +01003128 rcu_read_lock();
3129
3130 list_for_each_entry_rcu(sta, &local->sta_list, list) {
3131 if (sta->sdata == sdata &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003132 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
3133 jiffies)) {
3134 active++;
3135 break;
3136 }
3137 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003138
3139 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003140
3141 return active;
3142}
3143
3144
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003145static void ieee80211_sta_expire(struct net_device *dev, unsigned long exp_time)
Jiri Bencf0706e82007-05-05 11:45:53 -07003146{
3147 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3148 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02003149 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07003150 DECLARE_MAC_BUF(mac);
Johannes Bergd0709a62008-02-25 16:27:46 +01003151 unsigned long flags;
Jiri Bencf0706e82007-05-05 11:45:53 -07003152
Johannes Bergd0709a62008-02-25 16:27:46 +01003153 spin_lock_irqsave(&local->sta_lock, flags);
Jiri Bencf0706e82007-05-05 11:45:53 -07003154 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003155 if (time_after(jiffies, sta->last_rx + exp_time)) {
Joe Perches0795af52007-10-03 17:59:30 -07003156 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
3157 dev->name, print_mac(mac, sta->addr));
Luis Carlos Cobocb585bc2008-03-31 15:21:23 -07003158 __sta_info_unlink(&sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01003159 if (sta)
3160 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07003161 }
Johannes Bergd0709a62008-02-25 16:27:46 +01003162 spin_unlock_irqrestore(&local->sta_lock, flags);
Michael Wube8755e2007-07-27 15:43:23 +02003163
Johannes Bergd0709a62008-02-25 16:27:46 +01003164 list_for_each_entry_safe(sta, tmp, &tmp_list, list)
3165 sta_info_destroy(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -07003166}
3167
3168
3169static void ieee80211_sta_merge_ibss(struct net_device *dev,
3170 struct ieee80211_if_sta *ifsta)
3171{
3172 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
3173
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003174 ieee80211_sta_expire(dev, IEEE80211_IBSS_INACTIVITY_LIMIT);
Jiri Bencf0706e82007-05-05 11:45:53 -07003175 if (ieee80211_sta_active_ibss(dev))
3176 return;
3177
3178 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
3179 "IBSS networks with same SSID (merge)\n", dev->name);
3180 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
3181}
3182
3183
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003184#ifdef CONFIG_MAC80211_MESH
3185static void ieee80211_mesh_housekeeping(struct net_device *dev,
3186 struct ieee80211_if_sta *ifsta)
3187{
3188 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3189 bool free_plinks;
3190
3191 ieee80211_sta_expire(dev, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
3192 mesh_path_expire(dev);
3193
3194 free_plinks = mesh_plink_availables(sdata);
3195 if (free_plinks != sdata->u.sta.accepting_plinks)
3196 ieee80211_if_config_beacon(dev);
3197
3198 mod_timer(&ifsta->timer, jiffies +
3199 IEEE80211_MESH_HOUSEKEEPING_INTERVAL);
3200}
3201
3202
3203void ieee80211_start_mesh(struct net_device *dev)
3204{
3205 struct ieee80211_if_sta *ifsta;
3206 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3207 ifsta = &sdata->u.sta;
3208 ifsta->state = IEEE80211_MESH_UP;
3209 ieee80211_sta_timer((unsigned long)sdata);
3210}
3211#endif
3212
3213
Jiri Bencf0706e82007-05-05 11:45:53 -07003214void ieee80211_sta_timer(unsigned long data)
3215{
3216 struct ieee80211_sub_if_data *sdata =
3217 (struct ieee80211_sub_if_data *) data;
3218 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3219 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
3220
3221 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3222 queue_work(local->hw.workqueue, &ifsta->work);
3223}
3224
Jiri Bencf0706e82007-05-05 11:45:53 -07003225void ieee80211_sta_work(struct work_struct *work)
3226{
3227 struct ieee80211_sub_if_data *sdata =
3228 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
3229 struct net_device *dev = sdata->dev;
3230 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3231 struct ieee80211_if_sta *ifsta;
3232 struct sk_buff *skb;
3233
3234 if (!netif_running(dev))
3235 return;
3236
Zhu Yiece8edd2007-11-22 10:53:21 +08003237 if (local->sta_sw_scanning || local->sta_hw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003238 return;
3239
Johannes Berg51fb61e2007-12-19 01:31:27 +01003240 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003241 sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
3242 sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003243 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
Johannes Berg51fb61e2007-12-19 01:31:27 +01003244 "(type=%d)\n", dev->name, sdata->vif.type);
Jiri Bencf0706e82007-05-05 11:45:53 -07003245 return;
3246 }
3247 ifsta = &sdata->u.sta;
3248
3249 while ((skb = skb_dequeue(&ifsta->skb_queue)))
3250 ieee80211_sta_rx_queued_mgmt(dev, skb);
3251
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003252#ifdef CONFIG_MAC80211_MESH
Johannes Berg902acc72008-02-23 15:17:19 +01003253 if (ifsta->preq_queue_len &&
3254 time_after(jiffies,
3255 ifsta->last_preq + msecs_to_jiffies(ifsta->mshcfg.dot11MeshHWMPpreqMinInterval)))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003256 mesh_path_start_discovery(dev);
3257#endif
3258
Jiri Bencf0706e82007-05-05 11:45:53 -07003259 if (ifsta->state != IEEE80211_AUTHENTICATE &&
3260 ifsta->state != IEEE80211_ASSOCIATE &&
3261 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003262 if (ifsta->scan_ssid_len)
3263 ieee80211_sta_start_scan(dev, ifsta->scan_ssid, ifsta->scan_ssid_len);
3264 else
3265 ieee80211_sta_start_scan(dev, NULL, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003266 return;
3267 }
3268
3269 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
3270 if (ieee80211_sta_config_auth(dev, ifsta))
3271 return;
3272 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
3273 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
3274 return;
3275
3276 switch (ifsta->state) {
3277 case IEEE80211_DISABLED:
3278 break;
3279 case IEEE80211_AUTHENTICATE:
3280 ieee80211_authenticate(dev, ifsta);
3281 break;
3282 case IEEE80211_ASSOCIATE:
3283 ieee80211_associate(dev, ifsta);
3284 break;
3285 case IEEE80211_ASSOCIATED:
3286 ieee80211_associated(dev, ifsta);
3287 break;
3288 case IEEE80211_IBSS_SEARCH:
3289 ieee80211_sta_find_ibss(dev, ifsta);
3290 break;
3291 case IEEE80211_IBSS_JOINED:
3292 ieee80211_sta_merge_ibss(dev, ifsta);
3293 break;
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003294#ifdef CONFIG_MAC80211_MESH
3295 case IEEE80211_MESH_UP:
3296 ieee80211_mesh_housekeeping(dev, ifsta);
3297 break;
3298#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07003299 default:
3300 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
3301 ifsta->state);
3302 break;
3303 }
3304
3305 if (ieee80211_privacy_mismatch(dev, ifsta)) {
3306 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
3307 "mixed-cell disabled - disassociate\n", dev->name);
3308
3309 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
3310 ieee80211_set_disassoc(dev, ifsta, 0);
3311 }
3312}
3313
3314
3315static void ieee80211_sta_reset_auth(struct net_device *dev,
3316 struct ieee80211_if_sta *ifsta)
3317{
3318 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3319
3320 if (local->ops->reset_tsf) {
3321 /* Reset own TSF to allow time synchronization work. */
3322 local->ops->reset_tsf(local_to_hw(local));
3323 }
3324
3325 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
3326
3327
3328 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
3329 ifsta->auth_alg = WLAN_AUTH_OPEN;
3330 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
3331 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
3332 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
3333 ifsta->auth_alg = WLAN_AUTH_LEAP;
3334 else
3335 ifsta->auth_alg = WLAN_AUTH_OPEN;
3336 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
3337 ifsta->auth_alg);
3338 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003339 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
3340 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003341 netif_carrier_off(dev);
3342}
3343
3344
3345void ieee80211_sta_req_auth(struct net_device *dev,
3346 struct ieee80211_if_sta *ifsta)
3347{
3348 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3349 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3350
Johannes Berg51fb61e2007-12-19 01:31:27 +01003351 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003352 return;
3353
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003354 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
3355 IEEE80211_STA_AUTO_BSSID_SEL)) &&
3356 (ifsta->flags & (IEEE80211_STA_SSID_SET |
3357 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003358 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3359 queue_work(local->hw.workqueue, &ifsta->work);
3360 }
3361}
3362
3363static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
3364 const char *ssid, int ssid_len)
3365{
3366 int tmp, hidden_ssid;
3367
Michael Wu48225702007-10-19 17:14:36 -04003368 if (ssid_len == ifsta->ssid_len &&
3369 !memcmp(ifsta->ssid, ssid, ssid_len))
Jiri Bencf0706e82007-05-05 11:45:53 -07003370 return 1;
3371
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003372 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e82007-05-05 11:45:53 -07003373 return 0;
3374
3375 hidden_ssid = 1;
3376 tmp = ssid_len;
3377 while (tmp--) {
3378 if (ssid[tmp] != '\0') {
3379 hidden_ssid = 0;
3380 break;
3381 }
3382 }
3383
3384 if (hidden_ssid && ifsta->ssid_len == ssid_len)
3385 return 1;
3386
3387 if (ssid_len == 1 && ssid[0] == ' ')
3388 return 1;
3389
3390 return 0;
3391}
3392
3393static int ieee80211_sta_config_auth(struct net_device *dev,
3394 struct ieee80211_if_sta *ifsta)
3395{
3396 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3397 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3398 struct ieee80211_sta_bss *bss, *selected = NULL;
3399 int top_rssi = 0, freq;
3400
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003401 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
3402 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003403 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003404 ieee80211_sta_reset_auth(dev, ifsta);
3405 return 0;
3406 }
3407
3408 spin_lock_bh(&local->sta_bss_lock);
Johannes Berg8318d782008-01-24 19:38:38 +01003409 freq = local->oper_channel->center_freq;
Jiri Bencf0706e82007-05-05 11:45:53 -07003410 list_for_each_entry(bss, &local->sta_bss_list, list) {
3411 if (!(bss->capability & WLAN_CAPABILITY_ESS))
3412 continue;
3413
3414 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
3415 !!sdata->default_key)
3416 continue;
3417
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003418 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
3419 bss->freq != freq)
Jiri Bencf0706e82007-05-05 11:45:53 -07003420 continue;
3421
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003422 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003423 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
3424 continue;
3425
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003426 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e82007-05-05 11:45:53 -07003427 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
3428 continue;
3429
Bruno Randolf566bfe52008-05-08 19:15:40 +02003430 if (!selected || top_rssi < bss->signal) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003431 selected = bss;
Bruno Randolf566bfe52008-05-08 19:15:40 +02003432 top_rssi = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07003433 }
3434 }
3435 if (selected)
3436 atomic_inc(&selected->users);
3437 spin_unlock_bh(&local->sta_bss_lock);
3438
3439 if (selected) {
Johannes Berg8318d782008-01-24 19:38:38 +01003440 ieee80211_set_freq(local, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003441 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e82007-05-05 11:45:53 -07003442 ieee80211_sta_set_ssid(dev, selected->ssid,
3443 selected->ssid_len);
3444 ieee80211_sta_set_bssid(dev, selected->bssid);
Vladimir Koutnye2839d82008-03-18 21:14:07 +01003445 ieee80211_sta_def_wmm_params(dev, selected, 0);
Jiri Bencf0706e82007-05-05 11:45:53 -07003446 ieee80211_rx_bss_put(dev, selected);
3447 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07003448 ieee80211_sta_reset_auth(dev, ifsta);
3449 return 0;
3450 } else {
3451 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003452 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04003453 ieee80211_sta_start_scan(dev, NULL, 0);
3454 else
3455 ieee80211_sta_start_scan(dev, ifsta->ssid,
3456 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003457 ifsta->state = IEEE80211_AUTHENTICATE;
3458 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
3459 } else
3460 ifsta->state = IEEE80211_DISABLED;
3461 }
Jiri Bencf0706e82007-05-05 11:45:53 -07003462 return -1;
3463}
3464
Jiri Bencf0706e82007-05-05 11:45:53 -07003465
3466static int ieee80211_sta_create_ibss(struct net_device *dev,
3467 struct ieee80211_if_sta *ifsta)
3468{
3469 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3470 struct ieee80211_sta_bss *bss;
John W. Linvillecffdd302007-10-05 14:23:27 -04003471 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003472 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003473 u8 bssid[ETH_ALEN], *pos;
3474 int i;
Joe Perches0795af52007-10-03 17:59:30 -07003475 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07003476
3477#if 0
3478 /* Easier testing, use fixed BSSID. */
3479 memset(bssid, 0xfe, ETH_ALEN);
3480#else
3481 /* Generate random, not broadcast, locally administered BSSID. Mix in
3482 * own MAC address to make sure that devices that do not have proper
3483 * random number generator get different BSSID. */
3484 get_random_bytes(bssid, ETH_ALEN);
3485 for (i = 0; i < ETH_ALEN; i++)
3486 bssid[i] ^= dev->dev_addr[i];
3487 bssid[0] &= ~0x01;
3488 bssid[0] |= 0x02;
3489#endif
3490
Joe Perches0795af52007-10-03 17:59:30 -07003491 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
3492 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003493
Johannes Berg8318d782008-01-24 19:38:38 +01003494 bss = ieee80211_rx_bss_add(dev, bssid,
3495 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003496 sdata->u.sta.ssid, sdata->u.sta.ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003497 if (!bss)
3498 return -ENOMEM;
3499
Johannes Berg8318d782008-01-24 19:38:38 +01003500 bss->band = local->hw.conf.channel->band;
3501 sband = local->hw.wiphy->bands[bss->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07003502
3503 if (local->hw.conf.beacon_int == 0)
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003504 local->hw.conf.beacon_int = 10000;
Jiri Bencf0706e82007-05-05 11:45:53 -07003505 bss->beacon_int = local->hw.conf.beacon_int;
Jiri Bencf0706e82007-05-05 11:45:53 -07003506 bss->last_update = jiffies;
3507 bss->capability = WLAN_CAPABILITY_IBSS;
Johannes Berg988c0f72008-04-17 19:21:22 +02003508
3509 if (sdata->default_key)
Jiri Bencf0706e82007-05-05 11:45:53 -07003510 bss->capability |= WLAN_CAPABILITY_PRIVACY;
Johannes Berg988c0f72008-04-17 19:21:22 +02003511 else
Jiri Bencf0706e82007-05-05 11:45:53 -07003512 sdata->drop_unencrypted = 0;
Johannes Berg988c0f72008-04-17 19:21:22 +02003513
Johannes Berg8318d782008-01-24 19:38:38 +01003514 bss->supp_rates_len = sband->n_bitrates;
Jiri Bencf0706e82007-05-05 11:45:53 -07003515 pos = bss->supp_rates;
Johannes Berg8318d782008-01-24 19:38:38 +01003516 for (i = 0; i < sband->n_bitrates; i++) {
3517 int rate = sband->bitrates[i].bitrate;
Jiri Bencf0706e82007-05-05 11:45:53 -07003518 *pos++ = (u8) (rate / 5);
3519 }
3520
3521 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3522}
3523
3524
3525static int ieee80211_sta_find_ibss(struct net_device *dev,
3526 struct ieee80211_if_sta *ifsta)
3527{
3528 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3529 struct ieee80211_sta_bss *bss;
3530 int found = 0;
3531 u8 bssid[ETH_ALEN];
3532 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07003533 DECLARE_MAC_BUF(mac);
3534 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e82007-05-05 11:45:53 -07003535
3536 if (ifsta->ssid_len == 0)
3537 return -EINVAL;
3538
3539 active_ibss = ieee80211_sta_active_ibss(dev);
3540#ifdef CONFIG_MAC80211_IBSS_DEBUG
3541 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
3542 dev->name, active_ibss);
3543#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3544 spin_lock_bh(&local->sta_bss_lock);
3545 list_for_each_entry(bss, &local->sta_bss_list, list) {
3546 if (ifsta->ssid_len != bss->ssid_len ||
3547 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
3548 || !(bss->capability & WLAN_CAPABILITY_IBSS))
3549 continue;
3550#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003551 printk(KERN_DEBUG " bssid=%s found\n",
3552 print_mac(mac, bss->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003553#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3554 memcpy(bssid, bss->bssid, ETH_ALEN);
3555 found = 1;
3556 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
3557 break;
3558 }
3559 spin_unlock_bh(&local->sta_bss_lock);
3560
3561#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07003562 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
3563 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003564#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3565 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
Johannes Berg8318d782008-01-24 19:38:38 +01003566 (bss = ieee80211_rx_bss_get(dev, bssid,
3567 local->hw.conf.channel->center_freq,
John W. Linvillecffdd302007-10-05 14:23:27 -04003568 ifsta->ssid, ifsta->ssid_len))) {
Joe Perches0795af52007-10-03 17:59:30 -07003569 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e82007-05-05 11:45:53 -07003570 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07003571 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e82007-05-05 11:45:53 -07003572 return ieee80211_sta_join_ibss(dev, ifsta, bss);
3573 }
3574#ifdef CONFIG_MAC80211_IBSS_DEBUG
3575 printk(KERN_DEBUG " did not try to join ibss\n");
3576#endif /* CONFIG_MAC80211_IBSS_DEBUG */
3577
3578 /* Selected IBSS not found in current scan results - try to scan */
3579 if (ifsta->state == IEEE80211_IBSS_JOINED &&
3580 !ieee80211_sta_active_ibss(dev)) {
3581 mod_timer(&ifsta->timer, jiffies +
3582 IEEE80211_IBSS_MERGE_INTERVAL);
3583 } else if (time_after(jiffies, local->last_scan_completed +
3584 IEEE80211_SCAN_INTERVAL)) {
3585 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
3586 "join\n", dev->name);
3587 return ieee80211_sta_req_scan(dev, ifsta->ssid,
3588 ifsta->ssid_len);
3589 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
3590 int interval = IEEE80211_SCAN_INTERVAL;
3591
3592 if (time_after(jiffies, ifsta->ibss_join_req +
3593 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003594 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Johannes Berg8318d782008-01-24 19:38:38 +01003595 (!(local->oper_channel->flags &
3596 IEEE80211_CHAN_NO_IBSS)))
Jiri Bencf0706e82007-05-05 11:45:53 -07003597 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003598 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Johannes Berg8318d782008-01-24 19:38:38 +01003599 printk(KERN_DEBUG "%s: IBSS not allowed on"
3600 " %d MHz\n", dev->name,
3601 local->hw.conf.channel->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003602 }
3603
3604 /* No IBSS found - decrease scan interval and continue
3605 * scanning. */
3606 interval = IEEE80211_SCAN_INTERVAL_SLOW;
3607 }
3608
3609 ifsta->state = IEEE80211_IBSS_SEARCH;
3610 mod_timer(&ifsta->timer, jiffies + interval);
3611 return 0;
3612 }
3613
3614 return 0;
3615}
3616
3617
3618int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
3619{
Johannes Berg8318d782008-01-24 19:38:38 +01003620 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003621 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07003622
3623 if (len > IEEE80211_MAX_SSID_LEN)
3624 return -EINVAL;
3625
Jiri Bencf0706e82007-05-05 11:45:53 -07003626 ifsta = &sdata->u.sta;
3627
3628 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003629 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003630 memcpy(ifsta->ssid, ssid, len);
3631 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
3632 ifsta->ssid_len = len;
3633
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003634 if (len)
3635 ifsta->flags |= IEEE80211_STA_SSID_SET;
3636 else
3637 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
Johannes Berg51fb61e2007-12-19 01:31:27 +01003638 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003639 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003640 ifsta->ibss_join_req = jiffies;
3641 ifsta->state = IEEE80211_IBSS_SEARCH;
3642 return ieee80211_sta_find_ibss(dev, ifsta);
3643 }
3644 return 0;
3645}
3646
3647
3648int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
3649{
3650 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3651 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3652 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
3653 *len = ifsta->ssid_len;
3654 return 0;
3655}
3656
3657
3658int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
3659{
3660 struct ieee80211_sub_if_data *sdata;
3661 struct ieee80211_if_sta *ifsta;
3662 int res;
3663
3664 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3665 ifsta = &sdata->u.sta;
3666
3667 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
3668 memcpy(ifsta->bssid, bssid, ETH_ALEN);
3669 res = ieee80211_if_config(dev);
3670 if (res) {
3671 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
3672 "the low-level driver\n", dev->name);
3673 return res;
3674 }
3675 }
3676
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003677 if (is_valid_ether_addr(bssid))
3678 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e82007-05-05 11:45:53 -07003679 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003680 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
3681
Jiri Bencf0706e82007-05-05 11:45:53 -07003682 return 0;
3683}
3684
3685
3686static void ieee80211_send_nullfunc(struct ieee80211_local *local,
3687 struct ieee80211_sub_if_data *sdata,
3688 int powersave)
3689{
3690 struct sk_buff *skb;
3691 struct ieee80211_hdr *nullfunc;
3692 u16 fc;
3693
3694 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
3695 if (!skb) {
3696 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
3697 "frame\n", sdata->dev->name);
3698 return;
3699 }
3700 skb_reserve(skb, local->hw.extra_tx_headroom);
3701
3702 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
3703 memset(nullfunc, 0, 24);
3704 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
3705 IEEE80211_FCTL_TODS;
3706 if (powersave)
3707 fc |= IEEE80211_FCTL_PM;
3708 nullfunc->frame_control = cpu_to_le16(fc);
3709 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
3710 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
3711 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
3712
3713 ieee80211_sta_tx(sdata->dev, skb, 0);
3714}
3715
3716
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003717static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
3718{
3719 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
3720 ieee80211_vif_is_mesh(&sdata->vif))
3721 ieee80211_sta_timer((unsigned long)sdata);
3722}
3723
Jiri Bencf0706e82007-05-05 11:45:53 -07003724void ieee80211_scan_completed(struct ieee80211_hw *hw)
3725{
3726 struct ieee80211_local *local = hw_to_local(hw);
3727 struct net_device *dev = local->scan_dev;
3728 struct ieee80211_sub_if_data *sdata;
3729 union iwreq_data wrqu;
3730
3731 local->last_scan_completed = jiffies;
Zhu Yiece8edd2007-11-22 10:53:21 +08003732 memset(&wrqu, 0, sizeof(wrqu));
3733 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07003734
Zhu Yiece8edd2007-11-22 10:53:21 +08003735 if (local->sta_hw_scanning) {
3736 local->sta_hw_scanning = 0;
Mohamed Abbas675ef582008-03-20 08:14:29 -07003737 if (ieee80211_hw_config(local))
3738 printk(KERN_DEBUG "%s: failed to restore operational "
3739 "channel after scan\n", dev->name);
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003740 /* Restart STA timer for HW scan case */
3741 rcu_read_lock();
3742 list_for_each_entry_rcu(sdata, &local->interfaces, list)
3743 ieee80211_restart_sta_timer(sdata);
3744 rcu_read_unlock();
3745
Zhu Yiece8edd2007-11-22 10:53:21 +08003746 goto done;
3747 }
3748
3749 local->sta_sw_scanning = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003750 if (ieee80211_hw_config(local))
Bruno Randolf4b50e382007-11-16 17:04:01 +09003751 printk(KERN_DEBUG "%s: failed to restore operational "
Jiri Bencf0706e82007-05-05 11:45:53 -07003752 "channel after scan\n", dev->name);
3753
Johannes Berg4150c572007-09-17 01:29:23 -04003754
3755 netif_tx_lock_bh(local->mdev);
3756 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
3757 local->ops->configure_filter(local_to_hw(local),
3758 FIF_BCN_PRBRESP_PROMISC,
3759 &local->filter_flags,
3760 local->mdev->mc_count,
3761 local->mdev->mc_list);
3762
3763 netif_tx_unlock_bh(local->mdev);
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 /* No need to wake the master device. */
3769 if (sdata->dev == local->mdev)
3770 continue;
3771
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003772 /* Tell AP we're back */
3773 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
3774 sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
3775 ieee80211_send_nullfunc(local, sdata, 0);
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003776
Johannes Berg69d3b6f2008-03-05 10:58:40 +01003777 ieee80211_restart_sta_timer(sdata);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01003778
Jiri Bencf0706e82007-05-05 11:45:53 -07003779 netif_wake_queue(sdata->dev);
3780 }
Johannes Berg79010422007-09-18 17:29:21 -04003781 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003782
Zhu Yiece8edd2007-11-22 10:53:21 +08003783done:
Jiri Bencf0706e82007-05-05 11:45:53 -07003784 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003785 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003786 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003787 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e82007-05-05 11:45:53 -07003788 (!ifsta->state == IEEE80211_IBSS_JOINED &&
3789 !ieee80211_sta_active_ibss(dev)))
3790 ieee80211_sta_find_ibss(dev, ifsta);
3791 }
3792}
3793EXPORT_SYMBOL(ieee80211_scan_completed);
3794
3795void ieee80211_sta_scan_work(struct work_struct *work)
3796{
3797 struct ieee80211_local *local =
3798 container_of(work, struct ieee80211_local, scan_work.work);
3799 struct net_device *dev = local->scan_dev;
3800 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg8318d782008-01-24 19:38:38 +01003801 struct ieee80211_supported_band *sband;
Jiri Bencf0706e82007-05-05 11:45:53 -07003802 struct ieee80211_channel *chan;
3803 int skip;
3804 unsigned long next_delay = 0;
3805
Zhu Yiece8edd2007-11-22 10:53:21 +08003806 if (!local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003807 return;
3808
3809 switch (local->scan_state) {
3810 case SCAN_SET_CHANNEL:
Johannes Berg69d464d2008-01-28 17:08:56 +01003811 /*
3812 * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
3813 * after we successfully scanned the last channel of the last
3814 * band (and the last band is supported by the hw)
3815 */
Johannes Berg8318d782008-01-24 19:38:38 +01003816 if (local->scan_band < IEEE80211_NUM_BANDS)
3817 sband = local->hw.wiphy->bands[local->scan_band];
3818 else
3819 sband = NULL;
3820
Johannes Berg69d464d2008-01-28 17:08:56 +01003821 /*
3822 * If we are at an unsupported band and have more bands
3823 * left to scan, advance to the next supported one.
3824 */
3825 while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
Johannes Berg8318d782008-01-24 19:38:38 +01003826 local->scan_band++;
3827 sband = local->hw.wiphy->bands[local->scan_band];
3828 local->scan_channel_idx = 0;
3829 }
3830
Johannes Berg69d464d2008-01-28 17:08:56 +01003831 /* if no more bands/channels left, complete scan */
3832 if (!sband || local->scan_channel_idx >= sband->n_channels) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003833 ieee80211_scan_completed(local_to_hw(local));
3834 return;
3835 }
Johannes Berg8318d782008-01-24 19:38:38 +01003836 skip = 0;
3837 chan = &sband->channels[local->scan_channel_idx];
3838
3839 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg51fb61e2007-12-19 01:31:27 +01003840 (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
Johannes Berg8318d782008-01-24 19:38:38 +01003841 chan->flags & IEEE80211_CHAN_NO_IBSS))
Jiri Bencf0706e82007-05-05 11:45:53 -07003842 skip = 1;
3843
3844 if (!skip) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003845 local->scan_channel = chan;
3846 if (ieee80211_hw_config(local)) {
Johannes Berg8318d782008-01-24 19:38:38 +01003847 printk(KERN_DEBUG "%s: failed to set freq to "
3848 "%d MHz for scan\n", dev->name,
3849 chan->center_freq);
Jiri Bencf0706e82007-05-05 11:45:53 -07003850 skip = 1;
3851 }
3852 }
3853
Johannes Berg69d464d2008-01-28 17:08:56 +01003854 /* advance state machine to next channel/band */
Jiri Bencf0706e82007-05-05 11:45:53 -07003855 local->scan_channel_idx++;
Johannes Berg8318d782008-01-24 19:38:38 +01003856 if (local->scan_channel_idx >= sband->n_channels) {
Johannes Berg69d464d2008-01-28 17:08:56 +01003857 /*
3858 * scan_band may end up == IEEE80211_NUM_BANDS, but
3859 * we'll catch that case above and complete the scan
3860 * if that is the case.
3861 */
Johannes Berg8318d782008-01-24 19:38:38 +01003862 local->scan_band++;
3863 local->scan_channel_idx = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07003864 }
3865
3866 if (skip)
3867 break;
3868
3869 next_delay = IEEE80211_PROBE_DELAY +
3870 usecs_to_jiffies(local->hw.channel_change_time);
3871 local->scan_state = SCAN_SEND_PROBE;
3872 break;
3873 case SCAN_SEND_PROBE:
Johannes Berg8318d782008-01-24 19:38:38 +01003874 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003875 local->scan_state = SCAN_SET_CHANNEL;
Johannes Berg8318d782008-01-24 19:38:38 +01003876
3877 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
3878 break;
3879 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
3880 local->scan_ssid_len);
3881 next_delay = IEEE80211_CHANNEL_TIME;
Jiri Bencf0706e82007-05-05 11:45:53 -07003882 break;
3883 }
3884
Zhu Yiece8edd2007-11-22 10:53:21 +08003885 if (local->sta_sw_scanning)
Jiri Bencf0706e82007-05-05 11:45:53 -07003886 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3887 next_delay);
3888}
3889
3890
3891static int ieee80211_sta_start_scan(struct net_device *dev,
3892 u8 *ssid, size_t ssid_len)
3893{
3894 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3895 struct ieee80211_sub_if_data *sdata;
3896
3897 if (ssid_len > IEEE80211_MAX_SSID_LEN)
3898 return -EINVAL;
3899
3900 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
3901 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
3902 * BSSID: MACAddress
3903 * SSID
3904 * ScanType: ACTIVE, PASSIVE
3905 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
3906 * a Probe frame during active scanning
3907 * ChannelList
3908 * MinChannelTime (>= ProbeDelay), in TU
3909 * MaxChannelTime: (>= MinChannelTime), in TU
3910 */
3911
3912 /* MLME-SCAN.confirm
3913 * BSSDescriptionSet
3914 * ResultCode: SUCCESS, INVALID_PARAMETERS
3915 */
3916
Zhu Yiece8edd2007-11-22 10:53:21 +08003917 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003918 if (local->scan_dev == dev)
3919 return 0;
3920 return -EBUSY;
3921 }
3922
3923 if (local->ops->hw_scan) {
3924 int rc = local->ops->hw_scan(local_to_hw(local),
Zhu Yiece8edd2007-11-22 10:53:21 +08003925 ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003926 if (!rc) {
Zhu Yiece8edd2007-11-22 10:53:21 +08003927 local->sta_hw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003928 local->scan_dev = dev;
3929 }
3930 return rc;
3931 }
3932
Zhu Yiece8edd2007-11-22 10:53:21 +08003933 local->sta_sw_scanning = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07003934
Johannes Berg79010422007-09-18 17:29:21 -04003935 rcu_read_lock();
3936 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02003937
3938 /* Don't stop the master interface, otherwise we can't transmit
3939 * probes! */
3940 if (sdata->dev == local->mdev)
3941 continue;
3942
Jiri Bencf0706e82007-05-05 11:45:53 -07003943 netif_stop_queue(sdata->dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01003944 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003945 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07003946 ieee80211_send_nullfunc(local, sdata, 1);
3947 }
Johannes Berg79010422007-09-18 17:29:21 -04003948 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07003949
3950 if (ssid) {
3951 local->scan_ssid_len = ssid_len;
3952 memcpy(local->scan_ssid, ssid, ssid_len);
3953 } else
3954 local->scan_ssid_len = 0;
3955 local->scan_state = SCAN_SET_CHANNEL;
Jiri Bencf0706e82007-05-05 11:45:53 -07003956 local->scan_channel_idx = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01003957 local->scan_band = IEEE80211_BAND_2GHZ;
Jiri Bencf0706e82007-05-05 11:45:53 -07003958 local->scan_dev = dev;
3959
Johannes Berg4150c572007-09-17 01:29:23 -04003960 netif_tx_lock_bh(local->mdev);
3961 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
3962 local->ops->configure_filter(local_to_hw(local),
3963 FIF_BCN_PRBRESP_PROMISC,
3964 &local->filter_flags,
3965 local->mdev->mc_count,
3966 local->mdev->mc_list);
3967 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003968
3969 /* TODO: start scan as soon as all nullfunc frames are ACKed */
3970 queue_delayed_work(local->hw.workqueue, &local->scan_work,
3971 IEEE80211_CHANNEL_TIME);
3972
3973 return 0;
3974}
3975
3976
3977int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
3978{
3979 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3980 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3981 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3982
Johannes Berg51fb61e2007-12-19 01:31:27 +01003983 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07003984 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
3985
Zhu Yiece8edd2007-11-22 10:53:21 +08003986 if (local->sta_sw_scanning || local->sta_hw_scanning) {
Jiri Bencf0706e82007-05-05 11:45:53 -07003987 if (local->scan_dev == dev)
3988 return 0;
3989 return -EBUSY;
3990 }
3991
Helmut Schaaa0af5f12007-11-09 16:25:08 +01003992 ifsta->scan_ssid_len = ssid_len;
3993 if (ssid_len)
3994 memcpy(ifsta->scan_ssid, ssid, ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07003995 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
3996 queue_work(local->hw.workqueue, &ifsta->work);
3997 return 0;
3998}
3999
4000static char *
4001ieee80211_sta_scan_result(struct net_device *dev,
4002 struct ieee80211_sta_bss *bss,
4003 char *current_ev, char *end_buf)
4004{
4005 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4006 struct iw_event iwe;
4007
4008 if (time_after(jiffies,
4009 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
4010 return current_ev;
4011
Jiri Bencf0706e82007-05-05 11:45:53 -07004012 memset(&iwe, 0, sizeof(iwe));
4013 iwe.cmd = SIOCGIWAP;
4014 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
4015 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
4016 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4017 IW_EV_ADDR_LEN);
4018
4019 memset(&iwe, 0, sizeof(iwe));
4020 iwe.cmd = SIOCGIWESSID;
Johannes Berg902acc72008-02-23 15:17:19 +01004021 if (bss_mesh_cfg(bss)) {
4022 iwe.u.data.length = bss_mesh_id_len(bss);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004023 iwe.u.data.flags = 1;
4024 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
Johannes Berg902acc72008-02-23 15:17:19 +01004025 bss_mesh_id(bss));
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004026 } else {
4027 iwe.u.data.length = bss->ssid_len;
4028 iwe.u.data.flags = 1;
4029 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4030 bss->ssid);
4031 }
Jiri Bencf0706e82007-05-05 11:45:53 -07004032
Luis Carlos Cobo1d1b5352008-02-29 12:15:28 -08004033 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
4034 || bss_mesh_cfg(bss)) {
Jiri Bencf0706e82007-05-05 11:45:53 -07004035 memset(&iwe, 0, sizeof(iwe));
4036 iwe.cmd = SIOCGIWMODE;
Johannes Berg902acc72008-02-23 15:17:19 +01004037 if (bss_mesh_cfg(bss))
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004038 iwe.u.mode = IW_MODE_MESH;
4039 else if (bss->capability & WLAN_CAPABILITY_ESS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004040 iwe.u.mode = IW_MODE_MASTER;
4041 else
4042 iwe.u.mode = IW_MODE_ADHOC;
4043 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4044 IW_EV_UINT_LEN);
4045 }
4046
4047 memset(&iwe, 0, sizeof(iwe));
4048 iwe.cmd = SIOCGIWFREQ;
Johannes Berg8318d782008-01-24 19:38:38 +01004049 iwe.u.freq.m = bss->freq;
4050 iwe.u.freq.e = 6;
Jiri Bencf0706e82007-05-05 11:45:53 -07004051 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4052 IW_EV_FREQ_LEN);
Johannes Berg8318d782008-01-24 19:38:38 +01004053
4054 memset(&iwe, 0, sizeof(iwe));
4055 iwe.cmd = SIOCGIWFREQ;
4056 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
4057 iwe.u.freq.e = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07004058 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4059 IW_EV_FREQ_LEN);
4060
4061 memset(&iwe, 0, sizeof(iwe));
4062 iwe.cmd = IWEVQUAL;
Bruno Randolf566bfe52008-05-08 19:15:40 +02004063 iwe.u.qual.qual = bss->qual;
4064 iwe.u.qual.level = bss->signal;
Jiri Bencf0706e82007-05-05 11:45:53 -07004065 iwe.u.qual.noise = bss->noise;
4066 iwe.u.qual.updated = local->wstats_flags;
4067 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
4068 IW_EV_QUAL_LEN);
4069
4070 memset(&iwe, 0, sizeof(iwe));
4071 iwe.cmd = SIOCGIWENCODE;
4072 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
4073 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
4074 else
4075 iwe.u.data.flags = IW_ENCODE_DISABLED;
4076 iwe.u.data.length = 0;
4077 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
4078
4079 if (bss && bss->wpa_ie) {
4080 memset(&iwe, 0, sizeof(iwe));
4081 iwe.cmd = IWEVGENIE;
4082 iwe.u.data.length = bss->wpa_ie_len;
4083 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4084 bss->wpa_ie);
4085 }
4086
4087 if (bss && bss->rsn_ie) {
4088 memset(&iwe, 0, sizeof(iwe));
4089 iwe.cmd = IWEVGENIE;
4090 iwe.u.data.length = bss->rsn_ie_len;
4091 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
4092 bss->rsn_ie);
4093 }
4094
4095 if (bss && bss->supp_rates_len > 0) {
4096 /* display all supported rates in readable format */
4097 char *p = current_ev + IW_EV_LCP_LEN;
4098 int i;
4099
4100 memset(&iwe, 0, sizeof(iwe));
4101 iwe.cmd = SIOCGIWRATE;
4102 /* Those two flags are ignored... */
4103 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
4104
4105 for (i = 0; i < bss->supp_rates_len; i++) {
4106 iwe.u.bitrate.value = ((bss->supp_rates[i] &
4107 0x7f) * 500000);
4108 p = iwe_stream_add_value(current_ev, p,
4109 end_buf, &iwe, IW_EV_PARAM_LEN);
4110 }
4111 current_ev = p;
4112 }
4113
4114 if (bss) {
4115 char *buf;
4116 buf = kmalloc(30, GFP_ATOMIC);
4117 if (buf) {
4118 memset(&iwe, 0, sizeof(iwe));
4119 iwe.cmd = IWEVCUSTOM;
4120 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
4121 iwe.u.data.length = strlen(buf);
4122 current_ev = iwe_stream_add_point(current_ev, end_buf,
4123 &iwe, buf);
4124 kfree(buf);
4125 }
4126 }
4127
Johannes Berg902acc72008-02-23 15:17:19 +01004128 if (bss_mesh_cfg(bss)) {
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004129 char *buf;
John W. Linville24736702008-04-08 14:15:46 -04004130 u8 *cfg = bss_mesh_cfg(bss);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004131 buf = kmalloc(50, GFP_ATOMIC);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004132 if (buf) {
4133 memset(&iwe, 0, sizeof(iwe));
4134 iwe.cmd = IWEVCUSTOM;
John W. Linville24736702008-04-08 14:15:46 -04004135 sprintf(buf, "Mesh network (version %d)", cfg[0]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004136 iwe.u.data.length = strlen(buf);
4137 current_ev = iwe_stream_add_point(current_ev, end_buf,
4138 &iwe, buf);
4139 sprintf(buf, "Path Selection Protocol ID: "
John W. Linville24736702008-04-08 14:15:46 -04004140 "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
4141 cfg[4]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004142 iwe.u.data.length = strlen(buf);
4143 current_ev = iwe_stream_add_point(current_ev, end_buf,
4144 &iwe, buf);
4145 sprintf(buf, "Path Selection Metric ID: "
John W. Linville24736702008-04-08 14:15:46 -04004146 "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
4147 cfg[8]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004148 iwe.u.data.length = strlen(buf);
4149 current_ev = iwe_stream_add_point(current_ev, end_buf,
4150 &iwe, buf);
4151 sprintf(buf, "Congestion Control Mode ID: "
John W. Linville24736702008-04-08 14:15:46 -04004152 "0x%02X%02X%02X%02X", cfg[9], cfg[10],
4153 cfg[11], cfg[12]);
Luis Carlos Cobo4f5d4c42008-02-29 12:32:46 -08004154 iwe.u.data.length = strlen(buf);
4155 current_ev = iwe_stream_add_point(current_ev, end_buf,
4156 &iwe, buf);
4157 sprintf(buf, "Channel Precedence: "
John W. Linville24736702008-04-08 14:15:46 -04004158 "0x%02X%02X%02X%02X", cfg[13], cfg[14],
4159 cfg[15], cfg[16]);
Luis Carlos Cobof709fc62008-02-23 15:17:12 +01004160 iwe.u.data.length = strlen(buf);
4161 current_ev = iwe_stream_add_point(current_ev, end_buf,
4162 &iwe, buf);
4163 kfree(buf);
4164 }
4165 }
4166
Jiri Bencf0706e82007-05-05 11:45:53 -07004167 return current_ev;
4168}
4169
4170
4171int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
4172{
4173 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4174 char *current_ev = buf;
4175 char *end_buf = buf + len;
4176 struct ieee80211_sta_bss *bss;
4177
4178 spin_lock_bh(&local->sta_bss_lock);
4179 list_for_each_entry(bss, &local->sta_bss_list, list) {
4180 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
4181 spin_unlock_bh(&local->sta_bss_lock);
4182 return -E2BIG;
4183 }
4184 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
4185 end_buf);
4186 }
4187 spin_unlock_bh(&local->sta_bss_lock);
4188 return current_ev - buf;
4189}
4190
4191
4192int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
4193{
4194 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4195 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Johannes Berg988c0f72008-04-17 19:21:22 +02004196
Jiri Bencf0706e82007-05-05 11:45:53 -07004197 kfree(ifsta->extra_ie);
4198 if (len == 0) {
4199 ifsta->extra_ie = NULL;
4200 ifsta->extra_ie_len = 0;
4201 return 0;
4202 }
4203 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
4204 if (!ifsta->extra_ie) {
4205 ifsta->extra_ie_len = 0;
4206 return -ENOMEM;
4207 }
4208 memcpy(ifsta->extra_ie, ie, len);
4209 ifsta->extra_ie_len = len;
4210 return 0;
4211}
4212
4213
Johannes Berg988c0f72008-04-17 19:21:22 +02004214struct sta_info *ieee80211_ibss_add_sta(struct net_device *dev,
4215 struct sk_buff *skb, u8 *bssid,
4216 u8 *addr)
Jiri Bencf0706e82007-05-05 11:45:53 -07004217{
4218 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4219 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04004220 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07004221 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -07004222
4223 /* TODO: Could consider removing the least recently used entry and
4224 * allow new one to be added. */
4225 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
4226 if (net_ratelimit()) {
4227 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07004228 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e82007-05-05 11:45:53 -07004229 }
4230 return NULL;
4231 }
4232
Joe Perches0795af52007-10-03 17:59:30 -07004233 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04004234 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e82007-05-05 11:45:53 -07004235
Johannes Berg73651ee2008-02-25 16:27:47 +01004236 sta = sta_info_alloc(sdata, addr, GFP_ATOMIC);
4237 if (!sta)
Jiri Bencf0706e82007-05-05 11:45:53 -07004238 return NULL;
4239
Johannes Berg07346f812008-05-03 01:02:02 +02004240 set_sta_flags(sta, WLAN_STA_AUTHORIZED);
Johannes Berg238814f2008-01-28 17:19:37 +01004241
Johannes Berg8318d782008-01-24 19:38:38 +01004242 sta->supp_rates[local->hw.conf.channel->band] =
4243 sdata->u.sta.supp_rates_bits[local->hw.conf.channel->band];
Jiri Bencf0706e82007-05-05 11:45:53 -07004244
4245 rate_control_rate_init(sta, local);
4246
Johannes Berg93e5deb2008-04-01 15:21:00 +02004247 if (sta_info_insert(sta))
Johannes Berg73651ee2008-02-25 16:27:47 +01004248 return NULL;
Johannes Berg73651ee2008-02-25 16:27:47 +01004249
Johannes Bergd0709a62008-02-25 16:27:46 +01004250 return sta;
Jiri Bencf0706e82007-05-05 11:45:53 -07004251}
4252
4253
4254int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
4255{
4256 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4257 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4258
4259 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
4260 dev->name, reason);
4261
Johannes Berg51fb61e2007-12-19 01:31:27 +01004262 if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
4263 sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
Jiri Bencf0706e82007-05-05 11:45:53 -07004264 return -EINVAL;
4265
4266 ieee80211_send_deauth(dev, ifsta, reason);
4267 ieee80211_set_disassoc(dev, ifsta, 1);
4268 return 0;
4269}
4270
4271
4272int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
4273{
4274 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4275 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
4276
4277 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
4278 dev->name, reason);
4279
Johannes Berg51fb61e2007-12-19 01:31:27 +01004280 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
Jiri Bencf0706e82007-05-05 11:45:53 -07004281 return -EINVAL;
4282
Jiri Slabyd6f2da52007-08-28 17:01:54 -04004283 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e82007-05-05 11:45:53 -07004284 return -1;
4285
4286 ieee80211_send_disassoc(dev, ifsta, reason);
4287 ieee80211_set_disassoc(dev, ifsta, 0);
4288 return 0;
4289}
Mohamed Abbas84363e62008-04-04 16:59:58 -07004290
4291void ieee80211_notify_mac(struct ieee80211_hw *hw,
4292 enum ieee80211_notification_types notif_type)
4293{
4294 struct ieee80211_local *local = hw_to_local(hw);
4295 struct ieee80211_sub_if_data *sdata;
4296
4297 switch (notif_type) {
4298 case IEEE80211_NOTIFY_RE_ASSOC:
4299 rcu_read_lock();
4300 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
4301
4302 if (sdata->vif.type == IEEE80211_IF_TYPE_STA) {
4303 ieee80211_sta_req_auth(sdata->dev,
4304 &sdata->u.sta);
4305 }
4306
4307 }
4308 rcu_read_unlock();
4309 break;
4310 }
4311}
4312EXPORT_SYMBOL(ieee80211_notify_mac);