blob: 9aee1abae1279dedba8b74f8b4b692c40ad5340c [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * BSS client mode implementation
3 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
4 * Copyright 2004, Instant802 Networks, Inc.
5 * Copyright 2005, Devicescape Software, Inc.
6 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
7 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14/* TODO:
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 Bencf0706e82007-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 Bencf0706e82007-05-05 11:45:53 -070028#include <net/iw_handler.h>
29#include <asm/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070030
31#include <net/mac80211.h>
32#include "ieee80211_i.h"
33#include "ieee80211_rate.h"
34#include "hostapd_ioctl.h"
35
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 Bencf0706e82007-05-05 11:45:53 -070079 u8 *ssid;
Jiri Bencf0706e82007-05-05 11:45:53 -070080 u8 *supp_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -070081 u8 *fh_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070082 u8 *ds_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070083 u8 *cf_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070084 u8 *tim;
Jiri Bencf0706e82007-05-05 11:45:53 -070085 u8 *ibss_params;
Jiri Bencf0706e82007-05-05 11:45:53 -070086 u8 *challenge;
Jiri Bencf0706e82007-05-05 11:45:53 -070087 u8 *wpa;
Jiri Bencf0706e82007-05-05 11:45:53 -070088 u8 *rsn;
Jiri Bencf0706e82007-05-05 11:45:53 -070089 u8 *erp_info;
Jiri Bencf0706e82007-05-05 11:45:53 -070090 u8 *ext_supp_rates;
Jiri Bencf0706e82007-05-05 11:45:53 -070091 u8 *wmm_info;
Jiri Bencf0706e82007-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 Bencf0706e82007-05-05 11:45:53 -0700108 u8 wmm_param_len;
109};
110
111typedef enum { ParseOK = 0, ParseUnknown = 1, ParseFailed = -1 } ParseRes;
112
113
114static ParseRes ieee802_11_parse_elems(u8 *start, size_t len,
115 struct ieee802_11_elems *elems)
116{
117 size_t left = len;
118 u8 *pos = start;
119 int unknown = 0;
120
121 memset(elems, 0, sizeof(*elems));
122
123 while (left >= 2) {
124 u8 id, elen;
125
126 id = *pos++;
127 elen = *pos++;
128 left -= 2;
129
130 if (elen > left) {
131#if 0
132 if (net_ratelimit())
133 printk(KERN_DEBUG "IEEE 802.11 element parse "
134 "failed (id=%d elen=%d left=%d)\n",
135 id, elen, left);
136#endif
137 return ParseFailed;
138 }
139
140 switch (id) {
141 case WLAN_EID_SSID:
142 elems->ssid = pos;
143 elems->ssid_len = elen;
144 break;
145 case WLAN_EID_SUPP_RATES:
146 elems->supp_rates = pos;
147 elems->supp_rates_len = elen;
148 break;
149 case WLAN_EID_FH_PARAMS:
150 elems->fh_params = pos;
151 elems->fh_params_len = elen;
152 break;
153 case WLAN_EID_DS_PARAMS:
154 elems->ds_params = pos;
155 elems->ds_params_len = elen;
156 break;
157 case WLAN_EID_CF_PARAMS:
158 elems->cf_params = pos;
159 elems->cf_params_len = elen;
160 break;
161 case WLAN_EID_TIM:
162 elems->tim = pos;
163 elems->tim_len = elen;
164 break;
165 case WLAN_EID_IBSS_PARAMS:
166 elems->ibss_params = pos;
167 elems->ibss_params_len = elen;
168 break;
169 case WLAN_EID_CHALLENGE:
170 elems->challenge = pos;
171 elems->challenge_len = elen;
172 break;
173 case WLAN_EID_WPA:
174 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
175 pos[2] == 0xf2) {
176 /* Microsoft OUI (00:50:F2) */
177 if (pos[3] == 1) {
178 /* OUI Type 1 - WPA IE */
179 elems->wpa = pos;
180 elems->wpa_len = elen;
181 } else if (elen >= 5 && pos[3] == 2) {
182 if (pos[4] == 0) {
183 elems->wmm_info = pos;
184 elems->wmm_info_len = elen;
185 } else if (pos[4] == 1) {
186 elems->wmm_param = pos;
187 elems->wmm_param_len = elen;
188 }
189 }
190 }
191 break;
192 case WLAN_EID_RSN:
193 elems->rsn = pos;
194 elems->rsn_len = elen;
195 break;
196 case WLAN_EID_ERP_INFO:
197 elems->erp_info = pos;
198 elems->erp_info_len = elen;
199 break;
200 case WLAN_EID_EXT_SUPP_RATES:
201 elems->ext_supp_rates = pos;
202 elems->ext_supp_rates_len = elen;
203 break;
204 default:
205#if 0
206 printk(KERN_DEBUG "IEEE 802.11 element parse ignored "
207 "unknown element (id=%d elen=%d)\n",
208 id, elen);
209#endif
210 unknown++;
211 break;
212 }
213
214 left -= elen;
215 pos += elen;
216 }
217
218 /* Do not trigger error if left == 1 as Apple Airport base stations
219 * send AssocResps that are one spurious byte too long. */
220
221 return unknown ? ParseUnknown : ParseOK;
222}
223
224
225
226
227static int ecw2cw(int ecw)
228{
229 int cw = 1;
230 while (ecw > 0) {
231 cw <<= 1;
232 ecw--;
233 }
234 return cw - 1;
235}
236
237
238static void ieee80211_sta_wmm_params(struct net_device *dev,
239 struct ieee80211_if_sta *ifsta,
240 u8 *wmm_param, size_t wmm_param_len)
241{
242 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
243 struct ieee80211_tx_queue_params params;
244 size_t left;
245 int count;
246 u8 *pos;
247
248 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
249 return;
250 count = wmm_param[6] & 0x0f;
251 if (count == ifsta->wmm_last_param_set)
252 return;
253 ifsta->wmm_last_param_set = count;
254
255 pos = wmm_param + 8;
256 left = wmm_param_len - 8;
257
258 memset(&params, 0, sizeof(params));
259
260 if (!local->ops->conf_tx)
261 return;
262
263 local->wmm_acm = 0;
264 for (; left >= 4; left -= 4, pos += 4) {
265 int aci = (pos[0] >> 5) & 0x03;
266 int acm = (pos[0] >> 4) & 0x01;
267 int queue;
268
269 switch (aci) {
270 case 1:
271 queue = IEEE80211_TX_QUEUE_DATA3;
272 if (acm) {
273 local->wmm_acm |= BIT(0) | BIT(3);
274 }
275 break;
276 case 2:
277 queue = IEEE80211_TX_QUEUE_DATA1;
278 if (acm) {
279 local->wmm_acm |= BIT(4) | BIT(5);
280 }
281 break;
282 case 3:
283 queue = IEEE80211_TX_QUEUE_DATA0;
284 if (acm) {
285 local->wmm_acm |= BIT(6) | BIT(7);
286 }
287 break;
288 case 0:
289 default:
290 queue = IEEE80211_TX_QUEUE_DATA2;
291 if (acm) {
292 local->wmm_acm |= BIT(1) | BIT(2);
293 }
294 break;
295 }
296
297 params.aifs = pos[0] & 0x0f;
298 params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
299 params.cw_min = ecw2cw(pos[1] & 0x0f);
300 /* TXOP is in units of 32 usec; burst_time in 0.1 ms */
301 params.burst_time = (pos[2] | (pos[3] << 8)) * 32 / 100;
302 printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
303 "cWmin=%d cWmax=%d burst=%d\n",
304 dev->name, queue, aci, acm, params.aifs, params.cw_min,
305 params.cw_max, params.burst_time);
306 /* TODO: handle ACM (block TX, fallback to next lowest allowed
307 * AC for now) */
308 if (local->ops->conf_tx(local_to_hw(local), queue, &params)) {
309 printk(KERN_DEBUG "%s: failed to set TX queue "
310 "parameters for queue %d\n", dev->name, queue);
311 }
312 }
313}
314
315
Daniel Drake56282212007-07-10 19:32:10 +0200316static void ieee80211_handle_erp_ie(struct net_device *dev, u8 erp_value)
317{
Daniel Drake56282212007-07-10 19:32:10 +0200318 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
319 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
320 int use_protection = (erp_value & WLAN_ERP_USE_PROTECTION) != 0;
321
Daniel Drake63fc33c2007-07-10 19:32:11 +0200322 if (use_protection != sdata->use_protection) {
Daniel Drake56282212007-07-10 19:32:10 +0200323 if (net_ratelimit()) {
324 printk(KERN_DEBUG "%s: CTS protection %s (BSSID="
325 MAC_FMT ")\n",
326 dev->name,
327 use_protection ? "enabled" : "disabled",
328 MAC_ARG(ifsta->bssid));
329 }
Daniel Drake63fc33c2007-07-10 19:32:11 +0200330 sdata->use_protection = use_protection;
Daniel Drake56282212007-07-10 19:32:10 +0200331 }
332}
333
334
Jiri Bencf0706e82007-05-05 11:45:53 -0700335static void ieee80211_sta_send_associnfo(struct net_device *dev,
336 struct ieee80211_if_sta *ifsta)
337{
338 char *buf;
339 size_t len;
340 int i;
341 union iwreq_data wrqu;
342
343 if (!ifsta->assocreq_ies && !ifsta->assocresp_ies)
344 return;
345
346 buf = kmalloc(50 + 2 * (ifsta->assocreq_ies_len +
347 ifsta->assocresp_ies_len), GFP_ATOMIC);
348 if (!buf)
349 return;
350
351 len = sprintf(buf, "ASSOCINFO(");
352 if (ifsta->assocreq_ies) {
353 len += sprintf(buf + len, "ReqIEs=");
354 for (i = 0; i < ifsta->assocreq_ies_len; i++) {
355 len += sprintf(buf + len, "%02x",
356 ifsta->assocreq_ies[i]);
357 }
358 }
359 if (ifsta->assocresp_ies) {
360 if (ifsta->assocreq_ies)
361 len += sprintf(buf + len, " ");
362 len += sprintf(buf + len, "RespIEs=");
363 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
364 len += sprintf(buf + len, "%02x",
365 ifsta->assocresp_ies[i]);
366 }
367 }
368 len += sprintf(buf + len, ")");
369
370 if (len > IW_CUSTOM_MAX) {
371 len = sprintf(buf, "ASSOCRESPIE=");
372 for (i = 0; i < ifsta->assocresp_ies_len; i++) {
373 len += sprintf(buf + len, "%02x",
374 ifsta->assocresp_ies[i]);
375 }
376 }
377
378 memset(&wrqu, 0, sizeof(wrqu));
379 wrqu.data.length = len;
380 wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
381
382 kfree(buf);
383}
384
385
386static void ieee80211_set_associated(struct net_device *dev,
387 struct ieee80211_if_sta *ifsta, int assoc)
388{
389 union iwreq_data wrqu;
Daniel Drake63fc33c2007-07-10 19:32:11 +0200390 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700391
392 if (ifsta->associated == assoc)
393 return;
394
395 ifsta->associated = assoc;
396
397 if (assoc) {
398 struct ieee80211_sub_if_data *sdata;
Daniel Drake56282212007-07-10 19:32:10 +0200399 struct ieee80211_sta_bss *bss;
Jiri Bencf0706e82007-05-05 11:45:53 -0700400 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
401 if (sdata->type != IEEE80211_IF_TYPE_STA)
402 return;
Daniel Drake56282212007-07-10 19:32:10 +0200403
404 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
405 if (bss) {
406 if (bss->has_erp_value)
407 ieee80211_handle_erp_ie(dev, bss->erp_value);
408 ieee80211_rx_bss_put(dev, bss);
409 }
410
Jiri Bencf0706e82007-05-05 11:45:53 -0700411 netif_carrier_on(dev);
412 ifsta->prev_bssid_set = 1;
413 memcpy(ifsta->prev_bssid, sdata->u.sta.bssid, ETH_ALEN);
414 memcpy(wrqu.ap_addr.sa_data, sdata->u.sta.bssid, ETH_ALEN);
415 ieee80211_sta_send_associnfo(dev, ifsta);
416 } else {
417 netif_carrier_off(dev);
Daniel Drake63fc33c2007-07-10 19:32:11 +0200418 sdata->use_protection = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700419 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
420 }
421 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
422 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
423 ifsta->last_probe = jiffies;
424}
425
426static void ieee80211_set_disassoc(struct net_device *dev,
427 struct ieee80211_if_sta *ifsta, int deauth)
428{
429 if (deauth)
430 ifsta->auth_tries = 0;
431 ifsta->assoc_tries = 0;
432 ieee80211_set_associated(dev, ifsta, 0);
433}
434
435static void ieee80211_sta_tx(struct net_device *dev, struct sk_buff *skb,
436 int encrypt)
437{
438 struct ieee80211_sub_if_data *sdata;
439 struct ieee80211_tx_packet_data *pkt_data;
440
441 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
442 skb->dev = sdata->local->mdev;
443 skb_set_mac_header(skb, 0);
444 skb_set_network_header(skb, 0);
445 skb_set_transport_header(skb, 0);
446
447 pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
448 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
449 pkt_data->ifindex = sdata->dev->ifindex;
450 pkt_data->mgmt_iface = (sdata->type == IEEE80211_IF_TYPE_MGMT);
451 pkt_data->do_not_encrypt = !encrypt;
452
453 dev_queue_xmit(skb);
454}
455
456
457static void ieee80211_send_auth(struct net_device *dev,
458 struct ieee80211_if_sta *ifsta,
459 int transaction, u8 *extra, size_t extra_len,
460 int encrypt)
461{
462 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
463 struct sk_buff *skb;
464 struct ieee80211_mgmt *mgmt;
465
466 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
467 sizeof(*mgmt) + 6 + extra_len);
468 if (!skb) {
469 printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
470 "frame\n", dev->name);
471 return;
472 }
473 skb_reserve(skb, local->hw.extra_tx_headroom);
474
475 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
476 memset(mgmt, 0, 24 + 6);
477 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
478 IEEE80211_STYPE_AUTH);
479 if (encrypt)
480 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
481 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
482 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
483 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
484 mgmt->u.auth.auth_alg = cpu_to_le16(ifsta->auth_alg);
485 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
486 ifsta->auth_transaction = transaction + 1;
487 mgmt->u.auth.status_code = cpu_to_le16(0);
488 if (extra)
489 memcpy(skb_put(skb, extra_len), extra, extra_len);
490
491 ieee80211_sta_tx(dev, skb, encrypt);
492}
493
494
495static void ieee80211_authenticate(struct net_device *dev,
496 struct ieee80211_if_sta *ifsta)
497{
498 ifsta->auth_tries++;
499 if (ifsta->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
500 printk(KERN_DEBUG "%s: authentication with AP " MAC_FMT
501 " timed out\n",
502 dev->name, MAC_ARG(ifsta->bssid));
503 ifsta->state = IEEE80211_DISABLED;
504 return;
505 }
506
507 ifsta->state = IEEE80211_AUTHENTICATE;
508 printk(KERN_DEBUG "%s: authenticate with AP " MAC_FMT "\n",
509 dev->name, MAC_ARG(ifsta->bssid));
510
511 ieee80211_send_auth(dev, ifsta, 1, NULL, 0, 0);
512
513 mod_timer(&ifsta->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
514}
515
516
517static void ieee80211_send_assoc(struct net_device *dev,
518 struct ieee80211_if_sta *ifsta)
519{
520 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
521 struct ieee80211_hw_mode *mode;
522 struct sk_buff *skb;
523 struct ieee80211_mgmt *mgmt;
524 u8 *pos, *ies;
525 int i, len;
526 u16 capab;
527 struct ieee80211_sta_bss *bss;
528 int wmm = 0;
529
530 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
531 sizeof(*mgmt) + 200 + ifsta->extra_ie_len +
532 ifsta->ssid_len);
533 if (!skb) {
534 printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
535 "frame\n", dev->name);
536 return;
537 }
538 skb_reserve(skb, local->hw.extra_tx_headroom);
539
540 mode = local->oper_hw_mode;
541 capab = ifsta->capab;
542 if (mode->mode == MODE_IEEE80211G) {
543 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME |
544 WLAN_CAPABILITY_SHORT_PREAMBLE;
545 }
546 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
547 if (bss) {
548 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
549 capab |= WLAN_CAPABILITY_PRIVACY;
550 if (bss->wmm_ie) {
551 wmm = 1;
552 }
553 ieee80211_rx_bss_put(dev, bss);
554 }
555
556 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
557 memset(mgmt, 0, 24);
558 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
559 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
560 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
561
562 if (ifsta->prev_bssid_set) {
563 skb_put(skb, 10);
564 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
565 IEEE80211_STYPE_REASSOC_REQ);
566 mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
567 mgmt->u.reassoc_req.listen_interval = cpu_to_le16(1);
568 memcpy(mgmt->u.reassoc_req.current_ap, ifsta->prev_bssid,
569 ETH_ALEN);
570 } else {
571 skb_put(skb, 4);
572 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
573 IEEE80211_STYPE_ASSOC_REQ);
574 mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
575 mgmt->u.assoc_req.listen_interval = cpu_to_le16(1);
576 }
577
578 /* SSID */
579 ies = pos = skb_put(skb, 2 + ifsta->ssid_len);
580 *pos++ = WLAN_EID_SSID;
581 *pos++ = ifsta->ssid_len;
582 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
583
584 len = mode->num_rates;
585 if (len > 8)
586 len = 8;
587 pos = skb_put(skb, len + 2);
588 *pos++ = WLAN_EID_SUPP_RATES;
589 *pos++ = len;
590 for (i = 0; i < len; i++) {
591 int rate = mode->rates[i].rate;
592 if (mode->mode == MODE_ATHEROS_TURBO)
593 rate /= 2;
594 *pos++ = (u8) (rate / 5);
595 }
596
597 if (mode->num_rates > len) {
598 pos = skb_put(skb, mode->num_rates - len + 2);
599 *pos++ = WLAN_EID_EXT_SUPP_RATES;
600 *pos++ = mode->num_rates - len;
601 for (i = len; i < mode->num_rates; i++) {
602 int rate = mode->rates[i].rate;
603 if (mode->mode == MODE_ATHEROS_TURBO)
604 rate /= 2;
605 *pos++ = (u8) (rate / 5);
606 }
607 }
608
609 if (ifsta->extra_ie) {
610 pos = skb_put(skb, ifsta->extra_ie_len);
611 memcpy(pos, ifsta->extra_ie, ifsta->extra_ie_len);
612 }
613
614 if (wmm && ifsta->wmm_enabled) {
615 pos = skb_put(skb, 9);
616 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
617 *pos++ = 7; /* len */
618 *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
619 *pos++ = 0x50;
620 *pos++ = 0xf2;
621 *pos++ = 2; /* WME */
622 *pos++ = 0; /* WME info */
623 *pos++ = 1; /* WME ver */
624 *pos++ = 0;
625 }
626
627 kfree(ifsta->assocreq_ies);
628 ifsta->assocreq_ies_len = (skb->data + skb->len) - ies;
629 ifsta->assocreq_ies = kmalloc(ifsta->assocreq_ies_len, GFP_ATOMIC);
630 if (ifsta->assocreq_ies)
631 memcpy(ifsta->assocreq_ies, ies, ifsta->assocreq_ies_len);
632
633 ieee80211_sta_tx(dev, skb, 0);
634}
635
636
637static void ieee80211_send_deauth(struct net_device *dev,
638 struct ieee80211_if_sta *ifsta, u16 reason)
639{
640 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
641 struct sk_buff *skb;
642 struct ieee80211_mgmt *mgmt;
643
644 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
645 if (!skb) {
646 printk(KERN_DEBUG "%s: failed to allocate buffer for deauth "
647 "frame\n", dev->name);
648 return;
649 }
650 skb_reserve(skb, local->hw.extra_tx_headroom);
651
652 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
653 memset(mgmt, 0, 24);
654 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
655 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
656 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
657 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
658 IEEE80211_STYPE_DEAUTH);
659 skb_put(skb, 2);
660 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
661
662 ieee80211_sta_tx(dev, skb, 0);
663}
664
665
666static void ieee80211_send_disassoc(struct net_device *dev,
667 struct ieee80211_if_sta *ifsta, u16 reason)
668{
669 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
670 struct sk_buff *skb;
671 struct ieee80211_mgmt *mgmt;
672
673 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
674 if (!skb) {
675 printk(KERN_DEBUG "%s: failed to allocate buffer for disassoc "
676 "frame\n", dev->name);
677 return;
678 }
679 skb_reserve(skb, local->hw.extra_tx_headroom);
680
681 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
682 memset(mgmt, 0, 24);
683 memcpy(mgmt->da, ifsta->bssid, ETH_ALEN);
684 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
685 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
686 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
687 IEEE80211_STYPE_DISASSOC);
688 skb_put(skb, 2);
689 mgmt->u.disassoc.reason_code = cpu_to_le16(reason);
690
691 ieee80211_sta_tx(dev, skb, 0);
692}
693
694
695static int ieee80211_privacy_mismatch(struct net_device *dev,
696 struct ieee80211_if_sta *ifsta)
697{
698 struct ieee80211_sta_bss *bss;
699 int res = 0;
700
701 if (!ifsta || ifsta->mixed_cell ||
702 ifsta->key_mgmt != IEEE80211_KEY_MGMT_NONE)
703 return 0;
704
705 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
706 if (!bss)
707 return 0;
708
709 if (ieee80211_sta_wep_configured(dev) !=
710 !!(bss->capability & WLAN_CAPABILITY_PRIVACY))
711 res = 1;
712
713 ieee80211_rx_bss_put(dev, bss);
714
715 return res;
716}
717
718
719static void ieee80211_associate(struct net_device *dev,
720 struct ieee80211_if_sta *ifsta)
721{
722 ifsta->assoc_tries++;
723 if (ifsta->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
724 printk(KERN_DEBUG "%s: association with AP " MAC_FMT
725 " timed out\n",
726 dev->name, MAC_ARG(ifsta->bssid));
727 ifsta->state = IEEE80211_DISABLED;
728 return;
729 }
730
731 ifsta->state = IEEE80211_ASSOCIATE;
732 printk(KERN_DEBUG "%s: associate with AP " MAC_FMT "\n",
733 dev->name, MAC_ARG(ifsta->bssid));
734 if (ieee80211_privacy_mismatch(dev, ifsta)) {
735 printk(KERN_DEBUG "%s: mismatch in privacy configuration and "
736 "mixed-cell disabled - abort association\n", dev->name);
737 ifsta->state = IEEE80211_DISABLED;
738 return;
739 }
740
741 ieee80211_send_assoc(dev, ifsta);
742
743 mod_timer(&ifsta->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
744}
745
746
747static void ieee80211_associated(struct net_device *dev,
748 struct ieee80211_if_sta *ifsta)
749{
750 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
751 struct sta_info *sta;
752 int disassoc;
753
754 /* TODO: start monitoring current AP signal quality and number of
755 * missed beacons. Scan other channels every now and then and search
756 * for better APs. */
757 /* TODO: remove expired BSSes */
758
759 ifsta->state = IEEE80211_ASSOCIATED;
760
761 sta = sta_info_get(local, ifsta->bssid);
762 if (!sta) {
763 printk(KERN_DEBUG "%s: No STA entry for own AP " MAC_FMT "\n",
764 dev->name, MAC_ARG(ifsta->bssid));
765 disassoc = 1;
766 } else {
767 disassoc = 0;
768 if (time_after(jiffies,
769 sta->last_rx + IEEE80211_MONITORING_INTERVAL)) {
770 if (ifsta->probereq_poll) {
771 printk(KERN_DEBUG "%s: No ProbeResp from "
772 "current AP " MAC_FMT " - assume out of "
773 "range\n",
774 dev->name, MAC_ARG(ifsta->bssid));
775 disassoc = 1;
Michael Wube8755e2007-07-27 15:43:23 +0200776 sta_info_free(sta);
Jiri Bencf0706e82007-05-05 11:45:53 -0700777 ifsta->probereq_poll = 0;
778 } else {
779 ieee80211_send_probe_req(dev, ifsta->bssid,
780 local->scan_ssid,
781 local->scan_ssid_len);
782 ifsta->probereq_poll = 1;
783 }
784 } else {
785 ifsta->probereq_poll = 0;
786 if (time_after(jiffies, ifsta->last_probe +
787 IEEE80211_PROBE_INTERVAL)) {
788 ifsta->last_probe = jiffies;
789 ieee80211_send_probe_req(dev, ifsta->bssid,
790 ifsta->ssid,
791 ifsta->ssid_len);
792 }
793 }
794 sta_info_put(sta);
795 }
796 if (disassoc) {
797 union iwreq_data wrqu;
798 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
799 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
800 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
801 mod_timer(&ifsta->timer, jiffies +
802 IEEE80211_MONITORING_INTERVAL + 30 * HZ);
803 } else {
804 mod_timer(&ifsta->timer, jiffies +
805 IEEE80211_MONITORING_INTERVAL);
806 }
807}
808
809
810static void ieee80211_send_probe_req(struct net_device *dev, u8 *dst,
811 u8 *ssid, size_t ssid_len)
812{
813 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
814 struct ieee80211_hw_mode *mode;
815 struct sk_buff *skb;
816 struct ieee80211_mgmt *mgmt;
817 u8 *pos, *supp_rates, *esupp_rates = NULL;
818 int i;
819
820 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200);
821 if (!skb) {
822 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
823 "request\n", dev->name);
824 return;
825 }
826 skb_reserve(skb, local->hw.extra_tx_headroom);
827
828 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
829 memset(mgmt, 0, 24);
830 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
831 IEEE80211_STYPE_PROBE_REQ);
832 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
833 if (dst) {
834 memcpy(mgmt->da, dst, ETH_ALEN);
835 memcpy(mgmt->bssid, dst, ETH_ALEN);
836 } else {
837 memset(mgmt->da, 0xff, ETH_ALEN);
838 memset(mgmt->bssid, 0xff, ETH_ALEN);
839 }
840 pos = skb_put(skb, 2 + ssid_len);
841 *pos++ = WLAN_EID_SSID;
842 *pos++ = ssid_len;
843 memcpy(pos, ssid, ssid_len);
844
845 supp_rates = skb_put(skb, 2);
846 supp_rates[0] = WLAN_EID_SUPP_RATES;
847 supp_rates[1] = 0;
848 mode = local->oper_hw_mode;
849 for (i = 0; i < mode->num_rates; i++) {
850 struct ieee80211_rate *rate = &mode->rates[i];
851 if (!(rate->flags & IEEE80211_RATE_SUPPORTED))
852 continue;
853 if (esupp_rates) {
854 pos = skb_put(skb, 1);
855 esupp_rates[1]++;
856 } else if (supp_rates[1] == 8) {
857 esupp_rates = skb_put(skb, 3);
858 esupp_rates[0] = WLAN_EID_EXT_SUPP_RATES;
859 esupp_rates[1] = 1;
860 pos = &esupp_rates[2];
861 } else {
862 pos = skb_put(skb, 1);
863 supp_rates[1]++;
864 }
865 if (mode->mode == MODE_ATHEROS_TURBO)
866 *pos = rate->rate / 10;
867 else
868 *pos = rate->rate / 5;
869 }
870
871 ieee80211_sta_tx(dev, skb, 0);
872}
873
874
875static int ieee80211_sta_wep_configured(struct net_device *dev)
876{
877 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
878 if (!sdata || !sdata->default_key ||
879 sdata->default_key->alg != ALG_WEP)
880 return 0;
881 return 1;
882}
883
884
885static void ieee80211_auth_completed(struct net_device *dev,
886 struct ieee80211_if_sta *ifsta)
887{
888 printk(KERN_DEBUG "%s: authenticated\n", dev->name);
889 ifsta->authenticated = 1;
890 ieee80211_associate(dev, ifsta);
891}
892
893
894static void ieee80211_auth_challenge(struct net_device *dev,
895 struct ieee80211_if_sta *ifsta,
896 struct ieee80211_mgmt *mgmt,
897 size_t len)
898{
899 u8 *pos;
900 struct ieee802_11_elems elems;
901
902 printk(KERN_DEBUG "%s: replying to auth challenge\n", dev->name);
903 pos = mgmt->u.auth.variable;
904 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
905 == ParseFailed) {
906 printk(KERN_DEBUG "%s: failed to parse Auth(challenge)\n",
907 dev->name);
908 return;
909 }
910 if (!elems.challenge) {
911 printk(KERN_DEBUG "%s: no challenge IE in shared key auth "
912 "frame\n", dev->name);
913 return;
914 }
915 ieee80211_send_auth(dev, ifsta, 3, elems.challenge - 2,
916 elems.challenge_len + 2, 1);
917}
918
919
920static void ieee80211_rx_mgmt_auth(struct net_device *dev,
921 struct ieee80211_if_sta *ifsta,
922 struct ieee80211_mgmt *mgmt,
923 size_t len)
924{
925 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
926 u16 auth_alg, auth_transaction, status_code;
927
928 if (ifsta->state != IEEE80211_AUTHENTICATE &&
929 sdata->type != IEEE80211_IF_TYPE_IBSS) {
930 printk(KERN_DEBUG "%s: authentication frame received from "
931 MAC_FMT ", but not in authenticate state - ignored\n",
932 dev->name, MAC_ARG(mgmt->sa));
933 return;
934 }
935
936 if (len < 24 + 6) {
937 printk(KERN_DEBUG "%s: too short (%zd) authentication frame "
938 "received from " MAC_FMT " - ignored\n",
939 dev->name, len, MAC_ARG(mgmt->sa));
940 return;
941 }
942
943 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
944 memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
945 printk(KERN_DEBUG "%s: authentication frame received from "
946 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
947 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
948 MAC_ARG(mgmt->bssid));
949 return;
950 }
951
952 if (sdata->type != IEEE80211_IF_TYPE_IBSS &&
953 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0) {
954 printk(KERN_DEBUG "%s: authentication frame received from "
955 "unknown BSSID (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
956 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
957 MAC_ARG(mgmt->bssid));
958 return;
959 }
960
961 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
962 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
963 status_code = le16_to_cpu(mgmt->u.auth.status_code);
964
965 printk(KERN_DEBUG "%s: RX authentication from " MAC_FMT " (alg=%d "
966 "transaction=%d status=%d)\n",
967 dev->name, MAC_ARG(mgmt->sa), auth_alg,
968 auth_transaction, status_code);
969
970 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
971 /* IEEE 802.11 standard does not require authentication in IBSS
972 * networks and most implementations do not seem to use it.
973 * However, try to reply to authentication attempts if someone
974 * has actually implemented this.
975 * TODO: Could implement shared key authentication. */
976 if (auth_alg != WLAN_AUTH_OPEN || auth_transaction != 1) {
977 printk(KERN_DEBUG "%s: unexpected IBSS authentication "
978 "frame (alg=%d transaction=%d)\n",
979 dev->name, auth_alg, auth_transaction);
980 return;
981 }
982 ieee80211_send_auth(dev, ifsta, 2, NULL, 0, 0);
983 }
984
985 if (auth_alg != ifsta->auth_alg ||
986 auth_transaction != ifsta->auth_transaction) {
987 printk(KERN_DEBUG "%s: unexpected authentication frame "
988 "(alg=%d transaction=%d)\n",
989 dev->name, auth_alg, auth_transaction);
990 return;
991 }
992
993 if (status_code != WLAN_STATUS_SUCCESS) {
994 printk(KERN_DEBUG "%s: AP denied authentication (auth_alg=%d "
995 "code=%d)\n", dev->name, ifsta->auth_alg, status_code);
996 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG) {
997 u8 algs[3];
998 const int num_algs = ARRAY_SIZE(algs);
999 int i, pos;
1000 algs[0] = algs[1] = algs[2] = 0xff;
1001 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
1002 algs[0] = WLAN_AUTH_OPEN;
1003 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
1004 algs[1] = WLAN_AUTH_SHARED_KEY;
1005 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
1006 algs[2] = WLAN_AUTH_LEAP;
1007 if (ifsta->auth_alg == WLAN_AUTH_OPEN)
1008 pos = 0;
1009 else if (ifsta->auth_alg == WLAN_AUTH_SHARED_KEY)
1010 pos = 1;
1011 else
1012 pos = 2;
1013 for (i = 0; i < num_algs; i++) {
1014 pos++;
1015 if (pos >= num_algs)
1016 pos = 0;
1017 if (algs[pos] == ifsta->auth_alg ||
1018 algs[pos] == 0xff)
1019 continue;
1020 if (algs[pos] == WLAN_AUTH_SHARED_KEY &&
1021 !ieee80211_sta_wep_configured(dev))
1022 continue;
1023 ifsta->auth_alg = algs[pos];
1024 printk(KERN_DEBUG "%s: set auth_alg=%d for "
1025 "next try\n",
1026 dev->name, ifsta->auth_alg);
1027 break;
1028 }
1029 }
1030 return;
1031 }
1032
1033 switch (ifsta->auth_alg) {
1034 case WLAN_AUTH_OPEN:
1035 case WLAN_AUTH_LEAP:
1036 ieee80211_auth_completed(dev, ifsta);
1037 break;
1038 case WLAN_AUTH_SHARED_KEY:
1039 if (ifsta->auth_transaction == 4)
1040 ieee80211_auth_completed(dev, ifsta);
1041 else
1042 ieee80211_auth_challenge(dev, ifsta, mgmt, len);
1043 break;
1044 }
1045}
1046
1047
1048static void ieee80211_rx_mgmt_deauth(struct net_device *dev,
1049 struct ieee80211_if_sta *ifsta,
1050 struct ieee80211_mgmt *mgmt,
1051 size_t len)
1052{
1053 u16 reason_code;
1054
1055 if (len < 24 + 2) {
1056 printk(KERN_DEBUG "%s: too short (%zd) deauthentication frame "
1057 "received from " MAC_FMT " - ignored\n",
1058 dev->name, len, MAC_ARG(mgmt->sa));
1059 return;
1060 }
1061
1062 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1063 printk(KERN_DEBUG "%s: deauthentication frame received from "
1064 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1065 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1066 MAC_ARG(mgmt->bssid));
1067 return;
1068 }
1069
1070 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
1071
1072 printk(KERN_DEBUG "%s: RX deauthentication from " MAC_FMT
1073 " (reason=%d)\n",
1074 dev->name, MAC_ARG(mgmt->sa), reason_code);
1075
1076 if (ifsta->authenticated) {
1077 printk(KERN_DEBUG "%s: deauthenticated\n", dev->name);
1078 }
1079
1080 if (ifsta->state == IEEE80211_AUTHENTICATE ||
1081 ifsta->state == IEEE80211_ASSOCIATE ||
1082 ifsta->state == IEEE80211_ASSOCIATED) {
1083 ifsta->state = IEEE80211_AUTHENTICATE;
1084 mod_timer(&ifsta->timer, jiffies +
1085 IEEE80211_RETRY_AUTH_INTERVAL);
1086 }
1087
1088 ieee80211_set_disassoc(dev, ifsta, 1);
1089 ifsta->authenticated = 0;
1090}
1091
1092
1093static void ieee80211_rx_mgmt_disassoc(struct net_device *dev,
1094 struct ieee80211_if_sta *ifsta,
1095 struct ieee80211_mgmt *mgmt,
1096 size_t len)
1097{
1098 u16 reason_code;
1099
1100 if (len < 24 + 2) {
1101 printk(KERN_DEBUG "%s: too short (%zd) disassociation frame "
1102 "received from " MAC_FMT " - ignored\n",
1103 dev->name, len, MAC_ARG(mgmt->sa));
1104 return;
1105 }
1106
1107 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1108 printk(KERN_DEBUG "%s: disassociation frame received from "
1109 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1110 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1111 MAC_ARG(mgmt->bssid));
1112 return;
1113 }
1114
1115 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
1116
1117 printk(KERN_DEBUG "%s: RX disassociation from " MAC_FMT
1118 " (reason=%d)\n",
1119 dev->name, MAC_ARG(mgmt->sa), reason_code);
1120
1121 if (ifsta->associated)
1122 printk(KERN_DEBUG "%s: disassociated\n", dev->name);
1123
1124 if (ifsta->state == IEEE80211_ASSOCIATED) {
1125 ifsta->state = IEEE80211_ASSOCIATE;
1126 mod_timer(&ifsta->timer, jiffies +
1127 IEEE80211_RETRY_AUTH_INTERVAL);
1128 }
1129
1130 ieee80211_set_disassoc(dev, ifsta, 0);
1131}
1132
1133
1134static void ieee80211_rx_mgmt_assoc_resp(struct net_device *dev,
1135 struct ieee80211_if_sta *ifsta,
1136 struct ieee80211_mgmt *mgmt,
1137 size_t len,
1138 int reassoc)
1139{
1140 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1141 struct ieee80211_hw_mode *mode;
1142 struct sta_info *sta;
1143 u32 rates;
1144 u16 capab_info, status_code, aid;
1145 struct ieee802_11_elems elems;
1146 u8 *pos;
1147 int i, j;
1148
1149 /* AssocResp and ReassocResp have identical structure, so process both
1150 * of them in this function. */
1151
1152 if (ifsta->state != IEEE80211_ASSOCIATE) {
1153 printk(KERN_DEBUG "%s: association frame received from "
1154 MAC_FMT ", but not in associate state - ignored\n",
1155 dev->name, MAC_ARG(mgmt->sa));
1156 return;
1157 }
1158
1159 if (len < 24 + 6) {
1160 printk(KERN_DEBUG "%s: too short (%zd) association frame "
1161 "received from " MAC_FMT " - ignored\n",
1162 dev->name, len, MAC_ARG(mgmt->sa));
1163 return;
1164 }
1165
1166 if (memcmp(ifsta->bssid, mgmt->sa, ETH_ALEN) != 0) {
1167 printk(KERN_DEBUG "%s: association frame received from "
1168 "unknown AP (SA=" MAC_FMT " BSSID=" MAC_FMT ") - "
1169 "ignored\n", dev->name, MAC_ARG(mgmt->sa),
1170 MAC_ARG(mgmt->bssid));
1171 return;
1172 }
1173
1174 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
1175 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
1176 aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
1177 if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
1178 printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
1179 "set\n", dev->name, aid);
1180 aid &= ~(BIT(15) | BIT(14));
1181
1182 printk(KERN_DEBUG "%s: RX %sssocResp from " MAC_FMT " (capab=0x%x "
1183 "status=%d aid=%d)\n",
1184 dev->name, reassoc ? "Rea" : "A", MAC_ARG(mgmt->sa),
1185 capab_info, status_code, aid);
1186
1187 if (status_code != WLAN_STATUS_SUCCESS) {
1188 printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
1189 dev->name, status_code);
Zhu Yif11b0f02007-05-09 13:41:52 +08001190 if (status_code == WLAN_STATUS_REASSOC_NO_ASSOC)
1191 ifsta->prev_bssid_set = 0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001192 return;
1193 }
1194
1195 pos = mgmt->u.assoc_resp.variable;
1196 if (ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems)
1197 == ParseFailed) {
1198 printk(KERN_DEBUG "%s: failed to parse AssocResp\n",
1199 dev->name);
1200 return;
1201 }
1202
1203 if (!elems.supp_rates) {
1204 printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
1205 dev->name);
1206 return;
1207 }
1208
Daniel Drake56282212007-07-10 19:32:10 +02001209 /* it probably doesn't, but if the frame includes an ERP value then
1210 * update our stored copy */
1211 if (elems.erp_info && elems.erp_info_len >= 1) {
1212 struct ieee80211_sta_bss *bss
1213 = ieee80211_rx_bss_get(dev, ifsta->bssid);
1214 if (bss) {
1215 bss->erp_value = elems.erp_info[0];
1216 bss->has_erp_value = 1;
1217 ieee80211_rx_bss_put(dev, bss);
1218 }
1219 }
1220
Jiri Bencf0706e82007-05-05 11:45:53 -07001221 printk(KERN_DEBUG "%s: associated\n", dev->name);
1222 ifsta->aid = aid;
1223 ifsta->ap_capab = capab_info;
1224
1225 kfree(ifsta->assocresp_ies);
1226 ifsta->assocresp_ies_len = len - (pos - (u8 *) mgmt);
1227 ifsta->assocresp_ies = kmalloc(ifsta->assocresp_ies_len, GFP_ATOMIC);
1228 if (ifsta->assocresp_ies)
1229 memcpy(ifsta->assocresp_ies, pos, ifsta->assocresp_ies_len);
1230
1231 ieee80211_set_associated(dev, ifsta, 1);
1232
1233 /* Add STA entry for the AP */
1234 sta = sta_info_get(local, ifsta->bssid);
1235 if (!sta) {
1236 struct ieee80211_sta_bss *bss;
1237 sta = sta_info_add(local, dev, ifsta->bssid, GFP_ATOMIC);
1238 if (!sta) {
1239 printk(KERN_DEBUG "%s: failed to add STA entry for the"
1240 " AP\n", dev->name);
1241 return;
1242 }
1243 bss = ieee80211_rx_bss_get(dev, ifsta->bssid);
1244 if (bss) {
1245 sta->last_rssi = bss->rssi;
1246 sta->last_signal = bss->signal;
1247 sta->last_noise = bss->noise;
1248 ieee80211_rx_bss_put(dev, bss);
1249 }
1250 }
1251
1252 sta->dev = dev;
1253 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
1254 sta->assoc_ap = 1;
1255
1256 rates = 0;
1257 mode = local->oper_hw_mode;
1258 for (i = 0; i < elems.supp_rates_len; i++) {
1259 int rate = (elems.supp_rates[i] & 0x7f) * 5;
1260 if (mode->mode == MODE_ATHEROS_TURBO)
1261 rate *= 2;
1262 for (j = 0; j < mode->num_rates; j++)
1263 if (mode->rates[j].rate == rate)
1264 rates |= BIT(j);
1265 }
1266 for (i = 0; i < elems.ext_supp_rates_len; i++) {
1267 int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
1268 if (mode->mode == MODE_ATHEROS_TURBO)
1269 rate *= 2;
1270 for (j = 0; j < mode->num_rates; j++)
1271 if (mode->rates[j].rate == rate)
1272 rates |= BIT(j);
1273 }
1274 sta->supp_rates = rates;
1275
1276 rate_control_rate_init(sta, local);
1277
1278 if (elems.wmm_param && ifsta->wmm_enabled) {
1279 sta->flags |= WLAN_STA_WME;
1280 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1281 elems.wmm_param_len);
1282 }
1283
1284
1285 sta_info_put(sta);
1286
1287 ieee80211_associated(dev, ifsta);
1288}
1289
1290
1291/* Caller must hold local->sta_bss_lock */
1292static void __ieee80211_rx_bss_hash_add(struct net_device *dev,
1293 struct ieee80211_sta_bss *bss)
1294{
1295 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1296 bss->hnext = local->sta_bss_hash[STA_HASH(bss->bssid)];
1297 local->sta_bss_hash[STA_HASH(bss->bssid)] = bss;
1298}
1299
1300
1301/* Caller must hold local->sta_bss_lock */
1302static void __ieee80211_rx_bss_hash_del(struct net_device *dev,
1303 struct ieee80211_sta_bss *bss)
1304{
1305 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1306 struct ieee80211_sta_bss *b, *prev = NULL;
1307 b = local->sta_bss_hash[STA_HASH(bss->bssid)];
1308 while (b) {
1309 if (b == bss) {
1310 if (!prev)
1311 local->sta_bss_hash[STA_HASH(bss->bssid)] =
1312 bss->hnext;
1313 else
1314 prev->hnext = bss->hnext;
1315 break;
1316 }
1317 prev = b;
1318 b = b->hnext;
1319 }
1320}
1321
1322
1323static struct ieee80211_sta_bss *
1324ieee80211_rx_bss_add(struct net_device *dev, u8 *bssid)
1325{
1326 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1327 struct ieee80211_sta_bss *bss;
1328
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001329 bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
Jiri Bencf0706e82007-05-05 11:45:53 -07001330 if (!bss)
1331 return NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001332 atomic_inc(&bss->users);
1333 atomic_inc(&bss->users);
1334 memcpy(bss->bssid, bssid, ETH_ALEN);
1335
1336 spin_lock_bh(&local->sta_bss_lock);
1337 /* TODO: order by RSSI? */
1338 list_add_tail(&bss->list, &local->sta_bss_list);
1339 __ieee80211_rx_bss_hash_add(dev, bss);
1340 spin_unlock_bh(&local->sta_bss_lock);
1341 return bss;
1342}
1343
1344
1345static struct ieee80211_sta_bss *
1346ieee80211_rx_bss_get(struct net_device *dev, u8 *bssid)
1347{
1348 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1349 struct ieee80211_sta_bss *bss;
1350
1351 spin_lock_bh(&local->sta_bss_lock);
1352 bss = local->sta_bss_hash[STA_HASH(bssid)];
1353 while (bss) {
1354 if (memcmp(bss->bssid, bssid, ETH_ALEN) == 0) {
1355 atomic_inc(&bss->users);
1356 break;
1357 }
1358 bss = bss->hnext;
1359 }
1360 spin_unlock_bh(&local->sta_bss_lock);
1361 return bss;
1362}
1363
1364
1365static void ieee80211_rx_bss_free(struct ieee80211_sta_bss *bss)
1366{
1367 kfree(bss->wpa_ie);
1368 kfree(bss->rsn_ie);
1369 kfree(bss->wmm_ie);
1370 kfree(bss);
1371}
1372
1373
1374static void ieee80211_rx_bss_put(struct net_device *dev,
1375 struct ieee80211_sta_bss *bss)
1376{
1377 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1378 if (!atomic_dec_and_test(&bss->users))
1379 return;
1380
1381 spin_lock_bh(&local->sta_bss_lock);
1382 __ieee80211_rx_bss_hash_del(dev, bss);
1383 list_del(&bss->list);
1384 spin_unlock_bh(&local->sta_bss_lock);
1385 ieee80211_rx_bss_free(bss);
1386}
1387
1388
1389void ieee80211_rx_bss_list_init(struct net_device *dev)
1390{
1391 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1392 spin_lock_init(&local->sta_bss_lock);
1393 INIT_LIST_HEAD(&local->sta_bss_list);
1394}
1395
1396
1397void ieee80211_rx_bss_list_deinit(struct net_device *dev)
1398{
1399 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1400 struct ieee80211_sta_bss *bss, *tmp;
1401
1402 list_for_each_entry_safe(bss, tmp, &local->sta_bss_list, list)
1403 ieee80211_rx_bss_put(dev, bss);
1404}
1405
1406
1407static void ieee80211_rx_bss_info(struct net_device *dev,
1408 struct ieee80211_mgmt *mgmt,
1409 size_t len,
1410 struct ieee80211_rx_status *rx_status,
1411 int beacon)
1412{
1413 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1414 struct ieee802_11_elems elems;
1415 size_t baselen;
1416 int channel, invalid = 0, clen;
1417 struct ieee80211_sta_bss *bss;
1418 struct sta_info *sta;
1419 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1420 u64 timestamp;
1421
1422 if (!beacon && memcmp(mgmt->da, dev->dev_addr, ETH_ALEN))
1423 return; /* ignore ProbeResp to foreign address */
1424
1425#if 0
1426 printk(KERN_DEBUG "%s: RX %s from " MAC_FMT " to " MAC_FMT "\n",
1427 dev->name, beacon ? "Beacon" : "Probe Response",
1428 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da));
1429#endif
1430
1431 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1432 if (baselen > len)
1433 return;
1434
1435 timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1436
1437 if (sdata->type == IEEE80211_IF_TYPE_IBSS && beacon &&
1438 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0) {
1439#ifdef CONFIG_MAC80211_IBSS_DEBUG
1440 static unsigned long last_tsf_debug = 0;
1441 u64 tsf;
1442 if (local->ops->get_tsf)
1443 tsf = local->ops->get_tsf(local_to_hw(local));
1444 else
1445 tsf = -1LLU;
1446 if (time_after(jiffies, last_tsf_debug + 5 * HZ)) {
1447 printk(KERN_DEBUG "RX beacon SA=" MAC_FMT " BSSID="
1448 MAC_FMT " TSF=0x%llx BCN=0x%llx diff=%lld "
1449 "@%lu\n",
1450 MAC_ARG(mgmt->sa), MAC_ARG(mgmt->bssid),
1451 (unsigned long long)tsf,
1452 (unsigned long long)timestamp,
1453 (unsigned long long)(tsf - timestamp),
1454 jiffies);
1455 last_tsf_debug = jiffies;
1456 }
1457#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1458 }
1459
1460 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1461 &elems) == ParseFailed)
1462 invalid = 1;
1463
1464 if (sdata->type == IEEE80211_IF_TYPE_IBSS && elems.supp_rates &&
1465 memcmp(mgmt->bssid, sdata->u.sta.bssid, ETH_ALEN) == 0 &&
1466 (sta = sta_info_get(local, mgmt->sa))) {
1467 struct ieee80211_hw_mode *mode;
1468 struct ieee80211_rate *rates;
1469 size_t num_rates;
1470 u32 supp_rates, prev_rates;
1471 int i, j;
1472
1473 mode = local->sta_scanning ?
1474 local->scan_hw_mode : local->oper_hw_mode;
1475 rates = mode->rates;
1476 num_rates = mode->num_rates;
1477
1478 supp_rates = 0;
1479 for (i = 0; i < elems.supp_rates_len +
1480 elems.ext_supp_rates_len; i++) {
1481 u8 rate = 0;
1482 int own_rate;
1483 if (i < elems.supp_rates_len)
1484 rate = elems.supp_rates[i];
1485 else if (elems.ext_supp_rates)
1486 rate = elems.ext_supp_rates
1487 [i - elems.supp_rates_len];
1488 own_rate = 5 * (rate & 0x7f);
1489 if (mode->mode == MODE_ATHEROS_TURBO)
1490 own_rate *= 2;
1491 for (j = 0; j < num_rates; j++)
1492 if (rates[j].rate == own_rate)
1493 supp_rates |= BIT(j);
1494 }
1495
1496 prev_rates = sta->supp_rates;
1497 sta->supp_rates &= supp_rates;
1498 if (sta->supp_rates == 0) {
1499 /* No matching rates - this should not really happen.
1500 * Make sure that at least one rate is marked
1501 * supported to avoid issues with TX rate ctrl. */
1502 sta->supp_rates = sdata->u.sta.supp_rates_bits;
1503 }
1504 if (sta->supp_rates != prev_rates) {
1505 printk(KERN_DEBUG "%s: updated supp_rates set for "
1506 MAC_FMT " based on beacon info (0x%x & 0x%x -> "
1507 "0x%x)\n",
1508 dev->name, MAC_ARG(sta->addr), prev_rates,
1509 supp_rates, sta->supp_rates);
1510 }
1511 sta_info_put(sta);
1512 }
1513
1514 if (!elems.ssid)
1515 return;
1516
1517 if (elems.ds_params && elems.ds_params_len == 1)
1518 channel = elems.ds_params[0];
1519 else
1520 channel = rx_status->channel;
1521
1522 bss = ieee80211_rx_bss_get(dev, mgmt->bssid);
1523 if (!bss) {
1524 bss = ieee80211_rx_bss_add(dev, mgmt->bssid);
1525 if (!bss)
1526 return;
1527 } else {
1528#if 0
1529 /* TODO: order by RSSI? */
1530 spin_lock_bh(&local->sta_bss_lock);
1531 list_move_tail(&bss->list, &local->sta_bss_list);
1532 spin_unlock_bh(&local->sta_bss_lock);
1533#endif
1534 }
1535
1536 if (bss->probe_resp && beacon) {
1537 /* Do not allow beacon to override data from Probe Response. */
1538 ieee80211_rx_bss_put(dev, bss);
1539 return;
1540 }
1541
Daniel Drake56282212007-07-10 19:32:10 +02001542 /* save the ERP value so that it is available at association time */
1543 if (elems.erp_info && elems.erp_info_len >= 1) {
1544 bss->erp_value = elems.erp_info[0];
1545 bss->has_erp_value = 1;
1546 }
1547
Jiri Bencf0706e82007-05-05 11:45:53 -07001548 bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
1549 bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
1550 if (elems.ssid && elems.ssid_len <= IEEE80211_MAX_SSID_LEN) {
1551 memcpy(bss->ssid, elems.ssid, elems.ssid_len);
1552 bss->ssid_len = elems.ssid_len;
1553 }
1554
1555 bss->supp_rates_len = 0;
1556 if (elems.supp_rates) {
1557 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1558 if (clen > elems.supp_rates_len)
1559 clen = elems.supp_rates_len;
1560 memcpy(&bss->supp_rates[bss->supp_rates_len], elems.supp_rates,
1561 clen);
1562 bss->supp_rates_len += clen;
1563 }
1564 if (elems.ext_supp_rates) {
1565 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
1566 if (clen > elems.ext_supp_rates_len)
1567 clen = elems.ext_supp_rates_len;
1568 memcpy(&bss->supp_rates[bss->supp_rates_len],
1569 elems.ext_supp_rates, clen);
1570 bss->supp_rates_len += clen;
1571 }
1572
1573 if (elems.wpa &&
1574 (!bss->wpa_ie || bss->wpa_ie_len != elems.wpa_len ||
1575 memcmp(bss->wpa_ie, elems.wpa, elems.wpa_len))) {
1576 kfree(bss->wpa_ie);
1577 bss->wpa_ie = kmalloc(elems.wpa_len + 2, GFP_ATOMIC);
1578 if (bss->wpa_ie) {
1579 memcpy(bss->wpa_ie, elems.wpa - 2, elems.wpa_len + 2);
1580 bss->wpa_ie_len = elems.wpa_len + 2;
1581 } else
1582 bss->wpa_ie_len = 0;
1583 } else if (!elems.wpa && bss->wpa_ie) {
1584 kfree(bss->wpa_ie);
1585 bss->wpa_ie = NULL;
1586 bss->wpa_ie_len = 0;
1587 }
1588
1589 if (elems.rsn &&
1590 (!bss->rsn_ie || bss->rsn_ie_len != elems.rsn_len ||
1591 memcmp(bss->rsn_ie, elems.rsn, elems.rsn_len))) {
1592 kfree(bss->rsn_ie);
1593 bss->rsn_ie = kmalloc(elems.rsn_len + 2, GFP_ATOMIC);
1594 if (bss->rsn_ie) {
1595 memcpy(bss->rsn_ie, elems.rsn - 2, elems.rsn_len + 2);
1596 bss->rsn_ie_len = elems.rsn_len + 2;
1597 } else
1598 bss->rsn_ie_len = 0;
1599 } else if (!elems.rsn && bss->rsn_ie) {
1600 kfree(bss->rsn_ie);
1601 bss->rsn_ie = NULL;
1602 bss->rsn_ie_len = 0;
1603 }
1604
1605 if (elems.wmm_param &&
1606 (!bss->wmm_ie || bss->wmm_ie_len != elems.wmm_param_len ||
1607 memcmp(bss->wmm_ie, elems.wmm_param, elems.wmm_param_len))) {
1608 kfree(bss->wmm_ie);
1609 bss->wmm_ie = kmalloc(elems.wmm_param_len + 2, GFP_ATOMIC);
1610 if (bss->wmm_ie) {
1611 memcpy(bss->wmm_ie, elems.wmm_param - 2,
1612 elems.wmm_param_len + 2);
1613 bss->wmm_ie_len = elems.wmm_param_len + 2;
1614 } else
1615 bss->wmm_ie_len = 0;
1616 } else if (!elems.wmm_param && bss->wmm_ie) {
1617 kfree(bss->wmm_ie);
1618 bss->wmm_ie = NULL;
1619 bss->wmm_ie_len = 0;
1620 }
1621
1622
1623 bss->hw_mode = rx_status->phymode;
1624 bss->channel = channel;
1625 bss->freq = rx_status->freq;
1626 if (channel != rx_status->channel &&
1627 (bss->hw_mode == MODE_IEEE80211G ||
1628 bss->hw_mode == MODE_IEEE80211B) &&
1629 channel >= 1 && channel <= 14) {
1630 static const int freq_list[] = {
1631 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1632 2447, 2452, 2457, 2462, 2467, 2472, 2484
1633 };
1634 /* IEEE 802.11g/b mode can receive packets from neighboring
1635 * channels, so map the channel into frequency. */
1636 bss->freq = freq_list[channel - 1];
1637 }
1638 bss->timestamp = timestamp;
1639 bss->last_update = jiffies;
1640 bss->rssi = rx_status->ssi;
1641 bss->signal = rx_status->signal;
1642 bss->noise = rx_status->noise;
1643 if (!beacon)
1644 bss->probe_resp++;
1645 ieee80211_rx_bss_put(dev, bss);
1646}
1647
1648
1649static void ieee80211_rx_mgmt_probe_resp(struct net_device *dev,
1650 struct ieee80211_mgmt *mgmt,
1651 size_t len,
1652 struct ieee80211_rx_status *rx_status)
1653{
1654 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 0);
1655}
1656
1657
1658static void ieee80211_rx_mgmt_beacon(struct net_device *dev,
1659 struct ieee80211_mgmt *mgmt,
1660 size_t len,
1661 struct ieee80211_rx_status *rx_status)
1662{
Jiri Bencf0706e82007-05-05 11:45:53 -07001663 struct ieee80211_sub_if_data *sdata;
1664 struct ieee80211_if_sta *ifsta;
Jiri Bencf0706e82007-05-05 11:45:53 -07001665 size_t baselen;
1666 struct ieee802_11_elems elems;
1667
1668 ieee80211_rx_bss_info(dev, mgmt, len, rx_status, 1);
1669
1670 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1671 if (sdata->type != IEEE80211_IF_TYPE_STA)
1672 return;
1673 ifsta = &sdata->u.sta;
1674
1675 if (!ifsta->associated ||
1676 memcmp(ifsta->bssid, mgmt->bssid, ETH_ALEN) != 0)
1677 return;
1678
1679 /* Process beacon from the current BSS */
1680 baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
1681 if (baselen > len)
1682 return;
1683
1684 if (ieee802_11_parse_elems(mgmt->u.beacon.variable, len - baselen,
1685 &elems) == ParseFailed)
1686 return;
1687
Daniel Drake56282212007-07-10 19:32:10 +02001688 if (elems.erp_info && elems.erp_info_len >= 1)
1689 ieee80211_handle_erp_ie(dev, elems.erp_info[0]);
Jiri Bencf0706e82007-05-05 11:45:53 -07001690
1691 if (elems.wmm_param && ifsta->wmm_enabled) {
1692 ieee80211_sta_wmm_params(dev, ifsta, elems.wmm_param,
1693 elems.wmm_param_len);
1694 }
1695}
1696
1697
1698static void ieee80211_rx_mgmt_probe_req(struct net_device *dev,
1699 struct ieee80211_if_sta *ifsta,
1700 struct ieee80211_mgmt *mgmt,
1701 size_t len,
1702 struct ieee80211_rx_status *rx_status)
1703{
1704 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1705 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1706 int tx_last_beacon;
1707 struct sk_buff *skb;
1708 struct ieee80211_mgmt *resp;
1709 u8 *pos, *end;
1710
1711 if (sdata->type != IEEE80211_IF_TYPE_IBSS ||
1712 ifsta->state != IEEE80211_IBSS_JOINED ||
1713 len < 24 + 2 || !ifsta->probe_resp)
1714 return;
1715
1716 if (local->ops->tx_last_beacon)
1717 tx_last_beacon = local->ops->tx_last_beacon(local_to_hw(local));
1718 else
1719 tx_last_beacon = 1;
1720
1721#ifdef CONFIG_MAC80211_IBSS_DEBUG
1722 printk(KERN_DEBUG "%s: RX ProbeReq SA=" MAC_FMT " DA=" MAC_FMT " BSSID="
1723 MAC_FMT " (tx_last_beacon=%d)\n",
1724 dev->name, MAC_ARG(mgmt->sa), MAC_ARG(mgmt->da),
1725 MAC_ARG(mgmt->bssid), tx_last_beacon);
1726#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1727
1728 if (!tx_last_beacon)
1729 return;
1730
1731 if (memcmp(mgmt->bssid, ifsta->bssid, ETH_ALEN) != 0 &&
1732 memcmp(mgmt->bssid, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1733 return;
1734
1735 end = ((u8 *) mgmt) + len;
1736 pos = mgmt->u.probe_req.variable;
1737 if (pos[0] != WLAN_EID_SSID ||
1738 pos + 2 + pos[1] > end) {
1739 if (net_ratelimit()) {
1740 printk(KERN_DEBUG "%s: Invalid SSID IE in ProbeReq "
1741 "from " MAC_FMT "\n",
1742 dev->name, MAC_ARG(mgmt->sa));
1743 }
1744 return;
1745 }
1746 if (pos[1] != 0 &&
1747 (pos[1] != ifsta->ssid_len ||
1748 memcmp(pos + 2, ifsta->ssid, ifsta->ssid_len) != 0)) {
1749 /* Ignore ProbeReq for foreign SSID */
1750 return;
1751 }
1752
1753 /* Reply with ProbeResp */
1754 skb = skb_copy(ifsta->probe_resp, GFP_ATOMIC);
1755 if (!skb)
1756 return;
1757
1758 resp = (struct ieee80211_mgmt *) skb->data;
1759 memcpy(resp->da, mgmt->sa, ETH_ALEN);
1760#ifdef CONFIG_MAC80211_IBSS_DEBUG
1761 printk(KERN_DEBUG "%s: Sending ProbeResp to " MAC_FMT "\n",
1762 dev->name, MAC_ARG(resp->da));
1763#endif /* CONFIG_MAC80211_IBSS_DEBUG */
1764 ieee80211_sta_tx(dev, skb, 0);
1765}
1766
1767
1768void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
1769 struct ieee80211_rx_status *rx_status)
1770{
1771 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1772 struct ieee80211_sub_if_data *sdata;
1773 struct ieee80211_if_sta *ifsta;
1774 struct ieee80211_mgmt *mgmt;
1775 u16 fc;
1776
1777 if (skb->len < 24)
1778 goto fail;
1779
1780 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1781 ifsta = &sdata->u.sta;
1782
1783 mgmt = (struct ieee80211_mgmt *) skb->data;
1784 fc = le16_to_cpu(mgmt->frame_control);
1785
1786 switch (fc & IEEE80211_FCTL_STYPE) {
1787 case IEEE80211_STYPE_PROBE_REQ:
1788 case IEEE80211_STYPE_PROBE_RESP:
1789 case IEEE80211_STYPE_BEACON:
1790 memcpy(skb->cb, rx_status, sizeof(*rx_status));
1791 case IEEE80211_STYPE_AUTH:
1792 case IEEE80211_STYPE_ASSOC_RESP:
1793 case IEEE80211_STYPE_REASSOC_RESP:
1794 case IEEE80211_STYPE_DEAUTH:
1795 case IEEE80211_STYPE_DISASSOC:
1796 skb_queue_tail(&ifsta->skb_queue, skb);
1797 queue_work(local->hw.workqueue, &ifsta->work);
1798 return;
1799 default:
1800 printk(KERN_DEBUG "%s: received unknown management frame - "
1801 "stype=%d\n", dev->name,
1802 (fc & IEEE80211_FCTL_STYPE) >> 4);
1803 break;
1804 }
1805
1806 fail:
1807 kfree_skb(skb);
1808}
1809
1810
1811static void ieee80211_sta_rx_queued_mgmt(struct net_device *dev,
1812 struct sk_buff *skb)
1813{
1814 struct ieee80211_rx_status *rx_status;
1815 struct ieee80211_sub_if_data *sdata;
1816 struct ieee80211_if_sta *ifsta;
1817 struct ieee80211_mgmt *mgmt;
1818 u16 fc;
1819
1820 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1821 ifsta = &sdata->u.sta;
1822
1823 rx_status = (struct ieee80211_rx_status *) skb->cb;
1824 mgmt = (struct ieee80211_mgmt *) skb->data;
1825 fc = le16_to_cpu(mgmt->frame_control);
1826
1827 switch (fc & IEEE80211_FCTL_STYPE) {
1828 case IEEE80211_STYPE_PROBE_REQ:
1829 ieee80211_rx_mgmt_probe_req(dev, ifsta, mgmt, skb->len,
1830 rx_status);
1831 break;
1832 case IEEE80211_STYPE_PROBE_RESP:
1833 ieee80211_rx_mgmt_probe_resp(dev, mgmt, skb->len, rx_status);
1834 break;
1835 case IEEE80211_STYPE_BEACON:
1836 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len, rx_status);
1837 break;
1838 case IEEE80211_STYPE_AUTH:
1839 ieee80211_rx_mgmt_auth(dev, ifsta, mgmt, skb->len);
1840 break;
1841 case IEEE80211_STYPE_ASSOC_RESP:
1842 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 0);
1843 break;
1844 case IEEE80211_STYPE_REASSOC_RESP:
1845 ieee80211_rx_mgmt_assoc_resp(dev, ifsta, mgmt, skb->len, 1);
1846 break;
1847 case IEEE80211_STYPE_DEAUTH:
1848 ieee80211_rx_mgmt_deauth(dev, ifsta, mgmt, skb->len);
1849 break;
1850 case IEEE80211_STYPE_DISASSOC:
1851 ieee80211_rx_mgmt_disassoc(dev, ifsta, mgmt, skb->len);
1852 break;
1853 }
1854
1855 kfree_skb(skb);
1856}
1857
1858
1859void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
1860 struct ieee80211_rx_status *rx_status)
1861{
1862 struct ieee80211_mgmt *mgmt;
1863 u16 fc;
1864
1865 if (skb->len < 24) {
1866 dev_kfree_skb(skb);
1867 return;
1868 }
1869
1870 mgmt = (struct ieee80211_mgmt *) skb->data;
1871 fc = le16_to_cpu(mgmt->frame_control);
1872
1873 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1874 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP) {
1875 ieee80211_rx_mgmt_probe_resp(dev, mgmt,
1876 skb->len, rx_status);
1877 } else if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) {
1878 ieee80211_rx_mgmt_beacon(dev, mgmt, skb->len,
1879 rx_status);
1880 }
1881 }
1882
1883 dev_kfree_skb(skb);
1884}
1885
1886
1887static int ieee80211_sta_active_ibss(struct net_device *dev)
1888{
1889 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1890 int active = 0;
1891 struct sta_info *sta;
1892
Michael Wube8755e2007-07-27 15:43:23 +02001893 read_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07001894 list_for_each_entry(sta, &local->sta_list, list) {
1895 if (sta->dev == dev &&
1896 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
1897 jiffies)) {
1898 active++;
1899 break;
1900 }
1901 }
Michael Wube8755e2007-07-27 15:43:23 +02001902 read_unlock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07001903
1904 return active;
1905}
1906
1907
1908static void ieee80211_sta_expire(struct net_device *dev)
1909{
1910 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1911 struct sta_info *sta, *tmp;
Michael Wube8755e2007-07-27 15:43:23 +02001912 LIST_HEAD(tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001913
Michael Wube8755e2007-07-27 15:43:23 +02001914 write_lock_bh(&local->sta_lock);
Jiri Bencf0706e82007-05-05 11:45:53 -07001915 list_for_each_entry_safe(sta, tmp, &local->sta_list, list)
1916 if (time_after(jiffies, sta->last_rx +
1917 IEEE80211_IBSS_INACTIVITY_LIMIT)) {
1918 printk(KERN_DEBUG "%s: expiring inactive STA " MAC_FMT
1919 "\n", dev->name, MAC_ARG(sta->addr));
Michael Wube8755e2007-07-27 15:43:23 +02001920 __sta_info_get(sta);
1921 sta_info_remove(sta);
1922 list_add(&sta->list, &tmp_list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001923 }
Michael Wube8755e2007-07-27 15:43:23 +02001924 write_unlock_bh(&local->sta_lock);
1925
1926 list_for_each_entry_safe(sta, tmp, &tmp_list, list) {
1927 sta_info_free(sta);
1928 sta_info_put(sta);
1929 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001930}
1931
1932
1933static void ieee80211_sta_merge_ibss(struct net_device *dev,
1934 struct ieee80211_if_sta *ifsta)
1935{
1936 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
1937
1938 ieee80211_sta_expire(dev);
1939 if (ieee80211_sta_active_ibss(dev))
1940 return;
1941
1942 printk(KERN_DEBUG "%s: No active IBSS STAs - trying to scan for other "
1943 "IBSS networks with same SSID (merge)\n", dev->name);
1944 ieee80211_sta_req_scan(dev, ifsta->ssid, ifsta->ssid_len);
1945}
1946
1947
1948void ieee80211_sta_timer(unsigned long data)
1949{
1950 struct ieee80211_sub_if_data *sdata =
1951 (struct ieee80211_sub_if_data *) data;
1952 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
1953 struct ieee80211_local *local = wdev_priv(&sdata->wdev);
1954
1955 set_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1956 queue_work(local->hw.workqueue, &ifsta->work);
1957}
1958
1959
1960void ieee80211_sta_work(struct work_struct *work)
1961{
1962 struct ieee80211_sub_if_data *sdata =
1963 container_of(work, struct ieee80211_sub_if_data, u.sta.work);
1964 struct net_device *dev = sdata->dev;
1965 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1966 struct ieee80211_if_sta *ifsta;
1967 struct sk_buff *skb;
1968
1969 if (!netif_running(dev))
1970 return;
1971
1972 if (local->sta_scanning)
1973 return;
1974
1975 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1976 sdata->type != IEEE80211_IF_TYPE_IBSS) {
1977 printk(KERN_DEBUG "%s: ieee80211_sta_work: non-STA interface "
1978 "(type=%d)\n", dev->name, sdata->type);
1979 return;
1980 }
1981 ifsta = &sdata->u.sta;
1982
1983 while ((skb = skb_dequeue(&ifsta->skb_queue)))
1984 ieee80211_sta_rx_queued_mgmt(dev, skb);
1985
1986 if (ifsta->state != IEEE80211_AUTHENTICATE &&
1987 ifsta->state != IEEE80211_ASSOCIATE &&
1988 test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request)) {
1989 ieee80211_sta_start_scan(dev, NULL, 0);
1990 return;
1991 }
1992
1993 if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request)) {
1994 if (ieee80211_sta_config_auth(dev, ifsta))
1995 return;
1996 clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request);
1997 } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifsta->request))
1998 return;
1999
2000 switch (ifsta->state) {
2001 case IEEE80211_DISABLED:
2002 break;
2003 case IEEE80211_AUTHENTICATE:
2004 ieee80211_authenticate(dev, ifsta);
2005 break;
2006 case IEEE80211_ASSOCIATE:
2007 ieee80211_associate(dev, ifsta);
2008 break;
2009 case IEEE80211_ASSOCIATED:
2010 ieee80211_associated(dev, ifsta);
2011 break;
2012 case IEEE80211_IBSS_SEARCH:
2013 ieee80211_sta_find_ibss(dev, ifsta);
2014 break;
2015 case IEEE80211_IBSS_JOINED:
2016 ieee80211_sta_merge_ibss(dev, ifsta);
2017 break;
2018 default:
2019 printk(KERN_DEBUG "ieee80211_sta_work: Unknown state %d\n",
2020 ifsta->state);
2021 break;
2022 }
2023
2024 if (ieee80211_privacy_mismatch(dev, ifsta)) {
2025 printk(KERN_DEBUG "%s: privacy configuration mismatch and "
2026 "mixed-cell disabled - disassociate\n", dev->name);
2027
2028 ieee80211_send_disassoc(dev, ifsta, WLAN_REASON_UNSPECIFIED);
2029 ieee80211_set_disassoc(dev, ifsta, 0);
2030 }
2031}
2032
2033
2034static void ieee80211_sta_reset_auth(struct net_device *dev,
2035 struct ieee80211_if_sta *ifsta)
2036{
2037 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2038
2039 if (local->ops->reset_tsf) {
2040 /* Reset own TSF to allow time synchronization work. */
2041 local->ops->reset_tsf(local_to_hw(local));
2042 }
2043
2044 ifsta->wmm_last_param_set = -1; /* allow any WMM update */
2045
2046
2047 if (ifsta->auth_algs & IEEE80211_AUTH_ALG_OPEN)
2048 ifsta->auth_alg = WLAN_AUTH_OPEN;
2049 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
2050 ifsta->auth_alg = WLAN_AUTH_SHARED_KEY;
2051 else if (ifsta->auth_algs & IEEE80211_AUTH_ALG_LEAP)
2052 ifsta->auth_alg = WLAN_AUTH_LEAP;
2053 else
2054 ifsta->auth_alg = WLAN_AUTH_OPEN;
2055 printk(KERN_DEBUG "%s: Initial auth_alg=%d\n", dev->name,
2056 ifsta->auth_alg);
2057 ifsta->auth_transaction = -1;
2058 ifsta->associated = ifsta->auth_tries = ifsta->assoc_tries = 0;
2059 netif_carrier_off(dev);
2060}
2061
2062
2063void ieee80211_sta_req_auth(struct net_device *dev,
2064 struct ieee80211_if_sta *ifsta)
2065{
2066 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2067 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2068
2069 if (sdata->type != IEEE80211_IF_TYPE_STA)
2070 return;
2071
2072 if ((ifsta->bssid_set || ifsta->auto_bssid_sel) &&
2073 (ifsta->ssid_set || ifsta->auto_ssid_sel)) {
2074 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2075 queue_work(local->hw.workqueue, &ifsta->work);
2076 }
2077}
2078
2079static int ieee80211_sta_match_ssid(struct ieee80211_if_sta *ifsta,
2080 const char *ssid, int ssid_len)
2081{
2082 int tmp, hidden_ssid;
2083
2084 if (!memcmp(ifsta->ssid, ssid, ssid_len))
2085 return 1;
2086
2087 if (ifsta->auto_bssid_sel)
2088 return 0;
2089
2090 hidden_ssid = 1;
2091 tmp = ssid_len;
2092 while (tmp--) {
2093 if (ssid[tmp] != '\0') {
2094 hidden_ssid = 0;
2095 break;
2096 }
2097 }
2098
2099 if (hidden_ssid && ifsta->ssid_len == ssid_len)
2100 return 1;
2101
2102 if (ssid_len == 1 && ssid[0] == ' ')
2103 return 1;
2104
2105 return 0;
2106}
2107
2108static int ieee80211_sta_config_auth(struct net_device *dev,
2109 struct ieee80211_if_sta *ifsta)
2110{
2111 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2112 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2113 struct ieee80211_sta_bss *bss, *selected = NULL;
2114 int top_rssi = 0, freq;
2115
Jiri Bencf0706e82007-05-05 11:45:53 -07002116 if (!ifsta->auto_channel_sel && !ifsta->auto_bssid_sel &&
2117 !ifsta->auto_ssid_sel) {
2118 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002119 ieee80211_sta_reset_auth(dev, ifsta);
2120 return 0;
2121 }
2122
2123 spin_lock_bh(&local->sta_bss_lock);
2124 freq = local->oper_channel->freq;
2125 list_for_each_entry(bss, &local->sta_bss_list, list) {
2126 if (!(bss->capability & WLAN_CAPABILITY_ESS))
2127 continue;
2128
2129 if (!!(bss->capability & WLAN_CAPABILITY_PRIVACY) ^
2130 !!sdata->default_key)
2131 continue;
2132
2133 if (!ifsta->auto_channel_sel && bss->freq != freq)
2134 continue;
2135
2136 if (!ifsta->auto_bssid_sel &&
2137 memcmp(bss->bssid, ifsta->bssid, ETH_ALEN))
2138 continue;
2139
2140 if (!ifsta->auto_ssid_sel &&
2141 !ieee80211_sta_match_ssid(ifsta, bss->ssid, bss->ssid_len))
2142 continue;
2143
2144 if (!selected || top_rssi < bss->rssi) {
2145 selected = bss;
2146 top_rssi = bss->rssi;
2147 }
2148 }
2149 if (selected)
2150 atomic_inc(&selected->users);
2151 spin_unlock_bh(&local->sta_bss_lock);
2152
2153 if (selected) {
2154 ieee80211_set_channel(local, -1, selected->freq);
2155 if (!ifsta->ssid_set)
2156 ieee80211_sta_set_ssid(dev, selected->ssid,
2157 selected->ssid_len);
2158 ieee80211_sta_set_bssid(dev, selected->bssid);
2159 ieee80211_rx_bss_put(dev, selected);
2160 ifsta->state = IEEE80211_AUTHENTICATE;
Jiri Bencf0706e82007-05-05 11:45:53 -07002161 ieee80211_sta_reset_auth(dev, ifsta);
2162 return 0;
2163 } else {
2164 if (ifsta->state != IEEE80211_AUTHENTICATE) {
John W. Linvilleb9bf1e62007-08-07 16:33:15 -04002165 if (ifsta->auto_ssid_sel)
2166 ieee80211_sta_start_scan(dev, NULL, 0);
2167 else
2168 ieee80211_sta_start_scan(dev, ifsta->ssid,
2169 ifsta->ssid_len);
Jiri Bencf0706e82007-05-05 11:45:53 -07002170 ifsta->state = IEEE80211_AUTHENTICATE;
2171 set_bit(IEEE80211_STA_REQ_AUTH, &ifsta->request);
2172 } else
2173 ifsta->state = IEEE80211_DISABLED;
2174 }
Jiri Bencf0706e82007-05-05 11:45:53 -07002175 return -1;
2176}
2177
2178static int ieee80211_sta_join_ibss(struct net_device *dev,
2179 struct ieee80211_if_sta *ifsta,
2180 struct ieee80211_sta_bss *bss)
2181{
2182 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2183 int res, rates, i, j;
2184 struct sk_buff *skb;
2185 struct ieee80211_mgmt *mgmt;
2186 struct ieee80211_tx_control control;
2187 struct ieee80211_rate *rate;
2188 struct ieee80211_hw_mode *mode;
2189 struct rate_control_extra extra;
2190 u8 *pos;
2191 struct ieee80211_sub_if_data *sdata;
2192
2193 /* Remove possible STA entries from other IBSS networks. */
2194 sta_info_flush(local, NULL);
2195
2196 if (local->ops->reset_tsf) {
2197 /* Reset own TSF to allow time synchronization work. */
2198 local->ops->reset_tsf(local_to_hw(local));
2199 }
2200 memcpy(ifsta->bssid, bss->bssid, ETH_ALEN);
2201 res = ieee80211_if_config(dev);
2202 if (res)
2203 return res;
2204
2205 local->hw.conf.beacon_int = bss->beacon_int >= 10 ? bss->beacon_int : 10;
2206
2207 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2208 sdata->drop_unencrypted = bss->capability &
2209 WLAN_CAPABILITY_PRIVACY ? 1 : 0;
2210
2211 res = ieee80211_set_channel(local, -1, bss->freq);
2212
2213 if (!(local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)) {
2214 printk(KERN_DEBUG "%s: IBSS not allowed on channel %d "
2215 "(%d MHz)\n", dev->name, local->hw.conf.channel,
2216 local->hw.conf.freq);
2217 return -1;
2218 }
2219
2220 /* Set beacon template based on scan results */
2221 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 400);
2222 do {
2223 if (!skb)
2224 break;
2225
2226 skb_reserve(skb, local->hw.extra_tx_headroom);
2227
2228 mgmt = (struct ieee80211_mgmt *)
2229 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2230 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2231 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2232 IEEE80211_STYPE_BEACON);
2233 memset(mgmt->da, 0xff, ETH_ALEN);
2234 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
2235 memcpy(mgmt->bssid, ifsta->bssid, ETH_ALEN);
2236 mgmt->u.beacon.beacon_int =
2237 cpu_to_le16(local->hw.conf.beacon_int);
2238 mgmt->u.beacon.capab_info = cpu_to_le16(bss->capability);
2239
2240 pos = skb_put(skb, 2 + ifsta->ssid_len);
2241 *pos++ = WLAN_EID_SSID;
2242 *pos++ = ifsta->ssid_len;
2243 memcpy(pos, ifsta->ssid, ifsta->ssid_len);
2244
2245 rates = bss->supp_rates_len;
2246 if (rates > 8)
2247 rates = 8;
2248 pos = skb_put(skb, 2 + rates);
2249 *pos++ = WLAN_EID_SUPP_RATES;
2250 *pos++ = rates;
2251 memcpy(pos, bss->supp_rates, rates);
2252
2253 pos = skb_put(skb, 2 + 1);
2254 *pos++ = WLAN_EID_DS_PARAMS;
2255 *pos++ = 1;
2256 *pos++ = bss->channel;
2257
2258 pos = skb_put(skb, 2 + 2);
2259 *pos++ = WLAN_EID_IBSS_PARAMS;
2260 *pos++ = 2;
2261 /* FIX: set ATIM window based on scan results */
2262 *pos++ = 0;
2263 *pos++ = 0;
2264
2265 if (bss->supp_rates_len > 8) {
2266 rates = bss->supp_rates_len - 8;
2267 pos = skb_put(skb, 2 + rates);
2268 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2269 *pos++ = rates;
2270 memcpy(pos, &bss->supp_rates[8], rates);
2271 }
2272
2273 memset(&control, 0, sizeof(control));
2274 memset(&extra, 0, sizeof(extra));
2275 extra.mode = local->oper_hw_mode;
2276 rate = rate_control_get_rate(local, dev, skb, &extra);
2277 if (!rate) {
2278 printk(KERN_DEBUG "%s: Failed to determine TX rate "
2279 "for IBSS beacon\n", dev->name);
2280 break;
2281 }
2282 control.tx_rate = (local->short_preamble &&
2283 (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
2284 rate->val2 : rate->val;
2285 control.antenna_sel_tx = local->hw.conf.antenna_sel_tx;
2286 control.power_level = local->hw.conf.power_level;
2287 control.flags |= IEEE80211_TXCTL_NO_ACK;
2288 control.retry_limit = 1;
2289
2290 ifsta->probe_resp = skb_copy(skb, GFP_ATOMIC);
2291 if (ifsta->probe_resp) {
2292 mgmt = (struct ieee80211_mgmt *)
2293 ifsta->probe_resp->data;
2294 mgmt->frame_control =
2295 IEEE80211_FC(IEEE80211_FTYPE_MGMT,
2296 IEEE80211_STYPE_PROBE_RESP);
2297 } else {
2298 printk(KERN_DEBUG "%s: Could not allocate ProbeResp "
2299 "template for IBSS\n", dev->name);
2300 }
2301
2302 if (local->ops->beacon_update &&
2303 local->ops->beacon_update(local_to_hw(local),
2304 skb, &control) == 0) {
2305 printk(KERN_DEBUG "%s: Configured IBSS beacon "
2306 "template based on scan results\n", dev->name);
2307 skb = NULL;
2308 }
2309
2310 rates = 0;
2311 mode = local->oper_hw_mode;
2312 for (i = 0; i < bss->supp_rates_len; i++) {
2313 int bitrate = (bss->supp_rates[i] & 0x7f) * 5;
2314 if (mode->mode == MODE_ATHEROS_TURBO)
2315 bitrate *= 2;
2316 for (j = 0; j < mode->num_rates; j++)
2317 if (mode->rates[j].rate == bitrate)
2318 rates |= BIT(j);
2319 }
2320 ifsta->supp_rates_bits = rates;
2321 } while (0);
2322
2323 if (skb) {
2324 printk(KERN_DEBUG "%s: Failed to configure IBSS beacon "
2325 "template\n", dev->name);
2326 dev_kfree_skb(skb);
2327 }
2328
2329 ifsta->state = IEEE80211_IBSS_JOINED;
2330 mod_timer(&ifsta->timer, jiffies + IEEE80211_IBSS_MERGE_INTERVAL);
2331
2332 ieee80211_rx_bss_put(dev, bss);
2333
2334 return res;
2335}
2336
2337
2338static int ieee80211_sta_create_ibss(struct net_device *dev,
2339 struct ieee80211_if_sta *ifsta)
2340{
2341 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2342 struct ieee80211_sta_bss *bss;
2343 struct ieee80211_sub_if_data *sdata;
2344 struct ieee80211_hw_mode *mode;
2345 u8 bssid[ETH_ALEN], *pos;
2346 int i;
2347
2348#if 0
2349 /* Easier testing, use fixed BSSID. */
2350 memset(bssid, 0xfe, ETH_ALEN);
2351#else
2352 /* Generate random, not broadcast, locally administered BSSID. Mix in
2353 * own MAC address to make sure that devices that do not have proper
2354 * random number generator get different BSSID. */
2355 get_random_bytes(bssid, ETH_ALEN);
2356 for (i = 0; i < ETH_ALEN; i++)
2357 bssid[i] ^= dev->dev_addr[i];
2358 bssid[0] &= ~0x01;
2359 bssid[0] |= 0x02;
2360#endif
2361
2362 printk(KERN_DEBUG "%s: Creating new IBSS network, BSSID " MAC_FMT "\n",
2363 dev->name, MAC_ARG(bssid));
2364
2365 bss = ieee80211_rx_bss_add(dev, bssid);
2366 if (!bss)
2367 return -ENOMEM;
2368
2369 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2370 mode = local->oper_hw_mode;
2371
2372 if (local->hw.conf.beacon_int == 0)
2373 local->hw.conf.beacon_int = 100;
2374 bss->beacon_int = local->hw.conf.beacon_int;
2375 bss->hw_mode = local->hw.conf.phymode;
2376 bss->channel = local->hw.conf.channel;
2377 bss->freq = local->hw.conf.freq;
2378 bss->last_update = jiffies;
2379 bss->capability = WLAN_CAPABILITY_IBSS;
2380 if (sdata->default_key) {
2381 bss->capability |= WLAN_CAPABILITY_PRIVACY;
2382 } else
2383 sdata->drop_unencrypted = 0;
2384 bss->supp_rates_len = mode->num_rates;
2385 pos = bss->supp_rates;
2386 for (i = 0; i < mode->num_rates; i++) {
2387 int rate = mode->rates[i].rate;
2388 if (mode->mode == MODE_ATHEROS_TURBO)
2389 rate /= 2;
2390 *pos++ = (u8) (rate / 5);
2391 }
2392
2393 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2394}
2395
2396
2397static int ieee80211_sta_find_ibss(struct net_device *dev,
2398 struct ieee80211_if_sta *ifsta)
2399{
2400 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2401 struct ieee80211_sta_bss *bss;
2402 int found = 0;
2403 u8 bssid[ETH_ALEN];
2404 int active_ibss;
2405
2406 if (ifsta->ssid_len == 0)
2407 return -EINVAL;
2408
2409 active_ibss = ieee80211_sta_active_ibss(dev);
2410#ifdef CONFIG_MAC80211_IBSS_DEBUG
2411 printk(KERN_DEBUG "%s: sta_find_ibss (active_ibss=%d)\n",
2412 dev->name, active_ibss);
2413#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2414 spin_lock_bh(&local->sta_bss_lock);
2415 list_for_each_entry(bss, &local->sta_bss_list, list) {
2416 if (ifsta->ssid_len != bss->ssid_len ||
2417 memcmp(ifsta->ssid, bss->ssid, bss->ssid_len) != 0
2418 || !(bss->capability & WLAN_CAPABILITY_IBSS))
2419 continue;
2420#ifdef CONFIG_MAC80211_IBSS_DEBUG
2421 printk(KERN_DEBUG " bssid=" MAC_FMT " found\n",
2422 MAC_ARG(bss->bssid));
2423#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2424 memcpy(bssid, bss->bssid, ETH_ALEN);
2425 found = 1;
2426 if (active_ibss || memcmp(bssid, ifsta->bssid, ETH_ALEN) != 0)
2427 break;
2428 }
2429 spin_unlock_bh(&local->sta_bss_lock);
2430
2431#ifdef CONFIG_MAC80211_IBSS_DEBUG
2432 printk(KERN_DEBUG " sta_find_ibss: selected " MAC_FMT " current "
2433 MAC_FMT "\n", MAC_ARG(bssid), MAC_ARG(ifsta->bssid));
2434#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2435 if (found && memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0 &&
2436 (bss = ieee80211_rx_bss_get(dev, bssid))) {
2437 printk(KERN_DEBUG "%s: Selected IBSS BSSID " MAC_FMT
2438 " based on configured SSID\n",
2439 dev->name, MAC_ARG(bssid));
2440 return ieee80211_sta_join_ibss(dev, ifsta, bss);
2441 }
2442#ifdef CONFIG_MAC80211_IBSS_DEBUG
2443 printk(KERN_DEBUG " did not try to join ibss\n");
2444#endif /* CONFIG_MAC80211_IBSS_DEBUG */
2445
2446 /* Selected IBSS not found in current scan results - try to scan */
2447 if (ifsta->state == IEEE80211_IBSS_JOINED &&
2448 !ieee80211_sta_active_ibss(dev)) {
2449 mod_timer(&ifsta->timer, jiffies +
2450 IEEE80211_IBSS_MERGE_INTERVAL);
2451 } else if (time_after(jiffies, local->last_scan_completed +
2452 IEEE80211_SCAN_INTERVAL)) {
2453 printk(KERN_DEBUG "%s: Trigger new scan to find an IBSS to "
2454 "join\n", dev->name);
2455 return ieee80211_sta_req_scan(dev, ifsta->ssid,
2456 ifsta->ssid_len);
2457 } else if (ifsta->state != IEEE80211_IBSS_JOINED) {
2458 int interval = IEEE80211_SCAN_INTERVAL;
2459
2460 if (time_after(jiffies, ifsta->ibss_join_req +
2461 IEEE80211_IBSS_JOIN_TIMEOUT)) {
2462 if (ifsta->create_ibss &&
2463 local->oper_channel->flag & IEEE80211_CHAN_W_IBSS)
2464 return ieee80211_sta_create_ibss(dev, ifsta);
2465 if (ifsta->create_ibss) {
2466 printk(KERN_DEBUG "%s: IBSS not allowed on the"
2467 " configured channel %d (%d MHz)\n",
2468 dev->name, local->hw.conf.channel,
2469 local->hw.conf.freq);
2470 }
2471
2472 /* No IBSS found - decrease scan interval and continue
2473 * scanning. */
2474 interval = IEEE80211_SCAN_INTERVAL_SLOW;
2475 }
2476
2477 ifsta->state = IEEE80211_IBSS_SEARCH;
2478 mod_timer(&ifsta->timer, jiffies + interval);
2479 return 0;
2480 }
2481
2482 return 0;
2483}
2484
2485
2486int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len)
2487{
2488 struct ieee80211_sub_if_data *sdata;
2489 struct ieee80211_if_sta *ifsta;
2490 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2491
2492 if (len > IEEE80211_MAX_SSID_LEN)
2493 return -EINVAL;
2494
2495 /* TODO: This should always be done for IBSS, even if IEEE80211_QOS is
2496 * not defined. */
2497 if (local->ops->conf_tx) {
2498 struct ieee80211_tx_queue_params qparam;
2499 int i;
2500
2501 memset(&qparam, 0, sizeof(qparam));
2502 /* TODO: are these ok defaults for all hw_modes? */
2503 qparam.aifs = 2;
2504 qparam.cw_min =
2505 local->hw.conf.phymode == MODE_IEEE80211B ? 31 : 15;
2506 qparam.cw_max = 1023;
2507 qparam.burst_time = 0;
2508 for (i = IEEE80211_TX_QUEUE_DATA0; i < NUM_TX_DATA_QUEUES; i++)
2509 {
2510 local->ops->conf_tx(local_to_hw(local),
2511 i + IEEE80211_TX_QUEUE_DATA0,
2512 &qparam);
2513 }
2514 /* IBSS uses different parameters for Beacon sending */
2515 qparam.cw_min++;
2516 qparam.cw_min *= 2;
2517 qparam.cw_min--;
2518 local->ops->conf_tx(local_to_hw(local),
2519 IEEE80211_TX_QUEUE_BEACON, &qparam);
2520 }
2521
2522 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2523 ifsta = &sdata->u.sta;
2524
2525 if (ifsta->ssid_len != len || memcmp(ifsta->ssid, ssid, len) != 0)
2526 ifsta->prev_bssid_set = 0;
2527 memcpy(ifsta->ssid, ssid, len);
2528 memset(ifsta->ssid + len, 0, IEEE80211_MAX_SSID_LEN - len);
2529 ifsta->ssid_len = len;
2530
2531 ifsta->ssid_set = len ? 1 : 0;
2532 if (sdata->type == IEEE80211_IF_TYPE_IBSS && !ifsta->bssid_set) {
2533 ifsta->ibss_join_req = jiffies;
2534 ifsta->state = IEEE80211_IBSS_SEARCH;
2535 return ieee80211_sta_find_ibss(dev, ifsta);
2536 }
2537 return 0;
2538}
2539
2540
2541int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len)
2542{
2543 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2544 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2545 memcpy(ssid, ifsta->ssid, ifsta->ssid_len);
2546 *len = ifsta->ssid_len;
2547 return 0;
2548}
2549
2550
2551int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid)
2552{
2553 struct ieee80211_sub_if_data *sdata;
2554 struct ieee80211_if_sta *ifsta;
2555 int res;
2556
2557 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2558 ifsta = &sdata->u.sta;
2559
2560 if (memcmp(ifsta->bssid, bssid, ETH_ALEN) != 0) {
2561 memcpy(ifsta->bssid, bssid, ETH_ALEN);
2562 res = ieee80211_if_config(dev);
2563 if (res) {
2564 printk(KERN_DEBUG "%s: Failed to config new BSSID to "
2565 "the low-level driver\n", dev->name);
2566 return res;
2567 }
2568 }
2569
2570 if (!is_valid_ether_addr(bssid))
2571 ifsta->bssid_set = 0;
2572 else
2573 ifsta->bssid_set = 1;
2574 return 0;
2575}
2576
2577
2578static void ieee80211_send_nullfunc(struct ieee80211_local *local,
2579 struct ieee80211_sub_if_data *sdata,
2580 int powersave)
2581{
2582 struct sk_buff *skb;
2583 struct ieee80211_hdr *nullfunc;
2584 u16 fc;
2585
2586 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
2587 if (!skb) {
2588 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2589 "frame\n", sdata->dev->name);
2590 return;
2591 }
2592 skb_reserve(skb, local->hw.extra_tx_headroom);
2593
2594 nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
2595 memset(nullfunc, 0, 24);
2596 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
2597 IEEE80211_FCTL_TODS;
2598 if (powersave)
2599 fc |= IEEE80211_FCTL_PM;
2600 nullfunc->frame_control = cpu_to_le16(fc);
2601 memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
2602 memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
2603 memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
2604
2605 ieee80211_sta_tx(sdata->dev, skb, 0);
2606}
2607
2608
2609void ieee80211_scan_completed(struct ieee80211_hw *hw)
2610{
2611 struct ieee80211_local *local = hw_to_local(hw);
2612 struct net_device *dev = local->scan_dev;
2613 struct ieee80211_sub_if_data *sdata;
2614 union iwreq_data wrqu;
2615
2616 local->last_scan_completed = jiffies;
2617 wmb();
2618 local->sta_scanning = 0;
2619
2620 if (ieee80211_hw_config(local))
2621 printk(KERN_DEBUG "%s: failed to restore operational"
2622 "channel after scan\n", dev->name);
2623
2624 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2625 ieee80211_if_config(dev))
2626 printk(KERN_DEBUG "%s: failed to restore operational"
2627 "BSSID after scan\n", dev->name);
2628
2629 memset(&wrqu, 0, sizeof(wrqu));
2630 wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
2631
2632 read_lock(&local->sub_if_lock);
2633 list_for_each_entry(sdata, &local->sub_if_list, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02002634
2635 /* No need to wake the master device. */
2636 if (sdata->dev == local->mdev)
2637 continue;
2638
Jiri Bencf0706e82007-05-05 11:45:53 -07002639 if (sdata->type == IEEE80211_IF_TYPE_STA) {
2640 if (sdata->u.sta.associated)
2641 ieee80211_send_nullfunc(local, sdata, 0);
2642 ieee80211_sta_timer((unsigned long)sdata);
2643 }
Mattias Nissler14042cbe2007-06-08 15:31:13 +02002644
Jiri Bencf0706e82007-05-05 11:45:53 -07002645 netif_wake_queue(sdata->dev);
2646 }
2647 read_unlock(&local->sub_if_lock);
2648
2649 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2650 if (sdata->type == IEEE80211_IF_TYPE_IBSS) {
2651 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2652 if (!ifsta->bssid_set ||
2653 (!ifsta->state == IEEE80211_IBSS_JOINED &&
2654 !ieee80211_sta_active_ibss(dev)))
2655 ieee80211_sta_find_ibss(dev, ifsta);
2656 }
2657}
2658EXPORT_SYMBOL(ieee80211_scan_completed);
2659
2660void ieee80211_sta_scan_work(struct work_struct *work)
2661{
2662 struct ieee80211_local *local =
2663 container_of(work, struct ieee80211_local, scan_work.work);
2664 struct net_device *dev = local->scan_dev;
2665 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2666 struct ieee80211_hw_mode *mode;
2667 struct ieee80211_channel *chan;
2668 int skip;
2669 unsigned long next_delay = 0;
2670
2671 if (!local->sta_scanning)
2672 return;
2673
2674 switch (local->scan_state) {
2675 case SCAN_SET_CHANNEL:
2676 mode = local->scan_hw_mode;
2677 if (local->scan_hw_mode->list.next == &local->modes_list &&
2678 local->scan_channel_idx >= mode->num_channels) {
2679 ieee80211_scan_completed(local_to_hw(local));
2680 return;
2681 }
2682 skip = !(local->enabled_modes & (1 << mode->mode));
2683 chan = &mode->channels[local->scan_channel_idx];
2684 if (!(chan->flag & IEEE80211_CHAN_W_SCAN) ||
2685 (sdata->type == IEEE80211_IF_TYPE_IBSS &&
2686 !(chan->flag & IEEE80211_CHAN_W_IBSS)) ||
2687 (local->hw_modes & local->enabled_modes &
2688 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
2689 skip = 1;
2690
2691 if (!skip) {
2692#if 0
2693 printk(KERN_DEBUG "%s: scan channel %d (%d MHz)\n",
2694 dev->name, chan->chan, chan->freq);
2695#endif
2696
2697 local->scan_channel = chan;
2698 if (ieee80211_hw_config(local)) {
2699 printk(KERN_DEBUG "%s: failed to set channel "
2700 "%d (%d MHz) for scan\n", dev->name,
2701 chan->chan, chan->freq);
2702 skip = 1;
2703 }
2704 }
2705
2706 local->scan_channel_idx++;
2707 if (local->scan_channel_idx >= local->scan_hw_mode->num_channels) {
2708 if (local->scan_hw_mode->list.next != &local->modes_list) {
2709 local->scan_hw_mode = list_entry(local->scan_hw_mode->list.next,
2710 struct ieee80211_hw_mode,
2711 list);
2712 local->scan_channel_idx = 0;
2713 }
2714 }
2715
2716 if (skip)
2717 break;
2718
2719 next_delay = IEEE80211_PROBE_DELAY +
2720 usecs_to_jiffies(local->hw.channel_change_time);
2721 local->scan_state = SCAN_SEND_PROBE;
2722 break;
2723 case SCAN_SEND_PROBE:
2724 if (local->scan_channel->flag & IEEE80211_CHAN_W_ACTIVE_SCAN) {
2725 ieee80211_send_probe_req(dev, NULL, local->scan_ssid,
2726 local->scan_ssid_len);
2727 next_delay = IEEE80211_CHANNEL_TIME;
2728 } else
2729 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
2730 local->scan_state = SCAN_SET_CHANNEL;
2731 break;
2732 }
2733
2734 if (local->sta_scanning)
2735 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2736 next_delay);
2737}
2738
2739
2740static int ieee80211_sta_start_scan(struct net_device *dev,
2741 u8 *ssid, size_t ssid_len)
2742{
2743 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2744 struct ieee80211_sub_if_data *sdata;
2745
2746 if (ssid_len > IEEE80211_MAX_SSID_LEN)
2747 return -EINVAL;
2748
2749 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
2750 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
2751 * BSSID: MACAddress
2752 * SSID
2753 * ScanType: ACTIVE, PASSIVE
2754 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
2755 * a Probe frame during active scanning
2756 * ChannelList
2757 * MinChannelTime (>= ProbeDelay), in TU
2758 * MaxChannelTime: (>= MinChannelTime), in TU
2759 */
2760
2761 /* MLME-SCAN.confirm
2762 * BSSDescriptionSet
2763 * ResultCode: SUCCESS, INVALID_PARAMETERS
2764 */
2765
2766 if (local->sta_scanning) {
2767 if (local->scan_dev == dev)
2768 return 0;
2769 return -EBUSY;
2770 }
2771
2772 if (local->ops->hw_scan) {
2773 int rc = local->ops->hw_scan(local_to_hw(local),
2774 ssid, ssid_len);
2775 if (!rc) {
2776 local->sta_scanning = 1;
2777 local->scan_dev = dev;
2778 }
2779 return rc;
2780 }
2781
2782 local->sta_scanning = 1;
2783
2784 read_lock(&local->sub_if_lock);
2785 list_for_each_entry(sdata, &local->sub_if_list, list) {
Mattias Nissler14042cbe2007-06-08 15:31:13 +02002786
2787 /* Don't stop the master interface, otherwise we can't transmit
2788 * probes! */
2789 if (sdata->dev == local->mdev)
2790 continue;
2791
Jiri Bencf0706e82007-05-05 11:45:53 -07002792 netif_stop_queue(sdata->dev);
2793 if (sdata->type == IEEE80211_IF_TYPE_STA &&
2794 sdata->u.sta.associated)
2795 ieee80211_send_nullfunc(local, sdata, 1);
2796 }
2797 read_unlock(&local->sub_if_lock);
2798
2799 if (ssid) {
2800 local->scan_ssid_len = ssid_len;
2801 memcpy(local->scan_ssid, ssid, ssid_len);
2802 } else
2803 local->scan_ssid_len = 0;
2804 local->scan_state = SCAN_SET_CHANNEL;
2805 local->scan_hw_mode = list_entry(local->modes_list.next,
2806 struct ieee80211_hw_mode,
2807 list);
2808 local->scan_channel_idx = 0;
2809 local->scan_dev = dev;
2810
2811 if (!(local->hw.flags & IEEE80211_HW_NO_PROBE_FILTERING) &&
2812 ieee80211_if_config(dev))
2813 printk(KERN_DEBUG "%s: failed to set BSSID for scan\n",
2814 dev->name);
2815
2816 /* TODO: start scan as soon as all nullfunc frames are ACKed */
2817 queue_delayed_work(local->hw.workqueue, &local->scan_work,
2818 IEEE80211_CHANNEL_TIME);
2819
2820 return 0;
2821}
2822
2823
2824int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len)
2825{
2826 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2827 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
2828 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2829
2830 if (sdata->type != IEEE80211_IF_TYPE_STA)
2831 return ieee80211_sta_start_scan(dev, ssid, ssid_len);
2832
2833 if (local->sta_scanning) {
2834 if (local->scan_dev == dev)
2835 return 0;
2836 return -EBUSY;
2837 }
2838
2839 set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
2840 queue_work(local->hw.workqueue, &ifsta->work);
2841 return 0;
2842}
2843
2844static char *
2845ieee80211_sta_scan_result(struct net_device *dev,
2846 struct ieee80211_sta_bss *bss,
2847 char *current_ev, char *end_buf)
2848{
2849 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2850 struct iw_event iwe;
2851
2852 if (time_after(jiffies,
2853 bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
2854 return current_ev;
2855
2856 if (!(local->enabled_modes & (1 << bss->hw_mode)))
2857 return current_ev;
2858
2859 if (local->scan_flags & IEEE80211_SCAN_WPA_ONLY &&
2860 !bss->wpa_ie && !bss->rsn_ie)
2861 return current_ev;
2862
2863 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID &&
2864 (local->scan_ssid_len != bss->ssid_len ||
2865 memcmp(local->scan_ssid, bss->ssid, bss->ssid_len) != 0))
2866 return current_ev;
2867
2868 memset(&iwe, 0, sizeof(iwe));
2869 iwe.cmd = SIOCGIWAP;
2870 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2871 memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
2872 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2873 IW_EV_ADDR_LEN);
2874
2875 memset(&iwe, 0, sizeof(iwe));
2876 iwe.cmd = SIOCGIWESSID;
2877 iwe.u.data.length = bss->ssid_len;
2878 iwe.u.data.flags = 1;
2879 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2880 bss->ssid);
2881
2882 if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
2883 memset(&iwe, 0, sizeof(iwe));
2884 iwe.cmd = SIOCGIWMODE;
2885 if (bss->capability & WLAN_CAPABILITY_ESS)
2886 iwe.u.mode = IW_MODE_MASTER;
2887 else
2888 iwe.u.mode = IW_MODE_ADHOC;
2889 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2890 IW_EV_UINT_LEN);
2891 }
2892
2893 memset(&iwe, 0, sizeof(iwe));
2894 iwe.cmd = SIOCGIWFREQ;
2895 iwe.u.freq.m = bss->channel;
2896 iwe.u.freq.e = 0;
2897 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2898 IW_EV_FREQ_LEN);
2899 iwe.u.freq.m = bss->freq * 100000;
2900 iwe.u.freq.e = 1;
2901 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2902 IW_EV_FREQ_LEN);
2903
2904 memset(&iwe, 0, sizeof(iwe));
2905 iwe.cmd = IWEVQUAL;
2906 iwe.u.qual.qual = bss->signal;
2907 iwe.u.qual.level = bss->rssi;
2908 iwe.u.qual.noise = bss->noise;
2909 iwe.u.qual.updated = local->wstats_flags;
2910 current_ev = iwe_stream_add_event(current_ev, end_buf, &iwe,
2911 IW_EV_QUAL_LEN);
2912
2913 memset(&iwe, 0, sizeof(iwe));
2914 iwe.cmd = SIOCGIWENCODE;
2915 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
2916 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2917 else
2918 iwe.u.data.flags = IW_ENCODE_DISABLED;
2919 iwe.u.data.length = 0;
2920 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe, "");
2921
2922 if (bss && bss->wpa_ie) {
2923 memset(&iwe, 0, sizeof(iwe));
2924 iwe.cmd = IWEVGENIE;
2925 iwe.u.data.length = bss->wpa_ie_len;
2926 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2927 bss->wpa_ie);
2928 }
2929
2930 if (bss && bss->rsn_ie) {
2931 memset(&iwe, 0, sizeof(iwe));
2932 iwe.cmd = IWEVGENIE;
2933 iwe.u.data.length = bss->rsn_ie_len;
2934 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2935 bss->rsn_ie);
2936 }
2937
2938 if (bss && bss->supp_rates_len > 0) {
2939 /* display all supported rates in readable format */
2940 char *p = current_ev + IW_EV_LCP_LEN;
2941 int i;
2942
2943 memset(&iwe, 0, sizeof(iwe));
2944 iwe.cmd = SIOCGIWRATE;
2945 /* Those two flags are ignored... */
2946 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
2947
2948 for (i = 0; i < bss->supp_rates_len; i++) {
2949 iwe.u.bitrate.value = ((bss->supp_rates[i] &
2950 0x7f) * 500000);
2951 p = iwe_stream_add_value(current_ev, p,
2952 end_buf, &iwe, IW_EV_PARAM_LEN);
2953 }
2954 current_ev = p;
2955 }
2956
2957 if (bss) {
2958 char *buf;
2959 buf = kmalloc(30, GFP_ATOMIC);
2960 if (buf) {
2961 memset(&iwe, 0, sizeof(iwe));
2962 iwe.cmd = IWEVCUSTOM;
2963 sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
2964 iwe.u.data.length = strlen(buf);
2965 current_ev = iwe_stream_add_point(current_ev, end_buf,
2966 &iwe, buf);
2967 kfree(buf);
2968 }
2969 }
2970
2971 do {
2972 char *buf;
2973
2974 if (!(local->scan_flags & IEEE80211_SCAN_EXTRA_INFO))
2975 break;
2976
2977 buf = kmalloc(100, GFP_ATOMIC);
2978 if (!buf)
2979 break;
2980
2981 memset(&iwe, 0, sizeof(iwe));
2982 iwe.cmd = IWEVCUSTOM;
2983 sprintf(buf, "bcn_int=%d", bss->beacon_int);
2984 iwe.u.data.length = strlen(buf);
2985 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2986 buf);
2987
2988 memset(&iwe, 0, sizeof(iwe));
2989 iwe.cmd = IWEVCUSTOM;
2990 sprintf(buf, "capab=0x%04x", bss->capability);
2991 iwe.u.data.length = strlen(buf);
2992 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
2993 buf);
2994
2995 kfree(buf);
2996 break;
2997 } while (0);
2998
2999 return current_ev;
3000}
3001
3002
3003int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len)
3004{
3005 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3006 char *current_ev = buf;
3007 char *end_buf = buf + len;
3008 struct ieee80211_sta_bss *bss;
3009
3010 spin_lock_bh(&local->sta_bss_lock);
3011 list_for_each_entry(bss, &local->sta_bss_list, list) {
3012 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
3013 spin_unlock_bh(&local->sta_bss_lock);
3014 return -E2BIG;
3015 }
3016 current_ev = ieee80211_sta_scan_result(dev, bss, current_ev,
3017 end_buf);
3018 }
3019 spin_unlock_bh(&local->sta_bss_lock);
3020 return current_ev - buf;
3021}
3022
3023
3024int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len)
3025{
3026 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3027 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3028 kfree(ifsta->extra_ie);
3029 if (len == 0) {
3030 ifsta->extra_ie = NULL;
3031 ifsta->extra_ie_len = 0;
3032 return 0;
3033 }
3034 ifsta->extra_ie = kmalloc(len, GFP_KERNEL);
3035 if (!ifsta->extra_ie) {
3036 ifsta->extra_ie_len = 0;
3037 return -ENOMEM;
3038 }
3039 memcpy(ifsta->extra_ie, ie, len);
3040 ifsta->extra_ie_len = len;
3041 return 0;
3042}
3043
3044
3045struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
3046 struct sk_buff *skb, u8 *bssid,
3047 u8 *addr)
3048{
3049 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
3050 struct sta_info *sta;
John W. Linville91fa5582007-05-15 16:14:40 -04003051 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Jiri Bencf0706e82007-05-05 11:45:53 -07003052
3053 /* TODO: Could consider removing the least recently used entry and
3054 * allow new one to be added. */
3055 if (local->num_sta >= IEEE80211_IBSS_MAX_STA_ENTRIES) {
3056 if (net_ratelimit()) {
3057 printk(KERN_DEBUG "%s: No room for a new IBSS STA "
3058 "entry " MAC_FMT "\n", dev->name, MAC_ARG(addr));
3059 }
3060 return NULL;
3061 }
3062
3063 printk(KERN_DEBUG "%s: Adding new IBSS station " MAC_FMT " (dev=%s)\n",
3064 local->mdev->name, MAC_ARG(addr), dev->name);
3065
3066 sta = sta_info_add(local, dev, addr, GFP_ATOMIC);
3067 if (!sta)
3068 return NULL;
3069
3070 sta->supp_rates = sdata->u.sta.supp_rates_bits;
3071
3072 rate_control_rate_init(sta, local);
3073
3074 return sta; /* caller will call sta_info_put() */
3075}
3076
3077
3078int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason)
3079{
3080 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3081 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3082
3083 printk(KERN_DEBUG "%s: deauthenticate(reason=%d)\n",
3084 dev->name, reason);
3085
3086 if (sdata->type != IEEE80211_IF_TYPE_STA &&
3087 sdata->type != IEEE80211_IF_TYPE_IBSS)
3088 return -EINVAL;
3089
3090 ieee80211_send_deauth(dev, ifsta, reason);
3091 ieee80211_set_disassoc(dev, ifsta, 1);
3092 return 0;
3093}
3094
3095
3096int ieee80211_sta_disassociate(struct net_device *dev, u16 reason)
3097{
3098 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3099 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
3100
3101 printk(KERN_DEBUG "%s: disassociate(reason=%d)\n",
3102 dev->name, reason);
3103
3104 if (sdata->type != IEEE80211_IF_TYPE_STA)
3105 return -EINVAL;
3106
3107 if (!ifsta->associated)
3108 return -1;
3109
3110 ieee80211_send_disassoc(dev, ifsta, reason);
3111 ieee80211_set_disassoc(dev, ifsta, 0);
3112 return 0;
3113}