blob: 8dae26bbfdf9019ba49c7beb768e9f19a67dc0d6 [file] [log] [blame]
Jiri Bencf0706e822007-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:
15 * BSS table: use <BSSID,SSID> as the key to support multi-SSID APs
16 * order BSS list by RSSI(?) ("quality of AP")
17 * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
18 * SSID)
19 */
Geert Uytterhoeven5b323ed2007-05-08 18:40:27 -070020#include <linux/delay.h>
Jiri Bencf0706e822007-05-05 11:45:53 -070021#include <linux/if_ether.h>
22#include <linux/skbuff.h>
23#include <linux/netdevice.h>
24#include <linux/if_arp.h>
25#include <linux/wireless.h>
26#include <linux/random.h>
27#include <linux/etherdevice.h>
Jiri Bencf0706e822007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e822007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
33#include "ieee80211_rate.h"
Michael Buesch47f0c502007-09-27 15:10:44 +020034#include "ieee80211_led.h"
Jiri Bencf0706e822007-05-05 11:45:53 -070035
36#define IEEE80211_AUTH_TIMEOUT (HZ / 5)
37#define IEEE80211_AUTH_MAX_TRIES 3
38#define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
39#define IEEE80211_ASSOC_MAX_TRIES 3
40#define IEEE80211_MONITORING_INTERVAL (2 * HZ)
41#define IEEE80211_PROBE_INTERVAL (60 * HZ)
42#define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
43#define IEEE80211_SCAN_INTERVAL (2 * HZ)
44#define IEEE80211_SCAN_INTERVAL_SLOW (15 * HZ)
45#define IEEE80211_IBSS_JOIN_TIMEOUT (20 * HZ)
46
47#define IEEE80211_PROBE_DELAY (HZ / 33)
48#define IEEE80211_CHANNEL_TIME (HZ / 33)
49#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
50#define IEEE80211_SCAN_RESULT_EXPIRE (10 * HZ)
51#define IEEE80211_IBSS_MERGE_INTERVAL (30 * HZ)
52#define IEEE80211_IBSS_INACTIVITY_LIMIT (60 * HZ)
53
54#define IEEE80211_IBSS_MAX_STA_ENTRIES 128
55
56
57#define IEEE80211_FC(type, stype) cpu_to_le16(type | stype)
58
59#define ERP_INFO_USE_PROTECTION BIT(1)
60
61static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
62 u8 *ssid, size_t ssid_len);
63static struct ieee80211_sta_bss *
64ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid);
65static void ieee80211_rx_bss_put(struct net_device *dev,
66 struct ieee80211_sta_bss *bss);
67static int ieee80211_sta_find_ibss(struct net_device *dev,
68 struct ieee80211_if_sta *ifsta);
69static int ieee80211_sta_wep_configured(struct net_device *dev);
70static int ieee80211_sta_start_scan(struct net_device *dev,
71 u8 *ssid, size_t ssid_len);
72static int ieee80211_sta_config_auth(struct net_device *dev,
73 struct ieee80211_if_sta *ifsta);
74
75
76/* Parsed Information Elements */
77struct ieee802_11_elems {
Johannes Berg55582352007-07-10 19:32:09 +020078 /* pointers to IEs */
Jiri Bencf0706e822007-05-05 11:45:53 -070079 u8 *ssid;
Jiri Bencf0706e822007-05-05 11:45:53 -070080 u8 *supp_rates;
Jiri Bencf0706e822007-05-05 11:45:53 -070081 u8 *fh_params;
Jiri Bencf0706e822007-05-05 11:45:53 -070082 u8 *ds_params;
Jiri Bencf0706e822007-05-05 11:45:53 -070083 u8 *cf_params;
Jiri Bencf0706e822007-05-05 11:45:53 -070084 u8 *tim;
Jiri Bencf0706e822007-05-05 11:45:53 -070085 u8 *ibss_params;
Jiri Bencf0706e822007-05-05 11:45:53 -070086 u8 *challenge;
Jiri Bencf0706e822007-05-05 11:45:53 -070087 u8 *wpa;
Jiri Bencf0706e822007-05-05 11:45:53 -070088 u8 *rsn;
Jiri Bencf0706e822007-05-05 11:45:53 -070089 u8 *erp_info;
Jiri Bencf0706e822007-05-05 11:45:53 -070090 u8 *ext_supp_rates;
Jiri Bencf0706e822007-05-05 11:45:53 -070091 u8 *wmm_info;
Jiri Bencf0706e822007-05-05 11:45:53 -070092 u8 *wmm_param;
Johannes Berg55582352007-07-10 19:32:09 +020093
94 /* length of them, respectively */
95 u8 ssid_len;
96 u8 supp_rates_len;
97 u8 fh_params_len;
98 u8 ds_params_len;
99 u8 cf_params_len;
100 u8 tim_len;
101 u8 ibss_params_len;
102 u8 challenge_len;
103 u8 wpa_len;
104 u8 rsn_len;
105 u8 erp_info_len;
106 u8 ext_supp_rates_len;
107 u8 wmm_info_len;
Jiri Bencf0706e822007-05-05 11:45:53 -0700108 u8 wmm_param_len;
109};
110
Johannes Bergc095df52007-09-18 17:29:21 -0400111enum ParseRes { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 };
Jiri Bencf0706e822007-05-05 11:45:53 -0700112
Johannes Bergc095df52007-09-18 17:29:21 -0400113static enum ParseRes ieee802_11_parse_elems(u8 *start, size_t len,
114 struct ieee802_11_elems *elems)
Jiri Bencf0706e822007-05-05 11:45:53 -0700115{
116 size_t left = len;
117 u8 *pos = start;
118 int unknown = 0;
119
120 memset(elems, 0, sizeof(*elems));
121
122 while (left >= 2) {
123 u8 id, elen;
124
125 id = *pos++;
126 elen = *pos++;
127 left -= 2;
128
129 if (elen > left) {
130#if 0
131 if (net_ratelimit())
132 printk(KERN_DEBUG "IEEE 802.11 element parse "
133 "failed (id=%d elen=%d left=%d)\n",
134 id, elen, left);
135#endif
136 return ParseFailed;
137 }
138
139 switch (id) {
140 case WLAN_EID_SSID:
141 elems->ssid = pos;
142 elems->ssid_len = elen;
143 break;
144 case WLAN_EID_SUPP_RATES:
145 elems->supp_rates = pos;
146 elems->supp_rates_len = elen;
147 break;
148 case WLAN_EID_FH_PARAMS:
149 elems->fh_params = pos;
150 elems->fh_params_len = elen;
151 break;
152 case WLAN_EID_DS_PARAMS:
153 elems->ds_params = pos;
154 elems->ds_params_len = elen;
155 break;
156 case WLAN_EID_CF_PARAMS:
157 elems->cf_params = pos;
158 elems->cf_params_len = elen;
159 break;
160 case WLAN_EID_TIM:
161 elems->tim = pos;
162 elems->tim_len = elen;
163 break;
164 case WLAN_EID_IBSS_PARAMS:
165 elems->ibss_params = pos;
166 elems->ibss_params_len = elen;
167 break;
168 case WLAN_EID_CHALLENGE:
169 elems->challenge = pos;
170 elems->challenge_len = elen;
171 break;
172 case WLAN_EID_WPA:
173 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
174 pos[2] == 0xf2) {
175 /* Microsoft OUI (00:50:F2) */
176 if (pos[3] == 1) {
177 /* OUI Type 1 - WPA IE */
178 elems->wpa = pos;
179 elems->wpa_len = elen;
180 } else if (elen >= 5 && pos[3] == 2) {
181 if (pos[4] == 0) {
182 elems->wmm_info = pos;
183 elems->wmm_info_len = elen;
184 } else if (pos[4] == 1) {
185 elems->wmm_param = pos;
186 elems->wmm_param_len = elen;
187 }
188 }
189 }
190 break;
191 case WLAN_EID_RSN:
192 elems->rsn = pos;
193 elems->rsn_len = elen;
194 break;
195 case WLAN_EID_ERP_INFO:
196 elems->erp_info = pos;
197 elems->erp_info_len = elen;
198 break;
199 case WLAN_EID_EXT_SUPP_RATES:
200 elems->ext_supp_rates = pos;
201 elems->ext_supp_rates_len = elen;
202 break;
203 default:
204#if 0
205 printk(KERN_DEBUG "IEEE 802.11 element parse ignored "
206 "unknown element (id=%d elen=%d)\n",
207 id, elen);
208#endif
209 unknown++;
210 break;
211 }
212
213 left -= elen;
214 pos += elen;
215 }
216
217 /* Do not trigger error if left == 1 as Apple Airport base stations
218 * send AssocResps that are one spurious byte too long. */
219
220 return unknown ? ParseUnknown : ParseOK;
221}
222
223
224
225
226static int ecw2cw(int ecw)
227{
228 int cw = 1;
229 while (ecw > 0) {
230 cw <<= 1;
231 ecw--;
232 }
233 return cw - 1;
234}
235
Jiri Bencf0706e822007-05-05 11:45:53 -0700236static void ieee80211_sta_wmm_params(struct net_device *dev,
237 struct ieee80211_if_sta *ifsta,
238 u8 *wmm_param, size_t wmm_param_len)
239{
240 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
241 struct ieee80211_tx_queue_params params;
242 size_t left;
243 int count;
244 u8 *pos;
245
246 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
247 return;
248 count = wmm_param[6] & 0x0f;
249 if (count == ifsta->wmm_last_param_set)
250 return;
251 ifsta->wmm_last_param_set = count;
252
253 pos = wmm_param + 8;
254 left = wmm_param_len - 8;
255
256 memset(&params, 0, sizeof(params));
257
258 if (!local->ops->conf_tx)
259 return;
260
261 local->wmm_acm = 0;
262 for (; left >= 4; left -= 4, pos += 4) {
263 int aci = (pos[0] >> 5) & 0x03;
264 int acm = (pos[0] >> 4) & 0x01;
265 int queue;
266
267 switch (aci) {
268 case 1:
269 queue = IEEE80211_TX_QUEUE_DATA3;
270 if (acm) {
271 local->wmm_acm |= BIT(0) | BIT(3);
272 }
273 break;
274 case 2:
275 queue = IEEE80211_TX_QUEUE_DATA1;
276 if (acm) {
277 local->wmm_acm |= BIT(4) | BIT(5);
278 }
279 break;
280 case 3:
281 queue = IEEE80211_TX_QUEUE_DATA0;
282 if (acm) {
283 local->wmm_acm |= BIT(6) | BIT(7);
284 }
285 break;
286 case 0:
287 default:
288 queue = IEEE80211_TX_QUEUE_DATA2;
289 if (acm) {
290 local->wmm_acm |= BIT(1) | BIT(2);
291 }
292 break;
293 }
294
295 params.aifs = pos[0] & 0x0f;
296 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
297 params.cw_min = ecw2cw(pos[1] & 0x0f);
298 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
299 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
300 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
301 "cWmin=%d cWmax=%d burst=%d\n",
302 dev->name, queue, aci, acm, params.aifs, params.cw_min,
303 params.cw_max, params.burst_time);
304 /* TODO: handle ACM (block TX, fallback to next lowest allowed
305 * AC for now) */
306 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
307 printk(KERN_DEBUG "%s: failed to set TX queue "
308 "parameters for queue %d\n", dev->name, queue);
309 }
310 }
311}
312
313
Daniel Drake56282212007-07-10 19:32:10 +0200314static void ieee80211_handle_erp_ie(struct net_device *dev, u8 erp_value)
315{
Daniel Drake56282212007-07-10 19:32:10 +0200316 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
317 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
318 int use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200319 int preamble_mode = (erp_value & WLAN_ERP_BARKER_PREAMBLE) != 0;
Daniel Draked9430a32007-07-27 15:43:24 +0200320 u8 changes = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700321 DECLARE_MAC_BUF(mac);
Daniel Drake56282212007-07-10 19:32:10 +0200322
Jiri Slaby13262ff2007-08-28 17:01:54 -0400323 if (use_protection != !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION)) {
Daniel Drake56282212007-07-10 19:32:10 +0200324 if (net_ratelimit()) {
325 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
Joe Perches0795af52007-10-03 17:59:30 -0700326 "%s)\n",
Daniel Drake56282212007-07-10 19:32:10 +0200327 dev->name,
328 use_protection ? "enabled" : "disabled",
Joe Perches0795af52007-10-03 17:59:30 -0700329 print_mac(mac, ifsta->bssid));
Daniel Drake56282212007-07-10 19:32:10 +0200330 }
Jiri Slaby13262ff2007-08-28 17:01:54 -0400331 if (use_protection)
332 sdata->flags |= IEEE80211_SDATA_USE_PROTECTION;
333 else
334 sdata->flags &= ~IEEE80211_SDATA_USE_PROTECTION;
Daniel Draked9430a32007-07-27 15:43:24 +0200335 changes |= IEEE80211_ERP_CHANGE_PROTECTION;
Daniel Drake56282212007-07-10 19:32:10 +0200336 }
Daniel Drake7e9ed182007-07-27 15:43:24 +0200337
Jiri Slaby13262ff2007-08-28 17:01:54 -0400338 if (preamble_mode != !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE)) {
Daniel Drake7e9ed182007-07-27 15:43:24 +0200339 if (net_ratelimit()) {
340 printk(KERN_DEBUG "%s: switched to %s barker preamble"
Joe Perches0795af52007-10-03 17:59:30 -0700341 " (BSSID=%s)\n",
Daniel Drake7e9ed182007-07-27 15:43:24 +0200342 dev->name,
343 (preamble_mode == WLAN_ERP_PREAMBLE_SHORT) ?
344 "short" : "long",
Joe Perches0795af52007-10-03 17:59:30 -0700345 print_mac(mac, ifsta->bssid));
Daniel Drake7e9ed182007-07-27 15:43:24 +0200346 }
Jiri Slaby13262ff2007-08-28 17:01:54 -0400347 if (preamble_mode)
348 sdata->flags &= ~IEEE80211_SDATA_SHORT_PREAMBLE;
349 else
350 sdata->flags |= IEEE80211_SDATA_SHORT_PREAMBLE;
Daniel Draked9430a32007-07-27 15:43:24 +0200351 changes |= IEEE80211_ERP_CHANGE_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200352 }
Daniel Draked9430a32007-07-27 15:43:24 +0200353
354 if (changes)
355 ieee80211_erp_info_change_notify(dev, changes);
Daniel Drake56282212007-07-10 19:32:10 +0200356}
357
358
Jiri Bencf0706e822007-05-05 11:45:53 -0700359static void ieee80211_sta_send_associnfo(struct net_device *dev,
360 struct ieee80211_if_sta *ifsta)
361{
362 char *buf;
363 size_t len;
364 int i;
365 union iwreq_data wrqu;
366
367 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
368 return;
369
370 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200371 ifsta->assocresp_ies_len), GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -0700372 if (!buf)
373 return;
374
375 len = sprintf(buf, "ASSOCINFO(");
376 if (ifsta->assocreq_ies) {
377 len += sprintf(buf + len, "ReqIEs=");
378 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
379 len += sprintf(buf + len, "%02x",
380 ifsta->assocreq_ies[i]);
381 }
382 }
383 if (ifsta->assocresp_ies) {
384 if (ifsta->assocreq_ies)
385 len += sprintf(buf + len, " ");
386 len += sprintf(buf + len, "RespIEs=");
387 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
388 len += sprintf(buf + len, "%02x",
389 ifsta->assocresp_ies[i]);
390 }
391 }
392 len += sprintf(buf + len, ")");
393
394 if (len > IW_CUSTOM_MAX) {
395 len = sprintf(buf, "ASSOCRESPIE=");
396 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
397 len += sprintf(buf + len, "%02x",
398 ifsta->assocresp_ies[i]);
399 }
400 }
401
402 memset(&wrqu, 0, sizeof(wrqu));
403 wrqu.data.length = len;
404 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
405
406 kfree(buf);
407}
408
409
410static void ieee80211_set_associated(struct net_device *dev,
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400411 struct ieee80211_if_sta *ifsta,
Michael Buesch47f0c502007-09-27 15:10:44 +0200412 bool assoc)
Jiri Bencf0706e822007-05-05 11:45:53 -0700413{
Michael Buesch47f0c502007-09-27 15:10:44 +0200414 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Jiri Bencf0706e822007-05-05 11:45:53 -0700415 union iwreq_data wrqu;
416
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400417 if (!!(ifsta->flags & IEEE80211_STA_ASSOCIATED) == assoc)
Jiri Bencf0706e822007-05-05 11:45:53 -0700418 return;
419
Jiri Bencf0706e822007-05-05 11:45:53 -0700420 if (assoc) {
421 struct ieee80211_sub_if_data *sdata;
Daniel Drake56282212007-07-10 19:32:10 +0200422 struct ieee80211_sta_bss *bss;
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400423
424 ifsta->flags |= IEEE80211_STA_ASSOCIATED;
425
Jiri Bencf0706e822007-05-05 11:45:53 -0700426 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
427 if (sdata->type != IEEE80211_IF_TYPE_STA)
428 return;
Daniel Drake56282212007-07-10 19:32:10 +0200429
430 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
431 if (bss) {
432 if (bss->has_erp_value)
433 ieee80211_handle_erp_ie(dev, bss->erp_value);
434 ieee80211_rx_bss_put(dev, bss);
435 }
436
Jiri Bencf0706e822007-05-05 11:45:53 -0700437 netif_carrier_on(dev);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400438 ifsta->flags |= IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -0700439 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
440 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
441 ieee80211_sta_send_associnfo(dev, ifsta);
442 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400443 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
444
Jiri Bencf0706e822007-05-05 11:45:53 -0700445 netif_carrier_off(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200446 ieee80211_reset_erp_info(dev);
Jiri Bencf0706e822007-05-05 11:45:53 -0700447 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
448 }
449 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
450 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
451 ifsta->last_probe = jiffies;
Michael Buesch47f0c502007-09-27 15:10:44 +0200452 ieee80211_led_assoc(local, assoc);
Jiri Bencf0706e822007-05-05 11:45:53 -0700453}
454
455static void ieee80211_set_disassoc(struct net_device *dev,
456 struct ieee80211_if_sta *ifsta, int deauth)
457{
458 if (deauth)
459 ifsta->auth_tries = 0;
460 ifsta->assoc_tries = 0;
461 ieee80211_set_associated(dev, ifsta, 0);
462}
463
464static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
465 int encrypt)
466{
467 struct ieee80211_sub_if_data *sdata;
468 struct ieee80211_tx_packet_data *pkt_data;
469
470 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
471 skb->dev = sdata->local->mdev;
472 skb_set_mac_header(skb, 0);
473 skb_set_network_header(skb, 0);
474 skb_set_transport_header(skb, 0);
475
476 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
477 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
478 pkt_data->ifindex = sdata->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400479 if (!encrypt)
480 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Jiri Bencf0706e822007-05-05 11:45:53 -0700481
482 dev_queue_xmit(skb);
483}
484
485
486static void ieee80211_send_auth(struct net_device *dev,
487 struct ieee80211_if_sta *ifsta,
488 int transaction, u8 *extra, size_t extra_len,
489 int encrypt)
490{
491 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
492 struct sk_buff *skb;
493 struct ieee80211_mgmt *mgmt;
494
495 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
496 sizeof(*mgmt) + 6 + extra_len);
497 if (!skb) {
498 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
499 "frame\n", dev->name);
500 return;
501 }
502 skb_reserve(skb, local->hw.extra_tx_headroom);
503
504 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
505 memset(mgmt, 0, 24 + 6);
506 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
507 IEEE80211_STYPE_AUTH);
508 if (encrypt)
509 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
510 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
511 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
512 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
513 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
514 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
515 ifsta->auth_transaction = transaction + 1;
516 mgmt->u.auth.status_code = cpu_to_le16(0);
517 if (extra)
518 memcpy(skb_put(skb, extra_len), extra, extra_len);
519
520 ieee80211_sta_tx(dev, skb, encrypt);
521}
522
523
524static void ieee80211_authenticate(struct net_device *dev,
525 struct ieee80211_if_sta *ifsta)
526{
Joe Perches0795af52007-10-03 17:59:30 -0700527 DECLARE_MAC_BUF(mac);
528
Jiri Bencf0706e822007-05-05 11:45:53 -0700529 ifsta->auth_tries++;
530 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700531 printk(KERN_DEBUG "%s: authentication with AP %s"
Jiri Bencf0706e822007-05-05 11:45:53 -0700532 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700533 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700534 ifsta->state = IEEE80211_DISABLED;
535 return;
536 }
537
538 ifsta->state = IEEE80211_AUTHENTICATE;
Joe Perches0795af52007-10-03 17:59:30 -0700539 printk(KERN_DEBUG "%s: authenticate with AP %s\n",
540 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700541
542 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
543
544 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
545}
546
547
548static void ieee80211_send_assoc(struct net_device *dev,
549 struct ieee80211_if_sta *ifsta)
550{
551 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
552 struct ieee80211_hw_mode *mode;
553 struct sk_buff *skb;
554 struct ieee80211_mgmt *mgmt;
555 u8 *pos, *ies;
556 int i, len;
557 u16 capab;
558 struct ieee80211_sta_bss *bss;
559 int wmm = 0;
560
561 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
562 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
563 ifsta->ssid_len);
564 if (!skb) {
565 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
566 "frame\n", dev->name);
567 return;
568 }
569 skb_reserve(skb, local->hw.extra_tx_headroom);
570
571 mode = local->oper_hw_mode;
572 capab = ifsta->capab;
573 if (mode->mode == MODE_IEEE80211G) {
574 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
575 WLAN_CAPABILITY_SHORT_PREAMBLE;
576 }
577 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
578 if (bss) {
579 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
580 capab |= WLAN_CAPABILITY_PRIVACY;
581 if (bss->wmm_ie) {
582 wmm = 1;
583 }
584 ieee80211_rx_bss_put(dev, bss);
585 }
586
587 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
588 memset(mgmt, 0, 24);
589 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
590 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
591 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
592
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400593 if (ifsta->flags & IEEE80211_STA_PREV_BSSID_SET) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700594 skb_put(skb, 10);
595 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
596 IEEE80211_STYPE_REASSOC_REQ);
597 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
598 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
599 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
600 ETH_ALEN);
601 } else {
602 skb_put(skb, 4);
603 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
604 IEEE80211_STYPE_ASSOC_REQ);
605 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
606 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
607 }
608
609 /* SSID */
610 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
611 *pos++ = WLAN_EID_SSID;
612 *pos++ = ifsta->ssid_len;
613 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
614
615 len = mode->num_rates;
616 if (len > 8)
617 len = 8;
618 pos = skb_put(skb, len + 2);
619 *pos++ = WLAN_EID_SUPP_RATES;
620 *pos++ = len;
621 for (i = 0; i < len; i++) {
622 int rate = mode->rates[i].rate;
Jiri Bencf0706e822007-05-05 11:45:53 -0700623 *pos++ = (u8) (rate / 5);
624 }
625
626 if (mode->num_rates > len) {
627 pos = skb_put(skb, mode->num_rates - len + 2);
628 *pos++ = WLAN_EID_EXT_SUPP_RATES;
629 *pos++ = mode->num_rates - len;
630 for (i = len; i < mode->num_rates; i++) {
631 int rate = mode->rates[i].rate;
Jiri Bencf0706e822007-05-05 11:45:53 -0700632 *pos++ = (u8) (rate / 5);
633 }
634 }
635
636 if (ifsta->extra_ie) {
637 pos = skb_put(skb, ifsta->extra_ie_len);
638 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
639 }
640
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400641 if (wmm && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700642 pos = skb_put(skb, 9);
643 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
644 *pos++ = 7; /* len */
645 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
646 *pos++ = 0x50;
647 *pos++ = 0xf2;
648 *pos++ = 2; /* WME */
649 *pos++ = 0; /* WME info */
650 *pos++ = 1; /* WME ver */
651 *pos++ = 0;
652 }
653
654 kfree(ifsta->assocreq_ies);
655 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
Michael Wu0ec0b7a2007-07-27 15:43:24 +0200656 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -0700657 if (ifsta->assocreq_ies)
658 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
659
660 ieee80211_sta_tx(dev, skb, 0);
661}
662
663
664static void ieee80211_send_deauth(struct net_device *dev,
665 struct ieee80211_if_sta *ifsta, u16 reason)
666{
667 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
668 struct sk_buff *skb;
669 struct ieee80211_mgmt *mgmt;
670
671 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
672 if (!skb) {
673 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
674 "frame\n", dev->name);
675 return;
676 }
677 skb_reserve(skb, local->hw.extra_tx_headroom);
678
679 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
680 memset(mgmt, 0, 24);
681 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
682 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
683 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
684 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
685 IEEE80211_STYPE_DEAUTH);
686 skb_put(skb, 2);
687 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
688
689 ieee80211_sta_tx(dev, skb, 0);
690}
691
692
693static void ieee80211_send_disassoc(struct net_device *dev,
694 struct ieee80211_if_sta *ifsta, u16 reason)
695{
696 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
697 struct sk_buff *skb;
698 struct ieee80211_mgmt *mgmt;
699
700 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
701 if (!skb) {
702 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
703 "frame\n", dev->name);
704 return;
705 }
706 skb_reserve(skb, local->hw.extra_tx_headroom);
707
708 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
709 memset(mgmt, 0, 24);
710 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
711 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
712 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
713 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
714 IEEE80211_STYPE_DISASSOC);
715 skb_put(skb, 2);
716 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
717
718 ieee80211_sta_tx(dev, skb, 0);
719}
720
721
722static int ieee80211_privacy_mismatch(struct net_device *dev,
723 struct ieee80211_if_sta *ifsta)
724{
725 struct ieee80211_sta_bss *bss;
726 int res = 0;
727
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400728 if (!ifsta || (ifsta->flags & IEEE80211_STA_MIXED_CELL) ||
729 ifsta->key_management_enabled)
Jiri Bencf0706e822007-05-05 11:45:53 -0700730 return 0;
731
732 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
733 if (!bss)
734 return 0;
735
736 if (ieee80211_sta_wep_configured(dev) !=
737 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
738 res = 1;
739
740 ieee80211_rx_bss_put(dev, bss);
741
742 return res;
743}
744
745
746static void ieee80211_associate(struct net_device *dev,
747 struct ieee80211_if_sta *ifsta)
748{
Joe Perches0795af52007-10-03 17:59:30 -0700749 DECLARE_MAC_BUF(mac);
750
Jiri Bencf0706e822007-05-05 11:45:53 -0700751 ifsta->assoc_tries++;
752 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
Joe Perches0795af52007-10-03 17:59:30 -0700753 printk(KERN_DEBUG "%s: association with AP %s"
Jiri Bencf0706e822007-05-05 11:45:53 -0700754 " timed out\n",
Joe Perches0795af52007-10-03 17:59:30 -0700755 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700756 ifsta->state = IEEE80211_DISABLED;
757 return;
758 }
759
760 ifsta->state = IEEE80211_ASSOCIATE;
Joe Perches0795af52007-10-03 17:59:30 -0700761 printk(KERN_DEBUG "%s: associate with AP %s\n",
762 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700763 if (ieee80211_privacy_mismatch(dev, ifsta)) {
764 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
765 "mixed-cell disabled - abort association\n", dev->name);
766 ifsta->state = IEEE80211_DISABLED;
767 return;
768 }
769
770 ieee80211_send_assoc(dev, ifsta);
771
772 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
773}
774
775
776static void ieee80211_associated(struct net_device *dev,
777 struct ieee80211_if_sta *ifsta)
778{
779 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
780 struct sta_info *sta;
781 int disassoc;
Joe Perches0795af52007-10-03 17:59:30 -0700782 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -0700783
784 /* TODO: start monitoring current AP signal quality and number of
785 * missed beacons. Scan other channels every now and then and search
786 * for better APs. */
787 /* TODO: remove expired BSSes */
788
789 ifsta->state = IEEE80211_ASSOCIATED;
790
791 sta = sta_info_get(local, ifsta->bssid);
792 if (!sta) {
Joe Perches0795af52007-10-03 17:59:30 -0700793 printk(KERN_DEBUG "%s: No STA entry for own AP %s\n",
794 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700795 disassoc = 1;
796 } else {
797 disassoc = 0;
798 if (time_after(jiffies,
799 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400800 if (ifsta->flags & IEEE80211_STA_PROBEREQ_POLL) {
Jiri Bencf0706e822007-05-05 11:45:53 -0700801 printk(KERN_DEBUG "%s: No ProbeResp from "
Joe Perches0795af52007-10-03 17:59:30 -0700802 "current AP %s - assume out of "
Jiri Bencf0706e822007-05-05 11:45:53 -0700803 "range\n",
Joe Perches0795af52007-10-03 17:59:30 -0700804 dev->name, print_mac(mac, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700805 disassoc = 1;
Michael Wube8755e2007-07-27 15:43:23 +0200806 sta_info_free(sta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400807 } else
Jiri Bencf0706e822007-05-05 11:45:53 -0700808 ieee80211_send_probe_req(dev, ifsta->bssid,
809 local->scan_ssid,
810 local->scan_ssid_len);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400811 ifsta->flags ^= IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e822007-05-05 11:45:53 -0700812 } else {
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400813 ifsta->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
Jiri Bencf0706e822007-05-05 11:45:53 -0700814 if (time_after(jiffies, ifsta->last_probe +
815 IEEE80211_PROBE_INTERVAL)) {
816 ifsta->last_probe = jiffies;
817 ieee80211_send_probe_req(dev, ifsta->bssid,
818 ifsta->ssid,
819 ifsta->ssid_len);
820 }
821 }
822 sta_info_put(sta);
823 }
824 if (disassoc) {
825 union iwreq_data wrqu;
826 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
827 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
828 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
829 mod_timer(&ifsta->timer, jiffies +
830 IEEE80211_MONITORING_INTERVAL + 30 * HZ);
831 } else {
832 mod_timer(&ifsta->timer, jiffies +
833 IEEE80211_MONITORING_INTERVAL);
834 }
835}
836
837
838static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
839 u8 *ssid, size_t ssid_len)
840{
841 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
842 struct ieee80211_hw_mode *mode;
843 struct sk_buff *skb;
844 struct ieee80211_mgmt *mgmt;
845 u8 *pos, *supp_rates, *esupp_rates = NULL;
846 int i;
847
848 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
849 if (!skb) {
850 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
851 "request\n", dev->name);
852 return;
853 }
854 skb_reserve(skb, local->hw.extra_tx_headroom);
855
856 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
857 memset(mgmt, 0, 24);
858 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
859 IEEE80211_STYPE_PROBE_REQ);
860 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
861 if (dst) {
862 memcpy(mgmt->da, dst, ETH_ALEN);
863 memcpy(mgmt->bssid, dst, ETH_ALEN);
864 } else {
865 memset(mgmt->da, 0xff, ETH_ALEN);
866 memset(mgmt->bssid, 0xff, ETH_ALEN);
867 }
868 pos = skb_put(skb, 2 + ssid_len);
869 *pos++ = WLAN_EID_SSID;
870 *pos++ = ssid_len;
871 memcpy(pos, ssid, ssid_len);
872
873 supp_rates = skb_put(skb, 2);
874 supp_rates[0] = WLAN_EID_SUPP_RATES;
875 supp_rates[1] = 0;
876 mode = local->oper_hw_mode;
877 for (i = 0; i < mode->num_rates; i++) {
878 struct ieee80211_rate *rate = &mode->rates[i];
879 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
880 continue;
881 if (esupp_rates) {
882 pos = skb_put(skb, 1);
883 esupp_rates[1]++;
884 } else if (supp_rates[1] == 8) {
885 esupp_rates = skb_put(skb, 3);
886 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
887 esupp_rates[1] = 1;
888 pos = &esupp_rates[2];
889 } else {
890 pos = skb_put(skb, 1);
891 supp_rates[1]++;
892 }
Johannes Bergb708e612007-09-14 11:10:25 -0400893 *pos = rate->rate / 5;
Jiri Bencf0706e822007-05-05 11:45:53 -0700894 }
895
896 ieee80211_sta_tx(dev, skb, 0);
897}
898
899
900static int ieee80211_sta_wep_configured(struct net_device *dev)
901{
902 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
903 if (!sdata || !sdata->default_key ||
Johannes Berg8f20fc22007-08-28 17:01:54 -0400904 sdata->default_key->conf.alg != ALG_WEP)
Jiri Bencf0706e822007-05-05 11:45:53 -0700905 return 0;
906 return 1;
907}
908
909
910static void ieee80211_auth_completed(struct net_device *dev,
911 struct ieee80211_if_sta *ifsta)
912{
913 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
Jiri Slabyd6f2da52007-08-28 17:01:54 -0400914 ifsta->flags |= IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e822007-05-05 11:45:53 -0700915 ieee80211_associate(dev, ifsta);
916}
917
918
919static void ieee80211_auth_challenge(struct net_device *dev,
920 struct ieee80211_if_sta *ifsta,
921 struct ieee80211_mgmt *mgmt,
922 size_t len)
923{
924 u8 *pos;
925 struct ieee802_11_elems elems;
926
927 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
928 pos = mgmt->u.auth.variable;
929 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
930 == ParseFailed) {
931 printk(KERN_DEBUG "%s: failed to parse Auth(challenge)\n",
932 dev->name);
933 return;
934 }
935 if (!elems.challenge) {
936 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
937 "frame\n", dev->name);
938 return;
939 }
940 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
941 elems.challenge_len + 2, 1);
942}
943
944
945static void ieee80211_rx_mgmt_auth(struct net_device *dev,
946 struct ieee80211_if_sta *ifsta,
947 struct ieee80211_mgmt *mgmt,
948 size_t len)
949{
950 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
951 u16 auth_alg, auth_transaction, status_code;
Joe Perches0795af52007-10-03 17:59:30 -0700952 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -0700953
954 if (ifsta->state != IEEE80211_AUTHENTICATE &&
955 sdata->type != IEEE80211_IF_TYPE_IBSS) {
956 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -0700957 "%s, but not in authenticate state - ignored\n",
958 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -0700959 return;
960 }
961
962 if (len < 24 + 6) {
963 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
Joe Perches0795af52007-10-03 17:59:30 -0700964 "received from %s - ignored\n",
965 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -0700966 return;
967 }
968
969 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
970 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
971 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -0700972 "unknown AP (SA=%s BSSID=%s) - "
973 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
974 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700975 return;
976 }
977
978 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
979 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
980 printk(KERN_DEBUG "%s: authentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -0700981 "unknown BSSID (SA=%s BSSID=%s) - "
982 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
983 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -0700984 return;
985 }
986
987 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
988 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
989 status_code = le16_to_cpu(mgmt->u.auth.status_code);
990
Joe Perches0795af52007-10-03 17:59:30 -0700991 printk(KERN_DEBUG "%s: RX authentication from %s (alg=%d "
Jiri Bencf0706e822007-05-05 11:45:53 -0700992 "transaction=%d status=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700993 dev->name, print_mac(mac, mgmt->sa), auth_alg,
Jiri Bencf0706e822007-05-05 11:45:53 -0700994 auth_transaction, status_code);
995
996 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
997 /* IEEE 802.11 standard does not require authentication in IBSS
998 * networks and most implementations do not seem to use it.
999 * However, try to reply to authentication attempts if someone
1000 * has actually implemented this.
1001 * TODO: Could implement shared key authentication. */
1002 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
1003 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
1004 "frame (alg=%d transaction=%d)\n",
1005 dev->name, auth_alg, auth_transaction);
1006 return;
1007 }
1008 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
1009 }
1010
1011 if (auth_alg != ifsta->auth_alg ||
1012 auth_transaction != ifsta->auth_transaction) {
1013 printk(KERN_DEBUG "%s: unexpected authentication frame "
1014 "(alg=%d transaction=%d)\n",
1015 dev->name, auth_alg, auth_transaction);
1016 return;
1017 }
1018
1019 if (status_code != WLAN_STATUS_SUCCESS) {
1020 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
1021 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
1022 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
1023 u8 algs[3];
1024 const int num_algs = ARRAY_SIZE(algs);
1025 int i, pos;
1026 algs[0] = algs[1] = algs[2] = 0xff;
1027 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1028 algs[0] = WLAN_AUTH_OPEN;
1029 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1030 algs[1] = WLAN_AUTH_SHARED_KEY;
1031 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1032 algs[2] = WLAN_AUTH_LEAP;
1033 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1034 pos = 0;
1035 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1036 pos = 1;
1037 else
1038 pos = 2;
1039 for (i = 0; i < num_algs; i++) {
1040 pos++;
1041 if (pos >= num_algs)
1042 pos = 0;
1043 if (algs[pos] == ifsta->auth_alg ||
1044 algs[pos] == 0xff)
1045 continue;
1046 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1047 !ieee80211_sta_wep_configured(dev))
1048 continue;
1049 ifsta->auth_alg = algs[pos];
1050 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1051 "next try\n",
1052 dev->name, ifsta->auth_alg);
1053 break;
1054 }
1055 }
1056 return;
1057 }
1058
1059 switch (ifsta->auth_alg) {
1060 case WLAN_AUTH_OPEN:
1061 case WLAN_AUTH_LEAP:
1062 ieee80211_auth_completed(dev, ifsta);
1063 break;
1064 case WLAN_AUTH_SHARED_KEY:
1065 if (ifsta->auth_transaction == 4)
1066 ieee80211_auth_completed(dev, ifsta);
1067 else
1068 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1069 break;
1070 }
1071}
1072
1073
1074static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1075 struct ieee80211_if_sta *ifsta,
1076 struct ieee80211_mgmt *mgmt,
1077 size_t len)
1078{
1079 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001080 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001081
1082 if (len < 24 + 2) {
1083 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
Joe Perches0795af52007-10-03 17:59:30 -07001084 "received from %s - ignored\n",
1085 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001086 return;
1087 }
1088
1089 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1090 printk(KERN_DEBUG "%s: deauthentication frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001091 "unknown AP (SA=%s BSSID=%s) - "
1092 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1093 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001094 return;
1095 }
1096
1097 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1098
Joe Perches0795af52007-10-03 17:59:30 -07001099 printk(KERN_DEBUG "%s: RX deauthentication from %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07001100 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001101 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e822007-05-05 11:45:53 -07001102
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001103 if (ifsta->flags & IEEE80211_STA_AUTHENTICATED) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001104 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1105 }
1106
1107 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1108 ifsta->state == IEEE80211_ASSOCIATE ||
1109 ifsta->state == IEEE80211_ASSOCIATED) {
1110 ifsta->state = IEEE80211_AUTHENTICATE;
1111 mod_timer(&ifsta->timer, jiffies +
1112 IEEE80211_RETRY_AUTH_INTERVAL);
1113 }
1114
1115 ieee80211_set_disassoc(dev, ifsta, 1);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001116 ifsta->flags &= ~IEEE80211_STA_AUTHENTICATED;
Jiri Bencf0706e822007-05-05 11:45:53 -07001117}
1118
1119
1120static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1121 struct ieee80211_if_sta *ifsta,
1122 struct ieee80211_mgmt *mgmt,
1123 size_t len)
1124{
1125 u16 reason_code;
Joe Perches0795af52007-10-03 17:59:30 -07001126 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001127
1128 if (len < 24 + 2) {
1129 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
Joe Perches0795af52007-10-03 17:59:30 -07001130 "received from %s - ignored\n",
1131 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001132 return;
1133 }
1134
1135 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1136 printk(KERN_DEBUG "%s: disassociation frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001137 "unknown AP (SA=%s BSSID=%s) - "
1138 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1139 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001140 return;
1141 }
1142
1143 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1144
Joe Perches0795af52007-10-03 17:59:30 -07001145 printk(KERN_DEBUG "%s: RX disassociation from %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07001146 " (reason=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001147 dev->name, print_mac(mac, mgmt->sa), reason_code);
Jiri Bencf0706e822007-05-05 11:45:53 -07001148
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001149 if (ifsta->flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e822007-05-05 11:45:53 -07001150 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1151
1152 if (ifsta->state == IEEE80211_ASSOCIATED) {
1153 ifsta->state = IEEE80211_ASSOCIATE;
1154 mod_timer(&ifsta->timer, jiffies +
1155 IEEE80211_RETRY_AUTH_INTERVAL);
1156 }
1157
1158 ieee80211_set_disassoc(dev, ifsta, 0);
1159}
1160
1161
1162static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev,
1163 struct ieee80211_if_sta *ifsta,
1164 struct ieee80211_mgmt *mgmt,
1165 size_t len,
1166 int reassoc)
1167{
1168 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1169 struct ieee80211_hw_mode *mode;
1170 struct sta_info *sta;
1171 u32 rates;
1172 u16 capab_info, status_code, aid;
1173 struct ieee802_11_elems elems;
1174 u8 *pos;
1175 int i, j;
Joe Perches0795af52007-10-03 17:59:30 -07001176 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001177
1178 /* AssocResp and ReassocResp have identical structure, so process both
1179 * of them in this function. */
1180
1181 if (ifsta->state != IEEE80211_ASSOCIATE) {
1182 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001183 "%s, but not in associate state - ignored\n",
1184 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001185 return;
1186 }
1187
1188 if (len < 24 + 6) {
1189 printk(KERN_DEBUG "%s: too short (%zd) association frame "
Joe Perches0795af52007-10-03 17:59:30 -07001190 "received from %s - ignored\n",
1191 dev->name, len, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001192 return;
1193 }
1194
1195 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1196 printk(KERN_DEBUG "%s: association frame received from "
Joe Perches0795af52007-10-03 17:59:30 -07001197 "unknown AP (SA=%s BSSID=%s) - "
1198 "ignored\n", dev->name, print_mac(mac, mgmt->sa),
1199 print_mac(mac, mgmt->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07001200 return;
1201 }
1202
1203 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1204 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1205 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
Jiri Bencf0706e822007-05-05 11:45:53 -07001206
Joe Perches0795af52007-10-03 17:59:30 -07001207 printk(KERN_DEBUG "%s: RX %sssocResp from %s (capab=0x%x "
Jiri Bencf0706e822007-05-05 11:45:53 -07001208 "status=%d aid=%d)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001209 dev->name, reassoc ? "Rea" : "A", print_mac(mac, mgmt->sa),
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001210 capab_info, status_code, aid & ~(BIT(15) | BIT(14)));
Jiri Bencf0706e822007-05-05 11:45:53 -07001211
1212 if (status_code != WLAN_STATUS_SUCCESS) {
1213 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1214 dev->name, status_code);
Daniel Drake8a69aa92007-07-27 15:43:23 +02001215 /* if this was a reassociation, ensure we try a "full"
1216 * association next time. This works around some broken APs
1217 * which do not correctly reject reassociation requests. */
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001218 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07001219 return;
1220 }
1221
Johannes Berg1dd84aa2007-10-10 12:03:41 +02001222 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1223 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1224 "set\n", dev->name, aid);
1225 aid &= ~(BIT(15) | BIT(14));
1226
Jiri Bencf0706e822007-05-05 11:45:53 -07001227 pos = mgmt->u.assoc_resp.variable;
1228 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
1229 == ParseFailed) {
1230 printk(KERN_DEBUG "%s: failed to parse AssocResp\n",
1231 dev->name);
1232 return;
1233 }
1234
1235 if (!elems.supp_rates) {
1236 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1237 dev->name);
1238 return;
1239 }
1240
Daniel Drake56282212007-07-10 19:32:10 +02001241 /* it probably doesn't, but if the frame includes an ERP value then
1242 * update our stored copy */
1243 if (elems.erp_info && elems.erp_info_len >= 1) {
1244 struct ieee80211_sta_bss *bss
1245 = ieee80211_rx_bss_get(dev, ifsta->bssid);
1246 if (bss) {
1247 bss->erp_value = elems.erp_info[0];
1248 bss->has_erp_value = 1;
1249 ieee80211_rx_bss_put(dev, bss);
1250 }
1251 }
1252
Jiri Bencf0706e822007-05-05 11:45:53 -07001253 printk(KERN_DEBUG "%s: associated\n", dev->name);
1254 ifsta->aid = aid;
1255 ifsta->ap_capab = capab_info;
1256
1257 kfree(ifsta->assocresp_ies);
1258 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001259 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07001260 if (ifsta->assocresp_ies)
1261 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1262
1263 ieee80211_set_associated(dev, ifsta, 1);
1264
1265 /* Add STA entry for the AP */
1266 sta = sta_info_get(local, ifsta->bssid);
1267 if (!sta) {
1268 struct ieee80211_sta_bss *bss;
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001269 sta = sta_info_add(local, dev, ifsta->bssid, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07001270 if (!sta) {
1271 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1272 " AP\n", dev->name);
1273 return;
1274 }
1275 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
1276 if (bss) {
1277 sta->last_rssi = bss->rssi;
1278 sta->last_signal = bss->signal;
1279 sta->last_noise = bss->noise;
1280 ieee80211_rx_bss_put(dev, bss);
1281 }
1282 }
1283
1284 sta->dev = dev;
Johannes Bergc9ee23d2007-08-28 17:01:55 -04001285 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP;
Jiri Bencf0706e822007-05-05 11:45:53 -07001286
1287 rates = 0;
1288 mode = local->oper_hw_mode;
1289 for (i = 0; i < elems.supp_rates_len; i++) {
1290 int rate = (elems.supp_rates[i] & 0x7f) * 5;
Jiri Bencf0706e822007-05-05 11:45:53 -07001291 for (j = 0; j < mode->num_rates; j++)
1292 if (mode->rates[j].rate == rate)
1293 rates |= BIT(j);
1294 }
1295 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1296 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
Jiri Bencf0706e822007-05-05 11:45:53 -07001297 for (j = 0; j < mode->num_rates; j++)
1298 if (mode->rates[j].rate == rate)
1299 rates |= BIT(j);
1300 }
1301 sta->supp_rates = rates;
1302
1303 rate_control_rate_init(sta, local);
1304
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001305 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001306 sta->flags |= WLAN_STA_WME;
1307 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1308 elems.wmm_param_len);
1309 }
1310
1311
1312 sta_info_put(sta);
1313
1314 ieee80211_associated(dev, ifsta);
1315}
1316
1317
1318/* Caller must hold local->sta_bss_lock */
1319static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1320 struct ieee80211_sta_bss *bss)
1321{
1322 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1323 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1324 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1325}
1326
1327
1328/* Caller must hold local->sta_bss_lock */
1329static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1330 struct ieee80211_sta_bss *bss)
1331{
1332 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1333 struct ieee80211_sta_bss *b, *prev = NULL;
1334 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1335 while (b) {
1336 if (b == bss) {
1337 if (!prev)
1338 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1339 bss->hnext;
1340 else
1341 prev->hnext = bss->hnext;
1342 break;
1343 }
1344 prev = b;
1345 b = b->hnext;
1346 }
1347}
1348
1349
1350static struct ieee80211_sta_bss *
1351ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid)
1352{
1353 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1354 struct ieee80211_sta_bss *bss;
1355
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001356 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e822007-05-05 11:45:53 -07001357 if (!bss)
1358 return NULL;
Jiri Bencf0706e822007-05-05 11:45:53 -07001359 atomic_inc(&bss->users);
1360 atomic_inc(&bss->users);
1361 memcpy(bss->bssid, bssid, ETH_ALEN);
1362
1363 spin_lock_bh(&local->sta_bss_lock);
1364 /* TODO: order by RSSI? */
1365 list_add_tail(&bss->list, &local->sta_bss_list);
1366 __ieee80211_rx_bss_hash_add(dev, bss);
1367 spin_unlock_bh(&local->sta_bss_lock);
1368 return bss;
1369}
1370
1371
1372static struct ieee80211_sta_bss *
1373ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid)
1374{
1375 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1376 struct ieee80211_sta_bss *bss;
1377
1378 spin_lock_bh(&local->sta_bss_lock);
1379 bss = local->sta_bss_hash[STA_HASH(bssid)];
1380 while (bss) {
1381 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1382 atomic_inc(&bss->users);
1383 break;
1384 }
1385 bss = bss->hnext;
1386 }
1387 spin_unlock_bh(&local->sta_bss_lock);
1388 return bss;
1389}
1390
1391
1392static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1393{
1394 kfree(bss->wpa_ie);
1395 kfree(bss->rsn_ie);
1396 kfree(bss->wmm_ie);
1397 kfree(bss);
1398}
1399
1400
1401static void ieee80211_rx_bss_put(struct net_device *dev,
1402 struct ieee80211_sta_bss *bss)
1403{
1404 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1405 if (!atomic_dec_and_test(&bss->users))
1406 return;
1407
1408 spin_lock_bh(&local->sta_bss_lock);
1409 __ieee80211_rx_bss_hash_del(dev, bss);
1410 list_del(&bss->list);
1411 spin_unlock_bh(&local->sta_bss_lock);
1412 ieee80211_rx_bss_free(bss);
1413}
1414
1415
1416void ieee80211_rx_bss_list_init(struct net_device *dev)
1417{
1418 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1419 spin_lock_init(&local->sta_bss_lock);
1420 INIT_LIST_HEAD(&local->sta_bss_list);
1421}
1422
1423
1424void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1425{
1426 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1427 struct ieee80211_sta_bss *bss, *tmp;
1428
1429 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1430 ieee80211_rx_bss_put(dev, bss);
1431}
1432
1433
1434static void ieee80211_rx_bss_info(struct net_device *dev,
1435 struct ieee80211_mgmt *mgmt,
1436 size_t len,
1437 struct ieee80211_rx_status *rx_status,
1438 int beacon)
1439{
1440 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1441 struct ieee802_11_elems elems;
1442 size_t baselen;
1443 int channel, invalid = 0, clen;
1444 struct ieee80211_sta_bss *bss;
1445 struct sta_info *sta;
1446 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1447 u64 timestamp;
Joe Perches0795af52007-10-03 17:59:30 -07001448 DECLARE_MAC_BUF(mac);
1449 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e822007-05-05 11:45:53 -07001450
1451 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1452 return; /* ignore ProbeResp to foreign address */
1453
1454#if 0
Joe Perches0795af52007-10-03 17:59:30 -07001455 printk(KERN_DEBUG "%s: RX %s from %s to %s\n",
Jiri Bencf0706e822007-05-05 11:45:53 -07001456 dev->name, beacon ? "Beacon" : "Probe Response",
Joe Perches0795af52007-10-03 17:59:30 -07001457 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da));
Jiri Bencf0706e822007-05-05 11:45:53 -07001458#endif
1459
1460 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1461 if (baselen > len)
1462 return;
1463
1464 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1465
1466 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1467 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1468#ifdef CONFIG_MAC80211_IBSS_DEBUG
1469 static unsigned long last_tsf_debug = 0;
1470 u64 tsf;
1471 if (local->ops->get_tsf)
1472 tsf = local->ops->get_tsf(local_to_hw(local));
1473 else
1474 tsf = -1LLU;
1475 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
Joe Perches0795af52007-10-03 17:59:30 -07001476 printk(KERN_DEBUG "RX beacon SA=%s BSSID="
1477 "%s TSF=0x%llx BCN=0x%llx diff=%lld "
Jiri Bencf0706e822007-05-05 11:45:53 -07001478 "@%lu\n",
Joe Perches0795af52007-10-03 17:59:30 -07001479 print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->bssid),
Jiri Bencf0706e822007-05-05 11:45:53 -07001480 (unsigned long long)tsf,
1481 (unsigned long long)timestamp,
1482 (unsigned long long)(tsf - timestamp),
1483 jiffies);
1484 last_tsf_debug = jiffies;
1485 }
1486#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1487 }
1488
1489 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1490 &elems) == ParseFailed)
1491 invalid = 1;
1492
1493 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1494 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1495 (sta = sta_info_get(local, mgmt->sa))) {
1496 struct ieee80211_hw_mode *mode;
1497 struct ieee80211_rate *rates;
1498 size_t num_rates;
1499 u32 supp_rates, prev_rates;
1500 int i, j;
1501
1502 mode = local->sta_scanning ?
1503 local->scan_hw_mode : local->oper_hw_mode;
1504 rates = mode->rates;
1505 num_rates = mode->num_rates;
1506
1507 supp_rates = 0;
1508 for (i = 0; i < elems.supp_rates_len +
1509 elems.ext_supp_rates_len; i++) {
1510 u8 rate = 0;
1511 int own_rate;
1512 if (i < elems.supp_rates_len)
1513 rate = elems.supp_rates[i];
1514 else if (elems.ext_supp_rates)
1515 rate = elems.ext_supp_rates
1516 [i - elems.supp_rates_len];
1517 own_rate = 5 * (rate & 0x7f);
Jiri Bencf0706e822007-05-05 11:45:53 -07001518 for (j = 0; j < num_rates; j++)
1519 if (rates[j].rate == own_rate)
1520 supp_rates |= BIT(j);
1521 }
1522
1523 prev_rates = sta->supp_rates;
1524 sta->supp_rates &= supp_rates;
1525 if (sta->supp_rates == 0) {
1526 /* No matching rates - this should not really happen.
1527 * Make sure that at least one rate is marked
1528 * supported to avoid issues with TX rate ctrl. */
1529 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1530 }
1531 if (sta->supp_rates != prev_rates) {
1532 printk(KERN_DEBUG "%s: updated supp_rates set for "
Joe Perches0795af52007-10-03 17:59:30 -07001533 "%s based on beacon info (0x%x & 0x%x -> "
Jiri Bencf0706e822007-05-05 11:45:53 -07001534 "0x%x)\n",
Joe Perches0795af52007-10-03 17:59:30 -07001535 dev->name, print_mac(mac, sta->addr), prev_rates,
Jiri Bencf0706e822007-05-05 11:45:53 -07001536 supp_rates, sta->supp_rates);
1537 }
1538 sta_info_put(sta);
1539 }
1540
1541 if (!elems.ssid)
1542 return;
1543
1544 if (elems.ds_params && elems.ds_params_len == 1)
1545 channel = elems.ds_params[0];
1546 else
1547 channel = rx_status->channel;
1548
1549 bss = ieee80211_rx_bss_get(dev, mgmt->bssid);
1550 if (!bss) {
1551 bss = ieee80211_rx_bss_add(dev, mgmt->bssid);
1552 if (!bss)
1553 return;
1554 } else {
1555#if 0
1556 /* TODO: order by RSSI? */
1557 spin_lock_bh(&local->sta_bss_lock);
1558 list_move_tail(&bss->list, &local->sta_bss_list);
1559 spin_unlock_bh(&local->sta_bss_lock);
1560#endif
1561 }
1562
1563 if (bss->probe_resp && beacon) {
1564 /* Do not allow beacon to override data from Probe Response. */
1565 ieee80211_rx_bss_put(dev, bss);
1566 return;
1567 }
1568
Daniel Drake56282212007-07-10 19:32:10 +02001569 /* save the ERP value so that it is available at association time */
1570 if (elems.erp_info && elems.erp_info_len >= 1) {
1571 bss->erp_value = elems.erp_info[0];
1572 bss->has_erp_value = 1;
1573 }
1574
Jiri Bencf0706e822007-05-05 11:45:53 -07001575 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1576 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1577 if (elems.ssid && elems.ssid_len <= IEEE80211_MAX_SSID_LEN) {
1578 memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1579 bss->ssid_len = elems.ssid_len;
1580 }
1581
1582 bss->supp_rates_len = 0;
1583 if (elems.supp_rates) {
1584 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1585 if (clen > elems.supp_rates_len)
1586 clen = elems.supp_rates_len;
1587 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1588 clen);
1589 bss->supp_rates_len += clen;
1590 }
1591 if (elems.ext_supp_rates) {
1592 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1593 if (clen > elems.ext_supp_rates_len)
1594 clen = elems.ext_supp_rates_len;
1595 memcpy(&bss->supp_rates[bss->supp_rates_len],
1596 elems.ext_supp_rates, clen);
1597 bss->supp_rates_len += clen;
1598 }
1599
1600 if (elems.wpa &&
1601 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1602 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1603 kfree(bss->wpa_ie);
1604 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1605 if (bss->wpa_ie) {
1606 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1607 bss->wpa_ie_len = elems.wpa_len + 2;
1608 } else
1609 bss->wpa_ie_len = 0;
1610 } else if (!elems.wpa && bss->wpa_ie) {
1611 kfree(bss->wpa_ie);
1612 bss->wpa_ie = NULL;
1613 bss->wpa_ie_len = 0;
1614 }
1615
1616 if (elems.rsn &&
1617 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1618 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1619 kfree(bss->rsn_ie);
1620 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1621 if (bss->rsn_ie) {
1622 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1623 bss->rsn_ie_len = elems.rsn_len + 2;
1624 } else
1625 bss->rsn_ie_len = 0;
1626 } else if (!elems.rsn && bss->rsn_ie) {
1627 kfree(bss->rsn_ie);
1628 bss->rsn_ie = NULL;
1629 bss->rsn_ie_len = 0;
1630 }
1631
1632 if (elems.wmm_param &&
1633 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1634 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1635 kfree(bss->wmm_ie);
1636 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1637 if (bss->wmm_ie) {
1638 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1639 elems.wmm_param_len + 2);
1640 bss->wmm_ie_len = elems.wmm_param_len + 2;
1641 } else
1642 bss->wmm_ie_len = 0;
1643 } else if (!elems.wmm_param && bss->wmm_ie) {
1644 kfree(bss->wmm_ie);
1645 bss->wmm_ie = NULL;
1646 bss->wmm_ie_len = 0;
1647 }
1648
1649
1650 bss->hw_mode = rx_status->phymode;
1651 bss->channel = channel;
1652 bss->freq = rx_status->freq;
1653 if (channel != rx_status->channel &&
1654 (bss->hw_mode == MODE_IEEE80211G ||
1655 bss->hw_mode == MODE_IEEE80211B) &&
1656 channel >= 1 && channel <= 14) {
1657 static const int freq_list[] = {
1658 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1659 2447, 2452, 2457, 2462, 2467, 2472, 2484
1660 };
1661 /* IEEE 802.11g/b mode can receive packets from neighboring
1662 * channels, so map the channel into frequency. */
1663 bss->freq = freq_list[channel - 1];
1664 }
1665 bss->timestamp = timestamp;
1666 bss->last_update = jiffies;
1667 bss->rssi = rx_status->ssi;
1668 bss->signal = rx_status->signal;
1669 bss->noise = rx_status->noise;
1670 if (!beacon)
1671 bss->probe_resp++;
1672 ieee80211_rx_bss_put(dev, bss);
1673}
1674
1675
1676static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1677 struct ieee80211_mgmt *mgmt,
1678 size_t len,
1679 struct ieee80211_rx_status *rx_status)
1680{
1681 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1682}
1683
1684
1685static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1686 struct ieee80211_mgmt *mgmt,
1687 size_t len,
1688 struct ieee80211_rx_status *rx_status)
1689{
Jiri Bencf0706e822007-05-05 11:45:53 -07001690 struct ieee80211_sub_if_data *sdata;
1691 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e822007-05-05 11:45:53 -07001692 size_t baselen;
1693 struct ieee802_11_elems elems;
1694
1695 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1696
1697 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1698 if (sdata->type != IEEE80211_IF_TYPE_STA)
1699 return;
1700 ifsta = &sdata->u.sta;
1701
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001702 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED) ||
Jiri Bencf0706e822007-05-05 11:45:53 -07001703 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1704 return;
1705
1706 /* Process beacon from the current BSS */
1707 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1708 if (baselen > len)
1709 return;
1710
1711 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1712 &elems) == ParseFailed)
1713 return;
1714
Daniel Drake56282212007-07-10 19:32:10 +02001715 if (elems.erp_info && elems.erp_info_len >= 1)
1716 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
Jiri Bencf0706e822007-05-05 11:45:53 -07001717
Jiri Slabyd6f2da52007-08-28 17:01:54 -04001718 if (elems.wmm_param && (ifsta->flags & IEEE80211_STA_WMM_ENABLED)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07001719 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1720 elems.wmm_param_len);
1721 }
1722}
1723
1724
1725static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1726 struct ieee80211_if_sta *ifsta,
1727 struct ieee80211_mgmt *mgmt,
1728 size_t len,
1729 struct ieee80211_rx_status *rx_status)
1730{
1731 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1732 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1733 int tx_last_beacon;
1734 struct sk_buff *skb;
1735 struct ieee80211_mgmt *resp;
1736 u8 *pos, *end;
Joe Perches0795af52007-10-03 17:59:30 -07001737 DECLARE_MAC_BUF(mac);
1738#ifdef CONFIG_MAC80211_IBSS_DEBUG
1739 DECLARE_MAC_BUF(mac2);
1740 DECLARE_MAC_BUF(mac3);
1741#endif
Jiri Bencf0706e822007-05-05 11:45:53 -07001742
1743 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1744 ifsta->state != IEEE80211_IBSS_JOINED ||
1745 len < 24 + 2 || !ifsta->probe_resp)
1746 return;
1747
1748 if (local->ops->tx_last_beacon)
1749 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1750 else
1751 tx_last_beacon = 1;
1752
1753#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07001754 printk(KERN_DEBUG "%s: RX ProbeReq SA=%s DA=%s BSSID="
1755 "%s (tx_last_beacon=%d)\n",
1756 dev->name, print_mac(mac, mgmt->sa), print_mac(mac2, mgmt->da),
1757 print_mac(mac3, mgmt->bssid), tx_last_beacon);
Jiri Bencf0706e822007-05-05 11:45:53 -07001758#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1759
1760 if (!tx_last_beacon)
1761 return;
1762
1763 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1764 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1765 return;
1766
1767 end = ((u8 *) mgmt) + len;
1768 pos = mgmt->u.probe_req.variable;
1769 if (pos[0] != WLAN_EID_SSID ||
1770 pos + 2 + pos[1] > end) {
1771 if (net_ratelimit()) {
1772 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
Joe Perches0795af52007-10-03 17:59:30 -07001773 "from %s\n",
1774 dev->name, print_mac(mac, mgmt->sa));
Jiri Bencf0706e822007-05-05 11:45:53 -07001775 }
1776 return;
1777 }
1778 if (pos[1] != 0 &&
1779 (pos[1] != ifsta->ssid_len ||
1780 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1781 /* Ignore ProbeReq for foreign SSID */
1782 return;
1783 }
1784
1785 /* Reply with ProbeResp */
Michael Wu0ec0b7a2007-07-27 15:43:24 +02001786 skb = skb_copy(ifsta->probe_resp, GFP_KERNEL);
Jiri Bencf0706e822007-05-05 11:45:53 -07001787 if (!skb)
1788 return;
1789
1790 resp = (struct ieee80211_mgmt *) skb->data;
1791 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1792#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07001793 printk(KERN_DEBUG "%s: Sending ProbeResp to %s\n",
1794 dev->name, print_mac(mac, resp->da));
Jiri Bencf0706e822007-05-05 11:45:53 -07001795#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1796 ieee80211_sta_tx(dev, skb, 0);
1797}
1798
1799
1800void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1801 struct ieee80211_rx_status *rx_status)
1802{
1803 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1804 struct ieee80211_sub_if_data *sdata;
1805 struct ieee80211_if_sta *ifsta;
1806 struct ieee80211_mgmt *mgmt;
1807 u16 fc;
1808
1809 if (skb->len < 24)
1810 goto fail;
1811
1812 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1813 ifsta = &sdata->u.sta;
1814
1815 mgmt = (struct ieee80211_mgmt *) skb->data;
1816 fc = le16_to_cpu(mgmt->frame_control);
1817
1818 switch (fc & IEEE80211_FCTL_STYPE) {
1819 case IEEE80211_STYPE_PROBE_REQ:
1820 case IEEE80211_STYPE_PROBE_RESP:
1821 case IEEE80211_STYPE_BEACON:
1822 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1823 case IEEE80211_STYPE_AUTH:
1824 case IEEE80211_STYPE_ASSOC_RESP:
1825 case IEEE80211_STYPE_REASSOC_RESP:
1826 case IEEE80211_STYPE_DEAUTH:
1827 case IEEE80211_STYPE_DISASSOC:
1828 skb_queue_tail(&ifsta->skb_queue, skb);
1829 queue_work(local->hw.workqueue, &ifsta->work);
1830 return;
1831 default:
1832 printk(KERN_DEBUG "%s: received unknown management frame - "
1833 "stype=%d\n", dev->name,
1834 (fc & IEEE80211_FCTL_STYPE) >> 4);
1835 break;
1836 }
1837
1838 fail:
1839 kfree_skb(skb);
1840}
1841
1842
1843static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1844 struct sk_buff *skb)
1845{
1846 struct ieee80211_rx_status *rx_status;
1847 struct ieee80211_sub_if_data *sdata;
1848 struct ieee80211_if_sta *ifsta;
1849 struct ieee80211_mgmt *mgmt;
1850 u16 fc;
1851
1852 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1853 ifsta = &sdata->u.sta;
1854
1855 rx_status = (struct ieee80211_rx_status *) skb->cb;
1856 mgmt = (struct ieee80211_mgmt *) skb->data;
1857 fc = le16_to_cpu(mgmt->frame_control);
1858
1859 switch (fc & IEEE80211_FCTL_STYPE) {
1860 case IEEE80211_STYPE_PROBE_REQ:
1861 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1862 rx_status);
1863 break;
1864 case IEEE80211_STYPE_PROBE_RESP:
1865 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1866 break;
1867 case IEEE80211_STYPE_BEACON:
1868 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1869 break;
1870 case IEEE80211_STYPE_AUTH:
1871 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1872 break;
1873 case IEEE80211_STYPE_ASSOC_RESP:
1874 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1875 break;
1876 case IEEE80211_STYPE_REASSOC_RESP:
1877 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1878 break;
1879 case IEEE80211_STYPE_DEAUTH:
1880 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1881 break;
1882 case IEEE80211_STYPE_DISASSOC:
1883 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1884 break;
1885 }
1886
1887 kfree_skb(skb);
1888}
1889
1890
1891void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1892 struct ieee80211_rx_status *rx_status)
1893{
1894 struct ieee80211_mgmt *mgmt;
1895 u16 fc;
1896
1897 if (skb->len < 24) {
1898 dev_kfree_skb(skb);
1899 return;
1900 }
1901
1902 mgmt = (struct ieee80211_mgmt *) skb->data;
1903 fc = le16_to_cpu(mgmt->frame_control);
1904
1905 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1906 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1907 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1908 skb->len, rx_status);
1909 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1910 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1911 rx_status);
1912 }
1913 }
1914
1915 dev_kfree_skb(skb);
1916}
1917
1918
1919static int ieee80211_sta_active_ibss(struct net_device *dev)
1920{
1921 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1922 int active = 0;
1923 struct sta_info *sta;
1924
Michael Wube8755e2007-07-27 15:43:23 +02001925 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e822007-05-05 11:45:53 -07001926 list_for_each_entry(sta, &local->sta_list, list) {
1927 if (sta->dev == dev &&
1928 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1929 jiffies)) {
1930 active++;
1931 break;
1932 }
1933 }
Michael Wube8755e2007-07-27 15:43:23 +02001934 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e822007-05-05 11:45:53 -07001935
1936 return active;
1937}
1938
1939
1940static void ieee80211_sta_expire(struct net_device *dev)
1941{
1942 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1943 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02001944 LIST_HEAD(tmp_list);
Joe Perches0795af52007-10-03 17:59:30 -07001945 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07001946
Michael Wube8755e2007-07-27 15:43:23 +02001947 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e822007-05-05 11:45:53 -07001948 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1949 if (time_after(jiffies, sta->last_rx +
1950 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
Joe Perches0795af52007-10-03 17:59:30 -07001951 printk(KERN_DEBUG "%s: expiring inactive STA %s\n",
1952 dev->name, print_mac(mac, sta->addr));
Michael Wube8755e2007-07-27 15:43:23 +02001953 __sta_info_get(sta);
1954 sta_info_remove(sta);
1955 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e822007-05-05 11:45:53 -07001956 }
Michael Wube8755e2007-07-27 15:43:23 +02001957 write_unlock_bh(&local->sta_lock);
1958
1959 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
1960 sta_info_free(sta);
1961 sta_info_put(sta);
1962 }
Jiri Bencf0706e822007-05-05 11:45:53 -07001963}
1964
1965
1966static void ieee80211_sta_merge_ibss(struct net_device *dev,
1967 struct ieee80211_if_sta *ifsta)
1968{
1969 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1970
1971 ieee80211_sta_expire(dev);
1972 if (ieee80211_sta_active_ibss(dev))
1973 return;
1974
1975 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1976 "IBSS networks with same SSID (merge)\n", dev->name);
1977 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1978}
1979
1980
1981void ieee80211_sta_timer(unsigned long data)
1982{
1983 struct ieee80211_sub_if_data *sdata =
1984 (struct ieee80211_sub_if_data *) data;
1985 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1986 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
1987
1988 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1989 queue_work(local->hw.workqueue, &ifsta->work);
1990}
1991
1992
1993void ieee80211_sta_work(struct work_struct *work)
1994{
1995 struct ieee80211_sub_if_data *sdata =
1996 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
1997 struct net_device *dev = sdata->dev;
1998 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1999 struct ieee80211_if_sta *ifsta;
2000 struct sk_buff *skb;
2001
2002 if (!netif_running(dev))
2003 return;
2004
2005 if (local->sta_scanning)
2006 return;
2007
2008 if (sdata->type != IEEE80211_IF_TYPE_STA &&
2009 sdata->type != IEEE80211_IF_TYPE_IBSS) {
2010 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
2011 "(type=%d)\n", dev->name, sdata->type);
2012 return;
2013 }
2014 ifsta = &sdata->u.sta;
2015
2016 while ((skb = skb_dequeue(&ifsta->skb_queue)))
2017 ieee80211_sta_rx_queued_mgmt(dev, skb);
2018
2019 if (ifsta->state != IEEE80211_AUTHENTICATE &&
2020 ifsta->state != IEEE80211_ASSOCIATE &&
2021 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
2022 ieee80211_sta_start_scan(dev, NULL, 0);
2023 return;
2024 }
2025
2026 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
2027 if (ieee80211_sta_config_auth(dev, ifsta))
2028 return;
2029 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
2030 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
2031 return;
2032
2033 switch (ifsta->state) {
2034 case IEEE80211_DISABLED:
2035 break;
2036 case IEEE80211_AUTHENTICATE:
2037 ieee80211_authenticate(dev, ifsta);
2038 break;
2039 case IEEE80211_ASSOCIATE:
2040 ieee80211_associate(dev, ifsta);
2041 break;
2042 case IEEE80211_ASSOCIATED:
2043 ieee80211_associated(dev, ifsta);
2044 break;
2045 case IEEE80211_IBSS_SEARCH:
2046 ieee80211_sta_find_ibss(dev, ifsta);
2047 break;
2048 case IEEE80211_IBSS_JOINED:
2049 ieee80211_sta_merge_ibss(dev, ifsta);
2050 break;
2051 default:
2052 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2053 ifsta->state);
2054 break;
2055 }
2056
2057 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2058 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2059 "mixed-cell disabled - disassociate\n", dev->name);
2060
2061 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2062 ieee80211_set_disassoc(dev, ifsta, 0);
2063 }
2064}
2065
2066
2067static void ieee80211_sta_reset_auth(struct net_device *dev,
2068 struct ieee80211_if_sta *ifsta)
2069{
2070 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2071
2072 if (local->ops->reset_tsf) {
2073 /* Reset own TSF to allow time synchronization work. */
2074 local->ops->reset_tsf(local_to_hw(local));
2075 }
2076
2077 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2078
2079
2080 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2081 ifsta->auth_alg = WLAN_AUTH_OPEN;
2082 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2083 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2084 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2085 ifsta->auth_alg = WLAN_AUTH_LEAP;
2086 else
2087 ifsta->auth_alg = WLAN_AUTH_OPEN;
2088 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2089 ifsta->auth_alg);
2090 ifsta->auth_transaction = -1;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002091 ifsta->flags &= ~IEEE80211_STA_ASSOCIATED;
2092 ifsta->auth_tries = ifsta->assoc_tries = 0;
Jiri Bencf0706e822007-05-05 11:45:53 -07002093 netif_carrier_off(dev);
2094}
2095
2096
2097void ieee80211_sta_req_auth(struct net_device *dev,
2098 struct ieee80211_if_sta *ifsta)
2099{
2100 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2101 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2102
2103 if (sdata->type != IEEE80211_IF_TYPE_STA)
2104 return;
2105
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002106 if ((ifsta->flags & (IEEE80211_STA_BSSID_SET |
2107 IEEE80211_STA_AUTO_BSSID_SEL)) &&
2108 (ifsta->flags & (IEEE80211_STA_SSID_SET |
2109 IEEE80211_STA_AUTO_SSID_SEL))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002110 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2111 queue_work(local->hw.workqueue, &ifsta->work);
2112 }
2113}
2114
2115static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2116 const char *ssid, int ssid_len)
2117{
2118 int tmp, hidden_ssid;
2119
2120 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2121 return 1;
2122
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002123 if (ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL)
Jiri Bencf0706e822007-05-05 11:45:53 -07002124 return 0;
2125
2126 hidden_ssid = 1;
2127 tmp = ssid_len;
2128 while (tmp--) {
2129 if (ssid[tmp] != '\0') {
2130 hidden_ssid = 0;
2131 break;
2132 }
2133 }
2134
2135 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2136 return 1;
2137
2138 if (ssid_len == 1 && ssid[0] == ' ')
2139 return 1;
2140
2141 return 0;
2142}
2143
2144static int ieee80211_sta_config_auth(struct net_device *dev,
2145 struct ieee80211_if_sta *ifsta)
2146{
2147 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2148 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2149 struct ieee80211_sta_bss *bss, *selected = NULL;
2150 int top_rssi = 0, freq;
2151
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002152 if (!(ifsta->flags & (IEEE80211_STA_AUTO_SSID_SEL |
2153 IEEE80211_STA_AUTO_BSSID_SEL | IEEE80211_STA_AUTO_CHANNEL_SEL))) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002154 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e822007-05-05 11:45:53 -07002155 ieee80211_sta_reset_auth(dev, ifsta);
2156 return 0;
2157 }
2158
2159 spin_lock_bh(&local->sta_bss_lock);
2160 freq = local->oper_channel->freq;
2161 list_for_each_entry(bss, &local->sta_bss_list, list) {
2162 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2163 continue;
2164
2165 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2166 !!sdata->default_key)
2167 continue;
2168
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002169 if (!(ifsta->flags & IEEE80211_STA_AUTO_CHANNEL_SEL) &&
2170 bss->freq != freq)
Jiri Bencf0706e822007-05-05 11:45:53 -07002171 continue;
2172
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002173 if (!(ifsta->flags & IEEE80211_STA_AUTO_BSSID_SEL) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07002174 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2175 continue;
2176
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002177 if (!(ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07002178 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2179 continue;
2180
2181 if (!selected || top_rssi < bss->rssi) {
2182 selected = bss;
2183 top_rssi = bss->rssi;
2184 }
2185 }
2186 if (selected)
2187 atomic_inc(&selected->users);
2188 spin_unlock_bh(&local->sta_bss_lock);
2189
2190 if (selected) {
2191 ieee80211_set_channel(local, -1, selected->freq);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002192 if (!(ifsta->flags & IEEE80211_STA_SSID_SET))
Jiri Bencf0706e822007-05-05 11:45:53 -07002193 ieee80211_sta_set_ssid(dev, selected->ssid,
2194 selected->ssid_len);
2195 ieee80211_sta_set_bssid(dev, selected->bssid);
2196 ieee80211_rx_bss_put(dev, selected);
2197 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e822007-05-05 11:45:53 -07002198 ieee80211_sta_reset_auth(dev, ifsta);
2199 return 0;
2200 } else {
2201 if (ifsta->state != IEEE80211_AUTHENTICATE) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002202 if (ifsta->flags & IEEE80211_STA_AUTO_SSID_SEL)
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04002203 ieee80211_sta_start_scan(dev, NULL, 0);
2204 else
2205 ieee80211_sta_start_scan(dev, ifsta->ssid,
2206 ifsta->ssid_len);
Jiri Bencf0706e822007-05-05 11:45:53 -07002207 ifsta->state = IEEE80211_AUTHENTICATE;
2208 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2209 } else
2210 ifsta->state = IEEE80211_DISABLED;
2211 }
Jiri Bencf0706e822007-05-05 11:45:53 -07002212 return -1;
2213}
2214
2215static int ieee80211_sta_join_ibss(struct net_device *dev,
2216 struct ieee80211_if_sta *ifsta,
2217 struct ieee80211_sta_bss *bss)
2218{
2219 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2220 int res, rates, i, j;
2221 struct sk_buff *skb;
2222 struct ieee80211_mgmt *mgmt;
2223 struct ieee80211_tx_control control;
2224 struct ieee80211_rate *rate;
2225 struct ieee80211_hw_mode *mode;
2226 struct rate_control_extra extra;
2227 u8 *pos;
2228 struct ieee80211_sub_if_data *sdata;
2229
2230 /* Remove possible STA entries from other IBSS networks. */
2231 sta_info_flush(local, NULL);
2232
2233 if (local->ops->reset_tsf) {
2234 /* Reset own TSF to allow time synchronization work. */
2235 local->ops->reset_tsf(local_to_hw(local));
2236 }
2237 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2238 res = ieee80211_if_config(dev);
2239 if (res)
2240 return res;
2241
2242 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2243
2244 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2245 sdata->drop_unencrypted = bss->capability &
2246 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2247
2248 res = ieee80211_set_channel(local, -1, bss->freq);
2249
2250 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2251 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2252 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2253 local->hw.conf.freq);
2254 return -1;
2255 }
2256
2257 /* Set beacon template based on scan results */
2258 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2259 do {
2260 if (!skb)
2261 break;
2262
2263 skb_reserve(skb, local->hw.extra_tx_headroom);
2264
2265 mgmt = (struct ieee80211_mgmt *)
2266 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2267 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2268 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2269 IEEE80211_STYPE_BEACON);
2270 memset(mgmt->da, 0xff, ETH_ALEN);
2271 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2272 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2273 mgmt->u.beacon.beacon_int =
2274 cpu_to_le16(local->hw.conf.beacon_int);
2275 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2276
2277 pos = skb_put(skb, 2 + ifsta->ssid_len);
2278 *pos++ = WLAN_EID_SSID;
2279 *pos++ = ifsta->ssid_len;
2280 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2281
2282 rates = bss->supp_rates_len;
2283 if (rates > 8)
2284 rates = 8;
2285 pos = skb_put(skb, 2 + rates);
2286 *pos++ = WLAN_EID_SUPP_RATES;
2287 *pos++ = rates;
2288 memcpy(pos, bss->supp_rates, rates);
2289
2290 pos = skb_put(skb, 2 + 1);
2291 *pos++ = WLAN_EID_DS_PARAMS;
2292 *pos++ = 1;
2293 *pos++ = bss->channel;
2294
2295 pos = skb_put(skb, 2 + 2);
2296 *pos++ = WLAN_EID_IBSS_PARAMS;
2297 *pos++ = 2;
2298 /* FIX: set ATIM window based on scan results */
2299 *pos++ = 0;
2300 *pos++ = 0;
2301
2302 if (bss->supp_rates_len > 8) {
2303 rates = bss->supp_rates_len - 8;
2304 pos = skb_put(skb, 2 + rates);
2305 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2306 *pos++ = rates;
2307 memcpy(pos, &bss->supp_rates[8], rates);
2308 }
2309
2310 memset(&control, 0, sizeof(control));
2311 memset(&extra, 0, sizeof(extra));
2312 extra.mode = local->oper_hw_mode;
2313 rate = rate_control_get_rate(local, dev, skb, &extra);
2314 if (!rate) {
2315 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2316 "for IBSS beacon\n", dev->name);
2317 break;
2318 }
Jiri Slaby13262ff2007-08-28 17:01:54 -04002319 control.tx_rate =
2320 ((sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE) &&
2321 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
Jiri Bencf0706e822007-05-05 11:45:53 -07002322 rate->val2 : rate->val;
2323 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2324 control.power_level = local->hw.conf.power_level;
2325 control.flags |= IEEE80211_TXCTL_NO_ACK;
2326 control.retry_limit = 1;
2327
2328 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2329 if (ifsta->probe_resp) {
2330 mgmt = (struct ieee80211_mgmt *)
2331 ifsta->probe_resp->data;
2332 mgmt->frame_control =
2333 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2334 IEEE80211_STYPE_PROBE_RESP);
2335 } else {
2336 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2337 "template for IBSS\n", dev->name);
2338 }
2339
2340 if (local->ops->beacon_update &&
2341 local->ops->beacon_update(local_to_hw(local),
2342 skb, &control) == 0) {
2343 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2344 "template based on scan results\n", dev->name);
2345 skb = NULL;
2346 }
2347
2348 rates = 0;
2349 mode = local->oper_hw_mode;
2350 for (i = 0; i < bss->supp_rates_len; i++) {
2351 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
Jiri Bencf0706e822007-05-05 11:45:53 -07002352 for (j = 0; j < mode->num_rates; j++)
2353 if (mode->rates[j].rate == bitrate)
2354 rates |= BIT(j);
2355 }
2356 ifsta->supp_rates_bits = rates;
2357 } while (0);
2358
2359 if (skb) {
2360 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2361 "template\n", dev->name);
2362 dev_kfree_skb(skb);
2363 }
2364
2365 ifsta->state = IEEE80211_IBSS_JOINED;
2366 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2367
2368 ieee80211_rx_bss_put(dev, bss);
2369
2370 return res;
2371}
2372
2373
2374static int ieee80211_sta_create_ibss(struct net_device *dev,
2375 struct ieee80211_if_sta *ifsta)
2376{
2377 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2378 struct ieee80211_sta_bss *bss;
2379 struct ieee80211_sub_if_data *sdata;
2380 struct ieee80211_hw_mode *mode;
2381 u8 bssid[ETH_ALEN], *pos;
2382 int i;
Joe Perches0795af52007-10-03 17:59:30 -07002383 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07002384
2385#if 0
2386 /* Easier testing, use fixed BSSID. */
2387 memset(bssid, 0xfe, ETH_ALEN);
2388#else
2389 /* Generate random, not broadcast, locally administered BSSID. Mix in
2390 * own MAC address to make sure that devices that do not have proper
2391 * random number generator get different BSSID. */
2392 get_random_bytes(bssid, ETH_ALEN);
2393 for (i = 0; i < ETH_ALEN; i++)
2394 bssid[i] ^= dev->dev_addr[i];
2395 bssid[0] &= ~0x01;
2396 bssid[0] |= 0x02;
2397#endif
2398
Joe Perches0795af52007-10-03 17:59:30 -07002399 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID %s\n",
2400 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07002401
2402 bss = ieee80211_rx_bss_add(dev, bssid);
2403 if (!bss)
2404 return -ENOMEM;
2405
2406 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2407 mode = local->oper_hw_mode;
2408
2409 if (local->hw.conf.beacon_int == 0)
2410 local->hw.conf.beacon_int = 100;
2411 bss->beacon_int = local->hw.conf.beacon_int;
2412 bss->hw_mode = local->hw.conf.phymode;
2413 bss->channel = local->hw.conf.channel;
2414 bss->freq = local->hw.conf.freq;
2415 bss->last_update = jiffies;
2416 bss->capability = WLAN_CAPABILITY_IBSS;
2417 if (sdata->default_key) {
2418 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2419 } else
2420 sdata->drop_unencrypted = 0;
2421 bss->supp_rates_len = mode->num_rates;
2422 pos = bss->supp_rates;
2423 for (i = 0; i < mode->num_rates; i++) {
2424 int rate = mode->rates[i].rate;
Jiri Bencf0706e822007-05-05 11:45:53 -07002425 *pos++ = (u8) (rate / 5);
2426 }
2427
2428 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2429}
2430
2431
2432static int ieee80211_sta_find_ibss(struct net_device *dev,
2433 struct ieee80211_if_sta *ifsta)
2434{
2435 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2436 struct ieee80211_sta_bss *bss;
2437 int found = 0;
2438 u8 bssid[ETH_ALEN];
2439 int active_ibss;
Joe Perches0795af52007-10-03 17:59:30 -07002440 DECLARE_MAC_BUF(mac);
2441 DECLARE_MAC_BUF(mac2);
Jiri Bencf0706e822007-05-05 11:45:53 -07002442
2443 if (ifsta->ssid_len == 0)
2444 return -EINVAL;
2445
2446 active_ibss = ieee80211_sta_active_ibss(dev);
2447#ifdef CONFIG_MAC80211_IBSS_DEBUG
2448 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2449 dev->name, active_ibss);
2450#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2451 spin_lock_bh(&local->sta_bss_lock);
2452 list_for_each_entry(bss, &local->sta_bss_list, list) {
2453 if (ifsta->ssid_len != bss->ssid_len ||
2454 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2455 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2456 continue;
2457#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002458 printk(KERN_DEBUG " bssid=%s found\n",
2459 print_mac(mac, bss->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07002460#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2461 memcpy(bssid, bss->bssid, ETH_ALEN);
2462 found = 1;
2463 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2464 break;
2465 }
2466 spin_unlock_bh(&local->sta_bss_lock);
2467
2468#ifdef CONFIG_MAC80211_IBSS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -07002469 printk(KERN_DEBUG " sta_find_ibss: selected %s current "
2470 "%s\n", print_mac(mac, bssid), print_mac(mac2, ifsta->bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07002471#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2472 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2473 (bss = ieee80211_rx_bss_get(dev, bssid))) {
Joe Perches0795af52007-10-03 17:59:30 -07002474 printk(KERN_DEBUG "%s: Selected IBSS BSSID %s"
Jiri Bencf0706e822007-05-05 11:45:53 -07002475 " based on configured SSID\n",
Joe Perches0795af52007-10-03 17:59:30 -07002476 dev->name, print_mac(mac, bssid));
Jiri Bencf0706e822007-05-05 11:45:53 -07002477 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2478 }
2479#ifdef CONFIG_MAC80211_IBSS_DEBUG
2480 printk(KERN_DEBUG " did not try to join ibss\n");
2481#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2482
2483 /* Selected IBSS not found in current scan results - try to scan */
2484 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2485 !ieee80211_sta_active_ibss(dev)) {
2486 mod_timer(&ifsta->timer, jiffies +
2487 IEEE80211_IBSS_MERGE_INTERVAL);
2488 } else if (time_after(jiffies, local->last_scan_completed +
2489 IEEE80211_SCAN_INTERVAL)) {
2490 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2491 "join\n", dev->name);
2492 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2493 ifsta->ssid_len);
2494 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2495 int interval = IEEE80211_SCAN_INTERVAL;
2496
2497 if (time_after(jiffies, ifsta->ibss_join_req +
2498 IEEE80211_IBSS_JOIN_TIMEOUT)) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002499 if ((ifsta->flags & IEEE80211_STA_CREATE_IBSS) &&
Jiri Bencf0706e822007-05-05 11:45:53 -07002500 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2501 return ieee80211_sta_create_ibss(dev, ifsta);
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002502 if (ifsta->flags & IEEE80211_STA_CREATE_IBSS) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002503 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2504 " configured channel %d (%d MHz)\n",
2505 dev->name, local->hw.conf.channel,
2506 local->hw.conf.freq);
2507 }
2508
2509 /* No IBSS found - decrease scan interval and continue
2510 * scanning. */
2511 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2512 }
2513
2514 ifsta->state = IEEE80211_IBSS_SEARCH;
2515 mod_timer(&ifsta->timer, jiffies + interval);
2516 return 0;
2517 }
2518
2519 return 0;
2520}
2521
2522
2523int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2524{
2525 struct ieee80211_sub_if_data *sdata;
2526 struct ieee80211_if_sta *ifsta;
2527 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2528
2529 if (len > IEEE80211_MAX_SSID_LEN)
2530 return -EINVAL;
2531
2532 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2533 * not defined. */
2534 if (local->ops->conf_tx) {
2535 struct ieee80211_tx_queue_params qparam;
2536 int i;
2537
2538 memset(&qparam, 0, sizeof(qparam));
2539 /* TODO: are these ok defaults for all hw_modes? */
2540 qparam.aifs = 2;
2541 qparam.cw_min =
2542 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2543 qparam.cw_max = 1023;
2544 qparam.burst_time = 0;
2545 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2546 {
2547 local->ops->conf_tx(local_to_hw(local),
2548 i + IEEE80211_TX_QUEUE_DATA0,
2549 &qparam);
2550 }
2551 /* IBSS uses different parameters for Beacon sending */
2552 qparam.cw_min++;
2553 qparam.cw_min *= 2;
2554 qparam.cw_min--;
2555 local->ops->conf_tx(local_to_hw(local),
2556 IEEE80211_TX_QUEUE_BEACON, &qparam);
2557 }
2558
2559 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2560 ifsta = &sdata->u.sta;
2561
2562 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002563 ifsta->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07002564 memcpy(ifsta->ssid, ssid, len);
2565 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2566 ifsta->ssid_len = len;
2567
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002568 if (len)
2569 ifsta->flags |= IEEE80211_STA_SSID_SET;
2570 else
2571 ifsta->flags &= ~IEEE80211_STA_SSID_SET;
2572 if (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2573 !(ifsta->flags & IEEE80211_STA_BSSID_SET)) {
Jiri Bencf0706e822007-05-05 11:45:53 -07002574 ifsta->ibss_join_req = jiffies;
2575 ifsta->state = IEEE80211_IBSS_SEARCH;
2576 return ieee80211_sta_find_ibss(dev, ifsta);
2577 }
2578 return 0;
2579}
2580
2581
2582int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2583{
2584 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2585 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2586 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2587 *len = ifsta->ssid_len;
2588 return 0;
2589}
2590
2591
2592int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2593{
2594 struct ieee80211_sub_if_data *sdata;
2595 struct ieee80211_if_sta *ifsta;
2596 int res;
2597
2598 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2599 ifsta = &sdata->u.sta;
2600
2601 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2602 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2603 res = ieee80211_if_config(dev);
2604 if (res) {
2605 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2606 "the low-level driver\n", dev->name);
2607 return res;
2608 }
2609 }
2610
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002611 if (is_valid_ether_addr(bssid))
2612 ifsta->flags |= IEEE80211_STA_BSSID_SET;
Jiri Bencf0706e822007-05-05 11:45:53 -07002613 else
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002614 ifsta->flags &= ~IEEE80211_STA_BSSID_SET;
2615
Jiri Bencf0706e822007-05-05 11:45:53 -07002616 return 0;
2617}
2618
2619
2620static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2621 struct ieee80211_sub_if_data *sdata,
2622 int powersave)
2623{
2624 struct sk_buff *skb;
2625 struct ieee80211_hdr *nullfunc;
2626 u16 fc;
2627
2628 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2629 if (!skb) {
2630 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2631 "frame\n", sdata->dev->name);
2632 return;
2633 }
2634 skb_reserve(skb, local->hw.extra_tx_headroom);
2635
2636 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2637 memset(nullfunc, 0, 24);
2638 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2639 IEEE80211_FCTL_TODS;
2640 if (powersave)
2641 fc |= IEEE80211_FCTL_PM;
2642 nullfunc->frame_control = cpu_to_le16(fc);
2643 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2644 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2645 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2646
2647 ieee80211_sta_tx(sdata->dev, skb, 0);
2648}
2649
2650
2651void ieee80211_scan_completed(struct ieee80211_hw *hw)
2652{
2653 struct ieee80211_local *local = hw_to_local(hw);
2654 struct net_device *dev = local->scan_dev;
2655 struct ieee80211_sub_if_data *sdata;
2656 union iwreq_data wrqu;
2657
2658 local->last_scan_completed = jiffies;
2659 wmb();
2660 local->sta_scanning = 0;
2661
2662 if (ieee80211_hw_config(local))
2663 printk(KERN_DEBUG "%s: failed to restore operational"
2664 "channel after scan\n", dev->name);
2665
Johannes Berg4150c572007-09-17 01:29:23 -04002666
2667 netif_tx_lock_bh(local->mdev);
2668 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
2669 local->ops->configure_filter(local_to_hw(local),
2670 FIF_BCN_PRBRESP_PROMISC,
2671 &local->filter_flags,
2672 local->mdev->mc_count,
2673 local->mdev->mc_list);
2674
2675 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e822007-05-05 11:45:53 -07002676
2677 memset(&wrqu, 0, sizeof(wrqu));
2678 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2679
Johannes Berg79010422007-09-18 17:29:21 -04002680 rcu_read_lock();
2681 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cb2007-06-08 15:31:13 +02002682
2683 /* No need to wake the master device. */
2684 if (sdata->dev == local->mdev)
2685 continue;
2686
Jiri Bencf0706e822007-05-05 11:45:53 -07002687 if (sdata->type == IEEE80211_IF_TYPE_STA) {
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002688 if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED)
Jiri Bencf0706e822007-05-05 11:45:53 -07002689 ieee80211_send_nullfunc(local, sdata, 0);
2690 ieee80211_sta_timer((unsigned long)sdata);
2691 }
Mattias Nissler14042cb2007-06-08 15:31:13 +02002692
Jiri Bencf0706e822007-05-05 11:45:53 -07002693 netif_wake_queue(sdata->dev);
2694 }
Johannes Berg79010422007-09-18 17:29:21 -04002695 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07002696
2697 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2698 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2699 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002700 if (!(ifsta->flags & IEEE80211_STA_BSSID_SET) ||
Jiri Bencf0706e822007-05-05 11:45:53 -07002701 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2702 !ieee80211_sta_active_ibss(dev)))
2703 ieee80211_sta_find_ibss(dev, ifsta);
2704 }
2705}
2706EXPORT_SYMBOL(ieee80211_scan_completed);
2707
2708void ieee80211_sta_scan_work(struct work_struct *work)
2709{
2710 struct ieee80211_local *local =
2711 container_of(work, struct ieee80211_local, scan_work.work);
2712 struct net_device *dev = local->scan_dev;
2713 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2714 struct ieee80211_hw_mode *mode;
2715 struct ieee80211_channel *chan;
2716 int skip;
2717 unsigned long next_delay = 0;
2718
2719 if (!local->sta_scanning)
2720 return;
2721
2722 switch (local->scan_state) {
2723 case SCAN_SET_CHANNEL:
2724 mode = local->scan_hw_mode;
2725 if (local->scan_hw_mode->list.next == &local->modes_list &&
2726 local->scan_channel_idx >= mode->num_channels) {
2727 ieee80211_scan_completed(local_to_hw(local));
2728 return;
2729 }
2730 skip = !(local->enabled_modes & (1 << mode->mode));
2731 chan = &mode->channels[local->scan_channel_idx];
2732 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2733 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2734 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2735 (local->hw_modes & local->enabled_modes &
2736 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2737 skip = 1;
2738
2739 if (!skip) {
2740#if 0
2741 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2742 dev->name, chan->chan, chan->freq);
2743#endif
2744
2745 local->scan_channel = chan;
2746 if (ieee80211_hw_config(local)) {
2747 printk(KERN_DEBUG "%s: failed to set channel "
2748 "%d (%d MHz) for scan\n", dev->name,
2749 chan->chan, chan->freq);
2750 skip = 1;
2751 }
2752 }
2753
2754 local->scan_channel_idx++;
2755 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2756 if (local->scan_hw_mode->list.next != &local->modes_list) {
2757 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2758 struct ieee80211_hw_mode,
2759 list);
2760 local->scan_channel_idx = 0;
2761 }
2762 }
2763
2764 if (skip)
2765 break;
2766
2767 next_delay = IEEE80211_PROBE_DELAY +
2768 usecs_to_jiffies(local->hw.channel_change_time);
2769 local->scan_state = SCAN_SEND_PROBE;
2770 break;
2771 case SCAN_SEND_PROBE:
2772 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2773 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2774 local->scan_ssid_len);
2775 next_delay = IEEE80211_CHANNEL_TIME;
2776 } else
2777 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2778 local->scan_state = SCAN_SET_CHANNEL;
2779 break;
2780 }
2781
2782 if (local->sta_scanning)
2783 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2784 next_delay);
2785}
2786
2787
2788static int ieee80211_sta_start_scan(struct net_device *dev,
2789 u8 *ssid, size_t ssid_len)
2790{
2791 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2792 struct ieee80211_sub_if_data *sdata;
2793
2794 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2795 return -EINVAL;
2796
2797 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2798 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2799 * BSSID: MACAddress
2800 * SSID
2801 * ScanType: ACTIVE, PASSIVE
2802 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2803 * a Probe frame during active scanning
2804 * ChannelList
2805 * MinChannelTime (>= ProbeDelay), in TU
2806 * MaxChannelTime: (>= MinChannelTime), in TU
2807 */
2808
2809 /* MLME-SCAN.confirm
2810 * BSSDescriptionSet
2811 * ResultCode: SUCCESS, INVALID_PARAMETERS
2812 */
2813
2814 if (local->sta_scanning) {
2815 if (local->scan_dev == dev)
2816 return 0;
2817 return -EBUSY;
2818 }
2819
2820 if (local->ops->hw_scan) {
2821 int rc = local->ops->hw_scan(local_to_hw(local),
2822 ssid, ssid_len);
2823 if (!rc) {
2824 local->sta_scanning = 1;
2825 local->scan_dev = dev;
2826 }
2827 return rc;
2828 }
2829
2830 local->sta_scanning = 1;
2831
Johannes Berg79010422007-09-18 17:29:21 -04002832 rcu_read_lock();
2833 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Mattias Nissler14042cb2007-06-08 15:31:13 +02002834
2835 /* Don't stop the master interface, otherwise we can't transmit
2836 * probes! */
2837 if (sdata->dev == local->mdev)
2838 continue;
2839
Jiri Bencf0706e822007-05-05 11:45:53 -07002840 netif_stop_queue(sdata->dev);
2841 if (sdata->type == IEEE80211_IF_TYPE_STA &&
Jiri Slabyd6f2da52007-08-28 17:01:54 -04002842 (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e822007-05-05 11:45:53 -07002843 ieee80211_send_nullfunc(local, sdata, 1);
2844 }
Johannes Berg79010422007-09-18 17:29:21 -04002845 rcu_read_unlock();
Jiri Bencf0706e822007-05-05 11:45:53 -07002846
2847 if (ssid) {
2848 local->scan_ssid_len = ssid_len;
2849 memcpy(local->scan_ssid, ssid, ssid_len);
2850 } else
2851 local->scan_ssid_len = 0;
2852 local->scan_state = SCAN_SET_CHANNEL;
2853 local->scan_hw_mode = list_entry(local->modes_list.next,
2854 struct ieee80211_hw_mode,
2855 list);
2856 local->scan_channel_idx = 0;
2857 local->scan_dev = dev;
2858
Johannes Berg4150c572007-09-17 01:29:23 -04002859 netif_tx_lock_bh(local->mdev);
2860 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
2861 local->ops->configure_filter(local_to_hw(local),
2862 FIF_BCN_PRBRESP_PROMISC,
2863 &local->filter_flags,
2864 local->mdev->mc_count,
2865 local->mdev->mc_list);
2866 netif_tx_unlock_bh(local->mdev);
Jiri Bencf0706e822007-05-05 11:45:53 -07002867
2868 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2869 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2870 IEEE80211_CHANNEL_TIME);
2871
2872 return 0;
2873}
2874
2875
2876int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2877{
2878 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2879 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2880 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2881
2882 if (sdata->type != IEEE80211_IF_TYPE_STA)
2883 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2884
2885 if (local->sta_scanning) {
2886 if (local->scan_dev == dev)
2887 return 0;
2888 return -EBUSY;
2889 }
2890
2891 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2892 queue_work(local->hw.workqueue, &ifsta->work);
2893 return 0;
2894}
2895
2896static char *
2897ieee80211_sta_scan_result(struct net_device *dev,
2898 struct ieee80211_sta_bss *bss,
2899 char *current_ev, char *end_buf)
2900{
2901 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2902 struct iw_event iwe;
2903
2904 if (time_after(jiffies,
2905 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2906 return current_ev;
2907
2908 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2909 return current_ev;
2910
2911 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2912 !bss->wpa_ie && !bss->rsn_ie)
2913 return current_ev;
2914
2915 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2916 (local->scan_ssid_len != bss->ssid_len ||
2917 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2918 return current_ev;
2919
2920 memset(&iwe, 0, sizeof(iwe));
2921 iwe.cmd = SIOCGIWAP;
2922 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2923 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2924 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2925 IW_EV_ADDR_LEN);
2926
2927 memset(&iwe, 0, sizeof(iwe));
2928 iwe.cmd = SIOCGIWESSID;
2929 iwe.u.data.length = bss->ssid_len;
2930 iwe.u.data.flags = 1;
2931 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2932 bss->ssid);
2933
2934 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2935 memset(&iwe, 0, sizeof(iwe));
2936 iwe.cmd = SIOCGIWMODE;
2937 if (bss->capability & WLAN_CAPABILITY_ESS)
2938 iwe.u.mode = IW_MODE_MASTER;
2939 else
2940 iwe.u.mode = IW_MODE_ADHOC;
2941 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2942 IW_EV_UINT_LEN);
2943 }
2944
2945 memset(&iwe, 0, sizeof(iwe));
2946 iwe.cmd = SIOCGIWFREQ;
2947 iwe.u.freq.m = bss->channel;
2948 iwe.u.freq.e = 0;
2949 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2950 IW_EV_FREQ_LEN);
2951 iwe.u.freq.m = bss->freq * 100000;
2952 iwe.u.freq.e = 1;
2953 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2954 IW_EV_FREQ_LEN);
2955
2956 memset(&iwe, 0, sizeof(iwe));
2957 iwe.cmd = IWEVQUAL;
2958 iwe.u.qual.qual = bss->signal;
2959 iwe.u.qual.level = bss->rssi;
2960 iwe.u.qual.noise = bss->noise;
2961 iwe.u.qual.updated = local->wstats_flags;
2962 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2963 IW_EV_QUAL_LEN);
2964
2965 memset(&iwe, 0, sizeof(iwe));
2966 iwe.cmd = SIOCGIWENCODE;
2967 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2968 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2969 else
2970 iwe.u.data.flags = IW_ENCODE_DISABLED;
2971 iwe.u.data.length = 0;
2972 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2973
2974 if (bss && bss->wpa_ie) {
2975 memset(&iwe, 0, sizeof(iwe));
2976 iwe.cmd = IWEVGENIE;
2977 iwe.u.data.length = bss->wpa_ie_len;
2978 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2979 bss->wpa_ie);
2980 }
2981
2982 if (bss && bss->rsn_ie) {
2983 memset(&iwe, 0, sizeof(iwe));
2984 iwe.cmd = IWEVGENIE;
2985 iwe.u.data.length = bss->rsn_ie_len;
2986 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2987 bss->rsn_ie);
2988 }
2989
2990 if (bss && bss->supp_rates_len > 0) {
2991 /* display all supported rates in readable format */
2992 char *p = current_ev + IW_EV_LCP_LEN;
2993 int i;
2994
2995 memset(&iwe, 0, sizeof(iwe));
2996 iwe.cmd = SIOCGIWRATE;
2997 /* Those two flags are ignored... */
2998 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2999
3000 for (i = 0; i < bss->supp_rates_len; i++) {
3001 iwe.u.bitrate.value = ((bss->supp_rates[i] &
3002 0x7f) * 500000);
3003 p = iwe_stream_add_value(current_ev, p,
3004 end_buf, &iwe, IW_EV_PARAM_LEN);
3005 }
3006 current_ev = p;
3007 }
3008
3009 if (bss) {
3010 char *buf;
3011 buf = kmalloc(30, GFP_ATOMIC);
3012 if (buf) {
3013 memset(&iwe, 0, sizeof(iwe));
3014 iwe.cmd = IWEVCUSTOM;
3015 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
3016 iwe.u.data.length = strlen(buf);
3017 current_ev = iwe_stream_add_point(current_ev, end_buf,
3018 &iwe, buf);
3019 kfree(buf);
3020 }
3021 }
3022
3023 do {
3024 char *buf;
3025
3026 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
3027 break;
3028
3029 buf = kmalloc(100, GFP_ATOMIC);
3030 if (!buf)
3031 break;
3032
3033 memset(&iwe, 0, sizeof(iwe));
3034 iwe.cmd = IWEVCUSTOM;
3035 sprintf(buf, "bcn_int=%d", bss->beacon_int);
3036 iwe.u.data.length = strlen(buf);
3037 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3038 buf);
3039
3040 memset(&iwe, 0, sizeof(iwe));
3041 iwe.cmd = IWEVCUSTOM;
3042 sprintf(buf, "capab=0x%04x", bss->capability);
3043 iwe.u.data.length = strlen(buf);
3044 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
3045 buf);
3046
3047 kfree(buf);
3048 break;
3049 } while (0);
3050
3051 return current_ev;
3052}
3053
3054
3055int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3056{
3057 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3058 char *current_ev = buf;
3059 char *end_buf = buf + len;
3060 struct ieee80211_sta_bss *bss;
3061
3062 spin_lock_bh(&local->sta_bss_lock);
3063 list_for_each_entry(bss, &local->sta_bss_list, list) {
3064 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3065 spin_unlock_bh(&local->sta_bss_lock);
3066 return -E2BIG;
3067 }
3068 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3069 end_buf);
3070 }
3071 spin_unlock_bh(&local->sta_bss_lock);
3072 return current_ev - buf;
3073}
3074
3075
3076int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3077{
3078 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3079 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3080 kfree(ifsta->extra_ie);
3081 if (len == 0) {
3082 ifsta->extra_ie = NULL;
3083 ifsta->extra_ie_len = 0;
3084 return 0;
3085 }
3086 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3087 if (!ifsta->extra_ie) {
3088 ifsta->extra_ie_len = 0;
3089 return -ENOMEM;
3090 }
3091 memcpy(ifsta->extra_ie, ie, len);
3092 ifsta->extra_ie_len = len;
3093 return 0;
3094}
3095
3096
3097struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3098 struct sk_buff *skb, u8 *bssid,
3099 u8 *addr)
3100{
3101 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3102 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04003103 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Joe Perches0795af52007-10-03 17:59:30 -07003104 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e822007-05-05 11:45:53 -07003105
3106 /* TODO: Could consider removing the least recently used entry and
3107 * allow new one to be added. */
3108 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3109 if (net_ratelimit()) {
3110 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
Joe Perches0795af52007-10-03 17:59:30 -07003111 "entry %s\n", dev->name, print_mac(mac, addr));
Jiri Bencf0706e822007-05-05 11:45:53 -07003112 }
3113 return NULL;
3114 }
3115
Joe Perches0795af52007-10-03 17:59:30 -07003116 printk(KERN_DEBUG "%s: Adding new IBSS station %s (dev=%s)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04003117 wiphy_name(local->hw.wiphy), print_mac(mac, addr), dev->name);
Jiri Bencf0706e822007-05-05 11:45:53 -07003118
3119 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3120 if (!sta)
3121 return NULL;
3122
3123 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3124
3125 rate_control_rate_init(sta, local);
3126
3127 return sta; /* caller will call sta_info_put() */
3128}
3129
3130
3131int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3132{
3133 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3134 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3135
3136 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3137 dev->name, reason);
3138
3139 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3140 sdata->type != IEEE80211_IF_TYPE_IBSS)
3141 return -EINVAL;
3142
3143 ieee80211_send_deauth(dev, ifsta, reason);
3144 ieee80211_set_disassoc(dev, ifsta, 1);
3145 return 0;
3146}
3147
3148
3149int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3150{
3151 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3152 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3153
3154 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3155 dev->name, reason);
3156
3157 if (sdata->type != IEEE80211_IF_TYPE_STA)
3158 return -EINVAL;
3159
Jiri Slabyd6f2da52007-08-28 17:01:54 -04003160 if (!(ifsta->flags & IEEE80211_STA_ASSOCIATED))
Jiri Bencf0706e822007-05-05 11:45:53 -07003161 return -1;
3162
3163 ieee80211_send_disassoc(dev, ifsta, reason);
3164 ieee80211_set_disassoc(dev, ifsta, 0);
3165 return 0;
3166}